# HG changeset patch # User Yuya Nishihara # Date 1513862259 -32400 # Node ID a33be093ec62458599582687540842b15839b121 # Parent f1c54d0033272bba950eb86c63c2896def29270a templater: look up symbols/resources as if they were separated (issue5699) It wouldn't be easy to split the mapping dict into (symbols, resources). This patch instead rejects invalid lookup taking resources.keys() as source of truth. The doctest is updated since mapping['repo'] is now reserved for a repo object. diff -r f1c54d003327 -r a33be093ec62 mercurial/formatter.py --- a/mercurial/formatter.py Thu Dec 21 22:05:30 2017 +0900 +++ b/mercurial/formatter.py Thu Dec 21 22:17:39 2017 +0900 @@ -94,14 +94,14 @@ >>> def subrepos(ui, fm): ... fm.startitem() -... fm.write(b'repo', b'[%s]\\n', b'baz') +... fm.write(b'reponame', b'[%s]\\n', b'baz') ... files(ui, fm.nested(b'files')) ... fm.end() >>> show(subrepos) [baz] foo bar ->>> show(subrepos, template=b'{repo}: {join(files % "{path}", ", ")}\\n') +>>> show(subrepos, template=b'{reponame}: {join(files % "{path}", ", ")}\\n') baz: foo, bar """ @@ -491,7 +491,9 @@ and function""" return { 'cache': {}, # for templatekw/funcs to store reusable data + 'ctx': None, 'repo': repo, + 'revcache': None, # per-ctx cache; set later 'ui': ui, } diff -r f1c54d003327 -r a33be093ec62 mercurial/templater.py --- a/mercurial/templater.py Thu Dec 21 22:05:30 2017 +0900 +++ b/mercurial/templater.py Thu Dec 21 22:17:39 2017 +0900 @@ -1320,7 +1320,9 @@ def symbol(self, mapping, key): """Resolve symbol to value or function; None if nothing found""" - v = mapping.get(key) + v = None + if key not in self._resources: + v = mapping.get(key) if v is None: v = self._defaults.get(key) return v @@ -1328,11 +1330,13 @@ def resource(self, mapping, key): """Return internal data (e.g. cache) used for keyword/function evaluation""" - v = mapping.get(key) + v = None + if key in self._resources: + v = mapping.get(key) if v is None: v = self._resources.get(key) if v is None: - raise KeyError + raise error.Abort(_('template resource not available: %s') % key) return v def _load(self, t): diff -r f1c54d003327 -r a33be093ec62 tests/test-command-template.t --- a/tests/test-command-template.t Thu Dec 21 22:05:30 2017 +0900 +++ b/tests/test-command-template.t Thu Dec 21 22:17:39 2017 +0900 @@ -206,7 +206,13 @@ Internal resources shouldn't be exposed (issue5699): - $ hg log -r. -T '{cache}{repo}{templ}{ui}' + $ hg log -r. -T '{cache}{ctx}{repo}{revcache}{templ}{ui}' + +Never crash on internal resource not available: + + $ hg --cwd .. debugtemplate '{"c0bebeef"|shortest}\n' + abort: template resource not available: ctx + [255] Quoting for ui.logtemplate