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