Move stack reset function to a shared location
[stgit] / stgit / commands / reset.py
CommitLineData
3a3a4f2f
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
9690617a 20from stgit.argparse import opt
3a3a4f2f
KH
21from stgit.commands import common
22from stgit.lib import git, log, transaction
23from stgit.out import out
24
25help = 'Reset the patch stack to an earlier state'
26kind = 'stack'
9690617a 27usage = ['[options] <state> [<patchnames>]']
3a3a4f2f
KH
28description = """
29Reset the patch stack to an earlier state. The state is specified with
30a commit from a stack log; for a branch foo, StGit stores the stack
31log in foo.stgit^. So to undo the last N StGit commands (or rather,
32the last N log entries; there is not an exact one-to-one
33relationship), you would say
34
35 stg reset foo.stgit^~N
36
37or, if you are not sure how many steps to undo, you can view the log
38with "git log" or gitk
39
40 gitk foo.stgit^
41
42and then reset to any sha1 you see in the log.
43
44If one or more patch names are given, reset only those patches, and
45leave the rest alone."""
46
9690617a
KH
47options = [
48 opt('--hard', action = 'store_true',
49 short = 'Discard changes in your index/worktree')]
3a3a4f2f
KH
50
51directory = common.DirectoryHasRepositoryLib()
52
3a3a4f2f
KH
53def func(parser, options, args):
54 stack = directory.repository.current_stack
55 if len(args) >= 1:
56 ref, patches = args[0], args[1:]
67b01c13
KH
57 state = log.get_log_entry(stack.repository, ref,
58 stack.repository.rev_parse(ref))
3a3a4f2f
KH
59 else:
60 raise common.CmdException('Wrong number of arguments')
4ae6b67e
KH
61 trans = transaction.StackTransaction(stack, 'reset',
62 discard_changes = options.hard)
63 try:
64 log.reset_stack(trans, stack.repository.default_iw, state, patches)
65 except transaction.TransactionHalted:
66 pass
67 return trans.run(stack.repository.default_iw)