]>
Commit | Line | Data |
---|---|---|
0d24de9d SG |
1 | #!/usr/bin/python |
2 | # | |
3 | # Copyright (c) 2011 The Chromium OS Authors. | |
4 | # | |
5 | # See file CREDITS for list of people who contributed to this | |
6 | # project. | |
7 | # | |
8 | # This program is free software; you can redistribute it and/or | |
9 | # modify it under the terms of the GNU General Public License as | |
10 | # published by the Free Software Foundation; either version 2 of | |
11 | # the License, or (at your option) any later version. | |
12 | # | |
13 | # This program is distributed in the hope that it will be useful, | |
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | # GNU General Public License for more details. | |
17 | # | |
18 | # You should have received a copy of the GNU General Public License | |
19 | # along with this program; if not, write to the Free Software | |
20 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
21 | # MA 02111-1307 USA | |
22 | # | |
23 | ||
24 | """See README for more information""" | |
25 | ||
26 | from optparse import OptionParser | |
27 | import os | |
28 | import re | |
29 | import sys | |
30 | import unittest | |
31 | ||
32 | # Our modules | |
33 | import checkpatch | |
34 | import command | |
35 | import gitutil | |
36 | import patchstream | |
a1dcee84 | 37 | import project |
8568baed | 38 | import settings |
0d24de9d SG |
39 | import terminal |
40 | import test | |
41 | ||
42 | ||
43 | parser = OptionParser() | |
902a9715 SG |
44 | parser.add_option('-a', '--no-apply', action='store_false', |
45 | dest='apply_patches', default=True, | |
46 | help="Don't test-apply patches with git am") | |
0d24de9d SG |
47 | parser.add_option('-H', '--full-help', action='store_true', dest='full_help', |
48 | default=False, help='Display the README file') | |
49 | parser.add_option('-c', '--count', dest='count', type='int', | |
50 | default=-1, help='Automatically create patches from top n commits') | |
51 | parser.add_option('-i', '--ignore-errors', action='store_true', | |
52 | dest='ignore_errors', default=False, | |
53 | help='Send patches email even if patch errors are found') | |
54 | parser.add_option('-n', '--dry-run', action='store_true', dest='dry_run', | |
ca706e76 | 55 | default=False, help="Do a dry run (create but don't email patches)") |
99adf6ed VB |
56 | parser.add_option('-p', '--project', default=project.DetectProject(), |
57 | help="Project name; affects default option values and " | |
58 | "aliases [default: %default]") | |
6d819925 DA |
59 | parser.add_option('-r', '--in-reply-to', type='string', action='store', |
60 | help="Message ID that this series is in reply to") | |
0d24de9d SG |
61 | parser.add_option('-s', '--start', dest='start', type='int', |
62 | default=0, help='Commit to start creating patches from (0 = HEAD)') | |
a1318f7c SG |
63 | parser.add_option('-t', '--ignore-bad-tags', action='store_true', |
64 | default=False, help='Ignore bad tags / aliases') | |
65 | parser.add_option('--test', action='store_true', dest='test', | |
0d24de9d SG |
66 | default=False, help='run tests') |
67 | parser.add_option('-v', '--verbose', action='store_true', dest='verbose', | |
68 | default=False, help='Verbose output of errors and warnings') | |
69 | parser.add_option('--cc-cmd', dest='cc_cmd', type='string', action='store', | |
70 | default=None, help='Output cc list for patch file (used by git)') | |
99adf6ed VB |
71 | parser.add_option('--no-check', action='store_false', dest='check_patch', |
72 | default=True, | |
73 | help="Don't check for patch compliance") | |
0d24de9d SG |
74 | parser.add_option('--no-tags', action='store_false', dest='process_tags', |
75 | default=True, help="Don't process subject tags as aliaes") | |
76 | ||
77 | parser.usage = """patman [options] | |
78 | ||
79 | Create patches from commits in a branch, check them and email them as | |
ca706e76 | 80 | specified by tags you place in the commits. Use -n to do a dry run first.""" |
0d24de9d | 81 | |
8568baed | 82 | |
a1dcee84 DA |
83 | # Parse options twice: first to get the project and second to handle |
84 | # defaults properly (which depends on project). | |
85 | (options, args) = parser.parse_args() | |
86 | settings.Setup(parser, options.project, '') | |
0d24de9d SG |
87 | (options, args) = parser.parse_args() |
88 | ||
89 | # Run our meagre tests | |
90 | if options.test: | |
91 | import doctest | |
92 | ||
93 | sys.argv = [sys.argv[0]] | |
94 | suite = unittest.TestLoader().loadTestsFromTestCase(test.TestPatch) | |
95 | result = unittest.TestResult() | |
96 | suite.run(result) | |
97 | ||
656cffeb DA |
98 | for module in ['gitutil', 'settings']: |
99 | suite = doctest.DocTestSuite(module) | |
100 | suite.run(result) | |
0d24de9d SG |
101 | |
102 | # TODO: Surely we can just 'print' result? | |
103 | print result | |
104 | for test, err in result.errors: | |
105 | print err | |
106 | for test, err in result.failures: | |
107 | print err | |
108 | ||
109 | # Called from git with a patch filename as argument | |
110 | # Printout a list of additional CC recipients for this patch | |
111 | elif options.cc_cmd: | |
112 | fd = open(options.cc_cmd, 'r') | |
113 | re_line = re.compile('(\S*) (.*)') | |
114 | for line in fd.readlines(): | |
115 | match = re_line.match(line) | |
116 | if match and match.group(1) == args[0]: | |
117 | for cc in match.group(2).split(', '): | |
118 | cc = cc.strip() | |
119 | if cc: | |
120 | print cc | |
121 | fd.close() | |
122 | ||
123 | elif options.full_help: | |
124 | pager = os.getenv('PAGER') | |
125 | if not pager: | |
126 | pager = 'more' | |
127 | fname = os.path.join(os.path.dirname(sys.argv[0]), 'README') | |
128 | command.Run(pager, fname) | |
129 | ||
130 | # Process commits, produce patches files, check them, email them | |
131 | else: | |
132 | gitutil.Setup() | |
133 | ||
134 | if options.count == -1: | |
135 | # Work out how many patches to send if we can | |
136 | options.count = gitutil.CountCommitsToBranch() - options.start | |
137 | ||
138 | col = terminal.Color() | |
139 | if not options.count: | |
140 | str = 'No commits found to process - please use -c flag' | |
141 | print col.Color(col.RED, str) | |
142 | sys.exit(1) | |
143 | ||
144 | # Read the metadata from the commits | |
145 | if options.count: | |
146 | series = patchstream.GetMetaData(options.start, options.count) | |
147 | cover_fname, args = gitutil.CreatePatches(options.start, options.count, | |
148 | series) | |
149 | ||
150 | # Fix up the patch files to our liking, and insert the cover letter | |
151 | series = patchstream.FixPatches(series, args) | |
152 | if series and cover_fname and series.get('cover'): | |
153 | patchstream.InsertCoverLetter(cover_fname, series, options.count) | |
154 | ||
155 | # Do a few checks on the series | |
156 | series.DoChecks() | |
157 | ||
158 | # Check the patches, and run them through 'git am' just to be sure | |
99adf6ed VB |
159 | if options.check_patch: |
160 | ok = checkpatch.CheckPatches(options.verbose, args) | |
161 | else: | |
162 | ok = True | |
902a9715 SG |
163 | if options.apply_patches: |
164 | if not gitutil.ApplyPatches(options.verbose, args, | |
165 | options.count + options.start): | |
166 | ok = False | |
0d24de9d | 167 | |
a1318f7c SG |
168 | cc_file = series.MakeCcFile(options.process_tags, cover_fname, |
169 | not options.ignore_bad_tags) | |
d94566a1 | 170 | |
0d24de9d SG |
171 | # Email the patches out (giving the user time to check / cancel) |
172 | cmd = '' | |
173 | if ok or options.ignore_errors: | |
0d24de9d | 174 | cmd = gitutil.EmailPatches(series, cover_fname, args, |
a1318f7c SG |
175 | options.dry_run, not options.ignore_bad_tags, cc_file, |
176 | in_reply_to=options.in_reply_to) | |
0d24de9d SG |
177 | |
178 | # For a dry run, just show our actions as a sanity check | |
179 | if options.dry_run: | |
180 | series.ShowActions(args, cmd, options.process_tags) | |
d94566a1 DA |
181 | |
182 | os.remove(cc_file) |