blob: 4322882abd5857cf2a1c4185d185b1ab5d07e154 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass26132882012-01-14 15:12:45 +00002# Copyright (c) 2011 The Chromium OS Authors.
3#
Simon Glass26132882012-01-14 15:12:45 +00004
Sean Anderson48f46d62020-05-04 16:28:34 -04005from __future__ import print_function
6
7import collections
Simon Glass620639c2023-03-08 10:52:54 -08008import concurrent.futures
Doug Anderson05416af2012-12-03 14:40:43 +00009import itertools
Simon Glass26132882012-01-14 15:12:45 +000010import os
Simon Glass620639c2023-03-08 10:52:54 -080011import sys
12import time
Simon Glass26132882012-01-14 15:12:45 +000013
Simon Glassa997ea52020-04-17 18:09:04 -060014from patman import get_maintainer
Simon Glassa997ea52020-04-17 18:09:04 -060015from patman import settings
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
18from u_boot_pylib import tools
Simon Glass26132882012-01-14 15:12:45 +000019
20# Series-xxx tags that we understand
Simon Glassc72f3da2013-03-20 16:43:00 +000021valid_series = ['to', 'cc', 'version', 'changes', 'prefix', 'notes', 'name',
Sean Andersondc1cd132021-10-22 19:07:04 -040022 'cover_cc', 'process_log', 'links', 'patchwork_url', 'postfix']
Simon Glass26132882012-01-14 15:12:45 +000023
24class Series(dict):
25 """Holds information about a patch series, including all tags.
26
27 Vars:
28 cc: List of aliases/emails to Cc all patches to
29 commits: List of Commit objects, one for each patch
30 cover: List of lines in the cover letter
31 notes: List of lines in the notes
32 changes: (dict) List of changes for each version, The key is
33 the integer version number
Simon Glass29c5abc2013-05-02 14:46:02 +000034 allow_overwrite: Allow tags to overwrite an existing tag
Simon Glass26132882012-01-14 15:12:45 +000035 """
36 def __init__(self):
37 self.cc = []
38 self.to = []
Simon Glassc72f3da2013-03-20 16:43:00 +000039 self.cover_cc = []
Simon Glass26132882012-01-14 15:12:45 +000040 self.commits = []
41 self.cover = None
42 self.notes = []
43 self.changes = {}
Simon Glass29c5abc2013-05-02 14:46:02 +000044 self.allow_overwrite = False
Simon Glass414f1e02025-02-27 12:27:30 -070045 self.base_commit = None
46 self.branch = None
Simon Glass26132882012-01-14 15:12:45 +000047
Doug Anderson507e8e82012-12-03 14:40:42 +000048 # Written in MakeCcFile()
49 # key: name of patch file
50 # value: list of email addresses
51 self._generated_cc = {}
52
Simon Glass26132882012-01-14 15:12:45 +000053 # These make us more like a dictionary
54 def __setattr__(self, name, value):
55 self[name] = value
56
57 def __getattr__(self, name):
58 return self[name]
59
60 def AddTag(self, commit, line, name, value):
61 """Add a new Series-xxx tag along with its value.
62
63 Args:
64 line: Source line containing tag (useful for debug/error messages)
65 name: Tag name (part after 'Series-')
66 value: Tag value (part after 'Series-xxx: ')
Simon Glass8093a8f2020-10-29 21:46:25 -060067
68 Returns:
69 String warning if something went wrong, else None
Simon Glass26132882012-01-14 15:12:45 +000070 """
71 # If we already have it, then add to our list
Simon Glassc72f3da2013-03-20 16:43:00 +000072 name = name.replace('-', '_')
Simon Glass29c5abc2013-05-02 14:46:02 +000073 if name in self and not self.allow_overwrite:
Simon Glass26132882012-01-14 15:12:45 +000074 values = value.split(',')
75 values = [str.strip() for str in values]
76 if type(self[name]) != type([]):
77 raise ValueError("In %s: line '%s': Cannot add another value "
78 "'%s' to series '%s'" %
79 (commit.hash, line, values, self[name]))
80 self[name] += values
81
82 # Otherwise just set the value
83 elif name in valid_series:
Albert ARIBAUD77ec8bc2016-02-02 10:24:53 +010084 if name=="notes":
85 self[name] = [value]
86 else:
87 self[name] = value
Simon Glass26132882012-01-14 15:12:45 +000088 else:
Simon Glass8093a8f2020-10-29 21:46:25 -060089 return ("In %s: line '%s': Unknown 'Series-%s': valid "
Simon Glasse7ecd3f2012-09-27 15:06:02 +000090 "options are %s" % (commit.hash, line, name,
Simon Glass26132882012-01-14 15:12:45 +000091 ', '.join(valid_series)))
Simon Glass8093a8f2020-10-29 21:46:25 -060092 return None
Simon Glass26132882012-01-14 15:12:45 +000093
94 def AddCommit(self, commit):
95 """Add a commit into our list of commits
96
97 We create a list of tags in the commit subject also.
98
99 Args:
100 commit: Commit object to add
101 """
Simon Glass530ac272022-01-29 14:14:07 -0700102 commit.check_tags()
Simon Glass26132882012-01-14 15:12:45 +0000103 self.commits.append(commit)
104
105 def ShowActions(self, args, cmd, process_tags):
106 """Show what actions we will/would perform
107
108 Args:
109 args: List of patch files we created
110 cmd: The git command we would have run
111 process_tags: Process tags as if they were aliases
112 """
Simon Glassdea1ddf2025-04-07 22:51:44 +1200113 to_set = set(gitutil.build_email_list(self.to, settings.alias));
114 cc_set = set(gitutil.build_email_list(self.cc, settings.alias));
Peter Tyserc6af8022015-01-26 11:42:21 -0600115
Simon Glass26132882012-01-14 15:12:45 +0000116 col = terminal.Color()
Paul Burtonc3931342016-09-27 16:03:50 +0100117 print('Dry run, so not doing much. But I would do this:')
118 print()
119 print('Send a total of %d patch%s with %scover letter.' % (
Simon Glass26132882012-01-14 15:12:45 +0000120 len(args), '' if len(args) == 1 else 'es',
Paul Burtonc3931342016-09-27 16:03:50 +0100121 self.get('cover') and 'a ' or 'no '))
Simon Glass26132882012-01-14 15:12:45 +0000122
123 # TODO: Colour the patches according to whether they passed checks
124 for upto in range(len(args)):
125 commit = self.commits[upto]
Simon Glassf45d3742022-01-29 14:14:17 -0700126 print(col.build(col.GREEN, ' %s' % args[upto]))
Doug Anderson507e8e82012-12-03 14:40:42 +0000127 cc_list = list(self._generated_cc[commit.patch])
Simon Glass3ab178f2019-05-14 15:53:51 -0600128 for email in sorted(set(cc_list) - to_set - cc_set):
Simon Glass26132882012-01-14 15:12:45 +0000129 if email == None:
Simon Glass547cba62022-02-11 13:23:18 -0700130 email = col.build(col.YELLOW, '<alias not found>')
Simon Glass26132882012-01-14 15:12:45 +0000131 if email:
Simon Glass22c58342017-05-29 15:31:23 -0600132 print(' Cc: ', email)
Simon Glass26132882012-01-14 15:12:45 +0000133 print
Simon Glass3ab178f2019-05-14 15:53:51 -0600134 for item in sorted(to_set):
Paul Burtonc3931342016-09-27 16:03:50 +0100135 print('To:\t ', item)
Simon Glass3ab178f2019-05-14 15:53:51 -0600136 for item in sorted(cc_set - to_set):
Paul Burtonc3931342016-09-27 16:03:50 +0100137 print('Cc:\t ', item)
138 print('Version: ', self.get('version'))
139 print('Prefix:\t ', self.get('prefix'))
Sean Andersondc1cd132021-10-22 19:07:04 -0400140 print('Postfix:\t ', self.get('postfix'))
Simon Glass26132882012-01-14 15:12:45 +0000141 if self.cover:
Paul Burtonc3931342016-09-27 16:03:50 +0100142 print('Cover: %d lines' % len(self.cover))
Simon Glassdea1ddf2025-04-07 22:51:44 +1200143 cover_cc = gitutil.build_email_list(self.get('cover_cc', ''),
144 settings.alias)
Simon Glassc72f3da2013-03-20 16:43:00 +0000145 all_ccs = itertools.chain(cover_cc, *self._generated_cc.values())
Simon Glass3ab178f2019-05-14 15:53:51 -0600146 for email in sorted(set(all_ccs) - to_set - cc_set):
Paul Burtonc3931342016-09-27 16:03:50 +0100147 print(' Cc: ', email)
Simon Glass26132882012-01-14 15:12:45 +0000148 if cmd:
Paul Burtonc3931342016-09-27 16:03:50 +0100149 print('Git command: %s' % cmd)
Simon Glass26132882012-01-14 15:12:45 +0000150
151 def MakeChangeLog(self, commit):
152 """Create a list of changes for each version.
153
154 Return:
155 The change log as a list of strings, one per line
156
Simon Glassaa912af2012-10-30 06:15:16 +0000157 Changes in v4:
Otavio Salvadorcc1fbaa2012-08-18 07:46:04 +0000158 - Jog the dial back closer to the widget
159
Simon Glassaa912af2012-10-30 06:15:16 +0000160 Changes in v2:
Simon Glass26132882012-01-14 15:12:45 +0000161 - Fix the widget
162 - Jog the dial
163
Sean Anderson5ae4e8d2020-05-04 16:28:33 -0400164 If there are no new changes in a patch, a note will be added
165
166 (no changes since v2)
167
168 Changes in v2:
169 - Fix the widget
170 - Jog the dial
Simon Glass26132882012-01-14 15:12:45 +0000171 """
Sean Anderson48f46d62020-05-04 16:28:34 -0400172 # Collect changes from the series and this commit
173 changes = collections.defaultdict(list)
174 for version, changelist in self.changes.items():
175 changes[version] += changelist
176 if commit:
177 for version, changelist in commit.changes.items():
178 changes[version] += [[commit, text] for text in changelist]
179
180 versions = sorted(changes, reverse=True)
Sean Anderson5ae4e8d2020-05-04 16:28:33 -0400181 newest_version = 1
182 if 'version' in self:
183 newest_version = max(newest_version, int(self.version))
184 if versions:
185 newest_version = max(newest_version, versions[0])
186
Simon Glass26132882012-01-14 15:12:45 +0000187 final = []
Simon Glassec1d0422013-03-26 13:09:44 +0000188 process_it = self.get('process_log', '').split(',')
189 process_it = [item.strip() for item in process_it]
Simon Glass26132882012-01-14 15:12:45 +0000190 need_blank = False
Sean Anderson5ae4e8d2020-05-04 16:28:33 -0400191 for version in versions:
Simon Glass26132882012-01-14 15:12:45 +0000192 out = []
Sean Anderson48f46d62020-05-04 16:28:34 -0400193 for this_commit, text in changes[version]:
Simon Glass26132882012-01-14 15:12:45 +0000194 if commit and this_commit != commit:
195 continue
Simon Glassec1d0422013-03-26 13:09:44 +0000196 if 'uniq' not in process_it or text not in out:
197 out.append(text)
Simon Glassec1d0422013-03-26 13:09:44 +0000198 if 'sort' in process_it:
199 out = sorted(out)
Sean Anderson5ae4e8d2020-05-04 16:28:33 -0400200 have_changes = len(out) > 0
201 line = 'Changes in v%d:' % version
Simon Glassaa912af2012-10-30 06:15:16 +0000202 if have_changes:
203 out.insert(0, line)
Sean Anderson5ae4e8d2020-05-04 16:28:33 -0400204 if version < newest_version and len(final) == 0:
205 out.insert(0, '')
206 out.insert(0, '(no changes since v%d)' % version)
207 newest_version = 0
208 # Only add a new line if we output something
209 if need_blank:
210 out.insert(0, '')
211 need_blank = False
Simon Glassaa912af2012-10-30 06:15:16 +0000212 final += out
Sean Anderson5ae4e8d2020-05-04 16:28:33 -0400213 need_blank = need_blank or have_changes
214
215 if len(final) > 0:
Simon Glass26132882012-01-14 15:12:45 +0000216 final.append('')
Sean Anderson5ae4e8d2020-05-04 16:28:33 -0400217 elif newest_version != 1:
218 final = ['(no changes since v1)', '']
Simon Glass26132882012-01-14 15:12:45 +0000219 return final
220
221 def DoChecks(self):
222 """Check that each version has a change log
223
224 Print an error if something is wrong.
225 """
226 col = terminal.Color()
227 if self.get('version'):
228 changes_copy = dict(self.changes)
Otavio Salvadorfe07d072012-08-13 10:08:22 +0000229 for version in range(1, int(self.version) + 1):
Simon Glass26132882012-01-14 15:12:45 +0000230 if self.changes.get(version):
231 del changes_copy[version]
232 else:
Otavio Salvadorfe07d072012-08-13 10:08:22 +0000233 if version > 1:
234 str = 'Change log missing for v%d' % version
Simon Glassf45d3742022-01-29 14:14:17 -0700235 print(col.build(col.RED, str))
Simon Glass26132882012-01-14 15:12:45 +0000236 for version in changes_copy:
237 str = 'Change log for unknown version v%d' % version
Simon Glassf45d3742022-01-29 14:14:17 -0700238 print(col.build(col.RED, str))
Simon Glass26132882012-01-14 15:12:45 +0000239 elif self.changes:
240 str = 'Change log exists, but no version is set'
Simon Glassf45d3742022-01-29 14:14:17 -0700241 print(col.build(col.RED, str))
Simon Glass26132882012-01-14 15:12:45 +0000242
Simon Glass77f57df2023-03-08 10:52:53 -0800243 def GetCcForCommit(self, commit, process_tags, warn_on_error,
244 add_maintainers, limit, get_maintainer_script,
Simon Glass9938b7b2025-04-07 22:51:46 +1200245 all_skips, alias):
Simon Glass77f57df2023-03-08 10:52:53 -0800246 """Get the email CCs to use with a particular commit
247
248 Uses subject tags and get_maintainers.pl script to find people to cc
249 on a patch
250
251 Args:
252 commit (Commit): Commit to process
253 process_tags (bool): Process tags as if they were aliases
254 warn_on_error (bool): True to print a warning when an alias fails to
255 match, False to ignore it.
256 add_maintainers (bool or list of str): Either:
257 True/False to call the get_maintainers to CC maintainers
258 List of maintainers to include (for testing)
259 limit (int): Limit the length of the Cc list (None if no limit)
260 get_maintainer_script (str): The file name of the get_maintainer.pl
261 script (or compatible).
262 all_skips (set of str): Updated to include the set of bouncing email
263 addresses that were dropped from the output. This is essentially
264 a return value from this function.
Simon Glass9938b7b2025-04-07 22:51:46 +1200265 alias (dict): Alias dictionary
266 key: alias
267 value: list of aliases or email addresses
Simon Glass77f57df2023-03-08 10:52:53 -0800268
269 Returns:
270 list of str: List of email addresses to cc
271 """
272 cc = []
273 if process_tags:
Simon Glassdea1ddf2025-04-07 22:51:44 +1200274 cc += gitutil.build_email_list(commit.tags, settings.alias,
Simon Glass77f57df2023-03-08 10:52:53 -0800275 warn_on_error=warn_on_error)
Simon Glassdea1ddf2025-04-07 22:51:44 +1200276 cc += gitutil.build_email_list(commit.cc_list, settings.alias,
Simon Glass77f57df2023-03-08 10:52:53 -0800277 warn_on_error=warn_on_error)
278 if type(add_maintainers) == type(cc):
279 cc += add_maintainers
280 elif add_maintainers:
281 cc += get_maintainer.get_maintainer(get_maintainer_script,
282 commit.patch)
283 all_skips |= set(cc) & set(settings.bounces)
284 cc = list(set(cc) - set(settings.bounces))
285 if limit is not None:
286 cc = cc[:limit]
287 return cc
288
Simon Glass1f975b92021-01-23 08:56:15 -0700289 def MakeCcFile(self, process_tags, cover_fname, warn_on_error,
Simon Glass9938b7b2025-04-07 22:51:46 +1200290 add_maintainers, limit, get_maintainer_script, alias):
Simon Glass26132882012-01-14 15:12:45 +0000291 """Make a cc file for us to use for per-commit Cc automation
292
Doug Anderson507e8e82012-12-03 14:40:42 +0000293 Also stores in self._generated_cc to make ShowActions() faster.
294
Simon Glass26132882012-01-14 15:12:45 +0000295 Args:
Simon Glass77f57df2023-03-08 10:52:53 -0800296 process_tags (bool): Process tags as if they were aliases
297 cover_fname (str): If non-None the name of the cover letter.
298 warn_on_error (bool): True to print a warning when an alias fails to
299 match, False to ignore it.
300 add_maintainers (bool or list of str): Either:
Simon Glassb712eed2017-05-29 15:31:29 -0600301 True/False to call the get_maintainers to CC maintainers
302 List of maintainers to include (for testing)
Simon Glass77f57df2023-03-08 10:52:53 -0800303 limit (int): Limit the length of the Cc list (None if no limit)
304 get_maintainer_script (str): The file name of the get_maintainer.pl
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -0500305 script (or compatible).
Simon Glass9938b7b2025-04-07 22:51:46 +1200306 alias (dict): Alias dictionary
307 key: alias
308 value: list of aliases or email addresses
Simon Glass26132882012-01-14 15:12:45 +0000309 Return:
310 Filename of temp file created
311 """
Chris Packhame8d2a122017-09-01 20:57:53 +1200312 col = terminal.Color()
Simon Glass26132882012-01-14 15:12:45 +0000313 # Look for commit tags (of the form 'xxx:' at the start of the subject)
314 fname = '/tmp/patman.%d' % os.getpid()
Simon Glassf544a2d2019-10-31 07:42:51 -0600315 fd = open(fname, 'w', encoding='utf-8')
Doug Anderson05416af2012-12-03 14:40:43 +0000316 all_ccs = []
Simon Glass77f57df2023-03-08 10:52:53 -0800317 all_skips = set()
Simon Glass620639c2023-03-08 10:52:54 -0800318 with concurrent.futures.ThreadPoolExecutor(max_workers=16) as executor:
319 for i, commit in enumerate(self.commits):
320 commit.seq = i
321 commit.future = executor.submit(
322 self.GetCcForCommit, commit, process_tags, warn_on_error,
Simon Glass9938b7b2025-04-07 22:51:46 +1200323 add_maintainers, limit, get_maintainer_script, all_skips,
324 alias)
Simon Glass620639c2023-03-08 10:52:54 -0800325
326 # Show progress any commits that are taking forever
327 lastlen = 0
328 while True:
329 left = [commit for commit in self.commits
330 if not commit.future.done()]
331 if not left:
332 break
333 names = ', '.join(f'{c.seq + 1}:{c.subject}'
334 for c in left[:2])
335 out = f'\r{len(left)} remaining: {names}'[:79]
336 spaces = ' ' * (lastlen - len(out))
337 if lastlen: # Don't print anything the first time
338 print(out, spaces, end='')
339 sys.stdout.flush()
340 lastlen = len(out)
341 time.sleep(.25)
342 print(f'\rdone{" " * lastlen}\r', end='')
343 print('Cc processing complete')
344
Simon Glass26132882012-01-14 15:12:45 +0000345 for commit in self.commits:
Simon Glass620639c2023-03-08 10:52:54 -0800346 cc = commit.future.result()
Simon Glass0f94fa72017-05-29 15:31:30 -0600347 all_ccs += cc
Dmitry Torokhovef7f67d2019-10-21 20:09:56 -0700348 print(commit.patch, '\0'.join(sorted(set(cc))), file=fd)
Simon Glass0f94fa72017-05-29 15:31:30 -0600349 self._generated_cc[commit.patch] = cc
Simon Glass26132882012-01-14 15:12:45 +0000350
Simon Glass77f57df2023-03-08 10:52:53 -0800351 for x in sorted(all_skips):
352 print(col.build(col.YELLOW, f'Skipping "{x}"'))
353
Doug Anderson05416af2012-12-03 14:40:43 +0000354 if cover_fname:
Simon Glassdea1ddf2025-04-07 22:51:44 +1200355 cover_cc = gitutil.build_email_list(
356 self.get('cover_cc', ''), settings.alias)
Simon Glass39ed6bb2020-02-27 18:49:23 -0700357 cover_cc = list(set(cover_cc + all_ccs))
358 if limit is not None:
359 cover_cc = cover_cc[:limit]
Simon Glass9dfb3112020-11-08 20:36:18 -0700360 cc_list = '\0'.join([x for x in sorted(cover_cc)])
Robert Beckett3356f2c2019-11-13 18:39:45 +0000361 print(cover_fname, cc_list, file=fd)
Doug Anderson05416af2012-12-03 14:40:43 +0000362
Simon Glass26132882012-01-14 15:12:45 +0000363 fd.close()
364 return fname
365
366 def AddChange(self, version, commit, info):
367 """Add a new change line to a version.
368
369 This will later appear in the change log.
370
371 Args:
372 version: version number to add change list to
373 info: change line for this version
374 """
375 if not self.changes.get(version):
376 self.changes[version] = []
377 self.changes[version].append([commit, info])
378
379 def GetPatchPrefix(self):
380 """Get the patch version string
381
382 Return:
383 Patch string, like 'RFC PATCH v5' or just 'PATCH'
384 """
Simon Glass761648b2022-01-29 14:14:11 -0700385 git_prefix = gitutil.get_default_subject_prefix()
Wu, Josh9873b912015-04-15 10:25:18 +0800386 if git_prefix:
Paul Burtona5edb442016-09-27 16:03:49 +0100387 git_prefix = '%s][' % git_prefix
Wu, Josh9873b912015-04-15 10:25:18 +0800388 else:
389 git_prefix = ''
390
Simon Glass26132882012-01-14 15:12:45 +0000391 version = ''
392 if self.get('version'):
393 version = ' v%s' % self['version']
394
395 # Get patch name prefix
396 prefix = ''
397 if self.get('prefix'):
398 prefix = '%s ' % self['prefix']
Sean Andersondc1cd132021-10-22 19:07:04 -0400399
400 postfix = ''
401 if self.get('postfix'):
402 postfix = ' %s' % self['postfix']
403 return '%s%sPATCH%s%s' % (git_prefix, prefix, postfix, version)