diff: allow to use -rA:B syntax as well as -rA -rB
authorVadim Gelfer <vadim.gelfer@gmail.com>
Sun, 21 May 2006 23:07:19 -0700
changeset 2331 953dbfb2824c
parent 2330 b30aa02c85e7
child 2332 d5c9ba87a738
child 2333 de0c05afa511
diff: allow to use -rA:B syntax as well as -rA -rB
mercurial/commands.py
--- a/mercurial/commands.py	Sun May 21 22:14:11 2006 -0700
+++ b/mercurial/commands.py	Sun May 21 23:07:19 2006 -0700
@@ -179,39 +179,61 @@
 
 revrangesep = ':'
 
-def revrange(ui, repo, revs, revlog=None):
-    """Yield revision as strings from a list of revision specifications."""
-    if revlog is None:
-        revlog = repo.changelog
-    revcount = revlog.count()
-    def fix(val, defval):
-        if not val:
-            return defval
+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 = int(val)
-            if str(num) != val:
-                raise ValueError
-            if num < 0:
-                num += revcount
-            if num < 0:
-                num = 0
-            elif num >= revcount:
-                raise ValueError
-        except ValueError:
-            try:
-                num = repo.changelog.rev(repo.lookup(val))
-            except KeyError:
-                try:
-                    num = revlog.rev(revlog.lookup(val))
-                except KeyError:
-                    raise util.Abort(_('invalid revision identifier %s'), val)
-        return num
+            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.'''
+    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)
+            start = revfix(repo, start, 0)
+            end = revfix(repo, end, repo.changelog.count() - 1)
+        else:
+            start = revfix(repo, start, None)
+    elif len(revs) == 2:
+        if revrangesep in revs[0] or revrangesep in revs[1]:
+            raise util.Abort(_('too many revisions specified'))
+        start = revfix(repo, revs[0], None)
+        end = revfix(repo, revs[1], None)
+    else:
+        raise util.Abort(_('too many revisions specified'))
+    if end is not None: end = repo.lookup(str(end))
+    return repo.lookup(str(start)), end
+
+def revrange(ui, repo, revs):
+    """Yield revision as strings from a list of revision specifications."""
     seen = {}
     for spec in revs:
         if spec.find(revrangesep) >= 0:
             start, end = spec.split(revrangesep, 1)
-            start = fix(start, 0)
-            end = fix(end, revcount - 1)
+            start = revfix(repo, start, 0)
+            end = revfix(repo, end, repo.changelog.count() - 1)
             step = start > end and -1 or 1
             for rev in xrange(start, end+step, step):
                 if rev in seen:
@@ -219,7 +241,7 @@
                 seen[rev] = 1
                 yield str(rev)
         else:
-            rev = fix(spec, None)
+            rev = revfix(repo, spec, None)
             if rev in seen:
                 continue
             seen[rev] = 1
@@ -1361,15 +1383,7 @@
     it detects as binary. With -a, diff will generate a diff anyway,
     probably with undesirable results.
     """
-    node1, node2 = None, None
-    revs = [repo.lookup(x) for x in opts['rev']]
-
-    if len(revs) > 0:
-        node1 = revs[0]
-    if len(revs) > 1:
-        node2 = revs[1]
-    if len(revs) > 2:
-        raise util.Abort(_("too many revisions to diff"))
+    node1, node2 = revpair(ui, repo, opts['rev'])
 
     fns, matchfn, anypats = matchpats(repo, pats, opts)