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
22 from email
.Utils
import formatdate
24 from stgit
.exception
import *
25 from stgit
.utils
import *
26 from stgit
.out
import *
27 from stgit
.run
import *
28 from stgit
import git
, basedir
, templates
29 from stgit
.config
import config
30 from shutil
import copyfile
33 # stack exception class
34 class StackException(StgException
):
39 self
.should_print
= True
40 def __call__(self
, x
, until_test
, prefix
):
42 self
.should_print
= False
44 return x
[0:len(prefix
)] != prefix
50 __comment_prefix
= 'STG:'
51 __patch_prefix
= 'STG_PATCH:'
53 def __clean_comments(f
):
54 """Removes lines marked for status in a commit file
58 # remove status-prefixed lines
61 patch_filter
= FilterUntil()
62 until_test
= lambda t
: t
== (__patch_prefix
+ '\n')
63 lines
= [l
for l
in lines
if patch_filter(l
, until_test
, __comment_prefix
)]
65 # remove empty lines at the end
66 while len(lines
) != 0 and lines
[-1] == '\n':
69 f
.seek(0); f
.truncate()
72 # TODO: move this out of the stgit.stack module, it is really for
73 # higher level commands to handle the user interaction
74 def edit_file(series
, line
, comment
, show_patch
= True):
75 fname
= '.stgitmsg.txt'
76 tmpl
= templates
.get_template('patchdescr.tmpl')
85 print >> f
, __comment_prefix
, comment
86 print >> f
, __comment_prefix
, \
87 'Lines prefixed with "%s" will be automatically removed.' \
89 print >> f
, __comment_prefix
, \
90 'Trailing empty lines will be automatically removed.'
93 print >> f
, __patch_prefix
94 # series.get_patch(series.get_current()).get_top()
95 diff_str
= git
.diff(rev1
= series
.get_patch(series
.get_current()).get_bottom())
98 #Vim modeline must be near the end.
99 print >> f
, __comment_prefix
, 'vi: set textwidth=75 filetype=diff nobackup:'
104 f
= file(fname
, 'r+')
120 """An object with stgit-like properties stored as files in a directory
122 def _set_dir(self
, dir):
127 def create_empty_field(self
, name
):
128 create_empty_file(os
.path
.join(self
.__dir
, name
))
130 def _get_field(self
, name
, multiline
= False):
131 id_file
= os
.path
.join(self
.__dir
, name
)
132 if os
.path
.isfile(id_file
):
133 line
= read_string(id_file
, multiline
)
141 def _set_field(self
, name
, value
, multiline
= False):
142 fname
= os
.path
.join(self
.__dir
, name
)
143 if value
and value
!= '':
144 write_string(fname
, value
, multiline
)
145 elif os
.path
.isfile(fname
):
149 class Patch(StgitObject
):
150 """Basic patch implementation
152 def __init_refs(self
):
153 self
.__top
_ref
= self
.__refs
_base
+ '/' + self
.__name
154 self
.__log
_ref
= self
.__top
_ref
+ '.log'
156 def __init__(self
, name
, series_dir
, refs_base
):
157 self
.__series
_dir
= series_dir
159 self
._set
_dir
(os
.path
.join(self
.__series
_dir
, self
.__name
))
160 self
.__refs
_base
= refs_base
164 os
.mkdir(self
._dir
())
165 self
.create_empty_field('bottom')
166 self
.create_empty_field('top')
168 def delete(self
, keep_log
= False):
169 if os
.path
.isdir(self
._dir
()):
170 for f
in os
.listdir(self
._dir
()):
171 os
.remove(os
.path
.join(self
._dir
(), f
))
172 os
.rmdir(self
._dir
())
174 out
.warn('Patch directory "%s" does not exist' % self
._dir
())
176 # the reference might not exist if the repository was corrupted
177 git
.delete_ref(self
.__top
_ref
)
178 except git
.GitException
, e
:
180 if not keep_log
and git
.ref_exists(self
.__log
_ref
):
181 git
.delete_ref(self
.__log
_ref
)
186 def rename(self
, newname
):
188 old_top_ref
= self
.__top
_ref
189 old_log_ref
= self
.__log
_ref
190 self
.__name
= newname
191 self
._set
_dir
(os
.path
.join(self
.__series
_dir
, self
.__name
))
194 git
.rename_ref(old_top_ref
, self
.__top
_ref
)
195 if git
.ref_exists(old_log_ref
):
196 git
.rename_ref(old_log_ref
, self
.__log
_ref
)
197 os
.rename(olddir
, self
._dir
())
199 def __update_top_ref(self
, ref
):
200 git
.set_ref(self
.__top
_ref
, ref
)
202 def __update_log_ref(self
, ref
):
203 git
.set_ref(self
.__log
_ref
, ref
)
205 def update_top_ref(self
):
208 self
.__update
_top
_ref
(top
)
210 def get_old_bottom(self
):
211 return self
._get
_field
('bottom.old')
213 def get_bottom(self
):
214 return self
._get
_field
('bottom')
216 def set_bottom(self
, value
, backup
= False):
218 curr
= self
._get
_field
('bottom')
219 self
._set
_field
('bottom.old', curr
)
220 self
._set
_field
('bottom', value
)
222 def get_old_top(self
):
223 return self
._get
_field
('top.old')
226 return self
._get
_field
('top')
228 def set_top(self
, value
, backup
= False):
230 curr
= self
._get
_field
('top')
231 self
._set
_field
('top.old', curr
)
232 self
._set
_field
('top', value
)
233 self
.__update
_top
_ref
(value
)
235 def restore_old_boundaries(self
):
236 bottom
= self
._get
_field
('bottom.old')
237 top
= self
._get
_field
('top.old')
240 self
._set
_field
('bottom', bottom
)
241 self
._set
_field
('top', top
)
242 self
.__update
_top
_ref
(top
)
247 def get_description(self
):
248 return self
._get
_field
('description', True)
250 def set_description(self
, line
):
251 self
._set
_field
('description', line
, True)
253 def get_authname(self
):
254 return self
._get
_field
('authname')
256 def set_authname(self
, name
):
257 self
._set
_field
('authname', name
or git
.author().name
)
259 def get_authemail(self
):
260 return self
._get
_field
('authemail')
262 def set_authemail(self
, email
):
263 self
._set
_field
('authemail', email
or git
.author().email
)
265 def get_authdate(self
):
266 date
= self
._get
_field
('authdate')
270 if re
.match('[0-9]+\s+[+-][0-9]+', date
):
271 # Unix time (seconds) + time zone
272 secs_tz
= date
.split()
273 date
= formatdate(int(secs_tz
[0]))[:-5] + secs_tz
[1]
277 def set_authdate(self
, date
):
278 self
._set
_field
('authdate', date
or git
.author().date
)
280 def get_commname(self
):
281 return self
._get
_field
('commname')
283 def set_commname(self
, name
):
284 self
._set
_field
('commname', name
or git
.committer().name
)
286 def get_commemail(self
):
287 return self
._get
_field
('commemail')
289 def set_commemail(self
, email
):
290 self
._set
_field
('commemail', email
or git
.committer().email
)
293 return self
._get
_field
('log')
295 def set_log(self
, value
, backup
= False):
296 self
._set
_field
('log', value
)
297 self
.__update
_log
_ref
(value
)
299 # The current StGIT metadata format version.
302 class PatchSet(StgitObject
):
303 def __init__(self
, name
= None):
308 self
.set_name (git
.get_head_file())
309 self
.__base
_dir
= basedir
.get()
310 except git
.GitException
, ex
:
311 raise StackException
, 'GIT tree not initialised: %s' % ex
313 self
._set
_dir
(os
.path
.join(self
.__base
_dir
, 'patches', self
.get_name()))
317 def set_name(self
, name
):
321 return self
.__base
_dir
324 """Return the head of the branch
326 crt
= self
.get_current_patch()
330 return self
.get_base()
332 def get_protected(self
):
333 return os
.path
.isfile(os
.path
.join(self
._dir
(), 'protected'))
336 protect_file
= os
.path
.join(self
._dir
(), 'protected')
337 if not os
.path
.isfile(protect_file
):
338 create_empty_file(protect_file
)
341 protect_file
= os
.path
.join(self
._dir
(), 'protected')
342 if os
.path
.isfile(protect_file
):
343 os
.remove(protect_file
)
345 def __branch_descr(self
):
346 return 'branch.%s.description' % self
.get_name()
348 def get_description(self
):
349 return config
.get(self
.__branch
_descr
()) or ''
351 def set_description(self
, line
):
353 config
.set(self
.__branch
_descr
(), line
)
355 config
.unset(self
.__branch
_descr
())
357 def head_top_equal(self
):
358 """Return true if the head and the top are the same
360 crt
= self
.get_current_patch()
362 # we don't care, no patches applied
364 return git
.get_head() == crt
.get_top()
366 def is_initialised(self
):
367 """Checks if series is already initialised
369 return bool(config
.get(self
.format_version_key()))
372 def shortlog(patches
):
373 log
= ''.join(Run('git', 'log', '--pretty=short',
374 p
.get_top(), '^%s' % p
.get_bottom()).raw_output()
376 return Run('git', 'shortlog').raw_input(log
).raw_output()
378 class Series(PatchSet
):
379 """Class including the operations on series
381 def __init__(self
, name
= None):
382 """Takes a series name as the parameter.
384 PatchSet
.__init
__(self
, name
)
386 # Update the branch to the latest format version if it is
387 # initialized, but don't touch it if it isn't.
388 self
.update_to_current_format_version()
390 self
.__refs
_base
= 'refs/patches/%s' % self
.get_name()
392 self
.__applied
_file
= os
.path
.join(self
._dir
(), 'applied')
393 self
.__unapplied
_file
= os
.path
.join(self
._dir
(), 'unapplied')
394 self
.__hidden
_file
= os
.path
.join(self
._dir
(), 'hidden')
396 # where this series keeps its patches
397 self
.__patch
_dir
= os
.path
.join(self
._dir
(), 'patches')
400 self
.__trash
_dir
= os
.path
.join(self
._dir
(), 'trash')
402 def format_version_key(self
):
403 return 'branch.%s.stgit.stackformatversion' % self
.get_name()
405 def update_to_current_format_version(self
):
406 """Update a potentially older StGIT directory structure to the
407 latest version. Note: This function should depend as little as
408 possible on external functions that may change during a format
409 version bump, since it must remain able to process older formats."""
411 branch_dir
= os
.path
.join(self
._basedir
(), 'patches', self
.get_name())
412 def get_format_version():
413 """Return the integer format version number, or None if the
414 branch doesn't have any StGIT metadata at all, of any version."""
415 fv
= config
.get(self
.format_version_key())
416 ofv
= config
.get('branch.%s.stgitformatversion' % self
.get_name())
418 # Great, there's an explicitly recorded format version
419 # number, which means that the branch is initialized and
420 # of that exact version.
423 # Old name for the version info, upgrade it
424 config
.set(self
.format_version_key(), ofv
)
425 config
.unset('branch.%s.stgitformatversion' % self
.get_name())
427 elif os
.path
.isdir(os
.path
.join(branch_dir
, 'patches')):
428 # There's a .git/patches/<branch>/patches dirctory, which
429 # means this is an initialized version 1 branch.
431 elif os
.path
.isdir(branch_dir
):
432 # There's a .git/patches/<branch> directory, which means
433 # this is an initialized version 0 branch.
436 # The branch doesn't seem to be initialized at all.
438 def set_format_version(v
):
439 out
.info('Upgraded branch %s to format version %d' % (self
.get_name(), v
))
440 config
.set(self
.format_version_key(), '%d' % v
)
442 if not os
.path
.isdir(d
):
445 if os
.path
.exists(f
):
448 if git
.ref_exists(ref
):
452 if get_format_version() == 0:
453 mkdir(os
.path
.join(branch_dir
, 'trash'))
454 patch_dir
= os
.path
.join(branch_dir
, 'patches')
456 refs_base
= 'refs/patches/%s' % self
.get_name()
457 for patch
in (file(os
.path
.join(branch_dir
, 'unapplied')).readlines()
458 + file(os
.path
.join(branch_dir
, 'applied')).readlines()):
459 patch
= patch
.strip()
460 os
.rename(os
.path
.join(branch_dir
, patch
),
461 os
.path
.join(patch_dir
, patch
))
462 Patch(patch
, patch_dir
, refs_base
).update_top_ref()
463 set_format_version(1)
466 if get_format_version() == 1:
467 desc_file
= os
.path
.join(branch_dir
, 'description')
468 if os
.path
.isfile(desc_file
):
469 desc
= read_string(desc_file
)
471 config
.set('branch.%s.description' % self
.get_name(), desc
)
473 rm(os
.path
.join(branch_dir
, 'current'))
474 rm_ref('refs/bases/%s' % self
.get_name())
475 set_format_version(2)
477 # Make sure we're at the latest version.
478 if not get_format_version() in [None, FORMAT_VERSION
]:
479 raise StackException('Branch %s is at format version %d, expected %d'
480 % (self
.get_name(), get_format_version(), FORMAT_VERSION
))
482 def __patch_name_valid(self
, name
):
483 """Raise an exception if the patch name is not valid.
485 if not name
or re
.search('[^\w.-]', name
):
486 raise StackException
, 'Invalid patch name: "%s"' % name
488 def get_patch(self
, name
):
489 """Return a Patch object for the given name
491 return Patch(name
, self
.__patch
_dir
, self
.__refs
_base
)
493 def get_current_patch(self
):
494 """Return a Patch object representing the topmost patch, or
495 None if there is no such patch."""
496 crt
= self
.get_current()
499 return self
.get_patch(crt
)
501 def get_current(self
):
502 """Return the name of the topmost patch, or None if there is
505 applied
= self
.get_applied()
506 except StackException
:
507 # No "applied" file: branch is not initialized.
512 # No patches applied.
515 def get_applied(self
):
516 if not os
.path
.isfile(self
.__applied
_file
):
517 raise StackException
, 'Branch "%s" not initialised' % self
.get_name()
518 return read_strings(self
.__applied
_file
)
520 def set_applied(self
, applied
):
521 write_strings(self
.__applied
_file
, applied
)
523 def get_unapplied(self
):
524 if not os
.path
.isfile(self
.__unapplied
_file
):
525 raise StackException
, 'Branch "%s" not initialised' % self
.get_name()
526 return read_strings(self
.__unapplied
_file
)
528 def set_unapplied(self
, unapplied
):
529 write_strings(self
.__unapplied
_file
, unapplied
)
531 def get_hidden(self
):
532 if not os
.path
.isfile(self
.__hidden
_file
):
534 return read_strings(self
.__hidden
_file
)
537 # Return the parent of the bottommost patch, if there is one.
538 if os
.path
.isfile(self
.__applied
_file
):
539 bottommost
= file(self
.__applied
_file
).readline().strip()
541 return self
.get_patch(bottommost
).get_bottom()
542 # No bottommost patch, so just return HEAD
543 return git
.get_head()
545 def get_parent_remote(self
):
546 value
= config
.get('branch.%s.remote' % self
.get_name())
549 elif 'origin' in git
.remotes_list():
550 out
.note(('No parent remote declared for stack "%s",'
551 ' defaulting to "origin".' % self
.get_name()),
552 ('Consider setting "branch.%s.remote" and'
553 ' "branch.%s.merge" with "git config".'
554 % (self
.get_name(), self
.get_name())))
557 raise StackException
, 'Cannot find a parent remote for "%s"' % self
.get_name()
559 def __set_parent_remote(self
, remote
):
560 value
= config
.set('branch.%s.remote' % self
.get_name(), remote
)
562 def get_parent_branch(self
):
563 value
= config
.get('branch.%s.stgit.parentbranch' % self
.get_name())
566 elif git
.rev_parse('heads/origin'):
567 out
.note(('No parent branch declared for stack "%s",'
568 ' defaulting to "heads/origin".' % self
.get_name()),
569 ('Consider setting "branch.%s.stgit.parentbranch"'
570 ' with "git config".' % self
.get_name()))
571 return 'heads/origin'
573 raise StackException
, 'Cannot find a parent branch for "%s"' % self
.get_name()
575 def __set_parent_branch(self
, name
):
576 if config
.get('branch.%s.remote' % self
.get_name()):
577 # Never set merge if remote is not set to avoid
578 # possibly-erroneous lookups into 'origin'
579 config
.set('branch.%s.merge' % self
.get_name(), name
)
580 config
.set('branch.%s.stgit.parentbranch' % self
.get_name(), name
)
582 def set_parent(self
, remote
, localbranch
):
585 self
.__set
_parent
_remote
(remote
)
586 self
.__set
_parent
_branch
(localbranch
)
587 # We'll enforce this later
589 # raise StackException, 'Parent branch (%s) should be specified for %s' % localbranch, self.get_name()
591 def __patch_is_current(self
, patch
):
592 return patch
.get_name() == self
.get_current()
594 def patch_applied(self
, name
):
595 """Return true if the patch exists in the applied list
597 return name
in self
.get_applied()
599 def patch_unapplied(self
, name
):
600 """Return true if the patch exists in the unapplied list
602 return name
in self
.get_unapplied()
604 def patch_hidden(self
, name
):
605 """Return true if the patch is hidden.
607 return name
in self
.get_hidden()
609 def patch_exists(self
, name
):
610 """Return true if there is a patch with the given name, false
612 return self
.patch_applied(name
) or self
.patch_unapplied(name
) \
613 or self
.patch_hidden(name
)
615 def init(self
, create_at
=False, parent_remote
=None, parent_branch
=None):
616 """Initialises the stgit series
618 if self
.is_initialised():
619 raise StackException
, '%s already initialized' % self
.get_name()
620 for d
in [self
._dir
()]:
621 if os
.path
.exists(d
):
622 raise StackException
, '%s already exists' % d
624 if (create_at
!=False):
625 git
.create_branch(self
.get_name(), create_at
)
627 os
.makedirs(self
.__patch
_dir
)
629 self
.set_parent(parent_remote
, parent_branch
)
631 self
.create_empty_field('applied')
632 self
.create_empty_field('unapplied')
634 config
.set(self
.format_version_key(), str(FORMAT_VERSION
))
636 def rename(self
, to_name
):
639 to_stack
= Series(to_name
)
641 if to_stack
.is_initialised():
642 raise StackException
, '"%s" already exists' % to_stack
.get_name()
644 patches
= self
.get_applied() + self
.get_unapplied()
646 git
.rename_branch(self
.get_name(), to_name
)
648 for patch
in patches
:
649 git
.rename_ref('refs/patches/%s/%s' % (self
.get_name(), patch
),
650 'refs/patches/%s/%s' % (to_name
, patch
))
651 git
.rename_ref('refs/patches/%s/%s.log' % (self
.get_name(), patch
),
652 'refs/patches/%s/%s.log' % (to_name
, patch
))
653 if os
.path
.isdir(self
._dir
()):
654 rename(os
.path
.join(self
._basedir
(), 'patches'),
655 self
.get_name(), to_stack
.get_name())
657 # Rename the config section
658 for k
in ['branch.%s', 'branch.%s.stgit']:
659 config
.rename_section(k
% self
.get_name(), k
% to_name
)
661 self
.__init
__(to_name
)
663 def clone(self
, target_series
):
667 # allow cloning of branches not under StGIT control
668 base
= self
.get_base()
670 base
= git
.get_head()
671 Series(target_series
).init(create_at
= base
)
672 new_series
= Series(target_series
)
674 # generate an artificial description file
675 new_series
.set_description('clone of "%s"' % self
.get_name())
677 # clone self's entire series as unapplied patches
679 # allow cloning of branches not under StGIT control
680 applied
= self
.get_applied()
681 unapplied
= self
.get_unapplied()
682 patches
= applied
+ unapplied
685 patches
= applied
= unapplied
= []
687 patch
= self
.get_patch(p
)
688 newpatch
= new_series
.new_patch(p
, message
= patch
.get_description(),
689 can_edit
= False, unapplied
= True,
690 bottom
= patch
.get_bottom(),
691 top
= patch
.get_top(),
692 author_name
= patch
.get_authname(),
693 author_email
= patch
.get_authemail(),
694 author_date
= patch
.get_authdate())
696 out
.info('Setting log to %s' % patch
.get_log())
697 newpatch
.set_log(patch
.get_log())
699 out
.info('No log for %s' % p
)
701 # fast forward the cloned series to self's top
702 new_series
.forward_patches(applied
)
704 # Clone parent informations
705 value
= config
.get('branch.%s.remote' % self
.get_name())
707 config
.set('branch.%s.remote' % target_series
, value
)
709 value
= config
.get('branch.%s.merge' % self
.get_name())
711 config
.set('branch.%s.merge' % target_series
, value
)
713 value
= config
.get('branch.%s.stgit.parentbranch' % self
.get_name())
715 config
.set('branch.%s.stgit.parentbranch' % target_series
, value
)
717 def delete(self
, force
= False):
718 """Deletes an stgit series
720 if self
.is_initialised():
721 patches
= self
.get_unapplied() + self
.get_applied()
722 if not force
and patches
:
723 raise StackException
, \
724 'Cannot delete: the series still contains patches'
726 self
.get_patch(p
).delete()
728 # remove the trash directory if any
729 if os
.path
.exists(self
.__trash
_dir
):
730 for fname
in os
.listdir(self
.__trash
_dir
):
731 os
.remove(os
.path
.join(self
.__trash
_dir
, fname
))
732 os
.rmdir(self
.__trash
_dir
)
734 # FIXME: find a way to get rid of those manual removals
735 # (move functionality to StgitObject ?)
736 if os
.path
.exists(self
.__applied
_file
):
737 os
.remove(self
.__applied
_file
)
738 if os
.path
.exists(self
.__unapplied
_file
):
739 os
.remove(self
.__unapplied
_file
)
740 if os
.path
.exists(self
.__hidden
_file
):
741 os
.remove(self
.__hidden
_file
)
742 if os
.path
.exists(self
._dir
()+'/orig-base'):
743 os
.remove(self
._dir
()+'/orig-base')
745 if not os
.listdir(self
.__patch
_dir
):
746 os
.rmdir(self
.__patch
_dir
)
748 out
.warn('Patch directory %s is not empty' % self
.__patch
_dir
)
751 os
.removedirs(self
._dir
())
753 raise StackException('Series directory %s is not empty'
757 git
.delete_branch(self
.get_name())
759 out
.warn('Could not delete branch "%s"' % self
.get_name())
761 config
.remove_section('branch.%s' % self
.get_name())
762 config
.remove_section('branch.%s.stgit' % self
.get_name())
764 def refresh_patch(self
, files
= None, message
= None, edit
= False,
767 author_name
= None, author_email
= None,
769 committer_name
= None, committer_email
= None,
770 backup
= True, sign_str
= None, log
= 'refresh',
771 notes
= None, bottom
= None):
772 """Generates a new commit for the topmost patch
774 patch
= self
.get_current_patch()
776 raise StackException
, 'No patches applied'
778 descr
= patch
.get_description()
779 if not (message
or descr
):
785 # TODO: move this out of the stgit.stack module, it is really
786 # for higher level commands to handle the user interaction
787 if not message
and edit
:
788 descr
= edit_file(self
, descr
.rstrip(), \
789 'Please edit the description for patch "%s" ' \
790 'above.' % patch
.get_name(), show_patch
)
793 author_name
= patch
.get_authname()
795 author_email
= patch
.get_authemail()
797 author_date
= patch
.get_authdate()
798 if not committer_name
:
799 committer_name
= patch
.get_commname()
800 if not committer_email
:
801 committer_email
= patch
.get_commemail()
803 descr
= add_sign_line(descr
, sign_str
, committer_name
, committer_email
)
806 bottom
= patch
.get_bottom()
808 commit_id
= git
.commit(files
= files
,
809 message
= descr
, parents
= [bottom
],
810 cache_update
= cache_update
,
812 author_name
= author_name
,
813 author_email
= author_email
,
814 author_date
= author_date
,
815 committer_name
= committer_name
,
816 committer_email
= committer_email
)
818 patch
.set_bottom(bottom
, backup
= backup
)
819 patch
.set_top(commit_id
, backup
= backup
)
820 patch
.set_description(descr
)
821 patch
.set_authname(author_name
)
822 patch
.set_authemail(author_email
)
823 patch
.set_authdate(author_date
)
824 patch
.set_commname(committer_name
)
825 patch
.set_commemail(committer_email
)
828 self
.log_patch(patch
, log
, notes
)
832 def undo_refresh(self
):
833 """Undo the patch boundaries changes caused by 'refresh'
835 name
= self
.get_current()
838 patch
= self
.get_patch(name
)
839 old_bottom
= patch
.get_old_bottom()
840 old_top
= patch
.get_old_top()
842 # the bottom of the patch is not changed by refresh. If the
843 # old_bottom is different, there wasn't any previous 'refresh'
844 # command (probably only a 'push')
845 if old_bottom
!= patch
.get_bottom() or old_top
== patch
.get_top():
846 raise StackException
, 'No undo information available'
848 git
.reset(tree_id
= old_top
, check_out
= False)
849 if patch
.restore_old_boundaries():
850 self
.log_patch(patch
, 'undo')
852 def new_patch(self
, name
, message
= None, can_edit
= True,
853 unapplied
= False, show_patch
= False,
854 top
= None, bottom
= None, commit
= True,
855 author_name
= None, author_email
= None, author_date
= None,
856 committer_name
= None, committer_email
= None,
857 before_existing
= False, sign_str
= None):
858 """Creates a new patch, either pointing to an existing commit object,
859 or by creating a new commit object.
862 assert commit
or (top
and bottom
)
863 assert not before_existing
or (top
and bottom
)
864 assert not (commit
and before_existing
)
865 assert (top
and bottom
) or (not top
and not bottom
)
866 assert commit
or (not top
or (bottom
== git
.get_commit(top
).get_parent()))
869 self
.__patch
_name
_valid
(name
)
870 if self
.patch_exists(name
):
871 raise StackException
, 'Patch "%s" already exists' % name
873 # TODO: move this out of the stgit.stack module, it is really
874 # for higher level commands to handle the user interaction
876 return add_sign_line(msg
, sign_str
,
877 committer_name
or git
.committer().name
,
878 committer_email
or git
.committer().email
)
879 if not message
and can_edit
:
882 'Please enter the description for the patch above.',
885 descr
= sign(message
)
887 head
= git
.get_head()
890 name
= make_patch_name(descr
, self
.patch_exists
)
892 patch
= self
.get_patch(name
)
895 patch
.set_description(descr
)
896 patch
.set_authname(author_name
)
897 patch
.set_authemail(author_email
)
898 patch
.set_authdate(author_date
)
899 patch
.set_commname(committer_name
)
900 patch
.set_commemail(committer_email
)
903 insert_string(self
.__applied
_file
, patch
.get_name())
905 patches
= [patch
.get_name()] + self
.get_unapplied()
906 write_strings(self
.__unapplied
_file
, patches
)
909 append_string(self
.__applied
_file
, patch
.get_name())
914 top_commit
= git
.get_commit(top
)
917 top_commit
= git
.get_commit(head
)
919 # create a commit for the patch (may be empty if top == bottom);
920 # only commit on top of the current branch
921 assert(unapplied
or bottom
== head
)
922 commit_id
= git
.commit(message
= descr
, parents
= [bottom
],
923 cache_update
= False,
924 tree_id
= top_commit
.get_tree(),
925 allowempty
= True, set_head
= set_head
,
926 author_name
= author_name
,
927 author_email
= author_email
,
928 author_date
= author_date
,
929 committer_name
= committer_name
,
930 committer_email
= committer_email
)
931 # set the patch top to the new commit
932 patch
.set_bottom(bottom
)
933 patch
.set_top(commit_id
)
936 patch
.set_bottom(bottom
)
939 self
.log_patch(patch
, 'new')
943 def delete_patch(self
, name
, keep_log
= False):
946 self
.__patch
_name
_valid
(name
)
947 patch
= self
.get_patch(name
)
949 if self
.__patch
_is
_current
(patch
):
951 elif self
.patch_applied(name
):
952 raise StackException
, 'Cannot remove an applied patch, "%s", ' \
953 'which is not current' % name
954 elif not name
in self
.get_unapplied():
955 raise StackException
, 'Unknown patch "%s"' % name
957 # save the commit id to a trash file
958 write_string(os
.path
.join(self
.__trash
_dir
, name
), patch
.get_top())
960 patch
.delete(keep_log
= keep_log
)
962 unapplied
= self
.get_unapplied()
963 unapplied
.remove(name
)
964 write_strings(self
.__unapplied
_file
, unapplied
)
966 def forward_patches(self
, names
):
967 """Try to fast-forward an array of patches.
969 On return, patches in names[0:returned_value] have been pushed on the
970 stack. Apply the rest with push_patch
972 unapplied
= self
.get_unapplied()
978 assert(name
in unapplied
)
980 patch
= self
.get_patch(name
)
983 bottom
= patch
.get_bottom()
984 top
= patch
.get_top()
986 # top != bottom always since we have a commit for each patch
988 # reset the backup information. No logging since the
989 # patch hasn't changed
990 patch
.set_bottom(head
, backup
= True)
991 patch
.set_top(top
, backup
= True)
994 head_tree
= git
.get_commit(head
).get_tree()
995 bottom_tree
= git
.get_commit(bottom
).get_tree()
996 if head_tree
== bottom_tree
:
997 # We must just reparent this patch and create a new commit
999 descr
= patch
.get_description()
1000 author_name
= patch
.get_authname()
1001 author_email
= patch
.get_authemail()
1002 author_date
= patch
.get_authdate()
1003 committer_name
= patch
.get_commname()
1004 committer_email
= patch
.get_commemail()
1006 top_tree
= git
.get_commit(top
).get_tree()
1008 top
= git
.commit(message
= descr
, parents
= [head
],
1009 cache_update
= False,
1012 author_name
= author_name
,
1013 author_email
= author_email
,
1014 author_date
= author_date
,
1015 committer_name
= committer_name
,
1016 committer_email
= committer_email
)
1018 patch
.set_bottom(head
, backup
= True)
1019 patch
.set_top(top
, backup
= True)
1021 self
.log_patch(patch
, 'push(f)')
1024 # stop the fast-forwarding, must do a real merge
1028 unapplied
.remove(name
)
1035 append_strings(self
.__applied
_file
, names
[0:forwarded
])
1036 write_strings(self
.__unapplied
_file
, unapplied
)
1040 def merged_patches(self
, names
):
1041 """Test which patches were merged upstream by reverse-applying
1042 them in reverse order. The function returns the list of
1043 patches detected to have been applied. The state of the tree
1044 is restored to the original one
1046 patches
= [self
.get_patch(name
) for name
in names
]
1051 if git
.apply_diff(p
.get_top(), p
.get_bottom()):
1052 merged
.append(p
.get_name())
1059 def push_empty_patch(self
, name
):
1060 """Pushes an empty patch on the stack
1062 unapplied
= self
.get_unapplied()
1063 assert(name
in unapplied
)
1065 # patch = self.get_patch(name)
1066 head
= git
.get_head()
1068 append_string(self
.__applied
_file
, name
)
1070 unapplied
.remove(name
)
1071 write_strings(self
.__unapplied
_file
, unapplied
)
1073 self
.refresh_patch(bottom
= head
, cache_update
= False, log
= 'push(m)')
1075 def push_patch(self
, name
):
1076 """Pushes a patch on the stack
1078 unapplied
= self
.get_unapplied()
1079 assert(name
in unapplied
)
1081 patch
= self
.get_patch(name
)
1083 head
= git
.get_head()
1084 bottom
= patch
.get_bottom()
1085 top
= patch
.get_top()
1086 # top != bottom always since we have a commit for each patch
1089 # A fast-forward push. Just reset the backup
1090 # information. No need for logging
1091 patch
.set_bottom(bottom
, backup
= True)
1092 patch
.set_top(top
, backup
= True)
1095 append_string(self
.__applied
_file
, name
)
1097 unapplied
.remove(name
)
1098 write_strings(self
.__unapplied
_file
, unapplied
)
1101 # Need to create a new commit an merge in the old patch
1105 # Try the fast applying first. If this fails, fall back to the
1107 if not git
.apply_diff(bottom
, top
):
1108 # if git.apply_diff() fails, the patch requires a diff3
1109 # merge and can be reported as modified
1112 # merge can fail but the patch needs to be pushed
1114 git
.merge(bottom
, head
, top
, recursive
= True)
1115 except git
.GitException
, ex
:
1116 out
.error('The merge failed during "push".',
1117 'Use "refresh" after fixing the conflicts or'
1118 ' revert the operation with "push --undo".')
1120 append_string(self
.__applied
_file
, name
)
1122 unapplied
.remove(name
)
1123 write_strings(self
.__unapplied
_file
, unapplied
)
1126 # if the merge was OK and no conflicts, just refresh the patch
1127 # The GIT cache was already updated by the merge operation
1132 self
.refresh_patch(bottom
= head
, cache_update
= False, log
= log
)
1134 # we store the correctly merged files only for
1135 # tracking the conflict history. Note that the
1136 # git.merge() operations should always leave the index
1137 # in a valid state (i.e. only stage 0 files)
1138 self
.refresh_patch(bottom
= head
, cache_update
= False,
1140 raise StackException
, str(ex
)
1144 def undo_push(self
):
1145 name
= self
.get_current()
1148 patch
= self
.get_patch(name
)
1149 old_bottom
= patch
.get_old_bottom()
1150 old_top
= patch
.get_old_top()
1152 # the top of the patch is changed by a push operation only
1153 # together with the bottom (otherwise the top was probably
1154 # modified by 'refresh'). If they are both unchanged, there
1155 # was a fast forward
1156 if old_bottom
== patch
.get_bottom() and old_top
!= patch
.get_top():
1157 raise StackException
, 'No undo information available'
1160 self
.pop_patch(name
)
1161 ret
= patch
.restore_old_boundaries()
1163 self
.log_patch(patch
, 'undo')
1167 def pop_patch(self
, name
, keep
= False):
1168 """Pops the top patch from the stack
1170 applied
= self
.get_applied()
1172 assert(name
in applied
)
1174 patch
= self
.get_patch(name
)
1176 if git
.get_head_file() == self
.get_name():
1177 if keep
and not git
.apply_diff(git
.get_head(), patch
.get_bottom(),
1178 check_index
= False):
1179 raise StackException(
1180 'Failed to pop patches while preserving the local changes')
1181 git
.switch(patch
.get_bottom(), keep
)
1183 git
.set_branch(self
.get_name(), patch
.get_bottom())
1185 # save the new applied list
1186 idx
= applied
.index(name
) + 1
1188 popped
= applied
[:idx
]
1190 unapplied
= popped
+ self
.get_unapplied()
1191 write_strings(self
.__unapplied
_file
, unapplied
)
1195 write_strings(self
.__applied
_file
, applied
)
1197 def empty_patch(self
, name
):
1198 """Returns True if the patch is empty
1200 self
.__patch
_name
_valid
(name
)
1201 patch
= self
.get_patch(name
)
1202 bottom
= patch
.get_bottom()
1203 top
= patch
.get_top()
1207 elif git
.get_commit(top
).get_tree() \
1208 == git
.get_commit(bottom
).get_tree():
1213 def rename_patch(self
, oldname
, newname
):
1214 self
.__patch
_name
_valid
(newname
)
1216 applied
= self
.get_applied()
1217 unapplied
= self
.get_unapplied()
1219 if oldname
== newname
:
1220 raise StackException
, '"To" name and "from" name are the same'
1222 if newname
in applied
or newname
in unapplied
:
1223 raise StackException
, 'Patch "%s" already exists' % newname
1225 if oldname
in unapplied
:
1226 self
.get_patch(oldname
).rename(newname
)
1227 unapplied
[unapplied
.index(oldname
)] = newname
1228 write_strings(self
.__unapplied
_file
, unapplied
)
1229 elif oldname
in applied
:
1230 self
.get_patch(oldname
).rename(newname
)
1232 applied
[applied
.index(oldname
)] = newname
1233 write_strings(self
.__applied
_file
, applied
)
1235 raise StackException
, 'Unknown patch "%s"' % oldname
1237 def log_patch(self
, patch
, message
, notes
= None):
1238 """Generate a log commit for a patch
1240 top
= git
.get_commit(patch
.get_top())
1241 old_log
= patch
.get_log()
1244 # replace the current log entry
1246 raise StackException
, \
1247 'No log entry to annotate for patch "%s"' \
1250 log_commit
= git
.get_commit(old_log
)
1251 msg
= log_commit
.get_log().split('\n')[0]
1252 log_parent
= log_commit
.get_parent()
1254 parents
= [log_parent
]
1258 # generate a new log entry
1260 msg
= '%s\t%s' % (message
, top
.get_id_hash())
1267 msg
+= '\n\n' + notes
1269 log
= git
.commit(message
= msg
, parents
= parents
,
1270 cache_update
= False, tree_id
= top
.get_tree(),
1274 def hide_patch(self
, name
):
1275 """Add the patch to the hidden list.
1277 unapplied
= self
.get_unapplied()
1278 if name
not in unapplied
:
1279 # keep the checking order for backward compatibility with
1280 # the old hidden patches functionality
1281 if self
.patch_applied(name
):
1282 raise StackException
, 'Cannot hide applied patch "%s"' % name
1283 elif self
.patch_hidden(name
):
1284 raise StackException
, 'Patch "%s" already hidden' % name
1286 raise StackException
, 'Unknown patch "%s"' % name
1288 if not self
.patch_hidden(name
):
1289 # check needed for backward compatibility with the old
1290 # hidden patches functionality
1291 append_string(self
.__hidden
_file
, name
)
1293 unapplied
.remove(name
)
1294 write_strings(self
.__unapplied
_file
, unapplied
)
1296 def unhide_patch(self
, name
):
1297 """Remove the patch from the hidden list.
1299 hidden
= self
.get_hidden()
1300 if not name
in hidden
:
1301 if self
.patch_applied(name
) or self
.patch_unapplied(name
):
1302 raise StackException
, 'Patch "%s" not hidden' % name
1304 raise StackException
, 'Unknown patch "%s"' % name
1307 write_strings(self
.__hidden
_file
, hidden
)
1309 if not self
.patch_applied(name
) and not self
.patch_unapplied(name
):
1310 # check needed for backward compatibility with the old
1311 # hidden patches functionality
1312 append_string(self
.__unapplied
_file
, name
)