blob: e56dd01308f3e4bc843a76e1159e1413166dbb55 [file] [log] [blame]
Simon Glass0d24de92012-01-14 15:12:45 +00001#!/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
26from optparse import OptionParser
27import os
28import re
29import sys
30import unittest
31
32# Our modules
33import checkpatch
34import command
35import gitutil
36import patchstream
Doug Andersona1dcee82012-12-03 14:43:18 +000037import project
Doug Anderson8568bae2012-12-03 14:43:17 +000038import settings
Simon Glass0d24de92012-01-14 15:12:45 +000039import terminal
40import test
41
42
43parser = OptionParser()
44parser.add_option('-H', '--full-help', action='store_true', dest='full_help',
45 default=False, help='Display the README file')
46parser.add_option('-c', '--count', dest='count', type='int',
47 default=-1, help='Automatically create patches from top n commits')
48parser.add_option('-i', '--ignore-errors', action='store_true',
49 dest='ignore_errors', default=False,
50 help='Send patches email even if patch errors are found')
51parser.add_option('-n', '--dry-run', action='store_true', dest='dry_run',
52 default=False, help="Do a try run (create but don't email patches)")
53parser.add_option('-s', '--start', dest='start', type='int',
54 default=0, help='Commit to start creating patches from (0 = HEAD)')
55parser.add_option('-t', '--test', action='store_true', dest='test',
56 default=False, help='run tests')
57parser.add_option('-v', '--verbose', action='store_true', dest='verbose',
58 default=False, help='Verbose output of errors and warnings')
59parser.add_option('--cc-cmd', dest='cc_cmd', type='string', action='store',
60 default=None, help='Output cc list for patch file (used by git)')
61parser.add_option('--no-tags', action='store_false', dest='process_tags',
62 default=True, help="Don't process subject tags as aliaes")
Doug Andersona1dcee82012-12-03 14:43:18 +000063parser.add_option('-p', '--project', default=project.DetectProject(),
64 help="Project name; affects default option values and "
65 "aliases [default: %default]")
Simon Glass0d24de92012-01-14 15:12:45 +000066
67parser.usage = """patman [options]
68
69Create patches from commits in a branch, check them and email them as
70specified by tags you place in the commits. Use -n to """
71
Doug Anderson8568bae2012-12-03 14:43:17 +000072
Doug Andersona1dcee82012-12-03 14:43:18 +000073# Parse options twice: first to get the project and second to handle
74# defaults properly (which depends on project).
75(options, args) = parser.parse_args()
76settings.Setup(parser, options.project, '')
Simon Glass0d24de92012-01-14 15:12:45 +000077(options, args) = parser.parse_args()
78
79# Run our meagre tests
80if options.test:
81 import doctest
82
83 sys.argv = [sys.argv[0]]
84 suite = unittest.TestLoader().loadTestsFromTestCase(test.TestPatch)
85 result = unittest.TestResult()
86 suite.run(result)
87
Doug Anderson656cffe2012-12-03 14:43:19 +000088 for module in ['gitutil', 'settings']:
89 suite = doctest.DocTestSuite(module)
90 suite.run(result)
Simon Glass0d24de92012-01-14 15:12:45 +000091
92 # TODO: Surely we can just 'print' result?
93 print result
94 for test, err in result.errors:
95 print err
96 for test, err in result.failures:
97 print err
98
99# Called from git with a patch filename as argument
100# Printout a list of additional CC recipients for this patch
101elif options.cc_cmd:
102 fd = open(options.cc_cmd, 'r')
103 re_line = re.compile('(\S*) (.*)')
104 for line in fd.readlines():
105 match = re_line.match(line)
106 if match and match.group(1) == args[0]:
107 for cc in match.group(2).split(', '):
108 cc = cc.strip()
109 if cc:
110 print cc
111 fd.close()
112
113elif options.full_help:
114 pager = os.getenv('PAGER')
115 if not pager:
116 pager = 'more'
117 fname = os.path.join(os.path.dirname(sys.argv[0]), 'README')
118 command.Run(pager, fname)
119
120# Process commits, produce patches files, check them, email them
121else:
122 gitutil.Setup()
123
124 if options.count == -1:
125 # Work out how many patches to send if we can
126 options.count = gitutil.CountCommitsToBranch() - options.start
127
128 col = terminal.Color()
129 if not options.count:
130 str = 'No commits found to process - please use -c flag'
131 print col.Color(col.RED, str)
132 sys.exit(1)
133
134 # Read the metadata from the commits
135 if options.count:
136 series = patchstream.GetMetaData(options.start, options.count)
137 cover_fname, args = gitutil.CreatePatches(options.start, options.count,
138 series)
139
140 # Fix up the patch files to our liking, and insert the cover letter
141 series = patchstream.FixPatches(series, args)
142 if series and cover_fname and series.get('cover'):
143 patchstream.InsertCoverLetter(cover_fname, series, options.count)
144
145 # Do a few checks on the series
146 series.DoChecks()
147
148 # Check the patches, and run them through 'git am' just to be sure
149 ok = checkpatch.CheckPatches(options.verbose, args)
150 if not gitutil.ApplyPatches(options.verbose, args,
151 options.count + options.start):
152 ok = False
153
Doug Anderson31187252012-12-03 14:40:43 +0000154 cc_file = series.MakeCcFile(options.process_tags, cover_fname)
Doug Andersond94566a2012-12-03 14:40:42 +0000155
Simon Glass0d24de92012-01-14 15:12:45 +0000156 # Email the patches out (giving the user time to check / cancel)
157 cmd = ''
158 if ok or options.ignore_errors:
Simon Glass0d24de92012-01-14 15:12:45 +0000159 cmd = gitutil.EmailPatches(series, cover_fname, args,
160 options.dry_run, cc_file)
Simon Glass0d24de92012-01-14 15:12:45 +0000161
162 # For a dry run, just show our actions as a sanity check
163 if options.dry_run:
164 series.ShowActions(args, cmd, options.process_tags)
Doug Andersond94566a2012-12-03 14:40:42 +0000165
166 os.remove(cc_file)