diff mercurial/commands.py @ 35704:41ef02ba329b

merge: add `--abort` flag which can abort the merge Currently we don't have a good functionality to abort the merge and tell user to do `hg update -C .` which can leads to different results if user missed the '.' and moreover does not align with other abort functionalities like rebase, shelve etc. This patch adds `hg merge --abort` which will abort the ongoing merge and take us back to the chagneset where we started from. Works in both cases when merge resulted in conflicts and when there were no conflicts. .. feature:: A `--abort` flag to merge command to abort the ongoing merge. Differential Revision: https://phab.mercurial-scm.org/D1829
author Pulkit Goyal <7895pulkit@gmail.com>
date Mon, 08 Jan 2018 19:41:47 +0530
parents 1c929b4942a3
children 35a0f6f31eef
line wrap: on
line diff
--- a/mercurial/commands.py	Thu Jan 04 21:37:03 2018 -0800
+++ b/mercurial/commands.py	Mon Jan 08 19:41:47 2018 +0530
@@ -3530,7 +3530,8 @@
       _('force a merge including outstanding changes (DEPRECATED)')),
     ('r', 'rev', '', _('revision to merge'), _('REV')),
     ('P', 'preview', None,
-     _('review revisions to merge (no merge is performed)'))
+     _('review revisions to merge (no merge is performed)')),
+    ('', 'abort', None, _('abort the ongoing merge')),
      ] + mergetoolopts,
     _('[-P] [[-r] REV]'))
 def merge(ui, repo, node=None, **opts):
@@ -3555,7 +3556,7 @@
 
     See :hg:`help resolve` for information on handling file conflicts.
 
-    To undo an uncommitted merge, use :hg:`update --clean .` which
+    To undo an uncommitted merge, use :hg:`merge --abort` which
     will check out a clean copy of the original merge parent, losing
     all changes.
 
@@ -3563,6 +3564,16 @@
     """
 
     opts = pycompat.byteskwargs(opts)
+    abort = opts.get('abort')
+    if abort and repo.dirstate.p2() == nullid:
+        cmdutil.wrongtooltocontinue(repo, _('merge'))
+    if abort:
+        if node:
+            raise error.Abort(_("cannot specify a node with --abort"))
+        if opts.get('rev'):
+            raise error.Abort(_("cannot specify both --rev and --abort"))
+        if opts.get('preview'):
+            raise error.Abort(_("cannot specify --preview with --abort"))
     if opts.get('rev') and node:
         raise error.Abort(_("please specify just one revision"))
     if not node:
@@ -3571,7 +3582,7 @@
     if node:
         node = scmutil.revsingle(repo, node).node()
 
-    if not node:
+    if not node and not abort:
         node = repo[destutil.destmerge(repo)].node()
 
     if opts.get('preview'):
@@ -3592,7 +3603,7 @@
         force = opts.get('force')
         labels = ['working copy', 'merge rev']
         return hg.merge(repo, node, force=force, mergeforce=force,
-                        labels=labels)
+                        labels=labels, abort=abort)
     finally:
         ui.setconfig('ui', 'forcemerge', '', 'merge')