-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit.py
More file actions
202 lines (169 loc) · 5.75 KB
/
git.py
File metadata and controls
202 lines (169 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python3
################################################################################
#
# Description:
# ------------
# Generic git helper functions.
#
################################################################################
import subprocess
################################################################################
#
# A custom git exception class
#
################################################################################
class GitException(Exception):
pass
################################################################################
#
# Runs a git command
#
# Returns:
# the stdout output of the git command if the command succeeded
#
# Throws:
# GitException if the git command failed. The exception will have two
# arguments - the first is the command that failed and the second is the
# stderr output
#
################################################################################
def run_command(*args, **kwargs):
args = list(args)
args.insert(0, 'git')
kwargs['stdout'] = subprocess.PIPE
kwargs['stderr'] = subprocess.PIPE
proc = subprocess.Popen(args, **kwargs)
(stdout, stderr) = proc.communicate()
str_stdout = stdout.decode('utf-8')
str_stderr = stderr.decode('utf-8')
if proc.returncode != 0:
raise GitException(" ".join(args), str_stderr)
return str_stdout.rstrip('\n')
################################################################################
#
# Checks whether we are in a git repository
#
# Returns:
# The top-level directory of the git repository if we are in one, an empty
# string otherwise
#
################################################################################
def in_repo():
try:
git_top = run_command('rev-parse', '--show-toplevel')
except GitException as e:
return ""
return git_top
################################################################################
#
# Gets the current branch name
#
# Returns:
# The name of the currently checked out branch if there is one, an empty
# string otherwise
#
################################################################################
def get_current_branch():
try:
branch_name = run_command('symbolic-ref', '--short', 'HEAD')
except GitException as e:
return ""
return branch_name
################################################################################
#
# Checks whether something is a valid git commit (or an annotated tag that
# points at a commit)
#
# Params:
# ref = the reference to check
#
# Returns:
# True if the given string is a valid git commit, false otherwise
#
################################################################################
def is_valid_commit(ref):
ref = ref + '^{commit}'
try:
run_command('rev-parse', '--quiet', '--short', '--verify', ref)
except GitException as e:
return False
return True
################################################################################
#
# Checks whether something is a valid git object of any type
#
# Params:
# ref = the reference to check
#
# Returns:
# True if the given string is a valid git object, false otherwise
#
################################################################################
def is_valid_object(ref):
ref = ref + '^{object}'
try:
run_command('rev-parse', '--quiet', '--short', '--verify', ref)
except GitException as e:
return False
return True
################################################################################
#
# Gets a list of the basic details of all commits in the given range. Note
# that the start of the range is *not* included in the returned list.
#
# Each element of the list contains the information of a single commit
# starting from the end of the range and going towards the start. Each element
# contains the short hash of the commit followed by a space followed by the
# title of the commit.
#
# Params:
# start = start of the range (not included in the returned list)
# end = end of the range
#
# Returns:
# A list containing the basic details of all commits in the range. An
# empty list if there are no commits in the range or if an error occurred.
#
################################################################################
def get_commits_list(start, end):
commits_range = start + '..' + end
try:
git_log_output = run_command('log', '--oneline', commits_range)
except GitException as e:
print("Git command '{}' failed with the following error:".format(e.args[0]))
print(e.args[1].rstrip('\n'))
return []
if not git_log_output:
# There are no commits in the range
return []
return git_log_output.split('\n')
################################################################################
#
# Checks whether gpg signing of commits is enabled
#
# Returns:
# True if gpg signing of commits is enabled, false otherwise
#
################################################################################
def is_gpg_signing_enabled():
try:
run_command('config', 'user.signingkey')
except GitException as e:
return False
return True
################################################################################
#
# Gets the email address of the current user
#
# Returns:
# The email address of the current user, an empty string if an error
# occurred
#
################################################################################
def get_user_email():
try:
user_email = run_command('config', 'user.email')
except GitException as e:
return ""
return user_email
# vim: set tw=80 :