blob: 23026becd00683ce5eaadbf28f181e09ea89fa6d [file] [log] [blame]
Simon Glass26132882012-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 Anderson31ffd7f2012-12-03 14:43:18 +000037import project
Doug Anderson3d3077c2012-12-03 14:43:17 +000038import settings
Simon Glass26132882012-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',
Simon Glass33bdd9e2013-03-26 13:09:45 +000052 default=False, help="Do a dry run (create but don't email patches)")
Vadim Bendeburyc549f082013-01-09 16:00:10 +000053parser.add_option('-p', '--project', default=project.DetectProject(),
54 help="Project name; affects default option values and "
55 "aliases [default: %default]")
Doug Anderson06f27ac2013-03-17 10:31:04 +000056parser.add_option('-r', '--in-reply-to', type='string', action='store',
57 help="Message ID that this series is in reply to")
Simon Glass26132882012-01-14 15:12:45 +000058parser.add_option('-s', '--start', dest='start', type='int',
59 default=0, help='Commit to start creating patches from (0 = HEAD)')
Simon Glass12ea5f42013-03-26 13:09:42 +000060parser.add_option('-t', '--ignore-bad-tags', action='store_true',
61 default=False, help='Ignore bad tags / aliases')
62parser.add_option('--test', action='store_true', dest='test',
Simon Glass26132882012-01-14 15:12:45 +000063 default=False, help='run tests')
64parser.add_option('-v', '--verbose', action='store_true', dest='verbose',
65 default=False, help='Verbose output of errors and warnings')
66parser.add_option('--cc-cmd', dest='cc_cmd', type='string', action='store',
67 default=None, help='Output cc list for patch file (used by git)')
Vadim Bendeburyc549f082013-01-09 16:00:10 +000068parser.add_option('--no-check', action='store_false', dest='check_patch',
69 default=True,
70 help="Don't check for patch compliance")
Simon Glass26132882012-01-14 15:12:45 +000071parser.add_option('--no-tags', action='store_false', dest='process_tags',
72 default=True, help="Don't process subject tags as aliaes")
73
74parser.usage = """patman [options]
75
76Create patches from commits in a branch, check them and email them as
Simon Glass33bdd9e2013-03-26 13:09:45 +000077specified by tags you place in the commits. Use -n to do a dry run first."""
Simon Glass26132882012-01-14 15:12:45 +000078
Doug Anderson3d3077c2012-12-03 14:43:17 +000079
Doug Anderson31ffd7f2012-12-03 14:43:18 +000080# Parse options twice: first to get the project and second to handle
81# defaults properly (which depends on project).
82(options, args) = parser.parse_args()
83settings.Setup(parser, options.project, '')
Simon Glass26132882012-01-14 15:12:45 +000084(options, args) = parser.parse_args()
85
86# Run our meagre tests
87if options.test:
88 import doctest
89
90 sys.argv = [sys.argv[0]]
91 suite = unittest.TestLoader().loadTestsFromTestCase(test.TestPatch)
92 result = unittest.TestResult()
93 suite.run(result)
94
Doug Anderson06a95152012-12-03 14:43:19 +000095 for module in ['gitutil', 'settings']:
96 suite = doctest.DocTestSuite(module)
97 suite.run(result)
Simon Glass26132882012-01-14 15:12:45 +000098
99 # TODO: Surely we can just 'print' result?
100 print result
101 for test, err in result.errors:
102 print err
103 for test, err in result.failures:
104 print err
105
106# Called from git with a patch filename as argument
107# Printout a list of additional CC recipients for this patch
108elif options.cc_cmd:
109 fd = open(options.cc_cmd, 'r')
110 re_line = re.compile('(\S*) (.*)')
111 for line in fd.readlines():
112 match = re_line.match(line)
113 if match and match.group(1) == args[0]:
114 for cc in match.group(2).split(', '):
115 cc = cc.strip()
116 if cc:
117 print cc
118 fd.close()
119
120elif options.full_help:
121 pager = os.getenv('PAGER')
122 if not pager:
123 pager = 'more'
124 fname = os.path.join(os.path.dirname(sys.argv[0]), 'README')
125 command.Run(pager, fname)
126
127# Process commits, produce patches files, check them, email them
128else:
129 gitutil.Setup()
130
131 if options.count == -1:
132 # Work out how many patches to send if we can
133 options.count = gitutil.CountCommitsToBranch() - options.start
134
135 col = terminal.Color()
136 if not options.count:
137 str = 'No commits found to process - please use -c flag'
138 print col.Color(col.RED, str)
139 sys.exit(1)
140
141 # Read the metadata from the commits
142 if options.count:
143 series = patchstream.GetMetaData(options.start, options.count)
144 cover_fname, args = gitutil.CreatePatches(options.start, options.count,
145 series)
146
147 # Fix up the patch files to our liking, and insert the cover letter
148 series = patchstream.FixPatches(series, args)
149 if series and cover_fname and series.get('cover'):
150 patchstream.InsertCoverLetter(cover_fname, series, options.count)
151
152 # Do a few checks on the series
153 series.DoChecks()
154
155 # Check the patches, and run them through 'git am' just to be sure
Vadim Bendeburyc549f082013-01-09 16:00:10 +0000156 if options.check_patch:
157 ok = checkpatch.CheckPatches(options.verbose, args)
158 else:
159 ok = True
Simon Glass26132882012-01-14 15:12:45 +0000160 if not gitutil.ApplyPatches(options.verbose, args,
161 options.count + options.start):
162 ok = False
163
Simon Glass12ea5f42013-03-26 13:09:42 +0000164 cc_file = series.MakeCcFile(options.process_tags, cover_fname,
165 not options.ignore_bad_tags)
Doug Anderson507e8e82012-12-03 14:40:42 +0000166
Simon Glass26132882012-01-14 15:12:45 +0000167 # Email the patches out (giving the user time to check / cancel)
168 cmd = ''
169 if ok or options.ignore_errors:
Simon Glass26132882012-01-14 15:12:45 +0000170 cmd = gitutil.EmailPatches(series, cover_fname, args,
Simon Glass12ea5f42013-03-26 13:09:42 +0000171 options.dry_run, not options.ignore_bad_tags, cc_file,
172 in_reply_to=options.in_reply_to)
Simon Glass26132882012-01-14 15:12:45 +0000173
174 # For a dry run, just show our actions as a sanity check
175 if options.dry_run:
176 series.ShowActions(args, cmd, options.process_tags)
Doug Anderson507e8e82012-12-03 14:40:42 +0000177
178 os.remove(cc_file)