Commit | Line | Data |
---|---|---|
0d2cd1e4 CM |
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 | ||
6ef533bc | 18 | import sys, os, re, email |
99c52915 | 19 | from mailbox import UnixMailbox |
457c3093 | 20 | from StringIO import StringIO |
0d2cd1e4 CM |
21 | from optparse import OptionParser, make_option |
22 | ||
23 | from stgit.commands.common import * | |
24 | from stgit.utils import * | |
5e888f30 | 25 | from stgit.out import * |
20a52e06 | 26 | from stgit import argparse, stack, git |
0d2cd1e4 CM |
27 | |
28 | help = 'import a GNU diff file as a new patch' | |
575c575e | 29 | usage = """%prog [options] [<file>|<url>] |
0d2cd1e4 | 30 | |
b8a0986f CM |
31 | Create a new patch and apply the given GNU diff file (or the standard |
32 | input). By default, the file name is used as the patch name but this | |
388f63b6 | 33 | can be overridden with the '--name' option. The patch can either be a |
b8a0986f CM |
34 | normal file with the description at the top or it can have standard |
35 | mail format, the Subject, From and Date headers being used for | |
99c52915 CM |
36 | generating the patch information. The command can also read series and |
37 | mbox files. | |
38 | ||
39 | If a patch does not apply cleanly, the failed diff is written to the | |
40 | .stgit-failed.patch file and an empty StGIT patch is added to the | |
41 | stack. | |
0d2cd1e4 | 42 | |
b8a0986f | 43 | The patch description has to be separated from the data with a '---' |
99e73103 | 44 | line.""" |
0d2cd1e4 | 45 | |
6dd8fafa | 46 | directory = DirectoryHasRepository() |
0d2cd1e4 CM |
47 | options = [make_option('-m', '--mail', |
48 | help = 'import the patch from a standard e-mail file', | |
49 | action = 'store_true'), | |
99c52915 CM |
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'), | |
575c575e CW |
56 | make_option('-u', '--url', |
57 | help = 'import a patch from a URL', | |
58 | action = 'store_true'), | |
0d2cd1e4 CM |
59 | make_option('-n', '--name', |
60 | help = 'use NAME as the patch name'), | |
b0cdad5e CM |
61 | make_option('-t', '--strip', |
62 | help = 'strip numbering and extension from patch name', | |
63 | action = 'store_true'), | |
9417ece4 CM |
64 | make_option('-i', '--ignore', |
65 | help = 'ignore the applied patches in the series', | |
66 | action = 'store_true'), | |
034db15c CM |
67 | make_option('--replace', |
68 | help = 'replace the unapplied patches in the series', | |
69 | action = 'store_true'), | |
b21bc8d1 | 70 | make_option('-b', '--base', |
35344f86 | 71 | help = 'use BASE instead of HEAD for file importing'), |
33e580e0 CM |
72 | make_option('-e', '--edit', |
73 | help = 'invoke an editor for the patch description', | |
74 | action = 'store_true'), | |
9417ece4 | 75 | make_option('-p', '--showpatch', |
6ad48e48 PBG |
76 | help = 'show the patch content in the editor buffer', |
77 | action = 'store_true'), | |
0d2cd1e4 CM |
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', | |
130df01a | 89 | help = 'use COMMEMAIL as the committer e-mail') |
20a52e06 | 90 | ] + argparse.sign_options() |
0d2cd1e4 CM |
91 | |
92 | ||
b0cdad5e | 93 | def __strip_patch_name(name): |
bcb6d890 CM |
94 | stripped = re.sub('^[0-9]+-(.*)$', '\g<1>', name) |
95 | stripped = re.sub('^(.*)\.(diff|patch)$', '\g<1>', stripped) | |
96 | ||
97 | return stripped | |
b0cdad5e | 98 | |
613a2f16 PBG |
99 | def __replace_slashes_with_dashes(name): |
100 | stripped = name.replace('/', '-') | |
101 | ||
102 | return stripped | |
103 | ||
fd1c0cfc | 104 | def __create_patch(filename, message, author_name, author_email, |
99c52915 CM |
105 | author_date, diff, options): |
106 | """Create a new patch on the stack | |
0d2cd1e4 | 107 | """ |
fd1c0cfc CM |
108 | if options.name: |
109 | patch = options.name | |
110 | elif filename: | |
111 | patch = os.path.basename(filename) | |
112 | else: | |
113 | patch = '' | |
114 | if options.strip: | |
115 | patch = __strip_patch_name(patch) | |
6ef533bc | 116 | |
fff9bce5 | 117 | if not patch: |
c4f99b6c KH |
118 | if options.ignore or options.replace: |
119 | unacceptable_name = lambda name: False | |
120 | else: | |
121 | unacceptable_name = crt_series.patch_exists | |
122 | patch = make_patch_name(message, unacceptable_name) | |
fd1c0cfc CM |
123 | else: |
124 | # fix possible invalid characters in the patch name | |
125 | patch = re.sub('[^\w.]+', '-', patch).strip('-') | |
126 | ||
99c52915 | 127 | if options.ignore and patch in crt_series.get_applied(): |
27ac2b7e | 128 | out.info('Ignoring already applied patch "%s"' % patch) |
99c52915 CM |
129 | return |
130 | if options.replace and patch in crt_series.get_unapplied(): | |
c26ca1b2 | 131 | crt_series.delete_patch(patch, keep_log = True) |
fff9bce5 | 132 | |
95742cfc PBG |
133 | # refresh_patch() will invoke the editor in this case, with correct |
134 | # patch content | |
9d15ccd8 | 135 | if not message: |
95742cfc | 136 | can_edit = False |
9d15ccd8 | 137 | |
99c52915 CM |
138 | committer_name = committer_email = None |
139 | ||
140 | if options.author: | |
141 | options.authname, options.authemail = name_email(options.author) | |
142 | ||
0d2cd1e4 CM |
143 | # override the automatically parsed settings |
144 | if options.authname: | |
145 | author_name = options.authname | |
146 | if options.authemail: | |
147 | author_email = options.authemail | |
148 | if options.authdate: | |
149 | author_date = options.authdate | |
150 | if options.commname: | |
151 | committer_name = options.commname | |
152 | if options.commemail: | |
153 | committer_email = options.commemail | |
154 | ||
95742cfc | 155 | crt_series.new_patch(patch, message = message, can_edit = False, |
0d2cd1e4 CM |
156 | author_name = author_name, |
157 | author_email = author_email, | |
158 | author_date = author_date, | |
159 | committer_name = committer_name, | |
160 | committer_email = committer_email) | |
161 | ||
5f1629be CM |
162 | if not diff: |
163 | out.warn('No diff found, creating empty patch') | |
35344f86 | 164 | else: |
5f1629be CM |
165 | out.start('Importing patch "%s"' % patch) |
166 | if options.base: | |
6972fd6b KH |
167 | git.apply_patch(diff = diff, |
168 | base = git_id(crt_series, options.base)) | |
5f1629be CM |
169 | else: |
170 | git.apply_patch(diff = diff) | |
171 | crt_series.refresh_patch(edit = options.edit, | |
130df01a | 172 | show_patch = options.showpatch, |
cb688601 CM |
173 | sign_str = options.sign_str, |
174 | backup = False) | |
5f1629be | 175 | out.done() |
99c52915 | 176 | |
354d2031 CW |
177 | def __mkpatchname(name, suffix): |
178 | if name.lower().endswith(suffix.lower()): | |
179 | return name[:-len(suffix)] | |
180 | return name | |
181 | ||
182 | def __get_handle_and_name(filename): | |
183 | """Return a file object and a patch name derived from filename | |
184 | """ | |
185 | # see if it's a gzip'ed or bzip2'ed patch | |
186 | import bz2, gzip | |
187 | for copen, ext in [(gzip.open, '.gz'), (bz2.BZ2File, '.bz2')]: | |
188 | try: | |
189 | f = copen(filename) | |
190 | f.read(1) | |
191 | f.seek(0) | |
192 | return (f, __mkpatchname(filename, ext)) | |
193 | except IOError, e: | |
194 | pass | |
195 | ||
196 | # plain old file... | |
197 | return (open(filename), filename) | |
198 | ||
fd1c0cfc | 199 | def __import_file(filename, options, patch = None): |
99c52915 CM |
200 | """Import a patch from a file or standard input |
201 | """ | |
354d2031 | 202 | pname = None |
99c52915 | 203 | if filename: |
354d2031 | 204 | (f, pname) = __get_handle_and_name(filename) |
99c52915 CM |
205 | else: |
206 | f = sys.stdin | |
207 | ||
354d2031 CW |
208 | if patch: |
209 | pname = patch | |
210 | elif not pname: | |
211 | pname = filename | |
212 | ||
99c52915 CM |
213 | if options.mail: |
214 | try: | |
215 | msg = email.message_from_file(f) | |
216 | except Exception, ex: | |
217 | raise CmdException, 'error parsing the e-mail file: %s' % str(ex) | |
218 | message, author_name, author_email, author_date, diff = \ | |
ed60fdae | 219 | parse_mail(msg) |
99c52915 CM |
220 | else: |
221 | message, author_name, author_email, author_date, diff = \ | |
c37f9747 | 222 | parse_patch(f.read()) |
99c52915 CM |
223 | |
224 | if filename: | |
225 | f.close() | |
226 | ||
fd1c0cfc | 227 | __create_patch(pname, message, author_name, author_email, |
99c52915 | 228 | author_date, diff, options) |
9417ece4 CM |
229 | |
230 | def __import_series(filename, options): | |
231 | """Import a series of patches | |
232 | """ | |
233 | applied = crt_series.get_applied() | |
234 | ||
235 | if filename: | |
236 | f = file(filename) | |
237 | patchdir = os.path.dirname(filename) | |
238 | else: | |
239 | f = sys.stdin | |
240 | patchdir = '' | |
241 | ||
242 | for line in f: | |
243 | patch = re.sub('#.*$', '', line).strip() | |
244 | if not patch: | |
245 | continue | |
bcb6d890 | 246 | patchfile = os.path.join(patchdir, patch) |
613a2f16 | 247 | patch = __replace_slashes_with_dashes(patch); |
9417ece4 | 248 | |
fd1c0cfc | 249 | __import_file(patchfile, options, patch) |
99c52915 CM |
250 | |
251 | if filename: | |
252 | f.close() | |
253 | ||
254 | def __import_mbox(filename, options): | |
255 | """Import a series from an mbox file | |
256 | """ | |
257 | if filename: | |
258 | f = file(filename, 'rb') | |
259 | else: | |
457c3093 | 260 | f = StringIO(sys.stdin.read()) |
99c52915 CM |
261 | |
262 | try: | |
263 | mbox = UnixMailbox(f, email.message_from_file) | |
264 | except Exception, ex: | |
265 | raise CmdException, 'error parsing the mbox file: %s' % str(ex) | |
266 | ||
267 | for msg in mbox: | |
268 | message, author_name, author_email, author_date, diff = \ | |
ed60fdae | 269 | parse_mail(msg) |
99c52915 CM |
270 | __create_patch(None, message, author_name, author_email, |
271 | author_date, diff, options) | |
272 | ||
457c3093 | 273 | f.close() |
9417ece4 | 274 | |
575c575e CW |
275 | def __import_url(url, options): |
276 | """Import a patch from a URL | |
277 | """ | |
278 | import urllib | |
279 | import tempfile | |
280 | ||
281 | if not url: | |
282 | parser.error('URL argument required') | |
283 | ||
fd1c0cfc CM |
284 | patch = os.path.basename(urllib.unquote(url)) |
285 | filename = os.path.join(tempfile.gettempdir(), patch) | |
286 | urllib.urlretrieve(url, filename) | |
287 | __import_file(filename, options) | |
575c575e | 288 | |
9417ece4 CM |
289 | def func(parser, options, args): |
290 | """Import a GNU diff file as a new patch | |
291 | """ | |
292 | if len(args) > 1: | |
293 | parser.error('incorrect number of arguments') | |
294 | ||
295 | check_local_changes() | |
296 | check_conflicts() | |
6972fd6b | 297 | check_head_top_equal(crt_series) |
9417ece4 CM |
298 | |
299 | if len(args) == 1: | |
300 | filename = args[0] | |
301 | else: | |
302 | filename = None | |
303 | ||
47d51d91 CM |
304 | if filename: |
305 | filename = os.path.abspath(filename) | |
306 | directory.cd_to_topdir() | |
307 | ||
9417ece4 CM |
308 | if options.series: |
309 | __import_series(filename, options) | |
99c52915 CM |
310 | elif options.mbox: |
311 | __import_mbox(filename, options) | |
575c575e CW |
312 | elif options.url: |
313 | __import_url(filename, options) | |
9417ece4 | 314 | else: |
fd1c0cfc | 315 | __import_file(filename, options) |
9417ece4 | 316 | |
6972fd6b | 317 | print_crt_patch(crt_series) |