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