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_refs(self):
146 self.__top_ref = self.__refs_base + '/' + self.__name
147 self.__log_ref = self.__top_ref + '.log'
149 def __init__(self, name, series_dir, refs_base):
150 self.__series_dir = series_dir
152 self._set_dir(os.path.join(self.__series_dir, self.__name))
153 self.__refs_base = refs_base
157 os.mkdir(self._dir())
158 self.create_empty_field('bottom')
159 self.create_empty_field('top')
162 for f in os.listdir(self._dir()):
163 os.remove(os.path.join(self._dir(), f))
164 os.rmdir(self._dir())
165 git.delete_ref(self.__top_ref)
166 if git.ref_exists(self.__log_ref):
167 git.delete_ref(self.__log_ref)
172 def rename(self, newname):
174 old_top_ref = self.__top_ref
175 old_log_ref = self.__log_ref
176 self.__name = newname
177 self._set_dir(os.path.join(self.__series_dir, self.__name))
180 git.rename_ref(old_top_ref, self.__top_ref)
181 if git.ref_exists(old_log_ref):
182 git.rename_ref(old_log_ref, self.__log_ref)
183 os.rename(olddir, self._dir())
185 def __update_top_ref(self, ref):
186 git.set_ref(self.__top_ref, ref)
188 def __update_log_ref(self, ref):
189 git.set_ref(self.__log_ref, 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 class PatchSet(StgitObject):
280 def __init__(self, name = None):
285 self.set_name (git.get_head_file())
286 self.__base_dir = basedir.get()
287 except git.GitException, ex:
288 raise StackException, 'GIT tree not initialised: %s' % ex
290 self._set_dir(os.path.join(self.__base_dir, 'patches', self.get_name()))
294 def set_name(self, name):
298 return self.__base_dir
301 """Return the head of the branch
303 crt = self.get_current_patch()
307 return self.get_base()
309 def get_protected(self):
310 return os.path.isfile(os.path.join(self._dir(), 'protected'))
313 protect_file = os.path.join(self._dir(), 'protected')
314 if not os.path.isfile(protect_file):
315 create_empty_file(protect_file)
318 protect_file = os.path.join(self._dir(), 'protected')
319 if os.path.isfile(protect_file):
320 os.remove(protect_file)
322 def __branch_descr(self):
323 return 'branch.%s.description' % self.get_name()
325 def get_description(self):
326 return config.get(self.__branch_descr()) or ''
328 def set_description(self, line):
330 config.set(self.__branch_descr(), line)
332 config.unset(self.__branch_descr())
334 def head_top_equal(self):
335 """Return true if the head and the top are the same
337 crt = self.get_current_patch()
339 # we don't care, no patches applied
341 return git.get_head() == crt.get_top()
343 def is_initialised(self):
344 """Checks if series is already initialised
346 return bool(config.get(self.format_version_key()))
349 class Series(PatchSet):
350 """Class including the operations on series
352 def __init__(self, name = None):
353 """Takes a series name as the parameter.
355 PatchSet.__init__(self, name)
357 # Update the branch to the latest format version if it is
358 # initialized, but don't touch it if it isn't.
359 self.update_to_current_format_version()
361 self.__refs_base = 'refs/patches/%s' % self.get_name()
363 self.__applied_file = os.path.join(self._dir(), 'applied')
364 self.__unapplied_file = os.path.join(self._dir(), 'unapplied')
365 self.__hidden_file = os.path.join(self._dir(), 'hidden')
367 # where this series keeps its patches
368 self.__patch_dir = os.path.join(self._dir(), 'patches')
371 self.__trash_dir = os.path.join(self._dir(), 'trash')
373 def format_version_key(self):
374 return 'branch.%s.stgit.stackformatversion' % self.get_name()
376 def update_to_current_format_version(self):
377 """Update a potentially older StGIT directory structure to the
378 latest version. Note: This function should depend as little as
379 possible on external functions that may change during a format
380 version bump, since it must remain able to process older formats."""
382 branch_dir = os.path.join(self._basedir(), 'patches', self.get_name())
383 def get_format_version():
384 """Return the integer format version number, or None if the
385 branch doesn't have any StGIT metadata at all, of any version."""
386 fv = config.get(self.format_version_key())
387 ofv = config.get('branch.%s.stgitformatversion' % self.get_name())
389 # Great, there's an explicitly recorded format version
390 # number, which means that the branch is initialized and
391 # of that exact version.
394 # Old name for the version info, upgrade it
395 config.set(self.format_version_key(), ofv)
396 config.unset('branch.%s.stgitformatversion' % self.get_name())
398 elif os.path.isdir(os.path.join(branch_dir, 'patches')):
399 # There's a .git/patches/<branch>/patches dirctory, which
400 # means this is an initialized version 1 branch.
402 elif os.path.isdir(branch_dir):
403 # There's a .git/patches/<branch> directory, which means
404 # this is an initialized version 0 branch.
407 # The branch doesn't seem to be initialized at all.
409 def set_format_version(v):
410 out.info('Upgraded branch %s to format version %d' % (self.get_name(), v))
411 config.set(self.format_version_key(), '%d' % v)
413 if not os.path.isdir(d):
416 if os.path.exists(f):
419 if git.ref_exists(ref):
423 if get_format_version() == 0:
424 mkdir(os.path.join(branch_dir, 'trash'))
425 patch_dir = os.path.join(branch_dir, 'patches')
427 refs_base = 'refs/patches/%s' % self.get_name()
428 for patch in (file(os.path.join(branch_dir, 'unapplied')).readlines()
429 + file(os.path.join(branch_dir, 'applied')).readlines()):
430 patch = patch.strip()
431 os.rename(os.path.join(branch_dir, patch),
432 os.path.join(patch_dir, patch))
433 Patch(patch, patch_dir, refs_base).update_top_ref()
434 set_format_version(1)
437 if get_format_version() == 1:
438 desc_file = os.path.join(branch_dir, 'description')
439 if os.path.isfile(desc_file):
440 desc = read_string(desc_file)
442 config.set('branch.%s.description' % self.get_name(), desc)
444 rm(os.path.join(branch_dir, 'current'))
445 rm_ref('refs/bases/%s' % self.get_name())
446 set_format_version(2)
448 # Make sure we're at the latest version.
449 if not get_format_version() in [None, FORMAT_VERSION]:
450 raise StackException('Branch %s is at format version %d, expected %d'
451 % (self.get_name(), get_format_version(), FORMAT_VERSION))
453 def __patch_name_valid(self, name):
454 """Raise an exception if the patch name is not valid.
456 if not name or re.search('[^\w.-]', name):
457 raise StackException, 'Invalid patch name: "%s"' % name
459 def get_patch(self, name):
460 """Return a Patch object for the given name
462 return Patch(name, self.__patch_dir, self.__refs_base)
464 def get_current_patch(self):
465 """Return a Patch object representing the topmost patch, or
466 None if there is no such patch."""
467 crt = self.get_current()
470 return self.get_patch(crt)
472 def get_current(self):
473 """Return the name of the topmost patch, or None if there is
476 applied = self.get_applied()
477 except StackException:
478 # No "applied" file: branch is not initialized.
483 # No patches applied.
486 def get_applied(self):
487 if not os.path.isfile(self.__applied_file):
488 raise StackException, 'Branch "%s" not initialised' % self.get_name()
489 return read_strings(self.__applied_file)
491 def get_unapplied(self):
492 if not os.path.isfile(self.__unapplied_file):
493 raise StackException, 'Branch "%s" not initialised' % self.get_name()
494 return read_strings(self.__unapplied_file)
496 def get_hidden(self):
497 if not os.path.isfile(self.__hidden_file):
499 return read_strings(self.__hidden_file)
502 # Return the parent of the bottommost patch, if there is one.
503 if os.path.isfile(self.__applied_file):
504 bottommost = file(self.__applied_file).readline().strip()
506 return self.get_patch(bottommost).get_bottom()
507 # No bottommost patch, so just return HEAD
508 return git.get_head()
510 def get_parent_remote(self):
511 value = config.get('branch.%s.remote' % self.get_name())
514 elif 'origin' in git.remotes_list():
515 out.note(('No parent remote declared for stack "%s",'
516 ' defaulting to "origin".' % self.get_name()),
517 ('Consider setting "branch.%s.remote" and'
518 ' "branch.%s.merge" with "git repo-config".'
519 % (self.get_name(), self.get_name())))
522 raise StackException, 'Cannot find a parent remote for "%s"' % self.get_name()
524 def __set_parent_remote(self, remote):
525 value = config.set('branch.%s.remote' % self.get_name(), remote)
527 def get_parent_branch(self):
528 value = config.get('branch.%s.stgit.parentbranch' % self.get_name())
531 elif git.rev_parse('heads/origin'):
532 out.note(('No parent branch declared for stack "%s",'
533 ' defaulting to "heads/origin".' % self.get_name()),
534 ('Consider setting "branch.%s.stgit.parentbranch"'
535 ' with "git repo-config".' % self.get_name()))
536 return 'heads/origin'
538 raise StackException, 'Cannot find a parent branch for "%s"' % self.get_name()
540 def __set_parent_branch(self, name):
541 if config.get('branch.%s.remote' % self.get_name()):
542 # Never set merge if remote is not set to avoid
543 # possibly-erroneous lookups into 'origin'
544 config.set('branch.%s.merge' % self.get_name(), name)
545 config.set('branch.%s.stgit.parentbranch' % self.get_name(), name)
547 def set_parent(self, remote, localbranch):
549 self.__set_parent_remote(remote)
550 self.__set_parent_branch(localbranch)
551 # We'll enforce this later
553 # raise StackException, 'Parent branch (%s) should be specified for %s' % localbranch, self.get_name()
555 def __patch_is_current(self, patch):
556 return patch.get_name() == self.get_current()
558 def patch_applied(self, name):
559 """Return true if the patch exists in the applied list
561 return name in self.get_applied()
563 def patch_unapplied(self, name):
564 """Return true if the patch exists in the unapplied list
566 return name in self.get_unapplied()
568 def patch_hidden(self, name):
569 """Return true if the patch is hidden.
571 return name in self.get_hidden()
573 def patch_exists(self, name):
574 """Return true if there is a patch with the given name, false
576 return self.patch_applied(name) or self.patch_unapplied(name) \
577 or self.patch_hidden(name)
579 def init(self, create_at=False, parent_remote=None, parent_branch=None):
580 """Initialises the stgit series
582 if self.is_initialised():
583 raise StackException, '%s already initialized' % self.get_name()
584 for d in [self._dir()]:
585 if os.path.exists(d):
586 raise StackException, '%s already exists' % d
588 if (create_at!=False):
589 git.create_branch(self.get_name(), create_at)
591 os.makedirs(self.__patch_dir)
593 self.set_parent(parent_remote, parent_branch)
595 self.create_empty_field('applied')
596 self.create_empty_field('unapplied')
597 self._set_field('orig-base', git.get_head())
599 config.set(self.format_version_key(), str(FORMAT_VERSION))
601 def rename(self, to_name):
604 to_stack = Series(to_name)
606 if to_stack.is_initialised():
607 raise StackException, '"%s" already exists' % to_stack.get_name()
609 patches = self.get_applied() + self.get_unapplied()
611 git.rename_branch(self.get_name(), to_name)
613 for patch in patches:
614 git.rename_ref('refs/patches/%s/%s' % (self.get_name(), patch),
615 'refs/patches/%s/%s' % (to_name, patch))
616 git.rename_ref('refs/patches/%s/%s.log' % (self.get_name(), patch),
617 'refs/patches/%s/%s.log' % (to_name, patch))
618 if os.path.isdir(self._dir()):
619 rename(os.path.join(self._basedir(), 'patches'),
620 self.get_name(), to_stack.get_name())
622 # Rename the config section
623 for k in ['branch.%s', 'branch.%s.stgit']:
624 config.rename_section(k % self.get_name(), k % to_name)
626 self.__init__(to_name)
628 def clone(self, target_series):
632 # allow cloning of branches not under StGIT control
633 base = self.get_base()
635 base = git.get_head()
636 Series(target_series).init(create_at = base)
637 new_series = Series(target_series)
639 # generate an artificial description file
640 new_series.set_description('clone of "%s"' % self.get_name())
642 # clone self's entire series as unapplied patches
644 # allow cloning of branches not under StGIT control
645 applied = self.get_applied()
646 unapplied = self.get_unapplied()
647 patches = applied + unapplied
650 patches = applied = unapplied = []
652 patch = self.get_patch(p)
653 newpatch = new_series.new_patch(p, message = patch.get_description(),
654 can_edit = False, unapplied = True,
655 bottom = patch.get_bottom(),
656 top = patch.get_top(),
657 author_name = patch.get_authname(),
658 author_email = patch.get_authemail(),
659 author_date = patch.get_authdate())
661 out.info('Setting log to %s' % patch.get_log())
662 newpatch.set_log(patch.get_log())
664 out.info('No log for %s' % p)
666 # fast forward the cloned series to self's top
667 new_series.forward_patches(applied)
669 # Clone parent informations
670 value = config.get('branch.%s.remote' % self.get_name())
672 config.set('branch.%s.remote' % target_series, value)
674 value = config.get('branch.%s.merge' % self.get_name())
676 config.set('branch.%s.merge' % target_series, value)
678 value = config.get('branch.%s.stgit.parentbranch' % self.get_name())
680 config.set('branch.%s.stgit.parentbranch' % target_series, value)
682 def delete(self, force = False):
683 """Deletes an stgit series
685 if self.is_initialised():
686 patches = self.get_unapplied() + self.get_applied()
687 if not force and patches:
688 raise StackException, \
689 'Cannot delete: the series still contains patches'
691 self.get_patch(p).delete()
693 # remove the trash directory if any
694 if os.path.exists(self.__trash_dir):
695 for fname in os.listdir(self.__trash_dir):
696 os.remove(os.path.join(self.__trash_dir, fname))
697 os.rmdir(self.__trash_dir)
699 # FIXME: find a way to get rid of those manual removals
700 # (move functionality to StgitObject ?)
701 if os.path.exists(self.__applied_file):
702 os.remove(self.__applied_file)
703 if os.path.exists(self.__unapplied_file):
704 os.remove(self.__unapplied_file)
705 if os.path.exists(self.__hidden_file):
706 os.remove(self.__hidden_file)
707 if os.path.exists(self._dir()+'/orig-base'):
708 os.remove(self._dir()+'/orig-base')
710 if not os.listdir(self.__patch_dir):
711 os.rmdir(self.__patch_dir)
713 out.warn('Patch directory %s is not empty' % self.__patch_dir)
716 os.removedirs(self._dir())
718 raise StackException('Series directory %s is not empty'
722 git.delete_branch(self.get_name())
724 out.warn('Could not delete branch "%s"' % self.get_name())
726 # Cleanup parent informations
727 # FIXME: should one day make use of git-config --section-remove,
728 # scheduled for 1.5.1
729 config.unset('branch.%s.remote' % self.get_name())
730 config.unset('branch.%s.merge' % self.get_name())
731 config.unset('branch.%s.stgit.parentbranch' % self.get_name())
732 config.unset(self.format_version_key())
734 def refresh_patch(self, files = None, message = None, edit = False,
737 author_name = None, author_email = None,
739 committer_name = None, committer_email = None,
740 backup = False, sign_str = None, log = 'refresh',
742 """Generates a new commit for the given patch
744 name = self.get_current()
746 raise StackException, 'No patches applied'
748 patch = self.get_patch(name)
750 descr = patch.get_description()
751 if not (message or descr):
757 if not message and edit:
758 descr = edit_file(self, descr.rstrip(), \
759 'Please edit the description for patch "%s" ' \
760 'above.' % name, show_patch)
763 author_name = patch.get_authname()
765 author_email = patch.get_authemail()
767 author_date = patch.get_authdate()
768 if not committer_name:
769 committer_name = patch.get_commname()
770 if not committer_email:
771 committer_email = patch.get_commemail()
774 descr = descr.rstrip()
775 if descr.find("\nSigned-off-by:") < 0 \
776 and descr.find("\nAcked-by:") < 0:
779 descr = '%s\n%s: %s <%s>\n' % (descr, sign_str,
780 committer_name, committer_email)
782 bottom = patch.get_bottom()
784 commit_id = git.commit(files = files,
785 message = descr, parents = [bottom],
786 cache_update = cache_update,
788 author_name = author_name,
789 author_email = author_email,
790 author_date = author_date,
791 committer_name = committer_name,
792 committer_email = committer_email)
794 patch.set_bottom(bottom, backup = backup)
795 patch.set_top(commit_id, backup = backup)
796 patch.set_description(descr)
797 patch.set_authname(author_name)
798 patch.set_authemail(author_email)
799 patch.set_authdate(author_date)
800 patch.set_commname(committer_name)
801 patch.set_commemail(committer_email)
804 self.log_patch(patch, log, notes)
808 def undo_refresh(self):
809 """Undo the patch boundaries changes caused by 'refresh'
811 name = self.get_current()
814 patch = self.get_patch(name)
815 old_bottom = patch.get_old_bottom()
816 old_top = patch.get_old_top()
818 # the bottom of the patch is not changed by refresh. If the
819 # old_bottom is different, there wasn't any previous 'refresh'
820 # command (probably only a 'push')
821 if old_bottom != patch.get_bottom() or old_top == patch.get_top():
822 raise StackException, 'No undo information available'
824 git.reset(tree_id = old_top, check_out = False)
825 if patch.restore_old_boundaries():
826 self.log_patch(patch, 'undo')
828 def new_patch(self, name, message = None, can_edit = True,
829 unapplied = False, show_patch = False,
830 top = None, bottom = None, commit = True,
831 author_name = None, author_email = None, author_date = None,
832 committer_name = None, committer_email = None,
833 before_existing = False):
834 """Creates a new patch
838 self.__patch_name_valid(name)
839 if self.patch_exists(name):
840 raise StackException, 'Patch "%s" already exists' % name
842 if not message and can_edit:
845 'Please enter the description for the patch above.',
850 head = git.get_head()
853 name = make_patch_name(descr, self.patch_exists)
855 patch = self.get_patch(name)
863 patch.set_bottom(bottom)
865 patch.set_description(descr)
866 patch.set_authname(author_name)
867 patch.set_authemail(author_email)
868 patch.set_authdate(author_date)
869 patch.set_commname(committer_name)
870 patch.set_commemail(committer_email)
873 insert_string(self.__applied_file, patch.get_name())
874 # no need to commit anything as the object is already
875 # present (mainly used by 'uncommit')
878 patches = [patch.get_name()] + self.get_unapplied()
879 write_strings(self.__unapplied_file, patches)
882 append_string(self.__applied_file, patch.get_name())
886 # create a commit for the patch (may be empty if top == bottom);
887 # only commit on top of the current branch
888 assert(unapplied or bottom == head)
889 top_commit = git.get_commit(top)
890 commit_id = git.commit(message = descr, parents = [bottom],
891 cache_update = False,
892 tree_id = top_commit.get_tree(),
893 allowempty = True, set_head = set_head,
894 author_name = author_name,
895 author_email = author_email,
896 author_date = author_date,
897 committer_name = committer_name,
898 committer_email = committer_email)
899 # set the patch top to the new commit
900 patch.set_top(commit_id)
902 self.log_patch(patch, 'new')
906 def delete_patch(self, name):
909 self.__patch_name_valid(name)
910 patch = self.get_patch(name)
912 if self.__patch_is_current(patch):
914 elif self.patch_applied(name):
915 raise StackException, 'Cannot remove an applied patch, "%s", ' \
916 'which is not current' % name
917 elif not name in self.get_unapplied():
918 raise StackException, 'Unknown patch "%s"' % name
920 # save the commit id to a trash file
921 write_string(os.path.join(self.__trash_dir, name), patch.get_top())
925 unapplied = self.get_unapplied()
926 unapplied.remove(name)
927 write_strings(self.__unapplied_file, unapplied)
929 def forward_patches(self, names):
930 """Try to fast-forward an array of patches.
932 On return, patches in names[0:returned_value] have been pushed on the
933 stack. Apply the rest with push_patch
935 unapplied = self.get_unapplied()
941 assert(name in unapplied)
943 patch = self.get_patch(name)
946 bottom = patch.get_bottom()
947 top = patch.get_top()
949 # top != bottom always since we have a commit for each patch
951 # reset the backup information. No logging since the
952 # patch hasn't changed
953 patch.set_bottom(head, backup = True)
954 patch.set_top(top, backup = True)
957 head_tree = git.get_commit(head).get_tree()
958 bottom_tree = git.get_commit(bottom).get_tree()
959 if head_tree == bottom_tree:
960 # We must just reparent this patch and create a new commit
962 descr = patch.get_description()
963 author_name = patch.get_authname()
964 author_email = patch.get_authemail()
965 author_date = patch.get_authdate()
966 committer_name = patch.get_commname()
967 committer_email = patch.get_commemail()
969 top_tree = git.get_commit(top).get_tree()
971 top = git.commit(message = descr, parents = [head],
972 cache_update = False,
975 author_name = author_name,
976 author_email = author_email,
977 author_date = author_date,
978 committer_name = committer_name,
979 committer_email = committer_email)
981 patch.set_bottom(head, backup = True)
982 patch.set_top(top, backup = True)
984 self.log_patch(patch, 'push(f)')
987 # stop the fast-forwarding, must do a real merge
991 unapplied.remove(name)
998 append_strings(self.__applied_file, names[0:forwarded])
999 write_strings(self.__unapplied_file, unapplied)
1003 def merged_patches(self, names):
1004 """Test which patches were merged upstream by reverse-applying
1005 them in reverse order. The function returns the list of
1006 patches detected to have been applied. The state of the tree
1007 is restored to the original one
1009 patches = [self.get_patch(name) for name in names]
1014 if git.apply_diff(p.get_top(), p.get_bottom()):
1015 merged.append(p.get_name())
1022 def push_patch(self, name, empty = False):
1023 """Pushes a patch on the stack
1025 unapplied = self.get_unapplied()
1026 assert(name in unapplied)
1028 patch = self.get_patch(name)
1030 head = git.get_head()
1031 bottom = patch.get_bottom()
1032 top = patch.get_top()
1037 # top != bottom always since we have a commit for each patch
1039 # just make an empty patch (top = bottom = HEAD). This
1040 # option is useful to allow undoing already merged
1041 # patches. The top is updated by refresh_patch since we
1042 # need an empty commit
1043 patch.set_bottom(head, backup = True)
1044 patch.set_top(head, backup = True)
1046 elif head == bottom:
1047 # reset the backup information. No need for logging
1048 patch.set_bottom(bottom, backup = True)
1049 patch.set_top(top, backup = True)
1053 # new patch needs to be refreshed.
1054 # The current patch is empty after merge.
1055 patch.set_bottom(head, backup = True)
1056 patch.set_top(head, backup = True)
1058 # Try the fast applying first. If this fails, fall back to the
1060 if not git.apply_diff(bottom, top):
1061 # if git.apply_diff() fails, the patch requires a diff3
1062 # merge and can be reported as modified
1065 # merge can fail but the patch needs to be pushed
1067 git.merge(bottom, head, top, recursive = True)
1068 except git.GitException, ex:
1069 out.error('The merge failed during "push".',
1070 'Use "refresh" after fixing the conflicts or'
1071 ' revert the operation with "push --undo".')
1073 append_string(self.__applied_file, name)
1075 unapplied.remove(name)
1076 write_strings(self.__unapplied_file, unapplied)
1078 # head == bottom case doesn't need to refresh the patch
1079 if empty or head != bottom:
1081 # if the merge was OK and no conflicts, just refresh the patch
1082 # The GIT cache was already updated by the merge operation
1087 self.refresh_patch(cache_update = False, log = log)
1089 # we store the correctly merged files only for
1090 # tracking the conflict history. Note that the
1091 # git.merge() operations should always leave the index
1092 # in a valid state (i.e. only stage 0 files)
1093 self.refresh_patch(cache_update = False, log = 'push(c)')
1094 raise StackException, str(ex)
1098 def undo_push(self):
1099 name = self.get_current()
1102 patch = self.get_patch(name)
1103 old_bottom = patch.get_old_bottom()
1104 old_top = patch.get_old_top()
1106 # the top of the patch is changed by a push operation only
1107 # together with the bottom (otherwise the top was probably
1108 # modified by 'refresh'). If they are both unchanged, there
1109 # was a fast forward
1110 if old_bottom == patch.get_bottom() and old_top != patch.get_top():
1111 raise StackException, 'No undo information available'
1114 self.pop_patch(name)
1115 ret = patch.restore_old_boundaries()
1117 self.log_patch(patch, 'undo')
1121 def pop_patch(self, name, keep = False):
1122 """Pops the top patch from the stack
1124 applied = self.get_applied()
1126 assert(name in applied)
1128 patch = self.get_patch(name)
1130 if git.get_head_file() == self.get_name():
1131 if keep and not git.apply_diff(git.get_head(), patch.get_bottom()):
1132 raise StackException(
1133 'Failed to pop patches while preserving the local changes')
1134 git.switch(patch.get_bottom(), keep)
1136 git.set_branch(self.get_name(), patch.get_bottom())
1138 # save the new applied list
1139 idx = applied.index(name) + 1
1141 popped = applied[:idx]
1143 unapplied = popped + self.get_unapplied()
1144 write_strings(self.__unapplied_file, unapplied)
1148 write_strings(self.__applied_file, applied)
1150 def empty_patch(self, name):
1151 """Returns True if the patch is empty
1153 self.__patch_name_valid(name)
1154 patch = self.get_patch(name)
1155 bottom = patch.get_bottom()
1156 top = patch.get_top()
1160 elif git.get_commit(top).get_tree() \
1161 == git.get_commit(bottom).get_tree():
1166 def rename_patch(self, oldname, newname):
1167 self.__patch_name_valid(newname)
1169 applied = self.get_applied()
1170 unapplied = self.get_unapplied()
1172 if oldname == newname:
1173 raise StackException, '"To" name and "from" name are the same'
1175 if newname in applied or newname in unapplied:
1176 raise StackException, 'Patch "%s" already exists' % newname
1178 if oldname in unapplied:
1179 self.get_patch(oldname).rename(newname)
1180 unapplied[unapplied.index(oldname)] = newname
1181 write_strings(self.__unapplied_file, unapplied)
1182 elif oldname in applied:
1183 self.get_patch(oldname).rename(newname)
1185 applied[applied.index(oldname)] = newname
1186 write_strings(self.__applied_file, applied)
1188 raise StackException, 'Unknown patch "%s"' % oldname
1190 def log_patch(self, patch, message, notes = None):
1191 """Generate a log commit for a patch
1193 top = git.get_commit(patch.get_top())
1194 old_log = patch.get_log()
1197 # replace the current log entry
1199 raise StackException, \
1200 'No log entry to annotate for patch "%s"' \
1203 log_commit = git.get_commit(old_log)
1204 msg = log_commit.get_log().split('\n')[0]
1205 log_parent = log_commit.get_parent()
1207 parents = [log_parent]
1211 # generate a new log entry
1213 msg = '%s\t%s' % (message, top.get_id_hash())
1220 msg += '\n\n' + notes
1222 log = git.commit(message = msg, parents = parents,
1223 cache_update = False, tree_id = top.get_tree(),
1227 def hide_patch(self, name):
1228 """Add the patch to the hidden list.
1230 unapplied = self.get_unapplied()
1231 if name not in unapplied:
1232 # keep the checking order for backward compatibility with
1233 # the old hidden patches functionality
1234 if self.patch_applied(name):
1235 raise StackException, 'Cannot hide applied patch "%s"' % name
1236 elif self.patch_hidden(name):
1237 raise StackException, 'Patch "%s" already hidden' % name
1239 raise StackException, 'Unknown patch "%s"' % name
1241 if not self.patch_hidden(name):
1242 # check needed for backward compatibility with the old
1243 # hidden patches functionality
1244 append_string(self.__hidden_file, name)
1246 unapplied.remove(name)
1247 write_strings(self.__unapplied_file, unapplied)
1249 def unhide_patch(self, name):
1250 """Remove the patch from the hidden list.
1252 hidden = self.get_hidden()
1253 if not name in hidden:
1254 if self.patch_applied(name) or self.patch_unapplied(name):
1255 raise StackException, 'Patch "%s" not hidden' % name
1257 raise StackException, 'Unknown patch "%s"' % name
1260 write_strings(self.__hidden_file, hidden)
1262 if not self.patch_applied(name) and not self.patch_unapplied(name):
1263 # check needed for backward compatibility with the old
1264 # hidden patches functionality
1265 append_string(self.__unapplied_file, name)