t / t7810-grep.shon commit grep: add a grep.patternType configuration setting (84befcd)
   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
 480# Create 1024 file names that sort between "y" and "z" to make sure
 481# the two files are handled by different calls to an external grep.
 482# This depends on MAXARGS in builtin-grep.c being 1024 or less.
 483c32="0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v"
 484test_expect_success 'grep -C1, hunk mark between files' '
 485        for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
 486        git add y-?? &&
 487        git grep -C1 "^[yz]" >actual &&
 488        test_cmp expected actual
 489'
 490
 491test_expect_success 'grep -C1 hunk mark between files' '
 492        git grep -C1 "^[yz]" >actual &&
 493        test_cmp expected actual
 494'
 495
 496test_expect_success 'log grep setup' '
 497        echo a >>file &&
 498        test_tick &&
 499        GIT_AUTHOR_NAME="With * Asterisk" \
 500        GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
 501        git commit -a -m "second" &&
 502
 503        echo a >>file &&
 504        test_tick &&
 505        git commit -a -m "third" &&
 506
 507        echo a >>file &&
 508        test_tick &&
 509        GIT_AUTHOR_NAME="Night Fall" \
 510        GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
 511        git commit -a -m "fourth"
 512'
 513
 514test_expect_success 'log grep (1)' '
 515        git log --author=author --pretty=tformat:%s >actual &&
 516        ( echo third ; echo initial ) >expect &&
 517        test_cmp expect actual
 518'
 519
 520test_expect_success 'log grep (2)' '
 521        git log --author=" * " -F --pretty=tformat:%s >actual &&
 522        ( echo second ) >expect &&
 523        test_cmp expect actual
 524'
 525
 526test_expect_success 'log grep (3)' '
 527        git log --author="^A U" --pretty=tformat:%s >actual &&
 528        ( echo third ; echo initial ) >expect &&
 529        test_cmp expect actual
 530'
 531
 532test_expect_success 'log grep (4)' '
 533        git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
 534        ( echo second ) >expect &&
 535        test_cmp expect actual
 536'
 537
 538test_expect_success 'log grep (5)' '
 539        git log --author=Thor -F --pretty=tformat:%s >actual &&
 540        ( echo third ; echo initial ) >expect &&
 541        test_cmp expect actual
 542'
 543
 544test_expect_success 'log grep (6)' '
 545        git log --author=-0700  --pretty=tformat:%s >actual &&
 546        >expect &&
 547        test_cmp expect actual
 548'
 549
 550test_expect_success 'log --grep --author implicitly uses all-match' '
 551        # grep matches initial and second but not third
 552        # author matches only initial and third
 553        git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
 554        echo initial >expect &&
 555        test_cmp expect actual
 556'
 557
 558test_expect_success 'log with multiple --author uses union' '
 559        git log --author="Thor" --author="Aster" --format=%s >actual &&
 560        {
 561            echo third && echo second && echo initial
 562        } >expect &&
 563        test_cmp expect actual
 564'
 565
 566test_expect_success 'log with --grep and multiple --author uses all-match' '
 567        git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
 568        {
 569            echo third && echo initial
 570        } >expect &&
 571        test_cmp expect actual
 572'
 573
 574test_expect_success 'log with --grep and multiple --author uses all-match' '
 575        git log --author="Thor" --author="Night" --grep=q --format=%s >actual &&
 576        >expect &&
 577        test_cmp expect actual
 578'
 579
 580test_expect_success 'grep with CE_VALID file' '
 581        git update-index --assume-unchanged t/t &&
 582        rm t/t &&
 583        test "$(git grep test)" = "t/t:test" &&
 584        git update-index --no-assume-unchanged t/t &&
 585        git checkout t/t
 586'
 587
 588cat >expected <<EOF
 589hello.c=#include <stdio.h>
 590hello.c:        return 0;
 591EOF
 592
 593test_expect_success 'grep -p with userdiff' '
 594        git config diff.custom.funcname "^#" &&
 595        echo "hello.c diff=custom" >.gitattributes &&
 596        git grep -p return >actual &&
 597        test_cmp expected actual
 598'
 599
 600cat >expected <<EOF
 601hello.c=int main(int argc, const char **argv)
 602hello.c:        return 0;
 603EOF
 604
 605test_expect_success 'grep -p' '
 606        rm -f .gitattributes &&
 607        git grep -p return >actual &&
 608        test_cmp expected actual
 609'
 610
 611cat >expected <<EOF
 612hello.c-#include <stdio.h>
 613hello.c=int main(int argc, const char **argv)
 614hello.c-{
 615hello.c-        printf("Hello world.\n");
 616hello.c:        return 0;
 617EOF
 618
 619test_expect_success 'grep -p -B5' '
 620        git grep -p -B5 return >actual &&
 621        test_cmp expected actual
 622'
 623
 624cat >expected <<EOF
 625hello.c=int main(int argc, const char **argv)
 626hello.c-{
 627hello.c-        printf("Hello world.\n");
 628hello.c:        return 0;
 629hello.c-        /* char ?? */
 630hello.c-}
 631EOF
 632
 633test_expect_success 'grep -W' '
 634        git grep -W return >actual &&
 635        test_cmp expected actual
 636'
 637
 638cat >expected <<EOF
 639hello.c=        printf("Hello world.\n");
 640hello.c:        return 0;
 641hello.c-        /* char ?? */
 642EOF
 643
 644test_expect_success 'grep -W with userdiff' '
 645        test_when_finished "rm -f .gitattributes" &&
 646        git config diff.custom.xfuncname "(printf.*|})$" &&
 647        echo "hello.c diff=custom" >.gitattributes &&
 648        git grep -W return >actual &&
 649        test_cmp expected actual
 650'
 651
 652test_expect_success 'grep from a subdirectory to search wider area (1)' '
 653        mkdir -p s &&
 654        (
 655                cd s && git grep "x x x" ..
 656        )
 657'
 658
 659test_expect_success 'grep from a subdirectory to search wider area (2)' '
 660        mkdir -p s &&
 661        (
 662                cd s || exit 1
 663                ( git grep xxyyzz .. >out ; echo $? >status )
 664                ! test -s out &&
 665                test 1 = $(cat status)
 666        )
 667'
 668
 669cat >expected <<EOF
 670hello.c:int main(int argc, const char **argv)
 671EOF
 672
 673test_expect_success 'grep -Fi' '
 674        git grep -Fi "CHAR *" >actual &&
 675        test_cmp expected actual
 676'
 677
 678test_expect_success 'outside of git repository' '
 679        rm -fr non &&
 680        mkdir -p non/git/sub &&
 681        echo hello >non/git/file1 &&
 682        echo world >non/git/sub/file2 &&
 683        {
 684                echo file1:hello &&
 685                echo sub/file2:world
 686        } >non/expect.full &&
 687        echo file2:world >non/expect.sub &&
 688        (
 689                GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
 690                export GIT_CEILING_DIRECTORIES &&
 691                cd non/git &&
 692                test_must_fail git grep o &&
 693                git grep --no-index o >../actual.full &&
 694                test_cmp ../expect.full ../actual.full
 695                cd sub &&
 696                test_must_fail git grep o &&
 697                git grep --no-index o >../../actual.sub &&
 698                test_cmp ../../expect.sub ../../actual.sub
 699        ) &&
 700
 701        echo ".*o*" >non/git/.gitignore &&
 702        (
 703                GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
 704                export GIT_CEILING_DIRECTORIES &&
 705                cd non/git &&
 706                test_must_fail git grep o &&
 707                git grep --no-index --exclude-standard o >../actual.full &&
 708                test_cmp ../expect.full ../actual.full &&
 709
 710                {
 711                        echo ".gitignore:.*o*"
 712                        cat ../expect.full
 713                } >../expect.with.ignored &&
 714                git grep --no-index --no-exclude o >../actual.full &&
 715                test_cmp ../expect.with.ignored ../actual.full
 716        )
 717'
 718
 719test_expect_success 'inside git repository but with --no-index' '
 720        rm -fr is &&
 721        mkdir -p is/git/sub &&
 722        echo hello >is/git/file1 &&
 723        echo world >is/git/sub/file2 &&
 724        echo ".*o*" >is/git/.gitignore &&
 725        {
 726                echo file1:hello &&
 727                echo sub/file2:world
 728        } >is/expect.unignored &&
 729        {
 730                echo ".gitignore:.*o*" &&
 731                cat is/expect.unignored
 732        } >is/expect.full &&
 733        : >is/expect.empty &&
 734        echo file2:world >is/expect.sub &&
 735        (
 736                cd is/git &&
 737                git init &&
 738                test_must_fail git grep o >../actual.full &&
 739                test_cmp ../expect.empty ../actual.full &&
 740
 741                git grep --untracked o >../actual.unignored &&
 742                test_cmp ../expect.unignored ../actual.unignored &&
 743
 744                git grep --no-index o >../actual.full &&
 745                test_cmp ../expect.full ../actual.full &&
 746
 747                git grep --no-index --exclude-standard o >../actual.unignored &&
 748                test_cmp ../expect.unignored ../actual.unignored &&
 749
 750                cd sub &&
 751                test_must_fail git grep o >../../actual.sub &&
 752                test_cmp ../../expect.empty ../../actual.sub &&
 753
 754                git grep --no-index o >../../actual.sub &&
 755                test_cmp ../../expect.sub ../../actual.sub &&
 756
 757                git grep --untracked o >../../actual.sub &&
 758                test_cmp ../../expect.sub ../../actual.sub
 759        )
 760'
 761
 762test_expect_success 'setup double-dash tests' '
 763cat >double-dash <<EOF &&
 764--
 765->
 766other
 767EOF
 768git add double-dash
 769'
 770
 771cat >expected <<EOF
 772double-dash:->
 773EOF
 774test_expect_success 'grep -- pattern' '
 775        git grep -- "->" >actual &&
 776        test_cmp expected actual
 777'
 778test_expect_success 'grep -- pattern -- pathspec' '
 779        git grep -- "->" -- double-dash >actual &&
 780        test_cmp expected actual
 781'
 782test_expect_success 'grep -e pattern -- path' '
 783        git grep -e "->" -- double-dash >actual &&
 784        test_cmp expected actual
 785'
 786
 787cat >expected <<EOF
 788double-dash:--
 789EOF
 790test_expect_success 'grep -e -- -- path' '
 791        git grep -e -- -- double-dash >actual &&
 792        test_cmp expected actual
 793'
 794
 795cat >expected <<EOF
 796hello.c:int main(int argc, const char **argv)
 797hello.c:        printf("Hello world.\n");
 798EOF
 799
 800test_expect_success LIBPCRE 'grep --perl-regexp pattern' '
 801        git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
 802        test_cmp expected actual
 803'
 804
 805test_expect_success LIBPCRE 'grep -P pattern' '
 806        git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
 807        test_cmp expected actual
 808'
 809
 810test_expect_success 'grep pattern with grep.extendedRegexp=true' '
 811        >empty &&
 812        test_must_fail git -c grep.extendedregexp=true \
 813                grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
 814        test_cmp empty actual
 815'
 816
 817test_expect_success LIBPCRE 'grep -P pattern with grep.extendedRegexp=true' '
 818        git -c grep.extendedregexp=true \
 819                grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
 820        test_cmp expected actual
 821'
 822
 823test_expect_success LIBPCRE 'grep -P -v pattern' '
 824        {
 825                echo "ab:a+b*c"
 826                echo "ab:a+bc"
 827        } >expected &&
 828        git grep -P -v "abc" ab >actual &&
 829        test_cmp expected actual
 830'
 831
 832test_expect_success LIBPCRE 'grep -P -i pattern' '
 833        cat >expected <<-EOF &&
 834        hello.c:        printf("Hello world.\n");
 835        EOF
 836        git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
 837        test_cmp expected actual
 838'
 839
 840test_expect_success LIBPCRE 'grep -P -w pattern' '
 841        {
 842                echo "hello_world:Hello world"
 843                echo "hello_world:HeLLo world"
 844        } >expected &&
 845        git grep -P -w "He((?i)ll)o" hello_world >actual &&
 846        test_cmp expected actual
 847'
 848
 849test_expect_success 'grep -G invalidpattern properly dies ' '
 850        test_must_fail git grep -G "a["
 851'
 852
 853test_expect_success 'grep invalidpattern properly dies with grep.patternType=basic' '
 854        test_must_fail git -c grep.patterntype=basic grep "a["
 855'
 856
 857test_expect_success 'grep -E invalidpattern properly dies ' '
 858        test_must_fail git grep -E "a["
 859'
 860
 861test_expect_success 'grep invalidpattern properly dies with grep.patternType=extended' '
 862        test_must_fail git -c grep.patterntype=extended grep "a["
 863'
 864
 865test_expect_success LIBPCRE 'grep -P invalidpattern properly dies ' '
 866        test_must_fail git grep -P "a["
 867'
 868
 869test_expect_success LIBPCRE 'grep invalidpattern properly dies with grep.patternType=perl' '
 870        test_must_fail git -c grep.patterntype=perl grep "a["
 871'
 872
 873test_expect_success 'grep -G -E -F pattern' '
 874        echo "ab:a+b*c" >expected &&
 875        git grep -G -E -F "a+b*c" ab >actual &&
 876        test_cmp expected actual
 877'
 878
 879test_expect_success 'grep pattern with grep.patternType=basic, =extended, =fixed' '
 880        echo "ab:a+b*c" >expected &&
 881        git \
 882                -c grep.patterntype=basic \
 883                -c grep.patterntype=extended \
 884                -c grep.patterntype=fixed \
 885                grep "a+b*c" ab >actual &&
 886        test_cmp expected actual
 887'
 888
 889test_expect_success 'grep -E -F -G pattern' '
 890        echo "ab:a+bc" >expected &&
 891        git grep -E -F -G "a+b*c" ab >actual &&
 892        test_cmp expected actual
 893'
 894
 895test_expect_success 'grep pattern with grep.patternType=extended, =fixed, =basic' '
 896        echo "ab:a+bc" >expected &&
 897        git \
 898                -c grep.patterntype=extended \
 899                -c grep.patterntype=fixed \
 900                -c grep.patterntype=basic \
 901                grep "a+b*c" ab >actual &&
 902        test_cmp expected actual
 903'
 904
 905test_expect_success 'grep -F -G -E pattern' '
 906        echo "ab:abc" >expected &&
 907        git grep -F -G -E "a+b*c" ab >actual &&
 908        test_cmp expected actual
 909'
 910
 911test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended' '
 912        echo "ab:abc" >expected &&
 913        git \
 914                -c grep.patterntype=fixed \
 915                -c grep.patterntype=basic \
 916                -c grep.patterntype=extended \
 917                grep "a+b*c" ab >actual &&
 918        test_cmp expected actual
 919'
 920
 921test_expect_success 'grep -G -F -P -E pattern' '
 922        >empty &&
 923        test_must_fail git grep -G -F -P -E "a\x{2b}b\x{2a}c" ab >actual &&
 924        test_cmp empty actual
 925'
 926
 927test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
 928        >empty &&
 929        test_must_fail git \
 930                -c grep.patterntype=fixed \
 931                -c grep.patterntype=basic \
 932                -c grep.patterntype=perl \
 933                -c grep.patterntype=extended \
 934                grep "a\x{2b}b\x{2a}c" ab >actual &&
 935        test_cmp empty actual
 936'
 937
 938test_expect_success LIBPCRE 'grep -G -F -E -P pattern' '
 939        echo "ab:a+b*c" >expected &&
 940        git grep -G -F -E -P "a\x{2b}b\x{2a}c" ab >actual &&
 941        test_cmp expected actual
 942'
 943
 944test_expect_success LIBPCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
 945        echo "ab:a+b*c" >expected &&
 946        git \
 947                -c grep.patterntype=fixed \
 948                -c grep.patterntype=basic \
 949                -c grep.patterntype=extended \
 950                -c grep.patterntype=perl \
 951                grep "a\x{2b}b\x{2a}c" ab >actual &&
 952        test_cmp expected actual
 953'
 954
 955test_expect_success LIBPCRE 'grep -P pattern with grep.patternType=fixed' '
 956        echo "ab:a+b*c" >expected &&
 957        git \
 958                -c grep.patterntype=fixed \
 959                grep -P "a\x{2b}b\x{2a}c" ab >actual &&
 960        test_cmp expected actual
 961'
 962
 963test_expect_success 'grep -F pattern with grep.patternType=basic' '
 964        echo "ab:a+b*c" >expected &&
 965        git \
 966                -c grep.patterntype=basic \
 967                grep -F "*c" ab >actual &&
 968        test_cmp expected actual
 969'
 970
 971test_expect_success 'grep -G pattern with grep.patternType=fixed' '
 972        {
 973                echo "ab:a+b*c"
 974                echo "ab:a+bc"
 975        } >expected &&
 976        git \
 977                -c grep.patterntype=fixed \
 978                grep -G "a+b" ab >actual &&
 979        test_cmp expected actual
 980'
 981
 982test_expect_success 'grep -E pattern with grep.patternType=fixed' '
 983        {
 984                echo "ab:a+b*c"
 985                echo "ab:a+bc"
 986                echo "ab:abc"
 987        } >expected &&
 988        git \
 989                -c grep.patterntype=fixed \
 990                grep -E "a+" ab >actual &&
 991        test_cmp expected actual
 992'
 993
 994test_config() {
 995        git config "$1" "$2" &&
 996        test_when_finished "git config --unset $1"
 997}
 998
 999cat >expected <<EOF
1000hello.c<RED>:<RESET>int main(int argc, const char **argv)
1001hello.c<RED>-<RESET>{
1002<RED>--<RESET>
1003hello.c<RED>:<RESET>    /* char ?? */
1004hello.c<RED>-<RESET>}
1005<RED>--<RESET>
1006hello_world<RED>:<RESET>Hello_world
1007hello_world<RED>-<RESET>HeLLo_world
1008EOF
1009
1010test_expect_success 'grep --color, separator' '
1011        test_config color.grep.context          normal &&
1012        test_config color.grep.filename         normal &&
1013        test_config color.grep.function         normal &&
1014        test_config color.grep.linenumber       normal &&
1015        test_config color.grep.match            normal &&
1016        test_config color.grep.selected         normal &&
1017        test_config color.grep.separator        red &&
1018
1019        git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
1020        test_decode_color >actual &&
1021        test_cmp expected actual
1022'
1023
1024cat >expected <<EOF
1025hello.c:int main(int argc, const char **argv)
1026hello.c:        /* char ?? */
1027
1028hello_world:Hello_world
1029EOF
1030
1031test_expect_success 'grep --break' '
1032        git grep --break -e char -e lo_w hello.c hello_world >actual &&
1033        test_cmp expected actual
1034'
1035
1036cat >expected <<EOF
1037hello.c:int main(int argc, const char **argv)
1038hello.c-{
1039--
1040hello.c:        /* char ?? */
1041hello.c-}
1042
1043hello_world:Hello_world
1044hello_world-HeLLo_world
1045EOF
1046
1047test_expect_success 'grep --break with context' '
1048        git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
1049        test_cmp expected actual
1050'
1051
1052cat >expected <<EOF
1053hello.c
1054int main(int argc, const char **argv)
1055        /* char ?? */
1056hello_world
1057Hello_world
1058EOF
1059
1060test_expect_success 'grep --heading' '
1061        git grep --heading -e char -e lo_w hello.c hello_world >actual &&
1062        test_cmp expected actual
1063'
1064
1065cat >expected <<EOF
1066<BOLD;GREEN>hello.c<RESET>
10672:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
10686:      /* <BLACK;BYELLOW>char<RESET> ?? */
1069
1070<BOLD;GREEN>hello_world<RESET>
10713:Hel<BLACK;BYELLOW>lo_w<RESET>orld
1072EOF
1073
1074test_expect_success 'mimic ack-grep --group' '
1075        test_config color.grep.context          normal &&
1076        test_config color.grep.filename         "bold green" &&
1077        test_config color.grep.function         normal &&
1078        test_config color.grep.linenumber       normal &&
1079        test_config color.grep.match            "black yellow" &&
1080        test_config color.grep.selected         normal &&
1081        test_config color.grep.separator        normal &&
1082
1083        git grep --break --heading -n --color \
1084                -e char -e lo_w hello.c hello_world |
1085        test_decode_color >actual &&
1086        test_cmp expected actual
1087'
1088
1089cat >expected <<EOF
1090space: line with leading space1
1091space: line with leading space2
1092space: line with leading space3
1093EOF
1094
1095test_expect_success LIBPCRE 'grep -E "^ "' '
1096        git grep -E "^ " space >actual &&
1097        test_cmp expected actual
1098'
1099
1100test_expect_success LIBPCRE 'grep -P "^ "' '
1101        git grep -P "^ " space >actual &&
1102        test_cmp expected actual
1103'
1104
1105test_done