Skip to content

Commit

Permalink
fix: match diff delete for empty file
Browse files Browse the repository at this point in the history
GNU diff -u prints +0,0 for the special case of deleting into an empty file.

Co-authored-by: aymanbagabas <[email protected]>
  • Loading branch information
github-actions[bot] and aymanbagabas authored Apr 29, 2023
1 parent 7274190 commit 9b12743
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/UPSTREAM
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e5c9e6317588e6bedd8e80531f40267b8248f3c8
5283a0178901bd81703e379b9f8339457c277205
11 changes: 11 additions & 0 deletions difftest/difftest.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ var TestCases = []struct {
\ No newline at end of file
`[1:],
Edits: []diff.Edit{{Start: 0, End: 1, New: "B"}},
}, {
Name: "delete_empty",
In: "meow",
Out: "", // GNU diff -u special case: +0,0
Unified: UnifiedPrefix + `
@@ -1 +0,0 @@
-meow
\ No newline at end of file
`[1:],
Edits: []diff.Edit{{Start: 0, End: 4, New: ""}},
LineEdits: []diff.Edit{{Start: 0, End: 4, New: ""}},
}, {
Name: "append_empty",
In: "", // GNU diff -u special case: -0,0
Expand Down
2 changes: 1 addition & 1 deletion lcs/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,6 @@ be recomputed.
and can be found at
http://www.xmailserver.org/diff2.pdf
(There is a generic implementation of the algorithm the the repository with git hash
(There is a generic implementation of the algorithm the repository with git hash
b9ad7e4ade3a686d608e44475390ad428e60e7fc)
*/
3 changes: 3 additions & 0 deletions unified.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ func (u unified) String() string {
}
if toCount > 1 {
fmt.Fprintf(b, " +%d,%d", hunk.ToLine, toCount)
} else if hunk.ToLine == 1 && toCount == 0 {
// Match odd GNU diff -u behavior adding to empty file.
fmt.Fprintf(b, " +0,0")
} else {
fmt.Fprintf(b, " +%d", hunk.ToLine)
}
Expand Down

0 comments on commit 9b12743

Please sign in to comment.