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 *
24 from stgit import git, basedir, templates
25 from stgit.config import config
26 from shutil import copyfile
29 # stack exception class
30 class StackException(Exception):
35 self.should_print = True
36 def __call__(self, x, until_test, prefix):
38 self.should_print = False
40 return x[0:len(prefix)] != prefix
46 __comment_prefix = 'STG:'
47 __patch_prefix = 'STG_PATCH:'
49 def __clean_comments(f):
50 """Removes lines marked for status in a commit file
54 # remove status-prefixed lines
57 patch_filter = FilterUntil()
58 until_test = lambda t: t == (__patch_prefix + '\n')
59 lines = [l for l in lines if patch_filter(l, until_test, __comment_prefix)]
61 # remove empty lines at the end
62 while len(lines) != 0 and lines[-1] == '\n':
65 f.seek(0); f.truncate()
68 def edit_file(series, line, comment, show_patch = True):
69 fname = '.stgitmsg.txt'
70 tmpl = templates.get_template('patchdescr.tmpl')
79 print >> f, __comment_prefix, comment
80 print >> f, __comment_prefix, \
81 'Lines prefixed with "%s" will be automatically removed.' \
83 print >> f, __comment_prefix, \
84 'Trailing empty lines will be automatically removed.'
87 print >> f, __patch_prefix
88 # series.get_patch(series.get_current()).get_top()
89 git.diff([], series.get_patch(series.get_current()).get_bottom(), None, f)
91 #Vim modeline must be near the end.
92 print >> f, __comment_prefix, 'vi: set textwidth=75 filetype=diff nobackup:'
113 """An object with stgit-like properties stored as files in a directory
115 def _set_dir(self, dir):
120 def create_empty_field(self, name):
121 create_empty_file(os.path.join(self.__dir, name))
123 def _get_field(self, name, multiline = False):
124 id_file = os.path.join(self.__dir, name)
125 if os.path.isfile(id_file):
126 line = read_string(id_file, multiline)
134 def _set_field(self, name, value, multiline = False):
135 fname = os.path.join(self.__dir, name)
136 if value and value != '':
137 write_string(fname, value, multiline)
138 elif os.path.isfile(fname):
142 class Patch(StgitObject):
143 """Basic patch implementation
145 def __init__(self, name, series_dir, refs_dir):
146 self.__series_dir = series_dir
148 self._set_dir(os.path.join(self.__series_dir, self.__name))
149 self.__refs_dir = refs_dir
150 self.__top_ref_file = os.path.join(self.__refs_dir, self.__name)
151 self.__log_ref_file = os.path.join(self.__refs_dir,
152 self.__name + '.log')
155 os.mkdir(self._dir())
156 self.create_empty_field('bottom')
157 self.create_empty_field('top')
160 for f in os.listdir(self._dir()):
161 os.remove(os.path.join(self._dir(), f))
162 os.rmdir(self._dir())
163 os.remove(self.__top_ref_file)
164 if os.path.exists(self.__log_ref_file):
165 os.remove(self.__log_ref_file)
170 def rename(self, newname):
172 old_top_ref_file = self.__top_ref_file
173 old_log_ref_file = self.__log_ref_file
174 self.__name = newname
175 self._set_dir(os.path.join(self.__series_dir, self.__name))
176 self.__top_ref_file = os.path.join(self.__refs_dir, self.__name)
177 self.__log_ref_file = os.path.join(self.__refs_dir,
178 self.__name + '.log')
180 os.rename(olddir, self._dir())
181 os.rename(old_top_ref_file, self.__top_ref_file)
182 if os.path.exists(old_log_ref_file):
183 os.rename(old_log_ref_file, self.__log_ref_file)
185 def __update_top_ref(self, ref):
186 write_string(self.__top_ref_file, ref)
188 def __update_log_ref(self, ref):
189 write_string(self.__log_ref_file, ref)
191 def update_top_ref(self):
194 self.__update_top_ref(top)
196 def get_old_bottom(self):
197 return self._get_field('bottom.old')
199 def get_bottom(self):
200 return self._get_field('bottom')
202 def set_bottom(self, value, backup = False):
204 curr = self._get_field('bottom')
205 self._set_field('bottom.old', curr)
206 self._set_field('bottom', value)
208 def get_old_top(self):
209 return self._get_field('top.old')
212 return self._get_field('top')
214 def set_top(self, value, backup = False):
216 curr = self._get_field('top')
217 self._set_field('top.old', curr)
218 self._set_field('top', value)
219 self.__update_top_ref(value)
221 def restore_old_boundaries(self):
222 bottom = self._get_field('bottom.old')
223 top = self._get_field('top.old')
226 self._set_field('bottom', bottom)
227 self._set_field('top', top)
228 self.__update_top_ref(top)
233 def get_description(self):
234 return self._get_field('description', True)
236 def set_description(self, line):
237 self._set_field('description', line, True)
239 def get_authname(self):
240 return self._get_field('authname')
242 def set_authname(self, name):
243 self._set_field('authname', name or git.author().name)
245 def get_authemail(self):
246 return self._get_field('authemail')
248 def set_authemail(self, email):
249 self._set_field('authemail', email or git.author().email)
251 def get_authdate(self):
252 return self._get_field('authdate')
254 def set_authdate(self, date):
255 self._set_field('authdate', date or git.author().date)
257 def get_commname(self):
258 return self._get_field('commname')
260 def set_commname(self, name):
261 self._set_field('commname', name or git.committer().name)
263 def get_commemail(self):
264 return self._get_field('commemail')
266 def set_commemail(self, email):
267 self._set_field('commemail', email or git.committer().email)
270 return self._get_field('log')
272 def set_log(self, value, backup = False):
273 self._set_field('log', value)
274 self.__update_log_ref(value)
276 # The current StGIT metadata format version.
279 def format_version_key(branch):
280 return 'branch.%s.stgitformatversion' % branch
282 def update_to_current_format_version(branch, git_dir):
283 """Update a potentially older StGIT directory structure to the
284 latest version. Note: This function should depend as little as
285 possible on external functions that may change during a format
286 version bump, since it must remain able to process older formats."""
288 branch_dir = os.path.join(git_dir, 'patches', branch)
289 def get_format_version():
290 """Return the integer format version number, or None if the
291 branch doesn't have any StGIT metadata at all, of any version."""
292 fv = config.get(format_version_key(branch))
294 # Great, there's an explicitly recorded format version
295 # number, which means that the branch is initialized and
296 # of that exact version.
298 elif os.path.isdir(os.path.join(branch_dir, 'patches')):
299 # There's a .git/patches/<branch>/patches dirctory, which
300 # means this is an initialized version 1 branch.
302 elif os.path.isdir(branch_dir):
303 # There's a .git/patches/<branch> directory, which means
304 # this is an initialized version 0 branch.
307 # The branch doesn't seem to be initialized at all.
309 def set_format_version(v):
310 out.info('Upgraded branch %s to format version %d' % (branch, v))
311 config.set(format_version_key(branch), '%d' % v)
313 if not os.path.isdir(d):
316 if os.path.exists(f):
320 if get_format_version() == 0:
321 mkdir(os.path.join(branch_dir, 'trash'))
322 patch_dir = os.path.join(branch_dir, 'patches')
324 refs_dir = os.path.join(git_dir, 'refs', 'patches', branch)
326 for patch in (file(os.path.join(branch_dir, 'unapplied')).readlines()
327 + file(os.path.join(branch_dir, 'applied')).readlines()):
328 patch = patch.strip()
329 os.rename(os.path.join(branch_dir, patch),
330 os.path.join(patch_dir, patch))
331 Patch(patch, patch_dir, refs_dir).update_top_ref()
332 set_format_version(1)
335 if get_format_version() == 1:
336 desc_file = os.path.join(branch_dir, 'description')
337 if os.path.isfile(desc_file):
338 desc = read_string(desc_file)
340 config.set('branch.%s.description' % branch, desc)
342 rm(os.path.join(branch_dir, 'current'))
343 rm(os.path.join(git_dir, 'refs', 'bases', branch))
344 set_format_version(2)
346 # Make sure we're at the latest version.
347 if not get_format_version() in [None, FORMAT_VERSION]:
348 raise StackException('Branch %s is at format version %d, expected %d'
349 % (branch, get_format_version(), FORMAT_VERSION))
351 class Series(StgitObject):
352 """Class including the operations on series
354 def __init__(self, name = None):
355 """Takes a series name as the parameter.
361 self.__name = git.get_head_file()
362 self.__base_dir = basedir.get()
363 except git.GitException, ex:
364 raise StackException, 'GIT tree not initialised: %s' % ex
366 self._set_dir(os.path.join(self.__base_dir, 'patches', self.__name))
368 # Update the branch to the latest format version if it is
369 # initialized, but don't touch it if it isn't.
370 update_to_current_format_version(self.__name, self.__base_dir)
372 self.__refs_dir = os.path.join(self.__base_dir, 'refs', 'patches',
375 self.__applied_file = os.path.join(self._dir(), 'applied')
376 self.__unapplied_file = os.path.join(self._dir(), 'unapplied')
377 self.__hidden_file = os.path.join(self._dir(), 'hidden')
379 # where this series keeps its patches
380 self.__patch_dir = os.path.join(self._dir(), 'patches')
383 self.__trash_dir = os.path.join(self._dir(), 'trash')
385 def __patch_name_valid(self, name):
386 """Raise an exception if the patch name is not valid.
388 if not name or re.search('[^\w.-]', name):
389 raise StackException, 'Invalid patch name: "%s"' % name
391 def get_branch(self):
392 """Return the branch name for the Series object
396 def get_patch(self, name):
397 """Return a Patch object for the given name
399 return Patch(name, self.__patch_dir, self.__refs_dir)
401 def get_current_patch(self):
402 """Return a Patch object representing the topmost patch, or
403 None if there is no such patch."""
404 crt = self.get_current()
407 return Patch(crt, self.__patch_dir, self.__refs_dir)
409 def get_current(self):
410 """Return the name of the topmost patch, or None if there is
413 applied = self.get_applied()
414 except StackException:
415 # No "applied" file: branch is not initialized.
420 # No patches applied.
423 def get_applied(self):
424 if not os.path.isfile(self.__applied_file):
425 raise StackException, 'Branch "%s" not initialised' % self.__name
426 return read_strings(self.__applied_file)
428 def get_unapplied(self):
429 if not os.path.isfile(self.__unapplied_file):
430 raise StackException, 'Branch "%s" not initialised' % self.__name
431 return read_strings(self.__unapplied_file)
433 def get_hidden(self):
434 if not os.path.isfile(self.__hidden_file):
436 return read_strings(self.__hidden_file)
439 # Return the parent of the bottommost patch, if there is one.
440 if os.path.isfile(self.__applied_file):
441 bottommost = file(self.__applied_file).readline().strip()
443 return self.get_patch(bottommost).get_bottom()
444 # No bottommost patch, so just return HEAD
445 return git.get_head()
448 """Return the head of the branch
450 crt = self.get_current_patch()
454 return self.get_base()
456 def get_protected(self):
457 return os.path.isfile(os.path.join(self._dir(), 'protected'))
460 protect_file = os.path.join(self._dir(), 'protected')
461 if not os.path.isfile(protect_file):
462 create_empty_file(protect_file)
465 protect_file = os.path.join(self._dir(), 'protected')
466 if os.path.isfile(protect_file):
467 os.remove(protect_file)
469 def __branch_descr(self):
470 return 'branch.%s.description' % self.get_branch()
472 def get_description(self):
473 return config.get(self.__branch_descr()) or ''
475 def set_description(self, line):
477 config.set(self.__branch_descr(), line)
479 config.unset(self.__branch_descr())
481 def get_parent_remote(self):
482 value = config.get('branch.%s.remote' % self.__name)
485 elif 'origin' in git.remotes_list():
486 out.note(('No parent remote declared for stack "%s",'
487 ' defaulting to "origin".' % self.__name),
488 ('Consider setting "branch.%s.remote" and'
489 ' "branch.%s.merge" with "git repo-config".'
490 % (self.__name, self.__name)))
493 raise StackException, 'Cannot find a parent remote for "%s"' % self.__name
495 def __set_parent_remote(self, remote):
496 value = config.set('branch.%s.remote' % self.__name, remote)
498 def get_parent_branch(self):
499 value = config.get('branch.%s.stgit.parentbranch' % self.__name)
502 elif git.rev_parse('heads/origin'):
503 out.note(('No parent branch declared for stack "%s",'
504 ' defaulting to "heads/origin".' % self.__name),
505 ('Consider setting "branch.%s.stgit.parentbranch"'
506 ' with "git repo-config".' % self.__name))
507 return 'heads/origin'
509 raise StackException, 'Cannot find a parent branch for "%s"' % self.__name
511 def __set_parent_branch(self, name):
512 if config.get('branch.%s.remote' % self.__name):
513 # Never set merge if remote is not set to avoid
514 # possibly-erroneous lookups into 'origin'
515 config.set('branch.%s.merge' % self.__name, name)
516 config.set('branch.%s.stgit.parentbranch' % self.__name, name)
518 def set_parent(self, remote, localbranch):
520 self.__set_parent_remote(remote)
521 self.__set_parent_branch(localbranch)
522 # We'll enforce this later
524 # raise StackException, 'Parent branch (%s) should be specified for %s' % localbranch, self.__name
526 def __patch_is_current(self, patch):
527 return patch.get_name() == self.get_current()
529 def patch_applied(self, name):
530 """Return true if the patch exists in the applied list
532 return name in self.get_applied()
534 def patch_unapplied(self, name):
535 """Return true if the patch exists in the unapplied list
537 return name in self.get_unapplied()
539 def patch_hidden(self, name):
540 """Return true if the patch is hidden.
542 return name in self.get_hidden()
544 def patch_exists(self, name):
545 """Return true if there is a patch with the given name, false
547 return self.patch_applied(name) or self.patch_unapplied(name) \
548 or self.patch_hidden(name)
550 def head_top_equal(self):
551 """Return true if the head and the top are the same
553 crt = self.get_current_patch()
555 # we don't care, no patches applied
557 return git.get_head() == crt.get_top()
559 def is_initialised(self):
560 """Checks if series is already initialised
562 return bool(config.get(format_version_key(self.get_branch())))
564 def init(self, create_at=False, parent_remote=None, parent_branch=None):
565 """Initialises the stgit series
567 if self.is_initialised():
568 raise StackException, '%s already initialized' % self.get_branch()
569 for d in [self._dir(), self.__refs_dir]:
570 if os.path.exists(d):
571 raise StackException, '%s already exists' % d
573 if (create_at!=False):
574 git.create_branch(self.__name, create_at)
576 os.makedirs(self.__patch_dir)
578 self.set_parent(parent_remote, parent_branch)
580 self.create_empty_field('applied')
581 self.create_empty_field('unapplied')
582 os.makedirs(self.__refs_dir)
584 config.set(format_version_key(self.get_branch()), str(FORMAT_VERSION))
586 def rename(self, to_name):
589 to_stack = Series(to_name)
591 if to_stack.is_initialised():
592 raise StackException, '"%s" already exists' % to_stack.get_branch()
594 git.rename_branch(self.__name, to_name)
596 if os.path.isdir(self._dir()):
597 rename(os.path.join(self.__base_dir, 'patches'),
598 self.__name, to_stack.__name)
599 if os.path.exists(self.__refs_dir):
600 rename(os.path.join(self.__base_dir, 'refs', 'patches'),
601 self.__name, to_stack.__name)
603 # Rename the config section
604 config.rename_section("branch.%s" % self.__name,
605 "branch.%s" % to_name)
607 self.__init__(to_name)
609 def clone(self, target_series):
613 # allow cloning of branches not under StGIT control
614 base = self.get_base()
616 base = git.get_head()
617 Series(target_series).init(create_at = base)
618 new_series = Series(target_series)
620 # generate an artificial description file
621 new_series.set_description('clone of "%s"' % self.__name)
623 # clone self's entire series as unapplied patches
625 # allow cloning of branches not under StGIT control
626 applied = self.get_applied()
627 unapplied = self.get_unapplied()
628 patches = applied + unapplied
631 patches = applied = unapplied = []
633 patch = self.get_patch(p)
634 newpatch = new_series.new_patch(p, message = patch.get_description(),
635 can_edit = False, unapplied = True,
636 bottom = patch.get_bottom(),
637 top = patch.get_top(),
638 author_name = patch.get_authname(),
639 author_email = patch.get_authemail(),
640 author_date = patch.get_authdate())
642 out.info('Setting log to %s' % patch.get_log())
643 newpatch.set_log(patch.get_log())
645 out.info('No log for %s' % p)
647 # fast forward the cloned series to self's top
648 new_series.forward_patches(applied)
650 # Clone parent informations
651 value = config.get('branch.%s.remote' % self.__name)
653 config.set('branch.%s.remote' % target_series, value)
655 value = config.get('branch.%s.merge' % self.__name)
657 config.set('branch.%s.merge' % target_series, value)
659 value = config.get('branch.%s.stgit.parentbranch' % self.__name)
661 config.set('branch.%s.stgit.parentbranch' % target_series, value)
663 def delete(self, force = False):
664 """Deletes an stgit series
666 if self.is_initialised():
667 patches = self.get_unapplied() + self.get_applied()
668 if not force and patches:
669 raise StackException, \
670 'Cannot delete: the series still contains patches'
672 Patch(p, self.__patch_dir, self.__refs_dir).delete()
674 # remove the trash directory if any
675 if os.path.exists(self.__trash_dir):
676 for fname in os.listdir(self.__trash_dir):
677 os.remove(os.path.join(self.__trash_dir, fname))
678 os.rmdir(self.__trash_dir)
680 # FIXME: find a way to get rid of those manual removals
681 # (move functionality to StgitObject ?)
682 if os.path.exists(self.__applied_file):
683 os.remove(self.__applied_file)
684 if os.path.exists(self.__unapplied_file):
685 os.remove(self.__unapplied_file)
686 if os.path.exists(self.__hidden_file):
687 os.remove(self.__hidden_file)
688 if os.path.exists(self._dir()+'/orig-base'):
689 os.remove(self._dir()+'/orig-base')
691 if not os.listdir(self.__patch_dir):
692 os.rmdir(self.__patch_dir)
694 out.warn('Patch directory %s is not empty' % self.__patch_dir)
697 os.removedirs(self._dir())
699 raise StackException('Series directory %s is not empty'
703 os.removedirs(self.__refs_dir)
705 out.warn('Refs directory %s is not empty' % self.__refs_dir)
707 # Cleanup parent informations
708 # FIXME: should one day make use of git-config --section-remove,
709 # scheduled for 1.5.1
710 config.unset('branch.%s.remote' % self.__name)
711 config.unset('branch.%s.merge' % self.__name)
712 config.unset('branch.%s.stgit.parentbranch' % self.__name)
713 config.unset('branch.%s.stgitformatversion' % self.__name)
715 def refresh_patch(self, files = None, message = None, edit = False,
718 author_name = None, author_email = None,
720 committer_name = None, committer_email = None,
721 backup = False, sign_str = None, log = 'refresh'):
722 """Generates a new commit for the given patch
724 name = self.get_current()
726 raise StackException, 'No patches applied'
728 patch = Patch(name, self.__patch_dir, self.__refs_dir)
730 descr = patch.get_description()
731 if not (message or descr):
737 if not message and edit:
738 descr = edit_file(self, descr.rstrip(), \
739 'Please edit the description for patch "%s" ' \
740 'above.' % name, show_patch)
743 author_name = patch.get_authname()
745 author_email = patch.get_authemail()
747 author_date = patch.get_authdate()
748 if not committer_name:
749 committer_name = patch.get_commname()
750 if not committer_email:
751 committer_email = patch.get_commemail()
754 descr = descr.rstrip()
755 if descr.find("\nSigned-off-by:") < 0 \
756 and descr.find("\nAcked-by:") < 0:
759 descr = '%s\n%s: %s <%s>\n' % (descr, sign_str,
760 committer_name, committer_email)
762 bottom = patch.get_bottom()
764 commit_id = git.commit(files = files,
765 message = descr, parents = [bottom],
766 cache_update = cache_update,
768 author_name = author_name,
769 author_email = author_email,
770 author_date = author_date,
771 committer_name = committer_name,
772 committer_email = committer_email)
774 patch.set_bottom(bottom, backup = backup)
775 patch.set_top(commit_id, backup = backup)
776 patch.set_description(descr)
777 patch.set_authname(author_name)
778 patch.set_authemail(author_email)
779 patch.set_authdate(author_date)
780 patch.set_commname(committer_name)
781 patch.set_commemail(committer_email)
784 self.log_patch(patch, log)
788 def undo_refresh(self):
789 """Undo the patch boundaries changes caused by 'refresh'
791 name = self.get_current()
794 patch = Patch(name, self.__patch_dir, self.__refs_dir)
795 old_bottom = patch.get_old_bottom()
796 old_top = patch.get_old_top()
798 # the bottom of the patch is not changed by refresh. If the
799 # old_bottom is different, there wasn't any previous 'refresh'
800 # command (probably only a 'push')
801 if old_bottom != patch.get_bottom() or old_top == patch.get_top():
802 raise StackException, 'No undo information available'
804 git.reset(tree_id = old_top, check_out = False)
805 if patch.restore_old_boundaries():
806 self.log_patch(patch, 'undo')
808 def new_patch(self, name, message = None, can_edit = True,
809 unapplied = False, show_patch = False,
810 top = None, bottom = None,
811 author_name = None, author_email = None, author_date = None,
812 committer_name = None, committer_email = None,
813 before_existing = False, refresh = True):
814 """Creates a new patch
818 self.__patch_name_valid(name)
819 if self.patch_exists(name):
820 raise StackException, 'Patch "%s" already exists' % name
822 if not message and can_edit:
825 'Please enter the description for the patch above.',
830 head = git.get_head()
833 name = make_patch_name(descr, self.patch_exists)
835 patch = Patch(name, self.__patch_dir, self.__refs_dir)
839 patch.set_bottom(bottom)
841 patch.set_bottom(head)
847 patch.set_description(descr)
848 patch.set_authname(author_name)
849 patch.set_authemail(author_email)
850 patch.set_authdate(author_date)
851 patch.set_commname(committer_name)
852 patch.set_commemail(committer_email)
855 self.log_patch(patch, 'new')
857 patches = [patch.get_name()] + self.get_unapplied()
858 write_strings(self.__unapplied_file, patches)
859 elif before_existing:
860 self.log_patch(patch, 'new')
862 insert_string(self.__applied_file, patch.get_name())
864 append_string(self.__applied_file, patch.get_name())
866 self.refresh_patch(cache_update = False, log = 'new')
870 def delete_patch(self, name):
873 self.__patch_name_valid(name)
874 patch = Patch(name, self.__patch_dir, self.__refs_dir)
876 if self.__patch_is_current(patch):
878 elif self.patch_applied(name):
879 raise StackException, 'Cannot remove an applied patch, "%s", ' \
880 'which is not current' % name
881 elif not name in self.get_unapplied():
882 raise StackException, 'Unknown patch "%s"' % name
884 # save the commit id to a trash file
885 write_string(os.path.join(self.__trash_dir, name), patch.get_top())
889 unapplied = self.get_unapplied()
890 unapplied.remove(name)
891 write_strings(self.__unapplied_file, unapplied)
893 def forward_patches(self, names):
894 """Try to fast-forward an array of patches.
896 On return, patches in names[0:returned_value] have been pushed on the
897 stack. Apply the rest with push_patch
899 unapplied = self.get_unapplied()
905 assert(name in unapplied)
907 patch = Patch(name, self.__patch_dir, self.__refs_dir)
910 bottom = patch.get_bottom()
911 top = patch.get_top()
913 # top != bottom always since we have a commit for each patch
915 # reset the backup information. No logging since the
916 # patch hasn't changed
917 patch.set_bottom(head, backup = True)
918 patch.set_top(top, backup = True)
921 head_tree = git.get_commit(head).get_tree()
922 bottom_tree = git.get_commit(bottom).get_tree()
923 if head_tree == bottom_tree:
924 # We must just reparent this patch and create a new commit
926 descr = patch.get_description()
927 author_name = patch.get_authname()
928 author_email = patch.get_authemail()
929 author_date = patch.get_authdate()
930 committer_name = patch.get_commname()
931 committer_email = patch.get_commemail()
933 top_tree = git.get_commit(top).get_tree()
935 top = git.commit(message = descr, parents = [head],
936 cache_update = False,
939 author_name = author_name,
940 author_email = author_email,
941 author_date = author_date,
942 committer_name = committer_name,
943 committer_email = committer_email)
945 patch.set_bottom(head, backup = True)
946 patch.set_top(top, backup = True)
948 self.log_patch(patch, 'push(f)')
951 # stop the fast-forwarding, must do a real merge
955 unapplied.remove(name)
962 append_strings(self.__applied_file, names[0:forwarded])
963 write_strings(self.__unapplied_file, unapplied)
967 def merged_patches(self, names):
968 """Test which patches were merged upstream by reverse-applying
969 them in reverse order. The function returns the list of
970 patches detected to have been applied. The state of the tree
971 is restored to the original one
973 patches = [Patch(name, self.__patch_dir, self.__refs_dir)
979 if git.apply_diff(p.get_top(), p.get_bottom()):
980 merged.append(p.get_name())
987 def push_patch(self, name, empty = False):
988 """Pushes a patch on the stack
990 unapplied = self.get_unapplied()
991 assert(name in unapplied)
993 patch = Patch(name, self.__patch_dir, self.__refs_dir)
995 head = git.get_head()
996 bottom = patch.get_bottom()
997 top = patch.get_top()
1002 # top != bottom always since we have a commit for each patch
1004 # just make an empty patch (top = bottom = HEAD). This
1005 # option is useful to allow undoing already merged
1006 # patches. The top is updated by refresh_patch since we
1007 # need an empty commit
1008 patch.set_bottom(head, backup = True)
1009 patch.set_top(head, backup = True)
1011 elif head == bottom:
1012 # reset the backup information. No need for logging
1013 patch.set_bottom(bottom, backup = True)
1014 patch.set_top(top, backup = True)
1018 # new patch needs to be refreshed.
1019 # The current patch is empty after merge.
1020 patch.set_bottom(head, backup = True)
1021 patch.set_top(head, backup = True)
1023 # Try the fast applying first. If this fails, fall back to the
1025 if not git.apply_diff(bottom, top):
1026 # if git.apply_diff() fails, the patch requires a diff3
1027 # merge and can be reported as modified
1030 # merge can fail but the patch needs to be pushed
1032 git.merge(bottom, head, top, recursive = True)
1033 except git.GitException, ex:
1034 out.error('The merge failed during "push".',
1035 'Use "refresh" after fixing the conflicts or'
1036 ' revert the operation with "push --undo".')
1038 append_string(self.__applied_file, name)
1040 unapplied.remove(name)
1041 write_strings(self.__unapplied_file, unapplied)
1043 # head == bottom case doesn't need to refresh the patch
1044 if empty or head != bottom:
1046 # if the merge was OK and no conflicts, just refresh the patch
1047 # The GIT cache was already updated by the merge operation
1052 self.refresh_patch(cache_update = False, log = log)
1054 # we store the correctly merged files only for
1055 # tracking the conflict history. Note that the
1056 # git.merge() operations should always leave the index
1057 # in a valid state (i.e. only stage 0 files)
1058 self.refresh_patch(cache_update = False, log = 'push(c)')
1059 raise StackException, str(ex)
1063 def undo_push(self):
1064 name = self.get_current()
1067 patch = Patch(name, self.__patch_dir, self.__refs_dir)
1068 old_bottom = patch.get_old_bottom()
1069 old_top = patch.get_old_top()
1071 # the top of the patch is changed by a push operation only
1072 # together with the bottom (otherwise the top was probably
1073 # modified by 'refresh'). If they are both unchanged, there
1074 # was a fast forward
1075 if old_bottom == patch.get_bottom() and old_top != patch.get_top():
1076 raise StackException, 'No undo information available'
1079 self.pop_patch(name)
1080 ret = patch.restore_old_boundaries()
1082 self.log_patch(patch, 'undo')
1086 def pop_patch(self, name, keep = False):
1087 """Pops the top patch from the stack
1089 applied = self.get_applied()
1091 assert(name in applied)
1093 patch = Patch(name, self.__patch_dir, self.__refs_dir)
1095 if git.get_head_file() == self.get_branch():
1096 if keep and not git.apply_diff(git.get_head(), patch.get_bottom()):
1097 raise StackException(
1098 'Failed to pop patches while preserving the local changes')
1099 git.switch(patch.get_bottom(), keep)
1101 git.set_branch(self.get_branch(), patch.get_bottom())
1103 # save the new applied list
1104 idx = applied.index(name) + 1
1106 popped = applied[:idx]
1108 unapplied = popped + self.get_unapplied()
1109 write_strings(self.__unapplied_file, unapplied)
1113 write_strings(self.__applied_file, applied)
1115 def empty_patch(self, name):
1116 """Returns True if the patch is empty
1118 self.__patch_name_valid(name)
1119 patch = Patch(name, self.__patch_dir, self.__refs_dir)
1120 bottom = patch.get_bottom()
1121 top = patch.get_top()
1125 elif git.get_commit(top).get_tree() \
1126 == git.get_commit(bottom).get_tree():
1131 def rename_patch(self, oldname, newname):
1132 self.__patch_name_valid(newname)
1134 applied = self.get_applied()
1135 unapplied = self.get_unapplied()
1137 if oldname == newname:
1138 raise StackException, '"To" name and "from" name are the same'
1140 if newname in applied or newname in unapplied:
1141 raise StackException, 'Patch "%s" already exists' % newname
1143 if oldname in unapplied:
1144 Patch(oldname, self.__patch_dir, self.__refs_dir).rename(newname)
1145 unapplied[unapplied.index(oldname)] = newname
1146 write_strings(self.__unapplied_file, unapplied)
1147 elif oldname in applied:
1148 Patch(oldname, self.__patch_dir, self.__refs_dir).rename(newname)
1150 applied[applied.index(oldname)] = newname
1151 write_strings(self.__applied_file, applied)
1153 raise StackException, 'Unknown patch "%s"' % oldname
1155 def log_patch(self, patch, message):
1156 """Generate a log commit for a patch
1158 top = git.get_commit(patch.get_top())
1159 msg = '%s\t%s' % (message, top.get_id_hash())
1161 old_log = patch.get_log()
1167 log = git.commit(message = msg, parents = parents,
1168 cache_update = False, tree_id = top.get_tree(),
1172 def hide_patch(self, name):
1173 """Add the patch to the hidden list.
1175 unapplied = self.get_unapplied()
1176 if name not in unapplied:
1177 # keep the checking order for backward compatibility with
1178 # the old hidden patches functionality
1179 if self.patch_applied(name):
1180 raise StackException, 'Cannot hide applied patch "%s"' % name
1181 elif self.patch_hidden(name):
1182 raise StackException, 'Patch "%s" already hidden' % name
1184 raise StackException, 'Unknown patch "%s"' % name
1186 if not self.patch_hidden(name):
1187 # check needed for backward compatibility with the old
1188 # hidden patches functionality
1189 append_string(self.__hidden_file, name)
1191 unapplied.remove(name)
1192 write_strings(self.__unapplied_file, unapplied)
1194 def unhide_patch(self, name):
1195 """Remove the patch from the hidden list.
1197 hidden = self.get_hidden()
1198 if not name in hidden:
1199 if self.patch_applied(name) or self.patch_unapplied(name):
1200 raise StackException, 'Patch "%s" not hidden' % name
1202 raise StackException, 'Unknown patch "%s"' % name
1205 write_strings(self.__hidden_file, hidden)
1207 if not self.patch_applied(name) and not self.patch_unapplied(name):
1208 # check needed for backward compatibility with the old
1209 # hidden patches functionality
1210 append_string(self.__unapplied_file, name)