-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.py
More file actions
141 lines (113 loc) · 4.08 KB
/
Copy pathtemp.py
File metadata and controls
141 lines (113 loc) · 4.08 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
###
#inspired by https://gist.github.com/simonw/091b765a071d1558464371042db3b959
###
import subprocess
import datetime
# commit{} in commits[]
# commit_hash
# branch
# author
# date
# commit_note
# stat
def main():
raw_text = getGitLog()
commits = gitLogToList(raw_text)
constructred_commits = []
for commit_dict in commits:
constructred_commit_dict = constructForDB(commit_dict)
constructred_commits.append(constructred_commit_dict)
# saveCommitToDB(constructred_commit_dict, model)
return constructred_commits
def getGitLog():
raw = subprocess.check_output(
['git', 'log', '--decorate', '--shortstat'], stderr=subprocess.STDOUT
).decode("utf-8")
return raw
def gitLogToList(raw_text):
commits = []
current_commit = {}
lines = raw_text.split("\n")
for line in lines:
if line == '':
continue
if line.startswith('commit '):
if current_commit:
commits.append(current_commit)
current_commit = {}
commit_hash, branch = parseCommitLine(line)
current_commit['commit_hash'] = commit_hash
current_commit['branch'] = branch
elif line.startswith(' '):
commit_note = line.strip()
current_commit["commit_note"] = commit_note
elif line.startswith(' '):
current_commit['stat'] = line.strip()
else:
key, value = line.split(':', 1)
current_commit[key.lower()] = value.strip()
# Save the last commit
commits.append(current_commit)
return commits
def parseCommitLine(line):
_, commit_hash = line[:47].split(' ')
if '(' in line:
branch = line[48:][1:-1]
else:
branch = 'master'
return commit_hash, branch
def constructForDB(commit):
constructred_dict = {}
# commit_hash 40 char
constructred_dict['commit_hash'] = commit['commit_hash']
# branch 30 char
constructred_dict['branch'] = commit['branch']
# Author 30 char # Mail 30 char
author, email = commit['author'].split('<')
constructred_dict['author'] = author.strip()
constructred_dict['email'] = email[-1].strip()
# Date timestamp
datetime_object = datetime.datetime.strptime(
commit['date'], '%a %b %d %H:%M:%S %Y %z')
constructred_dict['datetime'] = datetime_object
# Commit_note 200 char
constructred_dict['commit_note'] = commit['commit_note']
# file_changed_count int # insertions_count int # deletions_count int
try:
file_changed_count, insertions_count, deletions_count = parseStatLine(
commit['stat'])
except KeyError:
if commit['merge']:
file_changed_count, insertions_count, deletions_count = (0, 0, 0)
else:
raise KeyError
constructred_dict['file_changed_count'] = file_changed_count
constructred_dict['insertions_count'] = insertions_count
constructred_dict['deletions_count'] = deletions_count
return constructred_dict
def parseStatLine(statLine):
stats = statLine.split(',')
file_changed_count = 0
insertions_count = 0
deletions_count = 0
for stat in stats:
if 'changed' in stat:
file_changed_count = int(stat.strip().split(' ')[0])
elif 'insertion' in stat:
insertions_count = int(stat.strip().split(' ')[0])
elif 'deletion' in stat:
deletions_count = int(stat.strip().split(' ')[0])
return (file_changed_count, insertions_count, deletions_count)
# def saveCommitToDB(constructred_commit):
# instance = MyModel(
# commit_hash=constructred_dict['commit_hash'],
# branch=constructred_dict['branch'],
# author=constructred_dict['author'],
# email=constructred_dict['email'],
# datetime_object=constructred_dict['datetime'],
# commit_note=constructred_dict['commit_note'],
# file_changed_count=constructred_dict['file_changed_count'],
# insertions_count=constructred_dict['insertions_count'],
# deletions_count=constructred_dict['deletions_count']
# )
# instance.save()