Fix export error introduced by recent convertion
[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
99c52915 19from mailbox import UnixMailbox
457c3093 20from StringIO import StringIO
0d2cd1e4
CM
21from optparse import OptionParser, make_option
22
23from stgit.commands.common import *
24from stgit.utils import *
5e888f30 25from stgit.out import *
0d2cd1e4
CM
26from stgit import stack, git
27
28
29help = 'import a GNU diff file as a new patch'
575c575e 30usage = """%prog [options] [<file>|<url>]
0d2cd1e4 31
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
6dd8fafa 47directory = DirectoryHasRepository()
0d2cd1e4
CM
48options = [make_option('-m', '--mail',
49 help = 'import the patch from a standard e-mail file',
50 action = 'store_true'),
99c52915
CM
51 make_option('-M', '--mbox',
52 help = 'import a series of patches from an mbox file',
53 action = 'store_true'),
54 make_option('-s', '--series',
55 help = 'import a series of patches',
56 action = 'store_true'),
575c575e
CW
57 make_option('-u', '--url',
58 help = 'import a patch from a URL',
59 action = 'store_true'),
0d2cd1e4
CM
60 make_option('-n', '--name',
61 help = 'use NAME as the patch name'),
b0cdad5e
CM
62 make_option('-t', '--strip',
63 help = 'strip numbering and extension from patch name',
64 action = 'store_true'),
9417ece4
CM
65 make_option('-i', '--ignore',
66 help = 'ignore the applied patches in the series',
67 action = 'store_true'),
034db15c
CM
68 make_option('--replace',
69 help = 'replace the unapplied patches in the series',
70 action = 'store_true'),
b21bc8d1 71 make_option('-b', '--base',
35344f86 72 help = 'use BASE instead of HEAD for file importing'),
33e580e0
CM
73 make_option('-e', '--edit',
74 help = 'invoke an editor for the patch description',
75 action = 'store_true'),
9417ece4 76 make_option('-p', '--showpatch',
6ad48e48
PBG
77 help = 'show the patch content in the editor buffer',
78 action = 'store_true'),
0d2cd1e4
CM
79 make_option('-a', '--author', metavar = '"NAME <EMAIL>"',
80 help = 'use "NAME <EMAIL>" as the author details'),
81 make_option('--authname',
82 help = 'use AUTHNAME as the author name'),
83 make_option('--authemail',
84 help = 'use AUTHEMAIL as the author e-mail'),
85 make_option('--authdate',
86 help = 'use AUTHDATE as the author date'),
87 make_option('--commname',
88 help = 'use COMMNAME as the committer name'),
89 make_option('--commemail',
130df01a
KH
90 help = 'use COMMEMAIL as the committer e-mail')
91 ] + make_sign_options()
0d2cd1e4
CM
92
93
b0cdad5e 94def __strip_patch_name(name):
bcb6d890
CM
95 stripped = re.sub('^[0-9]+-(.*)$', '\g<1>', name)
96 stripped = re.sub('^(.*)\.(diff|patch)$', '\g<1>', stripped)
97
98 return stripped
b0cdad5e 99
613a2f16
PBG
100def __replace_slashes_with_dashes(name):
101 stripped = name.replace('/', '-')
102
103 return stripped
104
fd1c0cfc 105def __create_patch(filename, message, author_name, author_email,
99c52915
CM
106 author_date, diff, options):
107 """Create a new patch on the stack
0d2cd1e4 108 """
fd1c0cfc
CM
109 if options.name:
110 patch = options.name
111 elif filename:
112 patch = os.path.basename(filename)
113 else:
114 patch = ''
115 if options.strip:
116 patch = __strip_patch_name(patch)
6ef533bc 117
fff9bce5 118 if not patch:
c4f99b6c
KH
119 if options.ignore or options.replace:
120 unacceptable_name = lambda name: False
121 else:
122 unacceptable_name = crt_series.patch_exists
123 patch = make_patch_name(message, unacceptable_name)
fd1c0cfc
CM
124 else:
125 # fix possible invalid characters in the patch name
126 patch = re.sub('[^\w.]+', '-', patch).strip('-')
127
99c52915 128 if options.ignore and patch in crt_series.get_applied():
27ac2b7e 129 out.info('Ignoring already applied patch "%s"' % patch)
99c52915
CM
130 return
131 if options.replace and patch in crt_series.get_unapplied():
c26ca1b2 132 crt_series.delete_patch(patch, keep_log = True)
fff9bce5 133
95742cfc
PBG
134 # refresh_patch() will invoke the editor in this case, with correct
135 # patch content
9d15ccd8 136 if not message:
95742cfc 137 can_edit = False
9d15ccd8 138
99c52915
CM
139 committer_name = committer_email = None
140
141 if options.author:
142 options.authname, options.authemail = name_email(options.author)
143
0d2cd1e4
CM
144 # override the automatically parsed settings
145 if options.authname:
146 author_name = options.authname
147 if options.authemail:
148 author_email = options.authemail
149 if options.authdate:
150 author_date = options.authdate
151 if options.commname:
152 committer_name = options.commname
153 if options.commemail:
154 committer_email = options.commemail
155
95742cfc 156 crt_series.new_patch(patch, message = message, can_edit = False,
0d2cd1e4
CM
157 author_name = author_name,
158 author_email = author_email,
159 author_date = author_date,
160 committer_name = committer_name,
161 committer_email = committer_email)
162
5f1629be
CM
163 if not diff:
164 out.warn('No diff found, creating empty patch')
35344f86 165 else:
5f1629be
CM
166 out.start('Importing patch "%s"' % patch)
167 if options.base:
6972fd6b
KH
168 git.apply_patch(diff = diff,
169 base = git_id(crt_series, options.base))
5f1629be
CM
170 else:
171 git.apply_patch(diff = diff)
172 crt_series.refresh_patch(edit = options.edit,
130df01a 173 show_patch = options.showpatch,
cb688601
CM
174 sign_str = options.sign_str,
175 backup = False)
5f1629be 176 out.done()
99c52915 177
fd1c0cfc 178def __import_file(filename, options, patch = None):
99c52915
CM
179 """Import a patch from a file or standard input
180 """
181 if filename:
182 f = file(filename)
183 else:
184 f = sys.stdin
185
186 if options.mail:
187 try:
188 msg = email.message_from_file(f)
189 except Exception, ex:
190 raise CmdException, 'error parsing the e-mail file: %s' % str(ex)
191 message, author_name, author_email, author_date, diff = \
ed60fdae 192 parse_mail(msg)
99c52915
CM
193 else:
194 message, author_name, author_email, author_date, diff = \
c37f9747 195 parse_patch(f.read())
99c52915
CM
196
197 if filename:
198 f.close()
199
fd1c0cfc
CM
200 if patch:
201 pname = patch
202 else:
203 pname = filename
204
205 __create_patch(pname, message, author_name, author_email,
99c52915 206 author_date, diff, options)
9417ece4
CM
207
208def __import_series(filename, options):
209 """Import a series of patches
210 """
211 applied = crt_series.get_applied()
212
213 if filename:
214 f = file(filename)
215 patchdir = os.path.dirname(filename)
216 else:
217 f = sys.stdin
218 patchdir = ''
219
220 for line in f:
221 patch = re.sub('#.*$', '', line).strip()
222 if not patch:
223 continue
bcb6d890 224 patchfile = os.path.join(patchdir, patch)
613a2f16 225 patch = __replace_slashes_with_dashes(patch);
9417ece4 226
fd1c0cfc 227 __import_file(patchfile, options, patch)
99c52915
CM
228
229 if filename:
230 f.close()
231
232def __import_mbox(filename, options):
233 """Import a series from an mbox file
234 """
235 if filename:
236 f = file(filename, 'rb')
237 else:
457c3093 238 f = StringIO(sys.stdin.read())
99c52915
CM
239
240 try:
241 mbox = UnixMailbox(f, email.message_from_file)
242 except Exception, ex:
243 raise CmdException, 'error parsing the mbox file: %s' % str(ex)
244
245 for msg in mbox:
246 message, author_name, author_email, author_date, diff = \
ed60fdae 247 parse_mail(msg)
99c52915
CM
248 __create_patch(None, message, author_name, author_email,
249 author_date, diff, options)
250
457c3093 251 f.close()
9417ece4 252
575c575e
CW
253def __import_url(url, options):
254 """Import a patch from a URL
255 """
256 import urllib
257 import tempfile
258
259 if not url:
260 parser.error('URL argument required')
261
fd1c0cfc
CM
262 patch = os.path.basename(urllib.unquote(url))
263 filename = os.path.join(tempfile.gettempdir(), patch)
264 urllib.urlretrieve(url, filename)
265 __import_file(filename, options)
575c575e 266
9417ece4
CM
267def func(parser, options, args):
268 """Import a GNU diff file as a new patch
269 """
270 if len(args) > 1:
271 parser.error('incorrect number of arguments')
272
273 check_local_changes()
274 check_conflicts()
6972fd6b 275 check_head_top_equal(crt_series)
9417ece4
CM
276
277 if len(args) == 1:
278 filename = args[0]
279 else:
280 filename = None
281
282 if options.series:
283 __import_series(filename, options)
99c52915
CM
284 elif options.mbox:
285 __import_mbox(filename, options)
575c575e
CW
286 elif options.url:
287 __import_url(filename, options)
9417ece4 288 else:
fd1c0cfc 289 __import_file(filename, options)
9417ece4 290
6972fd6b 291 print_crt_patch(crt_series)