Use a separate directory for patches under each branch subdir
[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, line, comment, show_patch = True):
68 fname = '.stgit.msg'
69 tmpl = os.path.join(git.get_base_dir(), 'patchdescr.tmpl')
70
71 f = file(fname, 'w+')
72 if line:
73 print >> f, line
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 nobackup:'
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 result = f.read()
112
113 f.close()
114 os.remove(fname)
115
116 return result
117
118 #
119 # Classes
120 #
121
122 class Patch:
123 """Basic patch implementation
124 """
125 def __init__(self, name, series_dir):
126 self.__series_dir = series_dir
127 self.__name = name
128 self.__dir = os.path.join(self.__series_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.__series_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 line = read_string(id_file, multiline)
154 if line == '':
155 return None
156 else:
157 return line
158 else:
159 return None
160
161 def __set_field(self, name, value, multiline = False):
162 fname = os.path.join(self.__dir, name)
163 if value and value != '':
164 write_string(fname, value, multiline)
165 elif os.path.isfile(fname):
166 os.remove(fname)
167
168 def get_old_bottom(self):
169 return self.__get_field('bottom.old')
170
171 def get_bottom(self):
172 return self.__get_field('bottom')
173
174 def set_bottom(self, value, backup = False):
175 if backup:
176 curr = self.__get_field('bottom')
177 if curr != value:
178 self.__set_field('bottom.old', curr)
179 else:
180 self.__set_field('bottom.old', None)
181 self.__set_field('bottom', value)
182
183 def get_old_top(self):
184 return self.__get_field('top.old')
185
186 def get_top(self):
187 return self.__get_field('top')
188
189 def set_top(self, value, backup = False):
190 if backup:
191 curr = self.__get_field('top')
192 if curr != value:
193 self.__set_field('top.old', curr)
194 else:
195 self.__set_field('top.old', None)
196 self.__set_field('top', value)
197
198 def restore_old_boundaries(self):
199 bottom = self.__get_field('bottom.old')
200 top = self.__get_field('top.old')
201
202 if top and bottom:
203 self.__set_field('bottom', bottom)
204 self.__set_field('top', top)
205 return True
206 else:
207 return False
208
209 def get_description(self):
210 return self.__get_field('description', True)
211
212 def set_description(self, line):
213 self.__set_field('description', line, True)
214
215 def get_authname(self):
216 return self.__get_field('authname')
217
218 def set_authname(self, name):
219 if not name and config.has_option('stgit', 'authname'):
220 name = config.get('stgit', 'authname')
221 self.__set_field('authname', name)
222
223 def get_authemail(self):
224 return self.__get_field('authemail')
225
226 def set_authemail(self, address):
227 if not address and config.has_option('stgit', 'authemail'):
228 address = config.get('stgit', 'authemail')
229 self.__set_field('authemail', address)
230
231 def get_authdate(self):
232 return self.__get_field('authdate')
233
234 def set_authdate(self, authdate):
235 self.__set_field('authdate', authdate)
236
237 def get_commname(self):
238 return self.__get_field('commname')
239
240 def set_commname(self, name):
241 if not name and config.has_option('stgit', 'commname'):
242 name = config.get('stgit', 'commname')
243 self.__set_field('commname', name)
244
245 def get_commemail(self):
246 return self.__get_field('commemail')
247
248 def set_commemail(self, address):
249 if not address and config.has_option('stgit', 'commemail'):
250 address = config.get('stgit', 'commemail')
251 self.__set_field('commemail', address)
252
253
254 class Series:
255 """Class including the operations on series
256 """
257 def __init__(self, name = None):
258 """Takes a series name as the parameter.
259 """
260 try:
261 if name:
262 self.__name = name
263 else:
264 self.__name = git.get_head_file()
265 base_dir = git.get_base_dir()
266 except git.GitException, ex:
267 raise StackException, 'GIT tree not initialised: %s' % ex
268
269 self.__series_dir = os.path.join(base_dir, 'patches',
270 self.__name)
271 self.__base_file = os.path.join(base_dir, 'refs', 'bases',
272 self.__name)
273
274 self.__applied_file = os.path.join(self.__series_dir, 'applied')
275 self.__unapplied_file = os.path.join(self.__series_dir, 'unapplied')
276 self.__current_file = os.path.join(self.__series_dir, 'current')
277 self.__descr_file = os.path.join(self.__series_dir, 'description')
278
279 # where this series keeps its patches
280 self.__patch_dir = os.path.join(self.__series_dir, 'patches')
281 if not os.path.isdir(self.__patch_dir):
282 self.__patch_dir = self.__series_dir
283
284 def get_branch(self):
285 """Return the branch name for the Series object
286 """
287 return self.__name
288
289 def __set_current(self, name):
290 """Sets the topmost patch
291 """
292 if name:
293 write_string(self.__current_file, name)
294 else:
295 create_empty_file(self.__current_file)
296
297 def get_patch(self, name):
298 """Return a Patch object for the given name
299 """
300 return Patch(name, self.__patch_dir)
301
302 def get_current(self):
303 """Return a Patch object representing the topmost patch
304 """
305 if os.path.isfile(self.__current_file):
306 name = read_string(self.__current_file)
307 else:
308 return None
309 if name == '':
310 return None
311 else:
312 return name
313
314 def get_applied(self):
315 if not os.path.isfile(self.__applied_file):
316 raise StackException, 'Branch "%s" not initialised' % self.__name
317 f = file(self.__applied_file)
318 names = [line.strip() for line in f.readlines()]
319 f.close()
320 return names
321
322 def get_unapplied(self):
323 if not os.path.isfile(self.__unapplied_file):
324 raise StackException, 'Branch "%s" not initialised' % self.__name
325 f = file(self.__unapplied_file)
326 names = [line.strip() for line in f.readlines()]
327 f.close()
328 return names
329
330 def get_base_file(self):
331 return self.__base_file
332
333 def get_protected(self):
334 return os.path.isfile(os.path.join(self.__series_dir, 'protected'))
335
336 def protect(self):
337 protect_file = os.path.join(self.__series_dir, 'protected')
338 if not os.path.isfile(protect_file):
339 create_empty_file(protect_file)
340
341 def unprotect(self):
342 protect_file = os.path.join(self.__series_dir, 'protected')
343 if os.path.isfile(protect_file):
344 os.remove(protect_file)
345
346 def get_description(self):
347 if os.path.isfile(self.__descr_file):
348 return read_string(self.__descr_file)
349 else:
350 return ''
351
352 def __patch_is_current(self, patch):
353 return patch.get_name() == read_string(self.__current_file)
354
355 def __patch_applied(self, name):
356 """Return true if the patch exists in the applied list
357 """
358 return name in self.get_applied()
359
360 def __patch_unapplied(self, name):
361 """Return true if the patch exists in the unapplied list
362 """
363 return name in self.get_unapplied()
364
365 def __begin_stack_check(self):
366 """Save the current HEAD into .git/refs/heads/base if the stack
367 is empty
368 """
369 if len(self.get_applied()) == 0:
370 head = git.get_head()
371 write_string(self.__base_file, head)
372
373 def __end_stack_check(self):
374 """Remove .git/refs/heads/base if the stack is empty.
375 This warning should never happen
376 """
377 if len(self.get_applied()) == 0 \
378 and read_string(self.__base_file) != git.get_head():
379 print 'Warning: stack empty but the HEAD and base are different'
380
381 def head_top_equal(self):
382 """Return true if the head and the top are the same
383 """
384 crt = self.get_current()
385 if not crt:
386 # we don't care, no patches applied
387 return True
388 return git.get_head() == Patch(crt, self.__patch_dir).get_top()
389
390 def is_initialised(self):
391 """Checks if series is already initialised
392 """
393 return os.path.isdir(self.__patch_dir)
394
395 def init(self):
396 """Initialises the stgit series
397 """
398 bases_dir = os.path.join(git.get_base_dir(), 'refs', 'bases')
399
400 if self.is_initialised():
401 raise StackException, self.__patch_dir + ' already exists'
402 os.makedirs(self.__patch_dir)
403
404 if not os.path.isdir(bases_dir):
405 os.makedirs(bases_dir)
406
407 create_empty_file(self.__applied_file)
408 create_empty_file(self.__unapplied_file)
409 create_empty_file(self.__descr_file)
410 os.makedirs(os.path.join(self.__series_dir, 'patches'))
411 self.__begin_stack_check()
412
413 def rename(self, to_name):
414 """Renames a series
415 """
416 to_stack = Series(to_name)
417
418 if to_stack.is_initialised():
419 raise StackException, '"%s" already exists' % to_stack.get_branch()
420 if os.path.exists(to_stack.__base_file):
421 os.remove(to_stack.__base_file)
422
423 git.rename_branch(self.__name, to_name)
424
425 if os.path.isdir(self.__series_dir):
426 os.rename(self.__series_dir, to_stack.__series_dir)
427 if os.path.exists(self.__base_file):
428 os.rename(self.__base_file, to_stack.__base_file)
429
430 self.__init__(to_name)
431
432 def clone(self, target_series):
433 """Clones a series
434 """
435 base = read_string(self.get_base_file())
436 git.create_branch(target_series, tree_id = base)
437 Series(target_series).init()
438 new_series = Series(target_series)
439
440 # generate an artificial description file
441 write_string(new_series.__descr_file, 'clone of "%s"' % self.__name)
442
443 # clone self's entire series as unapplied patches
444 patches = self.get_applied() + self.get_unapplied()
445 patches.reverse()
446 for p in patches:
447 patch = self.get_patch(p)
448 new_series.new_patch(p, message = patch.get_description(),
449 can_edit = False, unapplied = True,
450 bottom = patch.get_bottom(),
451 top = patch.get_top(),
452 author_name = patch.get_authname(),
453 author_email = patch.get_authemail(),
454 author_date = patch.get_authdate())
455
456 # fast forward the cloned series to self's top
457 new_series.forward_patches(self.get_applied())
458
459 def delete(self, force = False):
460 """Deletes an stgit series
461 """
462 if self.is_initialised():
463 patches = self.get_unapplied() + self.get_applied()
464 if not force and patches:
465 raise StackException, \
466 'Cannot delete: the series still contains patches'
467 for p in patches:
468 Patch(p, self.__patch_dir).delete()
469
470 if os.path.exists(self.__applied_file):
471 os.remove(self.__applied_file)
472 if os.path.exists(self.__unapplied_file):
473 os.remove(self.__unapplied_file)
474 if os.path.exists(self.__current_file):
475 os.remove(self.__current_file)
476 if os.path.exists(self.__descr_file):
477 os.remove(self.__descr_file)
478 if not os.listdir(self.__patch_dir):
479 os.rmdir(self.__patch_dir)
480 else:
481 print 'Patch directory %s is not empty.' % self.__name
482 if not os.listdir(self.__series_dir):
483 os.rmdir(self.__series_dir)
484 else:
485 print 'Series directory %s is not empty.' % self.__name
486
487 if os.path.exists(self.__base_file):
488 os.remove(self.__base_file)
489
490 def refresh_patch(self, files = None, message = None, edit = False,
491 show_patch = False,
492 cache_update = True,
493 author_name = None, author_email = None,
494 author_date = None,
495 committer_name = None, committer_email = None):
496 """Generates a new commit for the given patch
497 """
498 name = self.get_current()
499 if not name:
500 raise StackException, 'No patches applied'
501
502 patch = Patch(name, self.__patch_dir)
503
504 descr = patch.get_description()
505 if not (message or descr):
506 edit = True
507 descr = ''
508 elif message:
509 descr = message
510
511 if not message and edit:
512 descr = edit_file(self, descr.rstrip(), \
513 'Please edit the description for patch "%s" ' \
514 'above.' % name, show_patch)
515
516 if not author_name:
517 author_name = patch.get_authname()
518 if not author_email:
519 author_email = patch.get_authemail()
520 if not author_date:
521 author_date = patch.get_authdate()
522 if not committer_name:
523 committer_name = patch.get_commname()
524 if not committer_email:
525 committer_email = patch.get_commemail()
526
527 commit_id = git.commit(files = files,
528 message = descr, parents = [patch.get_bottom()],
529 cache_update = cache_update,
530 allowempty = True,
531 author_name = author_name,
532 author_email = author_email,
533 author_date = author_date,
534 committer_name = committer_name,
535 committer_email = committer_email)
536
537 patch.set_top(commit_id)
538 patch.set_description(descr)
539 patch.set_authname(author_name)
540 patch.set_authemail(author_email)
541 patch.set_authdate(author_date)
542 patch.set_commname(committer_name)
543 patch.set_commemail(committer_email)
544
545 return commit_id
546
547 def new_patch(self, name, message = None, can_edit = True,
548 unapplied = False, show_patch = False,
549 top = None, bottom = None,
550 author_name = None, author_email = None, author_date = None,
551 committer_name = None, committer_email = None):
552 """Creates a new patch
553 """
554 if self.__patch_applied(name) or self.__patch_unapplied(name):
555 raise StackException, 'Patch "%s" already exists' % name
556
557 if not message and can_edit:
558 descr = edit_file(self, None, \
559 'Please enter the description for patch "%s" ' \
560 'above.' % name, show_patch)
561 else:
562 descr = message
563
564 head = git.get_head()
565
566 self.__begin_stack_check()
567
568 patch = Patch(name, self.__patch_dir)
569 patch.create()
570
571 if bottom:
572 patch.set_bottom(bottom)
573 else:
574 patch.set_bottom(head)
575 if top:
576 patch.set_top(top)
577 else:
578 patch.set_top(head)
579
580 patch.set_description(descr)
581 patch.set_authname(author_name)
582 patch.set_authemail(author_email)
583 patch.set_authdate(author_date)
584 patch.set_commname(committer_name)
585 patch.set_commemail(committer_email)
586
587 if unapplied:
588 patches = [patch.get_name()] + self.get_unapplied()
589
590 f = file(self.__unapplied_file, 'w+')
591 f.writelines([line + '\n' for line in patches])
592 f.close()
593 else:
594 append_string(self.__applied_file, patch.get_name())
595 self.__set_current(name)
596
597 def delete_patch(self, name):
598 """Deletes a patch
599 """
600 patch = Patch(name, self.__patch_dir)
601
602 if self.__patch_is_current(patch):
603 self.pop_patch(name)
604 elif self.__patch_applied(name):
605 raise StackException, 'Cannot remove an applied patch, "%s", ' \
606 'which is not current' % name
607 elif not name in self.get_unapplied():
608 raise StackException, 'Unknown patch "%s"' % name
609
610 patch.delete()
611
612 unapplied = self.get_unapplied()
613 unapplied.remove(name)
614 f = file(self.__unapplied_file, 'w+')
615 f.writelines([line + '\n' for line in unapplied])
616 f.close()
617
618 def forward_patches(self, names):
619 """Try to fast-forward an array of patches.
620
621 On return, patches in names[0:returned_value] have been pushed on the
622 stack. Apply the rest with push_patch
623 """
624 unapplied = self.get_unapplied()
625 self.__begin_stack_check()
626
627 forwarded = 0
628 top = git.get_head()
629
630 for name in names:
631 assert(name in unapplied)
632
633 patch = Patch(name, self.__patch_dir)
634
635 head = top
636 bottom = patch.get_bottom()
637 top = patch.get_top()
638
639 # top != bottom always since we have a commit for each patch
640 if head == bottom:
641 # reset the backup information
642 patch.set_bottom(head, backup = True)
643 patch.set_top(top, backup = True)
644
645 else:
646 head_tree = git.get_commit(head).get_tree()
647 bottom_tree = git.get_commit(bottom).get_tree()
648 if head_tree == bottom_tree:
649 # We must just reparent this patch and create a new commit
650 # for it
651 descr = patch.get_description()
652 author_name = patch.get_authname()
653 author_email = patch.get_authemail()
654 author_date = patch.get_authdate()
655 committer_name = patch.get_commname()
656 committer_email = patch.get_commemail()
657
658 top_tree = git.get_commit(top).get_tree()
659
660 top = git.commit(message = descr, parents = [head],
661 cache_update = False,
662 tree_id = top_tree,
663 allowempty = True,
664 author_name = author_name,
665 author_email = author_email,
666 author_date = author_date,
667 committer_name = committer_name,
668 committer_email = committer_email)
669
670 patch.set_bottom(head, backup = True)
671 patch.set_top(top, backup = True)
672 else:
673 top = head
674 # stop the fast-forwarding, must do a real merge
675 break
676
677 forwarded+=1
678 unapplied.remove(name)
679
680 if forwarded == 0:
681 return 0
682
683 git.switch(top)
684
685 append_strings(self.__applied_file, names[0:forwarded])
686
687 f = file(self.__unapplied_file, 'w+')
688 f.writelines([line + '\n' for line in unapplied])
689 f.close()
690
691 self.__set_current(name)
692
693 return forwarded
694
695 def push_patch(self, name):
696 """Pushes a patch on the stack
697 """
698 unapplied = self.get_unapplied()
699 assert(name in unapplied)
700
701 self.__begin_stack_check()
702
703 patch = Patch(name, self.__patch_dir)
704
705 head = git.get_head()
706 bottom = patch.get_bottom()
707 top = patch.get_top()
708
709 ex = None
710 modified = False
711
712 # top != bottom always since we have a commit for each patch
713 if head == bottom:
714 # reset the backup information
715 patch.set_bottom(bottom, backup = True)
716 patch.set_top(top, backup = True)
717
718 git.switch(top)
719 else:
720 # new patch needs to be refreshed.
721 # The current patch is empty after merge.
722 patch.set_bottom(head, backup = True)
723 patch.set_top(head, backup = True)
724
725 # Try the fast applying first. If this fails, fall back to the
726 # three-way merge
727 if not git.apply_diff(bottom, top):
728 # if git.apply_diff() fails, the patch requires a diff3
729 # merge and can be reported as modified
730 modified = True
731
732 # merge can fail but the patch needs to be pushed
733 try:
734 git.merge(bottom, head, top)
735 except git.GitException, ex:
736 print >> sys.stderr, \
737 'The merge failed during "push". ' \
738 'Use "refresh" after fixing the conflicts'
739
740 append_string(self.__applied_file, name)
741
742 unapplied.remove(name)
743 f = file(self.__unapplied_file, 'w+')
744 f.writelines([line + '\n' for line in unapplied])
745 f.close()
746
747 self.__set_current(name)
748
749 # head == bottom case doesn't need to refresh the patch
750 if head != bottom:
751 if not ex:
752 # if the merge was OK and no conflicts, just refresh the patch
753 # The GIT cache was already updated by the merge operation
754 self.refresh_patch(cache_update = False)
755 else:
756 raise StackException, str(ex)
757
758 return modified
759
760 def undo_push(self):
761 name = self.get_current()
762 assert(name)
763
764 patch = Patch(name, self.__patch_dir)
765 git.reset()
766 self.pop_patch(name)
767 return patch.restore_old_boundaries()
768
769 def pop_patch(self, name):
770 """Pops the top patch from the stack
771 """
772 applied = self.get_applied()
773 applied.reverse()
774 assert(name in applied)
775
776 patch = Patch(name, self.__patch_dir)
777
778 git.switch(patch.get_bottom())
779
780 # save the new applied list
781 idx = applied.index(name) + 1
782
783 popped = applied[:idx]
784 popped.reverse()
785 unapplied = popped + self.get_unapplied()
786
787 f = file(self.__unapplied_file, 'w+')
788 f.writelines([line + '\n' for line in unapplied])
789 f.close()
790
791 del applied[:idx]
792 applied.reverse()
793
794 f = file(self.__applied_file, 'w+')
795 f.writelines([line + '\n' for line in applied])
796 f.close()
797
798 if applied == []:
799 self.__set_current(None)
800 else:
801 self.__set_current(applied[-1])
802
803 self.__end_stack_check()
804
805 def empty_patch(self, name):
806 """Returns True if the patch is empty
807 """
808 patch = Patch(name, self.__patch_dir)
809 bottom = patch.get_bottom()
810 top = patch.get_top()
811
812 if bottom == top:
813 return True
814 elif git.get_commit(top).get_tree() \
815 == git.get_commit(bottom).get_tree():
816 return True
817
818 return False
819
820 def rename_patch(self, oldname, newname):
821 applied = self.get_applied()
822 unapplied = self.get_unapplied()
823
824 if oldname == newname:
825 raise StackException, '"To" name and "from" name are the same'
826
827 if newname in applied or newname in unapplied:
828 raise StackException, 'Patch "%s" already exists' % newname
829
830 if oldname in unapplied:
831 Patch(oldname, self.__patch_dir).rename(newname)
832 unapplied[unapplied.index(oldname)] = newname
833
834 f = file(self.__unapplied_file, 'w+')
835 f.writelines([line + '\n' for line in unapplied])
836 f.close()
837 elif oldname in applied:
838 Patch(oldname, self.__patch_dir).rename(newname)
839 if oldname == self.get_current():
840 self.__set_current(newname)
841
842 applied[applied.index(oldname)] = newname
843
844 f = file(self.__applied_file, 'w+')
845 f.writelines([line + '\n' for line in applied])
846 f.close()
847 else:
848 raise StackException, 'Unknown patch "%s"' % oldname