- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit.py
More file actions
Latest commit
executable file
·200 lines (167 loc) · 5.66 KB
/
Copy pathgit.py
File metadata and controls
executable file
·200 lines (167 loc) · 5.66 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
#!/usr/bin/env python
################################################################################
#
# Description:
# ------------
# Generic git helper functions.
#
################################################################################
importsubprocess
################################################################################
#
# A custom git exception class
#
################################################################################
classGitException(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
#
################################################################################
defrun_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()
ifproc.returncode!=0:
raiseGitException(" ".join(args), stderr)
returnstdout.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
#
################################################################################
defin_repo():
try:
git_top=run_command('rev-parse', '--show-toplevel')
exceptGitExceptionase:
return""
returngit_top
################################################################################
#
# Gets the current branch name
#
# Returns:
# The name of the currently checked out branch if there is one, an empty
# string otherwise
#
################################################################################
defget_current_branch():
try:
branch_name=run_command('symbolic-ref', '--short', 'HEAD')
exceptGitExceptionase:
return""
returnbranch_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
#
################################################################################
defis_valid_commit(ref):
ref=ref+'^{commit}'
try:
run_command('rev-parse', '--quiet', '--short', '--verify', ref)
exceptGitExceptionase:
returnFalse
returnTrue
################################################################################
#
# 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
#
################################################################################
defis_valid_object(ref):
ref=ref+'^{object}'
try:
run_command('rev-parse', '--quiet', '--short', '--verify', ref)
exceptGitExceptionase:
returnFalse
returnTrue
################################################################################
#
# 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.
#
################################################################################
defget_commits_list(start, end):
commits_range=start+'..'+end
try:
git_log_output=run_command('log', '--oneline', commits_range)
exceptGitExceptionase:
print"Git command '{}' failed with the following error:".format(e.args[0])
printe.args[1].rstrip('\n')
return []
ifnotgit_log_output:
# There are no commits in the range
return []
returngit_log_output.split('\n')
################################################################################
#
# Checks whether gpg signing of commits is enabled
#
# Returns:
# True if gpg signing of commits is enabled, false otherwise
#
################################################################################
defis_gpg_signing_enabled():
try:
run_command('config', 'user.signingkey')
exceptGitExceptionase:
returnFalse
returnTrue
################################################################################
#
# Gets the email address of the current user
#
# Returns:
# The email address of the current user, an empty string if an error
# occurred
#
################################################################################
defget_user_email():
try:
user_email=run_command('config', 'user.email')
exceptGitExceptionase:
return""
returnuser_email
# vim: set tw=80 :