patman.py 6.4 KB

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