8970c04cda8bde7daa8cd3d6ea433eeb6de9ab5d
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 'EDITOR' in os
.environ
:
96 editor
= os
.environ
['EDITOR']
99 editor
+= ' %s' % fname
101 print 'Invoking the editor: "%s"...' % editor
,
103 print 'done (exit code: %d)' % os
.system(editor
)
105 f
= file(fname
, 'r+')
121 """Basic patch implementation
123 def __init__(self
, name
, patch_dir
):
124 self
.__patch_dir
= patch_dir
126 self
.__dir
= os
.path
.join(self
.__patch_dir
, self
.__name
)
130 create_empty_file(os
.path
.join(self
.__dir
, 'bottom'))
131 create_empty_file(os
.path
.join(self
.__dir
, 'top'))
134 for f
in os
.listdir(self
.__dir
):
135 os
.remove(os
.path
.join(self
.__dir
, f
))
141 def rename(self
, newname
):
143 self
.__name
= newname
144 self
.__dir
= os
.path
.join(self
.__patch_dir
, self
.__name
)
146 os
.rename(olddir
, self
.__dir
)
148 def __get_field(self
, name
, multiline
= False):
149 id_file
= os
.path
.join(self
.__dir
, name
)
150 if os
.path
.isfile(id_file
):
151 string
= read_string(id_file
, multiline
)
159 def __set_field(self
, name
, string
, multiline
= False):
160 fname
= os
.path
.join(self
.__dir
, name
)
161 if string
and string
!= '':
162 write_string(fname
, string
, multiline
)
163 elif os
.path
.isfile(fname
):
166 def get_bottom(self
):
167 return self
.__get_field('bottom')
169 def set_bottom(self
, string
, backup
= False):
171 self
.__set_field('bottom.old', self
.__get_field('bottom'))
172 self
.__set_field('bottom', string
)
175 return self
.__get_field('top')
177 def set_top(self
, string
, backup
= False):
179 self
.__set_field('top.old', self
.__get_field('top'))
180 self
.__set_field('top', string
)
182 def restore_old_boundaries(self
):
183 bottom
= self
.__get_field('bottom.old')
184 top
= self
.__get_field('top.old')
187 self
.__set_field('bottom', bottom
)
188 self
.__set_field('top', top
)
190 raise StackException
, 'No patch undo information'
192 def get_description(self
):
193 return self
.__get_field('description', True)
195 def set_description(self
, string
):
196 self
.__set_field('description', string
, True)
198 def get_authname(self
):
199 return self
.__get_field('authname')
201 def set_authname(self
, string
):
202 if not string
and config
.has_option('stgit', 'authname'):
203 string
= config
.get('stgit', 'authname')
204 self
.__set_field('authname', string
)
206 def get_authemail(self
):
207 return self
.__get_field('authemail')
209 def set_authemail(self
, string
):
210 if not string
and config
.has_option('stgit', 'authemail'):
211 string
= config
.get('stgit', 'authemail')
212 self
.__set_field('authemail', string
)
214 def get_authdate(self
):
215 return self
.__get_field('authdate')
217 def set_authdate(self
, string
):
218 self
.__set_field('authdate', string
)
220 def get_commname(self
):
221 return self
.__get_field('commname')
223 def set_commname(self
, string
):
224 if not string
and config
.has_option('stgit', 'commname'):
225 string
= config
.get('stgit', 'commname')
226 self
.__set_field('commname', string
)
228 def get_commemail(self
):
229 return self
.__get_field('commemail')
231 def set_commemail(self
, string
):
232 if not string
and config
.has_option('stgit', 'commemail'):
233 string
= config
.get('stgit', 'commemail')
234 self
.__set_field('commemail', string
)
238 """Class including the operations on series
240 def __init__(self
, name
= None):
241 """Takes a series name as the parameter. A valid .git/patches/name
242 directory should exist
247 self
.__name
= git
.get_head_file()
250 self
.__patch_dir
= os
.path
.join(git
.base_dir
, 'patches',
252 self
.__base_file
= os
.path
.join(git
.base_dir
, 'refs', 'bases',
254 self
.__applied_file
= os
.path
.join(self
.__patch_dir
, 'applied')
255 self
.__unapplied_file
= os
.path
.join(self
.__patch_dir
, 'unapplied')
256 self
.__current_file
= os
.path
.join(self
.__patch_dir
, 'current')
258 def __set_current(self
, name
):
259 """Sets the topmost patch
262 write_string(self
.__current_file
, name
)
264 create_empty_file(self
.__current_file
)
266 def get_patch(self
, name
):
267 """Return a Patch object for the given name
269 return Patch(name
, self
.__patch_dir
)
271 def get_current(self
):
272 """Return a Patch object representing the topmost patch
274 if os
.path
.isfile(self
.__current_file
):
275 name
= read_string(self
.__current_file
)
283 def get_applied(self
):
284 f
= file(self
.__applied_file
)
285 names
= [line
.strip() for line
in f
.readlines()]
289 def get_unapplied(self
):
290 f
= file(self
.__unapplied_file
)
291 names
= [line
.strip() for line
in f
.readlines()]
295 def get_base_file(self
):
296 return self
.__base_file
298 def __patch_is_current(self
, patch
):
299 return patch
.get_name() == read_string(self
.__current_file
)
301 def __patch_applied(self
, name
):
302 """Return true if the patch exists in the applied list
304 return name
in self
.get_applied()
306 def __patch_unapplied(self
, name
):
307 """Return true if the patch exists in the unapplied list
309 return name
in self
.get_unapplied()
311 def __begin_stack_check(self
):
312 """Save the current HEAD into .git/refs/heads/base if the stack
315 if len(self
.get_applied()) == 0:
316 head
= git
.get_head()
317 write_string(self
.__base_file
, head
)
319 def __end_stack_check(self
):
320 """Remove .git/refs/heads/base if the stack is empty.
321 This warning should never happen
323 if len(self
.get_applied()) == 0 \
324 and read_string(self
.__base_file
) != git
.get_head():
325 print 'Warning: stack empty but the HEAD and base are different'
327 def head_top_equal(self
):
328 """Return true if the head and the top are the same
330 crt
= self
.get_current()
332 # we don't care, no patches applied
334 return git
.get_head() == Patch(crt
, self
.__patch_dir
).get_top()
337 """Initialises the stgit series
339 bases_dir
= os
.path
.join(git
.base_dir
, 'refs', 'bases')
341 if os
.path
.isdir(self
.__patch_dir
):
342 raise StackException
, self
.__patch_dir
+ ' already exists'
343 os
.makedirs(self
.__patch_dir
)
345 if not os
.path
.isdir(bases_dir
):
346 os
.makedirs(bases_dir
)
348 create_empty_file(self
.__applied_file
)
349 create_empty_file(self
.__unapplied_file
)
350 self
.__begin_stack_check()
352 def refresh_patch(self
, message
= None, edit
= False, show_patch
= False,
354 author_name
= None, author_email
= None,
356 committer_name
= None, committer_email
= None,
357 commit_only
= False):
358 """Generates a new commit for the given patch
360 name
= self
.get_current()
362 raise StackException
, 'No patches applied'
364 patch
= Patch(name
, self
.__patch_dir
)
366 descr
= patch
.get_description()
367 if not (message
or descr
):
373 if not message
and edit
:
374 descr
= edit_file(self
, descr
.rstrip(), \
375 'Please edit the description for patch "%s" ' \
376 'above.' % name
, show_patch
)
379 author_name
= patch
.get_authname()
381 author_email
= patch
.get_authemail()
383 author_date
= patch
.get_authdate()
384 if not committer_name
:
385 committer_name
= patch
.get_commname()
386 if not committer_email
:
387 committer_email
= patch
.get_commemail()
389 commit_id
= git
.commit(message
= descr
, parents
= [patch
.get_bottom()],
390 cache_update
= cache_update
,
392 author_name
= author_name
,
393 author_email
= author_email
,
394 author_date
= author_date
,
395 committer_name
= committer_name
,
396 committer_email
= committer_email
)
399 patch
.set_top(commit_id
)
400 patch
.set_description(descr
)
401 patch
.set_authname(author_name
)
402 patch
.set_authemail(author_email
)
403 patch
.set_authdate(author_date
)
404 patch
.set_commname(committer_name
)
405 patch
.set_commemail(committer_email
)
409 def new_patch(self
, name
, message
= None, can_edit
= True, show_patch
= False,
410 author_name
= None, author_email
= None, author_date
= None,
411 committer_name
= None, committer_email
= None):
412 """Creates a new patch
414 if self
.__patch_applied(name
) or self
.__patch_unapplied(name
):
415 raise StackException
, 'Patch "%s" already exists' % name
417 if not message
and can_edit
:
418 descr
= edit_file(self
, None, \
419 'Please enter the description for patch "%s" ' \
420 'above.' % name
, show_patch
)
424 head
= git
.get_head()
426 self
.__begin_stack_check()
428 patch
= Patch(name
, self
.__patch_dir
)
430 patch
.set_bottom(head
)
432 patch
.set_description(descr
)
433 patch
.set_authname(author_name
)
434 patch
.set_authemail(author_email
)
435 patch
.set_authdate(author_date
)
436 patch
.set_commname(committer_name
)
437 patch
.set_commemail(committer_email
)
439 append_string(self
.__applied_file
, patch
.get_name())
440 self
.__set_current(name
)
442 def delete_patch(self
, name
):
445 patch
= Patch(name
, self
.__patch_dir
)
447 if self
.__patch_is_current(patch
):
449 elif self
.__patch_applied(name
):
450 raise StackException
, 'Cannot remove an applied patch, "%s", ' \
451 'which is not current' % name
452 elif not name
in self
.get_unapplied():
453 raise StackException
, 'Unknown patch "%s"' % name
457 unapplied
= self
.get_unapplied()
458 unapplied
.remove(name
)
459 f
= file(self
.__unapplied_file
, 'w+')
460 f
.writelines([line
+ '\n' for line
in unapplied
])
463 def push_patch(self
, name
):
464 """Pushes a patch on the stack
466 unapplied
= self
.get_unapplied()
467 assert(name
in unapplied
)
469 self
.__begin_stack_check()
471 patch
= Patch(name
, self
.__patch_dir
)
473 head
= git
.get_head()
474 bottom
= patch
.get_bottom()
475 top
= patch
.get_top()
479 # top != bottom always since we have a commit for each patch
481 # reset the backup information
482 patch
.set_bottom(bottom
, backup
= True)
483 patch
.set_top(top
, backup
= True)
487 # new patch needs to be refreshed.
488 # The current patch is empty after merge.
489 patch
.set_bottom(head
, backup
= True)
490 patch
.set_top(head
, backup
= True)
491 # merge/refresh can fail but the patch needs to be pushed
493 git
.merge(bottom
, head
, top
)
494 except git
.GitException
, ex
:
495 print >> sys
.stderr
, \
496 'The merge failed during "push". ' \
497 'Use "refresh" after fixing the conflicts'
500 append_string(self
.__applied_file
, name
)
502 unapplied
.remove(name
)
503 f
= file(self
.__unapplied_file
, 'w+')
504 f
.writelines([line
+ '\n' for line
in unapplied
])
507 self
.__set_current(name
)
509 # head == bottom case doesn't need to refresh the patch
512 # if the merge was OK and no conflicts, just refresh the patch
513 # The GIT cache was already updated by the merge operation
514 self
.refresh_patch(cache_update
= False)
516 raise StackException
, str(ex
)
519 name
= self
.get_current()
522 patch
= Patch(name
, self
.__patch_dir
)
525 patch
.restore_old_boundaries()
527 def pop_patch(self
, name
):
528 """Pops the top patch from the stack
530 applied
= self
.get_applied()
532 assert(name
in applied
)
534 patch
= Patch(name
, self
.__patch_dir
)
536 git
.switch(patch
.get_bottom())
538 # save the new applied list
539 idx
= applied
.index(name
) + 1
541 popped
= applied
[:idx
]
543 unapplied
= popped
+ self
.get_unapplied()
545 f
= file(self
.__unapplied_file
, 'w+')
546 f
.writelines([line
+ '\n' for line
in unapplied
])
552 f
= file(self
.__applied_file
, 'w+')
553 f
.writelines([line
+ '\n' for line
in applied
])
557 self
.__set_current(None)
559 self
.__set_current(applied
[-1])
561 self
.__end_stack_check()
563 def empty_patch(self
, name
):
564 """Returns True if the patch is empty
566 patch
= Patch(name
, self
.__patch_dir
)
567 bottom
= patch
.get_bottom()
568 top
= patch
.get_top()
572 elif git
.get_commit(top
).get_tree() \
573 == git
.get_commit(bottom
).get_tree():
578 def rename_patch(self
, oldname
, newname
):
579 applied
= self
.get_applied()
580 unapplied
= self
.get_unapplied()
582 if newname
in applied
or newname
in unapplied
:
583 raise StackException
, 'Patch "%s" already exists' % newname
585 if oldname
in unapplied
:
586 Patch(oldname
, self
.__patch_dir
).rename(newname
)
587 unapplied
[unapplied
.index(oldname
)] = newname
589 f
= file(self
.__unapplied_file
, 'w+')
590 f
.writelines([line
+ '\n' for line
in unapplied
])
592 elif oldname
in applied
:
593 Patch(oldname
, self
.__patch_dir
).rename(newname
)
594 if oldname
== self
.get_current():
595 self
.__set_current(newname
)
597 applied
[applied
.index(oldname
)] = newname
599 f
= file(self
.__applied_file
, 'w+')
600 f
.writelines([line
+ '\n' for line
in applied
])
603 raise StackException
, 'Unknown patch "%s"' % oldname