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