Skip to content

Commit 048e94a

Browse files
committed
patch_parse: correct parsing of patch containing not shown binary data.
When not shown binary data is added or removed in a patch, patch parser is currently returning 'error -1 - corrupt git binary header at line 4'. Fix it by correctly handling case where binary data is added/removed. Signed-off-by: Gregory Herrero <gregory.herrero@oracle.com>
1 parent b921964 commit 048e94a

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

src/patch_parse.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ static int parse_header_git_deletedfilemode(
231231
git_patch_parsed *patch,
232232
git_patch_parse_ctx *ctx)
233233
{
234-
git__free((char *)patch->base.delta->old_file.path);
234+
git__free((char *)patch->base.delta->new_file.path);
235235

236-
patch->base.delta->old_file.path = NULL;
236+
patch->base.delta->new_file.path = NULL;
237237
patch->base.delta->status = GIT_DELTA_DELETED;
238238
patch->base.delta->nfiles = 1;
239239

@@ -244,9 +244,9 @@ static int parse_header_git_newfilemode(
244244
git_patch_parsed *patch,
245245
git_patch_parse_ctx *ctx)
246246
{
247-
git__free((char *)patch->base.delta->new_file.path);
247+
git__free((char *)patch->base.delta->old_file.path);
248248

249-
patch->base.delta->new_file.path = NULL;
249+
patch->base.delta->old_file.path = NULL;
250250
patch->base.delta->status = GIT_DELTA_ADDED;
251251
patch->base.delta->nfiles = 1;
252252

@@ -884,6 +884,11 @@ static int parse_patch_binary_nodata(
884884
if (!old || !new)
885885
return git_parse_err("corrupt binary data without paths at line %"PRIuZ, ctx->parse_ctx.line_num);
886886

887+
if (patch->base.delta->status == GIT_DELTA_ADDED)
888+
old = "/dev/null";
889+
else if (patch->base.delta->status == GIT_DELTA_DELETED)
890+
new = "/dev/null";
891+
887892
if (git_parse_advance_expected_str(&ctx->parse_ctx, "Binary files ") < 0 ||
888893
git_parse_advance_expected_str(&ctx->parse_ctx, old) < 0 ||
889894
git_parse_advance_expected_str(&ctx->parse_ctx, " and ") < 0 ||

tests/patch/patch_common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,12 @@
878878
"index 27184d9..7c94f9e 100644\n" \
879879
"Binary files a/binary.bin and b/binary.bin differ\n"
880880

881+
#define PATCH_ADD_BINARY_NOT_PRINTED \
882+
"diff --git a/test.bin b/test.bin\n" \
883+
"new file mode 100644\n" \
884+
"index 0000000..9e0f96a\n" \
885+
"Binary files /dev/null and b/test.bin differ\n"
886+
881887
#define PATCH_ORIGINAL_NEW_FILE_WITH_SPACE \
882888
"diff --git a/sp ace.txt b/sp ace.txt\n" \
883889
"new file mode 100644\n" \

tests/patch/print.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,9 @@ void test_patch_print__binary_not_shown(void)
172172
patch_print_from_patchfile(PATCH_BINARY_NOT_PRINTED,
173173
strlen(PATCH_BINARY_NOT_PRINTED));
174174
}
175+
176+
void test_patch_print__binary_add_not_shown(void)
177+
{
178+
patch_print_from_patchfile(PATCH_ADD_BINARY_NOT_PRINTED,
179+
strlen(PATCH_ADD_BINARY_NOT_PRINTED));
180+
}

0 commit comments

Comments
 (0)