-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff.go
62 lines (50 loc) · 1.2 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
package memlayout
import (
"strings"
"github.com/sergi/go-diff/diffmatchpatch"
)
// ChangedStruct contains a struct that was changed in a diff with its previous
// version from the base revision.
type ChangedStruct struct {
// Base is the struct version in the base revision. May not be present.
Base *Struct
// Head is the struct version in the head revision.
Head Struct
}
// ChangedStructs returns all the changed structs in the diff.
func ChangedStructs(
base, head []byte,
structs []Struct,
) []Struct {
lines := changedLines(base, head)
var result []Struct
for _, s := range structs {
for _, l := range lines {
if l >= s.Start && l <= s.End {
result = append(result, s)
break
}
}
}
return result
}
func changedLines(base, head []byte) []int {
dmp := diffmatchpatch.New()
diff := dmp.DiffMain(string(base), string(head), false)
var acc int
var result []int
for _, d := range diff {
lines := len(strings.Split(d.Text, "\n"))
if d.Type == diffmatchpatch.DiffDelete {
result = append(result, acc)
continue
}
if d.Type == diffmatchpatch.DiffInsert {
for i := 1; i <= lines; i++ {
result = append(result, acc+i)
}
}
acc += lines
}
return result
}