t / t7002-grep.shon commit Fix mis-backport of t7002 (8de6518)
   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
 139done
 140
 141cat >expected <<EOF
 142file:foo mmap bar_mmap
 143EOF
 144
 145test_expect_success 'grep -e A --and -e B' '
 146        git grep -e "foo mmap" --and -e bar_mmap >actual &&
 147        test_cmp expected actual
 148'
 149
 150cat >expected <<EOF
 151file:foo_mmap bar mmap
 152file:foo_mmap bar mmap baz
 153EOF
 154
 155
 156test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
 157        git grep \( -e foo_ --or -e baz \) \
 158                --and -e " mmap" >actual &&
 159        test_cmp expected actual
 160'
 161
 162cat >expected <<EOF
 163file:foo mmap bar
 164EOF
 165
 166test_expect_success 'grep -e A --and --not -e B' '
 167        git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
 168        test_cmp expected actual
 169'
 170
 171test_expect_success 'grep -f, non-existent file' '
 172        test_must_fail git grep -f patterns
 173'
 174
 175cat >expected <<EOF
 176file:foo mmap bar
 177file:foo_mmap bar
 178file:foo_mmap bar mmap
 179file:foo mmap bar_mmap
 180file:foo_mmap bar mmap baz
 181EOF
 182
 183cat >pattern <<EOF
 184mmap
 185EOF
 186
 187test_expect_success 'grep -f, one pattern' '
 188        git grep -f pattern >actual &&
 189        test_cmp expected actual
 190'
 191
 192cat >expected <<EOF
 193file:foo mmap bar
 194file:foo_mmap bar
 195file:foo_mmap bar mmap
 196file:foo mmap bar_mmap
 197file:foo_mmap bar mmap baz
 198t/a/v:vvv
 199t/v:vvv
 200v:vvv
 201EOF
 202
 203cat >patterns <<EOF
 204mmap
 205vvv
 206EOF
 207
 208test_expect_success 'grep -f, multiple patterns' '
 209        git grep -f patterns >actual &&
 210        test_cmp expected actual
 211'
 212
 213cat >expected <<EOF
 214file:foo mmap bar
 215file:foo_mmap bar
 216file:foo_mmap bar mmap
 217file:foo mmap bar_mmap
 218file:foo_mmap bar mmap baz
 219t/a/v:vvv
 220t/v:vvv
 221v:vvv
 222EOF
 223
 224cat >patterns <<EOF
 225
 226mmap
 227
 228vvv
 229
 230EOF
 231
 232test_expect_success 'grep -f, ignore empty lines' '
 233        git grep -f patterns >actual &&
 234        test_cmp expected actual
 235'
 236
 237cat >expected <<EOF
 238y:y yy
 239--
 240z:zzz
 241EOF
 242
 243# Create 1024 file names that sort between "y" and "z" to make sure
 244# the two files are handled by different calls to an external grep.
 245# This depends on MAXARGS in builtin-grep.c being 1024 or less.
 246c32="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"
 247test_expect_success 'grep -C1, hunk mark between files' '
 248        for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
 249        git add y-?? &&
 250        git grep -C1 "^[yz]" >actual &&
 251        test_cmp expected actual
 252'
 253
 254test_expect_success 'grep -C1 --no-ext-grep, hunk mark between files' '
 255        git grep -C1 --no-ext-grep "^[yz]" >actual &&
 256        test_cmp expected actual
 257'
 258
 259test_expect_success 'log grep setup' '
 260        echo a >>file &&
 261        test_tick &&
 262        GIT_AUTHOR_NAME="With * Asterisk" \
 263        GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
 264        git commit -a -m "second" &&
 265
 266        echo a >>file &&
 267        test_tick &&
 268        git commit -a -m "third"
 269
 270'
 271
 272test_expect_success 'log grep (1)' '
 273        git log --author=author --pretty=tformat:%s >actual &&
 274        ( echo third ; echo initial ) >expect &&
 275        test_cmp expect actual
 276'
 277
 278test_expect_success 'log grep (2)' '
 279        git log --author=" * " -F --pretty=tformat:%s >actual &&
 280        ( echo second ) >expect &&
 281        test_cmp expect actual
 282'
 283
 284test_expect_success 'log grep (3)' '
 285        git log --author="^A U" --pretty=tformat:%s >actual &&
 286        ( echo third ; echo initial ) >expect &&
 287        test_cmp expect actual
 288'
 289
 290test_expect_success 'log grep (4)' '
 291        git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
 292        ( echo second ) >expect &&
 293        test_cmp expect actual
 294'
 295
 296test_expect_success 'log grep (5)' '
 297        git log --author=Thor -F --grep=Thu --pretty=tformat:%s >actual &&
 298        ( echo third ; echo initial ) >expect &&
 299        test_cmp expect actual
 300'
 301
 302test_expect_success 'log grep (6)' '
 303        git log --author=-0700  --pretty=tformat:%s >actual &&
 304        >expect &&
 305        test_cmp expect actual
 306'
 307
 308test_expect_success 'grep with CE_VALID file' '
 309        git update-index --assume-unchanged t/t &&
 310        rm t/t &&
 311        test "$(git grep --no-ext-grep test)" = "t/t:test" &&
 312        git update-index --no-assume-unchanged t/t &&
 313        git checkout t/t
 314'
 315
 316cat >expected <<EOF
 317hello.c=#include <stdio.h>
 318hello.c:        return 0;
 319EOF
 320
 321test_expect_success 'grep -p with userdiff' '
 322        git config diff.custom.funcname "^#" &&
 323        echo "hello.c diff=custom" >.gitattributes &&
 324        git grep -p return >actual &&
 325        test_cmp expected actual
 326'
 327
 328cat >expected <<EOF
 329hello.c=int main(int argc, const char **argv)
 330hello.c:        return 0;
 331EOF
 332
 333test_expect_success 'grep -p' '
 334        rm -f .gitattributes &&
 335        git grep -p return >actual &&
 336        test_cmp expected actual
 337'
 338
 339cat >expected <<EOF
 340hello.c-#include <stdio.h>
 341hello.c=int main(int argc, const char **argv)
 342hello.c-{
 343hello.c-        printf("Hello world.\n");
 344hello.c:        return 0;
 345EOF
 346
 347test_expect_success 'grep -p -B5' '
 348        git grep -p -B5 return >actual &&
 349        test_cmp expected actual
 350'
 351
 352test_expect_success 'grep from a subdirectory to search wider area (1)' '
 353        mkdir -p s &&
 354        (
 355                cd s && git grep "x x x" ..
 356        )
 357'
 358
 359test_expect_success 'grep from a subdirectory to search wider area (2)' '
 360        mkdir -p s &&
 361        (
 362                cd s || exit 1
 363                ( git grep xxyyzz .. >out ; echo $? >status )
 364                ! test -s out &&
 365                test 1 = $(cat status)
 366        )
 367'
 368
 369test_done