3 # Copyright (c) 2011 The Chromium OS Authors.
5 # See file CREDITS for list of people who contributed to this
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.
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.
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,
24 """See README for more information"""
26 from optparse import OptionParser
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',
52 default=False, help="Do a try run (create but don't email patches)")
53 parser.add_option('-p', '--project', default=project.DetectProject(),
54 help="Project name; affects default option values and "
55 "aliases [default: %default]")
56 parser.add_option('-r', '--in-reply-to', type='string', action='store',
57 help="Message ID that this series is in reply to")
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)')
66 parser.add_option('--no-check', action='store_false', dest='check_patch',
68 help="Don't check for patch compliance")
69 parser.add_option('--no-tags', action='store_false', dest='process_tags',
70 default=True, help="Don't process subject tags as aliaes")
72 parser.usage = """patman [options]
74 Create patches from commits in a branch, check them and email them as
75 specified by tags you place in the commits. Use -n to """
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, '')
82 (options, args) = parser.parse_args()
84 # Run our meagre tests
88 sys.argv = [sys.argv[0]]
89 suite = unittest.TestLoader().loadTestsFromTestCase(test.TestPatch)
90 result = unittest.TestResult()
93 for module in ['gitutil', 'settings']:
94 suite = doctest.DocTestSuite(module)
97 # TODO: Surely we can just 'print' result?
99 for test, err in result.errors:
101 for test, err in result.failures:
104 # Called from git with a patch filename as argument
105 # Printout a list of additional CC recipients for this patch
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(', '):
118 elif options.full_help:
119 pager = os.getenv('PAGER')
122 fname = os.path.join(os.path.dirname(sys.argv[0]), 'README')
123 command.Run(pager, fname)
125 # Process commits, produce patches files, check them, email them
129 if options.count == -1:
130 # Work out how many patches to send if we can
131 options.count = gitutil.CountCommitsToBranch() - options.start
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)
139 # Read the metadata from the commits
141 series = patchstream.GetMetaData(options.start, options.count)
142 cover_fname, args = gitutil.CreatePatches(options.start, options.count,
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)
150 # Do a few checks on the series
153 # Check the patches, and run them through 'git am' just to be sure
154 if options.check_patch:
155 ok = checkpatch.CheckPatches(options.verbose, args)
158 if not gitutil.ApplyPatches(options.verbose, args,
159 options.count + options.start):
162 cc_file = series.MakeCcFile(options.process_tags, cover_fname)
164 # Email the patches out (giving the user time to check / cancel)
166 if ok or options.ignore_errors:
167 cmd = gitutil.EmailPatches(series, cover_fname, args,
168 options.dry_run, cc_file, in_reply_to=options.in_reply_to)
170 # For a dry run, just show our actions as a sanity check
172 series.ShowActions(args, cmd, options.process_tags)