Patchwork [4,of,4] templatekw: add {namespaces} keyword

login
register
mail settings
Submitter Yuya Nishihara
Date Jan. 16, 2016, 12:47 p.m.
Message ID <bd2f040cf01bb0b64526.1452948473@mimosa>
Download mbox | patch
Permalink /patch/12797/
State Accepted
Headers show

Comments

Yuya Nishihara - Jan. 16, 2016, 12:47 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1452610608 -32400
#      Tue Jan 12 23:56:48 2016 +0900
# Node ID bd2f040cf01bb0b64526578be04ac863b10de833
# Parent  ba3ea5de17b6e47f6d004d239c5acfe6a408eef6
templatekw: add {namespaces} keyword

This provides a general-purpose interface to all custom namespaces.

The {namespaces} keyword honors the definition order of namespaces as they
are kept by sortdict.
Matt Mackall - Jan. 18, 2016, 1:39 a.m.
On Sat, 2016-01-16 at 21:47 +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1452610608 -32400
> #      Tue Jan 12 23:56:48 2016 +0900
> # Node ID bd2f040cf01bb0b64526578be04ac863b10de833
> # Parent  ba3ea5de17b6e47f6d004d239c5acfe6a408eef6
> templatekw: add {namespaces} keyword

These are queued for default, thanks.

-- 
Mathematics is the supreme nostalgia of our time.

Patch

diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -421,6 +421,19 @@  def shownames(namespace, **args):
     names = ns.names(repo, ctx.node())
     return showlist(ns.templatename, names, plural=namespace, **args)
 
+def shownamespaces(**args):
+    """:namespaces: Dict of lists. Names attached to this changeset per
+    namespace."""
+    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())
+    f = _showlist('namespace', list(namespaces), **args)
+    return _hybrid(f, namespaces,
+                   lambda k: {'namespace': k, 'names': namespaces[k]},
+                   lambda x: x['namespace'])
+
 def shownode(repo, ctx, templ, **args):
     """:node: String. The changeset identification hash, as a 40 hexadecimal
     digit string.
@@ -537,6 +550,7 @@  keywords = {
     'latesttag': showlatesttag,
     'latesttagdistance': showlatesttagdistance,
     'manifest': showmanifest,
+    'namespaces': shownamespaces,
     'node': shownode,
     'p1rev': showp1rev,
     'p1node': showp1node,
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
@@ -3304,6 +3304,28 @@  Test active bookmark templating
   1 f
   0 f
 
+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 -r2 -T '{namespaces % "{namespace}: {names}\n"}'
+  bookmarks: bar foo
+  tags: tip
+  branches: text.{rev}
+  $ hg log -r2 -T '{namespaces % "{namespace}:\n{names % " {name}\n"}"}'
+  bookmarks:
+   bar
+   foo
+  tags:
+   tip
+  branches:
+   text.{rev}
+  $ hg log -r2 -T '{get(namespaces, "bookmarks") % "{name}\n"}'
+  bar
+  foo
+
 Test stringify on sub expressions
 
   $ cd ..