2 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License version 2 as
6 published by the Free Software Foundation.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 import sys, os, re, email
19 from email.Header import decode_header, make_header
20 from mailbox import UnixMailbox
21 from StringIO import StringIO
22 from optparse import OptionParser, make_option
24 from stgit.commands.common import *
25 from stgit.utils import *
26 from stgit import stack, git
29 help = 'import a GNU diff file as a new patch'
30 usage = """%prog [options] [<file>]
32 Create a new patch and apply the given GNU diff file (or the standard
33 input). By default, the file name is used as the patch name but this
34 can be overridden with the '--name' option. The patch can either be a
35 normal file with the description at the top or it can have standard
36 mail format, the Subject, From and Date headers being used for
37 generating the patch information. The command can also read series and
40 If a patch does not apply cleanly, the failed diff is written to the
41 .stgit-failed.patch file and an empty StGIT patch is added to the
44 The patch description has to be separated from the data with a '---'
47 options = [make_option('-m', '--mail',
48 help = 'import the patch from a standard e-mail file',
49 action = 'store_true'),
50 make_option('-M', '--mbox',
51 help = 'import a series of patches from an mbox file',
52 action = 'store_true'),
53 make_option('-s', '--series',
54 help = 'import a series of patches',
55 action = 'store_true'),
56 make_option('-n', '--name',
57 help = 'use NAME as the patch name'),
58 make_option('-t', '--strip',
59 help = 'strip numbering and extension from patch name',
60 action = 'store_true'),
61 make_option('-i', '--ignore',
62 help = 'ignore the applied patches in the series',
63 action = 'store_true'),
64 make_option('--replace',
65 help = 'replace the unapplied patches in the series',
66 action = 'store_true'),
67 make_option('-b', '--base',
68 help = 'use BASE instead of HEAD for file importing'),
69 make_option('-e', '--edit',
70 help = 'invoke an editor for the patch description',
71 action = 'store_true'),
72 make_option('-p', '--showpatch',
73 help = 'show the patch content in the editor buffer',
74 action = 'store_true'),
75 make_option('-a', '--author', metavar = '"NAME <EMAIL>"',
76 help = 'use "NAME <EMAIL>" as the author details'),
77 make_option('--authname',
78 help = 'use AUTHNAME as the author name'),
79 make_option('--authemail',
80 help = 'use AUTHEMAIL as the author e-mail'),
81 make_option('--authdate',
82 help = 'use AUTHDATE as the author date'),
83 make_option('--commname',
84 help = 'use COMMNAME as the committer name'),
85 make_option('--commemail',
86 help = 'use COMMEMAIL as the committer e-mail')]
89 def __end_descr(line):
90 return re.match('---\s*$', line) or re.match('diff -', line) or \
91 re.match('Index: ', line)
93 def __strip_patch_name(name):
94 stripped = re.sub('^[0-9]+-(.*)$', '\g<1>', name)
95 stripped = re.sub('^(.*)\.(diff|patch)$', '\g<1>', stripped)
99 def __replace_slashes_with_dashes(name):
100 stripped = name.replace('/', '-')
104 def __parse_description(descr):
105 """Parse the patch description and return the new description and
106 author information (if any).
109 authname = authemail = authdate = None
111 descr_lines = [line.rstrip() for line in descr.split('\n')]
113 raise CmdException, "Empty patch description"
116 end = len(descr_lines)
118 # Parse the patch header
119 for pos in range(0, end):
120 if not descr_lines[pos]:
122 # check for a "From|Author:" line
123 if re.match('\s*(?:from|author):\s+', descr_lines[pos], re.I):
124 auth = re.findall('^.*?:\s+(.*)$', descr_lines[pos])[0]
125 authname, authemail = name_email(auth)
128 # check for a "Date:" line
129 if re.match('\s*date:\s+', descr_lines[pos], re.I):
130 authdate = re.findall('^.*?:\s+(.*)$', descr_lines[pos])[0]
136 subject = descr_lines[pos]
141 body = reduce(lambda x, y: x + '\n' + y, descr_lines[lasthdr:], '')
143 return (subject + body, authname, authemail, authdate)
145 def __parse_mail(msg):
146 """Parse the message object and return (description, authname,
147 authemail, authdate, diff)
149 def __decode_header(header):
150 """Decode a qp-encoded e-mail header as per rfc2047"""
152 words_enc = decode_header(header)
153 hobj = make_header(words_enc)
154 except Exception, ex:
155 raise CmdException, 'header decoding error: %s' % str(ex)
156 return unicode(hobj).encode('utf-8')
159 if msg.has_key('from'):
160 authname, authemail = name_email(__decode_header(msg['from']))
162 authname = authemail = None
164 # '\n\t' can be found on multi-line headers
165 descr = __decode_header(msg['subject']).replace('\n\t', ' ')
166 authdate = msg['date']
168 # remove the '[*PATCH*]' expression in the subject
170 descr = re.findall('^(\[.*?[Pp][Aa][Tt][Cc][Hh].*?\])?\s*(.*)$',
174 raise CmdException, 'Subject: line not found'
176 # the rest of the message
177 if msg.is_multipart():
178 # this is assuming that the first part is the patch
179 # description and the second part is the attached patch
180 descr += msg.get_payload(0).get_payload(decode = True)
181 diff = msg.get_payload(1).get_payload(decode = True)
183 diff = msg.get_payload(decode = True)
185 for line in diff.split('\n'):
186 if __end_descr(line):
192 # parse the description for author information
193 descr, descr_authname, descr_authemail, descr_authdate = \
194 __parse_description(descr)
196 authname = descr_authname
198 authemail = descr_authemail
200 authdate = descr_authdate
202 return (descr, authname, authemail, authdate, diff)
204 def __parse_patch(fobj):
205 """Parse the input file and return (description, authname,
206 authemail, authdate, diff)
210 line = fobj.readline()
214 if __end_descr(line):
222 descr, authname, authemail, authdate = __parse_description(descr)
224 # we don't yet have an agreed place for the creation date.
226 return (descr, authname, authemail, authdate, diff)
228 def __create_patch(patch, message, author_name, author_email,
229 author_date, diff, options):
230 """Create a new patch on the stack
233 raise CmdException, 'No diff found inside the patch'
236 patch = make_patch_name(message, crt_series.patch_exists,
237 alternative = not (options.ignore
240 if options.ignore and patch in crt_series.get_applied():
241 print 'Ignoring already applied patch "%s"' % patch
243 if options.replace and patch in crt_series.get_unapplied():
244 crt_series.delete_patch(patch)
246 # refresh_patch() will invoke the editor in this case, with correct
251 committer_name = committer_email = None
254 options.authname, options.authemail = name_email(options.author)
256 # override the automatically parsed settings
258 author_name = options.authname
259 if options.authemail:
260 author_email = options.authemail
262 author_date = options.authdate
264 committer_name = options.commname
265 if options.commemail:
266 committer_email = options.commemail
268 crt_series.new_patch(patch, message = message, can_edit = False,
269 author_name = author_name,
270 author_email = author_email,
271 author_date = author_date,
272 committer_name = committer_name,
273 committer_email = committer_email)
275 print 'Importing patch "%s"...' % patch,
279 git.apply_patch(diff = diff, base = git_id(options.base))
281 git.apply_patch(diff = diff)
283 crt_series.refresh_patch(edit = options.edit,
284 show_patch = options.showpatch)
288 def __import_file(patch, filename, options):
289 """Import a patch from a file or standard input
298 msg = email.message_from_file(f)
299 except Exception, ex:
300 raise CmdException, 'error parsing the e-mail file: %s' % str(ex)
301 message, author_name, author_email, author_date, diff = \
304 message, author_name, author_email, author_date, diff = \
310 __create_patch(patch, message, author_name, author_email,
311 author_date, diff, options)
313 def __import_series(filename, options):
314 """Import a series of patches
316 applied = crt_series.get_applied()
320 patchdir = os.path.dirname(filename)
326 patch = re.sub('#.*$', '', line).strip()
329 patchfile = os.path.join(patchdir, patch)
332 patch = __strip_patch_name(patch)
333 patch = __replace_slashes_with_dashes(patch);
335 __import_file(patch, patchfile, options)
340 def __import_mbox(filename, options):
341 """Import a series from an mbox file
344 f = file(filename, 'rb')
346 f = StringIO(sys.stdin.read())
349 mbox = UnixMailbox(f, email.message_from_file)
350 except Exception, ex:
351 raise CmdException, 'error parsing the mbox file: %s' % str(ex)
354 message, author_name, author_email, author_date, diff = \
356 __create_patch(None, message, author_name, author_email,
357 author_date, diff, options)
361 def func(parser, options, args):
362 """Import a GNU diff file as a new patch
365 parser.error('incorrect number of arguments')
367 check_local_changes()
369 check_head_top_equal()
377 __import_series(filename, options)
379 __import_mbox(filename, options)
384 patch = os.path.basename(filename)
388 patch = __strip_patch_name(patch)
390 __import_file(patch, filename, options)