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