forked from badele/gitcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitcheck.py
273 lines (219 loc) · 7.43 KB
/
gitcheck.py
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import re
import sys
import getopt
import fnmatch
# Class for terminal Color
class tcolor:
DEFAULT = "\033[0m"
BOLD = "\033[1m"
RED = "\033[91m"
GREEN = "\033[92m"
BLUE = "\033[96m"
ORANGE = "\033[93m"
MAGENTA = "\033[95m"
# Search all local repositories from current directory
def searchRepositories():
curdir = os.path.abspath(os.getcwd())
repo = []
rsearch = re.compile(r'^/?(.*?)/\.git')
for root, dirnames, filenames in os.walk(curdir):
for dirnames in fnmatch.filter(dirnames, '*.git'):
fdir = os.path.join(root, dirnames)
fdir = fdir.replace(curdir, '')
m = rsearch.match(fdir)
if m:
repo.append(m.group(1))
return repo
# Check state of a git repository
def checkRepository(rep, verbose=False, checkremote=False, ignoreBranch=r'^$'):
aitem = []
mitem = []
ditem = []
curdir = os.path.abspath(os.getcwd())
gsearch = re.compile(r'^.?([A-Z]) (.*)')
if checkremote:
updateRemote(rep)
branch = getDefaultBranch(rep)
if re.match(ignoreBranch, branch):
return
changes = getLocalFilesChange(rep)
ischange = len(changes) > 0
branch = getDefaultBranch(rep)
topush = ""
topull = ""
if branch != "":
remotes = getRemoteRepositories(rep)
for r in remotes:
count = len(getLocalToPush(rep, r, branch))
ischange = ischange or (count > 0)
if count > 0:
topush += " %s%s%s[%sTo Push:%s%s]" % (
tcolor.ORANGE,
r,
tcolor.DEFAULT,
tcolor.BLUE,
tcolor.DEFAULT,
count
)
for r in remotes:
count = len(getRemoteToPull(rep, r, branch))
ischange = ischange or (count > 0)
if count > 0:
topull += " %s%s%s[%sTo Pull:%s%s]" % (
tcolor.ORANGE,
r,
tcolor.DEFAULT,
tcolor.BLUE,
tcolor.DEFAULT,
count
)
if ischange:
color = tcolor.BOLD + tcolor.RED
else:
color = tcolor.DEFAULT + tcolor.GREEN
# Print result
prjname = "%s%s%s" % (color, rep, tcolor.DEFAULT)
if len(changes) > 0:
strlocal = "%sLocal%s[" % (tcolor.ORANGE, tcolor.DEFAULT)
strlocal += "%sTo Commit:%s%s" % (
tcolor.BLUE,
tcolor.DEFAULT,
len(getLocalFilesChange(rep))
)
strlocal += "]"
else:
strlocal = ""
print("%(prjname)s/%(branch)s %(strlocal)s%(topush)s%(topull)s" % locals())
if verbose:
if ischange > 0:
filename = " |--Local"
print(filename)
for c in changes:
filename = " |--%s%s%s" % (
tcolor.ORANGE,
c[1],
tcolor.DEFAULT)
print(filename)
if branch != "":
remotes = getRemoteRepositories(rep)
for r in remotes:
commits = getLocalToPush(rep, r, branch)
if len(commits) > 0:
rname = " |--%(r)s" % locals()
print(rname)
for commit in commits:
commit = " |--%s[To Push]%s %s%s%s" % (
tcolor.MAGENTA,
tcolor.DEFAULT,
tcolor.BLUE,
commit,
tcolor.DEFAULT)
print(commit)
if branch != "":
remotes = getRemoteRepositories(rep)
for r in remotes:
commits = getRemoteToPull(rep, r, branch)
if len(commits) > 0:
rname = " |--%(r)s" % locals()
print(rname)
for commit in commits:
commit = " |--%s[To Pull]%s %s%s%s" % (
tcolor.MAGENTA,
tcolor.DEFAULT,
tcolor.BLUE,
commit,
tcolor.DEFAULT)
print(commit)
def getLocalFilesChange(rep):
files = []
curdir = os.path.abspath(os.getcwd())
snbchange = re.compile(r'^(.{2}) (.*)')
result = gitExec(rep, "git status -suno"
% locals())
lines = result.split('\n')
for l in lines:
m = snbchange.match(l)
if m:
files.append([m.group(1), m.group(2)])
return files
def hasRemoteBranch(rep, remote, branch):
result = gitExec(rep, "git branch -r | grep '%(remote)s/%(branch)s'"
% locals())
return (result != "")
def getLocalToPush(rep, remote, branch):
if not hasRemoteBranch(rep, remote, branch):
return []
result = gitExec(rep, "git log %(remote)s/%(branch)s..HEAD --oneline"
% locals())
return [x for x in result.split('\n') if x]
def getRemoteToPull(rep, remote, branch):
if not hasRemoteBranch(rep, remote, branch):
return []
result = gitExec(rep, "git log HEAD..%(remote)s/%(branch)s --oneline"
% locals())
return [x for x in result.split('\n') if x]
def updateRemote(rep):
gitExec(rep, "git remote update")
# Get Default branch for repository
def getDefaultBranch(rep):
curdir = os.path.abspath(os.getcwd())
sbranch = re.compile(r'^\* (.*)')
gitbranch = gitExec(rep, "git branch | grep '*'"
% locals())
branch = ""
m = sbranch.match(gitbranch)
if m:
branch = m.group(1)
return branch
def getRemoteRepositories(rep):
result = gitExec(rep, "git remote"
% locals())
remotes = [x for x in result.split('\n') if x]
return remotes
# Custom git command
def gitExec(rep, command):
curdir = os.path.abspath(os.getcwd())
cmd = "cd %(curdir)s/%(rep)s ; %(command)s" % locals()
cmd = os.popen(cmd)
return cmd.read()
# Check all git repositories
def gitcheck(verbose, checkremote, ignoreBranch):
repo = searchRepositories()
for r in repo:
checkRepository(r, verbose, checkremote, ignoreBranch)
def usage():
print("Usage: %s [OPTIONS]" % (sys.argv[0]))
print("Check multiple git repository in one pass")
print("== Common options ==")
print(" -v, --verbose Show files & commits")
print(" -r, --remote force remote update(slow)")
print(" -i <re>, --ignore-branch <re> ignore branches matching the regex <re>")
def main():
try:
opts, args = getopt.getopt(
sys.argv[1:],
"vhri:",
["verbose", "help", "remote", "ignore-branch:"])
except getopt.GetoptError:
sys.exit(2)
verbose = False
checkremote = False
ignoreBranch = r'^$' # empty string
for opt, arg in opts:
if opt in ("-v", "--verbose"):
verbose = True
if opt in ("-r", "--remote"):
checkremote = True
if opt in ("-r", "--remote"):
checkremote = True
if opt in ("-i", "--ignore-branch"):
ignoreBranch = arg
if opt in ("-h", "--help"):
usage()
sys.exit(0)
gitcheck(verbose, checkremote, ignoreBranch)
if __name__ == "__main__":
main()