templater: look up symbols/resources as if they were separated (issue5699)
authorYuya Nishihara <yuya@tcha.org>
Thu, 21 Dec 2017 22:17:39 +0900
changeset 35474 a33be093ec62
parent 35473 f1c54d003327
child 35475 ea4d27aac557
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.
mercurial/formatter.py
mercurial/templater.py
tests/test-command-template.t
--- 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,
     }
 
--- 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):
--- 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