abort: added support for unshelve
authorTaapas Agrawal <taapas2897@gmail.com>
Wed, 26 Jun 2019 22:15:07 +0530
changeset 42599 3fb0493812c0
parent 42598 8ddfdcce4bd6
child 42600 3bc400ccbf99
abort: added support for unshelve This patch adds the support for shelve in `hg abort` plan. For this the logic to load a `shelvedstate` and the error handling for it had been shifted to a seperate function `_loadunshelvedstate()`. This returns a tuple with `state` file and `opts.` `hgabortunshelve()` has been created for independent calls. In case abortion of `unshelve` is called via `hg abort` the `shelvedstate` needs to be loaded seperately. This has been ensured by `_loadunshelvedstate()` `hgabortunshelve()` is then registered as `abortfunc` for state detection API. Results are shown as tests. Differential Revision: https://phab.mercurial-scm.org/D6579
mercurial/commands.py
mercurial/shelve.py
mercurial/state.py
tests/test-shelve2.t
--- a/mercurial/commands.py	Wed Jul 10 23:11:55 2019 +0530
+++ b/mercurial/commands.py	Wed Jun 26 22:15:07 2019 +0530
@@ -6182,6 +6182,12 @@
     with repo.wlock():
         return shelvemod.dounshelve(ui, repo, *shelved, **opts)
 
