t / t7810-grep.shon commit grep.c: teach 'git grep --only-matching' (9d8db06)
   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 <assert.h>
  13#include <stdio.h>
  14
  15int main(int argc, const char **argv)
  16{
  17        printf("Hello world.\n");
  18        return 0;
  19        /* char ?? */
  20}
  21EOF
  22
  23test_expect_success setup '
  24        {
  25                echo foo mmap bar
  26                echo foo_mmap bar
  27                echo foo_mmap bar mmap
  28                echo foo mmap bar_mmap
  29                echo foo_mmap bar mmap baz
  30        } >file &&
  31        {
  32                echo Hello world
  33                echo HeLLo world
  34                echo Hello_world
  35                echo HeLLo_world
  36        } >hello_world &&
  37        {
  38                echo "a+b*c"
  39                echo "a+bc"
  40                echo "abc"
  41        } >ab &&
  42        {
  43                echo d &&
  44                echo 0
  45        } >d0 &&
  46        echo vvv >v &&
  47        echo ww w >w &&
  48        echo x x xx x >x &&
  49        echo y yy >y &&
  50        echo zzz > z &&
  51        mkdir t &&
  52        echo test >t/t &&
  53        echo vvv >t/v &&
  54        mkdir t/a &&
  55        echo vvv >t/a/v &&
  56        {
  57                echo "line without leading space1"
  58                echo " line with leading space1"
  59                echo " line with leading space2"
  60                echo " line with leading space3"
  61                echo "line without leading space2"
  62        } >space &&
  63        cat >hello.ps1 <<-\EOF &&
  64        # No-op.
  65        function dummy() {}
  66
  67        # Say hello.
  68        function hello() {
  69          echo "Hello world."
  70        } # hello
  71
  72        # Still a no-op.
  73        function dummy() {}
  74        EOF
  75        git add . &&
  76        test_tick &&
  77        git commit -m initial
  78'
  79
  80test_expect_success 'grep should not segfault with a bad input' '
  81        test_must_fail git grep "("
  82'
  83
  84for H in HEAD ''
  85do
  86        case "$H" in
  87        HEAD)   HC='HEAD:' L='HEAD' ;;
  88        '')     HC= L='in working tree' ;;
  89        esac
  90
  91        test_expect_success "grep -w $L" '
  92                {
  93                        echo ${HC}file:1:foo mmap bar
  94                        echo ${HC}file:3:foo_mmap bar mmap
  95                        echo ${HC}file:4:foo mmap bar_mmap
  96                        echo ${HC}file:5:foo_mmap bar mmap baz
  97                } >expected &&
  98                git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
  99                test_cmp expected actual
 100        '
 101
 102        test_expect_success "grep -w $L (with --column)" '
 103                {
 104                        echo ${HC}file:5:foo mmap bar
 105                        echo ${HC}file:14:foo_mmap bar mmap
 106                        echo ${HC}file:5:foo mmap bar_mmap
 107                        echo ${HC}file:14:foo_mmap bar mmap baz
 108                } >expected &&
 109                git grep --column -w -e mmap $H >actual &&
 110                test_cmp expected actual
 111        '
 112
 113        test_expect_success "grep -w $L (with --column, extended OR)" '
 114                {
 115                        echo ${HC}file:14:foo_mmap bar mmap
 116                        echo ${HC}file:19:foo_mmap bar mmap baz
 117                } >expected &&
 118                git grep --column -w -e mmap$ --or -e baz $H >actual &&
 119                test_cmp expected actual
 120        '
 121
 122        test_expect_success "grep -w $L (with --column, --invert)" '
 123                {
 124                        echo ${HC}file:1:foo mmap bar
 125                        echo ${HC}file:1:foo_mmap bar
 126                        echo ${HC}file:1:foo_mmap bar mmap
 127                        echo ${HC}file:1:foo mmap bar_mmap
 128                } >expected &&
 129                git grep --column --invert -w -e baz $H -- file >actual &&
 130                test_cmp expected actual
 131        '
 132
 133        test_expect_success "grep $L (with --column, --invert, extended OR)" '
 134                {
 135                        echo ${HC}hello_world:6:HeLLo_world
 136                } >expected &&
 137                git grep --column --invert -e ll --or --not -e _ $H -- hello_world \
 138                        >actual &&
 139                test_cmp expected actual
 140        '
 141
 142        test_expect_success "grep $L (with --column, --invert, extended AND)" '
 143                {
 144                        echo ${HC}hello_world:3:Hello world
 145                        echo ${HC}hello_world:3:Hello_world
 146                        echo ${HC}hello_world:6:HeLLo_world
 147                } >expected &&
 148                git grep --column --invert --not -e _ --and --not -e ll $H -- hello_world \
 149                        >actual &&
 150                test_cmp expected actual
 151        '
 152
 153        test_expect_success "grep $L (with --column, double-negation)" '
 154                {
 155                        echo ${HC}file:1:foo_mmap bar mmap baz
 156                } >expected &&
 157                git grep --column --not \( --not -e foo --or --not -e baz \) $H -- file \
 158                        >actual &&
 159                test_cmp expected actual
 160        '
 161
 162        test_expect_success "grep -w $L (with --column, -C)" '
 163                {
 164                        echo ${HC}file:5:foo mmap bar
 165                        echo ${HC}file-foo_mmap bar
 166                        echo ${HC}file:14:foo_mmap bar mmap
 167                        echo ${HC}file:5:foo mmap bar_mmap
 168                        echo ${HC}file:14:foo_mmap bar mmap baz
 169                } >expected &&
 170                git grep --column -w -C1 -e mmap $H >actual &&
 171                test_cmp expected actual
 172        '
 173
 174        test_expect_success "grep -w $L (with --line-number, --column)" '
 175                {
 176                        echo ${HC}file:1:5:foo mmap bar
 177                        echo ${HC}file:3:14:foo_mmap bar mmap
 178                        echo ${HC}file:4:5:foo mmap bar_mmap
 179                        echo ${HC}file:5:14:foo_mmap bar mmap baz
 180                } >expected &&
 181                git grep -n --column -w -e mmap $H >actual &&
 182                test_cmp expected actual
 183        '
 184
 185        test_expect_success "grep -w $L (with non-extended patterns, --column)" '
 186                {
 187                        echo ${HC}file:5:foo mmap bar
 188                        echo ${HC}file:10:foo_mmap bar
 189                        echo ${HC}file:10:foo_mmap bar mmap
 190                        echo ${HC}file:5:foo mmap bar_mmap
 191                        echo ${HC}file:10:foo_mmap bar mmap baz
 192                } >expected &&
 193                git grep --column -w -e bar -e mmap $H >actual &&
 194                test_cmp expected actual
 195        '
 196
 197        test_expect_success "grep -w $L" '
 198                {
 199                        echo ${HC}file:1:foo mmap bar
 200                        echo ${HC}file:3:foo_mmap bar mmap
 201                        echo ${HC}file:4:foo mmap bar_mmap
 202                        echo ${HC}file:5:foo_mmap bar mmap baz
 203                } >expected &&
 204                git -c grep.linenumber=true grep -w -e mmap $H >actual &&
 205                test_cmp expected actual
 206        '
 207
 208        test_expect_success "grep -w $L" '
 209                {
 210                        echo ${HC}file:foo mmap bar
 211                        echo ${HC}file:foo_mmap bar mmap
 212                        echo ${HC}file:foo mmap bar_mmap
 213                        echo ${HC}file:foo_mmap bar mmap baz
 214                } >expected &&
 215                git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
 216                test_cmp expected actual
 217        '
 218
 219        test_expect_success "grep -w $L (w)" '
 220                : >expected &&
 221                test_must_fail git grep -n -w -e "^w" $H >actual &&
 222                test_cmp expected actual
 223        '
 224
 225        test_expect_success "grep -w $L (x)" '
 226                {
 227                        echo ${HC}x:1:x x xx x
 228                } >expected &&
 229                git grep -n -w -e "x xx* x" $H >actual &&
 230                test_cmp expected actual
 231        '
 232
 233        test_expect_success "grep -w $L (y-1)" '
 234                {
 235                        echo ${HC}y:1:y yy
 236                } >expected &&
 237                git grep -n -w -e "^y" $H >actual &&
 238                test_cmp expected actual
 239        '
 240
 241        test_expect_success "grep -w $L (y-2)" '
 242                : >expected &&
 243                if git grep -n -w -e "^y y" $H >actual
 244                then
 245                        echo should not have matched
 246                        cat actual
 247                        false
 248                else
 249                        test_cmp expected actual
 250                fi
 251        '
 252
 253        test_expect_success "grep -w $L (z)" '
 254                : >expected &&
 255                if git grep -n -w -e "^z" $H >actual
 256                then
 257                        echo should not have matched
 258                        cat actual
 259                        false
 260                else
 261                        test_cmp expected actual
 262                fi
 263        '
 264
 265        test_expect_success "grep $L (with --column, --only-matching)" '
 266                {
 267                        echo ${HC}file:1:5:mmap
 268                        echo ${HC}file:2:5:mmap
 269                        echo ${HC}file:3:5:mmap
 270                        echo ${HC}file:3:13:mmap
 271                        echo ${HC}file:4:5:mmap
 272                        echo ${HC}file:4:13:mmap
 273                        echo ${HC}file:5:5:mmap
 274                        echo ${HC}file:5:13:mmap
 275                } >expected &&
 276                git grep --column -n -o -e mmap $H >actual &&
 277                test_cmp expected actual
 278        '
 279
 280        test_expect_success "grep $L (t-1)" '
 281                echo "${HC}t/t:1:test" >expected &&
 282                git grep -n -e test $H >actual &&
 283                test_cmp expected actual
 284        '
 285
 286        test_expect_success "grep $L (t-2)" '
 287                echo "${HC}t:1:test" >expected &&
 288                (
 289                        cd t &&
 290                        git grep -n -e test $H
 291                ) >actual &&
 292                test_cmp expected actual
 293        '
 294
 295        test_expect_success "grep $L (t-3)" '
 296                echo "${HC}t/t:1:test" >expected &&
 297                (
 298                        cd t &&
 299                        git grep --full-name -n -e test $H
 300                ) >actual &&
 301                test_cmp expected actual
 302        '
 303
 304        test_expect_success "grep -c $L (no /dev/null)" '
 305                ! git grep -c test $H | grep /dev/null
 306        '
 307
 308        test_expect_success "grep --max-depth -1 $L" '
 309                {
 310                        echo ${HC}t/a/v:1:vvv
 311                        echo ${HC}t/v:1:vvv
 312                        echo ${HC}v:1:vvv
 313                } >expected &&
 314                git grep --max-depth -1 -n -e vvv $H >actual &&
 315                test_cmp expected actual
 316        '
 317
 318        test_expect_success "grep --max-depth 0 $L" '
 319                {
 320                        echo ${HC}v:1:vvv
 321                } >expected &&
 322                git grep --max-depth 0 -n -e vvv $H >actual &&
 323                test_cmp expected actual
 324        '
 325
 326        test_expect_success "grep --max-depth 0 -- '*' $L" '
 327                {
 328                        echo ${HC}t/a/v:1:vvv
 329                        echo ${HC}t/v:1:vvv
 330                        echo ${HC}v:1:vvv
 331                } >expected &&
 332                git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
 333                test_cmp expected actual
 334        '
 335
 336        test_expect_success "grep --max-depth 1 $L" '
 337                {
 338                        echo ${HC}t/v:1:vvv
 339                        echo ${HC}v:1:vvv
 340                } >expected &&
 341                git grep --max-depth 1 -n -e vvv $H >actual &&
 342                test_cmp expected actual
 343        '
 344
 345        test_expect_success "grep --max-depth 0 -- t $L" '
 346                {
 347                        echo ${HC}t/v:1:vvv
 348                } >expected &&
 349                git grep --max-depth 0 -n -e vvv $H -- t >actual &&
 350                test_cmp expected actual
 351        '
 352
 353        test_expect_success "grep --max-depth 0 -- . t $L" '
 354                {
 355                        echo ${HC}t/v:1:vvv
 356                        echo ${HC}v:1:vvv
 357                } >expected &&
 358                git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
 359                test_cmp expected actual
 360        '
 361
 362        test_expect_success "grep --max-depth 0 -- t . $L" '
 363                {
 364                        echo ${HC}t/v:1:vvv
 365                        echo ${HC}v:1:vvv
 366                } >expected &&
 367                git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
 368                test_cmp expected actual
 369        '
 370        test_expect_success "grep $L with grep.extendedRegexp=false" '
 371                echo "${HC}ab:a+bc" >expected &&
 372                git -c grep.extendedRegexp=false grep "a+b*c" $H ab >actual &&
 373                test_cmp expected actual
 374        '
 375
 376        test_expect_success "grep $L with grep.extendedRegexp=true" '
 377                echo "${HC}ab:abc" >expected &&
 378                git -c grep.extendedRegexp=true grep "a+b*c" $H ab >actual &&
 379                test_cmp expected actual
 380        '
 381
 382        test_expect_success "grep $L with grep.patterntype=basic" '
 383                echo "${HC}ab:a+bc" >expected &&
 384                git -c grep.patterntype=basic grep "a+b*c" $H ab >actual &&
 385                test_cmp expected actual
 386        '
 387
 388        test_expect_success "grep $L with grep.patterntype=extended" '
 389                echo "${HC}ab:abc" >expected &&
 390                git -c grep.patterntype=extended grep "a+b*c" $H ab >actual &&
 391                test_cmp expected actual
 392        '
 393
 394        test_expect_success "grep $L with grep.patterntype=fixed" '
 395                echo "${HC}ab:a+b*c" >expected &&
 396                git -c grep.patterntype=fixed grep "a+b*c" $H ab >actual &&
 397                test_cmp expected actual
 398        '
 399
 400        test_expect_success PCRE "grep $L with grep.patterntype=perl" '
 401                echo "${HC}ab:a+b*c" >expected &&
 402                git -c grep.patterntype=perl grep "a\x{2b}b\x{2a}c" $H ab >actual &&
 403                test_cmp expected actual
 404        '
 405
 406        test_expect_success !PCRE "grep $L with grep.patterntype=perl errors without PCRE" '
 407                test_must_fail git -c grep.patterntype=perl grep "foo.*bar"
 408        '
 409
 410        test_expect_success "grep $L with grep.patternType=default and grep.extendedRegexp=true" '
 411                echo "${HC}ab:abc" >expected &&
 412                git \
 413                        -c grep.patternType=default \
 414                        -c grep.extendedRegexp=true \
 415                        grep "a+b*c" $H ab >actual &&
 416                test_cmp expected actual
 417        '
 418
 419        test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=default" '
 420                echo "${HC}ab:abc" >expected &&
 421                git \
 422                        -c grep.extendedRegexp=true \
 423                        -c grep.patternType=default \
 424                        grep "a+b*c" $H ab >actual &&
 425                test_cmp expected actual
 426        '
 427
 428        test_expect_success "grep $L with grep.patternType=extended and grep.extendedRegexp=false" '
 429                echo "${HC}ab:abc" >expected &&
 430                git \
 431                        -c grep.patternType=extended \
 432                        -c grep.extendedRegexp=false \
 433                        grep "a+b*c" $H ab >actual &&
 434                test_cmp expected actual
 435        '
 436
 437        test_expect_success "grep $L with grep.patternType=basic and grep.extendedRegexp=true" '
 438                echo "${HC}ab:a+bc" >expected &&
 439                git \
 440                        -c grep.patternType=basic \
 441                        -c grep.extendedRegexp=true \
 442                        grep "a+b*c" $H ab >actual &&
 443                test_cmp expected actual
 444        '
 445
 446        test_expect_success "grep $L with grep.extendedRegexp=false and grep.patternType=extended" '
 447                echo "${HC}ab:abc" >expected &&
 448                git \
 449                        -c grep.extendedRegexp=false \
 450                        -c grep.patternType=extended \
 451                        grep "a+b*c" $H ab >actual &&
 452                test_cmp expected actual
 453        '
 454
 455        test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=basic" '
 456                echo "${HC}ab:a+bc" >expected &&
 457                git \
 458                        -c grep.extendedRegexp=true \
 459                        -c grep.patternType=basic \
 460                        grep "a+b*c" $H ab >actual &&
 461                test_cmp expected actual
 462        '
 463
 464        test_expect_success "grep --count $L" '
 465                echo ${HC}ab:3 >expected &&
 466                git grep --count -e b $H -- ab >actual &&
 467                test_cmp expected actual
 468        '
 469
 470        test_expect_success "grep --count -h $L" '
 471                echo 3 >expected &&
 472                git grep --count -h -e b $H -- ab >actual &&
 473                test_cmp expected actual
 474        '
 475done
 476
 477cat >expected <<EOF
 478file
 479EOF
 480test_expect_success 'grep -l -C' '
 481        git grep -l -C1 foo >actual &&
 482        test_cmp expected actual
 483'
 484
 485cat >expected <<EOF
 486file:5
 487EOF
 488test_expect_success 'grep -c -C' '
 489        git grep -c -C1 foo >actual &&
 490        test_cmp expected actual
 491'
 492
 493test_expect_success 'grep -L -C' '
 494        git ls-files >expected &&
 495        git grep -L -C1 nonexistent_string >actual &&
 496        test_cmp expected actual
 497'
 498
 499test_expect_success 'grep --files-without-match --quiet' '
 500        git grep --files-without-match --quiet nonexistent_string >actual &&
 501        test_cmp /dev/null actual
 502'
 503
 504cat >expected <<EOF
 505file:foo mmap bar_mmap
 506EOF
 507
 508test_expect_success 'grep -e A --and -e B' '
 509        git grep -e "foo mmap" --and -e bar_mmap >actual &&
 510        test_cmp expected actual
 511'
 512
 513cat >expected <<EOF
 514file:foo_mmap bar mmap
 515file:foo_mmap bar mmap baz
 516EOF
 517
 518
 519test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
 520        git grep \( -e foo_ --or -e baz \) \
 521                --and -e " mmap" >actual &&
 522        test_cmp expected actual
 523'
 524
 525cat >expected <<EOF
 526file:foo mmap bar
 527EOF
 528
 529test_expect_success 'grep -e A --and --not -e B' '
 530        git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
 531        test_cmp expected actual
 532'
 533
 534test_expect_success 'grep should ignore GREP_OPTIONS' '
 535        GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
 536        test_cmp expected actual
 537'
 538
 539test_expect_success 'grep -f, non-existent file' '
 540        test_must_fail git grep -f patterns
 541'
 542
 543cat >expected <<EOF
 544file:foo mmap bar
 545file:foo_mmap bar
 546file:foo_mmap bar mmap
 547file:foo mmap bar_mmap
 548file:foo_mmap bar mmap baz
 549EOF
 550
 551cat >pattern <<EOF
 552mmap
 553EOF
 554
 555test_expect_success 'grep -f, one pattern' '
 556        git grep -f pattern >actual &&
 557        test_cmp expected actual
 558'
 559
 560cat >expected <<EOF
 561file:foo mmap bar
 562file:foo_mmap bar
 563file:foo_mmap bar mmap
 564file:foo mmap bar_mmap
 565file:foo_mmap bar mmap baz
 566t/a/v:vvv
 567t/v:vvv
 568v:vvv
 569EOF
 570
 571cat >patterns <<EOF
 572mmap
 573vvv
 574EOF
 575
 576test_expect_success 'grep -f, multiple patterns' '
 577        git grep -f patterns >actual &&
 578        test_cmp expected actual
 579'
 580
 581test_expect_success 'grep, multiple patterns' '
 582        git grep "$(cat patterns)" >actual &&
 583        test_cmp expected actual
 584'
 585
 586cat >expected <<EOF
 587file:foo mmap bar
 588file:foo_mmap bar
 589file:foo_mmap bar mmap
 590file:foo mmap bar_mmap
 591file:foo_mmap bar mmap baz
 592t/a/v:vvv
 593t/v:vvv
 594v:vvv
 595EOF
 596
 597cat >patterns <<EOF
 598
 599mmap
 600
 601vvv
 602
 603EOF
 604
 605test_expect_success 'grep -f, ignore empty lines' '
 606        git grep -f patterns >actual &&
 607        test_cmp expected actual
 608'
 609
 610test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
 611        git grep -f - <patterns >actual &&
 612        test_cmp expected actual
 613'
 614
 615cat >expected <<EOF
 616y:y yy
 617--
 618z:zzz
 619EOF
 620
 621test_expect_success 'grep -q, silently report matches' '
 622        >empty &&
 623        git grep -q mmap >actual &&
 624        test_cmp empty actual &&
 625        test_must_fail git grep -q qfwfq >actual &&
 626        test_cmp empty actual
 627'
 628
 629test_expect_success 'grep -C1 hunk mark between files' '
 630        git grep -C1 "^[yz]" >actual &&
 631        test_cmp expected actual
 632'
 633
 634test_expect_success 'log grep setup' '
 635        echo a >>file &&
 636        test_tick &&
 637        GIT_AUTHOR_NAME="With * Asterisk" \
 638        GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
 639        git commit -a -m "second" &&
 640
 641        echo a >>file &&
 642        test_tick &&
 643        git commit -a -m "third" &&
 644
 645        echo a >>file &&
 646        test_tick &&
 647        GIT_AUTHOR_NAME="Night Fall" \
 648        GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
 649        git commit -a -m "fourth"
 650'
 651
 652test_expect_success 'log grep (1)' '
 653        git log --author=author --pretty=tformat:%s >actual &&
 654        {
 655                echo third && echo initial
 656        } >expect &&
 657        test_cmp expect actual
 658'
 659
 660test_expect_success 'log grep (2)' '
 661        git log --author=" * " -F --pretty=tformat:%s >actual &&
 662        {
 663                echo second
 664        } >expect &&
 665        test_cmp expect actual
 666'
 667
 668test_expect_success 'log grep (3)' '
 669        git log --author="^A U" --pretty=tformat:%s >actual &&
 670        {
 671                echo third && echo initial
 672        } >expect &&
 673        test_cmp expect actual
 674'
 675
 676test_expect_success 'log grep (4)' '
 677        git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
 678        {
 679                echo second
 680        } >expect &&
 681        test_cmp expect actual
 682'
 683
 684test_expect_success 'log grep (5)' '
 685        git log --author=Thor -F --pretty=tformat:%s >actual &&
 686        {
 687                echo third && echo initial
 688        } >expect &&
 689        test_cmp expect actual
 690'
 691
 692test_expect_success 'log grep (6)' '
 693        git log --author=-0700  --pretty=tformat:%s >actual &&
 694        >expect &&
 695        test_cmp expect actual
 696'
 697
 698test_expect_success 'log grep (7)' '
 699        git log -g --grep-reflog="commit: third" --pretty=tformat:%s >actual &&
 700        echo third >expect &&
 701        test_cmp expect actual
 702'
 703
 704test_expect_success 'log grep (8)' '
 705        git log -g --grep-reflog="commit: third" --grep-reflog="commit: second" --pretty=tformat:%s >actual &&
 706        {
 707                echo third && echo second
 708        } >expect &&
 709        test_cmp expect actual
 710'
 711
 712test_expect_success 'log grep (9)' '
 713        git log -g --grep-reflog="commit: third" --author="Thor" --pretty=tformat:%s >actual &&
 714        echo third >expect &&
 715        test_cmp expect actual
 716'
 717
 718test_expect_success 'log grep (9)' '
 719        git log -g --grep-reflog="commit: third" --author="non-existent" --pretty=tformat:%s >actual &&
 720        : >expect &&
 721        test_cmp expect actual
 722'
 723
 724test_expect_success 'log --grep-reflog can only be used under -g' '
 725        test_must_fail git log --grep-reflog="commit: third"
 726'
 727
 728test_expect_success 'log with multiple --grep uses union' '
 729        git log --grep=i --grep=r --format=%s >actual &&
 730        {
 731                echo fourth && echo third && echo initial
 732        } >expect &&
 733        test_cmp expect actual
 734'
 735
 736test_expect_success 'log --all-match with multiple --grep uses intersection' '
 737        git log --all-match --grep=i --grep=r --format=%s >actual &&
 738        {
 739                echo third
 740        } >expect &&
 741        test_cmp expect actual
 742'
 743
 744test_expect_success 'log with multiple --author uses union' '
 745        git log --author="Thor" --author="Aster" --format=%s >actual &&
 746        {
 747            echo third && echo second && echo initial
 748        } >expect &&
 749        test_cmp expect actual
 750'
 751
 752test_expect_success 'log --all-match with multiple --author still uses union' '
 753        git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
 754        {
 755            echo third && echo second && echo initial
 756        } >expect &&
 757        test_cmp expect actual
 758'
 759
 760test_expect_success 'log --grep --author uses intersection' '
 761        # grep matches only third and fourth
 762        # author matches only initial and third
 763        git log --author="A U Thor" --grep=r --format=%s >actual &&
 764        {
 765                echo third
 766        } >expect &&
 767        test_cmp expect actual
 768'
 769
 770test_expect_success 'log --grep --grep --author takes union of greps and intersects with author' '
 771        # grep matches initial and second but not third
 772        # author matches only initial and third
 773        git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
 774        {
 775                echo initial
 776        } >expect &&
 777        test_cmp expect actual
 778'
 779
 780test_expect_success 'log ---all-match -grep --author --author still takes union of authors and intersects with grep' '
 781        # grep matches only initial and third
 782        # author matches all but second
 783        git log --all-match --author="Thor" --author="Night" --grep=i --format=%s >actual &&
 784        {
 785            echo third && echo initial
 786        } >expect &&
 787        test_cmp expect actual
 788'
 789
 790test_expect_success 'log --grep --author --author takes union of authors and intersects with grep' '
 791        # grep matches only initial and third
 792        # author matches all but second
 793        git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
 794        {
 795            echo third && echo initial
 796        } >expect &&
 797        test_cmp expect actual
 798'
 799
 800test_expect_success 'log --all-match --grep --grep --author takes intersection' '
 801        # grep matches only third
 802        # author matches only initial and third
 803        git log --all-match --author="A U Thor" --grep=i --grep=r --format=%s >actual &&
 804        {
 805                echo third
 806        } >expect &&
 807        test_cmp expect actual
 808'
 809
 810test_expect_success 'log --author does not search in timestamp' '
 811        : >expect &&
 812        git log --author="$GIT_AUTHOR_DATE" >actual &&
 813        test_cmp expect actual
 814'
 815
 816test_expect_success 'log --committer does not search in timestamp' '
 817        : >expect &&
 818        git log --committer="$GIT_COMMITTER_DATE" >actual &&
 819        test_cmp expect actual
 820'
 821
 822test_expect_success 'grep with CE_VALID file' '
 823        git update-index --assume-unchanged t/t &&
 824        rm t/t &&
 825        test "$(git grep test)" = "t/t:test" &&
 826        git update-index --no-assume-unchanged t/t &&
 827        git checkout t/t
 828'
 829
 830cat >expected <<EOF
 831hello.c=#include <stdio.h>
 832hello.c:        return 0;
 833EOF
 834
 835test_expect_success 'grep -p with userdiff' '
 836        git config diff.custom.funcname "^#" &&
 837        echo "hello.c diff=custom" >.gitattributes &&
 838        git grep -p return >actual &&
 839        test_cmp expected actual
 840'
 841
 842cat >expected <<EOF
 843hello.c=int main(int argc, const char **argv)
 844hello.c:        return 0;
 845EOF
 846
 847test_expect_success 'grep -p' '
 848        rm -f .gitattributes &&
 849        git grep -p return >actual &&
 850        test_cmp expected actual
 851'
 852
 853cat >expected <<EOF
 854hello.c-#include <stdio.h>
 855hello.c-
 856hello.c=int main(int argc, const char **argv)
 857hello.c-{
 858hello.c-        printf("Hello world.\n");
 859hello.c:        return 0;
 860EOF
 861
 862test_expect_success 'grep -p -B5' '
 863        git grep -p -B5 return >actual &&
 864        test_cmp expected actual
 865'
 866
 867cat >expected <<EOF
 868hello.c=int main(int argc, const char **argv)
 869hello.c-{
 870hello.c-        printf("Hello world.\n");
 871hello.c:        return 0;
 872hello.c-        /* char ?? */
 873hello.c-}
 874EOF
 875
 876test_expect_success 'grep -W' '
 877        git grep -W return >actual &&
 878        test_cmp expected actual
 879'
 880
 881cat >expected <<EOF
 882hello.c-#include <assert.h>
 883hello.c:#include <stdio.h>
 884EOF
 885
 886test_expect_success 'grep -W shows no trailing empty lines' '
 887        git grep -W stdio >actual &&
 888        test_cmp expected actual
 889'
 890
 891test_expect_success 'grep -W with userdiff' '
 892        test_when_finished "rm -f .gitattributes" &&
 893        git config diff.custom.xfuncname "^function .*$" &&
 894        echo "hello.ps1 diff=custom" >.gitattributes &&
 895        git grep -W echo >function-context-userdiff-actual
 896'
 897
 898test_expect_success ' includes preceding comment' '
 899        grep "# Say hello" function-context-userdiff-actual
 900'
 901
 902test_expect_success ' includes function line' '
 903        grep "=function hello" function-context-userdiff-actual
 904'
 905
 906test_expect_success ' includes matching line' '
 907        grep ":  echo" function-context-userdiff-actual
 908'
 909
 910test_expect_success ' includes last line of the function' '
 911        grep "} # hello" function-context-userdiff-actual
 912'
 913
 914for threads in $(test_seq 0 10)
 915do
 916        test_expect_success "grep --threads=$threads & -c grep.threads=$threads" "
 917                git grep --threads=$threads . >actual.$threads &&
 918                if test $threads -ge 1
 919                then
 920                        test_cmp actual.\$(($threads - 1)) actual.$threads
 921                fi &&
 922                git -c grep.threads=$threads grep . >actual.$threads &&
 923                if test $threads -ge 1
 924                then
 925                        test_cmp actual.\$(($threads - 1)) actual.$threads
 926                fi
 927        "
 928done
 929
 930test_expect_success !PTHREADS,C_LOCALE_OUTPUT 'grep --threads=N or pack.threads=N warns when no pthreads' '
 931        git grep --threads=2 Hello hello_world 2>err &&
 932        grep ^warning: err >warnings &&
 933        test_line_count = 1 warnings &&
 934        grep -F "no threads support, ignoring --threads" err &&
 935        git -c grep.threads=2 grep Hello hello_world 2>err &&
 936        grep ^warning: err >warnings &&
 937        test_line_count = 1 warnings &&
 938        grep -F "no threads support, ignoring grep.threads" err &&
 939        git -c grep.threads=2 grep --threads=4 Hello hello_world 2>err &&
 940        grep ^warning: err >warnings &&
 941        test_line_count = 2 warnings &&
 942        grep -F "no threads support, ignoring --threads" err &&
 943        grep -F "no threads support, ignoring grep.threads" err &&
 944        git -c grep.threads=0 grep --threads=0 Hello hello_world 2>err &&
 945        test_line_count = 0 err
 946'
 947
 948test_expect_success 'grep from a subdirectory to search wider area (1)' '
 949        mkdir -p s &&
 950        (
 951                cd s && git grep "x x x" ..
 952        )
 953'
 954
 955test_expect_success 'grep from a subdirectory to search wider area (2)' '
 956        mkdir -p s &&
 957        (
 958                cd s || exit 1
 959                ( git grep xxyyzz .. >out ; echo $? >status )
 960                ! test -s out &&
 961                test 1 = $(cat status)
 962        )
 963'
 964
 965cat >expected <<EOF
 966hello.c:int main(int argc, const char **argv)
 967EOF
 968
 969test_expect_success 'grep -Fi' '
 970        git grep -Fi "CHAR *" >actual &&
 971        test_cmp expected actual
 972'
 973
 974test_expect_success 'outside of git repository' '
 975        rm -fr non &&
 976        mkdir -p non/git/sub &&
 977        echo hello >non/git/file1 &&
 978        echo world >non/git/sub/file2 &&
 979        {
 980                echo file1:hello &&
 981                echo sub/file2:world
 982        } >non/expect.full &&
 983        echo file2:world >non/expect.sub &&
 984        (
 985                GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
 986                export GIT_CEILING_DIRECTORIES &&
 987                cd non/git &&
 988                test_must_fail git grep o &&
 989                git grep --no-index o >../actual.full &&
 990                test_cmp ../expect.full ../actual.full &&
 991                cd sub &&
 992                test_must_fail git grep o &&
 993                git grep --no-index o >../../actual.sub &&
 994                test_cmp ../../expect.sub ../../actual.sub
 995        ) &&
 996
 997        echo ".*o*" >non/git/.gitignore &&
 998        (
 999                GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1000                export GIT_CEILING_DIRECTORIES &&
1001                cd non/git &&
1002                test_must_fail git grep o &&
1003                git grep --no-index --exclude-standard o >../actual.full &&
1004                test_cmp ../expect.full ../actual.full &&
1005
1006                {
1007                        echo ".gitignore:.*o*" &&
1008                        cat ../expect.full
1009                } >../expect.with.ignored &&
1010                git grep --no-index --no-exclude o >../actual.full &&
1011                test_cmp ../expect.with.ignored ../actual.full
1012        )
1013'
1014
1015test_expect_success 'outside of git repository with fallbackToNoIndex' '
1016        rm -fr non &&
1017        mkdir -p non/git/sub &&
1018        echo hello >non/git/file1 &&
1019        echo world >non/git/sub/file2 &&
1020        cat <<-\EOF >non/expect.full &&
1021        file1:hello
1022        sub/file2:world
1023        EOF
1024        echo file2:world >non/expect.sub &&
1025        (
1026                GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1027                export GIT_CEILING_DIRECTORIES &&
1028                cd non/git &&
1029                test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
1030                git -c grep.fallbackToNoIndex=true grep o >../actual.full &&
1031                test_cmp ../expect.full ../actual.full &&
1032                cd sub &&
1033                test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
1034                git -c grep.fallbackToNoIndex=true grep o >../../actual.sub &&
1035                test_cmp ../../expect.sub ../../actual.sub
1036        ) &&
1037
1038        echo ".*o*" >non/git/.gitignore &&
1039        (
1040                GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1041                export GIT_CEILING_DIRECTORIES &&
1042                cd non/git &&
1043                test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
1044                git -c grep.fallbackToNoIndex=true grep --exclude-standard o >../actual.full &&
1045                test_cmp ../expect.full ../actual.full &&
1046
1047                {
1048                        echo ".gitignore:.*o*" &&
1049                        cat ../expect.full
1050                } >../expect.with.ignored &&
1051                git -c grep.fallbackToNoIndex grep --no-exclude o >../actual.full &&
1052                test_cmp ../expect.with.ignored ../actual.full
1053        )
1054'
1055
1056test_expect_success 'inside git repository but with --no-index' '
1057        rm -fr is &&
1058        mkdir -p is/git/sub &&
1059        echo hello >is/git/file1 &&
1060        echo world >is/git/sub/file2 &&
1061        echo ".*o*" >is/git/.gitignore &&
1062        {
1063                echo file1:hello &&
1064                echo sub/file2:world
1065        } >is/expect.unignored &&
1066        {
1067                echo ".gitignore:.*o*" &&
1068                cat is/expect.unignored
1069        } >is/expect.full &&
1070        : >is/expect.empty &&
1071        echo file2:world >is/expect.sub &&
1072        (
1073                cd is/git &&
1074                git init &&
1075                test_must_fail git grep o >../actual.full &&
1076                test_cmp ../expect.empty ../actual.full &&
1077
1078                git grep --untracked o >../actual.unignored &&
1079                test_cmp ../expect.unignored ../actual.unignored &&
1080
1081                git grep --no-index o >../actual.full &&
1082                test_cmp ../expect.full ../actual.full &&
1083
1084                git grep --no-index --exclude-standard o >../actual.unignored &&
1085                test_cmp ../expect.unignored ../actual.unignored &&
1086
1087                cd sub &&
1088                test_must_fail git grep o >../../actual.sub &&
1089                test_cmp ../../expect.empty ../../actual.sub &&
1090
1091                git grep --no-index o >../../actual.sub &&
1092                test_cmp ../../expect.sub ../../actual.sub &&
1093
1094                git grep --untracked o >../../actual.sub &&
1095                test_cmp ../../expect.sub ../../actual.sub
1096        )
1097'
1098
1099test_expect_success 'grep --no-index descends into repos, but not .git' '
1100        rm -fr non &&
1101        mkdir -p non/git &&
1102        (
1103                GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1104                export GIT_CEILING_DIRECTORIES &&
1105                cd non/git &&
1106
1107                echo magic >file &&
1108                git init repo &&
1109                (
1110                        cd repo &&
1111                        echo magic >file &&
1112                        git add file &&
1113                        git commit -m foo &&
1114                        echo magic >.git/file
1115                ) &&
1116
1117                cat >expect <<-\EOF &&
1118                file
1119                repo/file
1120                EOF
1121                git grep -l --no-index magic >actual &&
1122                test_cmp expect actual
1123        )
1124'
1125
1126test_expect_success 'setup double-dash tests' '
1127cat >double-dash <<EOF &&
1128--
1129->
1130other
1131EOF
1132git add double-dash
1133'
1134
1135cat >expected <<EOF
1136double-dash:->
1137EOF
1138test_expect_success 'grep -- pattern' '
1139        git grep -- "->" >actual &&
1140        test_cmp expected actual
1141'
1142test_expect_success 'grep -- pattern -- pathspec' '
1143        git grep -- "->" -- double-dash >actual &&
1144        test_cmp expected actual
1145'
1146test_expect_success 'grep -e pattern -- path' '
1147        git grep -e "->" -- double-dash >actual &&
1148        test_cmp expected actual
1149'
1150
1151cat >expected <<EOF
1152double-dash:--
1153EOF
1154test_expect_success 'grep -e -- -- path' '
1155        git grep -e -- -- double-dash >actual &&
1156        test_cmp expected actual
1157'
1158
1159test_expect_success 'dashdash disambiguates rev as rev' '
1160        test_when_finished "rm -f master" &&
1161        echo content >master &&
1162        echo master:hello.c >expect &&
1163        git grep -l o master -- hello.c >actual &&
1164        test_cmp expect actual
1165'
1166
1167test_expect_success 'dashdash disambiguates pathspec as pathspec' '
1168        test_when_finished "git rm -f master" &&
1169        echo content >master &&
1170        git add master &&
1171        echo master:content >expect &&
1172        git grep o -- master >actual &&
1173        test_cmp expect actual
1174'
1175
1176test_expect_success 'report bogus arg without dashdash' '
1177        test_must_fail git grep o does-not-exist
1178'
1179
1180test_expect_success 'report bogus rev with dashdash' '
1181        test_must_fail git grep o hello.c --
1182'
1183
1184test_expect_success 'allow non-existent path with dashdash' '
1185        # We need a real match so grep exits with success.
1186        tree=$(git ls-tree HEAD |
1187               sed s/hello.c/not-in-working-tree/ |
1188               git mktree) &&
1189        git grep o "$tree" -- not-in-working-tree
1190'
1191
1192test_expect_success 'grep --no-index pattern -- path' '
1193        rm -fr non &&
1194        mkdir -p non/git &&
1195        (
1196                GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1197                export GIT_CEILING_DIRECTORIES &&
1198                cd non/git &&
1199                echo hello >hello &&
1200                echo goodbye >goodbye &&
1201                echo hello:hello >expect &&
1202                git grep --no-index o -- hello >actual &&
1203                test_cmp expect actual
1204        )
1205'
1206
1207test_expect_success 'grep --no-index complains of revs' '
1208        test_must_fail git grep --no-index o master -- 2>err &&
1209        test_i18ngrep "cannot be used with revs" err
1210'
1211
1212test_expect_success 'grep --no-index prefers paths to revs' '
1213        test_when_finished "rm -f master" &&
1214        echo content >master &&
1215        echo master:content >expect &&
1216        git grep --no-index o master >actual &&
1217        test_cmp expect actual
1218'
1219
1220test_expect_success 'grep --no-index does not "diagnose" revs' '
1221        test_must_fail git grep --no-index o :1:hello.c 2>err &&
1222        test_i18ngrep ! -i "did you mean" err
1223'
1224
1225cat >expected <<EOF
1226hello.c:int main(int argc, const char **argv)
1227hello.c:        printf("Hello world.\n");
1228EOF
1229
1230test_expect_success PCRE 'grep --perl-regexp pattern' '
1231        git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1232        test_cmp expected actual
1233'
1234
1235test_expect_success !PCRE 'grep --perl-regexp pattern errors without PCRE' '
1236        test_must_fail git grep --perl-regexp "foo.*bar"
1237'
1238
1239test_expect_success PCRE 'grep -P pattern' '
1240        git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1241        test_cmp expected actual
1242'
1243
1244test_expect_success LIBPCRE2 "grep -P with (*NO_JIT) doesn't error out" '
1245        git grep -P "(*NO_JIT)\p{Ps}.*?\p{Pe}" hello.c >actual &&
1246        test_cmp expected actual
1247
1248'
1249
1250test_expect_success !PCRE 'grep -P pattern errors without PCRE' '
1251        test_must_fail git grep -P "foo.*bar"
1252'
1253
1254test_expect_success 'grep pattern with grep.extendedRegexp=true' '
1255        >empty &&
1256        test_must_fail git -c grep.extendedregexp=true \
1257                grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1258        test_cmp empty actual
1259'
1260
1261test_expect_success PCRE 'grep -P pattern with grep.extendedRegexp=true' '
1262        git -c grep.extendedregexp=true \
1263                grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1264        test_cmp expected actual
1265'
1266
1267test_expect_success PCRE 'grep -P -v pattern' '
1268        {
1269                echo "ab:a+b*c"
1270                echo "ab:a+bc"
1271        } >expected &&
1272        git grep -P -v "abc" ab >actual &&
1273        test_cmp expected actual
1274'
1275
1276test_expect_success PCRE 'grep -P -i pattern' '
1277        cat >expected <<-EOF &&
1278        hello.c:        printf("Hello world.\n");
1279        EOF
1280        git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
1281        test_cmp expected actual
1282'
1283
1284test_expect_success PCRE 'grep -P -w pattern' '
1285        {
1286                echo "hello_world:Hello world"
1287                echo "hello_world:HeLLo world"
1288        } >expected &&
1289        git grep -P -w "He((?i)ll)o" hello_world >actual &&
1290        test_cmp expected actual
1291'
1292
1293test_expect_success PCRE 'grep -P backreferences work (the PCRE NO_AUTO_CAPTURE flag is not set)' '
1294        git grep -P -h "(?P<one>.)(?P=one)" hello_world >actual &&
1295        test_cmp hello_world actual &&
1296        git grep -P -h "(.)\1" hello_world >actual &&
1297        test_cmp hello_world actual
1298'
1299
1300test_expect_success 'grep -G invalidpattern properly dies ' '
1301        test_must_fail git grep -G "a["
1302'
1303
1304test_expect_success 'grep invalidpattern properly dies with grep.patternType=basic' '
1305        test_must_fail git -c grep.patterntype=basic grep "a["
1306'
1307
1308test_expect_success 'grep -E invalidpattern properly dies ' '
1309        test_must_fail git grep -E "a["
1310'
1311
1312test_expect_success 'grep invalidpattern properly dies with grep.patternType=extended' '
1313        test_must_fail git -c grep.patterntype=extended grep "a["
1314'
1315
1316test_expect_success PCRE 'grep -P invalidpattern properly dies ' '
1317        test_must_fail git grep -P "a["
1318'
1319
1320test_expect_success PCRE 'grep invalidpattern properly dies with grep.patternType=perl' '
1321        test_must_fail git -c grep.patterntype=perl grep "a["
1322'
1323
1324test_expect_success 'grep -G -E -F pattern' '
1325        echo "ab:a+b*c" >expected &&
1326        git grep -G -E -F "a+b*c" ab >actual &&
1327        test_cmp expected actual
1328'
1329
1330test_expect_success 'grep pattern with grep.patternType=basic, =extended, =fixed' '
1331        echo "ab:a+b*c" >expected &&
1332        git \
1333                -c grep.patterntype=basic \
1334                -c grep.patterntype=extended \
1335                -c grep.patterntype=fixed \
1336                grep "a+b*c" ab >actual &&
1337        test_cmp expected actual
1338'
1339
1340test_expect_success 'grep -E -F -G pattern' '
1341        echo "ab:a+bc" >expected &&
1342        git grep -E -F -G "a+b*c" ab >actual &&
1343        test_cmp expected actual
1344'
1345
1346test_expect_success 'grep pattern with grep.patternType=extended, =fixed, =basic' '
1347        echo "ab:a+bc" >expected &&
1348        git \
1349                -c grep.patterntype=extended \
1350                -c grep.patterntype=fixed \
1351                -c grep.patterntype=basic \
1352                grep "a+b*c" ab >actual &&
1353        test_cmp expected actual
1354'
1355
1356test_expect_success 'grep -F -G -E pattern' '
1357        echo "ab:abc" >expected &&
1358        git grep -F -G -E "a+b*c" ab >actual &&
1359        test_cmp expected actual
1360'
1361
1362test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended' '
1363        echo "ab:abc" >expected &&
1364        git \
1365                -c grep.patterntype=fixed \
1366                -c grep.patterntype=basic \
1367                -c grep.patterntype=extended \
1368                grep "a+b*c" ab >actual &&
1369        test_cmp expected actual
1370'
1371
1372test_expect_success 'grep -G -F -P -E pattern' '
1373        echo "d0:d" >expected &&
1374        git grep -G -F -P -E "[\d]" d0 >actual &&
1375        test_cmp expected actual
1376'
1377
1378test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
1379        echo "d0:d" >expected &&
1380        git \
1381                -c grep.patterntype=fixed \
1382                -c grep.patterntype=basic \
1383                -c grep.patterntype=perl \
1384                -c grep.patterntype=extended \
1385                grep "[\d]" d0 >actual &&
1386        test_cmp expected actual
1387'
1388
1389test_expect_success PCRE 'grep -G -F -E -P pattern' '
1390        echo "d0:0" >expected &&
1391        git grep -G -F -E -P "[\d]" d0 >actual &&
1392        test_cmp expected actual
1393'
1394
1395test_expect_success PCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1396        echo "d0:0" >expected &&
1397        git \
1398                -c grep.patterntype=fixed \
1399                -c grep.patterntype=basic \
1400                -c grep.patterntype=extended \
1401                -c grep.patterntype=perl \
1402                grep "[\d]" d0 >actual &&
1403        test_cmp expected actual
1404'
1405
1406test_expect_success PCRE 'grep -P pattern with grep.patternType=fixed' '
1407        echo "ab:a+b*c" >expected &&
1408        git \
1409                -c grep.patterntype=fixed \
1410                grep -P "a\x{2b}b\x{2a}c" ab >actual &&
1411        test_cmp expected actual
1412'
1413
1414test_expect_success 'grep -F pattern with grep.patternType=basic' '
1415        echo "ab:a+b*c" >expected &&
1416        git \
1417                -c grep.patterntype=basic \
1418                grep -F "*c" ab >actual &&
1419        test_cmp expected actual
1420'
1421
1422test_expect_success 'grep -G pattern with grep.patternType=fixed' '
1423        {
1424                echo "ab:a+b*c"
1425                echo "ab:a+bc"
1426        } >expected &&
1427        git \
1428                -c grep.patterntype=fixed \
1429                grep -G "a+b" ab >actual &&
1430        test_cmp expected actual
1431'
1432
1433test_expect_success 'grep -E pattern with grep.patternType=fixed' '
1434        {
1435                echo "ab:a+b*c"
1436                echo "ab:a+bc"
1437                echo "ab:abc"
1438        } >expected &&
1439        git \
1440                -c grep.patterntype=fixed \
1441                grep -E "a+" ab >actual &&
1442        test_cmp expected actual
1443'
1444
1445cat >expected <<EOF
1446hello.c<RED>:<RESET>int main(int argc, const char **argv)
1447hello.c<RED>-<RESET>{
1448<RED>--<RESET>
1449hello.c<RED>:<RESET>    /* char ?? */
1450hello.c<RED>-<RESET>}
1451<RED>--<RESET>
1452hello_world<RED>:<RESET>Hello_world
1453hello_world<RED>-<RESET>HeLLo_world
1454EOF
1455
1456test_expect_success 'grep --color, separator' '
1457        test_config color.grep.context          normal &&
1458        test_config color.grep.filename         normal &&
1459        test_config color.grep.function         normal &&
1460        test_config color.grep.linenumber       normal &&
1461        test_config color.grep.match            normal &&
1462        test_config color.grep.selected         normal &&
1463        test_config color.grep.separator        red &&
1464
1465        git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
1466        test_decode_color >actual &&
1467        test_cmp expected actual
1468'
1469
1470cat >expected <<EOF
1471hello.c:int main(int argc, const char **argv)
1472hello.c:        /* char ?? */
1473
1474hello_world:Hello_world
1475EOF
1476
1477test_expect_success 'grep --break' '
1478        git grep --break -e char -e lo_w hello.c hello_world >actual &&
1479        test_cmp expected actual
1480'
1481
1482cat >expected <<EOF
1483hello.c:int main(int argc, const char **argv)
1484hello.c-{
1485--
1486hello.c:        /* char ?? */
1487hello.c-}
1488
1489hello_world:Hello_world
1490hello_world-HeLLo_world
1491EOF
1492
1493test_expect_success 'grep --break with context' '
1494        git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
1495        test_cmp expected actual
1496'
1497
1498cat >expected <<EOF
1499hello.c
1500int main(int argc, const char **argv)
1501        /* char ?? */
1502hello_world
1503Hello_world
1504EOF
1505
1506test_expect_success 'grep --heading' '
1507        git grep --heading -e char -e lo_w hello.c hello_world >actual &&
1508        test_cmp expected actual
1509'
1510
1511cat >expected <<EOF
1512<BOLD;GREEN>hello.c<RESET>
15134:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
15148:      /* <BLACK;BYELLOW>char<RESET> ?? */
1515
1516<BOLD;GREEN>hello_world<RESET>
15173:Hel<BLACK;BYELLOW>lo_w<RESET>orld
1518EOF
1519
1520test_expect_success 'mimic ack-grep --group' '
1521        test_config color.grep.context          normal &&
1522        test_config color.grep.filename         "bold green" &&
1523        test_config color.grep.function         normal &&
1524        test_config color.grep.linenumber       normal &&
1525        test_config color.grep.match            "black yellow" &&
1526        test_config color.grep.selected         normal &&
1527        test_config color.grep.separator        normal &&
1528
1529        git grep --break --heading -n --color \
1530                -e char -e lo_w hello.c hello_world |
1531        test_decode_color >actual &&
1532        test_cmp expected actual
1533'
1534
1535cat >expected <<EOF
1536space: line with leading space1
1537space: line with leading space2
1538space: line with leading space3
1539EOF
1540
1541test_expect_success PCRE 'grep -E "^ "' '
1542        git grep -E "^ " space >actual &&
1543        test_cmp expected actual
1544'
1545
1546test_expect_success PCRE 'grep -P "^ "' '
1547        git grep -P "^ " space >actual &&
1548        test_cmp expected actual
1549'
1550
1551cat >expected <<EOF
1552space-line without leading space1
1553space: line <RED>with <RESET>leading space1
1554space: line <RED>with <RESET>leading <RED>space2<RESET>
1555space: line <RED>with <RESET>leading space3
1556space:line without leading <RED>space2<RESET>
1557EOF
1558
1559test_expect_success 'grep --color -e A -e B with context' '
1560        test_config color.grep.context          normal &&
1561        test_config color.grep.filename         normal &&
1562        test_config color.grep.function         normal &&
1563        test_config color.grep.linenumber       normal &&
1564        test_config color.grep.matchContext     normal &&
1565        test_config color.grep.matchSelected    red &&
1566        test_config color.grep.selected         normal &&
1567        test_config color.grep.separator        normal &&
1568
1569        git grep --color=always -C2 -e "with " -e space2  space |
1570        test_decode_color >actual &&
1571        test_cmp expected actual
1572'
1573
1574cat >expected <<EOF
1575space-line without leading space1
1576space- line with leading space1
1577space: line <RED>with <RESET>leading <RED>space2<RESET>
1578space- line with leading space3
1579space-line without leading space2
1580EOF
1581
1582test_expect_success 'grep --color -e A --and -e B with context' '
1583        test_config color.grep.context          normal &&
1584        test_config color.grep.filename         normal &&
1585        test_config color.grep.function         normal &&
1586        test_config color.grep.linenumber       normal &&
1587        test_config color.grep.matchContext     normal &&
1588        test_config color.grep.matchSelected    red &&
1589        test_config color.grep.selected         normal &&
1590        test_config color.grep.separator        normal &&
1591
1592        git grep --color=always -C2 -e "with " --and -e space2  space |
1593        test_decode_color >actual &&
1594        test_cmp expected actual
1595'
1596
1597cat >expected <<EOF
1598space-line without leading space1
1599space: line <RED>with <RESET>leading space1
1600space- line with leading space2
1601space: line <RED>with <RESET>leading space3
1602space-line without leading space2
1603EOF
1604
1605test_expect_success 'grep --color -e A --and --not -e B with context' '
1606        test_config color.grep.context          normal &&
1607        test_config color.grep.filename         normal &&
1608        test_config color.grep.function         normal &&
1609        test_config color.grep.linenumber       normal &&
1610        test_config color.grep.matchContext     normal &&
1611        test_config color.grep.matchSelected    red &&
1612        test_config color.grep.selected         normal &&
1613        test_config color.grep.separator        normal &&
1614
1615        git grep --color=always -C2 -e "with " --and --not -e space2  space |
1616        test_decode_color >actual &&
1617        test_cmp expected actual
1618'
1619
1620cat >expected <<EOF
1621hello.c-
1622hello.c=int main(int argc, const char **argv)
1623hello.c-{
1624hello.c:        pr<RED>int<RESET>f("<RED>Hello<RESET> world.\n");
1625hello.c-        return 0;
1626hello.c-        /* char ?? */
1627hello.c-}
1628EOF
1629
1630test_expect_success 'grep --color -e A --and -e B -p with context' '
1631        test_config color.grep.context          normal &&
1632        test_config color.grep.filename         normal &&
1633        test_config color.grep.function         normal &&
1634        test_config color.grep.linenumber       normal &&
1635        test_config color.grep.matchContext     normal &&
1636        test_config color.grep.matchSelected    red &&
1637        test_config color.grep.selected         normal &&
1638        test_config color.grep.separator        normal &&
1639
1640        git grep --color=always -p -C3 -e int --and -e Hello --no-index hello.c |
1641        test_decode_color >actual &&
1642        test_cmp expected actual
1643'
1644
1645test_expect_success 'grep can find things only in the work tree' '
1646        : >work-tree-only &&
1647        git add work-tree-only &&
1648        test_when_finished "git rm -f work-tree-only" &&
1649        echo "find in work tree" >work-tree-only &&
1650        git grep --quiet "find in work tree" &&
1651        test_must_fail git grep --quiet --cached "find in work tree" &&
1652        test_must_fail git grep --quiet "find in work tree" HEAD
1653'
1654
1655test_expect_success 'grep can find things only in the work tree (i-t-a)' '
1656        echo "intend to add this" >intend-to-add &&
1657        git add -N intend-to-add &&
1658        test_when_finished "git rm -f intend-to-add" &&
1659        git grep --quiet "intend to add this" &&
1660        test_must_fail git grep --quiet --cached "intend to add this" &&
1661        test_must_fail git grep --quiet "intend to add this" HEAD
1662'
1663
1664test_expect_success 'grep does not search work tree with assume unchanged' '
1665        echo "intend to add this" >intend-to-add &&
1666        git add -N intend-to-add &&
1667        git update-index --assume-unchanged intend-to-add &&
1668        test_when_finished "git rm -f intend-to-add" &&
1669        test_must_fail git grep --quiet "intend to add this" &&
1670        test_must_fail git grep --quiet --cached "intend to add this" &&
1671        test_must_fail git grep --quiet "intend to add this" HEAD
1672'
1673
1674test_expect_success 'grep can find things only in the index' '
1675        echo "only in the index" >cache-this &&
1676        git add cache-this &&
1677        rm cache-this &&
1678        test_when_finished "git rm --cached cache-this" &&
1679        test_must_fail git grep --quiet "only in the index" &&
1680        git grep --quiet --cached "only in the index" &&
1681        test_must_fail git grep --quiet "only in the index" HEAD
1682'
1683
1684test_expect_success 'grep does not report i-t-a with -L --cached' '
1685        echo "intend to add this" >intend-to-add &&
1686        git add -N intend-to-add &&
1687        test_when_finished "git rm -f intend-to-add" &&
1688        git ls-files | grep -v "^intend-to-add\$" >expected &&
1689        git grep -L --cached "nonexistent_string" >actual &&
1690        test_cmp expected actual
1691'
1692
1693test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
1694        echo "intend to add this" >intend-to-add-assume-unchanged &&
1695        git add -N intend-to-add-assume-unchanged &&
1696        test_when_finished "git rm -f intend-to-add-assume-unchanged" &&
1697        git update-index --assume-unchanged intend-to-add-assume-unchanged &&
1698        git ls-files | grep -v "^intend-to-add-assume-unchanged\$" >expected &&
1699        git grep -L "nonexistent_string" >actual &&
1700        test_cmp expected actual
1701'
1702
1703test_done