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
'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 |
121 sed -n -e "s|^Would remove ||p"
123 test "$would_clean" = ../src/part3.c
126 test_expect_success
'git clean with absolute path' '
128 mkdir -p build docs &&
129 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
132 git clean -n "$(pwd)/../src" |
134 sed -n -e "s|^Would remove ||p"
136 test "$would_clean" = ../src/part3.c
139 test_expect_success
'git clean with out of work tree relative path' '
141 mkdir -p build docs &&
142 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
145 test_must_fail git clean -n ../..
149 test_expect_success
'git clean with out of work tree absolute path' '
151 mkdir -p build docs &&
152 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
153 dd=$(cd .. && pwd) &&
156 test_must_fail git clean -n $dd
160 test_expect_success
'git clean -d with prefix and path' '
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 &&
164 (cd src/ && git clean -d feature/) &&
167 test -f src/part1.c &&
168 test -f src/part2.c &&
170 test -f src/part3.c &&
171 test ! -f src/feature/file.c &&
172 test -f docs/manual.txt &&
178 test_expect_success SYMLINKS
'git clean symbolic link' '
180 mkdir -p build docs &&
181 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
182 ln -s docs/manual.txt src/part4.c &&
186 test -f src/part1.c &&
187 test -f src/part2.c &&
189 test ! -f src/part3.c &&
190 test ! -f src/part4.c &&
191 test -f docs/manual.txt &&
197 test_expect_success
'git clean with wildcard' '
199 touch a.clean b.clean other.c &&
200 git clean "*.clean" &&
203 test -f src/part1.c &&
204 test -f src/part2.c &&
211 test_expect_success
'git clean -n' '
213 mkdir -p build docs &&
214 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
218 test -f src/part1.c &&
219 test -f src/part2.c &&
221 test -f src/part3.c &&
222 test -f docs/manual.txt &&
228 test_expect_success
'git clean -d' '
230 mkdir -p build docs &&
231 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
235 test -f src/part1.c &&
236 test -f src/part2.c &&
238 test ! -f src/part3.c &&
245 test_expect_success
'git clean -d src/ examples/' '
247 mkdir -p build docs examples &&
248 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so examples/1.c &&
249 git clean -d src/ examples/ &&
252 test -f src/part1.c &&
253 test -f src/part2.c &&
255 test ! -f src/part3.c &&
256 test ! -f examples/1.c &&
257 test -f docs/manual.txt &&
263 test_expect_success
'git clean -x' '
265 mkdir -p build docs &&
266 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
270 test -f src/part1.c &&
271 test -f src/part2.c &&
273 test ! -f src/part3.c &&
274 test -f docs/manual.txt &&
280 test_expect_success
'git clean -d -x' '
282 mkdir -p build docs &&
283 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
287 test -f src/part1.c &&
288 test -f src/part2.c &&
290 test ! -f src/part3.c &&
297 test_expect_success
'git clean -d -x with ignored tracked directory' '
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 &&
304 test -f src/part1.c &&
305 test -f src/part2.c &&
307 test -f src/part3.c &&
314 test_expect_success
'git clean -X' '
316 mkdir -p build docs &&
317 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
321 test -f src/part1.c &&
322 test -f src/part2.c &&
324 test -f src/part3.c &&
325 test -f docs/manual.txt &&
331 test_expect_success
'git clean -d -X' '
333 mkdir -p build docs &&
334 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
338 test -f src/part1.c &&
339 test -f src/part2.c &&
341 test -f src/part3.c &&
342 test -f docs/manual.txt &&
348 test_expect_success
'git clean -d -X with ignored tracked directory' '
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 &&
355 test -f src/part1.c &&
356 test -f src/part2.c &&
358 test ! -f src/part3.c &&
359 test -f docs/manual.txt &&
365 test_expect_success
'clean.requireForce defaults to true' '
367 git config --unset clean.requireForce &&
368 test_must_fail git clean
372 test_expect_success
'clean.requireForce' '
374 git config clean.requireForce true &&
375 test_must_fail git clean
379 test_expect_success
'clean.requireForce and -n' '
381 mkdir -p build docs &&
382 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
386 test -f src/part1.c &&
387 test -f src/part2.c &&
389 test -f src/part3.c &&
390 test -f docs/manual.txt &&
396 test_expect_success
'clean.requireForce and -f' '
400 test -f src/part1.c &&
401 test -f src/part2.c &&
403 test ! -f src/part3.c &&
404 test -f docs/manual.txt &&
410 test_expect_success
'clean.requireForce and --interactive' '
411 git clean --interactive </dev/null >output 2>error &&
412 test_grep ! "requireForce is true and" error &&
413 test_grep "\*\*\* Commands \*\*\*" output
416 test_expect_success
'core.excludesfile' '
418 echo excludes >excludes &&
419 echo included >included &&
420 git config core.excludesfile excludes &&
421 output=$(git clean -n excludes included 2>&1) &&
422 expr "$output" : ".*included" >/dev/null &&
423 ! expr "$output" : ".*excludes" >/dev/null
427 test_expect_success SANITY
'removal failure' '
431 test_when_finished "chmod 755 foo" &&
434 test_must_fail git clean -f -d)
437 test_expect_success
'nested git work tree' '
438 rm -fr foo bar baz &&
439 mkdir -p foo bar baz/boo &&
443 test_commit nested hello.world
452 test_commit deeply.nested deeper.world
455 test -f foo/.git/index &&
456 test -f foo/hello.world &&
457 test -f baz/boo/.git/index &&
458 test -f baz/boo/deeper.world &&
462 test_expect_success
'should clean things that almost look like git but are not' '
463 rm -fr almost_git almost_bare_git almost_submodule &&
464 mkdir -p almost_git/.git/objects &&
465 mkdir -p almost_git/.git/refs &&
466 cat >almost_git/.git/HEAD <<-\EOF &&
469 cp -r almost_git/.git/ almost_bare_git &&
470 mkdir almost_submodule/ &&
471 cat >almost_submodule/.git <<-\EOF &&
474 test_when_finished "rm -rf almost_*" &&
476 test_path_is_missing almost_git &&
477 test_path_is_missing almost_bare_git &&
478 test_path_is_missing almost_submodule
481 test_expect_success
'should not clean submodules' '
482 rm -fr repo to_clean sub1 sub2 &&
483 mkdir repo to_clean &&
487 test_commit msg hello.world
489 test_config_global protocol.file.allow always &&
490 git submodule add ./repo/.git sub1 &&
491 git commit -m "sub1" &&
492 git branch before_sub2 &&
493 git submodule add ./repo/.git sub2 &&
494 git commit -m "sub2" &&
495 git checkout before_sub2 &&
496 >to_clean/should_clean.this &&
498 test_path_is_file repo/.git/index &&
499 test_path_is_file repo/hello.world &&
500 test_path_is_file sub1/.git &&
501 test_path_is_file sub1/hello.world &&
502 test_path_is_file sub2/.git &&
503 test_path_is_file sub2/hello.world &&
504 test_path_is_missing to_clean
507 test_expect_success POSIXPERM
,SANITY
'should avoid cleaning possible submodules' '
508 rm -fr to_clean possible_sub1 &&
509 mkdir to_clean possible_sub1 &&
510 test_when_finished "rm -rf possible_sub*" &&
511 echo "gitdir: foo" >possible_sub1/.git &&
512 >possible_sub1/hello.world &&
513 chmod 0 possible_sub1/.git &&
514 >to_clean/should_clean.this &&
516 test_path_is_file possible_sub1/.git &&
517 test_path_is_file possible_sub1/hello.world &&
518 test_path_is_missing to_clean
521 test_expect_success
'nested (empty) git should be kept' '
522 rm -fr empty_repo to_clean &&
523 git init empty_repo &&
525 >to_clean/should_clean.this &&
526 # Note that we put the expect file in the .git directory so that it
527 # does not get cleaned.
528 find empty_repo | sort >.git/expect &&
530 find empty_repo | sort >actual &&
531 test_cmp .git/expect actual &&
532 test_path_is_missing to_clean
535 test_expect_success
'nested bare repositories should be cleaned' '
536 rm -fr bare1 bare2 subdir &&
537 git init --bare bare1 &&
538 git clone --local --bare . bare2 &&
540 cp -r bare2 subdir/bare3 &&
542 test_path_is_missing bare1 &&
543 test_path_is_missing bare2 &&
544 test_path_is_missing subdir
547 test_expect_failure
'nested (empty) bare repositories should be cleaned even when in .git' '
548 rm -fr strange_bare &&
549 mkdir strange_bare &&
550 git init --bare strange_bare/.git &&
552 test_path_is_missing strange_bare
555 test_expect_failure
'nested (non-empty) bare repositories should be cleaned even when in .git' '
556 rm -fr strange_bare &&
557 mkdir strange_bare &&
558 git clone --local --bare . strange_bare/.git &&
560 test_path_is_missing strange_bare
563 test_expect_success
'giving path in nested git work tree will NOT remove it' '
570 test_commit msg bar/baz/hello.world
572 find repo | sort >expect &&
573 git clean -f -d repo/bar/baz &&
574 find repo | sort >actual &&
575 test_cmp expect actual
578 test_expect_success
'giving path to nested .git will not remove it' '
580 mkdir repo untracked &&
584 test_commit msg hello.world
586 find repo | sort >expect &&
587 git clean -f -d repo/.git &&
588 find repo | sort >actual &&
589 test_cmp expect actual &&
590 test_path_is_dir untracked/
593 test_expect_success
'giving path to nested .git/ will NOT remove contents' '
594 rm -fr repo untracked &&
595 mkdir repo untracked &&
599 test_commit msg hello.world
601 find repo | sort >expect &&
602 git clean -f -d repo/.git/ &&
603 find repo | sort >actual &&
604 test_cmp expect actual &&
605 test_path_is_dir untracked/
608 test_expect_success
'force removal of nested git work tree' '
609 rm -fr foo bar baz &&
610 mkdir -p foo bar baz/boo &&
614 test_commit nested hello.world
623 test_commit deeply.nested deeper.world
625 git clean -f -f -d &&
631 test_expect_success
'git clean -e' '
639 git clean -f -e 1 -e 2 &&
647 test_expect_success SANITY
'git clean -d with an unreadable empty directory' '
650 git clean -dfx foo &&
654 test_expect_success
'git clean -d respects pathspecs (dir is prefix of pathspec)' '
657 git clean -df foobar &&
658 test_path_is_dir foo &&
659 test_path_is_missing foobar
662 test_expect_success
'git clean -d respects pathspecs (pathspec is prefix of dir)' '
666 test_path_is_missing foo &&
667 test_path_is_dir foobar
670 test_expect_success
'git clean -d skips untracked dirs containing ignored files' '
671 echo /foo/bar >.gitignore &&
672 echo ignoreme >>.gitignore &&
674 mkdir -p foo/a/aa/aaa foo/b/bb/bbb &&
675 touch foo/bar foo/baz foo/a/aa/ignoreme foo/b/ignoreme foo/b/bb/1 foo/b/bb/2 &&
677 test_path_is_dir foo &&
678 test_path_is_file foo/bar &&
679 test_path_is_missing foo/baz &&
680 test_path_is_file foo/a/aa/ignoreme &&
681 test_path_is_missing foo/a/aa/aaa &&
682 test_path_is_file foo/b/ignoreme &&
683 test_path_is_missing foo/b/bb
686 test_expect_success
'git clean -d skips nested repo containing ignored files' '
687 test_when_finished "rm -rf nested-repo-with-ignored-file" &&
689 git init nested-repo-with-ignored-file &&
691 cd nested-repo-with-ignored-file &&
694 git commit -m Initial &&
696 # This file is ignored by a .gitignore rule in the outer repo
697 # added in the previous test.
703 test_path_is_file nested-repo-with-ignored-file/.git/index &&
704 test_path_is_file nested-repo-with-ignored-file/ignoreme &&
705 test_path_is_file nested-repo-with-ignored-file/file
708 test_expect_success
'git clean handles being told what to clean' '
712 test_path_is_missing d1/ut &&
713 test_path_is_missing d2/ut
716 test_expect_success
'git clean handles being told what to clean, with -d' '
719 git clean -ffd */ut &&
720 test_path_is_missing d1/ut &&
721 test_path_is_missing d2/ut
724 test_expect_success
'git clean works if a glob is passed without -d' '
727 git clean -f "*ut" &&
728 test_path_is_missing d1/ut &&
729 test_path_is_missing d2/ut
732 test_expect_success
'git clean works if a glob is passed with -d' '
735 git clean -ffd "*ut" &&
736 test_path_is_missing d1/ut &&
737 test_path_is_missing d2/ut
740 test_expect_success MINGW
'handle clean & core.longpaths = false nicely' '
741 test_config core.longpaths false &&
742 a50=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
743 mkdir -p $a50$a50/$a50$a50/$a50$a50 &&
744 : >"$a50$a50/test.txt" 2>"$a50$a50/$a50$a50/$a50$a50/test.txt" &&
745 # create a temporary outside the working tree to hide from "git clean"
746 test_must_fail git clean -xdf 2>.git/err &&
747 # grepping for a strerror string is unportable but it is OK here with
749 test_grep "too long" .git/err
752 test_expect_success
'clean untracked paths by pathspec' '
753 git init untracked &&
754 mkdir untracked/dir &&
755 echo >untracked/dir/file.txt &&
756 git -C untracked clean -f dir/file.txt &&
757 ls untracked/dir >actual &&
758 test_must_be_empty actual
761 test_expect_success
'avoid traversing into ignored directories' '
762 test_when_finished rm -f output error trace.* &&
763 test_create_repo avoid-traversing-deep-hierarchy &&
765 cd avoid-traversing-deep-hierarchy &&
767 mkdir -p untracked/subdir/with/a &&
768 >untracked/subdir/with/a/random-file.txt &&
770 GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
771 git clean -ffdxn -e untracked
774 # Make sure we only visited into the top-level directory, and did
775 # not traverse into the "untracked" subdirectory since it was excluded
776 grep data.*read_directo.*directories-visited trace.output |
777 cut -d "|" -f 9 >trace.relevant &&
778 cat >trace.expect <<-EOF &&
779 ..directories-visited:1
781 test_cmp trace.expect trace.relevant
784 test_expect_success
'traverse into directories that may have ignored entries' '
785 test_when_finished rm -f output &&
786 test_create_repo need-to-traverse-into-hierarchy &&
788 cd need-to-traverse-into-hierarchy &&
789 mkdir -p modules/foobar/src/generated &&
790 > modules/foobar/src/generated/code.c &&
791 > modules/foobar/Makefile &&
792 echo "/modules/**/src/generated/" >.gitignore &&
794 git clean -fX modules/foobar >../output &&
796 grep Removing ../output &&
798 test_path_is_missing modules/foobar/src/generated/code.c &&
799 test_path_is_file modules/foobar/Makefile