Check bad head and clean iw before StackTransaction
[stgit.git] / stgit / commands / redo.py
blob33fd8031d6bb17687bce455a4348e0385223f795
1 from stgit.argparse import opt
2 from stgit.commands.common import CmdException, DirectoryHasRepository
3 from stgit.lib import log, transaction
5 __copyright__ = """
6 Copyright (C) 2008, Karl Hasselström <kha@treskal.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see http://www.gnu.org/licenses/.
19 """
21 help = 'Undo the last undo operation'
22 kind = 'stack'
23 usage = ['']
24 description = """
25 If the last command was an undo, reset the patch stack to the state it
26 had before the undo. Consecutive invocations of "stg redo" will undo
27 the effects of consecutive invocations of "stg undo".
29 It is an error to run "stg redo" if the last command was not an
30 undo."""
32 args = []
33 options = [
34 opt(
35 '-n',
36 '--number',
37 type='int',
38 metavar='N',
39 default=1,
40 short='Undo the last N undos',
42 opt(
43 '--hard',
44 action='store_true',
45 short='Discard changes in your index/worktree',
49 directory = DirectoryHasRepository()
52 def func(parser, options, args):
53 stack = directory.repository.current_stack
54 if options.number < 1:
55 raise CmdException('Bad number of undos to redo')
56 state = log.undo_state(stack, -options.number)
57 trans = transaction.StackTransaction(stack, discard_changes=options.hard)
58 try:
59 log.reset_stack(trans, stack.repository.default_iw, state)
60 except transaction.TransactionHalted:
61 pass
62 return trans.run(
63 'redo %d' % options.number, stack.repository.default_iw, allow_bad_head=True