Patchwork [3,of,6] templater: load template fragments from [templates] section in map file

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 14, 2017, 10:41 a.m.
Message ID <7a421fadc47ca6c23618.1507977672@mimosa>
Download mbox | patch
Permalink /patch/24889/
State Accepted
Headers show

Comments

Yuya Nishihara - Oct. 14, 2017, 10:41 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1507971061 -32400
#      Sat Oct 14 17:51:01 2017 +0900
# Node ID 7a421fadc47ca6c23618615e8e3938717bbc13d0
# Parent  f9e359b56a221e7cd9a83ee4c65cb3eae9f0d814
templater: load template fragments from [templates] section in map file

This allows us to %include map-cmdline.<style> file in our .hgrc files. The
syntax is slightly different as hgrc doesn't support loading an external
template file, but map-cmdline files don't use this feature, so the syntax
can be considered identical in practice.

Unnamed section is remapped for backward compatibility.

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -1340,12 +1340,12 @@  def _readmapfile(mapfile):
 
     base = os.path.dirname(mapfile)
     conf = config.config(includepaths=templatepaths())
-    conf.read(mapfile)
+    conf.read(mapfile, remap={'': 'templates'})
 
     cache = {}
     tmap = {}
 
-    val = conf.get('', '__base__')
+    val = conf.get('templates', '__base__')
     if val and val[0] not in "'\"":
         # treat as a pointer to a base class for this style
         path = util.normpath(os.path.join(base, val))
@@ -1364,13 +1364,14 @@  def _readmapfile(mapfile):
 
         cache, tmap = _readmapfile(path)
 
-    for key, val in conf[''].items():
+    for key, val in conf['templates'].items():
         if not val:
-            raise error.ParseError(_('missing value'), conf.source('', key))
+            raise error.ParseError(_('missing value'),
+                                   conf.source('templates', key))
         if val[0] in "'\"":
             if val[0] != val[-1]:
                 raise error.ParseError(_('unmatched quotes'),
-                                       conf.source('', key))
+                                       conf.source('templates', key))
             cache[key] = unquotestring(val)
         elif key != '__base__':
             val = 'default', val
diff --git a/mercurial/templates/map-cmdline.bisect b/mercurial/templates/map-cmdline.bisect
--- a/mercurial/templates/map-cmdline.bisect
+++ b/mercurial/templates/map-cmdline.bisect
@@ -1,5 +1,6 @@ 
 %include map-cmdline.default
 
+[templates]
 changeset = '{cset}{lbisect}{branches}{bookmarks}{tags}{parents}{user}{ldate}{summary}\n'
 changeset_quiet = '{lshortbisect} {rev}:{node|short}\n'
 changeset_verbose = '{cset}{lbisect}{branches}{bookmarks}{tags}{parents}{user}{ldate}{lfiles}{lfile_copies_switch}{description}\n'
diff --git a/mercurial/templates/map-cmdline.changelog b/mercurial/templates/map-cmdline.changelog
--- a/mercurial/templates/map-cmdline.changelog
+++ b/mercurial/templates/map-cmdline.changelog
@@ -1,3 +1,4 @@ 
+[templates]
 header = '{date|shortdate}  {author|person}  <{author|email}>\n\n'
 header_verbose = ''
 changeset = '\t* {files|stringify|fill68|tabindent}{desc|fill68|tabindent|strip}\n\t[{node|short}]{tags}{branches}\n\n'
diff --git a/mercurial/templates/map-cmdline.compact b/mercurial/templates/map-cmdline.compact
--- a/mercurial/templates/map-cmdline.compact
+++ b/mercurial/templates/map-cmdline.compact
@@ -1,3 +1,4 @@ 
+[templates]
 ldate = '{label("log.date",
                 "{date|isodate}")}'
 
diff --git a/mercurial/templates/map-cmdline.default b/mercurial/templates/map-cmdline.default
--- a/mercurial/templates/map-cmdline.default
+++ b/mercurial/templates/map-cmdline.default
@@ -1,5 +1,7 @@ 
 # Base templates. Due to name clashes with existing keywords, we have
 # to replace some keywords with 'lkeyword', for 'labelled keyword'
+
+[templates]
 changeset = '{cset}{branches}{bookmarks}{tags}{parents}{user}{ldate}{ltroubles}{summary}\n'
 changeset_quiet = '{lnode}'
 changeset_verbose = '{cset}{branches}{bookmarks}{tags}{parents}{user}{ldate}{ltroubles}{lfiles}{lfile_copies_switch}{description}\n'
diff --git a/mercurial/templates/map-cmdline.phases b/mercurial/templates/map-cmdline.phases
--- a/mercurial/templates/map-cmdline.phases
+++ b/mercurial/templates/map-cmdline.phases
@@ -1,3 +1,5 @@ 
 %include map-cmdline.default
+
+[templates]
 changeset = '{cset}{branches}{bookmarks}{tags}{lphase}{parents}{user}{ldate}{summary}\n'
 changeset_verbose = '{cset}{branches}{bookmarks}{tags}{lphase}{parents}{user}{ldate}{lfiles}{lfile_copies_switch}{description}\n'
diff --git a/mercurial/templates/map-cmdline.show b/mercurial/templates/map-cmdline.show
--- a/mercurial/templates/map-cmdline.show
+++ b/mercurial/templates/map-cmdline.show
@@ -3,6 +3,8 @@ 
 #   piggyback on existing values so color works.
 # * Obsolescence isn't considered for node labels. See _cset_labels in
 #   map-cmdline.default.
+
+[templates]
 showbookmarks = '{if(active, "*", " ")} {pad(bookmark, longestbookmarklen + 4)}{shortest(node, nodelen)}\n'
 
 showwork = '{cset_shortnode}{namespaces % cset_namespace} {cset_shortdesc}'
diff --git a/mercurial/templates/map-cmdline.status b/mercurial/templates/map-cmdline.status
--- a/mercurial/templates/map-cmdline.status
+++ b/mercurial/templates/map-cmdline.status
@@ -1,5 +1,6 @@ 
 %include map-cmdline.default
 
+[templates]
 # Override base templates
 changeset = '{cset}{branches}{bookmarks}{tags}{parents}{user}{ldate}{summary}{lfiles}\n'
 changeset_verbose = '{cset}{branches}{bookmarks}{tags}{parents}{user}{ldate}{description}{lfiles}\n'
diff --git a/mercurial/templates/map-cmdline.xml b/mercurial/templates/map-cmdline.xml
--- a/mercurial/templates/map-cmdline.xml
+++ b/mercurial/templates/map-cmdline.xml
@@ -1,3 +1,4 @@ 
+[templates]
 docheader = '<?xml version="1.0"?>\n<log>\n'
 docfooter = '</log>\n'
 
diff --git a/tests/test-command-template.t b/tests/test-command-template.t
--- a/tests/test-command-template.t
+++ b/tests/test-command-template.t
@@ -259,6 +259,25 @@  Test templates and style maps in files:
   $ hg log -l1 -T./map-simple
   8
 
+ a map file may have [templates] section:
+
+  $ cat <<'EOF' > map-simple
+  > [templates]
+  > changeset = "{rev}\n"
+  > EOF
+  $ hg log -l1 -T./map-simple
+  8
+
+ so it can be included in hgrc
+
+  $ cat <<'EOF' > myhgrc
+  > %include map-simple
+  > [templates]
+  > foo = "{changeset}"
+  > EOF
+  $ HGRCPATH=./myhgrc hg log -l1 -Tfoo
+  8
+
 Test template map inheritance
 
   $ echo "__base__ = map-cmdline.default" > map-simple