# HG changeset patch # User Yuya Nishihara # Date 1520771175 -32400 # Node ID 939e0983c1d9717e492ee09c837b40c6b4d2c8cf # Parent 036e4483d3a1bce5a2661b3c3f62355d8849ecb0 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. diff -r 036e4483d3a1 -r 939e0983c1d9 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, }