patman.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (c) 2011 The Chromium OS Authors.
  4. #
  5. # SPDX-License-Identifier: GPL-2.0+
  6. #
  7. """See README for more information"""
  8. from optparse import OptionParser
  9. import os
  10. import re
  11. import sys
  12. import unittest
  13. # Our modules
  14. try:
  15. from patman import checkpatch, command, gitutil, patchstream, \
  16. project, settings, terminal, test
  17. except ImportError:
  18. import checkpatch
  19. import command
  20. import gitutil
  21. import patchstream
  22. import project
  23. import settings
  24. import terminal
  25. import test
  26. parser = OptionParser()
  27. parser.add_option('-H', '--full-help', action='store_true', dest='full_help',
  28. default=False, help='Display the README file')
  29. parser.add_option('-c', '--count', dest='count', type='int',
  30. default=-1, help='Automatically create patches from top n commits')
  31. parser.add_option('-i', '--ignore-errors', action='store_true',
  32. dest='ignore_errors', default=False,
  33. help='Send patches email even if patch errors are found')
  34. parser.add_option('-m', '--no-maintainers', action='store_false',
  35. dest='add_maintainers', default=True,
  36. help="Don't cc the file maintainers automatically")
  37. parser.add_option('-n', '--dry-run', action='store_true', dest='dry_run',
  38. default=False, help="Do a dry run (create but don't email patches)")
  39. parser.add_option('-p', '--project', default=project.DetectProject(),
  40. help="Project name; affects default option values and "
  41. "aliases [default: %default]")
  42. parser.add_option('-r', '--in-reply-to', type='string', action='store',
  43. help="Message ID that this series is in reply to")
  44. parser.add_option('-s', '--start', dest='start', type='int',
  45. default=0, help='Commit to start creating patches from (0 = HEAD)')
  46. parser.add_option('-t', '--ignore-bad-tags', action='store_true',
  47. default=False, help='Ignore bad tags / aliases')
  48. parser.add_option('--test', action='store_true', dest='test',
  49. default=False, help='run tests')
  50. parser.add_option('-v', '--verbose', action='store_true', dest='verbose',
  51. default=False, help='Verbose output of errors and warnings')
  52. parser.add_option('--cc-cmd', dest='cc_cmd', type='string', action='store',
  53. default=None, help='Output cc list for patch file (used by git)')
  54. parser.add_option('--no-check', action='store_false', dest='check_patch',
  55. default=True,
  56. help="Don't check for patch compliance")
  57. parser.add_option('--no-tags', action='store_false', dest='process_tags',
  58. default=True, help="Don't process subject tags as aliaes")
  59. parser.usage += """
  60. Create patches from commits in a branch, check them and email them as
  61. specified by tags you place in the commits. Use -n to do a dry run first."""
  62. # Parse options twice: first to get the project and second to handle
  63. # defaults properly (which depends on project).
  64. (options, args) = parser.parse_args()
  65. settings.Setup(parser, options.project, '')
  66. (options, args) = parser.parse_args()
  67. if __name__ != "__main__":
  68. pass
  69. # Run our meagre tests
  70. elif options.test:
  71. import doctest
  72. sys.argv = [sys.argv[0]]
  73. suite = unittest.TestLoader().loadTestsFromTestCase(test.TestPatch)
  74. result = unittest.TestResult()
  75. suite.run(result)
  76. for module in ['gitutil', 'settings']:
  77. suite = doctest.DocTestSuite(module)
  78. suite.run(result)
  79. # TODO: Surely we can just 'print' result?
  80. print result
  81. for test, err in result.errors:
  82. print err
  83. for test, err in result.failures:
  84. print err
  85. # Called from git with a patch filename as argument
  86. # Printout a list of additional CC recipients for this patch
  87. elif options.cc_cmd:
  88. fd = open(options.cc_cmd, 'r')
  89. re_line = re.compile('(\S*) (.*)')
  90. for line in fd.readlines():
  91. match = re_line.match(line)
  92. if match and match.group(1) == args[0]:
  93. for cc in match.group(2).split(', '):
  94. cc = cc.strip()
  95. if cc:
  96. print cc
  97. fd.close()
  98. elif options.full_help:
  99. pager = os.getenv('PAGER')
  100. if not pager:
  101. pager = 'more'
  102. fname = os.path.join(os.path.dirname(sys.argv[0]), 'README')
  103. command.Run(pager, fname)
  104. # Process commits, produce patches files, check them, email them
  105. else:
  106. gitutil.Setup()
  107. if options.count == -1:
  108. # Work out how many patches to send if we can
  109. options.count = gitutil.CountCommitsToBranch() - options.start
  110. col = terminal.Color()
  111. if not options.count:
  112. str = 'No commits found to process - please use -c flag'
  113. sys.exit(col.Color(col.RED, str))
  114. # Read the metadata from the commits
  115. if options.count:
  116. series = patchstream.GetMetaData(options.start, options.count)
  117. cover_fname, args = gitutil.CreatePatches(options.start, options.count,
  118. series)
  119. # Fix up the patch files to our liking, and insert the cover letter
  120. series = patchstream.FixPatches(series, args)
  121. if series and cover_fname and series.get('cover'):
  122. patchstream.InsertCoverLetter(cover_fname, series, options.count)
  123. # Do a few checks on the series
  124. series.DoChecks()
  125. # Check the patches, and run them through 'git am' just to be sure
  126. if options.check_patch:
  127. ok = checkpatch.CheckPatches(options.verbose, args)
  128. else:
  129. ok = True
  130. cc_file = series.MakeCcFile(options.process_tags, cover_fname,
  131. not options.ignore_bad_tags,
  132. options.add_maintainers)
  133. # Email the patches out (giving the user time to check / cancel)
  134. cmd = ''
  135. its_a_go = ok or options.ignore_errors
  136. if its_a_go:
  137. cmd = gitutil.EmailPatches(series, cover_fname, args,
  138. options.dry_run, not options.ignore_bad_tags, cc_file,
  139. in_reply_to=options.in_reply_to)
  140. else:
  141. print col.Color(col.RED, "Not sending emails due to errors/warnings")
  142. # For a dry run, just show our actions as a sanity check
  143. if options.dry_run:
  144. series.ShowActions(args, cmd, options.process_tags)
  145. if not its_a_go:
  146. print col.Color(col.RED, "Email would not be sent")
  147. os.remove(cc_file)