t / t7002-grep.shon commit Merge branch 'th/maint-remote-update-help-string' into maint (d718c19)
   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 -f, non-existent file' '
 218        test_must_fail git grep -f patterns
 219'
 220
 221cat >expected <<EOF
 222file:foo mmap bar
 223file:foo_mmap bar
 224file:foo_mmap bar mmap
 225file:foo mmap bar_mmap
 226file:foo_mmap bar mmap baz
 227EOF
 228
 229cat >pattern <<EOF
 230mmap
 231EOF
 232
 233test_expect_success 'grep -f, one pattern' '
 234        git grep -f pattern >actual &&
 235        test_cmp expected actual
 236'
 237
 238cat >expected <<EOF
 239file:foo mmap bar
 240file:foo_mmap bar
 241file:foo_mmap bar mmap
 242file:foo mmap bar_mmap
 243file:foo_mmap bar mmap baz
 244t/a/v:vvv
 245t/v:vvv
 246v:vvv
 247EOF
 248
 249cat >patterns <<EOF
 250mmap
 251vvv
 252EOF
 253
 254test_expect_success 'grep -f, multiple patterns' '
 255        git grep -f patterns >actual &&
 256        test_cmp expected actual
 257'
 258
 259cat >expected <<EOF
 260file:foo mmap bar
 261file:foo_mmap bar
 262file:foo_mmap bar mmap
 263file:foo mmap bar_mmap
 264file:foo_mmap bar mmap baz
 265t/a/v:vvv
 266t/v:vvv
 267v:vvv
 268EOF
 269
 270cat >patterns <<EOF
 271
 272mmap
 273
 274vvv
 275
 276EOF
 277
 278test_expect_success 'grep -f, ignore empty lines' '
 279        git grep -f patterns >actual &&
 280        test_cmp expected actual
 281'
 282
 283cat >expected <<EOF
 284y:y yy
 285--
 286z:zzz
 287EOF
 288
 289# Create 1024 file names that sort between "y" and "z" to make sure
 290# the two files are handled by different calls to an external grep.
 291# This depends on MAXARGS in builtin-grep.c being 1024 or less.
 292c32="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"
 293test_expect_success 'grep -C1, hunk mark between files' '
 294        for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
 295        git add y-?? &&
 296        git grep -C1 "^[yz]" >actual &&
 297        test_cmp expected actual
 298'
 299
 300test_expect_success 'grep -C1 --no-ext-grep, hunk mark between files' '
 301        git grep -C1 --no-ext-grep "^[yz]" >actual &&
 302        test_cmp expected actual
 303'
 304
 305test_expect_success 'log grep setup' '
 306        echo a >>file &&
 307        test_tick &&
 308        GIT_AUTHOR_NAME="With * Asterisk" \
 309        GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
 310        git commit -a -m "second" &&
 311
 312        echo a >>file &&
 313        test_tick &&
 314        git commit -a -m "third"
 315
 316'
 317
 318test_expect_success 'log grep (1)' '
 319        git log --author=author --pretty=tformat:%s >actual &&
 320        ( echo third ; echo initial ) >expect &&
 321        test_cmp expect actual
 322'
 323
 324test_expect_success 'log grep (2)' '
 325        git log --author=" * " -F --pretty=tformat:%s >actual &&
 326        ( echo second ) >expect &&
 327        test_cmp expect actual
 328'
 329
 330test_expect_success 'log grep (3)' '
 331        git log --author="^A U" --pretty=tformat:%s >actual &&
 332        ( echo third ; echo initial ) >expect &&
 333        test_cmp expect actual
 334'
 335
 336test_expect_success 'log grep (4)' '
 337        git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
 338        ( echo second ) >expect &&
 339        test_cmp expect actual
 340'
 341
 342test_expect_success 'log grep (5)' '
 343        git log --author=Thor -F --grep=Thu --pretty=tformat:%s >actual &&
 344        ( echo third ; echo initial ) >expect &&
 345        test_cmp expect actual
 346'
 347
 348test_expect_success 'log grep (6)' '
 349        git log --author=-0700  --pretty=tformat:%s >actual &&
 350        >expect &&
 351        test_cmp expect actual
 352'
 353
 354test_expect_success 'grep with CE_VALID file' '
 355        git update-index --assume-unchanged t/t &&
 356        rm t/t &&
 357        test "$(git grep --no-ext-grep test)" = "t/t:test" &&
 358        git update-index --no-assume-unchanged t/t &&
 359        git checkout t/t
 360'
 361
 362cat >expected <<EOF
 363hello.c=#include <stdio.h>
 364hello.c:        return 0;
 365EOF
 366
 367test_expect_success 'grep -p with userdiff' '
 368        git config diff.custom.funcname "^#" &&
 369        echo "hello.c diff=custom" >.gitattributes &&
 370        git grep -p return >actual &&
 371        test_cmp expected actual
 372'
 373
 374cat >expected <<EOF
 375hello.c=int main(int argc, const char **argv)
 376hello.c:        return 0;
 377EOF
 378
 379test_expect_success 'grep -p' '
 380        rm -f .gitattributes &&
 381        git grep -p return >actual &&
 382        test_cmp expected actual
 383'
 384
 385cat >expected <<EOF
 386hello.c-#include <stdio.h>
 387hello.c=int main(int argc, const char **argv)
 388hello.c-{
 389hello.c-        printf("Hello world.\n");
 390hello.c:        return 0;
 391EOF
 392
 393test_expect_success 'grep -p -B5' '
 394        git grep -p -B5 return >actual &&
 395        test_cmp expected actual
 396'
 397
 398test_expect_success 'grep from a subdirectory to search wider area (1)' '
 399        mkdir -p s &&
 400        (
 401                cd s && git grep "x x x" ..
 402        )
 403'
 404
 405test_expect_success 'grep from a subdirectory to search wider area (2)' '
 406        mkdir -p s &&
 407        (
 408                cd s || exit 1
 409                ( git grep xxyyzz .. >out ; echo $? >status )
 410                ! test -s out &&
 411                test 1 = $(cat status)
 412        )
 413'
 414
 415cat >expected <<EOF
 416hello.c:int main(int argc, const char **argv)
 417EOF
 418
 419test_expect_success 'grep -Fi' '
 420        git grep -Fi "CHAR *" >actual &&
 421        test_cmp expected actual
 422'
 423
 424test_done