Commit 0126e3fc by Max Kostyukevich Committed by Patrick Steinhardt

apply: Fix a patch corruption related to EOFNL handling

Use of apply's API can lead to an improper patch application and a corruption
of the modified file.

The issue is caused by mishandling of the end of file changes if there are
several hunks to apply. The new line character is added to a line from a wrong
hunk.

The solution is to modify apply_hunk() to add the newline character at the end
of a line from a right hunk.
parent ae9b333a
...@@ -210,7 +210,7 @@ static int apply_hunk( ...@@ -210,7 +210,7 @@ static int apply_hunk(
case GIT_DIFF_LINE_CONTEXT_EOFNL: case GIT_DIFF_LINE_CONTEXT_EOFNL:
case GIT_DIFF_LINE_DEL_EOFNL: case GIT_DIFF_LINE_DEL_EOFNL:
case GIT_DIFF_LINE_ADD_EOFNL: case GIT_DIFF_LINE_ADD_EOFNL:
prev = i ? git_array_get(patch->lines, i - 1) : NULL; prev = i ? git_array_get(patch->lines, linenum - 1) : NULL;
if (prev && prev->content[prev->content_len - 1] == '\n') if (prev && prev->content[prev->content_len - 1] == '\n')
prev->content_len -= 1; prev->content_len -= 1;
break; break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment