Junio C Hamano | 42d0ee8 | 2006-05-06 00:15:54 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2006 Junio C Hamano |
| 4 | # |
| 5 | |
| 6 | test_description='Binary diff and apply |
| 7 | ' |
| 8 | |
Ævar Arnfjörð Bjarmason | 16d4bd4 | 2021-10-31 00:24:19 +0200 | [diff] [blame] | 9 | TEST_PASSES_SANITIZE_LEAK=true |
Junio C Hamano | 42d0ee8 | 2006-05-06 00:15:54 -0700 | [diff] [blame] | 10 | . ./test-lib.sh |
| 11 | |
Jonathan Nieder | 2983c0e | 2012-03-13 00:02:19 -0500 | [diff] [blame] | 12 | cat >expect.binary-numstat <<\EOF |
| 13 | 1 1 a |
| 14 | - - b |
| 15 | 1 1 c |
| 16 | - - d |
| 17 | EOF |
| 18 | |
Alexander Strasser | 1358bb4 | 2012-07-12 00:12:03 +0200 | [diff] [blame] | 19 | test_expect_success 'prepare repository' ' |
Alexander Strasser | 3e9cdf7 | 2012-07-16 22:45:40 +0200 | [diff] [blame] | 20 | echo AIT >a && echo BIT >b && echo CIT >c && echo DIT >d && |
| 21 | git update-index --add a b c d && |
| 22 | echo git >a && |
| 23 | cat "$TEST_DIRECTORY"/test-binary-1.png >b && |
| 24 | echo git >c && |
| 25 | cat b b >d |
Alexander Strasser | 1358bb4 | 2012-07-12 00:12:03 +0200 | [diff] [blame] | 26 | ' |
Junio C Hamano | 42d0ee8 | 2006-05-06 00:15:54 -0700 | [diff] [blame] | 27 | |
Eric Wong | c7053aa | 2006-05-25 19:06:16 -0700 | [diff] [blame] | 28 | cat > expected <<\EOF |
Junio C Hamano | 42d0ee8 | 2006-05-06 00:15:54 -0700 | [diff] [blame] | 29 | a | 2 +- |
| 30 | b | Bin |
| 31 | c | 2 +- |
| 32 | d | Bin |
| 33 | 4 files changed, 2 insertions(+), 2 deletions(-) |
Eric Wong | c7053aa | 2006-05-25 19:06:16 -0700 | [diff] [blame] | 34 | EOF |
Alexander Strasser | bb84e67 | 2012-07-12 00:12:21 +0200 | [diff] [blame] | 35 | test_expect_success 'apply --stat output for binary file change' ' |
Jonathan Nieder | 2983c0e | 2012-03-13 00:02:19 -0500 | [diff] [blame] | 36 | git diff >diff && |
| 37 | git apply --stat --summary <diff >current && |
Ævar Arnfjörð Bjarmason | 1108cea | 2021-02-11 02:53:53 +0100 | [diff] [blame] | 38 | test_cmp expected current |
Jonathan Nieder | 2983c0e | 2012-03-13 00:02:19 -0500 | [diff] [blame] | 39 | ' |
Junio C Hamano | 42d0ee8 | 2006-05-06 00:15:54 -0700 | [diff] [blame] | 40 | |
Alexander Strasser | de9658b | 2012-06-15 23:50:30 +0200 | [diff] [blame] | 41 | test_expect_success 'diff --shortstat output for binary file change' ' |
Alexander Strasser | 216f25f | 2012-07-16 22:45:10 +0200 | [diff] [blame] | 42 | tail -n 1 expected >expect && |
Alexander Strasser | de9658b | 2012-06-15 23:50:30 +0200 | [diff] [blame] | 43 | git diff --shortstat >current && |
Ævar Arnfjörð Bjarmason | 1108cea | 2021-02-11 02:53:53 +0100 | [diff] [blame] | 44 | test_cmp expect current |
Alexander Strasser | de9658b | 2012-06-15 23:50:30 +0200 | [diff] [blame] | 45 | ' |
| 46 | |
| 47 | test_expect_success 'diff --shortstat output for binary file change only' ' |
| 48 | echo " 1 file changed, 0 insertions(+), 0 deletions(-)" >expected && |
| 49 | git diff --shortstat -- b >current && |
Ævar Arnfjörð Bjarmason | 1108cea | 2021-02-11 02:53:53 +0100 | [diff] [blame] | 50 | test_cmp expected current |
Alexander Strasser | de9658b | 2012-06-15 23:50:30 +0200 | [diff] [blame] | 51 | ' |
| 52 | |
Jonathan Nieder | 2983c0e | 2012-03-13 00:02:19 -0500 | [diff] [blame] | 53 | test_expect_success 'apply --numstat notices binary file change' ' |
| 54 | git diff >diff && |
| 55 | git apply --numstat <diff >current && |
| 56 | test_cmp expect.binary-numstat current |
| 57 | ' |
| 58 | |
| 59 | test_expect_success 'apply --numstat understands diff --binary format' ' |
| 60 | git diff --binary >diff && |
| 61 | git apply --numstat <diff >current && |
| 62 | test_cmp expect.binary-numstat current |
| 63 | ' |
Junio C Hamano | 42d0ee8 | 2006-05-06 00:15:54 -0700 | [diff] [blame] | 64 | |
| 65 | # apply needs to be able to skip the binary material correctly |
| 66 | # in order to report the line number of a corrupt patch. |
Ævar Arnfjörð Bjarmason | a926c4b | 2021-02-11 02:53:51 +0100 | [diff] [blame] | 67 | test_expect_success 'apply detecting corrupt patch correctly' ' |
Alexander Strasser | 3e9cdf7 | 2012-07-16 22:45:40 +0200 | [diff] [blame] | 68 | git diff >output && |
| 69 | sed -e "s/-CIT/xCIT/" <output >broken && |
Alexander Strasser | c7c0a25 | 2012-07-16 22:47:22 +0200 | [diff] [blame] | 70 | test_must_fail git apply --stat --summary broken 2>detected && |
Elia Pinto | e6ce6f4 | 2014-04-30 09:22:58 -0700 | [diff] [blame] | 71 | detected=$(cat detected) && |
Christian Couder | dae197f | 2016-08-08 23:03:04 +0200 | [diff] [blame] | 72 | detected=$(expr "$detected" : "error.*at line \\([0-9]*\\)\$") && |
Elia Pinto | e6ce6f4 | 2014-04-30 09:22:58 -0700 | [diff] [blame] | 73 | detected=$(sed -ne "${detected}p" broken) && |
Alexander Strasser | 3e9cdf7 | 2012-07-16 22:45:40 +0200 | [diff] [blame] | 74 | test "$detected" = xCIT |
Alexander Strasser | 1358bb4 | 2012-07-12 00:12:03 +0200 | [diff] [blame] | 75 | ' |
Junio C Hamano | 42d0ee8 | 2006-05-06 00:15:54 -0700 | [diff] [blame] | 76 | |
Ævar Arnfjörð Bjarmason | a926c4b | 2021-02-11 02:53:51 +0100 | [diff] [blame] | 77 | test_expect_success 'apply detecting corrupt patch correctly' ' |
Alexander Strasser | 3e9cdf7 | 2012-07-16 22:45:40 +0200 | [diff] [blame] | 78 | git diff --binary | sed -e "s/-CIT/xCIT/" >broken && |
Alexander Strasser | c7c0a25 | 2012-07-16 22:47:22 +0200 | [diff] [blame] | 79 | test_must_fail git apply --stat --summary broken 2>detected && |
Elia Pinto | e6ce6f4 | 2014-04-30 09:22:58 -0700 | [diff] [blame] | 80 | detected=$(cat detected) && |
Christian Couder | dae197f | 2016-08-08 23:03:04 +0200 | [diff] [blame] | 81 | detected=$(expr "$detected" : "error.*at line \\([0-9]*\\)\$") && |
Elia Pinto | e6ce6f4 | 2014-04-30 09:22:58 -0700 | [diff] [blame] | 82 | detected=$(sed -ne "${detected}p" broken) && |
Alexander Strasser | 3e9cdf7 | 2012-07-16 22:45:40 +0200 | [diff] [blame] | 83 | test "$detected" = xCIT |
Alexander Strasser | 1358bb4 | 2012-07-12 00:12:03 +0200 | [diff] [blame] | 84 | ' |
Junio C Hamano | 42d0ee8 | 2006-05-06 00:15:54 -0700 | [diff] [blame] | 85 | |
Nanako Shiraishi | 3604e7c | 2008-09-03 17:59:29 +0900 | [diff] [blame] | 86 | test_expect_success 'initial commit' 'git commit -a -m initial' |
Junio C Hamano | 42d0ee8 | 2006-05-06 00:15:54 -0700 | [diff] [blame] | 87 | |
| 88 | # Try removal (b), modification (d), and creation (e). |
Alexander Strasser | 1358bb4 | 2012-07-12 00:12:03 +0200 | [diff] [blame] | 89 | test_expect_success 'diff-index with --binary' ' |
Alexander Strasser | 3e9cdf7 | 2012-07-16 22:45:40 +0200 | [diff] [blame] | 90 | echo AIT >a && mv b e && echo CIT >c && cat e >d && |
| 91 | git update-index --add --remove a b c d e && |
Elia Pinto | e6ce6f4 | 2014-04-30 09:22:58 -0700 | [diff] [blame] | 92 | tree0=$(git write-tree) && |
Alexander Strasser | 3e9cdf7 | 2012-07-16 22:45:40 +0200 | [diff] [blame] | 93 | git diff --cached --binary >current && |
| 94 | git apply --stat --summary current |
Alexander Strasser | 1358bb4 | 2012-07-12 00:12:03 +0200 | [diff] [blame] | 95 | ' |
Junio C Hamano | 42d0ee8 | 2006-05-06 00:15:54 -0700 | [diff] [blame] | 96 | |
Alexander Strasser | 1358bb4 | 2012-07-12 00:12:03 +0200 | [diff] [blame] | 97 | test_expect_success 'apply binary patch' ' |
Alexander Strasser | 3e9cdf7 | 2012-07-16 22:45:40 +0200 | [diff] [blame] | 98 | git reset --hard && |
| 99 | git apply --binary --index <current && |
Elia Pinto | e6ce6f4 | 2014-04-30 09:22:58 -0700 | [diff] [blame] | 100 | tree1=$(git write-tree) && |
Alexander Strasser | 3e9cdf7 | 2012-07-16 22:45:40 +0200 | [diff] [blame] | 101 | test "$tree1" = "$tree0" |
Alexander Strasser | 1358bb4 | 2012-07-12 00:12:03 +0200 | [diff] [blame] | 102 | ' |
Junio C Hamano | 42d0ee8 | 2006-05-06 00:15:54 -0700 | [diff] [blame] | 103 | |
Linus Torvalds | 71b989e | 2008-10-05 15:35:15 -0400 | [diff] [blame] | 104 | test_expect_success 'diff --no-index with binary creation' ' |
| 105 | echo Q | q_to_nul >binary && |
Eric Sunshine | 8327974 | 2018-07-01 20:23:41 -0400 | [diff] [blame] | 106 | # hide error code from diff, which just indicates differences |
| 107 | test_might_fail git diff --binary --no-index /dev/null binary >current && |
Linus Torvalds | 71b989e | 2008-10-05 15:35:15 -0400 | [diff] [blame] | 108 | rm binary && |
| 109 | git apply --binary <current && |
| 110 | echo Q >expected && |
| 111 | nul_to_q <binary >actual && |
| 112 | test_cmp expected actual |
| 113 | ' |
| 114 | |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 115 | cat >expect <<EOF |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 116 | binfilë | Bin 0 -> 1026 bytes |
| 117 | tëxtfilë | 10000 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 118 | EOF |
| 119 | |
| 120 | test_expect_success 'diff --stat with binary files and big change count' ' |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 121 | printf "\01\00%1024d" 1 >binfilë && |
| 122 | git add binfilë && |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 123 | i=0 && |
| 124 | while test $i -lt 10000; do |
| 125 | echo $i && |
Eric Sunshine | cbe1d9d | 2021-12-09 00:11:13 -0500 | [diff] [blame] | 126 | i=$(($i + 1)) || return 1 |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 127 | done >tëxtfilë && |
| 128 | git add tëxtfilë && |
| 129 | git -c core.quotepath=false diff --cached --stat binfilë tëxtfilë >output && |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 130 | grep " | " output >actual && |
| 131 | test_cmp expect actual |
| 132 | ' |
| 133 | |
Junio C Hamano | 42d0ee8 | 2006-05-06 00:15:54 -0700 | [diff] [blame] | 134 | test_done |