changeset 3132:81da3c45aabd

Move defaultrev into changectx This also causes tag on a repository with no working directory to default to tip.
author Brendan Cully <brendan@kublai.com>
date Fri, 15 Sep 2006 15:23:52 -0700
parents cff3c58a5766
children 15fde1970003
files mercurial/commands.py mercurial/context.py mercurial/localrepo.py
diffstat 3 files changed, 23 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Mon Sep 18 17:43:31 2006 +0200
+++ b/mercurial/commands.py	Fri Sep 15 15:23:52 2006 -0700
@@ -50,21 +50,6 @@
                              (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 @@
         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 @@
     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 @@
     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 @@
     %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 @@
                            '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 @@
             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:
--- a/mercurial/context.py	Mon Sep 18 17:43:31 2006 +0200
+++ b/mercurial/context.py	Fri Sep 15 15:23:52 2006 -0700
@@ -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)
 
--- a/mercurial/localrepo.py	Mon Sep 18 17:43:31 2006 +0200
+++ b/mercurial/localrepo.py	Fri Sep 15 15:23:52 2006 -0700
@@ -318,7 +318,7 @@
             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):