1 """The L{StackTransaction} class makes it possible to make complex
2 updates to an StGit stack in a safe and convenient way."""
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
12 class TransactionException(exception.StgException):
13 """Exception raised when something goes wrong with a
14 L{StackTransaction}."""
16 class TransactionHalted(TransactionException):
17 """Exception raised when a L{StackTransaction} stops part-way through.
18 Used to make a non-local jump from the transaction setup to the
19 part of the transaction code where the transaction is run."""
21 def _print_current_patch(old_applied, new_applied):
23 out.info('Now at patch "%s"' % pn)
24 if not old_applied and not new_applied:
27 now_at(new_applied[-1])
29 out.info('No patch applied')
30 elif old_applied[-1] == new_applied[-1]:
33 now_at(new_applied[-1])
35 class _TransPatchMap(dict):
36 """Maps patch names to sha1 strings."""
37 def __init__(self, stack):
40 def __getitem__(self, pn):
42 return dict.__getitem__(self, pn)
44 return self.__stack.patches.get(pn).commit
46 class StackTransaction(object):
47 """A stack transaction, used for making complex updates to an StGit
48 stack in one single operation that will either succeed or fail
51 The basic theory of operation is the following:
53 1. Create a transaction object.
59 except TransactionHalted:
62 block, update the transaction with e.g. methods like
63 L{pop_patches} and L{push_patch}. This may create new git
64 objects such as commits, but will not write any refs; this means
65 that in case of a fatal error we can just walk away, no clean-up
68 (Some operations may need to touch your index and working tree,
69 though. But they are cleaned up when needed.)
71 3. After the C{try} block -- wheher or not the setup ran to
72 completion or halted part-way through by raising a
73 L{TransactionHalted} exception -- call the transaction's L{run}
74 method. This will either succeed in writing the updated state to
75 your refs and index+worktree, or fail without having done
77 def __init__(self, stack, msg, allow_conflicts = False):
80 self.__patches = _TransPatchMap(stack)
81 self.__applied = list(self.__stack.patchorder.applied)
82 self.__unapplied = list(self.__stack.patchorder.unapplied)
83 self.__hidden = list(self.__stack.patchorder.hidden)
85 self.__current_tree = self.__stack.head.data.tree
86 self.__base = self.__stack.base
87 if isinstance(allow_conflicts, bool):
88 self.__allow_conflicts = lambda trans: allow_conflicts
90 self.__allow_conflicts = allow_conflicts
91 self.__temp_index = self.temp_index_tree = None
92 stack = property(lambda self: self.__stack)
93 patches = property(lambda self: self.__patches)
94 def __set_applied(self, val):
95 self.__applied = list(val)
96 applied = property(lambda self: self.__applied, __set_applied)
97 def __set_unapplied(self, val):
98 self.__unapplied = list(val)
99 unapplied = property(lambda self: self.__unapplied, __set_unapplied)
100 def __set_hidden(self, val):
101 self.__hidden = list(val)
102 hidden = property(lambda self: self.__hidden, __set_hidden)
103 def __set_base(self, val):
104 assert (not self.__applied
105 or self.patches[self.applied[0]].data.parent == val)
107 base = property(lambda self: self.__base, __set_base)
109 def temp_index(self):
110 if not self.__temp_index:
111 self.__temp_index = self.__stack.repository.temp_index()
112 atexit.register(self.__temp_index.delete)
113 return self.__temp_index
114 def __checkout(self, tree, iw):
115 if not self.__stack.head_top_equal():
117 'HEAD and top are not the same.',
118 'This can happen if you modify a branch with git.',
119 '"stg repair --help" explains more about what to do next.')
121 if self.__current_tree == tree:
122 # No tree change, but we still want to make sure that
123 # there are no unresolved conflicts. Conflicts
124 # conceptually "belong" to the topmost patch, and just
125 # carrying them along to another patch is confusing.
126 if (self.__allow_conflicts(self) or iw == None
127 or not iw.index.conflicts()):
129 out.error('Need to resolve conflicts first')
132 iw.checkout(self.__current_tree, tree)
133 self.__current_tree = tree
136 raise TransactionException(
137 'Command aborted (all changes rolled back)')
138 def __check_consistency(self):
139 remaining = set(self.__applied + self.__unapplied + self.__hidden)
140 for pn, commit in self.__patches.iteritems():
142 assert self.__stack.patches.exists(pn)
144 assert pn in remaining
148 return self.__patches[self.__applied[-1]]
151 def abort(self, iw = None):
152 # The only state we need to restore is index+worktree.
154 self.__checkout(self.__stack.head.data.tree, iw)
155 def run(self, iw = None, set_head = True):
156 """Execute the transaction. Will either succeed, or fail (with an
157 exception) and do nothing."""
158 self.__check_consistency()
159 new_head = self.__head
165 self.__checkout(new_head.data.tree, iw)
166 except git.CheckoutException:
167 # We have to abort the transaction.
170 self.__stack.set_head(new_head, self.__msg)
173 out.error(self.__error)
176 for pn, commit in self.__patches.iteritems():
177 if self.__stack.patches.exists(pn):
178 p = self.__stack.patches.get(pn)
182 p.set_commit(commit, self.__msg)
184 self.__stack.patches.new(pn, commit, self.__msg)
185 _print_current_patch(self.__stack.patchorder.applied, self.__applied)
186 self.__stack.patchorder.applied = self.__applied
187 self.__stack.patchorder.unapplied = self.__unapplied
188 self.__stack.patchorder.hidden = self.__hidden
189 log.log_entry(self.__stack, self.__msg)
192 return utils.STGIT_CONFLICT
194 return utils.STGIT_SUCCESS
196 def __halt(self, msg):
198 raise TransactionHalted(msg)
201 def __print_popped(popped):
204 elif len(popped) == 1:
205 out.info('Popped %s' % popped[0])
207 out.info('Popped %s -- %s' % (popped[-1], popped[0]))
209 def pop_patches(self, p):
210 """Pop all patches pn for which p(pn) is true. Return the list of
211 other patches that had to be popped to accomplish this. Always
214 for i in xrange(len(self.applied)):
215 if p(self.applied[i]):
216 popped = self.applied[i:]
219 popped1 = [pn for pn in popped if not p(pn)]
220 popped2 = [pn for pn in popped if p(pn)]
221 self.unapplied = popped1 + popped2 + self.unapplied
222 self.__print_popped(popped)
225 def delete_patches(self, p):
226 """Delete all patches pn for which p(pn) is true. Return the list of
227 other patches that had to be popped to accomplish this. Always
230 all_patches = self.applied + self.unapplied + self.hidden
231 for i in xrange(len(self.applied)):
232 if p(self.applied[i]):
233 popped = self.applied[i:]
236 popped = [pn for pn in popped if not p(pn)]
237 self.unapplied = popped + [pn for pn in self.unapplied if not p(pn)]
238 self.hidden = [pn for pn in self.hidden if not p(pn)]
239 self.__print_popped(popped)
240 for pn in all_patches:
242 s = ['', ' (empty)'][self.patches[pn].data.is_nochange()]
243 self.patches[pn] = None
244 out.info('Deleted %s%s' % (pn, s))
247 def push_patch(self, pn, iw = None):
248 """Attempt to push the named patch. If this results in conflicts,
249 halts the transaction. If index+worktree are given, spill any
250 conflicts to them."""
251 orig_cd = self.patches[pn].data
252 cd = orig_cd.set_committer(None)
253 s = ['', ' (empty)'][cd.is_nochange()]
254 oldparent = cd.parent
255 cd = cd.set_parent(self.__head)
256 base = oldparent.data.tree
257 ours = cd.parent.data.tree
259 tree, self.temp_index_tree = self.temp_index.merge(
260 base, ours, theirs, self.temp_index_tree)
261 merge_conflict = False
264 self.__halt('%s does not apply cleanly' % pn)
266 self.__checkout(ours, iw)
267 except git.CheckoutException:
268 self.__halt('Index/worktree dirty')
270 iw.merge(base, ours, theirs)
271 tree = iw.index.write_tree()
272 self.__current_tree = tree
274 except git.MergeConflictException:
276 merge_conflict = True
278 except git.MergeException, e:
280 cd = cd.set_tree(tree)
281 if any(getattr(cd, a) != getattr(orig_cd, a) for a in
282 ['parent', 'tree', 'author', 'message']):
283 self.patches[pn] = self.__stack.repository.commit(cd)
286 if pn in self.hidden:
291 self.applied.append(pn)
292 out.info('Pushed %s%s' % (pn, s))
294 # We've just caused conflicts, so we must allow them in
295 # the final checkout.
296 self.__allow_conflicts = lambda trans: True
298 self.__halt('Merge conflict')
300 def reorder_patches(self, applied, unapplied, hidden, iw = None):
301 """Push and pop patches to attain the given ordering."""
302 common = len(list(it.takewhile(lambda (a, b): a == b,
303 zip(self.applied, applied))))
304 to_pop = set(self.applied[common:])
305 self.pop_patches(lambda pn: pn in to_pop)
306 for pn in applied[common:]:
307 self.push_patch(pn, iw)
308 assert self.applied == applied
309 assert set(self.unapplied + self.hidden) == set(unapplied + hidden)
310 self.unapplied = unapplied