patman.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 terminal
  35. import test
  36. parser = OptionParser()
  37. parser.add_option('-H', '--full-help', action='store_true', dest='full_help',
  38. default=False, help='Display the README file')
  39. parser.add_option('-c', '--count', dest='count', type='int',
  40. default=-1, help='Automatically create patches from top n commits')
  41. parser.add_option('-i', '--ignore-errors', action='store_true',
  42. dest='ignore_errors', default=False,
  43. help='Send patches email even if patch errors are found')
  44. parser.add_option('-n', '--dry-run', action='store_true', dest='dry_run',
  45. default=False, help="Do a try run (create but don't email patches)")
  46. parser.add_option('-s', '--start', dest='start', type='int',
  47. default=0, help='Commit to start creating patches from (0 = HEAD)')
  48. parser.add_option('-t', '--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-tags', action='store_false', dest='process_tags',
  55. default=True, help="Don't process subject tags as aliaes")
  56. parser.usage = """patman [options]
  57. Create patches from commits in a branch, check them and email them as
  58. specified by tags you place in the commits. Use -n to """
  59. (options, args) = parser.parse_args()
  60. # Run our meagre tests
  61. if options.test:
  62. import doctest
  63. sys.argv = [sys.argv[0]]
  64. suite = unittest.TestLoader().loadTestsFromTestCase(test.TestPatch)
  65. result = unittest.TestResult()
  66. suite.run(result)
  67. suite = doctest.DocTestSuite('gitutil')
  68. suite.run(result)
  69. # TODO: Surely we can just 'print' result?
  70. print result
  71. for test, err in result.errors:
  72. print err
  73. for test, err in result.failures:
  74. print err
  75. # Called from git with a patch filename as argument
  76. # Printout a list of additional CC recipients for this patch
  77. elif options.cc_cmd:
  78. fd = open(options.cc_cmd, 'r')
  79. re_line = re.compile('(\S*) (.*)')
  80. for line in fd.readlines():
  81. match = re_line.match(line)
  82. if match and match.group(1) == args[0]:
  83. for cc in match.group(2).split(', '):
  84. cc = cc.strip()
  85. if cc:
  86. print cc
  87. fd.close()
  88. elif options.full_help:
  89. pager = os.getenv('PAGER')
  90. if not pager:
  91. pager = 'more'
  92. fname = os.path.join(os.path.dirname(sys.argv[0]), 'README')
  93. command.Run(pager, fname)
  94. # Process commits, produce patches files, check them, email them
  95. else:
  96. gitutil.Setup()
  97. if options.count == -1:
  98. # Work out how many patches to send if we can
  99. options.count = gitutil.CountCommitsToBranch() - options.start
  100. col = terminal.Color()
  101. if not options.count:
  102. str = 'No commits found to process - please use -c flag'
  103. print col.Color(col.RED, str)
  104. sys.exit(1)
  105. # Read the metadata from the commits
  106. if options.count:
  107. series = patchstream.GetMetaData(options.start, options.count)
  108. cover_fname, args = gitutil.CreatePatches(options.start, options.count,
  109. series)
  110. # Fix up the patch files to our liking, and insert the cover letter
  111. series = patchstream.FixPatches(series, args)
  112. if series and cover_fname and series.get('cover'):
  113. patchstream.InsertCoverLetter(cover_fname, series, options.count)
  114. # Do a few checks on the series
  115. series.DoChecks()
  116. # Check the patches, and run them through 'git am' just to be sure
  117. ok = checkpatch.CheckPatches(options.verbose, args)
  118. if not gitutil.ApplyPatches(options.verbose, args,
  119. options.count + options.start):
  120. ok = False
  121. cc_file = series.MakeCcFile(options.process_tags, cover_fname)
  122. # Email the patches out (giving the user time to check / cancel)
  123. cmd = ''
  124. if ok or options.ignore_errors:
  125. cmd = gitutil.EmailPatches(series, cover_fname, args,
  126. options.dry_run, cc_file)
  127. # For a dry run, just show our actions as a sanity check
  128. if options.dry_run:
  129. series.ShowActions(args, cmd, options.process_tags)
  130. os.remove(cc_file)