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