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):
35 __comment_prefix
= 'STG:'
37 def __clean_comments(f
):
38 """Removes lines marked for status in a commit file
42 # remove status-prefixed lines
43 lines
= filter(lambda x
: x
[0:len(__comment_prefix
)] != __comment_prefix
,
45 # remove empty lines at the end
46 while len(lines
) != 0 and lines
[-1] == '\n':
49 f
.seek(0); f
.truncate()
52 def edit_file(string
, comment
):
54 tmpl
= os
.path
.join(git
.base_dir
, 'patchdescr.tmpl')
59 elif os
.path
.isfile(tmpl
):
60 print >> f
, file(tmpl
).read().rstrip()
63 print >> f
, __comment_prefix
, comment
64 print >> f
, __comment_prefix
, \
65 'Lines prefixed with "%s" will be automatically removed.' \
67 print >> f
, __comment_prefix
, \
68 'Trailing empty lines will be automatically removed.'
72 if 'EDITOR' in os
.environ
:
73 editor
= os
.environ
['EDITOR']
76 editor
+= ' %s' % fname
78 print 'Invoking the editor: "%s"...' % editor
,
80 print 'done (exit code: %d)' % os
.system(editor
)
98 """Basic patch implementation
100 def __init__(self
, name
, patch_dir
):
101 self
.__patch_dir
= patch_dir
103 self
.__dir
= os
.path
.join(self
.__patch_dir
, self
.__name
)
107 create_empty_file(os
.path
.join(self
.__dir
, 'bottom'))
108 create_empty_file(os
.path
.join(self
.__dir
, 'top'))
111 for f
in os
.listdir(self
.__dir
):
112 os
.remove(os
.path
.join(self
.__dir
, f
))
118 def __get_field(self
, name
, multiline
= False):
119 id_file
= os
.path
.join(self
.__dir
, name
)
120 if os
.path
.isfile(id_file
):
121 string
= read_string(id_file
, multiline
)
129 def __set_field(self
, name
, string
, multiline
= False):
130 fname
= os
.path
.join(self
.__dir
, name
)
131 if string
and string
!= '':
132 write_string(fname
, string
, multiline
)
133 elif os
.path
.isfile(fname
):
136 def get_bottom(self
):
137 return self
.__get_field('bottom')
139 def set_bottom(self
, string
, backup
= False):
141 self
.__set_field('bottom.old', self
.__get_field('bottom'))
142 self
.__set_field('bottom', string
)
145 return self
.__get_field('top')
147 def set_top(self
, string
, backup
= False):
149 self
.__set_field('top.old', self
.__get_field('top'))
150 self
.__set_field('top', string
)
152 def restore_old_boundaries(self
):
153 bottom
= self
.__get_field('bottom.old')
154 top
= self
.__get_field('top.old')
157 self
.__set_field('bottom', bottom
)
158 self
.__set_field('top', top
)
160 raise StackException
, 'No patch undo information'
162 def get_description(self
):
163 return self
.__get_field('description', True)
165 def set_description(self
, string
):
166 self
.__set_field('description', string
, True)
168 def get_authname(self
):
169 return self
.__get_field('authname')
171 def set_authname(self
, string
):
172 if not string
and config
.has_option('stgit', 'authname'):
173 string
= config
.get('stgit', 'authname')
174 self
.__set_field('authname', string
)
176 def get_authemail(self
):
177 return self
.__get_field('authemail')
179 def set_authemail(self
, string
):
180 if not string
and config
.has_option('stgit', 'authemail'):
181 string
= config
.get('stgit', 'authemail')
182 self
.__set_field('authemail', string
)
184 def get_authdate(self
):
185 return self
.__get_field('authdate')
187 def set_authdate(self
, string
):
188 self
.__set_field('authdate', string
)
190 def get_commname(self
):
191 return self
.__get_field('commname')
193 def set_commname(self
, string
):
194 if not string
and config
.has_option('stgit', 'commname'):
195 string
= config
.get('stgit', 'commname')
196 self
.__set_field('commname', string
)
198 def get_commemail(self
):
199 return self
.__get_field('commemail')
201 def set_commemail(self
, string
):
202 if not string
and config
.has_option('stgit', 'commemail'):
203 string
= config
.get('stgit', 'commemail')
204 self
.__set_field('commemail', string
)
208 """Class including the operations on series
210 def __init__(self
, name
= None):
211 """Takes a series name as the parameter. A valid .git/patches/name
212 directory should exist
217 self
.__name
= git
.get_head_file()
220 self
.__patch_dir
= os
.path
.join(git
.base_dir
, 'patches',
222 self
.__base_file
= os
.path
.join(git
.base_dir
, 'refs', 'bases',
224 self
.__applied_file
= os
.path
.join(self
.__patch_dir
, 'applied')
225 self
.__unapplied_file
= os
.path
.join(self
.__patch_dir
, 'unapplied')
226 self
.__current_file
= os
.path
.join(self
.__patch_dir
, 'current')
228 def __set_current(self
, name
):
229 """Sets the topmost patch
232 write_string(self
.__current_file
, name
)
234 create_empty_file(self
.__current_file
)
236 def get_patch(self
, name
):
237 """Return a Patch object for the given name
239 return Patch(name
, self
.__patch_dir
)
241 def get_current(self
):
242 """Return a Patch object representing the topmost patch
244 if os
.path
.isfile(self
.__current_file
):
245 name
= read_string(self
.__current_file
)
253 def get_applied(self
):
254 f
= file(self
.__applied_file
)
255 names
= [line
.strip() for line
in f
.readlines()]
259 def get_unapplied(self
):
260 f
= file(self
.__unapplied_file
)
261 names
= [line
.strip() for line
in f
.readlines()]
265 def get_base_file(self
):
266 return self
.__base_file
268 def __patch_is_current(self
, patch
):
269 return patch
.get_name() == read_string(self
.__current_file
)
271 def __patch_applied(self
, name
):
272 """Return true if the patch exists in the applied list
274 return name
in self
.get_applied()
276 def __patch_unapplied(self
, name
):
277 """Return true if the patch exists in the unapplied list
279 return name
in self
.get_unapplied()
281 def __begin_stack_check(self
):
282 """Save the current HEAD into .git/refs/heads/base if the stack
285 if len(self
.get_applied()) == 0:
286 head
= git
.get_head()
287 if os
.path
.exists(self
.__base_file
):
288 raise StackException
, 'stack empty but the base file exists'
289 write_string(self
.__base_file
, head
)
291 def __end_stack_check(self
):
292 """Remove .git/refs/heads/base if the stack is empty
294 if len(self
.get_applied()) == 0:
295 if not os
.path
.exists(self
.__base_file
):
296 print 'Warning: stack empty but the base file is missing'
298 os
.remove(self
.__base_file
)
300 def head_top_equal(self
):
301 """Return true if the head and the top are the same
303 crt
= self
.get_current()
305 # we don't care, no patches applied
307 return git
.get_head() == Patch(crt
, self
.__patch_dir
).get_top()
310 """Initialises the stgit series
312 bases_dir
= os
.path
.join(git
.base_dir
, 'refs', 'bases')
314 if os
.path
.isdir(self
.__patch_dir
):
315 raise StackException
, self
.__patch_dir
+ ' already exists'
316 os
.makedirs(self
.__patch_dir
)
318 if not os
.path
.isdir(bases_dir
):
319 os
.makedirs(bases_dir
)
321 create_empty_file(self
.__applied_file
)
322 create_empty_file(self
.__unapplied_file
)
324 def refresh_patch(self
, message
= None, edit
= False,
325 author_name
= None, author_email
= None,
327 committer_name
= None, committer_email
= None):
328 """Generates a new commit for the given patch
330 name
= self
.get_current()
332 raise StackException
, 'No patches applied'
334 patch
= Patch(name
, self
.__patch_dir
)
336 descr
= patch
.get_description()
337 if not (message
or descr
):
343 if not message
and edit
:
344 descr
= edit_file(descr
.rstrip(), \
345 'Please edit the description for patch "%s" ' \
349 author_name
= patch
.get_authname()
351 author_email
= patch
.get_authemail()
353 author_date
= patch
.get_authdate()
354 if not committer_name
:
355 committer_name
= patch
.get_commname()
356 if not committer_email
:
357 committer_email
= patch
.get_commemail()
359 commit_id
= git
.commit(message
= descr
, parents
= [patch
.get_bottom()],
361 author_name
= author_name
,
362 author_email
= author_email
,
363 author_date
= author_date
,
364 committer_name
= committer_name
,
365 committer_email
= committer_email
)
367 patch
.set_top(commit_id
)
368 patch
.set_description(descr
)
369 patch
.set_authname(author_name
)
370 patch
.set_authemail(author_email
)
371 patch
.set_authdate(author_date
)
372 patch
.set_commname(committer_name
)
373 patch
.set_commemail(committer_email
)
375 def new_patch(self
, name
, message
= None, edit
= False,
376 author_name
= None, author_email
= None, author_date
= None,
377 committer_name
= None, committer_email
= None):
378 """Creates a new patch
380 if self
.__patch_applied(name
) or self
.__patch_unapplied(name
):
381 raise StackException
, 'Patch "%s" already exists' % name
384 descr
= edit_file(None, \
385 'Please enter the description for patch "%s" ' \
390 head
= git
.get_head()
392 self
.__begin_stack_check()
394 patch
= Patch(name
, self
.__patch_dir
)
396 patch
.set_bottom(head
)
398 patch
.set_description(descr
)
399 patch
.set_authname(author_name
)
400 patch
.set_authemail(author_email
)
401 patch
.set_authdate(author_date
)
402 patch
.set_commname(committer_name
)
403 patch
.set_commemail(committer_email
)
405 append_string(self
.__applied_file
, patch
.get_name())
406 self
.__set_current(name
)
408 def delete_patch(self
, name
):
411 patch
= Patch(name
, self
.__patch_dir
)
413 if self
.__patch_is_current(patch
):
415 elif self
.__patch_applied(name
):
416 raise StackException
, 'Cannot remove an applied patch, "%s", ' \
417 'which is not current' % name
418 elif not name
in self
.get_unapplied():
419 raise StackException
, 'Unknown patch "%s"' % name
423 unapplied
= self
.get_unapplied()
424 unapplied
.remove(name
)
425 f
= file(self
.__unapplied_file
, 'w+')
426 f
.writelines([line
+ '\n' for line
in unapplied
])
429 def push_patch(self
, name
):
430 """Pushes a patch on the stack
432 unapplied
= self
.get_unapplied()
433 assert(name
in unapplied
)
435 self
.__begin_stack_check()
437 patch
= Patch(name
, self
.__patch_dir
)
439 head
= git
.get_head()
440 bottom
= patch
.get_bottom()
441 top
= patch
.get_top()
445 # top != bottom always since we have a commit for each patch
447 # reset the backup information
448 patch
.set_bottom(bottom
, backup
= True)
449 patch
.set_top(top
, backup
= True)
453 # new patch needs to be refreshed.
454 # The current patch is empty after merge.
455 patch
.set_bottom(head
, backup
= True)
456 patch
.set_top(head
, backup
= True)
457 # merge/refresh can fail but the patch needs to be pushed
459 git
.merge(bottom
, head
, top
)
460 except git
.GitException
, ex
:
461 print >> sys
.stderr
, \
462 'The merge failed during "push". ' \
463 'Use "refresh" after fixing the conflicts'
466 append_string(self
.__applied_file
, name
)
468 unapplied
.remove(name
)
469 f
= file(self
.__unapplied_file
, 'w+')
470 f
.writelines([line
+ '\n' for line
in unapplied
])
473 self
.__set_current(name
)
476 # if the merge was OK and no conflicts, just refresh the patch
479 raise StackException
, str(ex
)
482 name
= self
.get_current()
485 patch
= Patch(name
, self
.__patch_dir
)
487 patch
.restore_old_boundaries()
489 def pop_patch(self
, name
):
490 """Pops the top patch from the stack
492 applied
= self
.get_applied()
494 assert(name
in applied
)
496 patch
= Patch(name
, self
.__patch_dir
)
498 git
.switch(patch
.get_bottom())
500 # save the new applied list
501 idx
= applied
.index(name
) + 1
503 popped
= applied
[:idx
]
505 unapplied
= popped
+ self
.get_unapplied()
507 f
= file(self
.__unapplied_file
, 'w+')
508 f
.writelines([line
+ '\n' for line
in unapplied
])
514 f
= file(self
.__applied_file
, 'w+')
515 f
.writelines([line
+ '\n' for line
in applied
])
519 self
.__set_current(None)
521 self
.__set_current(applied
[-1])
523 self
.__end_stack_check()
525 def empty_patch(self
, name
):
526 """Returns True if the patch is empty
528 patch
= Patch(name
, self
.__patch_dir
)
529 bottom
= patch
.get_bottom()
530 top
= patch
.get_top()
534 elif git
.Commit(top
).get_tree() == git
.Commit(bottom
).get_tree():