Do not create an empty patch if import failed without --reject
[stgit.git] / stgit / commands / imprt.py
blobde77635c99183c4eb655cb3e47f28ad39d9b67ba
1 __copyright__ = """
2 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
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.
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.
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 """
18 import sys, os, re, email, tarfile
19 from mailbox import UnixMailbox
20 from StringIO import StringIO
21 from stgit.argparse import opt
22 from stgit.commands.common import *
23 from stgit.utils import *
24 from stgit.out import *
25 from stgit import argparse, stack, git
27 name = 'import'
28 help = 'Import a GNU diff file as a new patch'
29 kind = 'patch'
30 usage = ['[options] [<file>|<url>]']
31 description = """
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.
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.
44 The patch description has to be separated from the data with a '---'
45 line."""
47 args = [argparse.files]
48 options = [
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',
54 short = 'Import a series of patches', long = """
55 Import a series of patches from a series file or a tar archive."""),
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('-p', '--strip', type = 'int', metavar = 'N',
61 short = 'Remove N leading slashes from diff paths (default 1)'),
62 opt('-t', '--stripname', action = 'store_true',
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'),
68 opt('-b', '--base', args = [argparse.commit],
69 short = 'Use BASE instead of HEAD for file importing'),
70 opt('--reject', action = 'store_true',
71 short = 'Leave the rejected hunks in corresponding *.rej files'),
72 opt('-e', '--edit', action = 'store_true',
73 short = 'Invoke an editor for the patch description'),
74 opt('-d', '--showdiff', action = 'store_true',
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'),
84 ] + argparse.sign_options()
86 directory = DirectoryHasRepository(log = True)
88 def __strip_patch_name(name):
89 stripped = re.sub('^[0-9]+-(.*)$', '\g<1>', name)
90 stripped = re.sub('^(.*)\.(diff|patch)$', '\g<1>', stripped)
92 return stripped
94 def __replace_slashes_with_dashes(name):
95 stripped = name.replace('/', '-')
97 return stripped
99 def __create_patch(filename, message, author_name, author_email,
100 author_date, diff, options):
101 """Create a new patch on the stack
103 if options.name:
104 patch = options.name
105 elif filename:
106 patch = os.path.basename(filename)
107 else:
108 patch = ''
109 if options.stripname:
110 patch = __strip_patch_name(patch)
112 if not patch:
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)
118 else:
119 # fix possible invalid characters in the patch name
120 patch = re.sub('[^\w.]+', '-', patch).strip('-')
122 if options.ignore and patch in crt_series.get_applied():
123 out.info('Ignoring already applied patch "%s"' % patch)
124 return
125 if options.replace and patch in crt_series.get_unapplied():
126 crt_series.delete_patch(patch, keep_log = True)
128 # refresh_patch() will invoke the editor in this case, with correct
129 # patch content
130 if not message:
131 can_edit = False
133 if options.author:
134 options.authname, options.authemail = name_email(options.author)
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
144 crt_series.new_patch(patch, message = message, can_edit = False,
145 author_name = author_name,
146 author_email = author_email,
147 author_date = author_date)
149 if not diff:
150 out.warn('No diff found, creating empty patch')
151 else:
152 out.start('Importing patch "%s"' % patch)
153 if options.base:
154 base = git_id(crt_series, options.base)
155 else:
156 base = None
157 try:
158 git.apply_patch(diff = diff, base = base, reject = options.reject,
159 strip = options.strip)
160 except git.GitException:
161 if not options.reject:
162 crt_series.delete_patch(patch)
163 raise
164 crt_series.refresh_patch(edit = options.edit,
165 show_patch = options.showdiff,
166 author_date = author_date,
167 sign_str = options.sign_str,
168 backup = False)
169 out.done()
171 def __mkpatchname(name, suffix):
172 if name.lower().endswith(suffix.lower()):
173 return name[:-len(suffix)]
174 return name
176 def __get_handle_and_name(filename):
177 """Return a file object and a patch name derived from filename
179 # see if it's a gzip'ed or bzip2'ed patch
180 import bz2, gzip
181 for copen, ext in [(gzip.open, '.gz'), (bz2.BZ2File, '.bz2')]:
182 try:
183 f = copen(filename)
184 f.read(1)
185 f.seek(0)
186 return (f, __mkpatchname(filename, ext))
187 except IOError, e:
188 pass
190 # plain old file...
191 return (open(filename), filename)
193 def __import_file(filename, options, patch = None):
194 """Import a patch from a file or standard input
196 pname = None
197 if filename:
198 (f, pname) = __get_handle_and_name(filename)
199 else:
200 f = sys.stdin
202 if patch:
203 pname = patch
204 elif not pname:
205 pname = filename
207 if options.mail:
208 try:
209 msg = email.message_from_file(f)
210 except Exception, ex:
211 raise CmdException, 'error parsing the e-mail file: %s' % str(ex)
212 message, author_name, author_email, author_date, diff = \
213 parse_mail(msg)
214 else:
215 message, author_name, author_email, author_date, diff = \
216 parse_patch(f.read(), contains_diff = True)
218 if filename:
219 f.close()
221 __create_patch(pname, message, author_name, author_email,
222 author_date, diff, options)
224 def __import_series(filename, options):
225 """Import a series of patches
227 applied = crt_series.get_applied()
229 if filename:
230 if tarfile.is_tarfile(filename):
231 __import_tarfile(filename, options)
232 return
233 f = file(filename)
234 patchdir = os.path.dirname(filename)
235 else:
236 f = sys.stdin
237 patchdir = ''
239 for line in f:
240 patch = re.sub('#.*$', '', line).strip()
241 if not patch:
242 continue
243 patchfile = os.path.join(patchdir, patch)
244 patch = __replace_slashes_with_dashes(patch);
246 __import_file(patchfile, options, patch)
248 if filename:
249 f.close()
251 def __import_mbox(filename, options):
252 """Import a series from an mbox file
254 if filename:
255 f = file(filename, 'rb')
256 else:
257 f = StringIO(sys.stdin.read())
259 try:
260 mbox = UnixMailbox(f, email.message_from_file)
261 except Exception, ex:
262 raise CmdException, 'error parsing the mbox file: %s' % str(ex)
264 for msg in mbox:
265 message, author_name, author_email, author_date, diff = \
266 parse_mail(msg)
267 __create_patch(None, message, author_name, author_email,
268 author_date, diff, options)
270 f.close()
272 def __import_url(url, options):
273 """Import a patch from a URL
275 import urllib
276 import tempfile
278 if not url:
279 raise CmdException('URL argument required')
281 patch = os.path.basename(urllib.unquote(url))
282 filename = os.path.join(tempfile.gettempdir(), patch)
283 urllib.urlretrieve(url, filename)
284 __import_file(filename, options)
286 def __import_tarfile(tar, options):
287 """Import patch series from a tar archive
289 import tempfile
290 import shutil
292 if not tarfile.is_tarfile(tar):
293 raise CmdException, "%s is not a tarfile!" % tar
295 t = tarfile.open(tar, 'r')
296 names = t.getnames()
298 # verify paths in the tarfile are safe
299 for n in names:
300 if n.startswith('/'):
301 raise CmdException, "Absolute path found in %s" % tar
302 if n.find("..") > -1:
303 raise CmdException, "Relative path found in %s" % tar
305 # find the series file
306 seriesfile = '';
307 for m in names:
308 if m.endswith('/series') or m == 'series':
309 seriesfile = m
310 break
311 if seriesfile == '':
312 raise CmdException, "no 'series' file found in %s" % tar
314 # unpack into a tmp dir
315 tmpdir = tempfile.mkdtemp('.stg')
316 t.extractall(tmpdir)
318 # apply the series
319 __import_series(os.path.join(tmpdir, seriesfile), options)
321 # cleanup the tmpdir
322 shutil.rmtree(tmpdir)
324 def func(parser, options, args):
325 """Import a GNU diff file as a new patch
327 if len(args) > 1:
328 parser.error('incorrect number of arguments')
330 check_local_changes()
331 check_conflicts()
332 check_head_top_equal(crt_series)
334 if len(args) == 1:
335 filename = args[0]
336 else:
337 filename = None
339 if not options.url and filename:
340 filename = os.path.abspath(filename)
341 directory.cd_to_topdir()
343 if options.series:
344 __import_series(filename, options)
345 elif options.mbox:
346 __import_mbox(filename, options)
347 elif options.url:
348 __import_url(filename, options)
349 else:
350 __import_file(filename, options)
352 print_crt_patch(crt_series)