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

login
register
mail settings
Submitter Sean Farley
Date Dec. 17, 2014, 12:02 a.m.
Message ID <957096b720f355369f56.1418774520@laptop.local>
Download mbox | patch
Permalink /patch/7135/
State Superseded
Commit 9266d1dd6a6edda6d3fdaab3008d73c4ff9568a6
Headers show

Comments

Sean Farley - Dec. 17, 2014, 12:02 a.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 957096b720f355369f56ceebd088a11b7ed37ebc
# Parent  3c155bf2bd32e0b719f6fe4dc66c635b2e87b40e
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 % "BABAR: {babar}\n"}'
Pierre-Yves David - Dec. 17, 2014, 6:58 a.m.
On 12/16/2014 04:02 PM, Sean Farley wrote:
> # HG changeset patch
> # User Sean Farley <sean.michael.farley@gmail.com>
> # Date 1413526749 25200
> #      Thu Oct 16 23:19:09 2014 -0700
> # Node ID 957096b720f355369f56ceebd088a11b7ed37ebc
> # Parent  3c155bf2bd32e0b719f6fe4dc66c635b2e87b40e
> 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 % "BABAR: {babar}\n"}'
>
> 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,18 @@ class namespaces(object):
>           if order is not None:
>               self._names.insert(order, namespace, val)
>           else:
>               self._names[namespace] = val
>
> +        # create template keyword for anything but branches and bookmarks,
> +        # since those have special logic for 'current' and 'default'
> +        if namespace not in ('bookmarks', 'branches'):
> +            def generatekw(**args):
> +                return templatekw.shownames(namespace, **args)
> +
> +            templatekw.keywords[namespace] = generatekw

Instead of adding special case to avoid over-writing bookmrks and 
branchs, we could automatically avoid adding new namespace if they 
already exists. This would avoid issue with possible extensions and 
running this multiple times.
Sean Farley - Dec. 17, 2014, 7:20 a.m.
Pierre-Yves David writes:

> On 12/16/2014 04:02 PM, Sean Farley wrote:
>> # HG changeset patch
>> # User Sean Farley <sean.michael.farley@gmail.com>
>> # Date 1413526749 25200
>> #      Thu Oct 16 23:19:09 2014 -0700
>> # Node ID 957096b720f355369f56ceebd088a11b7ed37ebc
>> # Parent  3c155bf2bd32e0b719f6fe4dc66c635b2e87b40e
>> 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 % "BABAR: {babar}\n"}'
>>
>> 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,18 @@ class namespaces(object):
>>           if order is not None:
>>               self._names.insert(order, namespace, val)
>>           else:
>>               self._names[namespace] = val
>>
>> +        # create template keyword for anything but branches and bookmarks,
>> +        # since those have special logic for 'current' and 'default'
>> +        if namespace not in ('bookmarks', 'branches'):
>> +            def generatekw(**args):
>> +                return templatekw.shownames(namespace, **args)
>> +
>> +            templatekw.keywords[namespace] = generatekw
>
> Instead of adding special case to avoid over-writing bookmrks and 
> branchs, we could automatically avoid adding new namespace if they 
> already exists. This would avoid issue with possible extensions and 
> running this multiple times.

Yep, that's a good idea.

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,18 @@  class namespaces(object):
         if order is not None:
             self._names.insert(order, namespace, val)
         else:
             self._names[namespace] = val
 
+        # create template keyword for anything but branches and bookmarks,
+        # since those have special logic for 'current' and 'default'
+        if namespace not in ('bookmarks', 'branches'):
+            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.