# HG changeset patch # User Yuya Nishihara # Date 2017-09-18 14:07:17 # Node ID 4c1cfe54c08ddfef0f1db9ab7ffbab109b93f9e6 # Parent 78590585c0db3d0b8c0fadb31f4ff115fae74fca templater: extend dot operator as a short for get(dict, key) diff --git a/mercurial/help/templates.txt b/mercurial/help/templates.txt --- a/mercurial/help/templates.txt +++ b/mercurial/help/templates.txt @@ -76,6 +76,7 @@ The dot operator can be used as a shorth - ``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 ======= diff --git a/mercurial/templater.py b/mercurial/templater.py --- a/mercurial/templater.py +++ b/mercurial/templater.py @@ -463,7 +463,8 @@ def runmember(context, mapping, data): 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 @@ def get(context, mapping, args): 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 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 @@ -3184,6 +3184,8 @@ Test evaluation of dot operator: $ 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