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.commands import common
21 from stgit.lib import git, log, transaction
22 from stgit.out import out
24 help = 'Reset the patch stack to an earlier state'
26 usage = ['<state> [<patchnames>]']
28 Reset the patch stack to an earlier state. The state is specified with
29 a commit from a stack log; for a branch foo, StGit stores the stack
30 log in foo.stgit^. So to undo the last N StGit commands (or rather,
31 the last N log entries; there is not an exact one-to-one
32 relationship), you would say
34 stg reset foo.stgit^~N
36 or, if you are not sure how many steps to undo, you can view the log
37 with "git log" or gitk
41 and then reset to any sha1 you see in the log.
43 If one or more patch names are given, reset only those patches, and
44 leave the rest alone."""
48 directory = common.DirectoryHasRepositoryLib()
50 def reset_stack(stack, iw, state, only_patches):
51 only_patches = set(only_patches)
54 return s & only_patches
57 patches_to_reset = mask(set(state.applied + state.unapplied + state.hidden))
58 existing_patches = set(stack.patchorder.all)
59 to_delete = mask(existing_patches - patches_to_reset)
60 trans = transaction.StackTransaction(stack, 'reset')
62 # If we have to change the stack base, we need to pop all patches
64 if not only_patches and trans.base != state.base:
65 trans.pop_patches(lambda pn: True)
66 out.info('Setting stack base to %s' % state.base.sha1)
67 trans.base = state.base
69 # In one go, do all the popping we have to in order to pop the
70 # patches we're going to delete or modify.
72 if only_patches and not pn in only_patches:
76 if stack.patches.get(pn).commit != state.patches.get(pn, None):
79 trans.pop_patches(mod)
81 # Delete and modify/create patches. We've previously popped all
82 # patches that we touch in this step.
83 trans.delete_patches(lambda pn: pn in to_delete)
84 for pn in patches_to_reset:
85 if pn in existing_patches:
86 if trans.patches[pn] == state.patches[pn]:
89 out.info('Resetting %s' % pn)
91 if pn in state.hidden:
92 trans.hidden.append(pn)
94 trans.unapplied.append(pn)
95 out.info('Resurrecting %s' % pn)
96 trans.patches[pn] = state.patches[pn]
98 # Push/pop patches as necessary.
101 # Push all the patches that we've popped, if they still
103 pushable = set(trans.unapplied)
104 for pn in stack.patchorder.applied:
106 trans.push_patch(pn, iw)
108 # Recreate the exact order specified by the goal state.
109 trans.reorder_patches(state.applied, state.unapplied,
111 except transaction.TransactionHalted:
115 def func(parser, options, args):
116 stack = directory.repository.current_stack
118 ref, patches = args[0], args[1:]
119 state = log.get_log_entry(stack.repository, ref)
121 raise common.CmdException('Wrong number of arguments')
122 return reset_stack(stack, stack.repository.default_iw, state, patches)