Skip to content

Commit d68fc51

Browse files
adonovangopherbot
authored andcommitted
internal/diff: Merge
This CL addes a Merge operator to the diff package. It performs a simple three-way merge on two ordered lists of valid Edits, and reports a conflict if any edit could not be applied cleanly. I suspect there is considerable latitude in the implementation. This versions considers two identical insertions as non-conflicting, as is the case for redundant imports of the same package; however, it may be inappropriate for, say, identical statements that increment a counter, where the correct resolution is to keep both copies. + tests. Update golang/go#68765 Update golang/go#67049 Change-Id: I7d8bf5b0b2e601c15d3ee787499e6adc012f884b Reviewed-on: https://go-review.googlesource.com/c/tools/+/643196 Auto-Submit: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Commit-Queue: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 9f450b0 commit d68fc51

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

Diff for: internal/diff/merge.go

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2025 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package diff
6+
7+
import (
8+
"slices"
9+
)
10+
11+
// Merge merges two valid, ordered lists of edits.
12+
// It returns zero if there was a conflict.
13+
//
14+
// If corresponding edits in x and y are identical,
15+
// they are coalesced in the result.
16+
//
17+
// If x and y both provide different insertions at the same point,
18+
// the insertions from x will be first in the result.
19+
//
20+
// TODO(adonovan): this algorithm could be improved, for example by
21+
// working harder to coalesce non-identical edits that share a common
22+
// deletion or common prefix of insertion (see the tests).
23+
// Survey the academic literature for insights.
24+
func Merge(x, y []Edit) ([]Edit, bool) {
25+
// Make a defensive (premature) copy of the arrays.
26+
x = slices.Clone(x)
27+
y = slices.Clone(y)
28+
29+
var merged []Edit
30+
add := func(edit Edit) {
31+
merged = append(merged, edit)
32+
}
33+
var xi, yi int
34+
for xi < len(x) && yi < len(y) {
35+
px := &x[xi]
36+
py := &y[yi]
37+
38+
if *px == *py {
39+
// x and y are identical: coalesce.
40+
add(*px)
41+
xi++
42+
yi++
43+
44+
} else if px.End <= py.Start {
45+
// x is entirely before y,
46+
// or an insertion at start of y.
47+
add(*px)
48+
xi++
49+
50+
} else if py.End <= px.Start {
51+
// y is entirely before x,
52+
// or an insertion at start of x.
53+
add(*py)
54+
yi++
55+
56+
} else if px.Start < py.Start {
57+
// x is partly before y:
58+
// split it into a deletion and an edit.
59+
add(Edit{px.Start, py.Start, ""})
60+
px.Start = py.Start
61+
62+
} else if py.Start < px.Start {
63+
// y is partly before x:
64+
// split it into a deletion and an edit.
65+
add(Edit{py.Start, px.Start, ""})
66+
py.Start = px.Start
67+
68+
} else {
69+
// x and y are unequal non-insertions
70+
// at the same point: conflict.
71+
return nil, false
72+
}
73+
}
74+
for ; xi < len(x); xi++ {
75+
add(x[xi])
76+
}
77+
for ; yi < len(y); yi++ {
78+
add(y[yi])
79+
}
80+
return merged, true
81+
}

Diff for: internal/diff/merge_test.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2025 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package diff_test
6+
7+
import (
8+
"testing"
9+
10+
"golang.org/x/tools/internal/diff"
11+
)
12+
13+
func TestMerge(t *testing.T) {
14+
// For convenience, we test Merge using strings, not sequences
15+
// of edits, though this does put us at the mercy of the diff
16+
// algorithm.
17+
for _, test := range []struct {
18+
base, x, y string
19+
want string // "!" => conflict
20+
}{
21+
// independent insertions
22+
{"abcdef", "abXcdef", "abcdeYf", "abXcdeYf"},
23+
// independent deletions
24+
{"abcdef", "acdef", "abcdf", "acdf"},
25+
// colocated insertions (X first)
26+
{"abcdef", "abcXdef", "abcYdef", "abcXYdef"},
27+
// colocated identical insertions (coalesced)
28+
{"abcdef", "abcXdef", "abcXdef", "abcXdef"},
29+
// colocated insertions with common prefix (X first)
30+
// TODO(adonovan): would "abcXYdef" be better?
31+
// i.e. should we dissect the insertions?
32+
{"abcdef", "abcXdef", "abcXYdef", "abcXXYdef"},
33+
// mix of identical and independent insertions (X first)
34+
{"abcdef", "aIbcdXef", "aIbcdYef", "aIbcdXYef"},
35+
// independent deletions
36+
{"abcdef", "def", "abc", ""},
37+
// overlapping deletions: conflict
38+
{"abcdef", "adef", "abef", "!"},
39+
// overlapping deletions with distinct insertions, X first
40+
{"abcdef", "abXef", "abcYf", "!"},
41+
// overlapping deletions with distinct insertions, Y first
42+
{"abcdef", "abcXf", "abYef", "!"},
43+
// overlapping deletions with common insertions
44+
{"abcdef", "abXef", "abcXf", "!"},
45+
// trailing insertions in X (observe X bias)
46+
{"abcdef", "aXbXcXdXeXfX", "aYbcdef", "aXYbXcXdXeXfX"},
47+
// trailing insertions in Y (observe X bias)
48+
{"abcdef", "aXbcdef", "aYbYcYdYeYfY", "aXYbYcYdYeYfY"},
49+
} {
50+
dx := diff.Strings(test.base, test.x)
51+
dy := diff.Strings(test.base, test.y)
52+
got := "!" // conflict
53+
if dz, ok := diff.Merge(dx, dy); ok {
54+
var err error
55+
got, err = diff.Apply(test.base, dz)
56+
if err != nil {
57+
t.Errorf("Merge(%q, %q, %q) produced invalid edits %v: %v", test.base, test.x, test.y, dz, err)
58+
continue
59+
}
60+
}
61+
if test.want != got {
62+
t.Errorf("base=%q x=%q y=%q: got %q, want %q", test.base, test.x, test.y, got, test.want)
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)