Patchwork [3,of,4,V2] formatter: unblock storing fctx as a template resource

login
register
mail settings
Submitter Yuya Nishihara
Date March 16, 2018, 4:03 p.m.
Message ID <f810d481d55c05739120.1521216200@mimosa>
Download mbox | patch
Permalink /patch/29568/
State New
Headers show

Comments

Yuya Nishihara - March 16, 2018, 4:03 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1520771175 -32400
#      Sun Mar 11 21:26:15 2018 +0900
# Node ID f810d481d55c0573912058c68c615bfa30e20805
# Parent  5d94efb75658163ab7db01acbefeb6469bc97739
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.

Patch

diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -188,7 +188,7 @@  class baseformatter(object):
     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 @@  class templateformatter(baseformatter):
             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 @@  def templateresources(ui, repo=None):
             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,
     }