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