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