t / t7810-grep.shon commit Merge branch 'nd/commit-hook-doc-fix' (fbcfbc4)
   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
  11cat >hello.c <<EOF
  12#include <assert.h>
  13#include <stdio.h>
  14
  15int main(int argc, const char **argv)
  16{
  17        printf("Hello world.\n");
  18        return 0;
  19        /* char ?? */
  20}
  21EOF
  22
  23test_expect_success setup '
  24        {
  25                echo foo mmap bar
  26                echo foo_mmap bar
  27                echo foo_mmap bar mmap
  28                echo foo mmap bar_mmap
  29                echo foo_mmap bar mmap baz
  30        } >file &&
  31        {
  32                echo Hello world
  33                echo HeLLo world
  34                echo Hello_world
  35                echo HeLLo_world
  36        } >hello_world &&
  37        {
  38                echo "a+b*c"
  39                echo "a+bc"
  40                echo "abc"
  41        } >ab &&
  42        {
  43                echo d &&
  44                echo 0
  45        } >d0 &&
  46        echo vvv >v &&
  47        echo ww w >w &&
  48        echo x x xx x >x &&
  49        echo y yy >y &&
  50        echo zzz > z &&
  51        mkdir t &&
  52        echo test >t/t &&
  53        echo vvv >t/v &&
  54        mkdir t/a &&
  55        echo vvv >t/a/v &&
  56        {
  57                echo "line without leading space1"
  58                echo " line with leading space1"
  59                echo " line with leading space2"
  60                echo " line with leading space3"
  61                echo "line without leading space2"
  62        } >space &&
  63        git add . &&
  64        test_tick &&
  65        git commit -m initial
  66'
  67
  68test_expect_success 'grep should not segfault with a bad input' '
  69        test_must_fail git grep "("
  70'
  71
  72for H in HEAD ''
  73do
  74        case "$H" in
  75        HEAD)   HC='HEAD:' L='HEAD' ;;
  76        '')     HC= L='in working tree' ;;
  77        esac
  78
  79        test_expect_success "grep -w $L" '
  80                {
  81                        echo ${HC}file:1:foo mmap bar
  82                        echo ${HC}file:3:foo_mmap bar mmap
  83                        echo ${HC}file:4:foo mmap bar_mmap
  84                        echo ${HC}file:5:foo_mmap bar mmap baz
  85                } >expected &&
  86                git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
  87                test_cmp expected actual
  88        '
  89
  90        test_expect_success "grep -w $L" '
  91                {
  92                        echo ${HC}file:1:foo mmap bar
  93                        echo ${HC}file:3:foo_mmap bar mmap
  94                        echo ${HC}file:4:foo mmap bar_mmap
  95                        echo ${HC}file:5:foo_mmap bar mmap baz
  96                } >expected &&
  97                git -c grep.linenumber=true grep -w -e mmap $H >actual &&
  98                test_cmp expected actual
  99        '
 100
 101        test_expect_success "grep -w $L" '
 102                {
 103                        echo ${HC}file:foo mmap bar
 104                        echo ${HC}file:foo_mmap bar mmap
 105                        echo ${HC}file:foo mmap bar_mmap
 106                        echo ${HC}file:foo_mmap bar mmap baz
 107                } >expected &&
 108                git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
 109                test_cmp expected actual
 110        '
 111
 112        test_expect_success "grep -w $L (w)" '
 113                : >expected &&
 114                test_must_fail git grep -n -w -e "^w" $H >actual &&
 115                test_cmp expected actual
 116        '
 117
 118        test_expect_success "grep -w $L (x)" '
 119                {
 120                        echo ${HC}x:1:x x xx x
 121                } >expected &&
 122                git grep -n -w -e "x xx* x" $H >actual &&
 123                test_cmp expected actual
 124        '
 125
 126        test_expect_success "grep -w $L (y-1)" '
 127                {
 128                        echo ${HC}y:1:y yy
 129                } >expected &&
 130                git grep -n -w -e "^y" $H >actual &&
 131                test_cmp expected actual
 132        '
 133
 134        test_expect_success "grep -w $L (y-2)" '
 135                : >expected &&
 136                if git grep -n -w -e "^y y" $H >actual
 137                then
 138                        echo should not have matched
 139                        cat actual
 140                        false
 141                else
 142                        test_cmp expected actual
 143                fi
 144        '
 145
 146        test_expect_success "grep -w $L (z)" '
 147                : >expected &&
 148                if git grep -n -w -e "^z" $H >actual
 149                then
 150                        echo should not have matched
 151                        cat actual
 152                        false
 153                else
 154                        test_cmp expected actual
 155                fi
 156        '
 157
 158        test_expect_success "grep $L (t-1)" '
 159                echo "${HC}t/t:1:test" >expected &&
 160                git grep -n -e test $H >actual &&
 161                test_cmp expected actual
 162        '
 163
 164        test_expect_success "grep $L (t-2)" '
 165                echo "${HC}t:1:test" >expected &&
 166                (
 167                        cd t &&
 168                        git grep -n -e test $H
 169                ) >actual &&
 170                test_cmp expected actual
 171        '
 172
 173        test_expect_success "grep $L (t-3)" '
 174                echo "${HC}t/t:1:test" >expected &&
 175                (
 176                        cd t &&
 177                        git grep --full-name -n -e test $H
 178                ) >actual &&
 179                test_cmp expected actual
 180        '
 181
 182        test_expect_success "grep -c $L (no /dev/null)" '
 183                ! git grep -c test $H | grep /dev/null
 184        '
 185
 186        test_expect_success "grep --max-depth -1 $L" '
 187                {
 188                        echo ${HC}t/a/v:1:vvv
 189                        echo ${HC}t/v:1:vvv
 190                        echo ${HC}v:1:vvv
 191                } >expected &&
 192                git grep --max-depth -1 -n -e vvv $H >actual &&
 193                test_cmp expected actual
 194        '
 195
 196        test_expect_success "grep --max-depth 0 $L" '
 197                {
 198                        echo ${HC}v:1:vvv
 199                } >expected &&
 200                git grep --max-depth 0 -n -e vvv $H >actual &&
 201                test_cmp expected actual
 202        '
 203
 204        test_expect_success "grep --max-depth 0 -- '*' $L" '
 205                {
 206                        echo ${HC}t/a/v:1:vvv
 207                        echo ${HC}t/v:1:vvv
 208                        echo ${HC}v:1:vvv
 209                } >expected &&
 210                git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
 211                test_cmp expected actual
 212        '
 213
 214        test_expect_success "grep --max-depth 1 $L" '
 215                {
 216                        echo ${HC}t/v:1:vvv
 217                        echo ${HC}v:1:vvv
 218                } >expected &&
 219                git grep --max-depth 1 -n -e vvv $H >actual &&
 220                test_cmp expected actual
 221        '
 222
 223        test_expect_success "grep --max-depth 0 -- t $L" '
 224                {
 225                        echo ${HC}t/v:1:vvv
 226                } >expected &&
 227                git grep --max-depth 0 -n -e vvv $H -- t >actual &&
 228                test_cmp expected actual
 229        '
 230
 231        test_expect_success "grep --max-depth 0 -- . t $L" '
 232                {
 233                        echo ${HC}t/v:1:vvv
 234                        echo ${HC}v:1:vvv
 235                } >expected &&
 236                git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
 237                test_cmp expected actual
 238        '
 239
 240        test_expect_success "grep --max-depth 0 -- t . $L" '
 241                {
 242                        echo ${HC}t/v:1:vvv
 243                        echo ${HC}v:1:vvv
 244                } >expected &&
 245                git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
 246                test_cmp expected actual
 247        '
 248        test_expect_success "grep $L with grep.extendedRegexp=false" '
 249                echo "${HC}ab:a+bc" >expected &&
 250                git -c grep.extendedRegexp=false grep "a+b*c" $H ab >actual &&
 251                test_cmp expected actual
 252        '
 253
 254        test_expect_success "grep $L with grep.extendedRegexp=true" '
 255                echo "${HC}ab:abc" >expected &&
 256                git -c grep.extendedRegexp=true grep "a+b*c" $H ab >actual &&
 257                test_cmp expected actual
 258        '
 259
 260        test_expect_success "grep $L with grep.patterntype=basic" '
 261                echo "${HC}ab:a+bc" >expected &&
 262                git -c grep.patterntype=basic grep "a+b*c" $H ab >actual &&
 263                test_cmp expected actual
 264        '
 265
 266        test_expect_success "grep $L with grep.patterntype=extended" '
 267                echo "${HC}ab:abc" >expected &&
 268                git -c grep.patterntype=extended grep "a+b*c" $H ab >actual &&
 269                test_cmp expected actual
 270        '
 271
 272        test_expect_success "grep $L with grep.patterntype=fixed" '
 273                echo "${HC}ab:a+b*c" >expected &&
 274                git -c grep.patterntype=fixed grep "a+b*c" $H ab >actual &&
 275                test_cmp expected actual
 276        '
 277
 278        test_expect_success LIBPCRE "grep $L with grep.patterntype=perl" '
 279                echo "${HC}ab:a+b*c" >expected &&
 280                git -c grep.patterntype=perl grep "a\x{2b}b\x{2a}c" $H ab >actual &&
 281                test_cmp expected actual
 282        '
 283
 284        test_expect_success "grep $L with grep.patternType=default and grep.extendedRegexp=true" '
 285                echo "${HC}ab:abc" >expected &&
 286                git \
 287                        -c grep.patternType=default \
 288                        -c grep.extendedRegexp=true \
 289                        grep "a+b*c" $H ab >actual &&
 290                test_cmp expected actual
 291        '
 292
 293        test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=default" '
 294                echo "${HC}ab:abc" >expected &&
 295                git \
 296                        -c grep.extendedRegexp=true \
 297                        -c grep.patternType=default \
 298                        grep "a+b*c" $H ab >actual &&
 299                test_cmp expected actual
 300        '
 301
 302        test_expect_success "grep $L with grep.patternType=extended and grep.extendedRegexp=false" '
 303                echo "${HC}ab:abc" >expected &&
 304                git \
 305                        -c grep.patternType=extended \
 306                        -c grep.extendedRegexp=false \
 307                        grep "a+b*c" $H ab >actual &&
 308                test_cmp expected actual
 309        '
 310
 311        test_expect_success "grep $L with grep.patternType=basic and grep.extendedRegexp=true" '
 312                echo "${HC}ab:a+bc" >expected &&
 313                git \
 314                        -c grep.patternType=basic \
 315                        -c grep.extendedRegexp=true \
 316                        grep "a+b*c" $H ab >actual &&
 317                test_cmp expected actual
 318        '
 319
 320        test_expect_success "grep $L with grep.extendedRegexp=false and grep.patternType=extended" '
 321                echo "${HC}ab:abc" >expected &&
 322                git \
 323                        -c grep.extendedRegexp=false \
 324                        -c grep.patternType=extended \
 325                        grep "a+b*c" $H ab >actual &&
 326                test_cmp expected actual
 327        '
 328
 329        test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=basic" '
 330                echo "${HC}ab:a+bc" >expected &&
 331                git \
 332                        -c grep.extendedRegexp=true \
 333                        -c grep.patternType=basic \
 334                        grep "a+b*c" $H ab >actual &&
 335                test_cmp expected actual
 336        '
 337
 338        test_expect_success "grep --count $L" '
 339                echo ${HC}ab:3 >expected &&
 340                git grep --count -e b $H -- ab >actual &&
 341                test_cmp expected actual
 342        '
 343
 344        test_expect_success "grep --count -h $L" '
 345                echo 3 >expected &&
 346                git grep --count -h -e b $H -- ab >actual &&
 347                test_cmp expected actual
 348        '
 349done
 350
 351cat >expected <<EOF
 352file
 353EOF
 354test_expect_success 'grep -l -C' '
 355        git grep -l -C1 foo >actual &&
 356        test_cmp expected actual
 357'
 358
 359cat >expected <<EOF
 360file:5
 361EOF
 362test_expect_success 'grep -c -C' '
 363        git grep -c -C1 foo >actual &&
 364        test_cmp expected actual
 365'
 366
 367test_expect_success 'grep -L -C' '
 368        git ls-files >expected &&
 369        git grep -L -C1 nonexistent_string >actual &&
 370        test_cmp expected actual
 371'
 372
 373cat >expected <<EOF
 374file:foo mmap bar_mmap
 375EOF
 376
 377test_expect_success 'grep -e A --and -e B' '
 378        git grep -e "foo mmap" --and -e bar_mmap >actual &&
 379        test_cmp expected actual
 380'
 381
 382cat >expected <<EOF
 383file:foo_mmap bar mmap
 384file:foo_mmap bar mmap baz
 385EOF
 386
 387
 388test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
 389        git grep \( -e foo_ --or -e baz \) \
 390                --and -e " mmap" >actual &&
 391        test_cmp expected actual
 392'
 393
 394cat >expected <<EOF
 395file:foo mmap bar
 396EOF
 397
 398test_expect_success 'grep -e A --and --not -e B' '
 399        git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
 400        test_cmp expected actual
 401'
 402
 403test_expect_success 'grep should ignore GREP_OPTIONS' '
 404        GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
 405        test_cmp expected actual
 406'
 407
 408test_expect_success 'grep -f, non-existent file' '
 409        test_must_fail git grep -f patterns
 410'
 411
 412cat >expected <<EOF
 413file:foo mmap bar
 414file:foo_mmap bar
 415file:foo_mmap bar mmap
 416file:foo mmap bar_mmap
 417file:foo_mmap bar mmap baz
 418EOF
 419
 420cat >pattern <<EOF
 421mmap
 422EOF
 423
 424test_expect_success 'grep -f, one pattern' '
 425        git grep -f pattern >actual &&
 426        test_cmp expected actual
 427'
 428
 429cat >expected <<EOF
 430file:foo mmap bar
 431file:foo_mmap bar
 432file:foo_mmap bar mmap
 433file:foo mmap bar_mmap
 434file:foo_mmap bar mmap baz
 435t/a/v:vvv
 436t/v:vvv
 437v:vvv
 438EOF
 439
 440cat >patterns <<EOF
 441mmap
 442vvv
 443EOF
 444
 445test_expect_success 'grep -f, multiple patterns' '
 446        git grep -f patterns >actual &&
 447        test_cmp expected actual
 448'
 449
 450test_expect_success 'grep, multiple patterns' '
 451        git grep "$(cat patterns)" >actual &&
 452        test_cmp expected actual
 453'
 454
 455cat >expected <<EOF
 456file:foo mmap bar
 457file:foo_mmap bar
 458file:foo_mmap bar mmap
 459file:foo mmap bar_mmap
 460file:foo_mmap bar mmap baz
 461t/a/v:vvv
 462t/v:vvv
 463v:vvv
 464EOF
 465
 466cat >patterns <<EOF
 467
 468mmap
 469
 470vvv
 471
 472EOF
 473
 474test_expect_success 'grep -f, ignore empty lines' '
 475        git grep -f patterns >actual &&
 476        test_cmp expected actual
 477'
 478
 479test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
 480        git grep -f - <patterns >actual &&
 481        test_cmp expected actual
 482'
 483
 484cat >expected <<EOF
 485y:y yy
 486--
 487z:zzz
 488EOF
 489
 490test_expect_success 'grep -q, silently report matches' '
 491        >empty &&
 492        git grep -q mmap >actual &&
 493        test_cmp empty actual &&
 494        test_must_fail git grep -q qfwfq >actual &&
 495        test_cmp empty actual
 496'
 497
 498test_expect_success 'grep -C1 hunk mark between files' '
 499        git grep -C1 "^[yz]" >actual &&
 500        test_cmp expected actual
 501'
 502
 503test_expect_success 'log grep setup' '
 504        echo a >>file &&
 505        test_tick &&
 506        GIT_AUTHOR_NAME="With * Asterisk" \
 507        GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
 508        git commit -a -m "second" &&
 509
 510        echo a >>file &&
 511        test_tick &&
 512        git commit -a -m "third" &&
 513
 514        echo a >>file &&
 515        test_tick &&
 516        GIT_AUTHOR_NAME="Night Fall" \
 517        GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
 518        git commit -a -m "fourth"
 519'
 520
 521test_expect_success 'log grep (1)' '
 522        git log --author=author --pretty=tformat:%s >actual &&
 523        {
 524                echo third && echo initial
 525        } >expect &&
 526        test_cmp expect actual
 527'
 528
 529test_expect_success 'log grep (2)' '
 530        git log --author=" * " -F --pretty=tformat:%s >actual &&
 531        {
 532                echo second
 533        } >expect &&
 534        test_cmp expect actual
 535'
 536
 537test_expect_success 'log grep (3)' '
 538        git log --author="^A U" --pretty=tformat:%s >actual &&
 539        {
 540                echo third && echo initial
 541        } >expect &&
 542        test_cmp expect actual
 543'
 544
 545test_expect_success 'log grep (4)' '
 546        git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
 547        {
 548                echo second
 549        } >expect &&
 550        test_cmp expect actual
 551'
 552
 553test_expect_success 'log grep (5)' '
 554        git log --author=Thor -F --pretty=tformat:%s >actual &&
 555        {
 556                echo third && echo initial
 557        } >expect &&
 558        test_cmp expect actual
 559'
 560
 561test_expect_success 'log grep (6)' '
 562        git log --author=-0700  --pretty=tformat:%s >actual &&
 563        >expect &&
 564        test_cmp expect actual
 565'
 566
 567test_expect_success 'log grep (7)' '
 568        git log -g --grep-reflog="commit: third" --pretty=tformat:%s >actual &&
 569        echo third >expect &&
 570        test_cmp expect actual
 571'
 572
 573test_expect_success 'log grep (8)' '
 574        git log -g --grep-reflog="commit: third" --grep-reflog="commit: second" --pretty=tformat:%s >actual &&
 575        {
 576                echo third && echo second
 577        } >expect &&
 578        test_cmp expect actual
 579'
 580
 581test_expect_success 'log grep (9)' '
 582        git log -g --grep-reflog="commit: third" --author="Thor" --pretty=tformat:%s >actual &&
 583        echo third >expect &&
 584        test_cmp expect actual
 585'
 586
 587test_expect_success 'log grep (9)' '
 588        git log -g --grep-reflog="commit: third" --author="non-existent" --pretty=tformat:%s >actual &&
 589        : >expect &&
 590        test_cmp expect actual
 591'
 592
 593test_expect_success 'log --grep-reflog can only be used under -g' '
 594        test_must_fail git log --grep-reflog="commit: third"
 595'
 596
 597test_expect_success 'log with multiple --grep uses union' '
 598        git log --grep=i --grep=r --format=%s >actual &&
 599        {
 600                echo fourth && echo third && echo initial
 601        } >expect &&
 602        test_cmp expect actual
 603'
 604
 605test_expect_success 'log --all-match with multiple --grep uses intersection' '
 606        git log --all-match --grep=i --grep=r --format=%s >actual &&
 607        {
 608                echo third
 609        } >expect &&
 610        test_cmp expect actual
 611'
 612
 613test_expect_success 'log with multiple --author uses union' '
 614        git log --author="Thor" --author="Aster" --format=%s >actual &&
 615        {
 616            echo third && echo second && echo initial
 617        } >expect &&
 618        test_cmp expect actual
 619'
 620
 621test_expect_success 'log --all-match with multiple --author still uses union' '
 622        git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
 623        {
 624            echo third && echo second && echo initial
 625        } >expect &&
 626        test_cmp expect actual
 627'
 628
 629test_expect_success 'log --grep --author uses intersection' '
 630        # grep matches only third and fourth
 631        # author matches only initial and third
 632        git log --author="A U Thor" --grep=r --format=%s >actual &&
 633        {
 634                echo third
 635        } >expect &&
 636        test_cmp expect actual
 637'
 638
 639test_expect_success 'log --grep --grep --author takes union of greps and intersects with author' '
 640        # grep matches initial and second but not third
 641        # author matches only initial and third
 642        git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
 643        {
 644                echo initial
 645        } >expect &&
 646        test_cmp expect actual
 647'
 648
 649test_expect_success 'log ---all-match -grep --author --author still takes union of authors and intersects with grep' '
 650        # grep matches only initial and third
 651        # author matches all but second
 652        git log --all-match --author="Thor" --author="Night" --grep=i --format=%s >actual &&
 653        {
 654            echo third && echo initial
 655        } >expect &&
 656        test_cmp expect actual
 657'
 658
 659test_expect_success 'log --grep --author --author takes union of authors and intersects with grep' '
 660        # grep matches only initial and third
 661        # author matches all but second
 662        git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
 663        {
 664            echo third && echo initial
 665        } >expect &&
 666        test_cmp expect actual
 667'
 668
 669test_expect_success 'log --all-match --grep --grep --author takes intersection' '
 670        # grep matches only third
 671        # author matches only initial and third
 672        git log --all-match --author="A U Thor" --grep=i --grep=r --format=%s >actual &&
 673        {
 674                echo third
 675        } >expect &&
 676        test_cmp expect actual
 677'
 678
 679test_expect_success 'log --author does not search in timestamp' '
 680        : >expect &&
 681        git log --author="$GIT_AUTHOR_DATE" >actual &&
 682        test_cmp expect actual
 683'
 684
 685test_expect_success 'log --committer does not search in timestamp' '
 686        : >expect &&
 687        git log --committer="$GIT_COMMITTER_DATE" >actual &&
 688        test_cmp expect actual
 689'
 690
 691test_expect_success 'grep with CE_VALID file' '
 692        git update-index --assume-unchanged t/t &&
 693        rm t/t &&
 694        test "$(git grep test)" = "t/t:test" &&
 695        git update-index --no-assume-unchanged t/t &&
 696        git checkout t/t
 697'
 698
 699cat >expected <<EOF
 700hello.c=#include <stdio.h>
 701hello.c:        return 0;
 702EOF
 703
 704test_expect_success 'grep -p with userdiff' '
 705        git config diff.custom.funcname "^#" &&
 706        echo "hello.c diff=custom" >.gitattributes &&
 707        git grep -p return >actual &&
 708        test_cmp expected actual
 709'
 710
 711cat >expected <<EOF
 712hello.c=int main(int argc, const char **argv)
 713hello.c:        return 0;
 714EOF
 715
 716test_expect_success 'grep -p' '
 717        rm -f .gitattributes &&
 718        git grep -p return >actual &&
 719        test_cmp expected actual
 720'
 721
 722cat >expected <<EOF
 723hello.c-#include <stdio.h>
 724hello.c-
 725hello.c=int main(int argc, const char **argv)
 726hello.c-{
 727hello.c-        printf("Hello world.\n");
 728hello.c:        return 0;
 729EOF
 730
 731test_expect_success 'grep -p -B5' '
 732        git grep -p -B5 return >actual &&
 733        test_cmp expected actual
 734'
 735
 736cat >expected <<EOF
 737hello.c=int main(int argc, const char **argv)
 738hello.c-{
 739hello.c-        printf("Hello world.\n");
 740hello.c:        return 0;
 741hello.c-        /* char ?? */
 742hello.c-}
 743EOF
 744
 745test_expect_success 'grep -W' '
 746        git grep -W return >actual &&
 747        test_cmp expected actual
 748'
 749
 750cat >expected <<EOF
 751hello.c-#include <assert.h>
 752hello.c:#include <stdio.h>
 753EOF
 754
 755test_expect_success 'grep -W shows no trailing empty lines' '
 756        git grep -W stdio >actual &&
 757        test_cmp expected actual
 758'
 759
 760cat >expected <<EOF
 761hello.c=        printf("Hello world.\n");
 762hello.c:        return 0;
 763hello.c-        /* char ?? */
 764EOF
 765
 766test_expect_success 'grep -W with userdiff' '
 767        test_when_finished "rm -f .gitattributes" &&
 768        git config diff.custom.xfuncname "(printf.*|})$" &&
 769        echo "hello.c diff=custom" >.gitattributes &&
 770        git grep -W return >actual &&
 771        test_cmp expected actual
 772'
 773
 774test_expect_success 'grep from a subdirectory to search wider area (1)' '
 775        mkdir -p s &&
 776        (
 777                cd s && git grep "x x x" ..
 778        )
 779'
 780
 781test_expect_success 'grep from a subdirectory to search wider area (2)' '
 782        mkdir -p s &&
 783        (
 784                cd s || exit 1
 785                ( git grep xxyyzz .. >out ; echo $? >status )
 786                ! test -s out &&
 787                test 1 = $(cat status)
 788        )
 789'
 790
 791cat >expected <<EOF
 792hello.c:int main(int argc, const char **argv)
 793EOF
 794
 795test_expect_success 'grep -Fi' '
 796        git grep -Fi "CHAR *" >actual &&
 797        test_cmp expected actual
 798'
 799
 800test_expect_success 'outside of git repository' '
 801        rm -fr non &&
 802        mkdir -p non/git/sub &&
 803        echo hello >non/git/file1 &&
 804        echo world >non/git/sub/file2 &&
 805        {
 806                echo file1:hello &&
 807                echo sub/file2:world
 808        } >non/expect.full &&
 809        echo file2:world >non/expect.sub &&
 810        (
 811                GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
 812                export GIT_CEILING_DIRECTORIES &&
 813                cd non/git &&
 814                test_must_fail git grep o &&
 815                git grep --no-index o >../actual.full &&
 816                test_cmp ../expect.full ../actual.full &&
 817                cd sub &&
 818                test_must_fail git grep o &&
 819                git grep --no-index o >../../actual.sub &&
 820                test_cmp ../../expect.sub ../../actual.sub
 821        ) &&
 822
 823        echo ".*o*" >non/git/.gitignore &&
 824        (
 825                GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
 826                export GIT_CEILING_DIRECTORIES &&
 827                cd non/git &&
 828                test_must_fail git grep o &&
 829                git grep --no-index --exclude-standard o >../actual.full &&
 830                test_cmp ../expect.full ../actual.full &&
 831
 832                {
 833                        echo ".gitignore:.*o*" &&
 834                        cat ../expect.full
 835                } >../expect.with.ignored &&
 836                git grep --no-index --no-exclude o >../actual.full &&
 837                test_cmp ../expect.with.ignored ../actual.full
 838        )
 839'
 840
 841test_expect_success 'outside of git repository with fallbackToNoIndex' '
 842        rm -fr non &&
 843        mkdir -p non/git/sub &&
 844        echo hello >non/git/file1 &&
 845        echo world >non/git/sub/file2 &&
 846        cat <<-\EOF >non/expect.full &&
 847        file1:hello
 848        sub/file2:world
 849        EOF
 850        echo file2:world >non/expect.sub &&
 851        (
 852                GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
 853                export GIT_CEILING_DIRECTORIES &&
 854                cd non/git &&
 855                test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
 856                git -c grep.fallbackToNoIndex=true grep o >../actual.full &&
 857                test_cmp ../expect.full ../actual.full &&
 858                cd sub &&
 859                test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
 860                git -c grep.fallbackToNoIndex=true grep o >../../actual.sub &&
 861                test_cmp ../../expect.sub ../../actual.sub
 862        ) &&
 863
 864        echo ".*o*" >non/git/.gitignore &&
 865        (
 866                GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
 867                export GIT_CEILING_DIRECTORIES &&
 868                cd non/git &&
 869                test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
 870                git -c grep.fallbackToNoIndex=true grep --exclude-standard o >../actual.full &&
 871                test_cmp ../expect.full ../actual.full &&
 872
 873                {
 874                        echo ".gitignore:.*o*" &&
 875                        cat ../expect.full
 876                } >../expect.with.ignored &&
 877                git -c grep.fallbackToNoIndex grep --no-exclude o >../actual.full &&
 878                test_cmp ../expect.with.ignored ../actual.full
 879        )
 880'
 881
 882test_expect_success 'inside git repository but with --no-index' '
 883        rm -fr is &&
 884        mkdir -p is/git/sub &&
 885        echo hello >is/git/file1 &&
 886        echo world >is/git/sub/file2 &&
 887        echo ".*o*" >is/git/.gitignore &&
 888        {
 889                echo file1:hello &&
 890                echo sub/file2:world
 891        } >is/expect.unignored &&
 892        {
 893                echo ".gitignore:.*o*" &&
 894                cat is/expect.unignored
 895        } >is/expect.full &&
 896        : >is/expect.empty &&
 897        echo file2:world >is/expect.sub &&
 898        (
 899                cd is/git &&
 900                git init &&
 901                test_must_fail git grep o >../actual.full &&
 902                test_cmp ../expect.empty ../actual.full &&
 903
 904                git grep --untracked o >../actual.unignored &&
 905                test_cmp ../expect.unignored ../actual.unignored &&
 906
 907                git grep --no-index o >../actual.full &&
 908                test_cmp ../expect.full ../actual.full &&
 909
 910                git grep --no-index --exclude-standard o >../actual.unignored &&
 911                test_cmp ../expect.unignored ../actual.unignored &&
 912
 913                cd sub &&
 914                test_must_fail git grep o >../../actual.sub &&
 915                test_cmp ../../expect.empty ../../actual.sub &&
 916
 917                git grep --no-index o >../../actual.sub &&
 918                test_cmp ../../expect.sub ../../actual.sub &&
 919
 920                git grep --untracked o >../../actual.sub &&
 921                test_cmp ../../expect.sub ../../actual.sub
 922        )
 923'
 924
 925test_expect_success 'grep --no-index descends into repos, but not .git' '
 926        rm -fr non &&
 927        mkdir -p non/git &&
 928        (
 929                GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
 930                export GIT_CEILING_DIRECTORIES &&
 931                cd non/git &&
 932
 933                echo magic >file &&
 934                git init repo &&
 935                (
 936                        cd repo &&
 937                        echo magic >file &&
 938                        git add file &&
 939                        git commit -m foo &&
 940                        echo magic >.git/file
 941                ) &&
 942
 943                cat >expect <<-\EOF &&
 944                file
 945                repo/file
 946                EOF
 947                git grep -l --no-index magic >actual &&
 948                test_cmp expect actual
 949        )
 950'
 951
 952test_expect_success 'setup double-dash tests' '
 953cat >double-dash <<EOF &&
 954--
 955->
 956other
 957EOF
 958git add double-dash
 959'
 960
 961cat >expected <<EOF
 962double-dash:->
 963EOF
 964test_expect_success 'grep -- pattern' '
 965        git grep -- "->" >actual &&
 966        test_cmp expected actual
 967'
 968test_expect_success 'grep -- pattern -- pathspec' '
 969        git grep -- "->" -- double-dash >actual &&
 970        test_cmp expected actual
 971'
 972test_expect_success 'grep -e pattern -- path' '
 973        git grep -e "->" -- double-dash >actual &&
 974        test_cmp expected actual
 975'
 976
 977cat >expected <<EOF
 978double-dash:--
 979EOF
 980test_expect_success 'grep -e -- -- path' '
 981        git grep -e -- -- double-dash >actual &&
 982        test_cmp expected actual
 983'
 984
 985test_expect_success 'dashdash disambiguates rev as rev' '
 986        test_when_finished "rm -f master" &&
 987        echo content >master &&
 988        echo master:hello.c >expect &&
 989        git grep -l o master -- hello.c >actual &&
 990        test_cmp expect actual
 991'
 992
 993test_expect_success 'dashdash disambiguates pathspec as pathspec' '
 994        test_when_finished "git rm -f master" &&
 995        echo content >master &&
 996        git add master &&
 997        echo master:content >expect &&
 998        git grep o -- master >actual &&
 999        test_cmp expect actual
1000'
1001
1002test_expect_success 'report bogus arg without dashdash' '
1003        test_must_fail git grep o does-not-exist
1004'
1005
1006test_expect_success 'report bogus rev with dashdash' '
1007        test_must_fail git grep o hello.c --
1008'
1009
1010test_expect_success 'allow non-existent path with dashdash' '
1011        # We need a real match so grep exits with success.
1012        tree=$(git ls-tree HEAD |
1013               sed s/hello.c/not-in-working-tree/ |
1014               git mktree) &&
1015        git grep o "$tree" -- not-in-working-tree
1016'
1017
1018test_expect_success 'grep --no-index pattern -- path' '
1019        rm -fr non &&
1020        mkdir -p non/git &&
1021        (
1022                GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1023                export GIT_CEILING_DIRECTORIES &&
1024                cd non/git &&
1025                echo hello >hello &&
1026                echo goodbye >goodbye &&
1027                echo hello:hello >expect &&
1028                git grep --no-index o -- hello >actual &&
1029                test_cmp expect actual
1030        )
1031'
1032
1033test_expect_success 'grep --no-index complains of revs' '
1034        test_must_fail git grep --no-index o master -- 2>err &&
1035        test_i18ngrep "cannot be used with revs" err
1036'
1037
1038test_expect_success 'grep --no-index prefers paths to revs' '
1039        test_when_finished "rm -f master" &&
1040        echo content >master &&
1041        echo master:content >expect &&
1042        git grep --no-index o master >actual &&
1043        test_cmp expect actual
1044'
1045
1046test_expect_success 'grep --no-index does not "diagnose" revs' '
1047        test_must_fail git grep --no-index o :1:hello.c 2>err &&
1048        test_i18ngrep ! -i "did you mean" err
1049'
1050
1051cat >expected <<EOF
1052hello.c:int main(int argc, const char **argv)
1053hello.c:        printf("Hello world.\n");
1054EOF
1055
1056test_expect_success LIBPCRE 'grep --perl-regexp pattern' '
1057        git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1058        test_cmp expected actual
1059'
1060
1061test_expect_success LIBPCRE 'grep -P pattern' '
1062        git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1063        test_cmp expected actual
1064'
1065
1066test_expect_success 'grep pattern with grep.extendedRegexp=true' '
1067        >empty &&
1068        test_must_fail git -c grep.extendedregexp=true \
1069                grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1070        test_cmp empty actual
1071'
1072
1073test_expect_success LIBPCRE 'grep -P pattern with grep.extendedRegexp=true' '
1074        git -c grep.extendedregexp=true \
1075                grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1076        test_cmp expected actual
1077'
1078
1079test_expect_success LIBPCRE 'grep -P -v pattern' '
1080        {
1081                echo "ab:a+b*c"
1082                echo "ab:a+bc"
1083        } >expected &&
1084        git grep -P -v "abc" ab >actual &&
1085        test_cmp expected actual
1086'
1087
1088test_expect_success LIBPCRE 'grep -P -i pattern' '
1089        cat >expected <<-EOF &&
1090        hello.c:        printf("Hello world.\n");
1091        EOF
1092        git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
1093        test_cmp expected actual
1094'
1095
1096test_expect_success LIBPCRE 'grep -P -w pattern' '
1097        {
1098                echo "hello_world:Hello world"
1099                echo "hello_world:HeLLo world"
1100        } >expected &&
1101        git grep -P -w "He((?i)ll)o" hello_world >actual &&
1102        test_cmp expected actual
1103'
1104
1105test_expect_success 'grep -G invalidpattern properly dies ' '
1106        test_must_fail git grep -G "a["
1107'
1108
1109test_expect_success 'grep invalidpattern properly dies with grep.patternType=basic' '
1110        test_must_fail git -c grep.patterntype=basic grep "a["
1111'
1112
1113test_expect_success 'grep -E invalidpattern properly dies ' '
1114        test_must_fail git grep -E "a["
1115'
1116
1117test_expect_success 'grep invalidpattern properly dies with grep.patternType=extended' '
1118        test_must_fail git -c grep.patterntype=extended grep "a["
1119'
1120
1121test_expect_success LIBPCRE 'grep -P invalidpattern properly dies ' '
1122        test_must_fail git grep -P "a["
1123'
1124
1125test_expect_success LIBPCRE 'grep invalidpattern properly dies with grep.patternType=perl' '
1126        test_must_fail git -c grep.patterntype=perl grep "a["
1127'
1128
1129test_expect_success 'grep -G -E -F pattern' '
1130        echo "ab:a+b*c" >expected &&
1131        git grep -G -E -F "a+b*c" ab >actual &&
1132        test_cmp expected actual
1133'
1134
1135test_expect_success 'grep pattern with grep.patternType=basic, =extended, =fixed' '
1136        echo "ab:a+b*c" >expected &&
1137        git \
1138                -c grep.patterntype=basic \
1139                -c grep.patterntype=extended \
1140                -c grep.patterntype=fixed \
1141                grep "a+b*c" ab >actual &&
1142        test_cmp expected actual
1143'
1144
1145test_expect_success 'grep -E -F -G pattern' '
1146        echo "ab:a+bc" >expected &&
1147        git grep -E -F -G "a+b*c" ab >actual &&
1148        test_cmp expected actual
1149'
1150
1151test_expect_success 'grep pattern with grep.patternType=extended, =fixed, =basic' '
1152        echo "ab:a+bc" >expected &&
1153        git \
1154                -c grep.patterntype=extended \
1155                -c grep.patterntype=fixed \
1156                -c grep.patterntype=basic \
1157                grep "a+b*c" ab >actual &&
1158        test_cmp expected actual
1159'
1160
1161test_expect_success 'grep -F -G -E pattern' '
1162        echo "ab:abc" >expected &&
1163        git grep -F -G -E "a+b*c" ab >actual &&
1164        test_cmp expected actual
1165'
1166
1167test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended' '
1168        echo "ab:abc" >expected &&
1169        git \
1170                -c grep.patterntype=fixed \
1171                -c grep.patterntype=basic \
1172                -c grep.patterntype=extended \
1173                grep "a+b*c" ab >actual &&
1174        test_cmp expected actual
1175'
1176
1177test_expect_success 'grep -G -F -P -E pattern' '
1178        echo "d0:d" >expected &&
1179        git grep -G -F -P -E "[\d]" d0 >actual &&
1180        test_cmp expected actual
1181'
1182
1183test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
1184        echo "d0:d" >expected &&
1185        git \
1186                -c grep.patterntype=fixed \
1187                -c grep.patterntype=basic \
1188                -c grep.patterntype=perl \
1189                -c grep.patterntype=extended \
1190                grep "[\d]" d0 >actual &&
1191        test_cmp expected actual
1192'
1193
1194test_expect_success LIBPCRE 'grep -G -F -E -P pattern' '
1195        echo "d0:0" >expected &&
1196        git grep -G -F -E -P "[\d]" d0 >actual &&
1197        test_cmp expected actual
1198'
1199
1200test_expect_success LIBPCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1201        echo "d0:0" >expected &&
1202        git \
1203                -c grep.patterntype=fixed \
1204                -c grep.patterntype=basic \
1205                -c grep.patterntype=extended \
1206                -c grep.patterntype=perl \
1207                grep "[\d]" d0 >actual &&
1208        test_cmp expected actual
1209'
1210
1211test_expect_success LIBPCRE 'grep -P pattern with grep.patternType=fixed' '
1212        echo "ab:a+b*c" >expected &&
1213        git \
1214                -c grep.patterntype=fixed \
1215                grep -P "a\x{2b}b\x{2a}c" ab >actual &&
1216        test_cmp expected actual
1217'
1218
1219test_expect_success 'grep -F pattern with grep.patternType=basic' '
1220        echo "ab:a+b*c" >expected &&
1221        git \
1222                -c grep.patterntype=basic \
1223                grep -F "*c" ab >actual &&
1224        test_cmp expected actual
1225'
1226
1227test_expect_success 'grep -G pattern with grep.patternType=fixed' '
1228        {
1229                echo "ab:a+b*c"
1230                echo "ab:a+bc"
1231        } >expected &&
1232        git \
1233                -c grep.patterntype=fixed \
1234                grep -G "a+b" ab >actual &&
1235        test_cmp expected actual
1236'
1237
1238test_expect_success 'grep -E pattern with grep.patternType=fixed' '
1239        {
1240                echo "ab:a+b*c"
1241                echo "ab:a+bc"
1242                echo "ab:abc"
1243        } >expected &&
1244        git \
1245                -c grep.patterntype=fixed \
1246                grep -E "a+" ab >actual &&
1247        test_cmp expected actual
1248'
1249
1250cat >expected <<EOF
1251hello.c<RED>:<RESET>int main(int argc, const char **argv)
1252hello.c<RED>-<RESET>{
1253<RED>--<RESET>
1254hello.c<RED>:<RESET>    /* char ?? */
1255hello.c<RED>-<RESET>}
1256<RED>--<RESET>
1257hello_world<RED>:<RESET>Hello_world
1258hello_world<RED>-<RESET>HeLLo_world
1259EOF
1260
1261test_expect_success 'grep --color, separator' '
1262        test_config color.grep.context          normal &&
1263        test_config color.grep.filename         normal &&
1264        test_config color.grep.function         normal &&
1265        test_config color.grep.linenumber       normal &&
1266        test_config color.grep.match            normal &&
1267        test_config color.grep.selected         normal &&
1268        test_config color.grep.separator        red &&
1269
1270        git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
1271        test_decode_color >actual &&
1272        test_cmp expected actual
1273'
1274
1275cat >expected <<EOF
1276hello.c:int main(int argc, const char **argv)
1277hello.c:        /* char ?? */
1278
1279hello_world:Hello_world
1280EOF
1281
1282test_expect_success 'grep --break' '
1283        git grep --break -e char -e lo_w hello.c hello_world >actual &&
1284        test_cmp expected actual
1285'
1286
1287cat >expected <<EOF
1288hello.c:int main(int argc, const char **argv)
1289hello.c-{
1290--
1291hello.c:        /* char ?? */
1292hello.c-}
1293
1294hello_world:Hello_world
1295hello_world-HeLLo_world
1296EOF
1297
1298test_expect_success 'grep --break with context' '
1299        git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
1300        test_cmp expected actual
1301'
1302
1303cat >expected <<EOF
1304hello.c
1305int main(int argc, const char **argv)
1306        /* char ?? */
1307hello_world
1308Hello_world
1309EOF
1310
1311test_expect_success 'grep --heading' '
1312        git grep --heading -e char -e lo_w hello.c hello_world >actual &&
1313        test_cmp expected actual
1314'
1315
1316cat >expected <<EOF
1317<BOLD;GREEN>hello.c<RESET>
13184:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
13198:      /* <BLACK;BYELLOW>char<RESET> ?? */
1320
1321<BOLD;GREEN>hello_world<RESET>
13223:Hel<BLACK;BYELLOW>lo_w<RESET>orld
1323EOF
1324
1325test_expect_success 'mimic ack-grep --group' '
1326        test_config color.grep.context          normal &&
1327        test_config color.grep.filename         "bold green" &&
1328        test_config color.grep.function         normal &&
1329        test_config color.grep.linenumber       normal &&
1330        test_config color.grep.match            "black yellow" &&
1331        test_config color.grep.selected         normal &&
1332        test_config color.grep.separator        normal &&
1333
1334        git grep --break --heading -n --color \
1335                -e char -e lo_w hello.c hello_world |
1336        test_decode_color >actual &&
1337        test_cmp expected actual
1338'
1339
1340cat >expected <<EOF
1341space: line with leading space1
1342space: line with leading space2
1343space: line with leading space3
1344EOF
1345
1346test_expect_success LIBPCRE 'grep -E "^ "' '
1347        git grep -E "^ " space >actual &&
1348        test_cmp expected actual
1349'
1350
1351test_expect_success LIBPCRE 'grep -P "^ "' '
1352        git grep -P "^ " space >actual &&
1353        test_cmp expected actual
1354'
1355
1356cat >expected <<EOF
1357space-line without leading space1
1358space: line <RED>with <RESET>leading space1
1359space: line <RED>with <RESET>leading <RED>space2<RESET>
1360space: line <RED>with <RESET>leading space3
1361space:line without leading <RED>space2<RESET>
1362EOF
1363
1364test_expect_success 'grep --color -e A -e B with context' '
1365        test_config color.grep.context          normal &&
1366        test_config color.grep.filename         normal &&
1367        test_config color.grep.function         normal &&
1368        test_config color.grep.linenumber       normal &&
1369        test_config color.grep.matchContext     normal &&
1370        test_config color.grep.matchSelected    red &&
1371        test_config color.grep.selected         normal &&
1372        test_config color.grep.separator        normal &&
1373
1374        git grep --color=always -C2 -e "with " -e space2  space |
1375        test_decode_color >actual &&
1376        test_cmp expected actual
1377'
1378
1379cat >expected <<EOF
1380space-line without leading space1
1381space- line with leading space1
1382space: line <RED>with <RESET>leading <RED>space2<RESET>
1383space- line with leading space3
1384space-line without leading space2
1385EOF
1386
1387test_expect_success 'grep --color -e A --and -e B with context' '
1388        test_config color.grep.context          normal &&
1389        test_config color.grep.filename         normal &&
1390        test_config color.grep.function         normal &&
1391        test_config color.grep.linenumber       normal &&
1392        test_config color.grep.matchContext     normal &&
1393        test_config color.grep.matchSelected    red &&
1394        test_config color.grep.selected         normal &&
1395        test_config color.grep.separator        normal &&
1396
1397        git grep --color=always -C2 -e "with " --and -e space2  space |
1398        test_decode_color >actual &&
1399        test_cmp expected actual
1400'
1401
1402cat >expected <<EOF
1403space-line without leading space1
1404space: line <RED>with <RESET>leading space1
1405space- line with leading space2
1406space: line <RED>with <RESET>leading space3
1407space-line without leading space2
1408EOF
1409
1410test_expect_success 'grep --color -e A --and --not -e B with context' '
1411        test_config color.grep.context          normal &&
1412        test_config color.grep.filename         normal &&
1413        test_config color.grep.function         normal &&
1414        test_config color.grep.linenumber       normal &&
1415        test_config color.grep.matchContext     normal &&
1416        test_config color.grep.matchSelected    red &&
1417        test_config color.grep.selected         normal &&
1418        test_config color.grep.separator        normal &&
1419
1420        git grep --color=always -C2 -e "with " --and --not -e space2  space |
1421        test_decode_color >actual &&
1422        test_cmp expected actual
1423'
1424
1425cat >expected <<EOF
1426hello.c-
1427hello.c=int main(int argc, const char **argv)
1428hello.c-{
1429hello.c:        pr<RED>int<RESET>f("<RED>Hello<RESET> world.\n");
1430hello.c-        return 0;
1431hello.c-        /* char ?? */
1432hello.c-}
1433EOF
1434
1435test_expect_success 'grep --color -e A --and -e B -p with context' '
1436        test_config color.grep.context          normal &&
1437        test_config color.grep.filename         normal &&
1438        test_config color.grep.function         normal &&
1439        test_config color.grep.linenumber       normal &&
1440        test_config color.grep.matchContext     normal &&
1441        test_config color.grep.matchSelected    red &&
1442        test_config color.grep.selected         normal &&
1443        test_config color.grep.separator        normal &&
1444
1445        git grep --color=always -p -C3 -e int --and -e Hello --no-index hello.c |
1446        test_decode_color >actual &&
1447        test_cmp expected actual
1448'
1449
1450test_expect_success 'grep can find things only in the work tree' '
1451        : >work-tree-only &&
1452        git add work-tree-only &&
1453        test_when_finished "git rm -f work-tree-only" &&
1454        echo "find in work tree" >work-tree-only &&
1455        git grep --quiet "find in work tree" &&
1456        test_must_fail git grep --quiet --cached "find in work tree" &&
1457        test_must_fail git grep --quiet "find in work tree" HEAD
1458'
1459
1460test_expect_success 'grep can find things only in the work tree (i-t-a)' '
1461        echo "intend to add this" >intend-to-add &&
1462        git add -N intend-to-add &&
1463        test_when_finished "git rm -f intend-to-add" &&
1464        git grep --quiet "intend to add this" &&
1465        test_must_fail git grep --quiet --cached "intend to add this" &&
1466        test_must_fail git grep --quiet "intend to add this" HEAD
1467'
1468
1469test_expect_success 'grep does not search work tree with assume unchanged' '
1470        echo "intend to add this" >intend-to-add &&
1471        git add -N intend-to-add &&
1472        git update-index --assume-unchanged intend-to-add &&
1473        test_when_finished "git rm -f intend-to-add" &&
1474        test_must_fail git grep --quiet "intend to add this" &&
1475        test_must_fail git grep --quiet --cached "intend to add this" &&
1476        test_must_fail git grep --quiet "intend to add this" HEAD
1477'
1478
1479test_expect_success 'grep can find things only in the index' '
1480        echo "only in the index" >cache-this &&
1481        git add cache-this &&
1482        rm cache-this &&
1483        test_when_finished "git rm --cached cache-this" &&
1484        test_must_fail git grep --quiet "only in the index" &&
1485        git grep --quiet --cached "only in the index" &&
1486        test_must_fail git grep --quiet "only in the index" HEAD
1487'
1488
1489test_expect_success 'grep does not report i-t-a with -L --cached' '
1490        echo "intend to add this" >intend-to-add &&
1491        git add -N intend-to-add &&
1492        test_when_finished "git rm -f intend-to-add" &&
1493        git ls-files | grep -v "^intend-to-add\$" >expected &&
1494        git grep -L --cached "nonexistent_string" >actual &&
1495        test_cmp expected actual
1496'
1497
1498test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
1499        echo "intend to add this" >intend-to-add-assume-unchanged &&
1500        git add -N intend-to-add-assume-unchanged &&
1501        test_when_finished "git rm -f intend-to-add-assume-unchanged" &&
1502        git update-index --assume-unchanged intend-to-add-assume-unchanged &&
1503        git ls-files | grep -v "^intend-to-add-assume-unchanged\$" >expected &&
1504        git grep -L "nonexistent_string" >actual &&
1505        test_cmp expected actual
1506'
1507
1508test_done