D3852: namespaces: let namespaces override singlenode() definition

martinvonz (Martin von Zweigbergk) phabricator at mercurial-scm.org
Thu Jun 28 05:13:22 UTC 2018


martinvonz created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  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 https://phab.mercurial-scm.org/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.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3852

AFFECTED FILES
  mercurial/namespaces.py

CHANGE DETAILS

diff --git a/mercurial/namespaces.py b/mercurial/namespaces.py
--- a/mercurial/namespaces.py
+++ b/mercurial/namespaces.py
@@ -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,18 @@
         self.listnames = listnames
         self.namemap = namemap
         self.nodemap = nodemap
+        if not singlenode:
+            def singlenode(repo, name):
+                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
+        self.singlenode = singlenode
 
         # if logname is not specified, use the template name as backup
         if self.logname is None:



To: martinvonz, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list