templatekw: expose color name in {namespaces} entries
authorGregory Szorc <gregory.szorc@gmail.com>
Sat, 24 Jun 2017 13:39:20 -0700
changeset 33047 de8e3681c402
parent 33046 11f768258dcc
child 33048 46fa46608ca5
templatekw: expose color name in {namespaces} entries Templates make use of a "log.<namespace>" label. The <namespace> value here differs from the actual namespace name in that the namespace itself is plural but the label/color value is singular. Expose the color name to the templating layer so log.* labels can be emitted for {namespaces}. As part of this, we refactored the logic to eliminate a gnarly comprehension. We store color names in their own dict because the lookup can occur in tight loops and we shouldn't have to go to repo.names[ns] multiple times for every changeset.
mercurial/templatekw.py
tests/test-command-template.t
--- a/mercurial/templatekw.py	Sat Jun 24 12:47:25 2017 -0700
+++ b/mercurial/templatekw.py	Sat Jun 24 13:39:20 2017 -0700
@@ -554,13 +554,24 @@
     args = pycompat.byteskwargs(args)
     ctx = args['ctx']
     repo = ctx.repo()
-    namespaces = util.sortdict((k, showlist('name', ns.names(repo, ctx.node()),
-                                            args))
-                               for k, ns in repo.names.iteritems())
+
+    namespaces = util.sortdict()
+    colornames = {}
+
+    for k, ns in repo.names.iteritems():
+        namespaces[k] = showlist('name', ns.names(repo, ctx.node()), args)
+        colornames[k] = ns.colorname
+
     f = _showlist('namespace', list(namespaces), args)
-    return _hybrid(f, namespaces,
-                   lambda k: {'namespace': k, 'names': namespaces[k]},
-                   lambda x: x['namespace'])
+
+    def makemap(ns):
+        return {
+            'namespace': ns,
+            'names': namespaces[ns],
+            'colorname': colornames[ns],
+        }
+
+    return _hybrid(f, namespaces, makemap, lambda x: x['namespace'])
 
 @templatekeyword('node')
 def shownode(repo, ctx, templ, **args):
--- a/tests/test-command-template.t	Sat Jun 24 12:47:25 2017 -0700
+++ b/tests/test-command-template.t	Sat Jun 24 13:39:20 2017 -0700
@@ -3894,10 +3894,31 @@
 
 Test namespaces dict
 
-  $ hg log -T '{rev}{namespaces % " {namespace}={join(names, ",")}"}\n'
-  2 bookmarks=bar,foo tags=tip branches=text.{rev}
-  1 bookmarks=baz tags= branches=text.{rev}
-  0 bookmarks= tags= branches=default
+  $ hg log -T '{rev}\n{namespaces % " {namespace} color={colorname}\n  {join(names, ",")}\n"}\n'
+  2
+   bookmarks color=bookmark
+    bar,foo
+   tags color=tag
+    tip
+   branches color=branch
+    text.{rev}
+  
+  1
+   bookmarks color=bookmark
+    baz
+   tags color=tag
+    
+   branches color=branch
+    text.{rev}
+  
+  0
+   bookmarks color=bookmark
+    
+   tags color=tag
+    
+   branches color=branch
+    default
+  
   $ hg log -r2 -T '{namespaces % "{namespace}: {names}\n"}'
   bookmarks: bar foo
   tags: tip