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