# HG changeset patch # User Sean Farley # Date 1413584920 25200 # Node ID 3b3a962e3677884205c9f8acfe21e3f2ed592e08 # Parent 3198aac7a95de5bf6e7a13be08a4ce60d7380620 namespaces: add a method to the first matching node for a given name diff -r 3198aac7a95d -r 3b3a962e3677 mercurial/namespaces.py --- 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)