Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 2 | # Copyright (c) 2011 The Chromium OS Authors. |
| 3 | # |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 4 | |
Simon Glass | c42b1d0 | 2020-10-29 21:46:17 -0600 | [diff] [blame] | 5 | """Handles parsing a stream of commits/emails from 'git log' or other source""" |
| 6 | |
Douglas Anderson | 52b5ee8 | 2019-09-27 09:23:56 -0700 | [diff] [blame] | 7 | import datetime |
Simon Glass | 06202d6 | 2020-10-29 21:46:27 -0600 | [diff] [blame] | 8 | import io |
Wu, Josh | 393aaa2 | 2015-04-03 10:51:17 +0800 | [diff] [blame] | 9 | import math |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 10 | import os |
| 11 | import re |
| 12 | import shutil |
| 13 | import tempfile |
| 14 | |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 15 | from patman import command |
| 16 | from patman import commit |
| 17 | from patman import gitutil |
| 18 | from patman.series import Series |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 19 | |
| 20 | # Tags that we detect and remove |
Simon Glass | 0602226 | 2020-10-29 21:46:18 -0600 | [diff] [blame] | 21 | RE_REMOVE = re.compile(r'^BUG=|^TEST=|^BRANCH=|^Review URL:' |
Simon Glass | c42b1d0 | 2020-10-29 21:46:17 -0600 | [diff] [blame] | 22 | r'|Reviewed-on:|Commit-\w*:') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 23 | |
| 24 | # Lines which are allowed after a TEST= line |
Simon Glass | 0602226 | 2020-10-29 21:46:18 -0600 | [diff] [blame] | 25 | RE_ALLOWED_AFTER_TEST = re.compile('^Signed-off-by:') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 26 | |
Ilya Yanok | 644c184 | 2012-08-06 23:46:05 +0000 | [diff] [blame] | 27 | # Signoffs |
Simon Glass | 0602226 | 2020-10-29 21:46:18 -0600 | [diff] [blame] | 28 | RE_SIGNOFF = re.compile('^Signed-off-by: *(.*)') |
Ilya Yanok | 644c184 | 2012-08-06 23:46:05 +0000 | [diff] [blame] | 29 | |
Sean Anderson | 48f46d6 | 2020-05-04 16:28:34 -0400 | [diff] [blame] | 30 | # Cover letter tag |
Simon Glass | 0602226 | 2020-10-29 21:46:18 -0600 | [diff] [blame] | 31 | RE_COVER = re.compile('^Cover-([a-z-]*): *(.*)') |
Simon Glass | c72f3da | 2013-03-20 16:43:00 +0000 | [diff] [blame] | 32 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 33 | # Patch series tag |
Simon Glass | 0602226 | 2020-10-29 21:46:18 -0600 | [diff] [blame] | 34 | RE_SERIES_TAG = re.compile('^Series-([a-z-]*): *(.*)') |
Albert ARIBAUD | d880efd | 2013-11-12 11:14:41 +0100 | [diff] [blame] | 35 | |
Douglas Anderson | 52b5ee8 | 2019-09-27 09:23:56 -0700 | [diff] [blame] | 36 | # Change-Id will be used to generate the Message-Id and then be stripped |
Simon Glass | 0602226 | 2020-10-29 21:46:18 -0600 | [diff] [blame] | 37 | RE_CHANGE_ID = re.compile('^Change-Id: *(.*)') |
Douglas Anderson | 52b5ee8 | 2019-09-27 09:23:56 -0700 | [diff] [blame] | 38 | |
Albert ARIBAUD | d880efd | 2013-11-12 11:14:41 +0100 | [diff] [blame] | 39 | # Commit series tag |
Simon Glass | 0602226 | 2020-10-29 21:46:18 -0600 | [diff] [blame] | 40 | RE_COMMIT_TAG = re.compile('^Commit-([a-z-]*): *(.*)') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 41 | |
| 42 | # Commit tags that we want to collect and keep |
Simon Glass | 0602226 | 2020-10-29 21:46:18 -0600 | [diff] [blame] | 43 | RE_TAG = re.compile('^(Tested-by|Acked-by|Reviewed-by|Patch-cc|Fixes): (.*)') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 44 | |
| 45 | # The start of a new commit in the git log |
Simon Glass | 0602226 | 2020-10-29 21:46:18 -0600 | [diff] [blame] | 46 | RE_COMMIT = re.compile('^commit ([0-9a-f]*)$') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 47 | |
| 48 | # We detect these since checkpatch doesn't always do it |
Simon Glass | 0602226 | 2020-10-29 21:46:18 -0600 | [diff] [blame] | 49 | RE_SPACE_BEFORE_TAB = re.compile('^[+].* \t') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 50 | |
Sean Anderson | 1a32f92 | 2020-05-04 16:28:35 -0400 | [diff] [blame] | 51 | # Match indented lines for changes |
Simon Glass | 0602226 | 2020-10-29 21:46:18 -0600 | [diff] [blame] | 52 | RE_LEADING_WHITESPACE = re.compile(r'^\s') |
Sean Anderson | 1a32f92 | 2020-05-04 16:28:35 -0400 | [diff] [blame] | 53 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 54 | # States we can be in - can we use range() and still have comments? |
| 55 | STATE_MSG_HEADER = 0 # Still in the message header |
| 56 | STATE_PATCH_SUBJECT = 1 # In patch subject (first line of log for a commit) |
| 57 | STATE_PATCH_HEADER = 2 # In patch header (after the subject) |
| 58 | STATE_DIFFS = 3 # In the diff part (past --- line) |
| 59 | |
| 60 | class PatchStream: |
| 61 | """Class for detecting/injecting tags in a patch or series of patches |
| 62 | |
| 63 | We support processing the output of 'git log' to read out the tags we |
| 64 | are interested in. We can also process a patch file in order to remove |
| 65 | unwanted tags or inject additional ones. These correspond to the two |
| 66 | phases of processing. |
| 67 | """ |
Simon Glass | 5f6caa5 | 2020-10-29 21:46:21 -0600 | [diff] [blame] | 68 | def __init__(self, series, is_log=False): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 69 | self.skip_blank = False # True to skip a single blank line |
| 70 | self.found_test = False # Found a TEST= line |
Sean Anderson | 48f46d6 | 2020-05-04 16:28:34 -0400 | [diff] [blame] | 71 | self.lines_after_test = 0 # Number of lines found after TEST= |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 72 | self.linenum = 1 # Output line number we are up to |
| 73 | self.in_section = None # Name of start...END section we are in |
| 74 | self.notes = [] # Series notes |
| 75 | self.section = [] # The current section...END section |
| 76 | self.series = series # Info about the patch series |
| 77 | self.is_log = is_log # True if indent like git log |
Sean Anderson | 48f46d6 | 2020-05-04 16:28:34 -0400 | [diff] [blame] | 78 | self.in_change = None # Name of the change list we are in |
| 79 | self.change_version = 0 # Non-zero if we are in a change list |
Sean Anderson | 1a32f92 | 2020-05-04 16:28:35 -0400 | [diff] [blame] | 80 | self.change_lines = [] # Lines of the current change |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 81 | self.blank_count = 0 # Number of blank lines stored up |
| 82 | self.state = STATE_MSG_HEADER # What state are we in? |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 83 | self.commit = None # Current commit |
| 84 | |
Simon Glass | 06202d6 | 2020-10-29 21:46:27 -0600 | [diff] [blame] | 85 | @staticmethod |
| 86 | def process_text(text, is_comment=False): |
| 87 | """Process some text through this class using a default Commit/Series |
| 88 | |
| 89 | Args: |
| 90 | text (str): Text to parse |
| 91 | is_comment (bool): True if this is a comment rather than a patch. |
| 92 | If True, PatchStream doesn't expect a patch subject at the |
| 93 | start, but jumps straight into the body |
| 94 | |
| 95 | Returns: |
| 96 | PatchStream: object with results |
| 97 | """ |
| 98 | pstrm = PatchStream(Series()) |
| 99 | pstrm.commit = commit.Commit(None) |
| 100 | infd = io.StringIO(text) |
| 101 | outfd = io.StringIO() |
| 102 | if is_comment: |
| 103 | pstrm.state = STATE_PATCH_HEADER |
| 104 | pstrm.process_stream(infd, outfd) |
| 105 | return pstrm |
| 106 | |
Simon Glass | b580039 | 2020-10-29 21:46:23 -0600 | [diff] [blame] | 107 | def _add_warn(self, warn): |
Simon Glass | 6d00f6c | 2020-10-29 21:46:24 -0600 | [diff] [blame] | 108 | """Add a new warning to report to the user about the current commit |
| 109 | |
| 110 | The new warning is added to the current commit if not already present. |
Simon Glass | b580039 | 2020-10-29 21:46:23 -0600 | [diff] [blame] | 111 | |
| 112 | Args: |
| 113 | warn (str): Warning to report |
Simon Glass | 6d00f6c | 2020-10-29 21:46:24 -0600 | [diff] [blame] | 114 | |
| 115 | Raises: |
| 116 | ValueError: Warning is generated with no commit associated |
Simon Glass | b580039 | 2020-10-29 21:46:23 -0600 | [diff] [blame] | 117 | """ |
Simon Glass | 6d00f6c | 2020-10-29 21:46:24 -0600 | [diff] [blame] | 118 | if not self.commit: |
| 119 | raise ValueError('Warning outside commit: %s' % warn) |
| 120 | if warn not in self.commit.warn: |
| 121 | self.commit.warn.append(warn) |
Simon Glass | b580039 | 2020-10-29 21:46:23 -0600 | [diff] [blame] | 122 | |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 123 | def _add_to_series(self, line, name, value): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 124 | """Add a new Series-xxx tag. |
| 125 | |
| 126 | When a Series-xxx tag is detected, we come here to record it, if we |
| 127 | are scanning a 'git log'. |
| 128 | |
| 129 | Args: |
Simon Glass | 06127f9 | 2020-10-29 21:46:22 -0600 | [diff] [blame] | 130 | line (str): Source line containing tag (useful for debug/error |
| 131 | messages) |
| 132 | name (str): Tag name (part after 'Series-') |
| 133 | value (str): Tag value (part after 'Series-xxx: ') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 134 | """ |
| 135 | if name == 'notes': |
| 136 | self.in_section = name |
| 137 | self.skip_blank = False |
| 138 | if self.is_log: |
Simon Glass | 8093a8f | 2020-10-29 21:46:25 -0600 | [diff] [blame] | 139 | warn = self.series.AddTag(self.commit, line, name, value) |
| 140 | if warn: |
| 141 | self.commit.warn.append(warn) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 142 | |
Simon Glass | 5f6caa5 | 2020-10-29 21:46:21 -0600 | [diff] [blame] | 143 | def _add_to_commit(self, name): |
Albert ARIBAUD | d880efd | 2013-11-12 11:14:41 +0100 | [diff] [blame] | 144 | """Add a new Commit-xxx tag. |
| 145 | |
| 146 | When a Commit-xxx tag is detected, we come here to record it. |
| 147 | |
| 148 | Args: |
Simon Glass | 06127f9 | 2020-10-29 21:46:22 -0600 | [diff] [blame] | 149 | name (str): Tag name (part after 'Commit-') |
Albert ARIBAUD | d880efd | 2013-11-12 11:14:41 +0100 | [diff] [blame] | 150 | """ |
| 151 | if name == 'notes': |
| 152 | self.in_section = 'commit-' + name |
| 153 | self.skip_blank = False |
| 154 | |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 155 | def _add_commit_rtag(self, rtag_type, who): |
Simon Glass | 08e91be | 2020-07-05 21:41:57 -0600 | [diff] [blame] | 156 | """Add a response tag to the current commit |
| 157 | |
| 158 | Args: |
Simon Glass | 06127f9 | 2020-10-29 21:46:22 -0600 | [diff] [blame] | 159 | rtag_type (str): rtag type (e.g. 'Reviewed-by') |
| 160 | who (str): Person who gave that rtag, e.g. |
| 161 | 'Fred Bloggs <fred@bloggs.org>' |
Simon Glass | 08e91be | 2020-07-05 21:41:57 -0600 | [diff] [blame] | 162 | """ |
| 163 | self.commit.AddRtag(rtag_type, who) |
| 164 | |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 165 | def _close_commit(self): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 166 | """Save the current commit into our commit list, and reset our state""" |
| 167 | if self.commit and self.is_log: |
| 168 | self.series.AddCommit(self.commit) |
| 169 | self.commit = None |
Bin Meng | d23121c | 2016-06-26 23:24:30 -0700 | [diff] [blame] | 170 | # If 'END' is missing in a 'Cover-letter' section, and that section |
| 171 | # happens to show up at the very end of the commit message, this is |
| 172 | # the chance for us to fix it up. |
| 173 | if self.in_section == 'cover' and self.is_log: |
| 174 | self.series.cover = self.section |
| 175 | self.in_section = None |
| 176 | self.skip_blank = True |
| 177 | self.section = [] |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 178 | |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 179 | def _parse_version(self, value, line): |
Sean Anderson | 48f46d6 | 2020-05-04 16:28:34 -0400 | [diff] [blame] | 180 | """Parse a version from a *-changes tag |
| 181 | |
| 182 | Args: |
Simon Glass | 06127f9 | 2020-10-29 21:46:22 -0600 | [diff] [blame] | 183 | value (str): Tag value (part after 'xxx-changes: ' |
| 184 | line (str): Source line containing tag |
Sean Anderson | 48f46d6 | 2020-05-04 16:28:34 -0400 | [diff] [blame] | 185 | |
| 186 | Returns: |
Simon Glass | 06127f9 | 2020-10-29 21:46:22 -0600 | [diff] [blame] | 187 | int: The version as an integer |
| 188 | |
| 189 | Raises: |
| 190 | ValueError: the value cannot be converted |
Sean Anderson | 48f46d6 | 2020-05-04 16:28:34 -0400 | [diff] [blame] | 191 | """ |
| 192 | try: |
| 193 | return int(value) |
Simon Glass | f9b6e0f | 2020-10-29 21:46:20 -0600 | [diff] [blame] | 194 | except ValueError: |
Sean Anderson | 48f46d6 | 2020-05-04 16:28:34 -0400 | [diff] [blame] | 195 | raise ValueError("%s: Cannot decode version info '%s'" % |
Simon Glass | c42b1d0 | 2020-10-29 21:46:17 -0600 | [diff] [blame] | 196 | (self.commit.hash, line)) |
Sean Anderson | 48f46d6 | 2020-05-04 16:28:34 -0400 | [diff] [blame] | 197 | |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 198 | def _finalise_change(self): |
| 199 | """_finalise a (multi-line) change and add it to the series or commit""" |
Sean Anderson | 1a32f92 | 2020-05-04 16:28:35 -0400 | [diff] [blame] | 200 | if not self.change_lines: |
| 201 | return |
| 202 | change = '\n'.join(self.change_lines) |
| 203 | |
| 204 | if self.in_change == 'Series': |
| 205 | self.series.AddChange(self.change_version, self.commit, change) |
| 206 | elif self.in_change == 'Cover': |
| 207 | self.series.AddChange(self.change_version, None, change) |
| 208 | elif self.in_change == 'Commit': |
| 209 | self.commit.AddChange(self.change_version, change) |
| 210 | self.change_lines = [] |
| 211 | |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 212 | def process_line(self, line): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 213 | """Process a single line of a patch file or commit log |
| 214 | |
| 215 | This process a line and returns a list of lines to output. The list |
| 216 | may be empty or may contain multiple output lines. |
| 217 | |
| 218 | This is where all the complicated logic is located. The class's |
| 219 | state is used to move between different states and detect things |
| 220 | properly. |
| 221 | |
| 222 | We can be in one of two modes: |
| 223 | self.is_log == True: This is 'git log' mode, where most output is |
| 224 | indented by 4 characters and we are scanning for tags |
| 225 | |
| 226 | self.is_log == False: This is 'patch' mode, where we already have |
| 227 | all the tags, and are processing patches to remove junk we |
| 228 | don't want, and add things we think are required. |
| 229 | |
| 230 | Args: |
Simon Glass | 06127f9 | 2020-10-29 21:46:22 -0600 | [diff] [blame] | 231 | line (str): text line to process |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 232 | |
| 233 | Returns: |
Simon Glass | 06127f9 | 2020-10-29 21:46:22 -0600 | [diff] [blame] | 234 | list: list of output lines, or [] if nothing should be output |
| 235 | |
| 236 | Raises: |
| 237 | ValueError: a fatal error occurred while parsing, e.g. an END |
| 238 | without a starting tag, or two commits with two change IDs |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 239 | """ |
| 240 | # Initially we have no output. Prepare the input line string |
| 241 | out = [] |
| 242 | line = line.rstrip('\n') |
Scott Wood | 616a0ab | 2014-09-25 14:30:46 -0500 | [diff] [blame] | 243 | |
Simon Glass | 0602226 | 2020-10-29 21:46:18 -0600 | [diff] [blame] | 244 | commit_match = RE_COMMIT.match(line) if self.is_log else None |
Scott Wood | 616a0ab | 2014-09-25 14:30:46 -0500 | [diff] [blame] | 245 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 246 | if self.is_log: |
| 247 | if line[:4] == ' ': |
| 248 | line = line[4:] |
| 249 | |
| 250 | # Handle state transition and skipping blank lines |
Simon Glass | 0602226 | 2020-10-29 21:46:18 -0600 | [diff] [blame] | 251 | series_tag_match = RE_SERIES_TAG.match(line) |
| 252 | change_id_match = RE_CHANGE_ID.match(line) |
| 253 | commit_tag_match = RE_COMMIT_TAG.match(line) |
| 254 | cover_match = RE_COVER.match(line) |
| 255 | signoff_match = RE_SIGNOFF.match(line) |
| 256 | leading_whitespace_match = RE_LEADING_WHITESPACE.match(line) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 257 | tag_match = None |
| 258 | if self.state == STATE_PATCH_HEADER: |
Simon Glass | 0602226 | 2020-10-29 21:46:18 -0600 | [diff] [blame] | 259 | tag_match = RE_TAG.match(line) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 260 | is_blank = not line.strip() |
| 261 | if is_blank: |
| 262 | if (self.state == STATE_MSG_HEADER |
| 263 | or self.state == STATE_PATCH_SUBJECT): |
| 264 | self.state += 1 |
| 265 | |
| 266 | # We don't have a subject in the text stream of patch files |
| 267 | # It has its own line with a Subject: tag |
| 268 | if not self.is_log and self.state == STATE_PATCH_SUBJECT: |
| 269 | self.state += 1 |
| 270 | elif commit_match: |
| 271 | self.state = STATE_MSG_HEADER |
| 272 | |
Bin Meng | 9228cc2 | 2016-06-26 23:24:32 -0700 | [diff] [blame] | 273 | # If a tag is detected, or a new commit starts |
Douglas Anderson | 52b5ee8 | 2019-09-27 09:23:56 -0700 | [diff] [blame] | 274 | if series_tag_match or commit_tag_match or change_id_match or \ |
Sean Anderson | 48f46d6 | 2020-05-04 16:28:34 -0400 | [diff] [blame] | 275 | cover_match or signoff_match or self.state == STATE_MSG_HEADER: |
Bin Meng | 50ffc98 | 2016-06-26 23:24:31 -0700 | [diff] [blame] | 276 | # but we are already in a section, this means 'END' is missing |
| 277 | # for that section, fix it up. |
Bin Meng | f83649f | 2016-06-26 23:24:29 -0700 | [diff] [blame] | 278 | if self.in_section: |
Simon Glass | b580039 | 2020-10-29 21:46:23 -0600 | [diff] [blame] | 279 | self._add_warn("Missing 'END' in section '%s'" % self.in_section) |
Bin Meng | f83649f | 2016-06-26 23:24:29 -0700 | [diff] [blame] | 280 | if self.in_section == 'cover': |
| 281 | self.series.cover = self.section |
| 282 | elif self.in_section == 'notes': |
| 283 | if self.is_log: |
| 284 | self.series.notes += self.section |
| 285 | elif self.in_section == 'commit-notes': |
| 286 | if self.is_log: |
| 287 | self.commit.notes += self.section |
| 288 | else: |
Simon Glass | b580039 | 2020-10-29 21:46:23 -0600 | [diff] [blame] | 289 | # This should not happen |
| 290 | raise ValueError("Unknown section '%s'" % self.in_section) |
Bin Meng | f83649f | 2016-06-26 23:24:29 -0700 | [diff] [blame] | 291 | self.in_section = None |
| 292 | self.skip_blank = True |
| 293 | self.section = [] |
Bin Meng | 50ffc98 | 2016-06-26 23:24:31 -0700 | [diff] [blame] | 294 | # but we are already in a change list, that means a blank line |
| 295 | # is missing, fix it up. |
| 296 | if self.in_change: |
Simon Glass | b580039 | 2020-10-29 21:46:23 -0600 | [diff] [blame] | 297 | self._add_warn("Missing 'blank line' in section '%s-changes'" % |
| 298 | self.in_change) |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 299 | self._finalise_change() |
Sean Anderson | 48f46d6 | 2020-05-04 16:28:34 -0400 | [diff] [blame] | 300 | self.in_change = None |
| 301 | self.change_version = 0 |
Bin Meng | f83649f | 2016-06-26 23:24:29 -0700 | [diff] [blame] | 302 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 303 | # If we are in a section, keep collecting lines until we see END |
| 304 | if self.in_section: |
| 305 | if line == 'END': |
| 306 | if self.in_section == 'cover': |
| 307 | self.series.cover = self.section |
| 308 | elif self.in_section == 'notes': |
| 309 | if self.is_log: |
| 310 | self.series.notes += self.section |
Albert ARIBAUD | d880efd | 2013-11-12 11:14:41 +0100 | [diff] [blame] | 311 | elif self.in_section == 'commit-notes': |
| 312 | if self.is_log: |
| 313 | self.commit.notes += self.section |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 314 | else: |
Simon Glass | b580039 | 2020-10-29 21:46:23 -0600 | [diff] [blame] | 315 | # This should not happen |
| 316 | raise ValueError("Unknown section '%s'" % self.in_section) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 317 | self.in_section = None |
| 318 | self.skip_blank = True |
| 319 | self.section = [] |
| 320 | else: |
| 321 | self.section.append(line) |
| 322 | |
Patrick Delaunay | 2870c1c | 2020-07-02 19:08:24 +0200 | [diff] [blame] | 323 | # If we are not in a section, it is an unexpected END |
| 324 | elif line == 'END': |
Simon Glass | c42b1d0 | 2020-10-29 21:46:17 -0600 | [diff] [blame] | 325 | raise ValueError("'END' wihout section") |
Patrick Delaunay | 2870c1c | 2020-07-02 19:08:24 +0200 | [diff] [blame] | 326 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 327 | # Detect the commit subject |
| 328 | elif not is_blank and self.state == STATE_PATCH_SUBJECT: |
| 329 | self.commit.subject = line |
| 330 | |
| 331 | # Detect the tags we want to remove, and skip blank lines |
Simon Glass | 0602226 | 2020-10-29 21:46:18 -0600 | [diff] [blame] | 332 | elif RE_REMOVE.match(line) and not commit_tag_match: |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 333 | self.skip_blank = True |
| 334 | |
| 335 | # TEST= should be the last thing in the commit, so remove |
| 336 | # everything after it |
| 337 | if line.startswith('TEST='): |
| 338 | self.found_test = True |
| 339 | elif self.skip_blank and is_blank: |
| 340 | self.skip_blank = False |
| 341 | |
Sean Anderson | 48f46d6 | 2020-05-04 16:28:34 -0400 | [diff] [blame] | 342 | # Detect Cover-xxx tags |
Bin Meng | d0bc03c | 2016-06-26 23:24:28 -0700 | [diff] [blame] | 343 | elif cover_match: |
Sean Anderson | 48f46d6 | 2020-05-04 16:28:34 -0400 | [diff] [blame] | 344 | name = cover_match.group(1) |
| 345 | value = cover_match.group(2) |
| 346 | if name == 'letter': |
| 347 | self.in_section = 'cover' |
| 348 | self.skip_blank = False |
| 349 | elif name == 'letter-cc': |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 350 | self._add_to_series(line, 'cover-cc', value) |
Sean Anderson | 48f46d6 | 2020-05-04 16:28:34 -0400 | [diff] [blame] | 351 | elif name == 'changes': |
| 352 | self.in_change = 'Cover' |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 353 | self.change_version = self._parse_version(value, line) |
Simon Glass | c72f3da | 2013-03-20 16:43:00 +0000 | [diff] [blame] | 354 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 355 | # If we are in a change list, key collected lines until a blank one |
| 356 | elif self.in_change: |
| 357 | if is_blank: |
| 358 | # Blank line ends this change list |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 359 | self._finalise_change() |
Sean Anderson | 48f46d6 | 2020-05-04 16:28:34 -0400 | [diff] [blame] | 360 | self.in_change = None |
| 361 | self.change_version = 0 |
Simon Glass | 46b3421 | 2014-04-20 10:50:14 -0600 | [diff] [blame] | 362 | elif line == '---': |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 363 | self._finalise_change() |
Sean Anderson | 48f46d6 | 2020-05-04 16:28:34 -0400 | [diff] [blame] | 364 | self.in_change = None |
| 365 | self.change_version = 0 |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 366 | out = self.process_line(line) |
Sean Anderson | 1a32f92 | 2020-05-04 16:28:35 -0400 | [diff] [blame] | 367 | elif self.is_log: |
| 368 | if not leading_whitespace_match: |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 369 | self._finalise_change() |
Sean Anderson | 1a32f92 | 2020-05-04 16:28:35 -0400 | [diff] [blame] | 370 | self.change_lines.append(line) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 371 | self.skip_blank = False |
| 372 | |
| 373 | # Detect Series-xxx tags |
Albert ARIBAUD | d880efd | 2013-11-12 11:14:41 +0100 | [diff] [blame] | 374 | elif series_tag_match: |
| 375 | name = series_tag_match.group(1) |
| 376 | value = series_tag_match.group(2) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 377 | if name == 'changes': |
| 378 | # value is the version number: e.g. 1, or 2 |
Sean Anderson | 48f46d6 | 2020-05-04 16:28:34 -0400 | [diff] [blame] | 379 | self.in_change = 'Series' |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 380 | self.change_version = self._parse_version(value, line) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 381 | else: |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 382 | self._add_to_series(line, name, value) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 383 | self.skip_blank = True |
| 384 | |
Douglas Anderson | 52b5ee8 | 2019-09-27 09:23:56 -0700 | [diff] [blame] | 385 | # Detect Change-Id tags |
| 386 | elif change_id_match: |
| 387 | value = change_id_match.group(1) |
| 388 | if self.is_log: |
| 389 | if self.commit.change_id: |
Simon Glass | c42b1d0 | 2020-10-29 21:46:17 -0600 | [diff] [blame] | 390 | raise ValueError( |
| 391 | "%s: Two Change-Ids: '%s' vs. '%s'" % self.commit.hash, |
| 392 | self.commit.change_id, value) |
Douglas Anderson | 52b5ee8 | 2019-09-27 09:23:56 -0700 | [diff] [blame] | 393 | self.commit.change_id = value |
| 394 | self.skip_blank = True |
| 395 | |
Albert ARIBAUD | d880efd | 2013-11-12 11:14:41 +0100 | [diff] [blame] | 396 | # Detect Commit-xxx tags |
| 397 | elif commit_tag_match: |
| 398 | name = commit_tag_match.group(1) |
| 399 | value = commit_tag_match.group(2) |
| 400 | if name == 'notes': |
Simon Glass | 5f6caa5 | 2020-10-29 21:46:21 -0600 | [diff] [blame] | 401 | self._add_to_commit(name) |
Albert ARIBAUD | d880efd | 2013-11-12 11:14:41 +0100 | [diff] [blame] | 402 | self.skip_blank = True |
Sean Anderson | 48f46d6 | 2020-05-04 16:28:34 -0400 | [diff] [blame] | 403 | elif name == 'changes': |
| 404 | self.in_change = 'Commit' |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 405 | self.change_version = self._parse_version(value, line) |
Patrick Delaunay | ed73b31 | 2020-07-02 19:52:54 +0200 | [diff] [blame] | 406 | else: |
Simon Glass | b580039 | 2020-10-29 21:46:23 -0600 | [diff] [blame] | 407 | self._add_warn('Line %d: Ignoring Commit-%s' % |
| 408 | (self.linenum, name)) |
Albert ARIBAUD | d880efd | 2013-11-12 11:14:41 +0100 | [diff] [blame] | 409 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 410 | # Detect the start of a new commit |
| 411 | elif commit_match: |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 412 | self._close_commit() |
Simon Glass | baec684 | 2014-10-15 02:27:00 -0600 | [diff] [blame] | 413 | self.commit = commit.Commit(commit_match.group(1)) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 414 | |
| 415 | # Detect tags in the commit message |
| 416 | elif tag_match: |
Simon Glass | 08e91be | 2020-07-05 21:41:57 -0600 | [diff] [blame] | 417 | rtag_type, who = tag_match.groups() |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 418 | self._add_commit_rtag(rtag_type, who) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 419 | # Remove Tested-by self, since few will take much notice |
Simon Glass | 08e91be | 2020-07-05 21:41:57 -0600 | [diff] [blame] | 420 | if (rtag_type == 'Tested-by' and |
| 421 | who.find(os.getenv('USER') + '@') != -1): |
Simon Glass | 3b762cc | 2020-10-29 21:46:28 -0600 | [diff] [blame^] | 422 | self._add_warn("Ignoring '%s'" % line) |
Simon Glass | 08e91be | 2020-07-05 21:41:57 -0600 | [diff] [blame] | 423 | elif rtag_type == 'Patch-cc': |
| 424 | self.commit.AddCc(who.split(',')) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 425 | else: |
Simon Glass | 27374d3 | 2014-08-28 09:43:38 -0600 | [diff] [blame] | 426 | out = [line] |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 427 | |
Simon Glass | 46b3421 | 2014-04-20 10:50:14 -0600 | [diff] [blame] | 428 | # Suppress duplicate signoffs |
| 429 | elif signoff_match: |
Simon Glass | b0cd341 | 2014-08-28 09:43:35 -0600 | [diff] [blame] | 430 | if (self.is_log or not self.commit or |
Simon Glass | c42b1d0 | 2020-10-29 21:46:17 -0600 | [diff] [blame] | 431 | self.commit.CheckDuplicateSignoff(signoff_match.group(1))): |
Simon Glass | 46b3421 | 2014-04-20 10:50:14 -0600 | [diff] [blame] | 432 | out = [line] |
| 433 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 434 | # Well that means this is an ordinary line |
| 435 | else: |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 436 | # Look for space before tab |
Simon Glass | f9b6e0f | 2020-10-29 21:46:20 -0600 | [diff] [blame] | 437 | mat = RE_SPACE_BEFORE_TAB.match(line) |
| 438 | if mat: |
Simon Glass | b580039 | 2020-10-29 21:46:23 -0600 | [diff] [blame] | 439 | self._add_warn('Line %d/%d has space before tab' % |
| 440 | (self.linenum, mat.start())) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 441 | |
| 442 | # OK, we have a valid non-blank line |
| 443 | out = [line] |
| 444 | self.linenum += 1 |
| 445 | self.skip_blank = False |
| 446 | if self.state == STATE_DIFFS: |
| 447 | pass |
| 448 | |
| 449 | # If this is the start of the diffs section, emit our tags and |
| 450 | # change log |
| 451 | elif line == '---': |
| 452 | self.state = STATE_DIFFS |
| 453 | |
Sean Anderson | 48f46d6 | 2020-05-04 16:28:34 -0400 | [diff] [blame] | 454 | # Output the tags (signoff first), then change list |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 455 | out = [] |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 456 | log = self.series.MakeChangeLog(self.commit) |
Simon Glass | b0cd341 | 2014-08-28 09:43:35 -0600 | [diff] [blame] | 457 | out += [line] |
| 458 | if self.commit: |
| 459 | out += self.commit.notes |
| 460 | out += [''] + log |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 461 | elif self.found_test: |
Simon Glass | 0602226 | 2020-10-29 21:46:18 -0600 | [diff] [blame] | 462 | if not RE_ALLOWED_AFTER_TEST.match(line): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 463 | self.lines_after_test += 1 |
| 464 | |
| 465 | return out |
| 466 | |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 467 | def finalise(self): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 468 | """Close out processing of this patch stream""" |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 469 | self._finalise_change() |
| 470 | self._close_commit() |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 471 | if self.lines_after_test: |
Simon Glass | b580039 | 2020-10-29 21:46:23 -0600 | [diff] [blame] | 472 | self._add_warn('Found %d lines after TEST=' % self.lines_after_test) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 473 | |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 474 | def _write_message_id(self, outfd): |
Douglas Anderson | 52b5ee8 | 2019-09-27 09:23:56 -0700 | [diff] [blame] | 475 | """Write the Message-Id into the output. |
| 476 | |
| 477 | This is based on the Change-Id in the original patch, the version, |
| 478 | and the prefix. |
| 479 | |
| 480 | Args: |
Simon Glass | 06127f9 | 2020-10-29 21:46:22 -0600 | [diff] [blame] | 481 | outfd (io.IOBase): Output stream file object |
Douglas Anderson | 52b5ee8 | 2019-09-27 09:23:56 -0700 | [diff] [blame] | 482 | """ |
| 483 | if not self.commit.change_id: |
| 484 | return |
| 485 | |
| 486 | # If the count is -1 we're testing, so use a fixed time |
| 487 | if self.commit.count == -1: |
| 488 | time_now = datetime.datetime(1999, 12, 31, 23, 59, 59) |
| 489 | else: |
| 490 | time_now = datetime.datetime.now() |
| 491 | |
| 492 | # In theory there is email.utils.make_msgid() which would be nice |
| 493 | # to use, but it already produces something way too long and thus |
| 494 | # will produce ugly commit lines if someone throws this into |
| 495 | # a "Link:" tag in the final commit. So (sigh) roll our own. |
| 496 | |
| 497 | # Start with the time; presumably we wouldn't send the same series |
| 498 | # with the same Change-Id at the exact same second. |
| 499 | parts = [time_now.strftime("%Y%m%d%H%M%S")] |
| 500 | |
| 501 | # These seem like they would be nice to include. |
| 502 | if 'prefix' in self.series: |
| 503 | parts.append(self.series['prefix']) |
| 504 | if 'version' in self.series: |
| 505 | parts.append("v%s" % self.series['version']) |
| 506 | |
| 507 | parts.append(str(self.commit.count + 1)) |
| 508 | |
| 509 | # The Change-Id must be last, right before the @ |
| 510 | parts.append(self.commit.change_id) |
| 511 | |
| 512 | # Join parts together with "." and write it out. |
| 513 | outfd.write('Message-Id: <%s@changeid>\n' % '.'.join(parts)) |
| 514 | |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 515 | def process_stream(self, infd, outfd): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 516 | """Copy a stream from infd to outfd, filtering out unwanting things. |
| 517 | |
| 518 | This is used to process patch files one at a time. |
| 519 | |
| 520 | Args: |
Simon Glass | 06127f9 | 2020-10-29 21:46:22 -0600 | [diff] [blame] | 521 | infd (io.IOBase): Input stream file object |
| 522 | outfd (io.IOBase): Output stream file object |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 523 | """ |
| 524 | # Extract the filename from each diff, for nice warnings |
| 525 | fname = None |
| 526 | last_fname = None |
| 527 | re_fname = re.compile('diff --git a/(.*) b/.*') |
Douglas Anderson | 52b5ee8 | 2019-09-27 09:23:56 -0700 | [diff] [blame] | 528 | |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 529 | self._write_message_id(outfd) |
Douglas Anderson | 52b5ee8 | 2019-09-27 09:23:56 -0700 | [diff] [blame] | 530 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 531 | while True: |
| 532 | line = infd.readline() |
| 533 | if not line: |
| 534 | break |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 535 | out = self.process_line(line) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 536 | |
| 537 | # Try to detect blank lines at EOF |
| 538 | for line in out: |
| 539 | match = re_fname.match(line) |
| 540 | if match: |
| 541 | last_fname = fname |
| 542 | fname = match.group(1) |
| 543 | if line == '+': |
| 544 | self.blank_count += 1 |
| 545 | else: |
| 546 | if self.blank_count and (line == '-- ' or match): |
Simon Glass | b580039 | 2020-10-29 21:46:23 -0600 | [diff] [blame] | 547 | self._add_warn("Found possible blank line(s) at end of file '%s'" % |
| 548 | last_fname) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 549 | outfd.write('+\n' * self.blank_count) |
| 550 | outfd.write(line + '\n') |
| 551 | self.blank_count = 0 |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 552 | self.finalise() |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 553 | |
| 554 | |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 555 | def get_metadata_for_list(commit_range, git_dir=None, count=None, |
| 556 | series=None, allow_overwrite=False): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 557 | """Reads out patch series metadata from the commits |
| 558 | |
| 559 | This does a 'git log' on the relevant commits and pulls out the tags we |
| 560 | are interested in. |
| 561 | |
| 562 | Args: |
Simon Glass | 06127f9 | 2020-10-29 21:46:22 -0600 | [diff] [blame] | 563 | commit_range (str): Range of commits to count (e.g. 'HEAD..base') |
| 564 | git_dir (str): Path to git repositiory (None to use default) |
| 565 | count (int): Number of commits to list, or None for no limit |
| 566 | series (Series): Object to add information into. By default a new series |
Simon Glass | 680da3c | 2012-12-15 10:42:06 +0000 | [diff] [blame] | 567 | is started. |
Simon Glass | 06127f9 | 2020-10-29 21:46:22 -0600 | [diff] [blame] | 568 | allow_overwrite (bool): Allow tags to overwrite an existing tag |
| 569 | |
Simon Glass | 680da3c | 2012-12-15 10:42:06 +0000 | [diff] [blame] | 570 | Returns: |
Simon Glass | 06127f9 | 2020-10-29 21:46:22 -0600 | [diff] [blame] | 571 | Series: Object containing information about the commits. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 572 | """ |
Simon Glass | 4e5d6bc | 2014-09-05 19:00:19 -0600 | [diff] [blame] | 573 | if not series: |
| 574 | series = Series() |
Simon Glass | 359b55a6 | 2014-09-05 19:00:23 -0600 | [diff] [blame] | 575 | series.allow_overwrite = allow_overwrite |
Simon Glass | fc372bc | 2016-03-06 19:45:33 -0700 | [diff] [blame] | 576 | params = gitutil.LogCmd(commit_range, reverse=True, count=count, |
Simon Glass | b9dbcb4 | 2014-08-09 15:33:10 -0600 | [diff] [blame] | 577 | git_dir=git_dir) |
| 578 | stdout = command.RunPipe([params], capture=True).stdout |
Simon Glass | f9b6e0f | 2020-10-29 21:46:20 -0600 | [diff] [blame] | 579 | pst = PatchStream(series, is_log=True) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 580 | for line in stdout.splitlines(): |
Simon Glass | f9b6e0f | 2020-10-29 21:46:20 -0600 | [diff] [blame] | 581 | pst.process_line(line) |
| 582 | pst.finalise() |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 583 | return series |
| 584 | |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 585 | def get_metadata(branch, start, count): |
Simon Glass | 680da3c | 2012-12-15 10:42:06 +0000 | [diff] [blame] | 586 | """Reads out patch series metadata from the commits |
| 587 | |
| 588 | This does a 'git log' on the relevant commits and pulls out the tags we |
| 589 | are interested in. |
| 590 | |
| 591 | Args: |
Simon Glass | 06127f9 | 2020-10-29 21:46:22 -0600 | [diff] [blame] | 592 | branch (str): Branch to use (None for current branch) |
| 593 | start (int): Commit to start from: 0=branch HEAD, 1=next one, etc. |
| 594 | count (int): Number of commits to list |
| 595 | |
| 596 | Returns: |
| 597 | Series: Object containing information about the commits. |
Simon Glass | 680da3c | 2012-12-15 10:42:06 +0000 | [diff] [blame] | 598 | """ |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 599 | return get_metadata_for_list( |
| 600 | '%s~%d' % (branch if branch else 'HEAD', start), None, count) |
Simon Glass | 680da3c | 2012-12-15 10:42:06 +0000 | [diff] [blame] | 601 | |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 602 | def get_metadata_for_test(text): |
Simon Glass | df1bc5c | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 603 | """Process metadata from a file containing a git log. Used for tests |
| 604 | |
| 605 | Args: |
| 606 | text: |
Simon Glass | 06127f9 | 2020-10-29 21:46:22 -0600 | [diff] [blame] | 607 | |
| 608 | Returns: |
| 609 | Series: Object containing information about the commits. |
Simon Glass | df1bc5c | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 610 | """ |
| 611 | series = Series() |
Simon Glass | f9b6e0f | 2020-10-29 21:46:20 -0600 | [diff] [blame] | 612 | pst = PatchStream(series, is_log=True) |
Simon Glass | df1bc5c | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 613 | for line in text.splitlines(): |
Simon Glass | f9b6e0f | 2020-10-29 21:46:20 -0600 | [diff] [blame] | 614 | pst.process_line(line) |
| 615 | pst.finalise() |
Simon Glass | df1bc5c | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 616 | return series |
| 617 | |
Simon Glass | f9b6e0f | 2020-10-29 21:46:20 -0600 | [diff] [blame] | 618 | def fix_patch(backup_dir, fname, series, cmt): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 619 | """Fix up a patch file, by adding/removing as required. |
| 620 | |
| 621 | We remove our tags from the patch file, insert changes lists, etc. |
| 622 | The patch file is processed in place, and overwritten. |
| 623 | |
| 624 | A backup file is put into backup_dir (if not None). |
| 625 | |
| 626 | Args: |
Simon Glass | 06127f9 | 2020-10-29 21:46:22 -0600 | [diff] [blame] | 627 | backup_dir (str): Path to directory to use to backup the file |
| 628 | fname (str): Filename to patch file to process |
| 629 | series (Series): Series information about this patch set |
| 630 | cmt (Commit): Commit object for this patch file |
| 631 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 632 | Return: |
Simon Glass | 06127f9 | 2020-10-29 21:46:22 -0600 | [diff] [blame] | 633 | list: A list of errors, each str, or [] if all ok. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 634 | """ |
| 635 | handle, tmpname = tempfile.mkstemp() |
Simon Glass | f544a2d | 2019-10-31 07:42:51 -0600 | [diff] [blame] | 636 | outfd = os.fdopen(handle, 'w', encoding='utf-8') |
| 637 | infd = open(fname, 'r', encoding='utf-8') |
Simon Glass | f9b6e0f | 2020-10-29 21:46:20 -0600 | [diff] [blame] | 638 | pst = PatchStream(series) |
| 639 | pst.commit = cmt |
| 640 | pst.process_stream(infd, outfd) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 641 | infd.close() |
| 642 | outfd.close() |
| 643 | |
| 644 | # Create a backup file if required |
| 645 | if backup_dir: |
| 646 | shutil.copy(fname, os.path.join(backup_dir, os.path.basename(fname))) |
| 647 | shutil.move(tmpname, fname) |
Simon Glass | 6d00f6c | 2020-10-29 21:46:24 -0600 | [diff] [blame] | 648 | return cmt.warn |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 649 | |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 650 | def fix_patches(series, fnames): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 651 | """Fix up a list of patches identified by filenames |
| 652 | |
| 653 | The patch files are processed in place, and overwritten. |
| 654 | |
| 655 | Args: |
Simon Glass | 06127f9 | 2020-10-29 21:46:22 -0600 | [diff] [blame] | 656 | series (Series): The Series object |
| 657 | fnames (:type: list of str): List of patch files to process |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 658 | """ |
| 659 | # Current workflow creates patches, so we shouldn't need a backup |
| 660 | backup_dir = None #tempfile.mkdtemp('clean-patch') |
| 661 | count = 0 |
| 662 | for fname in fnames: |
Simon Glass | f9b6e0f | 2020-10-29 21:46:20 -0600 | [diff] [blame] | 663 | cmt = series.commits[count] |
| 664 | cmt.patch = fname |
| 665 | cmt.count = count |
| 666 | result = fix_patch(backup_dir, fname, series, cmt) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 667 | if result: |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 668 | print('%d warnings for %s:' % (len(result), fname)) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 669 | for warn in result: |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 670 | print('\t', warn) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 671 | print |
| 672 | count += 1 |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 673 | print('Cleaned %d patches' % count) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 674 | |
Simon Glass | 93f61c0 | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 675 | def insert_cover_letter(fname, series, count): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 676 | """Inserts a cover letter with the required info into patch 0 |
| 677 | |
| 678 | Args: |
Simon Glass | 06127f9 | 2020-10-29 21:46:22 -0600 | [diff] [blame] | 679 | fname (str): Input / output filename of the cover letter file |
| 680 | series (Series): Series object |
| 681 | count (int): Number of patches in the series |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 682 | """ |
Simon Glass | f9b6e0f | 2020-10-29 21:46:20 -0600 | [diff] [blame] | 683 | fil = open(fname, 'r') |
| 684 | lines = fil.readlines() |
| 685 | fil.close() |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 686 | |
Simon Glass | f9b6e0f | 2020-10-29 21:46:20 -0600 | [diff] [blame] | 687 | fil = open(fname, 'w') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 688 | text = series.cover |
| 689 | prefix = series.GetPatchPrefix() |
| 690 | for line in lines: |
| 691 | if line.startswith('Subject:'): |
Wu, Josh | 393aaa2 | 2015-04-03 10:51:17 +0800 | [diff] [blame] | 692 | # if more than 10 or 100 patches, it should say 00/xx, 000/xxx, etc |
| 693 | zero_repeat = int(math.log10(count)) + 1 |
| 694 | zero = '0' * zero_repeat |
| 695 | line = 'Subject: [%s %s/%d] %s\n' % (prefix, zero, count, text[0]) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 696 | |
| 697 | # Insert our cover letter |
| 698 | elif line.startswith('*** BLURB HERE ***'): |
| 699 | # First the blurb test |
| 700 | line = '\n'.join(text[1:]) + '\n' |
| 701 | if series.get('notes'): |
| 702 | line += '\n'.join(series.notes) + '\n' |
| 703 | |
| 704 | # Now the change list |
| 705 | out = series.MakeChangeLog(None) |
| 706 | line += '\n' + '\n'.join(out) |
Simon Glass | f9b6e0f | 2020-10-29 21:46:20 -0600 | [diff] [blame] | 707 | fil.write(line) |
| 708 | fil.close() |