Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 2 | # Copyright (c) 2011 The Chromium OS Authors. |
| 3 | # |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 4 | |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 5 | from __future__ import print_function |
| 6 | |
Doug Anderson | 05416af | 2012-12-03 14:40:43 +0000 | [diff] [blame] | 7 | import itertools |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 8 | import os |
| 9 | |
Doug Anderson | c2c84bc | 2012-12-03 14:43:16 +0000 | [diff] [blame] | 10 | import get_maintainer |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 11 | import gitutil |
Chris Packham | e8d2a12 | 2017-09-01 20:57:53 +1200 | [diff] [blame] | 12 | import settings |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 13 | import terminal |
Simon Glass | 8bb7a7a | 2019-05-14 15:53:50 -0600 | [diff] [blame] | 14 | import tools |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 15 | |
| 16 | # Series-xxx tags that we understand |
Simon Glass | c72f3da | 2013-03-20 16:43:00 +0000 | [diff] [blame] | 17 | valid_series = ['to', 'cc', 'version', 'changes', 'prefix', 'notes', 'name', |
Simon Glass | 420d4ee | 2015-08-22 18:28:01 -0600 | [diff] [blame] | 18 | 'cover_cc', 'process_log'] |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 19 | |
| 20 | class Series(dict): |
| 21 | """Holds information about a patch series, including all tags. |
| 22 | |
| 23 | Vars: |
| 24 | cc: List of aliases/emails to Cc all patches to |
| 25 | commits: List of Commit objects, one for each patch |
| 26 | cover: List of lines in the cover letter |
| 27 | notes: List of lines in the notes |
| 28 | changes: (dict) List of changes for each version, The key is |
| 29 | the integer version number |
Simon Glass | 29c5abc | 2013-05-02 14:46:02 +0000 | [diff] [blame] | 30 | allow_overwrite: Allow tags to overwrite an existing tag |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 31 | """ |
| 32 | def __init__(self): |
| 33 | self.cc = [] |
| 34 | self.to = [] |
Simon Glass | c72f3da | 2013-03-20 16:43:00 +0000 | [diff] [blame] | 35 | self.cover_cc = [] |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 36 | self.commits = [] |
| 37 | self.cover = None |
| 38 | self.notes = [] |
| 39 | self.changes = {} |
Simon Glass | 29c5abc | 2013-05-02 14:46:02 +0000 | [diff] [blame] | 40 | self.allow_overwrite = False |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 41 | |
Doug Anderson | 507e8e8 | 2012-12-03 14:40:42 +0000 | [diff] [blame] | 42 | # Written in MakeCcFile() |
| 43 | # key: name of patch file |
| 44 | # value: list of email addresses |
| 45 | self._generated_cc = {} |
| 46 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 47 | # These make us more like a dictionary |
| 48 | def __setattr__(self, name, value): |
| 49 | self[name] = value |
| 50 | |
| 51 | def __getattr__(self, name): |
| 52 | return self[name] |
| 53 | |
| 54 | def AddTag(self, commit, line, name, value): |
| 55 | """Add a new Series-xxx tag along with its value. |
| 56 | |
| 57 | Args: |
| 58 | line: Source line containing tag (useful for debug/error messages) |
| 59 | name: Tag name (part after 'Series-') |
| 60 | value: Tag value (part after 'Series-xxx: ') |
| 61 | """ |
| 62 | # If we already have it, then add to our list |
Simon Glass | c72f3da | 2013-03-20 16:43:00 +0000 | [diff] [blame] | 63 | name = name.replace('-', '_') |
Simon Glass | 29c5abc | 2013-05-02 14:46:02 +0000 | [diff] [blame] | 64 | if name in self and not self.allow_overwrite: |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 65 | values = value.split(',') |
| 66 | values = [str.strip() for str in values] |
| 67 | if type(self[name]) != type([]): |
| 68 | raise ValueError("In %s: line '%s': Cannot add another value " |
| 69 | "'%s' to series '%s'" % |
| 70 | (commit.hash, line, values, self[name])) |
| 71 | self[name] += values |
| 72 | |
| 73 | # Otherwise just set the value |
| 74 | elif name in valid_series: |
Albert ARIBAUD | 77ec8bc | 2016-02-02 10:24:53 +0100 | [diff] [blame] | 75 | if name=="notes": |
| 76 | self[name] = [value] |
| 77 | else: |
| 78 | self[name] = value |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 79 | else: |
| 80 | raise ValueError("In %s: line '%s': Unknown 'Series-%s': valid " |
Simon Glass | e7ecd3f | 2012-09-27 15:06:02 +0000 | [diff] [blame] | 81 | "options are %s" % (commit.hash, line, name, |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 82 | ', '.join(valid_series))) |
| 83 | |
| 84 | def AddCommit(self, commit): |
| 85 | """Add a commit into our list of commits |
| 86 | |
| 87 | We create a list of tags in the commit subject also. |
| 88 | |
| 89 | Args: |
| 90 | commit: Commit object to add |
| 91 | """ |
| 92 | commit.CheckTags() |
| 93 | self.commits.append(commit) |
| 94 | |
| 95 | def ShowActions(self, args, cmd, process_tags): |
| 96 | """Show what actions we will/would perform |
| 97 | |
| 98 | Args: |
| 99 | args: List of patch files we created |
| 100 | cmd: The git command we would have run |
| 101 | process_tags: Process tags as if they were aliases |
| 102 | """ |
Peter Tyser | c6af802 | 2015-01-26 11:42:21 -0600 | [diff] [blame] | 103 | to_set = set(gitutil.BuildEmailList(self.to)); |
| 104 | cc_set = set(gitutil.BuildEmailList(self.cc)); |
| 105 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 106 | col = terminal.Color() |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 107 | print('Dry run, so not doing much. But I would do this:') |
| 108 | print() |
| 109 | print('Send a total of %d patch%s with %scover letter.' % ( |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 110 | len(args), '' if len(args) == 1 else 'es', |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 111 | self.get('cover') and 'a ' or 'no ')) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 112 | |
| 113 | # TODO: Colour the patches according to whether they passed checks |
| 114 | for upto in range(len(args)): |
| 115 | commit = self.commits[upto] |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 116 | print(col.Color(col.GREEN, ' %s' % args[upto])) |
Doug Anderson | 507e8e8 | 2012-12-03 14:40:42 +0000 | [diff] [blame] | 117 | cc_list = list(self._generated_cc[commit.patch]) |
Simon Glass | 3ab178f | 2019-05-14 15:53:51 -0600 | [diff] [blame] | 118 | for email in sorted(set(cc_list) - to_set - cc_set): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 119 | if email == None: |
| 120 | email = col.Color(col.YELLOW, "<alias '%s' not found>" |
| 121 | % tag) |
| 122 | if email: |
Simon Glass | 22c5834 | 2017-05-29 15:31:23 -0600 | [diff] [blame] | 123 | print(' Cc: ', email) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 124 | print |
Simon Glass | 3ab178f | 2019-05-14 15:53:51 -0600 | [diff] [blame] | 125 | for item in sorted(to_set): |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 126 | print('To:\t ', item) |
Simon Glass | 3ab178f | 2019-05-14 15:53:51 -0600 | [diff] [blame] | 127 | for item in sorted(cc_set - to_set): |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 128 | print('Cc:\t ', item) |
| 129 | print('Version: ', self.get('version')) |
| 130 | print('Prefix:\t ', self.get('prefix')) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 131 | if self.cover: |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 132 | print('Cover: %d lines' % len(self.cover)) |
Simon Glass | c72f3da | 2013-03-20 16:43:00 +0000 | [diff] [blame] | 133 | cover_cc = gitutil.BuildEmailList(self.get('cover_cc', '')) |
| 134 | all_ccs = itertools.chain(cover_cc, *self._generated_cc.values()) |
Simon Glass | 3ab178f | 2019-05-14 15:53:51 -0600 | [diff] [blame] | 135 | for email in sorted(set(all_ccs) - to_set - cc_set): |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 136 | print(' Cc: ', email) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 137 | if cmd: |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 138 | print('Git command: %s' % cmd) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 139 | |
| 140 | def MakeChangeLog(self, commit): |
| 141 | """Create a list of changes for each version. |
| 142 | |
| 143 | Return: |
| 144 | The change log as a list of strings, one per line |
| 145 | |
Simon Glass | aa912af | 2012-10-30 06:15:16 +0000 | [diff] [blame] | 146 | Changes in v4: |
Otavio Salvador | cc1fbaa | 2012-08-18 07:46:04 +0000 | [diff] [blame] | 147 | - Jog the dial back closer to the widget |
| 148 | |
Simon Glass | aa912af | 2012-10-30 06:15:16 +0000 | [diff] [blame] | 149 | Changes in v3: None |
| 150 | Changes in v2: |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 151 | - Fix the widget |
| 152 | - Jog the dial |
| 153 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 154 | etc. |
| 155 | """ |
| 156 | final = [] |
Simon Glass | ec1d042 | 2013-03-26 13:09:44 +0000 | [diff] [blame] | 157 | process_it = self.get('process_log', '').split(',') |
| 158 | process_it = [item.strip() for item in process_it] |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 159 | need_blank = False |
Otavio Salvador | cc1fbaa | 2012-08-18 07:46:04 +0000 | [diff] [blame] | 160 | for change in sorted(self.changes, reverse=True): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 161 | out = [] |
| 162 | for this_commit, text in self.changes[change]: |
| 163 | if commit and this_commit != commit: |
| 164 | continue |
Simon Glass | ec1d042 | 2013-03-26 13:09:44 +0000 | [diff] [blame] | 165 | if 'uniq' not in process_it or text not in out: |
| 166 | out.append(text) |
Simon Glass | aa912af | 2012-10-30 06:15:16 +0000 | [diff] [blame] | 167 | line = 'Changes in v%d:' % change |
| 168 | have_changes = len(out) > 0 |
Simon Glass | ec1d042 | 2013-03-26 13:09:44 +0000 | [diff] [blame] | 169 | if 'sort' in process_it: |
| 170 | out = sorted(out) |
Simon Glass | aa912af | 2012-10-30 06:15:16 +0000 | [diff] [blame] | 171 | if have_changes: |
| 172 | out.insert(0, line) |
| 173 | else: |
| 174 | out = [line + ' None'] |
| 175 | if need_blank: |
| 176 | out.insert(0, '') |
| 177 | final += out |
| 178 | need_blank = have_changes |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 179 | if self.changes: |
| 180 | final.append('') |
| 181 | return final |
| 182 | |
| 183 | def DoChecks(self): |
| 184 | """Check that each version has a change log |
| 185 | |
| 186 | Print an error if something is wrong. |
| 187 | """ |
| 188 | col = terminal.Color() |
| 189 | if self.get('version'): |
| 190 | changes_copy = dict(self.changes) |
Otavio Salvador | fe07d07 | 2012-08-13 10:08:22 +0000 | [diff] [blame] | 191 | for version in range(1, int(self.version) + 1): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 192 | if self.changes.get(version): |
| 193 | del changes_copy[version] |
| 194 | else: |
Otavio Salvador | fe07d07 | 2012-08-13 10:08:22 +0000 | [diff] [blame] | 195 | if version > 1: |
| 196 | str = 'Change log missing for v%d' % version |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 197 | print(col.Color(col.RED, str)) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 198 | for version in changes_copy: |
| 199 | str = 'Change log for unknown version v%d' % version |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 200 | print(col.Color(col.RED, str)) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 201 | elif self.changes: |
| 202 | str = 'Change log exists, but no version is set' |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 203 | print(col.Color(col.RED, str)) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 204 | |
Simon Glass | 46b84d8 | 2014-09-14 20:23:17 -0600 | [diff] [blame] | 205 | def MakeCcFile(self, process_tags, cover_fname, raise_on_error, |
Chris Packham | b84fb48 | 2018-06-07 20:45:06 +1200 | [diff] [blame] | 206 | add_maintainers, limit): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 207 | """Make a cc file for us to use for per-commit Cc automation |
| 208 | |
Doug Anderson | 507e8e8 | 2012-12-03 14:40:42 +0000 | [diff] [blame] | 209 | Also stores in self._generated_cc to make ShowActions() faster. |
| 210 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 211 | Args: |
| 212 | process_tags: Process tags as if they were aliases |
Doug Anderson | 05416af | 2012-12-03 14:40:43 +0000 | [diff] [blame] | 213 | cover_fname: If non-None the name of the cover letter. |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 214 | raise_on_error: True to raise an error when an alias fails to match, |
| 215 | False to just print a message. |
Simon Glass | b712eed | 2017-05-29 15:31:29 -0600 | [diff] [blame] | 216 | add_maintainers: Either: |
| 217 | True/False to call the get_maintainers to CC maintainers |
| 218 | List of maintainers to include (for testing) |
Chris Packham | b84fb48 | 2018-06-07 20:45:06 +1200 | [diff] [blame] | 219 | limit: Limit the length of the Cc list |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 220 | Return: |
| 221 | Filename of temp file created |
| 222 | """ |
Chris Packham | e8d2a12 | 2017-09-01 20:57:53 +1200 | [diff] [blame] | 223 | col = terminal.Color() |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 224 | # Look for commit tags (of the form 'xxx:' at the start of the subject) |
| 225 | fname = '/tmp/patman.%d' % os.getpid() |
| 226 | fd = open(fname, 'w') |
Doug Anderson | 05416af | 2012-12-03 14:40:43 +0000 | [diff] [blame] | 227 | all_ccs = [] |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 228 | for commit in self.commits: |
Simon Glass | 0f94fa7 | 2017-05-29 15:31:30 -0600 | [diff] [blame] | 229 | cc = [] |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 230 | if process_tags: |
Simon Glass | 0f94fa7 | 2017-05-29 15:31:30 -0600 | [diff] [blame] | 231 | cc += gitutil.BuildEmailList(commit.tags, |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 232 | raise_on_error=raise_on_error) |
Simon Glass | 0f94fa7 | 2017-05-29 15:31:30 -0600 | [diff] [blame] | 233 | cc += gitutil.BuildEmailList(commit.cc_list, |
Simon Glass | 12ea5f4 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 234 | raise_on_error=raise_on_error) |
Simon Glass | 0f94fa7 | 2017-05-29 15:31:30 -0600 | [diff] [blame] | 235 | if type(add_maintainers) == type(cc): |
| 236 | cc += add_maintainers |
Simon Glass | b712eed | 2017-05-29 15:31:29 -0600 | [diff] [blame] | 237 | elif add_maintainers: |
Simon Glass | 0f94fa7 | 2017-05-29 15:31:30 -0600 | [diff] [blame] | 238 | cc += get_maintainer.GetMaintainer(commit.patch) |
Chris Packham | e8d2a12 | 2017-09-01 20:57:53 +1200 | [diff] [blame] | 239 | for x in set(cc) & set(settings.bounces): |
| 240 | print(col.Color(col.YELLOW, 'Skipping "%s"' % x)) |
| 241 | cc = set(cc) - set(settings.bounces) |
Simon Glass | b097696 | 2019-05-14 15:53:54 -0600 | [diff] [blame] | 242 | cc = [tools.FromUnicode(m) for m in cc] |
Chris Packham | b84fb48 | 2018-06-07 20:45:06 +1200 | [diff] [blame] | 243 | if limit is not None: |
| 244 | cc = cc[:limit] |
Simon Glass | 0f94fa7 | 2017-05-29 15:31:30 -0600 | [diff] [blame] | 245 | all_ccs += cc |
Simon Glass | 3ab178f | 2019-05-14 15:53:51 -0600 | [diff] [blame] | 246 | print(commit.patch, ', '.join(sorted(set(cc))), file=fd) |
Simon Glass | 0f94fa7 | 2017-05-29 15:31:30 -0600 | [diff] [blame] | 247 | self._generated_cc[commit.patch] = cc |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 248 | |
Doug Anderson | 05416af | 2012-12-03 14:40:43 +0000 | [diff] [blame] | 249 | if cover_fname: |
Simon Glass | c72f3da | 2013-03-20 16:43:00 +0000 | [diff] [blame] | 250 | cover_cc = gitutil.BuildEmailList(self.get('cover_cc', '')) |
Simon Glass | b097696 | 2019-05-14 15:53:54 -0600 | [diff] [blame] | 251 | cover_cc = [tools.FromUnicode(m) for m in cover_cc] |
Simon Glass | 8bb7a7a | 2019-05-14 15:53:50 -0600 | [diff] [blame] | 252 | cc_list = ', '.join([tools.ToUnicode(x) |
Simon Glass | 3ab178f | 2019-05-14 15:53:51 -0600 | [diff] [blame] | 253 | for x in sorted(set(cover_cc + all_ccs))]) |
Chris Packham | cb17cae | 2017-02-07 20:11:00 +1300 | [diff] [blame] | 254 | print(cover_fname, cc_list.encode('utf-8'), file=fd) |
Doug Anderson | 05416af | 2012-12-03 14:40:43 +0000 | [diff] [blame] | 255 | |
Simon Glass | 2613288 | 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 | 9873b91 | 2015-04-15 10:25:18 +0800 | [diff] [blame] | 278 | git_prefix = gitutil.GetDefaultSubjectPrefix() |
| 279 | if git_prefix: |
Paul Burton | a5edb44 | 2016-09-27 16:03:49 +0100 | [diff] [blame] | 280 | git_prefix = '%s][' % git_prefix |
Wu, Josh | 9873b91 | 2015-04-15 10:25:18 +0800 | [diff] [blame] | 281 | else: |
| 282 | git_prefix = '' |
| 283 | |
Simon Glass | 2613288 | 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 | 9873b91 | 2015-04-15 10:25:18 +0800 | [diff] [blame] | 292 | return '%s%sPATCH%s' % (git_prefix, prefix, version) |