chiark / gitweb /
2d3bd22b57e98a36639f97d273e9cabef3648b33
[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
22
23 from stgit.utils import *
24 from stgit import git
25 from stgit.config import config
26
27
28 # stack exception class
29 class StackException(Exception):
30     pass
31
32 class FilterUntil:
33     def __init__(self):
34         self.should_print = True
35     def __call__(self, x, until_test, prefix):
36         if until_test(x):
37             self.should_print = False
38         if self.should_print:
39             return x[0:len(prefix)] != prefix
40         return False
41
42 #
43 # Functions
44 #
45 __comment_prefix = 'STG:'
46 __patch_prefix = 'STG_PATCH:'
47
48 def __clean_comments(f):
49     """Removes lines marked for status in a commit file
50     """
51     f.seek(0)
52
53     # remove status-prefixed lines
54     lines = f.readlines()
55
56     patch_filter = FilterUntil()
57     until_test = lambda t: t == (__patch_prefix + '\n')
58     lines = [l for l in lines if patch_filter(l, until_test, __comment_prefix)]
59
60     # remove empty lines at the end
61     while len(lines) != 0 and lines[-1] == '\n':
62         del lines[-1]
63
64     f.seek(0); f.truncate()
65     f.writelines(lines)
66
67 def edit_file(series, string, comment, show_patch = True):
68     fname = '.stgit.msg'
69     tmpl = os.path.join(git.base_dir, 'patchdescr.tmpl')
70
71     f = file(fname, 'w+')
72     if string:
73         print >> f, string
74     elif os.path.isfile(tmpl):
75         print >> f, file(tmpl).read().rstrip()
76     else:
77         print >> f
78     print >> f, __comment_prefix, comment
79     print >> f, __comment_prefix, \
80           'Lines prefixed with "%s" will be automatically removed.' \
81           % __comment_prefix
82     print >> f, __comment_prefix, \
83           'Trailing empty lines will be automatically removed.'
84
85     if show_patch:
86        print >> f, __patch_prefix
87        # series.get_patch(series.get_current()).get_top()
88        git.diff([], series.get_patch(series.get_current()).get_bottom(), None, f)
89
90     #Vim modeline must be near the end.
91     print >> f, __comment_prefix, 'vi: set textwidth=75 filetype=diff:'
92     f.close()
93
94     # the editor
95     if config.has_option('stgit', 'editor'):
96         editor = config.get('stgit', 'editor')
97     elif 'EDITOR' in os.environ:
98         editor = os.environ['EDITOR']
99     else:
100         editor = 'vi'
101     editor += ' %s' % fname
102
103     print 'Invoking the editor: "%s"...' % editor,
104     sys.stdout.flush()
105     print 'done (exit code: %d)' % os.system(editor)
106
107     f = file(fname, 'r+')
108
109     __clean_comments(f)
110     f.seek(0)
111     string = f.read()
112
113     f.close()
114     os.remove(fname)
115
116     return string
117
118 #
119 # Classes
120 #
121
122 class Patch:
123     """Basic patch implementation
124     """
125     def __init__(self, name, patch_dir):
126         self.__patch_dir = patch_dir
127         self.__name = name
128         self.__dir = os.path.join(self.__patch_dir, self.__name)
129
130     def create(self):
131         os.mkdir(self.__dir)
132         create_empty_file(os.path.join(self.__dir, 'bottom'))
133         create_empty_file(os.path.join(self.__dir, 'top'))
134
135     def delete(self):
136         for f in os.listdir(self.__dir):
137             os.remove(os.path.join(self.__dir, f))
138         os.rmdir(self.__dir)
139
140     def get_name(self):
141         return self.__name
142
143     def rename(self, newname):
144         olddir = self.__dir
145         self.__name = newname
146         self.__dir = os.path.join(self.__patch_dir, self.__name)
147
148         os.rename(olddir, self.__dir)
149
150     def __get_field(self, name, multiline = False):
151         id_file = os.path.join(self.__dir, name)
152         if os.path.isfile(id_file):
153             string = read_string(id_file, multiline)
154             if string == '':
155                 return None
156             else:
157                 return string
158         else:
159             return None
160
161     def __set_field(self, name, string, multiline = False):
162         fname = os.path.join(self.__dir, name)
163         if string and string != '':
164             write_string(fname, string, multiline)
165         elif os.path.isfile(fname):
166             os.remove(fname)
167
168     def get_bottom(self):
169         return self.__get_field('bottom')
170
171     def set_bottom(self, string, backup = False):
172         if backup:
173             self.__set_field('bottom.old', self.__get_field('bottom'))
174         self.__set_field('bottom', string)
175
176     def get_top(self):
177         return self.__get_field('top')
178
179     def set_top(self, string, backup = False):
180         if backup:
181             self.__set_field('top.old', self.__get_field('top'))
182         self.__set_field('top', string)
183
184     def restore_old_boundaries(self):
185         bottom = self.__get_field('bottom.old')
186         top = self.__get_field('top.old')
187
188         if top and bottom:
189             self.__set_field('bottom', bottom)
190             self.__set_field('top', top)
191         else:
192             raise StackException, 'No patch undo information'
193
194     def get_description(self):
195         return self.__get_field('description', True)
196
197     def set_description(self, string):
198         self.__set_field('description', string, True)
199
200     def get_authname(self):
201         return self.__get_field('authname')
202
203     def set_authname(self, string):
204         if not string and config.has_option('stgit', 'authname'):
205             string = config.get('stgit', 'authname')
206         self.__set_field('authname', string)
207
208     def get_authemail(self):
209         return self.__get_field('authemail')
210
211     def set_authemail(self, string):
212         if not string and config.has_option('stgit', 'authemail'):
213             string = config.get('stgit', 'authemail')
214         self.__set_field('authemail', string)
215
216     def get_authdate(self):
217         return self.__get_field('authdate')
218
219     def set_authdate(self, string):
220         self.__set_field('authdate', string)
221
222     def get_commname(self):
223         return self.__get_field('commname')
224
225     def set_commname(self, string):
226         if not string and config.has_option('stgit', 'commname'):
227             string = config.get('stgit', 'commname')
228         self.__set_field('commname', string)
229
230     def get_commemail(self):
231         return self.__get_field('commemail')
232
233     def set_commemail(self, string):
234         if not string and config.has_option('stgit', 'commemail'):
235             string = config.get('stgit', 'commemail')
236         self.__set_field('commemail', string)
237
238
239 class Series:
240     """Class including the operations on series
241     """
242     def __init__(self, name = None):
243         """Takes a series name as the parameter. A valid .git/patches/name
244         directory should exist
245         """
246         if name:
247             self.__name = name
248         else:
249             self.__name = git.get_head_file()
250
251         if self.__name:
252             self.__patch_dir = os.path.join(git.base_dir, 'patches',
253                                             self.__name)
254             self.__base_file = os.path.join(git.base_dir, 'refs', 'bases',
255                                             self.__name)
256             self.__applied_file = os.path.join(self.__patch_dir, 'applied')
257             self.__unapplied_file = os.path.join(self.__patch_dir, 'unapplied')
258             self.__current_file = os.path.join(self.__patch_dir, 'current')
259
260     def get_branch(self):
261         """Return the branch name for the Series object
262         """
263         return self.__name
264
265     def __set_current(self, name):
266         """Sets the topmost patch
267         """
268         if name:
269             write_string(self.__current_file, name)
270         else:
271             create_empty_file(self.__current_file)
272
273     def get_patch(self, name):
274         """Return a Patch object for the given name
275         """
276         return Patch(name, self.__patch_dir)
277
278     def get_current(self):
279         """Return a Patch object representing the topmost patch
280         """
281         if os.path.isfile(self.__current_file):
282             name = read_string(self.__current_file)
283         else:
284             return None
285         if name == '':
286             return None
287         else:
288             return name
289
290     def get_applied(self):
291         f = file(self.__applied_file)
292         names = [line.strip() for line in f.readlines()]
293         f.close()
294         return names
295
296     def get_unapplied(self):
297         f = file(self.__unapplied_file)
298         names = [line.strip() for line in f.readlines()]
299         f.close()
300         return names
301
302     def get_base_file(self):
303         return self.__base_file
304
305     def __patch_is_current(self, patch):
306         return patch.get_name() == read_string(self.__current_file)
307
308     def __patch_applied(self, name):
309         """Return true if the patch exists in the applied list
310         """
311         return name in self.get_applied()
312
313     def __patch_unapplied(self, name):
314         """Return true if the patch exists in the unapplied list
315         """
316         return name in self.get_unapplied()
317
318     def __begin_stack_check(self):
319         """Save the current HEAD into .git/refs/heads/base if the stack
320         is empty
321         """
322         if len(self.get_applied()) == 0:
323             head = git.get_head()
324             write_string(self.__base_file, head)
325
326     def __end_stack_check(self):
327         """Remove .git/refs/heads/base if the stack is empty.
328         This warning should never happen
329         """
330         if len(self.get_applied()) == 0 \
331            and read_string(self.__base_file) != git.get_head():
332             print 'Warning: stack empty but the HEAD and base are different'
333
334     def head_top_equal(self):
335         """Return true if the head and the top are the same
336         """
337         crt = self.get_current()
338         if not crt:
339             # we don't care, no patches applied
340             return True
341         return git.get_head() == Patch(crt, self.__patch_dir).get_top()
342
343     def init(self):
344         """Initialises the stgit series
345         """
346         bases_dir = os.path.join(git.base_dir, 'refs', 'bases')
347
348         if os.path.isdir(self.__patch_dir):
349             raise StackException, self.__patch_dir + ' already exists'
350         os.makedirs(self.__patch_dir)
351
352         if not os.path.isdir(bases_dir):
353             os.makedirs(bases_dir)
354
355         create_empty_file(self.__applied_file)
356         create_empty_file(self.__unapplied_file)
357         self.__begin_stack_check()
358
359     def refresh_patch(self, message = None, edit = False, show_patch = False,
360                       cache_update = True,
361                       author_name = None, author_email = None,
362                       author_date = None,
363                       committer_name = None, committer_email = None,
364                       commit_only = False):
365         """Generates a new commit for the given patch
366         """
367         name = self.get_current()
368         if not name:
369             raise StackException, 'No patches applied'
370
371         patch = Patch(name, self.__patch_dir)
372
373         descr = patch.get_description()
374         if not (message or descr):
375             edit = True
376             descr = ''
377         elif message:
378             descr = message
379
380         if not message and edit:
381             descr = edit_file(self, descr.rstrip(), \
382                               'Please edit the description for patch "%s" ' \
383                               'above.' % name, show_patch)
384
385         if not author_name:
386             author_name = patch.get_authname()
387         if not author_email:
388             author_email = patch.get_authemail()
389         if not author_date:
390             author_date = patch.get_authdate()
391         if not committer_name:
392             committer_name = patch.get_commname()
393         if not committer_email:
394             committer_email = patch.get_commemail()
395
396         commit_id = git.commit(message = descr, parents = [patch.get_bottom()],
397                                cache_update = cache_update,
398                                allowempty = True,
399                                author_name = author_name,
400                                author_email = author_email,
401                                author_date = author_date,
402                                committer_name = committer_name,
403                                committer_email = committer_email)
404
405         if not commit_only:
406             patch.set_top(commit_id)
407             patch.set_description(descr)
408             patch.set_authname(author_name)
409             patch.set_authemail(author_email)
410             patch.set_authdate(author_date)
411             patch.set_commname(committer_name)
412             patch.set_commemail(committer_email)
413
414         return commit_id
415
416     def new_patch(self, name, message = None, can_edit = True,
417                   unapplied = False, show_patch = False,
418                   top = None, bottom = None,
419                   author_name = None, author_email = None, author_date = None,
420                   committer_name = None, committer_email = None):
421         """Creates a new patch
422         """
423         if self.__patch_applied(name) or self.__patch_unapplied(name):
424             raise StackException, 'Patch "%s" already exists' % name
425
426         if not message and can_edit:
427             descr = edit_file(self, None, \
428                               'Please enter the description for patch "%s" ' \
429                               'above.' % name, show_patch)
430         else:
431             descr = message
432
433         head = git.get_head()
434
435         self.__begin_stack_check()
436
437         patch = Patch(name, self.__patch_dir)
438         patch.create()
439
440         if bottom:
441             patch.set_bottom(bottom)
442         else:
443             patch.set_bottom(head)
444         if top:
445             patch.set_top(top)
446         else:
447             patch.set_top(head)
448
449         patch.set_description(descr)
450         patch.set_authname(author_name)
451         patch.set_authemail(author_email)
452         patch.set_authdate(author_date)
453         patch.set_commname(committer_name)
454         patch.set_commemail(committer_email)
455
456         if unapplied:
457             patches = [patch.get_name()] + self.get_unapplied()
458
459             f = file(self.__unapplied_file, 'w+')
460             f.writelines([line + '\n' for line in patches])
461             f.close()
462         else:
463             append_string(self.__applied_file, patch.get_name())
464             self.__set_current(name)
465
466     def delete_patch(self, name):
467         """Deletes a patch
468         """
469         patch = Patch(name, self.__patch_dir)
470
471         if self.__patch_is_current(patch):
472             self.pop_patch(name)
473         elif self.__patch_applied(name):
474             raise StackException, 'Cannot remove an applied patch, "%s", ' \
475                   'which is not current' % name
476         elif not name in self.get_unapplied():
477             raise StackException, 'Unknown patch "%s"' % name
478
479         patch.delete()
480
481         unapplied = self.get_unapplied()
482         unapplied.remove(name)
483         f = file(self.__unapplied_file, 'w+')
484         f.writelines([line + '\n' for line in unapplied])
485         f.close()
486
487     def forward_patches(self, names):
488         """Try to fast-forward an array of patches.
489
490         On return, patches in names[0:returned_value] have been pushed on the
491         stack. Apply the rest with push_patch
492         """
493         unapplied = self.get_unapplied()
494         self.__begin_stack_check()
495
496         forwarded = 0
497         top = git.get_head()
498
499         for name in names:
500             assert(name in unapplied)
501
502             patch = Patch(name, self.__patch_dir)
503
504             head = top
505             bottom = patch.get_bottom()
506             top = patch.get_top()
507
508             # top != bottom always since we have a commit for each patch
509             if head == bottom:
510                 # reset the backup information
511                 patch.set_bottom(bottom, backup = True)
512                 patch.set_top(top, backup = True)
513
514             else:
515                 top = head
516                 # stop the fast-forwarding, must do a real merge
517                 break
518
519             forwarded+=1
520             unapplied.remove(name)
521
522         git.switch(top)
523
524         append_strings(self.__applied_file, names[0:forwarded])
525
526         f = file(self.__unapplied_file, 'w+')
527         f.writelines([line + '\n' for line in unapplied])
528         f.close()
529
530         self.__set_current(name)
531
532         return forwarded
533
534     def push_patch(self, name):
535         """Pushes a patch on the stack
536         """
537         unapplied = self.get_unapplied()
538         assert(name in unapplied)
539
540         self.__begin_stack_check()
541
542         patch = Patch(name, self.__patch_dir)
543
544         head = git.get_head()
545         bottom = patch.get_bottom()
546         top = patch.get_top()
547
548         ex = None
549
550         # top != bottom always since we have a commit for each patch
551         if head == bottom:
552             # reset the backup information
553             patch.set_bottom(bottom, backup = True)
554             patch.set_top(top, backup = True)
555
556             git.switch(top)
557         else:
558             # new patch needs to be refreshed.
559             # The current patch is empty after merge.
560             patch.set_bottom(head, backup = True)
561             patch.set_top(head, backup = True)
562             # merge/refresh can fail but the patch needs to be pushed
563             try:
564                 git.merge(bottom, head, top)
565             except git.GitException, ex:
566                 print >> sys.stderr, \
567                       'The merge failed during "push". ' \
568                       'Use "refresh" after fixing the conflicts'
569                 pass
570
571         append_string(self.__applied_file, name)
572
573         unapplied.remove(name)
574         f = file(self.__unapplied_file, 'w+')
575         f.writelines([line + '\n' for line in unapplied])
576         f.close()
577
578         self.__set_current(name)
579
580         # head == bottom case doesn't need to refresh the patch
581         if head != bottom:
582             if not ex:
583                 # if the merge was OK and no conflicts, just refresh the patch
584                 # The GIT cache was already updated by the merge operation
585                 self.refresh_patch(cache_update = False)
586             else:
587                 raise StackException, str(ex)
588
589     def undo_push(self):
590         name = self.get_current()
591         assert(name)
592
593         patch = Patch(name, self.__patch_dir)
594         git.reset()
595         self.pop_patch(name)
596         patch.restore_old_boundaries()
597
598     def pop_patch(self, name):
599         """Pops the top patch from the stack
600         """
601         applied = self.get_applied()
602         applied.reverse()
603         assert(name in applied)
604
605         patch = Patch(name, self.__patch_dir)
606
607         git.switch(patch.get_bottom())
608
609         # save the new applied list
610         idx = applied.index(name) + 1
611
612         popped = applied[:idx]
613         popped.reverse()
614         unapplied = popped + self.get_unapplied()
615
616         f = file(self.__unapplied_file, 'w+')
617         f.writelines([line + '\n' for line in unapplied])
618         f.close()
619
620         del applied[:idx]
621         applied.reverse()
622
623         f = file(self.__applied_file, 'w+')
624         f.writelines([line + '\n' for line in applied])
625         f.close()
626
627         if applied == []:
628             self.__set_current(None)
629         else:
630             self.__set_current(applied[-1])
631
632         self.__end_stack_check()
633
634     def empty_patch(self, name):
635         """Returns True if the patch is empty
636         """
637         patch = Patch(name, self.__patch_dir)
638         bottom = patch.get_bottom()
639         top = patch.get_top()
640
641         if bottom == top:
642             return True
643         elif git.get_commit(top).get_tree() \
644                  == git.get_commit(bottom).get_tree():
645             return True
646
647         return False
648
649     def rename_patch(self, oldname, newname):
650         applied = self.get_applied()
651         unapplied = self.get_unapplied()
652
653         if newname in applied or newname in unapplied:
654             raise StackException, 'Patch "%s" already exists' % newname
655
656         if oldname in unapplied:
657             Patch(oldname, self.__patch_dir).rename(newname)
658             unapplied[unapplied.index(oldname)] = newname
659
660             f = file(self.__unapplied_file, 'w+')
661             f.writelines([line + '\n' for line in unapplied])
662             f.close()
663         elif oldname in applied:
664             Patch(oldname, self.__patch_dir).rename(newname)
665             if oldname == self.get_current():
666                 self.__set_current(newname)
667
668             applied[applied.index(oldname)] = newname
669
670             f = file(self.__applied_file, 'w+')
671             f.writelines([line + '\n' for line in applied])
672             f.close()
673         else:
674             raise StackException, 'Unknown patch "%s"' % oldname