]>
Commit | Line | Data |
---|---|---|
27dedf0c JH |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2005 Junio C Hamano | |
4 | # | |
5 | ||
5be60078 | 6 | test_description='git apply handling binary patches |
27dedf0c JH |
7 | |
8 | ' | |
8f37854b | 9 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
334afbc7 JS |
10 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
11 | ||
71c79160 | 12 | TEST_PASSES_SANITIZE_LEAK=true |
27dedf0c JH |
13 | . ./test-lib.sh |
14 | ||
6a2abdc1 JH |
15 | test_expect_success 'setup' ' |
16 | cat >file1 <<-\EOF && | |
17 | A quick brown fox jumps over the lazy dog. | |
18 | A tiny little penguin runs around in circles. | |
19 | There is a flag with Linux written on it. | |
20 | A slow black-and-white panda just sits there, | |
21 | munching on his bamboo. | |
22 | EOF | |
23 | cat file1 >file2 && | |
24 | cat file1 >file4 && | |
25 | ||
4a45f7dd | 26 | git update-index --add --remove file1 file2 file4 && |
6a2abdc1 | 27 | git commit -m "Initial Version" 2>/dev/null && |
4a45f7dd BG |
28 | |
29 | git checkout -b binary && | |
94221d22 | 30 | perl -pe "y/x/\000/" <file1 >file3 && |
4a45f7dd BG |
31 | cat file3 >file4 && |
32 | git add file2 && | |
94221d22 | 33 | perl -pe "y/\000/v/" <file3 >file1 && |
4a45f7dd BG |
34 | rm -f file2 && |
35 | git update-index --add --remove file1 file2 file3 file4 && | |
6a2abdc1 | 36 | git commit -m "Second Version" && |
4a45f7dd | 37 | |
8f37854b JS |
38 | git diff-tree -p main binary >B.diff && |
39 | git diff-tree -p -C main binary >C.diff && | |
4a45f7dd | 40 | |
8f37854b JS |
41 | git diff-tree -p --binary main binary >BF.diff && |
42 | git diff-tree -p --binary -C main binary >CF.diff && | |
24305cd7 | 43 | |
8f37854b JS |
44 | git diff-tree -p --full-index main binary >B-index.diff && |
45 | git diff-tree -p -C --full-index main binary >C-index.diff && | |
24305cd7 | 46 | |
8f37854b | 47 | git diff-tree -p --binary --no-prefix main binary -- file3 >B0.diff && |
6a2abdc1 | 48 | |
24305cd7 | 49 | git init other-repo && |
6a2abdc1 JH |
50 | ( |
51 | cd other-repo && | |
8f37854b | 52 | git fetch .. main && |
6a2abdc1 | 53 | git reset --hard FETCH_HEAD |
24305cd7 | 54 | ) |
6a2abdc1 | 55 | ' |
27dedf0c JH |
56 | |
57 | test_expect_success 'stat binary diff -- should not fail.' \ | |
8f37854b | 58 | 'git checkout main && |
5be60078 | 59 | git apply --stat --summary B.diff' |
27dedf0c | 60 | |
6a2abdc1 | 61 | test_expect_success 'stat binary -p0 diff -- should not fail.' ' |
8f37854b | 62 | git checkout main && |
6a2abdc1 JH |
63 | git apply --stat -p0 B0.diff |
64 | ' | |
65 | ||
27dedf0c | 66 | test_expect_success 'stat binary diff (copy) -- should not fail.' \ |
8f37854b | 67 | 'git checkout main && |
5be60078 | 68 | git apply --stat --summary C.diff' |
27dedf0c | 69 | |
41ac414e | 70 | test_expect_success 'check binary diff -- should fail.' \ |
8f37854b | 71 | 'git checkout main && |
d492b31c | 72 | test_must_fail git apply --check B.diff' |
41ac414e JH |
73 | |
74 | test_expect_success 'check binary diff (copy) -- should fail.' \ | |
8f37854b | 75 | 'git checkout main && |
d492b31c | 76 | test_must_fail git apply --check C.diff' |
41ac414e JH |
77 | |
78 | test_expect_success \ | |
79 | 'check incomplete binary diff with replacement -- should fail.' ' | |
8f37854b | 80 | git checkout main && |
d492b31c | 81 | test_must_fail git apply --check --allow-binary-replacement B.diff |
41ac414e | 82 | ' |
27dedf0c | 83 | |
41ac414e JH |
84 | test_expect_success \ |
85 | 'check incomplete binary diff with replacement (copy) -- should fail.' ' | |
8f37854b | 86 | git checkout main && |
d492b31c | 87 | test_must_fail git apply --check --allow-binary-replacement C.diff |
41ac414e | 88 | ' |
27dedf0c JH |
89 | |
90 | test_expect_success 'check binary diff with replacement.' \ | |
8f37854b | 91 | 'git checkout main && |
5be60078 | 92 | git apply --check --allow-binary-replacement BF.diff' |
27dedf0c JH |
93 | |
94 | test_expect_success 'check binary diff with replacement (copy).' \ | |
8f37854b | 95 | 'git checkout main && |
5be60078 | 96 | git apply --check --allow-binary-replacement CF.diff' |
27dedf0c JH |
97 | |
98 | # Now we start applying them. | |
99 | ||
100 | do_reset () { | |
41ac414e | 101 | rm -f file? && |
3604e7c5 | 102 | git reset --hard && |
8f37854b | 103 | git checkout -f main |
27dedf0c JH |
104 | } |
105 | ||
41ac414e JH |
106 | test_expect_success 'apply binary diff -- should fail.' \ |
107 | 'do_reset && | |
d492b31c | 108 | test_must_fail git apply B.diff' |
27dedf0c | 109 | |
41ac414e JH |
110 | test_expect_success 'apply binary diff -- should fail.' \ |
111 | 'do_reset && | |
d492b31c | 112 | test_must_fail git apply --index B.diff' |
27dedf0c | 113 | |
41ac414e JH |
114 | test_expect_success 'apply binary diff (copy) -- should fail.' \ |
115 | 'do_reset && | |
d492b31c | 116 | test_must_fail git apply C.diff' |
27dedf0c | 117 | |
41ac414e JH |
118 | test_expect_success 'apply binary diff (copy) -- should fail.' \ |
119 | 'do_reset && | |
d492b31c | 120 | test_must_fail git apply --index C.diff' |
27dedf0c | 121 | |
24305cd7 JK |
122 | test_expect_success 'apply binary diff with full-index' ' |
123 | do_reset && | |
124 | git apply B-index.diff | |
125 | ' | |
126 | ||
127 | test_expect_success 'apply binary diff with full-index (copy)' ' | |
128 | do_reset && | |
129 | git apply C-index.diff | |
130 | ' | |
131 | ||
132 | test_expect_success 'apply full-index binary diff in new repo' ' | |
133 | (cd other-repo && | |
134 | do_reset && | |
135 | test_must_fail git apply ../B-index.diff) | |
136 | ' | |
137 | ||
2b6eef94 | 138 | test_expect_success 'apply binary diff without replacement.' \ |
41ac414e | 139 | 'do_reset && |
5be60078 | 140 | git apply BF.diff' |
27dedf0c | 141 | |
2b6eef94 | 142 | test_expect_success 'apply binary diff without replacement (copy).' \ |
41ac414e | 143 | 'do_reset && |
5be60078 | 144 | git apply CF.diff' |
27dedf0c JH |
145 | |
146 | test_expect_success 'apply binary diff.' \ | |
41ac414e | 147 | 'do_reset && |
5be60078 JH |
148 | git apply --allow-binary-replacement --index BF.diff && |
149 | test -z "$(git diff --name-status binary)"' | |
27dedf0c JH |
150 | |
151 | test_expect_success 'apply binary diff (copy).' \ | |
41ac414e | 152 | 'do_reset && |
5be60078 JH |
153 | git apply --allow-binary-replacement --index CF.diff && |
154 | test -z "$(git diff --name-status binary)"' | |
27dedf0c | 155 | |
6a2abdc1 JH |
156 | test_expect_success 'apply binary -p0 diff' ' |
157 | do_reset && | |
158 | git apply -p0 --index B0.diff && | |
159 | test -z "$(git diff --name-status binary -- file3)" | |
160 | ' | |
161 | ||
46d723ce JK |
162 | test_expect_success 'reject truncated binary diff' ' |
163 | do_reset && | |
164 | ||
165 | # this length is calculated to get us very close to | |
166 | # the 8192-byte strbuf we will use to read in the patch. | |
167 | test-tool genrandom foo 6205 >file1 && | |
168 | git diff --binary >patch && | |
169 | ||
170 | # truncate the patch at the second "literal" line, | |
171 | # but exclude the trailing newline. We must use perl | |
172 | # for this, since tools like "sed" cannot reliably | |
173 | # produce output without the trailing newline. | |
174 | perl -pe " | |
175 | if (/^literal/ && \$count++ >= 1) { | |
176 | chomp; | |
177 | print; | |
178 | exit 0; | |
179 | } | |
180 | " <patch >patch.trunc && | |
181 | ||
182 | do_reset && | |
183 | test_must_fail git apply patch.trunc | |
184 | ' | |
27dedf0c | 185 | test_done |