[PATCH 1 of 4] templater: add hook point to populate additional mapping items

Yuya Nishihara yuya at tcha.org
Thu Mar 22 15:02:41 UTC 2018


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1521118173 -32400
#      Thu Mar 15 21:49:33 2018 +0900
# Node ID 5614b5980fa6a42b7d85761389e30deab997bc3f
# Parent  d53ab441e3ce649d5151340ec06be72ee8daf3c4
templater: add hook point to populate additional mapping items

The 'revcache' dict will be inserted by this hook.

diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -517,6 +517,9 @@ class templateresources(templater.resour
             return None
         return get(self, context, mapping, key)
 
+    def populatemap(self, context, origmapping, newmapping):
+        return {}
+
     def _getsome(self, context, mapping, key):
         v = mapping.get(key)
         if v is not None:
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -577,6 +577,11 @@ class resourcemapper(object):
     def lookup(self, context, mapping, key):
         """Return a resource for the key if available; otherwise None"""
 
+    @abc.abstractmethod
+    def populatemap(self, context, origmapping, newmapping):
+        """Return a dict of additional mapping items which should be paired
+        with the given new mapping"""
+
 class nullresourcemapper(resourcemapper):
     def availablekeys(self, context, mapping):
         return set()
@@ -587,6 +592,9 @@ class nullresourcemapper(resourcemapper)
     def lookup(self, context, mapping, key):
         return None
 
+    def populatemap(self, context, origmapping, newmapping):
+        return {}
+
 class engine(object):
     '''template expansion engine.
 
@@ -634,6 +642,8 @@ class engine(object):
                    if (k in knownres  # not a symbol per self.symbol()
                        or newres.isdisjoint(self._defaultrequires(k)))}
         mapping.update(newmapping)
+        mapping.update(
+            self._resources.populatemap(self, origmapping, newmapping))
         return mapping
 
     def _defaultrequires(self, key):
@@ -689,6 +699,13 @@ class engine(object):
         mapping contains added elements for use during expansion. Is a
         generator.'''
         func, data = self._load(t)
+        # populate additional items only if they don't exist in the given
+        # mapping. this is slightly different from overlaymap() because the
+        # initial 'revcache' may contain pre-computed items.
+        extramapping = self._resources.populatemap(self, {}, mapping)
+        if extramapping:
+            extramapping.update(mapping)
+            mapping = extramapping
         return _flatten(func(self, mapping, data))
 
 engines = {'default': engine}


More information about the Mercurial-devel mailing list