t / t7002-grep.shon commit t7002: test for not using external grep on skip-worktree paths (8740773)
   1#!/bin/sh
   2#
   3# Copyright (c) 2006 Junio C Hamano
   4#
   5
   6test_description='git grep various.
   7'
   8
   9. ./test-lib.sh
  10
  11test_expect_success 'Check for external grep support' '
  12        case "$(git grep -h 2>&1|grep ext-grep)" in
  13        *"(default)"*)
  14                test_set_prereq EXTGREP
  15                true;;
  16        *"(ignored by this build)"*)
  17                true;;
  18        *)
  19                false;;
  20        esac
  21'
  22
  23cat >hello.c <<EOF
  24#include <stdio.h>
  25int main(int argc, const char **argv)
  26{
  27        printf("Hello world.\n");
  28        return 0;
  29}
  30EOF
  31
  32test_expect_success setup '
  33        {
  34                echo foo mmap bar
  35                echo foo_mmap bar
  36                echo foo_mmap bar mmap
  37                echo foo mmap bar_mmap
  38                echo foo_mmap bar mmap baz
  39        } >file &&
  40        echo vvv >v &&
  41        echo ww w >w &&
  42        echo x x xx x >x &&
  43        echo y yy >y &&
  44        echo zzz > z &&
  45        mkdir t &&
  46        echo test >t/t &&
  47        echo vvv >t/v &&
  48        mkdir t/a &&
  49        echo vvv >t/a/v &&
  50        git add . &&
  51        test_tick &&
  52        git commit -m initial
  53'
  54
  55test_expect_success 'grep should not segfault with a bad input' '
  56        test_must_fail git grep "("
  57'
  58
  59for H in HEAD ''
  60do
  61        case "$H" in
  62        HEAD)   HC='HEAD:' L='HEAD' ;;
  63        '')     HC= L='in working tree' ;;
  64        esac
  65
  66        test_expect_success "grep -w $L" '
  67                {
  68                        echo ${HC}file:1:foo mmap bar
  69                        echo ${HC}file:3:foo_mmap bar mmap
  70                        echo ${HC}file:4:foo mmap bar_mmap
  71                        echo ${HC}file:5:foo_mmap bar mmap baz
  72                } >expected &&
  73                git grep -n -w -e mmap $H >actual &&
  74                diff expected actual
  75        '
  76
  77        test_expect_success "grep -w $L (w)" '
  78                : >expected &&
  79                ! git grep -n -w -e "^w" >actual &&
  80                test_cmp expected actual
  81        '
  82
  83        test_expect_success "grep -w $L (x)" '
  84                {
  85                        echo ${HC}x:1:x x xx x
  86                } >expected &&
  87                git grep -n -w -e "x xx* x" $H >actual &&
  88                diff expected actual
  89        '
  90
  91        test_expect_success "grep -w $L (y-1)" '
  92                {
  93                        echo ${HC}y:1:y yy
  94                } >expected &&
  95                git grep -n -w -e "^y" $H >actual &&
  96                diff expected actual
  97        '
  98
  99        test_expect_success "grep -w $L (y-2)" '
 100                : >expected &&
 101                if git grep -n -w -e "^y y" $H >actual
 102                then
 103                        echo should not have matched
 104                        cat actual
 105                        false
 106                else
 107                        diff expected actual
 108                fi
 109        '
 110
 111        test_expect_success "grep -w $L (z)" '
 112                : >expected &&
 113                if git grep -n -w -e "^z" $H >actual
 114                then
 115                        echo should not have matched
 116                        cat actual
 117                        false
 118                else
 119                        diff expected actual
 120                fi
 121        '
 122
 123        test_expect_success "grep $L (t-1)" '
 124                echo "${HC}t/t:1:test" >expected &&
 125                git grep -n -e test $H >actual &&
 126                diff expected actual
 127        '
 128
 129        test_expect_success "grep $L (t-2)" '
 130                echo "${HC}t:1:test" >expected &&
 131                (
 132                        cd t &&
 133                        git grep -n -e test $H
 134                ) >actual &&
 135                diff expected actual
 136        '
 137
 138        test_expect_success "grep $L (t-3)" '
 139                echo "${HC}t/t:1:test" >expected &&
 140                (
 141                        cd t &&
 142                        git grep --full-name -n -e test $H
 143                ) >actual &&
 144                diff expected actual
 145        '
 146
 147        test_expect_success "grep -c $L (no /dev/null)" '
 148                ! git grep -c test $H | grep /dev/null
 149        '
 150
 151        test_expect_success "grep --max-depth -1 $L" '
 152                {
 153                        echo ${HC}t/a/v:1:vvv
 154                        echo ${HC}t/v:1:vvv
 155                        echo ${HC}v:1:vvv
 156                } >expected &&
 157                git grep --max-depth -1 -n -e vvv $H >actual &&
 158                test_cmp expected actual
 159        '
 160
 161        test_expect_success "grep --max-depth 0 $L" '
 162                {
 163                        echo ${HC}v:1:vvv
 164                } >expected &&
 165                git grep --max-depth 0 -n -e vvv $H >actual &&
 166                test_cmp expected actual
 167        '
 168
 169        test_expect_success "grep --max-depth 0 -- '*' $L" '
 170                {
 171                        echo ${HC}t/a/v:1:vvv
 172                        echo ${HC}t/v:1:vvv
 173                        echo ${HC}v:1:vvv
 174                } >expected &&
 175                git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
 176                test_cmp expected actual
 177        '
 178
 179        test_expect_success "grep --max-depth 1 $L" '
 180                {
 181                        echo ${HC}t/v:1:vvv
 182                        echo ${HC}v:1:vvv
 183                } >expected &&
 184                git grep --max-depth 1 -n -e vvv $H >actual &&
 185                test_cmp expected actual
 186        '
 187
 188        test_expect_success "grep --max-depth 0 -- t $L" '
 189                {
 190                        echo ${HC}t/v:1:vvv
 191                } >expected &&
 192                git grep --max-depth 0 -n -e vvv $H -- t >actual &&
 193                test_cmp expected actual
 194        '
 195
 196done
 197
 198cat >expected <<EOF
 199file:foo mmap bar_mmap
 200EOF
 201
 202test_expect_success 'grep -e A --and -e B' '
 203        git grep -e "foo mmap" --and -e bar_mmap >actual &&
 204        test_cmp expected actual
 205'
 206
 207cat >expected <<EOF
 208file:foo_mmap bar mmap
 209file:foo_mmap bar mmap baz
 210EOF
 211
 212
 213test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
 214        git grep \( -e foo_ --or -e baz \) \
 215                --and -e " mmap" >actual &&
 216        test_cmp expected actual
 217'
 218
 219cat >expected <<EOF
 220file:foo mmap bar
 221EOF
 222
 223test_expect_success 'grep -e A --and --not -e B' '
 224        git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
 225        test_cmp expected actual
 226'
 227
 228cat >expected <<EOF
 229y:y yy
 230--
 231z:zzz
 232EOF
 233
 234# Create 1024 file names that sort between "y" and "z" to make sure
 235# the two files are handled by different calls to an external grep.
 236# This depends on MAXARGS in builtin-grep.c being 1024 or less.
 237c32="0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v"
 238test_expect_success 'grep -C1, hunk mark between files' '
 239        for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
 240        git add y-?? &&
 241        git grep -C1 "^[yz]" >actual &&
 242        test_cmp expected actual
 243'
 244
 245test_expect_success 'grep -C1 --no-ext-grep, hunk mark between files' '
 246        git grep -C1 --no-ext-grep "^[yz]" >actual &&
 247        test_cmp expected actual
 248'
 249
 250test_expect_success 'log grep setup' '
 251        echo a >>file &&
 252        test_tick &&
 253        GIT_AUTHOR_NAME="With * Asterisk" \
 254        GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
 255        git commit -a -m "second" &&
 256
 257        echo a >>file &&
 258        test_tick &&
 259        git commit -a -m "third"
 260
 261'
 262
 263test_expect_success 'log grep (1)' '
 264        git log --author=author --pretty=tformat:%s >actual &&
 265        ( echo third ; echo initial ) >expect &&
 266        test_cmp expect actual
 267'
 268
 269test_expect_success 'log grep (2)' '
 270        git log --author=" * " -F --pretty=tformat:%s >actual &&
 271        ( echo second ) >expect &&
 272        test_cmp expect actual
 273'
 274
 275test_expect_success 'log grep (3)' '
 276        git log --author="^A U" --pretty=tformat:%s >actual &&
 277        ( echo third ; echo initial ) >expect &&
 278        test_cmp expect actual
 279'
 280
 281test_expect_success 'log grep (4)' '
 282        git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
 283        ( echo second ) >expect &&
 284        test_cmp expect actual
 285'
 286
 287test_expect_success 'log grep (5)' '
 288        git log --author=Thor -F --grep=Thu --pretty=tformat:%s >actual &&
 289        ( echo third ; echo initial ) >expect &&
 290        test_cmp expect actual
 291'
 292
 293test_expect_success 'log grep (6)' '
 294        git log --author=-0700  --pretty=tformat:%s >actual &&
 295        >expect &&
 296        test_cmp expect actual
 297'
 298
 299test_expect_success 'grep with CE_VALID file' '
 300        git update-index --assume-unchanged t/t &&
 301        rm t/t &&
 302        test "$(git grep --no-ext-grep test)" = "t/t:test" &&
 303        git update-index --no-assume-unchanged t/t &&
 304        git checkout t/t
 305'
 306
 307cat >expected <<EOF
 308hello.c=#include <stdio.h>
 309hello.c:        return 0;
 310EOF
 311
 312test_expect_success 'grep -p with userdiff' '
 313        git config diff.custom.funcname "^#" &&
 314        echo "hello.c diff=custom" >.gitattributes &&
 315        git grep -p return >actual &&
 316        test_cmp expected actual
 317'
 318
 319cat >expected <<EOF
 320hello.c=int main(int argc, const char **argv)
 321hello.c:        return 0;
 322EOF
 323
 324test_expect_success 'grep -p' '
 325        rm -f .gitattributes &&
 326        git grep -p return >actual &&
 327        test_cmp expected actual
 328'
 329
 330cat >expected <<EOF
 331hello.c-#include <stdio.h>
 332hello.c=int main(int argc, const char **argv)
 333hello.c-{
 334hello.c-        printf("Hello world.\n");
 335hello.c:        return 0;
 336EOF
 337
 338test_expect_success 'grep -p -B5' '
 339        git grep -p -B5 return >actual &&
 340        test_cmp expected actual
 341'
 342
 343test_expect_success EXTGREP 'external grep is called' '
 344        GIT_TRACE=2 git grep foo >/dev/null 2>actual &&
 345        grep "trace: grep:.*foo" actual >/dev/null
 346'
 347
 348test_expect_success EXTGREP 'no external grep when skip-worktree entries exist' '
 349        git update-index --skip-worktree file &&
 350        GIT_TRACE=2 git grep foo >/dev/null 2>actual &&
 351        ! grep "trace: grep:" actual >/dev/null &&
 352        git update-index --no-skip-worktree file
 353'
 354
 355test_done