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 rename(self, newname):
120 self.__name = newname
121 self.__dir = os.path.join(self.__patch_dir, self.__name)
123 os.rename(olddir, self.__dir)
125 def __get_field(self, name, multiline = False):
126 id_file = os.path.join(self.__dir, name)
127 if os.path.isfile(id_file):
128 string = read_string(id_file, multiline)
136 def __set_field(self, name, string, multiline = False):
137 fname = os.path.join(self.__dir, name)
138 if string and string != '':
139 write_string(fname, string, multiline)
140 elif os.path.isfile(fname):
143 def get_bottom(self):
144 return self.__get_field('bottom')
146 def set_bottom(self, string, backup = False):
148 self.__set_field('bottom.old', self.__get_field('bottom'))
149 self.__set_field('bottom', string)
152 return self.__get_field('top')
154 def set_top(self, string, backup = False):
156 self.__set_field('top.old', self.__get_field('top'))
157 self.__set_field('top', string)
159 def restore_old_boundaries(self):
160 bottom = self.__get_field('bottom.old')
161 top = self.__get_field('top.old')
164 self.__set_field('bottom', bottom)
165 self.__set_field('top', top)
167 raise StackException, 'No patch undo information'
169 def get_description(self):
170 return self.__get_field('description', True)
172 def set_description(self, string):
173 self.__set_field('description', string, True)
175 def get_authname(self):
176 return self.__get_field('authname')
178 def set_authname(self, string):
179 if not string and config.has_option('stgit', 'authname'):
180 string = config.get('stgit', 'authname')
181 self.__set_field('authname', string)
183 def get_authemail(self):
184 return self.__get_field('authemail')
186 def set_authemail(self, string):
187 if not string and config.has_option('stgit', 'authemail'):
188 string = config.get('stgit', 'authemail')
189 self.__set_field('authemail', string)
191 def get_authdate(self):
192 return self.__get_field('authdate')
194 def set_authdate(self, string):
195 self.__set_field('authdate', string)
197 def get_commname(self):
198 return self.__get_field('commname')
200 def set_commname(self, string):
201 if not string and config.has_option('stgit', 'commname'):
202 string = config.get('stgit', 'commname')
203 self.__set_field('commname', string)
205 def get_commemail(self):
206 return self.__get_field('commemail')
208 def set_commemail(self, string):
209 if not string and config.has_option('stgit', 'commemail'):
210 string = config.get('stgit', 'commemail')
211 self.__set_field('commemail', string)
215 """Class including the operations on series
217 def __init__(self, name = None):
218 """Takes a series name as the parameter. A valid .git/patches/name
219 directory should exist
224 self.__name = git.get_head_file()
227 self.__patch_dir = os.path.join(git.base_dir, 'patches',
229 self.__base_file = os.path.join(git.base_dir, 'refs', 'bases',
231 self.__applied_file = os.path.join(self.__patch_dir, 'applied')
232 self.__unapplied_file = os.path.join(self.__patch_dir, 'unapplied')
233 self.__current_file = os.path.join(self.__patch_dir, 'current')
235 def __set_current(self, name):
236 """Sets the topmost patch
239 write_string(self.__current_file, name)
241 create_empty_file(self.__current_file)
243 def get_patch(self, name):
244 """Return a Patch object for the given name
246 return Patch(name, self.__patch_dir)
248 def get_current(self):
249 """Return a Patch object representing the topmost patch
251 if os.path.isfile(self.__current_file):
252 name = read_string(self.__current_file)
260 def get_applied(self):
261 f = file(self.__applied_file)
262 names = [line.strip() for line in f.readlines()]
266 def get_unapplied(self):
267 f = file(self.__unapplied_file)
268 names = [line.strip() for line in f.readlines()]
272 def get_base_file(self):
273 return self.__base_file
275 def __patch_is_current(self, patch):
276 return patch.get_name() == read_string(self.__current_file)
278 def __patch_applied(self, name):
279 """Return true if the patch exists in the applied list
281 return name in self.get_applied()
283 def __patch_unapplied(self, name):
284 """Return true if the patch exists in the unapplied list
286 return name in self.get_unapplied()
288 def __begin_stack_check(self):
289 """Save the current HEAD into .git/refs/heads/base if the stack
292 if len(self.get_applied()) == 0:
293 head = git.get_head()
294 write_string(self.__base_file, head)
296 def __end_stack_check(self):
297 """Remove .git/refs/heads/base if the stack is empty.
298 This warning should never happen
300 if len(self.get_applied()) == 0 \
301 and read_string(self.__base_file) != git.get_head():
302 print 'Warning: stack empty but the HEAD and base are different'
304 def head_top_equal(self):
305 """Return true if the head and the top are the same
307 crt = self.get_current()
309 # we don't care, no patches applied
311 return git.get_head() == Patch(crt, self.__patch_dir).get_top()
314 """Initialises the stgit series
316 bases_dir = os.path.join(git.base_dir, 'refs', 'bases')
318 if os.path.isdir(self.__patch_dir):
319 raise StackException, self.__patch_dir + ' already exists'
320 os.makedirs(self.__patch_dir)
322 if not os.path.isdir(bases_dir):
323 os.makedirs(bases_dir)
325 create_empty_file(self.__applied_file)
326 create_empty_file(self.__unapplied_file)
327 self.__begin_stack_check()
329 def refresh_patch(self, message = None, edit = False,
330 author_name = None, author_email = None,
332 committer_name = None, committer_email = None):
333 """Generates a new commit for the given patch
335 name = self.get_current()
337 raise StackException, 'No patches applied'
339 patch = Patch(name, self.__patch_dir)
341 descr = patch.get_description()
342 if not (message or descr):
348 if not message and edit:
349 descr = edit_file(descr.rstrip(), \
350 'Please edit the description for patch "%s" ' \
354 author_name = patch.get_authname()
356 author_email = patch.get_authemail()
358 author_date = patch.get_authdate()
359 if not committer_name:
360 committer_name = patch.get_commname()
361 if not committer_email:
362 committer_email = patch.get_commemail()
364 commit_id = git.commit(message = descr, parents = [patch.get_bottom()],
366 author_name = author_name,
367 author_email = author_email,
368 author_date = author_date,
369 committer_name = committer_name,
370 committer_email = committer_email)
372 patch.set_top(commit_id)
373 patch.set_description(descr)
374 patch.set_authname(author_name)
375 patch.set_authemail(author_email)
376 patch.set_authdate(author_date)
377 patch.set_commname(committer_name)
378 patch.set_commemail(committer_email)
380 def new_patch(self, name, message = None, edit = False,
381 author_name = None, author_email = None, author_date = None,
382 committer_name = None, committer_email = None):
383 """Creates a new patch
385 if self.__patch_applied(name) or self.__patch_unapplied(name):
386 raise StackException, 'Patch "%s" already exists' % name
389 descr = edit_file(None, \
390 'Please enter the description for patch "%s" ' \
395 head = git.get_head()
397 self.__begin_stack_check()
399 patch = Patch(name, self.__patch_dir)
401 patch.set_bottom(head)
403 patch.set_description(descr)
404 patch.set_authname(author_name)
405 patch.set_authemail(author_email)
406 patch.set_authdate(author_date)
407 patch.set_commname(committer_name)
408 patch.set_commemail(committer_email)
410 append_string(self.__applied_file, patch.get_name())
411 self.__set_current(name)
413 def delete_patch(self, name):
416 patch = Patch(name, self.__patch_dir)
418 if self.__patch_is_current(patch):
420 elif self.__patch_applied(name):
421 raise StackException, 'Cannot remove an applied patch, "%s", ' \
422 'which is not current' % name
423 elif not name in self.get_unapplied():
424 raise StackException, 'Unknown patch "%s"' % name
428 unapplied = self.get_unapplied()
429 unapplied.remove(name)
430 f = file(self.__unapplied_file, 'w+')
431 f.writelines([line + '\n' for line in unapplied])
434 def push_patch(self, name):
435 """Pushes a patch on the stack
437 unapplied = self.get_unapplied()
438 assert(name in unapplied)
440 self.__begin_stack_check()
442 patch = Patch(name, self.__patch_dir)
444 head = git.get_head()
445 bottom = patch.get_bottom()
446 top = patch.get_top()
450 # top != bottom always since we have a commit for each patch
452 # reset the backup information
453 patch.set_bottom(bottom, backup = True)
454 patch.set_top(top, backup = True)
458 # new patch needs to be refreshed.
459 # The current patch is empty after merge.
460 patch.set_bottom(head, backup = True)
461 patch.set_top(head, backup = True)
462 # merge/refresh can fail but the patch needs to be pushed
464 git.merge(bottom, head, top)
465 except git.GitException, ex:
466 print >> sys.stderr, \
467 'The merge failed during "push". ' \
468 'Use "refresh" after fixing the conflicts'
471 append_string(self.__applied_file, name)
473 unapplied.remove(name)
474 f = file(self.__unapplied_file, 'w+')
475 f.writelines([line + '\n' for line in unapplied])
478 self.__set_current(name)
481 # if the merge was OK and no conflicts, just refresh the patch
484 raise StackException, str(ex)
487 name = self.get_current()
490 patch = Patch(name, self.__patch_dir)
492 patch.restore_old_boundaries()
494 def pop_patch(self, name):
495 """Pops the top patch from the stack
497 applied = self.get_applied()
499 assert(name in applied)
501 patch = Patch(name, self.__patch_dir)
503 git.switch(patch.get_bottom())
505 # save the new applied list
506 idx = applied.index(name) + 1
508 popped = applied[:idx]
510 unapplied = popped + self.get_unapplied()
512 f = file(self.__unapplied_file, 'w+')
513 f.writelines([line + '\n' for line in unapplied])
519 f = file(self.__applied_file, 'w+')
520 f.writelines([line + '\n' for line in applied])
524 self.__set_current(None)
526 self.__set_current(applied[-1])
528 self.__end_stack_check()
530 def empty_patch(self, name):
531 """Returns True if the patch is empty
533 patch = Patch(name, self.__patch_dir)
534 bottom = patch.get_bottom()
535 top = patch.get_top()
539 elif git.Commit(top).get_tree() == git.Commit(bottom).get_tree():
544 def rename_patch(self, oldname, newname):
545 applied = self.get_applied()
546 unapplied = self.get_unapplied()
548 if newname in applied or newname in unapplied:
549 raise StackException, 'Patch "%s" already exists' % newname
551 if oldname in unapplied:
552 Patch(oldname, self.__patch_dir).rename(newname)
553 unapplied[unapplied.index(oldname)] = newname
555 f = file(self.__unapplied_file, 'w+')
556 f.writelines([line + '\n' for line in unapplied])
558 elif oldname in applied:
559 Patch(oldname, self.__patch_dir).rename(newname)
560 if oldname == self.get_current():
561 self.__set_current(newname)
563 applied[applied.index(oldname)] = newname
565 f = file(self.__applied_file, 'w+')
566 f.writelines([line + '\n' for line in applied])
569 raise StackException, 'Unknown patch "%s"' % oldname