-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.go
More file actions
348 lines (293 loc) · 9.68 KB
/
Copy pathdiff.go
File metadata and controls
348 lines (293 loc) · 9.68 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright (c) 2026 Z5Labs and Contributors
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package dfcad
import (
"bytes"
"fmt"
"slices"
"strings"
)
// diffContext is how many unchanged lines are written either side of a change.
// Three is what every unified diff uses, which is what lets the output be read
// by anything that already reads one.
const diffContext = 3
// maxDiffDepth bounds the edit script search.
//
// Finding the shortest edit script costs time and memory in the number of
// differing lines, and a file whose canonical form shares almost nothing with
// what was written — a file whose forms have all been reordered — is the case
// where that number is largest and the answer is least useful. Past the bound
// the search gives up and the differing region is written as one replacement,
// which is a correct diff and a less minimal one.
const maxDiffDepth = 1000
// unified is the unified diff from old to new, labelled with path.
//
// The output is the format `patch` and every diff viewer already read: two
// header lines naming the file before and after, and then one hunk per run of
// changed lines with [diffContext] unchanged lines either side of it. Two
// inputs that are equal produce no output at all rather than an empty diff, so
// a caller can test the result rather than parse it.
//
// A file that does not end in a line feed is a different file from the same
// text that does, and the diff says so: the line is written with the marker a
// unified diff uses for it, which is what stops fmt from reporting a change it
// then appears not to describe.
func unified(path string, old, new []byte) string {
script := editScript(diffLines(old), diffLines(new))
hunks := hunks(script, diffContext)
if len(hunks) == 0 {
return ""
}
var out strings.Builder
// The two names are the file as it is and the file as it would be. They
// differ so that the diff says which side is which, and neither is a
// directory prefix, so that an absolute path stays one.
fmt.Fprintf(&out, "--- %s.orig\n+++ %s\n", path, path)
for _, h := range hunks {
h.write(&out)
}
return out.String()
}
// diffLines splits src into lines, each keeping its terminator.
//
// Keeping the line feed on the line is what lets a file that does not end in
// one differ from the same text that does. Splitting on the terminator and
// dropping it makes those two files equal, and a diff reporting no change on a
// file about to be rewritten is worse than no diff at all.
func diffLines(src []byte) []string {
var out []string
for len(src) > 0 {
i := bytes.IndexByte(src, '\n')
if i < 0 {
out = append(out, string(src))
break
}
out = append(out, string(src[:i+1]))
src = src[i+1:]
}
return out
}
// edit is one line of an edit script: a line kept, removed or added.
type edit struct {
// op is ' ' for a line both files hold, '-' for one only the first holds,
// and '+' for one only the second does. It is the character a unified diff
// writes the line with.
op byte
// text is the line, terminator included where it has one.
text string
}
// editScript is the sequence of kept, removed and added lines that turns a
// into b.
//
// Removals come before the additions they sit beside, which is the order a
// unified diff is read in and the order every other implementation writes.
func editScript(a, b []string) []edit {
var out []edit
ai, bi := 0, 0
for _, match := range common(a, b) {
for ; ai < match[0]; ai++ {
out = append(out, edit{op: '-', text: a[ai]})
}
for ; bi < match[1]; bi++ {
out = append(out, edit{op: '+', text: b[bi]})
}
out = append(out, edit{op: ' ', text: a[match[0]]})
ai, bi = match[0]+1, match[1]+1
}
for ; ai < len(a); ai++ {
out = append(out, edit{op: '-', text: a[ai]})
}
for ; bi < len(b); bi++ {
out = append(out, edit{op: '+', text: b[bi]})
}
return out
}
// common is the lines a and b share, as pairs of indices into each, in order.
//
// The ends the two files share are matched directly and only what is left is
// searched. A formatting change usually touches part of a file, so trimming
// first is what keeps the search over the differing region rather than over
// the whole file.
func common(a, b []string) [][2]int {
var out [][2]int
prefix := 0
for prefix < len(a) && prefix < len(b) && a[prefix] == b[prefix] {
out = append(out, [2]int{prefix, prefix})
prefix++
}
suffix := 0
for suffix < len(a)-prefix && suffix < len(b)-prefix && a[len(a)-1-suffix] == b[len(b)-1-suffix] {
suffix++
}
for _, match := range myers(a[prefix:len(a)-suffix], b[prefix:len(b)-suffix]) {
out = append(out, [2]int{match[0] + prefix, match[1] + prefix})
}
for i := suffix; i > 0; i-- {
out = append(out, [2]int{len(a) - i, len(b) - i})
}
return out
}
// myers is the longest common subsequence of a and b, as pairs of indices into
// each, by Myers' shortest-edit-script algorithm.
//
// It returns nothing when the two share no line and when the search reaches
// [maxDiffDepth] without finishing. Both mean the same thing to the caller —
// nothing here is common, so the whole of a is replaced by the whole of b —
// which is why the bound needs no separate answer.
func myers(a, b []string) [][2]int {
n, m := len(a), len(b)
if n == 0 || m == 0 {
return nil
}
// The furthest-reaching path on diagonal k after d edits is v[offset+k].
// A path after d edits lies on a diagonal between -d and d, so bounding d
// bounds the array as well as the number of them kept.
depth := min(n+m, maxDiffDepth)
offset := depth
v := make([]int, 2*depth+1)
// trace[d] is v as it was before the d-th edit, which is what the path is
// reconstructed from once an end is reached.
trace := make([][]int, 0, depth+1)
for d := 0; d <= depth; d++ {
trace = append(trace, slices.Clone(v))
for k := -d; k <= d; k += 2 {
var x int
switch {
case k == -d, k != d && v[offset+k-1] < v[offset+k+1]:
x = v[offset+k+1]
default:
x = v[offset+k-1] + 1
}
y := x - k
for x < n && y < m && a[x] == b[y] {
x, y = x+1, y+1
}
v[offset+k] = x
if x >= n && y >= m {
return backtrack(trace, offset, n, m)
}
}
}
return nil
}
// backtrack walks the recorded search back from the end of both files,
// collecting the pairs of lines the path matched.
func backtrack(trace [][]int, offset, n, m int) [][2]int {
var out [][2]int
x, y := n, m
for d := len(trace) - 1; d > 0; d-- {
v := trace[d]
k := x - y
// Which diagonal the path arrived from is the same choice the search
// made on the way out, read off the state it made it in.
var previous int
switch {
case k == -d, k != d && v[offset+k-1] < v[offset+k+1]:
previous = k + 1
default:
previous = k - 1
}
px := v[offset+previous]
py := px - previous
for x > px && y > py {
x, y = x-1, y-1
out = append(out, [2]int{x, y})
}
x, y = px, py
}
// The path before any edit is the run of equal lines the two files open
// with, which the trimming above has already taken when there was one.
for x > 0 && y > 0 {
x, y = x-1, y-1
out = append(out, [2]int{x, y})
}
slices.Reverse(out)
return out
}
// hunk is one run of changed lines with the unchanged lines around it, and
// where in each file it begins.
type hunk struct {
// a and b are the 0-based line numbers the hunk starts at in each file.
a, b int
// an and bn are how many lines of each it covers.
an, bn int
// edits are the lines themselves, in order.
edits []edit
}
// hunks groups an edit script into the hunks a unified diff writes: every
// changed line, with context unchanged lines either side, and two changes
// close enough to share their context written as one.
func hunks(script []edit, context int) []hunk {
var changes []int
for i, e := range script {
if e.op != ' ' {
changes = append(changes, i)
}
}
if len(changes) == 0 {
return nil
}
// lines[i] is how many lines of each file the first i edits account for,
// which is where a hunk starting at i begins and how long it runs.
as, bs := make([]int, len(script)+1), make([]int, len(script)+1)
for i, e := range script {
as[i+1], bs[i+1] = as[i], bs[i]
if e.op != '+' {
as[i+1]++
}
if e.op != '-' {
bs[i+1]++
}
}
var out []hunk
for start := 0; start < len(changes); {
// Two changes separated by no more than twice the context share it,
// and writing them as two hunks would write those lines twice.
end := start
for end+1 < len(changes) && changes[end+1]-changes[end] <= 2*context+1 {
end++
}
from := max(changes[start]-context, 0)
to := min(changes[end]+context, len(script)-1)
out = append(out, hunk{
a: as[from],
b: bs[from],
an: as[to+1] - as[from],
bn: bs[to+1] - bs[from],
edits: script[from : to+1],
})
start = end + 1
}
return out
}
// write appends the hunk's header and its lines.
func (h hunk) write(out *strings.Builder) {
fmt.Fprintf(out, "@@ -%s +%s @@\n", lineRange(h.a, h.an), lineRange(h.b, h.bn))
for _, e := range h.edits {
out.WriteByte(e.op)
out.WriteString(e.text)
// A line with no terminator is the last line of its file, and the
// marker is how a unified diff says so.
if !strings.HasSuffix(e.text, "\n") {
out.WriteString("\n\\ No newline at end of file\n")
}
}
}
// lineRange is how a hunk header spells the lines it covers: the 1-based
// number of the first, and the count where it is not one.
//
// A range covering nothing is written at the line it would have started at,
// which is how a unified diff spells an insertion into a file that has no line
// there.
func lineRange(start, count int) string {
switch count {
case 0:
return fmt.Sprintf("%d,0", start)
case 1:
return fmt.Sprintf("%d", start+1)
default:
return fmt.Sprintf("%d,%d", start+1, count)
}
}