Patchwork [3,of,6] templatekw: expose color name in {namespaces} entries

login
register
mail settings
Submitter Gregory Szorc
Date June 24, 2017, 10:13 p.m.
Message ID <3839334c22d631b956b0.1498342429@ubuntu-vm-main>
Download mbox | patch
Permalink /patch/21677/
State Accepted
Headers show

Comments

Gregory Szorc - June 24, 2017, 10:13 p.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1498336760 25200
#      Sat Jun 24 13:39:20 2017 -0700
# Node ID 3839334c22d631b956b036130387585c04f3b747
# Parent  9aad139f6e8778630347ac660ae93b9ad2cd2fe7
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.

Patch

diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -554,13 +554,24 @@  def shownamespaces(**args):
     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):
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
@@ -3894,10 +3894,31 @@  Test active bookmark templating
 
 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