| 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 | |
| 18 | import sys, os |
| 19 | from optparse import OptionParser, make_option |
| 20 | |
| 21 | from stgit.commands.common import * |
| 22 | from stgit.utils import * |
| 23 | from stgit import stack, git |
| 24 | |
| 25 | |
| 26 | help = 'import a GNU diff file as a new patch' |
| 27 | usage = """%prog [options] [<file>] |
| 28 | |
| 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. |
| 35 | |
| 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.""" |
| 39 | |
| 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'), |
| 45 | make_option('-a', '--author', metavar = '"NAME <EMAIL>"', |
| 46 | help = 'use "NAME <EMAIL>" as the author details'), |
| 47 | make_option('--authname', |
| 48 | help = 'use AUTHNAME as the author name'), |
| 49 | make_option('--authemail', |
| 50 | help = 'use AUTHEMAIL as the author e-mail'), |
| 51 | make_option('--authdate', |
| 52 | help = 'use AUTHDATE as the author date'), |
| 53 | make_option('--commname', |
| 54 | help = 'use COMMNAME as the committer name'), |
| 55 | make_option('--commemail', |
| 56 | help = 'use COMMEMAIL as the committer e-mail')] |
| 57 | |
| 58 | |
| 59 | def __parse_mail(filename = None): |
| 60 | """Parse the input file in a mail format and return (description, |
| 61 | authname, authemail, authdate) |
| 62 | """ |
| 63 | if filename: |
| 64 | f = file(filename) |
| 65 | else: |
| 66 | f = sys.stdin |
| 67 | |
| 68 | descr = authname = authemail = authdate = None |
| 69 | |
| 70 | # parse the headers |
| 71 | for line in f: |
| 72 | line = line.strip() |
| 73 | if re.match('from:\s+', line, re.I): |
| 74 | auth = re.findall('^.*?:\s+(.*)$', line)[0] |
| 75 | authname, authemail = name_email(auth) |
| 76 | elif re.match('date:\s+', line, re.I): |
| 77 | authdate = re.findall('^.*?:\s+(.*)$', line)[0] |
| 78 | elif re.match('subject:\s+', line, re.I): |
| 79 | descr = re.findall('^.*?:\s+(.*)$', line)[0] |
| 80 | elif line == '': |
| 81 | # end of headers |
| 82 | break |
| 83 | |
| 84 | # remove extra '[*PATCH]', 'name:' in the subject |
| 85 | if descr: |
| 86 | descr = re.findall('^(\[[^\s]*PATCH.*?\])?\s*([^\s]*:)?\s*(.*)$', |
| 87 | descr)[0][2] |
| 88 | descr += '\n\n' |
| 89 | else: |
| 90 | raise CmdException, 'Subject: line not found' |
| 91 | |
| 92 | # the rest of the patch description |
| 93 | for line in f: |
| 94 | if re.match('----*\s*$', line) or re.match('diff -', line): |
| 95 | break |
| 96 | else: |
| 97 | descr += line |
| 98 | descr.rstrip() |
| 99 | |
| 100 | if filename: |
| 101 | f.close() |
| 102 | |
| 103 | return (descr, authname, authemail, authdate) |
| 104 | |
| 105 | def __parse_patch(filename = None): |
| 106 | """Parse the input file and return (description, authname, |
| 107 | authemail, authdate) |
| 108 | """ |
| 109 | if filename: |
| 110 | f = file(filename) |
| 111 | else: |
| 112 | f = sys.stdin |
| 113 | |
| 114 | authname = authemail = authdate = None |
| 115 | |
| 116 | descr = '' |
| 117 | for line in f: |
| 118 | # the first 'Signed-of-by:' is the author |
| 119 | if not authname and re.match('signed-off-by:\s+', line, re.I): |
| 120 | auth = re.findall('^.*?:\s+(.*)$', line)[0] |
| 121 | authname, authemail = name_email(auth) |
| 122 | |
| 123 | if re.match('----*\s*$', line) or re.match('diff -', line): |
| 124 | break |
| 125 | else: |
| 126 | descr += line |
| 127 | descr.rstrip() |
| 128 | |
| 129 | if descr == '': |
| 130 | descr = None |
| 131 | |
| 132 | if filename: |
| 133 | f.close() |
| 134 | |
| 135 | return (descr, authname, authemail, authdate) |
| 136 | |
| 137 | def func(parser, options, args): |
| 138 | """Import a GNU diff file as a new patch |
| 139 | """ |
| 140 | if len(args) > 1: |
| 141 | parser.error('incorrect number of arguments') |
| 142 | |
| 143 | check_local_changes() |
| 144 | check_conflicts() |
| 145 | check_head_top_equal() |
| 146 | |
| 147 | if len(args) == 1: |
| 148 | filename = args[0] |
| 149 | patch = os.path.basename(filename) |
| 150 | elif options.name: |
| 151 | filename = None |
| 152 | patch = options.name |
| 153 | else: |
| 154 | raise CmdException, 'Unkown patch name' |
| 155 | |
| 156 | # the defaults |
| 157 | message = author_name = author_email = author_date = committer_name = \ |
| 158 | committer_email = None |
| 159 | |
| 160 | if options.author: |
| 161 | options.authname, options.authemail = name_email(options.author) |
| 162 | |
| 163 | if options.mail: |
| 164 | message, author_name, author_email, author_date = \ |
| 165 | __parse_mail(filename) |
| 166 | else: |
| 167 | message, author_name, author_email, author_date = \ |
| 168 | __parse_patch(filename) |
| 169 | |
| 170 | # override the automatically parsed settings |
| 171 | if options.authname: |
| 172 | author_name = options.authname |
| 173 | if options.authemail: |
| 174 | author_email = options.authemail |
| 175 | if options.authdate: |
| 176 | author_date = options.authdate |
| 177 | if options.commname: |
| 178 | committer_name = options.commname |
| 179 | if options.commemail: |
| 180 | committer_email = options.commemail |
| 181 | |
| 182 | crt_series.new_patch(patch, message = message, |
| 183 | author_name = author_name, |
| 184 | author_email = author_email, |
| 185 | author_date = author_date, |
| 186 | committer_name = committer_name, |
| 187 | committer_email = committer_email) |
| 188 | |
| 189 | print 'Importing patch %s...' % patch, |
| 190 | sys.stdout.flush() |
| 191 | |
| 192 | git.apply_patch(filename) |
| 193 | crt_series.refresh_patch() |
| 194 | |
| 195 | print 'done' |
| 196 | print_crt_patch() |