-
Notifications
You must be signed in to change notification settings - Fork 0
/
diffReader.py
222 lines (164 loc) · 5.71 KB
/
diffReader.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
import subprocess
import re
#-----------------------
# Defines
#-----------------------
path = "/Users/mystfit/Code/Python/Gitdive_Prototypes"
diffCmd = ["git", "log", "-p", "--reverse", '--pretty=format:"GD_commit|-%H||GD_commitAuthor|-%cn||GD_date|-%cd||GD_message|-%B"']
globalRemoves = 0
globalAdds = 0
#-----------------------
# Classes
#-----------------------
class LogEntry:
def __init__(self,entry):
logVars = entry[1:].rstrip('\n').split("||");
if(len(logVars) < 2):
self.commitHash = ""
self.author = ""
self.date = ""
self.message = ""
else:
self.commitHash = logVars[0].split("|-")[1]
self.author = logVars[1].split("|-")[1]
self.date = logVars[2].split("|-")[1]
self.message = logVars[3].split("|-")[1]
class DiffEntry:
def __init__(self):
self.fileNameA = ""
self.fileNameB = ""
self.diffType = "text"
self.fileMode = "updated"
self.hunkList = []
def getFilename(self):
if(self.fileNameB != "/dev/null"):
return self.fileNameB
elif(self.fileNameA != "/dev/null"):
return self.fileNameA
class DiffHunk:
def __init__(self):
self.startRemoveLines = 0
self.numRemoveLines= 0
self.startAddLines = 0
self.numAddLines= 0
self.hunkData = []
class CommitEntry:
def __init__(self, commitHash, author, date, message):
self.commitHash = commitHash
self.author = author
self.date = date
self.message = message
self.diffList = []
# Add a new raw diff to this commit
def addAndParseDiff(self, diff):
self.diffList.append(self.parseRawDiff(diff))
def serializeCommit(self):
commitText = "/***=== Commit begins\n"
commitText += "|== Commit hash: " + self.commitHash + "\n"
commitText += "|== Author: " + self.author + "\n"
commitText += "|== Date: " + self.date + "\n"
commitText += "|== Message: " + self.message + "\n\n"
commitText += self.serializeDiffs()
commitText += "\\***=== Commit ends\n\n\n"
return commitText
# Convert diffs into a readable string
def serializeDiffs(self):
hunkString = ""
for i in range(len(self.diffList[0])):
diff = self.diffList[0][i]
hunkString += "|/=== Diff File: " + diff.getFilename() + "\n"
for j in range(len(diff.hunkList)):
hunkString += "||/--- Code hunk begins\n"
for k in range(len(diff.hunkList[j].hunkData)):
hunkString += "|| " + diff.hunkList[j].hunkData[k]
hunkString += "||\\--- Code hunk ends\n"
hunkString += "|\\=== Diff Finish\n"
return hunkString + "\n"
# Convert raw diff lines into diff and hunk objects
def parseRawDiff(self, diff):
global globalAdds
global globalRemoves
parsedDiff = []
currentDiff = None
currentHunk = None
inDiffHeader = False
for i in range(len(diff)):
currentLine = diff[i];
#If the line starts with 'diff', then we make a new diff object and save off the current
if re.match('diff', currentLine):
#Set line context to header so we can expect diff metadata
inDiffHeader = True
# Create new file diff
currentDiff = DiffEntry()
parsedDiff.append(currentDiff)
if(inDiffHeader):
#Diff index
if(re.match("index ", currentLine)):
pass
elif(re.match("new file mode", currentLine)):
currentDiff.fileMode = "added"
elif(re.match("deleted file mode", currentLine)):
currentDiff.fileMode = "deleted"
#Binary file diff information
elif(re.match("Binary files", currentLine)):
currentDiff.diffType = "binary"
#File A name
elif(re.match("--- a/", currentLine)):
currentDiff.fileNameA = currentLine.split("--- a/")[1].rstrip("\n")
#File B name
elif(re.match("\+\+\+ b/", currentLine)):
currentDiff.fileNameB = currentLine.split("+++ b/")[1].rstrip("\n")
#New hunk section in diff
if(re.match("@@ ", currentLine)):
currentHunk = DiffHunk()
currentDiff.hunkList.append(currentHunk);
preDiffLines = currentLine.split(" ")[1].split(",");
postDiffLines = currentLine.split(" ")[2].split(",");
currentHunk.startRemoveLines = int(preDiffLines[0][1:])
currentHunk.startAddLines = int(postDiffLines[0][1:])
if (len(preDiffLines) > 1): currentHunk.numRemoveLines = int(preDiffLines[1])
if (len(postDiffLines) > 1): currentHunk.numAddLines = int(postDiffLines[1])
inDiffHeader = False
#Currently in a hunk
else:
if(currentHunk and not inDiffHeader):
currentHunk.hunkData.append(currentLine)
if(currentLine[:1] == "-"):
globalRemoves += 1
if(currentLine[:1] == "+"):
globalAdds += 1
return parsedDiff
def parseDiffLog(diffStream):
currLn = ""
log = None
commit = None
commitList = []
diffBlock = []
for line in iter(diffStream.readline, ''):
# Check for a new commit log entry
if(re.match('"GD_commit', line)):
log = LogEntry(line)
if(commit):
#Parse last commit added
commit.addAndParseDiff(diffBlock)
commitList.append(commit)
diffBlock = []
#Parse commit information before setting up new commit obj
commit = CommitEntry(log.commitHash, log.author, log.date, log.message)
else:
diffBlock.append(line)
return commitList
#-----------------------
# Program start
#-----------------------
# logStream = subprocess.Popen(logCmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, cwd=path)
# log = logStream.stdout.read().replace('\n\"\n', '\n').split("\n")
commitList = []
diffFile = open('diffFile.log', 'w')
diffFile.write("Git commit log for repository: " + path + "\n---------------------------------------------------\n\n");
diffStream = subprocess.Popen(diffCmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, cwd=path)
parsedDiffLog = parseDiffLog(diffStream.stdout)
for i in range(len(parsedDiffLog)):
diffFile.write(parsedDiffLog[i].serializeCommit())
print str(globalAdds) + " lines added"
print str(globalRemoves) + " lines removed"