blob: 2eeeef71dc6037b525b0e1a8edfae890492ed724 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass0d24de92012-01-14 15:12:45 +00002# Copyright (c) 2011 The Chromium OS Authors.
3#
Simon Glass0d24de92012-01-14 15:12:45 +00004
Sean Anderson6949f702020-05-04 16:28:34 -04005from __future__ import print_function
6
7import collections
Doug Anderson31187252012-12-03 14:40:43 +00008import itertools
Simon Glass0d24de92012-01-14 15:12:45 +00009import os
10
Simon Glassbf776672020-04-17 18:09:04 -060011from patman import get_maintainer
12from patman import gitutil
13from patman import settings
14from patman import terminal
15from patman import tools
Simon Glass0d24de92012-01-14 15:12:45 +000016
17# Series-xxx tags that we understand
Simon Glassfe2f8d92013-03-20 16:43:00 +000018valid_series = ['to', 'cc', 'version', 'changes', 'prefix', 'notes', 'name',
Sean Anderson082c1192021-10-22 19:07:04 -040019 'cover_cc', 'process_log', 'links', 'patchwork_url', 'postfix']
Simon Glass0d24de92012-01-14 15:12:45 +000020
21class Series(dict):
22 """Holds information about a patch series, including all tags.
23
24 Vars:
25 cc: List of aliases/emails to Cc all patches to
26 commits: List of Commit objects, one for each patch
27 cover: List of lines in the cover letter
28 notes: List of lines in the notes
29 changes: (dict) List of changes for each version, The key is
30 the integer version number
Simon Glassf0b739f2013-05-02 14:46:02 +000031 allow_overwrite: Allow tags to overwrite an existing tag
Simon Glass0d24de92012-01-14 15:12:45 +000032 """
33 def __init__(self):
34 self.cc = []
35 self.to = []
Simon Glassfe2f8d92013-03-20 16:43:00 +000036 self.cover_cc = []
Simon Glass0d24de92012-01-14 15:12:45 +000037 self.commits = []
38 self.cover = None
39 self.notes = []
40 self.changes = {}
Simon Glassf0b739f2013-05-02 14:46:02 +000041 self.allow_overwrite = False
Simon Glass0d24de92012-01-14 15:12:45 +000042
Doug Andersond94566a2012-12-03 14:40:42 +000043 # Written in MakeCcFile()
44 # key: name of patch file
45 # value: list of email addresses
46 self._generated_cc = {}
47
Simon Glass0d24de92012-01-14 15:12:45 +000048 # These make us more like a dictionary
49 def __setattr__(self, name, value):
50 self[name] = value
51
52 def __getattr__(self, name):
53 return self[name]
54
55 def AddTag(self, commit, line, name, value):
56 """Add a new Series-xxx tag along with its value.
57
58 Args:
59 line: Source line containing tag (useful for debug/error messages)
60 name: Tag name (part after 'Series-')
61 value: Tag value (part after 'Series-xxx: ')
Simon Glassdffa42c2020-10-29 21:46:25 -060062
63 Returns:
64 String warning if something went wrong, else None
Simon Glass0d24de92012-01-14 15:12:45 +000065 """
66 # If we already have it, then add to our list
Simon Glassfe2f8d92013-03-20 16:43:00 +000067 name = name.replace('-', '_')
Simon Glassf0b739f2013-05-02 14:46:02 +000068 if name in self and not self.allow_overwrite:
Simon Glass0d24de92012-01-14 15:12:45 +000069 values = value.split(',')
70 values = [str.strip() for str in values]
71 if type(self[name]) != type([]):
72 raise ValueError("In %s: line '%s': Cannot add another value "
73 "'%s' to series '%s'" %
74 (commit.hash, line, values, self[name]))
75 self[name] += values
76
77 # Otherwise just set the value
78 elif name in valid_series:
Albert ARIBAUD070b7812016-02-02 10:24:53 +010079 if name=="notes":
80 self[name] = [value]
81 else:
82 self[name] = value
Simon Glass0d24de92012-01-14 15:12:45 +000083 else:
Simon Glassdffa42c2020-10-29 21:46:25 -060084 return ("In %s: line '%s': Unknown 'Series-%s': valid "
Simon Glassef0e9de2012-09-27 15:06:02 +000085 "options are %s" % (commit.hash, line, name,
Simon Glass0d24de92012-01-14 15:12:45 +000086 ', '.join(valid_series)))
Simon Glassdffa42c2020-10-29 21:46:25 -060087 return None
Simon Glass0d24de92012-01-14 15:12:45 +000088
89 def AddCommit(self, commit):
90 """Add a commit into our list of commits
91
92 We create a list of tags in the commit subject also.
93
94 Args:
95 commit: Commit object to add
96 """
Simon Glassa3eeadf2022-01-29 14:14:07 -070097 commit.check_tags()
Simon Glass0d24de92012-01-14 15:12:45 +000098 self.commits.append(commit)
99
100 def ShowActions(self, args, cmd, process_tags):
101 """Show what actions we will/would perform
102
103 Args:
104 args: List of patch files we created
105 cmd: The git command we would have run
106 process_tags: Process tags as if they were aliases
107 """
Simon Glass0157b182022-01-29 14:14:11 -0700108 to_set = set(gitutil.build_email_list(self.to));
109 cc_set = set(gitutil.build_email_list(self.cc));
Peter Tyser21818302015-01-26 11:42:21 -0600110
Simon Glass0d24de92012-01-14 15:12:45 +0000111 col = terminal.Color()
Paul Burtona920a172016-09-27 16:03:50 +0100112 print('Dry run, so not doing much. But I would do this:')
113 print()
114 print('Send a total of %d patch%s with %scover letter.' % (
Simon Glass0d24de92012-01-14 15:12:45 +0000115 len(args), '' if len(args) == 1 else 'es',
Paul Burtona920a172016-09-27 16:03:50 +0100116 self.get('cover') and 'a ' or 'no '))
Simon Glass0d24de92012-01-14 15:12:45 +0000117
118 # TODO: Colour the patches according to whether they passed checks
119 for upto in range(len(args)):
120 commit = self.commits[upto]
Simon Glass252ac582022-01-29 14:14:17 -0700121 print(col.build(col.GREEN, ' %s' % args[upto]))
Doug Andersond94566a2012-12-03 14:40:42 +0000122 cc_list = list(self._generated_cc[commit.patch])
Simon Glassb644c662019-05-14 15:53:51 -0600123 for email in sorted(set(cc_list) - to_set - cc_set):
Simon Glass0d24de92012-01-14 15:12:45 +0000124 if email == None:
Simon Glass32cc6ae2022-02-11 13:23:18 -0700125 email = col.build(col.YELLOW, '<alias not found>')
Simon Glass0d24de92012-01-14 15:12:45 +0000126 if email:
Simon Glass6f8abf72017-05-29 15:31:23 -0600127 print(' Cc: ', email)
Simon Glass0d24de92012-01-14 15:12:45 +0000128 print
Simon Glassb644c662019-05-14 15:53:51 -0600129 for item in sorted(to_set):
Paul Burtona920a172016-09-27 16:03:50 +0100130 print('To:\t ', item)
Simon Glassb644c662019-05-14 15:53:51 -0600131 for item in sorted(cc_set - to_set):
Paul Burtona920a172016-09-27 16:03:50 +0100132 print('Cc:\t ', item)
133 print('Version: ', self.get('version'))
134 print('Prefix:\t ', self.get('prefix'))
Sean Anderson082c1192021-10-22 19:07:04 -0400135 print('Postfix:\t ', self.get('postfix'))
Simon Glass0d24de92012-01-14 15:12:45 +0000136 if self.cover:
Paul Burtona920a172016-09-27 16:03:50 +0100137 print('Cover: %d lines' % len(self.cover))
Simon Glass0157b182022-01-29 14:14:11 -0700138 cover_cc = gitutil.build_email_list(self.get('cover_cc', ''))
Simon Glassfe2f8d92013-03-20 16:43:00 +0000139 all_ccs = itertools.chain(cover_cc, *self._generated_cc.values())
Simon Glassb644c662019-05-14 15:53:51 -0600140 for email in sorted(set(all_ccs) - to_set - cc_set):
Paul Burtona920a172016-09-27 16:03:50 +0100141 print(' Cc: ', email)
Simon Glass0d24de92012-01-14 15:12:45 +0000142 if cmd:
Paul Burtona920a172016-09-27 16:03:50 +0100143 print('Git command: %s' % cmd)
Simon Glass0d24de92012-01-14 15:12:45 +0000144
145 def MakeChangeLog(self, commit):
146 """Create a list of changes for each version.
147
148 Return:
149 The change log as a list of strings, one per line
150
Simon Glass27e97602012-10-30 06:15:16 +0000151 Changes in v4:
Otavio Salvador244e6f92012-08-18 07:46:04 +0000152 - Jog the dial back closer to the widget
153
Simon Glass27e97602012-10-30 06:15:16 +0000154 Changes in v2:
Simon Glass0d24de92012-01-14 15:12:45 +0000155 - Fix the widget
156 - Jog the dial
157
Sean Andersonb0436b92020-05-04 16:28:33 -0400158 If there are no new changes in a patch, a note will be added
159
160 (no changes since v2)
161
162 Changes in v2:
163 - Fix the widget
164 - Jog the dial
Simon Glass0d24de92012-01-14 15:12:45 +0000165 """
Sean Anderson6949f702020-05-04 16:28:34 -0400166 # Collect changes from the series and this commit
167 changes = collections.defaultdict(list)
168 for version, changelist in self.changes.items():
169 changes[version] += changelist
170 if commit:
171 for version, changelist in commit.changes.items():
172 changes[version] += [[commit, text] for text in changelist]
173
174 versions = sorted(changes, reverse=True)
Sean Andersonb0436b92020-05-04 16:28:33 -0400175 newest_version = 1
176 if 'version' in self:
177 newest_version = max(newest_version, int(self.version))
178 if versions:
179 newest_version = max(newest_version, versions[0])
180
Simon Glass0d24de92012-01-14 15:12:45 +0000181 final = []
Simon Glass645b2712013-03-26 13:09:44 +0000182 process_it = self.get('process_log', '').split(',')
183 process_it = [item.strip() for item in process_it]
Simon Glass0d24de92012-01-14 15:12:45 +0000184 need_blank = False
Sean Andersonb0436b92020-05-04 16:28:33 -0400185 for version in versions:
Simon Glass0d24de92012-01-14 15:12:45 +0000186 out = []
Sean Anderson6949f702020-05-04 16:28:34 -0400187 for this_commit, text in changes[version]:
Simon Glass0d24de92012-01-14 15:12:45 +0000188 if commit and this_commit != commit:
189 continue
Simon Glass645b2712013-03-26 13:09:44 +0000190 if 'uniq' not in process_it or text not in out:
191 out.append(text)
Simon Glass645b2712013-03-26 13:09:44 +0000192 if 'sort' in process_it:
193 out = sorted(out)
Sean Andersonb0436b92020-05-04 16:28:33 -0400194 have_changes = len(out) > 0
195 line = 'Changes in v%d:' % version
Simon Glass27e97602012-10-30 06:15:16 +0000196 if have_changes:
197 out.insert(0, line)
Sean Andersonb0436b92020-05-04 16:28:33 -0400198 if version < newest_version and len(final) == 0:
199 out.insert(0, '')
200 out.insert(0, '(no changes since v%d)' % version)
201 newest_version = 0
202 # Only add a new line if we output something
203 if need_blank:
204 out.insert(0, '')
205 need_blank = False
Simon Glass27e97602012-10-30 06:15:16 +0000206 final += out
Sean Andersonb0436b92020-05-04 16:28:33 -0400207 need_blank = need_blank or have_changes
208
209 if len(final) > 0:
Simon Glass0d24de92012-01-14 15:12:45 +0000210 final.append('')
Sean Andersonb0436b92020-05-04 16:28:33 -0400211 elif newest_version != 1:
212 final = ['(no changes since v1)', '']
Simon Glass0d24de92012-01-14 15:12:45 +0000213 return final
214
215 def DoChecks(self):
216 """Check that each version has a change log
217
218 Print an error if something is wrong.
219 """
220 col = terminal.Color()
221 if self.get('version'):
222 changes_copy = dict(self.changes)
Otavio Salvadord5f81d82012-08-13 10:08:22 +0000223 for version in range(1, int(self.version) + 1):
Simon Glass0d24de92012-01-14 15:12:45 +0000224 if self.changes.get(version):
225 del changes_copy[version]
226 else:
Otavio Salvadord5f81d82012-08-13 10:08:22 +0000227 if version > 1:
228 str = 'Change log missing for v%d' % version
Simon Glass252ac582022-01-29 14:14:17 -0700229 print(col.build(col.RED, str))
Simon Glass0d24de92012-01-14 15:12:45 +0000230 for version in changes_copy:
231 str = 'Change log for unknown version v%d' % version
Simon Glass252ac582022-01-29 14:14:17 -0700232 print(col.build(col.RED, str))
Simon Glass0d24de92012-01-14 15:12:45 +0000233 elif self.changes:
234 str = 'Change log exists, but no version is set'
Simon Glass252ac582022-01-29 14:14:17 -0700235 print(col.build(col.RED, str))
Simon Glass0d24de92012-01-14 15:12:45 +0000236
Simon Glass0fb560d2021-01-23 08:56:15 -0700237 def MakeCcFile(self, process_tags, cover_fname, warn_on_error,
Maxim Cournoyer8c042fb2022-12-20 00:28:46 -0500238 add_maintainers, limit, get_maintainer_script):
Simon Glass0d24de92012-01-14 15:12:45 +0000239 """Make a cc file for us to use for per-commit Cc automation
240
Doug Andersond94566a2012-12-03 14:40:42 +0000241 Also stores in self._generated_cc to make ShowActions() faster.
242
Simon Glass0d24de92012-01-14 15:12:45 +0000243 Args:
244 process_tags: Process tags as if they were aliases
Doug Anderson31187252012-12-03 14:40:43 +0000245 cover_fname: If non-None the name of the cover letter.
Simon Glass0fb560d2021-01-23 08:56:15 -0700246 warn_on_error: True to print a warning when an alias fails to match,
247 False to ignore it.
Simon Glass1f487f82017-05-29 15:31:29 -0600248 add_maintainers: Either:
249 True/False to call the get_maintainers to CC maintainers
250 List of maintainers to include (for testing)
Simon Glass7d5b04e2020-07-05 21:41:49 -0600251 limit: Limit the length of the Cc list (None if no limit)
Maxim Cournoyer8c042fb2022-12-20 00:28:46 -0500252 get_maintainer_script: The file name of the get_maintainer.pl
253 script (or compatible).
Simon Glass0d24de92012-01-14 15:12:45 +0000254 Return:
255 Filename of temp file created
256 """
Chris Packhame11aa602017-09-01 20:57:53 +1200257 col = terminal.Color()
Simon Glass0d24de92012-01-14 15:12:45 +0000258 # Look for commit tags (of the form 'xxx:' at the start of the subject)
259 fname = '/tmp/patman.%d' % os.getpid()
Simon Glass272cd852019-10-31 07:42:51 -0600260 fd = open(fname, 'w', encoding='utf-8')
Doug Anderson31187252012-12-03 14:40:43 +0000261 all_ccs = []
Simon Glass0d24de92012-01-14 15:12:45 +0000262 for commit in self.commits:
Simon Glassa44f4fb2017-05-29 15:31:30 -0600263 cc = []
Simon Glass0d24de92012-01-14 15:12:45 +0000264 if process_tags:
Simon Glass0157b182022-01-29 14:14:11 -0700265 cc += gitutil.build_email_list(commit.tags,
Simon Glass0fb560d2021-01-23 08:56:15 -0700266 warn_on_error=warn_on_error)
Simon Glass0157b182022-01-29 14:14:11 -0700267 cc += gitutil.build_email_list(commit.cc_list,
Simon Glass0fb560d2021-01-23 08:56:15 -0700268 warn_on_error=warn_on_error)
Simon Glassa44f4fb2017-05-29 15:31:30 -0600269 if type(add_maintainers) == type(cc):
270 cc += add_maintainers
Simon Glass1f487f82017-05-29 15:31:29 -0600271 elif add_maintainers:
Maxim Cournoyer8c042fb2022-12-20 00:28:46 -0500272
273 cc += get_maintainer.get_maintainer(get_maintainer_script,
274 commit.patch)
Chris Packhame11aa602017-09-01 20:57:53 +1200275 for x in set(cc) & set(settings.bounces):
Simon Glass252ac582022-01-29 14:14:17 -0700276 print(col.build(col.YELLOW, 'Skipping "%s"' % x))
Bernhard Kirchen67637d42021-01-29 15:10:08 +0100277 cc = list(set(cc) - set(settings.bounces))
Chris Packham4fb35022018-06-07 20:45:06 +1200278 if limit is not None:
279 cc = cc[:limit]
Simon Glassa44f4fb2017-05-29 15:31:30 -0600280 all_ccs += cc
Dmitry Torokhov8ab452d2019-10-21 20:09:56 -0700281 print(commit.patch, '\0'.join(sorted(set(cc))), file=fd)
Simon Glassa44f4fb2017-05-29 15:31:30 -0600282 self._generated_cc[commit.patch] = cc
Simon Glass0d24de92012-01-14 15:12:45 +0000283
Doug Anderson31187252012-12-03 14:40:43 +0000284 if cover_fname:
Simon Glass0157b182022-01-29 14:14:11 -0700285 cover_cc = gitutil.build_email_list(self.get('cover_cc', ''))
Simon Glasscf0ef932020-02-27 18:49:23 -0700286 cover_cc = list(set(cover_cc + all_ccs))
287 if limit is not None:
288 cover_cc = cover_cc[:limit]
Simon Glassfc0056e2020-11-08 20:36:18 -0700289 cc_list = '\0'.join([x for x in sorted(cover_cc)])
Robert Beckett677dac22019-11-13 18:39:45 +0000290 print(cover_fname, cc_list, file=fd)
Doug Anderson31187252012-12-03 14:40:43 +0000291
Simon Glass0d24de92012-01-14 15:12:45 +0000292 fd.close()
293 return fname
294
295 def AddChange(self, version, commit, info):
296 """Add a new change line to a version.
297
298 This will later appear in the change log.
299
300 Args:
301 version: version number to add change list to
302 info: change line for this version
303 """
304 if not self.changes.get(version):
305 self.changes[version] = []
306 self.changes[version].append([commit, info])
307
308 def GetPatchPrefix(self):
309 """Get the patch version string
310
311 Return:
312 Patch string, like 'RFC PATCH v5' or just 'PATCH'
313 """
Simon Glass0157b182022-01-29 14:14:11 -0700314 git_prefix = gitutil.get_default_subject_prefix()
Wu, Josh3871cd82015-04-15 10:25:18 +0800315 if git_prefix:
Paul Burton12e54762016-09-27 16:03:49 +0100316 git_prefix = '%s][' % git_prefix
Wu, Josh3871cd82015-04-15 10:25:18 +0800317 else:
318 git_prefix = ''
319
Simon Glass0d24de92012-01-14 15:12:45 +0000320 version = ''
321 if self.get('version'):
322 version = ' v%s' % self['version']
323
324 # Get patch name prefix
325 prefix = ''
326 if self.get('prefix'):
327 prefix = '%s ' % self['prefix']
Sean Anderson082c1192021-10-22 19:07:04 -0400328
329 postfix = ''
330 if self.get('postfix'):
331 postfix = ' %s' % self['postfix']
332 return '%s%sPATCH%s%s' % (git_prefix, prefix, postfix, version)