Patchwork [2,of,6] formatter: fill missing resources by formatter, not by resource mapper

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 2, 2018, 7:42 a.m.
Message ID <84b86965ea131a83ad7b.1535874179@mimosa>
Download mbox | patch
Permalink /patch/34246/
State New
Headers show

Comments

Yuya Nishihara - Sept. 2, 2018, 7:42 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1535775705 -32400
#      Sat Sep 01 13:21:45 2018 +0900
# Node ID 84b86965ea131a83ad7b4a5adaad04b337a138b5
# Parent  abb6e1badc8635214d7bd5ef1886fd8f346a97ae
formatter: fill missing resources by formatter, not by resource mapper

While working on demand loading of ctx/fctx objects, I found it's weird
to support lookup in both directions. For instance, fctx can be loaded
from (ctx, path) pair, but ctx may also be derived from fctx.changectx()
in the original mapping. If the original mapping has had fctx but no ctx,
and if the new mapping provides {path}, we can't be sure if fctx should be
updated by fctx'.changectx()[path] or not.

This patch simply drops the support for the resolution in fctx -> ctx -> repo
direction.

Patch

diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -199,8 +199,13 @@  class baseformatter(object):
     def context(self, **ctxs):
         '''insert context objects to be used to render template keywords'''
         ctxs = pycompat.byteskwargs(ctxs)
-        assert all(k in {'ctx', 'fctx'} for k in ctxs)
+        assert all(k in {'repo', 'ctx', 'fctx'} for k in ctxs)
         if self._converter.storecontext:
+            # populate missing resources in fctx -> ctx -> repo order
+            if 'fctx' in ctxs and 'ctx' not in ctxs:
+                ctxs['ctx'] = ctxs['fctx'].changectx()
+            if 'ctx' in ctxs and 'repo' not in ctxs:
+                ctxs['repo'] = ctxs['ctx'].repo()
             self._item.update(ctxs)
     def datahint(self):
         '''set of field names to be referenced'''
@@ -578,27 +583,13 @@  class templateresources(templater.resour
         return self._resmap.get(key)
 
     def _hasctx(self, mapping):
-        return 'ctx' in mapping or 'fctx' in mapping
-
-    def _getctx(self, 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(self, mapping, key):
-        ctx = self._getctx(mapping, 'ctx')
-        if ctx is not None:
-            return ctx.repo()
-        return self._getsome(mapping, key)
+        return 'ctx' in mapping
 
     _gettermap = {
         'cache': _getsome,
-        'ctx': _getctx,
+        'ctx': _getsome,
         'fctx': _getsome,
-        'repo': _getrepo,
+        'repo': _getsome,
         'revcache': _getsome,
         'ui': _getsome,
     }