templating: make -T much more flexible
authorMatt Mackall <mpm@selenic.com>
Sat, 08 Mar 2014 17:38:50 -0600
changeset 20668 3a35ba2681ec
parent 20667 e96e9f805c19
child 20669 870d60294b04
templating: make -T much more flexible It can now accept styles and paths and references to settings in [templates].
hgext/keyword.py
mercurial/cmdutil.py
tests/test-command-template.t
--- a/hgext/keyword.py	Sat Mar 08 16:14:08 2014 -0600
+++ b/hgext/keyword.py	Sat Mar 08 17:38:50 2014 -0600
@@ -218,7 +218,7 @@
         '''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)
--- a/mercurial/cmdutil.py	Sat Mar 08 16:14:08 2014 -0600
+++ b/mercurial/cmdutil.py	Sat Mar 08 17:38:50 2014 -0600
@@ -1059,6 +1059,7 @@
                 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 @@
                 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):
--- a/tests/test-command-template.t	Sat Mar 08 16:14:08 2014 -0600
+++ b/tests/test-command-template.t	Sat Mar 08 17:38:50 2014 -0600
@@ -63,6 +63,29 @@
   $ 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 @@
 
 Compact style works:
 
-  $ hg log --style compact
+  $ hg log -Tcompact
   8[tip]   95c24699272e   2020-01-01 10:01 +0000   test
     third