Patchwork [7,of,8] formatter: add support for parts map of [templates] section

login
register
mail settings
Submitter Yuya Nishihara
Date June 20, 2017, 3:57 p.m.
Message ID <c719e1db3d6e014bbf6a.1497974249@mimosa>
Download mbox | patch
Permalink /patch/21539/
State Accepted
Headers show

Comments

Yuya Nishihara - June 20, 2017, 3:57 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1492864140 -32400
#      Sat Apr 22 21:29:00 2017 +0900
# Node ID c719e1db3d6e014bbf6ae3745c75c0f4e10d55be
# Parent  b6219e719a2828ff9cf391c5d98bcb7688110236
formatter: add support for parts map of [templates] section

Unlike a mapfile whose template is looked up by spec -> mapfile -> topic,
[templates] section is global. We use :sub-section syntax to define parts
per template.

Patch

diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -453,6 +453,11 @@  def templatepartsmap(spec, t, partnames)
     partsmap = {spec.ref: spec.ref}  # initial ref must exist in t
     if spec.mapfile:
         partsmap.update((p, p) for p in partnames if p in t)
+    elif spec.ref:
+        for part in partnames:
+            ref = '%s:%s' % (spec.ref, part)  # select config sub-section
+            if ref in t:
+                partsmap[part] = ref
     return partsmap
 
 def loadtemplater(ui, spec, cache=None):
diff --git a/mercurial/help/templates.txt b/mercurial/help/templates.txt
--- a/mercurial/help/templates.txt
+++ b/mercurial/help/templates.txt
@@ -117,6 +117,15 @@  another template::
 but be aware that the keywords cannot be overridden by templates. For example,
 a template defined as ``templates.rev`` cannot be referenced as ``{rev}``.
 
+A template defined in ``templates`` section may have sub templates which
+are inserted before/after/between items::
+
+  [templates]
+  myjson = ' {dict(rev, node|short)|json}'
+  myjson:docheader = '\{\n'
+  myjson:docfooter = '\n}\n'
+  myjson:separator = ',\n'
+
 Examples
 ========
 
diff --git a/tests/test-branches.t b/tests/test-branches.t
--- a/tests/test-branches.t
+++ b/tests/test-branches.t
@@ -544,6 +544,31 @@  template output:
    {"branch": "default", "node": "19709c5a4e75"}
   }
 
+  $ cat <<'EOF' >> .hg/hgrc
+  > [templates]
+  > myjson = ' {dict(branch, node|short)|json}'
+  > myjson:docheader = '\{\n'
+  > myjson:docfooter = '\n}\n'
+  > myjson:separator = ',\n'
+  > EOF
+  $ hg branches -T myjson
+  {
+   {"branch": "b", "node": "e23b5505d1ad"},
+   {"branch": "a branch *", "node": "10ff5895aa57"}, (glob)
+   {"branch": "a", "node": "d8cbc61dbaa6"},
+   {"branch": "default", "node": "19709c5a4e75"}
+  }
+
+  $ cat <<'EOF' >> .hg/hgrc
+  > [templates]
+  > :docheader = 'should not be selected as a docheader for literal templates\n'
+  > EOF
+  $ hg branches -T '{branch}\n'
+  b
+  a branch name much longer than the default justification used by branches
+  a
+  default
+
 Tests of revision branch name caching
 
 We rev branch cache is updated automatically. In these tests we use a trick to