1 # -*- coding: utf-8 -*-
4 Copyright (C) 2008, Karl Hasselström <kha@treskal.com>
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.
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.
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
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
25 help = 'Undo the last operation'
29 Reset the patch stack to the previous state. Consecutive invocations
30 of "stg undo" will take you ever further into the past."""
33 opt('-n', '--number', type = 'int', metavar = 'N', default = 1,
34 short = 'Undo the last N commands'),
35 opt('--hard', action = 'store_true',
36 short = 'Discard changes in your index/worktree')]
38 directory = common.DirectoryHasRepositoryLib()
40 def func(parser, options, args):
41 stack = directory.repository.current_stack
42 if options.number < 1:
43 raise common.CmdException('Bad number of commands to undo')
44 state = log.undo_state(stack, options.number)
45 trans = transaction.StackTransaction(stack, 'undo %d' % options.number,
46 discard_changes = options.hard)
48 log.reset_stack(trans, stack.repository.default_iw, state, [])
49 except transaction.TransactionHalted:
51 return trans.run(stack.repository.default_iw)