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