Fix the importing of multipart emails
[stgit] / stgit / commands / imprt.py
CommitLineData
0d2cd1e4
CM
1__copyright__ = """
2Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License version 2 as
6published by the Free Software Foundation.
7
8This program is distributed in the hope that it will be useful,
9but WITHOUT ANY WARRANTY; without even the implied warranty of
10MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License
14along with this program; if not, write to the Free Software
15Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16"""
17
6ef533bc 18import sys, os, re, email
2ac5a14c 19from email.Header import decode_header, make_header
99c52915 20from mailbox import UnixMailbox
457c3093 21from StringIO import StringIO
0d2cd1e4
CM
22from optparse import OptionParser, make_option
23
24from stgit.commands.common import *
25from stgit.utils import *
26from stgit import stack, git
27
28
29help = 'import a GNU diff file as a new patch'
575c575e 30usage = """%prog [options] [<file>|<url>]
0d2cd1e4 31
b8a0986f
CM
32Create a new patch and apply the given GNU diff file (or the standard
33input). By default, the file name is used as the patch name but this
388f63b6 34can be overridden with the '--name' option. The patch can either be a
b8a0986f
CM
35normal file with the description at the top or it can have standard
36mail format, the Subject, From and Date headers being used for
99c52915
CM
37generating the patch information. The command can also read series and
38mbox files.
39
40If 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
42stack.
0d2cd1e4 43
b8a0986f 44The patch description has to be separated from the data with a '---'
99e73103 45line."""
0d2cd1e4
CM
46
47options = [make_option('-m', '--mail',
48 help = 'import the patch from a standard e-mail file',
49 action = 'store_true'),
99c52915
CM
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'),
575c575e
CW
56 make_option('-u', '--url',
57 help = 'import a patch from a URL',
58 action = 'store_true'),
0d2cd1e4
CM
59 make_option('-n', '--name',
60 help = 'use NAME as the patch name'),
b0cdad5e
CM
61 make_option('-t', '--strip',
62 help = 'strip numbering and extension from patch name',
63 action = 'store_true'),
9417ece4
CM
64 make_option('-i', '--ignore',
65 help = 'ignore the applied patches in the series',
66 action = 'store_true'),
034db15c
CM
67 make_option('--replace',
68 help = 'replace the unapplied patches in the series',
69 action = 'store_true'),
b21bc8d1 70 make_option('-b', '--base',
35344f86 71 help = 'use BASE instead of HEAD for file importing'),
33e580e0
CM
72 make_option('-e', '--edit',
73 help = 'invoke an editor for the patch description',
74 action = 'store_true'),
9417ece4 75 make_option('-p', '--showpatch',
6ad48e48
PBG
76 help = 'show the patch content in the editor buffer',
77 action = 'store_true'),
0d2cd1e4
CM
78 make_option('-a', '--author', metavar = '"NAME <EMAIL>"',
79 help = 'use "NAME <EMAIL>" as the author details'),
80 make_option('--authname',
81 help = 'use AUTHNAME as the author name'),
82 make_option('--authemail',
83 help = 'use AUTHEMAIL as the author e-mail'),
84 make_option('--authdate',
85 help = 'use AUTHDATE as the author date'),
86 make_option('--commname',
87 help = 'use COMMNAME as the committer name'),
88 make_option('--commemail',
89 help = 'use COMMEMAIL as the committer e-mail')]
90
91
d4c43e19
PBG
92def __end_descr(line):
93 return re.match('---\s*$', line) or re.match('diff -', line) or \
94 re.match('Index: ', line)
99e73103 95
b0cdad5e 96def __strip_patch_name(name):
bcb6d890
CM
97 stripped = re.sub('^[0-9]+-(.*)$', '\g<1>', name)
98 stripped = re.sub('^(.*)\.(diff|patch)$', '\g<1>', stripped)
99
100 return stripped
b0cdad5e 101
613a2f16
PBG
102def __replace_slashes_with_dashes(name):
103 stripped = name.replace('/', '-')
104
105 return stripped
106
f21ba536
CM
107def __split_descr_diff(string):
108 """Return the description and the diff from the given string
109 """
110 descr = diff = ''
111 top = True
112
113 for line in string.split('\n'):
114 if top:
115 if not __end_descr(line):
116 descr += line + '\n'
117 continue
118 else:
119 top = False
120 diff += line + '\n'
121
122 return (descr.rstrip(), diff)
123
99e73103
CM
124def __parse_description(descr):
125 """Parse the patch description and return the new description and
126 author information (if any).
127 """
128 subject = body = ''
0543bc5f 129 authname = authemail = authdate = None
99e73103 130
0543bc5f 131 descr_lines = [line.rstrip() for line in descr.split('\n')]
99e73103
CM
132 if not descr_lines:
133 raise CmdException, "Empty patch description"
134
0543bc5f 135 lasthdr = 0
99e73103
CM
136 end = len(descr_lines)
137
0543bc5f 138 # Parse the patch header
61dabd0e 139 for pos in range(0, end):
0543bc5f
TM
140 if not descr_lines[pos]:
141 continue
142 # check for a "From|Author:" line
143 if re.match('\s*(?:from|author):\s+', descr_lines[pos], re.I):
144 auth = re.findall('^.*?:\s+(.*)$', descr_lines[pos])[0]
145 authname, authemail = name_email(auth)
146 lasthdr = pos + 1
147 continue
148 # check for a "Date:" line
149 if re.match('\s*date:\s+', descr_lines[pos], re.I):
150 authdate = re.findall('^.*?:\s+(.*)$', descr_lines[pos])[0]
151 lasthdr = pos + 1
152 continue
153 if subject:
154 break
155 # get the subject
156 subject = descr_lines[pos]
157 lasthdr = pos + 1
99e73103
CM
158
159 # get the body
0543bc5f
TM
160 if lasthdr < end:
161 body = reduce(lambda x, y: x + '\n' + y, descr_lines[lasthdr:], '')
99e73103 162
0543bc5f 163 return (subject + body, authname, authemail, authdate)
99e73103 164
99c52915
CM
165def __parse_mail(msg):
166 """Parse the message object and return (description, authname,
167 authemail, authdate, diff)
0d2cd1e4 168 """
2ac5a14c
CM
169 def __decode_header(header):
170 """Decode a qp-encoded e-mail header as per rfc2047"""
171 try:
172 words_enc = decode_header(header)
173 hobj = make_header(words_enc)
174 except Exception, ex:
175 raise CmdException, 'header decoding error: %s' % str(ex)
176 return unicode(hobj).encode('utf-8')
177
0d2cd1e4 178 # parse the headers
6ef533bc
CM
179 if msg.has_key('from'):
180 authname, authemail = name_email(__decode_header(msg['from']))
181 else:
182 authname = authemail = None
183
99c52915
CM
184 # '\n\t' can be found on multi-line headers
185 descr = __decode_header(msg['subject']).replace('\n\t', ' ')
6ef533bc 186 authdate = msg['date']
0d2cd1e4 187
186e6b6b 188 # remove the '[*PATCH*]' expression in the subject
0d2cd1e4 189 if descr:
dfeeba67 190 descr = re.findall('^(\[.*?[Pp][Aa][Tt][Cc][Hh].*?\])?\s*(.*)$',
7c02f338 191 descr)[0][1]
0d2cd1e4
CM
192 else:
193 raise CmdException, 'Subject: line not found'
194
6ef533bc 195 # the rest of the message
f21ba536
CM
196 msg_text = ''
197 for part in msg.walk():
198 if part.get_content_type() == 'text/plain':
199 msg_text += part.get_payload(decode = True)
6ef533bc 200
f21ba536
CM
201 rem_descr, diff = __split_descr_diff(msg_text)
202 if rem_descr:
203 descr += '\n\n' + rem_descr
204 if not diff:
205 out.warn('Message does not contain any diff')
0d2cd1e4 206
99e73103 207 # parse the description for author information
6ef533bc
CM
208 descr, descr_authname, descr_authemail, descr_authdate = \
209 __parse_description(descr)
99e73103
CM
210 if descr_authname:
211 authname = descr_authname
212 if descr_authemail:
213 authemail = descr_authemail
0543bc5f
TM
214 if descr_authdate:
215 authdate = descr_authdate
99e73103 216
6ef533bc 217 return (descr, authname, authemail, authdate, diff)
0d2cd1e4 218
99c52915 219def __parse_patch(fobj):
0d2cd1e4 220 """Parse the input file and return (description, authname,
99c52915 221 authemail, authdate, diff)
0d2cd1e4 222 """
f21ba536 223 descr, diff = __split_descr_diff(fobj.read())
0543bc5f 224 descr, authname, authemail, authdate = __parse_description(descr)
99e73103
CM
225
226 # we don't yet have an agreed place for the creation date.
227 # Just return None
6ef533bc 228 return (descr, authname, authemail, authdate, diff)
0d2cd1e4 229
fd1c0cfc 230def __create_patch(filename, message, author_name, author_email,
99c52915
CM
231 author_date, diff, options):
232 """Create a new patch on the stack
0d2cd1e4 233 """
fd1c0cfc
CM
234 if options.name:
235 patch = options.name
236 elif filename:
237 patch = os.path.basename(filename)
238 else:
239 patch = ''
240 if options.strip:
241 patch = __strip_patch_name(patch)
6ef533bc 242
fff9bce5 243 if not patch:
c4f99b6c
KH
244 if options.ignore or options.replace:
245 unacceptable_name = lambda name: False
246 else:
247 unacceptable_name = crt_series.patch_exists
248 patch = make_patch_name(message, unacceptable_name)
fd1c0cfc
CM
249 else:
250 # fix possible invalid characters in the patch name
251 patch = re.sub('[^\w.]+', '-', patch).strip('-')
252
253 if not diff:
254 raise CmdException, 'No diff found inside the patch'
99c52915
CM
255
256 if options.ignore and patch in crt_series.get_applied():
27ac2b7e 257 out.info('Ignoring already applied patch "%s"' % patch)
99c52915
CM
258 return
259 if options.replace and patch in crt_series.get_unapplied():
260 crt_series.delete_patch(patch)
fff9bce5 261
95742cfc
PBG
262 # refresh_patch() will invoke the editor in this case, with correct
263 # patch content
9d15ccd8 264 if not message:
95742cfc 265 can_edit = False
9d15ccd8 266
99c52915
CM
267 committer_name = committer_email = None
268
269 if options.author:
270 options.authname, options.authemail = name_email(options.author)
271
0d2cd1e4
CM
272 # override the automatically parsed settings
273 if options.authname:
274 author_name = options.authname
275 if options.authemail:
276 author_email = options.authemail
277 if options.authdate:
278 author_date = options.authdate
279 if options.commname:
280 committer_name = options.commname
281 if options.commemail:
282 committer_email = options.commemail
283
95742cfc 284 crt_series.new_patch(patch, message = message, can_edit = False,
0d2cd1e4
CM
285 author_name = author_name,
286 author_email = author_email,
287 author_date = author_date,
288 committer_name = committer_name,
289 committer_email = committer_email)
290
27ac2b7e 291 out.start('Importing patch "%s"' % patch)
35344f86 292 if options.base:
6ef533bc 293 git.apply_patch(diff = diff, base = git_id(options.base))
35344f86 294 else:
6ef533bc 295 git.apply_patch(diff = diff)
6ad48e48
PBG
296 crt_series.refresh_patch(edit = options.edit,
297 show_patch = options.showpatch)
27ac2b7e 298 out.done()
99c52915 299
fd1c0cfc 300def __import_file(filename, options, patch = None):
99c52915
CM
301 """Import a patch from a file or standard input
302 """
303 if filename:
304 f = file(filename)
305 else:
306 f = sys.stdin
307
308 if options.mail:
309 try:
310 msg = email.message_from_file(f)
311 except Exception, ex:
312 raise CmdException, 'error parsing the e-mail file: %s' % str(ex)
313 message, author_name, author_email, author_date, diff = \
314 __parse_mail(msg)
315 else:
316 message, author_name, author_email, author_date, diff = \
317 __parse_patch(f)
318
319 if filename:
320 f.close()
321
fd1c0cfc
CM
322 if patch:
323 pname = patch
324 else:
325 pname = filename
326
327 __create_patch(pname, message, author_name, author_email,
99c52915 328 author_date, diff, options)
9417ece4
CM
329
330def __import_series(filename, options):
331 """Import a series of patches
332 """
333 applied = crt_series.get_applied()
334
335 if filename:
336 f = file(filename)
337 patchdir = os.path.dirname(filename)
338 else:
339 f = sys.stdin
340 patchdir = ''
341
342 for line in f:
343 patch = re.sub('#.*$', '', line).strip()
344 if not patch:
345 continue
bcb6d890 346 patchfile = os.path.join(patchdir, patch)
613a2f16 347 patch = __replace_slashes_with_dashes(patch);
9417ece4 348
fd1c0cfc 349 __import_file(patchfile, options, patch)
99c52915
CM
350
351 if filename:
352 f.close()
353
354def __import_mbox(filename, options):
355 """Import a series from an mbox file
356 """
357 if filename:
358 f = file(filename, 'rb')
359 else:
457c3093 360 f = StringIO(sys.stdin.read())
99c52915
CM
361
362 try:
363 mbox = UnixMailbox(f, email.message_from_file)
364 except Exception, ex:
365 raise CmdException, 'error parsing the mbox file: %s' % str(ex)
366
367 for msg in mbox:
368 message, author_name, author_email, author_date, diff = \
369 __parse_mail(msg)
370 __create_patch(None, message, author_name, author_email,
371 author_date, diff, options)
372
457c3093 373 f.close()
9417ece4 374
575c575e
CW
375def __import_url(url, options):
376 """Import a patch from a URL
377 """
378 import urllib
379 import tempfile
380
381 if not url:
382 parser.error('URL argument required')
383
fd1c0cfc
CM
384 patch = os.path.basename(urllib.unquote(url))
385 filename = os.path.join(tempfile.gettempdir(), patch)
386 urllib.urlretrieve(url, filename)
387 __import_file(filename, options)
575c575e 388
9417ece4
CM
389def func(parser, options, args):
390 """Import a GNU diff file as a new patch
391 """
392 if len(args) > 1:
393 parser.error('incorrect number of arguments')
394
395 check_local_changes()
396 check_conflicts()
397 check_head_top_equal()
398
399 if len(args) == 1:
400 filename = args[0]
401 else:
402 filename = None
403
404 if options.series:
405 __import_series(filename, options)
99c52915
CM
406 elif options.mbox:
407 __import_mbox(filename, options)
575c575e
CW
408 elif options.url:
409 __import_url(filename, options)
9417ece4 410 else:
fd1c0cfc 411 __import_file(filename, options)
9417ece4 412
0d2cd1e4 413 print_crt_patch()