Never skip rebase
[stgit] / stgit / commands / common.py
1 """Function/variables common to all the commands
2 """
3
4 __copyright__ = """
5 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 """
20
21 import sys, os, os.path, re, email.Utils
22 from stgit.exception import *
23 from stgit.utils import *
24 from stgit.out import *
25 from stgit.run import *
26 from stgit import stack, git, basedir
27 from stgit.config import config, file_extensions
28 from stgit.lib import stack as libstack
29 from stgit.lib import git as libgit
30 from stgit.lib import log
31
32 # Command exception class
33 class CmdException(StgException):
34 pass
35
36 # Utility functions
37 def parse_rev(rev):
38 """Parse a revision specification into its branch:patch parts.
39 """
40 try:
41 branch, patch = rev.split(':', 1)
42 except ValueError:
43 branch = None
44 patch = rev
45
46 return (branch, patch)
47
48 def git_id(crt_series, rev):
49 """Return the GIT id
50 """
51 # TODO: remove this function once all the occurrences were converted
52 # to git_commit()
53 repository = libstack.Repository.default()
54 return git_commit(rev, repository, crt_series.get_name()).sha1
55
56 def get_public_ref(branch_name):
57 """Return the public ref of the branch."""
58 public_ref = config.get('branch.%s.public' % branch_name)
59 if not public_ref:
60 public_ref = 'refs/heads/%s.public' % branch_name
61 return public_ref
62
63 def git_commit(name, repository, branch_name = None):
64 """Return the a Commit object if 'name' is a patch name or Git commit.
65 The patch names allowed are in the form '<branch>:<patch>' and can
66 be followed by standard symbols used by git rev-parse. If <patch>
67 is '{base}', it represents the bottom of the stack. If <patch> is
68 {public}, it represents the public branch corresponding to the stack as
69 described in the 'publish' command.
70 """
71 # Try a [branch:]patch name first
72 branch, patch = parse_rev(name)
73 if not branch:
74 branch = branch_name or repository.current_branch_name
75
76 # The stack base
77 if patch.startswith('{base}'):
78 base_id = repository.get_stack(branch).base.sha1
79 return repository.rev_parse(base_id +
80 strip_prefix('{base}', patch))
81 elif patch.startswith('{public}'):
82 public_ref = get_public_ref(branch)
83 return repository.rev_parse(public_ref +
84 strip_prefix('{public}', patch),
85 discard_stderr = True)
86
87 # Other combination of branch and patch
88 try:
89 return repository.rev_parse('patches/%s/%s' % (branch, patch),
90 discard_stderr = True)
91 except libgit.RepositoryException:
92 pass
93
94 # Try a Git commit
95 try:
96 return repository.rev_parse(name, discard_stderr = True)
97 except libgit.RepositoryException:
98 raise CmdException('%s: Unknown patch or revision name' % name)
99
100 def color_diff_flags():
101 """Return the git flags for coloured diff output if the configuration and
102 stdout allows."""
103 stdout_is_tty = (sys.stdout.isatty() and 'true') or 'false'
104 if config.get_colorbool('color.diff', stdout_is_tty) == 'true':
105 return ['--color']
106 else:
107 return []
108
109 def check_local_changes():
110 if git.local_changes():
111 raise CmdException('local changes in the tree. Use "refresh" or'
112 ' "status --reset"')
113
114 def check_head_top_equal(crt_series):
115 if not crt_series.head_top_equal():
116 raise CmdException('HEAD and top are not the same. This can happen'
117 ' if you modify a branch with git. "stg repair'
118 ' --help" explains more about what to do next.')
119
120 def check_conflicts():
121 if git.get_conflicts():
122 raise CmdException('Unsolved conflicts. Please fix the conflicts'
123 ' then use "git add --update <files>" or revert the'
124 ' changes with "status --reset".')
125
126 def print_crt_patch(crt_series, branch = None):
127 if not branch:
128 patch = crt_series.get_current()
129 else:
130 patch = stack.Series(branch).get_current()
131
132 if patch:
133 out.info('Now at patch "%s"' % patch)
134 else:
135 out.info('No patches applied')
136
137 def resolved_all(reset = None):
138 conflicts = git.get_conflicts()
139 git.resolved(conflicts, reset)
140
141 def push_patches(crt_series, patches, check_merged = False):
142 """Push multiple patches onto the stack. This function is shared
143 between the push and pull commands
144 """
145 forwarded = crt_series.forward_patches(patches)
146 if forwarded > 1:
147 out.info('Fast-forwarded patches "%s" - "%s"'
148 % (patches[0], patches[forwarded - 1]))
149 elif forwarded == 1:
150 out.info('Fast-forwarded patch "%s"' % patches[0])
151
152 names = patches[forwarded:]
153
154 # check for patches merged upstream
155 if names and check_merged:
156 out.start('Checking for patches merged upstream')
157
158 merged = crt_series.merged_patches(names)
159
160 out.done('%d found' % len(merged))
161 else:
162 merged = []
163
164 for p in names:
165 out.start('Pushing patch "%s"' % p)
166
167 if p in merged:
168 crt_series.push_empty_patch(p)
169 out.done('merged upstream')
170 else:
171 modified = crt_series.push_patch(p)
172
173 if crt_series.empty_patch(p):
174 out.done('empty patch')
175 elif modified:
176 out.done('modified')
177 else:
178 out.done()
179
180 def pop_patches(crt_series, patches, keep = False):
181 """Pop the patches in the list from the stack. It is assumed that
182 the patches are listed in the stack reverse order.
183 """
184 if len(patches) == 0:
185 out.info('Nothing to push/pop')
186 else:
187 p = patches[-1]
188 if len(patches) == 1:
189 out.start('Popping patch "%s"' % p)
190 else:
191 out.start('Popping patches "%s" - "%s"' % (patches[0], p))
192 crt_series.pop_patch(p, keep)
193 out.done()
194
195 def parse_patches(patch_args, patch_list, boundary = 0, ordered = False):
196 """Parse patch_args list for patch names in patch_list and return
197 a list. The names can be individual patches and/or in the
198 patch1..patch2 format.
199 """
200 # in case it receives a tuple
201 patch_list = list(patch_list)
202 patches = []
203
204 for name in patch_args:
205 pair = name.split('..')
206 for p in pair:
207 if p and not p in patch_list:
208 raise CmdException, 'Unknown patch name: %s' % p
209
210 if len(pair) == 1:
211 # single patch name
212 pl = pair
213 elif len(pair) == 2:
214 # patch range [p1]..[p2]
215 # inclusive boundary
216 if pair[0]:
217 first = patch_list.index(pair[0])
218 else:
219 first = -1
220 # exclusive boundary
221 if pair[1]:
222 last = patch_list.index(pair[1]) + 1
223 else:
224 last = -1
225
226 # only cross the boundary if explicitly asked
227 if not boundary:
228 boundary = len(patch_list)
229 if first < 0:
230 if last <= boundary:
231 first = 0
232 else:
233 first = boundary
234 if last < 0:
235 if first < boundary:
236 last = boundary
237 else:
238 last = len(patch_list)
239
240 if last > first:
241 pl = patch_list[first:last]
242 else:
243 pl = patch_list[(last - 1):(first + 1)]
244 pl.reverse()
245 else:
246 raise CmdException, 'Malformed patch name: %s' % name
247
248 for p in pl:
249 if p in patches:
250 raise CmdException, 'Duplicate patch name: %s' % p
251
252 patches += pl
253
254 if ordered:
255 patches = [p for p in patch_list if p in patches]
256
257 return patches
258
259 def name_email(address):
260 p = email.Utils.parseaddr(address)
261 if p[1]:
262 return p
263 else:
264 raise CmdException('Incorrect "name <email>"/"email (name)" string: %s'
265 % address)
266
267 def name_email_date(address):
268 p = parse_name_email_date(address)
269 if p:
270 return p
271 else:
272 raise CmdException('Incorrect "name <email> date" string: %s' % address)
273
274 def address_or_alias(addr_pair):
275 """Return a name-email tuple the e-mail address is valid or look up
276 the aliases in the config files.
277 """
278 addr = addr_pair[1]
279 if '@' in addr:
280 # it's an e-mail address
281 return addr_pair
282 alias = config.get('mail.alias.' + addr)
283 if alias:
284 # it's an alias
285 return name_email(alias)
286 raise CmdException, 'unknown e-mail alias: %s' % addr
287
288 def prepare_rebase(crt_series):
289 # pop all patches
290 applied = crt_series.get_applied()
291 if len(applied) > 0:
292 out.start('Popping all applied patches')
293 crt_series.pop_patch(applied[0])
294 out.done()
295 return applied
296
297 def rebase(crt_series, target):
298 try:
299 tree_id = git_id(crt_series, target)
300 except:
301 # it might be that we use a custom rebase command with its own
302 # target type
303 tree_id = target
304 if target:
305 out.start('Rebasing to "%s"' % target)
306 else:
307 out.start('Rebasing to the default target')
308 git.rebase(tree_id = tree_id)
309 out.done()
310
311 def post_rebase(crt_series, applied, nopush, merged):
312 # memorize that we rebased to here
313 crt_series._set_field('orig-base', git.get_head())
314 # push the patches back
315 if not nopush:
316 push_patches(crt_series, applied, merged)
317
318 #
319 # Patch description/e-mail/diff parsing
320 #
321 def __end_descr(line):
322 return re.match('---\s*$', line) or re.match('diff -', line) or \
323 re.match('Index: ', line) or re.match('--- \w', line)
324
325 def __split_descr_diff(string):
326 """Return the description and the diff from the given string
327 """
328 descr = diff = ''
329 top = True
330
331 for line in string.split('\n'):
332 if top:
333 if not __end_descr(line):
334 descr += line + '\n'
335 continue
336 else:
337 top = False
338 diff += line + '\n'
339
340 return (descr.rstrip(), diff)
341
342 def __parse_description(descr):
343 """Parse the patch description and return the new description and
344 author information (if any).
345 """
346 subject = body = ''
347 authname = authemail = authdate = None
348
349 descr_lines = [line.rstrip() for line in descr.split('\n')]
350 if not descr_lines:
351 raise CmdException, "Empty patch description"
352
353 lasthdr = 0
354 end = len(descr_lines)
355 descr_strip = 0
356
357 # Parse the patch header
358 for pos in range(0, end):
359 if not descr_lines[pos]:
360 continue
361 # check for a "From|Author:" line
362 if re.match('\s*(?:from|author):\s+', descr_lines[pos], re.I):
363 auth = re.findall('^.*?:\s+(.*)$', descr_lines[pos])[0]
364 authname, authemail = name_email(auth)
365 lasthdr = pos + 1
366 continue
367 # check for a "Date:" line
368 if re.match('\s*date:\s+', descr_lines[pos], re.I):
369 authdate = re.findall('^.*?:\s+(.*)$', descr_lines[pos])[0]
370 lasthdr = pos + 1
371 continue
372 if subject:
373 break
374 # get the subject
375 subject = descr_lines[pos][descr_strip:]
376 if re.match('commit [\da-f]{40}$', subject):
377 # 'git show' output, look for the real subject
378 subject = ''
379 descr_strip = 4
380 lasthdr = pos + 1
381
382 # get the body
383 if lasthdr < end:
384 body = '\n' + '\n'.join(l[descr_strip:] for l in descr_lines[lasthdr:])
385
386 return (subject + body, authname, authemail, authdate)
387
388 def parse_mail(msg):
389 """Parse the message object and return (description, authname,
390 authemail, authdate, diff)
391 """
392 from email.Header import decode_header, make_header
393
394 def __decode_header(header):
395 """Decode a qp-encoded e-mail header as per rfc2047"""
396 try:
397 words_enc = decode_header(header)
398 hobj = make_header(words_enc)
399 except Exception, ex:
400 raise CmdException, 'header decoding error: %s' % str(ex)
401 return unicode(hobj).encode('utf-8')
402
403 # parse the headers
404 if msg.has_key('from'):
405 authname, authemail = name_email(__decode_header(msg['from']))
406 else:
407 authname = authemail = None
408
409 # '\n\t' can be found on multi-line headers
410 descr = __decode_header(msg['subject'])
411 descr = re.sub('\n[ \t]*', ' ', descr)
412 authdate = msg['date']
413
414 # remove the '[*PATCH*]' expression in the subject
415 if descr:
416 descr = re.findall('^(\[.*?[Pp][Aa][Tt][Cc][Hh].*?\])?\s*(.*)$',
417 descr)[0][1]
418 else:
419 raise CmdException, 'Subject: line not found'
420
421 # the rest of the message
422 msg_text = ''
423 for part in msg.walk():
424 if part.get_content_type() in ['text/plain',
425 'application/octet-stream']:
426 msg_text += part.get_payload(decode = True)
427
428 rem_descr, diff = __split_descr_diff(msg_text)
429 if rem_descr:
430 descr += '\n\n' + rem_descr
431
432 # parse the description for author information
433 descr, descr_authname, descr_authemail, descr_authdate = \
434 __parse_description(descr)
435 if descr_authname:
436 authname = descr_authname
437 if descr_authemail:
438 authemail = descr_authemail
439 if descr_authdate:
440 authdate = descr_authdate
441
442 return (descr, authname, authemail, authdate, diff)
443
444 def parse_patch(text, contains_diff):
445 """Parse the input text and return (description, authname,
446 authemail, authdate, diff)
447 """
448 if contains_diff:
449 (text, diff) = __split_descr_diff(text)
450 else:
451 diff = None
452 (descr, authname, authemail, authdate) = __parse_description(text)
453
454 # we don't yet have an agreed place for the creation date.
455 # Just return None
456 return (descr, authname, authemail, authdate, diff)
457
458 def readonly_constant_property(f):
459 """Decorator that converts a function that computes a value to an
460 attribute that returns the value. The value is computed only once,
461 the first time it is accessed."""
462 def new_f(self):
463 n = '__' + f.__name__
464 if not hasattr(self, n):
465 setattr(self, n, f(self))
466 return getattr(self, n)
467 return property(new_f)
468
469 def update_commit_data(cd, options):
470 """Return a new CommitData object updated according to the command line
471 options."""
472 # Set the commit message from commandline.
473 if options.message != None:
474 cd = cd.set_message(options.message)
475
476 # Modify author data.
477 cd = cd.set_author(options.author(cd.author))
478
479 # Add Signed-off-by: or similar.
480 if options.sign_str != None:
481 sign_str = options.sign_str
482 else:
483 sign_str = config.get("stgit.autosign")
484 if sign_str != None:
485 cd = cd.set_message(
486 add_sign_line(cd.message, sign_str,
487 cd.committer.name, cd.committer.email))
488
489 # Let user edit the commit message manually, unless
490 # --save-template or --message was specified.
491 if not getattr(options, 'save_template', None) and not options.message:
492 cd = cd.set_message(edit_string(cd.message, '.stgit-new.txt'))
493
494 return cd
495
496 class DirectoryException(StgException):
497 pass
498
499 class _Directory(object):
500 def __init__(self, needs_current_series = True, log = True):
501 self.needs_current_series = needs_current_series
502 self.log = log
503 @readonly_constant_property
504 def git_dir(self):
505 try:
506 return Run('git', 'rev-parse', '--git-dir'
507 ).discard_stderr().output_one_line()
508 except RunException:
509 raise DirectoryException('No git repository found')
510 @readonly_constant_property
511 def __topdir_path(self):
512 try:
513 lines = Run('git', 'rev-parse', '--show-cdup'
514 ).discard_stderr().output_lines()
515 if len(lines) == 0:
516 return '.'
517 elif len(lines) == 1:
518 return lines[0]
519 else:
520 raise RunException('Too much output')
521 except RunException:
522 raise DirectoryException('No git repository found')
523 @readonly_constant_property
524 def is_inside_git_dir(self):
525 return { 'true': True, 'false': False
526 }[Run('git', 'rev-parse', '--is-inside-git-dir'
527 ).output_one_line()]
528 @readonly_constant_property
529 def is_inside_worktree(self):
530 return { 'true': True, 'false': False
531 }[Run('git', 'rev-parse', '--is-inside-work-tree'
532 ).output_one_line()]
533 def cd_to_topdir(self):
534 os.chdir(self.__topdir_path)
535 def write_log(self, msg):
536 if self.log:
537 log.compat_log_entry(msg)
538
539 class DirectoryAnywhere(_Directory):
540 def setup(self):
541 pass
542
543 class DirectoryHasRepository(_Directory):
544 def setup(self):
545 self.git_dir # might throw an exception
546 log.compat_log_external_mods()
547
548 class DirectoryInWorktree(DirectoryHasRepository):
549 def setup(self):
550 DirectoryHasRepository.setup(self)
551 if not self.is_inside_worktree:
552 raise DirectoryException('Not inside a git worktree')
553
554 class DirectoryGotoToplevel(DirectoryInWorktree):
555 def setup(self):
556 DirectoryInWorktree.setup(self)
557 self.cd_to_topdir()
558
559 class DirectoryHasRepositoryLib(_Directory):
560 """For commands that use the new infrastructure in stgit.lib.*."""
561 def __init__(self):
562 self.needs_current_series = False
563 self.log = False # stgit.lib.transaction handles logging
564 def setup(self):
565 # This will throw an exception if we don't have a repository.
566 self.repository = libstack.Repository.default()