Commit | Line | Data |
---|---|---|
652b2e67 KH |
1 | """The L{StackTransaction} class makes it possible to make complex |
2 | updates to an StGit stack in a safe and convenient way.""" | |
3 | ||
afa3f9b9 KH |
4 | import atexit |
5 | ||
f9cc5e69 | 6 | from stgit import exception, utils |
352446d4 | 7 | from stgit.utils import any, all |
cbe4567e | 8 | from stgit.out import * |
dcd32afa | 9 | from stgit.lib import git |
cbe4567e KH |
10 | |
11 | class TransactionException(exception.StgException): | |
652b2e67 KH |
12 | """Exception raised when something goes wrong with a |
13 | L{StackTransaction}.""" | |
cbe4567e | 14 | |
dcd32afa | 15 | class TransactionHalted(TransactionException): |
652b2e67 KH |
16 | """Exception raised when a L{StackTransaction} stops part-way through. |
17 | Used to make a non-local jump from the transaction setup to the | |
18 | part of the transaction code where the transaction is run.""" | |
dcd32afa KH |
19 | |
20 | def _print_current_patch(old_applied, new_applied): | |
cbe4567e KH |
21 | def now_at(pn): |
22 | out.info('Now at patch "%s"' % pn) | |
23 | if not old_applied and not new_applied: | |
24 | pass | |
25 | elif not old_applied: | |
26 | now_at(new_applied[-1]) | |
27 | elif not new_applied: | |
28 | out.info('No patch applied') | |
29 | elif old_applied[-1] == new_applied[-1]: | |
30 | pass | |
31 | else: | |
32 | now_at(new_applied[-1]) | |
33 | ||
dcd32afa | 34 | class _TransPatchMap(dict): |
652b2e67 | 35 | """Maps patch names to sha1 strings.""" |
dcd32afa KH |
36 | def __init__(self, stack): |
37 | dict.__init__(self) | |
38 | self.__stack = stack | |
39 | def __getitem__(self, pn): | |
40 | try: | |
41 | return dict.__getitem__(self, pn) | |
42 | except KeyError: | |
43 | return self.__stack.patches.get(pn).commit | |
44 | ||
cbe4567e | 45 | class StackTransaction(object): |
652b2e67 KH |
46 | """A stack transaction, used for making complex updates to an StGit |
47 | stack in one single operation that will either succeed or fail | |
48 | cleanly. | |
49 | ||
50 | The basic theory of operation is the following: | |
51 | ||
52 | 1. Create a transaction object. | |
53 | ||
54 | 2. Inside a:: | |
55 | ||
56 | try | |
57 | ... | |
58 | except TransactionHalted: | |
59 | pass | |
60 | ||
61 | block, update the transaction with e.g. methods like | |
62 | L{pop_patches} and L{push_patch}. This may create new git | |
63 | objects such as commits, but will not write any refs; this means | |
64 | that in case of a fatal error we can just walk away, no clean-up | |
65 | required. | |
66 | ||
67 | (Some operations may need to touch your index and working tree, | |
68 | though. But they are cleaned up when needed.) | |
69 | ||
70 | 3. After the C{try} block -- wheher or not the setup ran to | |
71 | completion or halted part-way through by raising a | |
72 | L{TransactionHalted} exception -- call the transaction's L{run} | |
73 | method. This will either succeed in writing the updated state to | |
74 | your refs and index+worktree, or fail without having done | |
75 | anything.""" | |
781e549a | 76 | def __init__(self, stack, msg, allow_conflicts = False): |
cbe4567e KH |
77 | self.__stack = stack |
78 | self.__msg = msg | |
dcd32afa | 79 | self.__patches = _TransPatchMap(stack) |
cbe4567e KH |
80 | self.__applied = list(self.__stack.patchorder.applied) |
81 | self.__unapplied = list(self.__stack.patchorder.unapplied) | |
dcd2e3da | 82 | self.__hidden = list(self.__stack.patchorder.hidden) |
dcd32afa KH |
83 | self.__error = None |
84 | self.__current_tree = self.__stack.head.data.tree | |
3b0552a7 | 85 | self.__base = self.__stack.base |
781e549a KH |
86 | if isinstance(allow_conflicts, bool): |
87 | self.__allow_conflicts = lambda trans: allow_conflicts | |
88 | else: | |
89 | self.__allow_conflicts = allow_conflicts | |
afa3f9b9 | 90 | self.__temp_index = self.temp_index_tree = None |
dcd32afa KH |
91 | stack = property(lambda self: self.__stack) |
92 | patches = property(lambda self: self.__patches) | |
cbe4567e KH |
93 | def __set_applied(self, val): |
94 | self.__applied = list(val) | |
95 | applied = property(lambda self: self.__applied, __set_applied) | |
96 | def __set_unapplied(self, val): | |
97 | self.__unapplied = list(val) | |
98 | unapplied = property(lambda self: self.__unapplied, __set_unapplied) | |
dcd2e3da KH |
99 | def __set_hidden(self, val): |
100 | self.__hidden = list(val) | |
101 | hidden = property(lambda self: self.__hidden, __set_hidden) | |
3b0552a7 | 102 | def __set_base(self, val): |
980bde6a KH |
103 | assert (not self.__applied |
104 | or self.patches[self.applied[0]].data.parent == val) | |
3b0552a7 KH |
105 | self.__base = val |
106 | base = property(lambda self: self.__base, __set_base) | |
afa3f9b9 KH |
107 | @property |
108 | def temp_index(self): | |
109 | if not self.__temp_index: | |
110 | self.__temp_index = self.__stack.repository.temp_index() | |
111 | atexit.register(self.__temp_index.delete) | |
112 | return self.__temp_index | |
dcd32afa KH |
113 | def __checkout(self, tree, iw): |
114 | if not self.__stack.head_top_equal(): | |
115 | out.error( | |
116 | 'HEAD and top are not the same.', | |
117 | 'This can happen if you modify a branch with git.', | |
118 | '"stg repair --help" explains more about what to do next.') | |
119 | self.__abort() | |
781e549a KH |
120 | if self.__current_tree == tree: |
121 | # No tree change, but we still want to make sure that | |
122 | # there are no unresolved conflicts. Conflicts | |
123 | # conceptually "belong" to the topmost patch, and just | |
124 | # carrying them along to another patch is confusing. | |
125 | if (self.__allow_conflicts(self) or iw == None | |
126 | or not iw.index.conflicts()): | |
127 | return | |
128 | out.error('Need to resolve conflicts first') | |
129 | self.__abort() | |
130 | assert iw != None | |
131 | iw.checkout(self.__current_tree, tree) | |
132 | self.__current_tree = tree | |
dcd32afa KH |
133 | @staticmethod |
134 | def __abort(): | |
135 | raise TransactionException( | |
136 | 'Command aborted (all changes rolled back)') | |
cbe4567e | 137 | def __check_consistency(self): |
dcd2e3da | 138 | remaining = set(self.__applied + self.__unapplied + self.__hidden) |
cbe4567e KH |
139 | for pn, commit in self.__patches.iteritems(): |
140 | if commit == None: | |
141 | assert self.__stack.patches.exists(pn) | |
142 | else: | |
143 | assert pn in remaining | |
dcd32afa KH |
144 | @property |
145 | def __head(self): | |
cbe4567e | 146 | if self.__applied: |
dcd32afa | 147 | return self.__patches[self.__applied[-1]] |
cbe4567e | 148 | else: |
3b0552a7 | 149 | return self.__base |
59032ccd KH |
150 | def abort(self, iw = None): |
151 | # The only state we need to restore is index+worktree. | |
152 | if iw: | |
153 | self.__checkout(self.__stack.head.data.tree, iw) | |
bca31c2f | 154 | def run(self, iw = None, set_head = True): |
652b2e67 KH |
155 | """Execute the transaction. Will either succeed, or fail (with an |
156 | exception) and do nothing.""" | |
dcd32afa KH |
157 | self.__check_consistency() |
158 | new_head = self.__head | |
cbe4567e KH |
159 | |
160 | # Set branch head. | |
bca31c2f KH |
161 | if set_head: |
162 | if iw: | |
163 | try: | |
164 | self.__checkout(new_head.data.tree, iw) | |
165 | except git.CheckoutException: | |
166 | # We have to abort the transaction. | |
167 | self.abort(iw) | |
168 | self.__abort() | |
169 | self.__stack.set_head(new_head, self.__msg) | |
cbe4567e | 170 | |
dcd32afa KH |
171 | if self.__error: |
172 | out.error(self.__error) | |
173 | ||
cbe4567e KH |
174 | # Write patches. |
175 | for pn, commit in self.__patches.iteritems(): | |
176 | if self.__stack.patches.exists(pn): | |
177 | p = self.__stack.patches.get(pn) | |
178 | if commit == None: | |
179 | p.delete() | |
180 | else: | |
181 | p.set_commit(commit, self.__msg) | |
182 | else: | |
183 | self.__stack.patches.new(pn, commit, self.__msg) | |
dcd32afa | 184 | _print_current_patch(self.__stack.patchorder.applied, self.__applied) |
cbe4567e KH |
185 | self.__stack.patchorder.applied = self.__applied |
186 | self.__stack.patchorder.unapplied = self.__unapplied | |
dcd2e3da | 187 | self.__stack.patchorder.hidden = self.__hidden |
dcd32afa | 188 | |
f9cc5e69 KH |
189 | if self.__error: |
190 | return utils.STGIT_CONFLICT | |
191 | else: | |
192 | return utils.STGIT_SUCCESS | |
193 | ||
dcd32afa KH |
194 | def __halt(self, msg): |
195 | self.__error = msg | |
196 | raise TransactionHalted(msg) | |
197 | ||
198 | @staticmethod | |
199 | def __print_popped(popped): | |
200 | if len(popped) == 0: | |
201 | pass | |
202 | elif len(popped) == 1: | |
203 | out.info('Popped %s' % popped[0]) | |
204 | else: | |
205 | out.info('Popped %s -- %s' % (popped[-1], popped[0])) | |
206 | ||
207 | def pop_patches(self, p): | |
208 | """Pop all patches pn for which p(pn) is true. Return the list of | |
652b2e67 KH |
209 | other patches that had to be popped to accomplish this. Always |
210 | succeeds.""" | |
dcd32afa KH |
211 | popped = [] |
212 | for i in xrange(len(self.applied)): | |
213 | if p(self.applied[i]): | |
214 | popped = self.applied[i:] | |
215 | del self.applied[i:] | |
216 | break | |
217 | popped1 = [pn for pn in popped if not p(pn)] | |
218 | popped2 = [pn for pn in popped if p(pn)] | |
219 | self.unapplied = popped1 + popped2 + self.unapplied | |
220 | self.__print_popped(popped) | |
221 | return popped1 | |
222 | ||
223 | def delete_patches(self, p): | |
224 | """Delete all patches pn for which p(pn) is true. Return the list of | |
652b2e67 KH |
225 | other patches that had to be popped to accomplish this. Always |
226 | succeeds.""" | |
dcd32afa | 227 | popped = [] |
dcd2e3da | 228 | all_patches = self.applied + self.unapplied + self.hidden |
dcd32afa KH |
229 | for i in xrange(len(self.applied)): |
230 | if p(self.applied[i]): | |
231 | popped = self.applied[i:] | |
232 | del self.applied[i:] | |
233 | break | |
234 | popped = [pn for pn in popped if not p(pn)] | |
235 | self.unapplied = popped + [pn for pn in self.unapplied if not p(pn)] | |
dcd2e3da | 236 | self.hidden = [pn for pn in self.hidden if not p(pn)] |
dcd32afa KH |
237 | self.__print_popped(popped) |
238 | for pn in all_patches: | |
239 | if p(pn): | |
240 | s = ['', ' (empty)'][self.patches[pn].data.is_nochange()] | |
241 | self.patches[pn] = None | |
242 | out.info('Deleted %s%s' % (pn, s)) | |
243 | return popped | |
244 | ||
245 | def push_patch(self, pn, iw = None): | |
246 | """Attempt to push the named patch. If this results in conflicts, | |
247 | halts the transaction. If index+worktree are given, spill any | |
248 | conflicts to them.""" | |
352446d4 KH |
249 | orig_cd = self.patches[pn].data |
250 | cd = orig_cd.set_committer(None) | |
dcd32afa KH |
251 | s = ['', ' (empty)'][cd.is_nochange()] |
252 | oldparent = cd.parent | |
253 | cd = cd.set_parent(self.__head) | |
254 | base = oldparent.data.tree | |
255 | ours = cd.parent.data.tree | |
256 | theirs = cd.tree | |
afa3f9b9 KH |
257 | tree, self.temp_index_tree = self.temp_index.merge( |
258 | base, ours, theirs, self.temp_index_tree) | |
dcd32afa KH |
259 | merge_conflict = False |
260 | if not tree: | |
261 | if iw == None: | |
262 | self.__halt('%s does not apply cleanly' % pn) | |
263 | try: | |
264 | self.__checkout(ours, iw) | |
265 | except git.CheckoutException: | |
266 | self.__halt('Index/worktree dirty') | |
267 | try: | |
268 | iw.merge(base, ours, theirs) | |
269 | tree = iw.index.write_tree() | |
270 | self.__current_tree = tree | |
271 | s = ' (modified)' | |
363d432f | 272 | except git.MergeConflictException: |
dcd32afa KH |
273 | tree = ours |
274 | merge_conflict = True | |
275 | s = ' (conflict)' | |
363d432f KH |
276 | except git.MergeException, e: |
277 | self.__halt(str(e)) | |
dcd32afa | 278 | cd = cd.set_tree(tree) |
352446d4 KH |
279 | if any(getattr(cd, a) != getattr(orig_cd, a) for a in |
280 | ['parent', 'tree', 'author', 'message']): | |
281 | self.patches[pn] = self.__stack.repository.commit(cd) | |
282 | else: | |
283 | s = ' (unmodified)' | |
dcd2e3da KH |
284 | if pn in self.hidden: |
285 | x = self.hidden | |
286 | else: | |
287 | x = self.unapplied | |
288 | del x[x.index(pn)] | |
dcd32afa KH |
289 | self.applied.append(pn) |
290 | out.info('Pushed %s%s' % (pn, s)) | |
291 | if merge_conflict: | |
781e549a KH |
292 | # We've just caused conflicts, so we must allow them in |
293 | # the final checkout. | |
294 | self.__allow_conflicts = lambda trans: True | |
295 | ||
dcd32afa | 296 | self.__halt('Merge conflict') |