blob: b8a45912058a84a889356bba30f7e2bb18341bea [file] [log] [blame]
Simon Glass24725af2020-07-05 21:41:49 -06001# SPDX-License-Identifier: GPL-2.0+
2#
3# Copyright 2020 Google LLC
4#
5"""Handles the main control logic of patman
6
7This module provides various functions called by the main program to implement
8the features of patman.
9"""
10
11import os
12import sys
13
14from patman import checkpatch
Simon Glass24725af2020-07-05 21:41:49 -060015from patman import patchstream
Simon Glassba1b3b92025-02-09 14:26:00 -070016from u_boot_pylib import gitutil
Simon Glass131444f2023-02-23 18:18:04 -070017from u_boot_pylib import terminal
Simon Glass24725af2020-07-05 21:41:49 -060018
Maxim Cournoyer12f99fd2023-10-12 23:06:24 -040019
Simon Glass24725af2020-07-05 21:41:49 -060020def setup():
21 """Do required setup before doing anything"""
Simon Glass761648b2022-01-29 14:14:11 -070022 gitutil.setup()
Simon Glass24725af2020-07-05 21:41:49 -060023
Maxim Cournoyer12f99fd2023-10-12 23:06:24 -040024
25def prepare_patches(col, branch, count, start, end, ignore_binary, signoff,
26 keep_change_id=False):
Simon Glass24725af2020-07-05 21:41:49 -060027 """Figure out what patches to generate, then generate them
28
29 The patch files are written to the current directory, e.g. 0001_xxx.patch
30 0002_yyy.patch
31
32 Args:
33 col (terminal.Color): Colour output object
Simon Glass2eb4da72020-07-05 21:41:51 -060034 branch (str): Branch to create patches from (None = current)
Simon Glass24725af2020-07-05 21:41:49 -060035 count (int): Number of patches to produce, or -1 to produce patches for
36 the current branch back to the upstream commit
37 start (int): Start partch to use (0=first / top of branch)
Simon Glassb3bf4e12020-07-05 21:41:52 -060038 end (int): End patch to use (0=last one in series, 1=one before that,
39 etc.)
Simon Glass24725af2020-07-05 21:41:49 -060040 ignore_binary (bool): Don't generate patches for binary files
Maxim Cournoyer12f99fd2023-10-12 23:06:24 -040041 keep_change_id (bool): Preserve the Change-Id tag.
Simon Glass24725af2020-07-05 21:41:49 -060042
43 Returns:
44 Tuple:
45 Series object for this series (set of patches)
46 Filename of the cover letter as a string (None if none)
47 patch_files: List of patch filenames, each a string, e.g.
48 ['0001_xxx.patch', '0002_yyy.patch']
49 """
50 if count == -1:
51 # Work out how many patches to send if we can
Simon Glass761648b2022-01-29 14:14:11 -070052 count = (gitutil.count_commits_to_branch(branch) - start)
Simon Glass24725af2020-07-05 21:41:49 -060053
54 if not count:
Nicolas Boichat2173fb42020-07-13 10:50:01 +080055 str = 'No commits found to process - please use -c flag, or run:\n' \
56 ' git branch --set-upstream-to remote/branch'
Simon Glassf45d3742022-01-29 14:14:17 -070057 sys.exit(col.build(col.RED, str))
Simon Glass24725af2020-07-05 21:41:49 -060058
59 # Read the metadata from the commits
Simon Glassb3bf4e12020-07-05 21:41:52 -060060 to_do = count - end
Simon Glass93f61c02020-10-29 21:46:19 -060061 series = patchstream.get_metadata(branch, start, to_do)
Simon Glass761648b2022-01-29 14:14:11 -070062 cover_fname, patch_files = gitutil.create_patches(
Philipp Tomsich858531a2020-11-24 18:14:52 +010063 branch, start, to_do, ignore_binary, series, signoff)
Simon Glass24725af2020-07-05 21:41:49 -060064
65 # Fix up the patch files to our liking, and insert the cover letter
Simon Glassda1a6ec2025-03-28 07:02:20 -060066 patchstream.fix_patches(series, patch_files, keep_change_id,
67 insert_base_commit=not cover_fname)
Simon Glass24725af2020-07-05 21:41:49 -060068 if cover_fname and series.get('cover'):
Simon Glass93f61c02020-10-29 21:46:19 -060069 patchstream.insert_cover_letter(cover_fname, series, to_do)
Simon Glass24725af2020-07-05 21:41:49 -060070 return series, cover_fname, patch_files
71
Maxim Cournoyer12f99fd2023-10-12 23:06:24 -040072
Douglas Andersonbe4f2712022-07-19 14:56:27 -070073def check_patches(series, patch_files, run_checkpatch, verbose, use_tree):
Simon Glass24725af2020-07-05 21:41:49 -060074 """Run some checks on a set of patches
75
76 This santiy-checks the patman tags like Series-version and runs the patches
77 through checkpatch
78
79 Args:
80 series (Series): Series object for this series (set of patches)
81 patch_files (list): List of patch filenames, each a string, e.g.
82 ['0001_xxx.patch', '0002_yyy.patch']
83 run_checkpatch (bool): True to run checkpatch.pl
84 verbose (bool): True to print out every line of the checkpatch output as
85 it is parsed
Douglas Andersonbe4f2712022-07-19 14:56:27 -070086 use_tree (bool): If False we'll pass '--no-tree' to checkpatch.
Simon Glass24725af2020-07-05 21:41:49 -060087
88 Returns:
89 bool: True if the patches had no errors, False if they did
90 """
91 # Do a few checks on the series
92 series.DoChecks()
93
Simon Glassa59449c2023-03-08 10:52:52 -080094 # Check the patches
Simon Glass24725af2020-07-05 21:41:49 -060095 if run_checkpatch:
Douglas Andersonbe4f2712022-07-19 14:56:27 -070096 ok = checkpatch.check_patches(verbose, patch_files, use_tree)
Simon Glass24725af2020-07-05 21:41:49 -060097 else:
98 ok = True
99 return ok
100
101
102def email_patches(col, series, cover_fname, patch_files, process_tags, its_a_go,
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -0500103 ignore_bad_tags, add_maintainers, get_maintainer_script, limit,
104 dry_run, in_reply_to, thread, smtp_server):
Simon Glass24725af2020-07-05 21:41:49 -0600105 """Email patches to the recipients
106
107 This emails out the patches and cover letter using 'git send-email'. Each
108 patch is copied to recipients identified by the patch tag and output from
109 the get_maintainer.pl script. The cover letter is copied to all recipients
110 of any patch.
111
112 To make this work a CC file is created holding the recipients for each patch
113 and the cover letter. See the main program 'cc_cmd' for this logic.
114
115 Args:
116 col (terminal.Color): Colour output object
117 series (Series): Series object for this series (set of patches)
118 cover_fname (str): Filename of the cover letter as a string (None if
119 none)
120 patch_files (list): List of patch filenames, each a string, e.g.
121 ['0001_xxx.patch', '0002_yyy.patch']
122 process_tags (bool): True to process subject tags in each patch, e.g.
123 for 'dm: spi: Add SPI support' this would be 'dm' and 'spi'. The
124 tags are looked up in the configured sendemail.aliasesfile and also
125 in ~/.patman (see README)
126 its_a_go (bool): True if we are going to actually send the patches,
127 False if the patches have errors and will not be sent unless
128 @ignore_errors
129 ignore_bad_tags (bool): True to just print a warning for unknown tags,
130 False to halt with an error
131 add_maintainers (bool): Run the get_maintainer.pl script for each patch
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -0500132 get_maintainer_script (str): The script used to retrieve which
133 maintainers to cc
Simon Glass24725af2020-07-05 21:41:49 -0600134 limit (int): Limit on the number of people that can be cc'd on a single
135 patch or the cover letter (None if no limit)
136 dry_run (bool): Don't actually email the patches, just print out what
137 would be sent
138 in_reply_to (str): If not None we'll pass this to git as --in-reply-to.
139 Should be a message ID that this is in reply to.
140 thread (bool): True to add --thread to git send-email (make all patches
141 reply to cover-letter or first patch in series)
142 smtp_server (str): SMTP server to use to send patches (None for default)
143 """
144 cc_file = series.MakeCcFile(process_tags, cover_fname, not ignore_bad_tags,
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -0500145 add_maintainers, limit, get_maintainer_script)
Simon Glass24725af2020-07-05 21:41:49 -0600146
147 # Email the patches out (giving the user time to check / cancel)
148 cmd = ''
149 if its_a_go:
Simon Glass761648b2022-01-29 14:14:11 -0700150 cmd = gitutil.email_patches(
Simon Glass24725af2020-07-05 21:41:49 -0600151 series, cover_fname, patch_files, dry_run, not ignore_bad_tags,
152 cc_file, in_reply_to=in_reply_to, thread=thread,
153 smtp_server=smtp_server)
154 else:
Simon Glassf45d3742022-01-29 14:14:17 -0700155 print(col.build(col.RED, "Not sending emails due to errors/warnings"))
Simon Glass24725af2020-07-05 21:41:49 -0600156
157 # For a dry run, just show our actions as a sanity check
158 if dry_run:
159 series.ShowActions(patch_files, cmd, process_tags)
160 if not its_a_go:
Simon Glassf45d3742022-01-29 14:14:17 -0700161 print(col.build(col.RED, "Email would not be sent"))
Simon Glass24725af2020-07-05 21:41:49 -0600162
163 os.remove(cc_file)
164
Simon Glasseb101ac2020-07-05 21:41:53 -0600165def send(args):
Simon Glass24725af2020-07-05 21:41:49 -0600166 """Create, check and send patches by email
167
168 Args:
Simon Glasseb101ac2020-07-05 21:41:53 -0600169 args (argparse.Namespace): Arguments to patman
Simon Glass24725af2020-07-05 21:41:49 -0600170 """
171 setup()
172 col = terminal.Color()
173 series, cover_fname, patch_files = prepare_patches(
Simon Glasseb101ac2020-07-05 21:41:53 -0600174 col, args.branch, args.count, args.start, args.end,
Maxim Cournoyer12f99fd2023-10-12 23:06:24 -0400175 args.ignore_binary, args.add_signoff,
176 keep_change_id=args.keep_change_id)
Simon Glasseb101ac2020-07-05 21:41:53 -0600177 ok = check_patches(series, patch_files, args.check_patch,
Douglas Andersonbe4f2712022-07-19 14:56:27 -0700178 args.verbose, args.check_patch_use_tree)
Simon Glass2eb4da72020-07-05 21:41:51 -0600179
Simon Glass761648b2022-01-29 14:14:11 -0700180 ok = ok and gitutil.check_suppress_cc_config()
Nicolas Boichat0da95742020-07-13 10:50:00 +0800181
Simon Glasseb101ac2020-07-05 21:41:53 -0600182 its_a_go = ok or args.ignore_errors
Simon Glass4318b712020-10-29 21:46:10 -0600183 email_patches(
184 col, series, cover_fname, patch_files, args.process_tags,
185 its_a_go, args.ignore_bad_tags, args.add_maintainers,
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -0500186 args.get_maintainer_script, args.limit, args.dry_run,
187 args.in_reply_to, args.thread, args.smtp_server)
Simon Glass3db916d2020-10-29 21:46:35 -0600188
Simon Glass2112d072020-10-29 21:46:38 -0600189def patchwork_status(branch, count, start, end, dest_branch, force,
Simon Glassf9b03cf2020-11-03 13:54:14 -0700190 show_comments, url):
Simon Glass3db916d2020-10-29 21:46:35 -0600191 """Check the status of patches in patchwork
192
193 This finds the series in patchwork using the Series-link tag, checks for new
Simon Glass2112d072020-10-29 21:46:38 -0600194 comments and review tags, displays then and creates a new branch with the
195 review tags.
Simon Glass3db916d2020-10-29 21:46:35 -0600196
197 Args:
198 branch (str): Branch to create patches from (None = current)
199 count (int): Number of patches to produce, or -1 to produce patches for
200 the current branch back to the upstream commit
201 start (int): Start partch to use (0=first / top of branch)
202 end (int): End patch to use (0=last one in series, 1=one before that,
203 etc.)
Simon Glassd0a0a582020-10-29 21:46:36 -0600204 dest_branch (str): Name of new branch to create with the updated tags
205 (None to not create a branch)
206 force (bool): With dest_branch, force overwriting an existing branch
Simon Glass2112d072020-10-29 21:46:38 -0600207 show_comments (bool): True to display snippets from the comments
208 provided by reviewers
Simon Glass4acc93c2020-11-03 13:54:16 -0700209 url (str): URL of patchwork server, e.g. 'https://patchwork.ozlabs.org'.
210 This is ignored if the series provides a Series-patchwork-url tag.
Simon Glass3db916d2020-10-29 21:46:35 -0600211
212 Raises:
213 ValueError: if the branch has no Series-link value
214 """
215 if count == -1:
216 # Work out how many patches to send if we can
Simon Glass761648b2022-01-29 14:14:11 -0700217 count = (gitutil.count_commits_to_branch(branch) - start)
Simon Glass3db916d2020-10-29 21:46:35 -0600218
219 series = patchstream.get_metadata(branch, start, count - end)
220 warnings = 0
221 for cmt in series.commits:
222 if cmt.warn:
223 print('%d warnings for %s:' % (len(cmt.warn), cmt.hash))
224 for warn in cmt.warn:
225 print('\t', warn)
226 warnings += 1
227 print
228 if warnings:
229 raise ValueError('Please fix warnings before running status')
230 links = series.get('links')
231 if not links:
232 raise ValueError("Branch has no Series-links value")
233
234 # Find the link without a version number (we don't support versions yet)
235 found = [link for link in links.split() if not ':' in link]
236 if not found:
237 raise ValueError('Series-links has no current version (without :)')
238
Simon Glass4acc93c2020-11-03 13:54:16 -0700239 # Allow the series to override the URL
240 if 'patchwork_url' in series:
241 url = series.patchwork_url
242
Simon Glass3db916d2020-10-29 21:46:35 -0600243 # Import this here to avoid failing on other commands if the dependencies
244 # are not present
245 from patman import status
Simon Glass2112d072020-10-29 21:46:38 -0600246 status.check_patchwork_status(series, found[0], branch, dest_branch, force,
Simon Glassf9b03cf2020-11-03 13:54:14 -0700247 show_comments, url)