chiark / gitweb /
6fcdc62ae8de6f96007a1058e722c04910e3d134
[stgit] / stgit / commands / imprt.py
1 __copyright__ = """
2 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
3
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.
7
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.
12
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
16 """
17
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
23
24 from stgit.commands.common import *
25 from stgit.utils import *
26 from stgit import stack, git
27
28
29 help = 'import a GNU diff file as a new patch'
30 usage = """%prog [options] [<file>|<url>]
31
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
38 mbox files.
39
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
42 stack.
43
44 The patch description has to be separated from the data with a '---'
45 line."""
46
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('-u', '--url',
57                        help = 'import a patch from a URL',
58                        action = 'store_true'),
59            make_option('-n', '--name',
60                        help = 'use NAME as the patch name'),
61            make_option('-t', '--strip',
62                        help = 'strip numbering and extension from patch name',
63                        action = 'store_true'),
64            make_option('-i', '--ignore',
65                        help = 'ignore the applied patches in the series',
66                        action = 'store_true'),
67            make_option('--replace',
68                        help = 'replace the unapplied patches in the series',
69                        action = 'store_true'),
70            make_option('-b', '--base',
71                        help = 'use BASE instead of HEAD for file importing'),
72            make_option('-e', '--edit',
73                        help = 'invoke an editor for the patch description',
74                        action = 'store_true'),
75            make_option('-p', '--showpatch',
76                        help = 'show the patch content in the editor buffer',
77                        action = 'store_true'),
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
92 def __end_descr(line):
93     return re.match('---\s*$', line) or re.match('diff -', line) or \
94             re.match('Index: ', line)
95
96 def __strip_patch_name(name):
97     stripped = re.sub('^[0-9]+-(.*)$', '\g<1>', name)
98     stripped = re.sub('^(.*)\.(diff|patch)$', '\g<1>', stripped)
99
100     return stripped
101
102 def __replace_slashes_with_dashes(name):
103     stripped = name.replace('/', '-')
104
105     return stripped
106
107 def __parse_description(descr):
108     """Parse the patch description and return the new description and
109     author information (if any).
110     """
111     subject = body = ''
112     authname = authemail = authdate = None
113
114     descr_lines = [line.rstrip() for line in  descr.split('\n')]
115     if not descr_lines:
116         raise CmdException, "Empty patch description"
117
118     lasthdr = 0
119     end = len(descr_lines)
120
121     # Parse the patch header
122     for pos in range(0, end):
123         if not descr_lines[pos]:
124            continue
125         # check for a "From|Author:" line
126         if re.match('\s*(?:from|author):\s+', descr_lines[pos], re.I):
127             auth = re.findall('^.*?:\s+(.*)$', descr_lines[pos])[0]
128             authname, authemail = name_email(auth)
129             lasthdr = pos + 1
130             continue
131         # check for a "Date:" line
132         if re.match('\s*date:\s+', descr_lines[pos], re.I):
133             authdate = re.findall('^.*?:\s+(.*)$', descr_lines[pos])[0]
134             lasthdr = pos + 1
135             continue
136         if subject:
137             break
138         # get the subject
139         subject = descr_lines[pos]
140         lasthdr = pos + 1
141
142     # get the body
143     if lasthdr < end:
144         body = reduce(lambda x, y: x + '\n' + y, descr_lines[lasthdr:], '')
145
146     return (subject + body, authname, authemail, authdate)
147
148 def __parse_mail(msg):
149     """Parse the message object and return (description, authname,
150     authemail, authdate, diff)
151     """
152     def __decode_header(header):
153         """Decode a qp-encoded e-mail header as per rfc2047"""
154         try:
155             words_enc = decode_header(header)
156             hobj = make_header(words_enc)
157         except Exception, ex:
158             raise CmdException, 'header decoding error: %s' % str(ex)
159         return unicode(hobj).encode('utf-8')
160
161     # parse the headers
162     if msg.has_key('from'):
163         authname, authemail = name_email(__decode_header(msg['from']))
164     else:
165         authname = authemail = None
166
167     # '\n\t' can be found on multi-line headers
168     descr = __decode_header(msg['subject']).replace('\n\t', ' ')
169     authdate = msg['date']
170
171     # remove the '[*PATCH*]' expression in the subject
172     if descr:
173         descr = re.findall('^(\[.*?[Pp][Aa][Tt][Cc][Hh].*?\])?\s*(.*)$',
174                            descr)[0][1]
175         descr += '\n\n'
176     else:
177         raise CmdException, 'Subject: line not found'
178
179     # the rest of the message
180     if msg.is_multipart():
181         # this is assuming that the first part is the patch
182         # description and the second part is the attached patch
183         descr += msg.get_payload(0).get_payload(decode = True)
184         diff = msg.get_payload(1).get_payload(decode = True)
185     else:
186         diff = msg.get_payload(decode = True)
187
188         for line in diff.split('\n'):
189             if __end_descr(line):
190                 break
191             descr += line + '\n'
192
193     descr.rstrip()
194
195     # parse the description for author information
196     descr, descr_authname, descr_authemail, descr_authdate = \
197            __parse_description(descr)
198     if descr_authname:
199         authname = descr_authname
200     if descr_authemail:
201         authemail = descr_authemail
202     if descr_authdate:
203        authdate = descr_authdate
204
205     return (descr, authname, authemail, authdate, diff)
206
207 def __parse_patch(fobj):
208     """Parse the input file and return (description, authname,
209     authemail, authdate, diff)
210     """
211     descr = ''
212     while True:
213         line = fobj.readline()
214         if not line:
215             break
216
217         if __end_descr(line):
218             break
219         else:
220             descr += line
221     descr.rstrip()
222
223     diff = fobj.read()
224
225     descr, authname, authemail, authdate = __parse_description(descr)
226
227     # we don't yet have an agreed place for the creation date.
228     # Just return None
229     return (descr, authname, authemail, authdate, diff)
230
231 def __create_patch(filename, message, author_name, author_email,
232                    author_date, diff, options):
233     """Create a new patch on the stack
234     """
235     if options.name:
236         patch = options.name
237     elif filename:
238         patch = os.path.basename(filename)
239     else:
240         patch = ''
241     if options.strip:
242         patch = __strip_patch_name(patch)
243
244     if not patch:
245         if options.ignore or options.replace:
246             unacceptable_name = lambda name: False
247         else:
248             unacceptable_name = crt_series.patch_exists
249         patch = make_patch_name(message, unacceptable_name)
250     else:
251         # fix possible invalid characters in the patch name
252         patch = re.sub('[^\w.]+', '-', patch).strip('-')
253
254     if not diff:
255         raise CmdException, 'No diff found inside the patch'
256
257     if options.ignore and patch in crt_series.get_applied():
258         print 'Ignoring already applied patch "%s"' % patch
259         return
260     if options.replace and patch in crt_series.get_unapplied():
261         crt_series.delete_patch(patch)
262
263     # refresh_patch() will invoke the editor in this case, with correct
264     # patch content
265     if not message:
266         can_edit = False
267
268     committer_name = committer_email = None
269
270     if options.author:
271         options.authname, options.authemail = name_email(options.author)
272
273     # override the automatically parsed settings
274     if options.authname:
275         author_name = options.authname
276     if options.authemail:
277         author_email = options.authemail
278     if options.authdate:
279         author_date = options.authdate
280     if options.commname:
281         committer_name = options.commname
282     if options.commemail:
283         committer_email = options.commemail
284
285     crt_series.new_patch(patch, message = message, can_edit = False,
286                          author_name = author_name,
287                          author_email = author_email,
288                          author_date = author_date,
289                          committer_name = committer_name,
290                          committer_email = committer_email)
291
292     print 'Importing patch "%s"...' % patch,
293     sys.stdout.flush()
294
295     if options.base:
296         git.apply_patch(diff = diff, base = git_id(options.base))
297     else:
298         git.apply_patch(diff = diff)
299
300     crt_series.refresh_patch(edit = options.edit,
301                              show_patch = options.showpatch)
302
303     print 'done'    
304
305 def __import_file(filename, options, patch = None):
306     """Import a patch from a file or standard input
307     """
308     if filename:
309         f = file(filename)
310     else:
311         f = sys.stdin
312
313     if options.mail:
314         try:
315             msg = email.message_from_file(f)
316         except Exception, ex:
317             raise CmdException, 'error parsing the e-mail file: %s' % str(ex)
318         message, author_name, author_email, author_date, diff = \
319                  __parse_mail(msg)
320     else:
321         message, author_name, author_email, author_date, diff = \
322                  __parse_patch(f)
323
324     if filename:
325         f.close()
326
327     if patch:
328         pname = patch
329     else:
330         pname = filename
331
332     __create_patch(pname, message, author_name, author_email,
333                    author_date, diff, options)
334
335 def __import_series(filename, options):
336     """Import a series of patches
337     """
338     applied = crt_series.get_applied()
339
340     if filename:
341         f = file(filename)
342         patchdir = os.path.dirname(filename)
343     else:
344         f = sys.stdin
345         patchdir = ''
346
347     for line in f:
348         patch = re.sub('#.*$', '', line).strip()
349         if not patch:
350             continue
351         patchfile = os.path.join(patchdir, patch)
352         patch = __replace_slashes_with_dashes(patch);
353
354         __import_file(patchfile, options, patch)
355
356     if filename:
357         f.close()
358
359 def __import_mbox(filename, options):
360     """Import a series from an mbox file
361     """
362     if filename:
363         f = file(filename, 'rb')
364     else:
365         f = StringIO(sys.stdin.read())
366
367     try:
368         mbox = UnixMailbox(f, email.message_from_file)
369     except Exception, ex:
370         raise CmdException, 'error parsing the mbox file: %s' % str(ex)
371
372     for msg in mbox:
373         message, author_name, author_email, author_date, diff = \
374                  __parse_mail(msg)
375         __create_patch(None, message, author_name, author_email,
376                        author_date, diff, options)
377
378     f.close()
379
380 def __import_url(url, options):
381     """Import a patch from a URL
382     """
383     import urllib
384     import tempfile
385
386     if not url:
387         parser.error('URL argument required')
388
389     patch = os.path.basename(urllib.unquote(url))
390     filename = os.path.join(tempfile.gettempdir(), patch)
391     urllib.urlretrieve(url, filename)
392     __import_file(filename, options)
393
394 def func(parser, options, args):
395     """Import a GNU diff file as a new patch
396     """
397     if len(args) > 1:
398         parser.error('incorrect number of arguments')
399
400     check_local_changes()
401     check_conflicts()
402     check_head_top_equal()
403
404     if len(args) == 1:
405         filename = args[0]
406     else:
407         filename = None
408
409     if options.series:
410         __import_series(filename, options)
411     elif options.mbox:
412         __import_mbox(filename, options)
413     elif options.url:
414         __import_url(filename, options)
415     else:
416         __import_file(filename, options)
417
418     print_crt_patch()