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" ' \
388 head = git.get_head()
390 self.__begin_stack_check()
392 patch = Patch(name, self.__patch_dir)
394 patch.set_bottom(head)
396 patch.set_description(descr)
397 patch.set_authname(author_name)
398 patch.set_authemail(author_email)
399 patch.set_authdate(author_date)
400 patch.set_commname(committer_name)
401 patch.set_commemail(committer_email)
403 append_string(self.__applied_file, patch.get_name())
404 self.__set_current(name)
406 def delete_patch(self, name):
409 patch = Patch(name, self.__patch_dir)
411 if self.__patch_is_current(patch):
413 elif self.__patch_applied(name):
414 raise StackException, 'Cannot remove an applied patch, "%s", ' \
415 'which is not current' % name
416 elif not name in self.get_unapplied():
417 raise StackException, 'Unknown patch "%s"' % name
421 unapplied = self.get_unapplied()
422 unapplied.remove(name)
423 f = file(self.__unapplied_file, 'w+')
424 f.writelines([line + '\n' for line in unapplied])
427 def push_patch(self, name):
428 """Pushes a patch on the stack
430 unapplied = self.get_unapplied()
431 assert(name in unapplied)
433 self.__begin_stack_check()
435 patch = Patch(name, self.__patch_dir)
437 head = git.get_head()
438 bottom = patch.get_bottom()
439 top = patch.get_top()
443 # top != bottom always since we have a commit for each patch
445 # reset the backup information
446 patch.set_bottom(bottom, backup = True)
447 patch.set_top(top, backup = True)
451 # new patch needs to be refreshed.
452 # The current patch is empty after merge.
453 patch.set_bottom(head, backup = True)
454 patch.set_top(head, backup = True)
455 # merge/refresh can fail but the patch needs to be pushed
457 git.merge(bottom, head, top)
458 except git.GitException, ex:
459 print >> sys.stderr, \
460 'The merge failed during "push". ' \
461 'Use "refresh" after fixing the conflicts'
464 append_string(self.__applied_file, name)
466 unapplied.remove(name)
467 f = file(self.__unapplied_file, 'w+')
468 f.writelines([line + '\n' for line in unapplied])
471 self.__set_current(name)
474 # if the merge was OK and no conflicts, just refresh the patch
477 raise StackException, str(ex)
480 name = self.get_current()
483 patch = Patch(name, self.__patch_dir)
485 patch.restore_old_boundaries()
487 def pop_patch(self, name):
488 """Pops the top patch from the stack
490 applied = self.get_applied()
492 assert(name in applied)
494 patch = Patch(name, self.__patch_dir)
496 git.switch(patch.get_bottom())
498 # save the new applied list
499 idx = applied.index(name) + 1
501 popped = applied[:idx]
503 unapplied = popped + self.get_unapplied()
505 f = file(self.__unapplied_file, 'w+')
506 f.writelines([line + '\n' for line in unapplied])
512 f = file(self.__applied_file, 'w+')
513 f.writelines([line + '\n' for line in applied])
517 self.__set_current(None)
519 self.__set_current(applied[-1])
521 self.__end_stack_check()
523 def empty_patch(self, name):
524 """Returns True if the patch is empty
526 patch = Patch(name, self.__patch_dir)
527 bottom = patch.get_bottom()
528 top = patch.get_top()
532 elif git.Commit(top).get_tree() == git.Commit(bottom).get_tree():