[PATCH] Really fix import --edit invoking editor twice
[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
18import sys, os
19from optparse import OptionParser, make_option
20
21from stgit.commands.common import *
22from stgit.utils import *
23from stgit import stack, git
24
25
26help = 'import a GNU diff file as a new patch'
27usage = """%prog [options] [<file>]
28
29Create a new patch and apply the given GNU diff file (or the standard
30input). By default, the file name is used as the patch name but this
31can be overriden with the '--name' option. The patch can either be a
32normal file with the description at the top or it can have standard
33mail format, the Subject, From and Date headers being used for
34generating the patch information.
35
36The patch description has to be separated from the data with a '---'
37line. For a normal file, if no author information is given, the first
38'Signed-off-by:' line is used."""
39
40options = [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'),
33e580e0
CM
45 make_option('-e', '--edit',
46 help = 'invoke an editor for the patch description',
47 action = 'store_true'),
6ad48e48
PBG
48 make_option('-s', '--showpatch',
49 help = 'show the patch content in the editor buffer',
50 action = 'store_true'),
0d2cd1e4
CM
51 make_option('-a', '--author', metavar = '"NAME <EMAIL>"',
52 help = 'use "NAME <EMAIL>" as the author details'),
53 make_option('--authname',
54 help = 'use AUTHNAME as the author name'),
55 make_option('--authemail',
56 help = 'use AUTHEMAIL as the author e-mail'),
57 make_option('--authdate',
58 help = 'use AUTHDATE as the author date'),
59 make_option('--commname',
60 help = 'use COMMNAME as the committer name'),
61 make_option('--commemail',
62 help = 'use COMMEMAIL as the committer e-mail')]
63
64
65def __parse_mail(filename = None):
66 """Parse the input file in a mail format and return (description,
67 authname, authemail, authdate)
68 """
69 if filename:
70 f = file(filename)
71 else:
72 f = sys.stdin
73
74 descr = authname = authemail = authdate = None
75
76 # parse the headers
77 for line in f:
78 line = line.strip()
79 if re.match('from:\s+', line, re.I):
80 auth = re.findall('^.*?:\s+(.*)$', line)[0]
81 authname, authemail = name_email(auth)
82 elif re.match('date:\s+', line, re.I):
83 authdate = re.findall('^.*?:\s+(.*)$', line)[0]
84 elif re.match('subject:\s+', line, re.I):
85 descr = re.findall('^.*?:\s+(.*)$', line)[0]
86 elif line == '':
87 # end of headers
88 break
89
186e6b6b 90 # remove the '[*PATCH*]' expression in the subject
0d2cd1e4 91 if descr:
186e6b6b 92 descr = re.findall('^(\[[^\s]*PATCH.*?\])?\s*(.*)$', descr)[0][1]
0d2cd1e4
CM
93 descr += '\n\n'
94 else:
95 raise CmdException, 'Subject: line not found'
96
97 # the rest of the patch description
98 for line in f:
70893e13 99 if re.match('---\s*$', line) or re.match('diff -', line):
0d2cd1e4
CM
100 break
101 else:
102 descr += line
103 descr.rstrip()
104
105 if filename:
106 f.close()
107
108 return (descr, authname, authemail, authdate)
109
110def __parse_patch(filename = None):
111 """Parse the input file and return (description, authname,
112 authemail, authdate)
113 """
114 if filename:
115 f = file(filename)
116 else:
117 f = sys.stdin
118
119 authname = authemail = authdate = None
120
121 descr = ''
122 for line in f:
123 # the first 'Signed-of-by:' is the author
124 if not authname and re.match('signed-off-by:\s+', line, re.I):
125 auth = re.findall('^.*?:\s+(.*)$', line)[0]
126 authname, authemail = name_email(auth)
127
70893e13 128 if re.match('---\s*$', line) or re.match('diff -', line):
0d2cd1e4
CM
129 break
130 else:
131 descr += line
132 descr.rstrip()
133
134 if descr == '':
135 descr = None
136
137 if filename:
138 f.close()
139
140 return (descr, authname, authemail, authdate)
141
142def func(parser, options, args):
143 """Import a GNU diff file as a new patch
144 """
145 if len(args) > 1:
146 parser.error('incorrect number of arguments')
147
148 check_local_changes()
149 check_conflicts()
150 check_head_top_equal()
151
152 if len(args) == 1:
153 filename = args[0]
5185abc1 154 else:
0d2cd1e4 155 filename = None
5185abc1
CM
156
157 if options.name:
0d2cd1e4 158 patch = options.name
5185abc1
CM
159 elif filename:
160 patch = os.path.basename(filename)
0d2cd1e4
CM
161 else:
162 raise CmdException, 'Unkown patch name'
163
164 # the defaults
165 message = author_name = author_email = author_date = committer_name = \
166 committer_email = None
167
168 if options.author:
169 options.authname, options.authemail = name_email(options.author)
170
171 if options.mail:
172 message, author_name, author_email, author_date = \
173 __parse_mail(filename)
174 else:
175 message, author_name, author_email, author_date = \
176 __parse_patch(filename)
177
95742cfc
PBG
178 # refresh_patch() will invoke the editor in this case, with correct
179 # patch content
9d15ccd8 180 if not message:
95742cfc 181 can_edit = False
9d15ccd8 182
0d2cd1e4
CM
183 # override the automatically parsed settings
184 if options.authname:
185 author_name = options.authname
186 if options.authemail:
187 author_email = options.authemail
188 if options.authdate:
189 author_date = options.authdate
190 if options.commname:
191 committer_name = options.commname
192 if options.commemail:
193 committer_email = options.commemail
194
95742cfc 195 crt_series.new_patch(patch, message = message, can_edit = False,
0d2cd1e4
CM
196 author_name = author_name,
197 author_email = author_email,
198 author_date = author_date,
199 committer_name = committer_name,
200 committer_email = committer_email)
201
202 print 'Importing patch %s...' % patch,
203 sys.stdout.flush()
204
205 git.apply_patch(filename)
6ad48e48
PBG
206 crt_series.refresh_patch(edit = options.edit,
207 show_patch = options.showpatch)
0d2cd1e4
CM
208
209 print 'done'
210 print_crt_patch()