blob: 97bb048c4b7f7b13dad5f782487949cf7a3bd341 [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:
Simon Glass27d6f0f2025-05-08 06:35:42 +020028 cc (list of str): Aliases/emails to Cc all patches to
29 to (list of str): Aliases/emails to send patches to
30 commits (list of Commit): Commit objects, one for each patch
31 cover (list of str): Lines in the cover letter
32 notes (list of str): Lines in the notes
33 changes: (dict) List of changes for each version:
34 key (int): version number
35 value: tuple:
36 commit (Commit): Commit this relates to, or None if related to a
37 cover letter
38 info (str): change lines for this version (separated by \n)
39 allow_overwrite (bool): Allow tags to overwrite an existing tag
40 base_commit (Commit): Commit object at the base of this series
41 branch (str): Branch name of this series
42 _generated_cc (dict) written in MakeCcFile()
43 key: name of patch file
44 value: list of email addresses
Simon Glass26132882012-01-14 15:12:45 +000045 """
46 def __init__(self):
47 self.cc = []
48 self.to = []
Simon Glassc72f3da2013-03-20 16:43:00 +000049 self.cover_cc = []
Simon Glass26132882012-01-14 15:12:45 +000050 self.commits = []
51 self.cover = None
52 self.notes = []
53 self.changes = {}
Simon Glass29c5abc2013-05-02 14:46:02 +000054 self.allow_overwrite = False
Simon Glass414f1e02025-02-27 12:27:30 -070055 self.base_commit = None
56 self.branch = None
Doug Anderson507e8e82012-12-03 14:40:42 +000057 self._generated_cc = {}
58
Simon Glass26132882012-01-14 15:12:45 +000059 # These make us more like a dictionary
60 def __setattr__(self, name, value):
61 self[name] = value
62
63 def __getattr__(self, name):
64 return self[name]
65
66 def AddTag(self, commit, line, name, value):
67 """Add a new Series-xxx tag along with its value.
68
69 Args:
70 line: Source line containing tag (useful for debug/error messages)
71 name: Tag name (part after 'Series-')
72 value: Tag value (part after 'Series-xxx: ')
Simon Glass8093a8f2020-10-29 21:46:25 -060073
74 Returns:
75 String warning if something went wrong, else None
Simon Glass26132882012-01-14 15:12:45 +000076 """
77 # If we already have it, then add to our list
Simon Glassc72f3da2013-03-20 16:43:00 +000078 name = name.replace('-', '_')
Simon Glass29c5abc2013-05-02 14:46:02 +000079 if name in self and not self.allow_overwrite:
Simon Glass26132882012-01-14 15:12:45 +000080 values = value.split(',')
81 values = [str.strip() for str in values]
82 if type(self[name]) != type([]):
83 raise ValueError("In %s: line '%s': Cannot add another value "
84 "'%s' to series '%s'" %
85 (commit.hash, line, values, self[name]))
86 self[name] += values
87
88 # Otherwise just set the value
89 elif name in valid_series:
Albert ARIBAUD77ec8bc2016-02-02 10:24:53 +010090 if name=="notes":
91 self[name] = [value]
92 else:
93 self[name] = value
Simon Glass26132882012-01-14 15:12:45 +000094 else:
Simon Glass8093a8f2020-10-29 21:46:25 -060095 return ("In %s: line '%s': Unknown 'Series-%s': valid "
Simon Glasse7ecd3f2012-09-27 15:06:02 +000096 "options are %s" % (commit.hash, line, name,
Simon Glass26132882012-01-14 15:12:45 +000097 ', '.join(valid_series)))
Simon Glass8093a8f2020-10-29 21:46:25 -060098 return None
Simon Glass26132882012-01-14 15:12:45 +000099
100 def AddCommit(self, commit):
101 """Add a commit into our list of commits
102
103 We create a list of tags in the commit subject also.
104
105 Args:
106 commit: Commit object to add
107 """
Simon Glass530ac272022-01-29 14:14:07 -0700108 commit.check_tags()
Simon Glass26132882012-01-14 15:12:45 +0000109 self.commits.append(commit)
110
Simon Glass32f12a7e2025-04-07 22:51:47 +1200111 def ShowActions(self, args, cmd, process_tags, alias):
Simon Glass26132882012-01-14 15:12:45 +0000112 """Show what actions we will/would perform
113
114 Args:
115 args: List of patch files we created
116 cmd: The git command we would have run
117 process_tags: Process tags as if they were aliases
Simon Glass32f12a7e2025-04-07 22:51:47 +1200118 alias (dict): Alias dictionary
119 key: alias
120 value: list of aliases or email addresses
Simon Glass26132882012-01-14 15:12:45 +0000121 """
Simon Glass32f12a7e2025-04-07 22:51:47 +1200122 to_set = set(gitutil.build_email_list(self.to, alias));
123 cc_set = set(gitutil.build_email_list(self.cc, alias));
Peter Tyserc6af8022015-01-26 11:42:21 -0600124
Simon Glass26132882012-01-14 15:12:45 +0000125 col = terminal.Color()
Paul Burtonc3931342016-09-27 16:03:50 +0100126 print('Dry run, so not doing much. But I would do this:')
127 print()
128 print('Send a total of %d patch%s with %scover letter.' % (
Simon Glass26132882012-01-14 15:12:45 +0000129 len(args), '' if len(args) == 1 else 'es',
Paul Burtonc3931342016-09-27 16:03:50 +0100130 self.get('cover') and 'a ' or 'no '))
Simon Glass26132882012-01-14 15:12:45 +0000131
132 # TODO: Colour the patches according to whether they passed checks
133 for upto in range(len(args)):
134 commit = self.commits[upto]
Simon Glassf45d3742022-01-29 14:14:17 -0700135 print(col.build(col.GREEN, ' %s' % args[upto]))
Doug Anderson507e8e82012-12-03 14:40:42 +0000136 cc_list = list(self._generated_cc[commit.patch])
Simon Glass3ab178f2019-05-14 15:53:51 -0600137 for email in sorted(set(cc_list) - to_set - cc_set):
Simon Glass26132882012-01-14 15:12:45 +0000138 if email == None:
Simon Glass547cba62022-02-11 13:23:18 -0700139 email = col.build(col.YELLOW, '<alias not found>')
Simon Glass26132882012-01-14 15:12:45 +0000140 if email:
Simon Glass22c58342017-05-29 15:31:23 -0600141 print(' Cc: ', email)
Simon Glass26132882012-01-14 15:12:45 +0000142 print
Simon Glass3ab178f2019-05-14 15:53:51 -0600143 for item in sorted(to_set):
Paul Burtonc3931342016-09-27 16:03:50 +0100144 print('To:\t ', item)
Simon Glass3ab178f2019-05-14 15:53:51 -0600145 for item in sorted(cc_set - to_set):
Paul Burtonc3931342016-09-27 16:03:50 +0100146 print('Cc:\t ', item)
147 print('Version: ', self.get('version'))
148 print('Prefix:\t ', self.get('prefix'))
Sean Andersondc1cd132021-10-22 19:07:04 -0400149 print('Postfix:\t ', self.get('postfix'))
Simon Glass26132882012-01-14 15:12:45 +0000150 if self.cover:
Paul Burtonc3931342016-09-27 16:03:50 +0100151 print('Cover: %d lines' % len(self.cover))
Simon Glassdea1ddf2025-04-07 22:51:44 +1200152 cover_cc = gitutil.build_email_list(self.get('cover_cc', ''),
Simon Glass32f12a7e2025-04-07 22:51:47 +1200153 alias)
Simon Glassc72f3da2013-03-20 16:43:00 +0000154 all_ccs = itertools.chain(cover_cc, *self._generated_cc.values())
Simon Glass3ab178f2019-05-14 15:53:51 -0600155 for email in sorted(set(all_ccs) - to_set - cc_set):
Paul Burtonc3931342016-09-27 16:03:50 +0100156 print(' Cc: ', email)
Simon Glass26132882012-01-14 15:12:45 +0000157 if cmd:
Paul Burtonc3931342016-09-27 16:03:50 +0100158 print('Git command: %s' % cmd)
Simon Glass26132882012-01-14 15:12:45 +0000159
160 def MakeChangeLog(self, commit):
161 """Create a list of changes for each version.
162
163 Return:
164 The change log as a list of strings, one per line
165
Simon Glassaa912af2012-10-30 06:15:16 +0000166 Changes in v4:
Otavio Salvadorcc1fbaa2012-08-18 07:46:04 +0000167 - Jog the dial back closer to the widget
168
Simon Glassaa912af2012-10-30 06:15:16 +0000169 Changes in v2:
Simon Glass26132882012-01-14 15:12:45 +0000170 - Fix the widget
171 - Jog the dial
172
Sean Anderson5ae4e8d2020-05-04 16:28:33 -0400173 If there are no new changes in a patch, a note will be added
174
175 (no changes since v2)
176
177 Changes in v2:
178 - Fix the widget
179 - Jog the dial
Simon Glass26132882012-01-14 15:12:45 +0000180 """
Sean Anderson48f46d62020-05-04 16:28:34 -0400181 # Collect changes from the series and this commit
182 changes = collections.defaultdict(list)
183 for version, changelist in self.changes.items():
184 changes[version] += changelist
185 if commit:
186 for version, changelist in commit.changes.items():
187 changes[version] += [[commit, text] for text in changelist]
188
189 versions = sorted(changes, reverse=True)
Sean Anderson5ae4e8d2020-05-04 16:28:33 -0400190 newest_version = 1
191 if 'version' in self:
192 newest_version = max(newest_version, int(self.version))
193 if versions:
194 newest_version = max(newest_version, versions[0])
195
Simon Glass26132882012-01-14 15:12:45 +0000196 final = []
Simon Glassec1d0422013-03-26 13:09:44 +0000197 process_it = self.get('process_log', '').split(',')
198 process_it = [item.strip() for item in process_it]
Simon Glass26132882012-01-14 15:12:45 +0000199 need_blank = False
Sean Anderson5ae4e8d2020-05-04 16:28:33 -0400200 for version in versions:
Simon Glass26132882012-01-14 15:12:45 +0000201 out = []
Sean Anderson48f46d62020-05-04 16:28:34 -0400202 for this_commit, text in changes[version]:
Simon Glass26132882012-01-14 15:12:45 +0000203 if commit and this_commit != commit:
204 continue
Simon Glassec1d0422013-03-26 13:09:44 +0000205 if 'uniq' not in process_it or text not in out:
206 out.append(text)
Simon Glassec1d0422013-03-26 13:09:44 +0000207 if 'sort' in process_it:
208 out = sorted(out)
Sean Anderson5ae4e8d2020-05-04 16:28:33 -0400209 have_changes = len(out) > 0
210 line = 'Changes in v%d:' % version
Simon Glassaa912af2012-10-30 06:15:16 +0000211 if have_changes:
212 out.insert(0, line)
Sean Anderson5ae4e8d2020-05-04 16:28:33 -0400213 if version < newest_version and len(final) == 0:
214 out.insert(0, '')
215 out.insert(0, '(no changes since v%d)' % version)
216 newest_version = 0
217 # Only add a new line if we output something
218 if need_blank:
219 out.insert(0, '')
220 need_blank = False
Simon Glassaa912af2012-10-30 06:15:16 +0000221 final += out
Sean Anderson5ae4e8d2020-05-04 16:28:33 -0400222 need_blank = need_blank or have_changes
223
224 if len(final) > 0:
Simon Glass26132882012-01-14 15:12:45 +0000225 final.append('')
Sean Anderson5ae4e8d2020-05-04 16:28:33 -0400226 elif newest_version != 1:
227 final = ['(no changes since v1)', '']
Simon Glass26132882012-01-14 15:12:45 +0000228 return final
229
230 def DoChecks(self):
231 """Check that each version has a change log
232
233 Print an error if something is wrong.
234 """
235 col = terminal.Color()
236 if self.get('version'):
237 changes_copy = dict(self.changes)
Otavio Salvadorfe07d072012-08-13 10:08:22 +0000238 for version in range(1, int(self.version) + 1):
Simon Glass26132882012-01-14 15:12:45 +0000239 if self.changes.get(version):
240 del changes_copy[version]
241 else:
Otavio Salvadorfe07d072012-08-13 10:08:22 +0000242 if version > 1:
243 str = 'Change log missing for v%d' % version
Simon Glassf45d3742022-01-29 14:14:17 -0700244 print(col.build(col.RED, str))
Simon Glass26132882012-01-14 15:12:45 +0000245 for version in changes_copy:
246 str = 'Change log for unknown version v%d' % version
Simon Glassf45d3742022-01-29 14:14:17 -0700247 print(col.build(col.RED, str))
Simon Glass26132882012-01-14 15:12:45 +0000248 elif self.changes:
249 str = 'Change log exists, but no version is set'
Simon Glassf45d3742022-01-29 14:14:17 -0700250 print(col.build(col.RED, str))
Simon Glass26132882012-01-14 15:12:45 +0000251
Simon Glass77f57df2023-03-08 10:52:53 -0800252 def GetCcForCommit(self, commit, process_tags, warn_on_error,
253 add_maintainers, limit, get_maintainer_script,
Simon Glass5750cc22025-05-07 18:02:47 +0200254 all_skips, alias, cwd):
Simon Glass77f57df2023-03-08 10:52:53 -0800255 """Get the email CCs to use with a particular commit
256
257 Uses subject tags and get_maintainers.pl script to find people to cc
258 on a patch
259
260 Args:
261 commit (Commit): Commit to process
262 process_tags (bool): Process tags as if they were aliases
263 warn_on_error (bool): True to print a warning when an alias fails to
264 match, False to ignore it.
265 add_maintainers (bool or list of str): Either:
266 True/False to call the get_maintainers to CC maintainers
267 List of maintainers to include (for testing)
268 limit (int): Limit the length of the Cc list (None if no limit)
269 get_maintainer_script (str): The file name of the get_maintainer.pl
270 script (or compatible).
271 all_skips (set of str): Updated to include the set of bouncing email
272 addresses that were dropped from the output. This is essentially
273 a return value from this function.
Simon Glass9938b7b2025-04-07 22:51:46 +1200274 alias (dict): Alias dictionary
275 key: alias
276 value: list of aliases or email addresses
Simon Glass5750cc22025-05-07 18:02:47 +0200277 cwd (str): Path to use for patch filenames (None to use current dir)
Simon Glass77f57df2023-03-08 10:52:53 -0800278
279 Returns:
280 list of str: List of email addresses to cc
281 """
282 cc = []
283 if process_tags:
Simon Glass32f12a7e2025-04-07 22:51:47 +1200284 cc += gitutil.build_email_list(commit.tags, alias,
Simon Glass77f57df2023-03-08 10:52:53 -0800285 warn_on_error=warn_on_error)
Simon Glass32f12a7e2025-04-07 22:51:47 +1200286 cc += gitutil.build_email_list(commit.cc_list, alias,
Simon Glass77f57df2023-03-08 10:52:53 -0800287 warn_on_error=warn_on_error)
288 if type(add_maintainers) == type(cc):
289 cc += add_maintainers
290 elif add_maintainers:
Simon Glass5750cc22025-05-07 18:02:47 +0200291 fname = os.path.join(cwd or '', commit.patch)
292 cc += get_maintainer.get_maintainer(get_maintainer_script, fname)
Simon Glass77f57df2023-03-08 10:52:53 -0800293 all_skips |= set(cc) & set(settings.bounces)
294 cc = list(set(cc) - set(settings.bounces))
295 if limit is not None:
296 cc = cc[:limit]
297 return cc
298
Simon Glass1f975b92021-01-23 08:56:15 -0700299 def MakeCcFile(self, process_tags, cover_fname, warn_on_error,
Simon Glass5750cc22025-05-07 18:02:47 +0200300 add_maintainers, limit, get_maintainer_script, alias,
301 cwd=None):
Simon Glass26132882012-01-14 15:12:45 +0000302 """Make a cc file for us to use for per-commit Cc automation
303
Doug Anderson507e8e82012-12-03 14:40:42 +0000304 Also stores in self._generated_cc to make ShowActions() faster.
305
Simon Glass26132882012-01-14 15:12:45 +0000306 Args:
Simon Glass77f57df2023-03-08 10:52:53 -0800307 process_tags (bool): Process tags as if they were aliases
308 cover_fname (str): If non-None the name of the cover letter.
309 warn_on_error (bool): True to print a warning when an alias fails to
310 match, False to ignore it.
311 add_maintainers (bool or list of str): Either:
Simon Glassb712eed2017-05-29 15:31:29 -0600312 True/False to call the get_maintainers to CC maintainers
313 List of maintainers to include (for testing)
Simon Glass77f57df2023-03-08 10:52:53 -0800314 limit (int): Limit the length of the Cc list (None if no limit)
315 get_maintainer_script (str): The file name of the get_maintainer.pl
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -0500316 script (or compatible).
Simon Glass9938b7b2025-04-07 22:51:46 +1200317 alias (dict): Alias dictionary
318 key: alias
319 value: list of aliases or email addresses
Simon Glass5750cc22025-05-07 18:02:47 +0200320 cwd (str): Path to use for patch filenames (None to use current dir)
Simon Glass26132882012-01-14 15:12:45 +0000321 Return:
322 Filename of temp file created
323 """
Chris Packhame8d2a122017-09-01 20:57:53 +1200324 col = terminal.Color()
Simon Glass26132882012-01-14 15:12:45 +0000325 # Look for commit tags (of the form 'xxx:' at the start of the subject)
326 fname = '/tmp/patman.%d' % os.getpid()
Simon Glassf544a2d2019-10-31 07:42:51 -0600327 fd = open(fname, 'w', encoding='utf-8')
Doug Anderson05416af2012-12-03 14:40:43 +0000328 all_ccs = []
Simon Glass77f57df2023-03-08 10:52:53 -0800329 all_skips = set()
Simon Glass620639c2023-03-08 10:52:54 -0800330 with concurrent.futures.ThreadPoolExecutor(max_workers=16) as executor:
331 for i, commit in enumerate(self.commits):
332 commit.seq = i
333 commit.future = executor.submit(
334 self.GetCcForCommit, commit, process_tags, warn_on_error,
Simon Glass9938b7b2025-04-07 22:51:46 +1200335 add_maintainers, limit, get_maintainer_script, all_skips,
Simon Glass5750cc22025-05-07 18:02:47 +0200336 alias, cwd)
Simon Glass620639c2023-03-08 10:52:54 -0800337
338 # Show progress any commits that are taking forever
339 lastlen = 0
340 while True:
341 left = [commit for commit in self.commits
342 if not commit.future.done()]
343 if not left:
344 break
345 names = ', '.join(f'{c.seq + 1}:{c.subject}'
346 for c in left[:2])
347 out = f'\r{len(left)} remaining: {names}'[:79]
348 spaces = ' ' * (lastlen - len(out))
349 if lastlen: # Don't print anything the first time
350 print(out, spaces, end='')
351 sys.stdout.flush()
352 lastlen = len(out)
353 time.sleep(.25)
354 print(f'\rdone{" " * lastlen}\r', end='')
355 print('Cc processing complete')
356
Simon Glass26132882012-01-14 15:12:45 +0000357 for commit in self.commits:
Simon Glass620639c2023-03-08 10:52:54 -0800358 cc = commit.future.result()
Simon Glass0f94fa72017-05-29 15:31:30 -0600359 all_ccs += cc
Dmitry Torokhovef7f67d2019-10-21 20:09:56 -0700360 print(commit.patch, '\0'.join(sorted(set(cc))), file=fd)
Simon Glass0f94fa72017-05-29 15:31:30 -0600361 self._generated_cc[commit.patch] = cc
Simon Glass26132882012-01-14 15:12:45 +0000362
Simon Glass77f57df2023-03-08 10:52:53 -0800363 for x in sorted(all_skips):
364 print(col.build(col.YELLOW, f'Skipping "{x}"'))
365
Doug Anderson05416af2012-12-03 14:40:43 +0000366 if cover_fname:
Simon Glassdea1ddf2025-04-07 22:51:44 +1200367 cover_cc = gitutil.build_email_list(
Simon Glass32f12a7e2025-04-07 22:51:47 +1200368 self.get('cover_cc', ''), alias)
Simon Glass39ed6bb2020-02-27 18:49:23 -0700369 cover_cc = list(set(cover_cc + all_ccs))
370 if limit is not None:
371 cover_cc = cover_cc[:limit]
Simon Glass9dfb3112020-11-08 20:36:18 -0700372 cc_list = '\0'.join([x for x in sorted(cover_cc)])
Robert Beckett3356f2c2019-11-13 18:39:45 +0000373 print(cover_fname, cc_list, file=fd)
Doug Anderson05416af2012-12-03 14:40:43 +0000374
Simon Glass26132882012-01-14 15:12:45 +0000375 fd.close()
376 return fname
377
378 def AddChange(self, version, commit, info):
379 """Add a new change line to a version.
380
381 This will later appear in the change log.
382
383 Args:
Simon Glass27d6f0f2025-05-08 06:35:42 +0200384 version (int): version number to add change list to
385 commit (Commit): Commit this relates to, or None if related to a
386 cover letter
387 info (str): change lines for this version (separated by \n)
Simon Glass26132882012-01-14 15:12:45 +0000388 """
389 if not self.changes.get(version):
390 self.changes[version] = []
391 self.changes[version].append([commit, info])
392
393 def GetPatchPrefix(self):
394 """Get the patch version string
395
396 Return:
397 Patch string, like 'RFC PATCH v5' or just 'PATCH'
398 """
Simon Glass761648b2022-01-29 14:14:11 -0700399 git_prefix = gitutil.get_default_subject_prefix()
Wu, Josh9873b912015-04-15 10:25:18 +0800400 if git_prefix:
Paul Burtona5edb442016-09-27 16:03:49 +0100401 git_prefix = '%s][' % git_prefix
Wu, Josh9873b912015-04-15 10:25:18 +0800402 else:
403 git_prefix = ''
404
Simon Glass26132882012-01-14 15:12:45 +0000405 version = ''
406 if self.get('version'):
407 version = ' v%s' % self['version']
408
409 # Get patch name prefix
410 prefix = ''
411 if self.get('prefix'):
412 prefix = '%s ' % self['prefix']
Sean Andersondc1cd132021-10-22 19:07:04 -0400413
414 postfix = ''
415 if self.get('postfix'):
416 postfix = ' %s' % self['postfix']
417 return '%s%sPATCH%s%s' % (git_prefix, prefix, postfix, version)