go-diff icon indicating copy to clipboard operation
go-diff copied to clipboard

[BUG] v1.2.0 seems to produce incorrect diff

Open KevinWang15 opened this issue 3 years ago • 10 comments

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}]

KevinWang15 avatar Jul 06 '21 07:07 KevinWang15

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

eastlondoner avatar Aug 02 '21 13:08 eastlondoner

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.

peter-evans avatar Sep 13 '21 02:09 peter-evans

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

KevinWang15 avatar Sep 16 '21 05:09 KevinWang15

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.

schrej avatar Nov 16 '21 11:11 schrej

Any updates here?

aymanbagabas avatar Jan 19 '22 16:01 aymanbagabas

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

huguesalary avatar May 05 '22 22:05 huguesalary

go mod edit -exclude=github.com/sergi/[email protected]

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

uhthomas avatar May 29 '22 15:05 uhthomas

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

kdarkhan avatar Jan 23 '23 23:01 kdarkhan

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

AriehSchneier avatar Jul 14 '23 02:07 AriehSchneier

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

kdarkhan avatar Aug 03 '23 17:08 kdarkhan