control.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # SPDX-License-Identifier: GPL-2.0+
  2. #
  3. # Copyright 2020 Google LLC
  4. #
  5. """Handles the main control logic of patman
  6. This module provides various functions called by the main program to implement
  7. the features of patman.
  8. """
  9. import os
  10. import sys
  11. from patman import checkpatch
  12. from patman import gitutil
  13. from patman import patchstream
  14. from patman import terminal
  15. def setup():
  16. """Do required setup before doing anything"""
  17. gitutil.Setup()
  18. def prepare_patches(col, branch, count, start, end, ignore_binary, signoff):
  19. """Figure out what patches to generate, then generate them
  20. The patch files are written to the current directory, e.g. 0001_xxx.patch
  21. 0002_yyy.patch
  22. Args:
  23. col (terminal.Color): Colour output object
  24. branch (str): Branch to create patches from (None = current)
  25. count (int): Number of patches to produce, or -1 to produce patches for
  26. the current branch back to the upstream commit
  27. start (int): Start partch to use (0=first / top of branch)
  28. end (int): End patch to use (0=last one in series, 1=one before that,
  29. etc.)
  30. ignore_binary (bool): Don't generate patches for binary files
  31. Returns:
  32. Tuple:
  33. Series object for this series (set of patches)
  34. Filename of the cover letter as a string (None if none)
  35. patch_files: List of patch filenames, each a string, e.g.
  36. ['0001_xxx.patch', '0002_yyy.patch']
  37. """
  38. if count == -1:
  39. # Work out how many patches to send if we can
  40. count = (gitutil.CountCommitsToBranch(branch) - start)
  41. if not count:
  42. str = 'No commits found to process - please use -c flag, or run:\n' \
  43. ' git branch --set-upstream-to remote/branch'
  44. sys.exit(col.Color(col.RED, str))
  45. # Read the metadata from the commits
  46. to_do = count - end
  47. series = patchstream.get_metadata(branch, start, to_do)
  48. cover_fname, patch_files = gitutil.CreatePatches(
  49. branch, start, to_do, ignore_binary, series, signoff)
  50. # Fix up the patch files to our liking, and insert the cover letter
  51. patchstream.fix_patches(series, patch_files)
  52. if cover_fname and series.get('cover'):
  53. patchstream.insert_cover_letter(cover_fname, series, to_do)
  54. return series, cover_fname, patch_files
  55. def check_patches(series, patch_files, run_checkpatch, verbose):
  56. """Run some checks on a set of patches
  57. This santiy-checks the patman tags like Series-version and runs the patches
  58. through checkpatch
  59. Args:
  60. series (Series): Series object for this series (set of patches)
  61. patch_files (list): List of patch filenames, each a string, e.g.
  62. ['0001_xxx.patch', '0002_yyy.patch']
  63. run_checkpatch (bool): True to run checkpatch.pl
  64. verbose (bool): True to print out every line of the checkpatch output as
  65. it is parsed
  66. Returns:
  67. bool: True if the patches had no errors, False if they did
  68. """
  69. # Do a few checks on the series
  70. series.DoChecks()
  71. # Check the patches, and run them through 'git am' just to be sure
  72. if run_checkpatch:
  73. ok = checkpatch.CheckPatches(verbose, patch_files)
  74. else:
  75. ok = True
  76. return ok
  77. def email_patches(col, series, cover_fname, patch_files, process_tags, its_a_go,
  78. ignore_bad_tags, add_maintainers, limit, dry_run, in_reply_to,
  79. thread, smtp_server):
  80. """Email patches to the recipients
  81. This emails out the patches and cover letter using 'git send-email'. Each
  82. patch is copied to recipients identified by the patch tag and output from
  83. the get_maintainer.pl script. The cover letter is copied to all recipients
  84. of any patch.
  85. To make this work a CC file is created holding the recipients for each patch
  86. and the cover letter. See the main program 'cc_cmd' for this logic.
  87. Args:
  88. col (terminal.Color): Colour output object
  89. series (Series): Series object for this series (set of patches)
  90. cover_fname (str): Filename of the cover letter as a string (None if
  91. none)
  92. patch_files (list): List of patch filenames, each a string, e.g.
  93. ['0001_xxx.patch', '0002_yyy.patch']
  94. process_tags (bool): True to process subject tags in each patch, e.g.
  95. for 'dm: spi: Add SPI support' this would be 'dm' and 'spi'. The
  96. tags are looked up in the configured sendemail.aliasesfile and also
  97. in ~/.patman (see README)
  98. its_a_go (bool): True if we are going to actually send the patches,
  99. False if the patches have errors and will not be sent unless
  100. @ignore_errors
  101. ignore_bad_tags (bool): True to just print a warning for unknown tags,
  102. False to halt with an error
  103. add_maintainers (bool): Run the get_maintainer.pl script for each patch
  104. limit (int): Limit on the number of people that can be cc'd on a single
  105. patch or the cover letter (None if no limit)
  106. dry_run (bool): Don't actually email the patches, just print out what
  107. would be sent
  108. in_reply_to (str): If not None we'll pass this to git as --in-reply-to.
  109. Should be a message ID that this is in reply to.
  110. thread (bool): True to add --thread to git send-email (make all patches
  111. reply to cover-letter or first patch in series)
  112. smtp_server (str): SMTP server to use to send patches (None for default)
  113. """
  114. cc_file = series.MakeCcFile(process_tags, cover_fname, not ignore_bad_tags,
  115. add_maintainers, limit)
  116. # Email the patches out (giving the user time to check / cancel)
  117. cmd = ''
  118. if its_a_go:
  119. cmd = gitutil.EmailPatches(
  120. series, cover_fname, patch_files, dry_run, not ignore_bad_tags,
  121. cc_file, in_reply_to=in_reply_to, thread=thread,
  122. smtp_server=smtp_server)
  123. else:
  124. print(col.Color(col.RED, "Not sending emails due to errors/warnings"))
  125. # For a dry run, just show our actions as a sanity check
  126. if dry_run:
  127. series.ShowActions(patch_files, cmd, process_tags)
  128. if not its_a_go:
  129. print(col.Color(col.RED, "Email would not be sent"))
  130. os.remove(cc_file)
  131. def send(args):
  132. """Create, check and send patches by email
  133. Args:
  134. args (argparse.Namespace): Arguments to patman
  135. """
  136. setup()
  137. col = terminal.Color()
  138. series, cover_fname, patch_files = prepare_patches(
  139. col, args.branch, args.count, args.start, args.end,
  140. args.ignore_binary, args.add_signoff)
  141. ok = check_patches(series, patch_files, args.check_patch,
  142. args.verbose)
  143. ok = ok and gitutil.CheckSuppressCCConfig()
  144. its_a_go = ok or args.ignore_errors
  145. email_patches(
  146. col, series, cover_fname, patch_files, args.process_tags,
  147. its_a_go, args.ignore_bad_tags, args.add_maintainers,
  148. args.limit, args.dry_run, args.in_reply_to, args.thread,
  149. args.smtp_server)
  150. def patchwork_status(branch, count, start, end, dest_branch, force,
  151. show_comments, url):
  152. """Check the status of patches in patchwork
  153. This finds the series in patchwork using the Series-link tag, checks for new
  154. comments and review tags, displays then and creates a new branch with the
  155. review tags.
  156. Args:
  157. branch (str): Branch to create patches from (None = current)
  158. count (int): Number of patches to produce, or -1 to produce patches for
  159. the current branch back to the upstream commit
  160. start (int): Start partch to use (0=first / top of branch)
  161. end (int): End patch to use (0=last one in series, 1=one before that,
  162. etc.)
  163. dest_branch (str): Name of new branch to create with the updated tags
  164. (None to not create a branch)
  165. force (bool): With dest_branch, force overwriting an existing branch
  166. show_comments (bool): True to display snippets from the comments
  167. provided by reviewers
  168. url (str): URL of patchwork server, e.g. 'https://patchwork.ozlabs.org'.
  169. This is ignored if the series provides a Series-patchwork-url tag.
  170. Raises:
  171. ValueError: if the branch has no Series-link value
  172. """
  173. if count == -1:
  174. # Work out how many patches to send if we can
  175. count = (gitutil.CountCommitsToBranch(branch) - start)
  176. series = patchstream.get_metadata(branch, start, count - end)
  177. warnings = 0
  178. for cmt in series.commits:
  179. if cmt.warn:
  180. print('%d warnings for %s:' % (len(cmt.warn), cmt.hash))
  181. for warn in cmt.warn:
  182. print('\t', warn)
  183. warnings += 1
  184. print
  185. if warnings:
  186. raise ValueError('Please fix warnings before running status')
  187. links = series.get('links')
  188. if not links:
  189. raise ValueError("Branch has no Series-links value")
  190. # Find the link without a version number (we don't support versions yet)
  191. found = [link for link in links.split() if not ':' in link]
  192. if not found:
  193. raise ValueError('Series-links has no current version (without :)')
  194. # Allow the series to override the URL
  195. if 'patchwork_url' in series:
  196. url = series.patchwork_url
  197. # Import this here to avoid failing on other commands if the dependencies
  198. # are not present
  199. from patman import status
  200. status.check_patchwork_status(series, found[0], branch, dest_branch, force,
  201. show_comments, url)