Handle branch names with slashes
[stgit] / stgit / stack.py
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, basedir
25 from stgit.config import config
26
27
28 # stack exception class
29 class StackException(Exception):
30 pass
31
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
42 #
43 # Functions
44 #
45 __comment_prefix = 'STG:'
46 __patch_prefix = 'STG_PATCH:'
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
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
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
67 def edit_file(series, line, comment, show_patch = True):
68 fname = '.stgit.msg'
69 tmpl = os.path.join(basedir.get(), 'patchdescr.tmpl')
70
71 f = file(fname, 'w+')
72 if line:
73 print >> f, line
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.'
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 nobackup:'
92 f.close()
93
94 # the editor
95 if config.has_option('stgit', 'editor'):
96 editor = config.get('stgit', 'editor')
97 elif 'EDITOR' in os.environ:
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 result = f.read()
112
113 f.close()
114 os.remove(fname)
115
116 return result
117
118 #
119 # Classes
120 #
121
122 class Patch:
123 """Basic patch implementation
124 """
125 def __init__(self, name, series_dir, refs_dir):
126 self.__series_dir = series_dir
127 self.__name = name
128 self.__dir = os.path.join(self.__series_dir, self.__name)
129 self.__refs_dir = refs_dir
130 self.__top_ref_file = os.path.join(self.__refs_dir, self.__name)
131
132 def create(self):
133 os.mkdir(self.__dir)
134 create_empty_file(os.path.join(self.__dir, 'bottom'))
135 create_empty_file(os.path.join(self.__dir, 'top'))
136
137 def delete(self):
138 for f in os.listdir(self.__dir):
139 os.remove(os.path.join(self.__dir, f))
140 os.rmdir(self.__dir)
141 os.remove(self.__top_ref_file)
142
143 def get_name(self):
144 return self.__name
145
146 def rename(self, newname):
147 olddir = self.__dir
148 old_ref_file = self.__top_ref_file
149 self.__name = newname
150 self.__dir = os.path.join(self.__series_dir, self.__name)
151 self.__top_ref_file = os.path.join(self.__refs_dir, self.__name)
152
153 os.rename(olddir, self.__dir)
154 os.rename(old_ref_file, self.__top_ref_file)
155
156 def __update_top_ref(self, ref):
157 write_string(self.__top_ref_file, ref)
158
159 def update_top_ref(self):
160 top = self.get_top()
161 if top:
162 self.__update_top_ref(top)
163
164 def __get_field(self, name, multiline = False):
165 id_file = os.path.join(self.__dir, name)
166 if os.path.isfile(id_file):
167 line = read_string(id_file, multiline)
168 if line == '':
169 return None
170 else:
171 return line
172 else:
173 return None
174
175 def __set_field(self, name, value, multiline = False):
176 fname = os.path.join(self.__dir, name)
177 if value and value != '':
178 write_string(fname, value, multiline)
179 elif os.path.isfile(fname):
180 os.remove(fname)
181
182 def get_old_bottom(self):
183 return self.__get_field('bottom.old')
184
185 def get_bottom(self):
186 return self.__get_field('bottom')
187
188 def set_bottom(self, value, backup = False):
189 if backup:
190 curr = self.__get_field('bottom')
191 self.__set_field('bottom.old', curr)
192 self.__set_field('bottom', value)
193
194 def get_old_top(self):
195 return self.__get_field('top.old')
196
197 def get_top(self):
198 return self.__get_field('top')
199
200 def set_top(self, value, backup = False):
201 if backup:
202 curr = self.__get_field('top')
203 self.__set_field('top.old', curr)
204 self.__set_field('top', value)
205 self.__update_top_ref(value)
206
207 def restore_old_boundaries(self):
208 bottom = self.__get_field('bottom.old')
209 top = self.__get_field('top.old')
210
211 if top and bottom:
212 self.__set_field('bottom', bottom)
213 self.__set_field('top', top)
214 self.__update_top_ref(top)
215 return True
216 else:
217 return False
218
219 def get_description(self):
220 return self.__get_field('description', True)
221
222 def set_description(self, line):
223 self.__set_field('description', line, True)
224
225 def get_authname(self):
226 return self.__get_field('authname')
227
228 def set_authname(self, name):
229 if not name:
230 if config.has_option('stgit', 'authname'):
231 name = config.get('stgit', 'authname')
232 elif 'GIT_AUTHOR_NAME' in os.environ:
233 name = os.environ['GIT_AUTHOR_NAME']
234 self.__set_field('authname', name)
235
236 def get_authemail(self):
237 return self.__get_field('authemail')
238
239 def set_authemail(self, address):
240 if not address:
241 if config.has_option('stgit', 'authemail'):
242 address = config.get('stgit', 'authemail')
243 elif 'GIT_AUTHOR_EMAIL' in os.environ:
244 address = os.environ['GIT_AUTHOR_EMAIL']
245 self.__set_field('authemail', address)
246
247 def get_authdate(self):
248 return self.__get_field('authdate')
249
250 def set_authdate(self, date):
251 if not date and 'GIT_AUTHOR_DATE' in os.environ:
252 date = os.environ['GIT_AUTHOR_DATE']
253 self.__set_field('authdate', date)
254
255 def get_commname(self):
256 return self.__get_field('commname')
257
258 def set_commname(self, name):
259 if not name:
260 if config.has_option('stgit', 'commname'):
261 name = config.get('stgit', 'commname')
262 elif 'GIT_COMMITTER_NAME' in os.environ:
263 name = os.environ['GIT_COMMITTER_NAME']
264 self.__set_field('commname', name)
265
266 def get_commemail(self):
267 return self.__get_field('commemail')
268
269 def set_commemail(self, address):
270 if not address:
271 if config.has_option('stgit', 'commemail'):
272 address = config.get('stgit', 'commemail')
273 elif 'GIT_COMMITTER_EMAIL' in os.environ:
274 address = os.environ['GIT_COMMITTER_EMAIL']
275 self.__set_field('commemail', address)
276
277
278 class Series:
279 """Class including the operations on series
280 """
281 def __init__(self, name = None):
282 """Takes a series name as the parameter.
283 """
284 try:
285 if name:
286 self.__name = name
287 else:
288 self.__name = git.get_head_file()
289 self.__base_dir = basedir.get()
290 except git.GitException, ex:
291 raise StackException, 'GIT tree not initialised: %s' % ex
292
293 self.__series_dir = os.path.join(self.__base_dir, 'patches',
294 self.__name)
295 self.__refs_dir = os.path.join(self.__base_dir, 'refs', 'patches',
296 self.__name)
297 self.__base_file = os.path.join(self.__base_dir, 'refs', 'bases',
298 self.__name)
299
300 self.__applied_file = os.path.join(self.__series_dir, 'applied')
301 self.__unapplied_file = os.path.join(self.__series_dir, 'unapplied')
302 self.__current_file = os.path.join(self.__series_dir, 'current')
303 self.__descr_file = os.path.join(self.__series_dir, 'description')
304
305 # where this series keeps its patches
306 self.__patch_dir = os.path.join(self.__series_dir, 'patches')
307 if not os.path.isdir(self.__patch_dir):
308 self.__patch_dir = self.__series_dir
309
310 # if no __refs_dir, create and populate it (upgrade old repositories)
311 if self.is_initialised() and not os.path.isdir(self.__refs_dir):
312 os.makedirs(self.__refs_dir)
313 for patch in self.get_applied() + self.get_unapplied():
314 self.get_patch(patch).update_top_ref()
315
316 def get_branch(self):
317 """Return the branch name for the Series object
318 """
319 return self.__name
320
321 def __set_current(self, name):
322 """Sets the topmost patch
323 """
324 if name:
325 write_string(self.__current_file, name)
326 else:
327 create_empty_file(self.__current_file)
328
329 def get_patch(self, name):
330 """Return a Patch object for the given name
331 """
332 return Patch(name, self.__patch_dir, self.__refs_dir)
333
334 def get_current(self):
335 """Return a Patch object representing the topmost patch
336 """
337 if os.path.isfile(self.__current_file):
338 name = read_string(self.__current_file)
339 else:
340 return None
341 if name == '':
342 return None
343 else:
344 return name
345
346 def get_applied(self):
347 if not os.path.isfile(self.__applied_file):
348 raise StackException, 'Branch "%s" not initialised' % self.__name
349 f = file(self.__applied_file)
350 names = [line.strip() for line in f.readlines()]
351 f.close()
352 return names
353
354 def get_unapplied(self):
355 if not os.path.isfile(self.__unapplied_file):
356 raise StackException, 'Branch "%s" not initialised' % self.__name
357 f = file(self.__unapplied_file)
358 names = [line.strip() for line in f.readlines()]
359 f.close()
360 return names
361
362 def get_base_file(self):
363 self.__begin_stack_check()
364 return self.__base_file
365
366 def get_protected(self):
367 return os.path.isfile(os.path.join(self.__series_dir, 'protected'))
368
369 def protect(self):
370 protect_file = os.path.join(self.__series_dir, 'protected')
371 if not os.path.isfile(protect_file):
372 create_empty_file(protect_file)
373
374 def unprotect(self):
375 protect_file = os.path.join(self.__series_dir, 'protected')
376 if os.path.isfile(protect_file):
377 os.remove(protect_file)
378
379 def get_description(self):
380 if os.path.isfile(self.__descr_file):
381 return read_string(self.__descr_file)
382 else:
383 return ''
384
385 def __patch_is_current(self, patch):
386 return patch.get_name() == read_string(self.__current_file)
387
388 def __patch_applied(self, name):
389 """Return true if the patch exists in the applied list
390 """
391 return name in self.get_applied()
392
393 def __patch_unapplied(self, name):
394 """Return true if the patch exists in the unapplied list
395 """
396 return name in self.get_unapplied()
397
398 def __begin_stack_check(self):
399 """Save the current HEAD into .git/refs/heads/base if the stack
400 is empty
401 """
402 if len(self.get_applied()) == 0:
403 head = git.get_head()
404 write_string(self.__base_file, head)
405
406 def __end_stack_check(self):
407 """Remove .git/refs/heads/base if the stack is empty.
408 This warning should never happen
409 """
410 if len(self.get_applied()) == 0 \
411 and read_string(self.__base_file) != git.get_head():
412 print 'Warning: stack empty but the HEAD and base are different'
413
414 def head_top_equal(self):
415 """Return true if the head and the top are the same
416 """
417 crt = self.get_current()
418 if not crt:
419 # we don't care, no patches applied
420 return True
421 return git.get_head() == Patch(crt, self.__patch_dir,
422 self.__refs_dir).get_top()
423
424 def is_initialised(self):
425 """Checks if series is already initialised
426 """
427 return os.path.isdir(self.__patch_dir)
428
429 def init(self, create_at=False):
430 """Initialises the stgit series
431 """
432 bases_dir = os.path.join(self.__base_dir, 'refs', 'bases')
433
434 if os.path.exists(self.__patch_dir):
435 raise StackException, self.__patch_dir + ' already exists'
436 if os.path.exists(self.__refs_dir):
437 raise StackException, self.__refs_dir + ' already exists'
438 if os.path.exists(self.__base_file):
439 raise StackException, self.__base_file + ' already exists'
440
441 if (create_at!=False):
442 git.create_branch(self.__name, create_at)
443
444 os.makedirs(self.__patch_dir)
445
446 create_dirs(bases_dir)
447
448 create_empty_file(self.__applied_file)
449 create_empty_file(self.__unapplied_file)
450 create_empty_file(self.__descr_file)
451 os.makedirs(os.path.join(self.__series_dir, 'patches'))
452 os.makedirs(self.__refs_dir)
453 self.__begin_stack_check()
454
455 def convert(self):
456 """Either convert to use a separate patch directory, or
457 unconvert to place the patches in the same directory with
458 series control files
459 """
460 if self.__patch_dir == self.__series_dir:
461 print 'Converting old-style to new-style...',
462 sys.stdout.flush()
463
464 self.__patch_dir = os.path.join(self.__series_dir, 'patches')
465 os.makedirs(self.__patch_dir)
466
467 for p in self.get_applied() + self.get_unapplied():
468 src = os.path.join(self.__series_dir, p)
469 dest = os.path.join(self.__patch_dir, p)
470 os.rename(src, dest)
471
472 print 'done'
473
474 else:
475 print 'Converting new-style to old-style...',
476 sys.stdout.flush()
477
478 for p in self.get_applied() + self.get_unapplied():
479 src = os.path.join(self.__patch_dir, p)
480 dest = os.path.join(self.__series_dir, p)
481 os.rename(src, dest)
482
483 if not os.listdir(self.__patch_dir):
484 os.rmdir(self.__patch_dir)
485 print 'done'
486 else:
487 print 'Patch directory %s is not empty.' % self.__name
488
489 self.__patch_dir = self.__series_dir
490
491 def rename(self, to_name):
492 """Renames a series
493 """
494 to_stack = Series(to_name)
495
496 if to_stack.is_initialised():
497 raise StackException, '"%s" already exists' % to_stack.get_branch()
498 if os.path.exists(to_stack.__base_file):
499 os.remove(to_stack.__base_file)
500
501 git.rename_branch(self.__name, to_name)
502
503 if os.path.isdir(self.__series_dir):
504 rename(os.path.join(self.__base_dir, 'patches'),
505 self.__name, to_stack.__name)
506 if os.path.exists(self.__base_file):
507 rename(os.path.join(self.__base_dir, 'refs', 'bases'),
508 self.__name, to_stack.__name)
509 if os.path.exists(self.__refs_dir):
510 rename(os.path.join(self.__base_dir, 'refs', 'patches'),
511 self.__name, to_stack.__name)
512
513 self.__init__(to_name)
514
515 def clone(self, target_series):
516 """Clones a series
517 """
518 base = read_string(self.get_base_file())
519 Series(target_series).init(create_at = base)
520 new_series = Series(target_series)
521
522 # generate an artificial description file
523 write_string(new_series.__descr_file, 'clone of "%s"' % self.__name)
524
525 # clone self's entire series as unapplied patches
526 patches = self.get_applied() + self.get_unapplied()
527 patches.reverse()
528 for p in patches:
529 patch = self.get_patch(p)
530 new_series.new_patch(p, message = patch.get_description(),
531 can_edit = False, unapplied = True,
532 bottom = patch.get_bottom(),
533 top = patch.get_top(),
534 author_name = patch.get_authname(),
535 author_email = patch.get_authemail(),
536 author_date = patch.get_authdate())
537
538 # fast forward the cloned series to self's top
539 new_series.forward_patches(self.get_applied())
540
541 def delete(self, force = False):
542 """Deletes an stgit series
543 """
544 if self.is_initialised():
545 patches = self.get_unapplied() + self.get_applied()
546 if not force and patches:
547 raise StackException, \
548 'Cannot delete: the series still contains patches'
549 for p in patches:
550 Patch(p, self.__patch_dir, self.__refs_dir).delete()
551
552 if os.path.exists(self.__applied_file):
553 os.remove(self.__applied_file)
554 if os.path.exists(self.__unapplied_file):
555 os.remove(self.__unapplied_file)
556 if os.path.exists(self.__current_file):
557 os.remove(self.__current_file)
558 if os.path.exists(self.__descr_file):
559 os.remove(self.__descr_file)
560 if not os.listdir(self.__patch_dir):
561 os.rmdir(self.__patch_dir)
562 else:
563 print 'Patch directory %s is not empty.' % self.__name
564 if not os.listdir(self.__series_dir):
565 remove_dirs(os.path.join(self.__base_dir, 'patches'),
566 self.__name)
567 else:
568 print 'Series directory %s is not empty.' % self.__name
569 if not os.listdir(self.__refs_dir):
570 remove_dirs(os.path.join(self.__base_dir, 'refs', 'patches'),
571 self.__name)
572 else:
573 print 'Refs directory %s is not empty.' % self.__refs_dir
574
575 if os.path.exists(self.__base_file):
576 remove_file_and_dirs(
577 os.path.join(self.__base_dir, 'refs', 'bases'), self.__name)
578
579 def refresh_patch(self, files = None, message = None, edit = False,
580 show_patch = False,
581 cache_update = True,
582 author_name = None, author_email = None,
583 author_date = None,
584 committer_name = None, committer_email = None,
585 backup = False):
586 """Generates a new commit for the given patch
587 """
588 name = self.get_current()
589 if not name:
590 raise StackException, 'No patches applied'
591
592 patch = Patch(name, self.__patch_dir, self.__refs_dir)
593
594 descr = patch.get_description()
595 if not (message or descr):
596 edit = True
597 descr = ''
598 elif message:
599 descr = message
600
601 if not message and edit:
602 descr = edit_file(self, descr.rstrip(), \
603 'Please edit the description for patch "%s" ' \
604 'above.' % name, show_patch)
605
606 if not author_name:
607 author_name = patch.get_authname()
608 if not author_email:
609 author_email = patch.get_authemail()
610 if not author_date:
611 author_date = patch.get_authdate()
612 if not committer_name:
613 committer_name = patch.get_commname()
614 if not committer_email:
615 committer_email = patch.get_commemail()
616
617 bottom = patch.get_bottom()
618
619 commit_id = git.commit(files = files,
620 message = descr, parents = [bottom],
621 cache_update = cache_update,
622 allowempty = True,
623 author_name = author_name,
624 author_email = author_email,
625 author_date = author_date,
626 committer_name = committer_name,
627 committer_email = committer_email)
628
629 patch.set_bottom(bottom, backup = backup)
630 patch.set_top(commit_id, backup = backup)
631 patch.set_description(descr)
632 patch.set_authname(author_name)
633 patch.set_authemail(author_email)
634 patch.set_authdate(author_date)
635 patch.set_commname(committer_name)
636 patch.set_commemail(committer_email)
637
638 return commit_id
639
640 def undo_refresh(self):
641 """Undo the patch boundaries changes caused by 'refresh'
642 """
643 name = self.get_current()
644 assert(name)
645
646 patch = Patch(name, self.__patch_dir, self.__refs_dir)
647 old_bottom = patch.get_old_bottom()
648 old_top = patch.get_old_top()
649
650 # the bottom of the patch is not changed by refresh. If the
651 # old_bottom is different, there wasn't any previous 'refresh'
652 # command (probably only a 'push')
653 if old_bottom != patch.get_bottom() or old_top == patch.get_top():
654 raise StackException, 'No refresh undo information available'
655
656 git.reset(tree_id = old_top, check_out = False)
657 patch.restore_old_boundaries()
658
659 def new_patch(self, name, message = None, can_edit = True,
660 unapplied = False, show_patch = False,
661 top = None, bottom = None,
662 author_name = None, author_email = None, author_date = None,
663 committer_name = None, committer_email = None,
664 before_existing = False):
665 """Creates a new patch
666 """
667 if self.__patch_applied(name) or self.__patch_unapplied(name):
668 raise StackException, 'Patch "%s" already exists' % name
669
670 if not message and can_edit:
671 descr = edit_file(self, None, \
672 'Please enter the description for patch "%s" ' \
673 'above.' % name, show_patch)
674 else:
675 descr = message
676
677 head = git.get_head()
678
679 self.__begin_stack_check()
680
681 patch = Patch(name, self.__patch_dir, self.__refs_dir)
682 patch.create()
683
684 if bottom:
685 patch.set_bottom(bottom)
686 else:
687 patch.set_bottom(head)
688 if top:
689 patch.set_top(top)
690 else:
691 patch.set_top(head)
692
693 patch.set_description(descr)
694 patch.set_authname(author_name)
695 patch.set_authemail(author_email)
696 patch.set_authdate(author_date)
697 patch.set_commname(committer_name)
698 patch.set_commemail(committer_email)
699
700 if unapplied:
701 patches = [patch.get_name()] + self.get_unapplied()
702
703 f = file(self.__unapplied_file, 'w+')
704 f.writelines([line + '\n' for line in patches])
705 f.close()
706 else:
707 if before_existing:
708 insert_string(self.__applied_file, patch.get_name())
709 if not self.get_current():
710 self.__set_current(name)
711 else:
712 append_string(self.__applied_file, patch.get_name())
713 self.__set_current(name)
714
715 def delete_patch(self, name):
716 """Deletes a patch
717 """
718 patch = Patch(name, self.__patch_dir, self.__refs_dir)
719
720 if self.__patch_is_current(patch):
721 self.pop_patch(name)
722 elif self.__patch_applied(name):
723 raise StackException, 'Cannot remove an applied patch, "%s", ' \
724 'which is not current' % name
725 elif not name in self.get_unapplied():
726 raise StackException, 'Unknown patch "%s"' % name
727
728 patch.delete()
729
730 unapplied = self.get_unapplied()
731 unapplied.remove(name)
732 f = file(self.__unapplied_file, 'w+')
733 f.writelines([line + '\n' for line in unapplied])
734 f.close()
735 self.__begin_stack_check()
736
737 def forward_patches(self, names):
738 """Try to fast-forward an array of patches.
739
740 On return, patches in names[0:returned_value] have been pushed on the
741 stack. Apply the rest with push_patch
742 """
743 unapplied = self.get_unapplied()
744 self.__begin_stack_check()
745
746 forwarded = 0
747 top = git.get_head()
748
749 for name in names:
750 assert(name in unapplied)
751
752 patch = Patch(name, self.__patch_dir, self.__refs_dir)
753
754 head = top
755 bottom = patch.get_bottom()
756 top = patch.get_top()
757
758 # top != bottom always since we have a commit for each patch
759 if head == bottom:
760 # reset the backup information
761 patch.set_bottom(head, backup = True)
762 patch.set_top(top, backup = True)
763
764 else:
765 head_tree = git.get_commit(head).get_tree()
766 bottom_tree = git.get_commit(bottom).get_tree()
767 if head_tree == bottom_tree:
768 # We must just reparent this patch and create a new commit
769 # for it
770 descr = patch.get_description()
771 author_name = patch.get_authname()
772 author_email = patch.get_authemail()
773 author_date = patch.get_authdate()
774 committer_name = patch.get_commname()
775 committer_email = patch.get_commemail()
776
777 top_tree = git.get_commit(top).get_tree()
778
779 top = git.commit(message = descr, parents = [head],
780 cache_update = False,
781 tree_id = top_tree,
782 allowempty = True,
783 author_name = author_name,
784 author_email = author_email,
785 author_date = author_date,
786 committer_name = committer_name,
787 committer_email = committer_email)
788
789 patch.set_bottom(head, backup = True)
790 patch.set_top(top, backup = True)
791 else:
792 top = head
793 # stop the fast-forwarding, must do a real merge
794 break
795
796 forwarded+=1
797 unapplied.remove(name)
798
799 if forwarded == 0:
800 return 0
801
802 git.switch(top)
803
804 append_strings(self.__applied_file, names[0:forwarded])
805
806 f = file(self.__unapplied_file, 'w+')
807 f.writelines([line + '\n' for line in unapplied])
808 f.close()
809
810 self.__set_current(name)
811
812 return forwarded
813
814 def merged_patches(self, names):
815 """Test which patches were merged upstream by reverse-applying
816 them in reverse order. The function returns the list of
817 patches detected to have been applied. The state of the tree
818 is restored to the original one
819 """
820 patches = [Patch(name, self.__patch_dir, self.__refs_dir)
821 for name in names]
822 patches.reverse()
823
824 merged = []
825 for p in patches:
826 if git.apply_diff(p.get_top(), p.get_bottom()):
827 merged.append(p.get_name())
828 merged.reverse()
829
830 git.reset()
831
832 return merged
833
834 def push_patch(self, name, empty = False):
835 """Pushes a patch on the stack
836 """
837 unapplied = self.get_unapplied()
838 assert(name in unapplied)
839
840 self.__begin_stack_check()
841
842 patch = Patch(name, self.__patch_dir, self.__refs_dir)
843
844 head = git.get_head()
845 bottom = patch.get_bottom()
846 top = patch.get_top()
847
848 ex = None
849 modified = False
850
851 # top != bottom always since we have a commit for each patch
852 if empty:
853 # just make an empty patch (top = bottom = HEAD). This
854 # option is useful to allow undoing already merged
855 # patches. The top is updated by refresh_patch since we
856 # need an empty commit
857 patch.set_bottom(head, backup = True)
858 patch.set_top(head, backup = True)
859 modified = True
860 elif head == bottom:
861 # reset the backup information
862 patch.set_bottom(bottom, backup = True)
863 patch.set_top(top, backup = True)
864
865 git.switch(top)
866 else:
867 # new patch needs to be refreshed.
868 # The current patch is empty after merge.
869 patch.set_bottom(head, backup = True)
870 patch.set_top(head, backup = True)
871
872 # Try the fast applying first. If this fails, fall back to the
873 # three-way merge
874 if not git.apply_diff(bottom, top):
875 # if git.apply_diff() fails, the patch requires a diff3
876 # merge and can be reported as modified
877 modified = True
878
879 # merge can fail but the patch needs to be pushed
880 try:
881 git.merge(bottom, head, top)
882 except git.GitException, ex:
883 print >> sys.stderr, \
884 'The merge failed during "push". ' \
885 'Use "refresh" after fixing the conflicts'
886
887 append_string(self.__applied_file, name)
888
889 unapplied.remove(name)
890 f = file(self.__unapplied_file, 'w+')
891 f.writelines([line + '\n' for line in unapplied])
892 f.close()
893
894 self.__set_current(name)
895
896 # head == bottom case doesn't need to refresh the patch
897 if empty or head != bottom:
898 if not ex:
899 # if the merge was OK and no conflicts, just refresh the patch
900 # The GIT cache was already updated by the merge operation
901 self.refresh_patch(cache_update = False)
902 else:
903 raise StackException, str(ex)
904
905 return modified
906
907 def undo_push(self):
908 name = self.get_current()
909 assert(name)
910
911 patch = Patch(name, self.__patch_dir, self.__refs_dir)
912 old_bottom = patch.get_old_bottom()
913 old_top = patch.get_old_top()
914
915 # the top of the patch is changed by a push operation only
916 # together with the bottom (otherwise the top was probably
917 # modified by 'refresh'). If they are both unchanged, there
918 # was a fast forward
919 if old_bottom == patch.get_bottom() and old_top != patch.get_top():
920 raise StackException, 'No push undo information available'
921
922 git.reset()
923 self.pop_patch(name)
924 return patch.restore_old_boundaries()
925
926 def pop_patch(self, name):
927 """Pops the top patch from the stack
928 """
929 applied = self.get_applied()
930 applied.reverse()
931 assert(name in applied)
932
933 patch = Patch(name, self.__patch_dir, self.__refs_dir)
934
935 git.switch(patch.get_bottom())
936
937 # save the new applied list
938 idx = applied.index(name) + 1
939
940 popped = applied[:idx]
941 popped.reverse()
942 unapplied = popped + self.get_unapplied()
943
944 f = file(self.__unapplied_file, 'w+')
945 f.writelines([line + '\n' for line in unapplied])
946 f.close()
947
948 del applied[:idx]
949 applied.reverse()
950
951 f = file(self.__applied_file, 'w+')
952 f.writelines([line + '\n' for line in applied])
953 f.close()
954
955 if applied == []:
956 self.__set_current(None)
957 else:
958 self.__set_current(applied[-1])
959
960 self.__end_stack_check()
961
962 def empty_patch(self, name):
963 """Returns True if the patch is empty
964 """
965 patch = Patch(name, self.__patch_dir, self.__refs_dir)
966 bottom = patch.get_bottom()
967 top = patch.get_top()
968
969 if bottom == top:
970 return True
971 elif git.get_commit(top).get_tree() \
972 == git.get_commit(bottom).get_tree():
973 return True
974
975 return False
976
977 def rename_patch(self, oldname, newname):
978 applied = self.get_applied()
979 unapplied = self.get_unapplied()
980
981 if oldname == newname:
982 raise StackException, '"To" name and "from" name are the same'
983
984 if newname in applied or newname in unapplied:
985 raise StackException, 'Patch "%s" already exists' % newname
986
987 if oldname in unapplied:
988 Patch(oldname, self.__patch_dir, self.__refs_dir).rename(newname)
989 unapplied[unapplied.index(oldname)] = newname
990
991 f = file(self.__unapplied_file, 'w+')
992 f.writelines([line + '\n' for line in unapplied])
993 f.close()
994 elif oldname in applied:
995 Patch(oldname, self.__patch_dir, self.__refs_dir).rename(newname)
996 if oldname == self.get_current():
997 self.__set_current(newname)
998
999 applied[applied.index(oldname)] = newname
1000
1001 f = file(self.__applied_file, 'w+')
1002 f.writelines([line + '\n' for line in applied])
1003 f.close()
1004 else:
1005 raise StackException, 'Unknown patch "%s"' % oldname