chiark / gitweb /
b6378417d6217b09802da8d2e7694c67750a58a0
[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.commands import common
21 from stgit.lib import git, log, transaction
22 from stgit.out import out
23
24 help = 'Reset the patch stack to an earlier state'
25 kind = 'stack'
26 usage = ['<state> [<patchnames>]']
27 description = """
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
33
34   stg reset foo.stgit^~N
35
36 or, if you are not sure how many steps to undo, you can view the log
37 with "git log" or gitk
38
39   gitk foo.stgit^
40
41 and then reset to any sha1 you see in the log.
42
43 If one or more patch names are given, reset only those patches, and
44 leave the rest alone."""
45
46 options = []
47
48 directory = common.DirectoryHasRepositoryLib()
49
50 def reset_stack(stack, iw, state, only_patches):
51     only_patches = set(only_patches)
52     def mask(s):
53         if only_patches:
54             return s & only_patches
55         else:
56             return s
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')
61
62     # If we have to change the stack base, we need to pop all patches
63     # first.
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
68
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.
71     def mod(pn):
72         if only_patches and not pn in only_patches:
73             return False
74         if pn in to_delete:
75             return True
76         if stack.patches.get(pn).commit != state.patches.get(pn, None):
77             return True
78         return False
79     trans.pop_patches(mod)
80
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]:
87                 continue
88             else:
89                 out.info('Resetting %s' % pn)
90         else:
91             if pn in state.hidden:
92                 trans.hidden.append(pn)
93             else:
94                 trans.unapplied.append(pn)
95             out.info('Resurrecting %s' % pn)
96         trans.patches[pn] = state.patches[pn]
97
98     # Push/pop patches as necessary.
99     try:
100         if only_patches:
101             # Push all the patches that we've popped, if they still
102             # exist.
103             pushable = set(trans.unapplied)
104             for pn in stack.patchorder.applied:
105                 if pn in pushable:
106                     trans.push_patch(pn, iw)
107         else:
108             # Recreate the exact order specified by the goal state.
109             trans.reorder_patches(state.applied, state.unapplied,
110                                   state.hidden, iw)
111     except transaction.TransactionHalted:
112         pass
113     return trans.run(iw)
114
115 def func(parser, options, args):
116     stack = directory.repository.current_stack
117     if len(args) >= 1:
118         ref, patches = args[0], args[1:]
119         state = log.get_log_entry(stack.repository, ref)
120     else:
121         raise common.CmdException('Wrong number of arguments')
122     return reset_stack(stack, stack.repository.default_iw, state, patches)