aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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
}