Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
| 2 | # |
| 3 | # Copyright 2020 Google LLC |
| 4 | # |
| 5 | """Handles the main control logic of patman |
| 6 | |
| 7 | This module provides various functions called by the main program to implement |
| 8 | the features of patman. |
| 9 | """ |
| 10 | |
| 11 | import os |
Simon Glass | 3c0196f | 2025-04-29 07:21:58 -0600 | [diff] [blame] | 12 | import re |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 13 | import sys |
Simon Glass | 3c0196f | 2025-04-29 07:21:58 -0600 | [diff] [blame] | 14 | import traceback |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 15 | |
Simon Glass | 3c0196f | 2025-04-29 07:21:58 -0600 | [diff] [blame] | 16 | try: |
| 17 | from importlib import resources |
| 18 | except ImportError: |
| 19 | # for Python 3.6 |
| 20 | import importlib_resources as resources |
| 21 | |
Simon Glass | ba1b3b9 | 2025-02-09 14:26:00 -0700 | [diff] [blame] | 22 | from u_boot_pylib import gitutil |
Simon Glass | 131444f | 2023-02-23 18:18:04 -0700 | [diff] [blame] | 23 | from u_boot_pylib import terminal |
Simon Glass | 3c0196f | 2025-04-29 07:21:58 -0600 | [diff] [blame] | 24 | from u_boot_pylib import tools |
| 25 | from patman import checkpatch |
| 26 | from patman import patchstream |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 27 | |
Maxim Cournoyer | 12f99fd | 2023-10-12 23:06:24 -0400 | [diff] [blame] | 28 | |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 29 | def setup(): |
| 30 | """Do required setup before doing anything""" |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 31 | gitutil.setup() |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 32 | |
Maxim Cournoyer | 12f99fd | 2023-10-12 23:06:24 -0400 | [diff] [blame] | 33 | |
| 34 | def prepare_patches(col, branch, count, start, end, ignore_binary, signoff, |
| 35 | keep_change_id=False): |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 36 | """Figure out what patches to generate, then generate them |
| 37 | |
| 38 | The patch files are written to the current directory, e.g. 0001_xxx.patch |
| 39 | 0002_yyy.patch |
| 40 | |
| 41 | Args: |
| 42 | col (terminal.Color): Colour output object |
Simon Glass | 2eb4da7 | 2020-07-05 21:41:51 -0600 | [diff] [blame] | 43 | branch (str): Branch to create patches from (None = current) |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 44 | count (int): Number of patches to produce, or -1 to produce patches for |
| 45 | the current branch back to the upstream commit |
| 46 | start (int): Start partch to use (0=first / top of branch) |
Simon Glass | b3bf4e1 | 2020-07-05 21:41:52 -0600 | [diff] [blame] | 47 | end (int): End patch to use (0=last one in series, 1=one before that, |
| 48 | etc.) |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 49 | ignore_binary (bool): Don't generate patches for binary files |
Maxim Cournoyer | 12f99fd | 2023-10-12 23:06:24 -0400 | [diff] [blame] | 50 | keep_change_id (bool): Preserve the Change-Id tag. |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 51 | |
| 52 | Returns: |
| 53 | Tuple: |
| 54 | Series object for this series (set of patches) |
| 55 | Filename of the cover letter as a string (None if none) |
| 56 | patch_files: List of patch filenames, each a string, e.g. |
| 57 | ['0001_xxx.patch', '0002_yyy.patch'] |
| 58 | """ |
| 59 | if count == -1: |
| 60 | # Work out how many patches to send if we can |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 61 | count = (gitutil.count_commits_to_branch(branch) - start) |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 62 | |
| 63 | if not count: |
Nicolas Boichat | 2173fb4 | 2020-07-13 10:50:01 +0800 | [diff] [blame] | 64 | str = 'No commits found to process - please use -c flag, or run:\n' \ |
| 65 | ' git branch --set-upstream-to remote/branch' |
Simon Glass | f45d374 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 66 | sys.exit(col.build(col.RED, str)) |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 67 | |
| 68 | # Read the metadata from the commits |
Simon Glass | b3bf4e1 | 2020-07-05 21:41:52 -0600 | [diff] [blame] | 69 | to_do = count - end |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 70 | series = patchstream.get_metadata(branch, start, to_do) |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 71 | cover_fname, patch_files = gitutil.create_patches( |
Philipp Tomsich | 858531a | 2020-11-24 18:14:52 +0100 | [diff] [blame] | 72 | branch, start, to_do, ignore_binary, series, signoff) |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 73 | |
| 74 | # Fix up the patch files to our liking, and insert the cover letter |
Simon Glass | da1a6ec | 2025-03-28 07:02:20 -0600 | [diff] [blame] | 75 | patchstream.fix_patches(series, patch_files, keep_change_id, |
| 76 | insert_base_commit=not cover_fname) |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 77 | if cover_fname and series.get('cover'): |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 78 | patchstream.insert_cover_letter(cover_fname, series, to_do) |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 79 | return series, cover_fname, patch_files |
| 80 | |
Maxim Cournoyer | 12f99fd | 2023-10-12 23:06:24 -0400 | [diff] [blame] | 81 | |
Douglas Anderson | be4f271 | 2022-07-19 14:56:27 -0700 | [diff] [blame] | 82 | def check_patches(series, patch_files, run_checkpatch, verbose, use_tree): |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 83 | """Run some checks on a set of patches |
| 84 | |
| 85 | This santiy-checks the patman tags like Series-version and runs the patches |
| 86 | through checkpatch |
| 87 | |
| 88 | Args: |
| 89 | series (Series): Series object for this series (set of patches) |
| 90 | patch_files (list): List of patch filenames, each a string, e.g. |
| 91 | ['0001_xxx.patch', '0002_yyy.patch'] |
| 92 | run_checkpatch (bool): True to run checkpatch.pl |
| 93 | verbose (bool): True to print out every line of the checkpatch output as |
| 94 | it is parsed |
Douglas Anderson | be4f271 | 2022-07-19 14:56:27 -0700 | [diff] [blame] | 95 | use_tree (bool): If False we'll pass '--no-tree' to checkpatch. |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 96 | |
| 97 | Returns: |
| 98 | bool: True if the patches had no errors, False if they did |
| 99 | """ |
| 100 | # Do a few checks on the series |
| 101 | series.DoChecks() |
| 102 | |
Simon Glass | a59449c | 2023-03-08 10:52:52 -0800 | [diff] [blame] | 103 | # Check the patches |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 104 | if run_checkpatch: |
Douglas Anderson | be4f271 | 2022-07-19 14:56:27 -0700 | [diff] [blame] | 105 | ok = checkpatch.check_patches(verbose, patch_files, use_tree) |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 106 | else: |
| 107 | ok = True |
| 108 | return ok |
| 109 | |
| 110 | |
| 111 | def email_patches(col, series, cover_fname, patch_files, process_tags, its_a_go, |
Maxim Cournoyer | 3ef23e9 | 2022-12-20 00:28:46 -0500 | [diff] [blame] | 112 | ignore_bad_tags, add_maintainers, get_maintainer_script, limit, |
| 113 | dry_run, in_reply_to, thread, smtp_server): |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 114 | """Email patches to the recipients |
| 115 | |
| 116 | This emails out the patches and cover letter using 'git send-email'. Each |
| 117 | patch is copied to recipients identified by the patch tag and output from |
| 118 | the get_maintainer.pl script. The cover letter is copied to all recipients |
| 119 | of any patch. |
| 120 | |
| 121 | To make this work a CC file is created holding the recipients for each patch |
| 122 | and the cover letter. See the main program 'cc_cmd' for this logic. |
| 123 | |
| 124 | Args: |
| 125 | col (terminal.Color): Colour output object |
| 126 | series (Series): Series object for this series (set of patches) |
| 127 | cover_fname (str): Filename of the cover letter as a string (None if |
| 128 | none) |
| 129 | patch_files (list): List of patch filenames, each a string, e.g. |
| 130 | ['0001_xxx.patch', '0002_yyy.patch'] |
| 131 | process_tags (bool): True to process subject tags in each patch, e.g. |
| 132 | for 'dm: spi: Add SPI support' this would be 'dm' and 'spi'. The |
| 133 | tags are looked up in the configured sendemail.aliasesfile and also |
| 134 | in ~/.patman (see README) |
| 135 | its_a_go (bool): True if we are going to actually send the patches, |
| 136 | False if the patches have errors and will not be sent unless |
| 137 | @ignore_errors |
| 138 | ignore_bad_tags (bool): True to just print a warning for unknown tags, |
| 139 | False to halt with an error |
| 140 | add_maintainers (bool): Run the get_maintainer.pl script for each patch |
Maxim Cournoyer | 3ef23e9 | 2022-12-20 00:28:46 -0500 | [diff] [blame] | 141 | get_maintainer_script (str): The script used to retrieve which |
| 142 | maintainers to cc |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 143 | limit (int): Limit on the number of people that can be cc'd on a single |
| 144 | patch or the cover letter (None if no limit) |
| 145 | dry_run (bool): Don't actually email the patches, just print out what |
| 146 | would be sent |
| 147 | in_reply_to (str): If not None we'll pass this to git as --in-reply-to. |
| 148 | Should be a message ID that this is in reply to. |
| 149 | thread (bool): True to add --thread to git send-email (make all patches |
| 150 | reply to cover-letter or first patch in series) |
| 151 | smtp_server (str): SMTP server to use to send patches (None for default) |
| 152 | """ |
| 153 | cc_file = series.MakeCcFile(process_tags, cover_fname, not ignore_bad_tags, |
Maxim Cournoyer | 3ef23e9 | 2022-12-20 00:28:46 -0500 | [diff] [blame] | 154 | add_maintainers, limit, get_maintainer_script) |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 155 | |
| 156 | # Email the patches out (giving the user time to check / cancel) |
| 157 | cmd = '' |
| 158 | if its_a_go: |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 159 | cmd = gitutil.email_patches( |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 160 | series, cover_fname, patch_files, dry_run, not ignore_bad_tags, |
| 161 | cc_file, in_reply_to=in_reply_to, thread=thread, |
| 162 | smtp_server=smtp_server) |
| 163 | else: |
Simon Glass | f45d374 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 164 | print(col.build(col.RED, "Not sending emails due to errors/warnings")) |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 165 | |
| 166 | # For a dry run, just show our actions as a sanity check |
| 167 | if dry_run: |
| 168 | series.ShowActions(patch_files, cmd, process_tags) |
| 169 | if not its_a_go: |
Simon Glass | f45d374 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 170 | print(col.build(col.RED, "Email would not be sent")) |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 171 | |
| 172 | os.remove(cc_file) |
| 173 | |
Simon Glass | eb101ac | 2020-07-05 21:41:53 -0600 | [diff] [blame] | 174 | def send(args): |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 175 | """Create, check and send patches by email |
| 176 | |
| 177 | Args: |
Simon Glass | eb101ac | 2020-07-05 21:41:53 -0600 | [diff] [blame] | 178 | args (argparse.Namespace): Arguments to patman |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 179 | """ |
| 180 | setup() |
| 181 | col = terminal.Color() |
| 182 | series, cover_fname, patch_files = prepare_patches( |
Simon Glass | eb101ac | 2020-07-05 21:41:53 -0600 | [diff] [blame] | 183 | col, args.branch, args.count, args.start, args.end, |
Maxim Cournoyer | 12f99fd | 2023-10-12 23:06:24 -0400 | [diff] [blame] | 184 | args.ignore_binary, args.add_signoff, |
| 185 | keep_change_id=args.keep_change_id) |
Simon Glass | eb101ac | 2020-07-05 21:41:53 -0600 | [diff] [blame] | 186 | ok = check_patches(series, patch_files, args.check_patch, |
Douglas Anderson | be4f271 | 2022-07-19 14:56:27 -0700 | [diff] [blame] | 187 | args.verbose, args.check_patch_use_tree) |
Simon Glass | 2eb4da7 | 2020-07-05 21:41:51 -0600 | [diff] [blame] | 188 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 189 | ok = ok and gitutil.check_suppress_cc_config() |
Nicolas Boichat | 0da9574 | 2020-07-13 10:50:00 +0800 | [diff] [blame] | 190 | |
Simon Glass | eb101ac | 2020-07-05 21:41:53 -0600 | [diff] [blame] | 191 | its_a_go = ok or args.ignore_errors |
Simon Glass | 4318b71 | 2020-10-29 21:46:10 -0600 | [diff] [blame] | 192 | email_patches( |
| 193 | col, series, cover_fname, patch_files, args.process_tags, |
| 194 | its_a_go, args.ignore_bad_tags, args.add_maintainers, |
Maxim Cournoyer | 3ef23e9 | 2022-12-20 00:28:46 -0500 | [diff] [blame] | 195 | args.get_maintainer_script, args.limit, args.dry_run, |
| 196 | args.in_reply_to, args.thread, args.smtp_server) |
Simon Glass | 3db916d | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 197 | |
Simon Glass | 2112d07 | 2020-10-29 21:46:38 -0600 | [diff] [blame] | 198 | def patchwork_status(branch, count, start, end, dest_branch, force, |
Simon Glass | f9b03cf | 2020-11-03 13:54:14 -0700 | [diff] [blame] | 199 | show_comments, url): |
Simon Glass | 3db916d | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 200 | """Check the status of patches in patchwork |
| 201 | |
| 202 | This finds the series in patchwork using the Series-link tag, checks for new |
Simon Glass | 2112d07 | 2020-10-29 21:46:38 -0600 | [diff] [blame] | 203 | comments and review tags, displays then and creates a new branch with the |
| 204 | review tags. |
Simon Glass | 3db916d | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 205 | |
| 206 | Args: |
| 207 | branch (str): Branch to create patches from (None = current) |
| 208 | count (int): Number of patches to produce, or -1 to produce patches for |
| 209 | the current branch back to the upstream commit |
| 210 | start (int): Start partch to use (0=first / top of branch) |
| 211 | end (int): End patch to use (0=last one in series, 1=one before that, |
| 212 | etc.) |
Simon Glass | d0a0a58 | 2020-10-29 21:46:36 -0600 | [diff] [blame] | 213 | dest_branch (str): Name of new branch to create with the updated tags |
| 214 | (None to not create a branch) |
| 215 | force (bool): With dest_branch, force overwriting an existing branch |
Simon Glass | 2112d07 | 2020-10-29 21:46:38 -0600 | [diff] [blame] | 216 | show_comments (bool): True to display snippets from the comments |
| 217 | provided by reviewers |
Simon Glass | 4acc93c | 2020-11-03 13:54:16 -0700 | [diff] [blame] | 218 | url (str): URL of patchwork server, e.g. 'https://patchwork.ozlabs.org'. |
| 219 | This is ignored if the series provides a Series-patchwork-url tag. |
Simon Glass | 3db916d | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 220 | |
| 221 | Raises: |
| 222 | ValueError: if the branch has no Series-link value |
| 223 | """ |
| 224 | if count == -1: |
| 225 | # Work out how many patches to send if we can |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 226 | count = (gitutil.count_commits_to_branch(branch) - start) |
Simon Glass | 3db916d | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 227 | |
| 228 | series = patchstream.get_metadata(branch, start, count - end) |
| 229 | warnings = 0 |
| 230 | for cmt in series.commits: |
| 231 | if cmt.warn: |
| 232 | print('%d warnings for %s:' % (len(cmt.warn), cmt.hash)) |
| 233 | for warn in cmt.warn: |
| 234 | print('\t', warn) |
| 235 | warnings += 1 |
| 236 | print |
| 237 | if warnings: |
| 238 | raise ValueError('Please fix warnings before running status') |
| 239 | links = series.get('links') |
| 240 | if not links: |
| 241 | raise ValueError("Branch has no Series-links value") |
| 242 | |
| 243 | # Find the link without a version number (we don't support versions yet) |
| 244 | found = [link for link in links.split() if not ':' in link] |
| 245 | if not found: |
| 246 | raise ValueError('Series-links has no current version (without :)') |
| 247 | |
Simon Glass | 4acc93c | 2020-11-03 13:54:16 -0700 | [diff] [blame] | 248 | # Allow the series to override the URL |
| 249 | if 'patchwork_url' in series: |
| 250 | url = series.patchwork_url |
| 251 | |
Simon Glass | 3db916d | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 252 | # Import this here to avoid failing on other commands if the dependencies |
| 253 | # are not present |
| 254 | from patman import status |
Simon Glass | 2112d07 | 2020-10-29 21:46:38 -0600 | [diff] [blame] | 255 | status.check_patchwork_status(series, found[0], branch, dest_branch, force, |
Simon Glass | f9b03cf | 2020-11-03 13:54:14 -0700 | [diff] [blame] | 256 | show_comments, url) |
Simon Glass | 3c0196f | 2025-04-29 07:21:58 -0600 | [diff] [blame] | 257 | |
| 258 | |
| 259 | def do_patman(args): |
| 260 | if args.cmd == 'send': |
| 261 | # Called from git with a patch filename as argument |
| 262 | # Printout a list of additional CC recipients for this patch |
| 263 | if args.cc_cmd: |
| 264 | re_line = re.compile(r'(\S*) (.*)') |
| 265 | with open(args.cc_cmd, 'r', encoding='utf-8') as inf: |
| 266 | for line in inf.readlines(): |
| 267 | match = re_line.match(line) |
| 268 | if match and match.group(1) == args.patchfiles[0]: |
| 269 | for cca in match.group(2).split('\0'): |
| 270 | cca = cca.strip() |
| 271 | if cca: |
| 272 | print(cca) |
| 273 | |
| 274 | elif args.full_help: |
| 275 | with resources.path('patman', 'README.rst') as readme: |
| 276 | tools.print_full_help(str(readme)) |
| 277 | else: |
| 278 | # If we are not processing tags, no need to warning about bad ones |
| 279 | if not args.process_tags: |
| 280 | args.ignore_bad_tags = True |
| 281 | send(args) |
| 282 | |
Simon Glass | 78ee8f8 | 2025-04-29 07:22:09 -0600 | [diff] [blame^] | 283 | ret_code = 0 |
| 284 | try: |
| 285 | # Check status of patches in patchwork |
| 286 | if args.cmd == 'status': |
Simon Glass | 3c0196f | 2025-04-29 07:21:58 -0600 | [diff] [blame] | 287 | patchwork_status(args.branch, args.count, args.start, args.end, |
| 288 | args.dest_branch, args.force, args.show_comments, |
| 289 | args.patchwork_url) |
Simon Glass | 78ee8f8 | 2025-04-29 07:22:09 -0600 | [diff] [blame^] | 290 | except Exception as exc: |
| 291 | terminal.tprint(f'patman: {type(exc).__name__}: {exc}', |
| 292 | colour=terminal.Color.RED) |
| 293 | if args.debug: |
| 294 | print() |
| 295 | traceback.print_exc() |
| 296 | ret_code = 1 |
| 297 | return ret_code |