Skip to content

Commit 80dc031

Browse files
aymanbagabasfindleyr
authored andcommitted
internal/diff: unified: match diff delete for empty file
GNU diff -u prints +0,0 for the special case of deleting into an empty file. This also adds a test for this case. What version of Go are you using (go version)? `go version go1.20.3 darwin/amd64` What operating system and processor architecture are you using? MacOS/amd64 What did you do? ```sh echo -n "test" >src touch dst diff -u src dst ``` What did you expect to see? ``` --- src 2023-04-20 18:04:23 +++ dst 2023-04-20 18:04:23 @@ -1 +0,0 @@ -test \ No newline at end of file ``` What did you see instead? internal/diff prints the wrong unified diff, it should be `@@ -1 +0,0 @@` instead of `@@ -1 +1 @@`. ``` --- src +++ dst @@ -1 +1 @@ -test \ No newline at end of file ``` Change-Id: If23b609ada4a45249f1c382ebe8f821dde7aadc0 GitHub-Last-Rev: d08a1ba GitHub-Pull-Request: #436 Reviewed-on: https://go-review.googlesource.com/c/tools/+/487175 Reviewed-by: Alan Donovan <[email protected]> gopls-CI: kokoro <[email protected]> Reviewed-by: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Findley <[email protected]>
1 parent 165099b commit 80dc031

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

internal/diff/difftest/difftest.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ var TestCases = []struct {
119119
\ No newline at end of file
120120
`[1:],
121121
Edits: []diff.Edit{{Start: 0, End: 1, New: "B"}},
122+
}, {
123+
Name: "delete_empty",
124+
In: "meow",
125+
Out: "", // GNU diff -u special case: +0,0
126+
Unified: UnifiedPrefix + `
127+
@@ -1 +0,0 @@
128+
-meow
129+
\ No newline at end of file
130+
`[1:],
131+
Edits: []diff.Edit{{Start: 0, End: 4, New: ""}},
132+
LineEdits: []diff.Edit{{Start: 0, End: 4, New: ""}},
122133
}, {
123134
Name: "append_empty",
124135
In: "", // GNU diff -u special case: -0,0

internal/diff/unified.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ func (u unified) String() string {
226226
}
227227
if toCount > 1 {
228228
fmt.Fprintf(b, " +%d,%d", hunk.ToLine, toCount)
229+
} else if hunk.ToLine == 1 && toCount == 0 {
230+
// Match odd GNU diff -u behavior adding to empty file.
231+
fmt.Fprintf(b, " +0,0")
229232
} else {
230233
fmt.Fprintf(b, " +%d", hunk.ToLine)
231234
}

0 commit comments

Comments
 (0)