aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJialong Wang <jerrywang183@yahoo.com>2026-03-17 12:23:19 -0400
committerJunio C Hamano <gitster@pobox.com>2026-03-17 11:08:24 -0700
commitc5e15c9a58c0b32ec8be3ae96434834124de175e (patch)
tree08679ef3fea335dfbfd2672563b222a6acde4e1c
parentdc6ecd5354dca88d51b6d6562777fc8fc10d77e1 (diff)
downloadgit-c5e15c9a58c0b32ec8be3ae96434834124de175e.tar.xz
apply: report the location of corrupt patches
When parsing a corrupt patch, git apply reports only the line number. That does not tell the user which input the line number refers to. Include the patch input path in the error message so the reported location is easier to use. Reset the line number for each patch input so the reported location stays correct when multiple input files are provided. Add tests for file input, standard input, multiple patch inputs, and existing binary-diff corrupt patch cases. Signed-off-by: Jialong Wang <jerrywang183@yahoo.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--apply.c4
-rwxr-xr-xt/t4012-diff-binary.sh4
-rwxr-xr-xt/t4100-apply-stat.sh38
3 files changed, 42 insertions, 4 deletions
diff --git a/apply.c b/apply.c
index b6dd1066a0..b7b0a201b3 100644
--- a/apply.c
+++ b/apply.c
@@ -1875,7 +1875,8 @@ static int parse_single_patch(struct apply_state *state,
len = parse_fragment(state, line, size, patch, fragment);
if (len <= 0) {
free(fragment);
- return error(_("corrupt patch at line %d"), state->linenr);
+ return error(_("corrupt patch at %s:%d"),
+ state->patch_input_file, state->linenr);
}
fragment->patch = line;
fragment->size = len;
@@ -4825,6 +4826,7 @@ static int apply_patch(struct apply_state *state,
int flush_attributes = 0;
state->patch_input_file = filename;
+ state->linenr = 1;
if (read_patch_file(&buf, fd) < 0)
return -128;
offset = 0;
diff --git a/t/t4012-diff-binary.sh b/t/t4012-diff-binary.sh
index d1d30ac2a9..97b5ac0407 100755
--- a/t/t4012-diff-binary.sh
+++ b/t/t4012-diff-binary.sh
@@ -68,7 +68,7 @@ test_expect_success 'apply detecting corrupt patch correctly' '
sed -e "s/-CIT/xCIT/" <output >broken &&
test_must_fail git apply --stat --summary broken 2>detected &&
detected=$(cat detected) &&
- detected=$(expr "$detected" : "error.*at line \\([0-9]*\\)\$") &&
+ detected=$(expr "$detected" : "error.*broken:\\([0-9]*\\)\$") &&
detected=$(sed -ne "${detected}p" broken) &&
test "$detected" = xCIT
'
@@ -77,7 +77,7 @@ test_expect_success 'apply detecting corrupt patch correctly' '
git diff --binary | sed -e "s/-CIT/xCIT/" >broken &&
test_must_fail git apply --stat --summary broken 2>detected &&
detected=$(cat detected) &&
- detected=$(expr "$detected" : "error.*at line \\([0-9]*\\)\$") &&
+ detected=$(expr "$detected" : "error.*broken:\\([0-9]*\\)\$") &&
detected=$(sed -ne "${detected}p" broken) &&
test "$detected" = xCIT
'
diff --git a/t/t4100-apply-stat.sh b/t/t4100-apply-stat.sh
index a5664f3eb3..b19fc9fe50 100755
--- a/t/t4100-apply-stat.sh
+++ b/t/t4100-apply-stat.sh
@@ -48,7 +48,43 @@ test_expect_success 'applying a hunk header which overflows fails' '
+b
EOF
test_must_fail git apply patch 2>err &&
- echo "error: corrupt patch at line 4" >expect &&
+ echo "error: corrupt patch at patch:4" >expect &&
+ test_cmp expect err
+'
+
+test_expect_success 'applying a hunk header which overflows from stdin fails' '
+ cat >patch <<-\EOF &&
+ diff -u a/file b/file
+ --- a/file
+ +++ b/file
+ @@ -98765432109876543210 +98765432109876543210 @@
+ -a
+ +b
+ EOF
+ test_must_fail git apply <patch 2>err &&
+ echo "error: corrupt patch at <stdin>:4" >expect &&
+ test_cmp expect err
+'
+
+test_expect_success 'applying multiple patches reports the corrupted input' '
+ cat >good.patch <<-\EOF &&
+ diff -u a/file b/file
+ --- a/file
+ +++ b/file
+ @@ -1 +1 @@
+ -a
+ +b
+ EOF
+ cat >bad.patch <<-\EOF &&
+ diff -u a/file b/file
+ --- a/file
+ +++ b/file
+ @@ -98765432109876543210 +98765432109876543210 @@
+ -a
+ +b
+ EOF
+ test_must_fail git apply --stat --summary good.patch bad.patch 2>err &&
+ echo "error: corrupt patch at bad.patch:4" >expect &&
test_cmp expect err
'
test_done