cat: default to working dir parent instead of tip
authorBrendan Cully <brendan@kublai.com>
Thu, 14 Sep 2006 19:24:00 -0700
changeset 3095 25857e00af8e
parent 3094 41e5ecfb6c24
child 3096 f422c8265ae5
cat: default to working dir parent instead of tip This introduces a defaultrev function that chooses the working dir parent if a revision isn't specified, and uses it in several places.
mercurial/commands.py
tests/test-confused-revert.out
tests/test-ssh
--- a/mercurial/commands.py	Fri Sep 15 00:51:27 2006 +0200
+++ b/mercurial/commands.py	Thu Sep 14 19:24:00 2006 -0700
@@ -50,6 +50,21 @@
                              (logfile, inst.strerror))
     return message
 
+def defaultrev(repo, rev=None, default='tip'):
+    """returns rev if it is specified, otherwise the working dir
+    parent if there is only one, or tip if there is no working
+    dir"""
+    if rev:
+        return rev
+
+    p1, p2 = repo.dirstate.parents()
+    if p2 != nullid:
+        raise util.Abort(_('uncommitted merge - please provide a '
+                           'specific revision'))
+    if p1 != nullid:
+        return hex(p1)
+    return default
+
 def walkchangerevs(ui, repo, pats, opts):
     '''Iterate over files and the revs they changed in.
 
@@ -99,13 +114,7 @@
         return [], False, matchfn
 
     if follow:
-        p = repo.dirstate.parents()[0]
-        if p == nullid:
-            ui.warn(_('No working directory revision; defaulting to tip\n'))
-            start = 'tip'
-        else:
-            start = repo.changelog.rev(p)
-        defrange = '%s:0' % start
+        defrange = '%s:0' % defaultrev(repo)
     else:
         defrange = 'tip:0'
     revs = map(int, cmdutil.revrange(ui, repo, opts['rev'] or [defrange]))
@@ -637,7 +646,7 @@
     if not opts['user'] and not opts['changeset'] and not opts['date']:
         opts['number'] = 1
 
-    ctx = repo.changectx(opts['rev'] or repo.dirstate.parents()[0])
+    ctx = repo.changectx(defaultrev(repo, opts['rev']))
 
     for src, abs, rel, exact in cmdutil.walk(repo, pats, opts,
                                              node=ctx.node()):
@@ -684,14 +693,7 @@
     The default is the basename of the archive, with suffixes removed.
     '''
 
-    if opts['rev']:
-        node = repo.lookup(opts['rev'])
-    else:
-        node, p2 = repo.dirstate.parents()
-        if p2 != nullid:
-            raise util.Abort(_('uncommitted merge - please provide a '
-                               'specific revision'))
-
+    node = repo.lookup(defaultrev(repo, opts['rev']))
     dest = cmdutil.make_filename(repo, dest, node)
     if os.path.realpath(dest) == repo.root:
         raise util.Abort(_('repository root cannot be destination'))
@@ -797,7 +799,8 @@
     """output the latest or given revisions of files
 
     Print the specified files as they were at the given revision.
-    If no revision is given then the tip is used.
+    If no revision is given then working dir parent is used, or tip
+    if no revision is checked out.
 
     Output may be to a file, in which case the name of the file is
     given using a format string.  The formatting rules are the same as
@@ -807,7 +810,7 @@
     %d   dirname of file being printed, or '.' if in repo root
     %p   root-relative path name of file being printed
     """
-    ctx = repo.changectx(opts['rev'] or "-1")
+    ctx = repo.changectx(defaultrev(repo, opts['rev']))
     for src, abs, rel, exact in cmdutil.walk(repo, (file1,) + pats, opts,
                                              ctx.node()):
         fp = cmdutil.make_file(repo, opts['output'], ctx.node(), pathname=abs)
@@ -2225,13 +2228,7 @@
                            'use --all to revert the whole repo'))
 
     parent, p2 = repo.dirstate.parents()
-    if opts['rev']:
-        node = repo.lookup(opts['rev'])
-    elif p2 != nullid:
-        raise util.Abort(_('working dir has two parents; '
-                           'you must specify the revision to revert to'))
-    else:
-        node = parent
+    node = repo.lookup(defaultrev(repo, opts['rev']))
     mf = repo.manifest.read(repo.changelog.read(node)[0])
     if node == parent:
         pmf = mf
@@ -2531,15 +2528,10 @@
             raise util.Abort(_("use only one form to specify the revision"))
     if opts['rev']:
         rev_ = opts['rev']
-    if rev_:
-        r = repo.lookup(rev_)
-    else:
-        p1, p2 = repo.dirstate.parents()
-        if p1 == nullid:
-            raise util.Abort(_('no revision to tag'))
-        if p2 != nullid:
-            raise util.Abort(_('outstanding uncommitted merges'))
-        r = p1
+    r = defaultrev(repo, rev_, nullid)
+    if r == nullid:
+        raise util.Abort(_('no revision to tag'))
+    r = repo.lookup(r)
 
     message = opts['message']
     if not message:
--- a/tests/test-confused-revert.out	Fri Sep 15 00:51:27 2006 +0200
+++ b/tests/test-confused-revert.out	Thu Sep 14 19:24:00 2006 -0700
@@ -17,7 +17,7 @@
 A b
 R a
 %%% revert should fail
-abort: working dir has two parents; you must specify the revision to revert to
+abort: uncommitted merge - please provide a specific revision
 %%% revert should be ok now
 undeleting a
 forgetting b
--- a/tests/test-ssh	Fri Sep 15 00:51:27 2006 +0200
+++ b/tests/test-ssh	Thu Sep 14 19:24:00 2006 -0700
@@ -80,7 +80,7 @@
 echo "# check remote tip"
 hg tip
 hg verify
-hg cat foo
+hg cat -r tip foo
 
 echo z > z
 hg ci -A -m z -d '1000001 0' z