| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 4 |
# |
| 5 |
# This file is part of GCC. |
| 6 |
# |
| 7 |
# GCC is free software; you can redistribute it and/or modify it under |
| 8 |
# the terms of the GNU General Public License as published by the Free |
| 9 |
# Software Foundation; either version 3, or (at your option) any later |
| 10 |
# version. |
| 11 |
# |
| 12 |
# GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 |
# for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with GCC; see the file COPYING3. If not see |
| 19 |
# <http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
import difflib |
| 22 |
import os |
| 23 |
import re |
| 24 |
import sys |
| 25 |
from collections import defaultdict |
| 26 |
|
| 27 |
default_changelog_locations = { |
| 28 |
'c++tools', |
| 29 |
'config', |
| 30 |
'contrib', |
| 31 |
'contrib/header-tools', |
| 32 |
'contrib/reghunt', |
| 33 |
'contrib/regression', |
| 34 |
'fixincludes', |
| 35 |
'gcc/ada', |
| 36 |
'gcc/analyzer', |
| 37 |
'gcc/brig', |
| 38 |
'gcc/c', |
| 39 |
'gcc/c-family', |
| 40 |
'gcc', |
| 41 |
'gcc/cobol', |
| 42 |
'gcc/cp', |
| 43 |
'gcc/d', |
| 44 |
'gcc/fortran', |
| 45 |
'gcc/go', |
| 46 |
'gcc/jit', |
| 47 |
'gcc/lto', |
| 48 |
'gcc/m2', |
| 49 |
'gcc/objc', |
| 50 |
'gcc/objcp', |
| 51 |
'gcc/po', |
| 52 |
'gcc/rust', |
| 53 |
'gcc/testsuite', |
| 54 |
'gnattools', |
| 55 |
'gotools', |
| 56 |
'include', |
| 57 |
'intl', |
| 58 |
'libada', |
| 59 |
'libatomic', |
| 60 |
'libbacktrace', |
| 61 |
'libcc1', |
| 62 |
'libcody', |
| 63 |
'libcpp', |
| 64 |
'libcpp/po', |
| 65 |
'libdecnumber', |
| 66 |
'libffi', |
| 67 |
'libgcc', |
| 68 |
'libgcc/config/avr/libf7', |
| 69 |
'libgcc/config/libbid', |
| 70 |
'libgcobol', |
| 71 |
'libgfortran', |
| 72 |
'libgm2', |
| 73 |
'libgomp', |
| 74 |
'libgrust', |
| 75 |
'libhsail-rt', |
| 76 |
'libiberty', |
| 77 |
'libitm', |
| 78 |
'libobjc', |
| 79 |
'libphobos', |
| 80 |
'libquadmath', |
| 81 |
'libsanitizer', |
| 82 |
'libssp', |
| 83 |
'libstdc++-v3', |
| 84 |
'libvtv', |
| 85 |
'lto-plugin', |
| 86 |
'maintainer-scripts', |
| 87 |
'zlib'} |
| 88 |
|
| 89 |
bug_components = { |
| 90 |
'ada', |
| 91 |
'analyzer', |
| 92 |
'boehm-gc', |
| 93 |
'bootstrap', |
| 94 |
'c', |
| 95 |
'c++', |
| 96 |
'cobol', |
| 97 |
'd', |
| 98 |
'debug', |
| 99 |
'demangler', |
| 100 |
'diagnostics', |
| 101 |
'driver', |
| 102 |
'fastjar', |
| 103 |
'fortran', |
| 104 |
'gcov-profile', |
| 105 |
'go', |
| 106 |
'hsa', |
| 107 |
'inline-asm', |
| 108 |
'ipa', |
| 109 |
'java', |
| 110 |
'jit', |
| 111 |
'libbacktrace', |
| 112 |
'libcc1', |
| 113 |
'libf2c', |
| 114 |
'libffi', |
| 115 |
'libfortran', |
| 116 |
'libgcc', |
| 117 |
'libgcj', |
| 118 |
'libgdiagnostics', |
| 119 |
'libgomp', |
| 120 |
'libitm', |
| 121 |
'libobjc', |
| 122 |
'libquadmath', |
| 123 |
'libstdc++', |
| 124 |
'lto', |
| 125 |
'middle-end', |
| 126 |
'modula2', |
| 127 |
'objc', |
| 128 |
'objc++', |
| 129 |
'other', |
| 130 |
'pch', |
| 131 |
'pending', |
| 132 |
'plugins', |
| 133 |
'preprocessor', |
| 134 |
'regression', |
| 135 |
'rtl-optimization', |
| 136 |
'rust', |
| 137 |
'sanitizer', |
| 138 |
'sarif-replay', |
| 139 |
'spam', |
| 140 |
'target', |
| 141 |
'testsuite', |
| 142 |
'translation', |
| 143 |
'tree-optimization', |
| 144 |
'web'} |
| 145 |
|
| 146 |
ignored_prefixes = { |
| 147 |
'gcc/d/dmd/', |
| 148 |
'gcc/go/gofrontend/', |
| 149 |
'gcc/testsuite/gdc.test/', |
| 150 |
'gcc/testsuite/go.test/test/', |
| 151 |
'libffi/', |
| 152 |
'libgo/', |
| 153 |
'libphobos/libdruntime/', |
| 154 |
'libphobos/src/', |
| 155 |
'libsanitizer/', |
| 156 |
} |
| 157 |
|
| 158 |
wildcard_prefixes = { |
| 159 |
'gcc/testsuite/', |
| 160 |
'libstdc++-v3/doc/html/', |
| 161 |
'libstdc++-v3/testsuite/' |
| 162 |
} |
| 163 |
|
| 164 |
misc_files = { |
| 165 |
'gcc/DATESTAMP', |
| 166 |
'gcc/BASE-VER', |
| 167 |
'gcc/DEV-PHASE' |
| 168 |
} |
| 169 |
|
| 170 |
author_line_regex = \ |
| 171 |
re.compile(r'^(?P<datetime>\d{4}-\d{2}-\d{2})\ {2}(?P<name>.* <.*>)') |
| 172 |
additional_author_regex = re.compile(r'^\t(?P<spaces>\ *)?(?P<name>.* <.*>)') |
| 173 |
changelog_regex = re.compile(r'^(?:[fF]or +)?([a-z0-9+-/]*)ChangeLog:?') |
| 174 |
subject_pr_regex = re.compile(r'(^|\W)PR\s+(?P<component>[a-zA-Z0-9+-]+)/(?P<pr>\d{4,7})') |
| 175 |
subject_pr2_regex = re.compile(r'[(\[]PR\s*(?P<pr>\d{4,7})[)\]]') |
| 176 |
pr_regex = re.compile(r'\tPR (?P<component>[a-z0-9+-]+\/)?(?P<pr>[0-9]+)$') |
| 177 |
dr_regex = re.compile(r'\tDR ([0-9]+)$') |
| 178 |
star_prefix_regex = re.compile(r'\t\*(?P<spaces>\ *)(?P<content>.*)') |
| 179 |
end_of_location_regex = re.compile(r'[\[<(:]') |
| 180 |
item_empty_regex = re.compile(r'\t(\* \S+ )?\(\S+\):\s*$') |
| 181 |
item_parenthesis_regex = re.compile(r'\t(\*|\(\S+\):)') |
| 182 |
revert_regex = re.compile(r'This reverts commit (?P<hash>[0-9a-f]+)\.$') |
| 183 |
cherry_pick_regex = re.compile(r'cherry picked from commit (?P<hash>\w+)') |
| 184 |
|
| 185 |
LINE_LIMIT = 100 |
| 186 |
TAB_WIDTH = 8 |
| 187 |
CO_AUTHORED_BY_PREFIX = 'co-authored-by: ' |
| 188 |
|
| 189 |
REVIEW_PREFIXES = ('reviewed-by: ', 'reviewed-on: ', 'signed-off-by: ', |
| 190 |
'acked-by: ', 'tested-by: ', 'reported-by: ', |
| 191 |
'suggested-by: ') |
| 192 |
DATE_FORMAT = '%Y-%m-%d' |
| 193 |
|
| 194 |
|
| 195 |
def decode_path(path): |
| 196 |
# When core.quotepath is true (default value), utf8 chars are encoded like: |
| 197 |
# "b/ko\304\215ka.txt" |
| 198 |
# |
| 199 |
# The upstream bug is fixed: |
| 200 |
# https://github.com/gitpython-developers/GitPython/issues/1099 |
| 201 |
# |
| 202 |
# but we still need a workaround for older versions of the library. |
| 203 |
# Please take a look at the explanation of the transformation: |
| 204 |
# https://stackoverflow.com/questions/990169/how-do-convert-unicode-escape-sequences-to-unicode-characters-in-a-python-string |
| 205 |
|
| 206 |
if path.startswith('"') and path.endswith('"'): |
| 207 |
return (path.strip('"').encode('utf8').decode('unicode-escape') |
| 208 |
.encode('latin-1').decode('utf8')) |
| 209 |
else: |
| 210 |
return path |
| 211 |
|
| 212 |
|
| 213 |
class Error: |
| 214 |
def __init__(self, message, line=None, details=None): |
| 215 |
self.message = message |
| 216 |
self.line = line |
| 217 |
self.details = details |
| 218 |
|
| 219 |
def __repr__(self): |
| 220 |
s = self.message |
| 221 |
if self.line: |
| 222 |
s += ': "%s"' % self.line |
| 223 |
return s |
| 224 |
|
| 225 |
|
| 226 |
class ChangeLogEntry: |
| 227 |
def __init__(self, folder, authors, prs): |
| 228 |
self.folder = folder |
| 229 |
# The 'list.copy()' function is not available before Python 3.3 |
| 230 |
self.author_lines = list(authors) |
| 231 |
self.initial_prs = list(prs) |
| 232 |
self.prs = list(prs) |
| 233 |
self.lines = [] |
| 234 |
self.files = [] |
| 235 |
self.file_patterns = [] |
| 236 |
self.parentheses_stack = [] |
| 237 |
|
| 238 |
def parse_file_names(self): |
| 239 |
# Whether the content currently processed is between a star prefix the |
| 240 |
# end of the file list: a colon or an open paren. |
| 241 |
in_location = False |
| 242 |
|
| 243 |
for line in self.lines: |
| 244 |
# If this line matches the star prefix, start the location |
| 245 |
# processing on the information that follows the star. |
| 246 |
# Note that we need to skip macro names that can be in form of: |
| 247 |
# |
| 248 |
# * config/i386/i386.md (*fix_trunc<mode>_i387_1, |
| 249 |
# *add<mode>3_ne, *add<mode>3_eq_0, *add<mode>3_ne_0, |
| 250 |
# *fist<mode>2_<rounding>_1, *<code><mode>3_1): |
| 251 |
# |
| 252 |
m = star_prefix_regex.match(line) |
| 253 |
if m and len(m.group('spaces')) == 1: |
| 254 |
in_location = True |
| 255 |
line = m.group('content') |
| 256 |
|
| 257 |
if in_location: |
| 258 |
# Strip everything that is not a filename in "line": |
| 259 |
# entities "(NAME)", cases "<PATTERN>", conditions |
| 260 |
# "[COND]", entry text (the colon, if present, and |
| 261 |
# anything that follows it). |
| 262 |
m = end_of_location_regex.search(line) |
| 263 |
if m: |
| 264 |
line = line[:m.start()] |
| 265 |
in_location = False |
| 266 |
|
| 267 |
# At this point, all that's left is a list of filenames |
| 268 |
# separated by commas and whitespaces. |
| 269 |
for file in line.split(','): |
| 270 |
file = file.strip() |
| 271 |
if file: |
| 272 |
if file.endswith('*'): |
| 273 |
self.file_patterns.append(file[:-1]) |
| 274 |
else: |
| 275 |
self.files.append(file) |
| 276 |
|
| 277 |
@property |
| 278 |
def datetime(self): |
| 279 |
for author in self.author_lines: |
| 280 |
if author[1]: |
| 281 |
return author[1] |
| 282 |
return None |
| 283 |
|
| 284 |
@property |
| 285 |
def authors(self): |
| 286 |
return [author_line[0] for author_line in self.author_lines] |
| 287 |
|
| 288 |
@property |
| 289 |
def is_empty(self): |
| 290 |
return not self.lines and self.prs == self.initial_prs |
| 291 |
|
| 292 |
def contains_author(self, author): |
| 293 |
for author_lines in self.author_lines: |
| 294 |
if author_lines[0] == author: |
| 295 |
return True |
| 296 |
return False |
| 297 |
|
| 298 |
|
| 299 |
class GitInfo: |
| 300 |
def __init__(self, hexsha, date, author, lines, modified_files): |
| 301 |
self.hexsha = hexsha |
| 302 |
self.date = date |
| 303 |
self.author = author |
| 304 |
self.lines = lines |
| 305 |
self.modified_files = modified_files |
| 306 |
|
| 307 |
|
| 308 |
class GitCommit: |
| 309 |
def __init__(self, info, commit_to_info_hook=None, ref_name=None): |
| 310 |
self.original_info = info |
| 311 |
self.info = info |
| 312 |
self.message = None |
| 313 |
self.changes = None |
| 314 |
self.changelog_entries = [] |
| 315 |
self.errors = [] |
| 316 |
self.warnings = [] |
| 317 |
self.top_level_authors = [] |
| 318 |
self.co_authors = [] |
| 319 |
self.top_level_prs = [] |
| 320 |
self.subject_prs = set() |
| 321 |
self.cherry_pick_commit = None |
| 322 |
self.revert_commit = None |
| 323 |
self.commit_to_info_hook = commit_to_info_hook |
| 324 |
self.init_changelog_locations(ref_name) |
| 325 |
|
| 326 |
# Skip Update copyright years commits |
| 327 |
if self.info.lines and self.info.lines[0] == 'Update copyright years.': |
| 328 |
return |
| 329 |
|
| 330 |
if self.info.lines and len(self.info.lines) > 1 and self.info.lines[1]: |
| 331 |
self.errors.append(Error('Expected empty second line in commit message', info.lines[0])) |
| 332 |
|
| 333 |
# Identify first if the commit is a Revert commit |
| 334 |
for line in self.info.lines: |
| 335 |
m = revert_regex.fullmatch(line) |
| 336 |
if m: |
| 337 |
self.revert_commit = m.group('hash') |
| 338 |
break |
| 339 |
if self.revert_commit: |
| 340 |
# The following happens for get_email.py: |
| 341 |
if not self.commit_to_info_hook: |
| 342 |
self.warnings.append(f"Invoked script can not obtain info about " |
| 343 |
f"reverted commits such as '{self.revert_commit}'") |
| 344 |
return |
| 345 |
self.info = self.commit_to_info_hook(self.revert_commit) |
| 346 |
if not self.info: |
| 347 |
self.errors.append(Error('Cannot find to-be-reverted commit', self.revert_commit)) |
| 348 |
return |
| 349 |
|
| 350 |
self.check_commit_email() |
| 351 |
|
| 352 |
# Extract PR numbers form the subject line |
| 353 |
# Match either [PRnnnn] / (PRnnnn) or PR component/nnnn |
| 354 |
if self.info.lines and not self.revert_commit: |
| 355 |
self.subject_prs = {m.group('pr') for m in subject_pr2_regex.finditer(info.lines[0])} |
| 356 |
for m in subject_pr_regex.finditer(info.lines[0]): |
| 357 |
if not m.group('component') in bug_components: |
| 358 |
self.errors.append(Error('invalid PR component in subject', info.lines[0])) |
| 359 |
self.subject_prs.add(m.group('pr')) |
| 360 |
|
| 361 |
# Allow complete deletion of ChangeLog files in a commit |
| 362 |
project_files = [f for f in self.info.modified_files |
| 363 |
if (self.is_changelog_filename(f[0], allow_suffix=True) and f[1] != 'D') |
| 364 |
or f[0] in misc_files] |
| 365 |
ignored_files = [f for f in self.info.modified_files |
| 366 |
if self.in_ignored_location(f[0])] |
| 367 |
if len(project_files) == len(self.info.modified_files): |
| 368 |
# All modified files are only MISC files |
| 369 |
return |
| 370 |
elif project_files: |
| 371 |
err = 'ChangeLog, DATESTAMP, BASE-VER and DEV-PHASE updates ' \ |
| 372 |
'should be done separately from normal commits\n' \ |
| 373 |
'(note: ChangeLog entries will be automatically ' \ |
| 374 |
'added by a cron job)' |
| 375 |
self.errors.append(Error(err)) |
| 376 |
return |
| 377 |
|
| 378 |
all_are_ignored = (len(project_files) + len(ignored_files) |
| 379 |
== len(self.info.modified_files)) |
| 380 |
self.parse_lines(all_are_ignored) |
| 381 |
if self.changes: |
| 382 |
self.parse_changelog() |
| 383 |
self.parse_file_names() |
| 384 |
self.check_for_empty_description() |
| 385 |
self.check_for_broken_parentheses() |
| 386 |
self.deduce_changelog_locations() |
| 387 |
self.check_file_patterns() |
| 388 |
self.check_line_start() |
| 389 |
if not self.errors: |
| 390 |
self.check_mentioned_files() |
| 391 |
self.check_for_correct_changelog() |
| 392 |
if self.subject_prs: |
| 393 |
self.errors.append(Error('PR %s in subject but not in changelog' % |
| 394 |
', '.join(self.subject_prs), self.info.lines[0])) |
| 395 |
|
| 396 |
@property |
| 397 |
def success(self): |
| 398 |
return not self.errors |
| 399 |
|
| 400 |
@property |
| 401 |
def new_files(self): |
| 402 |
return [x[0] for x in self.info.modified_files if x[1] == 'A'] |
| 403 |
|
| 404 |
@classmethod |
| 405 |
def is_changelog_filename(cls, path, allow_suffix=False): |
| 406 |
basename = os.path.basename(path) |
| 407 |
if basename == 'ChangeLog': |
| 408 |
return True |
| 409 |
elif allow_suffix and basename.startswith('ChangeLog'): |
| 410 |
return True |
| 411 |
else: |
| 412 |
return False |
| 413 |
|
| 414 |
def find_changelog_location(self, name): |
| 415 |
if name.startswith('\t'): |
| 416 |
name = name[1:] |
| 417 |
if name.endswith(':'): |
| 418 |
name = name[:-1] |
| 419 |
if name.endswith('/'): |
| 420 |
name = name[:-1] |
| 421 |
return name if name in self.changelog_locations else None |
| 422 |
|
| 423 |
@classmethod |
| 424 |
def format_git_author(cls, author): |
| 425 |
assert '<' in author |
| 426 |
return author.replace('<', ' <') |
| 427 |
|
| 428 |
@classmethod |
| 429 |
def parse_git_name_status(cls, string): |
| 430 |
modified_files = [] |
| 431 |
for entry in string.split('\n'): |
| 432 |
parts = entry.split('\t') |
| 433 |
t = parts[0] |
| 434 |
if t == 'A' or t == 'D' or t == 'M': |
| 435 |
modified_files.append((parts[1], t)) |
| 436 |
elif t.startswith('R'): |
| 437 |
modified_files.append((parts[1], 'D')) |
| 438 |
modified_files.append((parts[2], 'A')) |
| 439 |
return modified_files |
| 440 |
|
| 441 |
def init_changelog_locations(self, ref_name): |
| 442 |
self.changelog_locations = list(default_changelog_locations) |
| 443 |
if ref_name: |
| 444 |
version = sys.maxsize |
| 445 |
if 'releases/gcc-' in ref_name: |
| 446 |
version = int(ref_name.split('-')[-1]) |
| 447 |
if version >= 12: |
| 448 |
# HSA and BRIG were removed in GCC 12 |
| 449 |
self.changelog_locations.remove('gcc/brig') |
| 450 |
self.changelog_locations.remove('libhsail-rt') |
| 451 |
|
| 452 |
def parse_lines(self, all_are_ignored): |
| 453 |
body = self.info.lines |
| 454 |
|
| 455 |
for i, b in enumerate(body): |
| 456 |
if not b: |
| 457 |
continue |
| 458 |
if (changelog_regex.match(b) or self.find_changelog_location(b) |
| 459 |
or star_prefix_regex.match(b) or pr_regex.match(b) |
| 460 |
or dr_regex.match(b) or author_line_regex.match(b) |
| 461 |
or b.lower().startswith(CO_AUTHORED_BY_PREFIX)): |
| 462 |
self.changes = body[i:] |
| 463 |
return |
| 464 |
if not all_are_ignored: |
| 465 |
self.errors.append(Error('cannot find a ChangeLog location in ' |
| 466 |
'message')) |
| 467 |
|
| 468 |
def parse_changelog(self): |
| 469 |
last_entry = None |
| 470 |
will_deduce = False |
| 471 |
for line in self.changes: |
| 472 |
if not line: |
| 473 |
if last_entry and will_deduce: |
| 474 |
last_entry = None |
| 475 |
continue |
| 476 |
if line != line.rstrip(): |
| 477 |
self.errors.append(Error('trailing whitespace', line)) |
| 478 |
if len(line.replace('\t', ' ' * TAB_WIDTH)) > LINE_LIMIT: |
| 479 |
# support long filenames |
| 480 |
if not line.startswith('\t* ') or not line.endswith(':') or ' ' in line[3:-1]: |
| 481 |
self.errors.append(Error('line exceeds %d character limit' |
| 482 |
% LINE_LIMIT, line)) |
| 483 |
m = changelog_regex.match(line) |
| 484 |
if m: |
| 485 |
last_entry = ChangeLogEntry(m.group(1).rstrip('/'), |
| 486 |
self.top_level_authors, |
| 487 |
self.top_level_prs) |
| 488 |
self.changelog_entries.append(last_entry) |
| 489 |
elif self.find_changelog_location(line): |
| 490 |
last_entry = ChangeLogEntry(self.find_changelog_location(line), |
| 491 |
self.top_level_authors, |
| 492 |
self.top_level_prs) |
| 493 |
self.changelog_entries.append(last_entry) |
| 494 |
else: |
| 495 |
author_tuple = None |
| 496 |
pr_line = None |
| 497 |
if author_line_regex.match(line): |
| 498 |
m = author_line_regex.match(line) |
| 499 |
author_tuple = (m.group('name'), m.group('datetime')) |
| 500 |
elif additional_author_regex.match(line): |
| 501 |
m = additional_author_regex.match(line) |
| 502 |
if len(m.group('spaces')) != 4: |
| 503 |
msg = 'additional author must be indented with '\ |
| 504 |
'one tab and four spaces' |
| 505 |
self.errors.append(Error(msg, line)) |
| 506 |
else: |
| 507 |
author_tuple = (m.group('name'), None) |
| 508 |
elif pr_regex.match(line): |
| 509 |
m = pr_regex.match(line) |
| 510 |
component = m.group('component') |
| 511 |
pr = m.group('pr') |
| 512 |
if not component: |
| 513 |
self.errors.append(Error('missing PR component', line)) |
| 514 |
continue |
| 515 |
elif not component[:-1] in bug_components: |
| 516 |
self.errors.append(Error('invalid PR component', line)) |
| 517 |
continue |
| 518 |
else: |
| 519 |
pr_line = line.lstrip() |
| 520 |
if pr in self.subject_prs: |
| 521 |
self.subject_prs.remove(pr) |
| 522 |
elif dr_regex.match(line): |
| 523 |
pr_line = line.lstrip() |
| 524 |
|
| 525 |
lowered_line = line.lower() |
| 526 |
if lowered_line.startswith(CO_AUTHORED_BY_PREFIX): |
| 527 |
name = line[len(CO_AUTHORED_BY_PREFIX):] |
| 528 |
author = self.format_git_author(name) |
| 529 |
self.co_authors.append(author) |
| 530 |
continue |
| 531 |
elif lowered_line.startswith(REVIEW_PREFIXES): |
| 532 |
continue |
| 533 |
else: |
| 534 |
m = cherry_pick_regex.search(line) |
| 535 |
if m: |
| 536 |
commit = m.group('hash') |
| 537 |
if self.cherry_pick_commit: |
| 538 |
msg = 'multiple cherry pick lines' |
| 539 |
self.errors.append(Error(msg, line)) |
| 540 |
else: |
| 541 |
self.cherry_pick_commit = commit |
| 542 |
continue |
| 543 |
|
| 544 |
# ChangeLog name will be deduced later |
| 545 |
if not last_entry: |
| 546 |
if author_tuple: |
| 547 |
self.top_level_authors.append(author_tuple) |
| 548 |
continue |
| 549 |
elif pr_line: |
| 550 |
# append to top_level_prs only when we haven't met |
| 551 |
# a ChangeLog entry |
| 552 |
if (pr_line not in self.top_level_prs |
| 553 |
and not self.changelog_entries): |
| 554 |
self.top_level_prs.append(pr_line) |
| 555 |
continue |
| 556 |
else: |
| 557 |
last_entry = ChangeLogEntry(None, |
| 558 |
self.top_level_authors, |
| 559 |
self.top_level_prs) |
| 560 |
self.changelog_entries.append(last_entry) |
| 561 |
will_deduce = True |
| 562 |
elif author_tuple: |
| 563 |
if not last_entry.contains_author(author_tuple[0]): |
| 564 |
last_entry.author_lines.append(author_tuple) |
| 565 |
continue |
| 566 |
|
| 567 |
if not line.startswith('\t'): |
| 568 |
err = Error('line should start with a tab', line) |
| 569 |
self.errors.append(err) |
| 570 |
elif pr_line: |
| 571 |
last_entry.prs.append(pr_line) |
| 572 |
else: |
| 573 |
m = star_prefix_regex.match(line) |
| 574 |
if m: |
| 575 |
if (len(m.group('spaces')) != 1 and |
| 576 |
not last_entry.parentheses_stack): |
| 577 |
msg = 'one space should follow asterisk' |
| 578 |
self.errors.append(Error(msg, line)) |
| 579 |
else: |
| 580 |
content = m.group('content') |
| 581 |
parts = content.split(':') |
| 582 |
if len(parts) > 1: |
| 583 |
for needle in ('()', '[]', '<>'): |
| 584 |
if ' ' + needle in parts[0]: |
| 585 |
msg = f'empty group "{needle}" found' |
| 586 |
self.errors.append(Error(msg, line)) |
| 587 |
last_entry.lines.append(line) |
| 588 |
self.process_parentheses(last_entry, line) |
| 589 |
else: |
| 590 |
if last_entry.is_empty: |
| 591 |
msg = 'first line should start with a tab, ' \ |
| 592 |
'an asterisk and a space' |
| 593 |
self.errors.append(Error(msg, line)) |
| 594 |
else: |
| 595 |
last_entry.lines.append(line) |
| 596 |
self.process_parentheses(last_entry, line) |
| 597 |
|
| 598 |
def process_parentheses(self, last_entry, line): |
| 599 |
for c in line: |
| 600 |
if c == '(': |
| 601 |
last_entry.parentheses_stack.append(line) |
| 602 |
elif c == ')': |
| 603 |
if not last_entry.parentheses_stack: |
| 604 |
msg = 'bad wrapping of parenthesis' |
| 605 |
self.errors.append(Error(msg, line)) |
| 606 |
else: |
| 607 |
del last_entry.parentheses_stack[-1] |
| 608 |
|
| 609 |
def parse_file_names(self): |
| 610 |
for entry in self.changelog_entries: |
| 611 |
entry.parse_file_names() |
| 612 |
|
| 613 |
def check_file_patterns(self): |
| 614 |
for entry in self.changelog_entries: |
| 615 |
for pattern in entry.file_patterns: |
| 616 |
name = os.path.join(entry.folder, pattern) |
| 617 |
if not [name.startswith(pr) for pr in wildcard_prefixes]: |
| 618 |
msg = 'unsupported wildcard prefix' |
| 619 |
self.errors.append(Error(msg, name)) |
| 620 |
|
| 621 |
def check_for_empty_description(self): |
| 622 |
for entry in self.changelog_entries: |
| 623 |
for i, line in enumerate(entry.lines): |
| 624 |
if (item_empty_regex.match(line) and |
| 625 |
(i == len(entry.lines) - 1 |
| 626 |
or not entry.lines[i+1].strip() |
| 627 |
or item_parenthesis_regex.match(entry.lines[i+1]))): |
| 628 |
msg = 'missing description of a change' |
| 629 |
self.errors.append(Error(msg, line)) |
| 630 |
|
| 631 |
def check_for_broken_parentheses(self): |
| 632 |
for entry in self.changelog_entries: |
| 633 |
if entry.parentheses_stack: |
| 634 |
msg = 'bad parentheses wrapping' |
| 635 |
self.errors.append(Error(msg, entry.parentheses_stack[-1])) |
| 636 |
|
| 637 |
def check_line_start(self): |
| 638 |
for entry in self.changelog_entries: |
| 639 |
for line in entry.lines: |
| 640 |
if line.startswith('\t '): |
| 641 |
msg = 'extra space after tab' |
| 642 |
self.errors.append(Error(msg, line)) |
| 643 |
|
| 644 |
def get_file_changelog_location(self, changelog_file): |
| 645 |
for file in self.info.modified_files: |
| 646 |
if file[0] == changelog_file: |
| 647 |
# root ChangeLog file |
| 648 |
return '' |
| 649 |
index = file[0].find('/' + changelog_file) |
| 650 |
if index != -1: |
| 651 |
return file[0][:index] |
| 652 |
return None |
| 653 |
|
| 654 |
def deduce_changelog_locations(self): |
| 655 |
for entry in self.changelog_entries: |
| 656 |
if entry.folder is None: |
| 657 |
changelog = None |
| 658 |
for file in entry.files: |
| 659 |
location = self.get_file_changelog_location(file) |
| 660 |
if (location == '' |
| 661 |
or (location and location in self.changelog_locations)): |
| 662 |
if changelog and changelog != location: |
| 663 |
msg = 'could not deduce ChangeLog file, ' \ |
| 664 |
'not unique location' |
| 665 |
self.errors.append(Error(msg)) |
| 666 |
return |
| 667 |
changelog = location |
| 668 |
if changelog is not None: |
| 669 |
entry.folder = changelog |
| 670 |
else: |
| 671 |
msg = 'could not deduce ChangeLog file' |
| 672 |
self.errors.append(Error(msg)) |
| 673 |
|
| 674 |
@classmethod |
| 675 |
def in_ignored_location(cls, path): |
| 676 |
for ignored in ignored_prefixes: |
| 677 |
if path.startswith(ignored): |
| 678 |
return True |
| 679 |
return False |
| 680 |
|
| 681 |
def get_changelog_by_path(self, path): |
| 682 |
components = path.split('/') |
| 683 |
while components: |
| 684 |
if '/'.join(components) in self.changelog_locations: |
| 685 |
break |
| 686 |
components = components[:-1] |
| 687 |
return '/'.join(components) |
| 688 |
|
| 689 |
def check_mentioned_files(self): |
| 690 |
folder_count = len([x.folder for x in self.changelog_entries]) |
| 691 |
assert folder_count == len(self.changelog_entries) |
| 692 |
|
| 693 |
mentioned_files = set() |
| 694 |
mentioned_patterns = [] |
| 695 |
used_patterns = set() |
| 696 |
for entry in self.changelog_entries: |
| 697 |
if not entry.files and not entry.file_patterns: |
| 698 |
msg = 'no files mentioned for ChangeLog in directory' |
| 699 |
self.errors.append(Error(msg, entry.folder)) |
| 700 |
assert not entry.folder.endswith('/') |
| 701 |
for file in entry.files: |
| 702 |
if not self.is_changelog_filename(file): |
| 703 |
item = os.path.join(entry.folder, file) |
| 704 |
if item in mentioned_files: |
| 705 |
msg = 'same file specified multiple times' |
| 706 |
self.errors.append(Error(msg, file)) |
| 707 |
else: |
| 708 |
mentioned_files.add(item) |
| 709 |
for pattern in entry.file_patterns: |
| 710 |
mentioned_patterns.append(os.path.join(entry.folder, pattern)) |
| 711 |
|
| 712 |
cand = [x[0] for x in self.info.modified_files |
| 713 |
if not self.is_changelog_filename(x[0])] |
| 714 |
changed_files = set(cand) |
| 715 |
for file in sorted(mentioned_files - changed_files): |
| 716 |
msg = 'unchanged file mentioned in a ChangeLog' |
| 717 |
candidates = difflib.get_close_matches(file, changed_files, 1) |
| 718 |
details = None |
| 719 |
if candidates: |
| 720 |
msg += f' (did you mean "{candidates[0]}"?)' |
| 721 |
details = '\n'.join(difflib.Differ().compare([file], [candidates[0]])).rstrip() |
| 722 |
self.errors.append(Error(msg, file, details)) |
| 723 |
auto_add_warnings = defaultdict(list) |
| 724 |
for file in sorted(changed_files - mentioned_files): |
| 725 |
if not self.in_ignored_location(file): |
| 726 |
if file in self.new_files: |
| 727 |
changelog_location = self.get_changelog_by_path(file) |
| 728 |
# Python2: we cannot use next(filter(...)) |
| 729 |
entries = filter(lambda x: x.folder == changelog_location, |
| 730 |
self.changelog_entries) |
| 731 |
entries = list(entries) |
| 732 |
entry = entries[0] if entries else None |
| 733 |
if not entry: |
| 734 |
prs = self.top_level_prs |
| 735 |
if not prs: |
| 736 |
# if all ChangeLog entries have identical PRs |
| 737 |
# then use them |
| 738 |
if self.changelog_entries: |
| 739 |
prs = self.changelog_entries[0].prs |
| 740 |
for entry in self.changelog_entries: |
| 741 |
if entry.prs != prs: |
| 742 |
prs = [] |
| 743 |
break |
| 744 |
entry = ChangeLogEntry(changelog_location, |
| 745 |
self.top_level_authors, |
| 746 |
prs) |
| 747 |
self.changelog_entries.append(entry) |
| 748 |
# strip prefix of the file |
| 749 |
assert file.startswith(entry.folder) |
| 750 |
# do not allow auto-addition of New files |
| 751 |
# for the top-level folder |
| 752 |
if entry.folder: |
| 753 |
file = file[len(entry.folder):].lstrip('/') |
| 754 |
entry.lines.append('\t* %s: New file.' % file) |
| 755 |
entry.files.append(file) |
| 756 |
auto_add_warnings[entry.folder].append(file) |
| 757 |
else: |
| 758 |
msg = 'new file in the top-level folder not mentioned in a ChangeLog' |
| 759 |
self.errors.append(Error(msg, file)) |
| 760 |
else: |
| 761 |
used_pattern = [p for p in mentioned_patterns |
| 762 |
if file.startswith(p)] |
| 763 |
used_pattern = used_pattern[0] if used_pattern else None |
| 764 |
if used_pattern: |
| 765 |
used_patterns.add(used_pattern) |
| 766 |
else: |
| 767 |
msg = 'changed file not mentioned in a ChangeLog' |
| 768 |
self.errors.append(Error(msg, file)) |
| 769 |
|
| 770 |
for pattern in mentioned_patterns: |
| 771 |
if pattern not in used_patterns: |
| 772 |
error = "pattern doesn't match any changed files" |
| 773 |
self.errors.append(Error(error, pattern)) |
| 774 |
for entry, val in auto_add_warnings.items(): |
| 775 |
if len(val) == 1: |
| 776 |
self.warnings.append(f"Auto-added new file '{entry}/{val[0]}'") |
| 777 |
else: |
| 778 |
self.warnings.append(f"Auto-added {len(val)} new files in '{entry}'") |
| 779 |
|
| 780 |
def check_for_correct_changelog(self): |
| 781 |
for entry in self.changelog_entries: |
| 782 |
for file in entry.files: |
| 783 |
full_path = os.path.join(entry.folder, file) |
| 784 |
changelog_location = self.get_changelog_by_path(full_path) |
| 785 |
if changelog_location != entry.folder: |
| 786 |
msg = 'wrong ChangeLog location "%s", should be "%s"' |
| 787 |
err = Error(msg % (entry.folder, changelog_location), file) |
| 788 |
self.errors.append(err) |
| 789 |
|
| 790 |
@classmethod |
| 791 |
def format_authors_in_changelog(cls, authors, timestamp, prefix=''): |
| 792 |
output = '' |
| 793 |
for i, author in enumerate(authors): |
| 794 |
if i == 0: |
| 795 |
output += '%s%s %s\n' % (prefix, timestamp, author) |
| 796 |
else: |
| 797 |
output += '%s\t %s\n' % (prefix, author) |
| 798 |
output += '\n' |
| 799 |
return output |
| 800 |
|
| 801 |
def to_changelog_entries(self, use_commit_ts=False): |
| 802 |
current_timestamp = self.info.date.strftime(DATE_FORMAT) |
| 803 |
for entry in self.changelog_entries: |
| 804 |
output = '' |
| 805 |
timestamp = entry.datetime |
| 806 |
if self.revert_commit: |
| 807 |
timestamp = current_timestamp |
| 808 |
orig_date = self.original_info.date |
| 809 |
current_timestamp = orig_date.strftime(DATE_FORMAT) |
| 810 |
elif self.cherry_pick_commit: |
| 811 |
info = (self.commit_to_info_hook |
| 812 |
and self.commit_to_info_hook(self.cherry_pick_commit)) |
| 813 |
# it can happen that it is a cherry-pick for a different |
| 814 |
# repository |
| 815 |
if info: |
| 816 |
timestamp = info.date.strftime(DATE_FORMAT) |
| 817 |
else: |
| 818 |
if self.commit_to_info_hook: |
| 819 |
self.warnings.append(f"Cherry-picked commit not found: '{self.cherry_pick_commit}'") |
| 820 |
else: |
| 821 |
self.warnings.append(f"Invoked script can not obtain info about " |
| 822 |
f"cherry-picked commits such as '{self.revert_commit}'") |
| 823 |
timestamp = current_timestamp |
| 824 |
elif not timestamp or use_commit_ts: |
| 825 |
timestamp = current_timestamp |
| 826 |
authors = entry.authors if entry.authors else [self.info.author] |
| 827 |
# add Co-Authored-By authors to all ChangeLog entries |
| 828 |
for author in self.co_authors: |
| 829 |
if author not in authors: |
| 830 |
authors.append(author) |
| 831 |
|
| 832 |
if self.cherry_pick_commit or self.revert_commit: |
| 833 |
original_author = self.original_info.author |
| 834 |
output += self.format_authors_in_changelog([original_author], |
| 835 |
current_timestamp) |
| 836 |
if self.revert_commit: |
| 837 |
output += '\tRevert:\n' |
| 838 |
else: |
| 839 |
output += '\tBackported from master:\n' |
| 840 |
output += self.format_authors_in_changelog(authors, |
| 841 |
timestamp, '\t') |
| 842 |
else: |
| 843 |
output += self.format_authors_in_changelog(authors, timestamp) |
| 844 |
for pr in entry.prs: |
| 845 |
output += '\t%s\n' % pr |
| 846 |
for line in entry.lines: |
| 847 |
output += line + '\n' |
| 848 |
yield (entry.folder, output.rstrip()) |
| 849 |
|
| 850 |
def print_output(self): |
| 851 |
for entry, output in self.to_changelog_entries(): |
| 852 |
print('------ %s/ChangeLog ------ ' % entry) |
| 853 |
print(output) |
| 854 |
|
| 855 |
def print_errors(self): |
| 856 |
print('Errors:') |
| 857 |
for error in self.errors: |
| 858 |
print(error) |
| 859 |
|
| 860 |
def print_warnings(self): |
| 861 |
if self.warnings: |
| 862 |
print('Warnings:') |
| 863 |
for warning in self.warnings: |
| 864 |
print(warning) |
| 865 |
|
| 866 |
def check_commit_email(self): |
| 867 |
# Parse 'Martin Liska <[email protected]>' |
| 868 |
email = self.info.author.split(' ')[-1].strip('<>') |
| 869 |
|
| 870 |
# Verify that all characters are ASCII |
| 871 |
# TODO: Python 3.7 provides a nicer function: isascii |
| 872 |
if len(email) != len(email.encode()): |
| 873 |
self.errors.append(Error(f'non-ASCII characters in git commit email address ({email})')) |