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