t / t7008-grep-binary.shon commit remote: add a macro for "refs/tags/*:refs/tags/*" (750d0da)
   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        : >expect &&
  61        test_must_fail git grep -I ina a >actual &&
  62        test_cmp expect actual
  63'
  64
  65test_expect_success 'git grep -c ina a' '
  66        echo a:1 >expect &&
  67        git grep -c ina a >actual &&
  68        test_cmp expect actual
  69'
  70
  71test_expect_success 'git grep -l ina a' '
  72        echo a >expect &&
  73        git grep -l ina a >actual &&
  74        test_cmp expect actual
  75'
  76
  77test_expect_success 'git grep -L bar a' '
  78        echo a >expect &&
  79        git grep -L bar a >actual &&
  80        test_cmp expect actual
  81'
  82
  83test_expect_success 'git grep -q ina a' '
  84        : >expect &&
  85        git grep -q ina a >actual &&
  86        test_cmp expect actual
  87'
  88
  89test_expect_success 'git grep -F ile a' '
  90        git grep -F ile a
  91'
  92
  93test_expect_success 'git grep -Fi iLE a' '
  94        git grep -Fi iLE a
  95'
  96
  97# This test actually passes on platforms where regexec() supports the
  98# flag REG_STARTEND.
  99test_expect_success 'git grep ile a' '
 100        git grep ile a
 101'
 102
 103test_expect_failure 'git grep .fi a' '
 104        git grep .fi a
 105'
 106
 107nul_match 1 '-F' 'yQf'
 108nul_match 0 '-F' 'yQx'
 109nul_match 1 '-Fi' 'YQf'
 110nul_match 0 '-Fi' 'YQx'
 111nul_match 1 '' 'yQf'
 112nul_match 0 '' 'yQx'
 113nul_match 1 '' 'æQð'
 114nul_match 1 '-F' 'eQm[*]c'
 115nul_match 1 '-Fi' 'EQM[*]C'
 116
 117# Regex patterns that would match but shouldn't with -F
 118nul_match 0 '-F' 'yQ[f]'
 119nul_match 0 '-F' '[y]Qf'
 120nul_match 0 '-Fi' 'YQ[F]'
 121nul_match 0 '-Fi' '[Y]QF'
 122nul_match 0 '-F' 'æQ[ð]'
 123nul_match 0 '-F' '[æ]Qð'
 124nul_match 0 '-Fi' 'ÆQ[Ð]'
 125nul_match 0 '-Fi' '[Æ]QÐ'
 126
 127# kwset is disabled on -i & non-ASCII. No way to match non-ASCII \0
 128# patterns case-insensitively.
 129nul_match T1 '-i' 'ÆQÐ'
 130
 131# \0 implicitly disables regexes. This is an undocumented internal
 132# limitation.
 133nul_match T1 '' 'yQ[f]'
 134nul_match T1 '' '[y]Qf'
 135nul_match T1 '-i' 'YQ[F]'
 136nul_match T1 '-i' '[Y]Qf'
 137nul_match T1 '' 'æQ[ð]'
 138nul_match T1 '' '[æ]Qð'
 139nul_match T1 '-i' 'ÆQ[Ð]'
 140
 141# ... because of \0 implicitly disabling regexes regexes that
 142# should/shouldn't match don't do the right thing.
 143nul_match T1 '' 'eQm.*cQ'
 144nul_match T1 '-i' 'EQM.*cQ'
 145nul_match T0 '' 'eQm[*]c'
 146nul_match T0 '-i' 'EQM[*]C'
 147
 148# Due to the REG_STARTEND extension when kwset() is disabled on -i &
 149# non-ASCII the string will be matched in its entirety, but the
 150# pattern will be cut off at the first \0.
 151nul_match 0 '-i' 'NOMATCHQð'
 152nul_match T0 '-i' '[Æ]QNOMATCH'
 153nul_match T0 '-i' '[æ]QNOMATCH'
 154# Matches, but for the wrong reasons, just stops at [æ]
 155nul_match 1 '-i' '[Æ]Qð'
 156nul_match 1 '-i' '[æ]Qð'
 157
 158# Ensure that the matcher doesn't regress to something that stops at
 159# \0
 160nul_match 0 '-F' 'yQ[f]'
 161nul_match 0 '-Fi' 'YQ[F]'
 162nul_match 0 '' 'yQNOMATCH'
 163nul_match 0 '' 'QNOMATCH'
 164nul_match 0 '-i' 'YQNOMATCH'
 165nul_match 0 '-i' 'QNOMATCH'
 166nul_match 0 '-F' 'æQ[ð]'
 167nul_match 0 '-Fi' 'ÆQ[Ð]'
 168nul_match 0 '' 'yQNÓMATCH'
 169nul_match 0 '' 'QNÓMATCH'
 170nul_match 0 '-i' 'YQNÓMATCH'
 171nul_match 0 '-i' 'QNÓMATCH'
 172
 173test_expect_success 'grep respects binary diff attribute' '
 174        echo text >t &&
 175        git add t &&
 176        echo t:text >expect &&
 177        git grep text t >actual &&
 178        test_cmp expect actual &&
 179        echo "t -diff" >.gitattributes &&
 180        echo "Binary file t matches" >expect &&
 181        git grep text t >actual &&
 182        test_cmp expect actual
 183'
 184
 185test_expect_success 'grep --cached respects binary diff attribute' '
 186        git grep --cached text t >actual &&
 187        test_cmp expect actual
 188'
 189
 190test_expect_success 'grep --cached respects binary diff attribute (2)' '
 191        git add .gitattributes &&
 192        rm .gitattributes &&
 193        git grep --cached text t >actual &&
 194        test_when_finished "git rm --cached .gitattributes" &&
 195        test_when_finished "git checkout .gitattributes" &&
 196        test_cmp expect actual
 197'
 198
 199test_expect_success 'grep revision respects binary diff attribute' '
 200        git commit -m new &&
 201        echo "Binary file HEAD:t matches" >expect &&
 202        git grep text HEAD -- t >actual &&
 203        test_when_finished "git reset HEAD^" &&
 204        test_cmp expect actual
 205'
 206
 207test_expect_success 'grep respects not-binary diff attribute' '
 208        echo binQary | q_to_nul >b &&
 209        git add b &&
 210        echo "Binary file b matches" >expect &&
 211        git grep bin b >actual &&
 212        test_cmp expect actual &&
 213        echo "b diff" >.gitattributes &&
 214        echo "b:binQary" >expect &&
 215        git grep bin b >actual.raw &&
 216        nul_to_q <actual.raw >actual &&
 217        test_cmp expect actual
 218'
 219
 220cat >nul_to_q_textconv <<'EOF'
 221#!/bin/sh
 222"$PERL_PATH" -pe 'y/\000/Q/' < "$1"
 223EOF
 224chmod +x nul_to_q_textconv
 225
 226test_expect_success 'setup textconv filters' '
 227        echo a diff=foo >.gitattributes &&
 228        git config diff.foo.textconv "\"$(pwd)\""/nul_to_q_textconv
 229'
 230
 231test_expect_success 'grep does not honor textconv' '
 232        test_must_fail git grep Qfile
 233'
 234
 235test_expect_success 'grep --textconv honors textconv' '
 236        echo "a:binaryQfileQm[*]cQ*æQð" >expect &&
 237        git grep --textconv Qfile >actual &&
 238        test_cmp expect actual
 239'
 240
 241test_expect_success 'grep --no-textconv does not honor textconv' '
 242        test_must_fail git grep --no-textconv Qfile
 243'
 244
 245test_expect_success 'grep --textconv blob honors textconv' '
 246        echo "HEAD:a:binaryQfileQm[*]cQ*æQð" >expect &&
 247        git grep --textconv Qfile HEAD:a >actual &&
 248        test_cmp expect actual
 249'
 250
 251test_done