Patchwork [V3] templater: support using templates with non-standard names from map file

login
register
mail settings
Submitter Alexander Plavin
Date Sept. 20, 2013, 3:35 p.m.
Message ID <5f0ec498a035f5a9c120.1379691353@debian-alexander.dolgopa>
Download mbox | patch
Permalink /patch/2565/
State Superseded
Commit 0361163efbaf8b96fac8db712a72b9dfa916079e
Headers show

Comments

Alexander Plavin - Sept. 20, 2013, 3:35 p.m.
# HG changeset patch
# User Alexander Plavin <alexander@plav.in>
# Date 1374620882 -14400
#      Wed Jul 24 03:08:02 2013 +0400
# Node ID 5f0ec498a035f5a9c1202e6095dea1f47101580a
# Parent  4074e05bd30287df5d39d0b36a3bb42dbb8701ad
templater: support using templates with non-standard names from map file

Allow to add arbitrarily-named entries to a template map file and then reference
them, to make it possible to deduplicate and simplify templates code.

Patch

diff -r 4074e05bd302 -r 5f0ec498a035 mercurial/templater.py
--- a/mercurial/templater.py	Sun Sep 01 10:40:11 2013 +0400
+++ b/mercurial/templater.py	Wed Jul 24 03:08:02 2013 +0400
@@ -139,7 +139,12 @@ 
 def runsymbol(context, mapping, key):
     v = mapping.get(key)
     if v is None:
-        v = context._defaults.get(key, '')
+        v = context._defaults.get(key)
+    if v is None:
+        try:
+            v = context.process(key, mapping)
+        except TemplateNotFound:
+            v = ''
     if util.safehasattr(v, '__call__'):
         return v(**mapping)
     if isinstance(v, types.GeneratorType):
@@ -461,6 +466,9 @@ 
             stylelist.append(split[1])
     return ", ".join(sorted(stylelist))
 
+class TemplateNotFound(util.Abort):
+    pass
+
 class templater(object):
 
     def __init__(self, mapfile, filters={}, defaults={}, cache={},
@@ -512,7 +520,7 @@ 
             try:
                 self.cache[t] = util.readfile(self.map[t][1])
             except KeyError, inst:
-                raise util.Abort(_('"%s" not in template map') % inst.args[0])
+                raise TemplateNotFound(_('"%s" not in template map') % inst.args[0])
             except IOError, inst:
                 raise IOError(inst.args[0], _('template file %s: %s') %
                               (self.map[t][1], inst.args[1]))
diff -r 4074e05bd302 -r 5f0ec498a035 tests/test-command-template.t
--- a/tests/test-command-template.t	Sun Sep 01 10:40:11 2013 +0400
+++ b/tests/test-command-template.t	Wed Jul 24 03:08:02 2013 +0400
@@ -500,6 +500,23 @@ 
   1
   0
 
+Defining non-standard name works:
+
+  $ cat <<EOF > t
+  > changeset = '{c}'
+  > c = q
+  > EOF
+  $ hg log --style ./t
+  8
+  7
+  6
+  5
+  4
+  3
+  2
+  1
+  0
+
 ui.style works:
 
   $ echo '[ui]' > .hg/hgrc