]>
Commit | Line | Data |
---|---|---|
ec0e0f25 MS |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2007 Michael Spang | |
4 | # | |
5 | ||
47a528ad | 6 | test_description='git clean basic tests' |
ec0e0f25 MS |
7 | |
8 | . ./test-lib.sh | |
9 | ||
562ca192 JH |
10 | git config clean.requireForce no |
11 | ||
ec0e0f25 MS |
12 | test_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 | ||
c28b3d6e NTND |
25 | test_expect_success 'git clean with skip-worktree .gitignore' ' |
26 | git update-index --skip-worktree .gitignore && | |
27 | rm .gitignore && | |
28 | mkdir -p build docs && | |
29 | touch a.out src/part3.c docs/manual.txt obj.o build/lib.so && | |
30 | git clean && | |
31 | test -f Makefile && | |
32 | test -f README && | |
33 | test -f src/part1.c && | |
34 | test -f src/part2.c && | |
35 | test ! -f a.out && | |
36 | test ! -f src/part3.c && | |
37 | test -f docs/manual.txt && | |
38 | test -f obj.o && | |
39 | test -f build/lib.so && | |
40 | git update-index --no-skip-worktree .gitignore && | |
41 | git checkout .gitignore | |
42 | ' | |
43 | ||
47a528ad | 44 | test_expect_success 'git clean' ' |
ec0e0f25 MS |
45 | |
46 | mkdir -p build docs && | |
47 | touch a.out src/part3.c docs/manual.txt obj.o build/lib.so && | |
47a528ad | 48 | git clean && |
ec0e0f25 MS |
49 | test -f Makefile && |
50 | test -f README && | |
51 | test -f src/part1.c && | |
52 | test -f src/part2.c && | |
53 | test ! -f a.out && | |
54 | test ! -f src/part3.c && | |
55 | test -f docs/manual.txt && | |
56 | test -f obj.o && | |
57 | test -f build/lib.so | |
58 | ||
59 | ' | |
60 | ||
47a528ad | 61 | test_expect_success 'git clean src/' ' |
ae3e76c2 SB |
62 | |
63 | mkdir -p build docs && | |
64 | touch a.out src/part3.c docs/manual.txt obj.o build/lib.so && | |
47a528ad | 65 | git clean src/ && |
ae3e76c2 SB |
66 | test -f Makefile && |
67 | test -f README && | |
68 | test -f src/part1.c && | |
69 | test -f src/part2.c && | |
70 | test -f a.out && | |
71 | test ! -f src/part3.c && | |
72 | test -f docs/manual.txt && | |
73 | test -f obj.o && | |
74 | test -f build/lib.so | |
75 | ||
76 | ' | |
77 | ||
47a528ad | 78 | test_expect_success 'git clean src/ src/' ' |
ae3e76c2 SB |
79 | |
80 | mkdir -p build docs && | |
81 | touch a.out src/part3.c docs/manual.txt obj.o build/lib.so && | |
47a528ad | 82 | git clean src/ src/ && |
ae3e76c2 SB |
83 | test -f Makefile && |
84 | test -f README && | |
85 | test -f src/part1.c && | |
86 | test -f src/part2.c && | |
87 | test -f a.out && | |
88 | test ! -f src/part3.c && | |
89 | test -f docs/manual.txt && | |
90 | test -f obj.o && | |
91 | test -f build/lib.so | |
92 | ||
93 | ' | |
94 | ||
47a528ad | 95 | test_expect_success 'git clean with prefix' ' |
ae3e76c2 | 96 | |
2b6f0b0a SB |
97 | mkdir -p build docs src/test && |
98 | touch a.out src/part3.c docs/manual.txt obj.o build/lib.so src/test/1.c && | |
47a528ad | 99 | (cd src/ && git clean) && |
ae3e76c2 SB |
100 | test -f Makefile && |
101 | test -f README && | |
102 | test -f src/part1.c && | |
103 | test -f src/part2.c && | |
104 | test -f a.out && | |
105 | test ! -f src/part3.c && | |
2b6f0b0a | 106 | test -f src/test/1.c && |
ae3e76c2 SB |
107 | test -f docs/manual.txt && |
108 | test -f obj.o && | |
109 | test -f build/lib.so | |
110 | ||
111 | ' | |
5b7570cf | 112 | |
a926c4b9 | 113 | test_expect_success 'git clean with relative prefix' ' |
5b7570cf JH |
114 | |
115 | mkdir -p build docs && | |
116 | touch a.out src/part3.c docs/manual.txt obj.o build/lib.so && | |
117 | would_clean=$( | |
118 | cd docs && | |
119 | git clean -n ../src | | |
e86bbcf9 | 120 | grep part3 | |
5b7570cf JH |
121 | sed -n -e "s|^Would remove ||p" |
122 | ) && | |
8ddfce71 | 123 | test "$would_clean" = ../src/part3.c |
5b7570cf JH |
124 | ' |
125 | ||
a926c4b9 | 126 | test_expect_success 'git clean with absolute path' ' |
5b7570cf JH |
127 | |
128 | mkdir -p build docs && | |
129 | touch a.out src/part3.c docs/manual.txt obj.o build/lib.so && | |
130 | would_clean=$( | |
131 | cd docs && | |
f69e836f | 132 | git clean -n "$(pwd)/../src" | |
e86bbcf9 | 133 | grep part3 | |
5b7570cf JH |
134 | sed -n -e "s|^Would remove ||p" |
135 | ) && | |
8ddfce71 | 136 | test "$would_clean" = ../src/part3.c |
5b7570cf JH |
137 | ' |
138 | ||
47a528ad | 139 | test_expect_success 'git clean with out of work tree relative path' ' |
5b7570cf JH |
140 | |
141 | mkdir -p build docs && | |
142 | touch a.out src/part3.c docs/manual.txt obj.o build/lib.so && | |
143 | ( | |
144 | cd docs && | |
145 | test_must_fail git clean -n ../.. | |
146 | ) | |
147 | ' | |
148 | ||
47a528ad | 149 | test_expect_success 'git clean with out of work tree absolute path' ' |
5b7570cf JH |
150 | |
151 | mkdir -p build docs && | |
152 | touch a.out src/part3.c docs/manual.txt obj.o build/lib.so && | |
153 | dd=$(cd .. && pwd) && | |
154 | ( | |
155 | cd docs && | |
156 | test_must_fail git clean -n $dd | |
157 | ) | |
158 | ' | |
159 | ||
47a528ad | 160 | test_expect_success 'git clean -d with prefix and path' ' |
ae3e76c2 SB |
161 | |
162 | mkdir -p build docs src/feature && | |
163 | touch a.out src/part3.c src/feature/file.c docs/manual.txt obj.o build/lib.so && | |
47a528ad | 164 | (cd src/ && git clean -d feature/) && |
ae3e76c2 SB |
165 | test -f Makefile && |
166 | test -f README && | |
167 | test -f src/part1.c && | |
168 | test -f src/part2.c && | |
169 | test -f a.out && | |
170 | test -f src/part3.c && | |
171 | test ! -f src/feature/file.c && | |
172 | test -f docs/manual.txt && | |
173 | test -f obj.o && | |
174 | test -f build/lib.so | |
175 | ||
176 | ' | |
177 | ||
7569c1f4 | 178 | test_expect_success SYMLINKS 'git clean symbolic link' ' |
ae3e76c2 SB |
179 | |
180 | mkdir -p build docs && | |
181 | touch a.out src/part3.c docs/manual.txt obj.o build/lib.so && | |
2dec68cf | 182 | ln -s docs/manual.txt src/part4.c && |
47a528ad | 183 | git clean && |
ae3e76c2 SB |
184 | test -f Makefile && |
185 | test -f README && | |
186 | test -f src/part1.c && | |
187 | test -f src/part2.c && | |
188 | test ! -f a.out && | |
189 | test ! -f src/part3.c && | |
190 | test ! -f src/part4.c && | |
191 | test -f docs/manual.txt && | |
192 | test -f obj.o && | |
193 | test -f build/lib.so | |
194 | ||
195 | ' | |
196 | ||
47a528ad | 197 | test_expect_success 'git clean with wildcard' ' |
d3357ab8 JK |
198 | |
199 | touch a.clean b.clean other.c && | |
47a528ad | 200 | git clean "*.clean" && |
d3357ab8 JK |
201 | test -f Makefile && |
202 | test -f README && | |
203 | test -f src/part1.c && | |
204 | test -f src/part2.c && | |
205 | test ! -f a.clean && | |
206 | test ! -f b.clean && | |
207 | test -f other.c | |
208 | ||
209 | ' | |
210 | ||
47a528ad | 211 | test_expect_success 'git clean -n' ' |
ec0e0f25 MS |
212 | |
213 | mkdir -p build docs && | |
214 | touch a.out src/part3.c docs/manual.txt obj.o build/lib.so && | |
47a528ad | 215 | git clean -n && |
ec0e0f25 MS |
216 | test -f Makefile && |
217 | test -f README && | |
218 | test -f src/part1.c && | |
219 | test -f src/part2.c && | |
220 | test -f a.out && | |
221 | test -f src/part3.c && | |
222 | test -f docs/manual.txt && | |
223 | test -f obj.o && | |
224 | test -f build/lib.so | |
225 | ||
226 | ' | |
227 | ||
47a528ad | 228 | test_expect_success 'git clean -d' ' |
ec0e0f25 MS |
229 | |
230 | mkdir -p build docs && | |
231 | touch a.out src/part3.c docs/manual.txt obj.o build/lib.so && | |
47a528ad | 232 | git clean -d && |
ec0e0f25 MS |
233 | test -f Makefile && |
234 | test -f README && | |
235 | test -f src/part1.c && | |
236 | test -f src/part2.c && | |
237 | test ! -f a.out && | |
238 | test ! -f src/part3.c && | |
239 | test ! -d docs && | |
240 | test -f obj.o && | |
241 | test -f build/lib.so | |
242 | ||
243 | ' | |
244 | ||
47a528ad | 245 | test_expect_success 'git clean -d src/ examples/' ' |
ae3e76c2 SB |
246 | |
247 | mkdir -p build docs examples && | |
248 | touch a.out src/part3.c docs/manual.txt obj.o build/lib.so examples/1.c && | |
47a528ad | 249 | git clean -d src/ examples/ && |
ae3e76c2 SB |
250 | test -f Makefile && |
251 | test -f README && | |
252 | test -f src/part1.c && | |
253 | test -f src/part2.c && | |
254 | test -f a.out && | |
255 | test ! -f src/part3.c && | |
256 | test ! -f examples/1.c && | |
257 | test -f docs/manual.txt && | |
258 | test -f obj.o && | |
259 | test -f build/lib.so | |
260 | ||
261 | ' | |
262 | ||
47a528ad | 263 | test_expect_success 'git clean -x' ' |
ec0e0f25 MS |
264 | |
265 | mkdir -p build docs && | |
266 | touch a.out src/part3.c docs/manual.txt obj.o build/lib.so && | |
47a528ad | 267 | git clean -x && |
ec0e0f25 MS |
268 | test -f Makefile && |
269 | test -f README && | |
270 | test -f src/part1.c && | |
271 | test -f src/part2.c && | |
272 | test ! -f a.out && | |
273 | test ! -f src/part3.c && | |
274 | test -f docs/manual.txt && | |
275 | test ! -f obj.o && | |
276 | test -f build/lib.so | |
277 | ||
278 | ' | |
279 | ||
47a528ad | 280 | test_expect_success 'git clean -d -x' ' |
ec0e0f25 MS |
281 | |
282 | mkdir -p build docs && | |
283 | touch a.out src/part3.c docs/manual.txt obj.o build/lib.so && | |
47a528ad | 284 | git clean -d -x && |
ec0e0f25 MS |
285 | test -f Makefile && |
286 | test -f README && | |
287 | test -f src/part1.c && | |
288 | test -f src/part2.c && | |
289 | test ! -f a.out && | |
290 | test ! -f src/part3.c && | |
291 | test ! -d docs && | |
292 | test ! -f obj.o && | |
293 | test ! -d build | |
294 | ||
295 | ' | |
296 | ||
5bd8e2d8 KB |
297 | test_expect_success 'git clean -d -x with ignored tracked directory' ' |
298 | ||
299 | mkdir -p build docs && | |
300 | touch a.out src/part3.c docs/manual.txt obj.o build/lib.so && | |
301 | git clean -d -x -e src && | |
302 | test -f Makefile && | |
303 | test -f README && | |
304 | test -f src/part1.c && | |
305 | test -f src/part2.c && | |
306 | test ! -f a.out && | |
307 | test -f src/part3.c && | |
308 | test ! -d docs && | |
309 | test ! -f obj.o && | |
310 | test ! -d build | |
311 | ||
312 | ' | |
313 | ||
47a528ad | 314 | test_expect_success 'git clean -X' ' |
ec0e0f25 MS |
315 | |
316 | mkdir -p build docs && | |
317 | touch a.out src/part3.c docs/manual.txt obj.o build/lib.so && | |
47a528ad | 318 | git clean -X && |
ec0e0f25 MS |
319 | test -f Makefile && |
320 | test -f README && | |
321 | test -f src/part1.c && | |
322 | test -f src/part2.c && | |
323 | test -f a.out && | |
324 | test -f src/part3.c && | |
325 | test -f docs/manual.txt && | |
326 | test ! -f obj.o && | |
327 | test -f build/lib.so | |
328 | ||
329 | ' | |
330 | ||
47a528ad | 331 | test_expect_success 'git clean -d -X' ' |
ec0e0f25 MS |
332 | |
333 | mkdir -p build docs && | |
334 | touch a.out src/part3.c docs/manual.txt obj.o build/lib.so && | |
47a528ad | 335 | git clean -d -X && |
ec0e0f25 MS |
336 | test -f Makefile && |
337 | test -f README && | |
338 | test -f src/part1.c && | |
339 | test -f src/part2.c && | |
340 | test -f a.out && | |
341 | test -f src/part3.c && | |
342 | test -f docs/manual.txt && | |
343 | test ! -f obj.o && | |
344 | test ! -d build | |
345 | ||
346 | ' | |
347 | ||
5bd8e2d8 KB |
348 | test_expect_success 'git clean -d -X with ignored tracked directory' ' |
349 | ||
350 | mkdir -p build docs && | |
351 | touch a.out src/part3.c docs/manual.txt obj.o build/lib.so && | |
352 | git clean -d -X -e src && | |
353 | test -f Makefile && | |
354 | test -f README && | |
355 | test -f src/part1.c && | |
356 | test -f src/part2.c && | |
357 | test -f a.out && | |
358 | test ! -f src/part3.c && | |
359 | test -f docs/manual.txt && | |
360 | test ! -f obj.o && | |
361 | test ! -d build | |
362 | ||
363 | ' | |
364 | ||
562ca192 JH |
365 | test_expect_success 'clean.requireForce defaults to true' ' |
366 | ||
367 | git config --unset clean.requireForce && | |
d492b31c | 368 | test_must_fail git clean |
562ca192 JH |
369 | |
370 | ' | |
371 | ||
ec0e0f25 MS |
372 | test_expect_success 'clean.requireForce' ' |
373 | ||
5be60078 | 374 | git config clean.requireForce true && |
d492b31c | 375 | test_must_fail git clean |
ec0e0f25 MS |
376 | |
377 | ' | |
378 | ||
379 | test_expect_success 'clean.requireForce and -n' ' | |
380 | ||
381 | mkdir -p build docs && | |
382 | touch a.out src/part3.c docs/manual.txt obj.o build/lib.so && | |
47a528ad | 383 | git clean -n && |
ec0e0f25 MS |
384 | test -f Makefile && |
385 | test -f README && | |
386 | test -f src/part1.c && | |
387 | test -f src/part2.c && | |
388 | test -f a.out && | |
389 | test -f src/part3.c && | |
390 | test -f docs/manual.txt && | |
391 | test -f obj.o && | |
392 | test -f build/lib.so | |
393 | ||
394 | ' | |
395 | ||
396 | test_expect_success 'clean.requireForce and -f' ' | |
397 | ||
47a528ad | 398 | git clean -f && |
ec0e0f25 MS |
399 | test -f README && |
400 | test -f src/part1.c && | |
401 | test -f src/part2.c && | |
402 | test ! -f a.out && | |
403 | test ! -f src/part3.c && | |
404 | test -f docs/manual.txt && | |
405 | test -f obj.o && | |
406 | test -f build/lib.so | |
407 | ||
408 | ' | |
409 | ||
a926c4b9 | 410 | test_expect_success 'core.excludesfile' ' |
b57321f5 JH |
411 | |
412 | echo excludes >excludes && | |
413 | echo included >included && | |
414 | git config core.excludesfile excludes && | |
415 | output=$(git clean -n excludes included 2>&1) && | |
416 | expr "$output" : ".*included" >/dev/null && | |
417 | ! expr "$output" : ".*excludes" >/dev/null | |
418 | ||
419 | ' | |
420 | ||
c91cfd19 | 421 | test_expect_success SANITY 'removal failure' ' |
aa9c83c2 MV |
422 | |
423 | mkdir foo && | |
424 | touch foo/bar && | |
45067fc9 | 425 | test_when_finished "chmod 755 foo" && |
e2c24076 JS |
426 | (exec <foo/bar && |
427 | chmod 0 foo && | |
45067fc9 | 428 | test_must_fail git clean -f -d) |
aa9c83c2 | 429 | ' |
aa9c83c2 | 430 | |
a0f4afbe | 431 | test_expect_success 'nested git work tree' ' |
ae2f203e JH |
432 | rm -fr foo bar baz && |
433 | mkdir -p foo bar baz/boo && | |
a0f4afbe JH |
434 | ( |
435 | cd foo && | |
436 | git init && | |
1733ed3d | 437 | test_commit nested hello.world |
a0f4afbe JH |
438 | ) && |
439 | ( | |
440 | cd bar && | |
441 | >goodbye.people | |
442 | ) && | |
ae2f203e JH |
443 | ( |
444 | cd baz/boo && | |
445 | git init && | |
1733ed3d | 446 | test_commit deeply.nested deeper.world |
ae2f203e | 447 | ) && |
a0f4afbe JH |
448 | git clean -f -d && |
449 | test -f foo/.git/index && | |
450 | test -f foo/hello.world && | |
ae2f203e JH |
451 | test -f baz/boo/.git/index && |
452 | test -f baz/boo/deeper.world && | |
a0f4afbe JH |
453 | ! test -d bar |
454 | ' | |
455 | ||
0179ca7a | 456 | test_expect_success 'should clean things that almost look like git but are not' ' |
91479b9c EE |
457 | rm -fr almost_git almost_bare_git almost_submodule && |
458 | mkdir -p almost_git/.git/objects && | |
459 | mkdir -p almost_git/.git/refs && | |
460 | cat >almost_git/.git/HEAD <<-\EOF && | |
461 | garbage | |
462 | EOF | |
463 | cp -r almost_git/.git/ almost_bare_git && | |
464 | mkdir almost_submodule/ && | |
465 | cat >almost_submodule/.git <<-\EOF && | |
466 | garbage | |
467 | EOF | |
468 | test_when_finished "rm -rf almost_*" && | |
91479b9c EE |
469 | git clean -f -d && |
470 | test_path_is_missing almost_git && | |
471 | test_path_is_missing almost_bare_git && | |
472 | test_path_is_missing almost_submodule | |
473 | ' | |
474 | ||
475 | test_expect_success 'should not clean submodules' ' | |
476 | rm -fr repo to_clean sub1 sub2 && | |
477 | mkdir repo to_clean && | |
478 | ( | |
479 | cd repo && | |
480 | git init && | |
481 | test_commit msg hello.world | |
482 | ) && | |
0d3beb71 | 483 | test_config_global protocol.file.allow always && |
91479b9c EE |
484 | git submodule add ./repo/.git sub1 && |
485 | git commit -m "sub1" && | |
486 | git branch before_sub2 && | |
487 | git submodule add ./repo/.git sub2 && | |
488 | git commit -m "sub2" && | |
489 | git checkout before_sub2 && | |
490 | >to_clean/should_clean.this && | |
491 | git clean -f -d && | |
492 | test_path_is_file repo/.git/index && | |
493 | test_path_is_file repo/hello.world && | |
494 | test_path_is_file sub1/.git && | |
495 | test_path_is_file sub1/hello.world && | |
496 | test_path_is_file sub2/.git && | |
497 | test_path_is_file sub2/hello.world && | |
498 | test_path_is_missing to_clean | |
499 | ' | |
500 | ||
cadfbef9 | 501 | test_expect_success POSIXPERM,SANITY 'should avoid cleaning possible submodules' ' |
91479b9c EE |
502 | rm -fr to_clean possible_sub1 && |
503 | mkdir to_clean possible_sub1 && | |
504 | test_when_finished "rm -rf possible_sub*" && | |
505 | echo "gitdir: foo" >possible_sub1/.git && | |
506 | >possible_sub1/hello.world && | |
507 | chmod 0 possible_sub1/.git && | |
508 | >to_clean/should_clean.this && | |
509 | git clean -f -d && | |
510 | test_path_is_file possible_sub1/.git && | |
511 | test_path_is_file possible_sub1/hello.world && | |
512 | test_path_is_missing to_clean | |
513 | ' | |
514 | ||
0179ca7a | 515 | test_expect_success 'nested (empty) git should be kept' ' |
91479b9c EE |
516 | rm -fr empty_repo to_clean && |
517 | git init empty_repo && | |
518 | mkdir to_clean && | |
519 | >to_clean/should_clean.this && | |
390c5b07 PS |
520 | # Note that we put the expect file in the .git directory so that it |
521 | # does not get cleaned. | |
522 | find empty_repo | sort >.git/expect && | |
91479b9c | 523 | git clean -f -d && |
390c5b07 PS |
524 | find empty_repo | sort >actual && |
525 | test_cmp .git/expect actual && | |
91479b9c EE |
526 | test_path_is_missing to_clean |
527 | ' | |
528 | ||
529 | test_expect_success 'nested bare repositories should be cleaned' ' | |
530 | rm -fr bare1 bare2 subdir && | |
531 | git init --bare bare1 && | |
532 | git clone --local --bare . bare2 && | |
533 | mkdir subdir && | |
534 | cp -r bare2 subdir/bare3 && | |
535 | git clean -f -d && | |
536 | test_path_is_missing bare1 && | |
537 | test_path_is_missing bare2 && | |
538 | test_path_is_missing subdir | |
539 | ' | |
540 | ||
0179ca7a | 541 | test_expect_failure 'nested (empty) bare repositories should be cleaned even when in .git' ' |
91479b9c EE |
542 | rm -fr strange_bare && |
543 | mkdir strange_bare && | |
544 | git init --bare strange_bare/.git && | |
545 | git clean -f -d && | |
546 | test_path_is_missing strange_bare | |
547 | ' | |
548 | ||
549 | test_expect_failure 'nested (non-empty) bare repositories should be cleaned even when in .git' ' | |
550 | rm -fr strange_bare && | |
551 | mkdir strange_bare && | |
552 | git clone --local --bare . strange_bare/.git && | |
553 | git clean -f -d && | |
554 | test_path_is_missing strange_bare | |
555 | ' | |
556 | ||
09487f2c | 557 | test_expect_success 'giving path in nested git work tree will NOT remove it' ' |
91479b9c EE |
558 | rm -fr repo && |
559 | mkdir repo && | |
560 | ( | |
561 | cd repo && | |
562 | git init && | |
563 | mkdir -p bar/baz && | |
564 | test_commit msg bar/baz/hello.world | |
565 | ) && | |
390c5b07 | 566 | find repo | sort >expect && |
91479b9c | 567 | git clean -f -d repo/bar/baz && |
390c5b07 PS |
568 | find repo | sort >actual && |
569 | test_cmp expect actual | |
91479b9c EE |
570 | ' |
571 | ||
572 | test_expect_success 'giving path to nested .git will not remove it' ' | |
573 | rm -fr repo && | |
574 | mkdir repo untracked && | |
575 | ( | |
576 | cd repo && | |
577 | git init && | |
578 | test_commit msg hello.world | |
579 | ) && | |
390c5b07 | 580 | find repo | sort >expect && |
91479b9c | 581 | git clean -f -d repo/.git && |
390c5b07 PS |
582 | find repo | sort >actual && |
583 | test_cmp expect actual && | |
91479b9c EE |
584 | test_path_is_dir untracked/ |
585 | ' | |
586 | ||
09487f2c | 587 | test_expect_success 'giving path to nested .git/ will NOT remove contents' ' |
91479b9c EE |
588 | rm -fr repo untracked && |
589 | mkdir repo untracked && | |
590 | ( | |
591 | cd repo && | |
592 | git init && | |
593 | test_commit msg hello.world | |
594 | ) && | |
390c5b07 | 595 | find repo | sort >expect && |
91479b9c | 596 | git clean -f -d repo/.git/ && |
390c5b07 PS |
597 | find repo | sort >actual && |
598 | test_cmp expect actual && | |
91479b9c EE |
599 | test_path_is_dir untracked/ |
600 | ' | |
601 | ||
a0f4afbe | 602 | test_expect_success 'force removal of nested git work tree' ' |
ae2f203e JH |
603 | rm -fr foo bar baz && |
604 | mkdir -p foo bar baz/boo && | |
a0f4afbe JH |
605 | ( |
606 | cd foo && | |
607 | git init && | |
1733ed3d | 608 | test_commit nested hello.world |
a0f4afbe JH |
609 | ) && |
610 | ( | |
611 | cd bar && | |
612 | >goodbye.people | |
613 | ) && | |
ae2f203e JH |
614 | ( |
615 | cd baz/boo && | |
616 | git init && | |
1733ed3d | 617 | test_commit deeply.nested deeper.world |
ae2f203e | 618 | ) && |
a0f4afbe JH |
619 | git clean -f -f -d && |
620 | ! test -d foo && | |
ae2f203e JH |
621 | ! test -d bar && |
622 | ! test -d baz | |
a0f4afbe JH |
623 | ' |
624 | ||
2c76c3fe JH |
625 | test_expect_success 'git clean -e' ' |
626 | rm -fr repo && | |
627 | mkdir repo && | |
628 | ( | |
629 | cd repo && | |
630 | git init && | |
84d69402 | 631 | touch known 1 2 3 && |
2c76c3fe JH |
632 | git add known && |
633 | git clean -f -e 1 -e 2 && | |
634 | test -e 1 && | |
635 | test -e 2 && | |
636 | ! (test -e 3) && | |
637 | test -e known | |
638 | ) | |
639 | ' | |
640 | ||
0235017e AR |
641 | test_expect_success SANITY 'git clean -d with an unreadable empty directory' ' |
642 | mkdir foo && | |
643 | chmod a= foo && | |
644 | git clean -dfx foo && | |
645 | ! test -d foo | |
646 | ' | |
647 | ||
cf424f5f JK |
648 | test_expect_success 'git clean -d respects pathspecs (dir is prefix of pathspec)' ' |
649 | mkdir -p foo && | |
650 | mkdir -p foobar && | |
651 | git clean -df foobar && | |
652 | test_path_is_dir foo && | |
653 | test_path_is_missing foobar | |
654 | ' | |
655 | ||
656 | test_expect_success 'git clean -d respects pathspecs (pathspec is prefix of dir)' ' | |
657 | mkdir -p foo && | |
658 | mkdir -p foobar && | |
659 | git clean -df foo && | |
660 | test_path_is_missing foo && | |
661 | test_path_is_dir foobar | |
662 | ' | |
663 | ||
6b1db431 | 664 | test_expect_success 'git clean -d skips untracked dirs containing ignored files' ' |
b3487ccc SL |
665 | echo /foo/bar >.gitignore && |
666 | echo ignoreme >>.gitignore && | |
667 | rm -rf foo && | |
668 | mkdir -p foo/a/aa/aaa foo/b/bb/bbb && | |
669 | touch foo/bar foo/baz foo/a/aa/ignoreme foo/b/ignoreme foo/b/bb/1 foo/b/bb/2 && | |
670 | git clean -df && | |
671 | test_path_is_dir foo && | |
672 | test_path_is_file foo/bar && | |
673 | test_path_is_missing foo/baz && | |
674 | test_path_is_file foo/a/aa/ignoreme && | |
675 | test_path_is_missing foo/a/aa/aaa && | |
676 | test_path_is_file foo/b/ignoreme && | |
677 | test_path_is_missing foo/b/bb | |
678 | ' | |
679 | ||
09487f2c | 680 | test_expect_success 'git clean -d skips nested repo containing ignored files' ' |
502c386f SG |
681 | test_when_finished "rm -rf nested-repo-with-ignored-file" && |
682 | ||
683 | git init nested-repo-with-ignored-file && | |
684 | ( | |
685 | cd nested-repo-with-ignored-file && | |
686 | >file && | |
687 | git add file && | |
688 | git commit -m Initial && | |
689 | ||
690 | # This file is ignored by a .gitignore rule in the outer repo | |
691 | # added in the previous test. | |
692 | >ignoreme | |
693 | ) && | |
694 | ||
695 | git clean -fd && | |
696 | ||
697 | test_path_is_file nested-repo-with-ignored-file/.git/index && | |
698 | test_path_is_file nested-repo-with-ignored-file/ignoreme && | |
699 | test_path_is_file nested-repo-with-ignored-file/file | |
700 | ' | |
701 | ||
89a1f4aa | 702 | test_expect_success 'git clean handles being told what to clean' ' |
7541cc53 EN |
703 | mkdir -p d1 d2 && |
704 | touch d1/ut d2/ut && | |
705 | git clean -f */ut && | |
706 | test_path_is_missing d1/ut && | |
707 | test_path_is_missing d2/ut | |
708 | ' | |
709 | ||
404ebced | 710 | test_expect_success 'git clean handles being told what to clean, with -d' ' |
7541cc53 EN |
711 | mkdir -p d1 d2 && |
712 | touch d1/ut d2/ut && | |
713 | git clean -ffd */ut && | |
714 | test_path_is_missing d1/ut && | |
715 | test_path_is_missing d2/ut | |
716 | ' | |
717 | ||
89a1f4aa | 718 | test_expect_success 'git clean works if a glob is passed without -d' ' |
7541cc53 EN |
719 | mkdir -p d1 d2 && |
720 | touch d1/ut d2/ut && | |
721 | git clean -f "*ut" && | |
722 | test_path_is_missing d1/ut && | |
723 | test_path_is_missing d2/ut | |
724 | ' | |
725 | ||
404ebced | 726 | test_expect_success 'git clean works if a glob is passed with -d' ' |
7541cc53 EN |
727 | mkdir -p d1 d2 && |
728 | touch d1/ut d2/ut && | |
729 | git clean -ffd "*ut" && | |
730 | test_path_is_missing d1/ut && | |
731 | test_path_is_missing d2/ut | |
732 | ' | |
733 | ||
b09364c4 JS |
734 | test_expect_success MINGW 'handle clean & core.longpaths = false nicely' ' |
735 | test_config core.longpaths false && | |
736 | a50=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && | |
737 | mkdir -p $a50$a50/$a50$a50/$a50$a50 && | |
738 | : >"$a50$a50/test.txt" 2>"$a50$a50/$a50$a50/$a50$a50/test.txt" && | |
739 | # create a temporary outside the working tree to hide from "git clean" | |
740 | test_must_fail git clean -xdf 2>.git/err && | |
741 | # grepping for a strerror string is unportable but it is OK here with | |
742 | # MINGW prereq | |
6789275d | 743 | test_grep "too long" .git/err |
b09364c4 JS |
744 | ' |
745 | ||
22705334 | 746 | test_expect_success 'clean untracked paths by pathspec' ' |
f365bf40 DS |
747 | git init untracked && |
748 | mkdir untracked/dir && | |
749 | echo >untracked/dir/file.txt && | |
750 | git -C untracked clean -f dir/file.txt && | |
751 | ls untracked/dir >actual && | |
752 | test_must_be_empty actual | |
753 | ' | |
754 | ||
aa6e1b21 | 755 | test_expect_success 'avoid traversing into ignored directories' ' |
2e4e43a6 EN |
756 | test_when_finished rm -f output error trace.* && |
757 | test_create_repo avoid-traversing-deep-hierarchy && | |
758 | ( | |
759 | cd avoid-traversing-deep-hierarchy && | |
760 | ||
761 | mkdir -p untracked/subdir/with/a && | |
762 | >untracked/subdir/with/a/random-file.txt && | |
763 | ||
764 | GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \ | |
765 | git clean -ffdxn -e untracked | |
766 | ) && | |
767 | ||
768 | # Make sure we only visited into the top-level directory, and did | |
769 | # not traverse into the "untracked" subdirectory since it was excluded | |
770 | grep data.*read_directo.*directories-visited trace.output | | |
771 | cut -d "|" -f 9 >trace.relevant && | |
772 | cat >trace.expect <<-EOF && | |
773 | ..directories-visited:1 | |
774 | EOF | |
775 | test_cmp trace.expect trace.relevant | |
776 | ' | |
777 | ||
dd55fc0d | 778 | test_expect_success 'traverse into directories that may have ignored entries' ' |
a97c7a8b EN |
779 | test_when_finished rm -f output && |
780 | test_create_repo need-to-traverse-into-hierarchy && | |
781 | ( | |
782 | cd need-to-traverse-into-hierarchy && | |
783 | mkdir -p modules/foobar/src/generated && | |
784 | > modules/foobar/src/generated/code.c && | |
785 | > modules/foobar/Makefile && | |
786 | echo "/modules/**/src/generated/" >.gitignore && | |
787 | ||
788 | git clean -fX modules/foobar >../output && | |
789 | ||
790 | grep Removing ../output && | |
791 | ||
792 | test_path_is_missing modules/foobar/src/generated/code.c && | |
793 | test_path_is_file modules/foobar/Makefile | |
794 | ) | |
795 | ' | |
796 | ||
ec0e0f25 | 797 | test_done |