formatter: unblock storing fctx as a template resource
authorYuya Nishihara <yuya@tcha.org>
Sun, 11 Mar 2018 21:26:15 +0900
changeset 36984 939e0983c1d9
parent 36983 036e4483d3a1
child 36985 66e64681e0a8
formatter: unblock storing fctx as a template resource To keep templateformatter._renderitem() simple, a repo instance is looked through ctx if available. This is probably good for future subrepo support where ctx.repo() may be different from the global repo.
mercurial/formatter.py
--- a/mercurial/formatter.py	Sun Mar 11 21:12:02 2018 +0900
+++ b/mercurial/formatter.py	Sun Mar 11 21:26:15 2018 +0900
@@ -188,7 +188,7 @@
     def context(self, **ctxs):
         '''insert context objects to be used to render template keywords'''
         ctxs = pycompat.byteskwargs(ctxs)
-        assert all(k == 'ctx' for k in ctxs)
+        assert all(k in {'ctx', 'fctx'} for k in ctxs)
         if self._converter.storecontext:
             self._item.update(ctxs)
     def data(self, **data):
@@ -395,13 +395,11 @@
             return
         ref = self._parts[part]
 
-        # TODO: add support for filectx
         props = {}
         # explicitly-defined fields precede templatekw
         props.update(item)
-        if 'ctx' in item:
+        if 'ctx' in item or 'fctx' in item:
             # but template resources must be always available
-            props['repo'] = props['ctx'].repo()
             props['revcache'] = {}
         props = pycompat.strkwargs(props)
         g = self._t(ref, **props)
@@ -513,10 +511,25 @@
             return v
         return resmap.get(key)
 
+    def getctx(context, mapping, key):
+        ctx = mapping.get('ctx')
+        if ctx is not None:
+            return ctx
+        fctx = mapping.get('fctx')
+        if fctx is not None:
+            return fctx.changectx()
+
+    def getrepo(context, mapping, key):
+        ctx = getctx(context, mapping, 'ctx')
+        if ctx is not None:
+            return ctx.repo()
+        return getsome(context, mapping, key)
+
     return {
         'cache': getsome,
-        'ctx': getsome,
-        'repo': getsome,
+        'ctx': getctx,
+        'fctx': getsome,
+        'repo': getrepo,
         'revcache': getsome,  # per-ctx cache; set later
         'ui': getsome,
     }