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