Patchwork [4,of,6] templater: load aliases from [templatealias] section in map file

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 14, 2017, 10:41 a.m.
Message ID <48c3ed364bcac4e271d2.1507977673@mimosa>
Download mbox | patch
Permalink /patch/24890/
State Accepted
Headers show

Comments

Yuya Nishihara - Oct. 14, 2017, 10:41 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1507972002 -32400
#      Sat Oct 14 18:06:42 2017 +0900
# Node ID 48c3ed364bcac4e271d21cc1cde1c73704641b0c
# Parent  7a421fadc47ca6c23618615e8e3938717bbc13d0
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.

Patch

diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -418,8 +418,8 @@  def lookuptemplate(ui, topic, tmpl):
 
     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].
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -1344,6 +1344,7 @@  def _readmapfile(mapfile):
 
     cache = {}
     tmap = {}
+    aliases = []
 
     val = conf.get('templates', '__base__')
     if val and val[0] not in "'\"":
@@ -1362,7 +1363,7 @@  def _readmapfile(mapfile):
                     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 @@  def _readmapfile(mapfile):
             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 @@  class templater(object):
                     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):
diff --git a/tests/test-command-template.t b/tests/test-command-template.t
--- a/tests/test-command-template.t
+++ b/tests/test-command-template.t
@@ -259,11 +259,13 @@  Test templates and style maps in files:
   $ 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 @@  Test templates and style maps in files:
   > EOF
   $ HGRCPATH=./myhgrc hg log -l1 -Tfoo
   8
+  $ HGRCPATH=./myhgrc hg log -l1 -T'{a}\n'
+  8
 
 Test template map inheritance