f6edb4d6dc66cedf25cda69d959989f54aaf8f74
   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        '
 253done
 254
 255cat >expected <<EOF
 256file
 257EOF
 258test_expect_success 'grep -l -C' '
 259        git grep -l -C1 foo >actual &&
 260        test_cmp expected actual
 261'
 262
 263cat >expected <<EOF
 264file:5
 265EOF
 266test_expect_success 'grep -l -C' '
 267        git grep -c -C1 foo >actual &&
 268        test_cmp expected actual
 269'
 270
 271test_expect_success 'grep -L -C' '
 272        git ls-files >expected &&
 273        git grep -L -C1 nonexistent_string >actual &&
 274        test_cmp expected actual
 275'
 276
 277cat >expected <<EOF
 278file:foo mmap bar_mmap
 279EOF
 280
 281test_expect_success 'grep -e A --and -e B' '
 282        git grep -e "foo mmap" --and -e bar_mmap >actual &&
 283        test_cmp expected actual
 284'
 285
 286cat >expected <<EOF
 287file:foo_mmap bar mmap
 288file:foo_mmap bar mmap baz
 289EOF
 290
 291
 292test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
 293        git grep \( -e foo_ --or -e baz \) \
 294                --and -e " mmap" >actual &&
 295        test_cmp expected actual
 296'
 297
 298cat >expected <<EOF
 299file:foo mmap bar
 300EOF
 301
 302test_expect_success 'grep -e A --and --not -e B' '
 303        git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
 304        test_cmp expected actual
 305'
 306
 307test_expect_success 'grep should ignore GREP_OPTIONS' '
 308        GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
 309        test_cmp expected actual
 310'
 311
 312test_expect_success 'grep -f, non-existent file' '
 313        test_must_fail git grep -f patterns
 314'
 315
 316cat >expected <<EOF
 317file:foo mmap bar
 318file:foo_mmap bar
 319file:foo_mmap bar mmap
 320file:foo mmap bar_mmap
 321file:foo_mmap bar mmap baz
 322EOF
 323
 324cat >pattern <<EOF
 325mmap
 326EOF
 327
 328test_expect_success 'grep -f, one pattern' '
 329        git grep -f pattern >actual &&
 330        test_cmp expected actual
 331'
 332
 333cat >expected <<EOF
 334file:foo mmap bar
 335file:foo_mmap bar
 336file:foo_mmap bar mmap
 337file:foo mmap bar_mmap
 338file:foo_mmap bar mmap baz
 339t/a/v:vvv
 340t/v:vvv
 341v:vvv
 342EOF
 343
 344cat >patterns <<EOF
 345mmap
 346vvv
 347EOF
 348
 349test_expect_success 'grep -f, multiple patterns' '
 350        git grep -f patterns >actual &&
 351        test_cmp expected actual
 352'
 353
 354test_expect_success 'grep, multiple patterns' '
 355        git grep "$(cat patterns)" >actual &&
 356        test_cmp expected actual
 357'
 358
 359cat >expected <<EOF
 360file:foo mmap bar
 361file:foo_mmap bar
 362file:foo_mmap bar mmap
 363file:foo mmap bar_mmap
 364file:foo_mmap bar mmap baz
 365t/a/v:vvv
 366t/v:vvv
 367v:vvv
 368EOF
 369
 370cat >patterns <<EOF
 371
 372mmap
 373
 374vvv
 375
 376EOF
 377
 378test_expect_success 'grep -f, ignore empty lines' '
 379        git grep -f patterns >actual &&
 380        test_cmp expected actual
 381'
 382
 383test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
 384        git grep -f - <patterns >actual &&
 385        test_cmp expected actual
 386'
 387
 388cat >expected <<EOF
 389y:y yy
 390--
 391z:zzz
 392EOF
 393
 394test_expect_success 'grep -q, silently report matches' '
 395        >empty &&
 396        git grep -q mmap >actual &&
 397        test_cmp empty actual &&
 398        test_must_fail git grep -q qfwfq >actual &&
 399        test_cmp empty actual
 400'
 401
 402# Create 1024 file names that sort between "y" and "z" to make sure
 403# the two files are handled by different calls to an external grep.
 404# This depends on MAXARGS in builtin-grep.c being 1024 or less.
 405c32="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"
 406test_expect_success 'grep -C1, hunk mark between files' '
 407        for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
 408        git add y-?? &&
 409        git grep -C1 "^[yz]" >actual &&
 410        test_cmp expected actual
 411'
 412
 413test_expect_success 'grep -C1 hunk mark between files' '
 414        git grep -C1 "^[yz]" >actual &&
 415        test_cmp expected actual
 416'
 417
 418test_expect_success 'log grep setup' '
 419        echo a >>file &&
 420        test_tick &&
 421        GIT_AUTHOR_NAME="With * Asterisk" \
 422        GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
 423        git commit -a -m "second" &&
 424
 425        echo a >>file &&
 426        test_tick &&
 427        git commit -a -m "third" &&
 428
 429        echo a >>file &&
 430        test_tick &&
 431        GIT_AUTHOR_NAME="Night Fall" \
 432        GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
 433        git commit -a -m "fourth"
 434'
 435
 436test_expect_success 'log grep (1)' '
 437        git log --author=author --pretty=tformat:%s >actual &&
 438        {
 439                echo third && echo initial
 440        } >expect &&
 441        test_cmp expect actual
 442'
 443
 444test_expect_success 'log grep (2)' '
 445        git log --author=" * " -F --pretty=tformat:%s >actual &&
 446        {
 447                echo second
 448        } >expect &&
 449        test_cmp expect actual
 450'
 451
 452test_expect_success 'log grep (3)' '
 453        git log --author="^A U" --pretty=tformat:%s >actual &&
 454        {
 455                echo third && echo initial
 456        } >expect &&
 457        test_cmp expect actual
 458'
 459
 460test_expect_success 'log grep (4)' '
 461        git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
 462        {
 463                echo second
 464        } >expect &&
 465        test_cmp expect actual
 466'
 467
 468test_expect_success 'log grep (5)' '
 469        git log --author=Thor -F --pretty=tformat:%s >actual &&
 470        {
 471                echo third && echo initial
 472        } >expect &&
 473        test_cmp expect actual
 474'
 475
 476test_expect_success 'log grep (6)' '
 477        git log --author=-0700  --pretty=tformat:%s >actual &&
 478        >expect &&
 479        test_cmp expect actual
 480'
 481
 482test_expect_success 'log with multiple --grep uses union' '
 483        git log --grep=i --grep=r --format=%s >actual &&
 484        {
 485                echo fourth && echo third && echo initial
 486        } >expect &&
 487        test_cmp expect actual
 488'
 489
 490test_expect_success 'log --all-match with multiple --grep uses intersection' '
 491        git log --all-match --grep=i --grep=r --format=%s >actual &&
 492        {
 493                echo third
 494        } >expect &&
 495        test_cmp expect actual
 496'
 497
 498test_expect_success 'log with multiple --author uses union' '
 499        git log --author="Thor" --author="Aster" --format=%s >actual &&
 500        {
 501            echo third && echo second && echo initial
 502        } >expect &&
 503        test_cmp expect actual
 504'
 505
 506test_expect_success 'log --all-match with multiple --author still uses union' '
 507        git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
 508        {
 509            echo third && echo second && echo initial
 510        } >expect &&
 511        test_cmp expect actual
 512'
 513
 514test_expect_success 'log --grep --author uses intersection' '
 515        # grep matches only third and fourth
 516        # author matches only initial and third
 517        git log --author="A U Thor" --grep=r --format=%s >actual &&
 518        {
 519                echo third
 520        } >expect &&
 521        test_cmp expect actual
 522'
 523
 524test_expect_success 'log --grep --grep --author takes union of greps and intersects with author' '
 525        # grep matches initial and second but not third
 526        # author matches only initial and third
 527        git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
 528        {
 529                echo initial
 530        } >expect &&
 531        test_cmp expect actual
 532'
 533
 534test_expect_success 'log --grep --author --author takes union of authors and intersects with grep' '
 535        # grep matches only initial and third
 536        # author matches all but second
 537        git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
 538        {
 539            echo third && echo initial
 540        } >expect &&
 541        test_cmp expect actual
 542'
 543
 544test_expect_success 'grep with CE_VALID file' '
 545        git update-index --assume-unchanged t/t &&
 546        rm t/t &&
 547        test "$(git grep test)" = "t/t:test" &&
 548        git update-index --no-assume-unchanged t/t &&
 549        git checkout t/t
 550'
 551
 552cat >expected <<EOF
 553hello.c=#include <stdio.h>
 554hello.c:        return 0;
 555EOF
 556
 557test_expect_success 'grep -p with userdiff' '
 558        git config diff.custom.funcname "^#" &&
 559        echo "hello.c diff=custom" >.gitattributes &&
 560        git grep -p return >actual &&
 561        test_cmp expected actual
 562'
 563
 564cat >expected <<EOF
 565hello.c=int main(int argc, const char **argv)
 566hello.c:        return 0;
 567EOF
 568
 569test_expect_success 'grep -p' '
 570        rm -f .gitattributes &&
 571        git grep -p return >actual &&
 572        test_cmp expected actual
 573'
 574
 575cat >expected <<EOF
 576hello.c-#include <stdio.h>
 577hello.c=int main(int argc, const char **argv)
 578hello.c-{
 579hello.c-        printf("Hello world.\n");
 580hello.c:        return 0;
 581EOF
 582
 583test_expect_success 'grep -p -B5' '
 584        git grep -p -B5 return >actual &&
 585        test_cmp expected actual
 586'
 587
 588cat >expected <<EOF
 589hello.c=int main(int argc, const char **argv)
 590hello.c-{
 591hello.c-        printf("Hello world.\n");
 592hello.c:        return 0;
 593hello.c-        /* char ?? */
 594hello.c-}
 595EOF
 596
 597test_expect_success 'grep -W' '
 598        git grep -W return >actual &&
 599        test_cmp expected actual
 600'
 601
 602cat >expected <<EOF
 603hello.c=        printf("Hello world.\n");
 604hello.c:        return 0;
 605hello.c-        /* char ?? */
 606EOF
 607
 608test_expect_success 'grep -W with userdiff' '
 609        test_when_finished "rm -f .gitattributes" &&
 610        git config diff.custom.xfuncname "(printf.*|})$" &&
 611        echo "hello.c diff=custom" >.gitattributes &&
 612        git grep -W return >actual &&
 613        test_cmp expected actual
 614'
 615
 616test_expect_success 'grep from a subdirectory to search wider area (1)' '
 617        mkdir -p s &&
 618        (
 619                cd s && git grep "x x x" ..
 620        )
 621'
 622
 623test_expect_success 'grep from a subdirectory to search wider area (2)' '
 624        mkdir -p s &&
 625        (
 626                cd s || exit 1
 627                ( git grep xxyyzz .. >out ; echo $? >status )
 628                ! test -s out &&
 629                test 1 = $(cat status)
 630        )
 631'
 632
 633cat >expected <<EOF
 634hello.c:int main(int argc, const char **argv)
 635EOF
 636
 637test_expect_success 'grep -Fi' '
 638        git grep -Fi "CHAR *" >actual &&
 639        test_cmp expected actual
 640'
 641
 642test_expect_success 'outside of git repository' '
 643        rm -fr non &&
 644        mkdir -p non/git/sub &&
 645        echo hello >non/git/file1 &&
 646        echo world >non/git/sub/file2 &&
 647        {
 648                echo file1:hello &&
 649                echo sub/file2:world
 650        } >non/expect.full &&
 651        echo file2:world >non/expect.sub &&
 652        (
 653                GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
 654                export GIT_CEILING_DIRECTORIES &&
 655                cd non/git &&
 656                test_must_fail git grep o &&
 657                git grep --no-index o >../actual.full &&
 658                test_cmp ../expect.full ../actual.full
 659                cd sub &&
 660                test_must_fail git grep o &&
 661                git grep --no-index o >../../actual.sub &&
 662                test_cmp ../../expect.sub ../../actual.sub
 663        ) &&
 664
 665        echo ".*o*" >non/git/.gitignore &&
 666        (
 667                GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
 668                export GIT_CEILING_DIRECTORIES &&
 669                cd non/git &&
 670                test_must_fail git grep o &&
 671                git grep --no-index --exclude-standard o >../actual.full &&
 672                test_cmp ../expect.full ../actual.full &&
 673
 674                {
 675                        echo ".gitignore:.*o*"
 676                        cat ../expect.full
 677                } >../expect.with.ignored &&
 678                git grep --no-index --no-exclude o >../actual.full &&
 679                test_cmp ../expect.with.ignored ../actual.full
 680        )
 681'
 682
 683test_expect_success 'inside git repository but with --no-index' '
 684        rm -fr is &&
 685        mkdir -p is/git/sub &&
 686        echo hello >is/git/file1 &&
 687        echo world >is/git/sub/file2 &&
 688        echo ".*o*" >is/git/.gitignore &&
 689        {
 690                echo file1:hello &&
 691                echo sub/file2:world
 692        } >is/expect.unignored &&
 693        {
 694                echo ".gitignore:.*o*" &&
 695                cat is/expect.unignored
 696        } >is/expect.full &&
 697        : >is/expect.empty &&
 698        echo file2:world >is/expect.sub &&
 699        (
 700                cd is/git &&
 701                git init &&
 702                test_must_fail git grep o >../actual.full &&
 703                test_cmp ../expect.empty ../actual.full &&
 704
 705                git grep --untracked o >../actual.unignored &&
 706                test_cmp ../expect.unignored ../actual.unignored &&
 707
 708                git grep --no-index o >../actual.full &&
 709                test_cmp ../expect.full ../actual.full &&
 710
 711                git grep --no-index --exclude-standard o >../actual.unignored &&
 712                test_cmp ../expect.unignored ../actual.unignored &&
 713
 714                cd sub &&
 715                test_must_fail git grep o >../../actual.sub &&
 716                test_cmp ../../expect.empty ../../actual.sub &&
 717
 718                git grep --no-index o >../../actual.sub &&
 719                test_cmp ../../expect.sub ../../actual.sub &&
 720
 721                git grep --untracked o >../../actual.sub &&
 722                test_cmp ../../expect.sub ../../actual.sub
 723        )
 724'
 725
 726test_expect_success 'setup double-dash tests' '
 727cat >double-dash <<EOF &&
 728--
 729->
 730other
 731EOF
 732git add double-dash
 733'
 734
 735cat >expected <<EOF
 736double-dash:->
 737EOF
 738test_expect_success 'grep -- pattern' '
 739        git grep -- "->" >actual &&
 740        test_cmp expected actual
 741'
 742test_expect_success 'grep -- pattern -- pathspec' '
 743        git grep -- "->" -- double-dash >actual &&
 744        test_cmp expected actual
 745'
 746test_expect_success 'grep -e pattern -- path' '
 747        git grep -e "->" -- double-dash >actual &&
 748        test_cmp expected actual
 749'
 750
 751cat >expected <<EOF
 752double-dash:--
 753EOF
 754test_expect_success 'grep -e -- -- path' '
 755        git grep -e -- -- double-dash >actual &&
 756        test_cmp expected actual
 757'
 758
 759cat >expected <<EOF
 760hello.c:int main(int argc, const char **argv)
 761hello.c:        printf("Hello world.\n");
 762EOF
 763
 764test_expect_success LIBPCRE 'grep --perl-regexp pattern' '
 765        git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
 766        test_cmp expected actual
 767'
 768
 769test_expect_success LIBPCRE 'grep -P pattern' '
 770        git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
 771        test_cmp expected actual
 772'
 773
 774test_expect_success 'grep pattern with grep.extendedRegexp=true' '
 775        >empty &&
 776        test_must_fail git -c grep.extendedregexp=true \
 777                grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
 778        test_cmp empty actual
 779'
 780
 781test_expect_success LIBPCRE 'grep -P pattern with grep.extendedRegexp=true' '
 782        git -c grep.extendedregexp=true \
 783                grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
 784        test_cmp expected actual
 785'
 786
 787test_expect_success LIBPCRE 'grep -P -v pattern' '
 788        {
 789                echo "ab:a+b*c"
 790                echo "ab:a+bc"
 791        } >expected &&
 792        git grep -P -v "abc" ab >actual &&
 793        test_cmp expected actual
 794'
 795
 796test_expect_success LIBPCRE 'grep -P -i pattern' '
 797        cat >expected <<-EOF &&
 798        hello.c:        printf("Hello world.\n");
 799        EOF
 800        git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
 801        test_cmp expected actual
 802'
 803
 804test_expect_success LIBPCRE 'grep -P -w pattern' '
 805        {
 806                echo "hello_world:Hello world"
 807                echo "hello_world:HeLLo world"
 808        } >expected &&
 809        git grep -P -w "He((?i)ll)o" hello_world >actual &&
 810        test_cmp expected actual
 811'
 812
 813test_expect_success 'grep -G invalidpattern properly dies ' '
 814        test_must_fail git grep -G "a["
 815'
 816
 817test_expect_success 'grep -E invalidpattern properly dies ' '
 818        test_must_fail git grep -E "a["
 819'
 820
 821test_expect_success LIBPCRE 'grep -P invalidpattern properly dies ' '
 822        test_must_fail git grep -P "a["
 823'
 824
 825test_expect_success 'grep -G -E -F pattern' '
 826        echo "ab:a+b*c" >expected &&
 827        git grep -G -E -F "a+b*c" ab >actual &&
 828        test_cmp expected actual
 829'
 830
 831test_expect_success 'grep -E -F -G pattern' '
 832        echo "ab:a+bc" >expected &&
 833        git grep -E -F -G "a+b*c" ab >actual &&
 834        test_cmp expected actual
 835'
 836
 837test_expect_success 'grep -F -G -E pattern' '
 838        echo "ab:abc" >expected &&
 839        git grep -F -G -E "a+b*c" ab >actual &&
 840        test_cmp expected actual
 841'
 842
 843test_expect_success 'grep -G -F -P -E pattern' '
 844        >empty &&
 845        test_must_fail git grep -G -F -P -E "a\x{2b}b\x{2a}c" ab >actual &&
 846        test_cmp empty actual
 847'
 848
 849test_expect_success LIBPCRE 'grep -G -F -E -P pattern' '
 850        echo "ab:a+b*c" >expected &&
 851        git grep -G -F -E -P "a\x{2b}b\x{2a}c" ab >actual &&
 852        test_cmp expected actual
 853'
 854
 855test_config() {
 856        git config "$1" "$2" &&
 857        test_when_finished "git config --unset $1"
 858}
 859
 860cat >expected <<EOF
 861hello.c<RED>:<RESET>int main(int argc, const char **argv)
 862hello.c<RED>-<RESET>{
 863<RED>--<RESET>
 864hello.c<RED>:<RESET>    /* char ?? */
 865hello.c<RED>-<RESET>}
 866<RED>--<RESET>
 867hello_world<RED>:<RESET>Hello_world
 868hello_world<RED>-<RESET>HeLLo_world
 869EOF
 870
 871test_expect_success 'grep --color, separator' '
 872        test_config color.grep.context          normal &&
 873        test_config color.grep.filename         normal &&
 874        test_config color.grep.function         normal &&
 875        test_config color.grep.linenumber       normal &&
 876        test_config color.grep.match            normal &&
 877        test_config color.grep.selected         normal &&
 878        test_config color.grep.separator        red &&
 879
 880        git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
 881        test_decode_color >actual &&
 882        test_cmp expected actual
 883'
 884
 885cat >expected <<EOF
 886hello.c:int main(int argc, const char **argv)
 887hello.c:        /* char ?? */
 888
 889hello_world:Hello_world
 890EOF
 891
 892test_expect_success 'grep --break' '
 893        git grep --break -e char -e lo_w hello.c hello_world >actual &&
 894        test_cmp expected actual
 895'
 896
 897cat >expected <<EOF
 898hello.c:int main(int argc, const char **argv)
 899hello.c-{
 900--
 901hello.c:        /* char ?? */
 902hello.c-}
 903
 904hello_world:Hello_world
 905hello_world-HeLLo_world
 906EOF
 907
 908test_expect_success 'grep --break with context' '
 909        git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
 910        test_cmp expected actual
 911'
 912
 913cat >expected <<EOF
 914hello.c
 915int main(int argc, const char **argv)
 916        /* char ?? */
 917hello_world
 918Hello_world
 919EOF
 920
 921test_expect_success 'grep --heading' '
 922        git grep --heading -e char -e lo_w hello.c hello_world >actual &&
 923        test_cmp expected actual
 924'
 925
 926cat >expected <<EOF
 927<BOLD;GREEN>hello.c<RESET>
 9282:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
 9296:      /* <BLACK;BYELLOW>char<RESET> ?? */
 930
 931<BOLD;GREEN>hello_world<RESET>
 9323:Hel<BLACK;BYELLOW>lo_w<RESET>orld
 933EOF
 934
 935test_expect_success 'mimic ack-grep --group' '
 936        test_config color.grep.context          normal &&
 937        test_config color.grep.filename         "bold green" &&
 938        test_config color.grep.function         normal &&
 939        test_config color.grep.linenumber       normal &&
 940        test_config color.grep.match            "black yellow" &&
 941        test_config color.grep.selected         normal &&
 942        test_config color.grep.separator        normal &&
 943
 944        git grep --break --heading -n --color \
 945                -e char -e lo_w hello.c hello_world |
 946        test_decode_color >actual &&
 947        test_cmp expected actual
 948'
 949
 950cat >expected <<EOF
 951space: line with leading space1
 952space: line with leading space2
 953space: line with leading space3
 954EOF
 955
 956test_expect_success LIBPCRE 'grep -E "^ "' '
 957        git grep -E "^ " space >actual &&
 958        test_cmp expected actual
 959'
 960
 961test_expect_success LIBPCRE 'grep -P "^ "' '
 962        git grep -P "^ " space >actual &&
 963        test_cmp expected actual
 964'
 965
 966test_done