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