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 | |
Ævar Arnfjörð Bjarmason | 77f6f44 | 2017-05-20 21:42:15 +0000 | [diff] [blame] | 7 | nul_match () { |
| 8 | matches=$1 |
| 9 | flags=$2 |
| 10 | pattern=$3 |
| 11 | pattern_human=$(echo "$pattern" | sed 's/Q/<NUL>/g') |
| 12 | |
| 13 | if test "$matches" = 1 |
| 14 | then |
| 15 | test_expect_success "git grep -f f $flags '$pattern_human' a" " |
| 16 | printf '$pattern' | q_to_nul >f && |
| 17 | git grep -f f $flags a |
| 18 | " |
| 19 | elif test "$matches" = 0 |
| 20 | then |
| 21 | test_expect_success "git grep -f f $flags '$pattern_human' a" " |
| 22 | printf '$pattern' | q_to_nul >f && |
| 23 | test_must_fail git grep -f f $flags a |
| 24 | " |
Ævar Arnfjörð Bjarmason | 966be95 | 2017-05-20 21:42:17 +0000 | [diff] [blame] | 25 | elif test "$matches" = T1 |
| 26 | then |
| 27 | test_expect_failure "git grep -f f $flags '$pattern_human' a" " |
| 28 | printf '$pattern' | q_to_nul >f && |
| 29 | git grep -f f $flags a |
| 30 | " |
| 31 | elif test "$matches" = T0 |
| 32 | then |
| 33 | test_expect_failure "git grep -f f $flags '$pattern_human' a" " |
| 34 | printf '$pattern' | q_to_nul >f && |
| 35 | test_must_fail git grep -f f $flags a |
| 36 | " |
Ævar Arnfjörð Bjarmason | 77f6f44 | 2017-05-20 21:42:15 +0000 | [diff] [blame] | 37 | else |
| 38 | test_expect_success "PANIC: Test framework error. Unknown matches value $matches" 'false' |
| 39 | fi |
| 40 | } |
| 41 | |
René Scharfe | aca20dd | 2010-05-22 23:26:39 +0200 | [diff] [blame] | 42 | test_expect_success 'setup' " |
Ævar Arnfjörð Bjarmason | 12fc32f | 2017-05-20 21:42:16 +0000 | [diff] [blame] | 43 | echo 'binaryQfileQm[*]cQ*æQð' | q_to_nul >a && |
René Scharfe | aca20dd | 2010-05-22 23:26:39 +0200 | [diff] [blame] | 44 | git add a && |
| 45 | git commit -m. |
| 46 | " |
| 47 | |
| 48 | test_expect_success 'git grep ina a' ' |
| 49 | echo Binary file a matches >expect && |
| 50 | git grep ina a >actual && |
| 51 | test_cmp expect actual |
| 52 | ' |
| 53 | |
| 54 | test_expect_success 'git grep -ah ina a' ' |
| 55 | git grep -ah ina a >actual && |
| 56 | test_cmp a actual |
| 57 | ' |
| 58 | |
| 59 | test_expect_success 'git grep -I ina a' ' |
René Scharfe | aca20dd | 2010-05-22 23:26:39 +0200 | [diff] [blame] | 60 | test_must_fail git grep -I ina a >actual && |
SZEDER Gábor | 1c5e94f | 2018-08-19 23:57:25 +0200 | [diff] [blame] | 61 | test_must_be_empty actual |
René Scharfe | aca20dd | 2010-05-22 23:26:39 +0200 | [diff] [blame] | 62 | ' |
| 63 | |
René Scharfe | c30c10c | 2010-05-22 23:29:35 +0200 | [diff] [blame] | 64 | test_expect_success 'git grep -c ina a' ' |
| 65 | echo a:1 >expect && |
| 66 | git grep -c ina a >actual && |
| 67 | test_cmp expect actual |
| 68 | ' |
| 69 | |
René Scharfe | 321ffcc | 2010-05-22 23:30:48 +0200 | [diff] [blame] | 70 | test_expect_success 'git grep -l ina a' ' |
| 71 | echo a >expect && |
| 72 | git grep -l ina a >actual && |
| 73 | test_cmp expect actual |
| 74 | ' |
| 75 | |
René Scharfe | aca20dd | 2010-05-22 23:26:39 +0200 | [diff] [blame] | 76 | test_expect_success 'git grep -L bar a' ' |
| 77 | echo a >expect && |
| 78 | git grep -L bar a >actual && |
| 79 | test_cmp expect actual |
| 80 | ' |
| 81 | |
| 82 | test_expect_success 'git grep -q ina a' ' |
René Scharfe | aca20dd | 2010-05-22 23:26:39 +0200 | [diff] [blame] | 83 | git grep -q ina a >actual && |
SZEDER Gábor | 1c5e94f | 2018-08-19 23:57:25 +0200 | [diff] [blame] | 84 | test_must_be_empty actual |
René Scharfe | aca20dd | 2010-05-22 23:26:39 +0200 | [diff] [blame] | 85 | ' |
| 86 | |
René Scharfe | 1baddf4 | 2010-05-22 23:32:43 +0200 | [diff] [blame] | 87 | test_expect_success 'git grep -F ile a' ' |
| 88 | git grep -F ile a |
| 89 | ' |
| 90 | |
René Scharfe | 52d799a | 2010-05-22 23:34:06 +0200 | [diff] [blame] | 91 | test_expect_success 'git grep -Fi iLE a' ' |
| 92 | git grep -Fi iLE a |
| 93 | ' |
| 94 | |
René Scharfe | f96e567 | 2010-05-22 23:35:07 +0200 | [diff] [blame] | 95 | # This test actually passes on platforms where regexec() supports the |
| 96 | # flag REG_STARTEND. |
Ævar Arnfjörð Bjarmason | 7e36de5 | 2010-08-17 09:24:41 +0000 | [diff] [blame] | 97 | test_expect_success 'git grep ile a' ' |
René Scharfe | f96e567 | 2010-05-22 23:35:07 +0200 | [diff] [blame] | 98 | git grep ile a |
| 99 | ' |
| 100 | |
| 101 | test_expect_failure 'git grep .fi a' ' |
| 102 | git grep .fi a |
| 103 | ' |
| 104 | |
Ævar Arnfjörð Bjarmason | 77f6f44 | 2017-05-20 21:42:15 +0000 | [diff] [blame] | 105 | nul_match 1 '-F' 'yQf' |
| 106 | nul_match 0 '-F' 'yQx' |
| 107 | nul_match 1 '-Fi' 'YQf' |
| 108 | nul_match 0 '-Fi' 'YQx' |
| 109 | nul_match 1 '' 'yQf' |
| 110 | nul_match 0 '' 'yQx' |
Ævar Arnfjörð Bjarmason | 966be95 | 2017-05-20 21:42:17 +0000 | [diff] [blame] | 111 | nul_match 1 '' 'æQð' |
| 112 | nul_match 1 '-F' 'eQm[*]c' |
| 113 | nul_match 1 '-Fi' 'EQM[*]C' |
| 114 | |
| 115 | # Regex patterns that would match but shouldn't with -F |
| 116 | nul_match 0 '-F' 'yQ[f]' |
| 117 | nul_match 0 '-F' '[y]Qf' |
| 118 | nul_match 0 '-Fi' 'YQ[F]' |
| 119 | nul_match 0 '-Fi' '[Y]QF' |
| 120 | nul_match 0 '-F' 'æQ[ð]' |
| 121 | nul_match 0 '-F' '[æ]Qð' |
| 122 | nul_match 0 '-Fi' 'ÆQ[Ð]' |
| 123 | nul_match 0 '-Fi' '[Æ]QÐ' |
| 124 | |
| 125 | # kwset is disabled on -i & non-ASCII. No way to match non-ASCII \0 |
| 126 | # patterns case-insensitively. |
| 127 | nul_match T1 '-i' 'ÆQÐ' |
| 128 | |
| 129 | # \0 implicitly disables regexes. This is an undocumented internal |
| 130 | # limitation. |
| 131 | nul_match T1 '' 'yQ[f]' |
| 132 | nul_match T1 '' '[y]Qf' |
| 133 | nul_match T1 '-i' 'YQ[F]' |
| 134 | nul_match T1 '-i' '[Y]Qf' |
| 135 | nul_match T1 '' 'æQ[ð]' |
| 136 | nul_match T1 '' '[æ]Qð' |
| 137 | nul_match T1 '-i' 'ÆQ[Ð]' |
| 138 | |
| 139 | # ... because of \0 implicitly disabling regexes regexes that |
| 140 | # should/shouldn't match don't do the right thing. |
| 141 | nul_match T1 '' 'eQm.*cQ' |
| 142 | nul_match T1 '-i' 'EQM.*cQ' |
| 143 | nul_match T0 '' 'eQm[*]c' |
| 144 | nul_match T0 '-i' 'EQM[*]C' |
| 145 | |
| 146 | # Due to the REG_STARTEND extension when kwset() is disabled on -i & |
| 147 | # non-ASCII the string will be matched in its entirety, but the |
| 148 | # pattern will be cut off at the first \0. |
| 149 | nul_match 0 '-i' 'NOMATCHQð' |
| 150 | nul_match T0 '-i' '[Æ]QNOMATCH' |
| 151 | nul_match T0 '-i' '[æ]QNOMATCH' |
| 152 | # Matches, but for the wrong reasons, just stops at [æ] |
| 153 | nul_match 1 '-i' '[Æ]Qð' |
| 154 | nul_match 1 '-i' '[æ]Qð' |
| 155 | |
| 156 | # Ensure that the matcher doesn't regress to something that stops at |
| 157 | # \0 |
| 158 | nul_match 0 '-F' 'yQ[f]' |
| 159 | nul_match 0 '-Fi' 'YQ[F]' |
| 160 | nul_match 0 '' 'yQNOMATCH' |
| 161 | nul_match 0 '' 'QNOMATCH' |
| 162 | nul_match 0 '-i' 'YQNOMATCH' |
| 163 | nul_match 0 '-i' 'QNOMATCH' |
| 164 | nul_match 0 '-F' 'æQ[ð]' |
| 165 | nul_match 0 '-Fi' 'ÆQ[Ð]' |
| 166 | nul_match 0 '' 'yQNÓMATCH' |
| 167 | nul_match 0 '' 'QNÓMATCH' |
| 168 | nul_match 0 '-i' 'YQNÓMATCH' |
| 169 | nul_match 0 '-i' 'QNÓMATCH' |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 170 | |
Jeff King | 41b59bf | 2012-02-02 03:21:02 -0500 | [diff] [blame] | 171 | test_expect_success 'grep respects binary diff attribute' ' |
| 172 | echo text >t && |
| 173 | git add t && |
| 174 | echo t:text >expect && |
| 175 | git grep text t >actual && |
| 176 | test_cmp expect actual && |
| 177 | echo "t -diff" >.gitattributes && |
| 178 | echo "Binary file t matches" >expect && |
| 179 | git grep text t >actual && |
| 180 | test_cmp expect actual |
| 181 | ' |
| 182 | |
Nguyễn Thái Ngọc Duy | 55c6168 | 2012-10-12 17:49:38 +0700 | [diff] [blame] | 183 | test_expect_success 'grep --cached respects binary diff attribute' ' |
| 184 | git grep --cached text t >actual && |
| 185 | test_cmp expect actual |
| 186 | ' |
| 187 | |
| 188 | test_expect_success 'grep --cached respects binary diff attribute (2)' ' |
| 189 | git add .gitattributes && |
| 190 | rm .gitattributes && |
| 191 | git grep --cached text t >actual && |
| 192 | test_when_finished "git rm --cached .gitattributes" && |
| 193 | test_when_finished "git checkout .gitattributes" && |
| 194 | test_cmp expect actual |
| 195 | ' |
| 196 | |
| 197 | test_expect_success 'grep revision respects binary diff attribute' ' |
| 198 | git commit -m new && |
| 199 | echo "Binary file HEAD:t matches" >expect && |
| 200 | git grep text HEAD -- t >actual && |
| 201 | test_when_finished "git reset HEAD^" && |
| 202 | test_cmp expect actual |
| 203 | ' |
| 204 | |
Jeff King | 41b59bf | 2012-02-02 03:21:02 -0500 | [diff] [blame] | 205 | test_expect_success 'grep respects not-binary diff attribute' ' |
| 206 | echo binQary | q_to_nul >b && |
| 207 | git add b && |
| 208 | echo "Binary file b matches" >expect && |
| 209 | git grep bin b >actual && |
| 210 | test_cmp expect actual && |
| 211 | echo "b diff" >.gitattributes && |
| 212 | echo "b:binQary" >expect && |
Lars Schneider | a0578e0 | 2016-02-17 09:57:46 +0100 | [diff] [blame] | 213 | git grep bin b >actual.raw && |
| 214 | nul_to_q <actual.raw >actual && |
Jeff King | 41b59bf | 2012-02-02 03:21:02 -0500 | [diff] [blame] | 215 | test_cmp expect actual |
| 216 | ' |
| 217 | |
Michael J Gruber | 97f6a9c | 2013-05-10 17:10:14 +0200 | [diff] [blame] | 218 | cat >nul_to_q_textconv <<'EOF' |
| 219 | #!/bin/sh |
| 220 | "$PERL_PATH" -pe 'y/\000/Q/' < "$1" |
| 221 | EOF |
| 222 | chmod +x nul_to_q_textconv |
| 223 | |
| 224 | test_expect_success 'setup textconv filters' ' |
| 225 | echo a diff=foo >.gitattributes && |
| 226 | git config diff.foo.textconv "\"$(pwd)\""/nul_to_q_textconv |
| 227 | ' |
| 228 | |
| 229 | test_expect_success 'grep does not honor textconv' ' |
| 230 | test_must_fail git grep Qfile |
| 231 | ' |
| 232 | |
Jeff King | 335ec3b | 2013-05-10 17:10:15 +0200 | [diff] [blame] | 233 | test_expect_success 'grep --textconv honors textconv' ' |
Ævar Arnfjörð Bjarmason | 12fc32f | 2017-05-20 21:42:16 +0000 | [diff] [blame] | 234 | echo "a:binaryQfileQm[*]cQ*æQð" >expect && |
Michael J Gruber | 97f6a9c | 2013-05-10 17:10:14 +0200 | [diff] [blame] | 235 | git grep --textconv Qfile >actual && |
| 236 | test_cmp expect actual |
| 237 | ' |
| 238 | |
| 239 | test_expect_success 'grep --no-textconv does not honor textconv' ' |
| 240 | test_must_fail git grep --no-textconv Qfile |
| 241 | ' |
| 242 | |
Michael J Gruber | afa15f3 | 2013-05-10 17:10:16 +0200 | [diff] [blame] | 243 | test_expect_success 'grep --textconv blob honors textconv' ' |
Ævar Arnfjörð Bjarmason | 12fc32f | 2017-05-20 21:42:16 +0000 | [diff] [blame] | 244 | echo "HEAD:a:binaryQfileQm[*]cQ*æQð" >expect && |
Michael J Gruber | 97f6a9c | 2013-05-10 17:10:14 +0200 | [diff] [blame] | 245 | git grep --textconv Qfile HEAD:a >actual && |
| 246 | test_cmp expect actual |
| 247 | ' |
| 248 | |
René Scharfe | aca20dd | 2010-05-22 23:26:39 +0200 | [diff] [blame] | 249 | test_done |