-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlog2json.py
More file actions
executable file
·109 lines (94 loc) · 3.04 KB
/
gitlog2json.py
File metadata and controls
executable file
·109 lines (94 loc) · 3.04 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
#!/usr/bin/env python
# commit
# auther
# date
# subtitle
# message
import re
import sys
import json
import argparse
import subprocess
def jsonize(log_range):
# commit, author, date, subtitle, message
NUM_STATE = 6
STATE_COMMIT = 0
STATE_AUTHOR = 1
STATE_DATE = 2
STATE_NEWLINE = 3
STATE_SUBJECT = 4
STATE_MSG = 5
state = STATE_COMMIT
msg = ""
if log_range:
proc = subprocess.Popen(["git", "log", "--no-merges", log_range], stdout=subprocess.PIPE)
else:
proc = subprocess.Popen(["git", "log", "--no-merges"], stdout=subprocess.PIPE)
rv = proc.wait()
if rv != 0:
return
o = open("gitlog.json", "w")
o.write("[\n")
while True:
line = proc.stdout.readline()
if line == '':
break
if state == STATE_COMMIT:
match = re.search("commit (\w{40})\n", line)
if match:
o.write(" {\n")
o.write(' "commit": %s,\n' % json.dumps(match.group(1)))
else:
print "parse error: ", state
break
elif state == STATE_AUTHOR:
match = re.search("Author: ([\w\.\s]+) <([\w\.]+@[\w\.-]+)>\n", line)
if match:
txt = "%s <%s>" % (match.group(1), match.group(2))
o.write(' "author": %s,\n' % json.dumps(match.group(1)))
o.write(' "email": %s,\n' % json.dumps(match.group(2)))
else:
print "parse error", state
break
elif state == STATE_DATE:
match = re.search("Date:\s{3}([\w\s:\+-]+)\n", line)
if match:
o.write(' "date": %s,\n' % json.dumps(match.group(1)))
else:
print "parse error: ", state
break
elif state == STATE_NEWLINE:
match = re.search("\n", line)
if not match:
print "parse error: ", state
break
elif state == STATE_SUBJECT:
o.write(' "subject": %s,\n' % json.dumps(line.rstrip("\n")))
elif state == STATE_MSG:
match = re.search("commit (\w{40})\n", line)
if match:
# finish up
o.write(' "message": %s\n' % json.dumps(msg))
o.write(' },\n\n')
# escape from msg
o.write(" {\n")
o.write(' "commit": %s,\n' % json.dumps(match.group(1)))
msg = ""
state = STATE_COMMIT
else:
msg = msg + line
continue
state = (state + 1) % NUM_STATE
o.write(' "message": %s\n' % json.dumps(msg))
o.write(' }\n')
o.write("]\n")
o.close()
def parse_args():
parser = argparse.ArgumentParser(description='Convert the git log to a json file')
parser.add_argument('log_range', nargs='?', help='<since>..<until>')
return parser.parse_args()
def main():
args = parse_args()
jsonize(args.log_range)
if __name__ == '__main__':
main()