patman.py 6.6 KB

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