]>
git.ipfire.org Git - thirdparty/gcc.git/blob - contrib/gcc-changelog/git_commit.py
3 # This file is part of GCC.
5 # GCC is free software; you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation; either version 3, or (at your option) any later
10 # GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 # You should have received a copy of the GNU General Public License
16 # along with GCC; see the file COPYING3. If not see
17 # <http://www.gnu.org/licenses/>. */
22 changelog_locations
= set([
25 'contrib/header-tools',
58 'libgcc/config/avr/libf7',
59 'libgcc/config/libbid',
77 bug_components
= set([
130 'gcc/go/gofrontend/',
131 'gcc/testsuite/go.test/test/',
133 'libphobos/libdruntime/',
144 author_line_regex
= \
145 re
.compile(r
'^(?P<datetime>\d{4}-\d{2}-\d{2})\ {2}(?P<name>.* <.*>)')
146 additional_author_regex
= re
.compile(r
'^\t(?P<spaces>\ *)?(?P<name>.* <.*>)')
147 changelog_regex
= re
.compile(r
'^([a-z0-9+-/]*)/ChangeLog:?')
148 pr_regex
= re
.compile(r
'\tPR (?P<component>[a-z+-]+\/)?([0-9]+)$')
149 dr_regex
= re
.compile(r
'\tDR ([0-9]+)$')
150 star_prefix_regex
= re
.compile(r
'\t\*(?P<spaces>\ *)(?P<content>.*)')
154 CO_AUTHORED_BY_PREFIX
= 'co-authored-by: '
155 CHERRY_PICK_PREFIX
= '(cherry picked from commit '
156 REVIEWED_BY_PREFIX
= 'reviewed-by: '
157 REVIEWED_ON_PREFIX
= 'reviewed-on: '
158 SIGNED_OFF_BY_PREFIX
= 'signed-off-by: '
160 REVIEW_PREFIXES
= (REVIEWED_BY_PREFIX
, REVIEWED_ON_PREFIX
,
161 SIGNED_OFF_BY_PREFIX
)
165 def __init__(self
, message
, line
=None):
166 self
.message
= message
172 s
+= ':"%s"' % self
.line
176 class ChangeLogEntry
:
177 def __init__(self
, folder
, authors
, prs
):
179 # Python2 has not 'copy' function
180 self
.author_lines
= list(authors
)
181 self
.initial_prs
= list(prs
)
188 for line
in self
.lines
:
189 m
= star_prefix_regex
.match(line
)
191 line
= m
.group('content')
193 line
= line
[:line
.index('(')]
195 line
= line
[:line
.index(':')]
196 for file in line
.split(','):
204 for author
in self
.author_lines
:
211 return [author_line
[0] for author_line
in self
.author_lines
]
215 return not self
.lines
and self
.prs
== self
.initial_prs
219 def __init__(self
, hexsha
, date
, author
, body
, modified_files
,
223 self
.modified_files
= modified_files
226 self
.changelog_entries
= []
230 self
.top_level_authors
= []
232 self
.top_level_prs
= []
234 project_files
= [f
for f
in self
.modified_files
235 if self
.is_changelog_filename(f
[0])
236 or f
[0] in misc_files
]
237 ignored_files
= [f
for f
in self
.modified_files
238 if self
.in_ignored_location(f
[0])]
239 if len(project_files
) == len(self
.modified_files
):
240 # All modified files are only MISC files
242 elif project_files
and strict
:
243 self
.errors
.append(Error('ChangeLog, DATESTAMP, BASE-VER and '
244 'DEV-PHASE updates should be done '
245 'separately from normal commits'))
248 all_are_ignored
= (len(project_files
) + len(ignored_files
)
249 == len(self
.modified_files
))
250 self
.parse_lines(all_are_ignored
)
252 self
.parse_changelog()
253 self
.deduce_changelog_locations()
255 self
.check_mentioned_files()
256 self
.check_for_correct_changelog()
260 return not self
.errors
264 return [x
[0] for x
in self
.modified_files
if x
[1] == 'A']
267 def is_changelog_filename(cls
, path
):
268 return path
.endswith('/ChangeLog') or path
== 'ChangeLog'
271 def find_changelog_location(cls
, name
):
272 if name
.startswith('\t'):
274 if name
.endswith(':'):
276 if name
.endswith('/'):
278 return name
if name
in changelog_locations
else None
281 def format_git_author(cls
, author
):
283 return author
.replace('<', ' <')
286 def parse_git_name_status(cls
, string
):
288 for entry
in string
.split('\n'):
289 parts
= entry
.split('\t')
291 if t
== 'A' or t
== 'D' or t
== 'M':
292 modified_files
.append((parts
[1], t
))
294 modified_files
.append((parts
[1], 'D'))
295 modified_files
.append((parts
[2], 'A'))
296 return modified_files
298 def parse_lines(self
, all_are_ignored
):
301 for i
, b
in enumerate(body
):
304 if (changelog_regex
.match(b
) or self
.find_changelog_location(b
)
305 or star_prefix_regex
.match(b
) or pr_regex
.match(b
)
306 or dr_regex
.match(b
) or author_line_regex
.match(b
)):
307 self
.changes
= body
[i
:]
309 if not all_are_ignored
:
310 self
.errors
.append(Error('cannot find a ChangeLog location in '
313 def parse_changelog(self
):
316 for line
in self
.changes
:
318 if last_entry
and will_deduce
:
321 if line
!= line
.rstrip():
322 self
.errors
.append(Error('trailing whitespace', line
))
323 if len(line
.replace('\t', ' ' * TAB_WIDTH
)) > LINE_LIMIT
:
324 self
.errors
.append(Error('line limit exceeds %d characters'
326 m
= changelog_regex
.match(line
)
328 last_entry
= ChangeLogEntry(m
.group(1), self
.top_level_authors
,
330 self
.changelog_entries
.append(last_entry
)
331 elif self
.find_changelog_location(line
):
332 last_entry
= ChangeLogEntry(self
.find_changelog_location(line
),
333 self
.top_level_authors
,
335 self
.changelog_entries
.append(last_entry
)
339 if author_line_regex
.match(line
):
340 m
= author_line_regex
.match(line
)
341 author_tuple
= (m
.group('name'), m
.group('datetime'))
342 elif additional_author_regex
.match(line
):
343 m
= additional_author_regex
.match(line
)
344 if len(m
.group('spaces')) != 4:
345 msg
= 'additional author must prepend with tab ' \
347 self
.errors
.append(Error(msg
, line
))
349 author_tuple
= (m
.group('name'), None)
350 elif pr_regex
.match(line
):
351 component
= pr_regex
.match(line
).group('component')
353 self
.errors
.append(Error('missing PR component', line
))
355 elif not component
[:-1] in bug_components
:
356 self
.errors
.append(Error('invalid PR component', line
))
359 pr_line
= line
.lstrip()
360 elif dr_regex
.match(line
):
361 pr_line
= line
.lstrip()
363 lowered_line
= line
.lower()
364 if lowered_line
.startswith(CO_AUTHORED_BY_PREFIX
):
365 name
= line
[len(CO_AUTHORED_BY_PREFIX
):]
366 author
= self
.format_git_author(name
)
367 self
.co_authors
.append(author
)
369 elif lowered_line
.startswith(REVIEW_PREFIXES
):
371 elif line
.startswith(CHERRY_PICK_PREFIX
):
374 # ChangeLog name will be deduced later
377 self
.top_level_authors
.append(author_tuple
)
380 # append to top_level_prs only when we haven't met
382 if (pr_line
not in self
.top_level_prs
383 and not self
.changelog_entries
):
384 self
.top_level_prs
.append(pr_line
)
387 last_entry
= ChangeLogEntry(None,
388 self
.top_level_authors
,
390 self
.changelog_entries
.append(last_entry
)
393 if author_tuple
not in last_entry
.author_lines
:
394 last_entry
.author_lines
.append(author_tuple
)
397 if not line
.startswith('\t'):
398 err
= Error('line should start with a tab', line
)
399 self
.errors
.append(err
)
401 last_entry
.prs
.append(pr_line
)
403 m
= star_prefix_regex
.match(line
)
405 if len(m
.group('spaces')) != 1:
406 err
= Error('one space should follow asterisk',
408 self
.errors
.append(err
)
410 last_entry
.lines
.append(line
)
412 if last_entry
.is_empty
:
413 msg
= 'first line should start with a tab, ' \
415 self
.errors
.append(Error(msg
, line
))
417 last_entry
.lines
.append(line
)
419 def get_file_changelog_location(self
, changelog_file
):
420 for file in self
.modified_files
:
421 if file[0] == changelog_file
:
422 # root ChangeLog file
424 index
= file[0].find('/' + changelog_file
)
426 return file[0][:index
]
429 def deduce_changelog_locations(self
):
430 for entry
in self
.changelog_entries
:
433 for file in entry
.files
:
434 location
= self
.get_file_changelog_location(file)
436 or (location
and location
in changelog_locations
)):
437 if changelog
and changelog
!= location
:
438 msg
= 'could not deduce ChangeLog file, ' \
439 'not unique location'
440 self
.errors
.append(Error(msg
))
443 if changelog
is not None:
444 entry
.folder
= changelog
446 msg
= 'could not deduce ChangeLog file'
447 self
.errors
.append(Error(msg
))
450 def in_ignored_location(cls
, path
):
451 for ignored
in ignored_prefixes
:
452 if path
.startswith(ignored
):
457 def get_changelog_by_path(cls
, path
):
458 components
= path
.split('/')
460 if '/'.join(components
) in changelog_locations
:
462 components
= components
[:-1]
463 return '/'.join(components
)
465 def check_mentioned_files(self
):
466 folder_count
= len([x
.folder
for x
in self
.changelog_entries
])
467 assert folder_count
== len(self
.changelog_entries
)
469 mentioned_files
= set()
470 for entry
in self
.changelog_entries
:
472 msg
= 'ChangeLog must contain a file entry'
473 self
.errors
.append(Error(msg
, entry
.folder
))
474 assert not entry
.folder
.endswith('/')
475 for file in entry
.files
:
476 if not self
.is_changelog_filename(file):
477 mentioned_files
.add(os
.path
.join(entry
.folder
, file))
479 cand
= [x
[0] for x
in self
.modified_files
480 if not self
.is_changelog_filename(x
[0])]
481 changed_files
= set(cand
)
482 for file in sorted(mentioned_files
- changed_files
):
483 self
.errors
.append(Error('file not changed in a patch', file))
484 for file in sorted(changed_files
- mentioned_files
):
485 if not self
.in_ignored_location(file):
486 if file in self
.new_files
:
487 changelog_location
= self
.get_changelog_by_path(file)
488 # Python2: we cannot use next(filter(...))
489 entries
= filter(lambda x
: x
.folder
== changelog_location
,
490 self
.changelog_entries
)
491 entries
= list(entries
)
492 entry
= entries
[0] if entries
else None
494 prs
= self
.top_level_prs
496 # if all ChangeLog entries have identical PRs
498 prs
= self
.changelog_entries
[0].prs
499 for entry
in self
.changelog_entries
:
503 entry
= ChangeLogEntry(changelog_location
,
504 self
.top_level_authors
,
506 self
.changelog_entries
.append(entry
)
507 # strip prefix of the file
508 assert file.startswith(entry
.folder
)
509 file = file[len(entry
.folder
):].lstrip('/')
510 entry
.lines
.append('\t* %s: New file.' % file)
512 msg
= 'changed file not mentioned in a ChangeLog'
513 self
.errors
.append(Error(msg
, file))
515 def check_for_correct_changelog(self
):
516 for entry
in self
.changelog_entries
:
517 for file in entry
.files
:
518 full_path
= os
.path
.join(entry
.folder
, file)
519 changelog_location
= self
.get_changelog_by_path(full_path
)
520 if changelog_location
!= entry
.folder
:
521 msg
= 'wrong ChangeLog location "%s", should be "%s"'
522 err
= Error(msg
% (entry
.folder
, changelog_location
), file)
523 self
.errors
.append(err
)
525 def to_changelog_entries(self
, use_commit_ts
=False):
526 for entry
in self
.changelog_entries
:
528 timestamp
= entry
.datetime
529 if not timestamp
or use_commit_ts
:
530 timestamp
= self
.date
.strftime('%Y-%m-%d')
531 authors
= entry
.authors
if entry
.authors
else [self
.author
]
532 # add Co-Authored-By authors to all ChangeLog entries
533 for author
in self
.co_authors
:
534 if author
not in authors
:
535 authors
.append(author
)
537 for i
, author
in enumerate(authors
):
539 output
+= '%s %s\n' % (timestamp
, author
)
541 output
+= '\t %s\n' % author
544 output
+= '\t%s\n' % pr
545 for line
in entry
.lines
:
546 output
+= line
+ '\n'
547 yield (entry
.folder
, output
.rstrip())
549 def print_output(self
):
550 for entry
, output
in self
.to_changelog_entries():
551 print('------ %s/ChangeLog ------ ' % entry
)
554 def print_errors(self
):
556 for error
in self
.errors
: