changeset 38490:4c0683655599

namespaces: let namespaces override singlenode() definition Some namespaces have multiple nodes per name (meaning that their namemap() returns multiple nodes). One such namespace is the "topics" namespace (from the evolve repo). We also have our own internal namespace at Google (for review units) that has multiple nodes per name. These namespaces may not want to use the default "pick highest revnum" resolution that we currently use when resolving a name to a single node. As an example, they may decide that `hg co <name>` should check out a commit that's last in some sense even if an earlier commit had just been amended and thus had a higher revnum [1]. This patch gives the namespace the option to continue to return multiple nodes and to override how the best node is picked. Allowing namespaces to override that may also be useful as an optimization (it may be cheaper for the namespace to find just that node). I have been arguing (in D3715) for using all the nodes returned from namemap() when resolving the symbol to a revset, so e.g. `hg log -r stable` would resolve to *all* nodes on stable, not just the one with the highest revnum (except that I don't actually think we should change it for the branch namespace because of BC). Most people seem opposed to that. If we decide not to do it, I think we can deprecate the namemap() function in favor of the new singlenode() (I find it weird to have namespaces, like the branch namespace, where namemap() isn't nodemap()'s inverse). I therefore think this patch makes sense regardless of what we decide on that issue. [1] Actually, even the branch namespace would have wanted to override singlenode() if it had supported multiple nodes. That's because closes branch heads are mostly ignored, so "hg co default" will not check out the highest-revnum node if that's a closed head. Differential Revision: https://phab.mercurial-scm.org/D3852
author Martin von Zweigbergk <martinvonz@google.com>
date Tue, 26 Jun 2018 10:02:01 -0700
parents 56b2074114b1
children 8d9d0d30cfcc
files mercurial/namespaces.py
diffstat 1 files changed, 24 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/namespaces.py	Wed Jun 27 12:24:21 2018 +0530
+++ b/mercurial/namespaces.py	Tue Jun 26 10:02:01 2018 -0700
@@ -95,21 +95,16 @@
 
     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.
+        Return the 'best' node for the given name. What's best is defined
+        by the namespace's singlenode() function. The first match returned by
+        a namespace in the defined precedence order is used.
 
         Raises a KeyError if there is no such node.
         """
         for ns, v in self._names.iteritems():
-            n = v.namemap(repo, name)
+            n = v.singlenode(repo, name)
             if n:
-                # return max revision number
-                if len(n) > 1:
-                    cl = repo.changelog
-                    maxrev = max(cl.rev(node) for node in n)
-                    return cl.node(maxrev)
-                return n[0]
+                return n
         raise KeyError(_('no such name: %s') % name)
 
 class namespace(object):
@@ -142,7 +137,7 @@
 
     def __init__(self, name, templatename=None, logname=None, colorname=None,
                  logfmt=None, listnames=None, namemap=None, nodemap=None,
-                 deprecated=None, builtin=False):
+                 deprecated=None, builtin=False, singlenode=None):
         """create a namespace
 
         name: the namespace to be registered (in plural form)
@@ -158,6 +153,7 @@
         nodemap: function that inputs a node, output name(s)
         deprecated: set of names to be masked for ordinary use
         builtin: whether namespace is implemented by core Mercurial
+        singlenode: function that inputs a name, output best node (or None)
         """
         self.name = name
         self.templatename = templatename
@@ -167,6 +163,8 @@
         self.listnames = listnames
         self.namemap = namemap
         self.nodemap = nodemap
+        if singlenode:
+            self.singlenode = singlenode
 
         # if logname is not specified, use the template name as backup
         if self.logname is None:
@@ -199,3 +197,18 @@
 
         """
         return sorted(self.namemap(repo, name))
+
+    def singlenode(self, repo, name):
+        """returns the best node for the given name
+
+        By default, the best node is the node from nodes() with the highest
+        revision number. It can be overriden by the namespace."""
+        n = self.namemap(repo, name)
+        if n:
+            # return max revision number
+            if len(n) > 1:
+                cl = repo.changelog
+                maxrev = max(cl.rev(node) for node in n)
+                return cl.node(maxrev)
+            return n[0]
+        return None