clean: require double -f options to nuke nested git repository and work tree
[git/git.git] / t / t7300-clean.sh
CommitLineData
ec0e0f25
MS
1#!/bin/sh
2#
3# Copyright (c) 2007 Michael Spang
4#
5
47a528ad 6test_description='git clean basic tests'
ec0e0f25
MS
7
8. ./test-lib.sh
9
562ca192
JH
10git config clean.requireForce no
11
ec0e0f25
MS
12test_expect_success 'setup' '
13
14 mkdir -p src &&
15 touch src/part1.c Makefile &&
16 echo build >.gitignore &&
17 echo \*.o >>.gitignore &&
5be60078 18 git add . &&
47a528ad 19 git commit -m setup &&
ec0e0f25 20 touch src/part2.c README &&
5be60078 21 git add .
ec0e0f25
MS
22
23'
24
47a528ad 25test_expect_success 'git clean' '
ec0e0f25
MS
26
27 mkdir -p build docs &&
28 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
47a528ad 29 git clean &&
ec0e0f25
MS
30 test -f Makefile &&
31 test -f README &&
32 test -f src/part1.c &&
33 test -f src/part2.c &&
34 test ! -f a.out &&
35 test ! -f src/part3.c &&
36 test -f docs/manual.txt &&
37 test -f obj.o &&
38 test -f build/lib.so
39
40'
41
47a528ad 42test_expect_success 'git clean src/' '
ae3e76c2
SB
43
44 mkdir -p build docs &&
45 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
47a528ad 46 git clean src/ &&
ae3e76c2
SB
47 test -f Makefile &&
48 test -f README &&
49 test -f src/part1.c &&
50 test -f src/part2.c &&
51 test -f a.out &&
52 test ! -f src/part3.c &&
53 test -f docs/manual.txt &&
54 test -f obj.o &&
55 test -f build/lib.so
56
57'
58
47a528ad 59test_expect_success 'git clean src/ src/' '
ae3e76c2
SB
60
61 mkdir -p build docs &&
62 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
47a528ad 63 git clean src/ src/ &&
ae3e76c2
SB
64 test -f Makefile &&
65 test -f README &&
66 test -f src/part1.c &&
67 test -f src/part2.c &&
68 test -f a.out &&
69 test ! -f src/part3.c &&
70 test -f docs/manual.txt &&
71 test -f obj.o &&
72 test -f build/lib.so
73
74'
75
47a528ad 76test_expect_success 'git clean with prefix' '
ae3e76c2 77
2b6f0b0a
SB
78 mkdir -p build docs src/test &&
79 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so src/test/1.c &&
47a528ad 80 (cd src/ && git clean) &&
ae3e76c2
SB
81 test -f Makefile &&
82 test -f README &&
83 test -f src/part1.c &&
84 test -f src/part2.c &&
85 test -f a.out &&
86 test ! -f src/part3.c &&
2b6f0b0a 87 test -f src/test/1.c &&
ae3e76c2
SB
88 test -f docs/manual.txt &&
89 test -f obj.o &&
90 test -f build/lib.so
91
92'
5b7570cf 93
47a528ad 94test_expect_success 'git clean with relative prefix' '
5b7570cf
JH
95
96 mkdir -p build docs &&
97 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
98 would_clean=$(
99 cd docs &&
100 git clean -n ../src |
101 sed -n -e "s|^Would remove ||p"
102 ) &&
103 test "$would_clean" = ../src/part3.c || {
104 echo "OOps <$would_clean>"
105 false
106 }
107'
108
47a528ad 109test_expect_success 'git clean with absolute path' '
5b7570cf
JH
110
111 mkdir -p build docs &&
112 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
113 would_clean=$(
114 cd docs &&
f69e836f 115 git clean -n "$(pwd)/../src" |
5b7570cf
JH
116 sed -n -e "s|^Would remove ||p"
117 ) &&
118 test "$would_clean" = ../src/part3.c || {
119 echo "OOps <$would_clean>"
120 false
121 }
122'
123
47a528ad 124test_expect_success 'git clean with out of work tree relative path' '
5b7570cf
JH
125
126 mkdir -p build docs &&
127 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
128 (
129 cd docs &&
130 test_must_fail git clean -n ../..
131 )
132'
133
47a528ad 134test_expect_success 'git clean with out of work tree absolute path' '
5b7570cf
JH
135
136 mkdir -p build docs &&
137 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
138 dd=$(cd .. && pwd) &&
139 (
140 cd docs &&
141 test_must_fail git clean -n $dd
142 )
143'
144
47a528ad 145test_expect_success 'git clean -d with prefix and path' '
ae3e76c2
SB
146
147 mkdir -p build docs src/feature &&
148 touch a.out src/part3.c src/feature/file.c docs/manual.txt obj.o build/lib.so &&
47a528ad 149 (cd src/ && git clean -d feature/) &&
ae3e76c2
SB
150 test -f Makefile &&
151 test -f README &&
152 test -f src/part1.c &&
153 test -f src/part2.c &&
154 test -f a.out &&
155 test -f src/part3.c &&
156 test ! -f src/feature/file.c &&
157 test -f docs/manual.txt &&
158 test -f obj.o &&
159 test -f build/lib.so
160
161'
162
47a528ad 163test_expect_success 'git clean symbolic link' '
ae3e76c2
SB
164
165 mkdir -p build docs &&
166 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
167 ln -s docs/manual.txt src/part4.c
47a528ad 168 git clean &&
ae3e76c2
SB
169 test -f Makefile &&
170 test -f README &&
171 test -f src/part1.c &&
172 test -f src/part2.c &&
173 test ! -f a.out &&
174 test ! -f src/part3.c &&
175 test ! -f src/part4.c &&
176 test -f docs/manual.txt &&
177 test -f obj.o &&
178 test -f build/lib.so
179
180'
181
47a528ad 182test_expect_success 'git clean with wildcard' '
d3357ab8
JK
183
184 touch a.clean b.clean other.c &&
47a528ad 185 git clean "*.clean" &&
d3357ab8
JK
186 test -f Makefile &&
187 test -f README &&
188 test -f src/part1.c &&
189 test -f src/part2.c &&
190 test ! -f a.clean &&
191 test ! -f b.clean &&
192 test -f other.c
193
194'
195
47a528ad 196test_expect_success 'git clean -n' '
ec0e0f25
MS
197
198 mkdir -p build docs &&
199 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
47a528ad 200 git clean -n &&
ec0e0f25
MS
201 test -f Makefile &&
202 test -f README &&
203 test -f src/part1.c &&
204 test -f src/part2.c &&
205 test -f a.out &&
206 test -f src/part3.c &&
207 test -f docs/manual.txt &&
208 test -f obj.o &&
209 test -f build/lib.so
210
211'
212
47a528ad 213test_expect_success 'git clean -d' '
ec0e0f25
MS
214
215 mkdir -p build docs &&
216 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
47a528ad 217 git clean -d &&
ec0e0f25
MS
218 test -f Makefile &&
219 test -f README &&
220 test -f src/part1.c &&
221 test -f src/part2.c &&
222 test ! -f a.out &&
223 test ! -f src/part3.c &&
224 test ! -d docs &&
225 test -f obj.o &&
226 test -f build/lib.so
227
228'
229
47a528ad 230test_expect_success 'git clean -d src/ examples/' '
ae3e76c2
SB
231
232 mkdir -p build docs examples &&
233 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so examples/1.c &&
47a528ad 234 git clean -d src/ examples/ &&
ae3e76c2
SB
235 test -f Makefile &&
236 test -f README &&
237 test -f src/part1.c &&
238 test -f src/part2.c &&
239 test -f a.out &&
240 test ! -f src/part3.c &&
241 test ! -f examples/1.c &&
242 test -f docs/manual.txt &&
243 test -f obj.o &&
244 test -f build/lib.so
245
246'
247
47a528ad 248test_expect_success 'git clean -x' '
ec0e0f25
MS
249
250 mkdir -p build docs &&
251 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
47a528ad 252 git clean -x &&
ec0e0f25
MS
253 test -f Makefile &&
254 test -f README &&
255 test -f src/part1.c &&
256 test -f src/part2.c &&
257 test ! -f a.out &&
258 test ! -f src/part3.c &&
259 test -f docs/manual.txt &&
260 test ! -f obj.o &&
261 test -f build/lib.so
262
263'
264
47a528ad 265test_expect_success 'git clean -d -x' '
ec0e0f25
MS
266
267 mkdir -p build docs &&
268 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
47a528ad 269 git clean -d -x &&
ec0e0f25
MS
270 test -f Makefile &&
271 test -f README &&
272 test -f src/part1.c &&
273 test -f src/part2.c &&
274 test ! -f a.out &&
275 test ! -f src/part3.c &&
276 test ! -d docs &&
277 test ! -f obj.o &&
278 test ! -d build
279
280'
281
47a528ad 282test_expect_success 'git clean -X' '
ec0e0f25
MS
283
284 mkdir -p build docs &&
285 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
47a528ad 286 git clean -X &&
ec0e0f25
MS
287 test -f Makefile &&
288 test -f README &&
289 test -f src/part1.c &&
290 test -f src/part2.c &&
291 test -f a.out &&
292 test -f src/part3.c &&
293 test -f docs/manual.txt &&
294 test ! -f obj.o &&
295 test -f build/lib.so
296
297'
298
47a528ad 299test_expect_success 'git clean -d -X' '
ec0e0f25
MS
300
301 mkdir -p build docs &&
302 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
47a528ad 303 git clean -d -X &&
ec0e0f25
MS
304 test -f Makefile &&
305 test -f README &&
306 test -f src/part1.c &&
307 test -f src/part2.c &&
308 test -f a.out &&
309 test -f src/part3.c &&
310 test -f docs/manual.txt &&
311 test ! -f obj.o &&
312 test ! -d build
313
314'
315
562ca192
JH
316test_expect_success 'clean.requireForce defaults to true' '
317
318 git config --unset clean.requireForce &&
d492b31c 319 test_must_fail git clean
562ca192
JH
320
321'
322
ec0e0f25
MS
323test_expect_success 'clean.requireForce' '
324
5be60078 325 git config clean.requireForce true &&
d492b31c 326 test_must_fail git clean
ec0e0f25
MS
327
328'
329
330test_expect_success 'clean.requireForce and -n' '
331
332 mkdir -p build docs &&
333 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
47a528ad 334 git clean -n &&
ec0e0f25
MS
335 test -f Makefile &&
336 test -f README &&
337 test -f src/part1.c &&
338 test -f src/part2.c &&
339 test -f a.out &&
340 test -f src/part3.c &&
341 test -f docs/manual.txt &&
342 test -f obj.o &&
343 test -f build/lib.so
344
345'
346
347test_expect_success 'clean.requireForce and -f' '
348
47a528ad 349 git clean -f &&
ec0e0f25
MS
350 test -f README &&
351 test -f src/part1.c &&
352 test -f src/part2.c &&
353 test ! -f a.out &&
354 test ! -f src/part3.c &&
355 test -f docs/manual.txt &&
356 test -f obj.o &&
357 test -f build/lib.so
358
359'
360
b57321f5
JH
361test_expect_success 'core.excludesfile' '
362
363 echo excludes >excludes &&
364 echo included >included &&
365 git config core.excludesfile excludes &&
366 output=$(git clean -n excludes included 2>&1) &&
367 expr "$output" : ".*included" >/dev/null &&
368 ! expr "$output" : ".*excludes" >/dev/null
369
370'
371
aa9c83c2
MV
372test_expect_success 'removal failure' '
373
374 mkdir foo &&
375 touch foo/bar &&
e2c24076
JS
376 (exec <foo/bar &&
377 chmod 0 foo &&
378 test_must_fail git clean -f -d)
aa9c83c2
MV
379
380'
381chmod 755 foo
382
a0f4afbe
JH
383test_expect_success 'nested git work tree' '
384 rm -fr foo bar &&
385 mkdir foo bar &&
386 (
387 cd foo &&
388 git init &&
389 >hello.world
390 git add . &&
391 git commit -a -m nested
392 ) &&
393 (
394 cd bar &&
395 >goodbye.people
396 ) &&
397 git clean -f -d &&
398 test -f foo/.git/index &&
399 test -f foo/hello.world &&
400 ! test -d bar
401'
402
403test_expect_success 'force removal of nested git work tree' '
404 rm -fr foo bar &&
405 mkdir foo bar &&
406 (
407 cd foo &&
408 git init &&
409 >hello.world
410 git add . &&
411 git commit -a -m nested
412 ) &&
413 (
414 cd bar &&
415 >goodbye.people
416 ) &&
417 git clean -f -f -d &&
418 ! test -d foo &&
419 ! test -d bar
420'
421
ec0e0f25 422test_done