Add the --reject option to fold
[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'),
c18842cc
CM
60 opt('-p', '--strip', type = 'int', metavar = 'N',
61 short = 'Remove N leading slashes from diff paths (default 1)'),
62 opt('-t', '--stripname', action = 'store_true',
575bbdae
KH
63 short = 'Strip numbering and extension from patch name'),
64 opt('-i', '--ignore', action = 'store_true',
65 short = 'Ignore the applied patches in the series'),
66 opt('--replace', action = 'store_true',
67 short = 'Replace the unapplied patches in the series'),
6c8a90e1 68 opt('-b', '--base', args = [argparse.commit],
575bbdae 69 short = 'Use BASE instead of HEAD for file importing'),
fc8dcca7
CM
70 opt('--reject', action = 'store_true',
71 short = 'leave the rejected hunks in corresponding *.rej files'),
575bbdae
KH
72 opt('-e', '--edit', action = 'store_true',
73 short = 'Invoke an editor for the patch description'),
c18842cc 74 opt('-d', '--showdiff', action = 'store_true',
575bbdae
KH
75 short = 'Show the patch content in the editor buffer'),
76 opt('-a', '--author', metavar = '"NAME <EMAIL>"',
77 short = 'Use "NAME <EMAIL>" as the author details'),
78 opt('--authname',
79 short = 'Use AUTHNAME as the author name'),
80 opt('--authemail',
81 short = 'Use AUTHEMAIL as the author e-mail'),
82 opt('--authdate',
83 short = 'Use AUTHDATE as the author date'),
575bbdae 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 = ''
c18842cc 109 if options.stripname:
fd1c0cfc 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 if options.author:
134 options.authname, options.authemail = name_email(options.author)
135
0d2cd1e4
CM
136 # override the automatically parsed settings
137 if options.authname:
138 author_name = options.authname
139 if options.authemail:
140 author_email = options.authemail
141 if options.authdate:
142 author_date = options.authdate
0d2cd1e4 143
95742cfc 144 crt_series.new_patch(patch, message = message, can_edit = False,
0d2cd1e4
CM
145 author_name = author_name,
146 author_email = author_email,
53388a71 147 author_date = author_date)
0d2cd1e4 148
5f1629be
CM
149 if not diff:
150 out.warn('No diff found, creating empty patch')
35344f86 151 else:
5f1629be
CM
152 out.start('Importing patch "%s"' % patch)
153 if options.base:
fc8dcca7 154 base = git_id(crt_series, options.base)
5f1629be 155 else:
fc8dcca7 156 base = None
c18842cc
CM
157 git.apply_patch(diff = diff, base = base, reject = options.reject,
158 strip = options.strip)
5f1629be 159 crt_series.refresh_patch(edit = options.edit,
c18842cc 160 show_patch = options.showdiff,
24a7f944 161 author_date = author_date,
cb688601
CM
162 sign_str = options.sign_str,
163 backup = False)
5f1629be 164 out.done()
99c52915 165
354d2031
CW
166def __mkpatchname(name, suffix):
167 if name.lower().endswith(suffix.lower()):
168 return name[:-len(suffix)]
169 return name
170
171def __get_handle_and_name(filename):
172 """Return a file object and a patch name derived from filename
173 """
174 # see if it's a gzip'ed or bzip2'ed patch
175 import bz2, gzip
176 for copen, ext in [(gzip.open, '.gz'), (bz2.BZ2File, '.bz2')]:
177 try:
178 f = copen(filename)
179 f.read(1)
180 f.seek(0)
181 return (f, __mkpatchname(filename, ext))
182 except IOError, e:
183 pass
184
185 # plain old file...
186 return (open(filename), filename)
187
fd1c0cfc 188def __import_file(filename, options, patch = None):
99c52915
CM
189 """Import a patch from a file or standard input
190 """
354d2031 191 pname = None
99c52915 192 if filename:
354d2031 193 (f, pname) = __get_handle_and_name(filename)
99c52915
CM
194 else:
195 f = sys.stdin
196
354d2031
CW
197 if patch:
198 pname = patch
199 elif not pname:
200 pname = filename
201
99c52915
CM
202 if options.mail:
203 try:
204 msg = email.message_from_file(f)
205 except Exception, ex:
206 raise CmdException, 'error parsing the e-mail file: %s' % str(ex)
207 message, author_name, author_email, author_date, diff = \
ed60fdae 208 parse_mail(msg)
99c52915
CM
209 else:
210 message, author_name, author_email, author_date, diff = \
ef954fe6 211 parse_patch(f.read(), contains_diff = True)
99c52915
CM
212
213 if filename:
214 f.close()
215
fd1c0cfc 216 __create_patch(pname, message, author_name, author_email,
99c52915 217 author_date, diff, options)
9417ece4
CM
218
219def __import_series(filename, options):
220 """Import a series of patches
221 """
222 applied = crt_series.get_applied()
223
224 if filename:
972de1db
CW
225 if tarfile.is_tarfile(filename):
226 __import_tarfile(filename, options)
227 return
9417ece4
CM
228 f = file(filename)
229 patchdir = os.path.dirname(filename)
230 else:
231 f = sys.stdin
232 patchdir = ''
233
234 for line in f:
235 patch = re.sub('#.*$', '', line).strip()
236 if not patch:
237 continue
bcb6d890 238 patchfile = os.path.join(patchdir, patch)
613a2f16 239 patch = __replace_slashes_with_dashes(patch);
9417ece4 240
fd1c0cfc 241 __import_file(patchfile, options, patch)
99c52915
CM
242
243 if filename:
244 f.close()
245
246def __import_mbox(filename, options):
247 """Import a series from an mbox file
248 """
249 if filename:
250 f = file(filename, 'rb')
251 else:
457c3093 252 f = StringIO(sys.stdin.read())
99c52915
CM
253
254 try:
255 mbox = UnixMailbox(f, email.message_from_file)
256 except Exception, ex:
257 raise CmdException, 'error parsing the mbox file: %s' % str(ex)
258
259 for msg in mbox:
260 message, author_name, author_email, author_date, diff = \
ed60fdae 261 parse_mail(msg)
99c52915
CM
262 __create_patch(None, message, author_name, author_email,
263 author_date, diff, options)
264
457c3093 265 f.close()
9417ece4 266
575c575e
CW
267def __import_url(url, options):
268 """Import a patch from a URL
269 """
270 import urllib
271 import tempfile
272
273 if not url:
4a8e79dc 274 raise CmdException('URL argument required')
575c575e 275
fd1c0cfc
CM
276 patch = os.path.basename(urllib.unquote(url))
277 filename = os.path.join(tempfile.gettempdir(), patch)
278 urllib.urlretrieve(url, filename)
279 __import_file(filename, options)
575c575e 280
972de1db
CW
281def __import_tarfile(tar, options):
282 """Import patch series from a tar archive
283 """
284 import tempfile
285 import shutil
286
287 if not tarfile.is_tarfile(tar):
288 raise CmdException, "%s is not a tarfile!" % tar
289
290 t = tarfile.open(tar, 'r')
291 names = t.getnames()
292
293 # verify paths in the tarfile are safe
294 for n in names:
295 if n.startswith('/'):
296 raise CmdException, "Absolute path found in %s" % tar
297 if n.find("..") > -1:
298 raise CmdException, "Relative path found in %s" % tar
299
300 # find the series file
301 seriesfile = '';
302 for m in names:
303 if m.endswith('/series') or m == 'series':
304 seriesfile = m
305 break
306 if seriesfile == '':
307 raise CmdException, "no 'series' file found in %s" % tar
308
309 # unpack into a tmp dir
310 tmpdir = tempfile.mkdtemp('.stg')
311 t.extractall(tmpdir)
312
313 # apply the series
314 __import_series(os.path.join(tmpdir, seriesfile), options)
315
316 # cleanup the tmpdir
317 shutil.rmtree(tmpdir)
318
9417ece4
CM
319def func(parser, options, args):
320 """Import a GNU diff file as a new patch
321 """
322 if len(args) > 1:
323 parser.error('incorrect number of arguments')
324
325 check_local_changes()
326 check_conflicts()
6972fd6b 327 check_head_top_equal(crt_series)
9417ece4
CM
328
329 if len(args) == 1:
330 filename = args[0]
331 else:
332 filename = None
333
4a8e79dc 334 if not options.url and filename:
47d51d91
CM
335 filename = os.path.abspath(filename)
336 directory.cd_to_topdir()
337
9417ece4
CM
338 if options.series:
339 __import_series(filename, options)
99c52915
CM
340 elif options.mbox:
341 __import_mbox(filename, options)
575c575e
CW
342 elif options.url:
343 __import_url(filename, options)
9417ece4 344 else:
fd1c0cfc 345 __import_file(filename, options)
9417ece4 346
6972fd6b 347 print_crt_patch(crt_series)