diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -6182,6 +6182,11 @@ with repo.wlock(): return shelvemod._dounshelve(ui, repo, *shelved, **opts) +statemod.addunfinished( + 'unshelve', fname='shelvedstate', continueflag=True, + cmdmsg=_('unshelve already in progress'), abortfunc=shelvemod.unshelveabort +) + @command('update|up|checkout|co', [('C', 'clean', None, _('discard uncommitted changes (no backup)')), ('c', 'check', None, _('require clean working directory')), diff --git a/mercurial/shelve.py b/mercurial/shelve.py --- a/mercurial/shelve.py +++ b/mercurial/shelve.py @@ -26,6 +26,7 @@ import errno import itertools import stat +import sys from .i18n import _ from . import ( @@ -624,9 +625,37 @@ raise error.Abort(_('working directory parents do not match unshelve ' 'state')) -def unshelveabort(ui, repo, state, opts): +def _loadshelvedstate(ui, repo, continuef=False, abortf=False, **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 continuef: + msg = _('corrupted shelved state file') + hint = _('please run hg unshelve --abort to abort unshelve ' + 'operation') + raise error.Abort(msg, hint=hint) + elif abortf: + msg = _('could not read shelved state file, your working copy ' + 'may be in an unexpected state\nplease update to some ' + 'commit\n') + ui.warn(msg) + shelvedstate.clear(repo) + sys.exit() + return (state, opts) + +def unshelveabort(ui, repo, state=None): """subcommand that abort an in-progress unshelve""" - with repo.lock(): + if not state: + statetuple = _loadshelvedstate(ui, repo, abortf=True) + state, opts = statetuple + with repo.wlock(), repo.lock(): try: checkparents(repo, state) @@ -864,31 +893,12 @@ 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: - msg = _('could not read shelved state file, your working copy ' - 'may be in an unexpected state\nplease update to some ' - 'commit\n') - ui.warn(msg) - shelvedstate.clear(repo) - return + statetuple = _loadshelvedstate(ui, repo, continuef=continuef, + abortf=abortf, **opts) + state, opts = statetuple 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: diff --git a/mercurial/state.py b/mercurial/state.py --- a/mercurial/state.py +++ b/mercurial/state.py @@ -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"), diff --git a/tests/test-shelve2.t b/tests/test-shelve2.t --- a/tests/test-shelve2.t +++ b/tests/test-shelve2.t @@ -1,4 +1,5 @@ #testcases stripbased phasebased +#testcases abortflag abortcommand $ cat <> $HGRCPATH > [extensions] @@ -19,6 +20,13 @@ #endif +#if abortflag + $ cat >> $HGRCPATH < [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,14 +710,16 @@ [255] Unshelve --abort works with a corrupted shelvedstate - $ hg unshelve --abort + $ hg abort could not read shelved state file, your working copy may be in an unexpected state please update to some commit 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 .. @@ -822,7 +839,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) @@ -841,7 +858,7 @@ [1] $ cat .hg/shelved/default.shelve node=82e0cb9893247d12667017593ce1e5655860f1ac - $ hg unshelve --abort + $ hg abort unshelve of 'default' aborted #endif