diff --git a/hgext/keyword.py b/hgext/keyword.py --- a/hgext/keyword.py +++ b/hgext/keyword.py @@ -218,7 +218,7 @@ class kwtemplater(object): '''Replaces keywords in data with expanded template.''' def kwsub(mobj): kw = mobj.group(1) - ct = cmdutil.changeset_templater(self.ui, self.repo, False, None + ct = cmdutil.changeset_templater(self.ui, self.repo, False, None, self.templates[kw], '', False) self.ui.pushbuffer() ct.show(ctx, root=self.repo.root, file=path) diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py --- a/mercurial/cmdutil.py +++ b/mercurial/cmdutil.py @@ -1059,6 +1059,7 @@ def gettemplate(ui, tmpl, style): tmpl = templater.parsestring(tmpl) except SyntaxError: tmpl = templater.parsestring(tmpl, quoted=False) + return tmpl, None else: style = util.expandpath(ui.config('ui', 'style', '')) @@ -1071,6 +1072,38 @@ def gettemplate(ui, tmpl, style): mapfile = mapname return None, mapfile + if not tmpl: + return None, None + + # looks like a literal template? + if '{' in tmpl: + return tmpl, None + + # perhaps a stock style? + if not os.path.split(tmpl)[0]: + mapname = (templater.templatepath('map-cmdline.' + tmpl) + or templater.templatepath(tmpl)) + if mapname and os.path.isfile(mapname): + return None, mapname + + # perhaps it's a reference to [templates] + t = ui.config('templates', tmpl) + if t: + try: + tmpl = templater.parsestring(t) + except SyntaxError: + tmpl = templater.parsestring(t, quoted=False) + return tmpl, None + + # perhaps it's a path to a map or a template + if ('/' in tmpl or '\\' in tmpl) and os.path.isfile(tmpl): + # is it a mapfile for a style? + if os.path.basename(tmpl).startswith("map-"): + return None, os.path.realpath(tmpl) + tmpl = open(tmpl).read() + return tmpl, None + + # constant string? return tmpl, None def show_changeset(ui, repo, opts, buffered=False): diff --git a/tests/test-command-template.t b/tests/test-command-template.t --- a/tests/test-command-template.t +++ b/tests/test-command-template.t @@ -63,6 +63,29 @@ Make sure user/global hgrc does not affe $ echo 'logtemplate =' >> .hg/hgrc $ echo 'style =' >> .hg/hgrc +Add some simple styles to settings + + $ echo '[templates]' >> .hg/hgrc + $ printf 'simple = "{rev}\\n"\n' >> .hg/hgrc + $ printf 'simple2 = {rev}\\n\n' >> .hg/hgrc + + $ hg log -l1 -Tsimple + 8 + $ hg log -l1 -Tsimple2 + 8 + +Test templates and style maps in files: + + $ echo "{rev}" > tmpl + $ hg log -l1 -T./tmpl + 8 + $ hg log -l1 -Tblah/blah + blah/blah (no-eol) + + $ printf 'changeset = "{rev}\\n"\n' > map-simple + $ hg log -l1 -T./map-simple + 8 + Default style is like normal output: $ hg log > log.out @@ -84,7 +107,7 @@ Revision with no copies (used to print a Compact style works: - $ hg log --style compact + $ hg log -Tcompact 8[tip] 95c24699272e 2020-01-01 10:01 +0000 test third