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 | ||
c1e4d7e0 | 21 | import sys, os, re |
41a6d859 CM |
22 | |
23 | from stgit.utils import * | |
5e888f30 | 24 | from stgit.out import * |
1f3bb017 | 25 | from stgit import git, basedir, templates |
41a6d859 | 26 | from stgit.config import config |
8fce9909 | 27 | from shutil import copyfile |
41a6d859 CM |
28 | |
29 | ||
30 | # stack exception class | |
31 | class StackException(Exception): | |
32 | pass | |
33 | ||
6ad48e48 PBG |
34 | class FilterUntil: |
35 | def __init__(self): | |
36 | self.should_print = True | |
37 | def __call__(self, x, until_test, prefix): | |
38 | if until_test(x): | |
39 | self.should_print = False | |
40 | if self.should_print: | |
41 | return x[0:len(prefix)] != prefix | |
42 | return False | |
43 | ||
41a6d859 CM |
44 | # |
45 | # Functions | |
46 | # | |
47 | __comment_prefix = 'STG:' | |
6ad48e48 | 48 | __patch_prefix = 'STG_PATCH:' |
41a6d859 CM |
49 | |
50 | def __clean_comments(f): | |
51 | """Removes lines marked for status in a commit file | |
52 | """ | |
53 | f.seek(0) | |
54 | ||
55 | # remove status-prefixed lines | |
6ad48e48 PBG |
56 | lines = f.readlines() |
57 | ||
58 | patch_filter = FilterUntil() | |
59 | until_test = lambda t: t == (__patch_prefix + '\n') | |
60 | lines = [l for l in lines if patch_filter(l, until_test, __comment_prefix)] | |
61 | ||
41a6d859 CM |
62 | # remove empty lines at the end |
63 | while len(lines) != 0 and lines[-1] == '\n': | |
64 | del lines[-1] | |
65 | ||
66 | f.seek(0); f.truncate() | |
67 | f.writelines(lines) | |
68 | ||
7cc615f3 | 69 | def edit_file(series, line, comment, show_patch = True): |
bd427e46 | 70 | fname = '.stgitmsg.txt' |
1f3bb017 | 71 | tmpl = templates.get_template('patchdescr.tmpl') |
41a6d859 CM |
72 | |
73 | f = file(fname, 'w+') | |
7cc615f3 CL |
74 | if line: |
75 | print >> f, line | |
1f3bb017 CM |
76 | elif tmpl: |
77 | print >> f, tmpl, | |
41a6d859 CM |
78 | else: |
79 | print >> f | |
80 | print >> f, __comment_prefix, comment | |
81 | print >> f, __comment_prefix, \ | |
82 | 'Lines prefixed with "%s" will be automatically removed.' \ | |
83 | % __comment_prefix | |
84 | print >> f, __comment_prefix, \ | |
85 | 'Trailing empty lines will be automatically removed.' | |
6ad48e48 PBG |
86 | |
87 | if show_patch: | |
88 | print >> f, __patch_prefix | |
89 | # series.get_patch(series.get_current()).get_top() | |
f1c5519a PR |
90 | diff_str = git.diff(rev1 = series.get_patch(series.get_current()).get_bottom()) |
91 | f.write(diff_str) | |
6ad48e48 PBG |
92 | |
93 | #Vim modeline must be near the end. | |
b83e37e0 | 94 | print >> f, __comment_prefix, 'vi: set textwidth=75 filetype=diff nobackup:' |
41a6d859 CM |
95 | f.close() |
96 | ||
83bb4e4c | 97 | call_editor(fname) |
41a6d859 CM |
98 | |
99 | f = file(fname, 'r+') | |
100 | ||
101 | __clean_comments(f) | |
102 | f.seek(0) | |
7cc615f3 | 103 | result = f.read() |
41a6d859 CM |
104 | |
105 | f.close() | |
106 | os.remove(fname) | |
107 | ||
7cc615f3 | 108 | return result |
41a6d859 CM |
109 | |
110 | # | |
111 | # Classes | |
112 | # | |
113 | ||
8fe7e9f0 YD |
114 | class StgitObject: |
115 | """An object with stgit-like properties stored as files in a directory | |
116 | """ | |
117 | def _set_dir(self, dir): | |
118 | self.__dir = dir | |
119 | def _dir(self): | |
120 | return self.__dir | |
121 | ||
122 | def create_empty_field(self, name): | |
123 | create_empty_file(os.path.join(self.__dir, name)) | |
124 | ||
125 | def _get_field(self, name, multiline = False): | |
126 | id_file = os.path.join(self.__dir, name) | |
127 | if os.path.isfile(id_file): | |
128 | line = read_string(id_file, multiline) | |
129 | if line == '': | |
130 | return None | |
131 | else: | |
132 | return line | |
133 | else: | |
134 | return None | |
135 | ||
136 | def _set_field(self, name, value, multiline = False): | |
137 | fname = os.path.join(self.__dir, name) | |
138 | if value and value != '': | |
139 | write_string(fname, value, multiline) | |
140 | elif os.path.isfile(fname): | |
141 | os.remove(fname) | |
142 | ||
143 | ||
144 | class Patch(StgitObject): | |
41a6d859 CM |
145 | """Basic patch implementation |
146 | """ | |
262d31dc KH |
147 | def __init_refs(self): |
148 | self.__top_ref = self.__refs_base + '/' + self.__name | |
149 | self.__log_ref = self.__top_ref + '.log' | |
150 | ||
151 | def __init__(self, name, series_dir, refs_base): | |
02ac3ad2 | 152 | self.__series_dir = series_dir |
41a6d859 | 153 | self.__name = name |
8fe7e9f0 | 154 | self._set_dir(os.path.join(self.__series_dir, self.__name)) |
262d31dc KH |
155 | self.__refs_base = refs_base |
156 | self.__init_refs() | |
41a6d859 CM |
157 | |
158 | def create(self): | |
8fe7e9f0 YD |
159 | os.mkdir(self._dir()) |
160 | self.create_empty_field('bottom') | |
161 | self.create_empty_field('top') | |
41a6d859 CM |
162 | |
163 | def delete(self): | |
8fe7e9f0 YD |
164 | for f in os.listdir(self._dir()): |
165 | os.remove(os.path.join(self._dir(), f)) | |
166 | os.rmdir(self._dir()) | |
262d31dc KH |
167 | git.delete_ref(self.__top_ref) |
168 | if git.ref_exists(self.__log_ref): | |
169 | git.delete_ref(self.__log_ref) | |
41a6d859 CM |
170 | |
171 | def get_name(self): | |
172 | return self.__name | |
173 | ||
e55b53e0 | 174 | def rename(self, newname): |
8fe7e9f0 | 175 | olddir = self._dir() |
262d31dc KH |
176 | old_top_ref = self.__top_ref |
177 | old_log_ref = self.__log_ref | |
e55b53e0 | 178 | self.__name = newname |
8fe7e9f0 | 179 | self._set_dir(os.path.join(self.__series_dir, self.__name)) |
262d31dc | 180 | self.__init_refs() |
e55b53e0 | 181 | |
262d31dc KH |
182 | git.rename_ref(old_top_ref, self.__top_ref) |
183 | if git.ref_exists(old_log_ref): | |
184 | git.rename_ref(old_log_ref, self.__log_ref) | |
8fe7e9f0 | 185 | os.rename(olddir, self._dir()) |
844a1640 CM |
186 | |
187 | def __update_top_ref(self, ref): | |
262d31dc | 188 | git.set_ref(self.__top_ref, ref) |
844a1640 | 189 | |
64354a2d | 190 | def __update_log_ref(self, ref): |
262d31dc | 191 | git.set_ref(self.__log_ref, ref) |
64354a2d | 192 | |
844a1640 CM |
193 | def update_top_ref(self): |
194 | top = self.get_top() | |
195 | if top: | |
196 | self.__update_top_ref(top) | |
e55b53e0 | 197 | |
54b09584 | 198 | def get_old_bottom(self): |
8fe7e9f0 | 199 | return self._get_field('bottom.old') |
54b09584 | 200 | |
41a6d859 | 201 | def get_bottom(self): |
8fe7e9f0 | 202 | return self._get_field('bottom') |
41a6d859 | 203 | |
7cc615f3 | 204 | def set_bottom(self, value, backup = False): |
41a6d859 | 205 | if backup: |
8fe7e9f0 YD |
206 | curr = self._get_field('bottom') |
207 | self._set_field('bottom.old', curr) | |
208 | self._set_field('bottom', value) | |
41a6d859 | 209 | |
54b09584 | 210 | def get_old_top(self): |
8fe7e9f0 | 211 | return self._get_field('top.old') |
54b09584 | 212 | |
41a6d859 | 213 | def get_top(self): |
8fe7e9f0 | 214 | return self._get_field('top') |
41a6d859 | 215 | |
7cc615f3 | 216 | def set_top(self, value, backup = False): |
41a6d859 | 217 | if backup: |
8fe7e9f0 YD |
218 | curr = self._get_field('top') |
219 | self._set_field('top.old', curr) | |
220 | self._set_field('top', value) | |
844a1640 | 221 | self.__update_top_ref(value) |
41a6d859 CM |
222 | |
223 | def restore_old_boundaries(self): | |
8fe7e9f0 YD |
224 | bottom = self._get_field('bottom.old') |
225 | top = self._get_field('top.old') | |
41a6d859 CM |
226 | |
227 | if top and bottom: | |
8fe7e9f0 YD |
228 | self._set_field('bottom', bottom) |
229 | self._set_field('top', top) | |
844a1640 | 230 | self.__update_top_ref(top) |
a5bbc44d | 231 | return True |
41a6d859 | 232 | else: |
a5bbc44d | 233 | return False |
41a6d859 CM |
234 | |
235 | def get_description(self): | |
8fe7e9f0 | 236 | return self._get_field('description', True) |
41a6d859 | 237 | |
7cc615f3 | 238 | def set_description(self, line): |
8fe7e9f0 | 239 | self._set_field('description', line, True) |
41a6d859 CM |
240 | |
241 | def get_authname(self): | |
8fe7e9f0 | 242 | return self._get_field('authname') |
41a6d859 | 243 | |
7cc615f3 | 244 | def set_authname(self, name): |
8fe7e9f0 | 245 | self._set_field('authname', name or git.author().name) |
41a6d859 CM |
246 | |
247 | def get_authemail(self): | |
8fe7e9f0 | 248 | return self._get_field('authemail') |
41a6d859 | 249 | |
9e3f506f | 250 | def set_authemail(self, email): |
8fe7e9f0 | 251 | self._set_field('authemail', email or git.author().email) |
41a6d859 CM |
252 | |
253 | def get_authdate(self): | |
8fe7e9f0 | 254 | return self._get_field('authdate') |
41a6d859 | 255 | |
4db741b1 | 256 | def set_authdate(self, date): |
8fe7e9f0 | 257 | self._set_field('authdate', date or git.author().date) |
41a6d859 CM |
258 | |
259 | def get_commname(self): | |
8fe7e9f0 | 260 | return self._get_field('commname') |
41a6d859 | 261 | |
7cc615f3 | 262 | def set_commname(self, name): |
8fe7e9f0 | 263 | self._set_field('commname', name or git.committer().name) |
41a6d859 CM |
264 | |
265 | def get_commemail(self): | |
8fe7e9f0 | 266 | return self._get_field('commemail') |
41a6d859 | 267 | |
9e3f506f | 268 | def set_commemail(self, email): |
8fe7e9f0 | 269 | self._set_field('commemail', email or git.committer().email) |
41a6d859 | 270 | |
64354a2d | 271 | def get_log(self): |
8fe7e9f0 | 272 | return self._get_field('log') |
64354a2d CM |
273 | |
274 | def set_log(self, value, backup = False): | |
8fe7e9f0 | 275 | self._set_field('log', value) |
64354a2d CM |
276 | self.__update_log_ref(value) |
277 | ||
598e9d3f KH |
278 | # The current StGIT metadata format version. |
279 | FORMAT_VERSION = 2 | |
280 | ||
47e24a74 | 281 | class PatchSet(StgitObject): |
dd1b8fcc YD |
282 | def __init__(self, name = None): |
283 | try: | |
284 | if name: | |
285 | self.set_name (name) | |
286 | else: | |
287 | self.set_name (git.get_head_file()) | |
288 | self.__base_dir = basedir.get() | |
289 | except git.GitException, ex: | |
290 | raise StackException, 'GIT tree not initialised: %s' % ex | |
291 | ||
292 | self._set_dir(os.path.join(self.__base_dir, 'patches', self.get_name())) | |
293 | ||
47e24a74 YD |
294 | def get_name(self): |
295 | return self.__name | |
296 | def set_name(self, name): | |
297 | self.__name = name | |
298 | ||
dd1b8fcc YD |
299 | def _basedir(self): |
300 | return self.__base_dir | |
301 | ||
47e24a74 YD |
302 | def get_head(self): |
303 | """Return the head of the branch | |
304 | """ | |
305 | crt = self.get_current_patch() | |
306 | if crt: | |
307 | return crt.get_top() | |
308 | else: | |
309 | return self.get_base() | |
310 | ||
311 | def get_protected(self): | |
312 | return os.path.isfile(os.path.join(self._dir(), 'protected')) | |
313 | ||
314 | def protect(self): | |
315 | protect_file = os.path.join(self._dir(), 'protected') | |
316 | if not os.path.isfile(protect_file): | |
317 | create_empty_file(protect_file) | |
318 | ||
319 | def unprotect(self): | |
320 | protect_file = os.path.join(self._dir(), 'protected') | |
321 | if os.path.isfile(protect_file): | |
322 | os.remove(protect_file) | |
323 | ||
324 | def __branch_descr(self): | |
325 | return 'branch.%s.description' % self.get_name() | |
326 | ||
327 | def get_description(self): | |
328 | return config.get(self.__branch_descr()) or '' | |
329 | ||
330 | def set_description(self, line): | |
331 | if line: | |
332 | config.set(self.__branch_descr(), line) | |
333 | else: | |
334 | config.unset(self.__branch_descr()) | |
335 | ||
336 | def head_top_equal(self): | |
337 | """Return true if the head and the top are the same | |
338 | """ | |
339 | crt = self.get_current_patch() | |
340 | if not crt: | |
341 | # we don't care, no patches applied | |
342 | return True | |
343 | return git.get_head() == crt.get_top() | |
344 | ||
345 | def is_initialised(self): | |
346 | """Checks if series is already initialised | |
347 | """ | |
9171769c | 348 | return bool(config.get(self.format_version_key())) |
47e24a74 YD |
349 | |
350 | ||
351 | class Series(PatchSet): | |
41a6d859 CM |
352 | """Class including the operations on series |
353 | """ | |
354 | def __init__(self, name = None): | |
40e65b92 | 355 | """Takes a series name as the parameter. |
41a6d859 | 356 | """ |
dd1b8fcc | 357 | PatchSet.__init__(self, name) |
598e9d3f KH |
358 | |
359 | # Update the branch to the latest format version if it is | |
360 | # initialized, but don't touch it if it isn't. | |
9171769c | 361 | self.update_to_current_format_version() |
598e9d3f | 362 | |
262d31dc | 363 | self.__refs_base = 'refs/patches/%s' % self.get_name() |
02ac3ad2 | 364 | |
8fe7e9f0 YD |
365 | self.__applied_file = os.path.join(self._dir(), 'applied') |
366 | self.__unapplied_file = os.path.join(self._dir(), 'unapplied') | |
841c7b2a | 367 | self.__hidden_file = os.path.join(self._dir(), 'hidden') |
02ac3ad2 CL |
368 | |
369 | # where this series keeps its patches | |
8fe7e9f0 | 370 | self.__patch_dir = os.path.join(self._dir(), 'patches') |
844a1640 | 371 | |
ac50371b | 372 | # trash directory |
8fe7e9f0 | 373 | self.__trash_dir = os.path.join(self._dir(), 'trash') |
ac50371b | 374 | |
9171769c | 375 | def format_version_key(self): |
69ffa22e | 376 | return 'branch.%s.stgit.stackformatversion' % self.get_name() |
9171769c YD |
377 | |
378 | def update_to_current_format_version(self): | |
379 | """Update a potentially older StGIT directory structure to the | |
380 | latest version. Note: This function should depend as little as | |
381 | possible on external functions that may change during a format | |
382 | version bump, since it must remain able to process older formats.""" | |
383 | ||
dd1b8fcc | 384 | branch_dir = os.path.join(self._basedir(), 'patches', self.get_name()) |
9171769c YD |
385 | def get_format_version(): |
386 | """Return the integer format version number, or None if the | |
387 | branch doesn't have any StGIT metadata at all, of any version.""" | |
388 | fv = config.get(self.format_version_key()) | |
69ffa22e | 389 | ofv = config.get('branch.%s.stgitformatversion' % self.get_name()) |
9171769c YD |
390 | if fv: |
391 | # Great, there's an explicitly recorded format version | |
392 | # number, which means that the branch is initialized and | |
393 | # of that exact version. | |
394 | return int(fv) | |
69ffa22e YD |
395 | elif ofv: |
396 | # Old name for the version info, upgrade it | |
397 | config.set(self.format_version_key(), ofv) | |
398 | config.unset('branch.%s.stgitformatversion' % self.get_name()) | |
399 | return int(ofv) | |
9171769c YD |
400 | elif os.path.isdir(os.path.join(branch_dir, 'patches')): |
401 | # There's a .git/patches/<branch>/patches dirctory, which | |
402 | # means this is an initialized version 1 branch. | |
403 | return 1 | |
404 | elif os.path.isdir(branch_dir): | |
405 | # There's a .git/patches/<branch> directory, which means | |
406 | # this is an initialized version 0 branch. | |
407 | return 0 | |
408 | else: | |
409 | # The branch doesn't seem to be initialized at all. | |
410 | return None | |
411 | def set_format_version(v): | |
412 | out.info('Upgraded branch %s to format version %d' % (self.get_name(), v)) | |
413 | config.set(self.format_version_key(), '%d' % v) | |
414 | def mkdir(d): | |
415 | if not os.path.isdir(d): | |
416 | os.makedirs(d) | |
417 | def rm(f): | |
418 | if os.path.exists(f): | |
419 | os.remove(f) | |
262d31dc KH |
420 | def rm_ref(ref): |
421 | if git.ref_exists(ref): | |
422 | git.delete_ref(ref) | |
9171769c YD |
423 | |
424 | # Update 0 -> 1. | |
425 | if get_format_version() == 0: | |
426 | mkdir(os.path.join(branch_dir, 'trash')) | |
427 | patch_dir = os.path.join(branch_dir, 'patches') | |
428 | mkdir(patch_dir) | |
262d31dc | 429 | refs_base = 'refs/patches/%s' % self.get_name() |
9171769c YD |
430 | for patch in (file(os.path.join(branch_dir, 'unapplied')).readlines() |
431 | + file(os.path.join(branch_dir, 'applied')).readlines()): | |
432 | patch = patch.strip() | |
433 | os.rename(os.path.join(branch_dir, patch), | |
434 | os.path.join(patch_dir, patch)) | |
262d31dc | 435 | Patch(patch, patch_dir, refs_base).update_top_ref() |
9171769c YD |
436 | set_format_version(1) |
437 | ||
438 | # Update 1 -> 2. | |
439 | if get_format_version() == 1: | |
440 | desc_file = os.path.join(branch_dir, 'description') | |
441 | if os.path.isfile(desc_file): | |
442 | desc = read_string(desc_file) | |
443 | if desc: | |
444 | config.set('branch.%s.description' % self.get_name(), desc) | |
445 | rm(desc_file) | |
446 | rm(os.path.join(branch_dir, 'current')) | |
262d31dc | 447 | rm_ref('refs/bases/%s' % self.get_name()) |
9171769c YD |
448 | set_format_version(2) |
449 | ||
450 | # Make sure we're at the latest version. | |
451 | if not get_format_version() in [None, FORMAT_VERSION]: | |
452 | raise StackException('Branch %s is at format version %d, expected %d' | |
453 | % (self.get_name(), get_format_version(), FORMAT_VERSION)) | |
454 | ||
c1e4d7e0 CM |
455 | def __patch_name_valid(self, name): |
456 | """Raise an exception if the patch name is not valid. | |
457 | """ | |
458 | if not name or re.search('[^\w.-]', name): | |
459 | raise StackException, 'Invalid patch name: "%s"' % name | |
460 | ||
41a6d859 CM |
461 | def get_patch(self, name): |
462 | """Return a Patch object for the given name | |
463 | """ | |
262d31dc | 464 | return Patch(name, self.__patch_dir, self.__refs_base) |
41a6d859 | 465 | |
4d0ba818 KH |
466 | def get_current_patch(self): |
467 | """Return a Patch object representing the topmost patch, or | |
468 | None if there is no such patch.""" | |
469 | crt = self.get_current() | |
470 | if not crt: | |
471 | return None | |
4c0dd299 | 472 | return self.get_patch(crt) |
4d0ba818 | 473 | |
41a6d859 | 474 | def get_current(self): |
4d0ba818 KH |
475 | """Return the name of the topmost patch, or None if there is |
476 | no such patch.""" | |
532cdf94 KH |
477 | try: |
478 | applied = self.get_applied() | |
479 | except StackException: | |
480 | # No "applied" file: branch is not initialized. | |
481 | return None | |
482 | try: | |
483 | return applied[-1] | |
484 | except IndexError: | |
485 | # No patches applied. | |
41a6d859 | 486 | return None |
41a6d859 CM |
487 | |
488 | def get_applied(self): | |
40e65b92 | 489 | if not os.path.isfile(self.__applied_file): |
d37ff079 | 490 | raise StackException, 'Branch "%s" not initialised' % self.get_name() |
17364282 | 491 | return read_strings(self.__applied_file) |
41a6d859 CM |
492 | |
493 | def get_unapplied(self): | |
40e65b92 | 494 | if not os.path.isfile(self.__unapplied_file): |
d37ff079 | 495 | raise StackException, 'Branch "%s" not initialised' % self.get_name() |
17364282 | 496 | return read_strings(self.__unapplied_file) |
41a6d859 | 497 | |
841c7b2a CM |
498 | def get_hidden(self): |
499 | if not os.path.isfile(self.__hidden_file): | |
500 | return [] | |
17364282 | 501 | return read_strings(self.__hidden_file) |
841c7b2a | 502 | |
ba66e579 | 503 | def get_base(self): |
16d69115 KH |
504 | # Return the parent of the bottommost patch, if there is one. |
505 | if os.path.isfile(self.__applied_file): | |
506 | bottommost = file(self.__applied_file).readline().strip() | |
507 | if bottommost: | |
508 | return self.get_patch(bottommost).get_bottom() | |
509 | # No bottommost patch, so just return HEAD | |
510 | return git.get_head() | |
ba66e579 | 511 | |
254d99f8 | 512 | def get_parent_remote(self): |
d37ff079 | 513 | value = config.get('branch.%s.remote' % self.get_name()) |
f72ad3d6 YD |
514 | if value: |
515 | return value | |
516 | elif 'origin' in git.remotes_list(): | |
27ac2b7e | 517 | out.note(('No parent remote declared for stack "%s",' |
d37ff079 | 518 | ' defaulting to "origin".' % self.get_name()), |
27ac2b7e | 519 | ('Consider setting "branch.%s.remote" and' |
82792b45 | 520 | ' "branch.%s.merge" with "git config".' |
d37ff079 | 521 | % (self.get_name(), self.get_name()))) |
f72ad3d6 YD |
522 | return 'origin' |
523 | else: | |
d37ff079 | 524 | raise StackException, 'Cannot find a parent remote for "%s"' % self.get_name() |
254d99f8 YD |
525 | |
526 | def __set_parent_remote(self, remote): | |
d37ff079 | 527 | value = config.set('branch.%s.remote' % self.get_name(), remote) |
254d99f8 | 528 | |
8866feda | 529 | def get_parent_branch(self): |
d37ff079 | 530 | value = config.get('branch.%s.stgit.parentbranch' % self.get_name()) |
8866feda YD |
531 | if value: |
532 | return value | |
533 | elif git.rev_parse('heads/origin'): | |
27ac2b7e | 534 | out.note(('No parent branch declared for stack "%s",' |
d37ff079 | 535 | ' defaulting to "heads/origin".' % self.get_name()), |
27ac2b7e | 536 | ('Consider setting "branch.%s.stgit.parentbranch"' |
82792b45 | 537 | ' with "git config".' % self.get_name())) |
8866feda YD |
538 | return 'heads/origin' |
539 | else: | |
d37ff079 | 540 | raise StackException, 'Cannot find a parent branch for "%s"' % self.get_name() |
8866feda YD |
541 | |
542 | def __set_parent_branch(self, name): | |
d37ff079 | 543 | if config.get('branch.%s.remote' % self.get_name()): |
4646e7a3 YD |
544 | # Never set merge if remote is not set to avoid |
545 | # possibly-erroneous lookups into 'origin' | |
d37ff079 YD |
546 | config.set('branch.%s.merge' % self.get_name(), name) |
547 | config.set('branch.%s.stgit.parentbranch' % self.get_name(), name) | |
8866feda YD |
548 | |
549 | def set_parent(self, remote, localbranch): | |
550 | if localbranch: | |
16881517 KH |
551 | if remote: |
552 | self.__set_parent_remote(remote) | |
8866feda | 553 | self.__set_parent_branch(localbranch) |
4646e7a3 YD |
554 | # We'll enforce this later |
555 | # else: | |
d37ff079 | 556 | # raise StackException, 'Parent branch (%s) should be specified for %s' % localbranch, self.get_name() |
8866feda | 557 | |
41a6d859 | 558 | def __patch_is_current(self, patch): |
8fe7e9f0 | 559 | return patch.get_name() == self.get_current() |
41a6d859 | 560 | |
ed0350be | 561 | def patch_applied(self, name): |
41a6d859 CM |
562 | """Return true if the patch exists in the applied list |
563 | """ | |
564 | return name in self.get_applied() | |
565 | ||
ed0350be | 566 | def patch_unapplied(self, name): |
41a6d859 CM |
567 | """Return true if the patch exists in the unapplied list |
568 | """ | |
569 | return name in self.get_unapplied() | |
570 | ||
841c7b2a CM |
571 | def patch_hidden(self, name): |
572 | """Return true if the patch is hidden. | |
573 | """ | |
574 | return name in self.get_hidden() | |
575 | ||
4d0ba818 KH |
576 | def patch_exists(self, name): |
577 | """Return true if there is a patch with the given name, false | |
578 | otherwise.""" | |
ca8b854c CM |
579 | return self.patch_applied(name) or self.patch_unapplied(name) \ |
580 | or self.patch_hidden(name) | |
4d0ba818 | 581 | |
8866feda | 582 | def init(self, create_at=False, parent_remote=None, parent_branch=None): |
41a6d859 CM |
583 | """Initialises the stgit series |
584 | """ | |
598e9d3f | 585 | if self.is_initialised(): |
d37ff079 | 586 | raise StackException, '%s already initialized' % self.get_name() |
262d31dc | 587 | for d in [self._dir()]: |
598e9d3f KH |
588 | if os.path.exists(d): |
589 | raise StackException, '%s already exists' % d | |
fe847176 | 590 | |
a22a62b6 | 591 | if (create_at!=False): |
d37ff079 | 592 | git.create_branch(self.get_name(), create_at) |
a22a62b6 | 593 | |
41a6d859 CM |
594 | os.makedirs(self.__patch_dir) |
595 | ||
8866feda | 596 | self.set_parent(parent_remote, parent_branch) |
41a6d859 | 597 | |
8fe7e9f0 YD |
598 | self.create_empty_field('applied') |
599 | self.create_empty_field('unapplied') | |
fb1cf8ec | 600 | self._set_field('orig-base', git.get_head()) |
41a6d859 | 601 | |
9171769c | 602 | config.set(self.format_version_key(), str(FORMAT_VERSION)) |
bad9dcfc | 603 | |
660ba985 CL |
604 | def rename(self, to_name): |
605 | """Renames a series | |
606 | """ | |
607 | to_stack = Series(to_name) | |
84bf6268 CL |
608 | |
609 | if to_stack.is_initialised(): | |
d37ff079 | 610 | raise StackException, '"%s" already exists' % to_stack.get_name() |
660ba985 | 611 | |
262d31dc KH |
612 | patches = self.get_applied() + self.get_unapplied() |
613 | ||
d37ff079 | 614 | git.rename_branch(self.get_name(), to_name) |
660ba985 | 615 | |
262d31dc KH |
616 | for patch in patches: |
617 | git.rename_ref('refs/patches/%s/%s' % (self.get_name(), patch), | |
618 | 'refs/patches/%s/%s' % (to_name, patch)) | |
619 | git.rename_ref('refs/patches/%s/%s.log' % (self.get_name(), patch), | |
620 | 'refs/patches/%s/%s.log' % (to_name, patch)) | |
8fe7e9f0 | 621 | if os.path.isdir(self._dir()): |
dd1b8fcc | 622 | rename(os.path.join(self._basedir(), 'patches'), |
d37ff079 | 623 | self.get_name(), to_stack.get_name()) |
660ba985 | 624 | |
cb5be4c3 | 625 | # Rename the config section |
337a0743 KH |
626 | for k in ['branch.%s', 'branch.%s.stgit']: |
627 | config.rename_section(k % self.get_name(), k % to_name) | |
cb5be4c3 | 628 | |
660ba985 CL |
629 | self.__init__(to_name) |
630 | ||
cc3db2b1 CL |
631 | def clone(self, target_series): |
632 | """Clones a series | |
633 | """ | |
09d8f8c5 CM |
634 | try: |
635 | # allow cloning of branches not under StGIT control | |
ba66e579 | 636 | base = self.get_base() |
09d8f8c5 CM |
637 | except: |
638 | base = git.get_head() | |
a22a62b6 | 639 | Series(target_series).init(create_at = base) |
cc3db2b1 CL |
640 | new_series = Series(target_series) |
641 | ||
642 | # generate an artificial description file | |
d37ff079 | 643 | new_series.set_description('clone of "%s"' % self.get_name()) |
cc3db2b1 CL |
644 | |
645 | # clone self's entire series as unapplied patches | |
09d8f8c5 CM |
646 | try: |
647 | # allow cloning of branches not under StGIT control | |
648 | applied = self.get_applied() | |
649 | unapplied = self.get_unapplied() | |
650 | patches = applied + unapplied | |
651 | patches.reverse() | |
652 | except: | |
653 | patches = applied = unapplied = [] | |
cc3db2b1 CL |
654 | for p in patches: |
655 | patch = self.get_patch(p) | |
8fce9909 YD |
656 | newpatch = new_series.new_patch(p, message = patch.get_description(), |
657 | can_edit = False, unapplied = True, | |
658 | bottom = patch.get_bottom(), | |
659 | top = patch.get_top(), | |
660 | author_name = patch.get_authname(), | |
661 | author_email = patch.get_authemail(), | |
662 | author_date = patch.get_authdate()) | |
663 | if patch.get_log(): | |
27ac2b7e | 664 | out.info('Setting log to %s' % patch.get_log()) |
8fce9909 YD |
665 | newpatch.set_log(patch.get_log()) |
666 | else: | |
27ac2b7e | 667 | out.info('No log for %s' % p) |
cc3db2b1 CL |
668 | |
669 | # fast forward the cloned series to self's top | |
09d8f8c5 | 670 | new_series.forward_patches(applied) |
cc3db2b1 | 671 | |
f32cdac5 | 672 | # Clone parent informations |
d37ff079 | 673 | value = config.get('branch.%s.remote' % self.get_name()) |
0579dae6 PR |
674 | if value: |
675 | config.set('branch.%s.remote' % target_series, value) | |
676 | ||
d37ff079 | 677 | value = config.get('branch.%s.merge' % self.get_name()) |
0579dae6 PR |
678 | if value: |
679 | config.set('branch.%s.merge' % target_series, value) | |
680 | ||
d37ff079 | 681 | value = config.get('branch.%s.stgit.parentbranch' % self.get_name()) |
f32cdac5 YD |
682 | if value: |
683 | config.set('branch.%s.stgit.parentbranch' % target_series, value) | |
684 | ||
fc804a49 CL |
685 | def delete(self, force = False): |
686 | """Deletes an stgit series | |
687 | """ | |
2d00440c | 688 | if self.is_initialised(): |
fc804a49 CL |
689 | patches = self.get_unapplied() + self.get_applied() |
690 | if not force and patches: | |
691 | raise StackException, \ | |
692 | 'Cannot delete: the series still contains patches' | |
fc804a49 | 693 | for p in patches: |
4c0dd299 | 694 | self.get_patch(p).delete() |
fc804a49 | 695 | |
c177ec71 YD |
696 | # remove the trash directory if any |
697 | if os.path.exists(self.__trash_dir): | |
698 | for fname in os.listdir(self.__trash_dir): | |
699 | os.remove(os.path.join(self.__trash_dir, fname)) | |
700 | os.rmdir(self.__trash_dir) | |
ac50371b | 701 | |
8fe7e9f0 | 702 | # FIXME: find a way to get rid of those manual removals |
a9d090f4 | 703 | # (move functionality to StgitObject ?) |
84bf6268 | 704 | if os.path.exists(self.__applied_file): |
fc804a49 | 705 | os.remove(self.__applied_file) |
84bf6268 | 706 | if os.path.exists(self.__unapplied_file): |
fc804a49 | 707 | os.remove(self.__unapplied_file) |
841c7b2a CM |
708 | if os.path.exists(self.__hidden_file): |
709 | os.remove(self.__hidden_file) | |
f9072c2f YD |
710 | if os.path.exists(self._dir()+'/orig-base'): |
711 | os.remove(self._dir()+'/orig-base') | |
737f3549 | 712 | |
fc804a49 CL |
713 | if not os.listdir(self.__patch_dir): |
714 | os.rmdir(self.__patch_dir) | |
715 | else: | |
27ac2b7e | 716 | out.warn('Patch directory %s is not empty' % self.__patch_dir) |
737f3549 | 717 | |
c7728cd5 | 718 | try: |
737f3549 | 719 | os.removedirs(self._dir()) |
c7728cd5 | 720 | except OSError: |
27ac2b7e KH |
721 | raise StackException('Series directory %s is not empty' |
722 | % self._dir()) | |
737f3549 | 723 | |
c7728cd5 | 724 | try: |
262d31dc KH |
725 | git.delete_branch(self.get_name()) |
726 | except GitException: | |
727 | out.warn('Could not delete branch "%s"' % self.get_name()) | |
fc804a49 | 728 | |
85289c08 YD |
729 | # Cleanup parent informations |
730 | # FIXME: should one day make use of git-config --section-remove, | |
731 | # scheduled for 1.5.1 | |
d37ff079 YD |
732 | config.unset('branch.%s.remote' % self.get_name()) |
733 | config.unset('branch.%s.merge' % self.get_name()) | |
734 | config.unset('branch.%s.stgit.parentbranch' % self.get_name()) | |
69ffa22e | 735 | config.unset(self.format_version_key()) |
85289c08 | 736 | |
026c0689 CM |
737 | def refresh_patch(self, files = None, message = None, edit = False, |
738 | show_patch = False, | |
6ad48e48 | 739 | cache_update = True, |
41a6d859 CM |
740 | author_name = None, author_email = None, |
741 | author_date = None, | |
f80bef49 | 742 | committer_name = None, committer_email = None, |
eff17c6b CM |
743 | backup = False, sign_str = None, log = 'refresh', |
744 | notes = None): | |
41a6d859 CM |
745 | """Generates a new commit for the given patch |
746 | """ | |
747 | name = self.get_current() | |
748 | if not name: | |
749 | raise StackException, 'No patches applied' | |
750 | ||
4c0dd299 | 751 | patch = self.get_patch(name) |
41a6d859 CM |
752 | |
753 | descr = patch.get_description() | |
754 | if not (message or descr): | |
755 | edit = True | |
756 | descr = '' | |
757 | elif message: | |
758 | descr = message | |
759 | ||
760 | if not message and edit: | |
6ad48e48 | 761 | descr = edit_file(self, descr.rstrip(), \ |
41a6d859 | 762 | 'Please edit the description for patch "%s" ' \ |
6ad48e48 | 763 | 'above.' % name, show_patch) |
41a6d859 CM |
764 | |
765 | if not author_name: | |
766 | author_name = patch.get_authname() | |
767 | if not author_email: | |
768 | author_email = patch.get_authemail() | |
769 | if not author_date: | |
770 | author_date = patch.get_authdate() | |
771 | if not committer_name: | |
772 | committer_name = patch.get_commname() | |
773 | if not committer_email: | |
774 | committer_email = patch.get_commemail() | |
775 | ||
130df01a | 776 | descr = add_sign_line(descr, sign_str, committer_name, committer_email) |
c40c3500 | 777 | |
f80bef49 CM |
778 | bottom = patch.get_bottom() |
779 | ||
026c0689 | 780 | commit_id = git.commit(files = files, |
f80bef49 | 781 | message = descr, parents = [bottom], |
402ad990 | 782 | cache_update = cache_update, |
41a6d859 CM |
783 | allowempty = True, |
784 | author_name = author_name, | |
785 | author_email = author_email, | |
786 | author_date = author_date, | |
787 | committer_name = committer_name, | |
788 | committer_email = committer_email) | |
789 | ||
f80bef49 CM |
790 | patch.set_bottom(bottom, backup = backup) |
791 | patch.set_top(commit_id, backup = backup) | |
84fcbc3b CM |
792 | patch.set_description(descr) |
793 | patch.set_authname(author_name) | |
794 | patch.set_authemail(author_email) | |
795 | patch.set_authdate(author_date) | |
796 | patch.set_commname(committer_name) | |
797 | patch.set_commemail(committer_email) | |
c14444b9 | 798 | |
64354a2d | 799 | if log: |
eff17c6b | 800 | self.log_patch(patch, log, notes) |
64354a2d | 801 | |
c14444b9 | 802 | return commit_id |
41a6d859 | 803 | |
f80bef49 CM |
804 | def undo_refresh(self): |
805 | """Undo the patch boundaries changes caused by 'refresh' | |
806 | """ | |
807 | name = self.get_current() | |
808 | assert(name) | |
809 | ||
4c0dd299 | 810 | patch = self.get_patch(name) |
f80bef49 CM |
811 | old_bottom = patch.get_old_bottom() |
812 | old_top = patch.get_old_top() | |
813 | ||
814 | # the bottom of the patch is not changed by refresh. If the | |
815 | # old_bottom is different, there wasn't any previous 'refresh' | |
816 | # command (probably only a 'push') | |
817 | if old_bottom != patch.get_bottom() or old_top == patch.get_top(): | |
06848fab | 818 | raise StackException, 'No undo information available' |
f80bef49 CM |
819 | |
820 | git.reset(tree_id = old_top, check_out = False) | |
64354a2d CM |
821 | if patch.restore_old_boundaries(): |
822 | self.log_patch(patch, 'undo') | |
f80bef49 | 823 | |
37a4d1bf CM |
824 | def new_patch(self, name, message = None, can_edit = True, |
825 | unapplied = False, show_patch = False, | |
0ec93bfd | 826 | top = None, bottom = None, commit = True, |
41a6d859 | 827 | author_name = None, author_email = None, author_date = None, |