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