Git 2.45
[git/gitster.git] / t / t4103-apply-binary.sh
blob144619ab873f5d55ff52e5324d15ab9921941856
1 #!/bin/sh
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
13 . ./test-lib.sh
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 &&
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 &&
31 cat file3 >file4 &&
32 git add file2 &&
33 perl -pe "y/\000/v/" <file3 >file1 &&
34 rm -f file2 &&
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 &&
51 cd other-repo &&
52 git fetch .. main &&
53 git reset --hard FETCH_HEAD
57 test_expect_success 'stat binary diff -- should not fail.' \
58 'git checkout main &&
59 git apply --stat --summary B.diff'
61 test_expect_success 'stat binary -p0 diff -- should not fail.' '
62 git checkout main &&
63 git apply --stat -p0 B0.diff
66 test_expect_success 'stat binary diff (copy) -- should not fail.' \
67 'git checkout main &&
68 git apply --stat --summary C.diff'
70 test_expect_success 'check binary diff -- should fail.' \
71 'git checkout main &&
72 test_must_fail git apply --check B.diff'
74 test_expect_success 'check binary diff (copy) -- should fail.' \
75 'git checkout main &&
76 test_must_fail git apply --check C.diff'
78 test_expect_success \
79 'check incomplete binary diff with replacement -- should fail.' '
80 git checkout main &&
81 test_must_fail git apply --check --allow-binary-replacement B.diff
84 test_expect_success \
85 'check incomplete binary diff with replacement (copy) -- should fail.' '
86 git checkout main &&
87 test_must_fail git apply --check --allow-binary-replacement C.diff
90 test_expect_success 'check binary diff with replacement.' \
91 'git checkout main &&
92 git apply --check --allow-binary-replacement BF.diff'
94 test_expect_success 'check binary diff with replacement (copy).' \
95 'git checkout main &&
96 git apply --check --allow-binary-replacement CF.diff'
98 # Now we start applying them.
100 do_reset () {
101 rm -f file? &&
102 git reset --hard &&
103 git checkout -f main
106 test_expect_success 'apply binary diff -- should fail.' \
107 'do_reset &&
108 test_must_fail git apply B.diff'
110 test_expect_success 'apply binary diff -- should fail.' \
111 'do_reset &&
112 test_must_fail git apply --index B.diff'
114 test_expect_success 'apply binary diff (copy) -- should fail.' \
115 'do_reset &&
116 test_must_fail git apply C.diff'
118 test_expect_success 'apply binary diff (copy) -- should fail.' \
119 'do_reset &&
120 test_must_fail git apply --index C.diff'
122 test_expect_success 'apply binary diff with full-index' '
123 do_reset &&
124 git apply B-index.diff
127 test_expect_success 'apply binary diff with full-index (copy)' '
128 do_reset &&
129 git apply C-index.diff
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)
138 test_expect_success 'apply binary diff without replacement.' \
139 'do_reset &&
140 git apply BF.diff'
142 test_expect_success 'apply binary diff without replacement (copy).' \
143 'do_reset &&
144 git apply CF.diff'
146 test_expect_success 'apply binary diff.' \
147 'do_reset &&
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).' \
152 'do_reset &&
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' '
157 do_reset &&
158 git apply -p0 --index B0.diff &&
159 test -z "$(git diff --name-status binary -- file3)"
162 test_expect_success 'reject truncated binary diff' '
163 do_reset &&
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.
174 perl -pe "
175 if (/^literal/ && \$count++ >= 1) {
176 chomp;
177 print;
178 exit 0;
180 " <patch >patch.trunc &&
182 do_reset &&
183 test_must_fail git apply patch.trunc
185 test_done