3 # Copyright (c) 2007 Michael Spang
6 test_description
='git clean basic tests'
10 git config clean.requireForce no
12 test_expect_success
'setup' '
15 touch src/part1.c Makefile &&
16 echo build >.gitignore &&
17 echo \*.o >>.gitignore &&
19 git commit -m setup &&
20 touch src/part2.c README &&
25 test_expect_success
'git clean with skip-worktree .gitignore' '
26 git update-index --skip-worktree .gitignore &&
28 mkdir -p build docs &&
29 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
33 test -f src/part1.c &&
34 test -f src/part2.c &&
36 test ! -f src/part3.c &&
37 test -f docs/manual.txt &&
39 test -f build/lib.so &&
40 git update-index --no-skip-worktree .gitignore &&
41 git checkout .gitignore
44 test_expect_success
'git clean' '
46 mkdir -p build docs &&
47 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
51 test -f src/part1.c &&
52 test -f src/part2.c &&
54 test ! -f src/part3.c &&
55 test -f docs/manual.txt &&
61 test_expect_success
'git clean src/' '
63 mkdir -p build docs &&
64 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
68 test -f src/part1.c &&
69 test -f src/part2.c &&
71 test ! -f src/part3.c &&
72 test -f docs/manual.txt &&
78 test_expect_success
'git clean src/ src/' '
80 mkdir -p build docs &&
81 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
82 git clean src/ src/ &&
85 test -f src/part1.c &&
86 test -f src/part2.c &&
88 test ! -f src/part3.c &&
89 test -f docs/manual.txt &&
95 test_expect_success
'git clean with prefix' '
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 &&
99 (cd src/ && git clean) &&
102 test -f src/part1.c &&
103 test -f src/part2.c &&
105 test ! -f src/part3.c &&
106 test -f src/test/1.c &&
107 test -f docs/manual.txt &&
113 test_expect_success C_LOCALE_OUTPUT
'git clean with relative prefix' '
115 mkdir -p build docs &&
116 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
119 git clean -n ../src |
120 sed -n -e "s|^Would remove ||p"
122 verbose test "$would_clean" = ../src/part3.c
125 test_expect_success C_LOCALE_OUTPUT
'git clean with absolute path' '
127 mkdir -p build docs &&
128 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
131 git clean -n "$(pwd)/../src" |
132 sed -n -e "s|^Would remove ||p"
134 verbose test "$would_clean" = ../src/part3.c
137 test_expect_success
'git clean with out of work tree relative path' '
139 mkdir -p build docs &&
140 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
143 test_must_fail git clean -n ../..
147 test_expect_success
'git clean with out of work tree absolute path' '
149 mkdir -p build docs &&
150 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
151 dd=$(cd .. && pwd) &&
154 test_must_fail git clean -n $dd
158 test_expect_success
'git clean -d with prefix and path' '
160 mkdir -p build docs src/feature &&
161 touch a.out src/part3.c src/feature/file.c docs/manual.txt obj.o build/lib.so &&
162 (cd src/ && git clean -d feature/) &&
165 test -f src/part1.c &&
166 test -f src/part2.c &&
168 test -f src/part3.c &&
169 test ! -f src/feature/file.c &&
170 test -f docs/manual.txt &&
176 test_expect_success SYMLINKS
'git clean symbolic link' '
178 mkdir -p build docs &&
179 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
180 ln -s docs/manual.txt src/part4.c &&
184 test -f src/part1.c &&
185 test -f src/part2.c &&
187 test ! -f src/part3.c &&
188 test ! -f src/part4.c &&
189 test -f docs/manual.txt &&
195 test_expect_success
'git clean with wildcard' '
197 touch a.clean b.clean other.c &&
198 git clean "*.clean" &&
201 test -f src/part1.c &&
202 test -f src/part2.c &&
209 test_expect_success
'git clean -n' '
211 mkdir -p build docs &&
212 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
216 test -f src/part1.c &&
217 test -f src/part2.c &&
219 test -f src/part3.c &&
220 test -f docs/manual.txt &&
226 test_expect_success
'git clean -d' '
228 mkdir -p build docs &&
229 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
233 test -f src/part1.c &&
234 test -f src/part2.c &&
236 test ! -f src/part3.c &&
243 test_expect_success
'git clean -d src/ examples/' '
245 mkdir -p build docs examples &&
246 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so examples/1.c &&
247 git clean -d src/ examples/ &&
250 test -f src/part1.c &&
251 test -f src/part2.c &&
253 test ! -f src/part3.c &&
254 test ! -f examples/1.c &&
255 test -f docs/manual.txt &&
261 test_expect_success
'git clean -x' '
263 mkdir -p build docs &&
264 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
268 test -f src/part1.c &&
269 test -f src/part2.c &&
271 test ! -f src/part3.c &&
272 test -f docs/manual.txt &&
278 test_expect_success
'git clean -d -x' '
280 mkdir -p build docs &&
281 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
285 test -f src/part1.c &&
286 test -f src/part2.c &&
288 test ! -f src/part3.c &&
295 test_expect_success
'git clean -d -x with ignored tracked directory' '
297 mkdir -p build docs &&
298 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
299 git clean -d -x -e src &&
302 test -f src/part1.c &&
303 test -f src/part2.c &&
305 test -f src/part3.c &&
312 test_expect_success
'git clean -X' '
314 mkdir -p build docs &&
315 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
319 test -f src/part1.c &&
320 test -f src/part2.c &&
322 test -f src/part3.c &&
323 test -f docs/manual.txt &&
329 test_expect_success
'git clean -d -X' '
331 mkdir -p build docs &&
332 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
336 test -f src/part1.c &&
337 test -f src/part2.c &&
339 test -f src/part3.c &&
340 test -f docs/manual.txt &&
346 test_expect_success
'git clean -d -X with ignored tracked directory' '
348 mkdir -p build docs &&
349 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
350 git clean -d -X -e src &&
353 test -f src/part1.c &&
354 test -f src/part2.c &&
356 test ! -f src/part3.c &&
357 test -f docs/manual.txt &&
363 test_expect_success
'clean.requireForce defaults to true' '
365 git config --unset clean.requireForce &&
366 test_must_fail git clean
370 test_expect_success
'clean.requireForce' '
372 git config clean.requireForce true &&
373 test_must_fail git clean
377 test_expect_success
'clean.requireForce and -n' '
379 mkdir -p build docs &&
380 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
384 test -f src/part1.c &&
385 test -f src/part2.c &&
387 test -f src/part3.c &&
388 test -f docs/manual.txt &&
394 test_expect_success
'clean.requireForce and -f' '
398 test -f src/part1.c &&
399 test -f src/part2.c &&
401 test ! -f src/part3.c &&
402 test -f docs/manual.txt &&
408 test_expect_success C_LOCALE_OUTPUT
'core.excludesfile' '
410 echo excludes >excludes &&
411 echo included >included &&
412 git config core.excludesfile excludes &&
413 output=$(git clean -n excludes included 2>&1) &&
414 expr "$output" : ".*included" >/dev/null &&
415 ! expr "$output" : ".*excludes" >/dev/null
419 test_expect_success SANITY
'removal failure' '
423 test_when_finished "chmod 755 foo" &&
426 test_must_fail git clean -f -d)
429 test_expect_success
'nested git work tree' '
430 rm -fr foo bar baz &&
431 mkdir -p foo bar baz/boo &&
437 git commit -a -m nested
448 git commit -a -m deeply.nested
451 test -f foo/.git/index &&
452 test -f foo/hello.world &&
453 test -f baz/boo/.git/index &&
454 test -f baz/boo/deeper.world &&
458 test_expect_success
'should clean things that almost look like git but are not' '
459 rm -fr almost_git almost_bare_git almost_submodule &&
460 mkdir -p almost_git/.git/objects &&
461 mkdir -p almost_git/.git/refs &&
462 cat >almost_git/.git/HEAD <<-\EOF &&
465 cp -r almost_git/.git/ almost_bare_git &&
466 mkdir almost_submodule/ &&
467 cat >almost_submodule/.git <<-\EOF &&
470 test_when_finished "rm -rf almost_*" &&
472 test_path_is_missing almost_git &&
473 test_path_is_missing almost_bare_git &&
474 test_path_is_missing almost_submodule
477 test_expect_success
'should not clean submodules' '
478 rm -fr repo to_clean sub1 sub2 &&
479 mkdir repo to_clean &&
483 test_commit msg hello.world
485 git submodule add ./repo/.git sub1 &&
486 git commit -m "sub1" &&
487 git branch before_sub2 &&
488 git submodule add ./repo/.git sub2 &&
489 git commit -m "sub2" &&
490 git checkout before_sub2 &&
491 >to_clean/should_clean.this &&
493 test_path_is_file repo/.git/index &&
494 test_path_is_file repo/hello.world &&
495 test_path_is_file sub1/.git &&
496 test_path_is_file sub1/hello.world &&
497 test_path_is_file sub2/.git &&
498 test_path_is_file sub2/hello.world &&
499 test_path_is_missing to_clean
502 test_expect_success
'should avoid cleaning possible submodules' '
503 rm -fr to_clean possible_sub1 &&
504 mkdir to_clean possible_sub1 &&
505 test_when_finished "rm -rf possible_sub*" &&
506 echo "gitdir: foo" >possible_sub1/.git &&
507 >possible_sub1/hello.world &&
508 chmod 0 possible_sub1/.git &&
509 >to_clean/should_clean.this &&
511 test_path_is_file possible_sub1/.git &&
512 test_path_is_file possible_sub1/hello.world &&
513 test_path_is_missing to_clean
516 test_expect_success
'nested (empty) git should be kept' '
517 rm -fr empty_repo to_clean &&
518 git init empty_repo &&
520 >to_clean/should_clean.this &&
522 test_path_is_file empty_repo/.git/HEAD &&
523 test_path_is_missing to_clean
526 test_expect_success
'nested bare repositories should be cleaned' '
527 rm -fr bare1 bare2 subdir &&
528 git init --bare bare1 &&
529 git clone --local --bare . bare2 &&
531 cp -r bare2 subdir/bare3 &&
533 test_path_is_missing bare1 &&
534 test_path_is_missing bare2 &&
535 test_path_is_missing subdir
538 test_expect_failure
'nested (empty) bare repositories should be cleaned even when in .git' '
539 rm -fr strange_bare &&
540 mkdir strange_bare &&
541 git init --bare strange_bare/.git &&
543 test_path_is_missing strange_bare
546 test_expect_failure
'nested (non-empty) bare repositories should be cleaned even when in .git' '
547 rm -fr strange_bare &&
548 mkdir strange_bare &&
549 git clone --local --bare . strange_bare/.git &&
551 test_path_is_missing strange_bare
554 test_expect_success
'giving path in nested git work tree will remove it' '
561 test_commit msg bar/baz/hello.world
563 git clean -f -d repo/bar/baz &&
564 test_path_is_file repo/.git/HEAD &&
565 test_path_is_dir repo/bar/ &&
566 test_path_is_missing repo/bar/baz
569 test_expect_success
'giving path to nested .git will not remove it' '
571 mkdir repo untracked &&
575 test_commit msg hello.world
577 git clean -f -d repo/.git &&
578 test_path_is_file repo/.git/HEAD &&
579 test_path_is_dir repo/.git/refs &&
580 test_path_is_dir repo/.git/objects &&
581 test_path_is_dir untracked/
584 test_expect_success
'giving path to nested .git/ will remove contents' '
585 rm -fr repo untracked &&
586 mkdir repo untracked &&
590 test_commit msg hello.world
592 git clean -f -d repo/.git/ &&
593 test_path_is_dir repo/.git &&
594 test_dir_is_empty repo/.git &&
595 test_path_is_dir untracked/
598 test_expect_success
'force removal of nested git work tree' '
599 rm -fr foo bar baz &&
600 mkdir -p foo bar baz/boo &&
606 git commit -a -m nested
617 git commit -a -m deeply.nested
619 git clean -f -f -d &&
625 test_expect_success
'git clean -e' '
633 git clean -f -e 1 -e 2 &&
641 test_expect_success SANITY
'git clean -d with an unreadable empty directory' '
644 git clean -dfx foo &&
648 test_expect_success
'git clean -d respects pathspecs (dir is prefix of pathspec)' '
651 git clean -df foobar &&
652 test_path_is_dir foo &&
653 test_path_is_missing foobar
656 test_expect_success
'git clean -d respects pathspecs (pathspec is prefix of dir)' '
660 test_path_is_missing foo &&
661 test_path_is_dir foobar