git-apply: fail if a patch cannot be applied.
Recently we fixed 'git-apply --stat' not to barf on a binary differences. But it accidentally broke the error detection when we actually attempt to apply them. This commit fixes the problem and adds test cases. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Nov 16, 2005 at 14:12 UTC
92927ed0aac56a86f85049215791fcd106af4b62
2 files changed
+85
-4
apply.c
+7
-4
@@ -891,7 +891,7 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
891
892
patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch);
893
894
- if (!patchsize && !metadata_changes(patch)) {
894
+ if (!patchsize) {
895
static const char binhdr[] = "Binary files ";
896
897
if (sizeof(binhdr) - 1 < size - offset - hdrsize &&
@@ -899,9 +899,12 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
899
sizeof(binhdr)-1))
900
patch->is_binary = 1;
901
902
- if (patch->is_binary && !apply && !check)
903
- ;
904
- else
902
+ /* Empty patch cannot be applied if:
903
+ * - it is a binary patch or
904
+ * - metadata does not change and is not a binary patch.
905
+ */
906
+ if ((apply || check) &&
907
+ (patch->is_binary || !metadata_changes(patch)))
908
die("patch with only garbage at line %d", linenr);
909
}
910
t/t4103-apply-binary.sh
new
+78
@@ -0,0 +1,78 @@
1
+#!/bin/sh
2
+#
3
+# Copyright (c) 2005 Junio C Hamano
4
+#
5
+
6
+test_description='git-apply handling binary patches
7
+
8
+'
9
+. ./test-lib.sh
10
+
11
+# setup
12
+
13
+cat >file1 <<EOF
14
+A quick brown fox jumps over the lazy dog.
15
+A tiny little penguin runs around in circles.
16
+There is a flag with Linux written on it.
17
+A slow black-and-white panda just sits there,
18
+munching on his bamboo.
19
+EOF
20
+cat file1 >file2
21
+cat file1 >file4
22
+
23
+git-update-index --add --remove file1 file2 file4
24
+git-commit -m 'Initial Version' 2>/dev/null
25
+
26
+git-checkout -b binary
27
+tr 'x' '\0' <file1 >file3
28
+cat file3 >file4
29
+git-add file2
30
+tr '\0' 'v' <file3 >file1
31
+rm -f file2
32
+git-update-index --add --remove file1 file2 file3 file4
33
+git-commit -m 'Second Version'
34
+
35
+git-diff-tree -p master binary >B.diff
36
+git-diff-tree -p -C master binary >C.diff
37
+
38
+test_expect_success 'stat binary diff -- should not fail.' \
39
+ 'git-checkout master
40
+ git-apply --stat --summary B.diff'
41
+
42
+test_expect_success 'stat binary diff (copy) -- should not fail.' \
43
+ 'git-checkout master
44
+ git-apply --stat --summary C.diff'
45
+
46
+test_expect_failure 'check binary diff -- should fail.' \
47
+ 'git-checkout master
48
+ git-apply --check B.diff'
49
+
50
+test_expect_failure 'check binary diff (copy) -- should fail.' \
51
+ 'git-checkout master
52
+ git-apply --check C.diff'
53
+
54
+# Now we start applying them.
55
+
56
+test_expect_failure 'apply binary diff -- should fail.' \
57
+ 'git-checkout master
58
+ git-apply B.diff'
59
+
60
+git-reset --hard
61
+
62
+test_expect_failure 'apply binary diff -- should fail.' \
63
+ 'git-checkout master
64
+ git-apply --index B.diff'
65
+
66
+git-reset --hard
67
+
68
+test_expect_failure 'apply binary diff (copy) -- should fail.' \
69
+ 'git-checkout master
70
+ git-apply C.diff'
71
+
72
+git-reset --hard
73
+
74
+test_expect_failure 'apply binary diff (copy) -- should fail.' \
75
+ 'git-checkout master
76
+ git-apply --index C.diff'
77
+
78
+test_done