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