1
|
|
|
# -*- coding: utf-8 -*- |
2
|
1 |
|
""" |
3
|
|
|
Git manipulation |
4
|
|
|
""" |
5
|
1 |
|
import re |
6
|
1 |
|
from git_app_version.helper.process import output_command, call_command |
7
|
1 |
|
import git_app_version.helper.date as datehelper |
8
|
|
|
|
9
|
|
|
|
10
|
1 |
|
class Git(object): |
|
|
|
|
11
|
|
|
|
12
|
1 |
|
def is_git_repo(self, cwd=None): |
|
|
|
|
13
|
1 |
|
return call_command(["git", "rev-parse"], cwd=cwd) == 0 |
14
|
|
|
|
15
|
1 |
|
def get_deploy_date(self): |
|
|
|
|
16
|
1 |
|
return datehelper.utcnow() |
17
|
|
|
|
18
|
1 |
|
def get_version(self, commit='HEAD', default=None, cwd=None): |
|
|
|
|
19
|
1 |
|
version = output_command( |
20
|
|
|
["git", "describe", "--tag", "--always", commit], cwd=cwd).strip() |
21
|
1 |
|
if version == '' or version is None: |
22
|
1 |
|
if not default: |
23
|
1 |
|
default = self.get_abbrev_commit(commit, cwd=cwd) |
24
|
|
|
|
25
|
1 |
|
version = default |
26
|
|
|
|
27
|
1 |
|
return version |
28
|
|
|
|
29
|
1 |
|
def get_branches(self, commit='HEAD', cwd=None): |
|
|
|
|
30
|
1 |
|
raw_branches = output_command(["git", |
31
|
|
|
"branch", |
32
|
|
|
"--no-color", |
33
|
|
|
"--remote", |
34
|
|
|
"--contains=" + commit], |
35
|
|
|
cwd=cwd).splitlines() |
36
|
|
|
|
37
|
1 |
|
regex_point = re.compile(r'->') # remove git reference pointing |
38
|
|
|
|
39
|
1 |
|
branches = [] |
40
|
1 |
|
for raw_branch in raw_branches: |
41
|
1 |
|
branch = raw_branch.strip() |
42
|
1 |
|
match = regex_point.search(branch) |
43
|
1 |
|
if not match: |
44
|
1 |
|
branches.append(branch) |
45
|
|
|
|
46
|
1 |
|
return branches |
47
|
|
|
|
48
|
1 |
|
def get_top_branches(self, branches=None, abbrev_commit=None, cwd=None): |
|
|
|
|
49
|
1 |
|
top_branch = [] |
50
|
|
|
|
51
|
1 |
|
if branches: |
52
|
1 |
|
for branch in branches: |
53
|
1 |
|
if abbrev_commit == self.get_abbrev_commit(branch, cwd=cwd): |
54
|
1 |
|
top_branch.append(branch) |
55
|
|
|
|
56
|
1 |
|
return top_branch |
57
|
|
|
|
58
|
1 |
|
def remove_remote_prefix(self, branches): |
|
|
|
|
59
|
1 |
|
regex_remote = re.compile(r'^[^/]+/') # remove git remote prefix |
60
|
|
|
|
61
|
1 |
|
clean_branches = [] |
62
|
1 |
|
for branch in branches: |
63
|
1 |
|
clean_branches.append(regex_remote.sub('', branch)) |
64
|
|
|
|
65
|
1 |
|
return clean_branches |
66
|
|
|
|
67
|
1 |
|
def get_committer_name(self, commit='HEAD', cwd=None): |
|
|
|
|
68
|
1 |
|
return self._get_log_field(field='cn', commit=commit, cwd=cwd) |
69
|
|
|
|
70
|
1 |
|
def get_committer_email(self, commit='HEAD', cwd=None): |
|
|
|
|
71
|
1 |
|
return self._get_log_field(field='ce', commit=commit, cwd=cwd) |
72
|
|
|
|
73
|
1 |
|
def get_author_name(self, commit='HEAD', cwd=None): |
|
|
|
|
74
|
1 |
|
return self._get_log_field(field='an', commit=commit, cwd=cwd) |
75
|
|
|
|
76
|
1 |
|
def get_author_email(self, commit='HEAD', cwd=None): |
|
|
|
|
77
|
1 |
|
return self._get_log_field(field='ae', commit=commit, cwd=cwd) |
78
|
|
|
|
79
|
1 |
|
def _get_log_field(self, field, commit='HEAD', cwd=None): |
|
|
|
|
80
|
1 |
|
return output_command(["git", |
81
|
|
|
"log", |
82
|
|
|
"-1", |
83
|
|
|
"--pretty=tformat:%" + field, |
84
|
|
|
"--no-color", |
85
|
|
|
commit], |
86
|
|
|
cwd=cwd).strip() |
87
|
|
|
|
88
|
1 |
|
def get_commit_date(self, commit='HEAD', cwd=None): |
|
|
|
|
89
|
1 |
|
return self._get_date(field='ci', commit=commit, cwd=cwd) |
90
|
|
|
|
91
|
1 |
|
def get_author_date(self, commit='HEAD', cwd=None): |
|
|
|
|
92
|
1 |
|
return self._get_date(field='ai', commit=commit, cwd=cwd) |
93
|
|
|
|
94
|
1 |
|
def _get_date(self, field, commit='HEAD', cwd=None): |
|
|
|
|
95
|
1 |
|
isodate = output_command(["git", |
96
|
|
|
"log", |
97
|
|
|
"-1", |
98
|
|
|
"--pretty=tformat:%" + field, |
99
|
|
|
"--no-color", |
100
|
|
|
"--date=local", |
101
|
|
|
commit], |
102
|
|
|
cwd=cwd).strip().replace(' ', |
103
|
|
|
'T', |
104
|
|
|
1).replace(' ', |
105
|
|
|
'') |
106
|
|
|
|
107
|
1 |
|
return datehelper.datetime_from_iso8601(isodate, utc=True) |
108
|
|
|
|
109
|
1 |
|
def get_full_commit(self, commit='HEAD', cwd=None): |
|
|
|
|
110
|
1 |
|
return output_command(["git", |
111
|
|
|
"rev-list", |
112
|
|
|
"--max-count=1", |
113
|
|
|
"--no-abbrev-commit", |
114
|
|
|
commit], |
115
|
|
|
cwd=cwd).strip() |
116
|
|
|
|
117
|
1 |
|
def get_abbrev_commit(self, commit='HEAD', cwd=None): |
|
|
|
|
118
|
1 |
|
return output_command(["git", |
119
|
|
|
"rev-list", |
120
|
|
|
"--max-count=1", |
121
|
|
|
"--abbrev-commit", |
122
|
|
|
commit], |
123
|
|
|
cwd=cwd).strip() |
124
|
|
|
|
125
|
1 |
|
def get_infos(self, commit='HEAD', cwd=None): |
|
|
|
|
126
|
1 |
|
deploy_date = self.get_deploy_date() |
127
|
1 |
|
abbrev_commit = self.get_abbrev_commit(commit, cwd=cwd) |
128
|
1 |
|
commit_date = self.get_commit_date(commit, cwd=cwd) |
129
|
1 |
|
author_date = self.get_author_date(commit, cwd=cwd) |
130
|
|
|
|
131
|
1 |
|
branches = self.get_branches(commit, cwd=cwd) |
132
|
1 |
|
top_branch = self.get_top_branches( |
133
|
|
|
branches=branches, abbrev_commit=abbrev_commit, cwd=cwd) |
134
|
|
|
|
135
|
1 |
|
return { |
136
|
|
|
'branches': self.remove_remote_prefix(branches), |
137
|
|
|
'top_branches': self.remove_remote_prefix(top_branch), |
138
|
|
|
'version': self.get_version(commit, default=abbrev_commit, cwd=cwd), |
139
|
|
|
'abbrev_commit': abbrev_commit, |
140
|
|
|
'full_commit': self.get_full_commit(commit, cwd=cwd), |
141
|
|
|
'author_name': self.get_author_name(commit, cwd=cwd), |
142
|
|
|
'author_email': self.get_author_email(commit, cwd=cwd), |
143
|
|
|
'committer_name': self.get_committer_name(commit, cwd=cwd), |
144
|
|
|
'committer_email': self.get_committer_email(commit, cwd=cwd), |
145
|
|
|
'commit_date': datehelper.iso8601_from_datetime(commit_date), |
146
|
|
|
'commit_timestamp': datehelper.timestamp_from_datetime(commit_date), |
147
|
|
|
'author_date': datehelper.iso8601_from_datetime(author_date), |
148
|
|
|
'author_timestamp': datehelper.timestamp_from_datetime(author_date), |
149
|
|
|
'deploy_date': datehelper.iso8601_from_datetime(deploy_date), |
150
|
|
|
'deploy_timestamp': datehelper.timestamp_from_datetime(deploy_date), |
151
|
|
|
} |
152
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.