]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | # SPDX-License-Identifier: GPL-2.0+ |
0d24de9d SG |
2 | # Copyright (c) 2011 The Chromium OS Authors. |
3 | # | |
0d24de9d | 4 | |
6949f70c SA |
5 | from __future__ import print_function |
6 | ||
7 | import collections | |
31187255 | 8 | import itertools |
0d24de9d SG |
9 | import os |
10 | ||
bf776679 SG |
11 | from patman import get_maintainer |
12 | from patman import gitutil | |
13 | from patman import settings | |
14 | from patman import terminal | |
15 | from patman import tools | |
0d24de9d SG |
16 | |
17 | # Series-xxx tags that we understand | |
fe2f8d9e | 18 | valid_series = ['to', 'cc', 'version', 'changes', 'prefix', 'notes', 'name', |
082c119a | 19 | 'cover_cc', 'process_log', 'links', 'patchwork_url', 'postfix'] |
0d24de9d SG |
20 | |
21 | class 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 | |
f0b739f1 | 31 | allow_overwrite: Allow tags to overwrite an existing tag |
0d24de9d SG |
32 | """ |
33 | def __init__(self): | |
34 | self.cc = [] | |
35 | self.to = [] | |
fe2f8d9e | 36 | self.cover_cc = [] |
0d24de9d SG |
37 | self.commits = [] |
38 | self.cover = None | |
39 | self.notes = [] | |
40 | self.changes = {} | |
f0b739f1 | 41 | self.allow_overwrite = False |
0d24de9d | 42 | |
d94566a1 DA |
43 | # Written in MakeCcFile() |
44 | # key: name of patch file | |
45 | # value: list of email addresses | |
46 | self._generated_cc = {} | |
47 | ||
0d24de9d SG |
48 | # 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: ') | |
dffa42c3 SG |
62 | |
63 | Returns: | |
64 | String warning if something went wrong, else None | |
0d24de9d SG |
65 | """ |
66 | # If we already have it, then add to our list | |
fe2f8d9e | 67 | name = name.replace('-', '_') |
f0b739f1 | 68 | if name in self and not self.allow_overwrite: |
0d24de9d SG |
69 | 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: | |
070b781b AA |
79 | if name=="notes": |
80 | self[name] = [value] | |
81 | else: | |
82 | self[name] = value | |
0d24de9d | 83 | else: |
dffa42c3 | 84 | return ("In %s: line '%s': Unknown 'Series-%s': valid " |
ef0e9de8 | 85 | "options are %s" % (commit.hash, line, name, |
0d24de9d | 86 | ', '.join(valid_series))) |
dffa42c3 | 87 | return None |
0d24de9d SG |
88 | |
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 | """ | |
a3eeadfe | 97 | commit.check_tags() |
0d24de9d SG |
98 | 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 | """ | |
2181830f PT |
108 | to_set = set(gitutil.BuildEmailList(self.to)); |
109 | cc_set = set(gitutil.BuildEmailList(self.cc)); | |
110 | ||
0d24de9d | 111 | col = terminal.Color() |
a920a17b PB |
112 | 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.' % ( | |
0d24de9d | 115 | len(args), '' if len(args) == 1 else 'es', |
a920a17b | 116 | self.get('cover') and 'a ' or 'no ')) |
0d24de9d SG |
117 | |
118 | # TODO: Colour the patches according to whether they passed checks | |
119 | for upto in range(len(args)): | |
120 | commit = self.commits[upto] | |
a920a17b | 121 | print(col.Color(col.GREEN, ' %s' % args[upto])) |
d94566a1 | 122 | cc_list = list(self._generated_cc[commit.patch]) |
b644c66f | 123 | for email in sorted(set(cc_list) - to_set - cc_set): |
0d24de9d SG |
124 | if email == None: |
125 | email = col.Color(col.YELLOW, "<alias '%s' not found>" | |
126 | % tag) | |
127 | if email: | |
6f8abf76 | 128 | print(' Cc: ', email) |
0d24de9d | 129 | |
b644c66f | 130 | for item in sorted(to_set): |
a920a17b | 131 | print('To:\t ', item) |
b644c66f | 132 | for item in sorted(cc_set - to_set): |
a920a17b PB |
133 | print('Cc:\t ', item) |
134 | print('Version: ', self.get('version')) | |
135 | print('Prefix:\t ', self.get('prefix')) | |
082c119a | 136 | print('Postfix:\t ', self.get('postfix')) |
0d24de9d | 137 | if self.cover: |
a920a17b | 138 | print('Cover: %d lines' % len(self.cover)) |
fe2f8d9e SG |
139 | cover_cc = gitutil.BuildEmailList(self.get('cover_cc', '')) |
140 | all_ccs = itertools.chain(cover_cc, *self._generated_cc.values()) | |
b644c66f | 141 | for email in sorted(set(all_ccs) - to_set - cc_set): |
a920a17b | 142 | print(' Cc: ', email) |
0d24de9d | 143 | if cmd: |
a920a17b | 144 | print('Git command: %s' % cmd) |
0d24de9d SG |
145 | |
146 | def MakeChangeLog(self, commit): | |
147 | """Create a list of changes for each version. | |
148 | ||
149 | Return: | |
150 | The change log as a list of strings, one per line | |
151 | ||
27e97600 | 152 | Changes in v4: |
244e6f97 OS |
153 | - Jog the dial back closer to the widget |
154 | ||
27e97600 | 155 | Changes in v2: |
0d24de9d SG |
156 | - Fix the widget |
157 | - Jog the dial | |
158 | ||
b0436b94 SA |
159 | If there are no new changes in a patch, a note will be added |
160 | ||
161 | (no changes since v2) | |
162 | ||
163 | Changes in v2: | |
164 | - Fix the widget | |
165 | - Jog the dial | |
0d24de9d | 166 | """ |
6949f70c SA |
167 | # Collect changes from the series and this commit |
168 | changes = collections.defaultdict(list) | |
169 | for version, changelist in self.changes.items(): | |
170 | changes[version] += changelist | |
171 | if commit: | |
172 | for version, changelist in commit.changes.items(): | |
173 | changes[version] += [[commit, text] for text in changelist] | |
174 | ||
175 | versions = sorted(changes, reverse=True) | |
b0436b94 SA |
176 | newest_version = 1 |
177 | if 'version' in self: | |
178 | newest_version = max(newest_version, int(self.version)) | |
179 | if versions: | |
180 | newest_version = max(newest_version, versions[0]) | |
181 | ||
0d24de9d | 182 | final = [] |
645b271a SG |
183 | process_it = self.get('process_log', '').split(',') |
184 | process_it = [item.strip() for item in process_it] | |
0d24de9d | 185 | need_blank = False |
b0436b94 | 186 | for version in versions: |
0d24de9d | 187 | out = [] |
6949f70c | 188 | for this_commit, text in changes[version]: |
0d24de9d SG |
189 | if commit and this_commit != commit: |
190 | continue | |
645b271a SG |
191 | if 'uniq' not in process_it or text not in out: |
192 | out.append(text) | |
645b271a SG |
193 | if 'sort' in process_it: |
194 | out = sorted(out) | |
b0436b94 SA |
195 | have_changes = len(out) > 0 |
196 | line = 'Changes in v%d:' % version | |
27e97600 SG |
197 | if have_changes: |
198 | out.insert(0, line) | |
b0436b94 SA |
199 | if version < newest_version and len(final) == 0: |
200 | out.insert(0, '') | |
201 | out.insert(0, '(no changes since v%d)' % version) | |
202 | newest_version = 0 | |
203 | # Only add a new line if we output something | |
204 | if need_blank: | |
205 | out.insert(0, '') | |
206 | need_blank = False | |
27e97600 | 207 | final += out |
b0436b94 SA |
208 | need_blank = need_blank or have_changes |
209 | ||
210 | if len(final) > 0: | |
0d24de9d | 211 | final.append('') |
b0436b94 SA |
212 | elif newest_version != 1: |
213 | final = ['(no changes since v1)', ''] | |
0d24de9d SG |
214 | return final |
215 | ||
216 | def DoChecks(self): | |
217 | """Check that each version has a change log | |
218 | ||
219 | Print an error if something is wrong. | |
220 | """ | |
221 | col = terminal.Color() | |
222 | if self.get('version'): | |
223 | changes_copy = dict(self.changes) | |
d5f81d8a | 224 | for version in range(1, int(self.version) + 1): |
0d24de9d SG |
225 | if self.changes.get(version): |
226 | del changes_copy[version] | |
227 | else: | |
d5f81d8a OS |
228 | if version > 1: |
229 | str = 'Change log missing for v%d' % version | |
a920a17b | 230 | print(col.Color(col.RED, str)) |
0d24de9d SG |
231 | for version in changes_copy: |
232 | str = 'Change log for unknown version v%d' % version | |
a920a17b | 233 | print(col.Color(col.RED, str)) |
0d24de9d SG |
234 | elif self.changes: |
235 | str = 'Change log exists, but no version is set' | |
a920a17b | 236 | print(col.Color(col.RED, str)) |
0d24de9d | 237 | |
0fb560d9 | 238 | def MakeCcFile(self, process_tags, cover_fname, warn_on_error, |
4fb35029 | 239 | add_maintainers, limit): |
0d24de9d SG |
240 | """Make a cc file for us to use for per-commit Cc automation |
241 | ||
d94566a1 DA |
242 | Also stores in self._generated_cc to make ShowActions() faster. |
243 | ||
0d24de9d SG |
244 | Args: |
245 | process_tags: Process tags as if they were aliases | |
31187255 | 246 | cover_fname: If non-None the name of the cover letter. |
0fb560d9 SG |
247 | warn_on_error: True to print a warning when an alias fails to match, |
248 | False to ignore it. | |
1f487f85 SG |
249 | add_maintainers: Either: |
250 | True/False to call the get_maintainers to CC maintainers | |
251 | List of maintainers to include (for testing) | |
7d5b04e8 | 252 | limit: Limit the length of the Cc list (None if no limit) |
0d24de9d SG |
253 | Return: |
254 | Filename of temp file created | |
255 | """ | |
e11aa602 | 256 | col = terminal.Color() |
0d24de9d SG |
257 | # Look for commit tags (of the form 'xxx:' at the start of the subject) |
258 | fname = '/tmp/patman.%d' % os.getpid() | |
272cd85d | 259 | fd = open(fname, 'w', encoding='utf-8') |
31187255 | 260 | all_ccs = [] |
0d24de9d | 261 | for commit in self.commits: |
a44f4fb7 | 262 | cc = [] |
0d24de9d | 263 | if process_tags: |
a44f4fb7 | 264 | cc += gitutil.BuildEmailList(commit.tags, |
0fb560d9 | 265 | warn_on_error=warn_on_error) |
a44f4fb7 | 266 | cc += gitutil.BuildEmailList(commit.cc_list, |
0fb560d9 | 267 | warn_on_error=warn_on_error) |
a44f4fb7 SG |
268 | if type(add_maintainers) == type(cc): |
269 | cc += add_maintainers | |
1f487f85 | 270 | elif add_maintainers: |
156e6553 SG |
271 | dir_list = [os.path.join(gitutil.GetTopLevel(), 'scripts')] |
272 | cc += get_maintainer.GetMaintainer(dir_list, commit.patch) | |
e11aa602 CP |
273 | for x in set(cc) & set(settings.bounces): |
274 | print(col.Color(col.YELLOW, 'Skipping "%s"' % x)) | |
67637d4b | 275 | cc = list(set(cc) - set(settings.bounces)) |
4fb35029 CP |
276 | if limit is not None: |
277 | cc = cc[:limit] | |
a44f4fb7 | 278 | all_ccs += cc |
8ab452d5 | 279 | print(commit.patch, '\0'.join(sorted(set(cc))), file=fd) |
a44f4fb7 | 280 | self._generated_cc[commit.patch] = cc |
0d24de9d | 281 | |
31187255 | 282 | if cover_fname: |
fe2f8d9e | 283 | cover_cc = gitutil.BuildEmailList(self.get('cover_cc', '')) |
cf0ef931 SG |
284 | cover_cc = list(set(cover_cc + all_ccs)) |
285 | if limit is not None: | |
286 | cover_cc = cover_cc[:limit] | |
fc0056e8 | 287 | cc_list = '\0'.join([x for x in sorted(cover_cc)]) |
677dac23 | 288 | print(cover_fname, cc_list, file=fd) |
31187255 | 289 | |
0d24de9d SG |
290 | fd.close() |
291 | return fname | |
292 | ||
293 | def AddChange(self, version, commit, info): | |
294 | """Add a new change line to a version. | |
295 | ||
296 | This will later appear in the change log. | |
297 | ||
298 | Args: | |
299 | version: version number to add change list to | |
300 | info: change line for this version | |
301 | """ | |
302 | if not self.changes.get(version): | |
303 | self.changes[version] = [] | |
304 | self.changes[version].append([commit, info]) | |
305 | ||
306 | def GetPatchPrefix(self): | |
307 | """Get the patch version string | |
308 | ||
309 | Return: | |
310 | Patch string, like 'RFC PATCH v5' or just 'PATCH' | |
311 | """ | |
3871cd85 WJ |
312 | git_prefix = gitutil.GetDefaultSubjectPrefix() |
313 | if git_prefix: | |
12e5476d | 314 | git_prefix = '%s][' % git_prefix |
3871cd85 WJ |
315 | else: |
316 | git_prefix = '' | |
317 | ||
0d24de9d SG |
318 | version = '' |
319 | if self.get('version'): | |
320 | version = ' v%s' % self['version'] | |
321 | ||
322 | # Get patch name prefix | |
323 | prefix = '' | |
324 | if self.get('prefix'): | |
325 | prefix = '%s ' % self['prefix'] | |
082c119a SA |
326 | |
327 | postfix = '' | |
328 | if self.get('postfix'): | |
329 | postfix = ' %s' % self['postfix'] | |
330 | return '%s%sPATCH%s%s' % (git_prefix, prefix, postfix, version) |