blob: cc6f80b2fd4e4dd917c45c5576979cbb4d2e9964 [file] [log] [blame]
Simon Glass0d24de92012-01-14 15:12:45 +00001# Copyright (c) 2011 The Chromium OS Authors.
2#
Wolfgang Denk1a459662013-07-08 09:37:19 +02003# SPDX-License-Identifier: GPL-2.0+
Simon Glass0d24de92012-01-14 15:12:45 +00004#
5
Doug Anderson31187252012-12-03 14:40:43 +00006import itertools
Simon Glass0d24de92012-01-14 15:12:45 +00007import os
8
Doug Anderson21a19d72012-12-03 14:43:16 +00009import get_maintainer
Simon Glass0d24de92012-01-14 15:12:45 +000010import gitutil
11import terminal
12
13# Series-xxx tags that we understand
Simon Glassfe2f8d92013-03-20 16:43:00 +000014valid_series = ['to', 'cc', 'version', 'changes', 'prefix', 'notes', 'name',
Simon Glassd9917b02015-08-22 18:28:01 -060015 'cover_cc', 'process_log']
Simon Glass0d24de92012-01-14 15:12:45 +000016
17class 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 Glassf0b739f2013-05-02 14:46:02 +000027 allow_overwrite: Allow tags to overwrite an existing tag
Simon Glass0d24de92012-01-14 15:12:45 +000028 """
29 def __init__(self):
30 self.cc = []
31 self.to = []
Simon Glassfe2f8d92013-03-20 16:43:00 +000032 self.cover_cc = []
Simon Glass0d24de92012-01-14 15:12:45 +000033 self.commits = []
34 self.cover = None
35 self.notes = []
36 self.changes = {}
Simon Glassf0b739f2013-05-02 14:46:02 +000037 self.allow_overwrite = False
Simon Glass0d24de92012-01-14 15:12:45 +000038
Doug Andersond94566a2012-12-03 14:40:42 +000039 # Written in MakeCcFile()
40 # key: name of patch file
41 # value: list of email addresses
42 self._generated_cc = {}
43
Simon Glass0d24de92012-01-14 15:12:45 +000044 # 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 Glassfe2f8d92013-03-20 16:43:00 +000060 name = name.replace('-', '_')
Simon Glassf0b739f2013-05-02 14:46:02 +000061 if name in self and not self.allow_overwrite:
Simon Glass0d24de92012-01-14 15:12:45 +000062 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:
Albert ARIBAUD070b7812016-02-02 10:24:53 +010072 if name=="notes":
73 self[name] = [value]
74 else:
75 self[name] = value
Simon Glass0d24de92012-01-14 15:12:45 +000076 else:
77 raise ValueError("In %s: line '%s': Unknown 'Series-%s': valid "
Simon Glassef0e9de2012-09-27 15:06:02 +000078 "options are %s" % (commit.hash, line, name,
Simon Glass0d24de92012-01-14 15:12:45 +000079 ', '.join(valid_series)))
80
81 def AddCommit(self, commit):
82 """Add a commit into our list of commits
83
84 We create a list of tags in the commit subject also.
85
86 Args:
87 commit: Commit object to add
88 """
89 commit.CheckTags()
90 self.commits.append(commit)
91
92 def ShowActions(self, args, cmd, process_tags):
93 """Show what actions we will/would perform
94
95 Args:
96 args: List of patch files we created
97 cmd: The git command we would have run
98 process_tags: Process tags as if they were aliases
99 """
Peter Tyser21818302015-01-26 11:42:21 -0600100 to_set = set(gitutil.BuildEmailList(self.to));
101 cc_set = set(gitutil.BuildEmailList(self.cc));
102
Simon Glass0d24de92012-01-14 15:12:45 +0000103 col = terminal.Color()
104 print 'Dry run, so not doing much. But I would do this:'
105 print
106 print 'Send a total of %d patch%s with %scover letter.' % (
107 len(args), '' if len(args) == 1 else 'es',
108 self.get('cover') and 'a ' or 'no ')
109
110 # TODO: Colour the patches according to whether they passed checks
111 for upto in range(len(args)):
112 commit = self.commits[upto]
113 print col.Color(col.GREEN, ' %s' % args[upto])
Doug Andersond94566a2012-12-03 14:40:42 +0000114 cc_list = list(self._generated_cc[commit.patch])
Peter Tyser21818302015-01-26 11:42:21 -0600115 for email in set(cc_list) - to_set - cc_set:
Simon Glass0d24de92012-01-14 15:12:45 +0000116 if email == None:
117 email = col.Color(col.YELLOW, "<alias '%s' not found>"
118 % tag)
119 if email:
120 print ' Cc: ',email
121 print
Peter Tyser21818302015-01-26 11:42:21 -0600122 for item in to_set:
Simon Glass0d24de92012-01-14 15:12:45 +0000123 print 'To:\t ', item
Peter Tyser21818302015-01-26 11:42:21 -0600124 for item in cc_set - to_set:
Simon Glass0d24de92012-01-14 15:12:45 +0000125 print 'Cc:\t ', item
126 print 'Version: ', self.get('version')
127 print 'Prefix:\t ', self.get('prefix')
128 if self.cover:
129 print 'Cover: %d lines' % len(self.cover)
Simon Glassfe2f8d92013-03-20 16:43:00 +0000130 cover_cc = gitutil.BuildEmailList(self.get('cover_cc', ''))
131 all_ccs = itertools.chain(cover_cc, *self._generated_cc.values())
Peter Tyser21818302015-01-26 11:42:21 -0600132 for email in set(all_ccs) - to_set - cc_set:
Doug Anderson31187252012-12-03 14:40:43 +0000133 print ' Cc: ',email
Simon Glass0d24de92012-01-14 15:12:45 +0000134 if cmd:
135 print 'Git command: %s' % cmd
136
137 def MakeChangeLog(self, commit):
138 """Create a list of changes for each version.
139
140 Return:
141 The change log as a list of strings, one per line
142
Simon Glass27e97602012-10-30 06:15:16 +0000143 Changes in v4:
Otavio Salvador244e6f92012-08-18 07:46:04 +0000144 - Jog the dial back closer to the widget
145
Simon Glass27e97602012-10-30 06:15:16 +0000146 Changes in v3: None
147 Changes in v2:
Simon Glass0d24de92012-01-14 15:12:45 +0000148 - Fix the widget
149 - Jog the dial
150
Simon Glass0d24de92012-01-14 15:12:45 +0000151 etc.
152 """
153 final = []
Simon Glass645b2712013-03-26 13:09:44 +0000154 process_it = self.get('process_log', '').split(',')
155 process_it = [item.strip() for item in process_it]
Simon Glass0d24de92012-01-14 15:12:45 +0000156 need_blank = False
Otavio Salvador244e6f92012-08-18 07:46:04 +0000157 for change in sorted(self.changes, reverse=True):
Simon Glass0d24de92012-01-14 15:12:45 +0000158 out = []
159 for this_commit, text in self.changes[change]:
160 if commit and this_commit != commit:
161 continue
Simon Glass645b2712013-03-26 13:09:44 +0000162 if 'uniq' not in process_it or text not in out:
163 out.append(text)
Simon Glass27e97602012-10-30 06:15:16 +0000164 line = 'Changes in v%d:' % change
165 have_changes = len(out) > 0
Simon Glass645b2712013-03-26 13:09:44 +0000166 if 'sort' in process_it:
167 out = sorted(out)
Simon Glass27e97602012-10-30 06:15:16 +0000168 if have_changes:
169 out.insert(0, line)
170 else:
171 out = [line + ' None']
172 if need_blank:
173 out.insert(0, '')
174 final += out
175 need_blank = have_changes
Simon Glass0d24de92012-01-14 15:12:45 +0000176 if self.changes:
177 final.append('')
178 return final
179
180 def DoChecks(self):
181 """Check that each version has a change log
182
183 Print an error if something is wrong.
184 """
185 col = terminal.Color()
186 if self.get('version'):
187 changes_copy = dict(self.changes)
Otavio Salvadord5f81d82012-08-13 10:08:22 +0000188 for version in range(1, int(self.version) + 1):
Simon Glass0d24de92012-01-14 15:12:45 +0000189 if self.changes.get(version):
190 del changes_copy[version]
191 else:
Otavio Salvadord5f81d82012-08-13 10:08:22 +0000192 if version > 1:
193 str = 'Change log missing for v%d' % version
194 print col.Color(col.RED, str)
Simon Glass0d24de92012-01-14 15:12:45 +0000195 for version in changes_copy:
196 str = 'Change log for unknown version v%d' % version
197 print col.Color(col.RED, str)
198 elif self.changes:
199 str = 'Change log exists, but no version is set'
200 print col.Color(col.RED, str)
201
Simon Glass983a2742014-09-14 20:23:17 -0600202 def MakeCcFile(self, process_tags, cover_fname, raise_on_error,
203 add_maintainers):
Simon Glass0d24de92012-01-14 15:12:45 +0000204 """Make a cc file for us to use for per-commit Cc automation
205
Doug Andersond94566a2012-12-03 14:40:42 +0000206 Also stores in self._generated_cc to make ShowActions() faster.
207
Simon Glass0d24de92012-01-14 15:12:45 +0000208 Args:
209 process_tags: Process tags as if they were aliases
Doug Anderson31187252012-12-03 14:40:43 +0000210 cover_fname: If non-None the name of the cover letter.
Simon Glassa1318f72013-03-26 13:09:42 +0000211 raise_on_error: True to raise an error when an alias fails to match,
212 False to just print a message.
Simon Glass983a2742014-09-14 20:23:17 -0600213 add_maintainers: Call the get_maintainers to CC maintainers
Simon Glass0d24de92012-01-14 15:12:45 +0000214 Return:
215 Filename of temp file created
216 """
217 # Look for commit tags (of the form 'xxx:' at the start of the subject)
218 fname = '/tmp/patman.%d' % os.getpid()
219 fd = open(fname, 'w')
Doug Anderson31187252012-12-03 14:40:43 +0000220 all_ccs = []
Simon Glass0d24de92012-01-14 15:12:45 +0000221 for commit in self.commits:
222 list = []
223 if process_tags:
Simon Glassa1318f72013-03-26 13:09:42 +0000224 list += gitutil.BuildEmailList(commit.tags,
225 raise_on_error=raise_on_error)
226 list += gitutil.BuildEmailList(commit.cc_list,
227 raise_on_error=raise_on_error)
Simon Glass983a2742014-09-14 20:23:17 -0600228 if add_maintainers:
229 list += get_maintainer.GetMaintainer(commit.patch)
Doug Anderson31187252012-12-03 14:40:43 +0000230 all_ccs += list
Peter Tyser21818302015-01-26 11:42:21 -0600231 print >>fd, commit.patch, ', '.join(set(list))
Doug Andersond94566a2012-12-03 14:40:42 +0000232 self._generated_cc[commit.patch] = list
Simon Glass0d24de92012-01-14 15:12:45 +0000233
Doug Anderson31187252012-12-03 14:40:43 +0000234 if cover_fname:
Simon Glassfe2f8d92013-03-20 16:43:00 +0000235 cover_cc = gitutil.BuildEmailList(self.get('cover_cc', ''))
236 print >>fd, cover_fname, ', '.join(set(cover_cc + all_ccs))
Doug Anderson31187252012-12-03 14:40:43 +0000237
Simon Glass0d24de92012-01-14 15:12:45 +0000238 fd.close()
239 return fname
240
241 def AddChange(self, version, commit, info):
242 """Add a new change line to a version.
243
244 This will later appear in the change log.
245
246 Args:
247 version: version number to add change list to
248 info: change line for this version
249 """
250 if not self.changes.get(version):
251 self.changes[version] = []
252 self.changes[version].append([commit, info])
253
254 def GetPatchPrefix(self):
255 """Get the patch version string
256
257 Return:
258 Patch string, like 'RFC PATCH v5' or just 'PATCH'
259 """
Wu, Josh3871cd82015-04-15 10:25:18 +0800260 git_prefix = gitutil.GetDefaultSubjectPrefix()
261 if git_prefix:
262 git_prefix = '%s][' % git_prefix
263 else:
264 git_prefix = ''
265
Simon Glass0d24de92012-01-14 15:12:45 +0000266 version = ''
267 if self.get('version'):
268 version = ' v%s' % self['version']
269
270 # Get patch name prefix
271 prefix = ''
272 if self.get('prefix'):
273 prefix = '%s ' % self['prefix']
Wu, Josh3871cd82015-04-15 10:25:18 +0800274 return '%s%sPATCH%s' % (git_prefix, prefix, version)