Patchwork [6,of,7,V2] namespaces: generate template keyword when registering a namespace

login
register
mail settings
Submitter Sean Farley
Date Dec. 18, 2014, 9:10 p.m.
Message ID <61be8fb2ece5b5a7f0ca.1418937050@laptop.local>
Download mbox | patch
Permalink /patch/7178/
State Accepted
Headers show

Comments

Sean Farley - Dec. 18, 2014, 9:10 p.m.
# HG changeset patch
# User Sean Farley <sean.michael.farley@gmail.com>
# Date 1413526749 25200
#      Thu Oct 16 23:19:09 2014 -0700
# Node ID 61be8fb2ece5b5a7f0ca6b5ecaa4c274c7e0190e
# Parent  077665448c9df324d59f06d905dd743bc7ae01e3
namespaces: generate template keyword when registering a namespace

For any namespace, we generate a template keyword. For example, given a
namespace 'babar', we automatically have the ability to use it in a template:

hg log -r . -T '{babars % "King: {babar}\n"}'

Furthermore, we only generate this keyword for a namespace if one doesn't
already exist. This is necessary for 'branches' and 'bookmarks' since both of
those have concepts of 'current' (something outside the namespace api) and also
allows extensions to override default behavior if desired.

Patch

diff --git a/mercurial/namespaces.py b/mercurial/namespaces.py
--- a/mercurial/namespaces.py
+++ b/mercurial/namespaces.py
@@ -1,7 +1,8 @@ 
 from i18n import _
 from mercurial import util
+import templatekw
 
 def tolist(val):
     """
     a convenience method to return an empty list instead of None
     """
@@ -72,10 +73,17 @@  class namespaces(object):
         if order is not None:
             self._names.insert(order, namespace, val)
         else:
             self._names[namespace] = val
 
+        # we only generate a template keyword if one does not already exist
+        if namespace not in templatekw.keywords:
+            def generatekw(**args):
+                return templatekw.shownames(namespace, **args)
+
+            templatekw.keywords[namespace] = generatekw
+
     def singlenode(self, repo, name):
         """
         Return the 'best' node for the given name. Best means the first node
         in the first nonempty list returned by a name-to-nodes mapping function
         in the defined precedence order.