blob: 9481a95f2f9cdaf21e887da4ed4dfc172b2ad085 [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,
90 add_maintainers, limit, get_maintainer_script)
91
92 # Email the patches out (giving the user time to check / cancel)
93 cmd = ''
94 if its_a_go:
95 cmd = gitutil.email_patches(
96 series, cover_fname, patch_files, dry_run, not ignore_bad_tags,
Simon Glass5efa3662025-04-07 22:51:45 +120097 cc_file, settings.alias, in_reply_to=in_reply_to, thread=thread,
Simon Glassc0257982025-04-29 07:22:11 -060098 smtp_server=smtp_server)
99 else:
100 print(col.build(col.RED, "Not sending emails due to errors/warnings"))
101
102 # For a dry run, just show our actions as a sanity check
103 if dry_run:
104 series.ShowActions(patch_files, cmd, process_tags)
105 if not its_a_go:
106 print(col.build(col.RED, "Email would not be sent"))
107
108 os.remove(cc_file)
109
110
111def prepare_patches(col, branch, count, start, end, ignore_binary, signoff,
112 keep_change_id=False):
113 """Figure out what patches to generate, then generate them
114
115 The patch files are written to the current directory, e.g. 0001_xxx.patch
116 0002_yyy.patch
117
118 Args:
119 col (terminal.Color): Colour output object
120 branch (str): Branch to create patches from (None = current)
121 count (int): Number of patches to produce, or -1 to produce patches for
122 the current branch back to the upstream commit
123 start (int): Start partch to use (0=first / top of branch)
124 end (int): End patch to use (0=last one in series, 1=one before that,
125 etc.)
126 ignore_binary (bool): Don't generate patches for binary files
127 keep_change_id (bool): Preserve the Change-Id tag.
128
129 Returns:
130 Tuple:
131 Series object for this series (set of patches)
132 Filename of the cover letter as a string (None if none)
133 patch_files: List of patch filenames, each a string, e.g.
134 ['0001_xxx.patch', '0002_yyy.patch']
135 """
136 if count == -1:
137 # Work out how many patches to send if we can
138 count = (gitutil.count_commits_to_branch(branch) - start)
139
140 if not count:
141 str = 'No commits found to process - please use -c flag, or run:\n' \
142 ' git branch --set-upstream-to remote/branch'
143 sys.exit(col.build(col.RED, str))
144
145 # Read the metadata from the commits
146 to_do = count - end
147 series = patchstream.get_metadata(branch, start, to_do)
148 cover_fname, patch_files = gitutil.create_patches(
149 branch, start, to_do, ignore_binary, series, signoff)
150
151 # Fix up the patch files to our liking, and insert the cover letter
152 patchstream.fix_patches(series, patch_files, keep_change_id,
153 insert_base_commit=not cover_fname)
154 if cover_fname and series.get('cover'):
155 patchstream.insert_cover_letter(cover_fname, series, to_do)
156 return series, cover_fname, patch_files
157
158
159def send(args):
160 """Create, check and send patches by email
161
162 Args:
163 args (argparse.Namespace): Arguments to patman
164 """
165 col = terminal.Color()
166 series, cover_fname, patch_files = prepare_patches(
167 col, args.branch, args.count, args.start, args.end,
168 args.ignore_binary, args.add_signoff,
169 keep_change_id=args.keep_change_id)
170 ok = check_patches(series, patch_files, args.check_patch,
171 args.verbose, args.check_patch_use_tree)
172
173 ok = ok and gitutil.check_suppress_cc_config()
174
175 its_a_go = ok or args.ignore_errors
176 email_patches(
177 col, series, cover_fname, patch_files, args.process_tags,
178 its_a_go, args.ignore_bad_tags, args.add_maintainers,
179 args.get_maintainer_script, args.limit, args.dry_run,
180 args.in_reply_to, args.thread, args.smtp_server)