+statemod.addunfinished(
+    'unshelve', fname='shelvedstate', continueflag=True,
+    abortfunc=shelvemod.hgabortunshelve,
+    cmdmsg=_('unshelve already in progress'),
+)
+
 @command('update|up|checkout|co',
     [('C', 'clean', None, _('discard uncommitted changes (no backup)')),
     ('c', 'check', None, _('require clean working directory')),
--- a/mercurial/shelve.py	Wed Jul 10 23:11:55 2019 +0530
+++ b/mercurial/shelve.py	Wed Jun 26 22:15:07 2019 +0530
@@ -624,7 +624,30 @@
         raise error.Abort(_('working directory parents do not match unshelve '
                            'state'))
 
-def unshelveabort(ui, repo, state, opts):
+def _loadshelvedstate(ui, repo, opts):
+    try:
+        state = shelvedstate.load(repo)
+        if opts.get('keep') is None:
+            opts['keep'] = state.keep
+    except IOError as err:
+        if err.errno != errno.ENOENT:
+            raise
+        cmdutil.wrongtooltocontinue(repo, _('unshelve'))
+    except error.CorruptedState as err:
+        ui.debug(pycompat.bytestr(err) + '\n')
+        if opts.get('continue'):
+            msg = _('corrupted shelved state file')
+            hint = _('please run hg unshelve --abort to abort unshelve '
+                     'operation')
+            raise error.Abort(msg, hint=hint)
+        elif opts.get('abort'):
+            shelvedstate.clear(repo)
+            raise error.Abort(_('could not read shelved state file, your '
+                                'working copy may be in an unexpected state\n'
+                                'please update to some commit\n'))
+    return state
+
+def unshelveabort(ui, repo, state):
     """subcommand that abort an in-progress unshelve"""
     with repo.lock():
         try:
@@ -642,6 +665,12 @@
             shelvedstate.clear(repo)
             ui.warn(_("unshelve of '%s' aborted\n") % state.name)
 
+def hgabortunshelve(ui, repo):
+    """logic to  abort unshelve using 'hg abort"""
+    with repo.wlock():
+        state = _loadshelvedstate(ui, repo, {'abort' : True})
+        return unshelveabort(ui, repo, state)
+
 def mergefiles(ui, repo, wctx, shelvectx):
     """updates to wctx and merges the changes from shelvectx into the
     dirstate."""
@@ -665,7 +694,6 @@
             if shfile.exists():
                 shfile.movetobackup()
         cleanupoldbackups(repo)
-
 def unshelvecontinue(ui, repo, state, opts):
     """subcommand to continue an in-progress unshelve"""
     # We're finishing off a merge. First parent is our original
@@ -864,29 +892,9 @@
         if abortf and opts.get('tool', False):
             ui.warn(_('tool option will be ignored\n'))
 
-        try:
-            state = shelvedstate.load(repo)
-            if opts.get('keep') is None:
-                opts['keep'] = state.keep
-        except IOError as err:
-            if err.errno != errno.ENOENT:
-                raise
-            cmdutil.wrongtooltocontinue(repo, _('unshelve'))
-        except error.CorruptedState as err:
-            ui.debug(pycompat.bytestr(err) + '\n')
-            if continuef:
-                msg = _('corrupted shelved state file')
-                hint = _('please run hg unshelve --abort to abort unshelve '
-                         'operation')
-                raise error.Abort(msg, hint=hint)
-            elif abortf:
-                shelvedstate.clear(repo)
-                raise error.Abort(_('could not read shelved state file, your '
-                                 'working copy may be in an unexpected state\n'
-                                 'please update to some commit\n'))
-
+        state = _loadshelvedstate(ui, repo, opts)
         if abortf:
-            return unshelveabort(ui, repo, state, opts)
+            return unshelveabort(ui, repo, state)
         elif continuef:
             return unshelvecontinue(ui, repo, state, opts)
     elif len(shelved) > 1:
--- a/mercurial/state.py	Wed Jul 10 23:11:55 2019 +0530
+++ b/mercurial/state.py	Wed Jun 26 22:15:07 2019 +0530
@@ -194,10 +194,6 @@
         _unfinishedstates.insert(0, statecheckobj)
 
 addunfinished(
-    'unshelve', fname='shelvedstate', continueflag=True,
-    cmdmsg=_('unshelve already in progress')
-)
-addunfinished(
     'update', fname='updatestate', clearable=True,
     cmdmsg=_('last update was interrupted'),
     cmdhint=_("use 'hg update' to get a consistent checkout"),
--- a/tests/test-shelve2.t	Wed Jul 10 23:11:55 2019 +0530
+++ b/tests/test-shelve2.t	Wed Jun 26 22:15:07 2019 +0530
@@ -1,4 +1,5 @@
 #testcases stripbased phasebased
+#testcases abortflag abortcommand
 
   $ cat <<EOF >> $HGRCPATH
   > [extensions]
@@ -19,6 +20,13 @@
 
 #endif
 
+#if abortflag
+  $ cat >> $HGRCPATH <<EOF
+  > [alias]
+  > abort = unshelve --abort
+  > EOF
+#endif
+
 shelve should leave dirstate clean (issue4055)
 
   $ hg init shelverebase
@@ -285,7 +293,14 @@
   >>>>>>> working-copy: aef214a5229c - shelve: changes to: commit stuff
   $ cat f.orig
   g
-  $ hg unshelve --abort
+
+#if abortcommand
+when in dry-run mode
+  $ hg abort --dry-run
+  unshelve in progress, will be aborted
+#endif
+
+  $ hg abort
   unshelve of 'default' aborted
   $ hg st
   ? f.orig
@@ -695,7 +710,7 @@
   [255]
 
 Unshelve --abort works with a corrupted shelvedstate
-  $ hg unshelve --abort
+  $ hg abort
   abort: could not read shelved state file, your working copy may be in an unexpected state
   please update to some commit
   
@@ -703,8 +718,10 @@
 
 Unshelve --abort fails with appropriate message if there's no unshelve in
 progress
-  $ hg unshelve --abort
-  abort: no unshelve in progress
+  $ 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]
   $ cd ..
 
@@ -824,7 +841,7 @@
   warning: conflicts while merging a! (edit, then use 'hg resolve --mark')
   unresolved conflicts (see 'hg resolve', then 'hg unshelve --continue')
   [1]
-  $ hg unshelve --abort
+  $ hg abort
   unshelve of 'default' aborted
 
 Unshelve without .shelve metadata (can happen when upgrading a repository with old shelve)
@@ -843,7 +860,7 @@
   [1]
   $ cat .hg/shelved/default.shelve
   node=82e0cb9893247d12667017593ce1e5655860f1ac
-  $ hg unshelve --abort
+  $ hg abort
   unshelve of 'default' aborted
 
 #endif