Skip to content

Commit c3a7892

Browse files
authored
Merge pull request libgit2#5209 from mkostyuk/apply-wrong-patch
apply: Fix a patch corruption related to EOFNL handling
2 parents 17d6cd4 + 585fbd7 commit c3a7892

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ static int apply_hunk(
219219
case GIT_DIFF_LINE_CONTEXT_EOFNL:
220220
case GIT_DIFF_LINE_DEL_EOFNL:
221221
case GIT_DIFF_LINE_ADD_EOFNL:
222-
prev = i ? git_array_get(patch->lines, i - 1) : NULL;
222+
prev = i ? git_array_get(patch->lines, linenum - 1) : NULL;
223223
if (prev && prev->content[prev->content_len - 1] == '\n')
224224
prev->content_len -= 1;
225225
break;

tests/apply/fromdiff.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ void test_apply_fromdiff__lastline(void)
131131
PATCH_ORIGINAL_TO_CHANGE_LASTLINE, NULL));
132132
}
133133

134+
void test_apply_fromdiff__change_middle_and_lastline_nocontext(void)
135+
{
136+
git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
137+
diff_opts.context_lines = 0;
138+
139+
cl_git_pass(apply_buf(
140+
FILE_ORIGINAL, "file.txt",
141+
FILE_CHANGE_MIDDLE_AND_LASTLINE, "file.txt",
142+
PATCH_ORIGINAL_TO_CHANGE_MIDDLE_AND_LASTLINE_NOCONTEXT, &diff_opts));
143+
}
144+
134145
void test_apply_fromdiff__prepend(void)
135146
{
136147
cl_git_pass(apply_buf(

tests/patch/patch_common.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,32 @@
263263
"-(this line is changed)\n" \
264264
"+(THIS line is changed!)\n"
265265

266+
/* A change in the middle and a deletion of the newline at the end of the file */
267+
268+
#define FILE_CHANGE_MIDDLE_AND_LASTLINE \
269+
"hey!\n" \
270+
"this is some context!\n" \
271+
"around some lines\n" \
272+
"that will change\n" \
273+
"yes it is!\n" \
274+
"(THIS line is changed!)\n" \
275+
"and this\n" \
276+
"is additional context\n" \
277+
"BELOW it! - (THIS line is changed!)"
278+
279+
#define PATCH_ORIGINAL_TO_CHANGE_MIDDLE_AND_LASTLINE_NOCONTEXT \
280+
"diff --git a/file.txt b/file.txt\n" \
281+
"index 9432026..e05d36c 100644\n" \
282+
"--- a/file.txt\n" \
283+
"+++ b/file.txt\n" \
284+
"@@ -6 +6 @@ yes it is!\n" \
285+
"-(this line is changed)\n" \
286+
"+(THIS line is changed!)\n" \
287+
"@@ -9 +9 @@ is additional context\n" \
288+
"-below it!\n" \
289+
"+BELOW it! - (THIS line is changed!)\n" \
290+
"\\ No newline at end of file\n"
291+
266292
/* A deletion at the beginning of the file and a change in the middle */
267293

268294
#define FILE_DELETE_AND_CHANGE \

0 commit comments

Comments
 (0)