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
19 from optparse import OptionParser, make_option
21 from stgit.commands.common import *
22 from stgit.utils import *
23 from stgit import stack, git
26 help = 'import a GNU diff file as a new patch'
27 usage = """%prog [options] [<file>]
29 Create a new patch and apply the given GNU diff file (or the standard
30 input). By default, the file name is used as the patch name but this
31 can be overridden with the '--name' option. The patch can either be a
32 normal file with the description at the top or it can have standard
33 mail format, the Subject, From and Date headers being used for
34 generating the patch information.
36 The patch description has to be separated from the data with a '---'
39 options = [make_option('-m', '--mail',
40 help = 'import the patch from a standard e-mail file',
41 action = 'store_true'),
42 make_option('-n', '--name',
43 help = 'use NAME as the patch name'),
44 make_option('-t', '--strip',
45 help = 'strip numbering and extension from patch name',
46 action = 'store_true'),
47 make_option('-s', '--series',
48 help = 'import a series of patches',
49 action = 'store_true'),
50 make_option('-i', '--ignore',
51 help = 'ignore the applied patches in the series',
52 action = 'store_true'),
53 make_option('-b', '--base',
54 help = 'use BASE instead of HEAD for file importing'),
55 make_option('-e', '--edit',
56 help = 'invoke an editor for the patch description',
57 action = 'store_true'),
58 make_option('-p', '--showpatch',
59 help = 'show the patch content in the editor buffer',
60 action = 'store_true'),
61 make_option('-a', '--author', metavar = '"NAME <EMAIL>"',
62 help = 'use "NAME <EMAIL>" as the author details'),
63 make_option('--authname',
64 help = 'use AUTHNAME as the author name'),
65 make_option('--authemail',
66 help = 'use AUTHEMAIL as the author e-mail'),
67 make_option('--authdate',
68 help = 'use AUTHDATE as the author date'),
69 make_option('--commname',
70 help = 'use COMMNAME as the committer name'),
71 make_option('--commemail',
72 help = 'use COMMEMAIL as the committer e-mail')]
75 def __end_descr(line):
76 return re.match('---\s*$', line) or re.match('diff -', line) or \
77 re.match('Index: ', line)
79 def __strip_patch_name(name):
80 return re.sub('^[0-9]+-(.*)\.(diff|patch)$', '\g<1>', name)
82 def __parse_description(descr):
83 """Parse the patch description and return the new description and
84 author information (if any).
87 authname = authemail = authdate = None
89 descr_lines = [line.rstrip() for line in descr.split('\n')]
91 raise CmdException, "Empty patch description"
94 end = len(descr_lines)
96 # Parse the patch header
97 for pos in range(0, end):
98 if not descr_lines[pos]:
100 # check for a "From|Author:" line
101 if re.match('\s*(?:from|author):\s+', descr_lines[pos], re.I):
102 auth = re.findall('^.*?:\s+(.*)$', descr_lines[pos])[0]
103 authname, authemail = name_email(auth)
106 # check for a "Date:" line
107 if re.match('\s*date:\s+', descr_lines[pos], re.I):
108 authdate = re.findall('^.*?:\s+(.*)$', descr_lines[pos])[0]
114 subject = descr_lines[pos]
119 body = reduce(lambda x, y: x + '\n' + y, descr_lines[lasthdr:], '')
121 return (subject + body, authname, authemail, authdate)
123 def __parse_mail(filename = None):
124 """Parse the input file in a mail format and return (description,
125 authname, authemail, authdate)
132 descr = authname = authemail = authdate = None
140 if re.match('from:\s+', line, re.I):
141 auth = re.findall('^.*?:\s+(.*)$', line)[0]
142 authname, authemail = name_email(auth)
143 elif re.match('date:\s+', line, re.I):
144 authdate = re.findall('^.*?:\s+(.*)$', line)[0]
145 elif re.match('subject:\s+', line, re.I):
146 descr = re.findall('^.*?:\s+(.*)$', line)[0]
151 # remove the '[*PATCH*]' expression in the subject
153 descr = re.findall('^(\[[^\s]*[Pp][Aa][Tt][Cc][Hh].*?\])?\s*(.*)$',
157 raise CmdException, 'Subject: line not found'
159 # the rest of the patch description
164 if __end_descr(line):
173 # parse the description for author information
174 descr, descr_authname, descr_authemail, descr_authdate = __parse_description(descr)
176 authname = descr_authname
178 authemail = descr_authemail
180 authdate = descr_authdate
182 return (descr, authname, authemail, authdate)
184 def __parse_patch(filename = None):
185 """Parse the input file and return (description, authname,
199 if __end_descr(line):
208 descr, authname, authemail, authdate = __parse_description(descr)
210 # we don't yet have an agreed place for the creation date.
212 return (descr, authname, authemail, authdate)
214 def __import_patch(patch, filename, options):
215 """Import a patch from a file or standard input
218 message = author_name = author_email = author_date = committer_name = \
219 committer_email = None
222 options.authname, options.authemail = name_email(options.author)
225 message, author_name, author_email, author_date = \
226 __parse_mail(filename)
228 message, author_name, author_email, author_date = \
229 __parse_patch(filename)
231 # refresh_patch() will invoke the editor in this case, with correct
236 # override the automatically parsed settings
238 author_name = options.authname
239 if options.authemail:
240 author_email = options.authemail
242 author_date = options.authdate
244 committer_name = options.commname
245 if options.commemail:
246 committer_email = options.commemail
248 crt_series.new_patch(patch, message = message, can_edit = False,
249 author_name = author_name,
250 author_email = author_email,
251 author_date = author_date,
252 committer_name = committer_name,
253 committer_email = committer_email)
255 print 'Importing patch "%s"...' % patch,
259 git.apply_patch(filename, git_id(options.base))
261 git.apply_patch(filename)
263 crt_series.refresh_patch(edit = options.edit,
264 show_patch = options.showpatch)
268 def __import_series(filename, options):
269 """Import a series of patches
271 applied = crt_series.get_applied()
275 patchdir = os.path.dirname(filename)
281 patch = re.sub('#.*$', '', line).strip()
285 patch = __strip_patch_name(patch)
286 if options.ignore and patch in applied:
287 print 'Ignoring already applied patch "%s"' % patch
290 patchfile = os.path.join(patchdir, patch)
291 __import_patch(patch, patchfile, options)
293 def func(parser, options, args):
294 """Import a GNU diff file as a new patch
297 parser.error('incorrect number of arguments')
299 check_local_changes()
301 check_head_top_equal()
309 __import_series(filename, options)
314 patch = os.path.basename(filename)
316 raise CmdException, 'Unknown patch name'
318 patch = __strip_patch_name(patch)
320 __import_patch(patch, filename, options)