[PATCH V4] revert: improve hint on abort if no files were specified

Adrian Buehlmann adrian at cadifra.com
Thu Jun 16 07:58:51 CDT 2011


# HG changeset patch
# User Adrian Buehlmann <adrian at cadifra.com>
# Date 1308220115 -7200
# Node ID 339963126a5ef93ba70f56945e6a8122a3d76fc1
# Parent  4819241ec1adc66ba310505d8b7c8fb9b65746a2
revert: improve hint on abort if no files were specified

and provide some additional status info in two notable cases as a friendly
reminder for the user (please see the examples below)

uncommitted merge:

  BEFORE:
    $ hg revert
    abort: no files or directories specified
    (use --all to revert all files)

  NEW:
    $ hg revert
    uncommitted merge
    abort: no files or directories specified
    (use --all to lose all changes you might have made)

uncommitted changes, revert to non-parent (most complex use case):

  BEFORE:
    $ hg revert -d 2011-05-05
    Found revision 7423 from Thu May 05 19:56:05 2011 +0200
    abort: no files or directories specified
    (use --all to revert all files)

  NEW:
    $ hg revert -d 2011-05-05
    Found revision 7423 from Thu May 05 19:56:05 2011 +0200
    uncommitted changes
    abort: no files or directories specified
    (use --all to lose all your changes, or 'hg update -r 7423' to update)

clean, revert to non-parent:

  BEFORE:
    $ hg revert -d 2011-05-05
    Found revision 7423 from Thu May 05 19:56:05 2011 +0200
    abort: no files or directories specified
    (use --all to revert all files)

  NEW:
    $ hg revert -d 2011-05-05
    Found revision 7423 from Thu May 05 19:56:05 2011 +0200
    abort: no files or directories specified
    (use --all to revert all files, or 'hg update -r 7423' to update)

uncommitted changes:

  BEFORE:
    $ hg revert
    abort: no files or directories specified
    (use --all to revert all files)

  NEW:
    $ hg revert
    abort: no files or directories specified
    (use --all to lose all your changes)

nothing changed:

  BEFORE:
    $ hg revert
    abort: no files or directories specified
    (use --all to revert all files)

  NEW:
    $ hg revert
    nothing changed

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -4177,10 +4177,6 @@
 
     parent, p2 = repo.dirstate.parents()
 
-    if not pats and not opts.get('all'):
-        raise util.Abort(_('no files or directories specified'),
-                         hint=_('use --all to revert all files'))
-
     ctx = scmutil.revsingle(repo, opts.get('rev'))
     node = ctx.node()
     mf = ctx.manifest()
@@ -4189,6 +4185,27 @@
     else:
         pmf = None
 
+    if not pats and not opts.get('all'):
+        msg = _("no files or directories specified")
+        if p2 != nullid:
+            ui.status(_("uncommitted merge\n"))
+            hint = _("use --all to lose all changes you might have made")
+            raise util.Abort(msg, hint=hint)
+        dirty = util.any(repo.status())
+        if node != parent:
+            if dirty:
+                ui.status(_("uncommitted changes\n"))
+                hint = _("use --all to lose all your changes,"
+                         " or 'hg update -r %s' to update") % ctx.rev()
+            else:
+                hint = _("use --all to revert all files,"
+                         " or 'hg update -r %s' to update") % ctx.rev()
+            raise util.Abort(msg, hint=hint)
+        elif dirty:
+            hint = _("use --all to lose all your changes")
+            raise util.Abort(msg, hint=hint)
+        ui.status(_("nothing changed\n"))
+
     # need all matching names in dirstate and manifest of target rev,
     # so have to walk both. do not print errors if files exist in one
     # but not other.
diff --git a/tests/test-confused-revert.t b/tests/test-confused-revert.t
--- a/tests/test-confused-revert.t
+++ b/tests/test-confused-revert.t
@@ -59,8 +59,9 @@
 Revert should fail:
 
   $ hg revert
+  uncommitted merge
   abort: no files or directories specified
-  (use --all to revert all files)
+  (use --all to lose all changes you might have made)
   [255]
 
 Revert should be ok now:
diff --git a/tests/test-revert.t b/tests/test-revert.t
--- a/tests/test-revert.t
+++ b/tests/test-revert.t
@@ -186,7 +186,7 @@
 
   $ hg revert -rtip
   abort: no files or directories specified
-  (use --all to revert all files)
+  (use --all to revert all files, or 'hg update -r 1' to update)
   [255]
 
 should succeed


More information about the Mercurial-devel mailing list