3 # Copyright (c) 2005 Junio C Hamano
6 test_description
='git apply handling binary patches
9 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
10 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
12 TEST_PASSES_SANITIZE_LEAK
=true
15 test_expect_success
'setup' '
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.
26 git update-index --add --remove file1 file2 file4 &&
27 git commit -m "Initial Version" 2>/dev/null &&
29 git checkout -b binary &&
30 perl -pe "y/x/\000/" <file1 >file3 &&
33 perl -pe "y/\000/v/" <file3 >file1 &&
35 git update-index --add --remove file1 file2 file3 file4 &&
36 git commit -m "Second Version" &&
38 git diff-tree -p main binary >B.diff &&
39 git diff-tree -p -C main binary >C.diff &&
41 git diff-tree -p --binary main binary >BF.diff &&
42 git diff-tree -p --binary -C main binary >CF.diff &&
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 &&
47 git diff-tree -p --binary --no-prefix main binary -- file3 >B0.diff &&
49 git init other-repo &&
53 git reset --hard FETCH_HEAD
57 test_expect_success
'stat binary diff -- should not fail.' \
59 git apply --stat --summary B.diff'
61 test_expect_success
'stat binary -p0 diff -- should not fail.' '
63 git apply --stat -p0 B0.diff
66 test_expect_success
'stat binary diff (copy) -- should not fail.' \
68 git apply --stat --summary C.diff'
70 test_expect_success
'check binary diff -- should fail.' \
72 test_must_fail git apply --check B.diff'
74 test_expect_success
'check binary diff (copy) -- should fail.' \
76 test_must_fail git apply --check C.diff'
79 'check incomplete binary diff with replacement -- should fail.' '
81 test_must_fail git apply --check --allow-binary-replacement B.diff
85 'check incomplete binary diff with replacement (copy) -- should fail.' '
87 test_must_fail git apply --check --allow-binary-replacement C.diff
90 test_expect_success
'check binary diff with replacement.' \
92 git apply --check --allow-binary-replacement BF.diff'
94 test_expect_success
'check binary diff with replacement (copy).' \
96 git apply --check --allow-binary-replacement CF.diff'
98 # Now we start applying them.
106 test_expect_success
'apply binary diff -- should fail.' \
108 test_must_fail git apply B.diff'
110 test_expect_success
'apply binary diff -- should fail.' \
112 test_must_fail git apply --index B.diff'
114 test_expect_success
'apply binary diff (copy) -- should fail.' \
116 test_must_fail git apply C.diff'
118 test_expect_success
'apply binary diff (copy) -- should fail.' \
120 test_must_fail git apply --index C.diff'
122 test_expect_success
'apply binary diff with full-index' '
124 git apply B-index.diff
127 test_expect_success
'apply binary diff with full-index (copy)' '
129 git apply C-index.diff
132 test_expect_success
'apply full-index binary diff in new repo' '
135 test_must_fail git apply ../B-index.diff)
138 test_expect_success
'apply binary diff without replacement.' \
142 test_expect_success
'apply binary diff without replacement (copy).' \
146 test_expect_success
'apply binary diff.' \
148 git apply --allow-binary-replacement --index BF.diff &&
149 test -z "$(git diff --name-status binary)"'
151 test_expect_success
'apply binary diff (copy).' \
153 git apply --allow-binary-replacement --index CF.diff &&
154 test -z "$(git diff --name-status binary)"'
156 test_expect_success
'apply binary -p0 diff' '
158 git apply -p0 --index B0.diff &&
159 test -z "$(git diff --name-status binary -- file3)"
162 test_expect_success
'reject truncated binary diff' '
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 &&
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.
175 if (/^literal/ && \$count++ >= 1) {
180 " <patch >patch.trunc &&
183 test_must_fail git apply patch.trunc