templater: load template fragments from [templates] section in map file
authorYuya Nishihara <yuya@tcha.org>
Sat, 14 Oct 2017 17:51:01 +0900
changeset 34714 f4aeb952ab77
parent 34713 e5a2cfc524d4
child 34715 f17a0e18c47e
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.
mercurial/templater.py
mercurial/templates/map-cmdline.bisect
mercurial/templates/map-cmdline.changelog
mercurial/templates/map-cmdline.compact
mercurial/templates/map-cmdline.default
mercurial/templates/map-cmdline.phases
mercurial/templates/map-cmdline.show
mercurial/templates/map-cmdline.status
mercurial/templates/map-cmdline.xml
tests/test-command-template.t
--- a/mercurial/templater.py	Sat Oct 14 17:41:41 2017 +0900
+++ b/mercurial/templater.py	Sat Oct 14 17:51:01 2017 +0900
@@ -1340,12 +1340,12 @@
 
     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 @@
 
         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
--- a/mercurial/templates/map-cmdline.bisect	Sat Oct 14 17:41:41 2017 +0900
+++ b/mercurial/templates/map-cmdline.bisect	Sat Oct 14 17:51:01 2017 +0900
@@ -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'
--- a/mercurial/templates/map-cmdline.changelog	Sat Oct 14 17:41:41 2017 +0900
+++ b/mercurial/templates/map-cmdline.changelog	Sat Oct 14 17:51:01 2017 +0900
@@ -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'
--- a/mercurial/templates/map-cmdline.compact	Sat Oct 14 17:41:41 2017 +0900
+++ b/mercurial/templates/map-cmdline.compact	Sat Oct 14 17:51:01 2017 +0900
@@ -1,3 +1,4 @@
+[templates]
 ldate = '{label("log.date",
                 "{date|isodate}")}'
 
--- a/mercurial/templates/map-cmdline.default	Sat Oct 14 17:41:41 2017 +0900
+++ b/mercurial/templates/map-cmdline.default	Sat Oct 14 17:51:01 2017 +0900
@@ -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'
--- a/mercurial/templates/map-cmdline.phases	Sat Oct 14 17:41:41 2017 +0900
+++ b/mercurial/templates/map-cmdline.phases	Sat Oct 14 17:51:01 2017 +0900
@@ -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'
--- a/mercurial/templates/map-cmdline.show	Sat Oct 14 17:41:41 2017 +0900
+++ b/mercurial/templates/map-cmdline.show	Sat Oct 14 17:51:01 2017 +0900
@@ -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}'
--- a/mercurial/templates/map-cmdline.status	Sat Oct 14 17:41:41 2017 +0900
+++ b/mercurial/templates/map-cmdline.status	Sat Oct 14 17:51:01 2017 +0900
@@ -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'
--- a/mercurial/templates/map-cmdline.xml	Sat Oct 14 17:41:41 2017 +0900
+++ b/mercurial/templates/map-cmdline.xml	Sat Oct 14 17:51:01 2017 +0900
@@ -1,3 +1,4 @@
+[templates]
 docheader = '<?xml version="1.0"?>\n<log>\n'
 docfooter = '</log>\n'
 
--- a/tests/test-command-template.t	Sat Oct 14 17:41:41 2017 +0900
+++ b/tests/test-command-template.t	Sat Oct 14 17:51:01 2017 +0900
@@ -259,6 +259,25 @@
   $ 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