chiark / gitweb /
84d72d57655c6832b2a946bd06b5142ecbbce0ab
[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):
79         """Create a new L{StackTransaction}.
80
81         @param discard_changes: Discard any changes in index+worktree
82         @type discard_changes: bool
83         @param allow_conflicts: Whether to allow pre-existing conflicts
84         @type allow_conflicts: bool or function of L{StackTransaction}"""
85         self.__stack = stack
86         self.__msg = msg
87         self.__patches = _TransPatchMap(stack)
88         self.__applied = list(self.__stack.patchorder.applied)
89         self.__unapplied = list(self.__stack.patchorder.unapplied)
90         self.__hidden = list(self.__stack.patchorder.hidden)
91         self.__conflicting_push = None
92         self.__error = None
93         self.__current_tree = self.__stack.head.data.tree
94         self.__base = self.__stack.base
95         self.__discard_changes = discard_changes
96         if isinstance(allow_conflicts, bool):
97             self.__allow_conflicts = lambda trans: allow_conflicts
98         else:
99             self.__allow_conflicts = allow_conflicts
100         self.__temp_index = self.temp_index_tree = None
101     stack = property(lambda self: self.__stack)
102     patches = property(lambda self: self.__patches)
103     def __set_applied(self, val):
104         self.__applied = list(val)
105     applied = property(lambda self: self.__applied, __set_applied)
106     def __set_unapplied(self, val):
107         self.__unapplied = list(val)
108     unapplied = property(lambda self: self.__unapplied, __set_unapplied)
109     def __set_hidden(self, val):
110         self.__hidden = list(val)
111     hidden = property(lambda self: self.__hidden, __set_hidden)
112     def __set_base(self, val):
113         assert (not self.__applied
114                 or self.patches[self.applied[0]].data.parent == val)
115         self.__base = val
116     base = property(lambda self: self.__base, __set_base)
117     @property
118     def temp_index(self):
119         if not self.__temp_index:
120             self.__temp_index = self.__stack.repository.temp_index()
121             atexit.register(self.__temp_index.delete)
122         return self.__temp_index
123     def __checkout(self, tree, iw):
124         if not self.__stack.head_top_equal():
125             out.error(
126                 'HEAD and top are not the same.',
127                 'This can happen if you modify a branch with git.',
128                 '"stg repair --help" explains more about what to do next.')
129             self.__abort()
130         if self.__current_tree == tree and not self.__discard_changes:
131             # No tree change, but we still want to make sure that
132             # there are no unresolved conflicts. Conflicts
133             # conceptually "belong" to the topmost patch, and just
134             # carrying them along to another patch is confusing.
135             if (self.__allow_conflicts(self) or iw == None
136                 or not iw.index.conflicts()):
137                 return
138             out.error('Need to resolve conflicts first')
139             self.__abort()
140         assert iw != None
141         if self.__discard_changes:
142             iw.checkout_hard(tree)
143         else:
144             iw.checkout(self.__current_tree, tree)
145         self.__current_tree = tree
146     @staticmethod
147     def __abort():
148         raise TransactionException(
149             'Command aborted (all changes rolled back)')
150     def __check_consistency(self):
151         remaining = set(self.__applied + self.__unapplied + self.__hidden)
152         for pn, commit in self.__patches.iteritems():
153             if commit == None:
154                 assert self.__stack.patches.exists(pn)
155             else:
156                 assert pn in remaining
157     @property
158     def __head(self):
159         if self.__applied:
160             return self.__patches[self.__applied[-1]]
161         else:
162             return self.__base
163     def abort(self, iw = None):
164         # The only state we need to restore is index+worktree.
165         if iw:
166             self.__checkout(self.__stack.head.data.tree, iw)
167     def run(self, iw = None, set_head = True):
168         """Execute the transaction. Will either succeed, or fail (with an
169         exception) and do nothing."""
170         self.__check_consistency()
171         new_head = self.__head
172
173         # Set branch head.
174         if set_head:
175             if iw:
176                 try:
177                     self.__checkout(new_head.data.tree, iw)
178                 except git.CheckoutException:
179                     # We have to abort the transaction.
180                     self.abort(iw)
181                     self.__abort()
182             self.__stack.set_head(new_head, self.__msg)
183
184         if self.__error:
185             out.error(self.__error)
186
187         # Write patches.
188         def write(msg):
189             for pn, commit in self.__patches.iteritems():
190                 if self.__stack.patches.exists(pn):
191                     p = self.__stack.patches.get(pn)
192                     if commit == None:
193                         p.delete()
194                     else:
195                         p.set_commit(commit, msg)
196                 else:
197                     self.__stack.patches.new(pn, commit, msg)
198             self.__stack.patchorder.applied = self.__applied
199             self.__stack.patchorder.unapplied = self.__unapplied
200             self.__stack.patchorder.hidden = self.__hidden
201             log.log_entry(self.__stack, msg)
202         old_applied = self.__stack.patchorder.applied
203         write(self.__msg)
204         if self.__conflicting_push != None:
205             self.__patches = _TransPatchMap(self.__stack)
206             self.__conflicting_push()
207             write(self.__msg + ' (CONFLICT)')
208         _print_current_patch(old_applied, self.__applied)
209
210         if self.__error:
211             return utils.STGIT_CONFLICT
212         else:
213             return utils.STGIT_SUCCESS
214
215     def __halt(self, msg):
216         self.__error = msg
217         raise TransactionHalted(msg)
218
219     @staticmethod
220     def __print_popped(popped):
221         if len(popped) == 0:
222             pass
223         elif len(popped) == 1:
224             out.info('Popped %s' % popped[0])
225         else:
226             out.info('Popped %s -- %s' % (popped[-1], popped[0]))
227
228     def pop_patches(self, p):
229         """Pop all patches pn for which p(pn) is true. Return the list of
230         other patches that had to be popped to accomplish this. Always
231         succeeds."""
232         popped = []
233         for i in xrange(len(self.applied)):
234             if p(self.applied[i]):
235                 popped = self.applied[i:]
236                 del self.applied[i:]
237                 break
238         popped1 = [pn for pn in popped if not p(pn)]
239         popped2 = [pn for pn in popped if p(pn)]
240         self.unapplied = popped1 + popped2 + self.unapplied
241         self.__print_popped(popped)
242         return popped1
243
244     def delete_patches(self, p):
245         """Delete all patches pn for which p(pn) is true. Return the list of
246         other patches that had to be popped to accomplish this. Always
247         succeeds."""
248         popped = []
249         all_patches = self.applied + self.unapplied + self.hidden
250         for i in xrange(len(self.applied)):
251             if p(self.applied[i]):
252                 popped = self.applied[i:]
253                 del self.applied[i:]
254                 break
255         popped = [pn for pn in popped if not p(pn)]
256         self.unapplied = popped + [pn for pn in self.unapplied if not p(pn)]
257         self.hidden = [pn for pn in self.hidden if not p(pn)]
258         self.__print_popped(popped)
259         for pn in all_patches:
260             if p(pn):
261                 s = ['', ' (empty)'][self.patches[pn].data.is_nochange()]
262                 self.patches[pn] = None
263                 out.info('Deleted %s%s' % (pn, s))
264         return popped
265
266     def push_patch(self, pn, iw = None):
267         """Attempt to push the named patch. If this results in conflicts,
268         halts the transaction. If index+worktree are given, spill any
269         conflicts to them."""
270         orig_cd = self.patches[pn].data
271         cd = orig_cd.set_committer(None)
272         s = ['', ' (empty)'][cd.is_nochange()]
273         oldparent = cd.parent
274         cd = cd.set_parent(self.__head)
275         base = oldparent.data.tree
276         ours = cd.parent.data.tree
277         theirs = cd.tree
278         tree, self.temp_index_tree = self.temp_index.merge(
279             base, ours, theirs, self.temp_index_tree)
280         merge_conflict = False
281         if not tree:
282             if iw == None:
283                 self.__halt('%s does not apply cleanly' % pn)
284             try:
285                 self.__checkout(ours, iw)
286             except git.CheckoutException:
287                 self.__halt('Index/worktree dirty')
288             try:
289                 iw.merge(base, ours, theirs)
290                 tree = iw.index.write_tree()
291                 self.__current_tree = tree
292                 s = ' (modified)'
293             except git.MergeConflictException:
294                 tree = ours
295                 merge_conflict = True
296                 s = ' (conflict)'
297             except git.MergeException, e:
298                 self.__halt(str(e))
299         cd = cd.set_tree(tree)
300         if any(getattr(cd, a) != getattr(orig_cd, a) for a in
301                ['parent', 'tree', 'author', 'message']):
302             comm = self.__stack.repository.commit(cd)
303         else:
304             comm = None
305             s = ' (unmodified)'
306         out.info('Pushed %s%s' % (pn, s))
307         def update():
308             if comm:
309                 self.patches[pn] = comm
310             if pn in self.hidden:
311                 x = self.hidden
312             else:
313                 x = self.unapplied
314             del x[x.index(pn)]
315             self.applied.append(pn)
316         if merge_conflict:
317             # We've just caused conflicts, so we must allow them in
318             # the final checkout.
319             self.__allow_conflicts = lambda trans: True
320
321             # Save this update so that we can run it a little later.
322             self.__conflicting_push = update
323             self.__halt('Merge conflict')
324         else:
325             # Update immediately.
326             update()
327
328     def reorder_patches(self, applied, unapplied, hidden, iw = None):
329         """Push and pop patches to attain the given ordering."""
330         common = len(list(it.takewhile(lambda (a, b): a == b,
331                                        zip(self.applied, applied))))
332         to_pop = set(self.applied[common:])
333         self.pop_patches(lambda pn: pn in to_pop)
334         for pn in applied[common:]:
335             self.push_patch(pn, iw)
336         assert self.applied == applied
337         assert set(self.unapplied + self.hidden) == set(unapplied + hidden)
338         self.unapplied = unapplied
339         self.hidden = hidden