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