xref: /netbsd-src/external/gpl3/gcc/dist/contrib/gcc-changelog/git_repository.py (revision a448f87c3717b704e85b1f0e5d631e4b5a0aae17)
1fb8a8121Smrg#!/usr/bin/env python3
2fb8a8121Smrg#
3fb8a8121Smrg# This file is part of GCC.
4fb8a8121Smrg#
5fb8a8121Smrg# GCC is free software; you can redistribute it and/or modify it under
6fb8a8121Smrg# the terms of the GNU General Public License as published by the Free
7fb8a8121Smrg# Software Foundation; either version 3, or (at your option) any later
8fb8a8121Smrg# version.
9fb8a8121Smrg#
10fb8a8121Smrg# GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11fb8a8121Smrg# WARRANTY; without even the implied warranty of MERCHANTABILITY or
12fb8a8121Smrg# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13fb8a8121Smrg# for more details.
14fb8a8121Smrg#
15fb8a8121Smrg# You should have received a copy of the GNU General Public License
16fb8a8121Smrg# along with GCC; see the file COPYING3.  If not see
17fb8a8121Smrg# <http://www.gnu.org/licenses/>.  */
18fb8a8121Smrg
19fb8a8121Smrgfrom datetime import datetime
20fb8a8121Smrg
21fb8a8121Smrgtry:
22fb8a8121Smrg    from git import Repo
23fb8a8121Smrgexcept ImportError:
24fb8a8121Smrg    print('Cannot import GitPython package, please install the package:')
25fb8a8121Smrg    print('  Fedora, openSUSE: python3-GitPython')
26fb8a8121Smrg    print('  Debian, Ubuntu: python3-git')
27fb8a8121Smrg    exit(1)
28fb8a8121Smrg
29fb8a8121Smrgfrom git_commit import GitCommit, GitInfo, decode_path
30fb8a8121Smrg
31fb8a8121Smrg
32*a448f87cSmrgdef parse_git_revisions(repo_path, revisions, ref_name=None):
33fb8a8121Smrg    repo = Repo(repo_path)
34fb8a8121Smrg
35fb8a8121Smrg    def commit_to_info(commit):
36fb8a8121Smrg        try:
37fb8a8121Smrg            c = repo.commit(commit)
38fb8a8121Smrg            diff = repo.commit(commit + '~').diff(commit)
39fb8a8121Smrg
40fb8a8121Smrg            modified_files = []
41fb8a8121Smrg            for file in diff:
42fb8a8121Smrg                if hasattr(file, 'renamed_file'):
43fb8a8121Smrg                    is_renamed = file.renamed_file
44fb8a8121Smrg                else:
45fb8a8121Smrg                    is_renamed = file.renamed
46fb8a8121Smrg                if file.new_file:
47fb8a8121Smrg                    t = 'A'
48fb8a8121Smrg                elif file.deleted_file:
49fb8a8121Smrg                    t = 'D'
50fb8a8121Smrg                elif is_renamed:
51fb8a8121Smrg                    # Consider that renamed files are two operations:
52fb8a8121Smrg                    # the deletion of the original name
53fb8a8121Smrg                    # and the addition of the new one.
54fb8a8121Smrg                    modified_files.append((decode_path(file.a_path), 'D'))
55fb8a8121Smrg                    t = 'A'
56fb8a8121Smrg                else:
57fb8a8121Smrg                    t = 'M'
58fb8a8121Smrg                modified_files.append((decode_path(file.b_path), t))
59fb8a8121Smrg
60fb8a8121Smrg            date = datetime.utcfromtimestamp(c.committed_date)
61fb8a8121Smrg            author = '%s  <%s>' % (c.author.name, c.author.email)
62fb8a8121Smrg            git_info = GitInfo(c.hexsha, date, author,
63fb8a8121Smrg                               c.message.split('\n'), modified_files)
64fb8a8121Smrg            return git_info
65fb8a8121Smrg        except ValueError:
66fb8a8121Smrg            return None
67fb8a8121Smrg
68fb8a8121Smrg    parsed_commits = []
69fb8a8121Smrg    if '..' in revisions:
70fb8a8121Smrg        commits = list(repo.iter_commits(revisions))
71fb8a8121Smrg    else:
72fb8a8121Smrg        commits = [repo.commit(revisions)]
73fb8a8121Smrg
74fb8a8121Smrg    for commit in commits:
75*a448f87cSmrg        git_commit = GitCommit(commit_to_info(commit.hexsha),
76*a448f87cSmrg                               commit_to_info_hook=commit_to_info,
77*a448f87cSmrg                               ref_name=ref_name)
78fb8a8121Smrg        parsed_commits.append(git_commit)
79fb8a8121Smrg    return parsed_commits
80