1 """Function/variables common to all the commands
5 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
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.
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.
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
21 import sys, os, os.path, re
22 from optparse import OptionParser, make_option
24 from stgit.utils import *
25 from stgit.out import *
26 from stgit import stack, git, basedir
27 from stgit.config import config, file_extensions
32 # Command exception class
33 class CmdException(Exception):
37 class RevParseException(Exception):
38 """Revision spec parse error."""
42 """Parse a revision specification into its
43 patchname@branchname//patch_id parts. If no branch name has a slash
44 in it, also accept / instead of //."""
45 if '/' in ''.join(git.get_heads()):
46 # We have branch names with / in them.
47 branch_chars = r'[^@]'
50 # No / in branch names.
51 branch_chars = r'[^@/]'
52 patch_id_mark = r'(/|//)'
53 patch_re = r'(?P<patch>[^@/]+)'
54 branch_re = r'@(?P<branch>%s+)' % branch_chars
55 patch_id_re = r'%s(?P<patch_id>[a-z.]*)' % patch_id_mark
58 m = re.match(r'^%s$' % patch_id_re, rev)
60 return None, None, m.group('patch_id')
62 # Try path[@branch]//patch_id.
63 m = re.match(r'^%s(%s)?%s$' % (patch_re, branch_re, patch_id_re), rev)
65 return m.group('patch'), m.group('branch'), m.group('patch_id')
68 m = re.match(r'^%s(%s)?$' % (patch_re, branch_re), rev)
70 return m.group('patch'), m.group('branch'), None
72 # No, we can't parse that.
73 raise RevParseException
81 patch, branch, patch_id = parse_rev(rev)
85 series = stack.Series(branch)
87 patch = series.get_current()
89 raise CmdException, 'No patches applied'
90 if patch in series.get_applied() or patch in series.get_unapplied() or \
91 patch in series.get_hidden():
92 if patch_id in ['top', '', None]:
93 return series.get_patch(patch).get_top()
94 elif patch_id == 'bottom':
95 return series.get_patch(patch).get_bottom()
96 elif patch_id == 'top.old':
97 return series.get_patch(patch).get_old_top()
98 elif patch_id == 'bottom.old':
99 return series.get_patch(patch).get_old_bottom()
100 elif patch_id == 'log':
101 return series.get_patch(patch).get_log()
102 if patch == 'base' and patch_id == None:
103 return series.get_base()
104 except RevParseException:
106 return git.rev_parse(rev + '^{commit}')
108 def check_local_changes():
109 if git.local_changes():
110 raise CmdException, \
111 'local changes in the tree. Use "refresh" or "status --reset"'
113 def check_head_top_equal():
114 if not crt_series.head_top_equal():
116 'HEAD and top are not the same. You probably committed\n'
117 ' changes to the tree outside of StGIT. To bring them\n'
118 ' into StGIT, use the "assimilate" command')
120 def check_conflicts():
121 if os.path.exists(os.path.join(basedir.get(), 'conflicts')):
122 raise CmdException, \
123 'Unsolved conflicts. Please resolve them first or\n' \
124 ' revert the changes with "status --reset"'
126 def print_crt_patch(branch = None):
128 patch = crt_series.get_current()
130 patch = stack.Series(branch).get_current()
133 out.info('Now at patch "%s"' % patch)
135 out.info('No patches applied')
137 def resolved(filename, reset = None):
139 reset_file = filename + file_extensions()[reset]
140 if os.path.isfile(reset_file):
141 if os.path.isfile(filename):
143 os.rename(reset_file, filename)
145 git.update_cache([filename], force = True)
147 for ext in file_extensions().values():
149 if os.path.isfile(fn):
152 def resolved_all(reset = None):
153 conflicts = git.get_conflicts()
155 for filename in conflicts:
156 resolved(filename, reset)
157 os.remove(os.path.join(basedir.get(), 'conflicts'))
159 def push_patches(patches, check_merged = False):
160 """Push multiple patches onto the stack. This function is shared
161 between the push and pull commands
163 forwarded = crt_series.forward_patches(patches)
165 out.info('Fast-forwarded patches "%s" - "%s"'
166 % (patches[0], patches[forwarded - 1]))
168 out.info('Fast-forwarded patch "%s"' % patches[0])
170 names = patches[forwarded:]
172 # check for patches merged upstream
173 if names and check_merged:
174 out.start('Checking for patches merged upstream')
176 merged = crt_series.merged_patches(names)
178 out.done('%d found' % len(merged))
183 out.start('Pushing patch "%s"' % p)
186 crt_series.push_empty_patch(p)
187 out.done('merged upstream')
189 modified = crt_series.push_patch(p)
191 if crt_series.empty_patch(p):
192 out.done('empty patch')
198 def pop_patches(patches, keep = False):
199 """Pop the patches in the list from the stack. It is assumed that
200 the patches are listed in the stack reverse order.
202 if len(patches) == 0:
203 out.info('Nothing to push/pop')
206 if len(patches) == 1:
207 out.start('Popping patch "%s"' % p)
209 out.start('Popping patches "%s" - "%s"' % (patches[0], p))
210 crt_series.pop_patch(p, keep)
213 def parse_patches(patch_args, patch_list, boundary = 0, ordered = False):
214 """Parse patch_args list for patch names in patch_list and return
215 a list. The names can be individual patches and/or in the
216 patch1..patch2 format.
220 for name in patch_args:
221 pair = name.split('..')
223 if p and not p in patch_list:
224 raise CmdException, 'Unknown patch name: %s' % p
230 # patch range [p1]..[p2]
233 first = patch_list.index(pair[0])
238 last = patch_list.index(pair[1]) + 1
242 # only cross the boundary if explicitly asked
244 boundary = len(patch_list)
254 last = len(patch_list)
257 pl = patch_list[first:last]
259 pl = patch_list[(last - 1):(first + 1)]
262 raise CmdException, 'Malformed patch name: %s' % name
266 raise CmdException, 'Duplicate patch name: %s' % p
271 patches = [p for p in patch_list if p in patches]
275 def name_email(address):
276 """Return a tuple consisting of the name and email parsed from a
277 standard 'name <email>' or 'email (name)' string
279 address = re.sub('[\\\\"]', '\\\\\g<0>', address)
280 str_list = re.findall('^(.*)\s*<(.*)>\s*$', address)
282 str_list = re.findall('^(.*)\s*\((.*)\)\s*$', address)
284 raise CmdException, 'Incorrect "name <email>"/"email (name)" string: %s' % address
285 return ( str_list[0][1], str_list[0][0] )
289 def name_email_date(address):
290 """Return a tuple consisting of the name, email and date parsed
291 from a 'name <email> date' string
293 address = re.sub('[\\\\"]', '\\\\\g<0>', address)
294 str_list = re.findall('^(.*)\s*<(.*)>\s*(.*)\s*$', address)
296 raise CmdException, 'Incorrect "name <email> date" string: %s' % address
300 def address_or_alias(addr_str):
301 """Return the address if it contains an e-mail address or look up
302 the aliases in the config files.
304 def __address_or_alias(addr):
307 if addr.find('@') >= 0:
308 # it's an e-mail address
310 alias = config.get('mail.alias.'+addr)
314 raise CmdException, 'unknown e-mail alias: %s' % addr
316 addr_list = [__address_or_alias(addr.strip())
317 for addr in addr_str.split(',')]
318 return ', '.join([addr for addr in addr_list if addr])
320 def prepare_rebase(force=None):
322 # Be sure we won't loose results of stg-(un)commit by error.
323 # Do not require an existing orig-base for compatibility with 0.12 and earlier.
324 origbase = crt_series._get_field('orig-base')
325 if origbase and crt_series.get_base() != origbase:
326 raise CmdException, 'Rebasing would possibly lose data'
329 applied = crt_series.get_applied()
331 out.start('Popping all applied patches')
332 crt_series.pop_patch(applied[0])
338 tree_id = git_id(target)
340 # it might be that we use a custom rebase command with its own
343 if tree_id == git.get_head():
344 out.info('Already at "%s", no need for rebasing.' % target)
347 out.start('Rebasing to "%s"' % target)
349 out.start('Rebasing to the default target')
350 git.rebase(tree_id = tree_id)
353 def post_rebase(applied, nopush, merged):
354 # memorize that we rebased to here
355 crt_series._set_field('orig-base', git.get_head())
356 # push the patches back
358 push_patches(applied, merged)
361 # Patch description/e-mail/diff parsing
363 def __end_descr(line):
364 return re.match('---\s*$', line) or re.match('diff -', line) or \
365 re.match('Index: ', line)
367 def __split_descr_diff(string):
368 """Return the description and the diff from the given string
373 for line in string.split('\n'):
375 if not __end_descr(line):
382 return (descr.rstrip(), diff)
384 def __parse_description(descr):
385 """Parse the patch description and return the new description and
386 author information (if any).
389 authname = authemail = authdate = None
391 descr_lines = [line.rstrip() for line in descr.split('\n')]
393 raise CmdException, "Empty patch description"
396 end = len(descr_lines)
398 # Parse the patch header
399 for pos in range(0, end):
400 if not descr_lines[pos]:
402 # check for a "From|Author:" line
403 if re.match('\s*(?:from|author):\s+', descr_lines[pos], re.I):
404 auth = re.findall('^.*?:\s+(.*)$', descr_lines[pos])[0]
405 authname, authemail = name_email(auth)
408 # check for a "Date:" line
409 if re.match('\s*date:\s+', descr_lines[pos], re.I):
410 authdate = re.findall('^.*?:\s+(.*)$', descr_lines[pos])[0]
416 subject = descr_lines[pos]
421 body = reduce(lambda x, y: x + '\n' + y, descr_lines[lasthdr:], '')
423 return (subject + body, authname, authemail, authdate)
426 """Parse the message object and return (description, authname,
427 authemail, authdate, diff)
429 from email.Header import decode_header, make_header
431 def __decode_header(header):
432 """Decode a qp-encoded e-mail header as per rfc2047"""
434 words_enc = decode_header(header)
435 hobj = make_header(words_enc)
436 except Exception, ex:
437 raise CmdException, 'header decoding error: %s' % str(ex)
438 return unicode(hobj).encode('utf-8')
441 if msg.has_key('from'):
442 authname, authemail = name_email(__decode_header(msg['from']))
444 authname = authemail = None
446 # '\n\t' can be found on multi-line headers
447 descr = __decode_header(msg['subject']).replace('\n\t', ' ')
448 authdate = msg['date']
450 # remove the '[*PATCH*]' expression in the subject
452 descr = re.findall('^(\[.*?[Pp][Aa][Tt][Cc][Hh].*?\])?\s*(.*)$',
455 raise CmdException, 'Subject: line not found'
457 # the rest of the message
459 for part in msg.walk():
460 if part.get_content_type() == 'text/plain':
461 msg_text += part.get_payload(decode = True)
463 rem_descr, diff = __split_descr_diff(msg_text)
465 descr += '\n\n' + rem_descr
467 # parse the description for author information
468 descr, descr_authname, descr_authemail, descr_authdate = \
469 __parse_description(descr)
471 authname = descr_authname
473 authemail = descr_authemail
475 authdate = descr_authdate
477 return (descr, authname, authemail, authdate, diff)
479 def parse_patch(fobj):
480 """Parse the input file and return (description, authname,
481 authemail, authdate, diff)
483 descr, diff = __split_descr_diff(fobj.read())
484 descr, authname, authemail, authdate = __parse_description(descr)
486 # we don't yet have an agreed place for the creation date.
488 return (descr, authname, authemail, authdate, diff)