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 | ||
32 | # | |
33 | # Functions | |
34 | # | |
35 | __comment_prefix = 'STG:' | |
36 | ||
37 | def __clean_comments(f): | |
38 | """Removes lines marked for status in a commit file | |
39 | """ | |
40 | f.seek(0) | |
41 | ||
42 | # remove status-prefixed lines | |
43 | lines = filter(lambda x: x[0:len(__comment_prefix)] != __comment_prefix, | |
44 | f.readlines()) | |
45 | # remove empty lines at the end | |
46 | while len(lines) != 0 and lines[-1] == '\n': | |
47 | del lines[-1] | |
48 | ||
49 | f.seek(0); f.truncate() | |
50 | f.writelines(lines) | |
51 | ||
52 | def edit_file(string, comment): | |
53 | fname = '.stgit.msg' | |
54 | tmpl = os.path.join(git.base_dir, 'patchdescr.tmpl') | |
55 | ||
56 | f = file(fname, 'w+') | |
57 | if string: | |
58 | print >> f, string | |
59 | elif os.path.isfile(tmpl): | |
60 | print >> f, file(tmpl).read().rstrip() | |
61 | else: | |
62 | print >> f | |
63 | print >> f, __comment_prefix, comment | |
64 | print >> f, __comment_prefix, \ | |
65 | 'Lines prefixed with "%s" will be automatically removed.' \ | |
66 | % __comment_prefix | |
67 | print >> f, __comment_prefix, \ | |
68 | 'Trailing empty lines will be automatically removed.' | |
69 | f.close() | |
70 | ||
71 | # the editor | |
72 | if 'EDITOR' in os.environ: | |
73 | editor = os.environ['EDITOR'] | |
74 | else: | |
75 | editor = 'vi' | |
76 | editor += ' %s' % fname | |
77 | ||
78 | print 'Invoking the editor: "%s"...' % editor, | |
79 | sys.stdout.flush() | |
80 | print 'done (exit code: %d)' % os.system(editor) | |
81 | ||
82 | f = file(fname, 'r+') | |
83 | ||
84 | __clean_comments(f) | |
85 | f.seek(0) | |
86 | string = f.read() | |
87 | ||
88 | f.close() | |
89 | os.remove(fname) | |
90 | ||
91 | return string | |
92 | ||
93 | # | |
94 | # Classes | |
95 | # | |
96 | ||
97 | class Patch: | |
98 | """Basic patch implementation | |
99 | """ | |
100 | def __init__(self, name, patch_dir): | |
101 | self.__patch_dir = patch_dir | |
102 | self.__name = name | |
103 | self.__dir = os.path.join(self.__patch_dir, self.__name) | |
104 | ||
105 | def create(self): | |
106 | os.mkdir(self.__dir) | |
107 | create_empty_file(os.path.join(self.__dir, 'bottom')) | |
108 | create_empty_file(os.path.join(self.__dir, 'top')) | |
109 | ||
110 | def delete(self): | |
111 | for f in os.listdir(self.__dir): | |
112 | os.remove(os.path.join(self.__dir, f)) | |
113 | os.rmdir(self.__dir) | |
114 | ||
115 | def get_name(self): | |
116 | return self.__name | |
117 | ||
e55b53e0 CM |
118 | def rename(self, newname): |
119 | olddir = self.__dir | |
120 | self.__name = newname | |
121 | self.__dir = os.path.join(self.__patch_dir, self.__name) | |
122 | ||
123 | os.rename(olddir, self.__dir) | |
124 | ||
41a6d859 CM |
125 | def __get_field(self, name, multiline = False): |
126 | id_file = os.path.join(self.__dir, name) | |
127 | if os.path.isfile(id_file): | |
128 | string = read_string(id_file, multiline) | |
129 | if string == '': | |
130 | return None | |
131 | else: | |
132 | return string | |
133 | else: | |
134 | return None | |
135 | ||
136 | def __set_field(self, name, string, multiline = False): | |
137 | fname = os.path.join(self.__dir, name) | |
138 | if string and string != '': | |
139 | write_string(fname, string, multiline) | |
140 | elif os.path.isfile(fname): | |
141 | os.remove(fname) | |
142 | ||
143 | def get_bottom(self): | |
144 | return self.__get_field('bottom') | |
145 | ||
146 | def set_bottom(self, string, backup = False): | |
147 | if backup: | |
148 | self.__set_field('bottom.old', self.__get_field('bottom')) | |
149 | self.__set_field('bottom', string) | |
150 | ||
151 | def get_top(self): | |
152 | return self.__get_field('top') | |
153 | ||
154 | def set_top(self, string, backup = False): | |
155 | if backup: | |
156 | self.__set_field('top.old', self.__get_field('top')) | |
157 | self.__set_field('top', string) | |
158 | ||
159 | def restore_old_boundaries(self): | |
160 | bottom = self.__get_field('bottom.old') | |
161 | top = self.__get_field('top.old') | |
162 | ||
163 | if top and bottom: | |
164 | self.__set_field('bottom', bottom) | |
165 | self.__set_field('top', top) | |
166 | else: | |
167 | raise StackException, 'No patch undo information' | |
168 | ||
169 | def get_description(self): | |
170 | return self.__get_field('description', True) | |
171 | ||
172 | def set_description(self, string): | |
173 | self.__set_field('description', string, True) | |
174 | ||
175 | def get_authname(self): | |
176 | return self.__get_field('authname') | |
177 | ||
178 | def set_authname(self, string): | |
179 | if not string and config.has_option('stgit', 'authname'): | |
180 | string = config.get('stgit', 'authname') | |
181 | self.__set_field('authname', string) | |
182 | ||
183 | def get_authemail(self): | |
184 | return self.__get_field('authemail') | |
185 | ||
186 | def set_authemail(self, string): | |
187 | if not string and config.has_option('stgit', 'authemail'): | |
188 | string = config.get('stgit', 'authemail') | |
189 | self.__set_field('authemail', string) | |
190 | ||
191 | def get_authdate(self): | |
192 | return self.__get_field('authdate') | |
193 | ||
194 | def set_authdate(self, string): | |
195 | self.__set_field('authdate', string) | |
196 | ||
197 | def get_commname(self): | |
198 | return self.__get_field('commname') | |
199 | ||
200 | def set_commname(self, string): | |
201 | if not string and config.has_option('stgit', 'commname'): | |
202 | string = config.get('stgit', 'commname') | |
203 | self.__set_field('commname', string) | |
204 | ||
205 | def get_commemail(self): | |
206 | return self.__get_field('commemail') | |
207 | ||
208 | def set_commemail(self, string): | |
209 | if not string and config.has_option('stgit', 'commemail'): | |
210 | string = config.get('stgit', 'commemail') | |
211 | self.__set_field('commemail', string) | |
212 | ||
213 | ||
214 | class Series: | |
215 | """Class including the operations on series | |
216 | """ | |
217 | def __init__(self, name = None): | |
218 | """Takes a series name as the parameter. A valid .git/patches/name | |
219 | directory should exist | |
220 | """ | |
221 | if name: | |
222 | self.__name = name | |
223 | else: | |
224 | self.__name = git.get_head_file() | |
225 | ||
226 | if self.__name: | |
227 | self.__patch_dir = os.path.join(git.base_dir, 'patches', | |
228 | self.__name) | |
229 | self.__base_file = os.path.join(git.base_dir, 'refs', 'bases', | |
230 | self.__name) | |
231 | self.__applied_file = os.path.join(self.__patch_dir, 'applied') | |
232 | self.__unapplied_file = os.path.join(self.__patch_dir, 'unapplied') | |
233 | self.__current_file = os.path.join(self.__patch_dir, 'current') | |
234 | ||
235 | def __set_current(self, name): | |
236 | """Sets the topmost patch | |
237 | """ | |
238 | if name: | |
239 | write_string(self.__current_file, name) | |
240 | else: | |
241 | create_empty_file(self.__current_file) | |
242 | ||
243 | def get_patch(self, name): | |
244 | """Return a Patch object for the given name | |
245 | """ | |
246 | return Patch(name, self.__patch_dir) | |
247 | ||
248 | def get_current(self): | |
249 | """Return a Patch object representing the topmost patch | |
250 | """ | |
251 | if os.path.isfile(self.__current_file): | |
252 | name = read_string(self.__current_file) | |
253 | else: | |
254 | return None | |
255 | if name == '': | |
256 | return None | |
257 | else: | |
258 | return name | |
259 | ||
260 | def get_applied(self): | |
261 | f = file(self.__applied_file) | |
262 | names = [line.strip() for line in f.readlines()] | |
263 | f.close() | |
264 | return names | |
265 | ||
266 | def get_unapplied(self): | |
267 | f = file(self.__unapplied_file) | |
268 | names = [line.strip() for line in f.readlines()] | |
269 | f.close() | |
270 | return names | |
271 | ||
272 | def get_base_file(self): | |
273 | return self.__base_file | |
274 | ||
275 | def __patch_is_current(self, patch): | |
276 | return patch.get_name() == read_string(self.__current_file) | |
277 | ||
278 | def __patch_applied(self, name): | |
279 | """Return true if the patch exists in the applied list | |
280 | """ | |
281 | return name in self.get_applied() | |
282 | ||
283 | def __patch_unapplied(self, name): | |
284 | """Return true if the patch exists in the unapplied list | |
285 | """ | |
286 | return name in self.get_unapplied() | |
287 | ||
288 | def __begin_stack_check(self): | |
289 | """Save the current HEAD into .git/refs/heads/base if the stack | |
290 | is empty | |
291 | """ | |
292 | if len(self.get_applied()) == 0: | |
293 | head = git.get_head() | |
41a6d859 CM |
294 | write_string(self.__base_file, head) |
295 | ||
296 | def __end_stack_check(self): | |
f338c3c0 CM |
297 | """Remove .git/refs/heads/base if the stack is empty. |
298 | This warning should never happen | |
41a6d859 | 299 | """ |
f338c3c0 CM |
300 | if len(self.get_applied()) == 0 \ |
301 | and read_string(self.__base_file) != git.get_head(): | |
302 | print 'Warning: stack empty but the HEAD and base are different' | |
41a6d859 CM |
303 | |
304 | def head_top_equal(self): | |
305 | """Return true if the head and the top are the same | |
306 | """ | |
307 | crt = self.get_current() | |
308 | if not crt: | |
309 | # we don't care, no patches applied | |
310 | return True | |
311 | return git.get_head() == Patch(crt, self.__patch_dir).get_top() | |
312 | ||
313 | def init(self): | |
314 | """Initialises the stgit series | |
315 | """ | |
316 | bases_dir = os.path.join(git.base_dir, 'refs', 'bases') | |
317 | ||
318 | if os.path.isdir(self.__patch_dir): | |
319 | raise StackException, self.__patch_dir + ' already exists' | |
320 | os.makedirs(self.__patch_dir) | |
321 | ||
322 | if not os.path.isdir(bases_dir): | |
323 | os.makedirs(bases_dir) | |
324 | ||
325 | create_empty_file(self.__applied_file) | |
326 | create_empty_file(self.__unapplied_file) | |
6872f46b | 327 | self.__begin_stack_check() |
41a6d859 CM |
328 | |
329 | def refresh_patch(self, message = None, edit = False, | |
330 | author_name = None, author_email = None, | |
331 | author_date = None, | |
c14444b9 CM |
332 | committer_name = None, committer_email = None, |
333 | commit_only = False): | |
41a6d859 CM |
334 | """Generates a new commit for the given patch |
335 | """ | |
336 | name = self.get_current() | |
337 | if not name: | |
338 | raise StackException, 'No patches applied' | |
339 | ||
340 | patch = Patch(name, self.__patch_dir) | |
341 | ||
342 | descr = patch.get_description() | |
343 | if not (message or descr): | |
344 | edit = True | |
345 | descr = '' | |
346 | elif message: | |
347 | descr = message | |
348 | ||
349 | if not message and edit: | |
350 | descr = edit_file(descr.rstrip(), \ | |
351 | 'Please edit the description for patch "%s" ' \ | |
352 | 'above.' % name) | |
353 | ||
354 | if not author_name: | |
355 | author_name = patch.get_authname() | |
356 | if not author_email: | |
357 | author_email = patch.get_authemail() | |
358 | if not author_date: | |
359 | author_date = patch.get_authdate() | |
360 | if not committer_name: | |
361 | committer_name = patch.get_commname() | |
362 | if not committer_email: | |
363 | committer_email = patch.get_commemail() | |
364 | ||
365 | commit_id = git.commit(message = descr, parents = [patch.get_bottom()], | |
366 | allowempty = True, | |
367 | author_name = author_name, | |
368 | author_email = author_email, | |
369 | author_date = author_date, | |
370 | committer_name = committer_name, | |
371 | committer_email = committer_email) | |
372 | ||
c14444b9 CM |
373 | if not commit_only: |
374 | patch.set_top(commit_id) | |
375 | patch.set_description(descr) | |
376 | patch.set_authname(author_name) | |
377 | patch.set_authemail(author_email) | |
378 | patch.set_authdate(author_date) | |
379 | patch.set_commname(committer_name) | |
380 | patch.set_commemail(committer_email) | |
381 | ||
382 | return commit_id | |
41a6d859 CM |
383 | |
384 | def new_patch(self, name, message = None, edit = False, | |
385 | author_name = None, author_email = None, author_date = None, | |
386 | committer_name = None, committer_email = None): | |
387 | """Creates a new patch | |
388 | """ | |
389 | if self.__patch_applied(name) or self.__patch_unapplied(name): | |
390 | raise StackException, 'Patch "%s" already exists' % name | |
391 | ||
392 | if not message: | |
393 | descr = edit_file(None, \ | |
394 | 'Please enter the description for patch "%s" ' \ | |
395 | 'above.' % name) | |
4de718c3 BL |
396 | else: |
397 | descr = message | |
41a6d859 CM |
398 | |
399 | head = git.get_head() | |
400 | ||
401 | self.__begin_stack_check() | |
402 | ||
403 | patch = Patch(name, self.__patch_dir) | |
404 | patch.create() | |
405 | patch.set_bottom(head) | |
406 | patch.set_top(head) | |
407 | patch.set_description(descr) | |
408 | patch.set_authname(author_name) | |
409 | patch.set_authemail(author_email) | |
410 | patch.set_authdate(author_date) | |
411 | patch.set_commname(committer_name) | |
412 | patch.set_commemail(committer_email) | |
413 | ||
414 | append_string(self.__applied_file, patch.get_name()) | |
415 | self.__set_current(name) | |
416 | ||
417 | def delete_patch(self, name): | |
418 | """Deletes a patch | |
419 | """ | |
420 | patch = Patch(name, self.__patch_dir) | |
421 | ||
422 | if self.__patch_is_current(patch): | |
423 | self.pop_patch(name) | |
424 | elif self.__patch_applied(name): | |
425 | raise StackException, 'Cannot remove an applied patch, "%s", ' \ | |
426 | 'which is not current' % name | |
427 | elif not name in self.get_unapplied(): | |
428 | raise StackException, 'Unknown patch "%s"' % name | |
429 | ||
430 | patch.delete() | |
431 | ||
432 | unapplied = self.get_unapplied() | |
433 | unapplied.remove(name) | |
434 | f = file(self.__unapplied_file, 'w+') | |
435 | f.writelines([line + '\n' for line in unapplied]) | |
436 | f.close() | |
437 | ||
438 | def push_patch(self, name): | |
439 | """Pushes a patch on the stack | |
440 | """ | |
441 | unapplied = self.get_unapplied() | |
442 | assert(name in unapplied) | |
443 | ||
444 | self.__begin_stack_check() | |
445 | ||
446 | patch = Patch(name, self.__patch_dir) | |
447 | ||
448 | head = git.get_head() | |
449 | bottom = patch.get_bottom() | |
450 | top = patch.get_top() | |
451 | ||
452 | ex = None | |
453 | ||
454 | # top != bottom always since we have a commit for each patch | |
455 | if head == bottom: | |
456 | # reset the backup information | |
457 | patch.set_bottom(bottom, backup = True) | |
458 | patch.set_top(top, backup = True) | |
459 | ||
460 | git.switch(top) | |
461 | else: | |
462 | # new patch needs to be refreshed. | |
463 | # The current patch is empty after merge. | |
464 | patch.set_bottom(head, backup = True) | |
465 | patch.set_top(head, backup = True) | |
466 | # merge/refresh can fail but the patch needs to be pushed | |
467 | try: | |
468 | git.merge(bottom, head, top) | |
469 | except git.GitException, ex: | |
470 | print >> sys.stderr, \ | |
471 | 'The merge failed during "push". ' \ | |
472 | 'Use "refresh" after fixing the conflicts' | |
473 | pass | |
474 | ||
475 | append_string(self.__applied_file, name) | |
476 | ||
477 | unapplied.remove(name) | |
478 | f = file(self.__unapplied_file, 'w+') | |
479 | f.writelines([line + '\n' for line in unapplied]) | |
480 | f.close() | |
481 | ||
482 | self.__set_current(name) | |
483 | ||
484 | if not ex: | |
485 | # if the merge was OK and no conflicts, just refresh the patch | |
486 | self.refresh_patch() | |
487 | else: | |
488 | raise StackException, str(ex) | |
489 | ||
490 | def undo_push(self): | |
491 | name = self.get_current() | |
492 | assert(name) | |
493 | ||
494 | patch = Patch(name, self.__patch_dir) | |
495 | self.pop_patch(name) | |
496 | patch.restore_old_boundaries() | |
497 | ||
498 | def pop_patch(self, name): | |
499 | """Pops the top patch from the stack | |
500 | """ | |
501 | applied = self.get_applied() | |
502 | applied.reverse() | |
503 | assert(name in applied) | |
504 | ||
505 | patch = Patch(name, self.__patch_dir) | |
506 | ||
507 | git.switch(patch.get_bottom()) | |
508 | ||
509 | # save the new applied list | |
510 | idx = applied.index(name) + 1 | |
511 | ||
512 | popped = applied[:idx] | |
513 | popped.reverse() | |
514 | unapplied = popped + self.get_unapplied() | |
515 | ||
516 | f = file(self.__unapplied_file, 'w+') | |
517 | f.writelines([line + '\n' for line in unapplied]) | |
518 | f.close() | |
519 | ||
520 | del applied[:idx] | |
521 | applied.reverse() | |
522 | ||
523 | f = file(self.__applied_file, 'w+') | |
524 | f.writelines([line + '\n' for line in applied]) | |
525 | f.close() | |
526 | ||
527 | if applied == []: | |
528 | self.__set_current(None) | |
529 | else: | |
530 | self.__set_current(applied[-1]) | |
531 | ||
532 | self.__end_stack_check() | |
533 | ||
534 | def empty_patch(self, name): | |
535 | """Returns True if the patch is empty | |
536 | """ | |
537 | patch = Patch(name, self.__patch_dir) | |
538 | bottom = patch.get_bottom() | |
539 | top = patch.get_top() | |
540 | ||
541 | if bottom == top: | |
542 | return True | |
543 | elif git.Commit(top).get_tree() == git.Commit(bottom).get_tree(): | |
544 | return True | |
545 | ||
546 | return False | |
e55b53e0 CM |
547 | |
548 | def rename_patch(self, oldname, newname): | |
549 | applied = self.get_applied() | |
550 | unapplied = self.get_unapplied() | |
551 | ||
552 | if newname in applied or newname in unapplied: | |
553 | raise StackException, 'Patch "%s" already exists' % newname | |
554 | ||
555 | if oldname in unapplied: | |
556 | Patch(oldname, self.__patch_dir).rename(newname) | |
557 | unapplied[unapplied.index(oldname)] = newname | |
558 | ||
559 | f = file(self.__unapplied_file, 'w+') | |
560 | f.writelines([line + '\n' for line in unapplied]) | |
561 | f.close() | |
562 | elif oldname in applied: | |
563 | Patch(oldname, self.__patch_dir).rename(newname) | |
564 | if oldname == self.get_current(): | |
565 | self.__set_current(newname) | |
566 | ||
567 | applied[applied.index(oldname)] = newname | |
568 | ||
569 | f = file(self.__applied_file, 'w+') | |
570 | f.writelines([line + '\n' for line in applied]) | |
571 | f.close() | |
572 | else: | |
573 | raise StackException, 'Unknown patch "%s"' % oldname |