patman.py 5.9 KB

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