1 # Copyright (c) 2011 The Chromium OS Authors.
3 # SPDX-License-Identifier: GPL-2.0+
6 from __future__ import print_function
15 # Series-xxx tags that we understand
16 valid_series = ['to', 'cc', 'version', 'changes', 'prefix', 'notes', 'name',
17 'cover_cc', 'process_log']
20 """Holds information about a patch series, including all tags.
23 cc: List of aliases/emails to Cc all patches to
24 commits: List of Commit objects, one for each patch
25 cover: List of lines in the cover letter
26 notes: List of lines in the notes
27 changes: (dict) List of changes for each version, The key is
28 the integer version number
29 allow_overwrite: Allow tags to overwrite an existing tag
39 self.allow_overwrite = False
41 # Written in MakeCcFile()
42 # key: name of patch file
43 # value: list of email addresses
44 self._generated_cc = {}
46 # These make us more like a dictionary
47 def __setattr__(self, name, value):
50 def __getattr__(self, name):
53 def AddTag(self, commit, line, name, value):
54 """Add a new Series-xxx tag along with its value.
57 line: Source line containing tag (useful for debug/error messages)
58 name: Tag name (part after 'Series-')
59 value: Tag value (part after 'Series-xxx: ')
61 # If we already have it, then add to our list
62 name = name.replace('-', '_')
63 if name in self and not self.allow_overwrite:
64 values = value.split(',')
65 values = [str.strip() for str in values]
66 if type(self[name]) != type([]):
67 raise ValueError("In %s: line '%s': Cannot add another value "
68 "'%s' to series '%s'" %
69 (commit.hash, line, values, self[name]))
72 # Otherwise just set the value
73 elif name in valid_series:
79 raise ValueError("In %s: line '%s': Unknown 'Series-%s': valid "
80 "options are %s" % (commit.hash, line, name,
81 ', '.join(valid_series)))
83 def AddCommit(self, commit):
84 """Add a commit into our list of commits
86 We create a list of tags in the commit subject also.
89 commit: Commit object to add
92 self.commits.append(commit)
94 def ShowActions(self, args, cmd, process_tags):
95 """Show what actions we will/would perform
98 args: List of patch files we created
99 cmd: The git command we would have run
100 process_tags: Process tags as if they were aliases
102 to_set = set(gitutil.BuildEmailList(self.to));
103 cc_set = set(gitutil.BuildEmailList(self.cc));
105 col = terminal.Color()
106 print('Dry run, so not doing much. But I would do this:')
108 print('Send a total of %d patch%s with %scover letter.' % (
109 len(args), '' if len(args) == 1 else 'es',
110 self.get('cover') and 'a ' or 'no '))
112 # TODO: Colour the patches according to whether they passed checks
113 for upto in range(len(args)):
114 commit = self.commits[upto]
115 print(col.Color(col.GREEN, ' %s' % args[upto]))
116 cc_list = list(self._generated_cc[commit.patch])
117 for email in set(cc_list) - to_set - cc_set:
119 email = col.Color(col.YELLOW, "<alias '%s' not found>"
122 print(' Cc: ', email)
125 print('To:\t ', item)
126 for item in cc_set - to_set:
127 print('Cc:\t ', item)
128 print('Version: ', self.get('version'))
129 print('Prefix:\t ', self.get('prefix'))
131 print('Cover: %d lines' % len(self.cover))
132 cover_cc = gitutil.BuildEmailList(self.get('cover_cc', ''))
133 all_ccs = itertools.chain(cover_cc, *self._generated_cc.values())
134 for email in set(all_ccs) - to_set - cc_set:
135 print(' Cc: ', email)
137 print('Git command: %s' % cmd)
139 def MakeChangeLog(self, commit):
140 """Create a list of changes for each version.
143 The change log as a list of strings, one per line
146 - Jog the dial back closer to the widget
156 process_it = self.get('process_log', '').split(',')
157 process_it = [item.strip() for item in process_it]
159 for change in sorted(self.changes, reverse=True):
161 for this_commit, text in self.changes[change]:
162 if commit and this_commit != commit:
164 if 'uniq' not in process_it or text not in out:
166 line = 'Changes in v%d:' % change
167 have_changes = len(out) > 0
168 if 'sort' in process_it:
173 out = [line + ' None']
177 need_blank = have_changes
183 """Check that each version has a change log
185 Print an error if something is wrong.
187 col = terminal.Color()
188 if self.get('version'):
189 changes_copy = dict(self.changes)
190 for version in range(1, int(self.version) + 1):
191 if self.changes.get(version):
192 del changes_copy[version]
195 str = 'Change log missing for v%d' % version
196 print(col.Color(col.RED, str))
197 for version in changes_copy:
198 str = 'Change log for unknown version v%d' % version
199 print(col.Color(col.RED, str))
201 str = 'Change log exists, but no version is set'
202 print(col.Color(col.RED, str))
204 def MakeCcFile(self, process_tags, cover_fname, raise_on_error,
206 """Make a cc file for us to use for per-commit Cc automation
208 Also stores in self._generated_cc to make ShowActions() faster.
211 process_tags: Process tags as if they were aliases
212 cover_fname: If non-None the name of the cover letter.
213 raise_on_error: True to raise an error when an alias fails to match,
214 False to just print a message.
215 add_maintainers: Call the get_maintainers to CC maintainers
217 Filename of temp file created
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')
223 for commit in self.commits:
226 list += gitutil.BuildEmailList(commit.tags,
227 raise_on_error=raise_on_error)
228 list += gitutil.BuildEmailList(commit.cc_list,
229 raise_on_error=raise_on_error)
231 list += get_maintainer.GetMaintainer(commit.patch)
233 print(commit.patch, ', '.join(set(list)), file=fd)
234 self._generated_cc[commit.patch] = list
237 cover_cc = gitutil.BuildEmailList(self.get('cover_cc', ''))
238 print(cover_fname, ', '.join(set(cover_cc + all_ccs)), file=fd)
243 def AddChange(self, version, commit, info):
244 """Add a new change line to a version.
246 This will later appear in the change log.
249 version: version number to add change list to
250 info: change line for this version
252 if not self.changes.get(version):
253 self.changes[version] = []
254 self.changes[version].append([commit, info])
256 def GetPatchPrefix(self):
257 """Get the patch version string
260 Patch string, like 'RFC PATCH v5' or just 'PATCH'
262 git_prefix = gitutil.GetDefaultSubjectPrefix()
264 git_prefix = '%s][' % git_prefix
269 if self.get('version'):
270 version = ' v%s' % self['version']
272 # Get patch name prefix
274 if self.get('prefix'):
275 prefix = '%s ' % self['prefix']
276 return '%s%sPATCH%s' % (git_prefix, prefix, version)