aboutsummaryrefslogtreecommitdiff
path: root/t
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2026-03-30 13:57:00 -0700
committerJunio C Hamano <gitster@pobox.com>2026-03-30 13:57:00 -0700
commit5032e70fc25ff18a3c16fb89d708884104c2d1a3 (patch)
treea1a2f666f1042c36adbe8b66d86973139f917595 /t
parent295eb2cc473675bafb1b523696a442e6969ccd41 (diff)
parent2ef795b027c10f5e253151106a6fb4265552cc04 (diff)
downloadgit-5032e70fc25ff18a3c16fb89d708884104c2d1a3.tar.xz
Merge branch 'jw/apply-corrupt-location'
"git apply" now reports the name of the input file along with the line number when it encounters a corrupt patch, and correctly resets the line counter when processing multiple patch files. * jw/apply-corrupt-location: apply: report input location in binary and garbage patch errors apply: report input location in header parsing errors apply: report the location of corrupt patches
Diffstat (limited to 't')
-rwxr-xr-xt/t4012-diff-binary.sh4
-rwxr-xr-xt/t4100-apply-stat.sh88
-rwxr-xr-xt/t4103-apply-binary.sh20
-rwxr-xr-xt/t4254-am-corrupt.sh3
4 files changed, 109 insertions, 6 deletions
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..8393076469 100755
--- a/t/t4100-apply-stat.sh
+++ b/t/t4100-apply-stat.sh
@@ -48,7 +48,93 @@ 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_expect_success 'applying a patch without a header reports the input' '
+ cat >fragment.patch <<-\EOF &&
+ @@ -1 +1 @@
+ -a
+ +b
+ EOF
+ test_must_fail git apply fragment.patch 2>err &&
+ echo "error: patch fragment without header at fragment.patch:1: @@ -1 +1 @@" >expect &&
+ test_cmp expect err
+'
+
+test_expect_success 'applying a patch with a missing filename reports the input' '
+ cat >missing.patch <<-\EOF &&
+ diff --git a/f b/f
+ index 7898192..6178079 100644
+ --- a/f
+ @@ -1 +1 @@
+ -a
+ +b
+ EOF
+ test_must_fail git apply missing.patch 2>err &&
+ echo "error: git diff header lacks filename information at missing.patch:4" >expect &&
+ test_cmp expect err
+'
+
+test_expect_success 'applying a patch with an invalid mode reports the input' '
+ cat >mode.patch <<-\EOF &&
+ diff --git a/f b/f
+ old mode 10x644
+ EOF
+ test_must_fail git apply mode.patch 2>err &&
+ cat >expect <<-\EOF &&
+ error: invalid mode at mode.patch:2: 10x644
+
+ EOF
+ test_cmp expect err
+'
+
+test_expect_success 'applying a patch with only garbage reports the input' '
+ cat >garbage.patch <<-\EOF &&
+ diff --git a/f b/f
+ --- a/f
+ +++ b/f
+ this is garbage
+ EOF
+ test_must_fail git apply garbage.patch 2>err &&
+ echo "error: patch with only garbage at garbage.patch:4" >expect &&
test_cmp expect err
'
test_done
diff --git a/t/t4103-apply-binary.sh b/t/t4103-apply-binary.sh
index 8e302a5a57..f2d41e06bc 100755
--- a/t/t4103-apply-binary.sh
+++ b/t/t4103-apply-binary.sh
@@ -179,6 +179,24 @@ test_expect_success PERL_TEST_HELPERS 'reject truncated binary diff' '
" <patch >patch.trunc &&
do_reset &&
- test_must_fail git apply patch.trunc
+ test_must_fail git apply patch.trunc 2>err &&
+ line=$(awk "END { print NR + 1 }" patch.trunc) &&
+ grep "error: corrupt binary patch at patch.trunc:$line: " err
+'
+
+test_expect_success 'reject unrecognized binary diff' '
+ cat >patch.bad <<-\EOF &&
+ diff --git a/f b/f
+ new file mode 100644
+ index 0000000..7898192
+ GIT binary patch
+ bogus
+ EOF
+ test_must_fail git apply patch.bad 2>err &&
+ cat >expect <<-\EOF &&
+ error: unrecognized binary patch at patch.bad:4
+ error: No valid patches in input (allow with "--allow-empty")
+ EOF
+ test_cmp expect err
'
test_done
diff --git a/t/t4254-am-corrupt.sh b/t/t4254-am-corrupt.sh
index ae0a56cf5e..96ddf3c53a 100755
--- a/t/t4254-am-corrupt.sh
+++ b/t/t4254-am-corrupt.sh
@@ -65,9 +65,8 @@ test_expect_success setup '
test_expect_success 'try to apply corrupted patch' '
test_when_finished "git am --abort" &&
test_must_fail git -c advice.amWorkDir=false -c advice.mergeConflict=false am bad-patch.diff 2>actual &&
- echo "error: git diff header lacks filename information (line 4)" >expected &&
test_path_is_file f &&
- test_cmp expected actual
+ test_grep "error: git diff header lacks filename information at .*rebase-apply/patch:4" actual
'
test_expect_success "NUL in commit message's body" '