-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.go
138 lines (128 loc) · 3.3 KB
/
diff.go
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
package main
import (
"strconv"
"strings"
)
type diff struct {
files []*diffFile
}
func newDiff() *diff {
return &diff{
files: make([]*diffFile, 0, 10),
}
}
func parseDiff(s string) (*diff, error) {
diff := newDiff()
changedFiles := splitByLinePrefix(s, "diff --git")
for i := 0; i < len(changedFiles); i++ {
changedSnippets := splitByLinePrefix(changedFiles[i], "@@")
newFilename, oldFilename := extractMetadata(changedSnippets[0])
diffFile := newDiffFile()
diffFile.newFilename = newFilename
diffFile.oldFilename = oldFilename
for j := 1; j < len(changedSnippets); j++ {
lines := strings.Split(changedSnippets[j], "\n")
addLineNumber, removeLineNumber := extractLineNumber(lines[0])
for k := 1; k < len(lines); k++ {
line := lines[k]
if len(line) > 0 && (line[0] == '+' || line[0] == '-') {
added := line[0] == '+'
var lineNumber uint
if added {
lineNumber = addLineNumber
addLineNumber++
} else {
lineNumber = removeLineNumber
removeLineNumber++
}
diffLine := diffLine{
content: line[1:],
added: added,
lineNumber: lineNumber,
}
diffFile.lines = append(diffFile.lines, &diffLine)
} else {
removeLineNumber++
addLineNumber++
}
}
}
diff.files = append(diff.files, diffFile)
}
return diff, nil
}
type diffFile struct {
newFilename string
oldFilename string
lines []*diffLine
}
func newDiffFile() *diffFile {
return &diffFile{
lines: make([]*diffLine, 0, 10),
}
}
type diffLine struct {
content string
lineNumber uint
added bool
}
func extractMetadata(s string) (string, string) {
newFilename := ""
oldFilename := ""
for _, l := range strings.Split(s, "\n") {
if strings.HasPrefix(l, "+++") {
i := strings.IndexRune(l, '/')
if filename := l[i+1:]; filename != "dev/null" {
newFilename = filename
}
} else if strings.HasPrefix(l, "---") {
i := strings.IndexRune(l, '/')
if filename := l[i+1:]; filename != "dev/null" {
oldFilename = filename
}
}
}
return newFilename, oldFilename
}
func extractLineNumber(s string) (uint, uint) {
var add uint = 0
var remove uint = 0
if strings.Contains(s, "+") && strings.Contains(s, ",") {
startIndex := strings.IndexRune(s, '+') + 1
endIndex := strings.IndexRune(s[startIndex:], ',') + startIndex
if endIndex < startIndex {
endIndex = strings.IndexRune(s[startIndex:], ' ') + startIndex
}
numberStr := s[startIndex:endIndex]
number, err := strconv.ParseUint(numberStr, 10, 0)
if err == nil {
add = uint(number)
}
}
if strings.Contains(s, "-") && strings.Contains(s, ",") {
startIndex := strings.IndexRune(s, '-') + 1
endIndex := strings.IndexRune(s[startIndex:], ',') + startIndex
if endIndex < startIndex {
endIndex = strings.IndexRune(s[startIndex:], ' ') + startIndex
}
numberStr := s[startIndex:endIndex]
number, err := strconv.ParseUint(numberStr, 10, 0)
if err == nil {
remove = uint(number)
}
}
return add, remove
}
func splitByLinePrefix(s, p string) []string {
split := make([]string, 0, 10)
lines := strings.Split(s, "\n")
sb := strings.Builder{}
for i, line := range lines {
if strings.HasPrefix(line, p) && i != 0 {
split = append(split, sb.String())
sb = strings.Builder{}
}
sb.WriteString(line + "\n")
}
return append(split, sb.String())
}