blob: 4fe465e9ab81714c9ee392f32959814cbbc9c4ae [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass0d24de92012-01-14 15:12:45 +00002# Copyright (c) 2011 The Chromium OS Authors.
3#
Simon Glass0d24de92012-01-14 15:12:45 +00004
Douglas Anderson833e4192019-09-27 09:23:56 -07005import datetime
Wu, Josh35ce2dc2015-04-03 10:51:17 +08006import math
Simon Glass0d24de92012-01-14 15:12:45 +00007import os
8import re
9import shutil
10import tempfile
11
Simon Glassbf776672020-04-17 18:09:04 -060012from patman import command
13from patman import commit
14from patman import gitutil
15from patman.series import Series
Simon Glass0d24de92012-01-14 15:12:45 +000016
17# Tags that we detect and remove
Douglas Anderson833e4192019-09-27 09:23:56 -070018re_remove = re.compile('^BUG=|^TEST=|^BRANCH=|^Review URL:'
Simon Glass3fefd5e2013-04-03 11:01:39 +000019 '|Reviewed-on:|Commit-\w*:')
Simon Glass0d24de92012-01-14 15:12:45 +000020
21# Lines which are allowed after a TEST= line
22re_allowed_after_test = re.compile('^Signed-off-by:')
23
Ilya Yanok05e5b732012-08-06 23:46:05 +000024# Signoffs
Simon Glass102061b2014-04-20 10:50:14 -060025re_signoff = re.compile('^Signed-off-by: *(.*)')
Ilya Yanok05e5b732012-08-06 23:46:05 +000026
Sean Anderson6949f702020-05-04 16:28:34 -040027# Cover letter tag
28re_cover = re.compile('^Cover-([a-z-]*): *(.*)')
Simon Glassfe2f8d92013-03-20 16:43:00 +000029
Simon Glass0d24de92012-01-14 15:12:45 +000030# Patch series tag
Albert ARIBAUD5c8fdd92013-11-12 11:14:41 +010031re_series_tag = re.compile('^Series-([a-z-]*): *(.*)')
32
Douglas Anderson833e4192019-09-27 09:23:56 -070033# Change-Id will be used to generate the Message-Id and then be stripped
34re_change_id = re.compile('^Change-Id: *(.*)')
35
Albert ARIBAUD5c8fdd92013-11-12 11:14:41 +010036# Commit series tag
37re_commit_tag = re.compile('^Commit-([a-z-]*): *(.*)')
Simon Glass0d24de92012-01-14 15:12:45 +000038
39# Commit tags that we want to collect and keep
Simon Glass659c89d2014-02-16 08:23:47 -070040re_tag = re.compile('^(Tested-by|Acked-by|Reviewed-by|Patch-cc): (.*)')
Simon Glass0d24de92012-01-14 15:12:45 +000041
42# The start of a new commit in the git log
Doug Anderson68618282013-03-01 11:11:07 +000043re_commit = re.compile('^commit ([0-9a-f]*)$')
Simon Glass0d24de92012-01-14 15:12:45 +000044
45# We detect these since checkpatch doesn't always do it
46re_space_before_tab = re.compile('^[+].* \t')
47
Sean Anderson0411fff2020-05-04 16:28:35 -040048# Match indented lines for changes
49re_leading_whitespace = re.compile('^\s')
50
Simon Glass0d24de92012-01-14 15:12:45 +000051# States we can be in - can we use range() and still have comments?
52STATE_MSG_HEADER = 0 # Still in the message header
53STATE_PATCH_SUBJECT = 1 # In patch subject (first line of log for a commit)
54STATE_PATCH_HEADER = 2 # In patch header (after the subject)
55STATE_DIFFS = 3 # In the diff part (past --- line)
56
57class PatchStream:
58 """Class for detecting/injecting tags in a patch or series of patches
59
60 We support processing the output of 'git log' to read out the tags we
61 are interested in. We can also process a patch file in order to remove
62 unwanted tags or inject additional ones. These correspond to the two
63 phases of processing.
64 """
65 def __init__(self, series, name=None, is_log=False):
66 self.skip_blank = False # True to skip a single blank line
67 self.found_test = False # Found a TEST= line
Sean Anderson6949f702020-05-04 16:28:34 -040068 self.lines_after_test = 0 # Number of lines found after TEST=
Simon Glass0d24de92012-01-14 15:12:45 +000069 self.warn = [] # List of warnings we have collected
70 self.linenum = 1 # Output line number we are up to
71 self.in_section = None # Name of start...END section we are in
72 self.notes = [] # Series notes
73 self.section = [] # The current section...END section
74 self.series = series # Info about the patch series
75 self.is_log = is_log # True if indent like git log
Sean Anderson6949f702020-05-04 16:28:34 -040076 self.in_change = None # Name of the change list we are in
77 self.change_version = 0 # Non-zero if we are in a change list
Sean Anderson0411fff2020-05-04 16:28:35 -040078 self.change_lines = [] # Lines of the current change
Simon Glass0d24de92012-01-14 15:12:45 +000079 self.blank_count = 0 # Number of blank lines stored up
80 self.state = STATE_MSG_HEADER # What state are we in?
Simon Glass0d24de92012-01-14 15:12:45 +000081 self.signoff = [] # Contents of signoff line
82 self.commit = None # Current commit
83
84 def AddToSeries(self, line, name, value):
85 """Add a new Series-xxx tag.
86
87 When a Series-xxx tag is detected, we come here to record it, if we
88 are scanning a 'git log'.
89
90 Args:
91 line: Source line containing tag (useful for debug/error messages)
92 name: Tag name (part after 'Series-')
93 value: Tag value (part after 'Series-xxx: ')
94 """
95 if name == 'notes':
96 self.in_section = name
97 self.skip_blank = False
98 if self.is_log:
99 self.series.AddTag(self.commit, line, name, value)
100
Albert ARIBAUD5c8fdd92013-11-12 11:14:41 +0100101 def AddToCommit(self, line, name, value):
102 """Add a new Commit-xxx tag.
103
104 When a Commit-xxx tag is detected, we come here to record it.
105
106 Args:
107 line: Source line containing tag (useful for debug/error messages)
108 name: Tag name (part after 'Commit-')
109 value: Tag value (part after 'Commit-xxx: ')
110 """
111 if name == 'notes':
112 self.in_section = 'commit-' + name
113 self.skip_blank = False
114
Simon Glass0d24de92012-01-14 15:12:45 +0000115 def CloseCommit(self):
116 """Save the current commit into our commit list, and reset our state"""
117 if self.commit and self.is_log:
118 self.series.AddCommit(self.commit)
119 self.commit = None
Bin Meng0d577182016-06-26 23:24:30 -0700120 # If 'END' is missing in a 'Cover-letter' section, and that section
121 # happens to show up at the very end of the commit message, this is
122 # the chance for us to fix it up.
123 if self.in_section == 'cover' and self.is_log:
124 self.series.cover = self.section
125 self.in_section = None
126 self.skip_blank = True
127 self.section = []
Simon Glass0d24de92012-01-14 15:12:45 +0000128
Sean Anderson6949f702020-05-04 16:28:34 -0400129 def ParseVersion(self, value, line):
130 """Parse a version from a *-changes tag
131
132 Args:
133 value: Tag value (part after 'xxx-changes: '
134 line: Source line containing tag
135
136 Returns:
137 The version as an integer
138 """
139 try:
140 return int(value)
141 except ValueError as str:
142 raise ValueError("%s: Cannot decode version info '%s'" %
143 (self.commit.hash, line))
144
Sean Anderson0411fff2020-05-04 16:28:35 -0400145 def FinalizeChange(self):
146 """Finalize a (multi-line) change and add it to the series or commit"""
147 if not self.change_lines:
148 return
149 change = '\n'.join(self.change_lines)
150
151 if self.in_change == 'Series':
152 self.series.AddChange(self.change_version, self.commit, change)
153 elif self.in_change == 'Cover':
154 self.series.AddChange(self.change_version, None, change)
155 elif self.in_change == 'Commit':
156 self.commit.AddChange(self.change_version, change)
157 self.change_lines = []
158
Simon Glass0d24de92012-01-14 15:12:45 +0000159 def ProcessLine(self, line):
160 """Process a single line of a patch file or commit log
161
162 This process a line and returns a list of lines to output. The list
163 may be empty or may contain multiple output lines.
164
165 This is where all the complicated logic is located. The class's
166 state is used to move between different states and detect things
167 properly.
168
169 We can be in one of two modes:
170 self.is_log == True: This is 'git log' mode, where most output is
171 indented by 4 characters and we are scanning for tags
172
173 self.is_log == False: This is 'patch' mode, where we already have
174 all the tags, and are processing patches to remove junk we
175 don't want, and add things we think are required.
176
177 Args:
178 line: text line to process
179
180 Returns:
181 list of output lines, or [] if nothing should be output
182 """
183 # Initially we have no output. Prepare the input line string
184 out = []
185 line = line.rstrip('\n')
Scott Wood4b89b812014-09-25 14:30:46 -0500186
187 commit_match = re_commit.match(line) if self.is_log else None
188
Simon Glass0d24de92012-01-14 15:12:45 +0000189 if self.is_log:
190 if line[:4] == ' ':
191 line = line[4:]
192
193 # Handle state transition and skipping blank lines
Albert ARIBAUD5c8fdd92013-11-12 11:14:41 +0100194 series_tag_match = re_series_tag.match(line)
Douglas Anderson833e4192019-09-27 09:23:56 -0700195 change_id_match = re_change_id.match(line)
Albert ARIBAUD5c8fdd92013-11-12 11:14:41 +0100196 commit_tag_match = re_commit_tag.match(line)
Bin Menge7df2182016-06-26 23:24:28 -0700197 cover_match = re_cover.match(line)
Simon Glass102061b2014-04-20 10:50:14 -0600198 signoff_match = re_signoff.match(line)
Sean Anderson0411fff2020-05-04 16:28:35 -0400199 leading_whitespace_match = re_leading_whitespace.match(line)
Simon Glass0d24de92012-01-14 15:12:45 +0000200 tag_match = None
201 if self.state == STATE_PATCH_HEADER:
202 tag_match = re_tag.match(line)
203 is_blank = not line.strip()
204 if is_blank:
205 if (self.state == STATE_MSG_HEADER
206 or self.state == STATE_PATCH_SUBJECT):
207 self.state += 1
208
209 # We don't have a subject in the text stream of patch files
210 # It has its own line with a Subject: tag
211 if not self.is_log and self.state == STATE_PATCH_SUBJECT:
212 self.state += 1
213 elif commit_match:
214 self.state = STATE_MSG_HEADER
215
Bin Meng94fbd3e2016-06-26 23:24:32 -0700216 # If a tag is detected, or a new commit starts
Douglas Anderson833e4192019-09-27 09:23:56 -0700217 if series_tag_match or commit_tag_match or change_id_match or \
Sean Anderson6949f702020-05-04 16:28:34 -0400218 cover_match or signoff_match or self.state == STATE_MSG_HEADER:
Bin Meng57b6b192016-06-26 23:24:31 -0700219 # but we are already in a section, this means 'END' is missing
220 # for that section, fix it up.
Bin Meng13b98d92016-06-26 23:24:29 -0700221 if self.in_section:
222 self.warn.append("Missing 'END' in section '%s'" % self.in_section)
223 if self.in_section == 'cover':
224 self.series.cover = self.section
225 elif self.in_section == 'notes':
226 if self.is_log:
227 self.series.notes += self.section
228 elif self.in_section == 'commit-notes':
229 if self.is_log:
230 self.commit.notes += self.section
231 else:
232 self.warn.append("Unknown section '%s'" % self.in_section)
233 self.in_section = None
234 self.skip_blank = True
235 self.section = []
Bin Meng57b6b192016-06-26 23:24:31 -0700236 # but we are already in a change list, that means a blank line
237 # is missing, fix it up.
238 if self.in_change:
Sean Anderson6949f702020-05-04 16:28:34 -0400239 self.warn.append("Missing 'blank line' in section '%s-changes'" % self.in_change)
Sean Anderson0411fff2020-05-04 16:28:35 -0400240 self.FinalizeChange()
Sean Anderson6949f702020-05-04 16:28:34 -0400241 self.in_change = None
242 self.change_version = 0
Bin Meng13b98d92016-06-26 23:24:29 -0700243
Simon Glass0d24de92012-01-14 15:12:45 +0000244 # If we are in a section, keep collecting lines until we see END
245 if self.in_section:
246 if line == 'END':
247 if self.in_section == 'cover':
248 self.series.cover = self.section
249 elif self.in_section == 'notes':
250 if self.is_log:
251 self.series.notes += self.section
Albert ARIBAUD5c8fdd92013-11-12 11:14:41 +0100252 elif self.in_section == 'commit-notes':
253 if self.is_log:
254 self.commit.notes += self.section
Simon Glass0d24de92012-01-14 15:12:45 +0000255 else:
256 self.warn.append("Unknown section '%s'" % self.in_section)
257 self.in_section = None
258 self.skip_blank = True
259 self.section = []
260 else:
261 self.section.append(line)
262
263 # Detect the commit subject
264 elif not is_blank and self.state == STATE_PATCH_SUBJECT:
265 self.commit.subject = line
266
267 # Detect the tags we want to remove, and skip blank lines
Albert ARIBAUD5c8fdd92013-11-12 11:14:41 +0100268 elif re_remove.match(line) and not commit_tag_match:
Simon Glass0d24de92012-01-14 15:12:45 +0000269 self.skip_blank = True
270
271 # TEST= should be the last thing in the commit, so remove
272 # everything after it
273 if line.startswith('TEST='):
274 self.found_test = True
275 elif self.skip_blank and is_blank:
276 self.skip_blank = False
277
Sean Anderson6949f702020-05-04 16:28:34 -0400278 # Detect Cover-xxx tags
Bin Menge7df2182016-06-26 23:24:28 -0700279 elif cover_match:
Sean Anderson6949f702020-05-04 16:28:34 -0400280 name = cover_match.group(1)
281 value = cover_match.group(2)
282 if name == 'letter':
283 self.in_section = 'cover'
284 self.skip_blank = False
285 elif name == 'letter-cc':
286 self.AddToSeries(line, 'cover-cc', value)
287 elif name == 'changes':
288 self.in_change = 'Cover'
289 self.change_version = self.ParseVersion(value, line)
Simon Glassfe2f8d92013-03-20 16:43:00 +0000290
Simon Glass0d24de92012-01-14 15:12:45 +0000291 # If we are in a change list, key collected lines until a blank one
292 elif self.in_change:
293 if is_blank:
294 # Blank line ends this change list
Sean Anderson0411fff2020-05-04 16:28:35 -0400295 self.FinalizeChange()
Sean Anderson6949f702020-05-04 16:28:34 -0400296 self.in_change = None
297 self.change_version = 0
Simon Glass102061b2014-04-20 10:50:14 -0600298 elif line == '---':
Sean Anderson0411fff2020-05-04 16:28:35 -0400299 self.FinalizeChange()
Sean Anderson6949f702020-05-04 16:28:34 -0400300 self.in_change = None
301 self.change_version = 0
Ilya Yanok05e5b732012-08-06 23:46:05 +0000302 out = self.ProcessLine(line)
Sean Anderson0411fff2020-05-04 16:28:35 -0400303 elif self.is_log:
304 if not leading_whitespace_match:
305 self.FinalizeChange()
306 self.change_lines.append(line)
Simon Glass0d24de92012-01-14 15:12:45 +0000307 self.skip_blank = False
308
309 # Detect Series-xxx tags
Albert ARIBAUD5c8fdd92013-11-12 11:14:41 +0100310 elif series_tag_match:
311 name = series_tag_match.group(1)
312 value = series_tag_match.group(2)
Simon Glass0d24de92012-01-14 15:12:45 +0000313 if name == 'changes':
314 # value is the version number: e.g. 1, or 2
Sean Anderson6949f702020-05-04 16:28:34 -0400315 self.in_change = 'Series'
316 self.change_version = self.ParseVersion(value, line)
Simon Glass0d24de92012-01-14 15:12:45 +0000317 else:
318 self.AddToSeries(line, name, value)
319 self.skip_blank = True
320
Douglas Anderson833e4192019-09-27 09:23:56 -0700321 # Detect Change-Id tags
322 elif change_id_match:
323 value = change_id_match.group(1)
324 if self.is_log:
325 if self.commit.change_id:
326 raise ValueError("%s: Two Change-Ids: '%s' vs. '%s'" %
327 (self.commit.hash, self.commit.change_id, value))
328 self.commit.change_id = value
329 self.skip_blank = True
330
Albert ARIBAUD5c8fdd92013-11-12 11:14:41 +0100331 # Detect Commit-xxx tags
332 elif commit_tag_match:
333 name = commit_tag_match.group(1)
334 value = commit_tag_match.group(2)
335 if name == 'notes':
336 self.AddToCommit(line, name, value)
337 self.skip_blank = True
Sean Anderson6949f702020-05-04 16:28:34 -0400338 elif name == 'changes':
339 self.in_change = 'Commit'
340 self.change_version = self.ParseVersion(value, line)
Albert ARIBAUD5c8fdd92013-11-12 11:14:41 +0100341
Simon Glass0d24de92012-01-14 15:12:45 +0000342 # Detect the start of a new commit
343 elif commit_match:
344 self.CloseCommit()
Simon Glass0b5b4092014-10-15 02:27:00 -0600345 self.commit = commit.Commit(commit_match.group(1))
Simon Glass0d24de92012-01-14 15:12:45 +0000346
347 # Detect tags in the commit message
348 elif tag_match:
Simon Glass0d24de92012-01-14 15:12:45 +0000349 # Remove Tested-by self, since few will take much notice
Ilya Yanokc7379142012-08-06 23:46:08 +0000350 if (tag_match.group(1) == 'Tested-by' and
Simon Glass0d24de92012-01-14 15:12:45 +0000351 tag_match.group(2).find(os.getenv('USER') + '@') != -1):
352 self.warn.append("Ignoring %s" % line)
Simon Glass659c89d2014-02-16 08:23:47 -0700353 elif tag_match.group(1) == 'Patch-cc':
Simon Glass0d24de92012-01-14 15:12:45 +0000354 self.commit.AddCc(tag_match.group(2).split(','))
355 else:
Simon Glassd0c57192014-08-28 09:43:38 -0600356 out = [line]
Simon Glass0d24de92012-01-14 15:12:45 +0000357
Simon Glass102061b2014-04-20 10:50:14 -0600358 # Suppress duplicate signoffs
359 elif signoff_match:
Simon Glasse752edc2014-08-28 09:43:35 -0600360 if (self.is_log or not self.commit or
Simon Glass6be6b6b2014-05-13 12:14:02 -0600361 self.commit.CheckDuplicateSignoff(signoff_match.group(1))):
Simon Glass102061b2014-04-20 10:50:14 -0600362 out = [line]
363
Simon Glass0d24de92012-01-14 15:12:45 +0000364 # Well that means this is an ordinary line
365 else:
Simon Glass0d24de92012-01-14 15:12:45 +0000366 # Look for space before tab
367 m = re_space_before_tab.match(line)
368 if m:
369 self.warn.append('Line %d/%d has space before tab' %
370 (self.linenum, m.start()))
371
372 # OK, we have a valid non-blank line
373 out = [line]
374 self.linenum += 1
375 self.skip_blank = False
376 if self.state == STATE_DIFFS:
377 pass
378
379 # If this is the start of the diffs section, emit our tags and
380 # change log
381 elif line == '---':
382 self.state = STATE_DIFFS
383
Sean Anderson6949f702020-05-04 16:28:34 -0400384 # Output the tags (signoff first), then change list
Simon Glass0d24de92012-01-14 15:12:45 +0000385 out = []
Simon Glass0d24de92012-01-14 15:12:45 +0000386 log = self.series.MakeChangeLog(self.commit)
Simon Glasse752edc2014-08-28 09:43:35 -0600387 out += [line]
388 if self.commit:
389 out += self.commit.notes
390 out += [''] + log
Simon Glass0d24de92012-01-14 15:12:45 +0000391 elif self.found_test:
392 if not re_allowed_after_test.match(line):
393 self.lines_after_test += 1
394
395 return out
396
397 def Finalize(self):
398 """Close out processing of this patch stream"""
Sean Anderson0411fff2020-05-04 16:28:35 -0400399 self.FinalizeChange()
Simon Glass0d24de92012-01-14 15:12:45 +0000400 self.CloseCommit()
401 if self.lines_after_test:
402 self.warn.append('Found %d lines after TEST=' %
403 self.lines_after_test)
404
Douglas Anderson833e4192019-09-27 09:23:56 -0700405 def WriteMessageId(self, outfd):
406 """Write the Message-Id into the output.
407
408 This is based on the Change-Id in the original patch, the version,
409 and the prefix.
410
411 Args:
412 outfd: Output stream file object
413 """
414 if not self.commit.change_id:
415 return
416
417 # If the count is -1 we're testing, so use a fixed time
418 if self.commit.count == -1:
419 time_now = datetime.datetime(1999, 12, 31, 23, 59, 59)
420 else:
421 time_now = datetime.datetime.now()
422
423 # In theory there is email.utils.make_msgid() which would be nice
424 # to use, but it already produces something way too long and thus
425 # will produce ugly commit lines if someone throws this into
426 # a "Link:" tag in the final commit. So (sigh) roll our own.
427
428 # Start with the time; presumably we wouldn't send the same series
429 # with the same Change-Id at the exact same second.
430 parts = [time_now.strftime("%Y%m%d%H%M%S")]
431
432 # These seem like they would be nice to include.
433 if 'prefix' in self.series:
434 parts.append(self.series['prefix'])
435 if 'version' in self.series:
436 parts.append("v%s" % self.series['version'])
437
438 parts.append(str(self.commit.count + 1))
439
440 # The Change-Id must be last, right before the @
441 parts.append(self.commit.change_id)
442
443 # Join parts together with "." and write it out.
444 outfd.write('Message-Id: <%s@changeid>\n' % '.'.join(parts))
445
Simon Glass0d24de92012-01-14 15:12:45 +0000446 def ProcessStream(self, infd, outfd):
447 """Copy a stream from infd to outfd, filtering out unwanting things.
448
449 This is used to process patch files one at a time.
450
451 Args:
452 infd: Input stream file object
453 outfd: Output stream file object
454 """
455 # Extract the filename from each diff, for nice warnings
456 fname = None
457 last_fname = None
458 re_fname = re.compile('diff --git a/(.*) b/.*')
Douglas Anderson833e4192019-09-27 09:23:56 -0700459
460 self.WriteMessageId(outfd)
461
Simon Glass0d24de92012-01-14 15:12:45 +0000462 while True:
463 line = infd.readline()
464 if not line:
465 break
466 out = self.ProcessLine(line)
467
468 # Try to detect blank lines at EOF
469 for line in out:
470 match = re_fname.match(line)
471 if match:
472 last_fname = fname
473 fname = match.group(1)
474 if line == '+':
475 self.blank_count += 1
476 else:
477 if self.blank_count and (line == '-- ' or match):
478 self.warn.append("Found possible blank line(s) at "
479 "end of file '%s'" % last_fname)
480 outfd.write('+\n' * self.blank_count)
481 outfd.write(line + '\n')
482 self.blank_count = 0
483 self.Finalize()
484
485
Simon Glasse62f9052012-12-15 10:42:06 +0000486def GetMetaDataForList(commit_range, git_dir=None, count=None,
Simon Glass950a2312014-09-05 19:00:23 -0600487 series = None, allow_overwrite=False):
Simon Glasse62f9052012-12-15 10:42:06 +0000488 """Reads out patch series metadata from the commits
489
490 This does a 'git log' on the relevant commits and pulls out the tags we
491 are interested in.
492
493 Args:
494 commit_range: Range of commits to count (e.g. 'HEAD..base')
495 git_dir: Path to git repositiory (None to use default)
496 count: Number of commits to list, or None for no limit
497 series: Series object to add information into. By default a new series
498 is started.
Simon Glass950a2312014-09-05 19:00:23 -0600499 allow_overwrite: Allow tags to overwrite an existing tag
Simon Glasse62f9052012-12-15 10:42:06 +0000500 Returns:
501 A Series object containing information about the commits.
502 """
Simon Glass891b7a02014-09-05 19:00:19 -0600503 if not series:
504 series = Series()
Simon Glass950a2312014-09-05 19:00:23 -0600505 series.allow_overwrite = allow_overwrite
Simon Glass9ad96982016-03-06 19:45:33 -0700506 params = gitutil.LogCmd(commit_range, reverse=True, count=count,
Simon Glasscda2a612014-08-09 15:33:10 -0600507 git_dir=git_dir)
508 stdout = command.RunPipe([params], capture=True).stdout
Simon Glasse62f9052012-12-15 10:42:06 +0000509 ps = PatchStream(series, is_log=True)
510 for line in stdout.splitlines():
511 ps.ProcessLine(line)
512 ps.Finalize()
513 return series
514
Simon Glass0d24de92012-01-14 15:12:45 +0000515def GetMetaData(start, count):
516 """Reads out patch series metadata from the commits
517
518 This does a 'git log' on the relevant commits and pulls out the tags we
519 are interested in.
520
521 Args:
522 start: Commit to start from: 0=HEAD, 1=next one, etc.
523 count: Number of commits to list
524 """
Simon Glasse62f9052012-12-15 10:42:06 +0000525 return GetMetaDataForList('HEAD~%d' % start, None, count)
Simon Glass0d24de92012-01-14 15:12:45 +0000526
Simon Glass6e87ae12017-05-29 15:31:31 -0600527def GetMetaDataForTest(text):
528 """Process metadata from a file containing a git log. Used for tests
529
530 Args:
531 text:
532 """
533 series = Series()
534 ps = PatchStream(series, is_log=True)
535 for line in text.splitlines():
536 ps.ProcessLine(line)
537 ps.Finalize()
538 return series
539
Simon Glass0d24de92012-01-14 15:12:45 +0000540def FixPatch(backup_dir, fname, series, commit):
541 """Fix up a patch file, by adding/removing as required.
542
543 We remove our tags from the patch file, insert changes lists, etc.
544 The patch file is processed in place, and overwritten.
545
546 A backup file is put into backup_dir (if not None).
547
548 Args:
549 fname: Filename to patch file to process
550 series: Series information about this patch set
551 commit: Commit object for this patch file
552 Return:
553 A list of errors, or [] if all ok.
554 """
555 handle, tmpname = tempfile.mkstemp()
Simon Glass272cd852019-10-31 07:42:51 -0600556 outfd = os.fdopen(handle, 'w', encoding='utf-8')
557 infd = open(fname, 'r', encoding='utf-8')
Simon Glass0d24de92012-01-14 15:12:45 +0000558 ps = PatchStream(series)
559 ps.commit = commit
560 ps.ProcessStream(infd, outfd)
561 infd.close()
562 outfd.close()
563
564 # Create a backup file if required
565 if backup_dir:
566 shutil.copy(fname, os.path.join(backup_dir, os.path.basename(fname)))
567 shutil.move(tmpname, fname)
568 return ps.warn
569
570def FixPatches(series, fnames):
571 """Fix up a list of patches identified by filenames
572
573 The patch files are processed in place, and overwritten.
574
575 Args:
576 series: The series object
577 fnames: List of patch files to process
578 """
579 # Current workflow creates patches, so we shouldn't need a backup
580 backup_dir = None #tempfile.mkdtemp('clean-patch')
581 count = 0
582 for fname in fnames:
583 commit = series.commits[count]
584 commit.patch = fname
Douglas Anderson833e4192019-09-27 09:23:56 -0700585 commit.count = count
Simon Glass0d24de92012-01-14 15:12:45 +0000586 result = FixPatch(backup_dir, fname, series, commit)
587 if result:
Paul Burtona920a172016-09-27 16:03:50 +0100588 print('%d warnings for %s:' % (len(result), fname))
Simon Glass0d24de92012-01-14 15:12:45 +0000589 for warn in result:
Paul Burtona920a172016-09-27 16:03:50 +0100590 print('\t', warn)
Simon Glass0d24de92012-01-14 15:12:45 +0000591 print
592 count += 1
Paul Burtona920a172016-09-27 16:03:50 +0100593 print('Cleaned %d patches' % count)
Simon Glass0d24de92012-01-14 15:12:45 +0000594
595def InsertCoverLetter(fname, series, count):
596 """Inserts a cover letter with the required info into patch 0
597
598 Args:
599 fname: Input / output filename of the cover letter file
600 series: Series object
601 count: Number of patches in the series
602 """
603 fd = open(fname, 'r')
604 lines = fd.readlines()
605 fd.close()
606
607 fd = open(fname, 'w')
608 text = series.cover
609 prefix = series.GetPatchPrefix()
610 for line in lines:
611 if line.startswith('Subject:'):
Wu, Josh35ce2dc2015-04-03 10:51:17 +0800612 # if more than 10 or 100 patches, it should say 00/xx, 000/xxx, etc
613 zero_repeat = int(math.log10(count)) + 1
614 zero = '0' * zero_repeat
615 line = 'Subject: [%s %s/%d] %s\n' % (prefix, zero, count, text[0])
Simon Glass0d24de92012-01-14 15:12:45 +0000616
617 # Insert our cover letter
618 elif line.startswith('*** BLURB HERE ***'):
619 # First the blurb test
620 line = '\n'.join(text[1:]) + '\n'
621 if series.get('notes'):
622 line += '\n'.join(series.notes) + '\n'
623
624 # Now the change list
625 out = series.MakeChangeLog(None)
626 line += '\n' + '\n'.join(out)
627 fd.write(line)
628 fd.close()