threads / discuss / 63061

Failing to apply patch generated with BSD (MacOS) diff -- multiple No newline at end of file

Subject: Failing to apply patch generated with BSD (MacOS) diff -- multiple No newline at end of file

## tl;dr

One message between Mar 4, 2025 and Mar 4, 2025.

replies: 0people: 1as markdown or json

Farid Zakaria· Mar 4, 2025, 23:24 UTC · lore

I was diagnosing an issue in a separate Git implementation (https://github.com/bluekeyes/go-gitdiff/issues/57) and noticed a related bug with Git.

BSD's diff seems to produce diffs where each hunk in the diff file may include '\ No newline at end of file' (This is different than what the GNU diff generates)

Applying the diff via git (git apply) removed multiple new lines from each corresponding hunk.

** What did you do before the bug happened? (Steps to reproduce your issue) **

I produced a diff with Apple BSD `diff` and it produced a diff with multiple hunks each with `\ No newline at end of file` in it

For instance ```

@@ -161,12 +161,7 @@
 this is line 161
 this is line 162
 this is line 163
-this is line 164
-this is line 165
-this is line 166
-this is line 167
-this is line 168
-this is line 169
+the number on the remaining lines is 5 ahead of their actual position
in the file
 this is line 170
 this is line 171
 this is line 172
\ No newline at end of file
@@ -197,4 +192,4 @@
 this is line 197
 this is line 198
 this is line 199
-this is line 200
+this is line 200
\ No newline at end of file
```

** What did you expect to happen? (Expected behavior) **

When I `git apply` it seems to have removed the new line from each
preceeding line in the hunk rather than at the end of the file.

This does not match what happens when I run `patch` program. It
correctly seems to skip the multiple occurrences of "No newline..."
and only strip the newline at the end of the file.

** What happened instead? (Actual behavior) **

Multiple new lines were stripped.

[System Info]
git version:
git version 2.44.1
cpu: arm64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /nix/store/4hnzq5yiv16j2k4rka60x3hnck9rrlxg-bash-5.2p32/bin/bash
feature: fsmonitor--daemon
uname: Darwin 23.6.0 Darwin Kernel Version 23.6.0: Thu Dec 19 20:47:53
PST 2024; root:xnu-10063.141.1.703.2~1/RELEASE_ARM64_T6030 arm64
compiler info: clang: 16.0.6
libc info: no libc information available
$SHELL (typically, interactive shell): /bin/zsh


[Enabled Hooks]

← back to recent threads