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