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