Rename "stg coalesce" to "stg squash"
[stgit] / stgit / commands / undo.py
CommitLineData
069a1e95
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 git, log, transaction
23from stgit.out import out
24
25help = 'Undo the last operation'
26kind = 'stack'
27usage = ['']
28description = """
29Reset the patch stack to the previous state. Consecutive invocations
30of "stg undo" will take you ever further into the past."""
31
6c8a90e1 32args = []
069a1e95
KH
33options = [
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
39directory = common.DirectoryHasRepositoryLib()
40
41def 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,
f4e6a60e
KH
47 discard_changes = options.hard,
48 allow_bad_head = True)
069a1e95 49 try:
c70033b4 50 log.reset_stack(trans, stack.repository.default_iw, state)
069a1e95
KH
51 except transaction.TransactionHalted:
52 pass
c70033b4 53 return trans.run(stack.repository.default_iw, allow_bad_head = True)