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