Simon Glass | 7d5b04e | 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 |
| 12 | import sys |
| 13 | |
| 14 | from patman import checkpatch |
| 15 | from patman import gitutil |
| 16 | from patman import patchstream |
Simon Glass | 4583c00 | 2023-02-23 18:18:04 -0700 | [diff] [blame] | 17 | from u_boot_pylib import terminal |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 18 | |
Maxim Cournoyer | a13af89 | 2023-10-12 23:06:24 -0400 | [diff] [blame] | 19 | |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 20 | def setup(): |
| 21 | """Do required setup before doing anything""" |
Simon Glass | 0157b18 | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 22 | gitutil.setup() |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 23 | |
Maxim Cournoyer | a13af89 | 2023-10-12 23:06:24 -0400 | [diff] [blame] | 24 | |
| 25 | def prepare_patches(col, branch, count, start, end, ignore_binary, signoff, |
| 26 | keep_change_id=False): |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 27 | """Figure out what patches to generate, then generate them |
| 28 | |
| 29 | The patch files are written to the current directory, e.g. 0001_xxx.patch |
| 30 | 0002_yyy.patch |
| 31 | |
| 32 | Args: |
| 33 | col (terminal.Color): Colour output object |
Simon Glass | 262130f | 2020-07-05 21:41:51 -0600 | [diff] [blame] | 34 | branch (str): Branch to create patches from (None = current) |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 35 | count (int): Number of patches to produce, or -1 to produce patches for |
| 36 | the current branch back to the upstream commit |
| 37 | start (int): Start partch to use (0=first / top of branch) |
Simon Glass | 137947e | 2020-07-05 21:41:52 -0600 | [diff] [blame] | 38 | end (int): End patch to use (0=last one in series, 1=one before that, |
| 39 | etc.) |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 40 | ignore_binary (bool): Don't generate patches for binary files |
Maxim Cournoyer | a13af89 | 2023-10-12 23:06:24 -0400 | [diff] [blame] | 41 | keep_change_id (bool): Preserve the Change-Id tag. |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 42 | |
| 43 | Returns: |
| 44 | Tuple: |
| 45 | Series object for this series (set of patches) |
| 46 | Filename of the cover letter as a string (None if none) |
| 47 | patch_files: List of patch filenames, each a string, e.g. |
| 48 | ['0001_xxx.patch', '0002_yyy.patch'] |
| 49 | """ |
| 50 | if count == -1: |
| 51 | # Work out how many patches to send if we can |
Simon Glass | 0157b18 | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 52 | count = (gitutil.count_commits_to_branch(branch) - start) |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 53 | |
| 54 | if not count: |
Nicolas Boichat | e1db5c9 | 2020-07-13 10:50:01 +0800 | [diff] [blame] | 55 | str = 'No commits found to process - please use -c flag, or run:\n' \ |
| 56 | ' git branch --set-upstream-to remote/branch' |
Simon Glass | 252ac58 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 57 | sys.exit(col.build(col.RED, str)) |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 58 | |
| 59 | # Read the metadata from the commits |
Simon Glass | 137947e | 2020-07-05 21:41:52 -0600 | [diff] [blame] | 60 | to_do = count - end |
Simon Glass | d93720e | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 61 | series = patchstream.get_metadata(branch, start, to_do) |
Simon Glass | 0157b18 | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 62 | cover_fname, patch_files = gitutil.create_patches( |
Philipp Tomsich | b3aff15 | 2020-11-24 18:14:52 +0100 | [diff] [blame] | 63 | branch, start, to_do, ignore_binary, series, signoff) |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 64 | |
| 65 | # Fix up the patch files to our liking, and insert the cover letter |
Maxim Cournoyer | a13af89 | 2023-10-12 23:06:24 -0400 | [diff] [blame] | 66 | patchstream.fix_patches(series, patch_files, keep_change_id) |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 67 | if cover_fname and series.get('cover'): |
Simon Glass | d93720e | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 68 | patchstream.insert_cover_letter(cover_fname, series, to_do) |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 69 | return series, cover_fname, patch_files |
| 70 | |
Maxim Cournoyer | a13af89 | 2023-10-12 23:06:24 -0400 | [diff] [blame] | 71 | |
Douglas Anderson | dce4322 | 2022-07-19 14:56:27 -0700 | [diff] [blame] | 72 | def check_patches(series, patch_files, run_checkpatch, verbose, use_tree): |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 73 | """Run some checks on a set of patches |
| 74 | |
| 75 | This santiy-checks the patman tags like Series-version and runs the patches |
| 76 | through checkpatch |
| 77 | |
| 78 | Args: |
| 79 | series (Series): Series object for this series (set of patches) |
| 80 | patch_files (list): List of patch filenames, each a string, e.g. |
| 81 | ['0001_xxx.patch', '0002_yyy.patch'] |
| 82 | run_checkpatch (bool): True to run checkpatch.pl |
| 83 | verbose (bool): True to print out every line of the checkpatch output as |
| 84 | it is parsed |
Douglas Anderson | dce4322 | 2022-07-19 14:56:27 -0700 | [diff] [blame] | 85 | use_tree (bool): If False we'll pass '--no-tree' to checkpatch. |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 86 | |
| 87 | Returns: |
| 88 | bool: True if the patches had no errors, False if they did |
| 89 | """ |
| 90 | # Do a few checks on the series |
| 91 | series.DoChecks() |
| 92 | |
Simon Glass | 98bc0b4 | 2023-03-08 10:52:52 -0800 | [diff] [blame] | 93 | # Check the patches |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 94 | if run_checkpatch: |
Douglas Anderson | dce4322 | 2022-07-19 14:56:27 -0700 | [diff] [blame] | 95 | ok = checkpatch.check_patches(verbose, patch_files, use_tree) |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 96 | else: |
| 97 | ok = True |
| 98 | return ok |
| 99 | |
| 100 | |
| 101 | def email_patches(col, series, cover_fname, patch_files, process_tags, its_a_go, |
Maxim Cournoyer | 8c042fb | 2022-12-20 00:28:46 -0500 | [diff] [blame] | 102 | ignore_bad_tags, add_maintainers, get_maintainer_script, limit, |
| 103 | dry_run, in_reply_to, thread, smtp_server): |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 104 | """Email patches to the recipients |
| 105 | |
| 106 | This emails out the patches and cover letter using 'git send-email'. Each |
| 107 | patch is copied to recipients identified by the patch tag and output from |
| 108 | the get_maintainer.pl script. The cover letter is copied to all recipients |
| 109 | of any patch. |
| 110 | |
| 111 | To make this work a CC file is created holding the recipients for each patch |
| 112 | and the cover letter. See the main program 'cc_cmd' for this logic. |
| 113 | |
| 114 | Args: |
| 115 | col (terminal.Color): Colour output object |
| 116 | series (Series): Series object for this series (set of patches) |
| 117 | cover_fname (str): Filename of the cover letter as a string (None if |
| 118 | none) |
| 119 | patch_files (list): List of patch filenames, each a string, e.g. |
| 120 | ['0001_xxx.patch', '0002_yyy.patch'] |
| 121 | process_tags (bool): True to process subject tags in each patch, e.g. |
| 122 | for 'dm: spi: Add SPI support' this would be 'dm' and 'spi'. The |
| 123 | tags are looked up in the configured sendemail.aliasesfile and also |
| 124 | in ~/.patman (see README) |
| 125 | its_a_go (bool): True if we are going to actually send the patches, |
| 126 | False if the patches have errors and will not be sent unless |
| 127 | @ignore_errors |
| 128 | ignore_bad_tags (bool): True to just print a warning for unknown tags, |
| 129 | False to halt with an error |
| 130 | add_maintainers (bool): Run the get_maintainer.pl script for each patch |
Maxim Cournoyer | 8c042fb | 2022-12-20 00:28:46 -0500 | [diff] [blame] | 131 | get_maintainer_script (str): The script used to retrieve which |
| 132 | maintainers to cc |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 133 | limit (int): Limit on the number of people that can be cc'd on a single |
| 134 | patch or the cover letter (None if no limit) |
| 135 | dry_run (bool): Don't actually email the patches, just print out what |
| 136 | would be sent |
| 137 | in_reply_to (str): If not None we'll pass this to git as --in-reply-to. |
| 138 | Should be a message ID that this is in reply to. |
| 139 | thread (bool): True to add --thread to git send-email (make all patches |
| 140 | reply to cover-letter or first patch in series) |
| 141 | smtp_server (str): SMTP server to use to send patches (None for default) |
| 142 | """ |
| 143 | cc_file = series.MakeCcFile(process_tags, cover_fname, not ignore_bad_tags, |
Maxim Cournoyer | 8c042fb | 2022-12-20 00:28:46 -0500 | [diff] [blame] | 144 | add_maintainers, limit, get_maintainer_script) |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 145 | |
| 146 | # Email the patches out (giving the user time to check / cancel) |
| 147 | cmd = '' |
| 148 | if its_a_go: |
Simon Glass | 0157b18 | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 149 | cmd = gitutil.email_patches( |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 150 | series, cover_fname, patch_files, dry_run, not ignore_bad_tags, |
| 151 | cc_file, in_reply_to=in_reply_to, thread=thread, |
| 152 | smtp_server=smtp_server) |
| 153 | else: |
Simon Glass | 252ac58 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 154 | print(col.build(col.RED, "Not sending emails due to errors/warnings")) |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 155 | |
| 156 | # For a dry run, just show our actions as a sanity check |
| 157 | if dry_run: |
| 158 | series.ShowActions(patch_files, cmd, process_tags) |
| 159 | if not its_a_go: |
Simon Glass | 252ac58 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 160 | print(col.build(col.RED, "Email would not be sent")) |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 161 | |
| 162 | os.remove(cc_file) |
| 163 | |
Simon Glass | fda1e37 | 2020-07-05 21:41:53 -0600 | [diff] [blame] | 164 | def send(args): |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 165 | """Create, check and send patches by email |
| 166 | |
| 167 | Args: |
Simon Glass | fda1e37 | 2020-07-05 21:41:53 -0600 | [diff] [blame] | 168 | args (argparse.Namespace): Arguments to patman |
Simon Glass | 7d5b04e | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 169 | """ |
| 170 | setup() |
| 171 | col = terminal.Color() |
| 172 | series, cover_fname, patch_files = prepare_patches( |
Simon Glass | fda1e37 | 2020-07-05 21:41:53 -0600 | [diff] [blame] | 173 | col, args.branch, args.count, args.start, args.end, |
Maxim Cournoyer | a13af89 | 2023-10-12 23:06:24 -0400 | [diff] [blame] | 174 | args.ignore_binary, args.add_signoff, |
| 175 | keep_change_id=args.keep_change_id) |
Simon Glass | fda1e37 | 2020-07-05 21:41:53 -0600 | [diff] [blame] | 176 | ok = check_patches(series, patch_files, args.check_patch, |
Douglas Anderson | dce4322 | 2022-07-19 14:56:27 -0700 | [diff] [blame] | 177 | args.verbose, args.check_patch_use_tree) |
Simon Glass | 262130f | 2020-07-05 21:41:51 -0600 | [diff] [blame] | 178 | |
Simon Glass | 0157b18 | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 179 | ok = ok and gitutil.check_suppress_cc_config() |
Nicolas Boichat | 9497756 | 2020-07-13 10:50:00 +0800 | [diff] [blame] | 180 | |
Simon Glass | fda1e37 | 2020-07-05 21:41:53 -0600 | [diff] [blame] | 181 | its_a_go = ok or args.ignore_errors |
Simon Glass | 4a9e578 | 2020-10-29 21:46:10 -0600 | [diff] [blame] | 182 | email_patches( |
| 183 | col, series, cover_fname, patch_files, args.process_tags, |
| 184 | its_a_go, args.ignore_bad_tags, args.add_maintainers, |
Maxim Cournoyer | 8c042fb | 2022-12-20 00:28:46 -0500 | [diff] [blame] | 185 | args.get_maintainer_script, args.limit, args.dry_run, |
| 186 | args.in_reply_to, args.thread, args.smtp_server) |
Simon Glass | dc6df97 | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 187 | |
Simon Glass | dc4b2a9 | 2020-10-29 21:46:38 -0600 | [diff] [blame] | 188 | def patchwork_status(branch, count, start, end, dest_branch, force, |
Simon Glass | 7cbf02e | 2020-11-03 13:54:14 -0700 | [diff] [blame] | 189 | show_comments, url): |
Simon Glass | dc6df97 | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 190 | """Check the status of patches in patchwork |
| 191 | |
| 192 | This finds the series in patchwork using the Series-link tag, checks for new |
Simon Glass | dc4b2a9 | 2020-10-29 21:46:38 -0600 | [diff] [blame] | 193 | comments and review tags, displays then and creates a new branch with the |
| 194 | review tags. |
Simon Glass | dc6df97 | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 195 | |
| 196 | Args: |
| 197 | branch (str): Branch to create patches from (None = current) |
| 198 | count (int): Number of patches to produce, or -1 to produce patches for |
| 199 | the current branch back to the upstream commit |
| 200 | start (int): Start partch to use (0=first / top of branch) |
| 201 | end (int): End patch to use (0=last one in series, 1=one before that, |
| 202 | etc.) |
Simon Glass | 8f9ba3a | 2020-10-29 21:46:36 -0600 | [diff] [blame] | 203 | dest_branch (str): Name of new branch to create with the updated tags |
| 204 | (None to not create a branch) |
| 205 | force (bool): With dest_branch, force overwriting an existing branch |
Simon Glass | dc4b2a9 | 2020-10-29 21:46:38 -0600 | [diff] [blame] | 206 | show_comments (bool): True to display snippets from the comments |
| 207 | provided by reviewers |
Simon Glass | fcbec65 | 2020-11-03 13:54:16 -0700 | [diff] [blame] | 208 | url (str): URL of patchwork server, e.g. 'https://patchwork.ozlabs.org'. |
| 209 | This is ignored if the series provides a Series-patchwork-url tag. |
Simon Glass | dc6df97 | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 210 | |
| 211 | Raises: |
| 212 | ValueError: if the branch has no Series-link value |
| 213 | """ |
| 214 | if count == -1: |
| 215 | # Work out how many patches to send if we can |
Simon Glass | 0157b18 | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 216 | count = (gitutil.count_commits_to_branch(branch) - start) |
Simon Glass | dc6df97 | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 217 | |
| 218 | series = patchstream.get_metadata(branch, start, count - end) |
| 219 | warnings = 0 |
| 220 | for cmt in series.commits: |
| 221 | if cmt.warn: |
| 222 | print('%d warnings for %s:' % (len(cmt.warn), cmt.hash)) |
| 223 | for warn in cmt.warn: |
| 224 | print('\t', warn) |
| 225 | warnings += 1 |
| 226 | print |
| 227 | if warnings: |
| 228 | raise ValueError('Please fix warnings before running status') |
| 229 | links = series.get('links') |
| 230 | if not links: |
| 231 | raise ValueError("Branch has no Series-links value") |
| 232 | |
| 233 | # Find the link without a version number (we don't support versions yet) |
| 234 | found = [link for link in links.split() if not ':' in link] |
| 235 | if not found: |
| 236 | raise ValueError('Series-links has no current version (without :)') |
| 237 | |
Simon Glass | fcbec65 | 2020-11-03 13:54:16 -0700 | [diff] [blame] | 238 | # Allow the series to override the URL |
| 239 | if 'patchwork_url' in series: |
| 240 | url = series.patchwork_url |
| 241 | |
Simon Glass | dc6df97 | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 242 | # Import this here to avoid failing on other commands if the dependencies |
| 243 | # are not present |
| 244 | from patman import status |
Simon Glass | dc4b2a9 | 2020-10-29 21:46:38 -0600 | [diff] [blame] | 245 | status.check_patchwork_status(series, found[0], branch, dest_branch, force, |
Simon Glass | 7cbf02e | 2020-11-03 13:54:14 -0700 | [diff] [blame] | 246 | show_comments, url) |