1 """Basic quilt-like functionality
5 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
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.
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.
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
23 from stgit
.utils
import *
25 from stgit
.config
import config
28 # stack exception class
29 class StackException(Exception):
34 self
.should_print
= True
35 def __call__(self
, x
, until_test
, prefix
):
37 self
.should_print
= False
39 return x
[0:len(prefix
)] != prefix
45 __comment_prefix
= 'STG:'
46 __patch_prefix
= 'STG_PATCH:'
48 def __clean_comments(f
):
49 """Removes lines marked for status in a commit file
53 # remove status-prefixed lines
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
)]
60 # remove empty lines at the end
61 while len(lines
) != 0 and lines
[-1] == '\n':
64 f
.seek(0); f
.truncate()
67 def edit_file(series
, line
, comment
, show_patch
= True):
69 tmpl
= os
.path
.join(git
.base_dir
, 'patchdescr.tmpl')
74 elif os
.path
.isfile(tmpl
):
75 print >> f
, file(tmpl
).read().rstrip()
78 print >> f
, __comment_prefix
, comment
79 print >> f
, __comment_prefix
, \
80 'Lines prefixed with "%s" will be automatically removed.' \
82 print >> f
, __comment_prefix
, \
83 'Trailing empty lines will be automatically removed.'
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
)
90 #Vim modeline must be near the end.
91 print >> f
, __comment_prefix
, 'vi: set textwidth=75 filetype=diff nobackup:'
95 if config
.has_option('stgit', 'editor'):
96 editor
= config
.get('stgit', 'editor')
97 elif 'EDITOR' in os
.environ
:
98 editor
= os
.environ
['EDITOR']
101 editor
+= ' %s' % fname
103 print 'Invoking the editor: "%s"...' % editor
,
105 print 'done (exit code: %d)' % os
.system(editor
)
107 f
= file(fname
, 'r+')
123 """Basic patch implementation
125 def __init__(self
, name
, patch_dir
):
126 self
.__patch
_dir
= patch_dir
128 self
.__dir
= os
.path
.join(self
.__patch
_dir
, self
.__name
)
132 create_empty_file(os
.path
.join(self
.__dir
, 'bottom'))
133 create_empty_file(os
.path
.join(self
.__dir
, 'top'))
136 for f
in os
.listdir(self
.__dir
):
137 os
.remove(os
.path
.join(self
.__dir
, f
))
143 def rename(self
, newname
):
145 self
.__name
= newname
146 self
.__dir
= os
.path
.join(self
.__patch
_dir
, self
.__name
)
148 os
.rename(olddir
, self
.__dir
)
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 line
= read_string(id_file
, multiline
)
161 def __set_field(self
, name
, value
, multiline
= False):
162 fname
= os
.path
.join(self
.__dir
, name
)
163 if value
and value
!= '':
164 write_string(fname
, value
, multiline
)
165 elif os
.path
.isfile(fname
):
168 def get_old_bottom(self
):
169 return self
.__get
_field
('bottom.old')
171 def get_bottom(self
):
172 return self
.__get
_field
('bottom')
174 def set_bottom(self
, value
, backup
= False):
176 curr
= self
.__get
_field
('bottom')
178 self
.__set
_field
('bottom.old', curr
)
180 self
.__set
_field
('bottom.old', None)
181 self
.__set
_field
('bottom', value
)
183 def get_old_top(self
):
184 return self
.__get
_field
('top.old')
187 return self
.__get
_field
('top')
189 def set_top(self
, value
, backup
= False):
191 curr
= self
.__get
_field
('top')
193 self
.__set
_field
('top.old', curr
)
195 self
.__set
_field
('top.old', None)
196 self
.__set
_field
('top', value
)
198 def restore_old_boundaries(self
):
199 bottom
= self
.__get
_field
('bottom.old')
200 top
= self
.__get
_field
('top.old')
203 self
.__set
_field
('bottom', bottom
)
204 self
.__set
_field
('top', top
)
209 def get_description(self
):
210 return self
.__get
_field
('description', True)
212 def set_description(self
, line
):
213 self
.__set
_field
('description', line
, True)
215 def get_authname(self
):
216 return self
.__get
_field
('authname')
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
)
223 def get_authemail(self
):
224 return self
.__get
_field
('authemail')
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
)
231 def get_authdate(self
):
232 return self
.__get
_field
('authdate')
234 def set_authdate(self
, authdate
):
235 self
.__set
_field
('authdate', authdate
)
237 def get_commname(self
):
238 return self
.__get
_field
('commname')
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
)
245 def get_commemail(self
):
246 return self
.__get
_field
('commemail')
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
)
255 """Class including the operations on series
257 def __init__(self
, name
= None):
258 """Takes a series name as the parameter.
263 self
.__name
= git
.get_head_file()
266 self
.__patch
_dir
= os
.path
.join(git
.base_dir
, 'patches',
268 self
.__base
_file
= os
.path
.join(git
.base_dir
, 'refs', 'bases',
270 self
.__applied
_file
= os
.path
.join(self
.__patch
_dir
, 'applied')
271 self
.__unapplied
_file
= os
.path
.join(self
.__patch
_dir
, 'unapplied')
272 self
.__current
_file
= os
.path
.join(self
.__patch
_dir
, 'current')
273 self
.__descr
_file
= os
.path
.join(self
.__patch
_dir
, 'description')
275 def get_branch(self
):
276 """Return the branch name for the Series object
280 def __set_current(self
, name
):
281 """Sets the topmost patch
284 write_string(self
.__current
_file
, name
)
286 create_empty_file(self
.__current
_file
)
288 def get_patch(self
, name
):
289 """Return a Patch object for the given name
291 return Patch(name
, self
.__patch
_dir
)
293 def get_current(self
):
294 """Return a Patch object representing the topmost patch
296 if os
.path
.isfile(self
.__current
_file
):
297 name
= read_string(self
.__current
_file
)
305 def get_applied(self
):
306 if not os
.path
.isfile(self
.__applied
_file
):
307 raise StackException
, 'Branch "%s" not initialised' % self
.__name
308 f
= file(self
.__applied
_file
)
309 names
= [line
.strip() for line
in f
.readlines()]
313 def get_unapplied(self
):
314 if not os
.path
.isfile(self
.__unapplied
_file
):
315 raise StackException
, 'Branch "%s" not initialised' % self
.__name
316 f
= file(self
.__unapplied
_file
)
317 names
= [line
.strip() for line
in f
.readlines()]
321 def get_base_file(self
):
322 return self
.__base
_file
324 def get_protected(self
):
325 return os
.path
.isfile(os
.path
.join(self
.__patch
_dir
, 'protected'))
328 protect_file
= os
.path
.join(self
.__patch
_dir
, 'protected')
329 if not os
.path
.isfile(protect_file
):
330 create_empty_file(protect_file
)
333 protect_file
= os
.path
.join(self
.__patch
_dir
, 'protected')
334 if os
.path
.isfile(protect_file
):
335 os
.remove(protect_file
)
337 def get_description(self
):
338 if os
.path
.isfile(self
.__descr
_file
):
339 return read_string(self
.__descr
_file
)
343 def __patch_is_current(self
, patch
):
344 return patch
.get_name() == read_string(self
.__current
_file
)
346 def __patch_applied(self
, name
):
347 """Return true if the patch exists in the applied list
349 return name
in self
.get_applied()
351 def __patch_unapplied(self
, name
):
352 """Return true if the patch exists in the unapplied list
354 return name
in self
.get_unapplied()
356 def __begin_stack_check(self
):
357 """Save the current HEAD into .git/refs/heads/base if the stack
360 if len(self
.get_applied()) == 0:
361 head
= git
.get_head()
362 write_string(self
.__base
_file
, head
)
364 def __end_stack_check(self
):
365 """Remove .git/refs/heads/base if the stack is empty.
366 This warning should never happen
368 if len(self
.get_applied()) == 0 \
369 and read_string(self
.__base
_file
) != git
.get_head():
370 print 'Warning: stack empty but the HEAD and base are different'
372 def head_top_equal(self
):
373 """Return true if the head and the top are the same
375 crt
= self
.get_current()
377 # we don't care, no patches applied
379 return git
.get_head() == Patch(crt
, self
.__patch
_dir
).get_top()
381 def is_initialised(self
):
382 """Checks if series is already initialised
384 return os
.path
.isdir(self
.__patch
_dir
)
387 """Initialises the stgit series
389 bases_dir
= os
.path
.join(git
.base_dir
, 'refs', 'bases')
391 if self
.is_initialised():
392 raise StackException
, self
.__patch
_dir
+ ' already exists'
393 os
.makedirs(self
.__patch
_dir
)
395 if not os
.path
.isdir(bases_dir
):
396 os
.makedirs(bases_dir
)
398 create_empty_file(self
.__applied
_file
)
399 create_empty_file(self
.__unapplied
_file
)
400 create_empty_file(self
.__descr
_file
)
401 self
.__begin
_stack
_check
()
403 def rename(self
, to_name
):
406 to_stack
= Series(to_name
)
408 if to_stack
.is_initialised():
409 raise StackException
, '"%s" already exists' % to_stack
.get_branch()
410 if os
.path
.exists(to_stack
.__base
_file
):
411 os
.remove(to_stack
.__base
_file
)
413 git
.rename_branch(self
.__name
, to_name
)
415 if os
.path
.isdir(self
.__patch
_dir
):
416 os
.rename(self
.__patch
_dir
, to_stack
.__patch
_dir
)
417 if os
.path
.exists(self
.__base
_file
):
418 os
.rename(self
.__base
_file
, to_stack
.__base
_file
)
420 self
.__init
__(to_name
)
422 def delete(self
, force
= False):
423 """Deletes an stgit series
425 if self
.is_initialised():
426 patches
= self
.get_unapplied() + self
.get_applied()
427 if not force
and patches
:
428 raise StackException
, \
429 'Cannot delete: the series still contains patches'
434 if os
.path
.exists(self
.__applied
_file
):
435 os
.remove(self
.__applied
_file
)
436 if os
.path
.exists(self
.__unapplied
_file
):
437 os
.remove(self
.__unapplied
_file
)
438 if os
.path
.exists(self
.__current
_file
):
439 os
.remove(self
.__current
_file
)
440 if os
.path
.exists(self
.__descr
_file
):
441 os
.remove(self
.__descr
_file
)
442 if not os
.listdir(self
.__patch
_dir
):
443 os
.rmdir(self
.__patch
_dir
)
445 print 'Series directory %s is not empty.' % self
.__name
447 if os
.path
.exists(self
.__base
_file
):
448 os
.remove(self
.__base
_file
)
450 def refresh_patch(self
, message
= None, edit
= False, show_patch
= False,
452 author_name
= None, author_email
= None,
454 committer_name
= None, committer_email
= None):
455 """Generates a new commit for the given patch
457 name
= self
.get_current()
459 raise StackException
, 'No patches applied'
461 patch
= Patch(name
, self
.__patch
_dir
)
463 descr
= patch
.get_description()
464 if not (message
or descr
):
470 if not message
and edit
:
471 descr
= edit_file(self
, descr
.rstrip(), \
472 'Please edit the description for patch "%s" ' \
473 'above.' % name
, show_patch
)
476 author_name
= patch
.get_authname()
478 author_email
= patch
.get_authemail()
480 author_date
= patch
.get_authdate()
481 if not committer_name
:
482 committer_name
= patch
.get_commname()
483 if not committer_email
:
484 committer_email
= patch
.get_commemail()
486 commit_id
= git
.commit(message
= descr
, parents
= [patch
.get_bottom()],
487 cache_update
= cache_update
,
489 author_name
= author_name
,
490 author_email
= author_email
,
491 author_date
= author_date
,
492 committer_name
= committer_name
,
493 committer_email
= committer_email
)
495 patch
.set_top(commit_id
)
496 patch
.set_description(descr
)
497 patch
.set_authname(author_name
)
498 patch
.set_authemail(author_email
)
499 patch
.set_authdate(author_date
)
500 patch
.set_commname(committer_name
)
501 patch
.set_commemail(committer_email
)
505 def new_patch(self
, name
, message
= None, can_edit
= True,
506 unapplied
= False, show_patch
= False,
507 top
= None, bottom
= None,
508 author_name
= None, author_email
= None, author_date
= None,
509 committer_name
= None, committer_email
= None):
510 """Creates a new patch
512 if self
.__patch
_applied
(name
) or self
.__patch
_unapplied
(name
):
513 raise StackException
, 'Patch "%s" already exists' % name
515 if not message
and can_edit
:
516 descr
= edit_file(self
, None, \
517 'Please enter the description for patch "%s" ' \
518 'above.' % name
, show_patch
)
522 head
= git
.get_head()
524 self
.__begin
_stack
_check
()
526 patch
= Patch(name
, self
.__patch
_dir
)
530 patch
.set_bottom(bottom
)
532 patch
.set_bottom(head
)
538 patch
.set_description(descr
)
539 patch
.set_authname(author_name
)
540 patch
.set_authemail(author_email
)
541 patch
.set_authdate(author_date
)
542 patch
.set_commname(committer_name
)
543 patch
.set_commemail(committer_email
)
546 patches
= [patch
.get_name()] + self
.get_unapplied()
548 f
= file(self
.__unapplied
_file
, 'w+')
549 f
.writelines([line
+ '\n' for line
in patches
])
552 append_string(self
.__applied
_file
, patch
.get_name())
553 self
.__set
_current
(name
)
555 def delete_patch(self
, name
):
558 patch
= Patch(name
, self
.__patch
_dir
)
560 if self
.__patch
_is
_current
(patch
):
562 elif self
.__patch
_applied
(name
):
563 raise StackException
, 'Cannot remove an applied patch, "%s", ' \
564 'which is not current' % name
565 elif not name
in self
.get_unapplied():
566 raise StackException
, 'Unknown patch "%s"' % name
570 unapplied
= self
.get_unapplied()
571 unapplied
.remove(name
)
572 f
= file(self
.__unapplied
_file
, 'w+')
573 f
.writelines([line
+ '\n' for line
in unapplied
])
576 def forward_patches(self
, names
):
577 """Try to fast-forward an array of patches.
579 On return, patches in names[0:returned_value] have been pushed on the
580 stack. Apply the rest with push_patch
582 unapplied
= self
.get_unapplied()
583 self
.__begin
_stack
_check
()
589 assert(name
in unapplied
)
591 patch
= Patch(name
, self
.__patch
_dir
)
594 bottom
= patch
.get_bottom()
595 top
= patch
.get_top()
597 # top != bottom always since we have a commit for each patch
599 # reset the backup information
600 patch
.set_bottom(head
, backup
= True)
601 patch
.set_top(top
, backup
= True)
604 head_tree
= git
.get_commit(head
).get_tree()
605 bottom_tree
= git
.get_commit(bottom
).get_tree()
606 if head_tree
== bottom_tree
:
607 # We must just reparent this patch and create a new commit
609 descr
= patch
.get_description()
610 author_name
= patch
.get_authname()
611 author_email
= patch
.get_authemail()
612 author_date
= patch
.get_authdate()
613 committer_name
= patch
.get_commname()
614 committer_email
= patch
.get_commemail()
616 top_tree
= git
.get_commit(top
).get_tree()
618 top
= git
.commit(message
= descr
, parents
= [head
],
619 cache_update
= False,
622 author_name
= author_name
,
623 author_email
= author_email
,
624 author_date
= author_date
,
625 committer_name
= committer_name
,
626 committer_email
= committer_email
)
628 patch
.set_bottom(head
, backup
= True)
629 patch
.set_top(top
, backup
= True)
632 # stop the fast-forwarding, must do a real merge
636 unapplied
.remove(name
)
640 append_strings(self
.__applied
_file
, names
[0:forwarded
])
642 f
= file(self
.__unapplied
_file
, 'w+')
643 f
.writelines([line
+ '\n' for line
in unapplied
])
646 self
.__set
_current
(name
)
650 def push_patch(self
, name
):
651 """Pushes a patch on the stack
653 unapplied
= self
.get_unapplied()
654 assert(name
in unapplied
)
656 self
.__begin
_stack
_check
()
658 patch
= Patch(name
, self
.__patch
_dir
)
660 head
= git
.get_head()
661 bottom
= patch
.get_bottom()
662 top
= patch
.get_top()
667 # top != bottom always since we have a commit for each patch
669 # reset the backup information
670 patch
.set_bottom(bottom
, backup
= True)
671 patch
.set_top(top
, backup
= True)
675 # new patch needs to be refreshed.
676 # The current patch is empty after merge.
677 patch
.set_bottom(head
, backup
= True)
678 patch
.set_top(head
, backup
= True)
680 # Try the fast applying first. If this fails, fall back to the
682 if not git
.apply_diff(bottom
, top
):
683 # if git.apply_diff() fails, the patch requires a diff3
684 # merge and can be reported as modified
687 # merge can fail but the patch needs to be pushed
689 git
.merge(bottom
, head
, top
)
690 except git
.GitException
, ex
:
691 print >> sys
.stderr
, \
692 'The merge failed during "push". ' \
693 'Use "refresh" after fixing the conflicts'
695 append_string(self
.__applied
_file
, name
)
697 unapplied
.remove(name
)
698 f
= file(self
.__unapplied
_file
, 'w+')
699 f
.writelines([line
+ '\n' for line
in unapplied
])
702 self
.__set
_current
(name
)
704 # head == bottom case doesn't need to refresh the patch
707 # if the merge was OK and no conflicts, just refresh the patch
708 # The GIT cache was already updated by the merge operation
709 self
.refresh_patch(cache_update
= False)
711 raise StackException
, str(ex
)
716 name
= self
.get_current()
719 patch
= Patch(name
, self
.__patch
_dir
)
722 return patch
.restore_old_boundaries()
724 def pop_patch(self
, name
):
725 """Pops the top patch from the stack
727 applied
= self
.get_applied()
729 assert(name
in applied
)
731 patch
= Patch(name
, self
.__patch
_dir
)
733 git
.switch(patch
.get_bottom())
735 # save the new applied list
736 idx
= applied
.index(name
) + 1
738 popped
= applied
[:idx
]
740 unapplied
= popped
+ self
.get_unapplied()
742 f
= file(self
.__unapplied
_file
, 'w+')
743 f
.writelines([line
+ '\n' for line
in unapplied
])
749 f
= file(self
.__applied
_file
, 'w+')
750 f
.writelines([line
+ '\n' for line
in applied
])
754 self
.__set
_current
(None)
756 self
.__set
_current
(applied
[-1])
758 self
.__end
_stack
_check
()
760 def empty_patch(self
, name
):
761 """Returns True if the patch is empty
763 patch
= Patch(name
, self
.__patch
_dir
)
764 bottom
= patch
.get_bottom()
765 top
= patch
.get_top()
769 elif git
.get_commit(top
).get_tree() \
770 == git
.get_commit(bottom
).get_tree():
775 def rename_patch(self
, oldname
, newname
):
776 applied
= self
.get_applied()
777 unapplied
= self
.get_unapplied()
779 if oldname
== newname
:
780 raise StackException
, '"To" name and "from" name are the same'
782 if newname
in applied
or newname
in unapplied
:
783 raise StackException
, 'Patch "%s" already exists' % newname
785 if oldname
in unapplied
:
786 Patch(oldname
, self
.__patch
_dir
).rename(newname
)
787 unapplied
[unapplied
.index(oldname
)] = newname
789 f
= file(self
.__unapplied
_file
, 'w+')
790 f
.writelines([line
+ '\n' for line
in unapplied
])
792 elif oldname
in applied
:
793 Patch(oldname
, self
.__patch
_dir
).rename(newname
)
794 if oldname
== self
.get_current():
795 self
.__set
_current
(newname
)
797 applied
[applied
.index(oldname
)] = newname
799 f
= file(self
.__applied
_file
, 'w+')
800 f
.writelines([line
+ '\n' for line
in applied
])
803 raise StackException
, 'Unknown patch "%s"' % oldname