blob: 1ed0aa967ece5a38cf49bc43178e4fba187d4cd4 [file] [log] [blame]
Junio C Hamanof87f9492005-07-24 15:26:09 -07001#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5
Junio C Hamano5be60072007-07-02 22:52:14 -07006test_description='git ls-files --others --exclude
Junio C Hamanof87f9492005-07-24 15:26:09 -07007
Junio C Hamano5be60072007-07-02 22:52:14 -07008This test runs git ls-files --others and tests --exclude patterns.
Junio C Hamanof87f9492005-07-24 15:26:09 -07009'
10
Ævar Arnfjörð Bjarmason272f0a52021-10-07 12:01:36 +020011TEST_PASSES_SANITIZE_LEAK=true
Junio C Hamanof87f9492005-07-24 15:26:09 -070012. ./test-lib.sh
13
14rm -fr one three
15for dir in . one one/two three
16do
17 mkdir -p $dir &&
Junio C Hamano1df092d2005-07-28 23:53:29 -070018 for i in 1 2 3 4 5 6 7 8
Junio C Hamanof87f9492005-07-24 15:26:09 -070019 do
20 >$dir/a.$i
21 done
22done
Finn Arne Gangstaddd482ee2009-02-10 15:20:17 +010023>"#ignore1"
24>"#ignore2"
25>"#hidden"
Junio C Hamanof87f9492005-07-24 15:26:09 -070026
27cat >expect <<EOF
28a.2
29a.4
30a.5
Junio C Hamano1df092d2005-07-28 23:53:29 -070031a.8
Junio C Hamanof87f9492005-07-24 15:26:09 -070032one/a.3
33one/a.4
34one/a.5
Junio C Hamano1df092d2005-07-28 23:53:29 -070035one/a.7
36one/two/a.2
Junio C Hamanof87f9492005-07-24 15:26:09 -070037one/two/a.3
38one/two/a.5
Junio C Hamano1df092d2005-07-28 23:53:29 -070039one/two/a.7
40one/two/a.8
Junio C Hamanof87f9492005-07-24 15:26:09 -070041three/a.2
42three/a.3
43three/a.4
44three/a.5
Junio C Hamano1df092d2005-07-28 23:53:29 -070045three/a.8
Junio C Hamanof87f9492005-07-24 15:26:09 -070046EOF
47
48echo '.gitignore
Finn Arne Gangstaddd482ee2009-02-10 15:20:17 +010049\#ignore1
50\#ignore2*
51\#hid*n
Junio C Hamanof87f9492005-07-24 15:26:09 -070052output
53expect
54.gitignore
Junio C Hamano1df092d2005-07-28 23:53:29 -070055*.7
56!*.8' >.git/ignore
Junio C Hamanof87f9492005-07-24 15:26:09 -070057
58echo '*.1
Junio C Hamano1df092d2005-07-28 23:53:29 -070059/*.3
60!*.6' >.gitignore
Junio C Hamanof87f9492005-07-24 15:26:09 -070061echo '*.2
Junio C Hamano1df092d2005-07-28 23:53:29 -070062two/*.4
63!*.7
64*.8' >one/.gitignore
65echo '!*.2
66!*.8' >one/two/.gitignore
Junio C Hamanof87f9492005-07-24 15:26:09 -070067
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +070068allignores='.gitignore one/.gitignore one/two/.gitignore'
69
Li Linchao18337d42022-07-03 15:49:09 +000070test_expect_success 'git ls-files --others with various exclude options.' '
71 git ls-files --others \
Junio C Hamano1df092d2005-07-28 23:53:29 -070072 --exclude=\*.6 \
Junio C Hamanof87f9492005-07-24 15:26:09 -070073 --exclude-per-directory=.gitignore \
74 --exclude-from=.git/ignore \
Li Linchao18337d42022-07-03 15:49:09 +000075 >output &&
76 test_cmp expect output
77'
Pavel Roskinda7bc9b2005-08-10 22:15:02 -040078
Alex Riesend317e432005-11-02 14:05:45 +010079# Test \r\n (MSDOS-like systems)
Alex Riesen0dbc4e82006-02-12 19:05:34 +010080printf '*.1\r\n/*.3\r\n!*.6\r\n' >.gitignore
Alex Riesend317e432005-11-02 14:05:45 +010081
Li Linchao18337d42022-07-03 15:49:09 +000082test_expect_success 'git ls-files --others with \r\n line endings.' '
83 git ls-files --others \
Alex Riesend317e432005-11-02 14:05:45 +010084 --exclude=\*.6 \
85 --exclude-per-directory=.gitignore \
86 --exclude-from=.git/ignore \
Li Linchao18337d42022-07-03 15:49:09 +000087 >output &&
88 test_cmp expect output
89'
Alex Riesend317e432005-11-02 14:05:45 +010090
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +070091test_expect_success 'setup skip-worktree gitignore' '
92 git add $allignores &&
93 git update-index --skip-worktree $allignores &&
94 rm $allignores
95'
96
Li Linchao18337d42022-07-03 15:49:09 +000097test_expect_success 'git ls-files --others with various exclude options.' '
98 git ls-files --others \
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +070099 --exclude=\*.6 \
100 --exclude-per-directory=.gitignore \
101 --exclude-from=.git/ignore \
Li Linchao18337d42022-07-03 15:49:09 +0000102 >output &&
103 test_cmp expect output
104'
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +0700105
Ævar Arnfjörð Bjarmason96ecf692022-07-28 01:13:40 +0200106test_expect_success 'restore gitignore' '
Nguyễn Thái Ngọc Duy08d595d2013-04-13 09:12:08 +1000107 git checkout --ignore-skip-worktree-bits $allignores &&
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +0700108 rm .git/index
109'
110
Finn Arne Gangstaddd482ee2009-02-10 15:20:17 +0100111cat > excludes-file <<\EOF
Johannes Schindelin0ba956d2007-05-22 01:12:17 +0100112*.[1-8]
113e*
Finn Arne Gangstaddd482ee2009-02-10 15:20:17 +0100114\#*
Johannes Schindelin0ba956d2007-05-22 01:12:17 +0100115EOF
116
Junio C Hamano5be60072007-07-02 22:52:14 -0700117git config core.excludesFile excludes-file
Johannes Schindelin0ba956d2007-05-22 01:12:17 +0100118
Matthieu Moy2556b992013-09-06 19:43:07 +0200119git -c status.displayCommentPrefix=true status | grep "^# " > output
Johannes Schindelin0ba956d2007-05-22 01:12:17 +0100120
121cat > expect << EOF
122# .gitignore
123# a.6
124# one/
125# output
126# three/
127EOF
128
Ævar Arnfjörð Bjarmason96ecf692022-07-28 01:13:40 +0200129test_expect_success 'git status honors core.excludesfile' \
Jeff King82ebb0b2008-03-12 17:36:36 -0400130 'test_cmp expect output'
Johannes Schindelin0ba956d2007-05-22 01:12:17 +0100131
Junio C Hamanod6b8fc32008-01-31 01:17:48 -0800132test_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
Jonathan Niedera48fcd82010-10-30 20:46:54 -0500160 >two &&
Junio C Hamanod6b8fc32008-01-31 01:17:48 -0800161 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
Michael J Gruber32738ed2008-12-18 18:11:18 +0100173test_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
Junio C Hamano5cee3492016-03-18 11:06:15 -0700179test_expect_success 'excluded directory overrides content patterns' '
Karsten Bleesc3c327d2013-05-29 22:32:36 +0200180
181 git ls-files --others --exclude="one" --exclude="!one/a.1" >output &&
Junio C Hamano5cee3492016-03-18 11:06:15 -0700182 if grep "^one/a.1" output
183 then
184 false
185 fi
Karsten Bleesc3c327d2013-05-29 22:32:36 +0200186'
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
Junio C Hamano472e7462010-01-08 23:07:17 -0800197test_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 &&
Ævar Arnfjörð Bjarmasond3c67512018-07-27 17:48:11 +0000214 test_must_be_empty actual
Junio C Hamano472e7462010-01-08 23:07:17 -0800215'
216
217test_expect_success 'subdirectory ignore (l1/l2)' '
218 (
219 cd top/l1/l2 &&
220 git ls-files -o --exclude-standard
221 ) >actual &&
Ævar Arnfjörð Bjarmasond3c67512018-07-27 17:48:11 +0000222 test_must_be_empty actual
Junio C Hamano472e7462010-01-08 23:07:17 -0800223'
224
Junio C Hamano48ffef92010-01-08 23:05:41 -0800225test_expect_success 'subdirectory ignore (l1)' '
Junio C Hamano472e7462010-01-08 23:07:17 -0800226 (
227 cd top/l1 &&
228 git ls-files -o --exclude-standard
229 ) >actual &&
Ævar Arnfjörð Bjarmasond3c67512018-07-27 17:48:11 +0000230 test_must_be_empty actual
Junio C Hamano472e7462010-01-08 23:07:17 -0800231'
232
Karsten Blees184d2a82013-04-15 21:08:02 +0200233test_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 &&
Ævar Arnfjörð Bjarmasond3c67512018-07-27 17:48:11 +0000252 test_must_be_empty actual
Karsten Blees184d2a82013-04-15 21:08:02 +0200253'
254
Karsten Blees5bd8e2d2013-04-15 21:10:05 +0200255test_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 &&
Ævar Arnfjörð Bjarmasond3c67512018-07-27 17:48:11 +0000277 test_must_be_empty actual
Karsten Blees5bd8e2d2013-04-15 21:10:05 +0200278'
279
Nguyễn Thái Ngọc Duya3ea4d72012-10-15 13:24:36 +0700280test_expect_success 'pattern matches prefix completely' '
Nguyễn Thái Ngọc Duya3ea4d72012-10-15 13:24:36 +0700281 git ls-files -i -o --exclude "/three/a.3[abc]" >actual &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +0200282 test_must_be_empty actual
Nguyễn Thái Ngọc Duya3ea4d72012-10-15 13:24:36 +0700283'
284
Nguyễn Thái Ngọc Duy237ec6e2012-10-15 13:26:02 +0700285test_expect_success 'ls-files with "**" patterns' '
Li Linchao18337d42022-07-03 15:49:09 +0000286 cat <<-\EOF >expect &&
287 a.1
288 one/a.1
289 one/two/a.1
290 three/a.1
291 EOF
Jeff King60687de2015-03-20 06:06:44 -0400292 git ls-files -o -i --exclude "**/a.1" >actual &&
Nguyễn Thái Ngọc Duy237ec6e2012-10-15 13:26:02 +0700293 test_cmp expect actual
294'
295
Elijah Newrendd55fc02021-05-12 17:28:20 +0000296test_expect_success 'ls-files with "**" patterns and --directory' '
Elijah Newrena97c7a82021-05-12 17:28:18 +0000297 # Expectation same as previous test
298 git ls-files --directory -o -i --exclude "**/a.1" >actual &&
299 test_cmp expect actual
300'
Nguyễn Thái Ngọc Duy237ec6e2012-10-15 13:26:02 +0700301
302test_expect_success 'ls-files with "**" patterns and no slashes' '
Nguyễn Thái Ngọc Duy237ec6e2012-10-15 13:26:02 +0700303 git ls-files -o -i --exclude "one**a.1" >actual &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +0200304 test_must_be_empty actual
Nguyễn Thái Ngọc Duy237ec6e2012-10-15 13:26:02 +0700305'
306
Pavel Roskinda7bc9b2005-08-10 22:15:02 -0400307test_done