Add parse_patches boundary in the delete.py file
[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
972de1db 18import sys, os, re, email, tarfile
99c52915 19from mailbox import UnixMailbox
457c3093 20from StringIO import StringIO
575bbdae 21from stgit.argparse import opt
0d2cd1e4
CM
22from stgit.commands.common import *
23from stgit.utils import *
5e888f30 24from stgit.out import *
20a52e06 25from stgit import argparse, stack, git
0d2cd1e4 26
575bbdae
KH
27name = 'import'
28help = 'Import a GNU diff file as a new patch'
33ff9cdd 29kind = 'patch'
575bbdae
KH
30usage = ['[options] [<file>|<url>]']
31description = """
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 46
6c8a90e1 47args = [argparse.files]
575bbdae
KH
48options = [
49 opt('-m', '--mail', action = 'store_true',
50 short = 'Import the patch from a standard e-mail file'),
51 opt('-M', '--mbox', action = 'store_true',
52 short = 'Import a series of patches from an mbox file'),
53 opt('-s', '--series', action = 'store_true',
972de1db
CW
54 short = 'Import a series of patches', long = """
55 Import a series of patches from a series file or a tar archive."""),
575bbdae
KH
56 opt('-u', '--url', action = 'store_true',
57 short = 'Import a patch from a URL'),
58 opt('-n', '--name',
59 short = 'Use NAME as the patch name'),
60 opt('-t', '--strip', action = 'store_true',
61 short = 'Strip numbering and extension from patch name'),
62 opt('-i', '--ignore', action = 'store_true',
63 short = 'Ignore the applied patches in the series'),
64 opt('--replace', action = 'store_true',
65 short = 'Replace the unapplied patches in the series'),
6c8a90e1 66 opt('-b', '--base', args = [argparse.commit],
575bbdae
KH
67 short = 'Use BASE instead of HEAD for file importing'),
68 opt('-e', '--edit', action = 'store_true',
69 short = 'Invoke an editor for the patch description'),
70 opt('-p', '--showpatch', action = 'store_true',
71 short = 'Show the patch content in the editor buffer'),
72 opt('-a', '--author', metavar = '"NAME <EMAIL>"',
73 short = 'Use "NAME <EMAIL>" as the author details'),
74 opt('--authname',
75 short = 'Use AUTHNAME as the author name'),
76 opt('--authemail',
77 short = 'Use AUTHEMAIL as the author e-mail'),
78 opt('--authdate',
79 short = 'Use AUTHDATE as the author date'),
80 opt('--commname',
81 short = 'Use COMMNAME as the committer name'),
82 opt('--commemail',
83 short = 'Use COMMEMAIL as the committer e-mail'),
84 ] + argparse.sign_options()
0d2cd1e4 85
117ed129 86directory = DirectoryHasRepository(log = True)
0d2cd1e4 87
b0cdad5e 88def __strip_patch_name(name):
bcb6d890
CM
89 stripped = re.sub('^[0-9]+-(.*)$', '\g<1>', name)
90 stripped = re.sub('^(.*)\.(diff|patch)$', '\g<1>', stripped)
91
92 return stripped
b0cdad5e 93
613a2f16
PBG
94def __replace_slashes_with_dashes(name):
95 stripped = name.replace('/', '-')
96
97 return stripped
98
fd1c0cfc 99def __create_patch(filename, message, author_name, author_email,
99c52915
CM
100 author_date, diff, options):
101 """Create a new patch on the stack
0d2cd1e4 102 """
fd1c0cfc
CM
103 if options.name:
104 patch = options.name
105 elif filename:
106 patch = os.path.basename(filename)
107 else:
108 patch = ''
109 if options.strip:
110 patch = __strip_patch_name(patch)
6ef533bc 111
fff9bce5 112 if not patch:
c4f99b6c
KH
113 if options.ignore or options.replace:
114 unacceptable_name = lambda name: False
115 else:
116 unacceptable_name = crt_series.patch_exists
117 patch = make_patch_name(message, unacceptable_name)
fd1c0cfc
CM
118 else:
119 # fix possible invalid characters in the patch name
120 patch = re.sub('[^\w.]+', '-', patch).strip('-')
121
99c52915 122 if options.ignore and patch in crt_series.get_applied():
27ac2b7e 123 out.info('Ignoring already applied patch "%s"' % patch)
99c52915
CM
124 return
125 if options.replace and patch in crt_series.get_unapplied():
c26ca1b2 126 crt_series.delete_patch(patch, keep_log = True)
fff9bce5 127
95742cfc
PBG
128 # refresh_patch() will invoke the editor in this case, with correct
129 # patch content
9d15ccd8 130 if not message:
95742cfc 131 can_edit = False
9d15ccd8 132
99c52915
CM
133 committer_name = committer_email = None
134
135 if options.author:
136 options.authname, options.authemail = name_email(options.author)
137
0d2cd1e4
CM
138 # override the automatically parsed settings
139 if options.authname:
140 author_name = options.authname
141 if options.authemail:
142 author_email = options.authemail
143 if options.authdate:
144 author_date = options.authdate
145 if options.commname:
146 committer_name = options.commname
147 if options.commemail:
148 committer_email = options.commemail
149
95742cfc 150 crt_series.new_patch(patch, message = message, can_edit = False,
0d2cd1e4
CM
151 author_name = author_name,
152 author_email = author_email,
153 author_date = author_date,
154 committer_name = committer_name,
155 committer_email = committer_email)
156
5f1629be
CM
157 if not diff:
158 out.warn('No diff found, creating empty patch')
35344f86 159 else:
5f1629be
CM
160 out.start('Importing patch "%s"' % patch)
161 if options.base:
6972fd6b
KH
162 git.apply_patch(diff = diff,
163 base = git_id(crt_series, options.base))
5f1629be
CM
164 else:
165 git.apply_patch(diff = diff)
166 crt_series.refresh_patch(edit = options.edit,
130df01a 167 show_patch = options.showpatch,
cb688601
CM
168 sign_str = options.sign_str,
169 backup = False)
5f1629be 170 out.done()
99c52915 171
354d2031
CW
172def __mkpatchname(name, suffix):
173 if name.lower().endswith(suffix.lower()):
174 return name[:-len(suffix)]
175 return name
176
177def __get_handle_and_name(filename):
178 """Return a file object and a patch name derived from filename
179 """
180 # see if it's a gzip'ed or bzip2'ed patch
181 import bz2, gzip
182 for copen, ext in [(gzip.open, '.gz'), (bz2.BZ2File, '.bz2')]:
183 try:
184 f = copen(filename)
185 f.read(1)
186 f.seek(0)
187 return (f, __mkpatchname(filename, ext))
188 except IOError, e:
189 pass
190
191 # plain old file...
192 return (open(filename), filename)
193
fd1c0cfc 194def __import_file(filename, options, patch = None):
99c52915
CM
195 """Import a patch from a file or standard input
196 """
354d2031 197 pname = None
99c52915 198 if filename:
354d2031 199 (f, pname) = __get_handle_and_name(filename)
99c52915
CM
200 else:
201 f = sys.stdin
202
354d2031
CW
203 if patch:
204 pname = patch
205 elif not pname:
206 pname = filename
207
99c52915
CM
208 if options.mail:
209 try:
210 msg = email.message_from_file(f)
211 except Exception, ex:
212 raise CmdException, 'error parsing the e-mail file: %s' % str(ex)
213 message, author_name, author_email, author_date, diff = \
ed60fdae 214 parse_mail(msg)
99c52915
CM
215 else:
216 message, author_name, author_email, author_date, diff = \
ef954fe6 217 parse_patch(f.read(), contains_diff = True)
99c52915
CM
218
219 if filename:
220 f.close()
221
fd1c0cfc 222 __create_patch(pname, message, author_name, author_email,
99c52915 223 author_date, diff, options)
9417ece4
CM
224
225def __import_series(filename, options):
226 """Import a series of patches
227 """
228 applied = crt_series.get_applied()
229
230 if filename:
972de1db
CW
231 if tarfile.is_tarfile(filename):
232 __import_tarfile(filename, options)
233 return
9417ece4
CM
234 f = file(filename)
235 patchdir = os.path.dirname(filename)
236 else:
237 f = sys.stdin
238 patchdir = ''
239
240 for line in f:
241 patch = re.sub('#.*$', '', line).strip()
242 if not patch:
243 continue
bcb6d890 244 patchfile = os.path.join(patchdir, patch)
613a2f16 245 patch = __replace_slashes_with_dashes(patch);
9417ece4 246
fd1c0cfc 247 __import_file(patchfile, options, patch)
99c52915
CM
248
249 if filename:
250 f.close()
251
252def __import_mbox(filename, options):
253 """Import a series from an mbox file
254 """
255 if filename:
256 f = file(filename, 'rb')
257 else:
457c3093 258 f = StringIO(sys.stdin.read())
99c52915
CM
259
260 try:
261 mbox = UnixMailbox(f, email.message_from_file)
262 except Exception, ex:
263 raise CmdException, 'error parsing the mbox file: %s' % str(ex)
264
265 for msg in mbox:
266 message, author_name, author_email, author_date, diff = \
ed60fdae 267 parse_mail(msg)
99c52915
CM
268 __create_patch(None, message, author_name, author_email,
269 author_date, diff, options)
270
457c3093 271 f.close()
9417ece4 272
575c575e
CW
273def __import_url(url, options):
274 """Import a patch from a URL
275 """
276 import urllib
277 import tempfile
278
279 if not url:
280 parser.error('URL argument required')
281
fd1c0cfc
CM
282 patch = os.path.basename(urllib.unquote(url))
283 filename = os.path.join(tempfile.gettempdir(), patch)
284 urllib.urlretrieve(url, filename)
285 __import_file(filename, options)
575c575e 286
972de1db
CW
287def __import_tarfile(tar, options):
288 """Import patch series from a tar archive
289 """
290 import tempfile
291 import shutil
292
293 if not tarfile.is_tarfile(tar):
294 raise CmdException, "%s is not a tarfile!" % tar
295
296 t = tarfile.open(tar, 'r')
297 names = t.getnames()
298
299 # verify paths in the tarfile are safe
300 for n in names:
301 if n.startswith('/'):
302 raise CmdException, "Absolute path found in %s" % tar
303 if n.find("..") > -1:
304 raise CmdException, "Relative path found in %s" % tar
305
306 # find the series file
307 seriesfile = '';
308 for m in names:
309 if m.endswith('/series') or m == 'series':
310 seriesfile = m
311 break
312 if seriesfile == '':
313 raise CmdException, "no 'series' file found in %s" % tar
314
315 # unpack into a tmp dir
316 tmpdir = tempfile.mkdtemp('.stg')
317 t.extractall(tmpdir)
318
319 # apply the series
320 __import_series(os.path.join(tmpdir, seriesfile), options)
321
322 # cleanup the tmpdir
323 shutil.rmtree(tmpdir)
324
9417ece4
CM
325def func(parser, options, args):
326 """Import a GNU diff file as a new patch
327 """
328 if len(args) > 1:
329 parser.error('incorrect number of arguments')
330
331 check_local_changes()
332 check_conflicts()
6972fd6b 333 check_head_top_equal(crt_series)
9417ece4
CM
334
335 if len(args) == 1:
336 filename = args[0]
337 else:
338 filename = None
339
47d51d91
CM
340 if filename:
341 filename = os.path.abspath(filename)
342 directory.cd_to_topdir()
343
9417ece4
CM
344 if options.series:
345 __import_series(filename, options)
99c52915
CM
346 elif options.mbox:
347 __import_mbox(filename, options)
575c575e
CW
348 elif options.url:
349 __import_url(filename, options)
9417ece4 350 else:
fd1c0cfc 351 __import_file(filename, options)
9417ece4 352
6972fd6b 353 print_crt_patch(crt_series)