changeset 34715:f17a0e18c47e

templater: load aliases from [templatealias] section in map file This seems sometimes useful as an alias can be a function, but a template fragment can't.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 14 Oct 2017 18:06:42 +0900
parents f4aeb952ab77
children d36a36ca7bc2
files mercurial/formatter.py mercurial/templater.py tests/test-command-template.t
diffstat 3 files changed, 14 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/formatter.py	Sat Oct 14 17:51:01 2017 +0900
+++ b/mercurial/formatter.py	Sat Oct 14 18:06:42 2017 +0900
@@ -418,8 +418,8 @@
 
     A map file defines a stand-alone template environment. If a map file
     selected, all templates defined in the file will be loaded, and the
-    template matching the given topic will be rendered. No aliases will be
-    loaded from user config.
+    template matching the given topic will be rendered. Aliases won't be
+    loaded from user config, but from the map file.
 
     If no map file selected, all templates in [templates] section will be
     available as well as aliases in [templatealias].
--- a/mercurial/templater.py	Sat Oct 14 17:51:01 2017 +0900
+++ b/mercurial/templater.py	Sat Oct 14 18:06:42 2017 +0900
@@ -1344,6 +1344,7 @@
 
     cache = {}
     tmap = {}
+    aliases = []
 
     val = conf.get('templates', '__base__')
     if val and val[0] not in "'\"":
@@ -1362,7 +1363,7 @@
                     path = p3
                     break
 
-        cache, tmap = _readmapfile(path)
+        cache, tmap, aliases = _readmapfile(path)
 
     for key, val in conf['templates'].items():
         if not val:
@@ -1378,7 +1379,8 @@
             if ':' in val[1]:
                 val = val[1].split(':', 1)
             tmap[key] = val[0], os.path.join(base, val[1])
-    return cache, tmap
+    aliases.extend(conf['templatealias'].items())
+    return cache, tmap, aliases
 
 class TemplateNotFound(error.Abort):
     pass
@@ -1412,9 +1414,10 @@
                     minchunk=1024, maxchunk=65536):
         """Create templater from the specified map file"""
         t = cls(filters, defaults, cache, [], minchunk, maxchunk)
-        cache, tmap = _readmapfile(mapfile)
+        cache, tmap, aliases = _readmapfile(mapfile)
         t.cache.update(cache)
         t.map = tmap
+        t._aliases = aliases
         return t
 
     def __contains__(self, key):
--- a/tests/test-command-template.t	Sat Oct 14 17:51:01 2017 +0900
+++ b/tests/test-command-template.t	Sat Oct 14 18:06:42 2017 +0900
@@ -259,11 +259,13 @@
   $ hg log -l1 -T./map-simple
   8
 
- a map file may have [templates] section:
+ a map file may have [templates] and [templatealias] sections:
 
   $ cat <<'EOF' > map-simple
   > [templates]
-  > changeset = "{rev}\n"
+  > changeset = "{a}\n"
+  > [templatealias]
+  > a = rev
   > EOF
   $ hg log -l1 -T./map-simple
   8
@@ -277,6 +279,8 @@
   > EOF
   $ HGRCPATH=./myhgrc hg log -l1 -Tfoo
   8
+  $ HGRCPATH=./myhgrc hg log -l1 -T'{a}\n'
+  8
 
 Test template map inheritance