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

Sean Farley sean.michael.farley at gmail.com
Sun Dec 14 18:37:55 CST 2014


# HG changeset patch
# User Sean Farley <sean.michael.farley at 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()
+                return n[0]
+        raise KeyError(_('no such name: %s') % name)


More information about the Mercurial-devel mailing list