patch_parse: properly parse EOFNL comment lines#5161
Conversation
When either the old or new file contents have no newline at the end of the file, then git-diff(1) will print out a "\ No newline at end of file" indicator. While we do correctly handle this in the case where the new file has this indcator, we fail to parse patches where the old file is missing a newline at EOF. Fix this bug by handling and missing newline indicators in the old file. Add tests to verify that we can parse such files.
|
@pks-t I based this on your PR and validated the patch output. Would appreciate a quick look if I missed something. |
|
|
I just rebase all the patches to test with examples, and will review (just if that helps). I called it https://github.com/albfan/libgit2/pull/new/fix-patch-to-diff-no-newline |
|
@pks-t without a deeper lot at this, Still not sure how this is translated to a hunk, so I will test with our vala integration project: https://gitlab.gnome.org/albfan/diferencia |
pks-t
left a comment
There was a problem hiding this comment.
Cool, thanks a lot for this! It mostly looks good to me, but we definitely need to fix our patch ID generation first before we can merge this. As said, just let me know whether you want to tackle that, otherwise I'll take over again.
| next = git_array_get(patch->lines, linenum + 1); | ||
| if (next->origin == GIT_DIFF_LINE_CONTEXT_EOFNL || | ||
| next->origin == GIT_DIFF_LINE_DEL_EOFNL || | ||
| next->origin == GIT_DIFF_LINE_ADD_EOFNL) { |
There was a problem hiding this comment.
Nit: indentation is off here. If conditionals span multiple lines, then the conditions should be indented by four spaces relative to the if, only:
if (next->origin == GIT_DIFF_LINE_CONTEXT_EOFNL ||
next->origin == GIT_DIFF_LINE_DEL_EOFNL ||
next->origin == GIT_DIFF_LINE_ADD_EOFNL) {
...
}
There was a problem hiding this comment.
But the project is set to indent with TAB
There was a problem hiding this comment.
Yeah. We had some misunderstandings around that in the past, but our rule is to follow git.git and thus linux.git's style. They use 8-space tabs in general, but indent multiline conditionals with four spaces. I've never used editorconfig, but if it is possible to specify this rule then we should certainly fix it.
|
|
||
| cl_git_pass(git_patch_from_buffer(&patch, content, strlen(content), NULL)); | ||
| cl_git_pass(git_patch_to_buf(&buf, patch)); | ||
| cl_assert_equal_strn(git_buf_cstr(&buf), content, strlen(content)); |
There was a problem hiding this comment.
This should be a cl_assert_equal_str, otherwise we wouldn't notice if the generated patch had unexpected trailing content
| cl_assert_equal_i(0, delta->new_file.size); | ||
| } | ||
|
|
||
| static void ensure_identical_patch_inout(const char *content) { |
There was a problem hiding this comment.
Style nit: Opening braces belong on their own line
| void test_diff_patchid__filename_with_spaces(void) | ||
| { | ||
| verify_patch_id(PATCH_APPEND_NO_NL, "f0ba05413beaef743b630e796153839462ee477a"); | ||
| verify_patch_id(PATCH_APPEND_NO_NL, "cc0d67fc516f3378baa8a72b248da6e26c5dbf4a"); |
There was a problem hiding this comment.
This is incorrect, though. git-patch-id(1) prints "f0ba05413beaef743b630e796153839462ee477a", so we cannot simply alter the expected ID here. So this indicates that we have to adjust how we calculate the patch ID when there's no trailing newline. Just let me know whether you want to amend that or whether I should take over again :)
There was a problem hiding this comment.
yes, please take over. probably better someone fixes this that knows what the issue is ^^
| break; | ||
| default: | ||
| git_error_set(GIT_ERROR_PATCH, "invalid line origin for patch"); | ||
| return -1; |
There was a problem hiding this comment.
It would be nice to avoid re-indenting the whole block so that it's easier to spot what exactly has changed
|
Oops, sorry for closing your PR, I confused this one with #5159. Anyway, I've added your fix to #5159 now and fixed two issues with patch IDs and another one with how we strip newlines when applying the patch. I can reopen this one if you feel like the combined fix in #5159 isn't sufficient yet, but I didn't want to force-push over your PR as I did quite some changes to your patch. |
|
It's fine! Thanks for filling in the missing stuff. /closed |
The patch output of #5159 did not produce a valid EOF comment