Patchwork [7,of,8] namespaces: add a method to the first matching node for a given name

login
register
mail settings
Submitter Sean Farley
Date Dec. 15, 2014, 12:37 a.m.
Message ID <85c150b347a9fc6c7178.1418603875@laptop.local>
Download mbox | patch
Permalink /patch/7100/
State Accepted
Headers show

Comments

Sean Farley - Dec. 15, 2014, 12:37 a.m.
# HG changeset patch
# User Sean Farley <sean.michael.farley@gmail.com>
# Date 1413584920 25200
#      Fri Oct 17 15:28:40 2014 -0700
# Node ID 85c150b347a9fc6c717864ba4f7e2d60812b5fad
# Parent  f00702f9fa2d70ae1bf8e246cf86e42f022afa06
namespaces: add a method to the first matching node for a given name
Pierre-Yves David - Dec. 15, 2014, 1:01 a.m.
On 12/14/2014 04:37 PM, Sean Farley wrote:
> # HG changeset patch
> # User Sean Farley <sean.michael.farley@gmail.com>
> # Date 1413584920 25200
> #      Fri Oct 17 15:28:40 2014 -0700
> # Node ID 85c150b347a9fc6c717864ba4f7e2d60812b5fad
> # Parent  f00702f9fa2d70ae1bf8e246cf86e42f022afa06
> namespaces: add a method to the first matching node for a given name
>
> diff --git a/mercurial/namespaces.py b/mercurial/namespaces.py
> --- a/mercurial/namespaces.py
> +++ b/mercurial/namespaces.py
> @@ -1,5 +1,6 @@
> +from i18n import _
>   from mercurial import util
>   import weakref
>
>   def multify(val):
>       """
> @@ -83,5 +84,23 @@ class namespaces(object):
>                  'nodemap': nodemap}
>           if order is not None:
>               self._names.insert(order, namespace, val)
>           else:
>               self._names[namespace] = val
> +
> +    def singlenode(self, 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.
> +
> +        Raises a KeyError if there is no such node.
> +        """
> +        for ns, v in self._names.iteritems():
> +            n = v['namemap'](self.repo, name)
> +            if n:
> +                # return max revision number
> +                if len(n) > 1:
> +                    maxrev = max([self.repo[node].rev() for node in n])
> +                    return self.repo[maxrev].node()

you should skip the part where you call self[repo].rev.

using changelog.rev() will be much more efficient.
Similarly, changelog.node() will be better.

Patch

diff --git a/mercurial/namespaces.py b/mercurial/namespaces.py
--- a/mercurial/namespaces.py
+++ b/mercurial/namespaces.py
@@ -1,5 +1,6 @@ 
+from i18n import _
 from mercurial import util
 import weakref
 
 def multify(val):
     """
@@ -83,5 +84,23 @@  class namespaces(object):
                'nodemap': nodemap}
         if order is not None:
             self._names.insert(order, namespace, val)
         else:
             self._names[namespace] = val
+
+    def singlenode(self, 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.
+
+        Raises a KeyError if there is no such node.
+        """
+        for ns, v in self._names.iteritems():
+            n = v['namemap'](self.repo, name)
+            if n:
+                # return max revision number
+                if len(n) > 1:
+                    maxrev = max([self.repo[node].rev() for node in n])
+                    return self.repo[maxrev].node()
+                return n[0]
+        raise KeyError(_('no such name: %s') % name)