New command: stg redo
[stgit] / stgit / commands / redo.py
CommitLineData
121c14e5
KH
1# -*- coding: utf-8 -*-
2
3__copyright__ = """
4Copyright (C) 2008, Karl Hasselström <kha@treskal.com>
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License version 2 as
8published by the Free Software Foundation.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18"""
19
20from stgit.argparse import opt
21from stgit.commands import common
22from stgit.lib import log, transaction
23
24help = 'Undo the last undo operation'
25kind = 'stack'
26usage = ['']
27description = """
28If the last command was an undo, reset the patch stack to the state it
29had before the undo. Consecutive invocations of "stg redo" will undo
30the effects of consecutive invocations of "stg undo".
31
32It is an error to run "stg redo" if the last command was not an
33undo."""
34
35options = [
36 opt('-n', '--number', type = 'int', metavar = 'N', default = 1,
37 short = 'Undo the last N undos'),
38 opt('--hard', action = 'store_true',
39 short = 'Discard changes in your index/worktree')]
40
41directory = common.DirectoryHasRepositoryLib()
42
43def func(parser, options, args):
44 stack = directory.repository.current_stack
45 if options.number < 1:
46 raise common.CmdException('Bad number of undos to redo')
47 state = log.undo_state(stack, -options.number)
48 trans = transaction.StackTransaction(stack, 'redo %d' % options.number,
49 discard_changes = options.hard)
50 try:
51 log.reset_stack(trans, stack.repository.default_iw, state, [])
52 except transaction.TransactionHalted:
53 pass
54 return trans.run(stack.repository.default_iw)