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