D4040: shortest: make isrev() a top-level function

martinvonz (Martin von Zweigbergk) phabricator at mercurial-scm.org
Wed Aug 1 21:58:48 UTC 2018


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

REVISION SUMMARY
  I'm going to add another caller in the next patch.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  mercurial/scmutil.py

CHANGE DETAILS

diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -462,33 +462,34 @@
     repo.changelog.rev(node)  # make sure node isn't filtered
     return node
 
+def mayberevnum(repo, prefix):
+    """Checks if the given prefix may be mistaken for a revision number"""
+    try:
+        i = int(prefix)
+        # if we are a pure int, then starting with zero will not be
+        # confused as a rev; or, obviously, if the int is larger
+        # than the value of the tip rev
+        if prefix[0:1] == b'0' or i > len(repo.changelog):
+            return False
+        return True
+    except ValueError:
+        return False
+
 def shortesthexnodeidprefix(repo, node, minlength=1):
     """Find the shortest unambiguous prefix that matches hexnode."""
     # _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.
-    cl = repo.unfiltered().changelog
-
-    def isrev(prefix):
-        try:
-            i = int(prefix)
-            # if we are a pure int, then starting with zero will not be
-            # confused as a rev; or, obviously, if the int is larger
-            # than the value of the tip rev
-            if prefix[0:1] == b'0' or i > len(cl):
-                return False
-            return True
-        except ValueError:
-            return False
 
     def disambiguate(prefix):
         """Disambiguate against revnums."""
         hexnode = hex(node)
         for length in range(len(prefix), len(hexnode) + 1):
             prefix = hexnode[:length]
-            if not isrev(prefix):
+            if not mayberevnum(repo, prefix):
                 return prefix
 
+    cl = repo.unfiltered().changelog
     revset = repo.ui.config('experimental', 'revisions.disambiguatewithin')
     if revset:
         if not util.safehasattr(repo, '_cacheddisambiguationrevs'):



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


More information about the Mercurial-devel mailing list