gcc/contrib/gcc-changelog/git_repository.py
Pierre-Marie de Rodat a634157de1 gcc-changelog: enhance handling of renamings
So far, we expect from a commit that renames a file to contain a
changelog entry only for the new name. For example, after the following
commit:

   $ git move foo bar
   $ git commit

We expect the following changelog:

   * bar: Renamed from foo.

Git does not keep track of renamings, only file deletions and additions.
The display of patches then uses heuristics (with config-dependent
parameters) to try to match deleted and added files in the same commit.
It is thus brittle to rely on this information.

This commit modifies changelog processing so that renames are considered
as a deletion of a file plus an addition of another file. The following
changelog is now expected for the above example:

   * foo: Move...
   * bar: Here.

contrib/

	* gcc-changelog/git_email.py (GitEmail.__init__): Interpret file
	renamings as a file deletion plus a file addition.
	* gcc-changelog/git_repository.py (parse_git_revisions):
	Likewise.
	* gcc-changelog/test_email.py: New testcase.
	* gcc-changelog/test_patches.txt: New testcase.
2020-05-28 11:14:44 +02:00

66 lines
2.2 KiB
Python
Executable File

#!/usr/bin/env python3
#
# This file is part of GCC.
#
# GCC is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later
# version.
#
# GCC is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with GCC; see the file COPYING3. If not see
# <http://www.gnu.org/licenses/>. */
from datetime import datetime
try:
from git import Repo
except ImportError:
print('Cannot import GitPython package, please install the package:')
print(' Fedora, openSUSE: python3-GitPython')
print(' Debian, Ubuntu: python3-git')
exit(1)
from git_commit import GitCommit
def parse_git_revisions(repo_path, revisions, strict=False):
repo = Repo(repo_path)
parsed_commits = []
if '..' in revisions:
commits = list(repo.iter_commits(revisions))
else:
commits = [repo.commit(revisions)]
for commit in commits:
diff = repo.commit(commit.hexsha + '~').diff(commit.hexsha)
modified_files = []
for file in diff:
if file.new_file:
t = 'A'
elif file.deleted_file:
t = 'D'
elif file.renamed_file:
# Consider that renamed files are two operations: the deletion
# of the original name and the addition of the new one.
modified_files.append((file.a_path, 'D'))
t = 'A'
else:
t = 'M'
modified_files.append((file.b_path, t))
date = datetime.utcfromtimestamp(commit.committed_date)
author = '%s <%s>' % (commit.author.name, commit.author.email)
git_commit = GitCommit(commit.hexsha, date, author,
commit.message.split('\n'), modified_files,
strict=strict)
parsed_commits.append(git_commit)
return parsed_commits