t / t7002-grep.shon commit Merge branch 'jk/maint-break-rename-reduce-memory' (a1b01c4)
   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}
  18EOF
  19
  20test_expect_success setup '
  21        {
  22                echo foo mmap bar
  23                echo foo_mmap bar
  24                echo foo_mmap bar mmap
  25                echo foo mmap bar_mmap
  26                echo foo_mmap bar mmap baz
  27        } >file &&
  28        echo vvv >v &&
  29        echo ww w >w &&
  30        echo x x xx x >x &&
  31        echo y yy >y &&
  32        echo zzz > z &&
  33        mkdir t &&
  34        echo test >t/t &&
  35        echo vvv >t/v &&
  36        mkdir t/a &&
  37        echo vvv >t/a/v &&
  38        git add . &&
  39        test_tick &&
  40        git commit -m initial
  41'
  42
  43test_expect_success 'grep should not segfault with a bad input' '
  44        test_must_fail git grep "("
  45'
  46
  47for H in HEAD ''
  48do
  49        case "$H" in
  50        HEAD)   HC='HEAD:' L='HEAD' ;;
  51        '')     HC= L='in working tree' ;;
  52        esac
  53
  54        test_expect_success "grep -w $L" '
  55                {
  56                        echo ${HC}file:1:foo mmap bar
  57                        echo ${HC}file:3:foo_mmap bar mmap
  58                        echo ${HC}file:4:foo mmap bar_mmap
  59                        echo ${HC}file:5:foo_mmap bar mmap baz
  60                } >expected &&
  61                git grep -n -w -e mmap $H >actual &&
  62                diff expected actual
  63        '
  64
  65        test_expect_success "grep -w $L (w)" '
  66                : >expected &&
  67                ! git grep -n -w -e "^w" >actual &&
  68                test_cmp expected actual
  69        '
  70
  71        test_expect_success "grep -w $L (x)" '
  72                {
  73                        echo ${HC}x:1:x x xx x
  74                } >expected &&
  75                git grep -n -w -e "x xx* x" $H >actual &&
  76                diff expected actual
  77        '
  78
  79        test_expect_success "grep -w $L (y-1)" '
  80                {
  81                        echo ${HC}y:1:y yy
  82                } >expected &&
  83                git grep -n -w -e "^y" $H >actual &&
  84                diff expected actual
  85        '
  86
  87        test_expect_success "grep -w $L (y-2)" '
  88                : >expected &&
  89                if git grep -n -w -e "^y y" $H >actual
  90                then
  91                        echo should not have matched
  92                        cat actual
  93                        false
  94                else
  95                        diff expected actual
  96                fi
  97        '
  98
  99        test_expect_success "grep -w $L (z)" '
 100                : >expected &&
 101                if git grep -n -w -e "^z" $H >actual
 102                then
 103                        echo should not have matched
 104                        cat actual
 105                        false
 106                else
 107                        diff expected actual
 108                fi
 109        '
 110
 111        test_expect_success "grep $L (t-1)" '
 112                echo "${HC}t/t:1:test" >expected &&
 113                git grep -n -e test $H >actual &&
 114                diff expected actual
 115        '
 116
 117        test_expect_success "grep $L (t-2)" '
 118                echo "${HC}t:1:test" >expected &&
 119                (
 120                        cd t &&
 121                        git grep -n -e test $H
 122                ) >actual &&
 123                diff expected actual
 124        '
 125
 126        test_expect_success "grep $L (t-3)" '
 127                echo "${HC}t/t:1:test" >expected &&
 128                (
 129                        cd t &&
 130                        git grep --full-name -n -e test $H
 131                ) >actual &&
 132                diff expected actual
 133        '
 134
 135        test_expect_success "grep -c $L (no /dev/null)" '
 136                ! git grep -c test $H | grep /dev/null
 137        '
 138
 139        test_expect_success "grep --max-depth -1 $L" '
 140                {
 141                        echo ${HC}t/a/v:1:vvv
 142                        echo ${HC}t/v:1:vvv
 143                        echo ${HC}v:1:vvv
 144                } >expected &&
 145                git grep --max-depth -1 -n -e vvv $H >actual &&
 146                test_cmp expected actual
 147        '
 148
 149        test_expect_success "grep --max-depth 0 $L" '
 150                {
 151                        echo ${HC}v:1:vvv
 152                } >expected &&
 153                git grep --max-depth 0 -n -e vvv $H >actual &&
 154                test_cmp expected actual
 155        '
 156
 157        test_expect_success "grep --max-depth 0 -- '*' $L" '
 158                {
 159                        echo ${HC}t/a/v:1:vvv
 160                        echo ${HC}t/v:1:vvv
 161                        echo ${HC}v:1:vvv
 162                } >expected &&
 163                git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
 164                test_cmp expected actual
 165        '
 166
 167        test_expect_success "grep --max-depth 1 $L" '
 168                {
 169                        echo ${HC}t/v:1:vvv
 170                        echo ${HC}v:1:vvv
 171                } >expected &&
 172                git grep --max-depth 1 -n -e vvv $H >actual &&
 173                test_cmp expected actual
 174        '
 175
 176        test_expect_success "grep --max-depth 0 -- t $L" '
 177                {
 178                        echo ${HC}t/v:1:vvv
 179                } >expected &&
 180                git grep --max-depth 0 -n -e vvv $H -- t >actual &&
 181                test_cmp expected actual
 182        '
 183
 184done
 185
 186cat >expected <<EOF
 187file:foo mmap bar_mmap
 188EOF
 189
 190test_expect_success 'grep -e A --and -e B' '
 191        git grep -e "foo mmap" --and -e bar_mmap >actual &&
 192        test_cmp expected actual
 193'
 194
 195cat >expected <<EOF
 196file:foo_mmap bar mmap
 197file:foo_mmap bar mmap baz
 198EOF
 199
 200
 201test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
 202        git grep \( -e foo_ --or -e baz \) \
 203                --and -e " mmap" >actual &&
 204        test_cmp expected actual
 205'
 206
 207cat >expected <<EOF
 208file:foo mmap bar
 209EOF
 210
 211test_expect_success 'grep -e A --and --not -e B' '
 212        git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
 213        test_cmp expected actual
 214'
 215
 216test_expect_success 'grep should ignore GREP_OPTIONS' '
 217        GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
 218        test_cmp expected actual
 219'
 220
 221test_expect_success 'grep -f, non-existent file' '
 222        test_must_fail git grep -f patterns
 223'
 224
 225cat >expected <<EOF
 226file:foo mmap bar
 227file:foo_mmap bar
 228file:foo_mmap bar mmap
 229file:foo mmap bar_mmap
 230file:foo_mmap bar mmap baz
 231EOF
 232
 233cat >pattern <<EOF
 234mmap
 235EOF
 236
 237test_expect_success 'grep -f, one pattern' '
 238        git grep -f pattern >actual &&
 239        test_cmp expected actual
 240'
 241
 242cat >expected <<EOF
 243file:foo mmap bar
 244file:foo_mmap bar
 245file:foo_mmap bar mmap
 246file:foo mmap bar_mmap
 247file:foo_mmap bar mmap baz
 248t/a/v:vvv
 249t/v:vvv
 250v:vvv
 251EOF
 252
 253cat >patterns <<EOF
 254mmap
 255vvv
 256EOF
 257
 258test_expect_success 'grep -f, multiple patterns' '
 259        git grep -f patterns >actual &&
 260        test_cmp expected actual
 261'
 262
 263cat >expected <<EOF
 264file:foo mmap bar
 265file:foo_mmap bar
 266file:foo_mmap bar mmap
 267file:foo mmap bar_mmap
 268file:foo_mmap bar mmap baz
 269t/a/v:vvv
 270t/v:vvv
 271v:vvv
 272EOF
 273
 274cat >patterns <<EOF
 275
 276mmap
 277
 278vvv
 279
 280EOF
 281
 282test_expect_success 'grep -f, ignore empty lines' '
 283        git grep -f patterns >actual &&
 284        test_cmp expected actual
 285'
 286
 287cat >expected <<EOF
 288y:y yy
 289--
 290z:zzz
 291EOF
 292
 293# Create 1024 file names that sort between "y" and "z" to make sure
 294# the two files are handled by different calls to an external grep.
 295# This depends on MAXARGS in builtin-grep.c being 1024 or less.
 296c32="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"
 297test_expect_success 'grep -C1, hunk mark between files' '
 298        for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
 299        git add y-?? &&
 300        git grep -C1 "^[yz]" >actual &&
 301        test_cmp expected actual
 302'
 303
 304test_expect_success 'grep -C1 --no-ext-grep, hunk mark between files' '
 305        git grep -C1 --no-ext-grep "^[yz]" >actual &&
 306        test_cmp expected actual
 307'
 308
 309test_expect_success 'log grep setup' '
 310        echo a >>file &&
 311        test_tick &&
 312        GIT_AUTHOR_NAME="With * Asterisk" \
 313        GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
 314        git commit -a -m "second" &&
 315
 316        echo a >>file &&
 317        test_tick &&
 318        git commit -a -m "third"
 319
 320'
 321
 322test_expect_success 'log grep (1)' '
 323        git log --author=author --pretty=tformat:%s >actual &&
 324        ( echo third ; echo initial ) >expect &&
 325        test_cmp expect actual
 326'
 327
 328test_expect_success 'log grep (2)' '
 329        git log --author=" * " -F --pretty=tformat:%s >actual &&
 330        ( echo second ) >expect &&
 331        test_cmp expect actual
 332'
 333
 334test_expect_success 'log grep (3)' '
 335        git log --author="^A U" --pretty=tformat:%s >actual &&
 336        ( echo third ; echo initial ) >expect &&
 337        test_cmp expect actual
 338'
 339
 340test_expect_success 'log grep (4)' '
 341        git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
 342        ( echo second ) >expect &&
 343        test_cmp expect actual
 344'
 345
 346test_expect_success 'log grep (5)' '
 347        git log --author=Thor -F --grep=Thu --pretty=tformat:%s >actual &&
 348        ( echo third ; echo initial ) >expect &&
 349        test_cmp expect actual
 350'
 351
 352test_expect_success 'log grep (6)' '
 353        git log --author=-0700  --pretty=tformat:%s >actual &&
 354        >expect &&
 355        test_cmp expect actual
 356'
 357
 358test_expect_success 'grep with CE_VALID file' '
 359        git update-index --assume-unchanged t/t &&
 360        rm t/t &&
 361        test "$(git grep --no-ext-grep test)" = "t/t:test" &&
 362        git update-index --no-assume-unchanged t/t &&
 363        git checkout t/t
 364'
 365
 366cat >expected <<EOF
 367hello.c=#include <stdio.h>
 368hello.c:        return 0;
 369EOF
 370
 371test_expect_success 'grep -p with userdiff' '
 372        git config diff.custom.funcname "^#" &&
 373        echo "hello.c diff=custom" >.gitattributes &&
 374        git grep -p return >actual &&
 375        test_cmp expected actual
 376'
 377
 378cat >expected <<EOF
 379hello.c=int main(int argc, const char **argv)
 380hello.c:        return 0;
 381EOF
 382
 383test_expect_success 'grep -p' '
 384        rm -f .gitattributes &&
 385        git grep -p return >actual &&
 386        test_cmp expected actual
 387'
 388
 389cat >expected <<EOF
 390hello.c-#include <stdio.h>
 391hello.c=int main(int argc, const char **argv)
 392hello.c-{
 393hello.c-        printf("Hello world.\n");
 394hello.c:        return 0;
 395EOF
 396
 397test_expect_success 'grep -p -B5' '
 398        git grep -p -B5 return >actual &&
 399        test_cmp expected actual
 400'
 401
 402test_expect_success 'grep from a subdirectory to search wider area (1)' '
 403        mkdir -p s &&
 404        (
 405                cd s && git grep "x x x" ..
 406        )
 407'
 408
 409test_expect_success 'grep from a subdirectory to search wider area (2)' '
 410        mkdir -p s &&
 411        (
 412                cd s || exit 1
 413                ( git grep xxyyzz .. >out ; echo $? >status )
 414                ! test -s out &&
 415                test 1 = $(cat status)
 416        )
 417'
 418
 419test_done