blob: a2db3cff07fea12837a51bc58245e04888905db2 [file] [log] [blame]
Simon Glassc0257982025-04-29 07:22:11 -06001# 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
Simon Glass5efa3662025-04-07 22:51:45 +120013from patman import settings
Simon Glassc0257982025-04-29 07:22:11 -060014from u_boot_pylib import gitutil
15from u_boot_pylib import terminal
16
17
Simon Glass5750cc22025-05-07 18:02:47 +020018def check_patches(series, patch_files, run_checkpatch, verbose, use_tree, cwd):
Simon Glassc0257982025-04-29 07:22:11 -060019 """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.
Simon Glass5750cc22025-05-07 18:02:47 +020032 cwd (str): Path to use for patch files (None to use current dir)
Simon Glassc0257982025-04-29 07:22:11 -060033
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:
Simon Glass5750cc22025-05-07 18:02:47 +020042 ok = checkpatch.check_patches(verbose, patch_files, use_tree, cwd)
Simon Glassc0257982025-04-29 07:22:11 -060043 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,
Simon Glass5750cc22025-05-07 18:02:47 +020050 dry_run, in_reply_to, thread, smtp_server, cwd=None):
Simon Glassc0257982025-04-29 07:22:11 -060051 """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)
Simon Glass5750cc22025-05-07 18:02:47 +020089 cwd (str): Path to use for patch files (None to use current dir)
Simon Glassc0257982025-04-29 07:22:11 -060090 """
91 cc_file = series.MakeCcFile(process_tags, cover_fname, not ignore_bad_tags,
Simon Glass9938b7b2025-04-07 22:51:46 +120092 add_maintainers, limit, get_maintainer_script,
Simon Glass5750cc22025-05-07 18:02:47 +020093 settings.alias, cwd)
Simon Glassc0257982025-04-29 07:22:11 -060094
95 # Email the patches out (giving the user time to check / cancel)
96 cmd = ''
97 if its_a_go:
98 cmd = gitutil.email_patches(
99 series, cover_fname, patch_files, dry_run, not ignore_bad_tags,
Simon Glass5750cc22025-05-07 18:02:47 +0200100 cc_file, alias=settings.alias, in_reply_to=in_reply_to,
101 thread=thread, smtp_server=smtp_server, cwd=cwd)
Simon Glassc0257982025-04-29 07:22:11 -0600102 else:
103 print(col.build(col.RED, "Not sending emails due to errors/warnings"))
104
105 # For a dry run, just show our actions as a sanity check
106 if dry_run:
Simon Glass32f12a7e2025-04-07 22:51:47 +1200107 series.ShowActions(patch_files, cmd, process_tags, settings.alias)
Simon Glassc0257982025-04-29 07:22:11 -0600108 if not its_a_go:
109 print(col.build(col.RED, "Email would not be sent"))
110
111 os.remove(cc_file)
112
113
114def prepare_patches(col, branch, count, start, end, ignore_binary, signoff,
Simon Glass5750cc22025-05-07 18:02:47 +0200115 keep_change_id=False, cwd=None):
Simon Glassc0257982025-04-29 07:22:11 -0600116 """Figure out what patches to generate, then generate them
117
118 The patch files are written to the current directory, e.g. 0001_xxx.patch
119 0002_yyy.patch
120
121 Args:
122 col (terminal.Color): Colour output object
123 branch (str): Branch to create patches from (None = current)
124 count (int): Number of patches to produce, or -1 to produce patches for
125 the current branch back to the upstream commit
126 start (int): Start partch to use (0=first / top of branch)
127 end (int): End patch to use (0=last one in series, 1=one before that,
128 etc.)
129 ignore_binary (bool): Don't generate patches for binary files
130 keep_change_id (bool): Preserve the Change-Id tag.
Simon Glass5750cc22025-05-07 18:02:47 +0200131 cwd (str): Path to use for git operations (None to use current dir)
Simon Glassc0257982025-04-29 07:22:11 -0600132
133 Returns:
134 Tuple:
135 Series object for this series (set of patches)
136 Filename of the cover letter as a string (None if none)
137 patch_files: List of patch filenames, each a string, e.g.
138 ['0001_xxx.patch', '0002_yyy.patch']
139 """
140 if count == -1:
141 # Work out how many patches to send if we can
142 count = (gitutil.count_commits_to_branch(branch) - start)
143
144 if not count:
145 str = 'No commits found to process - please use -c flag, or run:\n' \
146 ' git branch --set-upstream-to remote/branch'
147 sys.exit(col.build(col.RED, str))
148
149 # Read the metadata from the commits
150 to_do = count - end
151 series = patchstream.get_metadata(branch, start, to_do)
152 cover_fname, patch_files = gitutil.create_patches(
Simon Glass5750cc22025-05-07 18:02:47 +0200153 branch, start, to_do, ignore_binary, series, signoff,
154 cwd=cwd)
Simon Glassc0257982025-04-29 07:22:11 -0600155
156 # Fix up the patch files to our liking, and insert the cover letter
157 patchstream.fix_patches(series, patch_files, keep_change_id,
Simon Glass5750cc22025-05-07 18:02:47 +0200158 insert_base_commit=not cover_fname, cwd=cwd)
Simon Glassc0257982025-04-29 07:22:11 -0600159 if cover_fname and series.get('cover'):
Simon Glass5750cc22025-05-07 18:02:47 +0200160 patchstream.insert_cover_letter(cover_fname, series, to_do, cwd=cwd)
Simon Glassc0257982025-04-29 07:22:11 -0600161 return series, cover_fname, patch_files
162
163
Simon Glass5750cc22025-05-07 18:02:47 +0200164def send(args, cwd=None):
Simon Glassc0257982025-04-29 07:22:11 -0600165 """Create, check and send patches by email
166
167 Args:
168 args (argparse.Namespace): Arguments to patman
Simon Glass5750cc22025-05-07 18:02:47 +0200169 cwd (str): Path to use for git operations
Simon Glassc0257982025-04-29 07:22:11 -0600170 """
171 col = terminal.Color()
172 series, cover_fname, patch_files = prepare_patches(
173 col, args.branch, args.count, args.start, args.end,
174 args.ignore_binary, args.add_signoff,
Simon Glass5750cc22025-05-07 18:02:47 +0200175 keep_change_id=args.keep_change_id, cwd=cwd)
Simon Glassc0257982025-04-29 07:22:11 -0600176 ok = check_patches(series, patch_files, args.check_patch,
Simon Glass5750cc22025-05-07 18:02:47 +0200177 args.verbose, args.check_patch_use_tree, cwd)
Simon Glassc0257982025-04-29 07:22:11 -0600178
179 ok = ok and gitutil.check_suppress_cc_config()
180
181 its_a_go = ok or args.ignore_errors
182 email_patches(
183 col, series, cover_fname, patch_files, args.process_tags,
184 its_a_go, args.ignore_bad_tags, args.add_maintainers,
185 args.get_maintainer_script, args.limit, args.dry_run,
Simon Glass5750cc22025-05-07 18:02:47 +0200186 args.in_reply_to, args.thread, args.smtp_server, cwd=cwd)