namespaces: record and expose whether namespace is built-in
authorGregory Szorc <gregory.szorc@gmail.com>
Sat, 24 Jun 2017 14:52:15 -0700
changeset 33048 46fa46608ca5
parent 33047 de8e3681c402
child 33049 0b42c7ba46a6
namespaces: record and expose whether namespace is built-in Currently, the templating layer tends to treat each namespace as a one-off, with explicit usage of {bookmarks}, {tags}, {branch}, etc instead of using {namespaces}. It would be really useful if we could iterate over namespaces and operate on them generically. However, some consumers may wish to differentiate namespaces by whether they are built-in to core Mercurial or provided by extensions. Expected use cases include ignoring non-built-in namespaces or emitting a generic label for non-built-in namespaces. This commit introduces an attribute on namespace instances that says whether the namespace is "built-in" and then exposes this to the templating layer. As part of this, we implement a reusable extension for defining custom names on each changeset for testing. A second consumer will be introduced in a subsequent commit.
mercurial/namespaces.py
mercurial/templatekw.py
tests/revnamesext.py
tests/test-command-template.t
--- a/mercurial/namespaces.py	Sat Jun 24 13:39:20 2017 -0700
+++ b/mercurial/namespaces.py	Sat Jun 24 14:52:15 2017 -0700
@@ -35,7 +35,8 @@
                       # i18n: column positioning for "hg log"
                       logfmt=_("bookmark:    %s\n"),
                       listnames=bmknames,
-                      namemap=bmknamemap, nodemap=bmknodemap)
+                      namemap=bmknamemap, nodemap=bmknodemap,
+                      builtin=True)
         self.addnamespace(n)
 
         tagnames = lambda repo: [t for t, n in repo.tagslist()]
@@ -46,7 +47,8 @@
                       logfmt=_("tag:         %s\n"),
                       listnames=tagnames,
                       namemap=tagnamemap, nodemap=tagnodemap,
-                      deprecated={'tip'})
+                      deprecated={'tip'},
+                      builtin=True)
         self.addnamespace(n)
 
         bnames = lambda repo: repo.branchmap().keys()
@@ -56,7 +58,8 @@
                       # i18n: column positioning for "hg log"
                       logfmt=_("branch:      %s\n"),
                       listnames=bnames,
-                      namemap=bnamemap, nodemap=bnodemap)
+                      namemap=bnamemap, nodemap=bnodemap,
+                      builtin=True)
         self.addnamespace(n)
 
     def __getitem__(self, namespace):
@@ -134,12 +137,13 @@
       'namemap': function that takes a name and returns a list of nodes
       'nodemap': function that takes a node and returns a list of names
       'deprecated': set of names to be masked for ordinary use
-
+      'builtin': bool indicating if this namespace is supported by core
+                 Mercurial.
     """
 
     def __init__(self, name, templatename=None, logname=None, colorname=None,
                  logfmt=None, listnames=None, namemap=None, nodemap=None,
-                 deprecated=None):
+                 deprecated=None, builtin=False):
         """create a namespace
 
         name: the namespace to be registered (in plural form)
@@ -154,7 +158,7 @@
         namemap: function that inputs a name, output node(s)
         nodemap: function that inputs a node, output name(s)
         deprecated: set of names to be masked for ordinary use
-
+        builtin: whether namespace is implemented by core Mercurial
         """
         self.name = name
         self.templatename = templatename
@@ -183,6 +187,8 @@
         else:
             self.deprecated = deprecated
 
+        self.builtin = builtin
+
     def names(self, repo, node):
         """method that returns a (sorted) list of names in a namespace that
         match a given node"""
--- a/mercurial/templatekw.py	Sat Jun 24 13:39:20 2017 -0700
+++ b/mercurial/templatekw.py	Sat Jun 24 14:52:15 2017 -0700
@@ -557,10 +557,12 @@
 
     namespaces = util.sortdict()
     colornames = {}
+    builtins = {}
 
     for k, ns in repo.names.iteritems():
         namespaces[k] = showlist('name', ns.names(repo, ctx.node()), args)
         colornames[k] = ns.colorname
+        builtins[k] = ns.builtin
 
     f = _showlist('namespace', list(namespaces), args)
 
@@ -568,6 +570,7 @@
         return {
             'namespace': ns,
             'names': namespaces[ns],
+            'builtin': builtins[ns],
             'colorname': colornames[ns],
         }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/revnamesext.py	Sat Jun 24 14:52:15 2017 -0700
@@ -0,0 +1,18 @@
+# Dummy extension to define a namespace containing revision names
+
+from __future__ import absolute_import
+
+from mercurial import (
+    namespaces,
+)
+
+def reposetup(ui, repo):
+    names = {'r%d' % rev: repo[rev].node() for rev in repo}
+    namemap = lambda r, name: names.get(name)
+    nodemap = lambda r, node: ['r%d' % repo[node].rev()]
+
+    ns = namespaces.namespace('revnames', templatename='revname',
+                              logname='revname',
+                              listnames=lambda r: names.keys(),
+                              namemap=namemap, nodemap=nodemap)
+    repo.names.addnamespace(ns)
--- a/tests/test-command-template.t	Sat Jun 24 13:39:20 2017 -0700
+++ b/tests/test-command-template.t	Sat Jun 24 14:52:15 2017 -0700
@@ -3894,30 +3894,36 @@
 
 Test namespaces dict
 
-  $ hg log -T '{rev}\n{namespaces % " {namespace} color={colorname}\n  {join(names, ",")}\n"}\n'
+  $ hg --config extensions.revnamesext=$TESTDIR/revnamesext.py log -T '{rev}\n{namespaces % " {namespace} color={colorname} builtin={builtin}\n  {join(names, ",")}\n"}\n'
   2
-   bookmarks color=bookmark
+   bookmarks color=bookmark builtin=True
     bar,foo
-   tags color=tag
+   tags color=tag builtin=True
     tip
-   branches color=branch
+   branches color=branch builtin=True
     text.{rev}
+   revnames color=revname builtin=False
+    r2
   
   1
-   bookmarks color=bookmark
+   bookmarks color=bookmark builtin=True
     baz
-   tags color=tag
+   tags color=tag builtin=True
     
-   branches color=branch
+   branches color=branch builtin=True
     text.{rev}
+   revnames color=revname builtin=False
+    r1
   
   0
-   bookmarks color=bookmark
+   bookmarks color=bookmark builtin=True
+    
+   tags color=tag builtin=True
     
-   tags color=tag
-    
-   branches color=branch
+   branches color=branch builtin=True
     default
+   revnames color=revname builtin=False
+    r0
   
   $ hg log -r2 -T '{namespaces % "{namespace}: {names}\n"}'
   bookmarks: bar foo