patman.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. # Run our meagre tests
  68. if options.test:
  69. import doctest
  70. sys.argv = [sys.argv[0]]
  71. suite = unittest.TestLoader().loadTestsFromTestCase(test.TestPatch)
  72. result = unittest.TestResult()
  73. suite.run(result)
  74. for module in ['gitutil', 'settings']:
  75. suite = doctest.DocTestSuite(module)
  76. suite.run(result)
  77. # TODO: Surely we can just 'print' result?
  78. print result
  79. for test, err in result.errors:
  80. print err
  81. for test, err in result.failures:
  82. print err
  83. # Called from git with a patch filename as argument
  84. # Printout a list of additional CC recipients for this patch
  85. elif options.cc_cmd:
  86. fd = open(options.cc_cmd, 'r')
  87. re_line = re.compile('(\S*) (.*)')
  88. for line in fd.readlines():
  89. match = re_line.match(line)
  90. if match and match.group(1) == args[0]:
  91. for cc in match.group(2).split(', '):
  92. cc = cc.strip()
  93. if cc:
  94. print cc
  95. fd.close()
  96. elif options.full_help:
  97. pager = os.getenv('PAGER')
  98. if not pager:
  99. pager = 'more'
  100. fname = os.path.join(os.path.dirname(sys.argv[0]), 'README')
  101. command.Run(pager, fname)
  102. # Process commits, produce patches files, check them, email them
  103. else:
  104. gitutil.Setup()
  105. if options.count == -1:
  106. # Work out how many patches to send if we can
  107. options.count = gitutil.CountCommitsToBranch() - options.start
  108. col = terminal.Color()
  109. if not options.count:
  110. str = 'No commits found to process - please use -c flag'
  111. sys.exit(col.Color(col.RED, str))
  112. # Read the metadata from the commits
  113. if options.count:
  114. series = patchstream.GetMetaData(options.start, options.count)
  115. cover_fname, args = gitutil.CreatePatches(options.start, options.count,
  116. series)
  117. # Fix up the patch files to our liking, and insert the cover letter
  118. series = patchstream.FixPatches(series, args)
  119. if series and cover_fname and series.get('cover'):
  120. patchstream.InsertCoverLetter(cover_fname, series, options.count)
  121. # Do a few checks on the series
  122. series.DoChecks()
  123. # Check the patches, and run them through 'git am' just to be sure
  124. if options.check_patch:
  125. ok = checkpatch.CheckPatches(options.verbose, args)
  126. else:
  127. ok = True
  128. cc_file = series.MakeCcFile(options.process_tags, cover_fname,
  129. not options.ignore_bad_tags,
  130. options.add_maintainers)
  131. # Email the patches out (giving the user time to check / cancel)
  132. cmd = ''
  133. its_a_go = ok or options.ignore_errors
  134. if its_a_go:
  135. cmd = gitutil.EmailPatches(series, cover_fname, args,
  136. options.dry_run, not options.ignore_bad_tags, cc_file,
  137. in_reply_to=options.in_reply_to)
  138. else:
  139. print col.Color(col.RED, "Not sending emails due to errors/warnings")
  140. # For a dry run, just show our actions as a sanity check
  141. if options.dry_run:
  142. series.ShowActions(args, cmd, options.process_tags)
  143. if not its_a_go:
  144. print col.Color(col.RED, "Email would not be sent")
  145. os.remove(cc_file)