Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 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 |
Doug Anderson | a1dcee8 | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 37 | import project |
Doug Anderson | 8568bae | 2012-12-03 14:43:17 +0000 | [diff] [blame] | 38 | import settings |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 39 | import terminal |
| 40 | import test |
| 41 | |
| 42 | |
| 43 | parser = OptionParser() |
| 44 | parser.add_option('-H', '--full-help', action='store_true', dest='full_help', |
| 45 | default=False, help='Display the README file') |
| 46 | parser.add_option('-c', '--count', dest='count', type='int', |
| 47 | default=-1, help='Automatically create patches from top n commits') |
| 48 | parser.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') |
| 51 | parser.add_option('-n', '--dry-run', action='store_true', dest='dry_run', |
Simon Glass | ca706e7 | 2013-03-26 13:09:45 +0000 | [diff] [blame^] | 52 | default=False, help="Do a dry run (create but don't email patches)") |
Vadim Bendebury | 99adf6e | 2013-01-09 16:00:10 +0000 | [diff] [blame] | 53 | parser.add_option('-p', '--project', default=project.DetectProject(), |
| 54 | help="Project name; affects default option values and " |
| 55 | "aliases [default: %default]") |
Doug Anderson | 6d81992 | 2013-03-17 10:31:04 +0000 | [diff] [blame] | 56 | parser.add_option('-r', '--in-reply-to', type='string', action='store', |
| 57 | help="Message ID that this series is in reply to") |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 58 | parser.add_option('-s', '--start', dest='start', type='int', |
| 59 | default=0, help='Commit to start creating patches from (0 = HEAD)') |
| 60 | parser.add_option('-t', '--test', action='store_true', dest='test', |
| 61 | default=False, help='run tests') |
| 62 | parser.add_option('-v', '--verbose', action='store_true', dest='verbose', |
| 63 | default=False, help='Verbose output of errors and warnings') |
| 64 | parser.add_option('--cc-cmd', dest='cc_cmd', type='string', action='store', |
| 65 | default=None, help='Output cc list for patch file (used by git)') |
Vadim Bendebury | 99adf6e | 2013-01-09 16:00:10 +0000 | [diff] [blame] | 66 | parser.add_option('--no-check', action='store_false', dest='check_patch', |
| 67 | default=True, |
| 68 | help="Don't check for patch compliance") |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 69 | parser.add_option('--no-tags', action='store_false', dest='process_tags', |
| 70 | default=True, help="Don't process subject tags as aliaes") |
| 71 | |
| 72 | parser.usage = """patman [options] |
| 73 | |
| 74 | Create patches from commits in a branch, check them and email them as |
Simon Glass | ca706e7 | 2013-03-26 13:09:45 +0000 | [diff] [blame^] | 75 | specified by tags you place in the commits. Use -n to do a dry run first.""" |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 76 | |
Doug Anderson | 8568bae | 2012-12-03 14:43:17 +0000 | [diff] [blame] | 77 | |
Doug Anderson | a1dcee8 | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 78 | # Parse options twice: first to get the project and second to handle |
| 79 | # defaults properly (which depends on project). |
| 80 | (options, args) = parser.parse_args() |
| 81 | settings.Setup(parser, options.project, '') |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 82 | (options, args) = parser.parse_args() |
| 83 | |
| 84 | # Run our meagre tests |
| 85 | if options.test: |
| 86 | import doctest |
| 87 | |
| 88 | sys.argv = [sys.argv[0]] |
| 89 | suite = unittest.TestLoader().loadTestsFromTestCase(test.TestPatch) |
| 90 | result = unittest.TestResult() |
| 91 | suite.run(result) |
| 92 | |
Doug Anderson | 656cffe | 2012-12-03 14:43:19 +0000 | [diff] [blame] | 93 | for module in ['gitutil', 'settings']: |
| 94 | suite = doctest.DocTestSuite(module) |
| 95 | suite.run(result) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 96 | |
| 97 | # TODO: Surely we can just 'print' result? |
| 98 | print result |
| 99 | for test, err in result.errors: |
| 100 | print err |
| 101 | for test, err in result.failures: |
| 102 | print err |
| 103 | |
| 104 | # Called from git with a patch filename as argument |
| 105 | # Printout a list of additional CC recipients for this patch |
| 106 | elif options.cc_cmd: |
| 107 | fd = open(options.cc_cmd, 'r') |
| 108 | re_line = re.compile('(\S*) (.*)') |
| 109 | for line in fd.readlines(): |
| 110 | match = re_line.match(line) |
| 111 | if match and match.group(1) == args[0]: |
| 112 | for cc in match.group(2).split(', '): |
| 113 | cc = cc.strip() |
| 114 | if cc: |
| 115 | print cc |
| 116 | fd.close() |
| 117 | |
| 118 | elif options.full_help: |
| 119 | pager = os.getenv('PAGER') |
| 120 | if not pager: |
| 121 | pager = 'more' |
| 122 | fname = os.path.join(os.path.dirname(sys.argv[0]), 'README') |
| 123 | command.Run(pager, fname) |
| 124 | |
| 125 | # Process commits, produce patches files, check them, email them |
| 126 | else: |
| 127 | gitutil.Setup() |
| 128 | |
| 129 | if options.count == -1: |
| 130 | # Work out how many patches to send if we can |
| 131 | options.count = gitutil.CountCommitsToBranch() - options.start |
| 132 | |
| 133 | col = terminal.Color() |
| 134 | if not options.count: |
| 135 | str = 'No commits found to process - please use -c flag' |
| 136 | print col.Color(col.RED, str) |
| 137 | sys.exit(1) |
| 138 | |
| 139 | # Read the metadata from the commits |
| 140 | if options.count: |
| 141 | series = patchstream.GetMetaData(options.start, options.count) |
| 142 | cover_fname, args = gitutil.CreatePatches(options.start, options.count, |
| 143 | series) |
| 144 | |
| 145 | # Fix up the patch files to our liking, and insert the cover letter |
| 146 | series = patchstream.FixPatches(series, args) |
| 147 | if series and cover_fname and series.get('cover'): |
| 148 | patchstream.InsertCoverLetter(cover_fname, series, options.count) |
| 149 | |
| 150 | # Do a few checks on the series |
| 151 | series.DoChecks() |
| 152 | |
| 153 | # Check the patches, and run them through 'git am' just to be sure |
Vadim Bendebury | 99adf6e | 2013-01-09 16:00:10 +0000 | [diff] [blame] | 154 | if options.check_patch: |
| 155 | ok = checkpatch.CheckPatches(options.verbose, args) |
| 156 | else: |
| 157 | ok = True |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 158 | if not gitutil.ApplyPatches(options.verbose, args, |
| 159 | options.count + options.start): |
| 160 | ok = False |
| 161 | |
Doug Anderson | 3118725 | 2012-12-03 14:40:43 +0000 | [diff] [blame] | 162 | cc_file = series.MakeCcFile(options.process_tags, cover_fname) |
Doug Anderson | d94566a | 2012-12-03 14:40:42 +0000 | [diff] [blame] | 163 | |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 164 | # Email the patches out (giving the user time to check / cancel) |
| 165 | cmd = '' |
| 166 | if ok or options.ignore_errors: |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 167 | cmd = gitutil.EmailPatches(series, cover_fname, args, |
Doug Anderson | 6d81992 | 2013-03-17 10:31:04 +0000 | [diff] [blame] | 168 | options.dry_run, cc_file, in_reply_to=options.in_reply_to) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 169 | |
| 170 | # For a dry run, just show our actions as a sanity check |
| 171 | if options.dry_run: |
| 172 | series.ShowActions(args, cmd, options.process_tags) |
Doug Anderson | d94566a | 2012-12-03 14:40:42 +0000 | [diff] [blame] | 173 | |
| 174 | os.remove(cc_file) |