Import git show output easily
[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
6c8a90e1 24from stgit import argparse
3a3a4f2f
KH
25
26help = 'Reset the patch stack to an earlier state'
27kind = 'stack'
9690617a 28usage = ['[options] <state> [<patchnames>]']
3a3a4f2f
KH
29description = """
30Reset the patch stack to an earlier state. The state is specified with
de19d17b
KH
31a 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.
3a3a4f2f
KH
33
34If one or more patch names are given, reset only those patches, and
35leave the rest alone."""
36
6c8a90e1
KH
37args = [argparse.patch_range(argparse.applied_patches,
38 argparse.unapplied_patches,
39 argparse.hidden_patches)]
9690617a
KH
40options = [
41 opt('--hard', action = 'store_true',
42 short = 'Discard changes in your index/worktree')]
3a3a4f2f
KH
43
44directory = common.DirectoryHasRepositoryLib()
45
3a3a4f2f
KH
46def func(parser, options, args):
47 stack = directory.repository.current_stack
48 if len(args) >= 1:
49 ref, patches = args[0], args[1:]
67b01c13
KH
50 state = log.get_log_entry(stack.repository, ref,
51 stack.repository.rev_parse(ref))
3a3a4f2f
KH
52 else:
53 raise common.CmdException('Wrong number of arguments')
4ae6b67e 54 trans = transaction.StackTransaction(stack, 'reset',
f4e6a60e
KH
55 discard_changes = options.hard,
56 allow_bad_head = True)
4ae6b67e 57 try:
c70033b4
KH
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)
4ae6b67e
KH
63 except transaction.TransactionHalted:
64 pass
c70033b4 65 return trans.run(stack.repository.default_iw, allow_bad_head = not patches)