shortest: don't keep checking for longer prefix if node doesn't exist (API)
If revlog.shortest() is called with an invalid nodeid, we keep
checking if longer and longer prefixes are valid. We call
revlog._partialmatch() for each prefix. That function will give us
None if the node doesn't exist (and a RevlogError if it's ambiguous),
so there's no need to keep checking.
This patch instead makes revlog.shortest() raise a LookupError is the
node does not exist, and updates the caller to handle it. Before this
patch, revlog.shortest() would return the full hexnode for nonexistent
nodeids. By the same reasoning as in 7b2955624777 (scmutil: make
shortesthexnodeidprefix() take a full binary nodeid, 2018-04-14), it's
not revlog.shortest() that should decide how to present nonexistent
nodeids, so that's now moved to the template function.
This should speed up cases where {shortest()} is applied to an invalid
nodeid, but I couldn't think of a reasonable case where that would
happen.
Differential Revision: https://phab.mercurial-scm.org/D3461
--- a/mercurial/revlog.py Wed May 02 22:56:10 2018 -0700
+++ b/mercurial/revlog.py Sat May 05 00:16:43 2018 -0700
@@ -1516,13 +1516,14 @@
def isvalid(prefix):
try:
- if self._partialmatch(prefix) is None:
- return False
+ node = self._partialmatch(prefix)
except error.RevlogError:
return False
except error.WdirUnsupported:
# single 'ff...' match
return True
+ if node is None:
+ raise LookupError(node, self.indexfile, _('no node'))
return not isrev(prefix)
hexnode = hex(node)
--- a/mercurial/scmutil.py Wed May 02 22:56:10 2018 -0700
+++ b/mercurial/scmutil.py Sat May 05 00:16:43 2018 -0700
@@ -448,7 +448,10 @@
# _partialmatch() of filtered changelog could take O(len(repo)) time,
# which would be unacceptably slow. so we look for hash collision in
# unfiltered space, which means some hashes may be slightly longer.
- return repo.unfiltered().changelog.shortest(node, minlength)
+ try:
+ return repo.unfiltered().changelog.shortest(node, minlength)
+ except error.LookupError:
+ raise error.RepoLookupError()
def isrevsymbol(repo, symbol):
"""Checks if a symbol exists in the repo.
--- a/mercurial/templatefuncs.py Wed May 02 22:56:10 2018 -0700
+++ b/mercurial/templatefuncs.py Sat May 05 00:16:43 2018 -0700
@@ -608,7 +608,10 @@
return hexnode
if not node:
return hexnode
- return scmutil.shortesthexnodeidprefix(repo, node, minlength)
+ try:
+ return scmutil.shortesthexnodeidprefix(repo, node, minlength)
+ except error.RepoLookupError:
+ return hexnode
@templatefunc('strip(text[, chars])')
def strip(context, mapping, args):