[PATCH 4 of 6] formatter: put topic in templatespec tuple

Yuya Nishihara yuya at tcha.org
Thu Jun 15 10:52:30 EDT 2017

# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1492855620 -32400
#      Sat Apr 22 19:07:00 2017 +0900
# Node ID 5b3af6e677d49f273caeb3c1f7c6412865f264f9
# Parent  5fc9194c72e9a2882403f77ac66e364e50425ce0
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.

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -1580,7 +1580,7 @@ class changeset_templater(changeset_prin
     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,
         self._counter = itertools.count()
         self.cache = {}
@@ -1646,7 +1646,8 @@ class changeset_templater(changeset_prin
                 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
diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -349,7 +349,7 @@ class templateformatter(baseformatter):
         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 @@ class templateformatter(baseformatter):
 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 @@ def lookuptemplate(ui, topic, tmpl):
     # 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 @@ def lookuptemplate(ui, topic, tmpl):
     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'"""

More information about the Mercurial-devel mailing list