13323dddc7d8fa3583310df17ec8e8bf4fe4b822
[stgit] / stgit / lib / transaction.py
1 """The L{StackTransaction} class makes it possible to make complex
2 updates to an StGit stack in a safe and convenient way."""
3
4 import atexit
5 import itertools as it
6
7 from stgit import exception, utils
8 from stgit.utils import any, all
9 from stgit.out import *
10 from stgit.lib import git, log
11 from stgit.config import config
12
13 class TransactionException(exception.StgException):
14 """Exception raised when something goes wrong with a
15 L{StackTransaction}."""
16
17 class TransactionHalted(TransactionException):
18 """Exception raised when a L{StackTransaction} stops part-way through.
19 Used to make a non-local jump from the transaction setup to the
20 part of the transaction code where the transaction is run."""
21
22 def _print_current_patch(old_applied, new_applied):
23 def now_at(pn):
24 out.info('Now at patch "%s"' % pn)
25 if not old_applied and not new_applied:
26 pass
27 elif not old_applied:
28 now_at(new_applied[-1])
29 elif not new_applied:
30 out.info('No patch applied')
31 elif old_applied[-1] == new_applied[-1]:
32 pass
33 else:
34 now_at(new_applied[-1])
35
36 class _TransPatchMap(dict):
37 """Maps patch names to sha1 strings."""
38 def __init__(self, stack):
39 dict.__init__(self)
40 self.__stack = stack
41 def __getitem__(self, pn):
42 try:
43 return dict.__getitem__(self, pn)
44 except KeyError:
45 return self.__stack.patches.get(pn).commit
46
47 class StackTransaction(object):
48 """A stack transaction, used for making complex updates to an StGit
49 stack in one single operation that will either succeed or fail
50 cleanly.
51
52 The basic theory of operation is the following:
53
54 1. Create a transaction object.
55
56 2. Inside a::
57
58 try
59 ...
60 except TransactionHalted:
61 pass
62
63 block, update the transaction with e.g. methods like
64 L{pop_patches} and L{push_patch}. This may create new git
65 objects such as commits, but will not write any refs; this means
66 that in case of a fatal error we can just walk away, no clean-up
67 required.
68
69 (Some operations may need to touch your index and working tree,
70 though. But they are cleaned up when needed.)
71
72 3. After the C{try} block -- wheher or not the setup ran to
73 completion or halted part-way through by raising a
74 L{TransactionHalted} exception -- call the transaction's L{run}
75 method. This will either succeed in writing the updated state to
76 your refs and index+worktree, or fail without having done
77 anything."""
78 def __init__(self, stack, msg, discard_changes = False,
79 allow_conflicts = False, allow_bad_head = False,
80 check_clean_iw = None):
81 """Create a new L{StackTransaction}.
82
83 @param discard_changes: Discard any changes in index+worktree
84 @type discard_changes: bool
85 @param allow_conflicts: Whether to allow pre-existing conflicts
86 @type allow_conflicts: bool or function of L{StackTransaction}"""
87 self.__stack = stack
88 self.__msg = msg
89 self.__patches = _TransPatchMap(stack)
90 self.__applied = list(self.__stack.patchorder.applied)
91 self.__unapplied = list(self.__stack.patchorder.unapplied)
92 self.__hidden = list(self.__stack.patchorder.hidden)
93 self.__conflicting_push = None
94 self.__error = None
95 self.__current_tree = self.__stack.head.data.tree
96 self.__base = self.__stack.base
97 self.__discard_changes = discard_changes
98 self.__bad_head = None
99 self.__conflicts = None
100 if isinstance(allow_conflicts, bool):
101 self.__allow_conflicts = lambda trans: allow_conflicts
102 else:
103 self.__allow_conflicts = allow_conflicts
104 self.__temp_index = self.temp_index_tree = None
105 if not allow_bad_head:
106 self.__assert_head_top_equal()
107 if check_clean_iw:
108 self.__assert_index_worktree_clean(check_clean_iw)
109 stack = property(lambda self: self.__stack)
110 patches = property(lambda self: self.__patches)
111 def __set_applied(self, val):
112 self.__applied = list(val)
113 applied = property(lambda self: self.__applied, __set_applied)
114 def __set_unapplied(self, val):
115 self.__unapplied = list(val)
116 unapplied = property(lambda self: self.__unapplied, __set_unapplied)
117 def __set_hidden(self, val):
118 self.__hidden = list(val)
119 hidden = property(lambda self: self.__hidden, __set_hidden)
120 all_patches = property(lambda self: (self.__applied + self.__unapplied
121 + self.__hidden))
122 def __set_base(self, val):
123 assert (not self.__applied
124 or self.patches[self.applied[0]].data.parent == val)
125 self.__base = val
126 base = property(lambda self: self.__base, __set_base)
127 @property
128 def temp_index(self):
129 if not self.__temp_index:
130 self.__temp_index = self.__stack.repository.temp_index()
131 atexit.register(self.__temp_index.delete)
132 return self.__temp_index
133 @property
134 def top(self):
135 if self.__applied:
136 return self.__patches[self.__applied[-1]]
137 else:
138 return self.__base
139 def __get_head(self):
140 if self.__bad_head:
141 return self.__bad_head
142 else:
143 return self.top
144 def __set_head(self, val):
145 self.__bad_head = val
146 head = property(__get_head, __set_head)
147 def __assert_head_top_equal(self):
148 if not self.__stack.head_top_equal():
149 out.error(
150 'HEAD and top are not the same.',
151 'This can happen if you modify a branch with git.',
152 '"stg repair --help" explains more about what to do next.')
153 self.__abort()
154 def __assert_index_worktree_clean(self, iw):
155 if not iw.worktree_clean():
156 self.__halt('Worktree not clean. Use "refresh" or "status --reset"')
157 if not iw.index.is_clean(self.stack.head):
158 self.__halt('Index not clean. Use "refresh" or "status --reset"')
159 def __checkout(self, tree, iw, allow_bad_head):
160 if not allow_bad_head:
161 self.__assert_head_top_equal()
162 if self.__current_tree == tree and not self.__discard_changes:
163 # No tree change, but we still want to make sure that
164 # there are no unresolved conflicts. Conflicts
165 # conceptually "belong" to the topmost patch, and just
166 # carrying them along to another patch is confusing.
167 if (self.__allow_conflicts(self) or iw == None
168 or not iw.index.conflicts()):
169 return
170 out.error('Need to resolve conflicts first')
171 self.__abort()
172 assert iw != None
173 if self.__discard_changes:
174 iw.checkout_hard(tree)
175 else:
176 iw.checkout(self.__current_tree, tree)
177 self.__current_tree = tree
178 @staticmethod
179 def __abort():
180 raise TransactionException(
181 'Command aborted (all changes rolled back)')
182 def __check_consistency(self):
183 remaining = set(self.all_patches)
184 for pn, commit in self.__patches.iteritems():
185 if commit == None:
186 assert self.__stack.patches.exists(pn)
187 else:
188 assert pn in remaining
189 def abort(self, iw = None):
190 # The only state we need to restore is index+worktree.
191 if iw:
192 self.__checkout(self.__stack.head.data.tree, iw,
193 allow_bad_head = True)
194 def run(self, iw = None, set_head = True, allow_bad_head = False,
195 print_current_patch = True):
196 """Execute the transaction. Will either succeed, or fail (with an
197 exception) and do nothing."""
198 self.__check_consistency()
199 log.log_external_mods(self.__stack)
200 new_head = self.head
201
202 # Set branch head.
203 if set_head:
204 if iw:
205 try:
206 self.__checkout(new_head.data.tree, iw, allow_bad_head)
207 except git.CheckoutException:
208 # We have to abort the transaction.
209 self.abort(iw)
210 self.__abort()
211 self.__stack.set_head(new_head, self.__msg)
212
213 if self.__error:
214 if self.__conflicts:
215 out.error(*([self.__error] + self.__conflicts))
216 else:
217 out.error(self.__error)
218
219 # Write patches.
220 def write(msg):
221 for pn, commit in self.__patches.iteritems():
222 if self.__stack.patches.exists(pn):
223 p = self.__stack.patches.get(pn)
224 if commit == None:
225 p.delete()
226 else:
227 p.set_commit(commit, msg)
228 else:
229 self.__stack.patches.new(pn, commit, msg)
230 self.__stack.patchorder.applied = self.__applied
231 self.__stack.patchorder.unapplied = self.__unapplied
232 self.__stack.patchorder.hidden = self.__hidden
233 log.log_entry(self.__stack, msg)
234 old_applied = self.__stack.patchorder.applied
235 write(self.__msg)
236 if self.__conflicting_push != None:
237 self.__patches = _TransPatchMap(self.__stack)
238 self.__conflicting_push()
239 write(self.__msg + ' (CONFLICT)')
240 if print_current_patch:
241 _print_current_patch(old_applied, self.__applied)
242
243 if self.__error:
244 return utils.STGIT_CONFLICT
245 else:
246 return utils.STGIT_SUCCESS
247
248 def __halt(self, msg):
249 self.__error = msg
250 raise TransactionHalted(msg)
251
252 @staticmethod
253 def __print_popped(popped):
254 if len(popped) == 0:
255 pass
256 elif len(popped) == 1:
257 out.info('Popped %s' % popped[0])
258 else:
259 out.info('Popped %s -- %s' % (popped[-1], popped[0]))
260
261 def pop_patches(self, p):
262 """Pop all patches pn for which p(pn) is true. Return the list of
263 other patches that had to be popped to accomplish this. Always
264 succeeds."""
265 popped = []
266 for i in xrange(len(self.applied)):
267 if p(self.applied[i]):
268 popped = self.applied[i:]
269 del self.applied[i:]
270 break
271 popped1 = [pn for pn in popped if not p(pn)]
272 popped2 = [pn for pn in popped if p(pn)]
273 self.unapplied = popped1 + popped2 + self.unapplied
274 self.__print_popped(popped)
275 return popped1
276
277 def delete_patches(self, p, quiet = False):
278 """Delete all patches pn for which p(pn) is true. Return the list of
279 other patches that had to be popped to accomplish this. Always
280 succeeds."""
281 popped = []
282 all_patches = self.applied + self.unapplied + self.hidden
283 for i in xrange(len(self.applied)):
284 if p(self.applied[i]):
285 popped = self.applied[i:]
286 del self.applied[i:]
287 break
288 popped = [pn for pn in popped if not p(pn)]
289 self.unapplied = popped + [pn for pn in self.unapplied if not p(pn)]
290 self.hidden = [pn for pn in self.hidden if not p(pn)]
291 self.__print_popped(popped)
292 for pn in all_patches:
293 if p(pn):
294 s = ['', ' (empty)'][self.patches[pn].data.is_nochange()]
295 self.patches[pn] = None
296 if not quiet:
297 out.info('Deleted %s%s' % (pn, s))
298 return popped
299
300 def push_patch(self, pn, iw = None, allow_interactive = False,
301 already_merged = False):
302 """Attempt to push the named patch. If this results in conflicts,
303 halts the transaction. If index+worktree are given, spill any
304 conflicts to them."""
305 out.start('Pushing patch "%s"' % pn)
306 orig_cd = self.patches[pn].data
307 cd = orig_cd.set_committer(None)
308 oldparent = cd.parent
309 cd = cd.set_parent(self.top)
310 if already_merged:
311 # the resulting patch is empty
312 tree = cd.parent.data.tree
313 else:
314 base = oldparent.data.tree
315 ours = cd.parent.data.tree
316 theirs = cd.tree
317 tree, self.temp_index_tree = self.temp_index.merge(
318 base, ours, theirs, self.temp_index_tree)
319 s = ''
320 merge_conflict = False
321 if not tree:
322 if iw == None:
323 self.__halt('%s does not apply cleanly' % pn)
324 try:
325 self.__checkout(ours, iw, allow_bad_head = False)
326 except git.CheckoutException:
327 self.__halt('Index/worktree dirty')
328 try:
329 interactive = (allow_interactive and
330 config.get('stgit.autoimerge') == 'yes')
331 iw.merge(base, ours, theirs, interactive = interactive)
332 tree = iw.index.write_tree()
333 self.__current_tree = tree
334 s = 'modified'
335 except git.MergeConflictException, e:
336 tree = ours
337 merge_conflict = True
338 self.__conflicts = e.conflicts
339 s = 'conflict'
340 except git.MergeException, e:
341 self.__halt(str(e))
342 cd = cd.set_tree(tree)
343 if any(getattr(cd, a) != getattr(orig_cd, a) for a in
344 ['parent', 'tree', 'author', 'message']):
345 comm = self.__stack.repository.commit(cd)
346 self.head = comm
347 else:
348 comm = None
349 s = 'unmodified'
350 if already_merged:
351 s = 'merged'
352 elif not merge_conflict and cd.is_nochange():
353 s = 'empty'
354 out.done(s)
355 def update():
356 if comm:
357 self.patches[pn] = comm
358 if pn in self.hidden:
359 x = self.hidden
360 else:
361 x = self.unapplied
362 del x[x.index(pn)]
363 self.applied.append(pn)
364 if merge_conflict:
365 # We've just caused conflicts, so we must allow them in
366 # the final checkout.
367 self.__allow_conflicts = lambda trans: True
368
369 # Save this update so that we can run it a little later.
370 self.__conflicting_push = update
371 self.__halt("%d merge conflict(s)" % len(self.__conflicts))
372 else:
373 # Update immediately.
374 update()
375
376 def reorder_patches(self, applied, unapplied, hidden = None, iw = None):
377 """Push and pop patches to attain the given ordering."""
378 if hidden is None:
379 hidden = self.hidden
380 common = len(list(it.takewhile(lambda (a, b): a == b,
381 zip(self.applied, applied))))
382 to_pop = set(self.applied[common:])
383 self.pop_patches(lambda pn: pn in to_pop)
384 for pn in applied[common:]:
385 self.push_patch(pn, iw)
386 assert self.applied == applied
387 assert set(self.unapplied + self.hidden) == set(unapplied + hidden)
388 self.unapplied = unapplied
389 self.hidden = hidden
390
391 def check_merged(self, patches):
392 """Return a subset of patches already merged."""
393 out.start('Checking for patches merged upstream')
394 merged = []
395 if self.temp_index_tree != self.stack.head.data.tree:
396 self.temp_index.read_tree(self.stack.head.data.tree)
397 self.temp_index_tree = self.stack.head.data.tree
398 for pn in reversed(patches):
399 # check whether patch changes can be reversed in the current index
400 cd = self.patches[pn].data
401 if cd.is_nochange():
402 continue
403 try:
404 self.temp_index.apply_treediff(cd.tree, cd.parent.data.tree,
405 quiet = True)
406 merged.append(pn)
407 # The self.temp_index was modified by apply_treediff() so
408 # force read_tree() the next time merge() is used.
409 self.temp_index_tree = None
410 except git.MergeException:
411 pass
412 out.done('%d found' % len(merged))
413 return merged