1 # Copyright (c) 2011 The Chromium OS Authors.
3 # See file CREDITS for list of people who contributed to this
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.
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.
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,
29 # Series-xxx tags that we understand
30 valid_series = ['to', 'cc', 'version', 'changes', 'prefix', 'notes', 'name',
31 'cover-cc', 'process_log']
34 """Holds information about a patch series, including all tags.
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
53 # Written in MakeCcFile()
54 # key: name of patch file
55 # value: list of email addresses
56 self._generated_cc = {}
58 # These make us more like a dictionary
59 def __setattr__(self, name, value):
62 def __getattr__(self, name):
65 def AddTag(self, commit, line, name, value):
66 """Add a new Series-xxx tag along with its value.
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: ')
73 # If we already have it, then add to our list
74 name = name.replace('-', '_')
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]))
84 # Otherwise just set the value
85 elif name in valid_series:
88 raise ValueError("In %s: line '%s': Unknown 'Series-%s': valid "
89 "options are %s" % (commit.hash, line, name,
90 ', '.join(valid_series)))
92 def AddCommit(self, commit):
93 """Add a commit into our list of commits
95 We create a list of tags in the commit subject also.
98 commit: Commit object to add
101 self.commits.append(commit)
103 def ShowActions(self, args, cmd, process_tags):
104 """Show what actions we will/would perform
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
111 col = terminal.Color()
112 print 'Dry run, so not doing much. But I would do this:'
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 ')
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])
122 cc_list = list(self._generated_cc[commit.patch])
124 # Skip items in To list
127 map(cc_list.remove, gitutil.BuildEmailList(self.to))
131 for email in cc_list:
133 email = col.Color(col.YELLOW, "<alias '%s' not found>"
138 for item in gitutil.BuildEmailList(self.get('to', '<none>')):
140 for item in gitutil.BuildEmailList(self.cc):
142 print 'Version: ', self.get('version')
143 print 'Prefix:\t ', self.get('prefix')
145 print 'Cover: %d lines' % len(self.cover)
146 cover_cc = gitutil.BuildEmailList(self.get('cover_cc', ''))
147 all_ccs = itertools.chain(cover_cc, *self._generated_cc.values())
148 for email in set(all_ccs):
151 print 'Git command: %s' % cmd
153 def MakeChangeLog(self, commit):
154 """Create a list of changes for each version.
157 The change log as a list of strings, one per line
160 - Jog the dial back closer to the widget
170 process_it = self.get('process_log', '').split(',')
171 process_it = [item.strip() for item in process_it]
173 for change in sorted(self.changes, reverse=True):
175 for this_commit, text in self.changes[change]:
176 if commit and this_commit != commit:
178 if 'uniq' not in process_it or text not in out:
180 line = 'Changes in v%d:' % change
181 have_changes = len(out) > 0
182 if 'sort' in process_it:
187 out = [line + ' None']
191 need_blank = have_changes
197 """Check that each version has a change log
199 Print an error if something is wrong.
201 col = terminal.Color()
202 if self.get('version'):
203 changes_copy = dict(self.changes)
204 for version in range(1, int(self.version) + 1):
205 if self.changes.get(version):
206 del changes_copy[version]
209 str = 'Change log missing for v%d' % version
210 print col.Color(col.RED, str)
211 for version in changes_copy:
212 str = 'Change log for unknown version v%d' % version
213 print col.Color(col.RED, str)
215 str = 'Change log exists, but no version is set'
216 print col.Color(col.RED, str)
218 def MakeCcFile(self, process_tags, cover_fname, raise_on_error):
219 """Make a cc file for us to use for per-commit Cc automation
221 Also stores in self._generated_cc to make ShowActions() faster.
224 process_tags: Process tags as if they were aliases
225 cover_fname: If non-None the name of the cover letter.
226 raise_on_error: True to raise an error when an alias fails to match,
227 False to just print a message.
229 Filename of temp file created
231 # Look for commit tags (of the form 'xxx:' at the start of the subject)
232 fname = '/tmp/patman.%d' % os.getpid()
233 fd = open(fname, 'w')
235 for commit in self.commits:
238 list += gitutil.BuildEmailList(commit.tags,
239 raise_on_error=raise_on_error)
240 list += gitutil.BuildEmailList(commit.cc_list,
241 raise_on_error=raise_on_error)
242 list += get_maintainer.GetMaintainer(commit.patch)
244 print >>fd, commit.patch, ', '.join(list)
245 self._generated_cc[commit.patch] = list
248 cover_cc = gitutil.BuildEmailList(self.get('cover_cc', ''))
249 print >>fd, cover_fname, ', '.join(set(cover_cc + all_ccs))
254 def AddChange(self, version, commit, info):
255 """Add a new change line to a version.
257 This will later appear in the change log.
260 version: version number to add change list to
261 info: change line for this version
263 if not self.changes.get(version):
264 self.changes[version] = []
265 self.changes[version].append([commit, info])
267 def GetPatchPrefix(self):
268 """Get the patch version string
271 Patch string, like 'RFC PATCH v5' or just 'PATCH'
274 if self.get('version'):
275 version = ' v%s' % self['version']
277 # Get patch name prefix
279 if self.get('prefix'):
280 prefix = '%s ' % self['prefix']
281 return '%sPATCH%s' % (prefix, version)