Merge branch 'stable'
[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 from stgit import argparse
25
26 help = 'Reset the patch stack to an earlier state'
27 kind = 'stack'
28 usage = ['[options] <state> [<patchnames>]']
29 description = """
30 Reset the patch stack to an earlier state. The state is specified with
31 a commit id from a stack log; "stg log" lets you view this log, and
32 "stg reset" lets you reset to any state you see in the log.
33
34 If one or more patch names are given, reset only those patches, and
35 leave the rest alone."""
36
37 args = [argparse.patch_range(argparse.applied_patches,
38 argparse.unapplied_patches,
39 argparse.hidden_patches)]
40 options = [
41 opt('--hard', action = 'store_true',
42 short = 'Discard changes in your index/worktree')]
43
44 directory = common.DirectoryHasRepositoryLib()
45
46 def func(parser, options, args):
47 stack = directory.repository.current_stack
48 if len(args) >= 1:
49 ref, patches = args[0], args[1:]
50 state = log.get_log_entry(stack.repository, ref,
51 stack.repository.rev_parse(ref))
52 else:
53 raise common.CmdException('Wrong number of arguments')
54 trans = transaction.StackTransaction(stack, 'reset',
55 discard_changes = options.hard,
56 allow_bad_head = True)
57 try:
58 if patches:
59 log.reset_stack_partially(trans, stack.repository.default_iw,
60 state, patches)
61 else:
62 log.reset_stack(trans, stack.repository.default_iw, state)
63 except transaction.TransactionHalted:
64 pass
65 return trans.run(stack.repository.default_iw, allow_bad_head = not patches)