-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitWatch.py
84 lines (72 loc) · 2.05 KB
/
gitWatch.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
from subprocess import Popen, PIPE
import shutil
import time
import datetime
import sys
logFileName = 'gitWatch.log'
datetimeFormat = '%Y-%m-%d %H:%M:%S'
def callProcess(cmd):
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
return stdout.decode('utf-8', 'replace')
def report(filename, date = None):
print('report ' + filename)
branchmap = {}
with open(filename) as f:
lines = f.readlines()
lastBranch = ''
startDate = None
timeSpan = None
lastLine = lines[-1]
for l in lines:
splitted = l.split('\t')
dateTimeStr =splitted[0]
branch = splitted[1][:-1]
if branch.find('...') != -1:
branch = branch.split('...')[0]
if lastBranch != branch or l == lastLine:
endDate = datetime.datetime.strptime(dateTimeStr, datetimeFormat)
if startDate != None and lastBranch != 'end':
timeSpan = endDate - startDate
if lastBranch in branchmap:
branchmap[lastBranch] = branchmap[lastBranch] + timeSpan
else:
branchmap[lastBranch] = timeSpan
print (str(endDate).ljust(22), str(timeSpan).ljust(10), lastBranch)
lastBranch = branch
startDate = endDate
print(80*'-')
for k, v in branchmap.items():
print(k, v)
input("Press Enter to continue...")
def getLogLine(content):
now = datetime.datetime.now().strftime(datetimeFormat)
logLine = '%s\t%s' % (now, content)
return logLine
arglen = len(sys.argv)
if arglen > 1:
filename = sys.argv[1]
if arglen > 2:
date = sys.argv[2]
else:
date = None
report(filename, date)
exit()
lastBranch = ''
try:
while True:
result = callProcess('git status -b --porcelain')
firstLine = result.split('\n')[0]
branch = firstLine.split(' ')[1]
logLine = getLogLine(branch)
print (logLine)
log = open(logFileName, 'a')
print (logLine, file=log)
log.close()
time.sleep(60)
except (KeyboardInterrupt, SystemExit):
logLine = getLogLine('end')
print (logLine)
log = open(logFileName, 'a')
print (logLine, file=log)
log.close()