-
Notifications
You must be signed in to change notification settings - Fork 27
apply fails to handle diffs with files without newline or patches that remove newline at end of the file #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Investigating more; seems to be that
|
Okay even more discovery! I did the same with GNU diff and found:
Git seems to fail on that diff style as well. |
Thanks for reporting this. If I understand your investigation, this is specifically a problem with the output of the BSD If Git supports this type of patches, I should fix this library to also accept them. If Git fails on this style of patch, I'm less inclined to add support here, but could at least see how hard it might be. I'm reasonably confident that this works correctly with Git-style patches as there were existing tests for adding a final newline and changing content without changing the final newline. You were correct that there was no test for removing the final newline, but that seems to work as expected in the current version. |
@bluekeyes it's kind of hard to replicate because the "algorithm" for producing the hunks is a bit tricky. I think git is failing to apply this same patch which I sent an email to the mailing list. It's not clear to me if it's a "bug" in Git or something purposeful. Given the prevalence of MacOS for developers (Although I daily drive NixOS :P) seems like a foot gun given that Anyways, this caused me a lot of hair pulling trying to debug; thought I'd record the info here. |
It looks like the files, _, err := gitdiff.Parse(input)
if err != nil {
return err
}
for _, file := range files {
// For all but the last fragment, add a final newline to any lines that do not have one
// Only the last fragment in a file can end without a newline
for i := 0; i < len(files.TextFragments)-1; i++ {
lines := files.TextFragments[i].Lines
for n, line := range lines {
if line.NoEOL() {
lines[n].Line += "\n"
}
}
}
} Since Git currently behaves the same way as this library, I'm not going to make any changes. But if the Git developers determine this is a bug and update a future version of Git, I can also update the behavior here to match. |
In issue #57, it was reported that the library could not apply some patches generated by BSD `diff` because of the way that variant reports changes in files without trailing newlines. Since the library behavior matches Git, I don't consider this a bug, but update the README to mention that the support for standard unified diffs is only for the GNU variant of the `diff` tool.
I encountered this issue via aspect-build/rules_lint#487
tl;dr; If a file is missing a newline at the end or the patch removes the newline at the end, go-gitdiff fails applying the patch with the error:
I'll push a reproduction example through a failed test, where
git
succeeds.The offending check can be found
go-gitdiff/gitdiff/apply_text.go
Line 128 in fffa3cc
The text was updated successfully, but these errors were encountered: