Patchwork D3460: shortest: extract function for checking if a prefix is a revnum

login
register
mail settings
Submitter phabricator
Date May 7, 2018, 4:56 p.m.
Message ID <differential-rev-PHID-DREV-ijrgw7lrnneqzguir4on-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/31311/
State Superseded
Headers show

Comments

phabricator - May 7, 2018, 4:56 p.m.
martinvonz created this revision.
Herald added a reviewer: indygreg.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  Much of isvalid() was about testing if a prefix is a valid revnum. I
  want to reuse that soon, so let's move it out.
  
  There is no significant slowdown from the function call overhead.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  mercurial/revlog.py

CHANGE DETAILS




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

Patch

diff --git a/mercurial/revlog.py b/mercurial/revlog.py
--- a/mercurial/revlog.py
+++ b/mercurial/revlog.py
@@ -1502,6 +1502,18 @@ 
 
     def shortest(self, node, minlength=1):
         """Find the shortest unambiguous prefix that matches node."""
+        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] == '0' or i > len(self):
+                    return False
+                return True
+            except ValueError:
+                return False
+
         def isvalid(prefix):
             try:
                 if self._partialmatch(prefix) is None:
@@ -1511,16 +1523,7 @@ 
             except error.WdirUnsupported:
                 # single 'ff...' match
                 return True
-            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] == '0' or i > len(self):
-                    return True
-                return False
-            except ValueError:
-                return True
+            return not isrev(prefix)
 
         hexnode = hex(node)
         shortest = hexnode