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