changeset 34536:4c1cfe54c08d

templater: extend dot operator as a short for get(dict, key)
author Yuya Nishihara <yuya@tcha.org>
date Mon, 18 Sep 2017 23:07:17 +0900
parents 78590585c0db
children 8c3dd5e462cc
files mercurial/help/templates.txt mercurial/templater.py tests/test-command-template.t
diffstat 3 files changed, 8 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/help/templates.txt	Sat Sep 09 19:32:56 2017 +0900
+++ b/mercurial/help/templates.txt	Mon Sep 18 23:07:17 2017 +0900
@@ -76,6 +76,7 @@
 
 - ``expr.member`` is roughly equivalent to ``expr % "{member}"`` if ``expr``
   returns a non-list/dict. The returned value is not stringified.
+- ``dict.key`` is identical to ``get(dict, "key")``.
 
 Aliases
 =======
--- a/mercurial/templater.py	Sat Sep 09 19:32:56 2017 +0900
+++ b/mercurial/templater.py	Mon Sep 18 23:07:17 2017 +0900
@@ -463,7 +463,8 @@
         lm = mapping.copy()
         lm.update(d.tomap())
         return runsymbol(context, lm, memb)
-    # TODO: d.get(memb) if dict-like?
+    if util.safehasattr(d, 'get'):
+        return _getdictitem(d, memb)
 
     sym = findsymbolicname(darg)
     if sym:
@@ -751,6 +752,9 @@
         raise error.ParseError(_("get() expects a dict as first argument"))
 
     key = evalfuncarg(context, mapping, args[1])
+    return _getdictitem(dictarg, key)
+
+def _getdictitem(dictarg, key):
     val = dictarg.get(key)
     if val is None:
         return
--- a/tests/test-command-template.t	Sat Sep 09 19:32:56 2017 +0900
+++ b/tests/test-command-template.t	Mon Sep 18 23:07:17 2017 +0900
@@ -3184,6 +3184,8 @@
 
   $ hg log -R latesttag -l1 -T '{min(revset("0:9")).node}\n'
   ce3cec86e6c26bd9bdfc590a6b92abc9680f1796
+  $ hg log -R latesttag -r0 -T '{extras.branch}\n'
+  default
 
   $ hg log -R latesttag -l1 -T '{author.invalid}\n'
   hg: parse error: keyword 'author' has no member