changeset 33623:57c13c0d1cde

formatter: put topic in templatespec tuple This will allow us to change the initial template reference depending on how the template is looked up. For example, -Tdefault => (ref='changeset', tmpl=None, mapfile='map-cmdline.default') -T'{rev}' => (ref='', tmpl='{rev}', mapfile=None) A literal template given by -T option will be stored as an unnamed template, which will free up the template namespace so that we can load named templates from [templates] section of user config.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 22 Apr 2017 19:07:00 +0900
parents b425ec7fb7f6
children 883adaea9e80
files mercurial/cmdutil.py mercurial/formatter.py
diffstat 2 files changed, 13 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -1580,7 +1580,7 @@
 
     def __init__(self, ui, repo, tmplspec, matchfn, diffopts, buffered):
         changeset_printer.__init__(self, ui, repo, matchfn, diffopts, buffered)
-        self.t = formatter.loadtemplater(ui, 'changeset', tmplspec,
+        self.t = formatter.loadtemplater(ui, tmplspec,
                                          cache=templatekw.defaulttempl)
         self._counter = itertools.count()
         self.cache = {}
@@ -1646,7 +1646,8 @@
                 self.footer = templater.stringify(
                     self.t(self._parts['footer'], **props))
 
-logtemplatespec = formatter.templatespec
+def logtemplatespec(tmpl, mapfile):
+    return formatter.templatespec('changeset', tmpl, mapfile)
 
 def _lookuplogtemplate(ui, tmpl, style):
     """Find the template matching the given template spec or style
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -349,7 +349,7 @@
         self._out = out
         self._topic = topic
         spec = lookuptemplate(ui, topic, opts.get('template', ''))
-        self._t = loadtemplater(ui, topic, spec, cache=templatekw.defaulttempl)
+        self._t = loadtemplater(ui, spec, cache=templatekw.defaulttempl)
         self._counter = itertools.count()
         self._cache = {}  # for templatekw/funcs to store reusable data
     def context(self, **ctxs):
@@ -375,7 +375,7 @@
         self._out.write(templater.stringify(g))
 
 templatespec = collections.namedtuple(r'templatespec',
-                                      r'tmpl mapfile')
+                                      r'ref tmpl mapfile')
 
 def lookuptemplate(ui, topic, tmpl):
     """Find the template matching the given -T/--template spec 'tmpl'
@@ -395,19 +395,19 @@
 
     # looks like a literal template?
     if '{' in tmpl:
-        return templatespec(tmpl, None)
+        return templatespec(topic, 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 templatespec(None, mapname)
+            return templatespec(topic, None, mapname)
 
     # perhaps it's a reference to [templates]
     t = ui.config('templates', tmpl)
     if t:
-        return templatespec(templater.unquotestring(t), None)
+        return templatespec(topic, templater.unquotestring(t), None)
 
     if tmpl == 'list':
         ui.write(_("available styles: %s\n") % templater.stylelist())
@@ -417,21 +417,21 @@
     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 templatespec(None, os.path.realpath(tmpl))
+            return templatespec(topic, None, os.path.realpath(tmpl))
         with util.posixfile(tmpl, 'rb') as f:
             tmpl = f.read()
-        return templatespec(tmpl, None)
+        return templatespec(topic, tmpl, None)
 
     # constant string?
-    return templatespec(tmpl, None)
+    return templatespec(topic, tmpl, None)
 
-def loadtemplater(ui, topic, spec, cache=None):
+def loadtemplater(ui, spec, cache=None):
     """Create a templater from either a literal template or loading from
     a map file"""
     assert not (spec.tmpl and spec.mapfile)
     if spec.mapfile:
         return templater.templater.frommapfile(spec.mapfile, cache=cache)
-    return maketemplater(ui, topic, spec.tmpl, cache=cache)
+    return maketemplater(ui, spec.ref, spec.tmpl, cache=cache)
 
 def maketemplater(ui, topic, tmpl, cache=None):
     """Create a templater from a string template 'tmpl'"""