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 CM |
25 | from stgit.config import config |
26 | ||
27 | ||
28 | # stack exception class | |
29 | class StackException(Exception): | |
30 | pass | |
31 | ||
6ad48e48 PBG |
32 | class FilterUntil: |
33 | def __init__(self): | |
34 | self.should_print = True | |
35 | def __call__(self, x, until_test, prefix): | |
36 | if until_test(x): | |
37 | self.should_print = False | |
38 | if self.should_print: | |
39 | return x[0:len(prefix)] != prefix | |
40 | return False | |
41 | ||
41a6d859 CM |
42 | # |
43 | # Functions | |
44 | # | |
45 | __comment_prefix = 'STG:' | |
6ad48e48 | 46 | __patch_prefix = 'STG_PATCH:' |
41a6d859 CM |
47 | |
48 | def __clean_comments(f): | |
49 | """Removes lines marked for status in a commit file | |
50 | """ | |
51 | f.seek(0) | |
52 | ||
53 | # remove status-prefixed lines | |
6ad48e48 PBG |
54 | lines = f.readlines() |
55 | ||
56 | patch_filter = FilterUntil() | |
57 | until_test = lambda t: t == (__patch_prefix + '\n') | |
58 | lines = [l for l in lines if patch_filter(l, until_test, __comment_prefix)] | |
59 | ||
41a6d859 CM |
60 | # remove empty lines at the end |
61 | while len(lines) != 0 and lines[-1] == '\n': | |
62 | del lines[-1] | |
63 | ||
64 | f.seek(0); f.truncate() | |
65 | f.writelines(lines) | |
66 | ||
7cc615f3 | 67 | def edit_file(series, line, comment, show_patch = True): |
bd427e46 | 68 | fname = '.stgitmsg.txt' |
1f3bb017 | 69 | tmpl = templates.get_template('patchdescr.tmpl') |
41a6d859 CM |
70 | |
71 | f = file(fname, 'w+') | |
7cc615f3 CL |
72 | if line: |
73 | print >> f, line | |
1f3bb017 CM |
74 | elif tmpl: |
75 | print >> f, tmpl, | |
41a6d859 CM |
76 | else: |
77 | print >> f | |
78 | print >> f, __comment_prefix, comment | |
79 | print >> f, __comment_prefix, \ | |
80 | 'Lines prefixed with "%s" will be automatically removed.' \ | |
81 | % __comment_prefix | |
82 | print >> f, __comment_prefix, \ | |
83 | 'Trailing empty lines will be automatically removed.' | |
6ad48e48 PBG |
84 | |
85 | if show_patch: | |
86 | print >> f, __patch_prefix | |
87 | # series.get_patch(series.get_current()).get_top() | |
88 | git.diff([], series.get_patch(series.get_current()).get_bottom(), None, f) | |
89 | ||
90 | #Vim modeline must be near the end. | |
b83e37e0 | 91 | print >> f, __comment_prefix, 'vi: set textwidth=75 filetype=diff nobackup:' |
41a6d859 CM |
92 | f.close() |
93 | ||
83bb4e4c | 94 | call_editor(fname) |
41a6d859 CM |
95 | |
96 | f = file(fname, 'r+') | |
97 | ||
98 | __clean_comments(f) | |
99 | f.seek(0) | |
7cc615f3 | 100 | result = f.read() |
41a6d859 CM |
101 | |
102 | f.close() | |
103 | os.remove(fname) | |
104 | ||
7cc615f3 | 105 | return result |
41a6d859 CM |
106 | |
107 | # | |
108 | # Classes | |
109 | # | |
110 | ||
8fe7e9f0 YD |
111 | class StgitObject: |
112 | """An object with stgit-like properties stored as files in a directory | |
113 | """ | |
114 | def _set_dir(self, dir): | |
115 | self.__dir = dir | |
116 | def _dir(self): | |
117 | return self.__dir | |
118 | ||
119 | def create_empty_field(self, name): | |
120 | create_empty_file(os.path.join(self.__dir, name)) | |
121 | ||
122 | def _get_field(self, name, multiline = False): | |
123 | id_file = os.path.join(self.__dir, name) | |
124 | if os.path.isfile(id_file): | |
125 | line = read_string(id_file, multiline) | |
126 | if line == '': | |
127 | return None | |
128 | else: | |
129 | return line | |
130 | else: | |
131 | return None | |
132 | ||
133 | def _set_field(self, name, value, multiline = False): | |
134 | fname = os.path.join(self.__dir, name) | |
135 | if value and value != '': | |
136 | write_string(fname, value, multiline) | |
137 | elif os.path.isfile(fname): | |
138 | os.remove(fname) | |
139 | ||
140 | ||
141 | class Patch(StgitObject): | |
41a6d859 CM |
142 | """Basic patch implementation |
143 | """ | |
844a1640 | 144 | def __init__(self, name, series_dir, refs_dir): |
02ac3ad2 | 145 | self.__series_dir = series_dir |
41a6d859 | 146 | self.__name = name |
8fe7e9f0 | 147 | self._set_dir(os.path.join(self.__series_dir, self.__name)) |
844a1640 CM |
148 | self.__refs_dir = refs_dir |
149 | self.__top_ref_file = os.path.join(self.__refs_dir, self.__name) | |
64354a2d CM |
150 | self.__log_ref_file = os.path.join(self.__refs_dir, |
151 | self.__name + '.log') | |
41a6d859 CM |
152 | |
153 | def create(self): | |
8fe7e9f0 YD |
154 | os.mkdir(self._dir()) |
155 | self.create_empty_field('bottom') | |
156 | self.create_empty_field('top') | |
41a6d859 CM |
157 | |
158 | def delete(self): | |
8fe7e9f0 YD |
159 | for f in os.listdir(self._dir()): |
160 | os.remove(os.path.join(self._dir(), f)) | |
161 | os.rmdir(self._dir()) | |
844a1640 | 162 | os.remove(self.__top_ref_file) |
64354a2d CM |
163 | if os.path.exists(self.__log_ref_file): |
164 | os.remove(self.__log_ref_file) | |
41a6d859 CM |
165 | |
166 | def get_name(self): | |
167 | return self.__name | |
168 | ||
e55b53e0 | 169 | def rename(self, newname): |
8fe7e9f0 | 170 | olddir = self._dir() |
64354a2d CM |
171 | old_top_ref_file = self.__top_ref_file |
172 | old_log_ref_file = self.__log_ref_file | |
e55b53e0 | 173 | self.__name = newname |
8fe7e9f0 | 174 | self._set_dir(os.path.join(self.__series_dir, self.__name)) |
844a1640 | 175 | self.__top_ref_file = os.path.join(self.__refs_dir, self.__name) |
64354a2d CM |
176 | self.__log_ref_file = os.path.join(self.__refs_dir, |
177 | self.__name + '.log') | |
e55b53e0 | 178 | |
8fe7e9f0 | 179 | os.rename(olddir, self._dir()) |
64354a2d CM |
180 | os.rename(old_top_ref_file, self.__top_ref_file) |
181 | if os.path.exists(old_log_ref_file): | |
182 | os.rename(old_log_ref_file, self.__log_ref_file) | |
844a1640 CM |
183 | |
184 | def __update_top_ref(self, ref): | |
185 | write_string(self.__top_ref_file, ref) | |
186 | ||
64354a2d CM |
187 | def __update_log_ref(self, ref): |
188 | write_string(self.__log_ref_file, ref) | |
189 | ||
844a1640 CM |
190 | def update_top_ref(self): |
191 | top = self.get_top() | |
192 | if top: | |
193 | self.__update_top_ref(top) | |
e55b53e0 | 194 | |
54b09584 | 195 | def get_old_bottom(self): |
8fe7e9f0 | 196 | return self._get_field('bottom.old') |
54b09584 | 197 | |
41a6d859 | 198 | def get_bottom(self): |
8fe7e9f0 | 199 | return self._get_field('bottom') |
41a6d859 | 200 | |
7cc615f3 | 201 | def set_bottom(self, value, backup = False): |
41a6d859 | 202 | if backup: |
8fe7e9f0 YD |
203 | curr = self._get_field('bottom') |
204 | self._set_field('bottom.old', curr) | |
205 | self._set_field('bottom', value) | |
41a6d859 | 206 | |
54b09584 | 207 | def get_old_top(self): |
8fe7e9f0 | 208 | return self._get_field('top.old') |
54b09584 | 209 | |
41a6d859 | 210 | def get_top(self): |
8fe7e9f0 | 211 | return self._get_field('top') |
41a6d859 | 212 | |
7cc615f3 | 213 | def set_top(self, value, backup = False): |
41a6d859 | 214 | if backup: |
8fe7e9f0 YD |
215 | curr = self._get_field('top') |
216 | self._set_field('top.old', curr) | |
217 | self._set_field('top', value) | |
844a1640 | 218 | self.__update_top_ref(value) |
41a6d859 CM |
219 | |
220 | def restore_old_boundaries(self): | |
8fe7e9f0 YD |
221 | bottom = self._get_field('bottom.old') |
222 | top = self._get_field('top.old') | |
41a6d859 CM |
223 | |
224 | if top and bottom: | |
8fe7e9f0 YD |
225 | self._set_field('bottom', bottom) |
226 | self._set_field('top', top) | |
844a1640 | 227 | self.__update_top_ref(top) |
a5bbc44d | 228 | return True |
41a6d859 | 229 | else: |
a5bbc44d | 230 | return False |
41a6d859 CM |
231 | |
232 | def get_description(self): | |
8fe7e9f0 | 233 | return self._get_field('description', True) |
41a6d859 | 234 | |
7cc615f3 | 235 | def set_description(self, line): |
8fe7e9f0 | 236 | self._set_field('description', line, True) |
41a6d859 CM |
237 | |
238 | def get_authname(self): | |
8fe7e9f0 | 239 | return self._get_field('authname') |
41a6d859 | 240 | |
7cc615f3 | 241 | def set_authname(self, name): |
8fe7e9f0 | 242 | self._set_field('authname', name or git.author().name) |
41a6d859 CM |
243 | |
244 | def get_authemail(self): | |
8fe7e9f0 | 245 | return self._get_field('authemail') |
41a6d859 | 246 | |
9e3f506f | 247 | def set_authemail(self, email): |
8fe7e9f0 | 248 | self._set_field('authemail', email or git.author().email) |
41a6d859 CM |
249 | |
250 | def get_authdate(self): | |
8fe7e9f0 | 251 | return self._get_field('authdate') |
41a6d859 | 252 | |
4db741b1 | 253 | def set_authdate(self, date): |
8fe7e9f0 | 254 | self._set_field('authdate', date or git.author().date) |
41a6d859 CM |
255 | |
256 | def get_commname(self): | |
8fe7e9f0 | 257 | return self._get_field('commname') |
41a6d859 | 258 | |
7cc615f3 | 259 | def set_commname(self, name): |
8fe7e9f0 | 260 | self._set_field('commname', name or git.committer().name) |
41a6d859 CM |
261 | |
262 | def get_commemail(self): | |
8fe7e9f0 | 263 | return self._get_field('commemail') |
41a6d859 | 264 | |
9e3f506f | 265 | def set_commemail(self, email): |
8fe7e9f0 | 266 | self._set_field('commemail', email or git.committer().email) |
41a6d859 | 267 | |
64354a2d | 268 | def get_log(self): |
8fe7e9f0 | 269 | return self._get_field('log') |
64354a2d CM |
270 | |
271 | def set_log(self, value, backup = False): | |
8fe7e9f0 | 272 | self._set_field('log', value) |
64354a2d CM |
273 | self.__update_log_ref(value) |
274 | ||
41a6d859 | 275 | |
8fe7e9f0 | 276 | class Series(StgitObject): |
41a6d859 CM |
277 | """Class including the operations on series |
278 | """ | |
279 | def __init__(self, name = None): | |
40e65b92 | 280 | """Takes a series name as the parameter. |
41a6d859 | 281 | """ |
98290387 CM |
282 | try: |
283 | if name: | |
284 | self.__name = name | |
285 | else: | |
286 | self.__name = git.get_head_file() | |
170f576b | 287 | self.__base_dir = basedir.get() |
98290387 CM |
288 | except git.GitException, ex: |
289 | raise StackException, 'GIT tree not initialised: %s' % ex | |
290 | ||
8fe7e9f0 | 291 | self._set_dir(os.path.join(self.__base_dir, 'patches', self.__name)) |
844a1640 CM |
292 | self.__refs_dir = os.path.join(self.__base_dir, 'refs', 'patches', |
293 | self.__name) | |
294 | self.__base_file = os.path.join(self.__base_dir, 'refs', 'bases', | |
98290387 | 295 | self.__name) |
02ac3ad2 | 296 | |
8fe7e9f0 YD |
297 | self.__applied_file = os.path.join(self._dir(), 'applied') |
298 | self.__unapplied_file = os.path.join(self._dir(), 'unapplied') | |
841c7b2a | 299 | self.__hidden_file = os.path.join(self._dir(), 'hidden') |
8fe7e9f0 YD |
300 | self.__current_file = os.path.join(self._dir(), 'current') |
301 | self.__descr_file = os.path.join(self._dir(), 'description') | |
02ac3ad2 CL |
302 | |
303 | # where this series keeps its patches | |
8fe7e9f0 | 304 | self.__patch_dir = os.path.join(self._dir(), 'patches') |
02ac3ad2 | 305 | if not os.path.isdir(self.__patch_dir): |
8fe7e9f0 | 306 | self.__patch_dir = self._dir() |
41a6d859 | 307 | |
844a1640 CM |
308 | # if no __refs_dir, create and populate it (upgrade old repositories) |
309 | if self.is_initialised() and not os.path.isdir(self.__refs_dir): | |
310 | os.makedirs(self.__refs_dir) | |
311 | for patch in self.get_applied() + self.get_unapplied(): | |
312 | self.get_patch(patch).update_top_ref() | |
313 | ||
ac50371b | 314 | # trash directory |
8fe7e9f0 | 315 | self.__trash_dir = os.path.join(self._dir(), 'trash') |
ac50371b CM |
316 | if self.is_initialised() and not os.path.isdir(self.__trash_dir): |
317 | os.makedirs(self.__trash_dir) | |
318 | ||
c1e4d7e0 CM |
319 | def __patch_name_valid(self, name): |
320 | """Raise an exception if the patch name is not valid. | |
321 | """ | |
322 | if not name or re.search('[^\w.-]', name): | |
323 | raise StackException, 'Invalid patch name: "%s"' % name | |
324 | ||
629ddd02 CM |
325 | def get_branch(self): |
326 | """Return the branch name for the Series object | |
327 | """ | |
328 | return self.__name | |
329 | ||
41a6d859 CM |
330 | def __set_current(self, name): |
331 | """Sets the topmost patch | |
332 | """ | |
8fe7e9f0 | 333 | self._set_field('current', name) |
41a6d859 CM |
334 | |
335 | def get_patch(self, name): | |
336 | """Return a Patch object for the given name | |
337 | """ | |
844a1640 | 338 | return Patch(name, self.__patch_dir, self.__refs_dir) |
41a6d859 | 339 | |
4d0ba818 KH |
340 | def get_current_patch(self): |
341 | """Return a Patch object representing the topmost patch, or | |
342 | None if there is no such patch.""" | |
343 | crt = self.get_current() | |
344 | if not crt: | |
345 | return None | |
346 | return Patch(crt, self.__patch_dir, self.__refs_dir) | |
347 | ||
41a6d859 | 348 | def get_current(self): |
4d0ba818 KH |
349 | """Return the name of the topmost patch, or None if there is |
350 | no such patch.""" | |
8fe7e9f0 | 351 | name = self._get_field('current') |
41a6d859 CM |
352 | if name == '': |
353 | return None | |
354 | else: | |
355 | return name | |
356 | ||
357 | def get_applied(self): | |
40e65b92 | 358 | if not os.path.isfile(self.__applied_file): |
a2dcde71 | 359 | raise StackException, 'Branch "%s" not initialised' % self.__name |
41a6d859 CM |
360 | f = file(self.__applied_file) |
361 | names = [line.strip() for line in f.readlines()] | |
362 | f.close() | |
363 | return names | |
364 | ||
365 | def get_unapplied(self): | |
40e65b92 | 366 | if not os.path.isfile(self.__unapplied_file): |
a2dcde71 | 367 | raise StackException, 'Branch "%s" not initialised' % self.__name |
41a6d859 CM |
368 | f = file(self.__unapplied_file) |
369 | names = [line.strip() for line in f.readlines()] | |
370 | f.close() | |
371 | return names | |
372 | ||
841c7b2a CM |
373 | def get_hidden(self): |
374 | if not os.path.isfile(self.__hidden_file): | |
375 | return [] | |
376 | f = file(self.__hidden_file) | |
377 | names = [line.strip() for line in f.readlines()] | |
378 | f.close() | |
379 | return names | |
380 | ||
41a6d859 | 381 | def get_base_file(self): |