Skip to content

Commit bffd3de

Browse files
Jialong Wanggitster
authored andcommitted
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>
1 parent dc6ecd5 commit bffd3de

3 files changed

Lines changed: 42 additions & 4 deletions

File tree

apply.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1875,7 +1875,8 @@ static int parse_single_patch(struct apply_state *state,
18751875
len = parse_fragment(state, line, size, patch, fragment);
18761876
if (len <= 0) {
18771877
free(fragment);
1878-
return error(_("corrupt patch at line %d"), state->linenr);
1878+
return error(_("corrupt patch at %s:%d"),
1879+
state->patch_input_file, state->linenr);
18791880
}
18801881
fragment->patch = line;
18811882
fragment->size = len;
@@ -4825,6 +4826,7 @@ static int apply_patch(struct apply_state *state,
48254826
int flush_attributes = 0;
48264827

48274828
state->patch_input_file = filename;
4829+
state->linenr = 1;
48284830
if (read_patch_file(&buf, fd) < 0)
48294831
return -128;
48304832
offset = 0;

t/t4012-diff-binary.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ test_expect_success 'apply detecting corrupt patch correctly' '
6868
sed -e "s/-CIT/xCIT/" <output >broken &&
6969
test_must_fail git apply --stat --summary broken 2>detected &&
7070
detected=$(cat detected) &&
71-
detected=$(expr "$detected" : "error.*at line \\([0-9]*\\)\$") &&
71+
detected=$(expr "$detected" : "error.*broken:\\([0-9]*\\)\$") &&
7272
detected=$(sed -ne "${detected}p" broken) &&
7373
test "$detected" = xCIT
7474
'
@@ -77,7 +77,7 @@ test_expect_success 'apply detecting corrupt patch correctly' '
7777
git diff --binary | sed -e "s/-CIT/xCIT/" >broken &&
7878
test_must_fail git apply --stat --summary broken 2>detected &&
7979
detected=$(cat detected) &&
80-
detected=$(expr "$detected" : "error.*at line \\([0-9]*\\)\$") &&
80+
detected=$(expr "$detected" : "error.*broken:\\([0-9]*\\)\$") &&
8181
detected=$(sed -ne "${detected}p" broken) &&
8282
test "$detected" = xCIT
8383
'

t/t4100-apply-stat.sh

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,43 @@ test_expect_success 'applying a hunk header which overflows fails' '
4848
+b
4949
EOF
5050
test_must_fail git apply patch 2>err &&
51-
echo "error: corrupt patch at line 4" >expect &&
51+
echo "error: corrupt patch at patch:4" >expect &&
52+
test_cmp expect err
53+
'
54+
55+
test_expect_success 'applying a hunk header which overflows from stdin fails' '
56+
cat >patch <<-\EOF &&
57+
diff -u a/file b/file
58+
--- a/file
59+
+++ b/file
60+
@@ -98765432109876543210 +98765432109876543210 @@
61+
-a
62+
+b
63+
EOF
64+
test_must_fail git apply <patch 2>err &&
65+
echo "error: corrupt patch at <stdin>:4" >expect &&
66+
test_cmp expect err
67+
'
68+
69+
test_expect_success 'applying multiple patches reports the corrupted input' '
70+
cat >good.patch <<-\EOF &&
71+
diff -u a/file b/file
72+
--- a/file
73+
+++ b/file
74+
@@ -1 +1 @@
75+
-a
76+
+b
77+
EOF
78+
cat >bad.patch <<-\EOF &&
79+
diff -u a/file b/file
80+
--- a/file
81+
+++ b/file
82+
@@ -98765432109876543210 +98765432109876543210 @@
83+
-a
84+
+b
85+
EOF
86+
test_must_fail git apply --stat --summary good.patch bad.patch 2>err &&
87+
echo "error: corrupt patch at bad.patch:4" >expect &&
5288
test_cmp expect err
5389
'
5490
test_done

0 commit comments

Comments
 (0)