stg import now extracts Message-ID header
[stgit.git] / stgit / commands / reset.py
blob2fb2ad8c0002efb66d7418844f7bd21bcd84b61b
1 from stgit import utils
2 from stgit.argparse import opt, patch_range
3 from stgit.commands.common import CmdException, DirectoryHasRepository
4 from stgit.lib import log, transaction
6 __copyright__ = """
7 Copyright (C) 2008, Karl Hasselström <kha@treskal.com>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License version 2 as
11 published by the Free Software Foundation.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, see http://www.gnu.org/licenses/.
20 """
22 help = 'Reset the patch stack to an earlier state'
23 kind = 'stack'
24 usage = ['[options] [--] [<state> [<patchnames>]]']
25 description = """
26 Reset the patch stack to an earlier state. If no state is specified,
27 reset only the changes in the worktree.
29 The state is specified with a commit id from a stack log; "stg log" lets
30 you view this log, and "stg reset" lets you reset to any state you see
31 in the log. If one or more patch names are given, reset only those
32 patches, and leave the rest alone."""
34 args = [patch_range('applied_patches', 'unapplied_patches', 'hidden_patches')]
35 options = [
36 opt(
37 '--hard',
38 action='store_true',
39 short='Discard changes in your index/worktree',
43 directory = DirectoryHasRepository()
46 def func(parser, options, args):
47 stack = directory.repository.current_stack
48 iw = stack.repository.default_iw
49 if len(args) >= 1:
50 ref, patches = args[0], args[1:]
51 state = log.get_stack_state(
52 stack.repository, ref, stack.repository.rev_parse(ref)
54 elif options.hard:
55 iw.checkout_hard(stack.head.data.tree)
56 return utils.STGIT_SUCCESS
57 else:
58 raise CmdException('Wrong options or number of arguments')
60 trans = transaction.StackTransaction(
61 stack,
62 'reset',
63 discard_changes=options.hard,
64 allow_bad_head=True,
66 try:
67 if patches:
68 log.reset_stack_partially(trans, iw, state, patches)
69 else:
70 log.reset_stack(trans, iw, state)
71 except transaction.TransactionHalted:
72 pass
73 return trans.run(iw, allow_bad_head=not patches)