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