t / t7002-grep.shon commit Merge branch 'maint-1.6.3' into maint (f0df129)
   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 ww w >w &&
  29        echo x x xx x >x &&
  30        echo y yy >y &&
  31        echo zzz > z &&
  32        mkdir t &&
  33        echo test >t/t &&
  34        git add file w x y z t/t hello.c &&
  35        test_tick &&
  36        git commit -m initial
  37'
  38
  39test_expect_success 'grep should not segfault with a bad input' '
  40        test_must_fail git grep "("
  41'
  42
  43for H in HEAD ''
  44do
  45        case "$H" in
  46        HEAD)   HC='HEAD:' L='HEAD' ;;
  47        '')     HC= L='in working tree' ;;
  48        esac
  49
  50        test_expect_success "grep -w $L" '
  51                {
  52                        echo ${HC}file:1:foo mmap bar
  53                        echo ${HC}file:3:foo_mmap bar mmap
  54                        echo ${HC}file:4:foo mmap bar_mmap
  55                        echo ${HC}file:5:foo_mmap bar mmap baz
  56                } >expected &&
  57                git grep -n -w -e mmap $H >actual &&
  58                diff expected actual
  59        '
  60
  61        test_expect_success "grep -w $L (w)" '
  62                : >expected &&
  63                ! git grep -n -w -e "^w" >actual &&
  64                test_cmp expected actual
  65        '
  66
  67        test_expect_success "grep -w $L (x)" '
  68                {
  69                        echo ${HC}x:1:x x xx x
  70                } >expected &&
  71                git grep -n -w -e "x xx* x" $H >actual &&
  72                diff expected actual
  73        '
  74
  75        test_expect_success "grep -w $L (y-1)" '
  76                {
  77                        echo ${HC}y:1:y yy
  78                } >expected &&
  79                git grep -n -w -e "^y" $H >actual &&
  80                diff expected actual
  81        '
  82
  83        test_expect_success "grep -w $L (y-2)" '
  84                : >expected &&
  85                if git grep -n -w -e "^y y" $H >actual
  86                then
  87                        echo should not have matched
  88                        cat actual
  89                        false
  90                else
  91                        diff expected actual
  92                fi
  93        '
  94
  95        test_expect_success "grep -w $L (z)" '
  96                : >expected &&
  97                if git grep -n -w -e "^z" $H >actual
  98                then
  99                        echo should not have matched
 100                        cat actual
 101                        false
 102                else
 103                        diff expected actual
 104                fi
 105        '
 106
 107        test_expect_success "grep $L (t-1)" '
 108                echo "${HC}t/t:1:test" >expected &&
 109                git grep -n -e test $H >actual &&
 110                diff expected actual
 111        '
 112
 113        test_expect_success "grep $L (t-2)" '
 114                echo "${HC}t:1:test" >expected &&
 115                (
 116                        cd t &&
 117                        git grep -n -e test $H
 118                ) >actual &&
 119                diff expected actual
 120        '
 121
 122        test_expect_success "grep $L (t-3)" '
 123                echo "${HC}t/t:1:test" >expected &&
 124                (
 125                        cd t &&
 126                        git grep --full-name -n -e test $H
 127                ) >actual &&
 128                diff expected actual
 129        '
 130
 131        test_expect_success "grep -c $L (no /dev/null)" '
 132                ! git grep -c test $H | grep /dev/null
 133        '
 134
 135done
 136
 137cat >expected <<EOF
 138file:foo mmap bar_mmap
 139EOF
 140
 141test_expect_success 'grep -e A --and -e B' '
 142        git grep -e "foo mmap" --and -e bar_mmap >actual &&
 143        test_cmp expected actual
 144'
 145
 146cat >expected <<EOF
 147file:foo_mmap bar mmap
 148file:foo_mmap bar mmap baz
 149EOF
 150
 151
 152test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
 153        git grep \( -e foo_ --or -e baz \) \
 154                --and -e " mmap" >actual &&
 155        test_cmp expected actual
 156'
 157
 158cat >expected <<EOF
 159file:foo mmap bar
 160EOF
 161
 162test_expect_success 'grep -e A --and --not -e B' '
 163        git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
 164        test_cmp expected actual
 165'
 166
 167cat >expected <<EOF
 168y:y yy
 169--
 170z:zzz
 171EOF
 172
 173# Create 1024 file names that sort between "y" and "z" to make sure
 174# the two files are handled by different calls to an external grep.
 175# This depends on MAXARGS in builtin-grep.c being 1024 or less.
 176c32="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"
 177test_expect_success 'grep -C1, hunk mark between files' '
 178        for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
 179        git add y-?? &&
 180        git grep -C1 "^[yz]" >actual &&
 181        test_cmp expected actual
 182'
 183
 184test_expect_success 'grep -C1 --no-ext-grep, hunk mark between files' '
 185        git grep -C1 --no-ext-grep "^[yz]" >actual &&
 186        test_cmp expected actual
 187'
 188
 189test_expect_success 'log grep setup' '
 190        echo a >>file &&
 191        test_tick &&
 192        GIT_AUTHOR_NAME="With * Asterisk" \
 193        GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
 194        git commit -a -m "second" &&
 195
 196        echo a >>file &&
 197        test_tick &&
 198        git commit -a -m "third"
 199
 200'
 201
 202test_expect_success 'log grep (1)' '
 203        git log --author=author --pretty=tformat:%s >actual &&
 204        ( echo third ; echo initial ) >expect &&
 205        test_cmp expect actual
 206'
 207
 208test_expect_success 'log grep (2)' '
 209        git log --author=" * " -F --pretty=tformat:%s >actual &&
 210        ( echo second ) >expect &&
 211        test_cmp expect actual
 212'
 213
 214test_expect_success 'log grep (3)' '
 215        git log --author="^A U" --pretty=tformat:%s >actual &&
 216        ( echo third ; echo initial ) >expect &&
 217        test_cmp expect actual
 218'
 219
 220test_expect_success 'log grep (4)' '
 221        git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
 222        ( echo second ) >expect &&
 223        test_cmp expect actual
 224'
 225
 226test_expect_success 'log grep (5)' '
 227        git log --author=Thor -F --grep=Thu --pretty=tformat:%s >actual &&
 228        ( echo third ; echo initial ) >expect &&
 229        test_cmp expect actual
 230'
 231
 232test_expect_success 'log grep (6)' '
 233        git log --author=-0700  --pretty=tformat:%s >actual &&
 234        >expect &&
 235        test_cmp expect actual
 236'
 237
 238test_expect_success 'grep with CE_VALID file' '
 239        git update-index --assume-unchanged t/t &&
 240        rm t/t &&
 241        test "$(git grep --no-ext-grep test)" = "t/t:test" &&
 242        git update-index --no-assume-unchanged t/t &&
 243        git checkout t/t
 244'
 245
 246cat >expected <<EOF
 247hello.c=#include <stdio.h>
 248hello.c:        return 0;
 249EOF
 250
 251test_expect_success 'grep -p with userdiff' '
 252        git config diff.custom.funcname "^#" &&
 253        echo "hello.c diff=custom" >.gitattributes &&
 254        git grep -p return >actual &&
 255        test_cmp expected actual
 256'
 257
 258cat >expected <<EOF
 259hello.c=int main(int argc, const char **argv)
 260hello.c:        return 0;
 261EOF
 262
 263test_expect_success 'grep -p' '
 264        rm -f .gitattributes &&
 265        git grep -p return >actual &&
 266        test_cmp expected actual
 267'
 268
 269cat >expected <<EOF
 270hello.c-#include <stdio.h>
 271hello.c=int main(int argc, const char **argv)
 272hello.c-{
 273hello.c-        printf("Hello world.\n");
 274hello.c:        return 0;
 275EOF
 276
 277test_expect_success 'grep -p -B5' '
 278        git grep -p -B5 return >actual &&
 279        test_cmp expected actual
 280'
 281
 282test_done