diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -50,6 +50,21 @@ def logmessage(opts): (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 @@ def walkchangerevs(ui, repo, pats, opts) 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 @@ def annotate(ui, repo, *pats, **opts): 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 @@ def archive(ui, repo, dest, **opts): 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 @@ def cat(ui, repo, file1, *pats, **opts): """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 @@ def cat(ui, repo, file1, *pats, **opts): %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 @@ def revert(ui, repo, *pats, **opts): '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 @@ def tag(ui, repo, name, rev_=None, **opt 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: