Merge branch 'jc/sane-grep'
[git] / t / t3001-ls-files-others-exclude.sh
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 \
70     'git ls-files --others with various exclude options.' \
71     'git ls-files --others \
72        --exclude=\*.6 \
73        --exclude-per-directory=.gitignore \
74        --exclude-from=.git/ignore \
75        >output &&
76      test_cmp expect output'
77
78 # Test \r\n (MSDOS-like systems)
79 printf '*.1\r\n/*.3\r\n!*.6\r\n' >.gitignore
80
81 test_expect_success \
82     '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_cmp expect output'
89
90 test_expect_success 'setup skip-worktree gitignore' '
91         git add $allignores &&
92         git update-index --skip-worktree $allignores &&
93         rm $allignores
94 '
95
96 test_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
105 test_expect_success 'restore gitignore' '
106         git checkout --ignore-skip-worktree-bits $allignores &&
107         rm .git/index
108 '
109
110 cat > excludes-file <<\EOF
111 *.[1-8]
112 e*
113 \#*
114 EOF
115
116 git config core.excludesFile excludes-file
117
118 git -c status.displayCommentPrefix=true status | grep "^#       " > output
119
120 cat > expect << EOF
121 #       .gitignore
122 #       a.6
123 #       one/
124 #       output
125 #       three/
126 EOF
127
128 test_expect_success 'git status honors core.excludesfile' \
129         'test_cmp expect output'
130
131 test_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
144 test_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
157 test_expect_success 'trailing slash in exclude forces directory match (1)' '
158
159         >two &&
160         git ls-files --others --exclude=two/ >output &&
161         grep "^two" output
162
163 '
164
165 test_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
172 test_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
178 test_expect_success 'excluded directory does not override content patterns' '
179
180         git ls-files --others --exclude="one" --exclude="!one/a.1" >output &&
181         grep "^one/a.1" output
182 '
183
184 test_expect_success 'negated directory doesn'\''t affect content patterns' '
185
186         git ls-files --others --exclude="!one" --exclude="one/a.1" >output &&
187         if grep "^one/a.1" output
188         then
189                 false
190         fi
191 '
192
193 test_expect_success 'subdirectory ignore (setup)' '
194         mkdir -p top/l1/l2 &&
195         (
196                 cd top &&
197                 git init &&
198                 echo /.gitignore >.gitignore &&
199                 echo l1 >>.gitignore &&
200                 echo l2 >l1/.gitignore &&
201                 >l1/l2/l1
202         )
203 '
204
205 test_expect_success 'subdirectory ignore (toplevel)' '
206         (
207                 cd top &&
208                 git ls-files -o --exclude-standard
209         ) >actual &&
210         >expect &&
211         test_cmp expect actual
212 '
213
214 test_expect_success 'subdirectory ignore (l1/l2)' '
215         (
216                 cd top/l1/l2 &&
217                 git ls-files -o --exclude-standard
218         ) >actual &&
219         >expect &&
220         test_cmp expect actual
221 '
222
223 test_expect_success 'subdirectory ignore (l1)' '
224         (
225                 cd top/l1 &&
226                 git ls-files -o --exclude-standard
227         ) >actual &&
228         >expect &&
229         test_cmp expect actual
230 '
231
232 test_expect_success 'show/hide empty ignored directory (setup)' '
233         rm top/l1/l2/l1 &&
234         rm top/l1/.gitignore
235 '
236
237 test_expect_success 'show empty ignored directory with --directory' '
238         (
239                 cd top &&
240                 git ls-files -o -i --exclude l1 --directory
241         ) >actual &&
242         echo l1/ >expect &&
243         test_cmp expect actual
244 '
245
246 test_expect_success 'hide empty ignored directory with --no-empty-directory' '
247         (
248                 cd top &&
249                 git ls-files -o -i --exclude l1 --directory --no-empty-directory
250         ) >actual &&
251         >expect &&
252         test_cmp expect actual
253 '
254
255 test_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
263 test_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
272 test_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         >expect &&
278         test_cmp expect actual
279 '
280
281 test_expect_success 'pattern matches prefix completely' '
282         : >expect &&
283         git ls-files -i -o --exclude "/three/a.3[abc]" >actual &&
284         test_cmp expect 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
299 test_expect_success 'ls-files with "**" patterns and no slashes' '
300         : >expect &&
301         git ls-files -o -i --exclude "one**a.1" >actual &&
302         test_cmp expect actual
303 '
304
305 test_done