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