t / t7810-grep.shon commit t7810: avoid assumption about invalid regex syntax (7675c7b)
   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        {
  41                echo d &&
  42                echo 0
  43        } >d0 &&
  44        echo vvv >v &&
  45        echo ww w >w &&
  46        echo x x xx x >x &&
  47        echo y yy >y &&
  48        echo zzz > z &&
  49        mkdir t &&
  50        echo test >t/t &&
  51        echo vvv >t/v &&
  52        mkdir t/a &&
  53        echo vvv >t/a/v &&
  54        {
  55                echo "line without leading space1"
  56                echo " line with leading space1"
  57                echo " line with leading space2"
  58                echo " line with leading space3"
  59                echo "line without leading space2"
  60        } >space &&
  61        git add . &&
  62        test_tick &&
  63        git commit -m initial
  64'
  65
  66test_expect_success 'grep should not segfault with a bad input' '
  67        test_must_fail git grep "("
  68'
  69
  70for H in HEAD ''
  71do
  72        case "$H" in
  73        HEAD)   HC='HEAD:' L='HEAD' ;;
  74        '')     HC= L='in working tree' ;;
  75        esac
  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=false grep -n -w -e mmap $H >actual &&
  85                test_cmp expected actual
  86        '
  87
  88        test_expect_success "grep -w $L" '
  89                {
  90                        echo ${HC}file:1:foo mmap bar
  91                        echo ${HC}file:3:foo_mmap bar mmap
  92                        echo ${HC}file:4:foo mmap bar_mmap
  93                        echo ${HC}file:5:foo_mmap bar mmap baz
  94                } >expected &&
  95                git -c grep.linenumber=true grep -w -e mmap $H >actual &&
  96                test_cmp expected actual
  97        '
  98
  99        test_expect_success "grep -w $L" '
 100                {
 101                        echo ${HC}file:foo mmap bar
 102                        echo ${HC}file:foo_mmap bar mmap
 103                        echo ${HC}file:foo mmap bar_mmap
 104                        echo ${HC}file:foo_mmap bar mmap baz
 105                } >expected &&
 106                git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
 107                test_cmp expected actual
 108        '
 109
 110        test_expect_success "grep -w $L (w)" '
 111                : >expected &&
 112                test_must_fail git grep -n -w -e "^w" $H >actual &&
 113                test_cmp expected actual
 114        '
 115
 116        test_expect_success "grep -w $L (x)" '
 117                {
 118                        echo ${HC}x:1:x x xx x
 119                } >expected &&
 120                git grep -n -w -e "x xx* x" $H >actual &&
 121                test_cmp expected actual
 122        '
 123
 124        test_expect_success "grep -w $L (y-1)" '
 125                {
 126                        echo ${HC}y:1:y yy
 127                } >expected &&
 128                git grep -n -w -e "^y" $H >actual &&
 129                test_cmp expected actual
 130        '
 131
 132        test_expect_success "grep -w $L (y-2)" '
 133                : >expected &&
 134                if git grep -n -w -e "^y y" $H >actual
 135                then
 136                        echo should not have matched
 137                        cat actual
 138                        false
 139                else
 140                        test_cmp expected actual
 141                fi
 142        '
 143
 144        test_expect_success "grep -w $L (z)" '
 145                : >expected &&
 146                if git grep -n -w -e "^z" $H >actual
 147                then
 148                        echo should not have matched
 149                        cat actual
 150                        false
 151                else
 152                        test_cmp expected actual
 153                fi
 154        '
 155
 156        test_expect_success "grep $L (t-1)" '
 157                echo "${HC}t/t:1:test" >expected &&
 158                git grep -n -e test $H >actual &&
 159                test_cmp expected actual
 160        '
 161
 162        test_expect_success "grep $L (t-2)" '
 163                echo "${HC}t:1:test" >expected &&
 164                (
 165                        cd t &&
 166                        git grep -n -e test $H
 167                ) >actual &&
 168                test_cmp expected actual
 169        '
 170
 171        test_expect_success "grep $L (t-3)" '
 172                echo "${HC}t/t:1:test" >expected &&
 173                (
 174                        cd t &&
 175                        git grep --full-name -n -e test $H
 176                ) >actual &&
 177                test_cmp expected actual
 178        '
 179
 180        test_expect_success "grep -c $L (no /dev/null)" '
 181                ! git grep -c test $H | grep /dev/null
 182        '
 183
 184        test_expect_success "grep --max-depth -1 $L" '
 185                {
 186                        echo ${HC}t/a/v:1:vvv
 187                        echo ${HC}t/v:1:vvv
 188                        echo ${HC}v:1:vvv
 189                } >expected &&
 190                git grep --max-depth -1 -n -e vvv $H >actual &&
 191                test_cmp expected actual
 192        '
 193
 194        test_expect_success "grep --max-depth 0 $L" '
 195                {
 196                        echo ${HC}v:1:vvv
 197                } >expected &&
 198                git grep --max-depth 0 -n -e vvv $H >actual &&
 199                test_cmp expected actual
 200        '
 201
 202        test_expect_success "grep --max-depth 0 -- '*' $L" '
 203                {
 204                        echo ${HC}t/a/v:1:vvv
 205                        echo ${HC}t/v:1:vvv
 206                        echo ${HC}v:1:vvv
 207                } >expected &&
 208                git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
 209                test_cmp expected actual
 210        '
 211
 212        test_expect_success "grep --max-depth 1 $L" '
 213                {
 214                        echo ${HC}t/v:1:vvv
 215                        echo ${HC}v:1:vvv
 216                } >expected &&
 217                git grep --max-depth 1 -n -e vvv $H >actual &&
 218                test_cmp expected actual
 219        '
 220
 221        test_expect_success "grep --max-depth 0 -- t $L" '
 222                {
 223                        echo ${HC}t/v:1:vvv
 224                } >expected &&
 225                git grep --max-depth 0 -n -e vvv $H -- t >actual &&
 226                test_cmp expected actual
 227        '
 228
 229        test_expect_success "grep --max-depth 0 -- . t $L" '
 230                {
 231                        echo ${HC}t/v:1:vvv
 232                        echo ${HC}v:1:vvv
 233                } >expected &&
 234                git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
 235                test_cmp expected actual
 236        '
 237
 238        test_expect_success "grep --max-depth 0 -- t . $L" '
 239                {
 240                        echo ${HC}t/v:1:vvv
 241                        echo ${HC}v:1:vvv
 242                } >expected &&
 243                git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
 244                test_cmp expected actual
 245        '
 246        test_expect_success "grep $L with grep.extendedRegexp=false" '
 247                echo "${HC}ab:a+bc" >expected &&
 248                git -c grep.extendedRegexp=false grep "a+b*c" $H ab >actual &&
 249                test_cmp expected actual
 250        '
 251
 252        test_expect_success "grep $L with grep.extendedRegexp=true" '
 253                echo "${HC}ab:abc" >expected &&
 254                git -c grep.extendedRegexp=true grep "a+b*c" $H ab >actual &&
 255                test_cmp expected actual
 256        '
 257
 258        test_expect_success "grep $L with grep.patterntype=basic" '
 259                echo "${HC}ab:a+bc" >expected &&
 260                git -c grep.patterntype=basic grep "a+b*c" $H ab >actual &&
 261                test_cmp expected actual
 262        '
 263
 264        test_expect_success "grep $L with grep.patterntype=extended" '
 265                echo "${HC}ab:abc" >expected &&
 266                git -c grep.patterntype=extended grep "a+b*c" $H ab >actual &&
 267                test_cmp expected actual
 268        '
 269
 270        test_expect_success "grep $L with grep.patterntype=fixed" '
 271                echo "${HC}ab:a+b*c" >expected &&
 272                git -c grep.patterntype=fixed grep "a+b*c" $H ab >actual &&
 273                test_cmp expected actual
 274        '
 275
 276        test_expect_success LIBPCRE "grep $L with grep.patterntype=perl" '
 277                echo "${HC}ab:a+b*c" >expected &&
 278                git -c grep.patterntype=perl grep "a\x{2b}b\x{2a}c" $H ab >actual &&
 279                test_cmp expected actual
 280        '
 281
 282        test_expect_success "grep $L with grep.patternType=default and grep.extendedRegexp=true" '
 283                echo "${HC}ab:abc" >expected &&
 284                git \
 285                        -c grep.patternType=default \
 286                        -c grep.extendedRegexp=true \
 287                        grep "a+b*c" $H ab >actual &&
 288                test_cmp expected actual
 289        '
 290
 291        test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=default" '
 292                echo "${HC}ab:abc" >expected &&
 293                git \
 294                        -c grep.extendedRegexp=true \
 295                        -c grep.patternType=default \
 296                        grep "a+b*c" $H ab >actual &&
 297                test_cmp expected actual
 298        '
 299
 300        test_expect_success "grep $L with grep.patternType=extended and grep.extendedRegexp=false" '
 301                echo "${HC}ab:abc" >expected &&
 302                git \
 303                        -c grep.patternType=extended \
 304                        -c grep.extendedRegexp=false \
 305                        grep "a+b*c" $H ab >actual &&
 306                test_cmp expected actual
 307        '
 308
 309        test_expect_success "grep $L with grep.patternType=basic and grep.extendedRegexp=true" '
 310                echo "${HC}ab:a+bc" >expected &&
 311                git \
 312                        -c grep.patternType=basic \
 313                        -c grep.extendedRegexp=true \
 314                        grep "a+b*c" $H ab >actual &&
 315                test_cmp expected actual
 316        '
 317
 318        test_expect_success "grep $L with grep.extendedRegexp=false and grep.patternType=extended" '
 319                echo "${HC}ab:abc" >expected &&
 320                git \
 321                        -c grep.extendedRegexp=false \
 322                        -c grep.patternType=extended \
 323                        grep "a+b*c" $H ab >actual &&
 324                test_cmp expected actual
 325        '
 326
 327        test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=basic" '
 328                echo "${HC}ab:a+bc" >expected &&
 329                git \
 330                        -c grep.extendedRegexp=true \
 331                        -c grep.patternType=basic \
 332                        grep "a+b*c" $H ab >actual &&
 333                test_cmp expected actual
 334        '
 335
 336        test_expect_success "grep --count $L" '
 337                echo ${HC}ab:3 >expected &&
 338                git grep --count -e b $H -- ab >actual &&
 339                test_cmp expected actual
 340        '
 341
 342        test_expect_success "grep --count -h $L" '
 343                echo 3 >expected &&
 344                git grep --count -h -e b $H -- ab >actual &&
 345                test_cmp expected actual
 346        '
 347done
 348
 349cat >expected <<EOF
 350file
 351EOF
 352test_expect_success 'grep -l -C' '
 353        git grep -l -C1 foo >actual &&
 354        test_cmp expected actual
 355'
 356
 357cat >expected <<EOF
 358file:5
 359EOF
 360test_expect_success 'grep -l -C' '
 361        git grep -c -C1 foo >actual &&
 362        test_cmp expected actual
 363'
 364
 365test_expect_success 'grep -L -C' '
 366        git ls-files >expected &&
 367        git grep -L -C1 nonexistent_string >actual &&
 368        test_cmp expected actual
 369'
 370
 371cat >expected <<EOF
 372file:foo mmap bar_mmap
 373EOF
 374
 375test_expect_success 'grep -e A --and -e B' '
 376        git grep -e "foo mmap" --and -e bar_mmap >actual &&
 377        test_cmp expected actual
 378'
 379
 380cat >expected <<EOF
 381file:foo_mmap bar mmap
 382file:foo_mmap bar mmap baz
 383EOF
 384
 385
 386test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
 387        git grep \( -e foo_ --or -e baz \) \
 388                --and -e " mmap" >actual &&
 389        test_cmp expected actual
 390'
 391
 392cat >expected <<EOF
 393file:foo mmap bar
 394EOF
 395
 396test_expect_success 'grep -e A --and --not -e B' '
 397        git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
 398        test_cmp expected actual
 399'
 400
 401test_expect_success 'grep should ignore GREP_OPTIONS' '
 402        GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
 403        test_cmp expected actual
 404'
 405
 406test_expect_success 'grep -f, non-existent file' '
 407        test_must_fail git grep -f patterns
 408'
 409
 410cat >expected <<EOF
 411file:foo mmap bar
 412file:foo_mmap bar
 413file:foo_mmap bar mmap
 414file:foo mmap bar_mmap
 415file:foo_mmap bar mmap baz
 416EOF
 417
 418cat >pattern <<EOF
 419mmap
 420EOF
 421
 422test_expect_success 'grep -f, one pattern' '
 423        git grep -f pattern >actual &&
 424        test_cmp expected actual
 425'
 426
 427cat >expected <<EOF
 428file:foo mmap bar
 429file:foo_mmap bar
 430file:foo_mmap bar mmap
 431file:foo mmap bar_mmap
 432file:foo_mmap bar mmap baz
 433t/a/v:vvv
 434t/v:vvv
 435v:vvv
 436EOF
 437
 438cat >patterns <<EOF
 439mmap
 440vvv
 441EOF
 442
 443test_expect_success 'grep -f, multiple patterns' '
 444        git grep -f patterns >actual &&
 445        test_cmp expected actual
 446'
 447
 448test_expect_success 'grep, multiple patterns' '
 449        git grep "$(cat patterns)" >actual &&
 450        test_cmp expected actual
 451'
 452
 453cat >expected <<EOF
 454file:foo mmap bar
 455file:foo_mmap bar
 456file:foo_mmap bar mmap
 457file:foo mmap bar_mmap
 458file:foo_mmap bar mmap baz
 459t/a/v:vvv
 460t/v:vvv
 461v:vvv
 462EOF
 463
 464cat >patterns <<EOF
 465
 466mmap
 467
 468vvv
 469
 470EOF
 471
 472test_expect_success 'grep -f, ignore empty lines' '
 473        git grep -f patterns >actual &&
 474        test_cmp expected actual
 475'
 476
 477test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
 478        git grep -f - <patterns >actual &&
 479        test_cmp expected actual
 480'
 481
 482cat >expected <<EOF
 483y:y yy
 484--
 485z:zzz
 486EOF
 487
 488test_expect_success 'grep -q, silently report matches' '
 489        >empty &&
 490        git grep -q mmap >actual &&
 491        test_cmp empty actual &&
 492        test_must_fail git grep -q qfwfq >actual &&
 493        test_cmp empty actual
 494'
 495
 496test_expect_success 'grep -C1 hunk mark between files' '
 497        git grep -C1 "^[yz]" >actual &&
 498        test_cmp expected actual
 499'
 500
 501test_expect_success 'log grep setup' '
 502        echo a >>file &&
 503        test_tick &&
 504        GIT_AUTHOR_NAME="With * Asterisk" \
 505        GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
 506        git commit -a -m "second" &&
 507
 508        echo a >>file &&
 509        test_tick &&
 510        git commit -a -m "third" &&
 511
 512        echo a >>file &&
 513        test_tick &&
 514        GIT_AUTHOR_NAME="Night Fall" \
 515        GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
 516        git commit -a -m "fourth"
 517'
 518
 519test_expect_success 'log grep (1)' '
 520        git log --author=author --pretty=tformat:%s >actual &&
 521        {
 522                echo third && echo initial
 523        } >expect &&
 524        test_cmp expect actual
 525'
 526
 527test_expect_success 'log grep (2)' '
 528        git log --author=" * " -F --pretty=tformat:%s >actual &&
 529        {
 530                echo second
 531        } >expect &&
 532        test_cmp expect actual
 533'
 534
 535test_expect_success 'log grep (3)' '
 536        git log --author="^A U" --pretty=tformat:%s >actual &&
 537        {
 538                echo third && echo initial
 539        } >expect &&
 540        test_cmp expect actual
 541'
 542
 543test_expect_success 'log grep (4)' '
 544        git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
 545        {
 546                echo second
 547        } >expect &&
 548        test_cmp expect actual
 549'
 550
 551test_expect_success 'log grep (5)' '
 552        git log --author=Thor -F --pretty=tformat:%s >actual &&
 553        {
 554                echo third && echo initial
 555        } >expect &&
 556        test_cmp expect actual
 557'
 558
 559test_expect_success 'log grep (6)' '
 560        git log --author=-0700  --pretty=tformat:%s >actual &&
 561        >expect &&
 562        test_cmp expect actual
 563'
 564
 565test_expect_success 'log grep (7)' '
 566        git log -g --grep-reflog="commit: third" --pretty=tformat:%s >actual &&
 567        echo third >expect &&
 568        test_cmp expect actual
 569'
 570
 571test_expect_success 'log grep (8)' '
 572        git log -g --grep-reflog="commit: third" --grep-reflog="commit: second" --pretty=tformat:%s >actual &&
 573        {
 574                echo third && echo second
 575        } >expect &&
 576        test_cmp expect actual
 577'
 578
 579test_expect_success 'log grep (9)' '
 580        git log -g --grep-reflog="commit: third" --author="Thor" --pretty=tformat:%s >actual &&
 581        echo third >expect &&
 582        test_cmp expect actual
 583'
 584
 585test_expect_success 'log grep (9)' '
 586        git log -g --grep-reflog="commit: third" --author="non-existant" --pretty=tformat:%s >actual &&
 587        : >expect &&
 588        test_cmp expect actual
 589'
 590
 591test_expect_success 'log --grep-reflog can only be used under -g' '
 592        test_must_fail git log --grep-reflog="commit: third"
 593'
 594
 595test_expect_success 'log with multiple --grep uses union' '
 596        git log --grep=i --grep=r --format=%s >actual &&
 597        {
 598                echo fourth && echo third && echo initial
 599        } >expect &&
 600        test_cmp expect actual
 601'
 602
 603test_expect_success 'log --all-match with multiple --grep uses intersection' '
 604        git log --all-match --grep=i --grep=r --format=%s >actual &&
 605        {
 606                echo third
 607        } >expect &&
 608        test_cmp expect actual
 609'
 610
 611test_expect_success 'log with multiple --author uses union' '
 612        git log --author="Thor" --author="Aster" --format=%s >actual &&
 613        {
 614            echo third && echo second && echo initial
 615        } >expect &&
 616        test_cmp expect actual
 617'
 618
 619test_expect_success 'log --all-match with multiple --author still uses union' '
 620        git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
 621        {
 622            echo third && echo second && echo initial
 623        } >expect &&
 624        test_cmp expect actual
 625'
 626
 627test_expect_success 'log --grep --author uses intersection' '
 628        # grep matches only third and fourth
 629        # author matches only initial and third
 630        git log --author="A U Thor" --grep=r --format=%s >actual &&
 631        {
 632                echo third
 633        } >expect &&
 634        test_cmp expect actual
 635'
 636
 637test_expect_success 'log --grep --grep --author takes union of greps and intersects with author' '
 638        # grep matches initial and second but not third
 639        # author matches only initial and third
 640        git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
 641        {
 642                echo initial
 643        } >expect &&
 644        test_cmp expect actual
 645'
 646
 647test_expect_success 'log ---all-match -grep --author --author still takes union of authors and intersects with grep' '
 648        # grep matches only initial and third
 649        # author matches all but second
 650        git log --all-match --author="Thor" --author="Night" --grep=i --format=%s >actual &&
 651        {
 652            echo third && echo initial
 653        } >expect &&
 654        test_cmp expect actual
 655'
 656
 657test_expect_success 'log --grep --author --author takes union of authors and intersects with grep' '
 658        # grep matches only initial and third
 659        # author matches all but second
 660        git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
 661        {
 662            echo third && echo initial
 663        } >expect &&
 664        test_cmp expect actual
 665'
 666
 667test_expect_success 'log --all-match --grep --grep --author takes intersection' '
 668        # grep matches only third
 669        # author matches only initial and third
 670        git log --all-match --author="A U Thor" --grep=i --grep=r --format=%s >actual &&
 671        {
 672                echo third
 673        } >expect &&
 674        test_cmp expect actual
 675'
 676
 677test_expect_success 'log --author does not search in timestamp' '
 678        : >expect &&
 679        git log --author="$GIT_AUTHOR_DATE" >actual &&
 680        test_cmp expect actual
 681'
 682
 683test_expect_success 'log --committer does not search in timestamp' '
 684        : >expect &&
 685        git log --committer="$GIT_COMMITTER_DATE" >actual &&
 686        test_cmp expect actual
 687'
 688
 689test_expect_success 'grep with CE_VALID file' '
 690        git update-index --assume-unchanged t/t &&
 691        rm t/t &&
 692        test "$(git grep test)" = "t/t:test" &&
 693        git update-index --no-assume-unchanged t/t &&
 694        git checkout t/t
 695'
 696
 697cat >expected <<EOF
 698hello.c=#include <stdio.h>
 699hello.c:        return 0;
 700EOF
 701
 702test_expect_success 'grep -p with userdiff' '
 703        git config diff.custom.funcname "^#" &&
 704        echo "hello.c diff=custom" >.gitattributes &&
 705        git grep -p return >actual &&
 706        test_cmp expected actual
 707'
 708
 709cat >expected <<EOF
 710hello.c=int main(int argc, const char **argv)
 711hello.c:        return 0;
 712EOF
 713
 714test_expect_success 'grep -p' '
 715        rm -f .gitattributes &&
 716        git grep -p return >actual &&
 717        test_cmp expected actual
 718'
 719
 720cat >expected <<EOF
 721hello.c-#include <stdio.h>
 722hello.c=int main(int argc, const char **argv)
 723hello.c-{
 724hello.c-        printf("Hello world.\n");
 725hello.c:        return 0;
 726EOF
 727
 728test_expect_success 'grep -p -B5' '
 729        git grep -p -B5 return >actual &&
 730        test_cmp expected actual
 731'
 732
 733cat >expected <<EOF
 734hello.c=int main(int argc, const char **argv)
 735hello.c-{
 736hello.c-        printf("Hello world.\n");
 737hello.c:        return 0;
 738hello.c-        /* char ?? */
 739hello.c-}
 740EOF
 741
 742test_expect_success 'grep -W' '
 743        git grep -W return >actual &&
 744        test_cmp expected actual
 745'
 746
 747cat >expected <<EOF
 748hello.c=        printf("Hello world.\n");
 749hello.c:        return 0;
 750hello.c-        /* char ?? */
 751EOF
 752
 753test_expect_success 'grep -W with userdiff' '
 754        test_when_finished "rm -f .gitattributes" &&
 755        git config diff.custom.xfuncname "(printf.*|})$" &&
 756        echo "hello.c diff=custom" >.gitattributes &&
 757        git grep -W return >actual &&
 758        test_cmp expected actual
 759'
 760
 761test_expect_success 'grep from a subdirectory to search wider area (1)' '
 762        mkdir -p s &&
 763        (
 764                cd s && git grep "x x x" ..
 765        )
 766'
 767
 768test_expect_success 'grep from a subdirectory to search wider area (2)' '
 769        mkdir -p s &&
 770        (
 771                cd s || exit 1
 772                ( git grep xxyyzz .. >out ; echo $? >status )
 773                ! test -s out &&
 774                test 1 = $(cat status)
 775        )
 776'
 777
 778cat >expected <<EOF
 779hello.c:int main(int argc, const char **argv)
 780EOF
 781
 782test_expect_success 'grep -Fi' '
 783        git grep -Fi "CHAR *" >actual &&
 784        test_cmp expected actual
 785'
 786
 787test_expect_success 'outside of git repository' '
 788        rm -fr non &&
 789        mkdir -p non/git/sub &&
 790        echo hello >non/git/file1 &&
 791        echo world >non/git/sub/file2 &&
 792        {
 793                echo file1:hello &&
 794                echo sub/file2:world
 795        } >non/expect.full &&
 796        echo file2:world >non/expect.sub &&
 797        (
 798                GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
 799                export GIT_CEILING_DIRECTORIES &&
 800                cd non/git &&
 801                test_must_fail git grep o &&
 802                git grep --no-index o >../actual.full &&
 803                test_cmp ../expect.full ../actual.full &&
 804                cd sub &&
 805                test_must_fail git grep o &&
 806                git grep --no-index o >../../actual.sub &&
 807                test_cmp ../../expect.sub ../../actual.sub
 808        ) &&
 809
 810        echo ".*o*" >non/git/.gitignore &&
 811        (
 812                GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
 813                export GIT_CEILING_DIRECTORIES &&
 814                cd non/git &&
 815                test_must_fail git grep o &&
 816                git grep --no-index --exclude-standard o >../actual.full &&
 817                test_cmp ../expect.full ../actual.full &&
 818
 819                {
 820                        echo ".gitignore:.*o*" &&
 821                        cat ../expect.full
 822                } >../expect.with.ignored &&
 823                git grep --no-index --no-exclude o >../actual.full &&
 824                test_cmp ../expect.with.ignored ../actual.full
 825        )
 826'
 827
 828test_expect_success 'outside of git repository with fallbackToNoIndex' '
 829        rm -fr non &&
 830        mkdir -p non/git/sub &&
 831        echo hello >non/git/file1 &&
 832        echo world >non/git/sub/file2 &&
 833        cat <<-\EOF >non/expect.full &&
 834        file1:hello
 835        sub/file2:world
 836        EOF
 837        echo file2:world >non/expect.sub &&
 838        (
 839                GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
 840                export GIT_CEILING_DIRECTORIES &&
 841                cd non/git &&
 842                test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
 843                git -c grep.fallbackToNoIndex=true grep o >../actual.full &&
 844                test_cmp ../expect.full ../actual.full &&
 845                cd sub &&
 846                test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
 847                git -c grep.fallbackToNoIndex=true grep o >../../actual.sub &&
 848                test_cmp ../../expect.sub ../../actual.sub
 849        ) &&
 850
 851        echo ".*o*" >non/git/.gitignore &&
 852        (
 853                GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
 854                export GIT_CEILING_DIRECTORIES &&
 855                cd non/git &&
 856                test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
 857                git -c grep.fallbackToNoIndex=true grep --exclude-standard o >../actual.full &&
 858                test_cmp ../expect.full ../actual.full &&
 859
 860                {
 861                        echo ".gitignore:.*o*" &&
 862                        cat ../expect.full
 863                } >../expect.with.ignored &&
 864                git -c grep.fallbackToNoIndex grep --no-exclude o >../actual.full &&
 865                test_cmp ../expect.with.ignored ../actual.full
 866        )
 867'
 868
 869test_expect_success 'inside git repository but with --no-index' '
 870        rm -fr is &&
 871        mkdir -p is/git/sub &&
 872        echo hello >is/git/file1 &&
 873        echo world >is/git/sub/file2 &&
 874        echo ".*o*" >is/git/.gitignore &&
 875        {
 876                echo file1:hello &&
 877                echo sub/file2:world
 878        } >is/expect.unignored &&
 879        {
 880                echo ".gitignore:.*o*" &&
 881                cat is/expect.unignored
 882        } >is/expect.full &&
 883        : >is/expect.empty &&
 884        echo file2:world >is/expect.sub &&
 885        (
 886                cd is/git &&
 887                git init &&
 888                test_must_fail git grep o >../actual.full &&
 889                test_cmp ../expect.empty ../actual.full &&
 890
 891                git grep --untracked o >../actual.unignored &&
 892                test_cmp ../expect.unignored ../actual.unignored &&
 893
 894                git grep --no-index o >../actual.full &&
 895                test_cmp ../expect.full ../actual.full &&
 896
 897                git grep --no-index --exclude-standard o >../actual.unignored &&
 898                test_cmp ../expect.unignored ../actual.unignored &&
 899
 900                cd sub &&
 901                test_must_fail git grep o >../../actual.sub &&
 902                test_cmp ../../expect.empty ../../actual.sub &&
 903
 904                git grep --no-index o >../../actual.sub &&
 905                test_cmp ../../expect.sub ../../actual.sub &&
 906
 907                git grep --untracked o >../../actual.sub &&
 908                test_cmp ../../expect.sub ../../actual.sub
 909        )
 910'
 911
 912test_expect_success 'grep --no-index descends into repos, but not .git' '
 913        rm -fr non &&
 914        mkdir -p non/git &&
 915        (
 916                GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
 917                export GIT_CEILING_DIRECTORIES &&
 918                cd non/git &&
 919
 920                echo magic >file &&
 921                git init repo &&
 922                (
 923                        cd repo &&
 924                        echo magic >file &&
 925                        git add file &&
 926                        git commit -m foo &&
 927                        echo magic >.git/file
 928                ) &&
 929
 930                cat >expect <<-\EOF &&
 931                file
 932                repo/file
 933                EOF
 934                git grep -l --no-index magic >actual &&
 935                test_cmp expect actual
 936        )
 937'
 938
 939test_expect_success 'setup double-dash tests' '
 940cat >double-dash <<EOF &&
 941--
 942->
 943other
 944EOF
 945git add double-dash
 946'
 947
 948cat >expected <<EOF
 949double-dash:->
 950EOF
 951test_expect_success 'grep -- pattern' '
 952        git grep -- "->" >actual &&
 953        test_cmp expected actual
 954'
 955test_expect_success 'grep -- pattern -- pathspec' '
 956        git grep -- "->" -- double-dash >actual &&
 957        test_cmp expected actual
 958'
 959test_expect_success 'grep -e pattern -- path' '
 960        git grep -e "->" -- double-dash >actual &&
 961        test_cmp expected actual
 962'
 963
 964cat >expected <<EOF
 965double-dash:--
 966EOF
 967test_expect_success 'grep -e -- -- path' '
 968        git grep -e -- -- double-dash >actual &&
 969        test_cmp expected actual
 970'
 971
 972cat >expected <<EOF
 973hello.c:int main(int argc, const char **argv)
 974hello.c:        printf("Hello world.\n");
 975EOF
 976
 977test_expect_success LIBPCRE 'grep --perl-regexp pattern' '
 978        git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
 979        test_cmp expected actual
 980'
 981
 982test_expect_success LIBPCRE 'grep -P pattern' '
 983        git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
 984        test_cmp expected actual
 985'
 986
 987test_expect_success 'grep pattern with grep.extendedRegexp=true' '
 988        >empty &&
 989        test_must_fail git -c grep.extendedregexp=true \
 990                grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
 991        test_cmp empty actual
 992'
 993
 994test_expect_success LIBPCRE 'grep -P pattern with grep.extendedRegexp=true' '
 995        git -c grep.extendedregexp=true \
 996                grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
 997        test_cmp expected actual
 998'
 999
1000test_expect_success LIBPCRE 'grep -P -v pattern' '
1001        {
1002                echo "ab:a+b*c"
1003                echo "ab:a+bc"
1004        } >expected &&
1005        git grep -P -v "abc" ab >actual &&
1006        test_cmp expected actual
1007'
1008
1009test_expect_success LIBPCRE 'grep -P -i pattern' '
1010        cat >expected <<-EOF &&
1011        hello.c:        printf("Hello world.\n");
1012        EOF
1013        git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
1014        test_cmp expected actual
1015'
1016
1017test_expect_success LIBPCRE 'grep -P -w pattern' '
1018        {
1019                echo "hello_world:Hello world"
1020                echo "hello_world:HeLLo world"
1021        } >expected &&
1022        git grep -P -w "He((?i)ll)o" hello_world >actual &&
1023        test_cmp expected actual
1024'
1025
1026test_expect_success 'grep -G invalidpattern properly dies ' '
1027        test_must_fail git grep -G "a["
1028'
1029
1030test_expect_success 'grep invalidpattern properly dies with grep.patternType=basic' '
1031        test_must_fail git -c grep.patterntype=basic grep "a["
1032'
1033
1034test_expect_success 'grep -E invalidpattern properly dies ' '
1035        test_must_fail git grep -E "a["
1036'
1037
1038test_expect_success 'grep invalidpattern properly dies with grep.patternType=extended' '
1039        test_must_fail git -c grep.patterntype=extended grep "a["
1040'
1041
1042test_expect_success LIBPCRE 'grep -P invalidpattern properly dies ' '
1043        test_must_fail git grep -P "a["
1044'
1045
1046test_expect_success LIBPCRE 'grep invalidpattern properly dies with grep.patternType=perl' '
1047        test_must_fail git -c grep.patterntype=perl grep "a["
1048'
1049
1050test_expect_success 'grep -G -E -F pattern' '
1051        echo "ab:a+b*c" >expected &&
1052        git grep -G -E -F "a+b*c" ab >actual &&
1053        test_cmp expected actual
1054'
1055
1056test_expect_success 'grep pattern with grep.patternType=basic, =extended, =fixed' '
1057        echo "ab:a+b*c" >expected &&
1058        git \
1059                -c grep.patterntype=basic \
1060                -c grep.patterntype=extended \
1061                -c grep.patterntype=fixed \
1062                grep "a+b*c" ab >actual &&
1063        test_cmp expected actual
1064'
1065
1066test_expect_success 'grep -E -F -G pattern' '
1067        echo "ab:a+bc" >expected &&
1068        git grep -E -F -G "a+b*c" ab >actual &&
1069        test_cmp expected actual
1070'
1071
1072test_expect_success 'grep pattern with grep.patternType=extended, =fixed, =basic' '
1073        echo "ab:a+bc" >expected &&
1074        git \
1075                -c grep.patterntype=extended \
1076                -c grep.patterntype=fixed \
1077                -c grep.patterntype=basic \
1078                grep "a+b*c" ab >actual &&
1079        test_cmp expected actual
1080'
1081
1082test_expect_success 'grep -F -G -E pattern' '
1083        echo "ab:abc" >expected &&
1084        git grep -F -G -E "a+b*c" ab >actual &&
1085        test_cmp expected actual
1086'
1087
1088test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended' '
1089        echo "ab:abc" >expected &&
1090        git \
1091                -c grep.patterntype=fixed \
1092                -c grep.patterntype=basic \
1093                -c grep.patterntype=extended \
1094                grep "a+b*c" ab >actual &&
1095        test_cmp expected actual
1096'
1097
1098test_expect_success 'grep -G -F -P -E pattern' '
1099        echo "d0:d" >expected &&
1100        git grep -G -F -P -E "[\d]" d0 >actual &&
1101        test_cmp expected actual
1102'
1103
1104test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
1105        echo "d0:d" >expected &&
1106        git \
1107                -c grep.patterntype=fixed \
1108                -c grep.patterntype=basic \
1109                -c grep.patterntype=perl \
1110                -c grep.patterntype=extended \
1111                grep "[\d]" d0 >actual &&
1112        test_cmp expected actual
1113'
1114
1115test_expect_success LIBPCRE 'grep -G -F -E -P pattern' '
1116        echo "d0:0" >expected &&
1117        git grep -G -F -E -P "[\d]" d0 >actual &&
1118        test_cmp expected actual
1119'
1120
1121test_expect_success LIBPCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1122        echo "d0:0" >expected &&
1123        git \
1124                -c grep.patterntype=fixed \
1125                -c grep.patterntype=basic \
1126                -c grep.patterntype=extended \
1127                -c grep.patterntype=perl \
1128                grep "[\d]" d0 >actual &&
1129        test_cmp expected actual
1130'
1131
1132test_expect_success LIBPCRE 'grep -P pattern with grep.patternType=fixed' '
1133        echo "ab:a+b*c" >expected &&
1134        git \
1135                -c grep.patterntype=fixed \
1136                grep -P "a\x{2b}b\x{2a}c" ab >actual &&
1137        test_cmp expected actual
1138'
1139
1140test_expect_success 'grep -F pattern with grep.patternType=basic' '
1141        echo "ab:a+b*c" >expected &&
1142        git \
1143                -c grep.patterntype=basic \
1144                grep -F "*c" ab >actual &&
1145        test_cmp expected actual
1146'
1147
1148test_expect_success 'grep -G pattern with grep.patternType=fixed' '
1149        {
1150                echo "ab:a+b*c"
1151                echo "ab:a+bc"
1152        } >expected &&
1153        git \
1154                -c grep.patterntype=fixed \
1155                grep -G "a+b" ab >actual &&
1156        test_cmp expected actual
1157'
1158
1159test_expect_success 'grep -E pattern with grep.patternType=fixed' '
1160        {
1161                echo "ab:a+b*c"
1162                echo "ab:a+bc"
1163                echo "ab:abc"
1164        } >expected &&
1165        git \
1166                -c grep.patterntype=fixed \
1167                grep -E "a+" ab >actual &&
1168        test_cmp expected actual
1169'
1170
1171cat >expected <<EOF
1172hello.c<RED>:<RESET>int main(int argc, const char **argv)
1173hello.c<RED>-<RESET>{
1174<RED>--<RESET>
1175hello.c<RED>:<RESET>    /* char ?? */
1176hello.c<RED>-<RESET>}
1177<RED>--<RESET>
1178hello_world<RED>:<RESET>Hello_world
1179hello_world<RED>-<RESET>HeLLo_world
1180EOF
1181
1182test_expect_success 'grep --color, separator' '
1183        test_config color.grep.context          normal &&
1184        test_config color.grep.filename         normal &&
1185        test_config color.grep.function         normal &&
1186        test_config color.grep.linenumber       normal &&
1187        test_config color.grep.match            normal &&
1188        test_config color.grep.selected         normal &&
1189        test_config color.grep.separator        red &&
1190
1191        git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
1192        test_decode_color >actual &&
1193        test_cmp expected actual
1194'
1195
1196cat >expected <<EOF
1197hello.c:int main(int argc, const char **argv)
1198hello.c:        /* char ?? */
1199
1200hello_world:Hello_world
1201EOF
1202
1203test_expect_success 'grep --break' '
1204        git grep --break -e char -e lo_w hello.c hello_world >actual &&
1205        test_cmp expected actual
1206'
1207
1208cat >expected <<EOF
1209hello.c:int main(int argc, const char **argv)
1210hello.c-{
1211--
1212hello.c:        /* char ?? */
1213hello.c-}
1214
1215hello_world:Hello_world
1216hello_world-HeLLo_world
1217EOF
1218
1219test_expect_success 'grep --break with context' '
1220        git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
1221        test_cmp expected actual
1222'
1223
1224cat >expected <<EOF
1225hello.c
1226int main(int argc, const char **argv)
1227        /* char ?? */
1228hello_world
1229Hello_world
1230EOF
1231
1232test_expect_success 'grep --heading' '
1233        git grep --heading -e char -e lo_w hello.c hello_world >actual &&
1234        test_cmp expected actual
1235'
1236
1237cat >expected <<EOF
1238<BOLD;GREEN>hello.c<RESET>
12392:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
12406:      /* <BLACK;BYELLOW>char<RESET> ?? */
1241
1242<BOLD;GREEN>hello_world<RESET>
12433:Hel<BLACK;BYELLOW>lo_w<RESET>orld
1244EOF
1245
1246test_expect_success 'mimic ack-grep --group' '
1247        test_config color.grep.context          normal &&
1248        test_config color.grep.filename         "bold green" &&
1249        test_config color.grep.function         normal &&
1250        test_config color.grep.linenumber       normal &&
1251        test_config color.grep.match            "black yellow" &&
1252        test_config color.grep.selected         normal &&
1253        test_config color.grep.separator        normal &&
1254
1255        git grep --break --heading -n --color \
1256                -e char -e lo_w hello.c hello_world |
1257        test_decode_color >actual &&
1258        test_cmp expected actual
1259'
1260
1261cat >expected <<EOF
1262space: line with leading space1
1263space: line with leading space2
1264space: line with leading space3
1265EOF
1266
1267test_expect_success LIBPCRE 'grep -E "^ "' '
1268        git grep -E "^ " space >actual &&
1269        test_cmp expected actual
1270'
1271
1272test_expect_success LIBPCRE 'grep -P "^ "' '
1273        git grep -P "^ " space >actual &&
1274        test_cmp expected actual
1275'
1276
1277cat >expected <<EOF
1278space-line without leading space1
1279space: line <RED>with <RESET>leading space1
1280space: line <RED>with <RESET>leading <RED>space2<RESET>
1281space: line <RED>with <RESET>leading space3
1282space:line without leading <RED>space2<RESET>
1283EOF
1284
1285test_expect_success 'grep --color -e A -e B with context' '
1286        test_config color.grep.context          normal &&
1287        test_config color.grep.filename         normal &&
1288        test_config color.grep.function         normal &&
1289        test_config color.grep.linenumber       normal &&
1290        test_config color.grep.matchContext     normal &&
1291        test_config color.grep.matchSelected    red &&
1292        test_config color.grep.selected         normal &&
1293        test_config color.grep.separator        normal &&
1294
1295        git grep --color=always -C2 -e "with " -e space2  space |
1296        test_decode_color >actual &&
1297        test_cmp expected actual
1298'
1299
1300cat >expected <<EOF
1301space-line without leading space1
1302space- line with leading space1
1303space: line <RED>with <RESET>leading <RED>space2<RESET>
1304space- line with leading space3
1305space-line without leading space2
1306EOF
1307
1308test_expect_success 'grep --color -e A --and -e B with context' '
1309        test_config color.grep.context          normal &&
1310        test_config color.grep.filename         normal &&
1311        test_config color.grep.function         normal &&
1312        test_config color.grep.linenumber       normal &&
1313        test_config color.grep.matchContext     normal &&
1314        test_config color.grep.matchSelected    red &&
1315        test_config color.grep.selected         normal &&
1316        test_config color.grep.separator        normal &&
1317
1318        git grep --color=always -C2 -e "with " --and -e space2  space |
1319        test_decode_color >actual &&
1320        test_cmp expected actual
1321'
1322
1323cat >expected <<EOF
1324space-line without leading space1
1325space: line <RED>with <RESET>leading space1
1326space- line with leading space2
1327space: line <RED>with <RESET>leading space3
1328space-line without leading space2
1329EOF
1330
1331test_expect_success 'grep --color -e A --and --not -e B with context' '
1332        test_config color.grep.context          normal &&
1333        test_config color.grep.filename         normal &&
1334        test_config color.grep.function         normal &&
1335        test_config color.grep.linenumber       normal &&
1336        test_config color.grep.matchContext     normal &&
1337        test_config color.grep.matchSelected    red &&
1338        test_config color.grep.selected         normal &&
1339        test_config color.grep.separator        normal &&
1340
1341        git grep --color=always -C2 -e "with " --and --not -e space2  space |
1342        test_decode_color >actual &&
1343        test_cmp expected actual
1344'
1345
1346cat >expected <<EOF
1347hello.c-#include <stdio.h>
1348hello.c=int main(int argc, const char **argv)
1349hello.c-{
1350hello.c:        pr<RED>int<RESET>f("<RED>Hello<RESET> world.\n");
1351hello.c-        return 0;
1352hello.c-        /* char ?? */
1353hello.c-}
1354EOF
1355
1356test_expect_success 'grep --color -e A --and -e B -p with context' '
1357        test_config color.grep.context          normal &&
1358        test_config color.grep.filename         normal &&
1359        test_config color.grep.function         normal &&
1360        test_config color.grep.linenumber       normal &&
1361        test_config color.grep.matchContext     normal &&
1362        test_config color.grep.matchSelected    red &&
1363        test_config color.grep.selected         normal &&
1364        test_config color.grep.separator        normal &&
1365
1366        git grep --color=always -p -C3 -e int --and -e Hello --no-index hello.c |
1367        test_decode_color >actual &&
1368        test_cmp expected actual
1369'
1370
1371test_done