sergi / go-diff

Diff, match and patch text in Go
MIT License
1.81k stars 207 forks source link

[BUG] v1.2.0 seems to produce incorrect diff #123

Open KevinWang15 opened 3 years ago

KevinWang15 commented 3 years ago

This testcase:


func Test(t *testing.T) {
    src := "a\na: a\na:\n  a:\n    a\n    a: \"a\"\n    a: a\n    a: \"1\"\n    a: 1\n    -"
    dst := "abcde: xxxxx"

    dmp := New()
    wSrc, wDst, warray := dmp.DiffLinesToRunes(src, dst)
    diffs := dmp.DiffMainRunes(wSrc, wDst, false)
    diffs = dmp.DiffCharsToLines(diffs, warray)

    fmt.Printf("SRC:\n%s\n\nDST:\n%s\n\nDiff:\n%v", src, dst, diffs)
}

Will produce

SRC:
a
a: a
a:
  a:
    a
    a: "a"
    a: a
    a: "1"
    a: 1
    -

DST:
abcde: xxxxx

Diff:
[{Equal a
} {Delete a: a
a:
  a:
    a
    a: "a"
    a: a
    a: "1"
    a: 1
} {Equal a
} {Delete }]

In v1.2.0, which is incorrect.

In v1.1.0, it produces the correct diff of

SRC:
a
a: a
a:
  a:
    a
    a: "a"
    a: a
    a: "1"
    a: 1
    -

DST:
abcde: xxxxx

Diff:
[{Delete a
a: a
a:
  a:
    a
    a: "a"
    a: a
    a: "1"
    a: 1
    -} {Insert abcde: xxxxx}]
eastlondoner commented 3 years ago

I'm seeing this problem as well. I had to revert to 1.1.0

peter-evans commented 3 years ago

I also had this problem. All the examples I could find were producing garbage results until I found this issue and realised that 1.2.0 was the culprit. Switched to version 1.1.0 and all seems fine.

KevinWang15 commented 2 years ago

@sergi Please take a look when possible, as many other libraries are depending on this one, thanks!

schrej commented 2 years ago

This was broken with https://github.com/sergi/go-diff/commit/db1b095f5e7c905e196ff6bfd56189a41aa76309 by passing a array to diffLinesToStringsMunge instead of a map.

Calling that function with multiple files, but passing the same array, now leads to duplicated lines for each file in the array, instead of re-using existing lines.

aymanbagabas commented 2 years ago

Any updates here?

huguesalary commented 2 years ago

I ran into this issue as well. Reverting to 1.1.0 fixes it.

uhthomas commented 2 years ago
go mod edit -exclude=github.com/sergi/go-diff@v1.2.0

may be helpful to prevent an accidental upgrade with go mod tidy or go get -u ./....

kdarkhan commented 1 year ago

I created https://github.com/sergi/go-diff/pull/136 which should fix the issue.

AriehSchneier commented 1 year ago

@sergi Any chance of merging #120 or #136 to fix this? This library is currently giving incorrect results.

kdarkhan commented 1 year ago

https://github.com/sergi/go-diff/pull/136 was merged yesterday, so this should be resolved now.