simplify revrange and revpair
authorMatt Mackall <mpm@selenic.com>
Thu, 26 Oct 2006 17:15:55 -0500
changeset 3525 cf0f8d9256c7
parent 3524 a33a9af1ec03
child 3526 68341c06bc61
simplify revrange and revpair
mercurial/cmdutil.py
--- a/mercurial/cmdutil.py	Thu Oct 26 14:09:15 2006 -0500
+++ b/mercurial/cmdutil.py	Thu Oct 26 17:15:55 2006 -0500
@@ -13,43 +13,25 @@
 
 revrangesep = ':'
 
-def revfix(repo, val, defval):
-    '''turn user-level id of changeset into rev number.
-    user-level id can be tag, changeset, rev number, or negative rev
-    number relative to number of revs (-1 is tip, etc).'''
-    if not val:
-        return defval
-    try:
-        num = int(val)
-        if str(num) != val:
-            raise ValueError
-        if num < 0:
-            num += repo.changelog.count()
-        if num < 0:
-            num = 0
-        elif num >= repo.changelog.count():
-            raise ValueError
-    except ValueError:
-        try:
-            num = repo.changelog.rev(repo.lookup(val))
-        except KeyError:
-            raise util.Abort(_('invalid revision identifier %s') % val)
-    return num
-
 def revpair(ui, repo, revs):
     '''return pair of nodes, given list of revisions. second item can
     be None, meaning use working dir.'''
+
+    def revfix(repo, val, defval):
+        if not val and val != 0:
+            val = defval
+        return repo.lookup(val)
+
     if not revs:
         return repo.dirstate.parents()[0], None
     end = None
     if len(revs) == 1:
-        start = revs[0]
-        if revrangesep in start:
-            start, end = start.split(revrangesep, 1)
+        if revrangesep in revs[0]:
+            start, end = revs[0].split(revrangesep, 1)
             start = revfix(repo, start, 0)
             end = revfix(repo, end, repo.changelog.count() - 1)
         else:
-            start = revfix(repo, start, None)
+            start = revfix(repo, revs[0], None)
     elif len(revs) == 2:
         if revrangesep in revs[0] or revrangesep in revs[1]:
             raise util.Abort(_('too many revisions specified'))
@@ -57,11 +39,16 @@
         end = revfix(repo, revs[1], None)
     else:
         raise util.Abort(_('too many revisions specified'))
-    if end is not None: end = repo.lookup(end)
-    return repo.lookup(start), end
+    return start, end
 
 def revrange(ui, repo, revs):
     """Yield revision as strings from a list of revision specifications."""
+
+    def revfix(repo, val, defval):
+        if not val and val != 0:
+            return defval
+        return repo.changelog.rev(repo.lookup(val))
+
     seen = {}
     for spec in revs:
         if revrangesep in spec:
@@ -73,13 +60,13 @@
                 if rev in seen:
                     continue
                 seen[rev] = 1
-                yield str(rev)
+                yield rev
         else:
             rev = revfix(repo, spec, None)
             if rev in seen:
                 continue
             seen[rev] = 1
-            yield str(rev)
+            yield rev
 
 def make_filename(repo, pat, node,
                   total=None, seqno=None, revwidth=None, pathname=None):