blob: 083af0f6340d3c4342b988390b50da5775d478a3 [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
25import gitutil
26import terminal
27
28# Series-xxx tags that we understand
Simon Glassef0e9de2012-09-27 15:06:02 +000029valid_series = ['to', 'cc', 'version', 'changes', 'prefix', 'notes', 'name'];
Simon Glass0d24de92012-01-14 15:12:45 +000030
31class Series(dict):
32 """Holds information about a patch series, including all tags.
33
34 Vars:
35 cc: List of aliases/emails to Cc all patches to
36 commits: List of Commit objects, one for each patch
37 cover: List of lines in the cover letter
38 notes: List of lines in the notes
39 changes: (dict) List of changes for each version, The key is
40 the integer version number
41 """
42 def __init__(self):
43 self.cc = []
44 self.to = []
45 self.commits = []
46 self.cover = None
47 self.notes = []
48 self.changes = {}
49
Doug Andersond94566a2012-12-03 14:40:42 +000050 # Written in MakeCcFile()
51 # key: name of patch file
52 # value: list of email addresses
53 self._generated_cc = {}
54
Simon Glass0d24de92012-01-14 15:12:45 +000055 # These make us more like a dictionary
56 def __setattr__(self, name, value):
57 self[name] = value
58
59 def __getattr__(self, name):
60 return self[name]
61
62 def AddTag(self, commit, line, name, value):
63 """Add a new Series-xxx tag along with its value.
64
65 Args:
66 line: Source line containing tag (useful for debug/error messages)
67 name: Tag name (part after 'Series-')
68 value: Tag value (part after 'Series-xxx: ')
69 """
70 # If we already have it, then add to our list
71 if name in self:
72 values = value.split(',')
73 values = [str.strip() for str in values]
74 if type(self[name]) != type([]):
75 raise ValueError("In %s: line '%s': Cannot add another value "
76 "'%s' to series '%s'" %
77 (commit.hash, line, values, self[name]))
78 self[name] += values
79
80 # Otherwise just set the value
81 elif name in valid_series:
82 self[name] = value
83 else:
84 raise ValueError("In %s: line '%s': Unknown 'Series-%s': valid "
Simon Glassef0e9de2012-09-27 15:06:02 +000085 "options are %s" % (commit.hash, line, name,
Simon Glass0d24de92012-01-14 15:12:45 +000086 ', '.join(valid_series)))
87
88 def AddCommit(self, commit):
89 """Add a commit into our list of commits
90
91 We create a list of tags in the commit subject also.
92
93 Args:
94 commit: Commit object to add
95 """
96 commit.CheckTags()
97 self.commits.append(commit)
98
99 def ShowActions(self, args, cmd, process_tags):
100 """Show what actions we will/would perform
101
102 Args:
103 args: List of patch files we created
104 cmd: The git command we would have run
105 process_tags: Process tags as if they were aliases
106 """
107 col = terminal.Color()
108 print 'Dry run, so not doing much. But I would do this:'
109 print
110 print 'Send a total of %d patch%s with %scover letter.' % (
111 len(args), '' if len(args) == 1 else 'es',
112 self.get('cover') and 'a ' or 'no ')
113
114 # TODO: Colour the patches according to whether they passed checks
115 for upto in range(len(args)):
116 commit = self.commits[upto]
117 print col.Color(col.GREEN, ' %s' % args[upto])
Doug Andersond94566a2012-12-03 14:40:42 +0000118 cc_list = list(self._generated_cc[commit.patch])
Simon Glass0d24de92012-01-14 15:12:45 +0000119
Otavio Salvador43de0242012-08-18 07:19:51 +0000120 # Skip items in To list
121 if 'to' in self:
122 try:
123 map(cc_list.remove, gitutil.BuildEmailList(self.to))
124 except ValueError:
125 pass
126
Simon Glass0d24de92012-01-14 15:12:45 +0000127 for email in cc_list:
128 if email == None:
129 email = col.Color(col.YELLOW, "<alias '%s' not found>"
130 % tag)
131 if email:
132 print ' Cc: ',email
133 print
134 for item in gitutil.BuildEmailList(self.get('to', '<none>')):
135 print 'To:\t ', item
136 for item in gitutil.BuildEmailList(self.cc):
137 print 'Cc:\t ', item
138 print 'Version: ', self.get('version')
139 print 'Prefix:\t ', self.get('prefix')
140 if self.cover:
141 print 'Cover: %d lines' % len(self.cover)
Doug Anderson31187252012-12-03 14:40:43 +0000142 all_ccs = itertools.chain(*self._generated_cc.values())
143 for email in set(all_ccs):
144 print ' Cc: ',email
Simon Glass0d24de92012-01-14 15:12:45 +0000145 if cmd:
146 print 'Git command: %s' % cmd
147
148 def MakeChangeLog(self, commit):
149 """Create a list of changes for each version.
150
151 Return:
152 The change log as a list of strings, one per line
153
Simon Glass27e97602012-10-30 06:15:16 +0000154 Changes in v4:
Otavio Salvador244e6f92012-08-18 07:46:04 +0000155 - Jog the dial back closer to the widget
156
Simon Glass27e97602012-10-30 06:15:16 +0000157 Changes in v3: None
158 Changes in v2:
Simon Glass0d24de92012-01-14 15:12:45 +0000159 - Fix the widget
160 - Jog the dial
161
Simon Glass0d24de92012-01-14 15:12:45 +0000162 etc.
163 """
164 final = []
165 need_blank = False
Otavio Salvador244e6f92012-08-18 07:46:04 +0000166 for change in sorted(self.changes, reverse=True):
Simon Glass0d24de92012-01-14 15:12:45 +0000167 out = []
168 for this_commit, text in self.changes[change]:
169 if commit and this_commit != commit:
170 continue
Ilya Yanok73fe07a2012-08-06 23:46:07 +0000171 out.append(text)
Simon Glass27e97602012-10-30 06:15:16 +0000172 line = 'Changes in v%d:' % change
173 have_changes = len(out) > 0
174 if have_changes:
175 out.insert(0, line)
176 else:
177 out = [line + ' None']
178 if need_blank:
179 out.insert(0, '')
180 final += out
181 need_blank = have_changes
Simon Glass0d24de92012-01-14 15:12:45 +0000182 if self.changes:
183 final.append('')
184 return final
185
186 def DoChecks(self):
187 """Check that each version has a change log
188
189 Print an error if something is wrong.
190 """
191 col = terminal.Color()
192 if self.get('version'):
193 changes_copy = dict(self.changes)
Otavio Salvadord5f81d82012-08-13 10:08:22 +0000194 for version in range(1, int(self.version) + 1):
Simon Glass0d24de92012-01-14 15:12:45 +0000195 if self.changes.get(version):
196 del changes_copy[version]
197 else:
Otavio Salvadord5f81d82012-08-13 10:08:22 +0000198 if version > 1:
199 str = 'Change log missing for v%d' % version
200 print col.Color(col.RED, str)
Simon Glass0d24de92012-01-14 15:12:45 +0000201 for version in changes_copy:
202 str = 'Change log for unknown version v%d' % version
203 print col.Color(col.RED, str)
204 elif self.changes:
205 str = 'Change log exists, but no version is set'
206 print col.Color(col.RED, str)
207
Doug Anderson31187252012-12-03 14:40:43 +0000208 def MakeCcFile(self, process_tags, cover_fname):
Simon Glass0d24de92012-01-14 15:12:45 +0000209 """Make a cc file for us to use for per-commit Cc automation
210
Doug Andersond94566a2012-12-03 14:40:42 +0000211 Also stores in self._generated_cc to make ShowActions() faster.
212
Simon Glass0d24de92012-01-14 15:12:45 +0000213 Args:
214 process_tags: Process tags as if they were aliases
Doug Anderson31187252012-12-03 14:40:43 +0000215 cover_fname: If non-None the name of the cover letter.
Simon Glass0d24de92012-01-14 15:12:45 +0000216 Return:
217 Filename of temp file created
218 """
219 # Look for commit tags (of the form 'xxx:' at the start of the subject)
220 fname = '/tmp/patman.%d' % os.getpid()
221 fd = open(fname, 'w')
Doug Anderson31187252012-12-03 14:40:43 +0000222 all_ccs = []
Simon Glass0d24de92012-01-14 15:12:45 +0000223 for commit in self.commits:
224 list = []
225 if process_tags:
226 list += gitutil.BuildEmailList(commit.tags)
227 list += gitutil.BuildEmailList(commit.cc_list)
Doug Anderson31187252012-12-03 14:40:43 +0000228 all_ccs += list
Simon Glass0d24de92012-01-14 15:12:45 +0000229 print >>fd, commit.patch, ', '.join(list)
Doug Andersond94566a2012-12-03 14:40:42 +0000230 self._generated_cc[commit.patch] = list
Simon Glass0d24de92012-01-14 15:12:45 +0000231
Doug Anderson31187252012-12-03 14:40:43 +0000232 if cover_fname:
233 print >>fd, cover_fname, ', '.join(set(all_ccs))
234
Simon Glass0d24de92012-01-14 15:12:45 +0000235 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)