t / t7008-grep-binary.shon commit untracked-cache: be defensive about missing NULs in index (c6909f9)
   1#!/bin/sh
   2
   3test_description='git grep in binary files'
   4
   5. ./test-lib.sh
   6
   7nul_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                "
  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                "
  37        else
  38                test_expect_success "PANIC: Test framework error. Unknown matches value $matches" 'false'
  39        fi
  40}
  41
  42test_expect_success 'setup' "
  43        echo 'binaryQfileQm[*]cQ*æQð' | q_to_nul >a &&
  44        git add a &&
  45        git commit -m.
  46"
  47
  48test_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
  54test_expect_success 'git grep -ah ina a' '
  55        git grep -ah ina a >actual &&
  56        test_cmp a actual
  57'
  58
  59test_expect_success 'git grep -I ina a' '
  60        test_must_fail git grep -I ina a >actual &&
  61        test_must_be_empty actual
  62'
  63
  64test_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
  70test_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
  76test_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
  82test_expect_success 'git grep -q ina a' '
  83        git grep -q ina a >actual &&
  84        test_must_be_empty actual
  85'
  86
  87test_expect_success 'git grep -F ile a' '
  88        git grep -F ile a
  89'
  90
  91test_expect_success 'git grep -Fi iLE a' '
  92        git grep -Fi iLE a
  93'
  94
  95# This test actually passes on platforms where regexec() supports the
  96# flag REG_STARTEND.
  97test_expect_success 'git grep ile a' '
  98        git grep ile a
  99'
 100
 101test_expect_failure 'git grep .fi a' '
 102        git grep .fi a
 103'
 104
 105nul_match 1 '-F' 'yQf'
 106nul_match 0 '-F' 'yQx'
 107nul_match 1 '-Fi' 'YQf'
 108nul_match 0 '-Fi' 'YQx'
 109nul_match 1 '' 'yQf'
 110nul_match 0 '' 'yQx'
 111nul_match 1 '' 'æQð'
 112nul_match 1 '-F' 'eQm[*]c'
 113nul_match 1 '-Fi' 'EQM[*]C'
 114
 115# Regex patterns that would match but shouldn't with -F
 116nul_match 0 '-F' 'yQ[f]'
 117nul_match 0 '-F' '[y]Qf'
 118nul_match 0 '-Fi' 'YQ[F]'
 119nul_match 0 '-Fi' '[Y]QF'
 120nul_match 0 '-F' 'æQ[ð]'
 121nul_match 0 '-F' '[æ]Qð'
 122nul_match 0 '-Fi' 'ÆQ[Ð]'
 123nul_match 0 '-Fi' '[Æ]QÐ'
 124
 125# kwset is disabled on -i & non-ASCII. No way to match non-ASCII \0
 126# patterns case-insensitively.
 127nul_match T1 '-i' 'ÆQÐ'
 128
 129# \0 implicitly disables regexes. This is an undocumented internal
 130# limitation.
 131nul_match T1 '' 'yQ[f]'
 132nul_match T1 '' '[y]Qf'
 133nul_match T1 '-i' 'YQ[F]'
 134nul_match T1 '-i' '[Y]Qf'
 135nul_match T1 '' 'æQ[ð]'
 136nul_match T1 '' '[æ]Qð'
 137nul_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.
 141nul_match T1 '' 'eQm.*cQ'
 142nul_match T1 '-i' 'EQM.*cQ'
 143nul_match T0 '' 'eQm[*]c'
 144nul_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.
 149nul_match 0 '-i' 'NOMATCHQð'
 150nul_match T0 '-i' '[Æ]QNOMATCH'
 151nul_match T0 '-i' '[æ]QNOMATCH'
 152# Matches, but for the wrong reasons, just stops at [æ]
 153nul_match 1 '-i' '[Æ]Qð'
 154nul_match 1 '-i' '[æ]Qð'
 155
 156# Ensure that the matcher doesn't regress to something that stops at
 157# \0
 158nul_match 0 '-F' 'yQ[f]'
 159nul_match 0 '-Fi' 'YQ[F]'
 160nul_match 0 '' 'yQNOMATCH'
 161nul_match 0 '' 'QNOMATCH'
 162nul_match 0 '-i' 'YQNOMATCH'
 163nul_match 0 '-i' 'QNOMATCH'
 164nul_match 0 '-F' 'æQ[ð]'
 165nul_match 0 '-Fi' 'ÆQ[Ð]'
 166nul_match 0 '' 'yQNÓMATCH'
 167nul_match 0 '' 'QNÓMATCH'
 168nul_match 0 '-i' 'YQNÓMATCH'
 169nul_match 0 '-i' 'QNÓMATCH'
 170
 171test_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
 183test_expect_success 'grep --cached respects binary diff attribute' '
 184        git grep --cached text t >actual &&
 185        test_cmp expect actual
 186'
 187
 188test_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
 197test_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
 205test_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 &&
 213        git grep bin b >actual.raw &&
 214        nul_to_q <actual.raw >actual &&
 215        test_cmp expect actual
 216'
 217
 218cat >nul_to_q_textconv <<'EOF'
 219#!/bin/sh
 220"$PERL_PATH" -pe 'y/\000/Q/' < "$1"
 221EOF
 222chmod +x nul_to_q_textconv
 223
 224test_expect_success 'setup textconv filters' '
 225        echo a diff=foo >.gitattributes &&
 226        git config diff.foo.textconv "\"$(pwd)\""/nul_to_q_textconv
 227'
 228
 229test_expect_success 'grep does not honor textconv' '
 230        test_must_fail git grep Qfile
 231'
 232
 233test_expect_success 'grep --textconv honors textconv' '
 234        echo "a:binaryQfileQm[*]cQ*æQð" >expect &&
 235        git grep --textconv Qfile >actual &&
 236        test_cmp expect actual
 237'
 238
 239test_expect_success 'grep --no-textconv does not honor textconv' '
 240        test_must_fail git grep --no-textconv Qfile
 241'
 242
 243test_expect_success 'grep --textconv blob honors textconv' '
 244        echo "HEAD:a:binaryQfileQm[*]cQ*æQð" >expect &&
 245        git grep --textconv Qfile HEAD:a >actual &&
 246        test_cmp expect actual
 247'
 248
 249test_done