# HG changeset patch # User Brendan Cully # Date 1158359032 25200 # Node ID 81da3c45aabd8a3a990fdaa41720eb0dcc5ef891 # Parent cff3c58a576689ad0d7a5a76829f98d9d97515b5 Move defaultrev into changectx This also causes tag on a repository with no working directory to default to tip. diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -50,21 +50,6 @@ 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. @@ -114,7 +99,7 @@ def walkchangerevs(ui, repo, pats, opts) return [], False, matchfn if follow: - defrange = '%s:0' % defaultrev(repo) + defrange = '%s:0' % repo.changectx().rev() else: defrange = 'tip:0' revs = map(int, cmdutil.revrange(ui, repo, opts['rev'] or [defrange])) @@ -646,7 +631,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(defaultrev(repo, opts['rev'])) + ctx = repo.changectx(opts['rev']) for src, abs, rel, exact in cmdutil.walk(repo, pats, opts, node=ctx.node()): @@ -693,7 +678,7 @@ def archive(ui, repo, dest, **opts): The default is the basename of the archive, with suffixes removed. ''' - node = repo.lookup(defaultrev(repo, opts['rev'])) + node = repo.changectx(opts['rev']).node() dest = cmdutil.make_filename(repo, dest, node) if os.path.realpath(dest) == repo.root: raise util.Abort(_('repository root cannot be destination')) @@ -810,7 +795,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(defaultrev(repo, opts['rev'])) + ctx = repo.changectx(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) @@ -2228,7 +2213,10 @@ def revert(ui, repo, *pats, **opts): 'use --all to revert the whole repo')) parent, p2 = repo.dirstate.parents() - node = repo.lookup(defaultrev(repo, opts['rev'])) + if not opts['rev'] and p2 != nullid: + raise util.Abort(_('uncommitted merge - please provide a ' + 'specific revision')) + node = repo.changectx(opts['rev']).node() mf = repo.manifest.read(repo.changelog.read(node)[0]) if node == parent: pmf = mf @@ -2528,10 +2516,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'] - r = defaultrev(repo, rev_, nullid) - if r == nullid: - raise util.Abort(_('no revision to tag')) - r = repo.lookup(r) + if not rev_ and repo.dirstate.parents()[1] != nullid: + raise util.Abort(_('uncommitted merge - please provide a ' + 'specific revision')) + r = repo.changectx(rev_).node() message = opts['message'] if not message: diff --git a/mercurial/context.py b/mercurial/context.py --- a/mercurial/context.py +++ b/mercurial/context.py @@ -8,10 +8,19 @@ class changectx(object): """A changecontext object makes access to data related to a particular changeset convenient.""" - def __init__(self, repo, changeid): + def __init__(self, repo, changeid=None): """changeid is a revision number, node, or tag""" self._repo = repo + if not changeid: + p1, p2 = self._repo.dirstate.parents() + self._rev = self._repo.changelog.rev(p1) + if self._rev == -1: + changeid = 'tip' + else: + self._node = p1 + return + self._node = self._repo.lookup(changeid) self._rev = self._repo.changelog.rev(self._node) diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py --- a/mercurial/localrepo.py +++ b/mercurial/localrepo.py @@ -318,7 +318,7 @@ class localrepository(repo.repository): f = f[1:] return filelog.filelog(self.opener, f, self.revlogversion) - def changectx(self, changeid): + def changectx(self, changeid=None): return context.changectx(self, changeid) def filectx(self, path, changeid=None, fileid=None):