t / t7002-grep.shon commit Merge branch 'hm/imap-send-cram-md5' (39914cb)
   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                diff expected actual
  64        '
  65
  66        test_expect_success "grep -w $L (w)" '
  67                : >expected &&
  68                ! 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                diff 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                diff 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                        diff 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                        diff 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                diff 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                diff 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                diff 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
 185done
 186
 187cat >expected <<EOF
 188file:foo mmap bar_mmap
 189EOF
 190
 191test_expect_success 'grep -e A --and -e B' '
 192        git grep -e "foo mmap" --and -e bar_mmap >actual &&
 193        test_cmp expected actual
 194'
 195
 196cat >expected <<EOF
 197file:foo_mmap bar mmap
 198file:foo_mmap bar mmap baz
 199EOF
 200
 201
 202test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
 203        git grep \( -e foo_ --or -e baz \) \
 204                --and -e " mmap" >actual &&
 205        test_cmp expected actual
 206'
 207
 208cat >expected <<EOF
 209file:foo mmap bar
 210EOF
 211
 212test_expect_success 'grep -e A --and --not -e B' '
 213        git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
 214        test_cmp expected actual
 215'
 216
 217test_expect_success 'grep should ignore GREP_OPTIONS' '
 218        GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
 219        test_cmp expected actual
 220'
 221
 222test_expect_success 'grep -f, non-existent file' '
 223        test_must_fail git grep -f patterns
 224'
 225
 226cat >expected <<EOF
 227file:foo mmap bar
 228file:foo_mmap bar
 229file:foo_mmap bar mmap
 230file:foo mmap bar_mmap
 231file:foo_mmap bar mmap baz
 232EOF
 233
 234cat >pattern <<EOF
 235mmap
 236EOF
 237
 238test_expect_success 'grep -f, one pattern' '
 239        git grep -f pattern >actual &&
 240        test_cmp expected actual
 241'
 242
 243cat >expected <<EOF
 244file:foo mmap bar
 245file:foo_mmap bar
 246file:foo_mmap bar mmap
 247file:foo mmap bar_mmap
 248file:foo_mmap bar mmap baz
 249t/a/v:vvv
 250t/v:vvv
 251v:vvv
 252EOF
 253
 254cat >patterns <<EOF
 255mmap
 256vvv
 257EOF
 258
 259test_expect_success 'grep -f, multiple patterns' '
 260        git grep -f patterns >actual &&
 261        test_cmp expected actual
 262'
 263
 264cat >expected <<EOF
 265file:foo mmap bar
 266file:foo_mmap bar
 267file:foo_mmap bar mmap
 268file:foo mmap bar_mmap
 269file:foo_mmap bar mmap baz
 270t/a/v:vvv
 271t/v:vvv
 272v:vvv
 273EOF
 274
 275cat >patterns <<EOF
 276
 277mmap
 278
 279vvv
 280
 281EOF
 282
 283test_expect_success 'grep -f, ignore empty lines' '
 284        git grep -f patterns >actual &&
 285        test_cmp expected actual
 286'
 287
 288cat >expected <<EOF
 289y:y yy
 290--
 291z:zzz
 292EOF
 293
 294test_expect_success 'grep -q, silently report matches' '
 295        >empty &&
 296        git grep -q mmap >actual &&
 297        test_cmp empty actual &&
 298        test_must_fail git grep -q qfwfq >actual &&
 299        test_cmp empty actual
 300'
 301
 302# Create 1024 file names that sort between "y" and "z" to make sure
 303# the two files are handled by different calls to an external grep.
 304# This depends on MAXARGS in builtin-grep.c being 1024 or less.
 305c32="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"
 306test_expect_success 'grep -C1, hunk mark between files' '
 307        for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
 308        git add y-?? &&
 309        git grep -C1 "^[yz]" >actual &&
 310        test_cmp expected actual
 311'
 312
 313test_expect_success 'grep -C1 hunk mark between files' '
 314        git grep -C1 "^[yz]" >actual &&
 315        test_cmp expected actual
 316'
 317
 318test_expect_success 'log grep setup' '
 319        echo a >>file &&
 320        test_tick &&
 321        GIT_AUTHOR_NAME="With * Asterisk" \
 322        GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
 323        git commit -a -m "second" &&
 324
 325        echo a >>file &&
 326        test_tick &&
 327        git commit -a -m "third"
 328
 329'
 330
 331test_expect_success 'log grep (1)' '
 332        git log --author=author --pretty=tformat:%s >actual &&
 333        ( echo third ; echo initial ) >expect &&
 334        test_cmp expect actual
 335'
 336
 337test_expect_success 'log grep (2)' '
 338        git log --author=" * " -F --pretty=tformat:%s >actual &&
 339        ( echo second ) >expect &&
 340        test_cmp expect actual
 341'
 342
 343test_expect_success 'log grep (3)' '
 344        git log --author="^A U" --pretty=tformat:%s >actual &&
 345        ( echo third ; echo initial ) >expect &&
 346        test_cmp expect actual
 347'
 348
 349test_expect_success 'log grep (4)' '
 350        git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
 351        ( echo second ) >expect &&
 352        test_cmp expect actual
 353'
 354
 355test_expect_success 'log grep (5)' '
 356        git log --author=Thor -F --pretty=tformat:%s >actual &&
 357        ( echo third ; echo initial ) >expect &&
 358        test_cmp expect actual
 359'
 360
 361test_expect_success 'log grep (6)' '
 362        git log --author=-0700  --pretty=tformat:%s >actual &&
 363        >expect &&
 364        test_cmp expect actual
 365'
 366
 367test_expect_success 'log --grep --author implicitly uses all-match' '
 368        # grep matches initial and second but not third
 369        # author matches only initial and third
 370        git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
 371        echo initial >expect &&
 372        test_cmp expect actual
 373'
 374
 375test_expect_success 'grep with CE_VALID file' '
 376        git update-index --assume-unchanged t/t &&
 377        rm t/t &&
 378        test "$(git grep test)" = "t/t:test" &&
 379        git update-index --no-assume-unchanged t/t &&
 380        git checkout t/t
 381'
 382
 383cat >expected <<EOF
 384hello.c=#include <stdio.h>
 385hello.c:        return 0;
 386EOF
 387
 388test_expect_success 'grep -p with userdiff' '
 389        git config diff.custom.funcname "^#" &&
 390        echo "hello.c diff=custom" >.gitattributes &&
 391        git grep -p return >actual &&
 392        test_cmp expected actual
 393'
 394
 395cat >expected <<EOF
 396hello.c=int main(int argc, const char **argv)
 397hello.c:        return 0;
 398EOF
 399
 400test_expect_success 'grep -p' '
 401        rm -f .gitattributes &&
 402        git grep -p return >actual &&
 403        test_cmp expected actual
 404'
 405
 406cat >expected <<EOF
 407hello.c-#include <stdio.h>
 408hello.c=int main(int argc, const char **argv)
 409hello.c-{
 410hello.c-        printf("Hello world.\n");
 411hello.c:        return 0;
 412EOF
 413
 414test_expect_success 'grep -p -B5' '
 415        git grep -p -B5 return >actual &&
 416        test_cmp expected actual
 417'
 418
 419test_expect_success 'grep from a subdirectory to search wider area (1)' '
 420        mkdir -p s &&
 421        (
 422                cd s && git grep "x x x" ..
 423        )
 424'
 425
 426test_expect_success 'grep from a subdirectory to search wider area (2)' '
 427        mkdir -p s &&
 428        (
 429                cd s || exit 1
 430                ( git grep xxyyzz .. >out ; echo $? >status )
 431                ! test -s out &&
 432                test 1 = $(cat status)
 433        )
 434'
 435
 436cat >expected <<EOF
 437hello.c:int main(int argc, const char **argv)
 438EOF
 439
 440test_expect_success 'grep -Fi' '
 441        git grep -Fi "CHAR *" >actual &&
 442        test_cmp expected actual
 443'
 444
 445test_expect_success 'outside of git repository' '
 446        rm -fr non &&
 447        mkdir -p non/git/sub &&
 448        echo hello >non/git/file1 &&
 449        echo world >non/git/sub/file2 &&
 450        echo ".*o*" >non/git/.gitignore &&
 451        {
 452                echo file1:hello &&
 453                echo sub/file2:world
 454        } >non/expect.full &&
 455        echo file2:world >non/expect.sub
 456        (
 457                GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
 458                export GIT_CEILING_DIRECTORIES &&
 459                cd non/git &&
 460                test_must_fail git grep o &&
 461                git grep --no-index o >../actual.full &&
 462                test_cmp ../expect.full ../actual.full
 463                cd sub &&
 464                test_must_fail git grep o &&
 465                git grep --no-index o >../../actual.sub &&
 466                test_cmp ../../expect.sub ../../actual.sub
 467        )
 468'
 469
 470test_expect_success 'inside git repository but with --no-index' '
 471        rm -fr is &&
 472        mkdir -p is/git/sub &&
 473        echo hello >is/git/file1 &&
 474        echo world >is/git/sub/file2 &&
 475        echo ".*o*" >is/git/.gitignore &&
 476        {
 477                echo file1:hello &&
 478                echo sub/file2:world
 479        } >is/expect.full &&
 480        : >is/expect.empty &&
 481        echo file2:world >is/expect.sub
 482        (
 483                cd is/git &&
 484                git init &&
 485                test_must_fail git grep o >../actual.full &&
 486                test_cmp ../expect.empty ../actual.full &&
 487                git grep --no-index o >../actual.full &&
 488                test_cmp ../expect.full ../actual.full &&
 489                cd sub &&
 490                test_must_fail git grep o >../../actual.sub &&
 491                test_cmp ../../expect.empty ../../actual.sub &&
 492                git grep --no-index o >../../actual.sub &&
 493                test_cmp ../../expect.sub ../../actual.sub
 494        )
 495'
 496
 497test_expect_success 'setup double-dash tests' '
 498cat >double-dash <<EOF &&
 499--
 500->
 501other
 502EOF
 503git add double-dash
 504'
 505
 506cat >expected <<EOF
 507double-dash:->
 508EOF
 509test_expect_success 'grep -- pattern' '
 510        git grep -- "->" >actual &&
 511        test_cmp expected actual
 512'
 513test_expect_success 'grep -- pattern -- pathspec' '
 514        git grep -- "->" -- double-dash >actual &&
 515        test_cmp expected actual
 516'
 517test_expect_success 'grep -e pattern -- path' '
 518        git grep -e "->" -- double-dash >actual &&
 519        test_cmp expected actual
 520'
 521
 522cat >expected <<EOF
 523double-dash:--
 524EOF
 525test_expect_success 'grep -e -- -- path' '
 526        git grep -e -- -- double-dash >actual &&
 527        test_cmp expected actual
 528'
 529
 530test_done