chiark / gitweb /
f54bec4453f1bd7e2cc1f503f64499f2973019e8
[stgit] / stgit / stack.py
1 """Basic quilt-like functionality
2 """
3
4 __copyright__ = """
5 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
6
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.
10
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.
15
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
19 """
20
21 import sys, os, re
22 from email.Utils import formatdate
23
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
31
32
33 # stack exception class
34 class StackException(StgException):
35     pass
36
37 class FilterUntil:
38     def __init__(self):
39         self.should_print = True
40     def __call__(self, x, until_test, prefix):
41         if until_test(x):
42             self.should_print = False
43         if self.should_print:
44             return x[0:len(prefix)] != prefix
45         return False
46
47 #
48 # Functions
49 #
50 __comment_prefix = 'STG:'
51 __patch_prefix = 'STG_PATCH:'
52
53 def __clean_comments(f):
54     """Removes lines marked for status in a commit file
55     """
56     f.seek(0)
57
58     # remove status-prefixed lines
59     lines = f.readlines()
60
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)]
64
65     # remove empty lines at the end
66     while len(lines) != 0 and lines[-1] == '\n':
67         del lines[-1]
68
69     f.seek(0); f.truncate()
70     f.writelines(lines)
71
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')
77
78     f = file(fname, 'w+')
79     if line:
80         print >> f, line
81     elif tmpl:
82         print >> f, tmpl,
83     else:
84         print >> f
85     print >> f, __comment_prefix, comment
86     print >> f, __comment_prefix, \
87           'Lines prefixed with "%s" will be automatically removed.' \
88           % __comment_prefix
89     print >> f, __comment_prefix, \
90           'Trailing empty lines will be automatically removed.'
91
92     if show_patch:
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())
96        f.write(diff_str)
97
98     #Vim modeline must be near the end.
99     print >> f, __comment_prefix, 'vi: set textwidth=75 filetype=diff nobackup:'
100     f.close()
101
102     call_editor(fname)
103
104     f = file(fname, 'r+')
105
106     __clean_comments(f)
107     f.seek(0)
108     result = f.read()
109
110     f.close()
111     os.remove(fname)
112
113     return result
114
115 #
116 # Classes
117 #
118
119 class StgitObject:
120     """An object with stgit-like properties stored as files in a directory
121     """
122     def _set_dir(self, dir):
123         self.__dir = dir
124     def _dir(self):
125         return self.__dir
126
127     def create_empty_field(self, name):
128         create_empty_file(os.path.join(self.__dir, name))
129
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)
134             if line == '':
135                 return None
136             else:
137                 return line
138         else:
139             return None
140
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):
146             os.remove(fname)
147
148
149 class Patch(StgitObject):
150     """Basic patch implementation
151     """
152     def __init_refs(self):
153         self.__top_ref = self.__refs_base + '/' + self.__name
154         self.__log_ref = self.__top_ref + '.log'
155
156     def __init__(self, name, series_dir, refs_base):
157         self.__series_dir = series_dir
158         self.__name = name
159         self._set_dir(os.path.join(self.__series_dir, self.__name))
160         self.__refs_base = refs_base
161         self.__init_refs()
162
163     def create(self):
164         os.mkdir(self._dir())
165         self.create_empty_field('bottom')
166         self.create_empty_field('top')
167
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())
173         else:
174             out.warn('Patch directory "%s" does not exist' % self._dir())
175         try:
176             # the reference might not exist if the repository was corrupted
177             git.delete_ref(self.__top_ref)
178         except git.GitException, e:
179             out.warn(str(e))
180         if not keep_log and git.ref_exists(self.__log_ref):
181             git.delete_ref(self.__log_ref)
182
183     def get_name(self):
184         return self.__name
185
186     def rename(self, newname):
187         olddir = self._dir()
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))
192         self.__init_refs()
193
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())
198
199     def __update_top_ref(self, ref):
200         git.set_ref(self.__top_ref, ref)
201
202     def __update_log_ref(self, ref):
203         git.set_ref(self.__log_ref, ref)
204
205     def update_top_ref(self):
206         top = self.get_top()
207         if top:
208             self.__update_top_ref(top)
209
210     def get_old_bottom(self):
211         old_bottom = self._get_field('bottom.old')
212         old_top = self.get_old_top()
213         assert old_bottom == git.get_commit(old_top).get_parent()
214         return old_bottom
215
216     def get_bottom(self):
217         bottom = self._get_field('bottom')
218         top = self.get_top()
219         assert bottom == git.get_commit(top).get_parent()
220         return self._get_field('bottom')
221
222     def set_bottom(self, value, backup = False):
223         if backup:
224             curr = self._get_field('bottom')
225             self._set_field('bottom.old', curr)
226         self._set_field('bottom', value)
227
228     def get_old_top(self):
229         return self._get_field('top.old')
230
231     def get_top(self):
232         top = self._get_field('top')
233         try:
234             ref = git.rev_parse(self.__top_ref)
235         except:
236             ref = None
237         assert not ref or top == ref
238         return top
239
240     def set_top(self, value, backup = False):
241         if backup:
242             curr = self._get_field('top')
243             self._set_field('top.old', curr)
244         self._set_field('top', value)
245         self.__update_top_ref(value)
246         self.get_bottom() # check the assert
247
248     def restore_old_boundaries(self):
249         bottom = self._get_field('bottom.old')
250         top = self._get_field('top.old')
251
252         if top and bottom:
253             self._set_field('bottom', bottom)
254             self._set_field('top', top)
255             self.__update_top_ref(top)
256             return True
257         else:
258             return False
259
260     def get_description(self):
261         return self._get_field('description', True)
262
263     def set_description(self, line):
264         self._set_field('description', line, True)
265
266     def get_authname(self):
267         return self._get_field('authname')
268
269     def set_authname(self, name):
270         self._set_field('authname', name or git.author().name)
271
272     def get_authemail(self):
273         return self._get_field('authemail')
274
275     def set_authemail(self, email):
276         self._set_field('authemail', email or git.author().email)
277
278     def get_authdate(self):
279         date = self._get_field('authdate')
280         if not date:
281             return date
282
283         if re.match('[0-9]+\s+[+-][0-9]+', date):
284             # Unix time (seconds) + time zone
285             secs_tz = date.split()
286             date = formatdate(int(secs_tz[0]))[:-5] + secs_tz[1]
287
288         return date
289
290     def set_authdate(self, date):
291         self._set_field('authdate', date or git.author().date)
292
293     def get_commname(self):
294         return self._get_field('commname')
295
296     def set_commname(self, name):
297         self._set_field('commname', name or git.committer().name)
298
299     def get_commemail(self):
300         return self._get_field('commemail')
301
302     def set_commemail(self, email):
303         self._set_field('commemail', email or git.committer().email)
304
305     def get_log(self):
306         return self._get_field('log')
307
308     def set_log(self, value, backup = False):
309         self._set_field('log', value)
310         self.__update_log_ref(value)
311
312 # The current StGIT metadata format version.
313 FORMAT_VERSION = 2
314
315 class PatchSet(StgitObject):
316     def __init__(self, name = None):
317         try:
318             if name:
319                 self.set_name (name)
320             else:
321                 self.set_name (git.get_head_file())
322             self.__base_dir = basedir.get()
323         except git.GitException, ex:
324             raise StackException, 'GIT tree not initialised: %s' % ex
325
326         self._set_dir(os.path.join(self.__base_dir, 'patches', self.get_name()))
327
328     def get_name(self):
329         return self.__name
330     def set_name(self, name):
331         self.__name = name
332
333     def _basedir(self):
334         return self.__base_dir
335
336     def get_head(self):
337         """Return the head of the branch
338         """
339         crt = self.get_current_patch()
340         if crt:
341             return crt.get_top()
342         else:
343             return self.get_base()
344
345     def get_protected(self):
346         return os.path.isfile(os.path.join(self._dir(), 'protected'))
347
348     def protect(self):
349         protect_file = os.path.join(self._dir(), 'protected')
350         if not os.path.isfile(protect_file):
351             create_empty_file(protect_file)
352
353     def unprotect(self):
354         protect_file = os.path.join(self._dir(), 'protected')
355         if os.path.isfile(protect_file):
356             os.remove(protect_file)
357
358     def __branch_descr(self):
359         return 'branch.%s.description' % self.get_name()
360
361     def get_description(self):
362         return config.get(self.__branch_descr()) or ''
363
364     def set_description(self, line):
365         if line:
366             config.set(self.__branch_descr(), line)
367         else:
368             config.unset(self.__branch_descr())
369
370     def head_top_equal(self):
371         """Return true if the head and the top are the same
372         """
373         crt = self.get_current_patch()
374         if not crt:
375             # we don't care, no patches applied
376             return True
377         return git.get_head() == crt.get_top()
378
379     def is_initialised(self):
380         """Checks if series is already initialised
381         """
382         return bool(config.get(self.format_version_key()))
383
384
385 def shortlog(patches):
386     log = ''.join(Run('git', 'log', '--pretty=short',
387                       p.get_top(), '^%s' % p.get_bottom()).raw_output()
388                   for p in patches)
389     return Run('git', 'shortlog').raw_input(log).raw_output()
390
391 class Series(PatchSet):
392     """Class including the operations on series
393     """
394     def __init__(self, name = None):
395         """Takes a series name as the parameter.
396         """
397         PatchSet.__init__(self, name)
398
399         # Update the branch to the latest format version if it is
400         # initialized, but don't touch it if it isn't.
401         self.update_to_current_format_version()
402
403         self.__refs_base = 'refs/patches/%s' % self.get_name()
404
405         self.__applied_file = os.path.join(self._dir(), 'applied')
406         self.__unapplied_file = os.path.join(self._dir(), 'unapplied')
407         self.__hidden_file = os.path.join(self._dir(), 'hidden')
408
409         # where this series keeps its patches
410         self.__patch_dir = os.path.join(self._dir(), 'patches')
411
412         # trash directory
413         self.__trash_dir = os.path.join(self._dir(), 'trash')
414
415     def format_version_key(self):
416         return 'branch.%s.stgit.stackformatversion' % self.get_name()
417
418     def update_to_current_format_version(self):
419         """Update a potentially older StGIT directory structure to the
420         latest version. Note: This function should depend as little as
421         possible on external functions that may change during a format
422         version bump, since it must remain able to process older formats."""
423
424         branch_dir = os.path.join(self._basedir(), 'patches', self.get_name())
425         def get_format_version():
426             """Return the integer format version number, or None if the
427             branch doesn't have any StGIT metadata at all, of any version."""
428             fv = config.get(self.format_version_key())
429             ofv = config.get('branch.%s.stgitformatversion' % self.get_name())
430             if fv:
431                 # Great, there's an explicitly recorded format version
432                 # number, which means that the branch is initialized and
433                 # of that exact version.
434                 return int(fv)
435             elif ofv:
436                 # Old name for the version info, upgrade it
437                 config.set(self.format_version_key(), ofv)
438                 config.unset('branch.%s.stgitformatversion' % self.get_name())
439                 return int(ofv)
440             elif os.path.isdir(os.path.join(branch_dir, 'patches')):
441                 # There's a .git/patches/<branch>/patches dirctory, which
442                 # means this is an initialized version 1 branch.
443                 return 1
444             elif os.path.isdir(branch_dir):
445                 # There's a .git/patches/<branch> directory, which means
446                 # this is an initialized version 0 branch.
447                 return 0
448             else:
449                 # The branch doesn't seem to be initialized at all.
450                 return None
451         def set_format_version(v):
452             out.info('Upgraded branch %s to format version %d' % (self.get_name(), v))
453             config.set(self.format_version_key(), '%d' % v)
454         def mkdir(d):
455             if not os.path.isdir(d):
456                 os.makedirs(d)
457         def rm(f):
458             if os.path.exists(f):
459                 os.remove(f)
460         def rm_ref(ref):
461             if git.ref_exists(ref):
462                 git.delete_ref(ref)
463
464         # Update 0 -> 1.
465         if get_format_version() == 0:
466             mkdir(os.path.join(branch_dir, 'trash'))
467             patch_dir = os.path.join(branch_dir, 'patches')
468             mkdir(patch_dir)
469             refs_base = 'refs/patches/%s' % self.get_name()
470             for patch in (file(os.path.join(branch_dir, 'unapplied')).readlines()
471                           + file(os.path.join(branch_dir, 'applied')).readlines()):
472                 patch = patch.strip()
473                 os.rename(os.path.join(branch_dir, patch),
474                           os.path.join(patch_dir, patch))
475                 Patch(patch, patch_dir, refs_base).update_top_ref()
476             set_format_version(1)
477
478         # Update 1 -> 2.
479         if get_format_version() == 1:
480             desc_file = os.path.join(branch_dir, 'description')
481             if os.path.isfile(desc_file):
482                 desc = read_string(desc_file)
483                 if desc:
484                     config.set('branch.%s.description' % self.get_name(), desc)
485                 rm(desc_file)
486             rm(os.path.join(branch_dir, 'current'))
487             rm_ref('refs/bases/%s' % self.get_name())
488             set_format_version(2)
489
490         # Make sure we're at the latest version.
491         if not get_format_version() in [None, FORMAT_VERSION]:
492             raise StackException('Branch %s is at format version %d, expected %d'
493                                  % (self.get_name(), get_format_version(), FORMAT_VERSION))
494
495     def __patch_name_valid(self, name):
496         """Raise an exception if the patch name is not valid.
497         """
498         if not name or re.search('[^\w.-]', name):
499             raise StackException, 'Invalid patch name: "%s"' % name
500
501     def get_patch(self, name):
502         """Return a Patch object for the given name
503         """
504         return Patch(name, self.__patch_dir, self.__refs_base)
505
506     def get_current_patch(self):
507         """Return a Patch object representing the topmost patch, or
508         None if there is no such patch."""
509         crt = self.get_current()
510         if not crt:
511             return None
512         return self.get_patch(crt)
513
514     def get_current(self):
515         """Return the name of the topmost patch, or None if there is
516         no such patch."""
517         try:
518             applied = self.get_applied()
519         except StackException:
520             # No "applied" file: branch is not initialized.
521             return None
522         try:
523             return applied[-1]
524         except IndexError:
525             # No patches applied.
526             return None
527
528     def get_applied(self):
529         if not os.path.isfile(self.__applied_file):
530             raise StackException, 'Branch "%s" not initialised' % self.get_name()
531         return read_strings(self.__applied_file)
532
533     def set_applied(self, applied):
534         write_strings(self.__applied_file, applied)
535
536     def get_unapplied(self):
537         if not os.path.isfile(self.__unapplied_file):
538             raise StackException, 'Branch "%s" not initialised' % self.get_name()
539         return read_strings(self.__unapplied_file)
540
541     def set_unapplied(self, unapplied):
542         write_strings(self.__unapplied_file, unapplied)
543
544     def get_hidden(self):
545         if not os.path.isfile(self.__hidden_file):
546             return []
547         return read_strings(self.__hidden_file)
548
549     def get_base(self):
550         # Return the parent of the bottommost patch, if there is one.
551         if os.path.isfile(self.__applied_file):
552             bottommost = file(self.__applied_file).readline().strip()
553             if bottommost:
554                 return self.get_patch(bottommost).get_bottom()
555         # No bottommost patch, so just return HEAD
556         return git.get_head()
557
558     def get_parent_remote(self):
559         value = config.get('branch.%s.remote' % self.get_name())
560         if value:
561             return value
562         elif 'origin' in git.remotes_list():
563             out.note(('No parent remote declared for stack "%s",'
564                       ' defaulting to "origin".' % self.get_name()),
565                      ('Consider setting "branch.%s.remote" and'
566                       ' "branch.%s.merge" with "git config".'
567                       % (self.get_name(), self.get_name())))
568             return 'origin'
569         else:
570             raise StackException, 'Cannot find a parent remote for "%s"' % self.get_name()
571
572     def __set_parent_remote(self, remote):
573         value = config.set('branch.%s.remote' % self.get_name(), remote)
574
575     def get_parent_branch(self):
576         value = config.get('branch.%s.stgit.parentbranch' % self.get_name())
577         if value:
578             return value
579         elif git.rev_parse('heads/origin'):
580             out.note(('No parent branch declared for stack "%s",'
581                       ' defaulting to "heads/origin".' % self.get_name()),
582                      ('Consider setting "branch.%s.stgit.parentbranch"'
583                       ' with "git config".' % self.get_name()))
584             return 'heads/origin'
585         else:
586             raise StackException, 'Cannot find a parent branch for "%s"' % self.get_name()
587
588     def __set_parent_branch(self, name):
589         if config.get('branch.%s.remote' % self.get_name()):
590             # Never set merge if remote is not set to avoid
591             # possibly-erroneous lookups into 'origin'
592             config.set('branch.%s.merge' % self.get_name(), name)
593         config.set('branch.%s.stgit.parentbranch' % self.get_name(), name)
594
595     def set_parent(self, remote, localbranch):
596         if localbranch:
597             if remote:
598                 self.__set_parent_remote(remote)
599             self.__set_parent_branch(localbranch)
600         # We'll enforce this later
601 #         else:
602 #             raise StackException, 'Parent branch (%s) should be specified for %s' % localbranch, self.get_name()
603
604     def __patch_is_current(self, patch):
605         return patch.get_name() == self.get_current()
606
607     def patch_applied(self, name):
608         """Return true if the patch exists in the applied list
609         """
610         return name in self.get_applied()
611
612     def patch_unapplied(self, name):
613         """Return true if the patch exists in the unapplied list
614         """
615         return name in self.get_unapplied()
616
617     def patch_hidden(self, name):
618         """Return true if the patch is hidden.
619         """
620         return name in self.get_hidden()
621
622     def patch_exists(self, name):
623         """Return true if there is a patch with the given name, false
624         otherwise."""
625         return self.patch_applied(name) or self.patch_unapplied(name) \
626                or self.patch_hidden(name)
627
628     def init(self, create_at=False, parent_remote=None, parent_branch=None):
629         """Initialises the stgit series
630         """
631         if self.is_initialised():
632             raise StackException, '%s already initialized' % self.get_name()
633         for d in [self._dir()]:
634             if os.path.exists(d):
635                 raise StackException, '%s already exists' % d
636
637         if (create_at!=False):
638             git.create_branch(self.get_name(), create_at)
639
640         os.makedirs(self.__patch_dir)
641
642         self.set_parent(parent_remote, parent_branch)
643
644         self.create_empty_field('applied')
645         self.create_empty_field('unapplied')
646
647         config.set(self.format_version_key(), str(FORMAT_VERSION))
648
649     def rename(self, to_name):
650         """Renames a series
651         """
652         to_stack = Series(to_name)
653
654         if to_stack.is_initialised():
655             raise StackException, '"%s" already exists' % to_stack.get_name()
656
657         patches = self.get_applied() + self.get_unapplied()
658
659         git.rename_branch(self.get_name(), to_name)
660
661         for patch in patches:
662             git.rename_ref('refs/patches/%s/%s' % (self.get_name(), patch),
663                            'refs/patches/%s/%s' % (to_name, patch))
664             git.rename_ref('refs/patches/%s/%s.log' % (self.get_name(), patch),
665                            'refs/patches/%s/%s.log' % (to_name, patch))
666         if os.path.isdir(self._dir()):
667             rename(os.path.join(self._basedir(), 'patches'),
668                    self.get_name(), to_stack.get_name())
669
670         # Rename the config section
671         for k in ['branch.%s', 'branch.%s.stgit']:
672             config.rename_section(k % self.get_name(), k % to_name)
673
674         self.__init__(to_name)
675
676     def clone(self, target_series):
677         """Clones a series
678         """
679         try:
680             # allow cloning of branches not under StGIT control
681             base = self.get_base()
682         except:
683             base = git.get_head()
684         Series(target_series).init(create_at = base)
685         new_series = Series(target_series)
686
687         # generate an artificial description file
688         new_series.set_description('clone of "%s"' % self.get_name())
689
690         # clone self's entire series as unapplied patches
691         try:
692             # allow cloning of branches not under StGIT control
693             applied = self.get_applied()
694             unapplied = self.get_unapplied()
695             patches = applied + unapplied
696             patches.reverse()
697         except:
698             patches = applied = unapplied = []
699         for p in patches:
700             patch = self.get_patch(p)
701             newpatch = new_series.new_patch(p, message = patch.get_description(),
702                                             can_edit = False, unapplied = True,
703                                             bottom = patch.get_bottom(),
704                                             top = patch.get_top(),
705                                             author_name = patch.get_authname(),
706                                             author_email = patch.get_authemail(),
707                                             author_date = patch.get_authdate())
708             if patch.get_log():
709                 out.info('Setting log to %s' %  patch.get_log())
710                 newpatch.set_log(patch.get_log())
711             else:
712                 out.info('No log for %s' % p)
713
714         # fast forward the cloned series to self's top
715         new_series.forward_patches(applied)
716
717         # Clone parent informations
718         value = config.get('branch.%s.remote' % self.get_name())
719         if value:
720             config.set('branch.%s.remote' % target_series, value)
721
722         value = config.get('branch.%s.merge' % self.get_name())
723         if value:
724             config.set('branch.%s.merge' % target_series, value)
725
726         value = config.get('branch.%s.stgit.parentbranch' % self.get_name())
727         if value:
728             config.set('branch.%s.stgit.parentbranch' % target_series, value)
729
730     def delete(self, force = False):
731         """Deletes an stgit series
732         """
733         if self.is_initialised():
734             patches = self.get_unapplied() + self.get_applied()
735             if not force and patches:
736                 raise StackException, \
737                       'Cannot delete: the series still contains patches'
738             for p in patches:
739                 self.get_patch(p).delete()
740
741             # remove the trash directory if any
742             if os.path.exists(self.__trash_dir):
743                 for fname in os.listdir(self.__trash_dir):
744                     os.remove(os.path.join(self.__trash_dir, fname))
745                 os.rmdir(self.__trash_dir)
746
747             # FIXME: find a way to get rid of those manual removals
748             # (move functionality to StgitObject ?)
749             if os.path.exists(self.__applied_file):
750                 os.remove(self.__applied_file)
751             if os.path.exists(self.__unapplied_file):
752                 os.remove(self.__unapplied_file)
753             if os.path.exists(self.__hidden_file):
754                 os.remove(self.__hidden_file)
755             if os.path.exists(self._dir()+'/orig-base'):
756                 os.remove(self._dir()+'/orig-base')
757
758             if not os.listdir(self.__patch_dir):
759                 os.rmdir(self.__patch_dir)
760             else:
761                 out.warn('Patch directory %s is not empty' % self.__patch_dir)
762
763             try:
764                 os.removedirs(self._dir())
765             except OSError:
766                 raise StackException('Series directory %s is not empty'
767                                      % self._dir())
768
769         try:
770             git.delete_branch(self.get_name())
771         except GitException:
772             out.warn('Could not delete branch "%s"' % self.get_name())
773
774         config.remove_section('branch.%s' % self.get_name())
775         config.remove_section('branch.%s.stgit' % self.get_name())
776
777     def refresh_patch(self, files = None, message = None, edit = False,
778                       show_patch = False,
779                       cache_update = True,
780                       author_name = None, author_email = None,
781                       author_date = None,
782                       committer_name = None, committer_email = None,
783                       backup = True, sign_str = None, log = 'refresh',
784                       notes = None, bottom = None):
785         """Generates a new commit for the topmost patch
786         """
787         patch = self.get_current_patch()
788         if not patch:
789             raise StackException, 'No patches applied'
790
791         descr = patch.get_description()
792         if not (message or descr):
793             edit = True
794             descr = ''
795         elif message:
796             descr = message
797
798         # TODO: move this out of the stgit.stack module, it is really
799         # for higher level commands to handle the user interaction
800         if not message and edit:
801             descr = edit_file(self, descr.rstrip(), \
802                               'Please edit the description for patch "%s" ' \
803                               'above.' % patch.get_name(), show_patch)
804
805         if not author_name:
806             author_name = patch.get_authname()
807         if not author_email:
808             author_email = patch.get_authemail()
809         if not author_date:
810             author_date = patch.get_authdate()
811         if not committer_name:
812             committer_name = patch.get_commname()
813         if not committer_email:
814             committer_email = patch.get_commemail()
815
816         descr = add_sign_line(descr, sign_str, committer_name, committer_email)
817
818         if not bottom:
819             bottom = patch.get_bottom()
820
821         commit_id = git.commit(files = files,
822                                message = descr, parents = [bottom],
823                                cache_update = cache_update,
824                                allowempty = True,
825                                author_name = author_name,
826                                author_email = author_email,
827                                author_date = author_date,
828                                committer_name = committer_name,
829                                committer_email = committer_email)
830
831         patch.set_bottom(bottom, backup = backup)
832         patch.set_top(commit_id, backup = backup)
833         patch.set_description(descr)
834         patch.set_authname(author_name)
835         patch.set_authemail(author_email)
836         patch.set_authdate(author_date)
837         patch.set_commname(committer_name)
838         patch.set_commemail(committer_email)
839
840         if log:
841             self.log_patch(patch, log, notes)
842
843         return commit_id
844
845     def undo_refresh(self):
846         """Undo the patch boundaries changes caused by 'refresh'
847         """
848         name = self.get_current()
849         assert(name)
850
851         patch = self.get_patch(name)
852         old_bottom = patch.get_old_bottom()
853         old_top = patch.get_old_top()
854
855         # the bottom of the patch is not changed by refresh. If the
856         # old_bottom is different, there wasn't any previous 'refresh'
857         # command (probably only a 'push')
858         if old_bottom != patch.get_bottom() or old_top == patch.get_top():
859             raise StackException, 'No undo information available'
860
861         git.reset(tree_id = old_top, check_out = False)
862         if patch.restore_old_boundaries():
863             self.log_patch(patch, 'undo')
864
865     def new_patch(self, name, message = None, can_edit = True,
866                   unapplied = False, show_patch = False,
867                   top = None, bottom = None, commit = True,
868                   author_name = None, author_email = None, author_date = None,
869                   committer_name = None, committer_email = None,
870                   before_existing = False, sign_str = None):
871         """Creates a new patch, either pointing to an existing commit object,
872         or by creating a new commit object.
873         """
874
875         assert commit or (top and bottom)
876         assert not before_existing or (top and bottom)
877         assert not (commit and before_existing)
878         assert (top and bottom) or (not top and not bottom)
879         assert commit or (not top or (bottom == git.get_commit(top).get_parent()))
880
881         if name != None:
882             self.__patch_name_valid(name)
883             if self.patch_exists(name):
884                 raise StackException, 'Patch "%s" already exists' % name
885
886         # TODO: move this out of the stgit.stack module, it is really
887         # for higher level commands to handle the user interaction
888         def sign(msg):
889             return add_sign_line(msg, sign_str,
890                                  committer_name or git.committer().name,
891                                  committer_email or git.committer().email)
892         if not message and can_edit:
893             descr = edit_file(
894                 self, sign(''),
895                 'Please enter the description for the patch above.',
896                 show_patch)
897         else:
898             descr = sign(message)
899
900         head = git.get_head()
901
902         if name == None:
903             name = make_patch_name(descr, self.patch_exists)
904
905         patch = self.get_patch(name)
906         patch.create()
907
908         patch.set_description(descr)
909         patch.set_authname(author_name)
910         patch.set_authemail(author_email)
911         patch.set_authdate(author_date)
912         patch.set_commname(committer_name)
913         patch.set_commemail(committer_email)
914
915         if before_existing:
916             insert_string(self.__applied_file, patch.get_name())
917         elif unapplied:
918             patches = [patch.get_name()] + self.get_unapplied()
919             write_strings(self.__unapplied_file, patches)
920             set_head = False
921         else:
922             append_string(self.__applied_file, patch.get_name())
923             set_head = True
924
925         if commit:
926             if top:
927                 top_commit = git.get_commit(top)
928             else:
929                 bottom = head
930                 top_commit = git.get_commit(head)
931
932             # create a commit for the patch (may be empty if top == bottom);
933             # only commit on top of the current branch
934             assert(unapplied or bottom == head)
935             commit_id = git.commit(message = descr, parents = [bottom],
936                                    cache_update = False,
937                                    tree_id = top_commit.get_tree(),
938                                    allowempty = True, set_head = set_head,
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)
944             # set the patch top to the new commit
945             patch.set_bottom(bottom)
946             patch.set_top(commit_id)
947         else:
948             assert top != bottom
949             patch.set_bottom(bottom)
950             patch.set_top(top)
951
952         self.log_patch(patch, 'new')
953
954         return patch
955
956     def delete_patch(self, name, keep_log = False):
957         """Deletes a patch
958         """
959         self.__patch_name_valid(name)
960         patch = self.get_patch(name)
961
962         if self.__patch_is_current(patch):
963             self.pop_patch(name)
964         elif self.patch_applied(name):
965             raise StackException, 'Cannot remove an applied patch, "%s", ' \
966                   'which is not current' % name
967         elif not name in self.get_unapplied():
968             raise StackException, 'Unknown patch "%s"' % name
969
970         # save the commit id to a trash file
971         write_string(os.path.join(self.__trash_dir, name), patch.get_top())
972
973         patch.delete(keep_log = keep_log)
974
975         unapplied = self.get_unapplied()
976         unapplied.remove(name)
977         write_strings(self.__unapplied_file, unapplied)
978
979     def forward_patches(self, names):
980         """Try to fast-forward an array of patches.
981
982         On return, patches in names[0:returned_value] have been pushed on the
983         stack. Apply the rest with push_patch
984         """
985         unapplied = self.get_unapplied()
986
987         forwarded = 0
988         top = git.get_head()
989
990         for name in names:
991             assert(name in unapplied)
992
993             patch = self.get_patch(name)
994
995             head = top
996             bottom = patch.get_bottom()
997             top = patch.get_top()
998
999             # top != bottom always since we have a commit for each patch
1000             if head == bottom:
1001                 # reset the backup information. No logging since the
1002                 # patch hasn't changed
1003                 patch.set_bottom(head, backup = True)
1004                 patch.set_top(top, backup = True)
1005
1006             else:
1007                 head_tree = git.get_commit(head).get_tree()
1008                 bottom_tree = git.get_commit(bottom).get_tree()
1009                 if head_tree == bottom_tree:
1010                     # We must just reparent this patch and create a new commit
1011                     # for it
1012                     descr = patch.get_description()
1013                     author_name = patch.get_authname()
1014                     author_email = patch.get_authemail()
1015                     author_date = patch.get_authdate()
1016                     committer_name = patch.get_commname()
1017                     committer_email = patch.get_commemail()
1018
1019                     top_tree = git.get_commit(top).get_tree()
1020
1021                     top = git.commit(message = descr, parents = [head],
1022                                      cache_update = False,
1023                                      tree_id = top_tree,
1024                                      allowempty = True,
1025                                      author_name = author_name,
1026                                      author_email = author_email,
1027                                      author_date = author_date,
1028                                      committer_name = committer_name,
1029                                      committer_email = committer_email)
1030
1031                     patch.set_bottom(head, backup = True)
1032                     patch.set_top(top, backup = True)
1033
1034                     self.log_patch(patch, 'push(f)')
1035                 else:
1036                     top = head
1037                     # stop the fast-forwarding, must do a real merge
1038                     break
1039
1040             forwarded+=1
1041             unapplied.remove(name)
1042
1043         if forwarded == 0:
1044             return 0
1045
1046         git.switch(top)
1047
1048         append_strings(self.__applied_file, names[0:forwarded])
1049         write_strings(self.__unapplied_file, unapplied)
1050
1051         return forwarded
1052
1053     def merged_patches(self, names):
1054         """Test which patches were merged upstream by reverse-applying
1055         them in reverse order. The function returns the list of
1056         patches detected to have been applied. The state of the tree
1057         is restored to the original one
1058         """
1059         patches = [self.get_patch(name) for name in names]
1060         patches.reverse()
1061
1062         merged = []
1063         for p in patches:
1064             if git.apply_diff(p.get_top(), p.get_bottom()):
1065                 merged.append(p.get_name())
1066         merged.reverse()
1067
1068         git.reset()
1069
1070         return merged
1071
1072     def push_empty_patch(self, name):
1073         """Pushes an empty patch on the stack
1074         """
1075         unapplied = self.get_unapplied()
1076         assert(name in unapplied)
1077
1078         # patch = self.get_patch(name)
1079         head = git.get_head()
1080
1081         append_string(self.__applied_file, name)
1082
1083         unapplied.remove(name)
1084         write_strings(self.__unapplied_file, unapplied)
1085
1086         self.refresh_patch(bottom = head, cache_update = False, log = 'push(m)')
1087
1088     def push_patch(self, name):
1089         """Pushes a patch on the stack
1090         """
1091         unapplied = self.get_unapplied()
1092         assert(name in unapplied)
1093
1094         patch = self.get_patch(name)
1095
1096         head = git.get_head()
1097         bottom = patch.get_bottom()
1098         top = patch.get_top()
1099         # top != bottom always since we have a commit for each patch
1100
1101         if head == bottom:
1102             # A fast-forward push. Just reset the backup
1103             # information. No need for logging
1104             patch.set_bottom(bottom, backup = True)
1105             patch.set_top(top, backup = True)
1106
1107             git.switch(top)
1108             append_string(self.__applied_file, name)
1109
1110             unapplied.remove(name)
1111             write_strings(self.__unapplied_file, unapplied)
1112             return False
1113
1114         # Need to create a new commit an merge in the old patch
1115         ex = None
1116         modified = False
1117
1118         # Try the fast applying first. If this fails, fall back to the
1119         # three-way merge
1120         if not git.apply_diff(bottom, top):
1121             # if git.apply_diff() fails, the patch requires a diff3
1122             # merge and can be reported as modified
1123             modified = True
1124
1125             # merge can fail but the patch needs to be pushed
1126             try:
1127                 git.merge(bottom, head, top, recursive = True)
1128             except git.GitException, ex:
1129                 out.error('The merge failed during "push".',
1130                           'Use "refresh" after fixing the conflicts or'
1131                           ' revert the operation with "push --undo".')
1132
1133         append_string(self.__applied_file, name)
1134
1135         unapplied.remove(name)
1136         write_strings(self.__unapplied_file, unapplied)
1137
1138         if not ex:
1139             # if the merge was OK and no conflicts, just refresh the patch
1140             # The GIT cache was already updated by the merge operation
1141             if modified:
1142                 log = 'push(m)'
1143             else:
1144                 log = 'push'
1145             self.refresh_patch(bottom = head, cache_update = False, log = log)
1146         else:
1147             # we store the correctly merged files only for
1148             # tracking the conflict history. Note that the
1149             # git.merge() operations should always leave the index
1150             # in a valid state (i.e. only stage 0 files)
1151             self.refresh_patch(bottom = head, cache_update = False,
1152                                log = 'push(c)')
1153             raise StackException, str(ex)
1154
1155         return modified
1156
1157     def undo_push(self):
1158         name = self.get_current()
1159         assert(name)
1160
1161         patch = self.get_patch(name)
1162         old_bottom = patch.get_old_bottom()
1163         old_top = patch.get_old_top()
1164
1165         # the top of the patch is changed by a push operation only
1166         # together with the bottom (otherwise the top was probably
1167         # modified by 'refresh'). If they are both unchanged, there
1168         # was a fast forward
1169         if old_bottom == patch.get_bottom() and old_top != patch.get_top():
1170             raise StackException, 'No undo information available'
1171
1172         git.reset()
1173         self.pop_patch(name)
1174         ret = patch.restore_old_boundaries()
1175         if ret:
1176             self.log_patch(patch, 'undo')
1177
1178         return ret
1179
1180     def pop_patch(self, name, keep = False):
1181         """Pops the top patch from the stack
1182         """
1183         applied = self.get_applied()
1184         applied.reverse()
1185         assert(name in applied)
1186
1187         patch = self.get_patch(name)
1188
1189         if git.get_head_file() == self.get_name():
1190             if keep and not git.apply_diff(git.get_head(), patch.get_bottom(),
1191                                            check_index = False):
1192                 raise StackException(
1193                     'Failed to pop patches while preserving the local changes')
1194             git.switch(patch.get_bottom(), keep)
1195         else:
1196             git.set_branch(self.get_name(), patch.get_bottom())
1197
1198         # save the new applied list
1199         idx = applied.index(name) + 1
1200
1201         popped = applied[:idx]
1202         popped.reverse()
1203         unapplied = popped + self.get_unapplied()
1204         write_strings(self.__unapplied_file, unapplied)
1205
1206         del applied[:idx]
1207         applied.reverse()
1208         write_strings(self.__applied_file, applied)
1209
1210     def empty_patch(self, name):
1211         """Returns True if the patch is empty
1212         """
1213         self.__patch_name_valid(name)
1214         patch = self.get_patch(name)
1215         bottom = patch.get_bottom()
1216         top = patch.get_top()
1217
1218         if bottom == top:
1219             return True
1220         elif git.get_commit(top).get_tree() \
1221                  == git.get_commit(bottom).get_tree():
1222             return True
1223
1224         return False
1225
1226     def rename_patch(self, oldname, newname):
1227         self.__patch_name_valid(newname)
1228
1229         applied = self.get_applied()
1230         unapplied = self.get_unapplied()
1231
1232         if oldname == newname:
1233             raise StackException, '"To" name and "from" name are the same'
1234
1235         if newname in applied or newname in unapplied:
1236             raise StackException, 'Patch "%s" already exists' % newname
1237
1238         if oldname in unapplied:
1239             self.get_patch(oldname).rename(newname)
1240             unapplied[unapplied.index(oldname)] = newname
1241             write_strings(self.__unapplied_file, unapplied)
1242         elif oldname in applied:
1243             self.get_patch(oldname).rename(newname)
1244
1245             applied[applied.index(oldname)] = newname
1246             write_strings(self.__applied_file, applied)
1247         else:
1248             raise StackException, 'Unknown patch "%s"' % oldname
1249
1250     def log_patch(self, patch, message, notes = None):
1251         """Generate a log commit for a patch
1252         """
1253         top = git.get_commit(patch.get_top())
1254         old_log = patch.get_log()
1255
1256         if message is None:
1257             # replace the current log entry
1258             if not old_log:
1259                 raise StackException, \
1260                       'No log entry to annotate for patch "%s"' \
1261                       % patch.get_name()
1262             replace = True
1263             log_commit = git.get_commit(old_log)
1264             msg = log_commit.get_log().split('\n')[0]
1265             log_parent = log_commit.get_parent()
1266             if log_parent:
1267                 parents = [log_parent]
1268             else:
1269                 parents = []
1270         else:
1271             # generate a new log entry
1272             replace = False
1273             msg = '%s\t%s' % (message, top.get_id_hash())
1274             if old_log:
1275                 parents = [old_log]
1276             else:
1277                 parents = []
1278
1279         if notes:
1280             msg += '\n\n' + notes
1281
1282         log = git.commit(message = msg, parents = parents,
1283                          cache_update = False, tree_id = top.get_tree(),
1284                          allowempty = True)
1285         patch.set_log(log)
1286
1287     def hide_patch(self, name):
1288         """Add the patch to the hidden list.
1289         """
1290         unapplied = self.get_unapplied()
1291         if name not in unapplied:
1292             # keep the checking order for backward compatibility with
1293             # the old hidden patches functionality
1294             if self.patch_applied(name):
1295                 raise StackException, 'Cannot hide applied patch "%s"' % name
1296             elif self.patch_hidden(name):
1297                 raise StackException, 'Patch "%s" already hidden' % name
1298             else:
1299                 raise StackException, 'Unknown patch "%s"' % name
1300
1301         if not self.patch_hidden(name):
1302             # check needed for backward compatibility with the old
1303             # hidden patches functionality
1304             append_string(self.__hidden_file, name)
1305
1306         unapplied.remove(name)
1307         write_strings(self.__unapplied_file, unapplied)
1308
1309     def unhide_patch(self, name):
1310         """Remove the patch from the hidden list.
1311         """
1312         hidden = self.get_hidden()
1313         if not name in hidden:
1314             if self.patch_applied(name) or self.patch_unapplied(name):
1315                 raise StackException, 'Patch "%s" not hidden' % name
1316             else:
1317                 raise StackException, 'Unknown patch "%s"' % name
1318
1319         hidden.remove(name)
1320         write_strings(self.__hidden_file, hidden)
1321
1322         if not self.patch_applied(name) and not self.patch_unapplied(name):
1323             # check needed for backward compatibility with the old
1324             # hidden patches functionality
1325             append_string(self.__unapplied_file, name)