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