From 792bf5201ef2882fa1e1071b51f6d910b0bfceaf Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Tue, 21 Feb 2017 00:15:46 -0500 Subject: [PATCH] Fix diff edge case for trailing diffs --- pkg/diff/diff.go | 10 ++++++---- pkg/diff/diff_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/pkg/diff/diff.go b/pkg/diff/diff.go index 81f047db69..1e26bdde5d 100644 --- a/pkg/diff/diff.go +++ b/pkg/diff/diff.go @@ -158,11 +158,13 @@ func buildDiffLines(lString, rString string) []lineRecord { } } - if l != "" && r != "" { - if l == r { - results = append(results, lineRecord{Type: diffmatchpatch.DiffEqual, Line: l}) - } else { + if l != "" && l == r { + results = append(results, lineRecord{Type: diffmatchpatch.DiffEqual, Line: l}) + } else { + if l != "" { results = append(results, lineRecord{Type: diffmatchpatch.DiffDelete, Line: l}) + } + if r != "" { results = append(results, lineRecord{Type: diffmatchpatch.DiffInsert, Line: r}) } } diff --git a/pkg/diff/diff_test.go b/pkg/diff/diff_test.go index ec07ff92dd..8df57bd228 100644 --- a/pkg/diff/diff_test.go +++ b/pkg/diff/diff_test.go @@ -135,3 +135,39 @@ Line3` t.Fatalf("unexpected diff. expected=%v, actual=%v", expectedDiff, actual) } } + +func Test_Diff_4(t *testing.T) { + l := `A +B +C +D +E +F +` + r := `A +B +C +D +E +F` + expectedDiff := `... + D + E +- F ++ F +` + { + dmp := diffmatchpatch.New() + diffs := dmp.DiffMain(l, r, false) + + // We do need to cleanup, as otherwise we get some spurious changes on complex diffs + diffs = dmp.DiffCleanupSemantic(diffs) + + t.Logf("diffs %v", diffs) + } + + actual := FormatDiff(l, r) + if actual != expectedDiff { + t.Fatalf("unexpected diff. expected=%s, actual=%s", expectedDiff, actual) + } +}