Repository navigation
Expand file tree
/
Copy pathdiff.go
More file actions
233 lines (216 loc) · 5.52 KB
/
Copy pathdiff.go
File metadata and controls
233 lines (216 loc) · 5.52 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
package codemask
import (
"fmt"
"regexp"
"strconv"
"strings"
)
const devNull = "/dev/null"
type fileDiff struct {
oldPath, newPath string
binary bool
hunks []rawHunk
}
type rawHunk struct {
oldStart, oldCount int
newStart, newCount int
lines []diffLine
}
type diffLine struct {
op byte // ' ', '-' or '+'
text string
}
func (fd *fileDiff) path() string {
if fd.newPath == "" || fd.newPath == devNull {
return fd.oldPath
}
return fd.newPath
}
func (fd *fileDiff) status() string {
switch {
case fd.oldPath == devNull:
return "added"
case fd.newPath == devNull:
return "deleted"
case fd.oldPath != "" && fd.newPath != "" && fd.oldPath != fd.newPath:
return "renamed"
default:
return "modified"
}
}
var hunkHeader = regexp.MustCompile(`^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@`)
// parseDiff reads a unified diff, with or without git's extended headers.
// Hunk bodies are read by their declared line counts, never by sniffing, since a
// removed SQL comment ("-- note") looks exactly like a file header ("--- a/x").
func parseDiff(data []byte) ([]*fileDiff, error) {
lines := strings.Split(strings.ReplaceAll(string(data), "\r\n", "\n"), "\n")
var files []*fileDiff
var cur *fileDiff
start := func() *fileDiff {
cur = &fileDiff{}
files = append(files, cur)
return cur
}
for i := 0; i < len(lines); i++ {
l := lines[i]
switch {
case strings.HasPrefix(l, "diff --git "):
start()
cur.oldPath, cur.newPath = gitHeaderPaths(strings.TrimPrefix(l, "diff --git "))
case strings.HasPrefix(l, "--- ") && i+1 < len(lines) && strings.HasPrefix(lines[i+1], "+++ "):
if cur == nil || len(cur.hunks) > 0 {
start()
}
cur.oldPath = headerPath(strings.TrimPrefix(l, "--- "), "a/")
cur.newPath = headerPath(strings.TrimPrefix(lines[i+1], "+++ "), "b/")
i++
case strings.HasPrefix(l, "@@ "):
if cur == nil {
return nil, fmt.Errorf("codemask: line %d: hunk before any file header", i+1)
}
h, next, err := readHunk(lines, i)
if err != nil {
return nil, err
}
cur.hunks = append(cur.hunks, h)
i = next - 1
case cur == nil:
case strings.HasPrefix(l, "Binary files ") || l == "GIT binary patch":
cur.binary = true
case strings.HasPrefix(l, "new file mode"):
cur.oldPath = devNull
case strings.HasPrefix(l, "deleted file mode"):
cur.newPath = devNull
case strings.HasPrefix(l, "rename from "):
cur.oldPath = strings.TrimPrefix(l, "rename from ")
case strings.HasPrefix(l, "rename to "):
cur.newPath = strings.TrimPrefix(l, "rename to ")
default:
}
}
return files, nil
}
// readHunk reads the hunk whose header is at lines[i] and returns the index of
// the line after it.
func readHunk(lines []string, i int) (rawHunk, int, error) {
m := hunkHeader.FindStringSubmatch(lines[i])
if m == nil {
return rawHunk{}, 0, fmt.Errorf("codemask: line %d: bad hunk header %q", i+1, lines[i])
}
h := rawHunk{
oldStart: atoi(m[1]), oldCount: countOr1(m[2]),
newStart: atoi(m[3]), newCount: countOr1(m[4]),
}
oldLeft, newLeft := h.oldCount, h.newCount
j := i + 1
for ; (oldLeft > 0 || newLeft > 0) && j < len(lines); j++ {
l := lines[j]
if strings.HasPrefix(l, `\`) {
continue
}
op, text := byte(' '), ""
if l != "" {
op, text = l[0], l[1:]
}
switch op {
case ' ':
oldLeft--
newLeft--
case '-':
oldLeft--
case '+':
newLeft--
default:
return rawHunk{}, 0, fmt.Errorf("codemask: line %d: unexpected %q inside a hunk", j+1, l)
}
h.lines = append(h.lines, diffLine{op: op, text: strings.TrimSuffix(text, "\r")})
}
if oldLeft > 0 || newLeft > 0 {
return rawHunk{}, 0, fmt.Errorf("codemask: line %d: hunk ends early", j)
}
for j < len(lines) && strings.HasPrefix(lines[j], `\`) {
j++
}
return h, j, nil
}
func headerPath(s, prefix string) string {
if tab := strings.IndexByte(s, '\t'); tab >= 0 {
s = s[:tab]
}
if s == devNull {
return s
}
return strings.TrimPrefix(s, prefix)
}
// gitHeaderPaths splits "a/x b/x". The paths may contain spaces, so it splits
// at the last " b/".
func gitHeaderPaths(s string) (string, string) {
idx := strings.LastIndex(s, " b/")
if idx < 0 || !strings.HasPrefix(s, "a/") {
return "", ""
}
return s[2:idx], s[idx+3:]
}
func atoi(s string) int {
n, _ := strconv.Atoi(s)
return n
}
func countOr1(s string) int {
if s == "" {
return 1
}
return atoi(s)
}
// hunkSpans keeps the changed lines and ctx lines either side of them, one run
// of lines per hunk, for makeHunk to give a fresh header. The input's own
// section headings are dropped: they are source lines (a function signature, or
// a variable holding a string) that were never lexed.
func hunkSpans(lines []outLine, ctx int) [][]outLine {
keep := make([]bool, len(lines))
for i, l := range lines {
if l.op == ' ' {
continue
}
for j := max(0, i-ctx); j <= min(len(lines)-1, i+ctx); j++ {
keep[j] = true
}
}
var spans [][]outLine
for i := 0; i < len(lines); {
if !keep[i] {
i++
continue
}
j := i
for j < len(lines) && keep[j] {
j++
}
spans = append(spans, lines[i:j])
i = j
}
return spans
}
func makeHunk(lines []outLine) Hunk {
oldCount, newCount := 0, 0
out := make([]string, len(lines))
for i, l := range lines {
if l.op != '+' {
oldCount++
}
if l.op != '-' {
newCount++
}
out[i] = string(l.op) + l.text
}
oldStart, newStart := lines[0].oldPos, lines[0].newPos
if oldCount == 0 {
oldStart--
}
if newCount == 0 {
newStart--
}
return Hunk{
Header: fmt.Sprintf("@@ -%d,%d +%d,%d @@", oldStart, oldCount, newStart, newCount),
Lines: out,
}
}