-
Notifications
You must be signed in to change notification settings - Fork 1
/
diffparser.py
78 lines (58 loc) · 2.18 KB
/
diffparser.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
import re
from classes import *
getLinesNumsRE = re.compile(r'@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@')
def parseDiffLine(line):
diffLine = DiffLine()
if line.startswith("-"):
diffLine.state = DiffLineState.DELETE
elif line.startswith("+"):
diffLine.state = DiffLineState.INSERT
elif line.startswith(" "):
diffLine.state = DiffLineState.UNCHANGED
else:
return None
diffLine.text = line
return diffLine
def transformFilename(filename, regex):
return ( re.sub(regex, "", filename) )
def hackRemoveNonValid(diffFiles):
# Iterate backwards, as forward iteration does not handle removing items
for diffFile in reversed(diffFiles):
#diffFile = diffFiles[i]
if diffFile.changes == []:
if not settings.quiet:
print ( "Removing invalid input: %s" % (diffFile.diffLine) )
diffFiles.remove(diffFile)
def parseLines(lines, diffPrefixRegex):
diffFiles = []
diffFile = None
change = None
lineNum = 0
try:
for line in lines:
lineNum += 1
if line.startswith("diff"):
diffFile = DiffFile()
diffFiles.append(diffFile)
diffFile.diffLine = line
elif line.startswith("@@"):
(oldStr, newStr) = getLinesNumsRE.match(line).groups()
change = DiffChange()
diffFile.changes.append(change)
change.oldStartLine = int(oldStr)
change.newStartLine = int(newStr)
change.description = line
elif line.startswith("---"):
diffFile.oldPath = transformFilename(line[4:], diffPrefixRegex)
elif line.startswith("+++"):
diffFile.newPath = transformFilename(line[4:], diffPrefixRegex)
diffFile.pathChanged = diffFile.newPath != diffFile.oldPath
else:
diffLine = parseDiffLine(line)
if ( diffLine is None):
continue
change.lines.append( diffLine )
except Exception as e:
raise ParseError(lineNum, e)
hackRemoveNonValid(diffFiles);
return diffFiles