1 # -*- coding: utf-8 -*-
4 Copyright (C) 2008, Karl Hasselström <kha@treskal.com>
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.
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.
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
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
25 help = 'Reset the patch stack to an earlier state'
27 usage = ['[options] <state> [<patchnames>]']
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
35 stg reset foo.stgit^~N
37 or, if you are not sure how many steps to undo, you can view the log
38 with "git log" or gitk
42 and then reset to any sha1 you see in the log.
44 If one or more patch names are given, reset only those patches, and
45 leave the rest alone."""
48 opt('--hard', action = 'store_true',
49 short = 'Discard changes in your index/worktree')]
51 directory = common.DirectoryHasRepositoryLib()
53 def reset_stack(stack, iw, state, only_patches, hard):
54 only_patches = set(only_patches)
57 return s & only_patches
60 patches_to_reset = mask(set(state.applied + state.unapplied + state.hidden))
61 existing_patches = set(stack.patchorder.all)
62 to_delete = mask(existing_patches - patches_to_reset)
63 trans = transaction.StackTransaction(stack, 'reset', discard_changes = hard)
65 # If we have to change the stack base, we need to pop all patches
67 if not only_patches and trans.base != state.base:
68 trans.pop_patches(lambda pn: True)
69 out.info('Setting stack base to %s' % state.base.sha1)
70 trans.base = state.base
72 # In one go, do all the popping we have to in order to pop the
73 # patches we're going to delete or modify.
75 if only_patches and not pn in only_patches:
79 if stack.patches.get(pn).commit != state.patches.get(pn, None):
82 trans.pop_patches(mod)
84 # Delete and modify/create patches. We've previously popped all
85 # patches that we touch in this step.
86 trans.delete_patches(lambda pn: pn in to_delete)
87 for pn in patches_to_reset:
88 if pn in existing_patches:
89 if trans.patches[pn] == state.patches[pn]:
92 out.info('Resetting %s' % pn)
94 if pn in state.hidden:
95 trans.hidden.append(pn)
97 trans.unapplied.append(pn)
98 out.info('Resurrecting %s' % pn)
99 trans.patches[pn] = state.patches[pn]
101 # Push/pop patches as necessary.
104 # Push all the patches that we've popped, if they still
106 pushable = set(trans.unapplied)
107 for pn in stack.patchorder.applied:
109 trans.push_patch(pn, iw)
111 # Recreate the exact order specified by the goal state.
112 trans.reorder_patches(state.applied, state.unapplied,
114 except transaction.TransactionHalted:
118 def func(parser, options, args):
119 stack = directory.repository.current_stack
121 ref, patches = args[0], args[1:]
122 state = log.get_log_entry(stack.repository, ref)
124 raise common.CmdException('Wrong number of arguments')
125 return reset_stack(stack, stack.repository.default_iw, state, patches,