t / t7810-grep.shon commit Merge branch 'jn/maint-gitweb-invalid-regexp' into maint (3ecd0c8)
   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        git add . &&
  51        test_tick &&
  52        git commit -m initial
  53'
  54
  55test_expect_success 'grep should not segfault with a bad input' '
  56        test_must_fail git grep "("
  57'
  58
  59for H in HEAD ''
  60do
  61        case "$H" in
  62        HEAD)   HC='HEAD:' L='HEAD' ;;
  63        '')     HC= L='in working tree' ;;
  64        esac
  65
  66        test_expect_success "grep -w $L" '
  67                {
  68                        echo ${HC}file:1:foo mmap bar
  69                        echo ${HC}file:3:foo_mmap bar mmap
  70                        echo ${HC}file:4:foo mmap bar_mmap
  71                        echo ${HC}file:5:foo_mmap bar mmap baz
  72                } >expected &&
  73                git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
  74                test_cmp expected actual
  75        '
  76
  77        test_expect_success "grep -w $L" '
  78                {
  79                        echo ${HC}file:1:foo mmap bar
  80                        echo ${HC}file:3:foo_mmap bar mmap
  81                        echo ${HC}file:4:foo mmap bar_mmap
  82                        echo ${HC}file:5:foo_mmap bar mmap baz
  83                } >expected &&
  84                git -c grep.linenumber=true grep -w -e mmap $H >actual &&
  85                test_cmp expected actual
  86        '
  87
  88        test_expect_success "grep -w $L" '
  89                {
  90                        echo ${HC}file:foo mmap bar
  91                        echo ${HC}file:foo_mmap bar mmap
  92                        echo ${HC}file:foo mmap bar_mmap
  93                        echo ${HC}file:foo_mmap bar mmap baz
  94                } >expected &&
  95                git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
  96                test_cmp expected actual
  97        '
  98
  99        test_expect_success "grep -w $L (w)" '
 100                : >expected &&
 101                test_must_fail git grep -n -w -e "^w" >actual &&
 102                test_cmp expected actual
 103        '
 104
 105        test_expect_success "grep -w $L (x)" '
 106                {
 107                        echo ${HC}x:1:x x xx x
 108                } >expected &&
 109                git grep -n -w -e "x xx* x" $H >actual &&
 110                test_cmp expected actual
 111        '
 112
 113        test_expect_success "grep -w $L (y-1)" '
 114                {
 115                        echo ${HC}y:1:y yy
 116                } >expected &&
 117                git grep -n -w -e "^y" $H >actual &&
 118                test_cmp expected actual
 119        '
 120
 121        test_expect_success "grep -w $L (y-2)" '
 122                : >expected &&
 123                if git grep -n -w -e "^y y" $H >actual
 124                then
 125                        echo should not have matched
 126                        cat actual
 127                        false
 128                else
 129                        test_cmp expected actual
 130                fi
 131        '
 132
 133        test_expect_success "grep -w $L (z)" '
 134                : >expected &&
 135                if git grep -n -w -e "^z" $H >actual
 136                then
 137                        echo should not have matched
 138                        cat actual
 139                        false
 140                else
 141                        test_cmp expected actual
 142                fi
 143        '
 144
 145        test_expect_success "grep $L (t-1)" '
 146                echo "${HC}t/t:1:test" >expected &&
 147                git grep -n -e test $H >actual &&
 148                test_cmp expected actual
 149        '
 150
 151        test_expect_success "grep $L (t-2)" '
 152                echo "${HC}t:1:test" >expected &&
 153                (
 154                        cd t &&
 155                        git grep -n -e test $H
 156                ) >actual &&
 157                test_cmp expected actual
 158        '
 159
 160        test_expect_success "grep $L (t-3)" '
 161                echo "${HC}t/t:1:test" >expected &&
 162                (
 163                        cd t &&
 164                        git grep --full-name -n -e test $H
 165                ) >actual &&
 166                test_cmp expected actual
 167        '
 168
 169        test_expect_success "grep -c $L (no /dev/null)" '
 170                ! git grep -c test $H | grep /dev/null
 171        '
 172
 173        test_expect_success "grep --max-depth -1 $L" '
 174                {
 175                        echo ${HC}t/a/v:1:vvv
 176                        echo ${HC}t/v:1:vvv
 177                        echo ${HC}v:1:vvv
 178                } >expected &&
 179                git grep --max-depth -1 -n -e vvv $H >actual &&
 180                test_cmp expected actual
 181        '
 182
 183        test_expect_success "grep --max-depth 0 $L" '
 184                {
 185                        echo ${HC}v:1:vvv
 186                } >expected &&
 187                git grep --max-depth 0 -n -e vvv $H >actual &&
 188                test_cmp expected actual
 189        '
 190
 191        test_expect_success "grep --max-depth 0 -- '*' $L" '
 192                {
 193                        echo ${HC}t/a/v:1:vvv
 194                        echo ${HC}t/v:1:vvv
 195                        echo ${HC}v:1:vvv
 196                } >expected &&
 197                git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
 198                test_cmp expected actual
 199        '
 200
 201        test_expect_success "grep --max-depth 1 $L" '
 202                {
 203                        echo ${HC}t/v:1:vvv
 204                        echo ${HC}v:1:vvv
 205                } >expected &&
 206                git grep --max-depth 1 -n -e vvv $H >actual &&
 207                test_cmp expected actual
 208        '
 209
 210        test_expect_success "grep --max-depth 0 -- t $L" '
 211                {
 212                        echo ${HC}t/v:1:vvv
 213                } >expected &&
 214                git grep --max-depth 0 -n -e vvv $H -- t >actual &&
 215                test_cmp expected actual
 216        '
 217
 218        test_expect_success "grep --max-depth 0 -- . t $L" '
 219                {
 220                        echo ${HC}t/v:1:vvv
 221                        echo ${HC}v:1:vvv
 222                } >expected &&
 223                git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
 224                test_cmp expected actual
 225        '
 226
 227        test_expect_success "grep --max-depth 0 -- t . $L" '
 228                {
 229                        echo ${HC}t/v:1:vvv
 230                        echo ${HC}v:1:vvv
 231                } >expected &&
 232                git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
 233                test_cmp expected actual
 234        '
 235        test_expect_success "grep $L with grep.extendedRegexp=false" '
 236                echo "ab:a+bc" >expected &&
 237                git -c grep.extendedRegexp=false grep "a+b*c" ab >actual &&
 238                test_cmp expected actual
 239        '
 240
 241        test_expect_success "grep $L with grep.extendedRegexp=true" '
 242                echo "ab:abc" >expected &&
 243                git -c grep.extendedRegexp=true grep "a+b*c" ab >actual &&
 244                test_cmp expected actual
 245        '
 246done
 247
 248cat >expected <<EOF
 249file
 250EOF
 251test_expect_success 'grep -l -C' '
 252        git grep -l -C1 foo >actual &&
 253        test_cmp expected actual
 254'
 255
 256cat >expected <<EOF
 257file:5
 258EOF
 259test_expect_success 'grep -l -C' '
 260        git grep -c -C1 foo >actual &&
 261        test_cmp expected actual
 262'
 263
 264test_expect_success 'grep -L -C' '
 265        git ls-files >expected &&
 266        git grep -L -C1 nonexistent_string >actual &&
 267        test_cmp expected actual
 268'
 269
 270cat >expected <<EOF
 271file:foo mmap bar_mmap
 272EOF
 273
 274test_expect_success 'grep -e A --and -e B' '
 275        git grep -e "foo mmap" --and -e bar_mmap >actual &&
 276        test_cmp expected actual
 277'
 278
 279cat >expected <<EOF
 280file:foo_mmap bar mmap
 281file:foo_mmap bar mmap baz
 282EOF
 283
 284
 285test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
 286        git grep \( -e foo_ --or -e baz \) \
 287                --and -e " mmap" >actual &&
 288        test_cmp expected actual
 289'
 290
 291cat >expected <<EOF
 292file:foo mmap bar
 293EOF
 294
 295test_expect_success 'grep -e A --and --not -e B' '
 296        git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
 297        test_cmp expected actual
 298'
 299
 300test_expect_success 'grep should ignore GREP_OPTIONS' '
 301        GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
 302        test_cmp expected actual
 303'
 304
 305test_expect_success 'grep -f, non-existent file' '
 306        test_must_fail git grep -f patterns
 307'
 308
 309cat >expected <<EOF
 310file:foo mmap bar
 311file:foo_mmap bar
 312file:foo_mmap bar mmap
 313file:foo mmap bar_mmap
 314file:foo_mmap bar mmap baz
 315EOF
 316
 317cat >pattern <<EOF
 318mmap
 319EOF
 320
 321test_expect_success 'grep -f, one pattern' '
 322        git grep -f pattern >actual &&
 323        test_cmp expected actual
 324'
 325
 326cat >expected <<EOF
 327file:foo mmap bar
 328file:foo_mmap bar
 329file:foo_mmap bar mmap
 330file:foo mmap bar_mmap
 331file:foo_mmap bar mmap baz
 332t/a/v:vvv
 333t/v:vvv
 334v:vvv
 335EOF
 336
 337cat >patterns <<EOF
 338mmap
 339vvv
 340EOF
 341
 342test_expect_success 'grep -f, multiple patterns' '
 343        git grep -f patterns >actual &&
 344        test_cmp expected actual
 345'
 346
 347cat >expected <<EOF
 348file:foo mmap bar
 349file:foo_mmap bar
 350file:foo_mmap bar mmap
 351file:foo mmap bar_mmap
 352file:foo_mmap bar mmap baz
 353t/a/v:vvv
 354t/v:vvv
 355v:vvv
 356EOF
 357
 358cat >patterns <<EOF
 359
 360mmap
 361
 362vvv
 363
 364EOF
 365
 366test_expect_success 'grep -f, ignore empty lines' '
 367        git grep -f patterns >actual &&
 368        test_cmp expected actual
 369'
 370
 371test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
 372        git grep -f - <patterns >actual &&
 373        test_cmp expected actual
 374'
 375
 376cat >expected <<EOF
 377y:y yy
 378--
 379z:zzz
 380EOF
 381
 382test_expect_success 'grep -q, silently report matches' '
 383        >empty &&
 384        git grep -q mmap >actual &&
 385        test_cmp empty actual &&
 386        test_must_fail git grep -q qfwfq >actual &&
 387        test_cmp empty actual
 388'
 389
 390# Create 1024 file names that sort between "y" and "z" to make sure
 391# the two files are handled by different calls to an external grep.
 392# This depends on MAXARGS in builtin-grep.c being 1024 or less.
 393c32="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"
 394test_expect_success 'grep -C1, hunk mark between files' '
 395        for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
 396        git add y-?? &&
 397        git grep -C1 "^[yz]" >actual &&
 398        test_cmp expected actual
 399'
 400
 401test_expect_success 'grep -C1 hunk mark between files' '
 402        git grep -C1 "^[yz]" >actual &&
 403        test_cmp expected actual
 404'
 405
 406test_expect_success 'log grep setup' '
 407        echo a >>file &&
 408        test_tick &&
 409        GIT_AUTHOR_NAME="With * Asterisk" \
 410        GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
 411        git commit -a -m "second" &&
 412
 413        echo a >>file &&
 414        test_tick &&
 415        git commit -a -m "third" &&
 416
 417        echo a >>file &&
 418        test_tick &&
 419        GIT_AUTHOR_NAME="Night Fall" \
 420        GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
 421        git commit -a -m "fourth"
 422'
 423
 424test_expect_success 'log grep (1)' '
 425        git log --author=author --pretty=tformat:%s >actual &&
 426        ( echo third ; echo initial ) >expect &&
 427        test_cmp expect actual
 428'
 429
 430test_expect_success 'log grep (2)' '
 431        git log --author=" * " -F --pretty=tformat:%s >actual &&
 432        ( echo second ) >expect &&
 433        test_cmp expect actual
 434'
 435
 436test_expect_success 'log grep (3)' '
 437        git log --author="^A U" --pretty=tformat:%s >actual &&
 438        ( echo third ; echo initial ) >expect &&
 439        test_cmp expect actual
 440'
 441
 442test_expect_success 'log grep (4)' '
 443        git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
 444        ( echo second ) >expect &&
 445        test_cmp expect actual
 446'
 447
 448test_expect_success 'log grep (5)' '
 449        git log --author=Thor -F --pretty=tformat:%s >actual &&
 450        ( echo third ; echo initial ) >expect &&
 451        test_cmp expect actual
 452'
 453
 454test_expect_success 'log grep (6)' '
 455        git log --author=-0700  --pretty=tformat:%s >actual &&
 456        >expect &&
 457        test_cmp expect actual
 458'
 459
 460test_expect_success 'log --grep --author implicitly uses all-match' '
 461        # grep matches initial and second but not third
 462        # author matches only initial and third
 463        git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
 464        echo initial >expect &&
 465        test_cmp expect actual
 466'
 467
 468test_expect_success 'log with multiple --author uses union' '
 469        git log --author="Thor" --author="Aster" --format=%s >actual &&
 470        {
 471            echo third && echo second && echo initial
 472        } >expect &&
 473        test_cmp expect actual
 474'
 475
 476test_expect_success 'log with --grep and multiple --author uses all-match' '
 477        git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
 478        {
 479            echo third && echo initial
 480        } >expect &&
 481        test_cmp expect actual
 482'
 483
 484test_expect_success 'log with --grep and multiple --author uses all-match' '
 485        git log --author="Thor" --author="Night" --grep=q --format=%s >actual &&
 486        >expect &&
 487        test_cmp expect actual
 488'
 489
 490test_expect_success 'grep with CE_VALID file' '
 491        git update-index --assume-unchanged t/t &&
 492        rm t/t &&
 493        test "$(git grep test)" = "t/t:test" &&
 494        git update-index --no-assume-unchanged t/t &&
 495        git checkout t/t
 496'
 497
 498cat >expected <<EOF
 499hello.c=#include <stdio.h>
 500hello.c:        return 0;
 501EOF
 502
 503test_expect_success 'grep -p with userdiff' '
 504        git config diff.custom.funcname "^#" &&
 505        echo "hello.c diff=custom" >.gitattributes &&
 506        git grep -p return >actual &&
 507        test_cmp expected actual
 508'
 509
 510cat >expected <<EOF
 511hello.c=int main(int argc, const char **argv)
 512hello.c:        return 0;
 513EOF
 514
 515test_expect_success 'grep -p' '
 516        rm -f .gitattributes &&
 517        git grep -p return >actual &&
 518        test_cmp expected actual
 519'
 520
 521cat >expected <<EOF
 522hello.c-#include <stdio.h>
 523hello.c=int main(int argc, const char **argv)
 524hello.c-{
 525hello.c-        printf("Hello world.\n");
 526hello.c:        return 0;
 527EOF
 528
 529test_expect_success 'grep -p -B5' '
 530        git grep -p -B5 return >actual &&
 531        test_cmp expected actual
 532'
 533
 534cat >expected <<EOF
 535hello.c=int main(int argc, const char **argv)
 536hello.c-{
 537hello.c-        printf("Hello world.\n");
 538hello.c:        return 0;
 539hello.c-        /* char ?? */
 540hello.c-}
 541EOF
 542
 543test_expect_success 'grep -W' '
 544        git grep -W return >actual &&
 545        test_cmp expected actual
 546'
 547
 548cat >expected <<EOF
 549hello.c=        printf("Hello world.\n");
 550hello.c:        return 0;
 551hello.c-        /* char ?? */
 552EOF
 553
 554test_expect_success 'grep -W with userdiff' '
 555        test_when_finished "rm -f .gitattributes" &&
 556        git config diff.custom.xfuncname "(printf.*|})$" &&
 557        echo "hello.c diff=custom" >.gitattributes &&
 558        git grep -W return >actual &&
 559        test_cmp expected actual
 560'
 561
 562test_expect_success 'grep from a subdirectory to search wider area (1)' '
 563        mkdir -p s &&
 564        (
 565                cd s && git grep "x x x" ..
 566        )
 567'
 568
 569test_expect_success 'grep from a subdirectory to search wider area (2)' '
 570        mkdir -p s &&
 571        (
 572                cd s || exit 1
 573                ( git grep xxyyzz .. >out ; echo $? >status )
 574                ! test -s out &&
 575                test 1 = $(cat status)
 576        )
 577'
 578
 579cat >expected <<EOF
 580hello.c:int main(int argc, const char **argv)
 581EOF
 582
 583test_expect_success 'grep -Fi' '
 584        git grep -Fi "CHAR *" >actual &&
 585        test_cmp expected actual
 586'
 587
 588test_expect_success 'outside of git repository' '
 589        rm -fr non &&
 590        mkdir -p non/git/sub &&
 591        echo hello >non/git/file1 &&
 592        echo world >non/git/sub/file2 &&
 593        {
 594                echo file1:hello &&
 595                echo sub/file2:world
 596        } >non/expect.full &&
 597        echo file2:world >non/expect.sub &&
 598        (
 599                GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
 600                export GIT_CEILING_DIRECTORIES &&
 601                cd non/git &&
 602                test_must_fail git grep o &&
 603                git grep --no-index o >../actual.full &&
 604                test_cmp ../expect.full ../actual.full
 605                cd sub &&
 606                test_must_fail git grep o &&
 607                git grep --no-index o >../../actual.sub &&
 608                test_cmp ../../expect.sub ../../actual.sub
 609        ) &&
 610
 611        echo ".*o*" >non/git/.gitignore &&
 612        (
 613                GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
 614                export GIT_CEILING_DIRECTORIES &&
 615                cd non/git &&
 616                test_must_fail git grep o &&
 617                git grep --no-index --exclude-standard o >../actual.full &&
 618                test_cmp ../expect.full ../actual.full &&
 619
 620                {
 621                        echo ".gitignore:.*o*"
 622                        cat ../expect.full
 623                } >../expect.with.ignored &&
 624                git grep --no-index --no-exclude o >../actual.full &&
 625                test_cmp ../expect.with.ignored ../actual.full
 626        )
 627'
 628
 629test_expect_success 'inside git repository but with --no-index' '
 630        rm -fr is &&
 631        mkdir -p is/git/sub &&
 632        echo hello >is/git/file1 &&
 633        echo world >is/git/sub/file2 &&
 634        echo ".*o*" >is/git/.gitignore &&
 635        {
 636                echo file1:hello &&
 637                echo sub/file2:world
 638        } >is/expect.unignored &&
 639        {
 640                echo ".gitignore:.*o*" &&
 641                cat is/expect.unignored
 642        } >is/expect.full &&
 643        : >is/expect.empty &&
 644        echo file2:world >is/expect.sub &&
 645        (
 646                cd is/git &&
 647                git init &&
 648                test_must_fail git grep o >../actual.full &&
 649                test_cmp ../expect.empty ../actual.full &&
 650
 651                git grep --untracked o >../actual.unignored &&
 652                test_cmp ../expect.unignored ../actual.unignored &&
 653
 654                git grep --no-index o >../actual.full &&
 655                test_cmp ../expect.full ../actual.full &&
 656
 657                git grep --no-index --exclude-standard o >../actual.unignored &&
 658                test_cmp ../expect.unignored ../actual.unignored &&
 659
 660                cd sub &&
 661                test_must_fail git grep o >../../actual.sub &&
 662                test_cmp ../../expect.empty ../../actual.sub &&
 663
 664                git grep --no-index o >../../actual.sub &&
 665                test_cmp ../../expect.sub ../../actual.sub &&
 666
 667                git grep --untracked o >../../actual.sub &&
 668                test_cmp ../../expect.sub ../../actual.sub
 669        )
 670'
 671
 672test_expect_success 'setup double-dash tests' '
 673cat >double-dash <<EOF &&
 674--
 675->
 676other
 677EOF
 678git add double-dash
 679'
 680
 681cat >expected <<EOF
 682double-dash:->
 683EOF
 684test_expect_success 'grep -- pattern' '
 685        git grep -- "->" >actual &&
 686        test_cmp expected actual
 687'
 688test_expect_success 'grep -- pattern -- pathspec' '
 689        git grep -- "->" -- double-dash >actual &&
 690        test_cmp expected actual
 691'
 692test_expect_success 'grep -e pattern -- path' '
 693        git grep -e "->" -- double-dash >actual &&
 694        test_cmp expected actual
 695'
 696
 697cat >expected <<EOF
 698double-dash:--
 699EOF
 700test_expect_success 'grep -e -- -- path' '
 701        git grep -e -- -- double-dash >actual &&
 702        test_cmp expected actual
 703'
 704
 705cat >expected <<EOF
 706hello.c:int main(int argc, const char **argv)
 707hello.c:        printf("Hello world.\n");
 708EOF
 709
 710test_expect_success LIBPCRE 'grep --perl-regexp pattern' '
 711        git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
 712        test_cmp expected actual
 713'
 714
 715test_expect_success LIBPCRE 'grep -P pattern' '
 716        git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
 717        test_cmp expected actual
 718'
 719
 720test_expect_success 'grep pattern with grep.extendedRegexp=true' '
 721        >empty &&
 722        test_must_fail git -c grep.extendedregexp=true \
 723                grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
 724        test_cmp empty actual
 725'
 726
 727test_expect_success LIBPCRE 'grep -P pattern with grep.extendedRegexp=true' '
 728        git -c grep.extendedregexp=true \
 729                grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
 730        test_cmp expected actual
 731'
 732
 733test_expect_success LIBPCRE 'grep -P -v pattern' '
 734        {
 735                echo "ab:a+b*c"
 736                echo "ab:a+bc"
 737        } >expected &&
 738        git grep -P -v "abc" ab >actual &&
 739        test_cmp expected actual
 740'
 741
 742test_expect_success LIBPCRE 'grep -P -i pattern' '
 743        cat >expected <<-EOF &&
 744        hello.c:        printf("Hello world.\n");
 745        EOF
 746        git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
 747        test_cmp expected actual
 748'
 749
 750test_expect_success LIBPCRE 'grep -P -w pattern' '
 751        {
 752                echo "hello_world:Hello world"
 753                echo "hello_world:HeLLo world"
 754        } >expected &&
 755        git grep -P -w "He((?i)ll)o" hello_world >actual &&
 756        test_cmp expected actual
 757'
 758
 759test_expect_success 'grep -G invalidpattern properly dies ' '
 760        test_must_fail git grep -G "a["
 761'
 762
 763test_expect_success 'grep -E invalidpattern properly dies ' '
 764        test_must_fail git grep -E "a["
 765'
 766
 767test_expect_success LIBPCRE 'grep -P invalidpattern properly dies ' '
 768        test_must_fail git grep -P "a["
 769'
 770
 771test_expect_success 'grep -G -E -F pattern' '
 772        echo "ab:a+b*c" >expected &&
 773        git grep -G -E -F "a+b*c" ab >actual &&
 774        test_cmp expected actual
 775'
 776
 777test_expect_success 'grep -E -F -G pattern' '
 778        echo "ab:a+bc" >expected &&
 779        git grep -E -F -G "a+b*c" ab >actual &&
 780        test_cmp expected actual
 781'
 782
 783test_expect_success 'grep -F -G -E pattern' '
 784        echo "ab:abc" >expected &&
 785        git grep -F -G -E "a+b*c" ab >actual &&
 786        test_cmp expected actual
 787'
 788
 789test_expect_success 'grep -G -F -P -E pattern' '
 790        >empty &&
 791        test_must_fail git grep -G -F -P -E "a\x{2b}b\x{2a}c" ab >actual &&
 792        test_cmp empty actual
 793'
 794
 795test_expect_success LIBPCRE 'grep -G -F -E -P pattern' '
 796        echo "ab:a+b*c" >expected &&
 797        git grep -G -F -E -P "a\x{2b}b\x{2a}c" ab >actual &&
 798        test_cmp expected actual
 799'
 800
 801test_config() {
 802        git config "$1" "$2" &&
 803        test_when_finished "git config --unset $1"
 804}
 805
 806cat >expected <<EOF
 807hello.c<RED>:<RESET>int main(int argc, const char **argv)
 808hello.c<RED>-<RESET>{
 809<RED>--<RESET>
 810hello.c<RED>:<RESET>    /* char ?? */
 811hello.c<RED>-<RESET>}
 812<RED>--<RESET>
 813hello_world<RED>:<RESET>Hello_world
 814hello_world<RED>-<RESET>HeLLo_world
 815EOF
 816
 817test_expect_success 'grep --color, separator' '
 818        test_config color.grep.context          normal &&
 819        test_config color.grep.filename         normal &&
 820        test_config color.grep.function         normal &&
 821        test_config color.grep.linenumber       normal &&
 822        test_config color.grep.match            normal &&
 823        test_config color.grep.selected         normal &&
 824        test_config color.grep.separator        red &&
 825
 826        git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
 827        test_decode_color >actual &&
 828        test_cmp expected actual
 829'
 830
 831cat >expected <<EOF
 832hello.c:int main(int argc, const char **argv)
 833hello.c:        /* char ?? */
 834
 835hello_world:Hello_world
 836EOF
 837
 838test_expect_success 'grep --break' '
 839        git grep --break -e char -e lo_w hello.c hello_world >actual &&
 840        test_cmp expected actual
 841'
 842
 843cat >expected <<EOF
 844hello.c:int main(int argc, const char **argv)
 845hello.c-{
 846--
 847hello.c:        /* char ?? */
 848hello.c-}
 849
 850hello_world:Hello_world
 851hello_world-HeLLo_world
 852EOF
 853
 854test_expect_success 'grep --break with context' '
 855        git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
 856        test_cmp expected actual
 857'
 858
 859cat >expected <<EOF
 860hello.c
 861int main(int argc, const char **argv)
 862        /* char ?? */
 863hello_world
 864Hello_world
 865EOF
 866
 867test_expect_success 'grep --heading' '
 868        git grep --heading -e char -e lo_w hello.c hello_world >actual &&
 869        test_cmp expected actual
 870'
 871
 872cat >expected <<EOF
 873<BOLD;GREEN>hello.c<RESET>
 8742:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
 8756:      /* <BLACK;BYELLOW>char<RESET> ?? */
 876
 877<BOLD;GREEN>hello_world<RESET>
 8783:Hel<BLACK;BYELLOW>lo_w<RESET>orld
 879EOF
 880
 881test_expect_success 'mimic ack-grep --group' '
 882        test_config color.grep.context          normal &&
 883        test_config color.grep.filename         "bold green" &&
 884        test_config color.grep.function         normal &&
 885        test_config color.grep.linenumber       normal &&
 886        test_config color.grep.match            "black yellow" &&
 887        test_config color.grep.selected         normal &&
 888        test_config color.grep.separator        normal &&
 889
 890        git grep --break --heading -n --color \
 891                -e char -e lo_w hello.c hello_world |
 892        test_decode_color >actual &&
 893        test_cmp expected actual
 894'
 895
 896test_done