blob: 44ad931cf8758eb1b758d1e0e5b44f3d212da81e [file] [log] [blame]
Simon Glass0d24de92012-01-14 15:12:45 +00001# Copyright (c) 2011 The Chromium OS Authors.
2#
3# See file CREDITS for list of people who contributed to this
4# project.
5#
6# This program is free software; you can redistribute it and/or
7# modify it under the terms of the GNU General Public License as
8# published by the Free Software Foundation; either version 2 of
9# the License, or (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19# MA 02111-1307 USA
20#
21
Doug Anderson31187252012-12-03 14:40:43 +000022import itertools
Simon Glass0d24de92012-01-14 15:12:45 +000023import os
24
Doug Anderson21a19d72012-12-03 14:43:16 +000025import get_maintainer
Simon Glass0d24de92012-01-14 15:12:45 +000026import gitutil
27import terminal
28
29# Series-xxx tags that we understand
Simon Glassfe2f8d92013-03-20 16:43:00 +000030valid_series = ['to', 'cc', 'version', 'changes', 'prefix', 'notes', 'name',
31 'cover-cc']
Simon Glass0d24de92012-01-14 15:12:45 +000032
33class Series(dict):
34 """Holds information about a patch series, including all tags.
35
36 Vars:
37 cc: List of aliases/emails to Cc all patches to
38 commits: List of Commit objects, one for each patch
39 cover: List of lines in the cover letter
40 notes: List of lines in the notes
41 changes: (dict) List of changes for each version, The key is
42 the integer version number
43 """
44 def __init__(self):
45 self.cc = []
46 self.to = []
Simon Glassfe2f8d92013-03-20 16:43:00 +000047 self.cover_cc = []
Simon Glass0d24de92012-01-14 15:12:45 +000048 self.commits = []
49 self.cover = None
50 self.notes = []
51 self.changes = {}
52
Doug Andersond94566a2012-12-03 14:40:42 +000053 # Written in MakeCcFile()
54 # key: name of patch file
55 # value: list of email addresses
56 self._generated_cc = {}
57
Simon Glass0d24de92012-01-14 15:12:45 +000058 # These make us more like a dictionary
59 def __setattr__(self, name, value):
60 self[name] = value
61
62 def __getattr__(self, name):
63 return self[name]
64
65 def AddTag(self, commit, line, name, value):
66 """Add a new Series-xxx tag along with its value.
67
68 Args:
69 line: Source line containing tag (useful for debug/error messages)
70 name: Tag name (part after 'Series-')
71 value: Tag value (part after 'Series-xxx: ')
72 """
73 # If we already have it, then add to our list
Simon Glassfe2f8d92013-03-20 16:43:00 +000074 name = name.replace('-', '_')
Simon Glass0d24de92012-01-14 15:12:45 +000075 if name in self:
76 values = value.split(',')
77 values = [str.strip() for str in values]
78 if type(self[name]) != type([]):
79 raise ValueError("In %s: line '%s': Cannot add another value "
80 "'%s' to series '%s'" %
81 (commit.hash, line, values, self[name]))
82 self[name] += values
83
84 # Otherwise just set the value
85 elif name in valid_series:
86 self[name] = value
87 else:
88 raise ValueError("In %s: line '%s': Unknown 'Series-%s': valid "
Simon Glassef0e9de2012-09-27 15:06:02 +000089 "options are %s" % (commit.hash, line, name,
Simon Glass0d24de92012-01-14 15:12:45 +000090 ', '.join(valid_series)))
91
92 def AddCommit(self, commit):
93 """Add a commit into our list of commits
94
95 We create a list of tags in the commit subject also.
96
97 Args:
98 commit: Commit object to add
99 """
100 commit.CheckTags()
101 self.commits.append(commit)
102
103 def ShowActions(self, args, cmd, process_tags):
104 """Show what actions we will/would perform
105
106 Args:
107 args: List of patch files we created
108 cmd: The git command we would have run
109 process_tags: Process tags as if they were aliases
110 """
111 col = terminal.Color()
112 print 'Dry run, so not doing much. But I would do this:'
113 print
114 print 'Send a total of %d patch%s with %scover letter.' % (
115 len(args), '' if len(args) == 1 else 'es',
116 self.get('cover') and 'a ' or 'no ')
117
118 # TODO: Colour the patches according to whether they passed checks
119 for upto in range(len(args)):
120 commit = self.commits[upto]
121 print col.Color(col.GREEN, ' %s' % args[upto])
Doug Andersond94566a2012-12-03 14:40:42 +0000122 cc_list = list(self._generated_cc[commit.patch])
Simon Glass0d24de92012-01-14 15:12:45 +0000123
Otavio Salvador43de0242012-08-18 07:19:51 +0000124 # Skip items in To list
125 if 'to' in self:
126 try:
127 map(cc_list.remove, gitutil.BuildEmailList(self.to))
128 except ValueError:
129 pass
130
Simon Glass0d24de92012-01-14 15:12:45 +0000131 for email in cc_list:
132 if email == None:
133 email = col.Color(col.YELLOW, "<alias '%s' not found>"
134 % tag)
135 if email:
136 print ' Cc: ',email
137 print
138 for item in gitutil.BuildEmailList(self.get('to', '<none>')):
139 print 'To:\t ', item
140 for item in gitutil.BuildEmailList(self.cc):
141 print 'Cc:\t ', item
142 print 'Version: ', self.get('version')
143 print 'Prefix:\t ', self.get('prefix')
144 if self.cover:
145 print 'Cover: %d lines' % len(self.cover)
Simon Glassfe2f8d92013-03-20 16:43:00 +0000146 cover_cc = gitutil.BuildEmailList(self.get('cover_cc', ''))
147 all_ccs = itertools.chain(cover_cc, *self._generated_cc.values())
Doug Anderson31187252012-12-03 14:40:43 +0000148 for email in set(all_ccs):
149 print ' Cc: ',email
Simon Glass0d24de92012-01-14 15:12:45 +0000150 if cmd:
151 print 'Git command: %s' % cmd
152
153 def MakeChangeLog(self, commit):
154 """Create a list of changes for each version.
155
156 Return:
157 The change log as a list of strings, one per line
158
Simon Glass27e97602012-10-30 06:15:16 +0000159 Changes in v4:
Otavio Salvador244e6f92012-08-18 07:46:04 +0000160 - Jog the dial back closer to the widget
161
Simon Glass27e97602012-10-30 06:15:16 +0000162 Changes in v3: None
163 Changes in v2:
Simon Glass0d24de92012-01-14 15:12:45 +0000164 - Fix the widget
165 - Jog the dial
166
Simon Glass0d24de92012-01-14 15:12:45 +0000167 etc.
168 """
169 final = []
170 need_blank = False
Otavio Salvador244e6f92012-08-18 07:46:04 +0000171 for change in sorted(self.changes, reverse=True):
Simon Glass0d24de92012-01-14 15:12:45 +0000172 out = []
173 for this_commit, text in self.changes[change]:
174 if commit and this_commit != commit:
175 continue
Ilya Yanok73fe07a2012-08-06 23:46:07 +0000176 out.append(text)
Simon Glass27e97602012-10-30 06:15:16 +0000177 line = 'Changes in v%d:' % change
178 have_changes = len(out) > 0
179 if have_changes:
180 out.insert(0, line)
181 else:
182 out = [line + ' None']
183 if need_blank:
184 out.insert(0, '')
185 final += out
186 need_blank = have_changes
Simon Glass0d24de92012-01-14 15:12:45 +0000187 if self.changes:
188 final.append('')
189 return final
190
191 def DoChecks(self):
192 """Check that each version has a change log
193
194 Print an error if something is wrong.
195 """
196 col = terminal.Color()
197 if self.get('version'):
198 changes_copy = dict(self.changes)
Otavio Salvadord5f81d82012-08-13 10:08:22 +0000199 for version in range(1, int(self.version) + 1):
Simon Glass0d24de92012-01-14 15:12:45 +0000200 if self.changes.get(version):
201 del changes_copy[version]
202 else:
Otavio Salvadord5f81d82012-08-13 10:08:22 +0000203 if version > 1:
204 str = 'Change log missing for v%d' % version
205 print col.Color(col.RED, str)
Simon Glass0d24de92012-01-14 15:12:45 +0000206 for version in changes_copy:
207 str = 'Change log for unknown version v%d' % version
208 print col.Color(col.RED, str)
209 elif self.changes:
210 str = 'Change log exists, but no version is set'
211 print col.Color(col.RED, str)
212
Doug Anderson31187252012-12-03 14:40:43 +0000213 def MakeCcFile(self, process_tags, cover_fname):
Simon Glass0d24de92012-01-14 15:12:45 +0000214 """Make a cc file for us to use for per-commit Cc automation
215
Doug Andersond94566a2012-12-03 14:40:42 +0000216 Also stores in self._generated_cc to make ShowActions() faster.
217
Simon Glass0d24de92012-01-14 15:12:45 +0000218 Args:
219 process_tags: Process tags as if they were aliases
Doug Anderson31187252012-12-03 14:40:43 +0000220 cover_fname: If non-None the name of the cover letter.
Simon Glass0d24de92012-01-14 15:12:45 +0000221 Return:
222 Filename of temp file created
223 """
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 Anderson31187252012-12-03 14:40:43 +0000227 all_ccs = []
Simon Glass0d24de92012-01-14 15:12:45 +0000228 for commit in self.commits:
229 list = []
230 if process_tags:
231 list += gitutil.BuildEmailList(commit.tags)
232 list += gitutil.BuildEmailList(commit.cc_list)
Doug Anderson21a19d72012-12-03 14:43:16 +0000233 list += get_maintainer.GetMaintainer(commit.patch)
Doug Anderson31187252012-12-03 14:40:43 +0000234 all_ccs += list
Simon Glass0d24de92012-01-14 15:12:45 +0000235 print >>fd, commit.patch, ', '.join(list)
Doug Andersond94566a2012-12-03 14:40:42 +0000236 self._generated_cc[commit.patch] = list
Simon Glass0d24de92012-01-14 15:12:45 +0000237
Doug Anderson31187252012-12-03 14:40:43 +0000238 if cover_fname:
Simon Glassfe2f8d92013-03-20 16:43:00 +0000239 cover_cc = gitutil.BuildEmailList(self.get('cover_cc', ''))
240 print >>fd, cover_fname, ', '.join(set(cover_cc + all_ccs))
Doug Anderson31187252012-12-03 14:40:43 +0000241
Simon Glass0d24de92012-01-14 15:12:45 +0000242 fd.close()
243 return fname
244
245 def AddChange(self, version, commit, info):
246 """Add a new change line to a version.
247
248 This will later appear in the change log.
249
250 Args:
251 version: version number to add change list to
252 info: change line for this version
253 """
254 if not self.changes.get(version):
255 self.changes[version] = []
256 self.changes[version].append([commit, info])
257
258 def GetPatchPrefix(self):
259 """Get the patch version string
260
261 Return:
262 Patch string, like 'RFC PATCH v5' or just 'PATCH'
263 """
264 version = ''
265 if self.get('version'):
266 version = ' v%s' % self['version']
267
268 # Get patch name prefix
269 prefix = ''
270 if self.get('prefix'):
271 prefix = '%s ' % self['prefix']
272 return '%sPATCH%s' % (prefix, version)