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 * | |
1f3bb017 | 24 | from stgit import git, basedir, templates |
41a6d859 | 25 | from stgit.config import config |
8fce9909 | 26 | from shutil import copyfile |
41a6d859 CM |
27 | |
28 | ||
29 | # stack exception class | |
30 | class StackException(Exception): | |
31 | pass | |
32 | ||
6ad48e48 PBG |
33 | class FilterUntil: |
34 | def __init__(self): | |
35 | self.should_print = True | |
36 | def __call__(self, x, until_test, prefix): | |
37 | if until_test(x): | |
38 | self.should_print = False | |
39 | if self.should_print: | |
40 | return x[0:len(prefix)] != prefix | |
41 | return False | |
42 | ||
41a6d859 CM |
43 | # |
44 | # Functions | |
45 | # | |
46 | __comment_prefix = 'STG:' | |
6ad48e48 | 47 | __patch_prefix = 'STG_PATCH:' |
41a6d859 CM |
48 | |
49 | def __clean_comments(f): | |
50 | """Removes lines marked for status in a commit file | |
51 | """ | |
52 | f.seek(0) | |
53 | ||
54 | # remove status-prefixed lines | |
6ad48e48 PBG |
55 | lines = f.readlines() |
56 | ||
57 | patch_filter = FilterUntil() | |
58 | until_test = lambda t: t == (__patch_prefix + '\n') | |
59 | lines = [l for l in lines if patch_filter(l, until_test, __comment_prefix)] | |
60 | ||
41a6d859 CM |
61 | # remove empty lines at the end |
62 | while len(lines) != 0 and lines[-1] == '\n': | |
63 | del lines[-1] | |
64 | ||
65 | f.seek(0); f.truncate() | |
66 | f.writelines(lines) | |
67 | ||
7cc615f3 | 68 | def edit_file(series, line, comment, show_patch = True): |
bd427e46 | 69 | fname = '.stgitmsg.txt' |
1f3bb017 | 70 | tmpl = templates.get_template('patchdescr.tmpl') |
41a6d859 CM |
71 | |
72 | f = file(fname, 'w+') | |
7cc615f3 CL |
73 | if line: |
74 | print >> f, line | |
1f3bb017 CM |
75 | elif tmpl: |
76 | print >> f, tmpl, | |
41a6d859 CM |
77 | else: |
78 | print >> f | |
79 | print >> f, __comment_prefix, comment | |
80 | print >> f, __comment_prefix, \ | |
81 | 'Lines prefixed with "%s" will be automatically removed.' \ | |
82 | % __comment_prefix | |
83 | print >> f, __comment_prefix, \ | |
84 | 'Trailing empty lines will be automatically removed.' | |
6ad48e48 PBG |
85 | |
86 | if show_patch: | |
87 | print >> f, __patch_prefix | |
88 | # series.get_patch(series.get_current()).get_top() | |
89 | git.diff([], series.get_patch(series.get_current()).get_bottom(), None, f) | |
90 | ||
91 | #Vim modeline must be near the end. | |
b83e37e0 | 92 | print >> f, __comment_prefix, 'vi: set textwidth=75 filetype=diff nobackup:' |
41a6d859 CM |
93 | f.close() |
94 | ||
83bb4e4c | 95 | call_editor(fname) |
41a6d859 CM |
96 | |
97 | f = file(fname, 'r+') | |
98 | ||
99 | __clean_comments(f) | |
100 | f.seek(0) | |
7cc615f3 | 101 | result = f.read() |
41a6d859 CM |
102 | |
103 | f.close() | |
104 | os.remove(fname) | |
105 | ||
7cc615f3 | 106 | return result |
41a6d859 CM |
107 | |
108 | # | |
109 | # Classes | |
110 | # | |
111 | ||
8fe7e9f0 YD |
112 | class StgitObject: |
113 | """An object with stgit-like properties stored as files in a directory | |
114 | """ | |
115 | def _set_dir(self, dir): | |
116 | self.__dir = dir | |
117 | def _dir(self): | |
118 | return self.__dir | |
119 | ||
120 | def create_empty_field(self, name): | |
121 | create_empty_file(os.path.join(self.__dir, name)) | |
122 | ||
123 | def _get_field(self, name, multiline = False): | |
124 | id_file = os.path.join(self.__dir, name) | |
125 | if os.path.isfile(id_file): | |
126 | line = read_string(id_file, multiline) | |
127 | if line == '': | |
128 | return None | |
129 | else: | |
130 | return line | |
131 | else: | |
132 | return None | |
133 | ||
134 | def _set_field(self, name, value, multiline = False): | |
135 | fname = os.path.join(self.__dir, name) | |
136 | if value and value != '': | |
137 | write_string(fname, value, multiline) | |
138 | elif os.path.isfile(fname): | |
139 | os.remove(fname) | |
140 | ||
141 | ||
142 | class Patch(StgitObject): | |
41a6d859 CM |
143 | """Basic patch implementation |
144 | """ | |
844a1640 | 145 | def __init__(self, name, series_dir, refs_dir): |
02ac3ad2 | 146 | self.__series_dir = series_dir |
41a6d859 | 147 | self.__name = name |
8fe7e9f0 | 148 | self._set_dir(os.path.join(self.__series_dir, self.__name)) |
844a1640 CM |
149 | self.__refs_dir = refs_dir |
150 | self.__top_ref_file = os.path.join(self.__refs_dir, self.__name) | |
64354a2d CM |
151 | self.__log_ref_file = os.path.join(self.__refs_dir, |
152 | self.__name + '.log') | |
41a6d859 CM |
153 | |
154 | def create(self): | |
8fe7e9f0 YD |
155 | os.mkdir(self._dir()) |
156 | self.create_empty_field('bottom') | |
157 | self.create_empty_field('top') | |
41a6d859 CM |
158 | |
159 | def delete(self): | |
8fe7e9f0 YD |
160 | for f in os.listdir(self._dir()): |
161 | os.remove(os.path.join(self._dir(), f)) | |
162 | os.rmdir(self._dir()) | |
844a1640 | 163 | os.remove(self.__top_ref_file) |
64354a2d CM |
164 | if os.path.exists(self.__log_ref_file): |
165 | os.remove(self.__log_ref_file) | |
41a6d859 CM |
166 | |
167 | def get_name(self): | |
168 | return self.__name | |
169 | ||
e55b53e0 | 170 | def rename(self, newname): |
8fe7e9f0 | 171 | olddir = self._dir() |
64354a2d CM |
172 | old_top_ref_file = self.__top_ref_file |
173 | old_log_ref_file = self.__log_ref_file | |
e55b53e0 | 174 | self.__name = newname |
8fe7e9f0 | 175 | self._set_dir(os.path.join(self.__series_dir, self.__name)) |
844a1640 | 176 | self.__top_ref_file = os.path.join(self.__refs_dir, self.__name) |
64354a2d CM |
177 | self.__log_ref_file = os.path.join(self.__refs_dir, |
178 | self.__name + '.log') | |
e55b53e0 | 179 | |
8fe7e9f0 | 180 | os.rename(olddir, self._dir()) |
64354a2d CM |
181 | os.rename(old_top_ref_file, self.__top_ref_file) |
182 | if os.path.exists(old_log_ref_file): | |
183 | os.rename(old_log_ref_file, self.__log_ref_file) | |
844a1640 CM |
184 | |
185 | def __update_top_ref(self, ref): | |
186 | write_string(self.__top_ref_file, ref) | |
187 | ||
64354a2d CM |
188 | def __update_log_ref(self, ref): |
189 | write_string(self.__log_ref_file, ref) | |
190 | ||
844a1640 CM |
191 | def update_top_ref(self): |
192 | top = self.get_top() | |
193 | if top: | |
194 | self.__update_top_ref(top) | |
e55b53e0 | 195 | |
54b09584 | 196 | def get_old_bottom(self): |
8fe7e9f0 | 197 | return self._get_field('bottom.old') |
54b09584 | 198 | |
41a6d859 | 199 | def get_bottom(self): |
8fe7e9f0 | 200 | return self._get_field('bottom') |
41a6d859 | 201 | |
7cc615f3 | 202 | def set_bottom(self, value, backup = False): |
41a6d859 | 203 | if backup: |
8fe7e9f0 YD |
204 | curr = self._get_field('bottom') |
205 | self._set_field('bottom.old', curr) | |
206 | self._set_field('bottom', value) | |
41a6d859 | 207 | |
54b09584 | 208 | def get_old_top(self): |
8fe7e9f0 | 209 | return self._get_field('top.old') |
54b09584 | 210 | |
41a6d859 | 211 | def get_top(self): |
8fe7e9f0 | 212 | return self._get_field('top') |
41a6d859 | 213 | |
7cc615f3 | 214 | def set_top(self, value, backup = False): |
41a6d859 | 215 | if backup: |
8fe7e9f0 YD |
216 | curr = self._get_field('top') |
217 | self._set_field('top.old', curr) | |
218 | self._set_field('top', value) | |
844a1640 | 219 | self.__update_top_ref(value) |
41a6d859 CM |
220 | |
221 | def restore_old_boundaries(self): | |
8fe7e9f0 YD |
222 | bottom = self._get_field('bottom.old') |
223 | top = self._get_field('top.old') | |
41a6d859 CM |
224 | |
225 | if top and bottom: | |
8fe7e9f0 YD |
226 | self._set_field('bottom', bottom) |
227 | self._set_field('top', top) | |
844a1640 | 228 | self.__update_top_ref(top) |
a5bbc44d | 229 | return True |
41a6d859 | 230 | else: |
a5bbc44d | 231 | return False |
41a6d859 CM |
232 | |
233 | def get_description(self): | |
8fe7e9f0 | 234 | return self._get_field('description', True) |
41a6d859 | 235 | |
7cc615f3 | 236 | def set_description(self, line): |
8fe7e9f0 | 237 | self._set_field('description', line, True) |
41a6d859 CM |
238 | |
239 | def get_authname(self): | |
8fe7e9f0 | 240 | return self._get_field('authname') |
41a6d859 | 241 | |
7cc615f3 | 242 | def set_authname(self, name): |
8fe7e9f0 | 243 | self._set_field('authname', name or git.author().name) |
41a6d859 CM |
244 | |
245 | def get_authemail(self): | |
8fe7e9f0 | 246 | return self._get_field('authemail') |
41a6d859 | 247 | |
9e3f506f | 248 | def set_authemail(self, email): |
8fe7e9f0 | 249 | self._set_field('authemail', email or git.author().email) |
41a6d859 CM |
250 | |
251 | def get_authdate(self): | |
8fe7e9f0 | 252 | return self._get_field('authdate') |
41a6d859 | 253 | |
4db741b1 | 254 | def set_authdate(self, date): |
8fe7e9f0 | 255 | self._set_field('authdate', date or git.author().date) |
41a6d859 CM |
256 | |
257 | def get_commname(self): | |
8fe7e9f0 | 258 | return self._get_field('commname') |
41a6d859 | 259 | |
7cc615f3 | 260 | def set_commname(self, name): |
8fe7e9f0 | 261 | self._set_field('commname', name or git.committer().name) |
41a6d859 CM |
262 | |
263 | def get_commemail(self): | |
8fe7e9f0 | 264 | return self._get_field('commemail') |
41a6d859 | 265 | |
9e3f506f | 266 | def set_commemail(self, email): |
8fe7e9f0 | 267 | self._set_field('commemail', email or git.committer().email) |
41a6d859 | 268 | |
64354a2d | 269 | def get_log(self): |
8fe7e9f0 | 270 | return self._get_field('log') |
64354a2d CM |
271 | |
272 | def set_log(self, value, backup = False): | |
8fe7e9f0 | 273 | self._set_field('log', value) |
64354a2d CM |
274 | self.__update_log_ref(value) |
275 | ||
598e9d3f KH |
276 | # The current StGIT metadata format version. |
277 | FORMAT_VERSION = 2 | |
278 | ||
279 | def format_version_key(branch): | |
280 | return 'branch.%s.stgitformatversion' % branch | |
281 | ||
282 | def update_to_current_format_version(branch, git_dir): | |
283 | """Update a potentially older StGIT directory structure to the | |
284 | latest version. Note: This function should depend as little as | |
285 | possible on external functions that may change during a format | |
286 | version bump, since it must remain able to process older formats.""" | |
287 | ||
288 | branch_dir = os.path.join(git_dir, 'patches', branch) | |
289 | def get_format_version(): | |
290 | """Return the integer format version number, or None if the | |
291 | branch doesn't have any StGIT metadata at all, of any version.""" | |
292 | fv = config.get(format_version_key(branch)) | |
293 | if fv: | |
294 | # Great, there's an explicitly recorded format version | |
295 | # number, which means that the branch is initialized and | |
296 | # of that exact version. | |
297 | return int(fv) | |
298 | elif os.path.isdir(os.path.join(branch_dir, 'patches')): | |
299 | # There's a .git/patches/<branch>/patches dirctory, which | |
300 | # means this is an initialized version 1 branch. | |
301 | return 1 | |
302 | elif os.path.isdir(branch_dir): | |
303 | # There's a .git/patches/<branch> directory, which means | |
304 | # this is an initialized version 0 branch. | |
305 | return 0 | |
306 | else: | |
307 | # The branch doesn't seem to be initialized at all. | |
308 | return None | |
309 | def set_format_version(v): | |
27ac2b7e | 310 | out.info('Upgraded branch %s to format version %d' % (branch, v)) |
598e9d3f KH |
311 | config.set(format_version_key(branch), '%d' % v) |
312 | def mkdir(d): | |
313 | if not os.path.isdir(d): | |
314 | os.makedirs(d) | |
315 | def rm(f): | |
316 | if os.path.exists(f): | |
317 | os.remove(f) | |
318 | ||
319 | # Update 0 -> 1. | |
320 | if get_format_version() == 0: | |
321 | mkdir(os.path.join(branch_dir, 'trash')) | |
322 | patch_dir = os.path.join(branch_dir, 'patches') | |
323 | mkdir(patch_dir) | |
324 | refs_dir = os.path.join(git_dir, 'refs', 'patches', branch) | |
325 | mkdir(refs_dir) | |
326 | for patch in (file(os.path.join(branch_dir, 'unapplied')).readlines() | |
327 | + file(os.path.join(branch_dir, 'applied')).readlines()): | |
328 | patch = patch.strip() | |
329 | os.rename(os.path.join(branch_dir, patch), | |
330 | os.path.join(patch_dir, patch)) | |
331 | Patch(patch, patch_dir, refs_dir).update_top_ref() | |
332 | set_format_version(1) | |
333 | ||
334 | # Update 1 -> 2. | |
335 | if get_format_version() == 1: | |
336 | desc_file = os.path.join(branch_dir, 'description') | |
337 | if os.path.isfile(desc_file): | |
338 | desc = read_string(desc_file) | |
339 | if desc: | |
340 | config.set('branch.%s.description' % branch, desc) | |
341 | rm(desc_file) | |
342 | rm(os.path.join(branch_dir, 'current')) | |
343 | rm(os.path.join(git_dir, 'refs', 'bases', branch)) | |
344 | set_format_version(2) | |
345 | ||
346 | # Make sure we're at the latest version. | |
347 | if not get_format_version() in [None, FORMAT_VERSION]: | |
348 | raise StackException('Branch %s is at format version %d, expected %d' | |
349 | % (branch, get_format_version(), FORMAT_VERSION)) | |
41a6d859 | 350 | |
8fe7e9f0 | 351 | class Series(StgitObject): |
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 | """ |
98290387 CM |
357 | try: |
358 | if name: | |
d37ff079 | 359 | self.set_name (name) |
98290387 | 360 | else: |
d37ff079 | 361 | self.set_name (git.get_head_file()) |
170f576b | 362 | self.__base_dir = basedir.get() |
98290387 CM |
363 | except git.GitException, ex: |
364 | raise StackException, 'GIT tree not initialised: %s' % ex | |
365 | ||
d37ff079 | 366 | self._set_dir(os.path.join(self.__base_dir, 'patches', self.get_name())) |
598e9d3f KH |
367 | |
368 | # Update the branch to the latest format version if it is | |
369 | # initialized, but don't touch it if it isn't. | |
d37ff079 | 370 | update_to_current_format_version(self.get_name(), self.__base_dir) |
598e9d3f | 371 | |
844a1640 | 372 | self.__refs_dir = os.path.join(self.__base_dir, 'refs', 'patches', |
d37ff079 | 373 | self.get_name()) |
02ac3ad2 | 374 | |
8fe7e9f0 YD |
375 | self.__applied_file = os.path.join(self._dir(), 'applied') |
376 | self.__unapplied_file = os.path.join(self._dir(), 'unapplied') | |
841c7b2a | 377 | self.__hidden_file = os.path.join(self._dir(), 'hidden') |
02ac3ad2 CL |
378 | |
379 | # where this series keeps its patches | |
8fe7e9f0 | 380 | self.__patch_dir = os.path.join(self._dir(), 'patches') |
844a1640 | 381 | |
ac50371b | 382 | # trash directory |
8fe7e9f0 | 383 | self.__trash_dir = os.path.join(self._dir(), 'trash') |
ac50371b | 384 | |
c1e4d7e0 CM |
385 | def __patch_name_valid(self, name): |
386 | """Raise an exception if the patch name is not valid. | |
387 | """ | |
388 | if not name or re.search('[^\w.-]', name): | |
389 | raise StackException, 'Invalid patch name: "%s"' % name | |
390 | ||
d37ff079 | 391 | def get_name(self): |
629ddd02 | 392 | return self.__name |
d37ff079 YD |
393 | def set_name(self, name): |
394 | self.__name = name | |
395 | ||
41a6d859 CM |
396 | def get_patch(self, name): |
397 | """Return a Patch object for the given name | |
398 | """ | |
844a1640 | 399 | return Patch(name, self.__patch_dir, self.__refs_dir) |
41a6d859 | 400 | |
4d0ba818 KH |
401 | def get_current_patch(self): |
402 | """Return a Patch object representing the topmost patch, or | |
403 | None if there is no such patch.""" | |
404 | crt = self.get_current() | |
405 | if not crt: | |
406 | return None | |
407 | return Patch(crt, self.__patch_dir, self.__refs_dir) | |
408 | ||
41a6d859 | 409 | def get_current(self): |
4d0ba818 KH |
410 | """Return the name of the topmost patch, or None if there is |
411 | no such patch.""" | |
532cdf94 KH |
412 | try: |
413 | applied = self.get_applied() | |
414 | except StackException: | |
415 | # No "applied" file: branch is not initialized. | |
416 | return None | |
417 | try: | |
418 | return applied[-1] | |
419 | except IndexError: | |
420 | # No patches applied. | |
41a6d859 | 421 | return None |
41a6d859 CM |
422 | |
423 | def get_applied(self): | |
40e65b92 | 424 | if not os.path.isfile(self.__applied_file): |
d37ff079 | 425 | raise StackException, 'Branch "%s" not initialised' % self.get_name() |
17364282 | 426 | return read_strings(self.__applied_file) |
41a6d859 CM |
427 | |
428 | def get_unapplied(self): | |
40e65b92 | 429 | if not os.path.isfile(self.__unapplied_file): |
d37ff079 | 430 | raise StackException, 'Branch "%s" not initialised' % self.get_name() |
17364282 | 431 | return read_strings(self.__unapplied_file) |
41a6d859 | 432 | |
841c7b2a CM |
433 | def get_hidden(self): |
434 | if not os.path.isfile(self.__hidden_file): | |
435 | return [] | |
17364282 | 436 | return read_strings(self.__hidden_file) |
841c7b2a | 437 | |
ba66e579 | 438 | def get_base(self): |
16d69115 KH |
439 | # Return the parent of the bottommost patch, if there is one. |
440 | if os.path.isfile(self.__applied_file): | |
441 | bottommost = file(self.__applied_file).readline().strip() | |
442 | if bottommost: | |
443 | return self.get_patch(bottommost).get_bottom() | |
444 | # No bottommost patch, so just return HEAD | |
445 | return git.get_head() | |
ba66e579 | 446 | |
e078133e CM |
447 | def get_head(self): |
448 | """Return the head of the branch | |
449 | """ | |
450 | crt = self.get_current_patch() | |
451 | if crt: | |
452 | return crt.get_top() | |
453 | else: | |
454 | return self.get_base() | |
455 | ||
0b4b9499 | 456 | def get_protected(self): |
8fe7e9f0 | 457 | return os.path.isfile(os.path.join(self._dir(), 'protected')) |
0b4b9499 CL |
458 | |
459 | def protect(self): | |
8fe7e9f0 | 460 | protect_file = os.path.join(self._dir(), 'protected') |
0b4b9499 CL |
461 | if not os.path.isfile(protect_file): |
462 | create_empty_file(protect_file) | |
463 | ||
464 | def unprotect(self): | |
8fe7e9f0 | 465 | protect_file = os.path.join(self._dir(), 'protected') |
0b4b9499 CL |
466 | if os.path.isfile(protect_file): |
467 | os.remove(protect_file) | |
468 | ||
4975762e | 469 | def __branch_descr(self): |
d37ff079 | 470 | return 'branch.%s.description' % self.get_name() |
4975762e | 471 | |
c1fe1f99 | 472 | def get_description(self): |
598e9d3f | 473 | return config.get(self.__branch_descr()) or '' |
8fe7e9f0 YD |
474 | |
475 | def set_description(self, line): | |
4975762e KH |
476 | if line: |
477 | config.set(self.__branch_descr(), line) | |
478 | else: | |
479 | config.unset(self.__branch_descr()) | |
c1fe1f99 | 480 | |
254d99f8 | 481 | def get_parent_remote(self): |
d37ff079 | 482 | value = config.get('branch.%s.remote' % self.get_name()) |
f72ad3d6 YD |
483 | if value: |
484 | return value | |
485 | elif 'origin' in git.remotes_list(): | |
27ac2b7e | 486 | out.note(('No parent remote declared for stack "%s",' |
d37ff079 | 487 | ' defaulting to "origin".' % self.get_name()), |
27ac2b7e KH |
488 | ('Consider setting "branch.%s.remote" and' |
489 | ' "branch.%s.merge" with "git repo-config".' | |
d37ff079 | 490 | % (self.get_name(), self.get_name()))) |
f72ad3d6 YD |
491 | return 'origin' |
492 | else: | |
d37ff079 | 493 | raise StackException, 'Cannot find a parent remote for "%s"' % self.get_name() |
254d99f8 YD |
494 | |
495 | def __set_parent_remote(self, remote): | |
d37ff079 | 496 | value = config.set('branch.%s.remote' % self.get_name(), remote) |
254d99f8 | 497 | |
8866feda | 498 | def get_parent_branch(self): |
d37ff079 | 499 | value = config.get('branch.%s.stgit.parentbranch' % self.get_name()) |
8866feda YD |
500 | if value: |
501 | return value | |
502 | elif git.rev_parse('heads/origin'): | |
27ac2b7e | 503 | out.note(('No parent branch declared for stack "%s",' |
d37ff079 | 504 | ' defaulting to "heads/origin".' % self.get_name()), |
27ac2b7e | 505 | ('Consider setting "branch.%s.stgit.parentbranch"' |
d37ff079 | 506 | ' with "git repo-config".' % self.get_name())) |
8866feda YD |
507 | return 'heads/origin' |
508 | else: | |
d37ff079 | 509 | raise StackException, 'Cannot find a parent branch for "%s"' % self.get_name() |
8866feda YD |
510 | |
511 | def __set_parent_branch(self, name): | |
d37ff079 | 512 | if config.get('branch.%s.remote' % self.get_name()): |
4646e7a3 YD |
513 | # Never set merge if remote is not set to avoid |
514 | # possibly-erroneous lookups into 'origin' | |
d37ff079 YD |
515 | config.set('branch.%s.merge' % self.get_name(), name) |
516 | config.set('branch.%s.stgit.parentbranch' % self.get_name(), name) | |
8866feda YD |
517 | |
518 | def set_parent(self, remote, localbranch): | |
519 | if localbranch: | |
1fa161d6 | 520 | self.__set_parent_remote(remote) |
8866feda | 521 | self.__set_parent_branch(localbranch) |
4646e7a3 YD |
522 | # We'll enforce this later |
523 | # else: | |
d37ff079 | 524 | # raise StackException, 'Parent branch (%s) should be specified for %s' % localbranch, self.get_name() |
8866feda | 525 | |
41a6d859 | 526 | def __patch_is_current(self, patch): |
8fe7e9f0 | 527 | return patch.get_name() == self.get_current() |
41a6d859 | 528 | |
ed0350be | 529 | def patch_applied(self, name): |
41a6d859 CM |
530 | """Return true if the patch exists in the applied list |
531 | """ | |
532 | return name in self.get_applied() | |
533 | ||
ed0350be | 534 | def patch_unapplied(self, name): |
41a6d859 CM |
535 | """Return true if the patch exists in the unapplied list |
536 | """ | |
537 | return name in self.get_unapplied() | |
538 | ||
841c7b2a CM |
539 | def patch_hidden(self, name): |
540 | """Return true if the patch is hidden. | |
541 | """ | |
542 | return name in self.get_hidden() | |
543 | ||
4d0ba818 KH |
544 | def patch_exists(self, name): |
545 | """Return true if there is a patch with the given name, false | |
546 | otherwise.""" | |
ca8b854c CM |
547 | return self.patch_applied(name) or self.patch_unapplied(name) \ |
548 | or self.patch_hidden(name) | |
4d0ba818 | 549 | |
41a6d859 CM |
550 | def head_top_equal(self): |
551 | """Return true if the head and the top are the same | |
552 | """ | |
4d0ba818 | 553 | crt = self.get_current_patch() |
41a6d859 CM |
554 | if not crt: |
555 | # we don't care, no patches applied | |
556 | return True | |
4d0ba818 | 557 | return git.get_head() == crt.get_top() |
41a6d859 | 558 | |
2d00440c CL |
559 | def is_initialised(self): |
560 | """Checks if series is already initialised | |
561 | """ | |
d37ff079 | 562 | return bool(config.get(format_version_key(self.get_name()))) |
2d00440c | 563 | |
8866feda | 564 | def init(self, create_at=False, parent_remote=None, parent_branch=None): |
41a6d859 CM |
565 | """Initialises the stgit series |
566 | """ | |
598e9d3f | 567 | if self.is_initialised(): |
d37ff079 | 568 | raise StackException, '%s already initialized' % self.get_name() |
598e9d3f KH |
569 | for d in [self._dir(), self.__refs_dir]: |
570 | if os.path.exists(d): | |
571 | raise StackException, '%s already exists' % d | |
fe847176 | 572 | |
a22a62b6 | 573 | if (create_at!=False): |
d37ff079 | 574 | git.create_branch(self.get_name(), create_at) |
a22a62b6 | 575 | |
41a6d859 CM |
576 | os.makedirs(self.__patch_dir) |
577 | ||
8866feda | 578 | self.set_parent(parent_remote, parent_branch) |
41a6d859 | 579 | |
8fe7e9f0 YD |
580 | self.create_empty_field('applied') |
581 | self.create_empty_field('unapplied') | |
844a1640 | 582 | os.makedirs(self.__refs_dir) |
41a6d859 | 583 | |
d37ff079 | 584 | config.set(format_version_key(self.get_name()), str(FORMAT_VERSION)) |
bad9dcfc | 585 | |
660ba985 CL |
586 | def rename(self, to_name): |
587 | """Renames a series | |
588 | """ | |
589 | to_stack = Series(to_name) | |
84bf6268 CL |
590 | |
591 | if to_stack.is_initialised(): | |
d37ff079 | 592 | raise StackException, '"%s" already exists' % to_stack.get_name() |
660ba985 | 593 | |
d37ff079 | 594 | git.rename_branch(self.get_name(), to_name) |
660ba985 | 595 | |
8fe7e9f0 | 596 | if os.path.isdir(self._dir()): |