From e1b84ebc2a06d28cc1890e0eee69632363bb961f Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sun, 12 Apr 2026 21:37:49 +0700 Subject: text/diff: skip writing Unified if no difference found --- lib/text/diff/diff_test.go | 14 ++++++++++++++ lib/text/diff/unified.go | 3 +++ 2 files changed, 17 insertions(+) diff --git a/lib/text/diff/diff_test.go b/lib/text/diff/diff_test.go index 001e962c..d2009bfe 100644 --- a/lib/text/diff/diff_test.go +++ b/lib/text/diff/diff_test.go @@ -219,3 +219,17 @@ func TestFiles_LevelChunks(t *testing.T) { } } } + +func TestUnified_WithNewLine(t *testing.T) { + exp := "a\nb" + got := "a\nb\n" + + var sb strings.Builder + diff := Unified([]byte(exp), []byte(got)) + if diff.IsMatched { + return + } + diff.WriteUnified(&sb, 3) + + t.Fatalf(`sb: %s`, sb.String()) +} diff --git a/lib/text/diff/unified.go b/lib/text/diff/unified.go index 17eed2c2..1ed58c35 100644 --- a/lib/text/diff/unified.go +++ b/lib/text/diff/unified.go @@ -105,6 +105,9 @@ func findInsertIndex(unified []Line, next *Line, start int) (idx int) { // The ncontext must be 0 or positive, otherwise it will be set to 0 (no // context added). func (diff Data) WriteUnified(w io.Writer, ncontext int) (err error) { + if len(diff.Unified) == 0 { + return nil + } if ncontext < 0 { ncontext = 0 } -- cgit v1.3