diff mercurial/hg.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 e925f33ac088
children 7ffbd911dbc9
line wrap: on
line diff
--- a/mercurial/hg.py	Thu Jan 04 21:37:03 2018 -0800
+++ b/mercurial/hg.py	Mon Jan 08 19:41:47 2018 +0530
@@ -14,7 +14,10 @@
 import shutil
 
 from .i18n import _
-from .node import nullid
+from .node import (
+    hex,
+    nullid,
+)
 
 from . import (
     bookmarks,
@@ -844,16 +847,32 @@
 
     return ret
 
-def merge(repo, node, force=None, remind=True, mergeforce=False, labels=None):
+def merge(repo, node, force=None, remind=True, mergeforce=False, labels=None,
+          abort=False):
     """Branch merge with node, resolving changes. Return true if any
     unresolved conflicts."""
-    stats = mergemod.update(repo, node, True, force, mergeforce=mergeforce,
-                            labels=labels)
+    if not abort:
+        stats = mergemod.update(repo, node, True, force, mergeforce=mergeforce,
+                                labels=labels)
+    else:
+        ms = mergemod.mergestate.read(repo)
+        if ms.active():
+            # there were conflicts
+            node = hex(ms._local)
+        else:
+            # there were no conficts, mergestate was not stored
+            node = repo['.'].hex()
+
+        repo.ui.status(_("aborting the merge, updating back to"
+                         " %s\n") % node[:12])
+        stats = mergemod.update(repo, node, branchmerge=False, force=True,
+                                labels=labels)
+
     _showstats(repo, stats)
     if stats[3]:
         repo.ui.status(_("use 'hg resolve' to retry unresolved file merges "
-                         "or 'hg update -C .' to abandon\n"))
-    elif remind:
+                         "or 'hg merge --abort' to abandon\n"))
+    elif remind and not abort:
         repo.ui.status(_("(branch merge, don't forget to commit)\n"))
     return stats[3] > 0