changeset_templater: backport separator template from formatter
authorYuya Nishihara <yuya@tcha.org>
Sat, 17 Jun 2017 16:32:20 +0900
changeset 32951 050efe9a1644
parent 32950 5100ce217dfa
child 32952 61b60b28c381
changeset_templater: backport separator template from formatter As commented, this should be used with docheader and docfooter, not with header nor footer. That's one reason why no props are passed to templater when rendering a separator. (See map-cmdline.changelog to understand what the "header" is.)
mercurial/cmdutil.py
tests/test-command-template.t
--- a/mercurial/cmdutil.py	Sat Apr 22 21:50:51 2017 +0900
+++ b/mercurial/cmdutil.py	Sat Jun 17 16:32:20 2017 +0900
@@ -1587,7 +1587,8 @@
         self._tref = tmplspec.ref
         self._parts = {'header': '', 'footer': '',
                        tmplspec.ref: tmplspec.ref,
-                       'docheader': '', 'docfooter': ''}
+                       'docheader': '', 'docfooter': '',
+                       'separator': ''}
         if tmplspec.mapfile:
             # find correct templates for current mode, for backward
             # compatibility with 'log -v/-q/--debug' using a mapfile
@@ -1621,11 +1622,17 @@
         props['ctx'] = ctx
         props['repo'] = self.repo
         props['ui'] = self.repo.ui
-        props['index'] = next(self._counter)
+        props['index'] = index = next(self._counter)
         props['revcache'] = {'copies': copies}
         props['cache'] = self.cache
         props = pycompat.strkwargs(props)
 
+        # write separator, which wouldn't work well with the header part below
+        # since there's inherently a conflict between header (across items) and
+        # separator (per item)
+        if self._parts['separator'] and index > 0:
+            self.ui.write(templater.stringify(self.t(self._parts['separator'])))
+
         # write header
         if self._parts['header']:
             h = templater.stringify(self.t(self._parts['header'], **props))
--- a/tests/test-command-template.t	Sat Apr 22 21:50:51 2017 +0900
+++ b/tests/test-command-template.t	Sat Jun 17 16:32:20 2017 +0900
@@ -257,6 +257,20 @@
   summary:     third
   
 
+Test docheader, docfooter and separator in template map
+
+  $ cat <<'EOF' > map-myjson
+  > docheader = '\{\n'
+  > docfooter = '\n}\n'
+  > separator = ',\n'
+  > changeset = ' {dict(rev, node|short)|json}'
+  > EOF
+  $ hg log -l2 -T./map-myjson
+  {
+   {"node": "95c24699272e", "rev": 8},
+   {"node": "29114dbae42b", "rev": 7}
+  }
+
 Template should precede style option
 
   $ hg log -l1 --style default -T '{rev}\n'