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
, string
, 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:'
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 string
= read_string(id_file
, multiline
)
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
):
168 def get_bottom(self
):
169 return self
.__get_field('bottom')
171 def set_bottom(self
, string
, backup
= False):
173 curr
= self
.__get_field('bottom')
175 self
.__set_field('bottom.old', curr
)
177 self
.__set_field('bottom.old', None)
178 self
.__set_field('bottom', string
)
181 return self
.__get_field('top')
183 def set_top(self
, string
, backup
= False):
185 curr
= self
.__get_field('top')
187 self
.__set_field('top.old', curr
)
189 self
.__set_field('top.old', None)
190 self
.__set_field('top', string
)
192 def restore_old_boundaries(self
):
193 bottom
= self
.__get_field('bottom.old')
194 top
= self
.__get_field('top.old')
197 self
.__set_field('bottom', bottom
)
198 self
.__set_field('top', top
)
203 def get_description(self
):
204 return self
.__get_field('description', True)
206 def set_description(self
, string
):
207 self
.__set_field('description', string
, True)
209 def get_authname(self
):
210 return self
.__get_field('authname')
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
)
217 def get_authemail(self
):
218 return self
.__get_field('authemail')
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
)
225 def get_authdate(self
):
226 return self
.__get_field('authdate')
228 def set_authdate(self
, string
):
229 self
.__set_field('authdate', string
)
231 def get_commname(self
):
232 return self
.__get_field('commname')
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
)
239 def get_commemail(self
):
240 return self
.__get_field('commemail')
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
)
249 """Class including the operations on series
251 def __init__(self
, name
= None):
252 """Takes a series name as the parameter.
257 self
.__name
= git
.get_head_file()
260 self
.__patch_dir
= os
.path
.join(git
.base_dir
, 'patches',
262 self
.__base_file
= os
.path
.join(git
.base_dir
, 'refs', 'bases',
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')
268 def get_branch(self
):
269 """Return the branch name for the Series object
273 def __set_current(self
, name
):
274 """Sets the topmost patch
277 write_string(self
.__current_file
, name
)
279 create_empty_file(self
.__current_file
)
281 def get_patch(self
, name
):
282 """Return a Patch object for the given name
284 return Patch(name
, self
.__patch_dir
)
286 def get_current(self
):
287 """Return a Patch object representing the topmost patch
289 if os
.path
.isfile(self
.__current_file
):
290 name
= read_string(self
.__current_file
)
298 def get_applied(self
):
299 if not os
.path
.isfile(self
.__applied_file
):
300 raise StackException
, 'Branch "%s" not initialised' % self
.__name
301 f
= file(self
.__applied_file
)
302 names
= [line
.strip() for line
in f
.readlines()]
306 def get_unapplied(self
):
307 if not os
.path
.isfile(self
.__unapplied_file
):
308 raise StackException
, 'Branch "%s" not initialised' % self
.__name
309 f
= file(self
.__unapplied_file
)
310 names
= [line
.strip() for line
in f
.readlines()]
314 def get_base_file(self
):
315 return self
.__base_file
317 def __patch_is_current(self
, patch
):
318 return patch
.get_name() == read_string(self
.__current_file
)
320 def __patch_applied(self
, name
):
321 """Return true if the patch exists in the applied list
323 return name
in self
.get_applied()
325 def __patch_unapplied(self
, name
):
326 """Return true if the patch exists in the unapplied list
328 return name
in self
.get_unapplied()
330 def __begin_stack_check(self
):
331 """Save the current HEAD into .git/refs/heads/base if the stack
334 if len(self
.get_applied()) == 0:
335 head
= git
.get_head()
336 write_string(self
.__base_file
, head
)
338 def __end_stack_check(self
):
339 """Remove .git/refs/heads/base if the stack is empty.
340 This warning should never happen
342 if len(self
.get_applied()) == 0 \
343 and read_string(self
.__base_file
) != git
.get_head():
344 print 'Warning: stack empty but the HEAD and base are different'
346 def head_top_equal(self
):
347 """Return true if the head and the top are the same
349 crt
= self
.get_current()
351 # we don't care, no patches applied
353 return git
.get_head() == Patch(crt
, self
.__patch_dir
).get_top()
356 """Initialises the stgit series
358 bases_dir
= os
.path
.join(git
.base_dir
, 'refs', 'bases')
360 if os
.path
.isdir(self
.__patch_dir
):
361 raise StackException
, self
.__patch_dir
+ ' already exists'
362 os
.makedirs(self
.__patch_dir
)
364 if not os
.path
.isdir(bases_dir
):
365 os
.makedirs(bases_dir
)
367 create_empty_file(self
.__applied_file
)
368 create_empty_file(self
.__unapplied_file
)
369 self
.__begin_stack_check()
371 def refresh_patch(self
, message
= None, edit
= False, show_patch
= False,
373 author_name
= None, author_email
= None,
375 committer_name
= None, committer_email
= None,
376 commit_only
= False):
377 """Generates a new commit for the given patch
379 name
= self
.get_current()
381 raise StackException
, 'No patches applied'
383 patch
= Patch(name
, self
.__patch_dir
)
385 descr
= patch
.get_description()
386 if not (message
or descr
):
392 if not message
and edit
:
393 descr
= edit_file(self
, descr
.rstrip(), \
394 'Please edit the description for patch "%s" ' \
395 'above.' % name
, show_patch
)
398 author_name
= patch
.get_authname()
400 author_email
= patch
.get_authemail()
402 author_date
= patch
.get_authdate()
403 if not committer_name
:
404 committer_name
= patch
.get_commname()
405 if not committer_email
:
406 committer_email
= patch
.get_commemail()
408 commit_id
= git
.commit(message
= descr
, parents
= [patch
.get_bottom()],
409 cache_update
= cache_update
,
411 author_name
= author_name
,
412 author_email
= author_email
,
413 author_date
= author_date
,
414 committer_name
= committer_name
,
415 committer_email
= committer_email
)
418 patch
.set_top(commit_id
)
419 patch
.set_description(descr
)
420 patch
.set_authname(author_name
)
421 patch
.set_authemail(author_email
)
422 patch
.set_authdate(author_date
)
423 patch
.set_commname(committer_name
)
424 patch
.set_commemail(committer_email
)
428 def new_patch(self
, name
, message
= None, can_edit
= True,
429 unapplied
= False, show_patch
= False,
430 top
= None, bottom
= None,
431 author_name
= None, author_email
= None, author_date
= None,
432 committer_name
= None, committer_email
= None):
433 """Creates a new patch
435 if self
.__patch_applied(name
) or self
.__patch_unapplied(name
):
436 raise StackException
, 'Patch "%s" already exists' % name
438 if not message
and can_edit
:
439 descr
= edit_file(self
, None, \
440 'Please enter the description for patch "%s" ' \
441 'above.' % name
, show_patch
)
445 head
= git
.get_head()
447 self
.__begin_stack_check()
449 patch
= Patch(name
, self
.__patch_dir
)
453 patch
.set_bottom(bottom
)
455 patch
.set_bottom(head
)
461 patch
.set_description(descr
)
462 patch
.set_authname(author_name
)
463 patch
.set_authemail(author_email
)
464 patch
.set_authdate(author_date
)
465 patch
.set_commname(committer_name
)
466 patch
.set_commemail(committer_email
)
469 patches
= [patch
.get_name()] + self
.get_unapplied()
471 f
= file(self
.__unapplied_file
, 'w+')
472 f
.writelines([line
+ '\n' for line
in patches
])
475 append_string(self
.__applied_file
, patch
.get_name())
476 self
.__set_current(name
)
478 def delete_patch(self
, name
):
481 patch
= Patch(name
, self
.__patch_dir
)
483 if self
.__patch_is_current(patch
):
485 elif self
.__patch_applied(name
):
486 raise StackException
, 'Cannot remove an applied patch, "%s", ' \
487 'which is not current' % name
488 elif not name
in self
.get_unapplied():
489 raise StackException
, 'Unknown patch "%s"' % name
493 unapplied
= self
.get_unapplied()
494 unapplied
.remove(name
)
495 f
= file(self
.__unapplied_file
, 'w+')
496 f
.writelines([line
+ '\n' for line
in unapplied
])
499 def forward_patches(self
, names
):
500 """Try to fast-forward an array of patches.
502 On return, patches in names[0:returned_value] have been pushed on the
503 stack. Apply the rest with push_patch
505 unapplied
= self
.get_unapplied()
506 self
.__begin_stack_check()
512 assert(name
in unapplied
)
514 patch
= Patch(name
, self
.__patch_dir
)
517 bottom
= patch
.get_bottom()
518 top
= patch
.get_top()
520 # top != bottom always since we have a commit for each patch
522 # reset the backup information
523 patch
.set_bottom(head
, backup
= True)
524 patch
.set_top(top
, backup
= True)
527 head_tree
= git
.get_commit(head
).get_tree()
528 bottom_tree
= git
.get_commit(bottom
).get_tree()
529 if head_tree
== bottom_tree
:
530 # We must just reparent this patch and create a new commit
532 descr
= patch
.get_description()
533 author_name
= patch
.get_authname()
534 author_email
= patch
.get_authemail()
535 author_date
= patch
.get_authdate()
536 committer_name
= patch
.get_commname()
537 committer_email
= patch
.get_commemail()
539 top_tree
= git
.get_commit(top
).get_tree()
541 top
= git
.commit(message
= descr
, parents
= [head
],
542 cache_update
= False,
545 author_name
= author_name
,
546 author_email
= author_email
,
547 author_date
= author_date
,
548 committer_name
= committer_name
,
549 committer_email
= committer_email
)
551 patch
.set_bottom(head
, backup
= True)
552 patch
.set_top(top
, backup
= True)
555 # stop the fast-forwarding, must do a real merge
559 unapplied
.remove(name
)
563 append_strings(self
.__applied_file
, names
[0:forwarded
])
565 f
= file(self
.__unapplied_file
, 'w+')
566 f
.writelines([line
+ '\n' for line
in unapplied
])
569 self
.__set_current(name
)
573 def push_patch(self
, name
):
574 """Pushes a patch on the stack
576 unapplied
= self
.get_unapplied()
577 assert(name
in unapplied
)
579 self
.__begin_stack_check()
581 patch
= Patch(name
, self
.__patch_dir
)
583 head
= git
.get_head()
584 bottom
= patch
.get_bottom()
585 top
= patch
.get_top()
589 # top != bottom always since we have a commit for each patch
591 # reset the backup information
592 patch
.set_bottom(bottom
, backup
= True)
593 patch
.set_top(top
, backup
= True)
597 # new patch needs to be refreshed.
598 # The current patch is empty after merge.
599 patch
.set_bottom(head
, backup
= True)
600 patch
.set_top(head
, backup
= True)
602 # Try the fast applying first. If this fails, fall back to the
604 if not git
.apply_diff(bottom
, top
):
605 # merge can fail but the patch needs to be pushed
607 git
.merge(bottom
, head
, top
)
608 except git
.GitException
, ex
:
609 print >> sys
.stderr
, \
610 'The merge failed during "push". ' \
611 'Use "refresh" after fixing the conflicts'
613 append_string(self
.__applied_file
, name
)
615 unapplied
.remove(name
)
616 f
= file(self
.__unapplied_file
, 'w+')
617 f
.writelines([line
+ '\n' for line
in unapplied
])
620 self
.__set_current(name
)
622 # head == bottom case doesn't need to refresh the patch
625 # if the merge was OK and no conflicts, just refresh the patch
626 # The GIT cache was already updated by the merge operation
627 self
.refresh_patch(cache_update
= False)
629 raise StackException
, str(ex
)
632 name
= self
.get_current()
635 patch
= Patch(name
, self
.__patch_dir
)
638 return patch
.restore_old_boundaries()
640 def pop_patch(self
, name
):
641 """Pops the top patch from the stack
643 applied
= self
.get_applied()
645 assert(name
in applied
)
647 patch
= Patch(name
, self
.__patch_dir
)
649 git
.switch(patch
.get_bottom())
651 # save the new applied list
652 idx
= applied
.index(name
) + 1
654 popped
= applied
[:idx
]
656 unapplied
= popped
+ self
.get_unapplied()
658 f
= file(self
.__unapplied_file
, 'w+')
659 f
.writelines([line
+ '\n' for line
in unapplied
])
665 f
= file(self
.__applied_file
, 'w+')
666 f
.writelines([line
+ '\n' for line
in applied
])
670 self
.__set_current(None)
672 self
.__set_current(applied
[-1])
674 self
.__end_stack_check()
676 def empty_patch(self
, name
):
677 """Returns True if the patch is empty
679 patch
= Patch(name
, self
.__patch_dir
)
680 bottom
= patch
.get_bottom()
681 top
= patch
.get_top()
685 elif git
.get_commit(top
).get_tree() \
686 == git
.get_commit(bottom
).get_tree():
691 def rename_patch(self
, oldname
, newname
):
692 applied
= self
.get_applied()
693 unapplied
= self
.get_unapplied()
695 if newname
in applied
or newname
in unapplied
:
696 raise StackException
, 'Patch "%s" already exists' % newname
698 if oldname
in unapplied
:
699 Patch(oldname
, self
.__patch_dir
).rename(newname
)
700 unapplied
[unapplied
.index(oldname
)] = newname
702 f
= file(self
.__unapplied_file
, 'w+')
703 f
.writelines([line
+ '\n' for line
in unapplied
])
705 elif oldname
in applied
:
706 Patch(oldname
, self
.__patch_dir
).rename(newname
)
707 if oldname
== self
.get_current():
708 self
.__set_current(newname
)
710 applied
[applied
.index(oldname
)] = newname
712 f
= file(self
.__applied_file
, 'w+')
713 f
.writelines([line
+ '\n' for line
in applied
])
716 raise StackException
, 'Unknown patch "%s"' % oldname