formatter: remove template resources from nested items before generating JSON
authorYuya Nishihara <yuya@tcha.org>
Sat, 17 Mar 2018 23:34:38 +0900
changeset 37502 40c7347f6848
parent 37501 0f4de9c27973
child 37503 49a8c2cc7978
formatter: remove template resources from nested items before generating JSON
mercurial/templateutil.py
tests/test-annotate.t
--- a/mercurial/templateutil.py	Thu Mar 15 21:09:37 2018 +0900
+++ b/mercurial/templateutil.py	Sat Mar 17 23:34:38 2018 +0900
@@ -200,7 +200,14 @@
         return self.join(context, mapping, self._defaultsep)
 
     def tovalue(self, context, mapping):
-        return list(self.itermaps(context))
+        knownres = context.knownresourcekeys()
+        items = []
+        for nm in self.itermaps(context):
+            # drop internal resources (recursively) which shouldn't be displayed
+            lm = context.overlaymap(mapping, nm)
+            items.append({k: unwrapvalue(context, lm, v)
+                          for k, v in nm.iteritems() if k not in knownres})
+        return items
 
 class mappinggenerator(_mappingsequence):
     """Wrapper for generator of template mappings
--- a/tests/test-annotate.t	Thu Mar 15 21:09:37 2018 +0900
+++ b/tests/test-annotate.t	Sat Mar 17 23:34:38 2018 +0900
@@ -105,6 +105,8 @@
 
 several filters can be applied to '{lines}'
 
+  $ hg annotate -T'{lines|json}\n' a
+  [{"line": "a\n", "rev": 0}, {"line": "a\n", "rev": 1}, {"line": "a\n", "rev": 1}]
   $ hg annotate -T'{lines|stringify}' a
   0: a
   1: a