René Scharfe | aca20dd | 2010-05-22 23:26:39 +0200 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='git grep in binary files' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
| 7 | test_expect_success 'setup' " |
Brandon Casey | f985487 | 2010-09-09 14:15:57 -0500 | [diff] [blame] | 8 | echo 'binaryQfile' | q_to_nul >a && |
René Scharfe | aca20dd | 2010-05-22 23:26:39 +0200 | [diff] [blame] | 9 | git add a && |
| 10 | git commit -m. |
| 11 | " |
| 12 | |
| 13 | test_expect_success 'git grep ina a' ' |
| 14 | echo Binary file a matches >expect && |
| 15 | git grep ina a >actual && |
| 16 | test_cmp expect actual |
| 17 | ' |
| 18 | |
| 19 | test_expect_success 'git grep -ah ina a' ' |
| 20 | git grep -ah ina a >actual && |
| 21 | test_cmp a actual |
| 22 | ' |
| 23 | |
| 24 | test_expect_success 'git grep -I ina a' ' |
| 25 | : >expect && |
| 26 | test_must_fail git grep -I ina a >actual && |
| 27 | test_cmp expect actual |
| 28 | ' |
| 29 | |
René Scharfe | c30c10c | 2010-05-22 23:29:35 +0200 | [diff] [blame] | 30 | test_expect_success 'git grep -c ina a' ' |
| 31 | echo a:1 >expect && |
| 32 | git grep -c ina a >actual && |
| 33 | test_cmp expect actual |
| 34 | ' |
| 35 | |
René Scharfe | 321ffcc | 2010-05-22 23:30:48 +0200 | [diff] [blame] | 36 | test_expect_success 'git grep -l ina a' ' |
| 37 | echo a >expect && |
| 38 | git grep -l ina a >actual && |
| 39 | test_cmp expect actual |
| 40 | ' |
| 41 | |
René Scharfe | aca20dd | 2010-05-22 23:26:39 +0200 | [diff] [blame] | 42 | test_expect_success 'git grep -L bar a' ' |
| 43 | echo a >expect && |
| 44 | git grep -L bar a >actual && |
| 45 | test_cmp expect actual |
| 46 | ' |
| 47 | |
| 48 | test_expect_success 'git grep -q ina a' ' |
| 49 | : >expect && |
| 50 | git grep -q ina a >actual && |
| 51 | test_cmp expect actual |
| 52 | ' |
| 53 | |
René Scharfe | 1baddf4 | 2010-05-22 23:32:43 +0200 | [diff] [blame] | 54 | test_expect_success 'git grep -F ile a' ' |
| 55 | git grep -F ile a |
| 56 | ' |
| 57 | |
René Scharfe | 52d799a | 2010-05-22 23:34:06 +0200 | [diff] [blame] | 58 | test_expect_success 'git grep -Fi iLE a' ' |
| 59 | git grep -Fi iLE a |
| 60 | ' |
| 61 | |
René Scharfe | f96e567 | 2010-05-22 23:35:07 +0200 | [diff] [blame] | 62 | # This test actually passes on platforms where regexec() supports the |
| 63 | # flag REG_STARTEND. |
Ævar Arnfjörð Bjarmason | 7e36de5 | 2010-08-17 09:24:41 +0000 | [diff] [blame] | 64 | test_expect_success 'git grep ile a' ' |
René Scharfe | f96e567 | 2010-05-22 23:35:07 +0200 | [diff] [blame] | 65 | git grep ile a |
| 66 | ' |
| 67 | |
| 68 | test_expect_failure 'git grep .fi a' ' |
| 69 | git grep .fi a |
| 70 | ' |
| 71 | |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 72 | test_expect_success 'git grep -F y<NUL>f a' " |
Brandon Casey | f985487 | 2010-09-09 14:15:57 -0500 | [diff] [blame] | 73 | printf 'yQf' | q_to_nul >f && |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 74 | git grep -f f -F a |
| 75 | " |
| 76 | |
| 77 | test_expect_success 'git grep -F y<NUL>x a' " |
Brandon Casey | f985487 | 2010-09-09 14:15:57 -0500 | [diff] [blame] | 78 | printf 'yQx' | q_to_nul >f && |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 79 | test_must_fail git grep -f f -F a |
| 80 | " |
| 81 | |
| 82 | test_expect_success 'git grep -Fi Y<NUL>f a' " |
Brandon Casey | f985487 | 2010-09-09 14:15:57 -0500 | [diff] [blame] | 83 | printf 'YQf' | q_to_nul >f && |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 84 | git grep -f f -Fi a |
| 85 | " |
| 86 | |
Fredrik Kuivinen | 9ecedde | 2011-08-21 00:42:18 +0200 | [diff] [blame] | 87 | test_expect_success 'git grep -Fi Y<NUL>x a' " |
Brandon Casey | f985487 | 2010-09-09 14:15:57 -0500 | [diff] [blame] | 88 | printf 'YQx' | q_to_nul >f && |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 89 | test_must_fail git grep -f f -Fi a |
| 90 | " |
| 91 | |
| 92 | test_expect_success 'git grep y<NUL>f a' " |
Brandon Casey | f985487 | 2010-09-09 14:15:57 -0500 | [diff] [blame] | 93 | printf 'yQf' | q_to_nul >f && |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 94 | git grep -f f a |
| 95 | " |
| 96 | |
Fredrik Kuivinen | 9ecedde | 2011-08-21 00:42:18 +0200 | [diff] [blame] | 97 | test_expect_success 'git grep y<NUL>x a' " |
Brandon Casey | f985487 | 2010-09-09 14:15:57 -0500 | [diff] [blame] | 98 | printf 'yQx' | q_to_nul >f && |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 99 | test_must_fail git grep -f f a |
| 100 | " |
| 101 | |
Jeff King | 41b59bf | 2012-02-02 03:21:02 -0500 | [diff] [blame] | 102 | test_expect_success 'grep respects binary diff attribute' ' |
| 103 | echo text >t && |
| 104 | git add t && |
| 105 | echo t:text >expect && |
| 106 | git grep text t >actual && |
| 107 | test_cmp expect actual && |
| 108 | echo "t -diff" >.gitattributes && |
| 109 | echo "Binary file t matches" >expect && |
| 110 | git grep text t >actual && |
| 111 | test_cmp expect actual |
| 112 | ' |
| 113 | |
Nguyễn Thái Ngọc Duy | 55c6168 | 2012-10-12 17:49:38 +0700 | [diff] [blame] | 114 | test_expect_success 'grep --cached respects binary diff attribute' ' |
| 115 | git grep --cached text t >actual && |
| 116 | test_cmp expect actual |
| 117 | ' |
| 118 | |
| 119 | test_expect_success 'grep --cached respects binary diff attribute (2)' ' |
| 120 | git add .gitattributes && |
| 121 | rm .gitattributes && |
| 122 | git grep --cached text t >actual && |
| 123 | test_when_finished "git rm --cached .gitattributes" && |
| 124 | test_when_finished "git checkout .gitattributes" && |
| 125 | test_cmp expect actual |
| 126 | ' |
| 127 | |
| 128 | test_expect_success 'grep revision respects binary diff attribute' ' |
| 129 | git commit -m new && |
| 130 | echo "Binary file HEAD:t matches" >expect && |
| 131 | git grep text HEAD -- t >actual && |
| 132 | test_when_finished "git reset HEAD^" && |
| 133 | test_cmp expect actual |
| 134 | ' |
| 135 | |
Jeff King | 41b59bf | 2012-02-02 03:21:02 -0500 | [diff] [blame] | 136 | test_expect_success 'grep respects not-binary diff attribute' ' |
| 137 | echo binQary | q_to_nul >b && |
| 138 | git add b && |
| 139 | echo "Binary file b matches" >expect && |
| 140 | git grep bin b >actual && |
| 141 | test_cmp expect actual && |
| 142 | echo "b diff" >.gitattributes && |
| 143 | echo "b:binQary" >expect && |
Lars Schneider | a0578e0 | 2016-02-17 09:57:46 +0100 | [diff] [blame^] | 144 | git grep bin b >actual.raw && |
| 145 | nul_to_q <actual.raw >actual && |
Jeff King | 41b59bf | 2012-02-02 03:21:02 -0500 | [diff] [blame] | 146 | test_cmp expect actual |
| 147 | ' |
| 148 | |
Michael J Gruber | 97f6a9c | 2013-05-10 17:10:14 +0200 | [diff] [blame] | 149 | cat >nul_to_q_textconv <<'EOF' |
| 150 | #!/bin/sh |
| 151 | "$PERL_PATH" -pe 'y/\000/Q/' < "$1" |
| 152 | EOF |
| 153 | chmod +x nul_to_q_textconv |
| 154 | |
| 155 | test_expect_success 'setup textconv filters' ' |
| 156 | echo a diff=foo >.gitattributes && |
| 157 | git config diff.foo.textconv "\"$(pwd)\""/nul_to_q_textconv |
| 158 | ' |
| 159 | |
| 160 | test_expect_success 'grep does not honor textconv' ' |
| 161 | test_must_fail git grep Qfile |
| 162 | ' |
| 163 | |
Jeff King | 335ec3b | 2013-05-10 17:10:15 +0200 | [diff] [blame] | 164 | test_expect_success 'grep --textconv honors textconv' ' |
Michael J Gruber | 97f6a9c | 2013-05-10 17:10:14 +0200 | [diff] [blame] | 165 | echo "a:binaryQfile" >expect && |
| 166 | git grep --textconv Qfile >actual && |
| 167 | test_cmp expect actual |
| 168 | ' |
| 169 | |
| 170 | test_expect_success 'grep --no-textconv does not honor textconv' ' |
| 171 | test_must_fail git grep --no-textconv Qfile |
| 172 | ' |
| 173 | |
Michael J Gruber | afa15f3 | 2013-05-10 17:10:16 +0200 | [diff] [blame] | 174 | test_expect_success 'grep --textconv blob honors textconv' ' |
Michael J Gruber | 97f6a9c | 2013-05-10 17:10:14 +0200 | [diff] [blame] | 175 | echo "HEAD:a:binaryQfile" >expect && |
| 176 | git grep --textconv Qfile HEAD:a >actual && |
| 177 | test_cmp expect actual |
| 178 | ' |
| 179 | |
René Scharfe | aca20dd | 2010-05-22 23:26:39 +0200 | [diff] [blame] | 180 | test_done |