blob: be6f061122c29015fe7460db20cc34178f61622b [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
Doug Anderson31187252012-12-03 14:40:43 +00005import itertools
Simon Glass0d24de92012-01-14 15:12:45 +00006import os
7
Simon Glassbf776672020-04-17 18:09:04 -06008from patman import get_maintainer
9from patman import gitutil
10from patman import settings
11from patman import terminal
12from patman import tools
Simon Glass0d24de92012-01-14 15:12:45 +000013
14# Series-xxx tags that we understand
Simon Glassfe2f8d92013-03-20 16:43:00 +000015valid_series = ['to', 'cc', 'version', 'changes', 'prefix', 'notes', 'name',
Simon Glassd9917b02015-08-22 18:28:01 -060016 'cover_cc', 'process_log']
Simon Glass0d24de92012-01-14 15:12:45 +000017
18class Series(dict):
19 """Holds information about a patch series, including all tags.
20
21 Vars:
22 cc: List of aliases/emails to Cc all patches to
23 commits: List of Commit objects, one for each patch
24 cover: List of lines in the cover letter
25 notes: List of lines in the notes
26 changes: (dict) List of changes for each version, The key is
27 the integer version number
Simon Glassf0b739f2013-05-02 14:46:02 +000028 allow_overwrite: Allow tags to overwrite an existing tag
Simon Glass0d24de92012-01-14 15:12:45 +000029 """
30 def __init__(self):
31 self.cc = []
32 self.to = []
Simon Glassfe2f8d92013-03-20 16:43:00 +000033 self.cover_cc = []
Simon Glass0d24de92012-01-14 15:12:45 +000034 self.commits = []
35 self.cover = None
36 self.notes = []
37 self.changes = {}
Simon Glassf0b739f2013-05-02 14:46:02 +000038 self.allow_overwrite = False
Simon Glass0d24de92012-01-14 15:12:45 +000039
Doug Andersond94566a2012-12-03 14:40:42 +000040 # Written in MakeCcFile()
41 # key: name of patch file
42 # value: list of email addresses
43 self._generated_cc = {}
44
Simon Glass0d24de92012-01-14 15:12:45 +000045 # These make us more like a dictionary
46 def __setattr__(self, name, value):
47 self[name] = value
48
49 def __getattr__(self, name):
50 return self[name]
51
52 def AddTag(self, commit, line, name, value):
53 """Add a new Series-xxx tag along with its value.
54
55 Args:
56 line: Source line containing tag (useful for debug/error messages)
57 name: Tag name (part after 'Series-')
58 value: Tag value (part after 'Series-xxx: ')
59 """
60 # If we already have it, then add to our list
Simon Glassfe2f8d92013-03-20 16:43:00 +000061 name = name.replace('-', '_')
Simon Glassf0b739f2013-05-02 14:46:02 +000062 if name in self and not self.allow_overwrite:
Simon Glass0d24de92012-01-14 15:12:45 +000063 values = value.split(',')
64 values = [str.strip() for str in values]
65 if type(self[name]) != type([]):
66 raise ValueError("In %s: line '%s': Cannot add another value "
67 "'%s' to series '%s'" %
68 (commit.hash, line, values, self[name]))
69 self[name] += values
70
71 # Otherwise just set the value
72 elif name in valid_series:
Albert ARIBAUD070b7812016-02-02 10:24:53 +010073 if name=="notes":
74 self[name] = [value]
75 else:
76 self[name] = value
Simon Glass0d24de92012-01-14 15:12:45 +000077 else:
78 raise ValueError("In %s: line '%s': Unknown 'Series-%s': valid "
Simon Glassef0e9de2012-09-27 15:06:02 +000079 "options are %s" % (commit.hash, line, name,
Simon Glass0d24de92012-01-14 15:12:45 +000080 ', '.join(valid_series)))
81
82 def AddCommit(self, commit):
83 """Add a commit into our list of commits
84
85 We create a list of tags in the commit subject also.
86
87 Args:
88 commit: Commit object to add
89 """
90 commit.CheckTags()
91 self.commits.append(commit)
92
93 def ShowActions(self, args, cmd, process_tags):
94 """Show what actions we will/would perform
95
96 Args:
97 args: List of patch files we created
98 cmd: The git command we would have run
99 process_tags: Process tags as if they were aliases
100 """
Peter Tyser21818302015-01-26 11:42:21 -0600101 to_set = set(gitutil.BuildEmailList(self.to));
102 cc_set = set(gitutil.BuildEmailList(self.cc));
103
Simon Glass0d24de92012-01-14 15:12:45 +0000104 col = terminal.Color()
Paul Burtona920a172016-09-27 16:03:50 +0100105 print('Dry run, so not doing much. But I would do this:')
106 print()
107 print('Send a total of %d patch%s with %scover letter.' % (
Simon Glass0d24de92012-01-14 15:12:45 +0000108 len(args), '' if len(args) == 1 else 'es',
Paul Burtona920a172016-09-27 16:03:50 +0100109 self.get('cover') and 'a ' or 'no '))
Simon Glass0d24de92012-01-14 15:12:45 +0000110
111 # TODO: Colour the patches according to whether they passed checks
112 for upto in range(len(args)):
113 commit = self.commits[upto]
Paul Burtona920a172016-09-27 16:03:50 +0100114 print(col.Color(col.GREEN, ' %s' % args[upto]))
Doug Andersond94566a2012-12-03 14:40:42 +0000115 cc_list = list(self._generated_cc[commit.patch])
Simon Glassb644c662019-05-14 15:53:51 -0600116 for email in sorted(set(cc_list) - to_set - cc_set):
Simon Glass0d24de92012-01-14 15:12:45 +0000117 if email == None:
118 email = col.Color(col.YELLOW, "<alias '%s' not found>"
119 % tag)
120 if email:
Simon Glass6f8abf72017-05-29 15:31:23 -0600121 print(' Cc: ', email)
Simon Glass0d24de92012-01-14 15:12:45 +0000122 print
Simon Glassb644c662019-05-14 15:53:51 -0600123 for item in sorted(to_set):
Paul Burtona920a172016-09-27 16:03:50 +0100124 print('To:\t ', item)
Simon Glassb644c662019-05-14 15:53:51 -0600125 for item in sorted(cc_set - to_set):
Paul Burtona920a172016-09-27 16:03:50 +0100126 print('Cc:\t ', item)
127 print('Version: ', self.get('version'))
128 print('Prefix:\t ', self.get('prefix'))
Simon Glass0d24de92012-01-14 15:12:45 +0000129 if self.cover:
Paul Burtona920a172016-09-27 16:03:50 +0100130 print('Cover: %d lines' % len(self.cover))
Simon Glassfe2f8d92013-03-20 16:43:00 +0000131 cover_cc = gitutil.BuildEmailList(self.get('cover_cc', ''))
132 all_ccs = itertools.chain(cover_cc, *self._generated_cc.values())
Simon Glassb644c662019-05-14 15:53:51 -0600133 for email in sorted(set(all_ccs) - to_set - cc_set):
Paul Burtona920a172016-09-27 16:03:50 +0100134 print(' Cc: ', email)
Simon Glass0d24de92012-01-14 15:12:45 +0000135 if cmd:
Paul Burtona920a172016-09-27 16:03:50 +0100136 print('Git command: %s' % cmd)
Simon Glass0d24de92012-01-14 15:12:45 +0000137
138 def MakeChangeLog(self, commit):
139 """Create a list of changes for each version.
140
141 Return:
142 The change log as a list of strings, one per line
143
Simon Glass27e97602012-10-30 06:15:16 +0000144 Changes in v4:
Otavio Salvador244e6f92012-08-18 07:46:04 +0000145 - Jog the dial back closer to the widget
146
Simon Glass27e97602012-10-30 06:15:16 +0000147 Changes in v2:
Simon Glass0d24de92012-01-14 15:12:45 +0000148 - Fix the widget
149 - Jog the dial
150
Sean Andersonb0436b92020-05-04 16:28:33 -0400151 If there are no new changes in a patch, a note will be added
152
153 (no changes since v2)
154
155 Changes in v2:
156 - Fix the widget
157 - Jog the dial
Simon Glass0d24de92012-01-14 15:12:45 +0000158 """
Sean Andersonb0436b92020-05-04 16:28:33 -0400159 versions = sorted(self.changes, reverse=True)
160 newest_version = 1
161 if 'version' in self:
162 newest_version = max(newest_version, int(self.version))
163 if versions:
164 newest_version = max(newest_version, versions[0])
165
Simon Glass0d24de92012-01-14 15:12:45 +0000166 final = []
Simon Glass645b2712013-03-26 13:09:44 +0000167 process_it = self.get('process_log', '').split(',')
168 process_it = [item.strip() for item in process_it]
Simon Glass0d24de92012-01-14 15:12:45 +0000169 need_blank = False
Sean Andersonb0436b92020-05-04 16:28:33 -0400170 for version in versions:
Simon Glass0d24de92012-01-14 15:12:45 +0000171 out = []
Sean Andersonb0436b92020-05-04 16:28:33 -0400172 for this_commit, text in self.changes[version]:
Simon Glass0d24de92012-01-14 15:12:45 +0000173 if commit and this_commit != commit:
174 continue
Simon Glass645b2712013-03-26 13:09:44 +0000175 if 'uniq' not in process_it or text not in out:
176 out.append(text)
Simon Glass645b2712013-03-26 13:09:44 +0000177 if 'sort' in process_it:
178 out = sorted(out)
Sean Andersonb0436b92020-05-04 16:28:33 -0400179 have_changes = len(out) > 0
180 line = 'Changes in v%d:' % version
Simon Glass27e97602012-10-30 06:15:16 +0000181 if have_changes:
182 out.insert(0, line)
Sean Andersonb0436b92020-05-04 16:28:33 -0400183 if version < newest_version and len(final) == 0:
184 out.insert(0, '')
185 out.insert(0, '(no changes since v%d)' % version)
186 newest_version = 0
187 # Only add a new line if we output something
188 if need_blank:
189 out.insert(0, '')
190 need_blank = False
Simon Glass27e97602012-10-30 06:15:16 +0000191 final += out
Sean Andersonb0436b92020-05-04 16:28:33 -0400192 need_blank = need_blank or have_changes
193
194 if len(final) > 0:
Simon Glass0d24de92012-01-14 15:12:45 +0000195 final.append('')
Sean Andersonb0436b92020-05-04 16:28:33 -0400196 elif newest_version != 1:
197 final = ['(no changes since v1)', '']
Simon Glass0d24de92012-01-14 15:12:45 +0000198 return final
199
200 def DoChecks(self):
201 """Check that each version has a change log
202
203 Print an error if something is wrong.
204 """
205 col = terminal.Color()
206 if self.get('version'):
207 changes_copy = dict(self.changes)
Otavio Salvadord5f81d82012-08-13 10:08:22 +0000208 for version in range(1, int(self.version) + 1):
Simon Glass0d24de92012-01-14 15:12:45 +0000209 if self.changes.get(version):
210 del changes_copy[version]
211 else:
Otavio Salvadord5f81d82012-08-13 10:08:22 +0000212 if version > 1:
213 str = 'Change log missing for v%d' % version
Paul Burtona920a172016-09-27 16:03:50 +0100214 print(col.Color(col.RED, str))
Simon Glass0d24de92012-01-14 15:12:45 +0000215 for version in changes_copy:
216 str = 'Change log for unknown version v%d' % version
Paul Burtona920a172016-09-27 16:03:50 +0100217 print(col.Color(col.RED, str))
Simon Glass0d24de92012-01-14 15:12:45 +0000218 elif self.changes:
219 str = 'Change log exists, but no version is set'
Paul Burtona920a172016-09-27 16:03:50 +0100220 print(col.Color(col.RED, str))
Simon Glass0d24de92012-01-14 15:12:45 +0000221
Simon Glass983a2742014-09-14 20:23:17 -0600222 def MakeCcFile(self, process_tags, cover_fname, raise_on_error,
Chris Packham4fb35022018-06-07 20:45:06 +1200223 add_maintainers, limit):
Simon Glass0d24de92012-01-14 15:12:45 +0000224 """Make a cc file for us to use for per-commit Cc automation
225
Doug Andersond94566a2012-12-03 14:40:42 +0000226 Also stores in self._generated_cc to make ShowActions() faster.
227
Simon Glass0d24de92012-01-14 15:12:45 +0000228 Args:
229 process_tags: Process tags as if they were aliases
Doug Anderson31187252012-12-03 14:40:43 +0000230 cover_fname: If non-None the name of the cover letter.
Simon Glassa1318f72013-03-26 13:09:42 +0000231 raise_on_error: True to raise an error when an alias fails to match,
232 False to just print a message.
Simon Glass1f487f82017-05-29 15:31:29 -0600233 add_maintainers: Either:
234 True/False to call the get_maintainers to CC maintainers
235 List of maintainers to include (for testing)
Chris Packham4fb35022018-06-07 20:45:06 +1200236 limit: Limit the length of the Cc list
Simon Glass0d24de92012-01-14 15:12:45 +0000237 Return:
238 Filename of temp file created
239 """
Chris Packhame11aa602017-09-01 20:57:53 +1200240 col = terminal.Color()
Simon Glass0d24de92012-01-14 15:12:45 +0000241 # Look for commit tags (of the form 'xxx:' at the start of the subject)
242 fname = '/tmp/patman.%d' % os.getpid()
Simon Glass272cd852019-10-31 07:42:51 -0600243 fd = open(fname, 'w', encoding='utf-8')
Doug Anderson31187252012-12-03 14:40:43 +0000244 all_ccs = []
Simon Glass0d24de92012-01-14 15:12:45 +0000245 for commit in self.commits:
Simon Glassa44f4fb2017-05-29 15:31:30 -0600246 cc = []
Simon Glass0d24de92012-01-14 15:12:45 +0000247 if process_tags:
Simon Glassa44f4fb2017-05-29 15:31:30 -0600248 cc += gitutil.BuildEmailList(commit.tags,
Simon Glassa1318f72013-03-26 13:09:42 +0000249 raise_on_error=raise_on_error)
Simon Glassa44f4fb2017-05-29 15:31:30 -0600250 cc += gitutil.BuildEmailList(commit.cc_list,
Simon Glassa1318f72013-03-26 13:09:42 +0000251 raise_on_error=raise_on_error)
Simon Glassa44f4fb2017-05-29 15:31:30 -0600252 if type(add_maintainers) == type(cc):
253 cc += add_maintainers
Simon Glass1f487f82017-05-29 15:31:29 -0600254 elif add_maintainers:
Simon Glassa44f4fb2017-05-29 15:31:30 -0600255 cc += get_maintainer.GetMaintainer(commit.patch)
Chris Packhame11aa602017-09-01 20:57:53 +1200256 for x in set(cc) & set(settings.bounces):
257 print(col.Color(col.YELLOW, 'Skipping "%s"' % x))
258 cc = set(cc) - set(settings.bounces)
Simon Glassf6a6aaf2019-05-14 15:53:54 -0600259 cc = [tools.FromUnicode(m) for m in cc]
Chris Packham4fb35022018-06-07 20:45:06 +1200260 if limit is not None:
261 cc = cc[:limit]
Simon Glassa44f4fb2017-05-29 15:31:30 -0600262 all_ccs += cc
Dmitry Torokhov8ab452d2019-10-21 20:09:56 -0700263 print(commit.patch, '\0'.join(sorted(set(cc))), file=fd)
Simon Glassa44f4fb2017-05-29 15:31:30 -0600264 self._generated_cc[commit.patch] = cc
Simon Glass0d24de92012-01-14 15:12:45 +0000265
Doug Anderson31187252012-12-03 14:40:43 +0000266 if cover_fname:
Simon Glassfe2f8d92013-03-20 16:43:00 +0000267 cover_cc = gitutil.BuildEmailList(self.get('cover_cc', ''))
Simon Glassf6a6aaf2019-05-14 15:53:54 -0600268 cover_cc = [tools.FromUnicode(m) for m in cover_cc]
Simon Glasscf0ef932020-02-27 18:49:23 -0700269 cover_cc = list(set(cover_cc + all_ccs))
270 if limit is not None:
271 cover_cc = cover_cc[:limit]
272 cc_list = '\0'.join([tools.ToUnicode(x) for x in sorted(cover_cc)])
Robert Beckett677dac22019-11-13 18:39:45 +0000273 print(cover_fname, cc_list, file=fd)
Doug Anderson31187252012-12-03 14:40:43 +0000274
Simon Glass0d24de92012-01-14 15:12:45 +0000275 fd.close()
276 return fname
277
278 def AddChange(self, version, commit, info):
279 """Add a new change line to a version.
280
281 This will later appear in the change log.
282
283 Args:
284 version: version number to add change list to
285 info: change line for this version
286 """
287 if not self.changes.get(version):
288 self.changes[version] = []
289 self.changes[version].append([commit, info])
290
291 def GetPatchPrefix(self):
292 """Get the patch version string
293
294 Return:
295 Patch string, like 'RFC PATCH v5' or just 'PATCH'
296 """
Wu, Josh3871cd82015-04-15 10:25:18 +0800297 git_prefix = gitutil.GetDefaultSubjectPrefix()
298 if git_prefix:
Paul Burton12e54762016-09-27 16:03:49 +0100299 git_prefix = '%s][' % git_prefix
Wu, Josh3871cd82015-04-15 10:25:18 +0800300 else:
301 git_prefix = ''
302
Simon Glass0d24de92012-01-14 15:12:45 +0000303 version = ''
304 if self.get('version'):
305 version = ' v%s' % self['version']
306
307 # Get patch name prefix
308 prefix = ''
309 if self.get('prefix'):
310 prefix = '%s ' % self['prefix']
Wu, Josh3871cd82015-04-15 10:25:18 +0800311 return '%s%sPATCH%s' % (git_prefix, prefix, version)