blob: 88c0d877d400bc77035e010dab85b00c78eda6c0 [file] [log] [blame]
Simon Glass26132882012-01-14 15:12:45 +00001# Copyright (c) 2011 The Chromium OS Authors.
2#
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02003# SPDX-License-Identifier: GPL-2.0+
Simon Glass26132882012-01-14 15:12:45 +00004#
5
Doug Anderson05416af2012-12-03 14:40:43 +00006import itertools
Simon Glass26132882012-01-14 15:12:45 +00007import os
8
Doug Andersonc2c84bc2012-12-03 14:43:16 +00009import get_maintainer
Simon Glass26132882012-01-14 15:12:45 +000010import gitutil
11import terminal
12
13# Series-xxx tags that we understand
Simon Glassc72f3da2013-03-20 16:43:00 +000014valid_series = ['to', 'cc', 'version', 'changes', 'prefix', 'notes', 'name',
Simon Glassec1d0422013-03-26 13:09:44 +000015 'cover-cc', 'process_log']
Simon Glass26132882012-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 Glass29c5abc2013-05-02 14:46:02 +000027 allow_overwrite: Allow tags to overwrite an existing tag
Simon Glass26132882012-01-14 15:12:45 +000028 """
29 def __init__(self):
30 self.cc = []
31 self.to = []
Simon Glassc72f3da2013-03-20 16:43:00 +000032 self.cover_cc = []
Simon Glass26132882012-01-14 15:12:45 +000033 self.commits = []
34 self.cover = None
35 self.notes = []
36 self.changes = {}
Simon Glass29c5abc2013-05-02 14:46:02 +000037 self.allow_overwrite = False
Simon Glass26132882012-01-14 15:12:45 +000038
Doug Anderson507e8e82012-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 Glass26132882012-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 Glassc72f3da2013-03-20 16:43:00 +000060 name = name.replace('-', '_')
Simon Glass29c5abc2013-05-02 14:46:02 +000061 if name in self and not self.allow_overwrite:
Simon Glass26132882012-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:
72 self[name] = value
73 else:
74 raise ValueError("In %s: line '%s': Unknown 'Series-%s': valid "
Simon Glasse7ecd3f2012-09-27 15:06:02 +000075 "options are %s" % (commit.hash, line, name,
Simon Glass26132882012-01-14 15:12:45 +000076 ', '.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 """
97 col = terminal.Color()
98 print 'Dry run, so not doing much. But I would do this:'
99 print
100 print 'Send a total of %d patch%s with %scover letter.' % (
101 len(args), '' if len(args) == 1 else 'es',
102 self.get('cover') and 'a ' or 'no ')
103
104 # TODO: Colour the patches according to whether they passed checks
105 for upto in range(len(args)):
106 commit = self.commits[upto]
107 print col.Color(col.GREEN, ' %s' % args[upto])
Doug Anderson507e8e82012-12-03 14:40:42 +0000108 cc_list = list(self._generated_cc[commit.patch])
Simon Glass26132882012-01-14 15:12:45 +0000109
Otavio Salvador3fbb9422012-08-18 07:19:51 +0000110 # Skip items in To list
111 if 'to' in self:
112 try:
113 map(cc_list.remove, gitutil.BuildEmailList(self.to))
114 except ValueError:
115 pass
116
Simon Glass26132882012-01-14 15:12:45 +0000117 for email in cc_list:
118 if email == None:
119 email = col.Color(col.YELLOW, "<alias '%s' not found>"
120 % tag)
121 if email:
122 print ' Cc: ',email
123 print
124 for item in gitutil.BuildEmailList(self.get('to', '<none>')):
125 print 'To:\t ', item
126 for item in gitutil.BuildEmailList(self.cc):
127 print 'Cc:\t ', item
128 print 'Version: ', self.get('version')
129 print 'Prefix:\t ', self.get('prefix')
130 if self.cover:
131 print 'Cover: %d lines' % len(self.cover)
Simon Glassc72f3da2013-03-20 16:43:00 +0000132 cover_cc = gitutil.BuildEmailList(self.get('cover_cc', ''))
133 all_ccs = itertools.chain(cover_cc, *self._generated_cc.values())
Doug Anderson05416af2012-12-03 14:40:43 +0000134 for email in set(all_ccs):
135 print ' Cc: ',email
Simon Glass26132882012-01-14 15:12:45 +0000136 if cmd:
137 print 'Git command: %s' % cmd
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 Glassaa912af2012-10-30 06:15:16 +0000145 Changes in v4:
Otavio Salvadorcc1fbaa2012-08-18 07:46:04 +0000146 - Jog the dial back closer to the widget
147
Simon Glassaa912af2012-10-30 06:15:16 +0000148 Changes in v3: None
149 Changes in v2:
Simon Glass26132882012-01-14 15:12:45 +0000150 - Fix the widget
151 - Jog the dial
152
Simon Glass26132882012-01-14 15:12:45 +0000153 etc.
154 """
155 final = []
Simon Glassec1d0422013-03-26 13:09:44 +0000156 process_it = self.get('process_log', '').split(',')
157 process_it = [item.strip() for item in process_it]
Simon Glass26132882012-01-14 15:12:45 +0000158 need_blank = False
Otavio Salvadorcc1fbaa2012-08-18 07:46:04 +0000159 for change in sorted(self.changes, reverse=True):
Simon Glass26132882012-01-14 15:12:45 +0000160 out = []
161 for this_commit, text in self.changes[change]:
162 if commit and this_commit != commit:
163 continue
Simon Glassec1d0422013-03-26 13:09:44 +0000164 if 'uniq' not in process_it or text not in out:
165 out.append(text)
Simon Glassaa912af2012-10-30 06:15:16 +0000166 line = 'Changes in v%d:' % change
167 have_changes = len(out) > 0
Simon Glassec1d0422013-03-26 13:09:44 +0000168 if 'sort' in process_it:
169 out = sorted(out)
Simon Glassaa912af2012-10-30 06:15:16 +0000170 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 Glass26132882012-01-14 15:12:45 +0000178 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 Salvadorfe07d072012-08-13 10:08:22 +0000190 for version in range(1, int(self.version) + 1):
Simon Glass26132882012-01-14 15:12:45 +0000191 if self.changes.get(version):
192 del changes_copy[version]
193 else:
Otavio Salvadorfe07d072012-08-13 10:08:22 +0000194 if version > 1:
195 str = 'Change log missing for v%d' % version
196 print col.Color(col.RED, str)
Simon Glass26132882012-01-14 15:12:45 +0000197 for version in changes_copy:
198 str = 'Change log for unknown version v%d' % version
199 print col.Color(col.RED, str)
200 elif self.changes:
201 str = 'Change log exists, but no version is set'
202 print col.Color(col.RED, str)
203
Simon Glass12ea5f42013-03-26 13:09:42 +0000204 def MakeCcFile(self, process_tags, cover_fname, raise_on_error):
Simon Glass26132882012-01-14 15:12:45 +0000205 """Make a cc file for us to use for per-commit Cc automation
206
Doug Anderson507e8e82012-12-03 14:40:42 +0000207 Also stores in self._generated_cc to make ShowActions() faster.
208
Simon Glass26132882012-01-14 15:12:45 +0000209 Args:
210 process_tags: Process tags as if they were aliases
Doug Anderson05416af2012-12-03 14:40:43 +0000211 cover_fname: If non-None the name of the cover letter.
Simon Glass12ea5f42013-03-26 13:09:42 +0000212 raise_on_error: True to raise an error when an alias fails to match,
213 False to just print a message.
Simon Glass26132882012-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 Anderson05416af2012-12-03 14:40:43 +0000220 all_ccs = []
Simon Glass26132882012-01-14 15:12:45 +0000221 for commit in self.commits:
222 list = []
223 if process_tags:
Simon Glass12ea5f42013-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)
Doug Andersonc2c84bc2012-12-03 14:43:16 +0000228 list += get_maintainer.GetMaintainer(commit.patch)
Doug Anderson05416af2012-12-03 14:40:43 +0000229 all_ccs += list
Simon Glass26132882012-01-14 15:12:45 +0000230 print >>fd, commit.patch, ', '.join(list)
Doug Anderson507e8e82012-12-03 14:40:42 +0000231 self._generated_cc[commit.patch] = list
Simon Glass26132882012-01-14 15:12:45 +0000232
Doug Anderson05416af2012-12-03 14:40:43 +0000233 if cover_fname:
Simon Glassc72f3da2013-03-20 16:43:00 +0000234 cover_cc = gitutil.BuildEmailList(self.get('cover_cc', ''))
235 print >>fd, cover_fname, ', '.join(set(cover_cc + all_ccs))
Doug Anderson05416af2012-12-03 14:40:43 +0000236
Simon Glass26132882012-01-14 15:12:45 +0000237 fd.close()
238 return fname
239
240 def AddChange(self, version, commit, info):
241 """Add a new change line to a version.
242
243 This will later appear in the change log.
244
245 Args:
246 version: version number to add change list to
247 info: change line for this version
248 """
249 if not self.changes.get(version):
250 self.changes[version] = []
251 self.changes[version].append([commit, info])
252
253 def GetPatchPrefix(self):
254 """Get the patch version string
255
256 Return:
257 Patch string, like 'RFC PATCH v5' or just 'PATCH'
258 """
259 version = ''
260 if self.get('version'):
261 version = ' v%s' % self['version']
262
263 # Get patch name prefix
264 prefix = ''
265 if self.get('prefix'):
266 prefix = '%s ' % self['prefix']
267 return '%sPATCH%s' % (prefix, version)