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 overriden 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 '---'
37 line. For a normal file, if no author information is given, the first
38 'Signed-off-by:' line is used."""
40 options = [make_option('-m', '--mail',
41 help = 'import the patch from a standard e-mail file',
42 action = 'store_true'),
43 make_option('-n', '--name',
44 help = 'use NAME as the patch name'),
46 help = 'use BASE instead of HEAD for file importing'),
47 make_option('-e', '--edit',
48 help = 'invoke an editor for the patch description',
49 action = 'store_true'),
50 make_option('-s', '--showpatch',
51 help = 'show the patch content in the editor buffer',
52 action = 'store_true'),
53 make_option('-a', '--author', metavar = '"NAME <EMAIL>"',
54 help = 'use "NAME <EMAIL>" as the author details'),
55 make_option('--authname',
56 help = 'use AUTHNAME as the author name'),
57 make_option('--authemail',
58 help = 'use AUTHEMAIL as the author e-mail'),
59 make_option('--authdate',
60 help = 'use AUTHDATE as the author date'),
61 make_option('--commname',
62 help = 'use COMMNAME as the committer name'),
63 make_option('--commemail',
64 help = 'use COMMEMAIL as the committer e-mail')]
67 def __end_descr(line):
68 return re.match('---\s*$', line) or re.match('diff -', line) or \
69 re.match('Index: ', line)
71 def __parse_mail(filename = None):
72 """Parse the input file in a mail format and return (description,
73 authname, authemail, authdate)
80 descr = authname = authemail = authdate = None
88 if re.match('from:\s+', line, re.I):
89 auth = re.findall('^.*?:\s+(.*)$', line)[0]
90 authname, authemail = name_email(auth)
91 elif re.match('date:\s+', line, re.I):
92 authdate = re.findall('^.*?:\s+(.*)$', line)[0]
93 elif re.match('subject:\s+', line, re.I):
94 descr = re.findall('^.*?:\s+(.*)$', line)[0]
99 # remove the '[*PATCH*]' expression in the subject
101 descr = re.findall('^(\[[^\s]*[Pp][Aa][Tt][Cc][Hh].*?\])?\s*(.*)$',
105 raise CmdException, 'Subject: line not found'
107 # the rest of the patch description
112 if __end_descr(line):
121 return (descr, authname, authemail, authdate)
123 def __parse_patch(filename = None):
124 """Parse the input file and return (description, authname,
132 authname = authemail = authdate = None
140 # the first 'Signed-of-by:' is the author
141 if not authname and re.match('signed-off-by:\s+', line, re.I):
142 auth = re.findall('^.*?:\s+(.*)$', line)[0]
143 authname, authemail = name_email(auth)
145 if __end_descr(line):
157 return (descr, authname, authemail, authdate)
159 def func(parser, options, args):
160 """Import a GNU diff file as a new patch
163 parser.error('incorrect number of arguments')
165 check_local_changes()
167 check_head_top_equal()
177 patch = os.path.basename(filename)
179 raise CmdException, 'Unkown patch name'
182 message = author_name = author_email = author_date = committer_name = \
183 committer_email = None
186 options.authname, options.authemail = name_email(options.author)
189 message, author_name, author_email, author_date = \
190 __parse_mail(filename)
192 message, author_name, author_email, author_date = \
193 __parse_patch(filename)
195 # refresh_patch() will invoke the editor in this case, with correct
200 # override the automatically parsed settings
202 author_name = options.authname
203 if options.authemail:
204 author_email = options.authemail
206 author_date = options.authdate
208 committer_name = options.commname
209 if options.commemail:
210 committer_email = options.commemail
212 crt_series.new_patch(patch, message = message, can_edit = False,
213 author_name = author_name,
214 author_email = author_email,
215 author_date = author_date,
216 committer_name = committer_name,
217 committer_email = committer_email)
219 print 'Importing patch %s...' % patch,
223 orig_head = git.get_head()
224 git.switch(options.base)
227 git.apply_patch(filename)
228 except git.GitException, ex:
229 print >> sys.stderr, '"git apply" failed'
230 git.switch(orig_head)
233 top = crt_series.refresh_patch(commit_only = True)
234 git.switch(orig_head)
235 git.merge(options.base, orig_head, top)
237 git.apply_patch(filename)
239 crt_series.refresh_patch(edit = options.edit,
240 show_patch = options.showpatch)