dir.c: git-status --ignored: don't scan the work tree twice
[git/git.git] / t / t3001-ls-files-others-exclude.sh
CommitLineData
f87f9497
JH
1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5
5be60078 6test_description='git ls-files --others --exclude
f87f9497 7
5be60078 8This test runs git ls-files --others and tests --exclude patterns.
f87f9497
JH
9'
10
11. ./test-lib.sh
12
13rm -fr one three
14for dir in . one one/two three
15do
16 mkdir -p $dir &&
1df092d2 17 for i in 1 2 3 4 5 6 7 8
f87f9497
JH
18 do
19 >$dir/a.$i
20 done
21done
dd482eea
FAG
22>"#ignore1"
23>"#ignore2"
24>"#hidden"
f87f9497
JH
25
26cat >expect <<EOF
27a.2
28a.4
29a.5
1df092d2 30a.8
f87f9497
JH
31one/a.3
32one/a.4
33one/a.5
1df092d2
JH
34one/a.7
35one/two/a.2
f87f9497
JH
36one/two/a.3
37one/two/a.5
1df092d2
JH
38one/two/a.7
39one/two/a.8
f87f9497
JH
40three/a.2
41three/a.3
42three/a.4
43three/a.5
1df092d2 44three/a.8
f87f9497
JH
45EOF
46
47echo '.gitignore
dd482eea
FAG
48\#ignore1
49\#ignore2*
50\#hid*n
f87f9497
JH
51output
52expect
53.gitignore
1df092d2
JH
54*.7
55!*.8' >.git/ignore
f87f9497
JH
56
57echo '*.1
1df092d2
JH
58/*.3
59!*.6' >.gitignore
f87f9497 60echo '*.2
1df092d2
JH
61two/*.4
62!*.7
63*.8' >one/.gitignore
64echo '!*.2
65!*.8' >one/two/.gitignore
f87f9497 66
c28b3d6e
NTND
67allignores='.gitignore one/.gitignore one/two/.gitignore'
68
f87f9497 69test_expect_success \
5be60078
JH
70 'git ls-files --others with various exclude options.' \
71 'git ls-files --others \
1df092d2 72 --exclude=\*.6 \
f87f9497
JH
73 --exclude-per-directory=.gitignore \
74 --exclude-from=.git/ignore \
75 >output &&
3af82863 76 test_cmp expect output'
da7bc9b0 77
d317e438 78# Test \r\n (MSDOS-like systems)
0dbc4e89 79printf '*.1\r\n/*.3\r\n!*.6\r\n' >.gitignore
d317e438
AR
80
81test_expect_success \
5be60078
JH
82 'git ls-files --others with \r\n line endings.' \
83 'git ls-files --others \
d317e438
AR
84 --exclude=\*.6 \
85 --exclude-per-directory=.gitignore \
86 --exclude-from=.git/ignore \
87 >output &&
3af82863 88 test_cmp expect output'
d317e438 89
c28b3d6e
NTND
90test_expect_success 'setup skip-worktree gitignore' '
91 git add $allignores &&
92 git update-index --skip-worktree $allignores &&
93 rm $allignores
94'
95
96test_expect_success \
97 'git ls-files --others with various exclude options.' \
98 'git ls-files --others \
99 --exclude=\*.6 \
100 --exclude-per-directory=.gitignore \
101 --exclude-from=.git/ignore \
102 >output &&
103 test_cmp expect output'
104
105test_expect_success 'restore gitignore' '
106 git checkout $allignores &&
107 rm .git/index
108'
109
dd482eea 110cat > excludes-file <<\EOF
0ba956d3
JS
111*.[1-8]
112e*
dd482eea 113\#*
0ba956d3
JS
114EOF
115
5be60078 116git config core.excludesFile excludes-file
0ba956d3 117
637efc34 118git status | grep "^# " > output
0ba956d3
JS
119
120cat > expect << EOF
121# .gitignore
122# a.6
123# one/
124# output
125# three/
126EOF
127
0cb0e143 128test_expect_success 'git status honors core.excludesfile' \
82ebb0b6 129 'test_cmp expect output'
0ba956d3 130
d6b8fc30
JH
131test_expect_success 'trailing slash in exclude allows directory match(1)' '
132
133 git ls-files --others --exclude=one/ >output &&
134 if grep "^one/" output
135 then
136 echo Ooops
137 false
138 else
139 : happy
140 fi
141
142'
143
144test_expect_success 'trailing slash in exclude allows directory match (2)' '
145
146 git ls-files --others --exclude=one/two/ >output &&
147 if grep "^one/two/" output
148 then
149 echo Ooops
150 false
151 else
152 : happy
153 fi
154
155'
156
157test_expect_success 'trailing slash in exclude forces directory match (1)' '
158
a48fcd83 159 >two &&
d6b8fc30
JH
160 git ls-files --others --exclude=two/ >output &&
161 grep "^two" output
162
163'
164
165test_expect_success 'trailing slash in exclude forces directory match (2)' '
166
167 git ls-files --others --exclude=one/a.1/ >output &&
168 grep "^one/a.1" output
169
170'
171
32738edf
MG
172test_expect_success 'negated exclude matches can override previous ones' '
173
174 git ls-files --others --exclude="a.*" --exclude="!a.1" >output &&
175 grep "^a.1" output
176'
177
472e7469
JH
178test_expect_success 'subdirectory ignore (setup)' '
179 mkdir -p top/l1/l2 &&
180 (
181 cd top &&
182 git init &&
183 echo /.gitignore >.gitignore &&
184 echo l1 >>.gitignore &&
185 echo l2 >l1/.gitignore &&
186 >l1/l2/l1
187 )
188'
189
190test_expect_success 'subdirectory ignore (toplevel)' '
191 (
192 cd top &&
193 git ls-files -o --exclude-standard
194 ) >actual &&
195 >expect &&
196 test_cmp expect actual
197'
198
199test_expect_success 'subdirectory ignore (l1/l2)' '
200 (
201 cd top/l1/l2 &&
202 git ls-files -o --exclude-standard
203 ) >actual &&
204 >expect &&
205 test_cmp expect actual
206'
207
48ffef96 208test_expect_success 'subdirectory ignore (l1)' '
472e7469
JH
209 (
210 cd top/l1 &&
211 git ls-files -o --exclude-standard
212 ) >actual &&
213 >expect &&
214 test_cmp expect actual
215'
216
184d2a8e
KB
217test_expect_success 'show/hide empty ignored directory (setup)' '
218 rm top/l1/l2/l1 &&
219 rm top/l1/.gitignore
220'
221
222test_expect_success 'show empty ignored directory with --directory' '
223 (
224 cd top &&
225 git ls-files -o -i --exclude l1 --directory
226 ) >actual &&
227 echo l1/ >expect &&
228 test_cmp expect actual
229'
230
231test_expect_success 'hide empty ignored directory with --no-empty-directory' '
232 (
233 cd top &&
234 git ls-files -o -i --exclude l1 --directory --no-empty-directory
235 ) >actual &&
236 >expect &&
237 test_cmp expect actual
238'
239
5bd8e2d8
KB
240test_expect_success 'show/hide empty ignored sub-directory (setup)' '
241 > top/l1/tracked &&
242 (
243 cd top &&
244 git add -f l1/tracked
245 )
246'
247
248test_expect_success 'show empty ignored sub-directory with --directory' '
249 (
250 cd top &&
251 git ls-files -o -i --exclude l1 --directory
252 ) >actual &&
253 echo l1/l2/ >expect &&
254 test_cmp expect actual
255'
256
257test_expect_success 'hide empty ignored sub-directory with --no-empty-directory' '
258 (
259 cd top &&
260 git ls-files -o -i --exclude l1 --directory --no-empty-directory
261 ) >actual &&
262 >expect &&
263 test_cmp expect actual
264'
265
a3ea4d71
NTND
266test_expect_success 'pattern matches prefix completely' '
267 : >expect &&
268 git ls-files -i -o --exclude "/three/a.3[abc]" >actual &&
269 test_cmp expect actual
270'
271
237ec6e4
NTND
272test_expect_success 'ls-files with "**" patterns' '
273 cat <<\EOF >expect &&
274a.1
275one/a.1
276one/two/a.1
277three/a.1
278EOF
279 git ls-files -o -i --exclude "**/a.1" >actual
280 test_cmp expect actual
281'
282
283
284test_expect_success 'ls-files with "**" patterns and no slashes' '
285 : >expect &&
286 git ls-files -o -i --exclude "one**a.1" >actual &&
287 test_cmp expect actual
288'
289
da7bc9b0 290test_done