1
|
|
|
# -*- coding: utf-8 -*- |
2
|
1 |
|
""" |
3
|
|
|
Git manipulation |
4
|
|
|
""" |
5
|
1 |
|
import re |
6
|
|
|
|
7
|
1 |
|
from git import GitCommandError, Repo |
|
|
|
|
8
|
1 |
|
from git.exc import InvalidGitRepositoryError, NoSuchPathError |
|
|
|
|
9
|
|
|
|
10
|
1 |
|
import git_app_version.helper.date as dthelper |
11
|
|
|
|
12
|
|
|
|
13
|
1 |
|
class GitHandler(object): |
|
|
|
|
14
|
|
|
|
15
|
1 |
|
def __init__(self, path): |
16
|
1 |
|
try: |
17
|
1 |
|
self.repo = Repo(path) |
18
|
1 |
|
except (InvalidGitRepositoryError, NoSuchPathError): |
19
|
1 |
|
raise ValueError( |
20
|
|
|
'The directory \'{}\' is not a git repository.'.format(path)) |
21
|
|
|
|
22
|
1 |
|
def get_deploy_date(self): |
|
|
|
|
23
|
1 |
|
return dthelper.utcnow() |
24
|
|
|
|
25
|
1 |
|
def get_version(self, commit='HEAD', default=''): |
|
|
|
|
26
|
1 |
|
try: |
27
|
1 |
|
version = self.repo.git.describe( |
28
|
|
|
'--tag', '--always', commit).strip() |
29
|
1 |
|
except GitCommandError: |
30
|
1 |
|
version = '' |
31
|
|
|
|
32
|
1 |
|
if not version: |
33
|
1 |
|
version = default |
34
|
|
|
|
35
|
1 |
|
return version |
36
|
|
|
|
37
|
1 |
|
def get_branches(self, commit='HEAD'): |
|
|
|
|
38
|
1 |
|
raw = self.repo.git.branch("--no-color", |
39
|
|
|
"--remote", |
40
|
|
|
"--contains=" + commit) |
41
|
1 |
|
raw_branches = raw.splitlines() |
42
|
|
|
|
43
|
1 |
|
regex_point = re.compile(r'->') # remove git reference pointing |
44
|
|
|
|
45
|
1 |
|
branches = [] |
46
|
1 |
|
for raw_branch in raw_branches: |
47
|
1 |
|
branch = raw_branch.strip() |
48
|
1 |
|
match = regex_point.search(branch) |
49
|
1 |
|
if not match: |
50
|
1 |
|
branches.append(branch) |
51
|
|
|
|
52
|
1 |
|
return branches |
53
|
|
|
|
54
|
1 |
|
def get_top_branches(self, branches, abbrev_commit=None): |
|
|
|
|
55
|
1 |
|
top_branch = [] |
56
|
|
|
|
57
|
1 |
|
for branch in branches: |
58
|
1 |
|
if abbrev_commit == self.get_abbrev_commit(branch): |
59
|
1 |
|
top_branch.append(branch) |
60
|
|
|
|
61
|
1 |
|
return top_branch |
62
|
|
|
|
63
|
1 |
|
def remove_remote_prefix(self, branches): |
|
|
|
|
64
|
1 |
|
regex_remote = re.compile(r'^[^/]+/') # remove git remote prefix |
65
|
|
|
|
66
|
1 |
|
clean_branches = [] |
67
|
1 |
|
for branch in branches: |
68
|
1 |
|
clean_branches.append(regex_remote.sub('', branch)) |
69
|
|
|
|
70
|
1 |
|
return clean_branches |
71
|
|
|
|
72
|
1 |
|
def get_committer_name(self, commit='HEAD'): |
|
|
|
|
73
|
1 |
|
return self.repo.commit(commit).committer.name |
74
|
|
|
|
75
|
1 |
|
def get_committer_email(self, commit='HEAD'): |
|
|
|
|
76
|
1 |
|
return self.repo.commit(commit).committer.email |
77
|
|
|
|
78
|
1 |
|
def get_author_name(self, commit='HEAD'): |
|
|
|
|
79
|
1 |
|
return self.repo.commit(commit).author.name |
80
|
|
|
|
81
|
1 |
|
def get_author_email(self, commit='HEAD'): |
|
|
|
|
82
|
1 |
|
return self.repo.commit(commit).author.email |
83
|
|
|
|
84
|
1 |
|
def get_commit_date(self, commit='HEAD'): |
|
|
|
|
85
|
1 |
|
return self.repo.commit(commit).committed_datetime |
86
|
|
|
|
87
|
1 |
|
def get_author_date(self, commit='HEAD'): |
|
|
|
|
88
|
1 |
|
return self.repo.commit(commit).authored_datetime |
89
|
|
|
|
90
|
1 |
|
def get_full_commit(self, commit='HEAD'): |
|
|
|
|
91
|
1 |
|
return self.repo.commit(commit).hexsha |
92
|
|
|
|
93
|
1 |
|
def get_abbrev_commit(self, commit='HEAD'): |
|
|
|
|
94
|
1 |
|
return self.repo.commit(commit).hexsha[0:7] |
95
|
|
|
|
96
|
1 |
|
def get_message(self, commit='HEAD'): |
|
|
|
|
97
|
1 |
|
return self.repo.commit(commit).message.strip() |
98
|
|
|
|
99
|
1 |
|
def get_infos(self, commit='HEAD'): |
|
|
|
|
100
|
1 |
|
deploy_date = self.get_deploy_date() |
101
|
1 |
|
abbrev_commit = self.get_abbrev_commit(commit) |
102
|
1 |
|
commit_date = self.get_commit_date(commit) |
103
|
1 |
|
author_date = self.get_author_date(commit) |
104
|
|
|
|
105
|
1 |
|
branches = self.get_branches(commit) |
106
|
1 |
|
top_branch = self.get_top_branches( |
107
|
|
|
branches=branches, abbrev_commit=abbrev_commit) |
108
|
|
|
|
109
|
1 |
|
return { |
110
|
|
|
'branches': self.remove_remote_prefix(branches), |
111
|
|
|
'top_branches': self.remove_remote_prefix(top_branch), |
112
|
|
|
'version': self.get_version(commit, default=abbrev_commit), |
113
|
|
|
'abbrev_commit': abbrev_commit, |
114
|
|
|
'full_commit': self.get_full_commit(commit), |
115
|
|
|
'message': self.get_message(commit), |
116
|
|
|
'author_name': self.get_author_name(commit), |
117
|
|
|
'author_email': self.get_author_email(commit), |
118
|
|
|
'committer_name': self.get_committer_name(commit), |
119
|
|
|
'committer_email': self.get_committer_email(commit), |
120
|
|
|
'commit_date': dthelper.iso8601_from_datetime(commit_date), |
121
|
|
|
'commit_timestamp': dthelper.timestamp_from_datetime(commit_date), |
122
|
|
|
'author_date': dthelper.iso8601_from_datetime(author_date), |
123
|
|
|
'author_timestamp': dthelper.timestamp_from_datetime(author_date), |
124
|
|
|
'deploy_date': dthelper.iso8601_from_datetime(deploy_date), |
125
|
|
|
'deploy_timestamp': dthelper.timestamp_from_datetime(deploy_date), |
126
|
|
|
} |
127
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.py
files in your module folders. Make sure that you place one file in each sub-folder.