├── go.mod ├── diff3_test.go ├── README.md ├── LICENSE ├── go.sum └── diff3.go /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/nasdf/diff3 2 | 3 | go 1.21 4 | 5 | require ( 6 | github.com/sergi/go-diff v1.1.0 7 | github.com/stretchr/testify v1.4.0 8 | ) 9 | 10 | require ( 11 | github.com/davecgh/go-spew v1.1.1 // indirect 12 | github.com/pmezard/go-difflib v1.0.0 // indirect 13 | gopkg.in/yaml.v2 v2.2.4 // indirect 14 | ) 15 | -------------------------------------------------------------------------------- /diff3_test.go: -------------------------------------------------------------------------------- 1 | package diff3 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | type TestCase struct { 10 | textO string 11 | textA string 12 | textB string 13 | expect string 14 | } 15 | 16 | var testCases = []TestCase{ 17 | { 18 | textO: "Aa\nBb\nCc\n", 19 | textA: "Aa\nBb\nCc\nDd\nEe\nFf\n", 20 | textB: "Aa\nBb\n", 21 | expect: "Aa\nBb\n<<<<<<<\nCc\nDd\nEe\nFf\n=======\n>>>>>>>\n", 22 | }, 23 | { 24 | textO: "celery\ngarlic\nonions\nsalmon\ntomatoes\nwine\n", 25 | textA: "celery\nsalmon\ntomatoes\ngarlic\nonions\nwine\n", 26 | textB: "celery\ngarlic\nsalmon\ntomatoes\nonions\nwine\n", 27 | expect: "celery\nsalmon\ntomatoes\ngarlic\n<<<<<<<\nonions\n=======\nsalmon\ntomatoes\nonions\n>>>>>>>\nwine\n", 28 | }, 29 | } 30 | 31 | func TestMerge(t *testing.T) { 32 | for _, tc := range testCases { 33 | result := Merge(tc.textO, tc.textA, tc.textB) 34 | assert.Equal(t, tc.expect, result) 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Diff3 2 | 3 | A diff3 text merge implementation in Go based on the awesome paper below. 4 | 5 | ["A Formal Investigation of Diff3" by Sanjeev Khanna, Keshav Kunal, and Benjamin C. Pierce](https://www.cis.upenn.edu/~bcpierce/papers/diff3-short.pdf) 6 | 7 | ## Usage 8 | 9 | ```go 10 | import "github.com/nasdf/diff3" 11 | textO := "original" 12 | textA := "changesA" 13 | textB := "changesB" 14 | merge := diff3.Merge(textO, textA, textB) 15 | ``` 16 | 17 | ### Customize seperators 18 | 19 | ```go 20 | diff3.Sep1 = "$$$$$$$" 21 | diff3.Sep2 = "@@@@@@@" 22 | diff3.Sep3 = "*******" 23 | ``` 24 | 25 | ### Customize DiffMatchPatch settings 26 | 27 | ```go 28 | diff3.DiffMatchPatch.DiffTimeout = time.Second 29 | diff3.DiffMatchPatch.DiffEditCost = 4 30 | diff3.DiffMatchPatch.MatchThreshold = 0.5 31 | diff3.DiffMatchPatch.MatchDistance = 1000 32 | diff3.DiffMatchPatch.PatchDeleteThreshold = 0.5 33 | diff3.DiffMatchPatch.PatchMargin = 4 34 | diff3.DiffMatchPatch.MatchMaxBits = 32 35 | ``` 36 | 37 | ## License 38 | 39 | MIT -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Keenan Nemetz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 3 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 4 | github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= 5 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 6 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 7 | github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= 8 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 9 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 10 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 11 | github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= 12 | github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= 13 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 14 | github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= 15 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 16 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 17 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= 18 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 19 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 20 | gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= 21 | gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 22 | -------------------------------------------------------------------------------- /diff3.go: -------------------------------------------------------------------------------- 1 | package diff3 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | 7 | "github.com/sergi/go-diff/diffmatchpatch" 8 | ) 9 | 10 | const ( 11 | // Sep1 signifies the start of a conflict. 12 | Sep1 = "<<<<<<<" 13 | // Sep2 signifies the middle of a conflict. 14 | Sep2 = "=======" 15 | // Sep3 signifies the end of a conflict. 16 | Sep3 = ">>>>>>>" 17 | ) 18 | 19 | // DiffMatchPatch contains the diff algorithm settings. 20 | var DiffMatchPatch = diffmatchpatch.New() 21 | 22 | // Merge implements the diff3 algorithm to merge two texts into a common base. 23 | func Merge(textO, textA, textB string) string { 24 | runesO, runesA, linesA := DiffMatchPatch.DiffLinesToRunes(textO, textA) 25 | _, runesB, linesB := DiffMatchPatch.DiffLinesToRunes(textO, textB) 26 | 27 | diffsA := DiffMatchPatch.DiffMainRunes(runesO, runesA, false) 28 | diffsB := DiffMatchPatch.DiffMainRunes(runesO, runesB, false) 29 | 30 | matchesA := matches(diffsA, runesA) 31 | matchesB := matches(diffsB, runesB) 32 | 33 | var result strings.Builder 34 | indexO, indexA, indexB := 0, 0, 0 35 | for { 36 | i := nextMismatch(indexO, indexA, indexB, runesA, runesB, matchesA, matchesB) 37 | 38 | o, a, b := 0, 0, 0 39 | if i == 1 { 40 | o, a, b = nextMatch(indexO, runesO, matchesA, matchesB) 41 | } else if i > 1 { 42 | o, a, b = indexO+i, indexA+i, indexB+i 43 | } 44 | 45 | if o == 0 || a == 0 || b == 0 { 46 | break 47 | } 48 | 49 | chunk(indexO, indexA, indexB, o-1, a-1, b-1, runesO, runesA, runesB, linesA, linesB, &result) 50 | indexO, indexA, indexB = o-1, a-1, b-1 51 | } 52 | 53 | chunk(indexO, indexA, indexB, len(runesO), len(runesA), len(runesB), runesO, runesA, runesB, linesA, linesB, &result) 54 | return result.String() 55 | } 56 | 57 | // matches returns a map of the non-crossing matches. 58 | func matches(diffs []diffmatchpatch.Diff, runes []rune) map[int]int { 59 | matches := make(map[int]int) 60 | for _, d := range diffs { 61 | if d.Type != diffmatchpatch.DiffEqual { 62 | continue 63 | } 64 | 65 | for _, r := range d.Text { 66 | matches[int(r)] = indexOf(runes, r) + 1 67 | } 68 | } 69 | return matches 70 | } 71 | 72 | // nextMismatch searches for the next index where a or b is not equal to o. 73 | func nextMismatch(indexO, indexA, indexB int, runesA, runesB []rune, matchesA, matchesB map[int]int) int { 74 | stop := min(len(runesA), len(runesB)) 75 | for i := 1; i <= stop; i++ { 76 | a, okA := matchesA[indexO+i] 77 | b, okB := matchesB[indexO+i] 78 | 79 | if !okA || a != indexA+i || !okB || b != indexB+i { 80 | return i 81 | } 82 | } 83 | return stop 84 | } 85 | 86 | // nextMatch searches for the next index where a and b are equal to o. 87 | func nextMatch(indexO int, runesO []rune, matchesA, matchesB map[int]int) (int, int, int) { 88 | for o := indexO + 1; o <= len(runesO); o++ { 89 | a, okA := matchesA[o] 90 | b, okB := matchesB[o] 91 | 92 | if okA && okB { 93 | return o, a, b 94 | } 95 | } 96 | return 0, 0, 0 97 | } 98 | 99 | // chunk merges the lines from o, a, and b into a single text. 100 | func chunk(indexO, indexA, indexB, o, a, b int, runesO, runesA, runesB []rune, linesA, linesB []string, result *strings.Builder) { 101 | chunkO := buildChunk(linesA, runesO[indexO:o]) 102 | chunkA := buildChunk(linesA, runesA[indexA:a]) 103 | chunkB := buildChunk(linesB, runesB[indexB:b]) 104 | 105 | switch { 106 | case chunkA == chunkB: 107 | fmt.Fprint(result, chunkO) 108 | case chunkO == chunkA: 109 | fmt.Fprint(result, chunkB) 110 | case chunkO == chunkB: 111 | fmt.Fprint(result, chunkA) 112 | default: 113 | fmt.Fprintf(result, "%s\n%s%s\n%s%s\n", Sep1, chunkA, Sep2, chunkB, Sep3) 114 | } 115 | } 116 | 117 | // indexOf returns the index of the first occurance of the given value. 118 | func indexOf(runes []rune, value rune) int { 119 | for i, r := range runes { 120 | if r == value { 121 | return i 122 | } 123 | } 124 | return -1 125 | } 126 | 127 | // buildChunk assembles the lines of the chunk into a string. 128 | func buildChunk(lines []string, runes []rune) string { 129 | var chunk strings.Builder 130 | for _, r := range runes { 131 | fmt.Fprint(&chunk, lines[int(r)]) 132 | } 133 | return chunk.String() 134 | } 135 | --------------------------------------------------------------------------------