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