Add support for multipart messages to import
[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
0d2cd1e4
CM
20from optparse import OptionParser, make_option
21
22from stgit.commands.common import *
23from stgit.utils import *
24from stgit import stack, git
25
26
27help = 'import a GNU diff file as a new patch'
b8a0986f 28usage = """%prog [options] [<file>]
0d2cd1e4 29
b8a0986f
CM
30Create a new patch and apply the given GNU diff file (or the standard
31input). By default, the file name is used as the patch name but this
388f63b6 32can be overridden with the '--name' option. The patch can either be a
b8a0986f
CM
33normal file with the description at the top or it can have standard
34mail format, the Subject, From and Date headers being used for
35generating the patch information.
0d2cd1e4 36
b8a0986f 37The patch description has to be separated from the data with a '---'
99e73103 38line."""
0d2cd1e4
CM
39
40options = [make_option('-m', '--mail',
41 help = 'import the patch from a standard e-mail file',
42 action = 'store_true'),
43 make_option('-n', '--name',
44 help = 'use NAME as the patch name'),
b0cdad5e
CM
45 make_option('-t', '--strip',
46 help = 'strip numbering and extension from patch name',
47 action = 'store_true'),
9417ece4
CM
48 make_option('-s', '--series',
49 help = 'import a series of patches',
50 action = 'store_true'),
51 make_option('-i', '--ignore',
52 help = 'ignore the applied patches in the series',
53 action = 'store_true'),
034db15c
CM
54 make_option('--replace',
55 help = 'replace the unapplied patches in the series',
56 action = 'store_true'),
b21bc8d1 57 make_option('-b', '--base',
35344f86 58 help = 'use BASE instead of HEAD for file importing'),
33e580e0
CM
59 make_option('-e', '--edit',
60 help = 'invoke an editor for the patch description',
61 action = 'store_true'),
9417ece4 62 make_option('-p', '--showpatch',
6ad48e48
PBG
63 help = 'show the patch content in the editor buffer',
64 action = 'store_true'),
0d2cd1e4
CM
65 make_option('-a', '--author', metavar = '"NAME <EMAIL>"',
66 help = 'use "NAME <EMAIL>" as the author details'),
67 make_option('--authname',
68 help = 'use AUTHNAME as the author name'),
69 make_option('--authemail',
70 help = 'use AUTHEMAIL as the author e-mail'),
71 make_option('--authdate',
72 help = 'use AUTHDATE as the author date'),
73 make_option('--commname',
74 help = 'use COMMNAME as the committer name'),
75 make_option('--commemail',
76 help = 'use COMMEMAIL as the committer e-mail')]
77
78
d4c43e19
PBG
79def __end_descr(line):
80 return re.match('---\s*$', line) or re.match('diff -', line) or \
81 re.match('Index: ', line)
99e73103 82
b0cdad5e 83def __strip_patch_name(name):
bcb6d890
CM
84 stripped = re.sub('^[0-9]+-(.*)$', '\g<1>', name)
85 stripped = re.sub('^(.*)\.(diff|patch)$', '\g<1>', stripped)
86
87 return stripped
b0cdad5e 88
613a2f16
PBG
89def __replace_slashes_with_dashes(name):
90 stripped = name.replace('/', '-')
91
92 return stripped
93
99e73103
CM
94def __parse_description(descr):
95 """Parse the patch description and return the new description and
96 author information (if any).
97 """
98 subject = body = ''
0543bc5f 99 authname = authemail = authdate = None
99e73103 100
0543bc5f 101 descr_lines = [line.rstrip() for line in descr.split('\n')]
99e73103
CM
102 if not descr_lines:
103 raise CmdException, "Empty patch description"
104
0543bc5f 105 lasthdr = 0
99e73103
CM
106 end = len(descr_lines)
107
0543bc5f 108 # Parse the patch header
61dabd0e 109 for pos in range(0, end):
0543bc5f
TM
110 if not descr_lines[pos]:
111 continue
112 # check for a "From|Author:" line
113 if re.match('\s*(?:from|author):\s+', descr_lines[pos], re.I):
114 auth = re.findall('^.*?:\s+(.*)$', descr_lines[pos])[0]
115 authname, authemail = name_email(auth)
116 lasthdr = pos + 1
117 continue
118 # check for a "Date:" line
119 if re.match('\s*date:\s+', descr_lines[pos], re.I):
120 authdate = re.findall('^.*?:\s+(.*)$', descr_lines[pos])[0]
121 lasthdr = pos + 1
122 continue
123 if subject:
124 break
125 # get the subject
126 subject = descr_lines[pos]
127 lasthdr = pos + 1
99e73103
CM
128
129 # get the body
0543bc5f
TM
130 if lasthdr < end:
131 body = reduce(lambda x, y: x + '\n' + y, descr_lines[lasthdr:], '')
99e73103 132
0543bc5f 133 return (subject + body, authname, authemail, authdate)
99e73103 134
0d2cd1e4
CM
135def __parse_mail(filename = None):
136 """Parse the input file in a mail format and return (description,
137 authname, authemail, authdate)
138 """
2ac5a14c
CM
139 def __decode_header(header):
140 """Decode a qp-encoded e-mail header as per rfc2047"""
141 try:
142 words_enc = decode_header(header)
143 hobj = make_header(words_enc)
144 except Exception, ex:
145 raise CmdException, 'header decoding error: %s' % str(ex)
146 return unicode(hobj).encode('utf-8')
147
0d2cd1e4
CM
148 if filename:
149 f = file(filename)
150 else:
151 f = sys.stdin
152
6ef533bc
CM
153 msg = email.message_from_file(f)
154
155 if filename:
156 f.close()
0d2cd1e4
CM
157
158 # parse the headers
6ef533bc
CM
159 if msg.has_key('from'):
160 authname, authemail = name_email(__decode_header(msg['from']))
161 else:
162 authname = authemail = None
163
164 descr = __decode_header(msg['subject'])
165 authdate = msg['date']
0d2cd1e4 166
186e6b6b 167 # remove the '[*PATCH*]' expression in the subject
0d2cd1e4 168 if descr:
7c02f338
CM
169 descr = re.findall('^(\[[^\s]*[Pp][Aa][Tt][Cc][Hh].*?\])?\s*(.*)$',
170 descr)[0][1]
0d2cd1e4
CM
171 descr += '\n\n'
172 else:
173 raise CmdException, 'Subject: line not found'
174
6ef533bc
CM
175 # the rest of the message
176 if msg.is_multipart():
177 descr += msg.get_payload(0, decode = True)
178 diff = msg.get_payload(1, decode = True)
179 else:
180 diff = msg.get_payload(decode = True)
0d2cd1e4 181
6ef533bc
CM
182 for line in diff.split('\n'):
183 if __end_descr(line):
184 break
185 descr += line + '\n'
186
187 descr.rstrip()
0d2cd1e4 188
99e73103 189 # parse the description for author information
6ef533bc
CM
190 descr, descr_authname, descr_authemail, descr_authdate = \
191 __parse_description(descr)
99e73103
CM
192 if descr_authname:
193 authname = descr_authname
194 if descr_authemail:
195 authemail = descr_authemail
0543bc5f
TM
196 if descr_authdate:
197 authdate = descr_authdate
99e73103 198
6ef533bc 199 return (descr, authname, authemail, authdate, diff)
0d2cd1e4
CM
200
201def __parse_patch(filename = None):
202 """Parse the input file and return (description, authname,
203 authemail, authdate)
204 """
205 if filename:
206 f = file(filename)
207 else:
208 f = sys.stdin
209
0d2cd1e4 210 descr = ''
6fe6b1bd
CM
211 while True:
212 line = f.readline()
213 if not line:
214 break
215
d4c43e19 216 if __end_descr(line):
0d2cd1e4
CM
217 break
218 else:
219 descr += line
220 descr.rstrip()
221
6ef533bc
CM
222 diff = f.read()
223
0d2cd1e4
CM
224 if filename:
225 f.close()
226
0543bc5f 227 descr, authname, authemail, authdate = __parse_description(descr)
99e73103
CM
228
229 # we don't yet have an agreed place for the creation date.
230 # Just return None
6ef533bc 231 return (descr, authname, authemail, authdate, diff)
0d2cd1e4 232
9417ece4
CM
233def __import_patch(patch, filename, options):
234 """Import a patch from a file or standard input
0d2cd1e4 235 """
0d2cd1e4
CM
236 # the defaults
237 message = author_name = author_email = author_date = committer_name = \
238 committer_email = None
239
240 if options.author:
241 options.authname, options.authemail = name_email(options.author)
242
243 if options.mail:
6ef533bc 244 message, author_name, author_email, author_date, diff = \
0d2cd1e4
CM
245 __parse_mail(filename)
246 else:
6ef533bc 247 message, author_name, author_email, author_date, diff = \
0d2cd1e4
CM
248 __parse_patch(filename)
249
6ef533bc
CM
250 if not diff:
251 raise CmdException, 'No diff found inside the patch'
252
fff9bce5 253 if not patch:
b839b1cf 254 patch = make_patch_name(message, crt_series.patch_exists)
fff9bce5 255
95742cfc
PBG
256 # refresh_patch() will invoke the editor in this case, with correct
257 # patch content
9d15ccd8 258 if not message:
95742cfc 259 can_edit = False
9d15ccd8 260
0d2cd1e4
CM
261 # override the automatically parsed settings
262 if options.authname:
263 author_name = options.authname
264 if options.authemail:
265 author_email = options.authemail
266 if options.authdate:
267 author_date = options.authdate
268 if options.commname:
269 committer_name = options.commname
270 if options.commemail:
271 committer_email = options.commemail
272
034db15c
CM
273 if options.replace and patch in crt_series.get_unapplied():
274 crt_series.delete_patch(patch)
275
95742cfc 276 crt_series.new_patch(patch, message = message, can_edit = False,
0d2cd1e4
CM
277 author_name = author_name,
278 author_email = author_email,
279 author_date = author_date,
280 committer_name = committer_name,
281 committer_email = committer_email)
282
9417ece4 283 print 'Importing patch "%s"...' % patch,
0d2cd1e4
CM
284 sys.stdout.flush()
285
35344f86 286 if options.base:
6ef533bc 287 git.apply_patch(diff = diff, base = git_id(options.base))
35344f86 288 else:
6ef533bc 289 git.apply_patch(diff = diff)
35344f86 290
6ad48e48
PBG
291 crt_series.refresh_patch(edit = options.edit,
292 show_patch = options.showpatch)
0d2cd1e4
CM
293
294 print 'done'
9417ece4
CM
295
296def __import_series(filename, options):
297 """Import a series of patches
298 """
299 applied = crt_series.get_applied()
300
301 if filename:
302 f = file(filename)
303 patchdir = os.path.dirname(filename)
304 else:
305 f = sys.stdin
306 patchdir = ''
307
308 for line in f:
309 patch = re.sub('#.*$', '', line).strip()
310 if not patch:
311 continue
bcb6d890
CM
312 patchfile = os.path.join(patchdir, patch)
313
b0cdad5e
CM
314 if options.strip:
315 patch = __strip_patch_name(patch)
613a2f16 316 patch = __replace_slashes_with_dashes(patch);
9417ece4
CM
317 if options.ignore and patch in applied:
318 print 'Ignoring already applied patch "%s"' % patch
319 continue
320
9417ece4
CM
321 __import_patch(patch, patchfile, options)
322
323def func(parser, options, args):
324 """Import a GNU diff file as a new patch
325 """
326 if len(args) > 1:
327 parser.error('incorrect number of arguments')
328
329 check_local_changes()
330 check_conflicts()
331 check_head_top_equal()
332
333 if len(args) == 1:
334 filename = args[0]
335 else:
336 filename = None
337
338 if options.series:
339 __import_series(filename, options)
340 else:
341 if options.name:
342 patch = options.name
343 elif filename:
344 patch = os.path.basename(filename)
345 else:
fff9bce5 346 patch = ''
b0cdad5e
CM
347 if options.strip:
348 patch = __strip_patch_name(patch)
9417ece4
CM
349
350 __import_patch(patch, filename, options)
351
0d2cd1e4 352 print_crt_patch()