aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2026-04-12 21:37:49 +0700
committerShulhan <ms@kilabit.info>2026-04-12 22:33:31 +0700
commite1b84ebc2a06d28cc1890e0eee69632363bb961f (patch)
treeafde30105baa008245cfcb17213bea6838c729fc
parentc1a33bead8068e02c34fafb09c5519d5e5c18706 (diff)
downloadpakakeh.go-e1b84ebc2a06d28cc1890e0eee69632363bb961f.tar.xz
text/diff: skip writing Unified if no difference found
-rw-r--r--lib/text/diff/diff_test.go14
-rw-r--r--lib/text/diff/unified.go3
2 files changed, 17 insertions, 0 deletions
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
}