]>
Commit | Line | Data |
---|---|---|
0d24de9d SG |
1 | # Copyright (c) 2011 The Chromium OS Authors. |
2 | # | |
1a459660 | 3 | # SPDX-License-Identifier: GPL-2.0+ |
0d24de9d SG |
4 | # |
5 | ||
a920a17b PB |
6 | from __future__ import print_function |
7 | ||
31187255 | 8 | import itertools |
0d24de9d SG |
9 | import os |
10 | ||
21a19d70 | 11 | import get_maintainer |
0d24de9d SG |
12 | import gitutil |
13 | import terminal | |
14 | ||
15 | # Series-xxx tags that we understand | |
fe2f8d9e | 16 | valid_series = ['to', 'cc', 'version', 'changes', 'prefix', 'notes', 'name', |
d9917b0b | 17 | 'cover_cc', 'process_log'] |
0d24de9d SG |
18 | |
19 | class Series(dict): | |
20 | """Holds information about a patch series, including all tags. | |
21 | ||
22 | Vars: | |
23 | cc: List of aliases/emails to Cc all patches to | |
24 | commits: List of Commit objects, one for each patch | |
25 | cover: List of lines in the cover letter | |
26 | notes: List of lines in the notes | |
27 | changes: (dict) List of changes for each version, The key is | |
28 | the integer version number | |
f0b739f1 | 29 | allow_overwrite: Allow tags to overwrite an existing tag |
0d24de9d SG |
30 | """ |
31 | def __init__(self): | |
32 | self.cc = [] | |
33 | self.to = [] | |
fe2f8d9e | 34 | self.cover_cc = [] |
0d24de9d SG |
35 | self.commits = [] |
36 | self.cover = None | |
37 | self.notes = [] | |
38 | self.changes = {} | |
f0b739f1 | 39 | self.allow_overwrite = False |
0d24de9d | 40 | |
d94566a1 DA |
41 | # Written in MakeCcFile() |
42 | # key: name of patch file | |
43 | # value: list of email addresses | |
44 | self._generated_cc = {} | |
45 | ||
0d24de9d SG |
46 | # These make us more like a dictionary |
47 | def __setattr__(self, name, value): | |
48 | self[name] = value | |
49 | ||
50 | def __getattr__(self, name): | |
51 | return self[name] | |
52 | ||
53 | def AddTag(self, commit, line, name, value): | |
54 | """Add a new Series-xxx tag along with its value. | |
55 | ||
56 | Args: | |
57 | line: Source line containing tag (useful for debug/error messages) | |
58 | name: Tag name (part after 'Series-') | |
59 | value: Tag value (part after 'Series-xxx: ') | |
60 | """ | |
61 | # If we already have it, then add to our list | |
fe2f8d9e | 62 | name = name.replace('-', '_') |
f0b739f1 | 63 | if name in self and not self.allow_overwrite: |
0d24de9d SG |
64 | values = value.split(',') |
65 | values = [str.strip() for str in values] | |
66 | if type(self[name]) != type([]): | |
67 | raise ValueError("In %s: line '%s': Cannot add another value " | |
68 | "'%s' to series '%s'" % | |
69 | (commit.hash, line, values, self[name])) | |
70 | self[name] += values | |
71 | ||
72 | # Otherwise just set the value | |
73 | elif name in valid_series: | |
070b781b AA |
74 | if name=="notes": |
75 | self[name] = [value] | |
76 | else: | |
77 | self[name] = value | |
0d24de9d SG |
78 | else: |
79 | raise ValueError("In %s: line '%s': Unknown 'Series-%s': valid " | |
ef0e9de8 | 80 | "options are %s" % (commit.hash, line, name, |
0d24de9d SG |
81 | ', '.join(valid_series))) |
82 | ||
83 | def AddCommit(self, commit): | |
84 | """Add a commit into our list of commits | |
85 | ||
86 | We create a list of tags in the commit subject also. | |
87 | ||
88 | Args: | |
89 | commit: Commit object to add | |
90 | """ | |
91 | commit.CheckTags() | |
92 | self.commits.append(commit) | |
93 | ||
94 | def ShowActions(self, args, cmd, process_tags): | |
95 | """Show what actions we will/would perform | |
96 | ||
97 | Args: | |
98 | args: List of patch files we created | |
99 | cmd: The git command we would have run | |
100 | process_tags: Process tags as if they were aliases | |
101 | """ | |
2181830f PT |
102 | to_set = set(gitutil.BuildEmailList(self.to)); |
103 | cc_set = set(gitutil.BuildEmailList(self.cc)); | |
104 | ||
0d24de9d | 105 | col = terminal.Color() |
a920a17b PB |
106 | print('Dry run, so not doing much. But I would do this:') |
107 | print() | |
108 | print('Send a total of %d patch%s with %scover letter.' % ( | |
0d24de9d | 109 | len(args), '' if len(args) == 1 else 'es', |
a920a17b | 110 | self.get('cover') and 'a ' or 'no ')) |
0d24de9d SG |
111 | |
112 | # TODO: Colour the patches according to whether they passed checks | |
113 | for upto in range(len(args)): | |
114 | commit = self.commits[upto] | |
a920a17b | 115 | print(col.Color(col.GREEN, ' %s' % args[upto])) |
d94566a1 | 116 | cc_list = list(self._generated_cc[commit.patch]) |
2181830f | 117 | for email in set(cc_list) - to_set - cc_set: |
0d24de9d SG |
118 | if email == None: |
119 | email = col.Color(col.YELLOW, "<alias '%s' not found>" | |
120 | % tag) | |
121 | if email: | |
6f8abf76 | 122 | print(' Cc: ', email) |
0d24de9d | 123 | |
2181830f | 124 | for item in to_set: |
a920a17b | 125 | print('To:\t ', item) |
2181830f | 126 | for item in cc_set - to_set: |
a920a17b PB |
127 | print('Cc:\t ', item) |
128 | print('Version: ', self.get('version')) | |
129 | print('Prefix:\t ', self.get('prefix')) | |
0d24de9d | 130 | if self.cover: |
a920a17b | 131 | print('Cover: %d lines' % len(self.cover)) |
fe2f8d9e SG |
132 | cover_cc = gitutil.BuildEmailList(self.get('cover_cc', '')) |
133 | all_ccs = itertools.chain(cover_cc, *self._generated_cc.values()) | |
2181830f | 134 | for email in set(all_ccs) - to_set - cc_set: |
a920a17b | 135 | print(' Cc: ', email) |
0d24de9d | 136 | if cmd: |
a920a17b | 137 | print('Git command: %s' % cmd) |
0d24de9d SG |
138 | |
139 | def MakeChangeLog(self, commit): | |
140 | """Create a list of changes for each version. | |
141 | ||
142 | Return: | |
143 | The change log as a list of strings, one per line | |
144 | ||
27e97600 | 145 | Changes in v4: |
244e6f97 OS |
146 | - Jog the dial back closer to the widget |
147 | ||
27e97600 SG |
148 | Changes in v3: None |
149 | Changes in v2: | |
0d24de9d SG |
150 | - Fix the widget |
151 | - Jog the dial | |
152 | ||
0d24de9d SG |
153 | etc. |
154 | """ | |
155 | final = [] | |
645b271a SG |
156 | process_it = self.get('process_log', '').split(',') |
157 | process_it = [item.strip() for item in process_it] | |
0d24de9d | 158 | need_blank = False |
244e6f97 | 159 | for change in sorted(self.changes, reverse=True): |
0d24de9d SG |
160 | out = [] |
161 | for this_commit, text in self.changes[change]: | |
162 | if commit and this_commit != commit: | |
163 | continue | |
645b271a SG |
164 | if 'uniq' not in process_it or text not in out: |
165 | out.append(text) | |
27e97600 SG |
166 | line = 'Changes in v%d:' % change |
167 | have_changes = len(out) > 0 | |
645b271a SG |
168 | if 'sort' in process_it: |
169 | out = sorted(out) | |
27e97600 SG |
170 | if have_changes: |
171 | out.insert(0, line) | |
172 | else: | |
173 | out = [line + ' None'] | |
174 | if need_blank: | |
175 | out.insert(0, '') | |
176 | final += out | |
177 | need_blank = have_changes | |
0d24de9d SG |
178 | if self.changes: |
179 | final.append('') | |
180 | return final | |
181 | ||
182 | def DoChecks(self): | |
183 | """Check that each version has a change log | |
184 | ||
185 | Print an error if something is wrong. | |
186 | """ | |
187 | col = terminal.Color() | |
188 | if self.get('version'): | |
189 | changes_copy = dict(self.changes) | |
d5f81d8a | 190 | for version in range(1, int(self.version) + 1): |
0d24de9d SG |
191 | if self.changes.get(version): |
192 | del changes_copy[version] | |
193 | else: | |
d5f81d8a OS |
194 | if version > 1: |
195 | str = 'Change log missing for v%d' % version | |
a920a17b | 196 | print(col.Color(col.RED, str)) |
0d24de9d SG |
197 | for version in changes_copy: |
198 | str = 'Change log for unknown version v%d' % version | |
a920a17b | 199 | print(col.Color(col.RED, str)) |
0d24de9d SG |
200 | elif self.changes: |
201 | str = 'Change log exists, but no version is set' | |
a920a17b | 202 | print(col.Color(col.RED, str)) |
0d24de9d | 203 | |
983a2749 SG |
204 | def MakeCcFile(self, process_tags, cover_fname, raise_on_error, |
205 | add_maintainers): | |
0d24de9d SG |
206 | """Make a cc file for us to use for per-commit Cc automation |
207 | ||
d94566a1 DA |
208 | Also stores in self._generated_cc to make ShowActions() faster. |
209 | ||
0d24de9d SG |
210 | Args: |
211 | process_tags: Process tags as if they were aliases | |
31187255 | 212 | cover_fname: If non-None the name of the cover letter. |
a1318f7c SG |
213 | raise_on_error: True to raise an error when an alias fails to match, |
214 | False to just print a message. | |
1f487f85 SG |
215 | add_maintainers: Either: |
216 | True/False to call the get_maintainers to CC maintainers | |
217 | List of maintainers to include (for testing) | |
0d24de9d SG |
218 | Return: |
219 | Filename of temp file created | |
220 | """ | |
221 | # Look for commit tags (of the form 'xxx:' at the start of the subject) | |
222 | fname = '/tmp/patman.%d' % os.getpid() | |
223 | fd = open(fname, 'w') | |
31187255 | 224 | all_ccs = [] |
0d24de9d | 225 | for commit in self.commits: |
a44f4fb7 | 226 | cc = [] |
0d24de9d | 227 | if process_tags: |
a44f4fb7 | 228 | cc += gitutil.BuildEmailList(commit.tags, |
a1318f7c | 229 | raise_on_error=raise_on_error) |
a44f4fb7 | 230 | cc += gitutil.BuildEmailList(commit.cc_list, |
a1318f7c | 231 | raise_on_error=raise_on_error) |
a44f4fb7 SG |
232 | if type(add_maintainers) == type(cc): |
233 | cc += add_maintainers | |
1f487f85 | 234 | elif add_maintainers: |
a44f4fb7 SG |
235 | cc += get_maintainer.GetMaintainer(commit.patch) |
236 | cc = [m.encode('utf-8') if type(m) != str else m for m in cc] | |
237 | all_ccs += cc | |
238 | print(commit.patch, ', '.join(set(cc)), file=fd) | |
239 | self._generated_cc[commit.patch] = cc | |
0d24de9d | 240 | |
31187255 | 241 | if cover_fname: |
fe2f8d9e | 242 | cover_cc = gitutil.BuildEmailList(self.get('cover_cc', '')) |
6f8abf76 SG |
243 | cover_cc = [m.encode('utf-8') if type(m) != str else m |
244 | for m in cover_cc] | |
245 | cc_list = ', '.join([x.decode('utf-8') | |
246 | for x in set(cover_cc + all_ccs)]) | |
f11a0af7 | 247 | print(cover_fname, cc_list.encode('utf-8'), file=fd) |
31187255 | 248 | |
0d24de9d SG |
249 | fd.close() |
250 | return fname | |
251 | ||
252 | def AddChange(self, version, commit, info): | |
253 | """Add a new change line to a version. | |
254 | ||
255 | This will later appear in the change log. | |
256 | ||
257 | Args: | |
258 | version: version number to add change list to | |
259 | info: change line for this version | |
260 | """ | |
261 | if not self.changes.get(version): | |
262 | self.changes[version] = [] | |
263 | self.changes[version].append([commit, info]) | |
264 | ||
265 | def GetPatchPrefix(self): | |
266 | """Get the patch version string | |
267 | ||
268 | Return: | |
269 | Patch string, like 'RFC PATCH v5' or just 'PATCH' | |
270 | """ | |
3871cd85 WJ |
271 | git_prefix = gitutil.GetDefaultSubjectPrefix() |
272 | if git_prefix: | |
12e5476d | 273 | git_prefix = '%s][' % git_prefix |
3871cd85 WJ |
274 | else: |
275 | git_prefix = '' | |
276 | ||
0d24de9d SG |
277 | version = '' |
278 | if self.get('version'): | |
279 | version = ' v%s' % self['version'] | |
280 | ||
281 | # Get patch name prefix | |
282 | prefix = '' | |
283 | if self.get('prefix'): | |
284 | prefix = '%s ' % self['prefix'] | |
3871cd85 | 285 | return '%s%sPATCH%s' % (git_prefix, prefix, version) |