changeset 42600:3bc400ccbf99

abort: added support for merge This adds support of `hg merge --abort` to `hg abort` plan. This involves refactoring `hg.merge` into two different functions removing the abort logic of `merge` from `hg.merge` and then creating a seperate `hg.abortmerge` to handle the abort logic so that the abortion of merge can be called independently. `hg.abortmerge` is then registered as `abortfunc` for the state detection API so that `commands.abort` can use it to deal with an unfinished merge operation. Results are shown as tests. Differential Revision: https://phab.mercurial-scm.org/D6588
author Taapas Agrawal <taapas2897@gmail.com>
date Sun, 30 Jun 2019 01:07:14 +0530
parents 3fb0493812c0
children 862f6bddacce
files mercurial/commands.py mercurial/hg.py mercurial/state.py tests/test-commit-unresolved.t tests/test-shelve2.t
diffstat 5 files changed, 58 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Wed Jun 26 22:15:07 2019 +0530
+++ b/mercurial/commands.py	Sun Jun 30 01:07:14 2019 +0530
@@ -4024,6 +4024,14 @@
         return hg.merge(repo, node, force=force, mergeforce=force,
                         labels=labels, abort=abort)
 
+statemod.addunfinished(
+    'merge', fname=None, clearable=True, allowcommit=True,
+    cmdmsg=_('outstanding uncommitted merge'), abortfunc=hg.abortmerge,
+    statushint=_('To continue:    hg commit\n'
+                 'To abort:       hg merge --abort'),
+    cmdhint=_("use 'hg commit' or 'hg merge --abort'")
+)
+
 @command('outgoing|out',
     [('f', 'force', None, _('run even when the destination is unrelated')),
     ('r', 'rev', [],
--- a/mercurial/hg.py	Wed Jun 26 22:15:07 2019 +0530
+++ b/mercurial/hg.py	Sun Jun 30 01:07:14 2019 +0530
@@ -956,31 +956,35 @@
           abort=False):
     """Branch merge with node, resolving changes. Return true if any
     unresolved conflicts."""
-    if not abort:
-        stats = mergemod.update(repo, node, branchmerge=True, force=force,
-                                mergeforce=mergeforce, labels=labels)
-    else:
-        ms = mergemod.mergestate.read(repo)
-        if ms.active():
-            # there were conflicts
-            node = ms.localctx.hex()
-        else:
-            # there were no conficts, mergestate was not stored
-            node = repo['.'].hex()
+    if abort:
+        return abortmerge(repo.ui, repo, labels=labels)
 
-        repo.ui.status(_("aborting the merge, updating back to"
-                         " %s\n") % node[:12])
-        stats = mergemod.update(repo, node, branchmerge=False, force=True,
-                                labels=labels)
-
+    stats = mergemod.update(repo, node, branchmerge=True, force=force,
+                            mergeforce=mergeforce, labels=labels)
     _showstats(repo, stats)
     if stats.unresolvedcount:
         repo.ui.status(_("use 'hg resolve' to retry unresolved file merges "
                          "or 'hg merge --abort' to abandon\n"))
-    elif remind and not abort:
+    elif remind:
         repo.ui.status(_("(branch merge, don't forget to commit)\n"))
     return stats.unresolvedcount > 0
 
+def abortmerge(ui, repo, labels=None):
+    ms = mergemod.mergestate.read(repo)
+    if ms.active():
+        # there were conflicts
+        node = ms.localctx.hex()
+    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)
+    return stats.unresolvedcount > 0
+
 def _incoming(displaychlist, subreporecurse, ui, repo, source,
         opts, buffered=False):
     """
--- a/mercurial/state.py	Wed Jun 26 22:15:07 2019 +0530
+++ b/mercurial/state.py	Sun Jun 30 01:07:14 2019 +0530
@@ -205,13 +205,6 @@
                  'To mark the changeset bad:     hg bisect --bad\n'
                  'To abort:                      hg bisect --reset\n')
 )
-addunfinished(
-    'merge', fname=None, clearable=True, allowcommit=True,
-    cmdmsg=_('outstanding uncommitted merge'),
-    statushint=_('To continue:    hg commit\n'
-                 'To abort:       hg merge --abort'),
-    cmdhint=_("use 'hg commit' or 'hg merge --abort'")
-)
 
 def getrepostate(repo):
     # experimental config: commands.status.skipstates
--- a/tests/test-commit-unresolved.t	Wed Jun 26 22:15:07 2019 +0530
+++ b/tests/test-commit-unresolved.t	Sun Jun 30 01:07:14 2019 +0530
@@ -1,3 +1,11 @@
+#testcases abortcommand abortflag
+#if abortflag
+  $ cat >> $HGRCPATH <<EOF
+  > [alias]
+  > abort = merge --abort
+  > EOF
+#endif
+
   $ addcommit () {
   >     echo $1 > $1
   >     hg add $1
@@ -36,9 +44,11 @@
 
 Testing the abort functionality first in case of conflicts
 
-  $ hg merge --abort
-  abort: no merge in progress
+  $ hg abort
+  abort: no merge in progress (abortflag !)
+  abort: no operation in progress (abortcommand !)
   [255]
+
   $ hg merge
   merging A
   warning: conflicts while merging A! (edit, then use 'hg resolve --mark')
@@ -53,7 +63,13 @@
   abort: cannot specify both --rev and --abort
   [255]
 
-  $ hg merge --abort
+#if abortcommand
+when in dry-run mode
+  $ hg abort --dry-run
+  merge in progress, will be aborted
+#endif
+
+  $ hg abort
   aborting the merge, updating back to e45016d2b3d3
   1 files updated, 0 files merged, 1 files removed, 0 files unresolved
 
@@ -131,7 +147,7 @@
   abort: cannot specify --preview with --abort
   [255]
 
-  $ hg merge --abort
+  $ hg abort
   aborting the merge, updating back to 68352a18a7c4
   1 files updated, 0 files merged, 1 files removed, 0 files unresolved
 
--- a/tests/test-shelve2.t	Wed Jun 26 22:15:07 2019 +0530
+++ b/tests/test-shelve2.t	Sun Jun 30 01:07:14 2019 +0530
@@ -718,11 +718,16 @@
 
 Unshelve --abort fails with appropriate message if there's no unshelve in
 progress
+
+#if abortflag
+  $ hg unshelve --abort
+  abort: no unshelve in progress
+  [255]
+#else
   $ hg abort
-  abort: no unshelve in progress (abortflag !)
-  abort: merge in progress but does not support 'hg abort' (no-abortflag !)
-  (use 'hg commit' or 'hg merge --abort') (no-abortflag !)
-  [255]
+  aborting the merge, updating back to 9451eaa6eee3
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+#endif
   $ cd ..
 
 Unshelve respects --keep even if user intervention is needed