-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathgitcheck.py
More file actions
executable file
·585 lines (498 loc) · 20.7 KB
/
gitcheck.py
File metadata and controls
executable file
·585 lines (498 loc) · 20.7 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import, division, print_function
import os
import re
import sys
import getopt
import time
import subprocess
from subprocess import PIPE
import smtplib
from smtplib import SMTPException
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import shlex
from os.path import expanduser
from time import strftime
import json
from colored import fg, bg, attr
# Global vars
argopts = {}
colortheme = None
#Load custom parameters from ~/mygitcheck.py
configfile = expanduser('~/mygitcheck.py')
if os.path.exists(configfile):
sys.path.append(expanduser('~'))
import mygitcheck as userconf
# Try to load colortheme
if hasattr(userconf, 'colortheme'):
colortheme = userconf.colortheme
if colortheme is None:
# Default theme
defaultcolor = attr('reset') + fg('white')
colortheme = {
'default': defaultcolor,
'prjchanged': attr('reset') + attr('bold') + fg('deep_pink_1a'),
'prjremote': attr('reverse') + fg('light_cyan'),
'prjname': attr('reset') + fg('chartreuse_1'),
'reponame': attr('reset') + fg('light_goldenrod_2b'),
'branchname': defaultcolor,
'fileupdated': attr('reset') + fg('light_goldenrod_2b'),
'remoteto': attr('reset') + fg('deep_sky_blue_3b'),
'committo': attr('reset') + fg('violet'),
'commitinfo': attr('reset') + fg('deep_sky_blue_3b'),
'commitstate': attr('reset') + fg('deep_pink_1a'),
'bell': "\a",
'reset': "\033[2J\033[H"
}
class html:
msg = "<ul>\n"
topull = ""
topush = ""
strlocal = ""
prjname = ""
path = ""
timestamp = ""
commit = ""
def showDebug(mess, level='info'):
if argopts.get('debugmod', False):
print(mess)
# Search all local repositories from current directory
def searchRepositories():
showDebug('Beginning scan... building list of git folders')
dirs = argopts.get('searchDir', [os.path.abspath(os.getcwd())])
repo = set()
for curdir in dirs:
if curdir[-1:] == '/':
curdir = curdir[:-1]
showDebug(" Scan git repositories from %s" % curdir)
html.path = curdir
startinglevel = curdir.count(os.sep)
for directory, dirnames, filenames in os.walk(curdir):
level = directory.count(os.sep) - startinglevel
if argopts.get('depth', None) is None or level <= argopts.get('depth', None):
if '.git' in dirnames:
showDebug(" Add %s repository" % directory)
repo.add(directory)
showDebug('Done')
return sorted(repo)
# Check state of a git repository
def checkRepository(rep, branch):
aitem = []
mitem = []
ditem = []
gsearch = re.compile(r'^.?([A-Z]) (.*)')
if re.match(argopts.get('ignoreBranch', r'^$'), branch):
return False
changes = getLocalFilesChange(rep)
ischange = len(changes) > 0
actionNeeded = False # actionNeeded is branch push/pull, not local file change.
topush = ""
topull = ""
commit = ""
html.topush = ""
html.topull = ""
if branch != "":
remotes = getRemoteRepositories(rep)
hasremotes = bool(remotes)
for r in remotes:
count = len(getLocalToPush(rep, r, branch))
ischange = ischange or (count > 0)
actionNeeded = actionNeeded or (count > 0)
if count > 0:
topush += " %s%s%s[%sTo Push:%s%s]" % (
colortheme['reponame'],
r,
colortheme['default'],
colortheme['remoteto'],
colortheme['default'],
count
)
html.topush += '<b style="color:black">%s</b>[<b style="color:blue">To Push:</b><b style="color:black">%s</b>]' % (
r,
count
)
for r in remotes:
count = len(getRemoteToPull(rep, r, branch))
ischange = ischange or (count > 0)
actionNeeded = actionNeeded or (count > 0)
if count > 0:
topull += " %s%s%s[%sTo Pull:%s%s]" % (
colortheme['reponame'],
r,
colortheme['default'],
colortheme['remoteto'],
colortheme['default'],
count
)
html.topull += '<b style="color:black">%s</b>[<b style="color:blue">To Pull:</b><b style="color:black">%s</b>]' % (
r,
count
)
if ischange or not argopts.get('quiet', False):
# Remove trailing slash from repository/directory name
if rep[-1:] == '/':
rep = rep[:-1]
# Do some magic to not show the absolute path as repository name
# Case 1: script was started in a directory that is a git repo
if rep == os.path.abspath(os.getcwd()):
(head, tail) = os.path.split(rep)
if tail != '':
repname = tail
# Case 2: script was started in a directory with possible subdirs that contain git repos
elif rep.find(os.path.abspath(os.getcwd())) == 0:
repname = rep[len(os.path.abspath(os.getcwd())) + 1:]
# Case 3: script was started with -d and above cases do not apply
else:
repname = rep
# @Xuning: print commit hash if asked
if argopts.get('commit', False):
gitCommit = getLatestShortCommit(rep)
html.commit = '(%s)' % (gitCommit)
commit = "(%s)" % (gitCommit)
elif argopts.get('Commit', False):
gitCommit = getLatestCommit(rep)
html.commit = '(%s)' % (gitCommit)
commit = "(%s)" % (gitCommit)
if ischange:
prjname = "%s%s%s" % (colortheme['prjchanged'], repname, colortheme['default'])
html.prjname = '<b style="color:red">%s</b>' % (repname)
elif not hasremotes:
prjname = "%s%s%s" % (colortheme['prjremote'], repname, colortheme['default'])
html.prjname = '<b style="color:magenta">%s</b>' % (repname)
else:
prjname = "%s%s%s" % (colortheme['prjname'], repname, colortheme['default'])
html.prjname = '<b style="color:green">%s</b>' % (repname)
# Print result
if len(changes) > 0:
strlocal = "%sLocal%s[" % (colortheme['reponame'], colortheme['default'])
lenFilesChnaged = len(getLocalFilesChange(rep))
strlocal += "%sTo Commit:%s%s" % (
colortheme['remoteto'],
colortheme['default'],
lenFilesChnaged
)
html.strlocal = '<b style="color:orange"> Local</b><b style="color:black">['
html.strlocal += "To Commit:%s" % (
lenFilesChnaged
)
strlocal += "]"
html.strlocal += "]</b>"
else:
strlocal = ""
html.strlocal = ""
if argopts.get('email', False):
html.msg += "<li>%s/%s %s %s %s</li>\n" % (html.prjname, branch, html.strlocal, html.topush, html.topull)
else:
cbranch = "%s%s" % (colortheme['branchname'], branch)
print("%(prjname)s/%(cbranch)s %(commit)s %(strlocal)s%(topush)s%(topull)s" % locals())
if argopts.get('verbose', False):
if ischange > 0:
filename = " |--Local"
if not argopts.get('email', False):
print(filename)
html.msg += '<ul><li><b>Local</b></li></ul>\n<ul>\n'
for c in changes:
filename = " |--%s%s%s %s%s" % (
colortheme['commitstate'],
c[0],
colortheme['fileupdated'],
c[1],
colortheme['default'])
html.msg += '<li> <b style="color:orange">[To Commit] </b>%s</li>\n' % c[1]
if not argopts.get('email', False): print(filename)
html.msg += '</ul>\n'
if branch != "":
remotes = getRemoteRepositories(rep)
for r in remotes:
commits = getLocalToPush(rep, r, branch)
if len(commits) > 0:
rname = " |--%(r)s" % locals()
html.msg += '<ul><li><b>%(r)s</b></li>\n</ul>\n<ul>\n' % locals()
if not argopts.get('email', False): print(rname)
for commit in commits:
pcommit = " |--%s[To Push]%s %s%s%s" % (
colortheme['committo'],
colortheme['default'],
colortheme['commitinfo'],
commit,
colortheme['default'])
html.msg += '<li><b style="color:blue">[To Push] </b>%s</li>\n' % commit
if not argopts.get('email', False): print(pcommit)
html.msg += '</ul>\n'
if branch != "":
remotes = getRemoteRepositories(rep)
for r in remotes:
commits = getRemoteToPull(rep, r, branch)
if len(commits) > 0:
rname = " |--%(r)s" % locals()
html.msg += '<ul><li><b>%(r)s</b></li>\n</ul>\n<ul>\n' % locals()
if not argopts.get('email', False): print(rname)
for commit in commits:
pcommit = " |--%s[To Pull]%s %s%s%s" % (
colortheme['committo'],
colortheme['default'],
colortheme['commitinfo'],
commit,
colortheme['default'])
html.msg += '<li><b style="color:blue">[To Pull] </b>%s</li>\n' % commit
if not argopts.get('email', False): print(pcommit)
html.msg += '</ul>\n'
return actionNeeded
def getLocalFilesChange(rep):
files = []
#curdir = os.path.abspath(os.getcwd())
snbchange = re.compile(r'^(.{2}) (.*)')
onlyTrackedArg = "" if argopts.get('checkUntracked', False) else "uno"
result = gitExec(rep, "status -s" + onlyTrackedArg)
lines = result.split('\n')
for l in lines:
if not re.match(argopts.get('ignoreLocal', r'^$'), l):
m = snbchange.match(l)
if m:
files.append([m.group(1), m.group(2)])
return files
def hasRemoteBranch(rep, remote, branch):
result = gitExec(rep, 'branch -r')
return '%s/%s' % (remote, branch) in result
def getLocalToPush(rep, remote, branch):
if not hasRemoteBranch(rep, remote, branch):
return []
result = gitExec(rep, "log %(remote)s/%(branch)s..%(branch)s --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, "log %(branch)s..%(remote)s/%(branch)s --oneline"
% locals())
return [x for x in result.split('\n') if x]
def updateRemote(rep):
gitExec(rep, "remote update")
# Get Default branch for repository
def getDefaultBranch(rep):
sbranch = re.compile(r'^\* (.*)', flags=re.MULTILINE)
gitbranch = gitExec(rep, "branch"
% locals())
branch = ""
m = sbranch.search(gitbranch)
if m:
branch = m.group(1)
return {branch}
# Get all branches for repository
def getAllBranches(rep):
gitbranch = gitExec(rep, "branch"
% locals())
branch = gitbranch.splitlines()
return [b[2:] for b in branch]
# @Xuning: Get latest commit for repository on the branch
def getLatestShortCommit(rep):
gitCommit = gitExec(rep, "rev-parse --short HEAD"
% locals())
return gitCommit.strip()
def getLatestCommit(rep):
gitCommit = gitExec(rep, "rev-parse HEAD"
% locals())
return gitCommit.strip()
def getRemoteRepositories(rep):
result = gitExec(rep, "remote"
% locals())
remotes = [x for x in result.split('\n') if x]
return remotes
def gitExec(path, cmd):
commandToExecute = "git -C \"%s\" %s" % (path, cmd)
cmdargs = shlex.split(commandToExecute)
showDebug("EXECUTE GIT COMMAND '%s'" % cmdargs)
p = subprocess.Popen(cmdargs, stdout=PIPE, stderr=PIPE)
output, errors = p.communicate()
if p.returncode:
print('Failed running %s' % commandToExecute)
raise Exception(errors)
return output.decode('utf-8')
# Check all git repositories
def gitcheck():
showDebug("Global Vars: %s" % argopts)
repo = searchRepositories()
actionNeeded = False
if argopts.get('checkremote', False):
for r in repo:
print ("Updating %s remotes..." % r)
updateRemote(r)
if argopts.get('watchInterval', 0) > 0:
print(colortheme['reset'])
print(strftime("%Y-%m-%d %H:%M:%S"))
showDebug("Processing repositories... please wait.")
for r in repo:
if (argopts.get('checkall', False)):
branch = getAllBranches(r)
else:
branch = getDefaultBranch(r)
for b in branch:
if checkRepository(r, b):
actionNeeded = True
html.timestamp = strftime("%Y-%m-%d %H:%M:%S")
html.msg += "</ul>\n<p>Report created on %s</p>\n" % html.timestamp
if actionNeeded and argopts.get('bellOnActionNeeded', False):
print(colortheme['bell'])
def sendReport(content):
userPath = expanduser('~')
filepath = r'%s\Documents\.gitcheck' % userPath
filename = filepath + "//mail.properties"
config = json.load(open(filename))
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Gitcheck Report (%s)" % (html.path)
msg['From'] = config['from']
msg['To'] = config['to']
# Create the body of the message (a plain-text and an HTML version).
text = "Gitcheck report for %s created on %s\n\n This file can be seen in html only." % (html.path, html.timestamp)
htmlcontent = "<html>\n<head>\n<h1>Gitcheck Report</h1>\n<h2>%s</h2>\n</head>\n<body>\n<p>%s</p>\n</body>\n</html>" % (
html.path, content
)
# Write html file to disk
f = open(filepath + '//result.html', 'w')
f.write(htmlcontent)
print ("File saved under %s\\result.html" % filepath)
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(htmlcontent, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
try:
print ("Sending email to %s" % config['to'])
# Send the message via local SMTP server.
s = smtplib.SMTP(config['smtp'], config['smtp_port'])
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(config['from'], config['to'], msg.as_string())
s.quit()
except SMTPException as e:
print("Error sending email : %s" % str(e))
def initEmailConfig():
config = {
'smtp': 'yourserver',
'smtp_port': 25,
'from': 'from@server.com',
'to': 'to@server.com'
}
userPath = expanduser('~')
saveFilePath = r'%s\Documents\.gitcheck' % userPath
if not os.path.exists(saveFilePath):
os.makedirs(saveFilePath)
filename = saveFilePath + '\mail.properties'
json.dump(config, fp=open(filename, 'w'), indent=4)
print('Please, modify config file located here : %s' % filename)
def readDefaultConfig():
filename = expanduser('~/.gitcheck')
if os.path.exists(filename):
pass
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(" --debug Show debug message")
print(" -r, --remote force remote update (slow)")
print(" -u, --untracked Show untracked files")
print(" -b, --bell bell on action needed")
print(" -w <sec>, --watch=<sec> after displaying, wait <sec> and run again")
print(" -i <re>, --ignore-branch=<re> ignore branches matching the regex <re>")
print(" -d <dir>, --dir=<dir> Search <dir> for repositories (can be used multiple times)")
print(" -m <maxdepth>, --maxdepth=<maxdepth> Limit the depth of repositories search")
print(" -q, --quiet Display info only when repository needs action")
print(" -e, --email Send an email with result as html, using mail.properties parameters")
print(" -a, --all-branch Show the status of all branches")
print(" -l <re>, --localignore=<re> ignore changes in local files which match the regex <re>")
print(" --init-email Initialize mail.properties file (has to be modified by user using JSON Format)")
print(" -c --commit Show short commit hash (git rev-parse --short HEAD) ")
print(" -C --Commit Show long commit hash (git rev-parse HEAD) ")
def main():
try:
opts, args = getopt.getopt(
sys.argv[1:],
"vhrubcCw:i:d:m:q:e:al:",
[
"verbose", "debug", "help", "remote", "untracked", "bell", "commit", "Commit", "watch=", "ignore-branch=",
"dir=", "maxdepth=", "quiet", "email", "init-email", "all-branch", "localignore="
]
)
except getopt.GetoptError as e:
if e.opt == 'w' and 'requires argument' in e.msg:
print("Please indicate nb seconds for refresh ex: gitcheck -w10")
else:
print(e.msg)
sys.exit(2)
readDefaultConfig()
for opt, arg in opts:
if opt in ["-v", "--verbose"]:
argopts['verbose'] = True
elif opt in ["--debug"]:
argopts['debugmod'] = True
elif opt in ["-r", "--remote"]:
argopts['checkremote'] = True
elif opt in ["-u", "--untracked"]:
argopts['checkUntracked'] = True
elif opt in ["-b", "--bell"]:
argopts['bellOnActionNeeded'] = True
elif opt in ["-w", "--watch"]:
try:
argopts['watchInterval'] = float(arg)
except ValueError:
print("option %s requires numeric value" % opt)
sys.exit(2)
elif opt in ["-i", "--ignore-branch"]:
argopts['ignoreBranch'] = arg
elif opt in ["-l", "--localignore"]:
argopts['ignoreLocal'] = arg
elif opt in ["-d", "--dir"]:
dirs = argopts.get('searchDir', [])
if (dirs == []):
argopts['searchDir'] = dirs
dirs.append(arg)
elif opt in ["-m", '--maxdepth']:
try:
argopts['depth'] = int(arg)
except ValueError:
print("option %s requires int value" % opt)
sys.exit(2)
elif opt in ["-q", "--quiet"]:
argopts['quiet'] = True
elif opt in ["-e", "--email"]:
argopts['email'] = True
elif opt in ["-a", "--all-branch"]:
argopts['checkall'] = True
elif opt in ["-c", "--commit"]:
argopts['commit'] = True
elif opt in ["-C", "--Commit"]:
argopts['Commit'] = True
elif opt in ["--init-email"]:
initEmailConfig()
sys.exit(0)
elif opt in ["-h", "--help"]:
usage()
sys.exit(0)
# else:
# print "Unhandled option %s" % opt
# sys.exit(2)
while True:
try:
gitcheck()
if argopts.get('email', False):
sendReport(html.msg)
except (KeyboardInterrupt, SystemExit):
raise
except Exception as e:
print ("Unexpected error:", str(e))
if argopts.get('watchInterval', 0) > 0:
time.sleep(argopts.get('watchInterval', 0))
else:
break
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
sys.exit(0)