Patchwork [4,of,5] formatter: remove template resources from nested items before generating JSON

login
register
mail settings
Submitter Yuya Nishihara
Date April 7, 2018, 9:05 a.m.
Message ID <575c4d56f14ef75c8dd7.1523091943@mimosa>
Download mbox | patch
Permalink /patch/30538/
State Accepted
Headers show

Comments

Yuya Nishihara - April 7, 2018, 9:05 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1521297278 -32400
#      Sat Mar 17 23:34:38 2018 +0900
# Node ID 575c4d56f14ef75c8dd779062d66258d5c62c5ca
# Parent  a5029147b12caa69c76c5a4f9522e6b2c43ee5ae
formatter: remove template resources from nested items before generating JSON

Patch

diff --git a/mercurial/templateutil.py b/mercurial/templateutil.py
--- a/mercurial/templateutil.py
+++ b/mercurial/templateutil.py
@@ -200,7 +200,14 @@  class _mappingsequence(wrapped):
         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
diff --git a/tests/test-annotate.t b/tests/test-annotate.t
--- a/tests/test-annotate.t
+++ b/tests/test-annotate.t
@@ -105,6 +105,8 @@  default output of '{lines}' should be re
 
 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