t / t7810-grep.shon commit Merge branch 'jn/maint-commit-missing-template' (5cb3c9b)
   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        echo vvv >v &&
  30        echo ww w >w &&
  31        echo x x xx x >x &&
  32        echo y yy >y &&
  33        echo zzz > z &&
  34        mkdir t &&
  35        echo test >t/t &&
  36        echo vvv >t/v &&
  37        mkdir t/a &&
  38        echo vvv >t/a/v &&
  39        git add . &&
  40        test_tick &&
  41        git commit -m initial
  42'
  43
  44test_expect_success 'grep should not segfault with a bad input' '
  45        test_must_fail git grep "("
  46'
  47
  48for H in HEAD ''
  49do
  50        case "$H" in
  51        HEAD)   HC='HEAD:' L='HEAD' ;;
  52        '')     HC= L='in working tree' ;;
  53        esac
  54
  55        test_expect_success "grep -w $L" '
  56                {
  57                        echo ${HC}file:1:foo mmap bar
  58                        echo ${HC}file:3:foo_mmap bar mmap
  59                        echo ${HC}file:4:foo mmap bar_mmap
  60                        echo ${HC}file:5:foo_mmap bar mmap baz
  61                } >expected &&
  62                git grep -n -w -e mmap $H >actual &&
  63                test_cmp expected actual
  64        '
  65
  66        test_expect_success "grep -w $L (w)" '
  67                : >expected &&
  68                test_must_fail git grep -n -w -e "^w" >actual &&
  69                test_cmp expected actual
  70        '
  71
  72        test_expect_success "grep -w $L (x)" '
  73                {
  74                        echo ${HC}x:1:x x xx x
  75                } >expected &&
  76                git grep -n -w -e "x xx* x" $H >actual &&
  77                test_cmp expected actual
  78        '
  79
  80        test_expect_success "grep -w $L (y-1)" '
  81                {
  82                        echo ${HC}y:1:y yy
  83                } >expected &&
  84                git grep -n -w -e "^y" $H >actual &&
  85                test_cmp expected actual
  86        '
  87
  88        test_expect_success "grep -w $L (y-2)" '
  89                : >expected &&
  90                if git grep -n -w -e "^y y" $H >actual
  91                then
  92                        echo should not have matched
  93                        cat actual
  94                        false
  95                else
  96                        test_cmp expected actual
  97                fi
  98        '
  99
 100        test_expect_success "grep -w $L (z)" '
 101                : >expected &&
 102                if git grep -n -w -e "^z" $H >actual
 103                then
 104                        echo should not have matched
 105                        cat actual
 106                        false
 107                else
 108                        test_cmp expected actual
 109                fi
 110        '
 111
 112        test_expect_success "grep $L (t-1)" '
 113                echo "${HC}t/t:1:test" >expected &&
 114                git grep -n -e test $H >actual &&
 115                test_cmp expected actual
 116        '
 117
 118        test_expect_success "grep $L (t-2)" '
 119                echo "${HC}t:1:test" >expected &&
 120                (
 121                        cd t &&
 122                        git grep -n -e test $H
 123                ) >actual &&
 124                test_cmp expected actual
 125        '
 126
 127        test_expect_success "grep $L (t-3)" '
 128                echo "${HC}t/t:1:test" >expected &&
 129                (
 130                        cd t &&
 131                        git grep --full-name -n -e test $H
 132                ) >actual &&
 133                test_cmp expected actual
 134        '
 135
 136        test_expect_success "grep -c $L (no /dev/null)" '
 137                ! git grep -c test $H | grep /dev/null
 138        '
 139
 140        test_expect_success "grep --max-depth -1 $L" '
 141                {
 142                        echo ${HC}t/a/v:1:vvv
 143                        echo ${HC}t/v:1:vvv
 144                        echo ${HC}v:1:vvv
 145                } >expected &&
 146                git grep --max-depth -1 -n -e vvv $H >actual &&
 147                test_cmp expected actual
 148        '
 149
 150        test_expect_success "grep --max-depth 0 $L" '
 151                {
 152                        echo ${HC}v:1:vvv
 153                } >expected &&
 154                git grep --max-depth 0 -n -e vvv $H >actual &&
 155                test_cmp expected actual
 156        '
 157
 158        test_expect_success "grep --max-depth 0 -- '*' $L" '
 159                {
 160                        echo ${HC}t/a/v:1:vvv
 161                        echo ${HC}t/v:1:vvv
 162                        echo ${HC}v:1:vvv
 163                } >expected &&
 164                git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
 165                test_cmp expected actual
 166        '
 167
 168        test_expect_success "grep --max-depth 1 $L" '
 169                {
 170                        echo ${HC}t/v:1:vvv
 171                        echo ${HC}v:1:vvv
 172                } >expected &&
 173                git grep --max-depth 1 -n -e vvv $H >actual &&
 174                test_cmp expected actual
 175        '
 176
 177        test_expect_success "grep --max-depth 0 -- t $L" '
 178                {
 179                        echo ${HC}t/v:1:vvv
 180                } >expected &&
 181                git grep --max-depth 0 -n -e vvv $H -- t >actual &&
 182                test_cmp expected actual
 183        '
 184
 185        test_expect_success "grep --max-depth 0 -- . t $L" '
 186                {
 187                        echo ${HC}t/v:1:vvv
 188                        echo ${HC}v:1:vvv
 189                } >expected &&
 190                git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
 191                test_cmp expected actual
 192        '
 193
 194        test_expect_success "grep --max-depth 0 -- t . $L" '
 195                {
 196                        echo ${HC}t/v:1:vvv
 197                        echo ${HC}v:1:vvv
 198                } >expected &&
 199                git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
 200                test_cmp expected actual
 201        '
 202
 203done
 204
 205cat >expected <<EOF
 206file:foo mmap bar_mmap
 207EOF
 208
 209test_expect_success 'grep -e A --and -e B' '
 210        git grep -e "foo mmap" --and -e bar_mmap >actual &&
 211        test_cmp expected actual
 212'
 213
 214cat >expected <<EOF
 215file:foo_mmap bar mmap
 216file:foo_mmap bar mmap baz
 217EOF
 218
 219
 220test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
 221        git grep \( -e foo_ --or -e baz \) \
 222                --and -e " mmap" >actual &&
 223        test_cmp expected actual
 224'
 225
 226cat >expected <<EOF
 227file:foo mmap bar
 228EOF
 229
 230test_expect_success 'grep -e A --and --not -e B' '
 231        git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
 232        test_cmp expected actual
 233'
 234
 235test_expect_success 'grep should ignore GREP_OPTIONS' '
 236        GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
 237        test_cmp expected actual
 238'
 239
 240test_expect_success 'grep -f, non-existent file' '
 241        test_must_fail git grep -f patterns
 242'
 243
 244cat >expected <<EOF
 245file:foo mmap bar
 246file:foo_mmap bar
 247file:foo_mmap bar mmap
 248file:foo mmap bar_mmap
 249file:foo_mmap bar mmap baz
 250EOF
 251
 252cat >pattern <<EOF
 253mmap
 254EOF
 255
 256test_expect_success 'grep -f, one pattern' '
 257        git grep -f pattern >actual &&
 258        test_cmp expected actual
 259'
 260
 261cat >expected <<EOF
 262file:foo mmap bar
 263file:foo_mmap bar
 264file:foo_mmap bar mmap
 265file:foo mmap bar_mmap
 266file:foo_mmap bar mmap baz
 267t/a/v:vvv
 268t/v:vvv
 269v:vvv
 270EOF
 271
 272cat >patterns <<EOF
 273mmap
 274vvv
 275EOF
 276
 277test_expect_success 'grep -f, multiple patterns' '
 278        git grep -f patterns >actual &&
 279        test_cmp expected actual
 280'
 281
 282cat >expected <<EOF
 283file:foo mmap bar
 284file:foo_mmap bar
 285file:foo_mmap bar mmap
 286file:foo mmap bar_mmap
 287file:foo_mmap bar mmap baz
 288t/a/v:vvv
 289t/v:vvv
 290v:vvv
 291EOF
 292
 293cat >patterns <<EOF
 294
 295mmap
 296
 297vvv
 298
 299EOF
 300
 301test_expect_success 'grep -f, ignore empty lines' '
 302        git grep -f patterns >actual &&
 303        test_cmp expected actual
 304'
 305
 306cat >expected <<EOF
 307y:y yy
 308--
 309z:zzz
 310EOF
 311
 312test_expect_success 'grep -q, silently report matches' '
 313        >empty &&
 314        git grep -q mmap >actual &&
 315        test_cmp empty actual &&
 316        test_must_fail git grep -q qfwfq >actual &&
 317        test_cmp empty actual
 318'
 319
 320# Create 1024 file names that sort between "y" and "z" to make sure
 321# the two files are handled by different calls to an external grep.
 322# This depends on MAXARGS in builtin-grep.c being 1024 or less.
 323c32="0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v"
 324test_expect_success 'grep -C1, hunk mark between files' '
 325        for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
 326        git add y-?? &&
 327        git grep -C1 "^[yz]" >actual &&
 328        test_cmp expected actual
 329'
 330
 331test_expect_success 'grep -C1 hunk mark between files' '
 332        git grep -C1 "^[yz]" >actual &&
 333        test_cmp expected actual
 334'
 335
 336test_expect_success 'log grep setup' '
 337        echo a >>file &&
 338        test_tick &&
 339        GIT_AUTHOR_NAME="With * Asterisk" \
 340        GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
 341        git commit -a -m "second" &&
 342
 343        echo a >>file &&
 344        test_tick &&
 345        git commit -a -m "third" &&
 346
 347        echo a >>file &&
 348        test_tick &&
 349        GIT_AUTHOR_NAME="Night Fall" \
 350        GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
 351        git commit -a -m "fourth"
 352'
 353
 354test_expect_success 'log grep (1)' '
 355        git log --author=author --pretty=tformat:%s >actual &&
 356        ( echo third ; echo initial ) >expect &&
 357        test_cmp expect actual
 358'
 359
 360test_expect_success 'log grep (2)' '
 361        git log --author=" * " -F --pretty=tformat:%s >actual &&
 362        ( echo second ) >expect &&
 363        test_cmp expect actual
 364'
 365
 366test_expect_success 'log grep (3)' '
 367        git log --author="^A U" --pretty=tformat:%s >actual &&
 368        ( echo third ; echo initial ) >expect &&
 369        test_cmp expect actual
 370'
 371
 372test_expect_success 'log grep (4)' '
 373        git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
 374        ( echo second ) >expect &&
 375        test_cmp expect actual
 376'
 377
 378test_expect_success 'log grep (5)' '
 379        git log --author=Thor -F --pretty=tformat:%s >actual &&
 380        ( echo third ; echo initial ) >expect &&
 381        test_cmp expect actual
 382'
 383
 384test_expect_success 'log grep (6)' '
 385        git log --author=-0700  --pretty=tformat:%s >actual &&
 386        >expect &&
 387        test_cmp expect actual
 388'
 389
 390test_expect_success 'log --grep --author implicitly uses all-match' '
 391        # grep matches initial and second but not third
 392        # author matches only initial and third
 393        git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
 394        echo initial >expect &&
 395        test_cmp expect actual
 396'
 397
 398test_expect_success 'log with multiple --author uses union' '
 399        git log --author="Thor" --author="Aster" --format=%s >actual &&
 400        {
 401            echo third && echo second && echo initial
 402        } >expect &&
 403        test_cmp expect actual
 404'
 405
 406test_expect_success 'log with --grep and multiple --author uses all-match' '
 407        git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
 408        {
 409            echo third && echo initial
 410        } >expect &&
 411        test_cmp expect actual
 412'
 413
 414test_expect_success 'log with --grep and multiple --author uses all-match' '
 415        git log --author="Thor" --author="Night" --grep=q --format=%s >actual &&
 416        >expect &&
 417        test_cmp expect actual
 418'
 419
 420test_expect_success 'grep with CE_VALID file' '
 421        git update-index --assume-unchanged t/t &&
 422        rm t/t &&
 423        test "$(git grep test)" = "t/t:test" &&
 424        git update-index --no-assume-unchanged t/t &&
 425        git checkout t/t
 426'
 427
 428cat >expected <<EOF
 429hello.c=#include <stdio.h>
 430hello.c:        return 0;
 431EOF
 432
 433test_expect_success 'grep -p with userdiff' '
 434        git config diff.custom.funcname "^#" &&
 435        echo "hello.c diff=custom" >.gitattributes &&
 436        git grep -p return >actual &&
 437        test_cmp expected actual
 438'
 439
 440cat >expected <<EOF
 441hello.c=int main(int argc, const char **argv)
 442hello.c:        return 0;
 443EOF
 444
 445test_expect_success 'grep -p' '
 446        rm -f .gitattributes &&
 447        git grep -p return >actual &&
 448        test_cmp expected actual
 449'
 450
 451cat >expected <<EOF
 452hello.c-#include <stdio.h>
 453hello.c=int main(int argc, const char **argv)
 454hello.c-{
 455hello.c-        printf("Hello world.\n");
 456hello.c:        return 0;
 457EOF
 458
 459test_expect_success 'grep -p -B5' '
 460        git grep -p -B5 return >actual &&
 461        test_cmp expected actual
 462'
 463
 464test_expect_success 'grep from a subdirectory to search wider area (1)' '
 465        mkdir -p s &&
 466        (
 467                cd s && git grep "x x x" ..
 468        )
 469'
 470
 471test_expect_success 'grep from a subdirectory to search wider area (2)' '
 472        mkdir -p s &&
 473        (
 474                cd s || exit 1
 475                ( git grep xxyyzz .. >out ; echo $? >status )
 476                ! test -s out &&
 477                test 1 = $(cat status)
 478        )
 479'
 480
 481cat >expected <<EOF
 482hello.c:int main(int argc, const char **argv)
 483EOF
 484
 485test_expect_success 'grep -Fi' '
 486        git grep -Fi "CHAR *" >actual &&
 487        test_cmp expected actual
 488'
 489
 490test_expect_success 'outside of git repository' '
 491        rm -fr non &&
 492        mkdir -p non/git/sub &&
 493        echo hello >non/git/file1 &&
 494        echo world >non/git/sub/file2 &&
 495        echo ".*o*" >non/git/.gitignore &&
 496        {
 497                echo file1:hello &&
 498                echo sub/file2:world
 499        } >non/expect.full &&
 500        echo file2:world >non/expect.sub &&
 501        (
 502                GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
 503                export GIT_CEILING_DIRECTORIES &&
 504                cd non/git &&
 505                test_must_fail git grep o &&
 506                git grep --no-index o >../actual.full &&
 507                test_cmp ../expect.full ../actual.full
 508                cd sub &&
 509                test_must_fail git grep o &&
 510                git grep --no-index o >../../actual.sub &&
 511                test_cmp ../../expect.sub ../../actual.sub
 512        )
 513'
 514
 515test_expect_success 'inside git repository but with --no-index' '
 516        rm -fr is &&
 517        mkdir -p is/git/sub &&
 518        echo hello >is/git/file1 &&
 519        echo world >is/git/sub/file2 &&
 520        echo ".*o*" >is/git/.gitignore &&
 521        {
 522                echo file1:hello &&
 523                echo sub/file2:world
 524        } >is/expect.full &&
 525        : >is/expect.empty &&
 526        echo file2:world >is/expect.sub &&
 527        (
 528                cd is/git &&
 529                git init &&
 530                test_must_fail git grep o >../actual.full &&
 531                test_cmp ../expect.empty ../actual.full &&
 532                git grep --no-index o >../actual.full &&
 533                test_cmp ../expect.full ../actual.full &&
 534                cd sub &&
 535                test_must_fail git grep o >../../actual.sub &&
 536                test_cmp ../../expect.empty ../../actual.sub &&
 537                git grep --no-index o >../../actual.sub &&
 538                test_cmp ../../expect.sub ../../actual.sub
 539        )
 540'
 541
 542test_expect_success 'setup double-dash tests' '
 543cat >double-dash <<EOF &&
 544--
 545->
 546other
 547EOF
 548git add double-dash
 549'
 550
 551cat >expected <<EOF
 552double-dash:->
 553EOF
 554test_expect_success 'grep -- pattern' '
 555        git grep -- "->" >actual &&
 556        test_cmp expected actual
 557'
 558test_expect_success 'grep -- pattern -- pathspec' '
 559        git grep -- "->" -- double-dash >actual &&
 560        test_cmp expected actual
 561'
 562test_expect_success 'grep -e pattern -- path' '
 563        git grep -e "->" -- double-dash >actual &&
 564        test_cmp expected actual
 565'
 566
 567cat >expected <<EOF
 568double-dash:--
 569EOF
 570test_expect_success 'grep -e -- -- path' '
 571        git grep -e -- -- double-dash >actual &&
 572        test_cmp expected actual
 573'
 574
 575test_done