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>

Jialong Wang committed Mar 17, 2026 at 12:23 UTC 2ef795b027c10f5e253151106a6fb4265552cc04
3 files changed +37 -5
apply.c
+6 -4
@@ -2110,8 +2110,8 @@ static struct fragment *parse_binary_hunk(struct apply_state *state,
2110 corrupt:
2111 free(data);
2112 *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);
2115 return NULL;
2116 }
2117
@@ -2147,7 +2147,8 @@ static int parse_binary(struct apply_state *state,
2147 forward = parse_binary_hunk(state, &buffer, &size, &status, &used);
2148 if (!forward && !status)
2149 /* 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);
2152 if (status)
2153 /* otherwise we already gave an error message */
2154 return status;
@@ -2309,7 +2310,8 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
2310 */
2311 if ((state->apply || state->check) &&
2312 (!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);
2315 return -128;
2316 }
2317 }
t/t4100-apply-stat.sh
+12
@@ -125,4 +125,16 @@ test_expect_success 'applying a patch with an invalid mode reports the input' '
125 EOF
126 test_cmp expect err
127 '
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 +'
140 test_done
t/t4103-apply-binary.sh
+19 -1
@@ -179,6 +179,24 @@ test_expect_success PERL_TEST_HELPERS 'reject truncated binary diff' '
179 " <patch >patch.trunc &&
180
181 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
201 '
202 test_done