Commit | Line | Data |
---|---|---|
41a6d859 CM |
1 | """Basic quilt-like functionality |
2 | """ | |
3 | ||
4 | __copyright__ = """ | |
5 | Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com> | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License version 2 as | |
9 | published by the Free Software Foundation. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | """ | |
20 | ||
21 | import sys, os | |
22 | ||
23 | from stgit.utils import * | |
24 | from stgit import git | |
25 | from stgit.config import config | |
26 | ||
27 | ||
28 | # stack exception class | |
29 | class StackException(Exception): | |
30 | pass | |
31 | ||
6ad48e48 PBG |
32 | class FilterUntil: |
33 | def __init__(self): | |
34 | self.should_print = True | |
35 | def __call__(self, x, until_test, prefix): | |
36 | if until_test(x): | |
37 | self.should_print = False | |
38 | if self.should_print: | |
39 | return x[0:len(prefix)] != prefix | |
40 | return False | |
41 | ||
41a6d859 CM |
42 | # |
43 | # Functions | |
44 | # | |
45 | __comment_prefix = 'STG:' | |
6ad48e48 | 46 | __patch_prefix = 'STG_PATCH:' |
41a6d859 CM |
47 | |
48 | def __clean_comments(f): | |
49 | """Removes lines marked for status in a commit file | |
50 | """ | |
51 | f.seek(0) | |
52 | ||
53 | # remove status-prefixed lines | |
6ad48e48 PBG |
54 | lines = f.readlines() |
55 | ||
56 | patch_filter = FilterUntil() | |
57 | until_test = lambda t: t == (__patch_prefix + '\n') | |
58 | lines = [l for l in lines if patch_filter(l, until_test, __comment_prefix)] | |
59 | ||
41a6d859 CM |
60 | # remove empty lines at the end |
61 | while len(lines) != 0 and lines[-1] == '\n': | |
62 | del lines[-1] | |
63 | ||
64 | f.seek(0); f.truncate() | |
65 | f.writelines(lines) | |
66 | ||
7cc615f3 | 67 | def edit_file(series, line, comment, show_patch = True): |
41a6d859 | 68 | fname = '.stgit.msg' |
bae29ddd | 69 | tmpl = os.path.join(git.get_base_dir(), 'patchdescr.tmpl') |
41a6d859 CM |
70 | |
71 | f = file(fname, 'w+') | |
7cc615f3 CL |
72 | if line: |
73 | print >> f, line | |
41a6d859 CM |
74 | elif os.path.isfile(tmpl): |
75 | print >> f, file(tmpl).read().rstrip() | |
76 | else: | |
77 | print >> f | |
78 | print >> f, __comment_prefix, comment | |
79 | print >> f, __comment_prefix, \ | |
80 | 'Lines prefixed with "%s" will be automatically removed.' \ | |
81 | % __comment_prefix | |
82 | print >> f, __comment_prefix, \ | |
83 | 'Trailing empty lines will be automatically removed.' | |
6ad48e48 PBG |
84 | |
85 | if show_patch: | |
86 | print >> f, __patch_prefix | |
87 | # series.get_patch(series.get_current()).get_top() | |
88 | git.diff([], series.get_patch(series.get_current()).get_bottom(), None, f) | |
89 | ||
90 | #Vim modeline must be near the end. | |
b83e37e0 | 91 | print >> f, __comment_prefix, 'vi: set textwidth=75 filetype=diff nobackup:' |
41a6d859 CM |
92 | f.close() |
93 | ||
94 | # the editor | |
cd076ff6 CL |
95 | if config.has_option('stgit', 'editor'): |
96 | editor = config.get('stgit', 'editor') | |
97 | elif 'EDITOR' in os.environ: | |
41a6d859 CM |
98 | editor = os.environ['EDITOR'] |
99 | else: | |
100 | editor = 'vi' | |
101 | editor += ' %s' % fname | |
102 | ||
103 | print 'Invoking the editor: "%s"...' % editor, | |
104 | sys.stdout.flush() | |
105 | print 'done (exit code: %d)' % os.system(editor) | |
106 | ||
107 | f = file(fname, 'r+') | |
108 | ||
109 | __clean_comments(f) | |
110 | f.seek(0) | |
7cc615f3 | 111 | result = f.read() |
41a6d859 CM |
112 | |
113 | f.close() | |
114 | os.remove(fname) | |
115 | ||
7cc615f3 | 116 | return result |
41a6d859 CM |
117 | |
118 | # | |
119 | # Classes | |
120 | # | |
121 | ||
122 | class Patch: | |
123 | """Basic patch implementation | |
124 | """ | |
125 | def __init__(self, name, patch_dir): | |
126 | self.__patch_dir = patch_dir | |
127 | self.__name = name | |
128 | self.__dir = os.path.join(self.__patch_dir, self.__name) | |
129 | ||
130 | def create(self): | |
131 | os.mkdir(self.__dir) | |
132 | create_empty_file(os.path.join(self.__dir, 'bottom')) | |
133 | create_empty_file(os.path.join(self.__dir, 'top')) | |
134 | ||
135 | def delete(self): | |
136 | for f in os.listdir(self.__dir): | |
137 | os.remove(os.path.join(self.__dir, f)) | |
138 | os.rmdir(self.__dir) | |
139 | ||
140 | def get_name(self): | |
141 | return self.__name | |
142 | ||
e55b53e0 CM |
143 | def rename(self, newname): |
144 | olddir = self.__dir | |
145 | self.__name = newname | |
146 | self.__dir = os.path.join(self.__patch_dir, self.__name) | |
147 | ||
148 | os.rename(olddir, self.__dir) | |
149 | ||
41a6d859 CM |
150 | def __get_field(self, name, multiline = False): |
151 | id_file = os.path.join(self.__dir, name) | |
152 | if os.path.isfile(id_file): | |
7cc615f3 CL |
153 | line = read_string(id_file, multiline) |
154 | if line == '': | |
41a6d859 CM |
155 | return None |
156 | else: | |
7cc615f3 | 157 | return line |
41a6d859 CM |
158 | else: |
159 | return None | |
160 | ||
7cc615f3 | 161 | def __set_field(self, name, value, multiline = False): |
41a6d859 | 162 | fname = os.path.join(self.__dir, name) |
7cc615f3 CL |
163 | if value and value != '': |
164 | write_string(fname, value, multiline) | |
41a6d859 CM |
165 | elif os.path.isfile(fname): |
166 | os.remove(fname) | |
167 | ||
54b09584 PBG |
168 | def get_old_bottom(self): |
169 | return self.__get_field('bottom.old') | |
170 | ||
41a6d859 CM |
171 | def get_bottom(self): |
172 | return self.__get_field('bottom') | |
173 | ||
7cc615f3 | 174 | def set_bottom(self, value, backup = False): |
41a6d859 | 175 | if backup: |
a5bbc44d | 176 | curr = self.__get_field('bottom') |
7cc615f3 | 177 | if curr != value: |
a5bbc44d PBG |
178 | self.__set_field('bottom.old', curr) |
179 | else: | |
180 | self.__set_field('bottom.old', None) | |
7cc615f3 | 181 | self.__set_field('bottom', value) |
41a6d859 | 182 | |
54b09584 PBG |
183 | def get_old_top(self): |
184 | return self.__get_field('top.old') | |
185 | ||
41a6d859 CM |
186 | def get_top(self): |
187 | return self.__get_field('top') | |
188 | ||
7cc615f3 | 189 | def set_top(self, value, backup = False): |
41a6d859 | 190 | if backup: |
a5bbc44d | 191 | curr = self.__get_field('top') |
7cc615f3 | 192 | if curr != value: |
a5bbc44d PBG |
193 | self.__set_field('top.old', curr) |
194 | else: | |
195 | self.__set_field('top.old', None) | |
7cc615f3 | 196 | self.__set_field('top', value) |
41a6d859 CM |
197 | |
198 | def restore_old_boundaries(self): | |
199 | bottom = self.__get_field('bottom.old') | |
200 | top = self.__get_field('top.old') | |
201 | ||
202 | if top and bottom: | |
203 | self.__set_field('bottom', bottom) | |
204 | self.__set_field('top', top) | |
a5bbc44d | 205 | return True |
41a6d859 | 206 | else: |
a5bbc44d | 207 | return False |
41a6d859 CM |
208 | |
209 | def get_description(self): | |
210 | return self.__get_field('description', True) | |
211 | ||
7cc615f3 CL |
212 | def set_description(self, line): |
213 | self.__set_field('description', line, True) | |
41a6d859 CM |
214 | |
215 | def get_authname(self): | |
216 | return self.__get_field('authname') | |
217 | ||
7cc615f3 CL |
218 | def set_authname(self, name): |
219 | if not name and config.has_option('stgit', 'authname'): | |
220 | name = config.get('stgit', 'authname') | |
221 | self.__set_field('authname', name) | |
41a6d859 CM |
222 | |
223 | def get_authemail(self): | |
224 | return self.__get_field('authemail') | |
225 | ||
7cc615f3 CL |
226 | def set_authemail(self, address): |
227 | if not address and config.has_option('stgit', 'authemail'): | |
228 | address = config.get('stgit', 'authemail') | |
229 | self.__set_field('authemail', address) | |
41a6d859 CM |
230 | |
231 | def get_authdate(self): | |
232 | return self.__get_field('authdate') | |
233 | ||
7cc615f3 CL |
234 | def set_authdate(self, authdate): |
235 | self.__set_field('authdate', authdate) | |
41a6d859 CM |
236 | |
237 | def get_commname(self): | |
238 | return self.__get_field('commname') | |
239 | ||
7cc615f3 CL |
240 | def set_commname(self, name): |
241 | if not name and config.has_option('stgit', 'commname'): | |
242 | name = config.get('stgit', 'commname') | |
243 | self.__set_field('commname', name) | |
41a6d859 CM |
244 | |
245 | def get_commemail(self): | |
246 | return self.__get_field('commemail') | |
247 | ||
7cc615f3 CL |
248 | def set_commemail(self, address): |
249 | if not address and config.has_option('stgit', 'commemail'): | |
250 | address = config.get('stgit', 'commemail') | |
251 | self.__set_field('commemail', address) | |
41a6d859 CM |
252 | |
253 | ||
254 | class Series: | |
255 | """Class including the operations on series | |
256 | """ | |
257 | def __init__(self, name = None): | |
40e65b92 | 258 | """Takes a series name as the parameter. |
41a6d859 CM |
259 | """ |
260 | if name: | |
261 | self.__name = name | |
262 | else: | |
263 | self.__name = git.get_head_file() | |
264 | ||
265 | if self.__name: | |
bae29ddd CL |
266 | base_dir = git.get_base_dir() |
267 | self.__patch_dir = os.path.join(base_dir, 'patches', | |
41a6d859 | 268 | self.__name) |
bae29ddd | 269 | self.__base_file = os.path.join(base_dir, 'refs', 'bases', |
41a6d859 CM |
270 | self.__name) |
271 | self.__applied_file = os.path.join(self.__patch_dir, 'applied') | |
272 | self.__unapplied_file = os.path.join(self.__patch_dir, 'unapplied') | |
273 | self.__current_file = os.path.join(self.__patch_dir, 'current') | |
c1fe1f99 | 274 | self.__descr_file = os.path.join(self.__patch_dir, 'description') |
41a6d859 | 275 | |
629ddd02 CM |
276 | def get_branch(self): |
277 | """Return the branch name for the Series object | |
278 | """ | |
279 | return self.__name | |
280 | ||
41a6d859 CM |
281 | def __set_current(self, name): |
282 | """Sets the topmost patch | |
283 | """ | |
284 | if name: | |
285 | write_string(self.__current_file, name) | |
286 | else: | |
287 | create_empty_file(self.__current_file) | |
288 | ||
289 | def get_patch(self, name): | |
290 | """Return a Patch object for the given name | |
291 | """ | |
292 | return Patch(name, self.__patch_dir) | |
293 | ||
294 | def get_current(self): | |
295 | """Return a Patch object representing the topmost patch | |
296 | """ | |
297 | if os.path.isfile(self.__current_file): | |
298 | name = read_string(self.__current_file) | |
299 | else: | |
300 | return None | |
301 | if name == '': | |
302 | return None | |
303 | else: | |
304 | return name | |
305 | ||
306 | def get_applied(self): | |
40e65b92 | 307 | if not os.path.isfile(self.__applied_file): |
a2dcde71 | 308 | raise StackException, 'Branch "%s" not initialised' % self.__name |
41a6d859 CM |
309 | f = file(self.__applied_file) |
310 | names = [line.strip() for line in f.readlines()] | |
311 | f.close() | |
312 | return names | |
313 | ||
314 | def get_unapplied(self): | |
40e65b92 | 315 | if not os.path.isfile(self.__unapplied_file): |
a2dcde71 | 316 | raise StackException, 'Branch "%s" not initialised' % self.__name |
41a6d859 CM |
317 | f = file(self.__unapplied_file) |
318 | names = [line.strip() for line in f.readlines()] | |
319 | f.close() | |
320 | return names | |
321 | ||
322 | def get_base_file(self): | |
323 | return self.__base_file | |
324 | ||
0b4b9499 CL |
325 | def get_protected(self): |
326 | return os.path.isfile(os.path.join(self.__patch_dir, 'protected')) | |
327 | ||
328 | def protect(self): | |
329 | protect_file = os.path.join(self.__patch_dir, 'protected') | |
330 | if not os.path.isfile(protect_file): | |
331 | create_empty_file(protect_file) | |
332 | ||
333 | def unprotect(self): | |
334 | protect_file = os.path.join(self.__patch_dir, 'protected') | |
335 | if os.path.isfile(protect_file): | |
336 | os.remove(protect_file) | |
337 | ||
c1fe1f99 CL |
338 | def get_description(self): |
339 | if os.path.isfile(self.__descr_file): | |
340 | return read_string(self.__descr_file) | |
341 | else: | |
342 | return '' | |
343 | ||
41a6d859 CM |
344 | def __patch_is_current(self, patch): |
345 | return patch.get_name() == read_string(self.__current_file) | |
346 | ||
347 | def __patch_applied(self, name): | |
348 | """Return true if the patch exists in the applied list | |
349 | """ | |
350 | return name in self.get_applied() | |
351 | ||
352 | def __patch_unapplied(self, name): | |
353 | """Return true if the patch exists in the unapplied list | |
354 | """ | |
355 | return name in self.get_unapplied() | |
356 | ||
357 | def __begin_stack_check(self): | |
358 | """Save the current HEAD into .git/refs/heads/base if the stack | |
359 | is empty | |
360 | """ | |
361 | if len(self.get_applied()) == 0: | |
362 | head = git.get_head() | |
41a6d859 CM |
363 | write_string(self.__base_file, head) |
364 | ||
365 | def __end_stack_check(self): | |
f338c3c0 CM |
366 | """Remove .git/refs/heads/base if the stack is empty. |
367 | This warning should never happen | |
41a6d859 | 368 | """ |
f338c3c0 CM |
369 | if len(self.get_applied()) == 0 \ |
370 | and read_string(self.__base_file) != git.get_head(): | |
371 | print 'Warning: stack empty but the HEAD and base are different' | |
41a6d859 CM |
372 | |
373 | def head_top_equal(self): | |
374 | """Return true if the head and the top are the same | |
375 | """ | |
376 | crt = self.get_current() | |
377 | if not crt: | |
378 | # we don't care, no patches applied | |
379 | return True | |
380 | return git.get_head() == Patch(crt, self.__patch_dir).get_top() | |
381 | ||
2d00440c CL |
382 | def is_initialised(self): |
383 | """Checks if series is already initialised | |
384 | """ | |
385 | return os.path.isdir(self.__patch_dir) | |
386 | ||
41a6d859 CM |
387 | def init(self): |
388 | """Initialises the stgit series | |
389 | """ | |
bae29ddd | 390 | bases_dir = os.path.join(git.get_base_dir(), 'refs', 'bases') |
41a6d859 | 391 | |
2d00440c | 392 | if self.is_initialised(): |
41a6d859 CM |
393 | raise StackException, self.__patch_dir + ' already exists' |
394 | os.makedirs(self.__patch_dir) | |
395 | ||
396 | if not os.path.isdir(bases_dir): | |
397 | os.makedirs(bases_dir) | |
398 | ||
399 | create_empty_file(self.__applied_file) | |
400 | create_empty_file(self.__unapplied_file) | |
c1fe1f99 | 401 | create_empty_file(self.__descr_file) |
6872f46b | 402 | self.__begin_stack_check() |
41a6d859 | 403 | |
660ba985 CL |
404 | def rename(self, to_name): |
405 | """Renames a series | |
406 | """ | |
407 | to_stack = Series(to_name) | |
84bf6268 CL |
408 | |
409 | if to_stack.is_initialised(): | |
410 | raise StackException, '"%s" already exists' % to_stack.get_branch() | |
411 | if os.path.exists(to_stack.__base_file): | |
412 | os.remove(to_stack.__base_file) | |
660ba985 CL |
413 | |
414 | git.rename_branch(self.__name, to_name) | |
415 | ||
416 | if os.path.isdir(self.__patch_dir): | |
417 | os.rename(self.__patch_dir, to_stack.__patch_dir) | |
84bf6268 | 418 | if os.path.exists(self.__base_file): |
660ba985 CL |
419 | os.rename(self.__base_file, to_stack.__base_file) |
420 | ||
421 | self.__init__(to_name) | |
422 | ||
cc3db2b1 CL |
423 | def clone(self, target_series): |
424 | """Clones a series | |
425 | """ | |
426 | base = read_string(self.get_base_file()) | |
427 | git.create_branch(target_series, tree_id = base) | |
428 | Series(target_series).init() | |
429 | new_series = Series(target_series) | |
430 | ||
431 | # generate an artificial description file | |
432 | write_string(new_series.__descr_file, 'clone of "%s"' % self.__name) | |
433 | ||
434 | # clone self's entire series as unapplied patches | |
435 | patches = self.get_applied() + self.get_unapplied() | |
436 | patches.reverse() | |
437 | for p in patches: | |
438 | patch = self.get_patch(p) | |
439 | new_series.new_patch(p, message = patch.get_description(), | |
440 | can_edit = False, unapplied = True, | |
441 | bottom = patch.get_bottom(), | |
442 | top = patch.get_top(), | |
443 | author_name = patch.get_authname(), | |
444 | author_email = patch.get_authemail(), | |
445 | author_date = patch.get_authdate()) | |
446 | ||
447 | # fast forward the cloned series to self's top | |
448 | new_series.forward_patches(self.get_applied()) | |
449 | ||
fc804a49 CL |
450 | def delete(self, force = False): |
451 | """Deletes an stgit series | |
452 | """ | |
2d00440c | 453 | if self.is_initialised(): |
fc804a49 CL |
454 | patches = self.get_unapplied() + self.get_applied() |
455 | if not force and patches: | |
456 | raise StackException, \ | |
457 | 'Cannot delete: the series still contains patches' | |
fc804a49 | 458 | for p in patches: |
5a636ab3 | 459 | Patch(p, self.__patch_dir).delete() |
fc804a49 | 460 | |
84bf6268 | 461 | if os.path.exists(self.__applied_file): |
fc804a49 | 462 | os.remove(self.__applied_file) |
84bf6268 | 463 | if os.path.exists(self.__unapplied_file): |
fc804a49 | 464 | os.remove(self.__unapplied_file) |
84bf6268 | 465 | if os.path.exists(self.__current_file): |
fc804a49 | 466 | os.remove(self.__current_file) |
84bf6268 | 467 | if os.path.exists(self.__descr_file): |
c1fe1f99 | 468 | os.remove(self.__descr_file) |
fc804a49 CL |
469 | if not os.listdir(self.__patch_dir): |
470 | os.rmdir(self.__patch_dir) | |
471 | else: | |
472 | print 'Series directory %s is not empty.' % self.__name | |
473 | ||
84bf6268 | 474 | if os.path.exists(self.__base_file): |
fc804a49 CL |
475 | os.remove(self.__base_file) |
476 | ||
6ad48e48 PBG |
477 | def refresh_patch(self, message = None, edit = False, show_patch = False, |
478 | cache_update = True, | |
41a6d859 CM |
479 | author_name = None, author_email = None, |
480 | author_date = None, | |
84fcbc3b | 481 | committer_name = None, committer_email = None): |
41a6d859 CM |
482 | """Generates a new commit for the given patch |
483 | """ | |
484 | name = self.get_current() | |
485 | if not name: | |
486 | raise StackException, 'No patches applied' | |
487 | ||
488 | patch = Patch(name, self.__patch_dir) | |
489 | ||
490 | descr = patch.get_description() | |
491 | if not (message or descr): | |
492 | edit = True | |
493 | descr = '' | |
494 | elif message: | |
495 | descr = message | |
496 | ||
497 | if not message and edit: | |
6ad48e48 | 498 | descr = edit_file(self, descr.rstrip(), \ |
41a6d859 | 499 | 'Please edit the description for patch "%s" ' \ |
6ad48e48 | 500 | 'above.' % name, show_patch) |
41a6d859 CM |
501 | |
502 | if not author_name: | |
503 | author_name = patch.get_authname() | |
504 | if not author_email: | |
505 | author_email = patch.get_authemail() | |
506 | if not author_date: | |
507 | author_date = patch.get_authdate() | |
508 | if not committer_name: | |
509 | committer_name = patch.get_commname() | |
510 | if not committer_email: | |
511 | committer_email = patch.get_commemail() | |
512 | ||
513 | commit_id = git.commit(message = descr, parents = [patch.get_bottom()], | |
402ad990 | 514 | cache_update = cache_update, |
41a6d859 CM |
515 | allowempty = True, |
516 | author_name = author_name, | |
517 | author_email = author_email, | |
518 | author_date = author_date, | |
519 | committer_name = committer_name, | |
520 | committer_email = committer_email) | |
521 | ||
84fcbc3b CM |
522 | patch.set_top(commit_id) |
523 | patch.set_description(descr) | |
524 | patch.set_authname(author_name) | |
525 | patch.set_authemail(author_email) | |
526 | patch.set_authdate(author_date) | |
527 | patch.set_commname(committer_name) | |
528 | patch.set_commemail(committer_email) | |
c14444b9 CM |
529 | |
530 | return commit_id | |
41a6d859 | 531 | |
37a4d1bf CM |
532 | def new_patch(self, name, message = None, can_edit = True, |
533 | unapplied = False, show_patch = False, | |
534 | top = None, bottom = None, | |
41a6d859 CM |
535 | author_name = None, author_email = None, author_date = None, |
536 | committer_name = None, committer_email = None): | |
537 | """Creates a new patch | |
538 | """ | |
539 | if self.__patch_applied(name) or self.__patch_unapplied(name): | |
540 | raise StackException, 'Patch "%s" already exists' % name | |
541 | ||
95742cfc | 542 | if not message and can_edit: |
6ad48e48 | 543 | descr = edit_file(self, None, \ |
41a6d859 | 544 | 'Please enter the description for patch "%s" ' \ |
6ad48e48 | 545 | 'above.' % name, show_patch) |
4de718c3 BL |
546 | else: |
547 | descr = message | |
41a6d859 CM |
548 | |
549 | head = git.get_head() | |
550 | ||
551 | self.__begin_stack_check() | |
552 | ||
553 | patch = Patch(name, self.__patch_dir) | |
554 | patch.create() | |
37a4d1bf CM |
555 | |
556 | if bottom: | |
557 | patch.set_bottom(bottom) | |
558 | else: | |
559 | patch.set_bottom(head) | |
560 | if top: | |
561 | patch.set_top(top) | |
562 | else: | |
563 | patch.set_top(head) | |
564 | ||
41a6d859 CM |
565 | patch.set_description(descr) |
566 | patch.set_authname(author_name) | |
567 | patch.set_authemail(author_email) | |
568 | patch.set_authdate(author_date) | |
569 | patch.set_commname(committer_name) | |
570 | patch.set_commemail(committer_email) | |
571 | ||
37a4d1bf CM |
572 | if unapplied: |
573 | patches = [patch.get_name()] + self.get_unapplied() | |
574 | ||
575 | f = file(self.__unapplied_file, 'w+') | |
576 | f.writelines([line + '\n' for line in patches]) | |
577 | f.close() | |
578 | else: | |
579 | append_string(self.__applied_file, patch.get_name()) | |
580 | self.__set_current(name) | |
41a6d859 CM |
581 | |
582 | def delete_patch(self, name): | |
583 | """Deletes a patch | |
584 | """ | |
585 | patch = Patch(name, self.__patch_dir) | |
586 | ||
587 | if self.__patch_is_current(patch): | |
588 | self.pop_patch(name) | |
589 | elif self.__patch_applied(name): | |
590 | raise StackException, 'Cannot remove an applied patch, "%s", ' \ | |
591 | 'which is not current' % name | |
592 | elif not name in self.get_unapplied(): | |
593 | raise StackException, 'Unknown patch "%s"' % name | |
594 | ||
595 | patch.delete() | |
596 | ||
597 | unapplied = self.get_unapplied() | |
598 | unapplied.remove(name) | |
599 | f = file(self.__unapplied_file, 'w+') | |
600 | f.writelines([line + '\n' for line in unapplied]) | |
601 | f.close() | |
602 | ||
680e3a32 PBG |
603 | def forward_patches(self, names): |
604 | """Try to fast-forward an array of patches. | |
605 | ||
606 | On return, patches in names[0:returned_value] have been pushed on the | |
607 | stack. Apply the rest with push_patch | |
608 | """ | |
609 | unapplied = self.get_unapplied() | |
610 | self.__begin_stack_check() | |
611 | ||
612 | forwarded = 0 | |
613 | top = git.get_head() | |
614 | ||
615 | for name in names: | |
616 | assert(name in unapplied) | |
617 | ||
618 | patch = Patch(name, self.__patch_dir) | |
619 | ||
620 | head = top | |
621 | bottom = patch.get_bottom() | |
622 | top = patch.get_top() | |
623 | ||
624 | # top != bottom always since we have a commit for each patch | |
625 | if head == bottom: | |
626 | # reset the backup information | |
d3cf7d86 | 627 | patch.set_bottom(head, backup = True) |
680e3a32 PBG |
628 | patch.set_top(top, backup = True) |
629 | ||
630 | else: | |
d3cf7d86 PBG |
631 | head_tree = git.get_commit(head).get_tree() |
632 | bottom_tree = git.get_commit(bottom).get_tree() | |
633 | if head_tree == bottom_tree: | |
634 | # We must just reparent this patch and create a new commit | |
635 | # for it | |
636 | descr = patch.get_description() | |
637 | author_name = patch.get_authname() | |
638 | author_email = patch.get_authemail() | |
639 | author_date = patch.get_authdate() | |
640 | committer_name = patch.get_commname() | |
641 | committer_email = patch.get_commemail() | |
642 | ||
643 | top_tree = git.get_commit(top).get_tree() | |
644 | ||
645 | top = git.commit(message = descr, parents = [head], | |
646 | cache_update = False, | |
647 | tree_id = top_tree, | |
648 | allowempty = True, | |
649 | author_name = author_name, | |
650 | author_email = author_email, | |
651 | author_date = author_date, | |
652 | committer_name = committer_name, | |
653 | committer_email = committer_email) | |
654 | ||
655 | patch.set_bottom(head, backup = True) | |
656 | patch.set_top(top, backup = True) | |
657 | else: | |
658 | top = head | |
659 | # stop the fast-forwarding, must do a real merge | |
660 | break | |
680e3a32 PBG |
661 | |
662 | forwarded+=1 | |
663 | unapplied.remove(name) | |
664 | ||
06dbe791 CL |
665 | if forwarded == 0: |
666 | return 0 | |
667 | ||
680e3a32 PBG |
668 | git.switch(top) |
669 | ||
670 | append_strings(self.__applied_file, names[0:forwarded]) | |
671 | ||
672 | f = file(self.__unapplied_file, 'w+') | |
673 | f.writelines([line + '\n' for line in unapplied]) | |
674 | f.close() | |
675 | ||
676 | self.__set_current(name) | |
677 | ||
678 | return forwarded | |
679 | ||
41a6d859 CM |
680 | def push_patch(self, name): |
681 | """Pushes a patch on the stack | |
682 | """ | |
683 | unapplied = self.get_unapplied() | |
684 | assert(name in unapplied) | |
685 | ||
686 | self.__begin_stack_check() | |
687 | ||
688 | patch = Patch(name, self.__patch_dir) | |
689 | ||
690 | head = git.get_head() | |
691 | bottom = patch.get_bottom() | |
692 | top = patch.get_top() | |
693 | ||
694 | ex = None | |
718a5e51 | 695 | modified = False |
41a6d859 CM |
696 | |
697 | # top != bottom always since we have a commit for each patch | |
698 | if head == bottom: | |
699 | # reset the backup information | |
700 | patch.set_bottom(bottom, backup = True) | |
701 | patch.set_top(top, backup = True) | |
702 | ||
703 | git.switch(top) | |
704 | else: | |
705 | # new patch needs to be refreshed. | |
706 | # The current patch is empty after merge. | |
707 | patch.set_bottom(head, backup = True) | |
708 | patch.set_top(head, backup = True) | |
575a7e7c CM |
709 | |
710 | # Try the fast applying first. If this fails, fall back to the | |
711 | # three-way merge | |
712 | if not git.apply_diff(bottom, top): | |
718a5e51 CM |
713 | # if git.apply_diff() fails, the patch requires a diff3 |
714 | # merge and can be reported as modified | |
715 | modified = True | |
716 | ||
575a7e7c CM |
717 | # merge can fail but the patch needs to be pushed |
718 | try: | |
719 | git.merge(bottom, head, top) | |
720 | except git.GitException, ex: | |
721 | print >> sys.stderr, \ | |
722 | 'The merge failed during "push". ' \ | |
723 | 'Use "refresh" after fixing the conflicts' | |
41a6d859 CM |
724 | |
725 | append_string(self.__applied_file, name) | |
726 | ||
727 | unapplied.remove(name) | |
728 | f = file(self.__unapplied_file, 'w+') | |
729 | f.writelines([line + '\n' for line in unapplied]) | |
730 | f.close() | |
731 | ||
732 | self.__set_current(name) | |
733 | ||
a322940b CM |
734 | # head == bottom case doesn't need to refresh the patch |
735 | if head != bottom: | |
736 | if not ex: | |
737 | # if the merge was OK and no conflicts, just refresh the patch | |
402ad990 CM |
738 | # The GIT cache was already updated by the merge operation |
739 | self.refresh_patch(cache_update = False) | |
a322940b CM |
740 | else: |
741 | raise StackException, str(ex) | |
41a6d859 | 742 | |
718a5e51 CM |
743 | return modified |
744 | ||
41a6d859 CM |
745 | def undo_push(self): |
746 | name = self.get_current() | |
747 | assert(name) | |
748 | ||
749 | patch = Patch(name, self.__patch_dir) | |
05d593c0 | 750 | git.reset() |
41a6d859 | 751 | self.pop_patch(name) |
a5bbc44d | 752 | return patch.restore_old_boundaries() |
41a6d859 CM |
753 | |
754 | def pop_patch(self, name): | |
755 | """Pops the top patch from the stack | |
756 | """ | |
757 | applied = self.get_applied() | |
758 | applied.reverse() | |
759 | assert(name in applied) | |
760 | ||
761 | patch = Patch(name, self.__patch_dir) | |
762 | ||
763 | git.switch(patch.get_bottom()) | |
764 | ||
765 | # save the new applied list | |
766 | idx = applied.index(name) + 1 | |
767 | ||
768 | popped = applied[:idx] | |
769 | popped.reverse() | |
770 | unapplied = popped + self.get_unapplied() | |
771 | ||
772 | f = file(self.__unapplied_file, 'w+') | |
773 | f.writelines([line + '\n' for line in unapplied]) | |
774 | f.close() | |
775 | ||
776 | del applied[:idx] | |
777 | applied.reverse() | |
778 | ||
779 | f = file(self.__applied_file, 'w+') | |
780 | f.writelines([line + '\n' for line in applied]) | |
781 | f.close() | |
782 | ||
783 | if applied == []: | |
784 | self.__set_current(None) | |
785 | else: | |
786 | self.__set_current(applied[-1]) | |
787 | ||
788 | self.__end_stack_check() | |
789 | ||
790 | def empty_patch(self, name): | |
791 | """Returns True if the patch is empty | |
792 | """ | |
793 | patch = Patch(name, self.__patch_dir) | |
794 | bottom = patch.get_bottom() | |
795 | top = patch.get_top() | |
796 | ||
797 | if bottom == top: | |
798 | return True | |
8e29bcd2 CM |
799 | elif git.get_commit(top).get_tree() \ |
800 | == git.get_commit(bottom).get_tree(): | |
41a6d859 CM |
801 | return True |
802 | ||
803 | return False | |
e55b53e0 CM |
804 | |
805 | def rename_patch(self, oldname, newname): | |
806 | applied = self.get_applied() | |
807 | unapplied = self.get_unapplied() | |
808 | ||
e19173dc CL |
809 | if oldname == newname: |
810 | raise StackException, '"To" name and "from" name are the same' | |
811 | ||
e55b53e0 CM |
812 | if newname in applied or newname in unapplied: |
813 | raise StackException, 'Patch "%s" already exists' % newname | |
814 | ||
815 | if oldname in unapplied: | |
816 | Patch(oldname, self.__patch_dir).rename(newname) | |
817 | unapplied[unapplied.index(oldname)] = newname | |
818 | ||
819 | f = file(self.__unapplied_file, 'w+') | |
820 | f.writelines([line + '\n' for line in unapplied]) | |
821 | f.close() | |
822 | elif oldname in applied: | |
823 | Patch(oldname, self.__patch_dir).rename(newname) | |
824 | if oldname == self.get_current(): | |
825 | self.__set_current(newname) | |
826 | ||
827 | applied[applied.index(oldname)] = newname | |
828 | ||
829 | f = file(self.__applied_file, 'w+') | |
830 | f.writelines([line + '\n' for line in applied]) | |
831 | f.close() | |
832 | else: | |
833 | raise StackException, 'Unknown patch "%s"' % oldname |