30eaa9a54b8b7ea855a2cb781202cfc79e29f217
   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 <stdio.h>
  13int main(int argc, const char **argv)
  14{
  15        printf("Hello world.\n");
  16        return 0;
  17        /* char ?? */
  18}
  19EOF
  20
  21test_expect_success setup '
  22        {
  23                echo foo mmap bar
  24                echo foo_mmap bar
  25                echo foo_mmap bar mmap
  26                echo foo mmap bar_mmap
  27                echo foo_mmap bar mmap baz
  28        } >file &&
  29        {
  30                echo Hello world
  31                echo HeLLo world
  32                echo Hello_world
  33                echo HeLLo_world
  34        } >hello_world &&
  35        {
  36                echo "a+b*c"
  37                echo "a+bc"
  38                echo "abc"
  39        } >ab &&
  40        echo vvv >v &&
  41        echo ww w >w &&
  42        echo x x xx x >x &&
  43        echo y yy >y &&
  44        echo zzz > z &&
  45        mkdir t &&
  46        echo test >t/t &&
  47        echo vvv >t/v &&
  48        mkdir t/a &&
  49        echo vvv >t/a/v &&
  50        {
  51                echo "line without leading space1"
  52                echo " line with leading space1"
  53                echo " line with leading space2"
  54                echo " line with leading space3"
  55                echo "line without leading space2"
  56        } >space &&
  57        git add . &&
  58        test_tick &&
  59        git commit -m initial
  60'
  61
  62test_expect_success 'grep should not segfault with a bad input' '
  63        test_must_fail git grep "("
  64'
  65
  66for H in HEAD ''
  67do
  68        case "$H" in
  69        HEAD)   HC='HEAD:' L='HEAD' ;;
  70        '')     HC= L='in working tree' ;;
  71        esac
  72
  73        test_expect_success "grep -w $L" '
  74                {
  75                        echo ${HC}file:1:foo mmap bar
  76                        echo ${HC}file:3:foo_mmap bar mmap
  77                        echo ${HC}file:4:foo mmap bar_mmap
  78                        echo ${HC}file:5:foo_mmap bar mmap baz
  79                } >expected &&
  80                git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
  81                test_cmp expected actual
  82        '
  83
  84        test_expect_success "grep -w $L" '
  85                {
  86                        echo ${HC}file:1:foo mmap bar
  87                        echo ${HC}file:3:foo_mmap bar mmap
  88                        echo ${HC}file:4:foo mmap bar_mmap
  89                        echo ${HC}file:5:foo_mmap bar mmap baz
  90                } >expected &&
  91                git -c grep.linenumber=true grep -w -e mmap $H >actual &&
  92                test_cmp expected actual
  93        '
  94
  95        test_expect_success "grep -w $L" '
  96                {
  97                        echo ${HC}file:foo mmap bar
  98                        echo ${HC}file:foo_mmap bar mmap
  99                        echo ${HC}file:foo mmap bar_mmap
 100                        echo ${HC}file:foo_mmap bar mmap baz
 101                } >expected &&
 102                git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
 103                test_cmp expected actual
 104        '
 105
 106        test_expect_success "grep -w $L (w)" '
 107                : >expected &&
 108                test_must_fail git grep -n -w -e "^w" >actual &&
 109                test_cmp expected actual
 110        '
 111
 112        test_expect_success "grep -w $L (x)" '
 113                {
 114                        echo ${HC}x:1:x x xx x
 115                } >expected &&
 116                git grep -n -w -e "x xx* x" $H >actual &&
 117                test_cmp expected actual
 118        '
 119
 120        test_expect_success "grep -w $L (y-1)" '
 121                {
 122                        echo ${HC}y:1:y yy
 123                } >expected &&
 124                git grep -n -w -e "^y" $H >actual &&
 125                test_cmp expected actual
 126        '
 127
 128        test_expect_success "grep -w $L (y-2)" '
 129                : >expected &&
 130                if git grep -n -w -e "^y y" $H >actual
 131                then
 132                        echo should not have matched
 133                        cat actual
 134                        false
 135                else
 136                        test_cmp expected actual
 137                fi
 138        '
 139
 140        test_expect_success "grep -w $L (z)" '
 141                : >expected &&
 142                if git grep -n -w -e "^z" $H >actual
 143                then
 144                        echo should not have matched
 145                        cat actual
 146                        false
 147                else
 148                        test_cmp expected actual
 149                fi
 150        '
 151
 152        test_expect_success "grep $L (t-1)" '
 153                echo "${HC}t/t:1:test" >expected &&
 154                git grep -n -e test $H >actual &&
 155                test_cmp expected actual
 156        '
 157
 158        test_expect_success "grep $L (t-2)" '
 159                echo "${HC}t:1:test" >expected &&
 160                (
 161                        cd t &&
 162                        git grep -n -e test $H
 163                ) >actual &&
 164                test_cmp expected actual
 165        '
 166
 167        test_expect_success "grep $L (t-3)" '
 168                echo "${HC}t/t:1:test" >expected &&
 169                (
 170                        cd t &&
 171                        git grep --full-name -n -e test $H
 172                ) >actual &&
 173                test_cmp expected actual
 174        '
 175
 176        test_expect_success "grep -c $L (no /dev/null)" '
 177                ! git grep -c test $H | grep /dev/null
 178        '
 179
 180        test_expect_success "grep --max-depth -1 $L" '
 181                {
 182                        echo ${HC}t/a/v:1:vvv
 183                        echo ${HC}t/v:1:vvv
 184                        echo ${HC}v:1:vvv
 185                } >expected &&
 186                git grep --max-depth -1 -n -e vvv $H >actual &&
 187                test_cmp expected actual
 188        '
 189
 190        test_expect_success "grep --max-depth 0 $L" '
 191                {
 192                        echo ${HC}v:1:vvv
 193                } >expected &&
 194                git grep --max-depth 0 -n -e vvv $H >actual &&
 195                test_cmp expected actual
 196        '
 197
 198        test_expect_success "grep --max-depth 0 -- '*' $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 0 -n -e vvv $H -- "*" >actual &&
 205                test_cmp expected actual
 206        '
 207
 208        test_expect_success "grep --max-depth 1 $L" '
 209                {
 210                        echo ${HC}t/v:1:vvv
 211                        echo ${HC}v:1:vvv
 212                } >expected &&
 213                git grep --max-depth 1 -n -e vvv $H >actual &&
 214                test_cmp expected actual
 215        '
 216
 217        test_expect_success "grep --max-depth 0 -- t $L" '
 218                {
 219                        echo ${HC}t/v:1:vvv
 220                } >expected &&
 221                git grep --max-depth 0 -n -e vvv $H -- t >actual &&
 222                test_cmp expected actual
 223        '
 224
 225        test_expect_success "grep --max-depth 0 -- . t $L" '
 226                {
 227                        echo ${HC}t/v:1:vvv
 228                        echo ${HC}v:1:vvv
 229                } >expected &&
 230                git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
 231                test_cmp expected actual
 232        '
 233
 234        test_expect_success "grep --max-depth 0 -- t . $L" '
 235                {
 236                        echo ${HC}t/v:1:vvv
 237                        echo ${HC}v:1:vvv
 238                } >expected &&
 239                git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
 240                test_cmp expected actual
 241        '
 242        test_expect_success "grep $L with grep.extendedRegexp=false" '
 243                echo "ab:a+bc" >expected &&
 244                git -c grep.extendedRegexp=false grep "a+b*c" ab >actual &&
 245                test_cmp expected actual
 246        '
 247
 248        test_expect_success "grep $L with grep.extendedRegexp=true" '
 249                echo "ab:abc" >expected &&
 250                git -c grep.extendedRegexp=true grep "a+b*c" ab >actual &&
 251                test_cmp expected actual
 252        '
 253
 254        test_expect_success "grep $L with grep.patterntype=basic" '
 255                echo "ab:a+bc" >expected &&
 256                git -c grep.patterntype=basic grep "a+b*c" ab >actual &&
 257                test_cmp expected actual
 258        '
 259
 260        test_expect_success "grep $L with grep.patterntype=extended" '
 261                echo "ab:abc" >expected &&
 262                git -c grep.patterntype=extended grep "a+b*c" ab >actual &&
 263                test_cmp expected actual
 264        '
 265
 266        test_expect_success "grep $L with grep.patterntype=fixed" '
 267                echo "ab:a+b*c" >expected &&
 268                git -c grep.patterntype=fixed grep "a+b*c" ab >actual &&
 269                test_cmp expected actual
 270        '
 271
 272        test_expect_success LIBPCRE "grep $L with grep.patterntype=perl" '
 273                echo "ab:a+b*c" >expected &&
 274                git -c grep.patterntype=perl grep "a\x{2b}b\x{2a}c" ab >actual &&
 275                test_cmp expected actual
 276        '
 277
 278        test_expect_success "grep $L with grep.patternType=default and grep.extendedRegexp=true" '
 279                echo "ab:abc" >expected &&
 280                git \
 281                        -c grep.patternType=default \
 282                        -c grep.extendedRegexp=true \
 283                        grep "a+b*c" ab >actual &&
 284                test_cmp expected actual
 285        '
 286
 287        test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=default" '
 288                echo "ab:abc" >expected &&
 289                git \
 290                        -c grep.extendedRegexp=true \
 291                        -c grep.patternType=default \
 292                        grep "a+b*c" ab >actual &&
 293                test_cmp expected actual
 294        '
 295
 296        test_expect_success 'grep $L with grep.patternType=extended and grep.extendedRegexp=false' '
 297                echo "ab:abc" >expected &&
 298                git \
 299                        -c grep.patternType=extended \
 300                        -c grep.extendedRegexp=false \
 301                        grep "a+b*c" ab >actual &&
 302                test_cmp expected actual
 303        '
 304
 305        test_expect_success 'grep $L with grep.patternType=basic and grep.extendedRegexp=true' '
 306                echo "ab:a+bc" >expected &&
 307                git \
 308                        -c grep.patternType=basic \
 309                        -c grep.extendedRegexp=true \
 310                        grep "a+b*c" ab >actual &&
 311                test_cmp expected actual
 312        '
 313
 314        test_expect_success 'grep $L with grep.extendedRegexp=false and grep.patternType=extended' '
 315                echo "ab:abc" >expected &&
 316                git \
 317                        -c grep.extendedRegexp=false \
 318                        -c grep.patternType=extended \
 319                        grep "a+b*c" ab >actual &&
 320                test_cmp expected actual
 321        '
 322
 323        test_expect_success 'grep $L with grep.extendedRegexp=true and grep.patternType=basic' '
 324                echo "ab:a+bc" >expected &&
 325                git \
 326                        -c grep.extendedRegexp=true \
 327                        -c grep.patternType=basic \
 328                        grep "a+b*c" ab >actual &&
 329                test_cmp expected actual
 330        '
 331done
 332
 333cat >expected <<EOF
 334file
 335EOF
 336test_expect_success 'grep -l -C' '
 337        git grep -l -C1 foo >actual &&
 338        test_cmp expected actual
 339'
 340
 341cat >expected <<EOF
 342file:5
 343EOF
 344test_expect_success 'grep -l -C' '
 345        git grep -c -C1 foo >actual &&
 346        test_cmp expected actual
 347'
 348
 349test_expect_success 'grep -L -C' '
 350        git ls-files >expected &&
 351        git grep -L -C1 nonexistent_string >actual &&
 352        test_cmp expected actual
 353'
 354
 355cat >expected <<EOF
 356file:foo mmap bar_mmap
 357EOF
 358
 359test_expect_success 'grep -e A --and -e B' '
 360        git grep -e "foo mmap" --and -e bar_mmap >actual &&
 361        test_cmp expected actual
 362'
 363
 364cat >expected <<EOF
 365file:foo_mmap bar mmap
 366file:foo_mmap bar mmap baz
 367EOF
 368
 369
 370test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
 371        git grep \( -e foo_ --or -e baz \) \
 372                --and -e " mmap" >actual &&
 373        test_cmp expected actual
 374'
 375
 376cat >expected <<EOF
 377file:foo mmap bar
 378EOF
 379
 380test_expect_success 'grep -e A --and --not -e B' '
 381        git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
 382        test_cmp expected actual
 383'
 384
 385test_expect_success 'grep should ignore GREP_OPTIONS' '
 386        GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
 387        test_cmp expected actual
 388'
 389
 390test_expect_success 'grep -f, non-existent file' '
 391        test_must_fail git grep -f patterns
 392'
 393
 394cat >expected <<EOF
 395file:foo mmap bar
 396file:foo_mmap bar
 397file:foo_mmap bar mmap
 398file:foo mmap bar_mmap
 399file:foo_mmap bar mmap baz
 400EOF
 401
 402cat >pattern <<EOF
 403mmap
 404EOF
 405
 406test_expect_success 'grep -f, one pattern' '
 407        git grep -f pattern >actual &&
 408        test_cmp expected actual
 409'
 410
 411cat >expected <<EOF
 412file:foo mmap bar
 413file:foo_mmap bar
 414file:foo_mmap bar mmap
 415file:foo mmap bar_mmap
 416file:foo_mmap bar mmap baz
 417t/a/v:vvv
 418t/v:vvv
 419v:vvv
 420EOF
 421
 422cat >patterns <<EOF
 423mmap
 424vvv
 425EOF
 426
 427test_expect_success 'grep -f, multiple patterns' '
 428        git grep -f patterns >actual &&
 429        test_cmp expected actual
 430'
 431
 432test_expect_success 'grep, multiple patterns' '
 433        git grep "$(cat patterns)" >actual &&
 434        test_cmp expected actual
 435'
 436
 437cat >expected <<EOF
 438file:foo mmap bar
 439file:foo_mmap bar
 440file:foo_mmap bar mmap
 441file:foo mmap bar_mmap
 442file:foo_mmap bar mmap baz
 443t/a/v:vvv
 444t/v:vvv
 445v:vvv
 446EOF
 447
 448cat >patterns <<EOF
 449
 450mmap
 451
 452vvv
 453
 454EOF
 455
 456test_expect_success 'grep -f, ignore empty lines' '
 457        git grep -f patterns >actual &&
 458        test_cmp expected actual
 459'
 460
 461test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
 462        git grep -f - <patterns >actual &&
 463        test_cmp expected actual
 464'
 465
 466cat >expected <<EOF
 467y:y yy
 468--
 469z:zzz
 470EOF
 471
 472test_expect_success 'grep -q, silently report matches' '
 473        >empty &&
 474        git grep -q mmap >actual &&
 475        test_cmp empty actual &&
 476        test_must_fail git grep -q qfwfq >actual &&
 477        test_cmp empty actual
 478'
 479
 480test_expect_success 'grep -C1 hunk mark between files' '
 481        git grep -C1 "^[yz]" >actual &&
 482        test_cmp expected actual
 483'
 484
 485test_expect_success 'log grep setup' '
 486        echo a >>file &&
 487        test_tick &&
 488        GIT_AUTHOR_NAME="With * Asterisk" \
 489        GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
 490        git commit -a -m "second" &&
 491
 492        echo a >>file &&
 493        test_tick &&
 494        git commit -a -m "third" &&
 495
 496        echo a >>file &&
 497        test_tick &&
 498        GIT_AUTHOR_NAME="Night Fall" \
 499        GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
 500        git commit -a -m "fourth"
 501'
 502
 503test_expect_success 'log grep (1)' '
 504        git log --author=author --pretty=tformat:%s >actual &&
 505        {
 506                echo third && echo initial
 507        } >expect &&
 508        test_cmp expect actual
 509'
 510
 511test_expect_success 'log grep (2)' '
 512        git log --author=" * " -F --pretty=tformat:%s >actual &&
 513        {
 514                echo second
 515        } >expect &&
 516        test_cmp expect actual
 517'
 518
 519test_expect_success 'log grep (3)' '
 520        git log --author="^A U" --pretty=tformat:%s >actual &&
 521        {
 522                echo third && echo initial
 523        } >expect &&
 524        test_cmp expect actual
 525'
 526
 527test_expect_success 'log grep (4)' '
 528        git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
 529        {
 530                echo second
 531        } >expect &&
 532        test_cmp expect actual
 533'
 534
 535test_expect_success 'log grep (5)' '
 536        git log --author=Thor -F --pretty=tformat:%s >actual &&
 537        {
 538                echo third && echo initial
 539        } >expect &&
 540        test_cmp expect actual
 541'
 542
 543test_expect_success 'log grep (6)' '
 544        git log --author=-0700  --pretty=tformat:%s >actual &&
 545        >expect &&
 546        test_cmp expect actual
 547'
 548
 549test_expect_success 'log with multiple --grep uses union' '
 550        git log --grep=i --grep=r --format=%s >actual &&
 551        {
 552                echo fourth && echo third && echo initial
 553        } >expect &&
 554        test_cmp expect actual
 555'
 556
 557test_expect_success 'log --all-match with multiple --grep uses intersection' '
 558        git log --all-match --grep=i --grep=r --format=%s >actual &&
 559        {
 560                echo third
 561        } >expect &&
 562        test_cmp expect actual
 563'
 564
 565test_expect_success 'log with multiple --author uses union' '
 566        git log --author="Thor" --author="Aster" --format=%s >actual &&
 567        {
 568            echo third && echo second && echo initial
 569        } >expect &&
 570        test_cmp expect actual
 571'
 572
 573test_expect_success 'log --all-match with multiple --author still uses union' '
 574        git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
 575        {
 576            echo third && echo second && echo initial
 577        } >expect &&
 578        test_cmp expect actual
 579'
 580
 581test_expect_success 'log --grep --author uses intersection' '
 582        # grep matches only third and fourth
 583        # author matches only initial and third
 584        git log --author="A U Thor" --grep=r --format=%s >actual &&
 585        {
 586                echo third
 587        } >expect &&
 588        test_cmp expect actual
 589'
 590
 591test_expect_success 'log --grep --grep --author takes union of greps and intersects with author' '
 592        # grep matches initial and second but not third
 593        # author matches only initial and third
 594        git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
 595        {
 596                echo initial
 597        } >expect &&
 598        test_cmp expect actual
 599'
 600
 601test_expect_success 'log ---all-match -grep --author --author still takes union of authors and intersects with grep' '
 602        # grep matches only initial and third
 603        # author matches all but second
 604        git log --all-match --author="Thor" --author="Night" --grep=i --format=%s >actual &&
 605        {
 606            echo third && echo initial
 607        } >expect &&
 608        test_cmp expect actual
 609'
 610
 611test_expect_success 'log --grep --author --author takes union of authors and intersects with grep' '
 612        # grep matches only initial and third
 613        # author matches all but second
 614        git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
 615        {
 616            echo third && echo initial
 617        } >expect &&
 618        test_cmp expect actual
 619'
 620
 621test_expect_success 'log --all-match --grep --grep --author takes intersection' '
 622        # grep matches only third
 623        # author matches only initial and third
 624        git log --all-match --author="A U Thor" --grep=i --grep=r --format=%s >actual &&
 625        {
 626                echo third
 627        } >expect &&
 628        test_cmp expect actual
 629'
 630
 631test_expect_success 'log --author does not search in timestamp' '
 632        : >expect &&
 633        git log --author="$GIT_AUTHOR_DATE" >actual &&
 634        test_cmp expect actual
 635'
 636
 637test_expect_success 'log --committer does not search in timestamp' '
 638        : >expect &&
 639        git log --committer="$GIT_COMMITTER_DATE" >actual &&
 640        test_cmp expect actual
 641'
 642
 643test_expect_success 'grep with CE_VALID file' '
 644        git update-index --assume-unchanged t/t &&
 645        rm t/t &&
 646        test "$(git grep test)" = "t/t:test" &&
 647        git update-index --no-assume-unchanged t/t &&
 648        git checkout t/t
 649'
 650
 651cat >expected <<EOF
 652hello.c=#include <stdio.h>
 653hello.c:        return 0;
 654EOF
 655
 656test_expect_success 'grep -p with userdiff' '
 657        git config diff.custom.funcname "^#" &&
 658        echo "hello.c diff=custom" >.gitattributes &&
 659        git grep -p return >actual &&
 660        test_cmp expected actual
 661'
 662
 663cat >expected <<EOF
 664hello.c=int main(int argc, const char **argv)
 665hello.c:        return 0;
 666EOF
 667
 668test_expect_success 'grep -p' '
 669        rm -f .gitattributes &&
 670        git grep -p return >actual &&
 671        test_cmp expected actual
 672'
 673
 674cat >expected <<EOF
 675hello.c-#include <stdio.h>
 676hello.c=int main(int argc, const char **argv)
 677hello.c-{
 678hello.c-        printf("Hello world.\n");
 679hello.c:        return 0;
 680EOF
 681
 682test_expect_success 'grep -p -B5' '
 683        git grep -p -B5 return >actual &&
 684        test_cmp expected actual
 685'
 686
 687cat >expected <<EOF
 688hello.c=int main(int argc, const char **argv)
 689hello.c-{
 690hello.c-        printf("Hello world.\n");
 691hello.c:        return 0;
 692hello.c-        /* char ?? */
 693hello.c-}
 694EOF
 695
 696test_expect_success 'grep -W' '
 697        git grep -W return >actual &&
 698        test_cmp expected actual
 699'
 700
 701cat >expected <<EOF
 702hello.c=        printf("Hello world.\n");
 703hello.c:        return 0;
 704hello.c-        /* char ?? */
 705EOF
 706
 707test_expect_success 'grep -W with userdiff' '
 708        test_when_finished "rm -f .gitattributes" &&
 709        git config diff.custom.xfuncname "(printf.*|})$" &&
 710        echo "hello.c diff=custom" >.gitattributes &&
 711        git grep -W return >actual &&
 712        test_cmp expected actual
 713'
 714
 715test_expect_success 'grep from a subdirectory to search wider area (1)' '
 716        mkdir -p s &&
 717        (
 718                cd s && git grep "x x x" ..
 719        )
 720'
 721
 722test_expect_success 'grep from a subdirectory to search wider area (2)' '
 723        mkdir -p s &&
 724        (
 725                cd s || exit 1
 726                ( git grep xxyyzz .. >out ; echo $? >status )
 727                ! test -s out &&
 728                test 1 = $(cat status)
 729        )
 730'
 731
 732cat >expected <<EOF
 733hello.c:int main(int argc, const char **argv)
 734EOF
 735
 736test_expect_success 'grep -Fi' '
 737        git grep -Fi "CHAR *" >actual &&
 738        test_cmp expected actual
 739'
 740
 741test_expect_success 'outside of git repository' '
 742        rm -fr non &&
 743        mkdir -p non/git/sub &&
 744        echo hello >non/git/file1 &&
 745        echo world >non/git/sub/file2 &&
 746        {
 747                echo file1:hello &&
 748                echo sub/file2:world
 749        } >non/expect.full &&
 750        echo file2:world >non/expect.sub &&
 751        (
 752                GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
 753                export GIT_CEILING_DIRECTORIES &&
 754                cd non/git &&
 755                test_must_fail git grep o &&
 756                git grep --no-index o >../actual.full &&
 757                test_cmp ../expect.full ../actual.full
 758                cd sub &&
 759                test_must_fail git grep o &&
 760                git grep --no-index o >../../actual.sub &&
 761                test_cmp ../../expect.sub ../../actual.sub
 762        ) &&
 763
 764        echo ".*o*" >non/git/.gitignore &&
 765        (
 766                GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
 767                export GIT_CEILING_DIRECTORIES &&
 768                cd non/git &&
 769                test_must_fail git grep o &&
 770                git grep --no-index --exclude-standard o >../actual.full &&
 771                test_cmp ../expect.full ../actual.full &&
 772
 773                {
 774                        echo ".gitignore:.*o*"
 775                        cat ../expect.full
 776                } >../expect.with.ignored &&
 777                git grep --no-index --no-exclude o >../actual.full &&
 778                test_cmp ../expect.with.ignored ../actual.full
 779        )
 780'
 781
 782test_expect_success 'inside git repository but with --no-index' '
 783        rm -fr is &&
 784        mkdir -p is/git/sub &&
 785        echo hello >is/git/file1 &&
 786        echo world >is/git/sub/file2 &&
 787        echo ".*o*" >is/git/.gitignore &&
 788        {
 789                echo file1:hello &&
 790                echo sub/file2:world
 791        } >is/expect.unignored &&
 792        {
 793                echo ".gitignore:.*o*" &&
 794                cat is/expect.unignored
 795        } >is/expect.full &&
 796        : >is/expect.empty &&
 797        echo file2:world >is/expect.sub &&
 798        (
 799                cd is/git &&
 800                git init &&
 801                test_must_fail git grep o >../actual.full &&
 802                test_cmp ../expect.empty ../actual.full &&
 803
 804                git grep --untracked o >../actual.unignored &&
 805                test_cmp ../expect.unignored ../actual.unignored &&
 806
 807                git grep --no-index o >../actual.full &&
 808                test_cmp ../expect.full ../actual.full &&
 809
 810                git grep --no-index --exclude-standard o >../actual.unignored &&
 811                test_cmp ../expect.unignored ../actual.unignored &&
 812
 813                cd sub &&
 814                test_must_fail git grep o >../../actual.sub &&
 815                test_cmp ../../expect.empty ../../actual.sub &&
 816
 817                git grep --no-index o >../../actual.sub &&
 818                test_cmp ../../expect.sub ../../actual.sub &&
 819
 820                git grep --untracked o >../../actual.sub &&
 821                test_cmp ../../expect.sub ../../actual.sub
 822        )
 823'
 824
 825test_expect_success 'setup double-dash tests' '
 826cat >double-dash <<EOF &&
 827--
 828->
 829other
 830EOF
 831git add double-dash
 832'
 833
 834cat >expected <<EOF
 835double-dash:->
 836EOF
 837test_expect_success 'grep -- pattern' '
 838        git grep -- "->" >actual &&
 839        test_cmp expected actual
 840'
 841test_expect_success 'grep -- pattern -- pathspec' '
 842        git grep -- "->" -- double-dash >actual &&
 843        test_cmp expected actual
 844'
 845test_expect_success 'grep -e pattern -- path' '
 846        git grep -e "->" -- double-dash >actual &&
 847        test_cmp expected actual
 848'
 849
 850cat >expected <<EOF
 851double-dash:--
 852EOF
 853test_expect_success 'grep -e -- -- path' '
 854        git grep -e -- -- double-dash >actual &&
 855        test_cmp expected actual
 856'
 857
 858cat >expected <<EOF
 859hello.c:int main(int argc, const char **argv)
 860hello.c:        printf("Hello world.\n");
 861EOF
 862
 863test_expect_success LIBPCRE 'grep --perl-regexp pattern' '
 864        git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
 865        test_cmp expected actual
 866'
 867
 868test_expect_success LIBPCRE 'grep -P pattern' '
 869        git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
 870        test_cmp expected actual
 871'
 872
 873test_expect_success 'grep pattern with grep.extendedRegexp=true' '
 874        >empty &&
 875        test_must_fail git -c grep.extendedregexp=true \
 876                grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
 877        test_cmp empty actual
 878'
 879
 880test_expect_success LIBPCRE 'grep -P pattern with grep.extendedRegexp=true' '
 881        git -c grep.extendedregexp=true \
 882                grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
 883        test_cmp expected actual
 884'
 885
 886test_expect_success LIBPCRE 'grep -P -v pattern' '
 887        {
 888                echo "ab:a+b*c"
 889                echo "ab:a+bc"
 890        } >expected &&
 891        git grep -P -v "abc" ab >actual &&
 892        test_cmp expected actual
 893'
 894
 895test_expect_success LIBPCRE 'grep -P -i pattern' '
 896        cat >expected <<-EOF &&
 897        hello.c:        printf("Hello world.\n");
 898        EOF
 899        git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
 900        test_cmp expected actual
 901'
 902
 903test_expect_success LIBPCRE 'grep -P -w pattern' '
 904        {
 905                echo "hello_world:Hello world"
 906                echo "hello_world:HeLLo world"
 907        } >expected &&
 908        git grep -P -w "He((?i)ll)o" hello_world >actual &&
 909        test_cmp expected actual
 910'
 911
 912test_expect_success 'grep -G invalidpattern properly dies ' '
 913        test_must_fail git grep -G "a["
 914'
 915
 916test_expect_success 'grep invalidpattern properly dies with grep.patternType=basic' '
 917        test_must_fail git -c grep.patterntype=basic grep "a["
 918'
 919
 920test_expect_success 'grep -E invalidpattern properly dies ' '
 921        test_must_fail git grep -E "a["
 922'
 923
 924test_expect_success 'grep invalidpattern properly dies with grep.patternType=extended' '
 925        test_must_fail git -c grep.patterntype=extended grep "a["
 926'
 927
 928test_expect_success LIBPCRE 'grep -P invalidpattern properly dies ' '
 929        test_must_fail git grep -P "a["
 930'
 931
 932test_expect_success LIBPCRE 'grep invalidpattern properly dies with grep.patternType=perl' '
 933        test_must_fail git -c grep.patterntype=perl grep "a["
 934'
 935
 936test_expect_success 'grep -G -E -F pattern' '
 937        echo "ab:a+b*c" >expected &&
 938        git grep -G -E -F "a+b*c" ab >actual &&
 939        test_cmp expected actual
 940'
 941
 942test_expect_success 'grep pattern with grep.patternType=basic, =extended, =fixed' '
 943        echo "ab:a+b*c" >expected &&
 944        git \
 945                -c grep.patterntype=basic \
 946                -c grep.patterntype=extended \
 947                -c grep.patterntype=fixed \
 948                grep "a+b*c" ab >actual &&
 949        test_cmp expected actual
 950'
 951
 952test_expect_success 'grep -E -F -G pattern' '
 953        echo "ab:a+bc" >expected &&
 954        git grep -E -F -G "a+b*c" ab >actual &&
 955        test_cmp expected actual
 956'
 957
 958test_expect_success 'grep pattern with grep.patternType=extended, =fixed, =basic' '
 959        echo "ab:a+bc" >expected &&
 960        git \
 961                -c grep.patterntype=extended \
 962                -c grep.patterntype=fixed \
 963                -c grep.patterntype=basic \
 964                grep "a+b*c" ab >actual &&
 965        test_cmp expected actual
 966'
 967
 968test_expect_success 'grep -F -G -E pattern' '
 969        echo "ab:abc" >expected &&
 970        git grep -F -G -E "a+b*c" ab >actual &&
 971        test_cmp expected actual
 972'
 973
 974test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended' '
 975        echo "ab:abc" >expected &&
 976        git \
 977                -c grep.patterntype=fixed \
 978                -c grep.patterntype=basic \
 979                -c grep.patterntype=extended \
 980                grep "a+b*c" ab >actual &&
 981        test_cmp expected actual
 982'
 983
 984test_expect_success 'grep -G -F -P -E pattern' '
 985        >empty &&
 986        test_must_fail git grep -G -F -P -E "a\x{2b}b\x{2a}c" ab >actual &&
 987        test_cmp empty actual
 988'
 989
 990test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
 991        >empty &&
 992        test_must_fail git \
 993                -c grep.patterntype=fixed \
 994                -c grep.patterntype=basic \
 995                -c grep.patterntype=perl \
 996                -c grep.patterntype=extended \
 997                grep "a\x{2b}b\x{2a}c" ab >actual &&
 998        test_cmp empty actual
 999'
1000
1001test_expect_success LIBPCRE 'grep -G -F -E -P pattern' '
1002        echo "ab:a+b*c" >expected &&
1003        git grep -G -F -E -P "a\x{2b}b\x{2a}c" ab >actual &&
1004        test_cmp expected actual
1005'
1006
1007test_expect_success LIBPCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1008        echo "ab:a+b*c" >expected &&
1009        git \
1010                -c grep.patterntype=fixed \
1011                -c grep.patterntype=basic \
1012                -c grep.patterntype=extended \
1013                -c grep.patterntype=perl \
1014                grep "a\x{2b}b\x{2a}c" ab >actual &&
1015        test_cmp expected actual
1016'
1017
1018test_expect_success LIBPCRE 'grep -P pattern with grep.patternType=fixed' '
1019        echo "ab:a+b*c" >expected &&
1020        git \
1021                -c grep.patterntype=fixed \
1022                grep -P "a\x{2b}b\x{2a}c" ab >actual &&
1023        test_cmp expected actual
1024'
1025
1026test_expect_success 'grep -F pattern with grep.patternType=basic' '
1027        echo "ab:a+b*c" >expected &&
1028        git \
1029                -c grep.patterntype=basic \
1030                grep -F "*c" ab >actual &&
1031        test_cmp expected actual
1032'
1033
1034test_expect_success 'grep -G pattern with grep.patternType=fixed' '
1035        {
1036                echo "ab:a+b*c"
1037                echo "ab:a+bc"
1038        } >expected &&
1039        git \
1040                -c grep.patterntype=fixed \
1041                grep -G "a+b" ab >actual &&
1042        test_cmp expected actual
1043'
1044
1045test_expect_success 'grep -E pattern with grep.patternType=fixed' '
1046        {
1047                echo "ab:a+b*c"
1048                echo "ab:a+bc"
1049                echo "ab:abc"
1050        } >expected &&
1051        git \
1052                -c grep.patterntype=fixed \
1053                grep -E "a+" ab >actual &&
1054        test_cmp expected actual
1055'
1056
1057test_config() {
1058        git config "$1" "$2" &&
1059        test_when_finished "git config --unset $1"
1060}
1061
1062cat >expected <<EOF
1063hello.c<RED>:<RESET>int main(int argc, const char **argv)
1064hello.c<RED>-<RESET>{
1065<RED>--<RESET>
1066hello.c<RED>:<RESET>    /* char ?? */
1067hello.c<RED>-<RESET>}
1068<RED>--<RESET>
1069hello_world<RED>:<RESET>Hello_world
1070hello_world<RED>-<RESET>HeLLo_world
1071EOF
1072
1073test_expect_success 'grep --color, separator' '
1074        test_config color.grep.context          normal &&
1075        test_config color.grep.filename         normal &&
1076        test_config color.grep.function         normal &&
1077        test_config color.grep.linenumber       normal &&
1078        test_config color.grep.match            normal &&
1079        test_config color.grep.selected         normal &&
1080        test_config color.grep.separator        red &&
1081
1082        git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
1083        test_decode_color >actual &&
1084        test_cmp expected actual
1085'
1086
1087cat >expected <<EOF
1088hello.c:int main(int argc, const char **argv)
1089hello.c:        /* char ?? */
1090
1091hello_world:Hello_world
1092EOF
1093
1094test_expect_success 'grep --break' '
1095        git grep --break -e char -e lo_w hello.c hello_world >actual &&
1096        test_cmp expected actual
1097'
1098
1099cat >expected <<EOF
1100hello.c:int main(int argc, const char **argv)
1101hello.c-{
1102--
1103hello.c:        /* char ?? */
1104hello.c-}
1105
1106hello_world:Hello_world
1107hello_world-HeLLo_world
1108EOF
1109
1110test_expect_success 'grep --break with context' '
1111        git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
1112        test_cmp expected actual
1113'
1114
1115cat >expected <<EOF
1116hello.c
1117int main(int argc, const char **argv)
1118        /* char ?? */
1119hello_world
1120Hello_world
1121EOF
1122
1123test_expect_success 'grep --heading' '
1124        git grep --heading -e char -e lo_w hello.c hello_world >actual &&
1125        test_cmp expected actual
1126'
1127
1128cat >expected <<EOF
1129<BOLD;GREEN>hello.c<RESET>
11302:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
11316:      /* <BLACK;BYELLOW>char<RESET> ?? */
1132
1133<BOLD;GREEN>hello_world<RESET>
11343:Hel<BLACK;BYELLOW>lo_w<RESET>orld
1135EOF
1136
1137test_expect_success 'mimic ack-grep --group' '
1138        test_config color.grep.context          normal &&
1139        test_config color.grep.filename         "bold green" &&
1140        test_config color.grep.function         normal &&
1141        test_config color.grep.linenumber       normal &&
1142        test_config color.grep.match            "black yellow" &&
1143        test_config color.grep.selected         normal &&
1144        test_config color.grep.separator        normal &&
1145
1146        git grep --break --heading -n --color \
1147                -e char -e lo_w hello.c hello_world |
1148        test_decode_color >actual &&
1149        test_cmp expected actual
1150'
1151
1152cat >expected <<EOF
1153space: line with leading space1
1154space: line with leading space2
1155space: line with leading space3
1156EOF
1157
1158test_expect_success LIBPCRE 'grep -E "^ "' '
1159        git grep -E "^ " space >actual &&
1160        test_cmp expected actual
1161'
1162
1163test_expect_success LIBPCRE 'grep -P "^ "' '
1164        git grep -P "^ " space >actual &&
1165        test_cmp expected actual
1166'
1167
1168test_done