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 | |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 5 | from __future__ import print_function |
| 6 | |
Doug Anderson | 3118725 | 2012-12-03 14:40:43 +0000 | [diff] [blame] | 7 | import itertools |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 8 | import os |
| 9 | |
Doug Anderson | 21a19d7 | 2012-12-03 14:43:16 +0000 | [diff] [blame] | 10 | import get_maintainer |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 11 | import gitutil |
Chris Packham | e11aa60 | 2017-09-01 20:57:53 +1200 | [diff] [blame] | 12 | import settings |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 13 | import terminal |
| 14 | |
| 15 | # Series-xxx tags that we understand |
Simon Glass | fe2f8d9 | 2013-03-20 16:43:00 +0000 | [diff] [blame] | 16 | valid_series = ['to', 'cc', 'version', 'changes', 'prefix', 'notes', 'name', |
Simon Glass | d9917b0 | 2015-08-22 18:28:01 -0600 | [diff] [blame] | 17 | 'cover_cc', 'process_log'] |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 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 |
Simon Glass | f0b739f | 2013-05-02 14:46:02 +0000 | [diff] [blame] | 29 | allow_overwrite: Allow tags to overwrite an existing tag |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 30 | """ |
| 31 | def __init__(self): |
| 32 | self.cc = [] |
| 33 | self.to = [] |
Simon Glass | fe2f8d9 | 2013-03-20 16:43:00 +0000 | [diff] [blame] | 34 | self.cover_cc = [] |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 35 | self.commits = [] |
| 36 | self.cover = None |
| 37 | self.notes = [] |
| 38 | self.changes = {} |
Simon Glass | f0b739f | 2013-05-02 14:46:02 +0000 | [diff] [blame] | 39 | self.allow_overwrite = False |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 40 | |
Doug Anderson | d94566a | 2012-12-03 14:40:42 +0000 | [diff] [blame] | 41 | # Written in MakeCcFile() |
| 42 | # key: name of patch file |
| 43 | # value: list of email addresses |
| 44 | self._generated_cc = {} |
| 45 | |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 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 |
Simon Glass | fe2f8d9 | 2013-03-20 16:43:00 +0000 | [diff] [blame] | 62 | name = name.replace('-', '_') |
Simon Glass | f0b739f | 2013-05-02 14:46:02 +0000 | [diff] [blame] | 63 | if name in self and not self.allow_overwrite: |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 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: |
Albert ARIBAUD | 070b781 | 2016-02-02 10:24:53 +0100 | [diff] [blame] | 74 | if name=="notes": |
| 75 | self[name] = [value] |
| 76 | else: |
| 77 | self[name] = value |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 78 | else: |
| 79 | raise ValueError("In %s: line '%s': Unknown 'Series-%s': valid " |
Simon Glass | ef0e9de | 2012-09-27 15:06:02 +0000 | [diff] [blame] | 80 | "options are %s" % (commit.hash, line, name, |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 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 | """ |
Peter Tyser | 2181830 | 2015-01-26 11:42:21 -0600 | [diff] [blame] | 102 | to_set = set(gitutil.BuildEmailList(self.to)); |
| 103 | cc_set = set(gitutil.BuildEmailList(self.cc)); |
| 104 | |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 105 | col = terminal.Color() |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 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.' % ( |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 109 | len(args), '' if len(args) == 1 else 'es', |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 110 | self.get('cover') and 'a ' or 'no ')) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 111 | |
| 112 | # TODO: Colour the patches according to whether they passed checks |
| 113 | for upto in range(len(args)): |
| 114 | commit = self.commits[upto] |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 115 | print(col.Color(col.GREEN, ' %s' % args[upto])) |
Doug Anderson | d94566a | 2012-12-03 14:40:42 +0000 | [diff] [blame] | 116 | cc_list = list(self._generated_cc[commit.patch]) |
Peter Tyser | 2181830 | 2015-01-26 11:42:21 -0600 | [diff] [blame] | 117 | for email in set(cc_list) - to_set - cc_set: |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 118 | if email == None: |
| 119 | email = col.Color(col.YELLOW, "<alias '%s' not found>" |
| 120 | % tag) |
| 121 | if email: |
Simon Glass | 6f8abf7 | 2017-05-29 15:31:23 -0600 | [diff] [blame] | 122 | print(' Cc: ', email) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 123 | print |
Peter Tyser | 2181830 | 2015-01-26 11:42:21 -0600 | [diff] [blame] | 124 | for item in to_set: |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 125 | print('To:\t ', item) |
Peter Tyser | 2181830 | 2015-01-26 11:42:21 -0600 | [diff] [blame] | 126 | for item in cc_set - to_set: |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 127 | print('Cc:\t ', item) |
| 128 | print('Version: ', self.get('version')) |
| 129 | print('Prefix:\t ', self.get('prefix')) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 130 | if self.cover: |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 131 | print('Cover: %d lines' % len(self.cover)) |
Simon Glass | fe2f8d9 | 2013-03-20 16:43:00 +0000 | [diff] [blame] | 132 | cover_cc = gitutil.BuildEmailList(self.get('cover_cc', '')) |
| 133 | all_ccs = itertools.chain(cover_cc, *self._generated_cc.values()) |
Peter Tyser | 2181830 | 2015-01-26 11:42:21 -0600 | [diff] [blame] | 134 | for email in set(all_ccs) - to_set - cc_set: |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 135 | print(' Cc: ', email) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 136 | if cmd: |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 137 | print('Git command: %s' % cmd) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 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 | |
Simon Glass | 27e9760 | 2012-10-30 06:15:16 +0000 | [diff] [blame] | 145 | Changes in v4: |
Otavio Salvador | 244e6f9 | 2012-08-18 07:46:04 +0000 | [diff] [blame] | 146 | - Jog the dial back closer to the widget |
| 147 | |
Simon Glass | 27e9760 | 2012-10-30 06:15:16 +0000 | [diff] [blame] | 148 | Changes in v3: None |
| 149 | Changes in v2: |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 150 | - Fix the widget |
| 151 | - Jog the dial |
| 152 | |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 153 | etc. |
| 154 | """ |
| 155 | final = [] |
Simon Glass | 645b271 | 2013-03-26 13:09:44 +0000 | [diff] [blame] | 156 | process_it = self.get('process_log', '').split(',') |
| 157 | process_it = [item.strip() for item in process_it] |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 158 | need_blank = False |
Otavio Salvador | 244e6f9 | 2012-08-18 07:46:04 +0000 | [diff] [blame] | 159 | for change in sorted(self.changes, reverse=True): |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 160 | out = [] |
| 161 | for this_commit, text in self.changes[change]: |
| 162 | if commit and this_commit != commit: |
| 163 | continue |
Simon Glass | 645b271 | 2013-03-26 13:09:44 +0000 | [diff] [blame] | 164 | if 'uniq' not in process_it or text not in out: |
| 165 | out.append(text) |
Simon Glass | 27e9760 | 2012-10-30 06:15:16 +0000 | [diff] [blame] | 166 | line = 'Changes in v%d:' % change |
| 167 | have_changes = len(out) > 0 |
Simon Glass | 645b271 | 2013-03-26 13:09:44 +0000 | [diff] [blame] | 168 | if 'sort' in process_it: |
| 169 | out = sorted(out) |
Simon Glass | 27e9760 | 2012-10-30 06:15:16 +0000 | [diff] [blame] | 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 |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 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) |
Otavio Salvador | d5f81d8 | 2012-08-13 10:08:22 +0000 | [diff] [blame] | 190 | for version in range(1, int(self.version) + 1): |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 191 | if self.changes.get(version): |
| 192 | del changes_copy[version] |
| 193 | else: |
Otavio Salvador | d5f81d8 | 2012-08-13 10:08:22 +0000 | [diff] [blame] | 194 | if version > 1: |
| 195 | str = 'Change log missing for v%d' % version |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 196 | print(col.Color(col.RED, str)) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 197 | for version in changes_copy: |
| 198 | str = 'Change log for unknown version v%d' % version |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 199 | print(col.Color(col.RED, str)) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 200 | elif self.changes: |
| 201 | str = 'Change log exists, but no version is set' |
Paul Burton | a920a17 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 202 | print(col.Color(col.RED, str)) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 203 | |
Simon Glass | 983a274 | 2014-09-14 20:23:17 -0600 | [diff] [blame] | 204 | def MakeCcFile(self, process_tags, cover_fname, raise_on_error, |
| 205 | add_maintainers): |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 206 | """Make a cc file for us to use for per-commit Cc automation |
| 207 | |
Doug Anderson | d94566a | 2012-12-03 14:40:42 +0000 | [diff] [blame] | 208 | Also stores in self._generated_cc to make ShowActions() faster. |
| 209 | |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 210 | Args: |
| 211 | process_tags: Process tags as if they were aliases |
Doug Anderson | 3118725 | 2012-12-03 14:40:43 +0000 | [diff] [blame] | 212 | cover_fname: If non-None the name of the cover letter. |
Simon Glass | a1318f7 | 2013-03-26 13:09:42 +0000 | [diff] [blame] | 213 | raise_on_error: True to raise an error when an alias fails to match, |
| 214 | False to just print a message. |
Simon Glass | 1f487f8 | 2017-05-29 15:31:29 -0600 | [diff] [blame] | 215 | add_maintainers: Either: |
| 216 | True/False to call the get_maintainers to CC maintainers |
| 217 | List of maintainers to include (for testing) |
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() |
| 224 | fd = open(fname, 'w') |
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 | a44f4fb | 2017-05-29 15:31:30 -0600 | [diff] [blame] | 240 | cc = [m.encode('utf-8') if type(m) != str else m for m in cc] |
| 241 | all_ccs += cc |
| 242 | print(commit.patch, ', '.join(set(cc)), file=fd) |
| 243 | self._generated_cc[commit.patch] = cc |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 244 | |
Doug Anderson | 3118725 | 2012-12-03 14:40:43 +0000 | [diff] [blame] | 245 | if cover_fname: |
Simon Glass | fe2f8d9 | 2013-03-20 16:43:00 +0000 | [diff] [blame] | 246 | cover_cc = gitutil.BuildEmailList(self.get('cover_cc', '')) |
Simon Glass | 6f8abf7 | 2017-05-29 15:31:23 -0600 | [diff] [blame] | 247 | cover_cc = [m.encode('utf-8') if type(m) != str else m |
| 248 | for m in cover_cc] |
| 249 | cc_list = ', '.join([x.decode('utf-8') |
| 250 | for x in set(cover_cc + all_ccs)]) |
Chris Packham | f11a0af | 2017-02-07 20:11:00 +1300 | [diff] [blame] | 251 | print(cover_fname, cc_list.encode('utf-8'), file=fd) |
Doug Anderson | 3118725 | 2012-12-03 14:40:43 +0000 | [diff] [blame] | 252 | |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 253 | fd.close() |
| 254 | return fname |
| 255 | |
| 256 | def AddChange(self, version, commit, info): |
| 257 | """Add a new change line to a version. |
| 258 | |
| 259 | This will later appear in the change log. |
| 260 | |
| 261 | Args: |
| 262 | version: version number to add change list to |
| 263 | info: change line for this version |
| 264 | """ |
| 265 | if not self.changes.get(version): |
| 266 | self.changes[version] = [] |
| 267 | self.changes[version].append([commit, info]) |
| 268 | |
| 269 | def GetPatchPrefix(self): |
| 270 | """Get the patch version string |
| 271 | |
| 272 | Return: |
| 273 | Patch string, like 'RFC PATCH v5' or just 'PATCH' |
| 274 | """ |
Wu, Josh | 3871cd8 | 2015-04-15 10:25:18 +0800 | [diff] [blame] | 275 | git_prefix = gitutil.GetDefaultSubjectPrefix() |
| 276 | if git_prefix: |
Paul Burton | 12e5476 | 2016-09-27 16:03:49 +0100 | [diff] [blame] | 277 | git_prefix = '%s][' % git_prefix |
Wu, Josh | 3871cd8 | 2015-04-15 10:25:18 +0800 | [diff] [blame] | 278 | else: |
| 279 | git_prefix = '' |
| 280 | |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 281 | version = '' |
| 282 | if self.get('version'): |
| 283 | version = ' v%s' % self['version'] |
| 284 | |
| 285 | # Get patch name prefix |
| 286 | prefix = '' |
| 287 | if self.get('prefix'): |
| 288 | prefix = '%s ' % self['prefix'] |
Wu, Josh | 3871cd8 | 2015-04-15 10:25:18 +0800 | [diff] [blame] | 289 | return '%s%sPATCH%s' % (git_prefix, prefix, version) |