-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrange_diff.go
367 lines (332 loc) · 7.47 KB
/
range_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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package git
import (
"fmt"
"math"
"sort"
"strings"
"github.com/bluekeyes/go-gitdiff/gitdiff"
ha "github.com/oddg/hungarian-algorithm"
"github.com/sergi/go-diff/diffmatchpatch"
)
var COST_MAX = 65536
var RANGE_DIFF_CREATION_FACTOR_DEFAULT = 60
type PatchRange struct {
*Patch
Matching int
Diff string
DiffSize int
Shown bool
}
func NewPatchRange(patch *Patch) *PatchRange {
diff := patch.CalcDiff()
return &PatchRange{
Patch: patch,
Matching: -1,
Diff: diff,
DiffSize: len(diff),
Shown: false,
}
}
type RangeDiffOutput struct {
Header *RangeDiffHeader
Order int
Files []*RangeDiffFile
Type string
}
func output(a []*PatchRange, b []*PatchRange) []*RangeDiffOutput {
outputs := []*RangeDiffOutput{}
for i, patchA := range a {
if patchA.Matching == -1 {
hdr := NewRangeDiffHeader(patchA, nil, i+1, -1)
outputs = append(
outputs,
&RangeDiffOutput{
Header: hdr,
Type: "rm",
Order: i + 1,
},
)
}
}
for j, patchB := range b {
if patchB.Matching == -1 {
hdr := NewRangeDiffHeader(nil, patchB, -1, j+1)
outputs = append(
outputs,
&RangeDiffOutput{
Header: hdr,
Type: "add",
Order: j + 1,
},
)
continue
}
patchA := a[patchB.Matching]
if patchB.ContentSha == patchA.ContentSha {
hdr := NewRangeDiffHeader(patchA, patchB, patchB.Matching+1, patchA.Matching+1)
outputs = append(
outputs,
&RangeDiffOutput{
Header: hdr,
Type: "equal",
Order: patchA.Matching + 1,
},
)
} else {
hdr := NewRangeDiffHeader(patchA, patchB, patchB.Matching+1, patchA.Matching+1)
diff := outputDiff(patchA, patchB)
outputs = append(
outputs,
&RangeDiffOutput{
Order: patchA.Matching + 1,
Header: hdr,
Files: diff,
Type: "diff",
},
)
}
}
sort.Slice(outputs, func(i, j int) bool {
return outputs[i].Order < outputs[j].Order
})
return outputs
}
type RangeDiffDiff struct {
OuterType string
InnerType string
Text string
}
func toRangeDiffDiff(diff []diffmatchpatch.Diff) []RangeDiffDiff {
result := []RangeDiffDiff{}
for _, line := range diff {
outer := "equal"
switch line.Type {
case diffmatchpatch.DiffInsert:
outer = "insert"
case diffmatchpatch.DiffDelete:
outer = "delete"
}
fmtLine := strings.Split(line.Text, "\n")
for idx, ln := range fmtLine {
text := ln
if idx < len(fmtLine)-1 {
text = ln + "\n"
}
st := RangeDiffDiff{
Text: text,
OuterType: outer,
InnerType: "equal",
}
if strings.HasPrefix(text, "+") {
st.InnerType = "insert"
} else if strings.HasPrefix(text, "-") {
st.InnerType = "delete"
}
result = append(result, st)
}
}
return result
}
func DoDiff(src, dst string) []RangeDiffDiff {
dmp := diffmatchpatch.New()
wSrc, wDst, warray := dmp.DiffLinesToChars(src, dst)
diffs := dmp.DiffMain(wSrc, wDst, false)
diffs = dmp.DiffCharsToLines(diffs, warray)
return toRangeDiffDiff(diffs)
}
type RangeDiffFile struct {
OldFile *gitdiff.File
NewFile *gitdiff.File
Diff []RangeDiffDiff
}
func outputDiff(patchA, patchB *PatchRange) []*RangeDiffFile {
diffs := []*RangeDiffFile{}
for _, fileA := range patchA.Files {
for _, fileB := range patchB.Files {
if fileA.NewName == fileB.NewName {
strA := ""
for _, frag := range fileA.TextFragments {
for _, line := range frag.Lines {
strA += line.String()
}
}
strB := ""
for _, frag := range fileB.TextFragments {
for _, line := range frag.Lines {
strB += line.String()
}
}
curDiff := DoDiff(strA, strB)
hasDiff := false
for _, dd := range curDiff {
if dd.OuterType != "equal" {
hasDiff = true
break
}
}
if !hasDiff {
continue
}
// curDiff := DoDiff(fileA.String(), fileB.String())
fp := &RangeDiffFile{
OldFile: fileA,
NewFile: fileB,
Diff: curDiff,
}
diffs = append(diffs, fp)
}
}
}
return diffs
}
// RangeDiffHeader is a header combining old and new change pairs.
type RangeDiffHeader struct {
OldIdx int
OldSha string
NewIdx int
NewSha string
Title string
ContentEqual bool
}
func NewRangeDiffHeader(a *PatchRange, b *PatchRange, aIndex, bIndex int) *RangeDiffHeader {
hdr := &RangeDiffHeader{}
if a == nil {
hdr.NewIdx = bIndex
hdr.NewSha = b.CommitSha
hdr.Title = b.Title
return hdr
}
if b == nil {
hdr.OldIdx = aIndex
hdr.OldSha = a.CommitSha
hdr.Title = a.Title
return hdr
}
hdr.OldIdx = aIndex
hdr.NewIdx = bIndex
hdr.OldSha = a.CommitSha
hdr.NewSha = b.CommitSha
if a.ContentSha == b.ContentSha {
hdr.Title = a.Title
hdr.ContentEqual = true
} else {
hdr.Title = b.Title
}
return hdr
}
func (hdr *RangeDiffHeader) String() string {
if hdr.OldIdx == 0 {
return fmt.Sprintf("-: ------- > %d: %s %s\n", hdr.NewIdx, truncateSha(hdr.NewSha), hdr.Title)
}
if hdr.NewIdx == 0 {
return fmt.Sprintf("%d: %s < -: ------- %s\n", hdr.OldIdx, truncateSha(hdr.OldSha), hdr.Title)
}
if hdr.ContentEqual {
return fmt.Sprintf(
"%d: %s = %d: %s %s\n",
hdr.OldIdx, truncateSha(hdr.OldSha),
hdr.NewIdx, truncateSha(hdr.NewSha),
hdr.Title,
)
}
return fmt.Sprintf(
"%d: %s ! %d: %s %s",
hdr.OldIdx, truncateSha(hdr.OldSha),
hdr.NewIdx, truncateSha(hdr.NewSha),
hdr.Title,
)
}
func RangeDiff(a []*Patch, b []*Patch) []*RangeDiffOutput {
aPatches := []*PatchRange{}
for _, patch := range a {
aPatches = append(aPatches, NewPatchRange(patch))
}
bPatches := []*PatchRange{}
for _, patch := range b {
bPatches = append(bPatches, NewPatchRange(patch))
}
findExactMatches(aPatches, bPatches)
getCorrespondences(aPatches, bPatches, RANGE_DIFF_CREATION_FACTOR_DEFAULT)
return output(aPatches, bPatches)
}
func RangeDiffToStr(diffs []*RangeDiffOutput) string {
output := ""
for _, diff := range diffs {
output += diff.Header.String()
for _, f := range diff.Files {
output += fmt.Sprintf("\n@@ %s\n", f.NewFile.NewName)
for _, d := range f.Diff {
switch d.OuterType {
case "equal":
output += d.Text
case "insert":
output += d.Text
case "delete":
output += d.Text
}
}
}
}
return output
}
func findExactMatches(a []*PatchRange, b []*PatchRange) {
for i, patchA := range a {
for j, patchB := range b {
if patchA.ContentSha == patchB.ContentSha {
patchA.Matching = j
patchB.Matching = i
}
}
}
}
func createMatrix(rows, cols int) [][]int {
mat := make([][]int, rows)
for i := range mat {
mat[i] = make([]int, cols)
}
return mat
}
func diffsize(a *PatchRange, b *PatchRange) int {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(a.Diff, b.Diff, false)
return len(diffs)
}
func getCorrespondences(a []*PatchRange, b []*PatchRange, creationFactor int) {
n := len(a) + len(b)
cost := createMatrix(n, n)
for i, patchA := range a {
var c int
for j, patchB := range b {
if patchA.Matching == j {
c = 0
} else if patchA.Matching == -1 && patchB.Matching == -1 {
c = diffsize(patchA, patchB)
} else {
c = COST_MAX
}
cost[i][j] = c
}
}
for j, patchB := range b {
creationCost := (patchB.DiffSize * creationFactor) / 100
if patchB.Matching >= 0 {
creationCost = math.MaxInt32
}
for i := len(a); i < n; i++ {
cost[i][j] = creationCost
}
}
for i := len(a); i < n; i++ {
for j := len(b); j < n; j++ {
cost[i][j] = 0
}
}
assignment, _ := ha.Solve(cost)
for i := range a {
j := assignment[i]
if j >= 0 && j < len(b) {
a[i].Matching = j
b[j].Matching = i
}
}
}