Skip to content

Commit 2ef795b

Browse files
Jialong Wanggitster
authored andcommitted
apply: report input location in binary and garbage patch errors
Several binary parsing paths in apply.c still report only line numbers. When more than one patch input is fed to a single invocation, that does not tell the user which input the line belongs to. Report the patch input location for corrupt and unrecognized binary patches, as well as the "patch with only garbage" case, and update the related tests. Signed-off-by: Jialong Wang <jerrywang183@yahoo.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 838c0ba commit 2ef795b

3 files changed

Lines changed: 37 additions & 5 deletions

File tree

apply.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,8 +2110,8 @@ static struct fragment *parse_binary_hunk(struct apply_state *state,
21102110
corrupt:
21112111
free(data);
21122112
*status_p = -1;
2113-
error(_("corrupt binary patch at line %d: %.*s"),
2114-
state->linenr-1, llen-1, buffer);
2113+
error(_("corrupt binary patch at %s:%d: %.*s"),
2114+
state->patch_input_file, state->linenr-1, llen-1, buffer);
21152115
return NULL;
21162116
}
21172117

@@ -2147,7 +2147,8 @@ static int parse_binary(struct apply_state *state,
21472147
forward = parse_binary_hunk(state, &buffer, &size, &status, &used);
21482148
if (!forward && !status)
21492149
/* there has to be one hunk (forward hunk) */
2150-
return error(_("unrecognized binary patch at line %d"), state->linenr-1);
2150+
return error(_("unrecognized binary patch at %s:%d"),
2151+
state->patch_input_file, state->linenr-1);
21512152
if (status)
21522153
/* otherwise we already gave an error message */
21532154
return status;
@@ -2309,7 +2310,8 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
23092310
*/
23102311
if ((state->apply || state->check) &&
23112312
(!patch->is_binary && !metadata_changes(patch))) {
2312-
error(_("patch with only garbage at line %d"), state->linenr);
2313+
error(_("patch with only garbage at %s:%d"),
2314+
state->patch_input_file, state->linenr);
23132315
return -128;
23142316
}
23152317
}

t/t4100-apply-stat.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,16 @@ test_expect_success 'applying a patch with an invalid mode reports the input' '
125125
EOF
126126
test_cmp expect err
127127
'
128+
129+
test_expect_success 'applying a patch with only garbage reports the input' '
130+
cat >garbage.patch <<-\EOF &&
131+
diff --git a/f b/f
132+
--- a/f
133+
+++ b/f
134+
this is garbage
135+
EOF
136+
test_must_fail git apply garbage.patch 2>err &&
137+
echo "error: patch with only garbage at garbage.patch:4" >expect &&
138+
test_cmp expect err
139+
'
128140
test_done

t/t4103-apply-binary.sh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,24 @@ test_expect_success PERL_TEST_HELPERS 'reject truncated binary diff' '
179179
" <patch >patch.trunc &&
180180
181181
do_reset &&
182-
test_must_fail git apply patch.trunc
182+
test_must_fail git apply patch.trunc 2>err &&
183+
line=$(awk "END { print NR + 1 }" patch.trunc) &&
184+
grep "error: corrupt binary patch at patch.trunc:$line: " err
185+
'
186+
187+
test_expect_success 'reject unrecognized binary diff' '
188+
cat >patch.bad <<-\EOF &&
189+
diff --git a/f b/f
190+
new file mode 100644
191+
index 0000000..7898192
192+
GIT binary patch
193+
bogus
194+
EOF
195+
test_must_fail git apply patch.bad 2>err &&
196+
cat >expect <<-\EOF &&
197+
error: unrecognized binary patch at patch.bad:4
198+
error: No valid patches in input (allow with "--allow-empty")
199+
EOF
200+
test_cmp expect err
183201
'
184202
test_done

0 commit comments

Comments
 (0)