changeset 26222:3095b1027661

templater: add new docheader/footer components for XML (issue4135) The existing header/footer components were templated per-changeset, and thus couldn't be correctly printed for an empty log
author Matt Mackall <mpm@selenic.com>
date Wed, 26 Aug 2015 16:27:14 -0500
parents ae65b1b4cb46
children ed12abab068e
files mercurial/cmdutil.py mercurial/templates/map-cmdline.xml tests/test-command-template.t
diffstat 3 files changed, 19 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/cmdutil.py	Thu Sep 10 09:52:17 2015 -0400
+++ b/mercurial/cmdutil.py	Wed Aug 26 16:27:14 2015 -0500
@@ -1422,7 +1422,8 @@
             (self.ui.debugflag, 'debug'),
         ]
 
-        self._parts = {'header': '', 'footer': '', 'changeset': 'changeset'}
+        self._parts = {'header': '', 'footer': '', 'changeset': 'changeset',
+                       'docheader': '', 'docfooter': ''}
         for mode, postfix in tmplmodes:
             for t in self._parts:
                 cur = t
@@ -1431,6 +1432,16 @@
                 if mode and cur in self.t:
                     self._parts[t] = cur
 
+        if self._parts['docheader']:
+            self.ui.write(templater.stringify(self.t(self._parts['docheader'])))
+
+    def close(self):
+        if self._parts['docfooter']:
+            if not self.footer:
+                self.footer = ""
+            self.footer += templater.stringify(self.t(self._parts['docfooter']))
+        return super(changeset_templater, self).close()
+
     def _show(self, ctx, copies, matchfn, props):
         '''show a single changeset or file revision'''
 
--- a/mercurial/templates/map-cmdline.xml	Thu Sep 10 09:52:17 2015 -0400
+++ b/mercurial/templates/map-cmdline.xml	Wed Aug 26 16:27:14 2015 -0500
@@ -1,5 +1,5 @@
-header = '<?xml version="1.0"?>\n<log>\n'
-footer = '</log>\n'
+docheader = '<?xml version="1.0"?>\n<log>\n'
+docfooter = '</log>\n'
 
 changeset = '<logentry revision="{rev}" node="{node}">\n{branches}{bookmarks}{tags}{parents}<author email="{author|email|xmlescape}">{author|person|xmlescape}</author>\n<date>{date|rfc3339date}</date>\n<msg xml:space="preserve">{desc|xmlescape}</msg>\n</logentry>\n'
 changeset_verbose = '<logentry revision="{rev}" node="{node}">\n{branches}{bookmarks}{tags}{parents}<author email="{author|email|xmlescape}">{author|person|xmlescape}</author>\n<date>{date|rfc3339date}</date>\n<msg xml:space="preserve">{desc|xmlescape}</msg>\n<paths>\n{file_adds}{file_dels}{file_mods}</paths>\n{file_copies}</logentry>\n'
--- a/tests/test-command-template.t	Thu Sep 10 09:52:17 2015 -0400
+++ b/tests/test-command-template.t	Wed Aug 26 16:27:14 2015 -0500
@@ -343,6 +343,11 @@
 
 Test xml styles:
 
+  $ hg log --style xml -r 'not all()'
+  <?xml version="1.0"?>
+  <log>
+  </log>
+
   $ hg log --style xml
   <?xml version="1.0"?>
   <log>