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