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 self
.__set_field('bottom.old', self
.__get_field('bottom'))
174 self
.__set_field('bottom', string
)
177 return self
.__get_field('top')
179 def set_top(self
, string
, backup
= False):
181 self
.__set_field('top.old', self
.__get_field('top'))
182 self
.__set_field('top', string
)
184 def restore_old_boundaries(self
):
185 bottom
= self
.__get_field('bottom.old')
186 top
= self
.__get_field('top.old')
189 self
.__set_field('bottom', bottom
)
190 self
.__set_field('top', top
)
192 raise StackException
, 'No patch undo information'
194 def get_description(self
):
195 return self
.__get_field('description', True)
197 def set_description(self
, string
):
198 self
.__set_field('description', string
, True)
200 def get_authname(self
):
201 return self
.__get_field('authname')
203 def set_authname(self
, string
):
204 if not string
and config
.has_option('stgit', 'authname'):
205 string
= config
.get('stgit', 'authname')
206 self
.__set_field('authname', string
)
208 def get_authemail(self
):
209 return self
.__get_field('authemail')
211 def set_authemail(self
, string
):
212 if not string
and config
.has_option('stgit', 'authemail'):
213 string
= config
.get('stgit', 'authemail')
214 self
.__set_field('authemail', string
)
216 def get_authdate(self
):
217 return self
.__get_field('authdate')
219 def set_authdate(self
, string
):
220 self
.__set_field('authdate', string
)
222 def get_commname(self
):
223 return self
.__get_field('commname')
225 def set_commname(self
, string
):
226 if not string
and config
.has_option('stgit', 'commname'):
227 string
= config
.get('stgit', 'commname')
228 self
.__set_field('commname', string
)
230 def get_commemail(self
):
231 return self
.__get_field('commemail')
233 def set_commemail(self
, string
):
234 if not string
and config
.has_option('stgit', 'commemail'):
235 string
= config
.get('stgit', 'commemail')
236 self
.__set_field('commemail', string
)
240 """Class including the operations on series
242 def __init__(self
, name
= None):
243 """Takes a series name as the parameter. A valid .git/patches/name
244 directory should exist
249 self
.__name
= git
.get_head_file()
252 self
.__patch_dir
= os
.path
.join(git
.base_dir
, 'patches',
254 self
.__base_file
= os
.path
.join(git
.base_dir
, 'refs', 'bases',
256 self
.__applied_file
= os
.path
.join(self
.__patch_dir
, 'applied')
257 self
.__unapplied_file
= os
.path
.join(self
.__patch_dir
, 'unapplied')
258 self
.__current_file
= os
.path
.join(self
.__patch_dir
, 'current')
260 def __set_current(self
, name
):
261 """Sets the topmost patch
264 write_string(self
.__current_file
, name
)
266 create_empty_file(self
.__current_file
)
268 def get_patch(self
, name
):
269 """Return a Patch object for the given name
271 return Patch(name
, self
.__patch_dir
)
273 def get_current(self
):
274 """Return a Patch object representing the topmost patch
276 if os
.path
.isfile(self
.__current_file
):
277 name
= read_string(self
.__current_file
)
285 def get_applied(self
):
286 f
= file(self
.__applied_file
)
287 names
= [line
.strip() for line
in f
.readlines()]
291 def get_unapplied(self
):
292 f
= file(self
.__unapplied_file
)
293 names
= [line
.strip() for line
in f
.readlines()]
297 def get_base_file(self
):
298 return self
.__base_file
300 def __patch_is_current(self
, patch
):
301 return patch
.get_name() == read_string(self
.__current_file
)
303 def __patch_applied(self
, name
):
304 """Return true if the patch exists in the applied list
306 return name
in self
.get_applied()
308 def __patch_unapplied(self
, name
):
309 """Return true if the patch exists in the unapplied list
311 return name
in self
.get_unapplied()
313 def __begin_stack_check(self
):
314 """Save the current HEAD into .git/refs/heads/base if the stack
317 if len(self
.get_applied()) == 0:
318 head
= git
.get_head()
319 write_string(self
.__base_file
, head
)
321 def __end_stack_check(self
):
322 """Remove .git/refs/heads/base if the stack is empty.
323 This warning should never happen
325 if len(self
.get_applied()) == 0 \
326 and read_string(self
.__base_file
) != git
.get_head():
327 print 'Warning: stack empty but the HEAD and base are different'
329 def head_top_equal(self
):
330 """Return true if the head and the top are the same
332 crt
= self
.get_current()
334 # we don't care, no patches applied
336 return git
.get_head() == Patch(crt
, self
.__patch_dir
).get_top()
339 """Initialises the stgit series
341 bases_dir
= os
.path
.join(git
.base_dir
, 'refs', 'bases')
343 if os
.path
.isdir(self
.__patch_dir
):
344 raise StackException
, self
.__patch_dir
+ ' already exists'
345 os
.makedirs(self
.__patch_dir
)
347 if not os
.path
.isdir(bases_dir
):
348 os
.makedirs(bases_dir
)
350 create_empty_file(self
.__applied_file
)
351 create_empty_file(self
.__unapplied_file
)
352 self
.__begin_stack_check()
354 def refresh_patch(self
, message
= None, edit
= False, show_patch
= False,
356 author_name
= None, author_email
= None,
358 committer_name
= None, committer_email
= None,
359 commit_only
= False):
360 """Generates a new commit for the given patch
362 name
= self
.get_current()
364 raise StackException
, 'No patches applied'
366 patch
= Patch(name
, self
.__patch_dir
)
368 descr
= patch
.get_description()
369 if not (message
or descr
):
375 if not message
and edit
:
376 descr
= edit_file(self
, descr
.rstrip(), \
377 'Please edit the description for patch "%s" ' \
378 'above.' % name
, show_patch
)
381 author_name
= patch
.get_authname()
383 author_email
= patch
.get_authemail()
385 author_date
= patch
.get_authdate()
386 if not committer_name
:
387 committer_name
= patch
.get_commname()
388 if not committer_email
:
389 committer_email
= patch
.get_commemail()
391 commit_id
= git
.commit(message
= descr
, parents
= [patch
.get_bottom()],
392 cache_update
= cache_update
,
394 author_name
= author_name
,
395 author_email
= author_email
,
396 author_date
= author_date
,
397 committer_name
= committer_name
,
398 committer_email
= committer_email
)
401 patch
.set_top(commit_id
)
402 patch
.set_description(descr
)
403 patch
.set_authname(author_name
)
404 patch
.set_authemail(author_email
)
405 patch
.set_authdate(author_date
)
406 patch
.set_commname(committer_name
)
407 patch
.set_commemail(committer_email
)
411 def new_patch(self
, name
, message
= None, can_edit
= True, show_patch
= False,
412 author_name
= None, author_email
= None, author_date
= None,
413 committer_name
= None, committer_email
= None):
414 """Creates a new patch
416 if self
.__patch_applied(name
) or self
.__patch_unapplied(name
):
417 raise StackException
, 'Patch "%s" already exists' % name
419 if not message
and can_edit
:
420 descr
= edit_file(self
, None, \
421 'Please enter the description for patch "%s" ' \
422 'above.' % name
, show_patch
)
426 head
= git
.get_head()
428 self
.__begin_stack_check()
430 patch
= Patch(name
, self
.__patch_dir
)
432 patch
.set_bottom(head
)
434 patch
.set_description(descr
)
435 patch
.set_authname(author_name
)
436 patch
.set_authemail(author_email
)
437 patch
.set_authdate(author_date
)
438 patch
.set_commname(committer_name
)
439 patch
.set_commemail(committer_email
)
441 append_string(self
.__applied_file
, patch
.get_name())
442 self
.__set_current(name
)
444 def delete_patch(self
, name
):
447 patch
= Patch(name
, self
.__patch_dir
)
449 if self
.__patch_is_current(patch
):
451 elif self
.__patch_applied(name
):
452 raise StackException
, 'Cannot remove an applied patch, "%s", ' \
453 'which is not current' % name
454 elif not name
in self
.get_unapplied():
455 raise StackException
, 'Unknown patch "%s"' % name
459 unapplied
= self
.get_unapplied()
460 unapplied
.remove(name
)
461 f
= file(self
.__unapplied_file
, 'w+')
462 f
.writelines([line
+ '\n' for line
in unapplied
])
465 def forward_patches(self
, names
):
466 """Try to fast-forward an array of patches.
468 On return, patches in names[0:returned_value] have been pushed on the
469 stack. Apply the rest with push_patch
471 unapplied
= self
.get_unapplied()
472 self
.__begin_stack_check()
478 assert(name
in unapplied
)
480 patch
= Patch(name
, self
.__patch_dir
)
483 bottom
= patch
.get_bottom()
484 top
= patch
.get_top()
486 # top != bottom always since we have a commit for each patch
488 # reset the backup information
489 patch
.set_bottom(bottom
, backup
= True)
490 patch
.set_top(top
, backup
= True)
494 # stop the fast-forwarding, must do a real merge
498 unapplied
.remove(name
)
502 append_strings(self
.__applied_file
, names
[0:forwarded
])
504 f
= file(self
.__unapplied_file
, 'w+')
505 f
.writelines([line
+ '\n' for line
in unapplied
])
508 self
.__set_current(name
)
512 def push_patch(self
, name
):
513 """Pushes a patch on the stack
515 unapplied
= self
.get_unapplied()
516 assert(name
in unapplied
)
518 self
.__begin_stack_check()
520 patch
= Patch(name
, self
.__patch_dir
)
522 head
= git
.get_head()
523 bottom
= patch
.get_bottom()
524 top
= patch
.get_top()
528 # top != bottom always since we have a commit for each patch
530 # reset the backup information
531 patch
.set_bottom(bottom
, backup
= True)
532 patch
.set_top(top
, backup
= True)
536 # new patch needs to be refreshed.
537 # The current patch is empty after merge.
538 patch
.set_bottom(head
, backup
= True)
539 patch
.set_top(head
, backup
= True)
540 # merge/refresh can fail but the patch needs to be pushed
542 git
.merge(bottom
, head
, top
)
543 except git
.GitException
, ex
:
544 print >> sys
.stderr
, \
545 'The merge failed during "push". ' \
546 'Use "refresh" after fixing the conflicts'
549 append_string(self
.__applied_file
, name
)
551 unapplied
.remove(name
)
552 f
= file(self
.__unapplied_file
, 'w+')
553 f
.writelines([line
+ '\n' for line
in unapplied
])
556 self
.__set_current(name
)
558 # head == bottom case doesn't need to refresh the patch
561 # if the merge was OK and no conflicts, just refresh the patch
562 # The GIT cache was already updated by the merge operation
563 self
.refresh_patch(cache_update
= False)
565 raise StackException
, str(ex
)
568 name
= self
.get_current()
571 patch
= Patch(name
, self
.__patch_dir
)
574 patch
.restore_old_boundaries()
576 def pop_patch(self
, name
):
577 """Pops the top patch from the stack
579 applied
= self
.get_applied()
581 assert(name
in applied
)
583 patch
= Patch(name
, self
.__patch_dir
)
585 git
.switch(patch
.get_bottom())
587 # save the new applied list
588 idx
= applied
.index(name
) + 1
590 popped
= applied
[:idx
]
592 unapplied
= popped
+ self
.get_unapplied()
594 f
= file(self
.__unapplied_file
, 'w+')
595 f
.writelines([line
+ '\n' for line
in unapplied
])
601 f
= file(self
.__applied_file
, 'w+')
602 f
.writelines([line
+ '\n' for line
in applied
])
606 self
.__set_current(None)
608 self
.__set_current(applied
[-1])
610 self
.__end_stack_check()
612 def empty_patch(self
, name
):
613 """Returns True if the patch is empty
615 patch
= Patch(name
, self
.__patch_dir
)
616 bottom
= patch
.get_bottom()
617 top
= patch
.get_top()
621 elif git
.get_commit(top
).get_tree() \
622 == git
.get_commit(bottom
).get_tree():
627 def rename_patch(self
, oldname
, newname
):
628 applied
= self
.get_applied()
629 unapplied
= self
.get_unapplied()
631 if newname
in applied
or newname
in unapplied
:
632 raise StackException
, 'Patch "%s" already exists' % newname
634 if oldname
in unapplied
:
635 Patch(oldname
, self
.__patch_dir
).rename(newname
)
636 unapplied
[unapplied
.index(oldname
)] = newname
638 f
= file(self
.__unapplied_file
, 'w+')
639 f
.writelines([line
+ '\n' for line
in unapplied
])
641 elif oldname
in applied
:
642 Patch(oldname
, self
.__patch_dir
).rename(newname
)
643 if oldname
== self
.get_current():
644 self
.__set_current(newname
)
646 applied
[applied
.index(oldname
)] = newname
648 f
= file(self
.__applied_file
, 'w+')
649 f
.writelines([line
+ '\n' for line
in applied
])
652 raise StackException
, 'Unknown patch "%s"' % oldname