Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 2 | # Copyright (c) 2011 The Chromium OS Authors. |
| 3 | # |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 4 | |
Doug Anderson | 3118725 | 2012-12-03 14:40:43 +0000 | [diff] [blame] | 5 | import itertools |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 6 | import os |
| 7 | |
Simon Glass | bf77667 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 8 | from patman import get_maintainer |
| 9 | from patman import gitutil |
| 10 | from patman import settings |
| 11 | from patman import terminal |
| 12 | from patman import tools |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 13 | |
| 14 | # Series-xxx tags that we understand |
Simon Glass | fe2f8d9 | 2013-03-20 16:43:00 +0000 | [diff] [blame] | 15 | valid_series = ['to', 'cc', 'version', 'changes', 'prefix', 'notes', 'name', |
Simon Glass | d9917b0 | 2015-08-22 18:28:01 -0600 | [diff] [blame] | 16 | 'cover_cc', 'process_log'] |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 17 | |
| 18 | class 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 Glass | f0b739f | 2013-05-02 14:46:02 +0000 | [diff] [blame] | 28 | allow_overwrite: Allow tags to overwrite an existing tag |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 29 | """ |
| 30 | def __init__(self): |
| 31 | self.cc = [] |
| 32 | self.to = [] |
Simon Glass | fe2f8d9 | 2013-03-20 16:43:00 +0000 | [diff] [blame] | 33 | self.cover_cc = [] |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 34 | self.commits = [] |
| 35 | self.cover = None |
| 36 | self.notes = [] |
| 37 | self.changes = {} |
Simon Glass | f0b739f | 2013-05-02 14:46:02 +0000 | [diff] [blame] | 38 | self.allow_overwrite = False |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 39 | |
Doug Anderson | d94566a | 2012-12-03 14:40:42 +0000 | [diff] [blame] | 40 | # Written in MakeCcFile() |
| 41 | # key: name of patch file |
| 42 | # value: list of email addresses |
| 43 | self._generated_cc = {} |
| 44 | |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 45 | # 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 Glass | fe2f8d9 | 2013-03-20 16:43:00 +0000 | [diff] [blame] | 61 | name = name.replace('-', '_') |
Simon Glass | f0b739f | 2013-05-02 14:46:02 +0000 | [diff] [blame] | 62 | if name in self and not self.allow_overwrite: |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 63 | 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 ARIBAUD | 070b781 | 2016-02-02 10:24:53 +0100 | [diff] [blame] | 73 | if name=="notes": |
| 74 | self[name] = [value] |
| 75 | else: |
| 76 | self[name] = value |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 77 | else: |
| 78 | raise ValueError("In %s: line '%s': Unknown 'Series-%s': valid " |
Simon Glass | ef0e9de | 2012-09-27 15:06:02 +0000 | [diff] [blame] | 79 | "options are %s" % (commit.hash, line, name, |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 80 | ', '.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 Tyser | 2181830 | 2015-01-26 11:42:21 -0600 | [diff] [blame] | 101 | to_set = set(gitutil.BuildEmailList(self.to)); |
| 102 | cc_set = set(gitutil.BuildEmailList(self.cc)); |
| 103 | |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 104 | col = terminal.Color() |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 105 | 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 Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 108 | len(args), '' if len(args) == 1 else 'es', |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 109 | self.get('cover') and 'a ' or 'no ')) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 110 | |
| 111 | # TODO: Colour the patches according to whether they passed checks |
| 112 | for upto in range(len(args)): |
| 113 | commit = self.commits[upto] |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 114 | print(col.Color(col.GREEN, ' %s' % args[upto])) |
Doug Anderson | d94566a | 2012-12-03 14:40:42 +0000 | [diff] [blame] | 115 | cc_list = list(self._generated_cc[commit.patch]) |
Simon Glass | b644c66 | 2019-05-14 15:53:51 -0600 | [diff] [blame] | 116 | for email in sorted(set(cc_list) - to_set - cc_set): |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 117 | if email == None: |
| 118 | email = col.Color(col.YELLOW, "<alias '%s' not found>" |
| 119 | % tag) |
| 120 | if email: |
Simon Glass | 6f8abf7 | 2017-05-29 15:31:23 -0600 | [diff] [blame] | 121 | print(' Cc: ', email) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 122 | print |
Simon Glass | b644c66 | 2019-05-14 15:53:51 -0600 | [diff] [blame] | 123 | for item in sorted(to_set): |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 124 | print('To:\t ', item) |
Simon Glass | b644c66 | 2019-05-14 15:53:51 -0600 | [diff] [blame] | 125 | for item in sorted(cc_set - to_set): |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 126 | print('Cc:\t ', item) |
| 127 | print('Version: ', self.get('version')) |
| 128 | print('Prefix:\t ', self.get('prefix')) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 129 | if self.cover: |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 130 | print('Cover: %d lines' % len(self.cover)) |
Simon Glass | fe2f8d9 | 2013-03-20 16:43:00 +0000 | [diff] [blame] | 131 | cover_cc = gitutil.BuildEmailList(self.get('cover_cc', '')) |
| 132 | all_ccs = itertools.chain(cover_cc, *self._generated_cc.values()) |
Simon Glass | b644c66 | 2019-05-14 15:53:51 -0600 | [diff] [blame] | 133 | for email in sorted(set(all_ccs) - to_set - cc_set): |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 134 | print(' Cc: ', email) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 135 | if cmd: |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 136 | print('Git command: %s' % cmd) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 137 | |
| 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 Glass | 27e9760 | 2012-10-30 06:15:16 +0000 | [diff] [blame] | 144 | Changes in v4: |
Otavio Salvador | 244e6f9 | 2012-08-18 07:46:04 +0000 | [diff] [blame] | 145 | - Jog the dial back closer to the widget |
| 146 | |
Simon Glass | 27e9760 | 2012-10-30 06:15:16 +0000 | [diff] [blame] | 147 | Changes in v3: None |
| 148 | Changes in v2: |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 149 | - Fix the widget |
| 150 | - Jog the dial |
| 151 | |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 152 | etc. |
| 153 | """ |
| 154 | final = [] |
Simon Glass | 645b271 | 2013-03-26 13:09:44 +0000 | [diff] [blame] | 155 | process_it = self.get('process_log', '').split(',') |
| 156 | process_it = [item.strip() for item in process_it] |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 157 | need_blank = False |
Otavio Salvador | 244e6f9 | 2012-08-18 07:46:04 +0000 | [diff] [blame] | 158 | for change in sorted(self.changes, reverse=True): |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 159 | out = [] |
| 160 | for this_commit, text in self.changes[change]: |
| 161 | if commit and this_commit != commit: |
| 162 | continue |
Simon Glass | 645b271 | 2013-03-26 13:09:44 +0000 | [diff] [blame] | 163 | if 'uniq' not in process_it or text not in out: |
| 164 | out.append(text) |
Simon Glass | 27e9760 | 2012-10-30 06:15:16 +0000 | [diff] [blame] | 165 | line = 'Changes in v%d:' % change |
| 166 | have_changes = len(out) > 0 |
Simon Glass | 645b271 | 2013-03-26 13:09:44 +0000 | [diff] [blame] | 167 | if 'sort' in process_it: |
| 168 | out = sorted(out) |
Simon Glass | 27e9760 | 2012-10-30 06:15:16 +0000 | [diff] [blame] | 169 | if have_changes: |
| 170 | out.insert(0, line) |
| 171 | else: |
| 172 | out = [line + ' None'] |
| 173 | if need_blank: |
| 174 | out.insert(0, '') |
| 175 | final += out |
| 176 | need_blank = have_changes |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 177 | if self.changes: |
| 178 | final.append('') |
| 179 | return final |
| 180 | |
| 181 | def DoChecks(self): |
| 182 | """Check that each version has a change log |
| 183 | |
| 184 | Print an error if something is wrong. |
| 185 | """ |
| 186 | col = terminal.Color() |
| 187 | if self.get('version'): |
| 188 | changes_copy = dict(self.changes) |
Otavio Salvador | d5f81d8 | 2012-08-13 10:08:22 +0000 | [diff] [blame] | 189 | for version in range(1, int(self.version) + 1): |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 190 | if self.changes.get(version): |
| 191 | del changes_copy[version] |
| 192 | else: |
Otavio Salvador | d5f81d8 | 2012-08-13 10:08:22 +0000 | [diff] [blame] | 193 | if version > 1: |
| 194 | str = 'Change log missing for v%d' % version |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 195 | print(col.Color(col.RED, str)) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 196 | for version in changes_copy: |
| 197 | str = 'Change log for unknown version v%d' % version |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 198 | print(col.Color(col.RED, str)) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 199 | elif self.changes: |
| 200 | str = 'Change log exists, but no version is set' |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 201 | print(col.Color(col.RED, str)) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 202 | |
Simon Glass | 983a274 | 2014-09-14 20:23:17 -0600 | [diff] [blame] | 203 | def MakeCcFile(self, process_tags, cover_fname, raise_on_error, |
Chris Packham | 4fb3502 | 2018-06-07 20:45:06 +1200 | [diff] [blame] | 204 | add_maintainers, limit): |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 205 | """Make a cc file for us to use for per-commit Cc automation |
| 206 | |
Doug Anderson | d94566a | 2012-12-03 14:40:42 +0000 | [diff] [blame] | 207 | Also stores in self._generated_cc to make ShowActions() faster. |
| 208 | |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 209 | Args: |
| 210 | process_tags: Process tags as if they were aliases |
Doug Anderson | 3118725 | 2012-12-03 14:40:43 +0000 | [diff] [blame] | 211 | cover_fname: If non-None the name of the cover letter. |
Simon Glass | a1318f7 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 212 | raise_on_error: True to raise an error when an alias fails to match, |
| 213 | False to just print a message. |
Simon Glass | 1f487f8 | 2017-05-29 15:31:29 -0600 | [diff] [blame] | 214 | add_maintainers: Either: |
| 215 | True/False to call the get_maintainers to CC maintainers |
| 216 | List of maintainers to include (for testing) |
Chris Packham | 4fb3502 | 2018-06-07 20:45:06 +1200 | [diff] [blame] | 217 | limit: Limit the length of the Cc list |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 218 | Return: |
| 219 | Filename of temp file created |
| 220 | """ |
Chris Packham | e11aa60 | 2017-09-01 20:57:53 +1200 | [diff] [blame] | 221 | col = terminal.Color() |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 222 | # Look for commit tags (of the form 'xxx:' at the start of the subject) |
| 223 | fname = '/tmp/patman.%d' % os.getpid() |
Simon Glass | 272cd85 | 2019-10-31 07:42:51 -0600 | [diff] [blame] | 224 | fd = open(fname, 'w', encoding='utf-8') |
Doug Anderson | 3118725 | 2012-12-03 14:40:43 +0000 | [diff] [blame] | 225 | all_ccs = [] |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 226 | for commit in self.commits: |
Simon Glass | a44f4fb | 2017-05-29 15:31:30 -0600 | [diff] [blame] | 227 | cc = [] |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 228 | if process_tags: |
Simon Glass | a44f4fb | 2017-05-29 15:31:30 -0600 | [diff] [blame] | 229 | cc += gitutil.BuildEmailList(commit.tags, |
Simon Glass | a1318f7 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 230 | raise_on_error=raise_on_error) |
Simon Glass | a44f4fb | 2017-05-29 15:31:30 -0600 | [diff] [blame] | 231 | cc += gitutil.BuildEmailList(commit.cc_list, |
Simon Glass | a1318f7 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 232 | raise_on_error=raise_on_error) |
Simon Glass | a44f4fb | 2017-05-29 15:31:30 -0600 | [diff] [blame] | 233 | if type(add_maintainers) == type(cc): |
| 234 | cc += add_maintainers |
Simon Glass | 1f487f8 | 2017-05-29 15:31:29 -0600 | [diff] [blame] | 235 | elif add_maintainers: |
Simon Glass | a44f4fb | 2017-05-29 15:31:30 -0600 | [diff] [blame] | 236 | cc += get_maintainer.GetMaintainer(commit.patch) |
Chris Packham | e11aa60 | 2017-09-01 20:57:53 +1200 | [diff] [blame] | 237 | for x in set(cc) & set(settings.bounces): |
| 238 | print(col.Color(col.YELLOW, 'Skipping "%s"' % x)) |
| 239 | cc = set(cc) - set(settings.bounces) |
Simon Glass | f6a6aaf | 2019-05-14 15:53:54 -0600 | [diff] [blame] | 240 | cc = [tools.FromUnicode(m) for m in cc] |
Chris Packham | 4fb3502 | 2018-06-07 20:45:06 +1200 | [diff] [blame] | 241 | if limit is not None: |
| 242 | cc = cc[:limit] |
Simon Glass | a44f4fb | 2017-05-29 15:31:30 -0600 | [diff] [blame] | 243 | all_ccs += cc |
Dmitry Torokhov | 8ab452d | 2019-10-21 20:09:56 -0700 | [diff] [blame] | 244 | print(commit.patch, '\0'.join(sorted(set(cc))), file=fd) |
Simon Glass | a44f4fb | 2017-05-29 15:31:30 -0600 | [diff] [blame] | 245 | self._generated_cc[commit.patch] = cc |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 246 | |
Doug Anderson | 3118725 | 2012-12-03 14:40:43 +0000 | [diff] [blame] | 247 | if cover_fname: |
Simon Glass | fe2f8d9 | 2013-03-20 16:43:00 +0000 | [diff] [blame] | 248 | cover_cc = gitutil.BuildEmailList(self.get('cover_cc', '')) |
Simon Glass | f6a6aaf | 2019-05-14 15:53:54 -0600 | [diff] [blame] | 249 | cover_cc = [tools.FromUnicode(m) for m in cover_cc] |
Simon Glass | cf0ef93 | 2020-02-27 18:49:23 -0700 | [diff] [blame] | 250 | cover_cc = list(set(cover_cc + all_ccs)) |
| 251 | if limit is not None: |
| 252 | cover_cc = cover_cc[:limit] |
| 253 | cc_list = '\0'.join([tools.ToUnicode(x) for x in sorted(cover_cc)]) |
Robert Beckett | 677dac2 | 2019-11-13 18:39:45 +0000 | [diff] [blame] | 254 | print(cover_fname, cc_list, file=fd) |
Doug Anderson | 3118725 | 2012-12-03 14:40:43 +0000 | [diff] [blame] | 255 | |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 256 | fd.close() |
| 257 | return fname |
| 258 | |
| 259 | def AddChange(self, version, commit, info): |
| 260 | """Add a new change line to a version. |
| 261 | |
| 262 | This will later appear in the change log. |
| 263 | |
| 264 | Args: |
| 265 | version: version number to add change list to |
| 266 | info: change line for this version |
| 267 | """ |
| 268 | if not self.changes.get(version): |
| 269 | self.changes[version] = [] |
| 270 | self.changes[version].append([commit, info]) |
| 271 | |
| 272 | def GetPatchPrefix(self): |
| 273 | """Get the patch version string |
| 274 | |
| 275 | Return: |
| 276 | Patch string, like 'RFC PATCH v5' or just 'PATCH' |
| 277 | """ |
Wu, Josh | 3871cd8 | 2015-04-15 10:25:18 +0800 | [diff] [blame] | 278 | git_prefix = gitutil.GetDefaultSubjectPrefix() |
| 279 | if git_prefix: |
Paul Burton | 12e5476 | 2016-09-27 16:03:49 +0100 | [diff] [blame] | 280 | git_prefix = '%s][' % git_prefix |
Wu, Josh | 3871cd8 | 2015-04-15 10:25:18 +0800 | [diff] [blame] | 281 | else: |
| 282 | git_prefix = '' |
| 283 | |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 284 | version = '' |
| 285 | if self.get('version'): |
| 286 | version = ' v%s' % self['version'] |
| 287 | |
| 288 | # Get patch name prefix |
| 289 | prefix = '' |
| 290 | if self.get('prefix'): |
| 291 | prefix = '%s ' % self['prefix'] |
Wu, Josh | 3871cd8 | 2015-04-15 10:25:18 +0800 | [diff] [blame] | 292 | return '%s%sPATCH%s' % (git_prefix, prefix, version) |