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

login
register
mail settings
Submitter Alexander Plavin
Date Sept. 2, 2013, 7:46 p.m.
Message ID <c71151e59f50d5a112f9.1378151171@debian-alexander.dolgopa>
Download mbox | patch
Permalink /patch/2302/
State Superseded
Commit 0361163efbaf8b96fac8db712a72b9dfa916079e
Headers show

Comments

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

This allows adding arbitrarily-named entries to a template map file, and then
referencing them, giving the ability to deduplicate and simplify templates code.
Augie Fackler - Sept. 6, 2013, 7:35 p.m.
On Fri, Sep 6, 2013 at 3:34 PM, Alexander Plavin <alexander@plav.in> wrote:
> Nice suggestion, to subclass Abort - didn't think of it. And if that is 'at a minimum', what would be the (more) optimal solution?


A more optimal solution would be to not subclass abort, and make
clients of the templater that want to abort on a templater failure do
so explicitly.

Patch

diff -r 3dfc0bf05e89 -r c71151e59f50 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 util.Abort:
+            v = ''
     if util.safehasattr(v, '__call__'):
         return v(**mapping)
     if isinstance(v, types.GeneratorType):
diff -r 3dfc0bf05e89 -r c71151e59f50 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