changeset 23559:3b3a962e3677

namespaces: add a method to the first matching node for a given name
author Sean Farley <sean.michael.farley@gmail.com>
date Fri, 17 Oct 2014 15:28:40 -0700
parents 3198aac7a95d
children aead63705504
files mercurial/namespaces.py
diffstat 1 files changed, 20 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/namespaces.py	Sun Dec 14 12:53:50 2014 -0800
+++ b/mercurial/namespaces.py	Fri Oct 17 15:28:40 2014 -0700
@@ -1,3 +1,4 @@
+from i18n import _
 from mercurial import util
 import weakref
 
@@ -58,3 +59,22 @@
             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:
+                    cl = self.repo.changelog
+                    maxrev = max(cl.rev(node) for node in n)
+                    return cl.node(maxrev)
+                return n[0]
+        raise KeyError(_('no such name: %s') % name)