grep: print context hunk marks between files
[git/git.git] / t / t7002-grep.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Junio C Hamano
4 #
5
6 test_description='git grep various.
7 '
8
9 . ./test-lib.sh
10
11 test_expect_success setup '
12 {
13 echo foo mmap bar
14 echo foo_mmap bar
15 echo foo_mmap bar mmap
16 echo foo mmap bar_mmap
17 echo foo_mmap bar mmap baz
18 } >file &&
19 echo ww w >w &&
20 echo x x xx x >x &&
21 echo y yy >y &&
22 echo zzz > z &&
23 mkdir t &&
24 echo test >t/t &&
25 git add file w x y z t/t &&
26 test_tick &&
27 git commit -m initial
28 '
29
30 test_expect_success 'grep should not segfault with a bad input' '
31 test_must_fail git grep "("
32 '
33
34 for H in HEAD ''
35 do
36 case "$H" in
37 HEAD) HC='HEAD:' L='HEAD' ;;
38 '') HC= L='in working tree' ;;
39 esac
40
41 test_expect_success "grep -w $L" '
42 {
43 echo ${HC}file:1:foo mmap bar
44 echo ${HC}file:3:foo_mmap bar mmap
45 echo ${HC}file:4:foo mmap bar_mmap
46 echo ${HC}file:5:foo_mmap bar mmap baz
47 } >expected &&
48 git grep -n -w -e mmap $H >actual &&
49 diff expected actual
50 '
51
52 test_expect_success "grep -w $L (w)" '
53 : >expected &&
54 ! git grep -n -w -e "^w" >actual &&
55 test_cmp expected actual
56 '
57
58 test_expect_success "grep -w $L (x)" '
59 {
60 echo ${HC}x:1:x x xx x
61 } >expected &&
62 git grep -n -w -e "x xx* x" $H >actual &&
63 diff expected actual
64 '
65
66 test_expect_success "grep -w $L (y-1)" '
67 {
68 echo ${HC}y:1:y yy
69 } >expected &&
70 git grep -n -w -e "^y" $H >actual &&
71 diff expected actual
72 '
73
74 test_expect_success "grep -w $L (y-2)" '
75 : >expected &&
76 if git grep -n -w -e "^y y" $H >actual
77 then
78 echo should not have matched
79 cat actual
80 false
81 else
82 diff expected actual
83 fi
84 '
85
86 test_expect_success "grep -w $L (z)" '
87 : >expected &&
88 if git grep -n -w -e "^z" $H >actual
89 then
90 echo should not have matched
91 cat actual
92 false
93 else
94 diff expected actual
95 fi
96 '
97
98 test_expect_success "grep $L (t-1)" '
99 echo "${HC}t/t:1:test" >expected &&
100 git grep -n -e test $H >actual &&
101 diff expected actual
102 '
103
104 test_expect_success "grep $L (t-2)" '
105 echo "${HC}t:1:test" >expected &&
106 (
107 cd t &&
108 git grep -n -e test $H
109 ) >actual &&
110 diff expected actual
111 '
112
113 test_expect_success "grep $L (t-3)" '
114 echo "${HC}t/t:1:test" >expected &&
115 (
116 cd t &&
117 git grep --full-name -n -e test $H
118 ) >actual &&
119 diff expected actual
120 '
121
122 test_expect_success "grep -c $L (no /dev/null)" '
123 ! git grep -c test $H | grep /dev/null
124 '
125
126 done
127
128 cat >expected <<EOF
129 file:foo mmap bar_mmap
130 EOF
131
132 test_expect_success 'grep -e A --and -e B' '
133 git grep -e "foo mmap" --and -e bar_mmap >actual &&
134 test_cmp expected actual
135 '
136
137 cat >expected <<EOF
138 file:foo_mmap bar mmap
139 file:foo_mmap bar mmap baz
140 EOF
141
142
143 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
144 git grep \( -e foo_ --or -e baz \) \
145 --and -e " mmap" >actual &&
146 test_cmp expected actual
147 '
148
149 cat >expected <<EOF
150 file:foo mmap bar
151 EOF
152
153 test_expect_success 'grep -e A --and --not -e B' '
154 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
155 test_cmp expected actual
156 '
157
158 cat >expected <<EOF
159 y:y yy
160 --
161 z:zzz
162 EOF
163
164 # Create 1024 file names that sort between "y" and "z" to make sure
165 # the two files are handled by different calls to an external grep.
166 # This depends on MAXARGS in builtin-grep.c being 1024 or less.
167 c32="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"
168 test_expect_success 'grep -C1, hunk mark between files' '
169 for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
170 git add y-?? &&
171 git grep -C1 "^[yz]" >actual &&
172 test_cmp expected actual
173 '
174
175 test_expect_success 'grep -C1 --no-ext-grep, hunk mark between files' '
176 git grep -C1 --no-ext-grep "^[yz]" >actual &&
177 test_cmp expected actual
178 '
179
180 test_expect_success 'log grep setup' '
181 echo a >>file &&
182 test_tick &&
183 GIT_AUTHOR_NAME="With * Asterisk" \
184 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
185 git commit -a -m "second" &&
186
187 echo a >>file &&
188 test_tick &&
189 git commit -a -m "third"
190
191 '
192
193 test_expect_success 'log grep (1)' '
194 git log --author=author --pretty=tformat:%s >actual &&
195 ( echo third ; echo initial ) >expect &&
196 test_cmp expect actual
197 '
198
199 test_expect_success 'log grep (2)' '
200 git log --author=" * " -F --pretty=tformat:%s >actual &&
201 ( echo second ) >expect &&
202 test_cmp expect actual
203 '
204
205 test_expect_success 'log grep (3)' '
206 git log --author="^A U" --pretty=tformat:%s >actual &&
207 ( echo third ; echo initial ) >expect &&
208 test_cmp expect actual
209 '
210
211 test_expect_success 'log grep (4)' '
212 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
213 ( echo second ) >expect &&
214 test_cmp expect actual
215 '
216
217 test_expect_success 'log grep (5)' '
218 git log --author=Thor -F --grep=Thu --pretty=tformat:%s >actual &&
219 ( echo third ; echo initial ) >expect &&
220 test_cmp expect actual
221 '
222
223 test_expect_success 'log grep (6)' '
224 git log --author=-0700 --pretty=tformat:%s >actual &&
225 >expect &&
226 test_cmp expect actual
227 '
228
229 test_expect_success 'grep with CE_VALID file' '
230 git update-index --assume-unchanged t/t &&
231 rm t/t &&
232 test "$(git grep --no-ext-grep t)" = "t/t:test" &&
233 git update-index --no-assume-unchanged t/t &&
234 git checkout t/t
235 '
236
237 test_done