2020-05-13 14:27:30 +02:00
|
|
|
#!/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)
|
|
|
|
|
2021-01-06 08:11:57 +01:00
|
|
|
from git_commit import GitCommit, GitInfo, decode_path
|
2020-05-13 14:27:30 +02:00
|
|
|
|
|
|
|
|
2021-05-10 14:13:53 +02:00
|
|
|
def parse_git_revisions(repo_path, revisions, ref_name=None):
|
2020-05-13 14:27:30 +02:00
|
|
|
repo = Repo(repo_path)
|
|
|
|
|
2020-06-30 10:12:45 +02:00
|
|
|
def commit_to_info(commit):
|
2020-06-17 10:41:17 +02:00
|
|
|
try:
|
|
|
|
c = repo.commit(commit)
|
2020-06-30 10:12:45 +02:00
|
|
|
diff = repo.commit(commit + '~').diff(commit)
|
|
|
|
|
|
|
|
modified_files = []
|
|
|
|
for file in diff:
|
2020-06-30 17:44:45 +02:00
|
|
|
if hasattr(file, 'renamed_file'):
|
|
|
|
is_renamed = file.renamed_file
|
|
|
|
else:
|
|
|
|
is_renamed = file.renamed
|
2020-06-30 10:12:45 +02:00
|
|
|
if file.new_file:
|
|
|
|
t = 'A'
|
|
|
|
elif file.deleted_file:
|
|
|
|
t = 'D'
|
2020-06-30 17:44:45 +02:00
|
|
|
elif is_renamed:
|
2020-06-30 10:12:45 +02:00
|
|
|
# Consider that renamed files are two operations:
|
|
|
|
# the deletion of the original name
|
|
|
|
# and the addition of the new one.
|
2021-01-06 08:11:57 +01:00
|
|
|
modified_files.append((decode_path(file.a_path), 'D'))
|
2020-06-30 10:12:45 +02:00
|
|
|
t = 'A'
|
|
|
|
else:
|
|
|
|
t = 'M'
|
2021-01-06 08:11:57 +01:00
|
|
|
modified_files.append((decode_path(file.b_path), t))
|
2020-06-30 10:12:45 +02:00
|
|
|
|
|
|
|
date = datetime.utcfromtimestamp(c.committed_date)
|
|
|
|
author = '%s <%s>' % (c.author.name, c.author.email)
|
|
|
|
git_info = GitInfo(c.hexsha, date, author,
|
|
|
|
c.message.split('\n'), modified_files)
|
|
|
|
return git_info
|
2020-06-17 10:41:17 +02:00
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
2020-05-13 14:27:30 +02:00
|
|
|
parsed_commits = []
|
|
|
|
if '..' in revisions:
|
|
|
|
commits = list(repo.iter_commits(revisions))
|
|
|
|
else:
|
|
|
|
commits = [repo.commit(revisions)]
|
|
|
|
|
|
|
|
for commit in commits:
|
2021-05-10 14:13:53 +02:00
|
|
|
git_commit = GitCommit(commit_to_info(commit.hexsha),
|
2021-05-10 13:51:22 +02:00
|
|
|
commit_to_info_hook=commit_to_info,
|
|
|
|
ref_name=ref_name)
|
2020-05-13 14:27:30 +02:00
|
|
|
parsed_commits.append(git_commit)
|
|
|
|
return parsed_commits
|