Merge branch 'es/worktree-repair-copied' into cw/worktrees-relative
[git/gitster.git] / t / t7300-clean.sh
blob0aae0dee67078b73855d0152d18863e6a9ad104d
1 #!/bin/sh
3 # Copyright (c) 2007 Michael Spang
6 test_description='git clean basic tests'
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
11 git config clean.requireForce no
13 test_expect_success 'setup' '
15 mkdir -p src &&
16 touch src/part1.c Makefile &&
17 echo build >.gitignore &&
18 echo \*.o >>.gitignore &&
19 git add . &&
20 git commit -m setup &&
21 touch src/part2.c README &&
22 git add .
26 test_expect_success 'git clean with skip-worktree .gitignore' '
27 git update-index --skip-worktree .gitignore &&
28 rm .gitignore &&
29 mkdir -p build docs &&
30 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
31 git clean &&
32 test -f Makefile &&
33 test -f README &&
34 test -f src/part1.c &&
35 test -f src/part2.c &&
36 test ! -f a.out &&
37 test ! -f src/part3.c &&
38 test -f docs/manual.txt &&
39 test -f obj.o &&
40 test -f build/lib.so &&
41 git update-index --no-skip-worktree .gitignore &&
42 git checkout .gitignore
45 test_expect_success 'git clean' '
47 mkdir -p build docs &&
48 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
49 git clean &&
50 test -f Makefile &&
51 test -f README &&
52 test -f src/part1.c &&
53 test -f src/part2.c &&
54 test ! -f a.out &&
55 test ! -f src/part3.c &&
56 test -f docs/manual.txt &&
57 test -f obj.o &&
58 test -f build/lib.so
62 test_expect_success 'git clean src/' '
64 mkdir -p build docs &&
65 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
66 git clean src/ &&
67 test -f Makefile &&
68 test -f README &&
69 test -f src/part1.c &&
70 test -f src/part2.c &&
71 test -f a.out &&
72 test ! -f src/part3.c &&
73 test -f docs/manual.txt &&
74 test -f obj.o &&
75 test -f build/lib.so
79 test_expect_success 'git clean src/ src/' '
81 mkdir -p build docs &&
82 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
83 git clean src/ src/ &&
84 test -f Makefile &&
85 test -f README &&
86 test -f src/part1.c &&
87 test -f src/part2.c &&
88 test -f a.out &&
89 test ! -f src/part3.c &&
90 test -f docs/manual.txt &&
91 test -f obj.o &&
92 test -f build/lib.so
96 test_expect_success 'git clean with prefix' '
98 mkdir -p build docs src/test &&
99 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so src/test/1.c &&
100 (cd src/ && git clean) &&
101 test -f Makefile &&
102 test -f README &&
103 test -f src/part1.c &&
104 test -f src/part2.c &&
105 test -f a.out &&
106 test ! -f src/part3.c &&
107 test -f src/test/1.c &&
108 test -f docs/manual.txt &&
109 test -f obj.o &&
110 test -f build/lib.so
114 test_expect_success 'git clean with relative prefix' '
116 mkdir -p build docs &&
117 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
118 would_clean=$(
119 cd docs &&
120 git clean -n ../src |
121 grep part3 |
122 sed -n -e "s|^Would remove ||p"
123 ) &&
124 test "$would_clean" = ../src/part3.c
127 test_expect_success 'git clean with absolute path' '
129 mkdir -p build docs &&
130 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
131 would_clean=$(
132 cd docs &&
133 git clean -n "$(pwd)/../src" |
134 grep part3 |
135 sed -n -e "s|^Would remove ||p"
136 ) &&
137 test "$would_clean" = ../src/part3.c
140 test_expect_success 'git clean with out of work tree relative path' '
142 mkdir -p build docs &&
143 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
145 cd docs &&
146 test_must_fail git clean -n ../..
150 test_expect_success 'git clean with out of work tree absolute path' '
152 mkdir -p build docs &&
153 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
154 dd=$(cd .. && pwd) &&
156 cd docs &&
157 test_must_fail git clean -n $dd
161 test_expect_success 'git clean -d with prefix and path' '
163 mkdir -p build docs src/feature &&
164 touch a.out src/part3.c src/feature/file.c docs/manual.txt obj.o build/lib.so &&
165 (cd src/ && git clean -d feature/) &&
166 test -f Makefile &&
167 test -f README &&
168 test -f src/part1.c &&
169 test -f src/part2.c &&
170 test -f a.out &&
171 test -f src/part3.c &&
172 test ! -f src/feature/file.c &&
173 test -f docs/manual.txt &&
174 test -f obj.o &&
175 test -f build/lib.so
179 test_expect_success SYMLINKS 'git clean symbolic link' '
181 mkdir -p build docs &&
182 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
183 ln -s docs/manual.txt src/part4.c &&
184 git clean &&
185 test -f Makefile &&
186 test -f README &&
187 test -f src/part1.c &&
188 test -f src/part2.c &&
189 test ! -f a.out &&
190 test ! -f src/part3.c &&
191 test ! -f src/part4.c &&
192 test -f docs/manual.txt &&
193 test -f obj.o &&
194 test -f build/lib.so
198 test_expect_success 'git clean with wildcard' '
200 touch a.clean b.clean other.c &&
201 git clean "*.clean" &&
202 test -f Makefile &&
203 test -f README &&
204 test -f src/part1.c &&
205 test -f src/part2.c &&
206 test ! -f a.clean &&
207 test ! -f b.clean &&
208 test -f other.c
212 test_expect_success 'git clean -n' '
214 mkdir -p build docs &&
215 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
216 git clean -n &&
217 test -f Makefile &&
218 test -f README &&
219 test -f src/part1.c &&
220 test -f src/part2.c &&
221 test -f a.out &&
222 test -f src/part3.c &&
223 test -f docs/manual.txt &&
224 test -f obj.o &&
225 test -f build/lib.so
229 test_expect_success 'git clean -d' '
231 mkdir -p build docs &&
232 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
233 git clean -d &&
234 test -f Makefile &&
235 test -f README &&
236 test -f src/part1.c &&
237 test -f src/part2.c &&
238 test ! -f a.out &&
239 test ! -f src/part3.c &&
240 test ! -d docs &&
241 test -f obj.o &&
242 test -f build/lib.so
246 test_expect_success 'git clean -d src/ examples/' '
248 mkdir -p build docs examples &&
249 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so examples/1.c &&
250 git clean -d src/ examples/ &&
251 test -f Makefile &&
252 test -f README &&
253 test -f src/part1.c &&
254 test -f src/part2.c &&
255 test -f a.out &&
256 test ! -f src/part3.c &&
257 test ! -f examples/1.c &&
258 test -f docs/manual.txt &&
259 test -f obj.o &&
260 test -f build/lib.so
264 test_expect_success 'git clean -x' '
266 mkdir -p build docs &&
267 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
268 git clean -x &&
269 test -f Makefile &&
270 test -f README &&
271 test -f src/part1.c &&
272 test -f src/part2.c &&
273 test ! -f a.out &&
274 test ! -f src/part3.c &&
275 test -f docs/manual.txt &&
276 test ! -f obj.o &&
277 test -f build/lib.so
281 test_expect_success 'git clean -d -x' '
283 mkdir -p build docs &&
284 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
285 git clean -d -x &&
286 test -f Makefile &&
287 test -f README &&
288 test -f src/part1.c &&
289 test -f src/part2.c &&
290 test ! -f a.out &&
291 test ! -f src/part3.c &&
292 test ! -d docs &&
293 test ! -f obj.o &&
294 test ! -d build
298 test_expect_success 'git clean -d -x with ignored tracked directory' '
300 mkdir -p build docs &&
301 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
302 git clean -d -x -e src &&
303 test -f Makefile &&
304 test -f README &&
305 test -f src/part1.c &&
306 test -f src/part2.c &&
307 test ! -f a.out &&
308 test -f src/part3.c &&
309 test ! -d docs &&
310 test ! -f obj.o &&
311 test ! -d build
315 test_expect_success 'git clean -X' '
317 mkdir -p build docs &&
318 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
319 git clean -X &&
320 test -f Makefile &&
321 test -f README &&
322 test -f src/part1.c &&
323 test -f src/part2.c &&
324 test -f a.out &&
325 test -f src/part3.c &&
326 test -f docs/manual.txt &&
327 test ! -f obj.o &&
328 test -f build/lib.so
332 test_expect_success 'git clean -d -X' '
334 mkdir -p build docs &&
335 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
336 git clean -d -X &&
337 test -f Makefile &&
338 test -f README &&
339 test -f src/part1.c &&
340 test -f src/part2.c &&
341 test -f a.out &&
342 test -f src/part3.c &&
343 test -f docs/manual.txt &&
344 test ! -f obj.o &&
345 test ! -d build
349 test_expect_success 'git clean -d -X with ignored tracked directory' '
351 mkdir -p build docs &&
352 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
353 git clean -d -X -e src &&
354 test -f Makefile &&
355 test -f README &&
356 test -f src/part1.c &&
357 test -f src/part2.c &&
358 test -f a.out &&
359 test ! -f src/part3.c &&
360 test -f docs/manual.txt &&
361 test ! -f obj.o &&
362 test ! -d build
366 test_expect_success 'clean.requireForce defaults to true' '
368 git config --unset clean.requireForce &&
369 test_must_fail git clean
373 test_expect_success 'clean.requireForce' '
375 git config clean.requireForce true &&
376 test_must_fail git clean
380 test_expect_success 'clean.requireForce and -n' '
382 mkdir -p build docs &&
383 touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
384 git clean -n &&
385 test -f Makefile &&
386 test -f README &&
387 test -f src/part1.c &&
388 test -f src/part2.c &&
389 test -f a.out &&
390 test -f src/part3.c &&
391 test -f docs/manual.txt &&
392 test -f obj.o &&
393 test -f build/lib.so
397 test_expect_success 'clean.requireForce and -f' '
399 git clean -f &&
400 test -f README &&
401 test -f src/part1.c &&
402 test -f src/part2.c &&
403 test ! -f a.out &&
404 test ! -f src/part3.c &&
405 test -f docs/manual.txt &&
406 test -f obj.o &&
407 test -f build/lib.so
411 test_expect_success 'clean.requireForce and --interactive' '
412 git clean --interactive </dev/null >output 2>error &&
413 test_grep ! "requireForce is true and" error &&
414 test_grep "\*\*\* Commands \*\*\*" output
417 test_expect_success 'core.excludesfile' '
419 echo excludes >excludes &&
420 echo included >included &&
421 git config core.excludesfile excludes &&
422 output=$(git clean -n excludes included 2>&1) &&
423 expr "$output" : ".*included" >/dev/null &&
424 ! expr "$output" : ".*excludes" >/dev/null
428 test_expect_success SANITY 'removal failure' '
430 mkdir foo &&
431 touch foo/bar &&
432 test_when_finished "chmod 755 foo" &&
433 (exec <foo/bar &&
434 chmod 0 foo &&
435 test_must_fail git clean -f -d)
438 test_expect_success 'nested git work tree' '
439 rm -fr foo bar baz &&
440 mkdir -p foo bar baz/boo &&
442 cd foo &&
443 git init &&
444 test_commit nested hello.world
445 ) &&
447 cd bar &&
448 >goodbye.people
449 ) &&
451 cd baz/boo &&
452 git init &&
453 test_commit deeply.nested deeper.world
454 ) &&
455 git clean -f -d &&
456 test -f foo/.git/index &&
457 test -f foo/hello.world &&
458 test -f baz/boo/.git/index &&
459 test -f baz/boo/deeper.world &&
460 ! test -d bar
463 test_expect_success 'should clean things that almost look like git but are not' '
464 rm -fr almost_git almost_bare_git almost_submodule &&
465 mkdir -p almost_git/.git/objects &&
466 mkdir -p almost_git/.git/refs &&
467 cat >almost_git/.git/HEAD <<-\EOF &&
468 garbage
470 cp -r almost_git/.git/ almost_bare_git &&
471 mkdir almost_submodule/ &&
472 cat >almost_submodule/.git <<-\EOF &&
473 garbage
475 test_when_finished "rm -rf almost_*" &&
476 git clean -f -d &&
477 test_path_is_missing almost_git &&
478 test_path_is_missing almost_bare_git &&
479 test_path_is_missing almost_submodule
482 test_expect_success 'should not clean submodules' '
483 rm -fr repo to_clean sub1 sub2 &&
484 mkdir repo to_clean &&
486 cd repo &&
487 git init &&
488 test_commit msg hello.world
489 ) &&
490 test_config_global protocol.file.allow always &&
491 git submodule add ./repo/.git sub1 &&
492 git commit -m "sub1" &&
493 git branch before_sub2 &&
494 git submodule add ./repo/.git sub2 &&
495 git commit -m "sub2" &&
496 git checkout before_sub2 &&
497 >to_clean/should_clean.this &&
498 git clean -f -d &&
499 test_path_is_file repo/.git/index &&
500 test_path_is_file repo/hello.world &&
501 test_path_is_file sub1/.git &&
502 test_path_is_file sub1/hello.world &&
503 test_path_is_file sub2/.git &&
504 test_path_is_file sub2/hello.world &&
505 test_path_is_missing to_clean
508 test_expect_success POSIXPERM,SANITY 'should avoid cleaning possible submodules' '
509 rm -fr to_clean possible_sub1 &&
510 mkdir to_clean possible_sub1 &&
511 test_when_finished "rm -rf possible_sub*" &&
512 echo "gitdir: foo" >possible_sub1/.git &&
513 >possible_sub1/hello.world &&
514 chmod 0 possible_sub1/.git &&
515 >to_clean/should_clean.this &&
516 git clean -f -d &&
517 test_path_is_file possible_sub1/.git &&
518 test_path_is_file possible_sub1/hello.world &&
519 test_path_is_missing to_clean
522 test_expect_success 'nested (empty) git should be kept' '
523 rm -fr empty_repo to_clean &&
524 git init empty_repo &&
525 mkdir to_clean &&
526 >to_clean/should_clean.this &&
527 # Note that we put the expect file in the .git directory so that it
528 # does not get cleaned.
529 find empty_repo | sort >.git/expect &&
530 git clean -f -d &&
531 find empty_repo | sort >actual &&
532 test_cmp .git/expect actual &&
533 test_path_is_missing to_clean
536 test_expect_success 'nested bare repositories should be cleaned' '
537 rm -fr bare1 bare2 subdir &&
538 git init --bare bare1 &&
539 git clone --local --bare . bare2 &&
540 mkdir subdir &&
541 cp -r bare2 subdir/bare3 &&
542 git clean -f -d &&
543 test_path_is_missing bare1 &&
544 test_path_is_missing bare2 &&
545 test_path_is_missing subdir
548 test_expect_failure 'nested (empty) bare repositories should be cleaned even when in .git' '
549 rm -fr strange_bare &&
550 mkdir strange_bare &&
551 git init --bare strange_bare/.git &&
552 git clean -f -d &&
553 test_path_is_missing strange_bare
556 test_expect_failure 'nested (non-empty) bare repositories should be cleaned even when in .git' '
557 rm -fr strange_bare &&
558 mkdir strange_bare &&
559 git clone --local --bare . strange_bare/.git &&
560 git clean -f -d &&
561 test_path_is_missing strange_bare
564 test_expect_success 'giving path in nested git work tree will NOT remove it' '
565 rm -fr repo &&
566 mkdir repo &&
568 cd repo &&
569 git init &&
570 mkdir -p bar/baz &&
571 test_commit msg bar/baz/hello.world
572 ) &&
573 find repo | sort >expect &&
574 git clean -f -d repo/bar/baz &&
575 find repo | sort >actual &&
576 test_cmp expect actual
579 test_expect_success 'giving path to nested .git will not remove it' '
580 rm -fr repo &&
581 mkdir repo untracked &&
583 cd repo &&
584 git init &&
585 test_commit msg hello.world
586 ) &&
587 find repo | sort >expect &&
588 git clean -f -d repo/.git &&
589 find repo | sort >actual &&
590 test_cmp expect actual &&
591 test_path_is_dir untracked/
594 test_expect_success 'giving path to nested .git/ will NOT remove contents' '
595 rm -fr repo untracked &&
596 mkdir repo untracked &&
598 cd repo &&
599 git init &&
600 test_commit msg hello.world
601 ) &&
602 find repo | sort >expect &&
603 git clean -f -d repo/.git/ &&
604 find repo | sort >actual &&
605 test_cmp expect actual &&
606 test_path_is_dir untracked/
609 test_expect_success 'force removal of nested git work tree' '
610 rm -fr foo bar baz &&
611 mkdir -p foo bar baz/boo &&
613 cd foo &&
614 git init &&
615 test_commit nested hello.world
616 ) &&
618 cd bar &&
619 >goodbye.people
620 ) &&
622 cd baz/boo &&
623 git init &&
624 test_commit deeply.nested deeper.world
625 ) &&
626 git clean -f -f -d &&
627 ! test -d foo &&
628 ! test -d bar &&
629 ! test -d baz
632 test_expect_success 'git clean -e' '
633 rm -fr repo &&
634 mkdir repo &&
636 cd repo &&
637 git init &&
638 touch known 1 2 3 &&
639 git add known &&
640 git clean -f -e 1 -e 2 &&
641 test -e 1 &&
642 test -e 2 &&
643 ! (test -e 3) &&
644 test -e known
648 test_expect_success SANITY 'git clean -d with an unreadable empty directory' '
649 mkdir foo &&
650 chmod a= foo &&
651 git clean -dfx foo &&
652 ! test -d foo
655 test_expect_success 'git clean -d respects pathspecs (dir is prefix of pathspec)' '
656 mkdir -p foo &&
657 mkdir -p foobar &&
658 git clean -df foobar &&
659 test_path_is_dir foo &&
660 test_path_is_missing foobar
663 test_expect_success 'git clean -d respects pathspecs (pathspec is prefix of dir)' '
664 mkdir -p foo &&
665 mkdir -p foobar &&
666 git clean -df foo &&
667 test_path_is_missing foo &&
668 test_path_is_dir foobar
671 test_expect_success 'git clean -d skips untracked dirs containing ignored files' '
672 echo /foo/bar >.gitignore &&
673 echo ignoreme >>.gitignore &&
674 rm -rf foo &&
675 mkdir -p foo/a/aa/aaa foo/b/bb/bbb &&
676 touch foo/bar foo/baz foo/a/aa/ignoreme foo/b/ignoreme foo/b/bb/1 foo/b/bb/2 &&
677 git clean -df &&
678 test_path_is_dir foo &&
679 test_path_is_file foo/bar &&
680 test_path_is_missing foo/baz &&
681 test_path_is_file foo/a/aa/ignoreme &&
682 test_path_is_missing foo/a/aa/aaa &&
683 test_path_is_file foo/b/ignoreme &&
684 test_path_is_missing foo/b/bb
687 test_expect_success 'git clean -d skips nested repo containing ignored files' '
688 test_when_finished "rm -rf nested-repo-with-ignored-file" &&
690 git init nested-repo-with-ignored-file &&
692 cd nested-repo-with-ignored-file &&
693 >file &&
694 git add file &&
695 git commit -m Initial &&
697 # This file is ignored by a .gitignore rule in the outer repo
698 # added in the previous test.
699 >ignoreme
700 ) &&
702 git clean -fd &&
704 test_path_is_file nested-repo-with-ignored-file/.git/index &&
705 test_path_is_file nested-repo-with-ignored-file/ignoreme &&
706 test_path_is_file nested-repo-with-ignored-file/file
709 test_expect_success 'git clean handles being told what to clean' '
710 mkdir -p d1 d2 &&
711 touch d1/ut d2/ut &&
712 git clean -f */ut &&
713 test_path_is_missing d1/ut &&
714 test_path_is_missing d2/ut
717 test_expect_success 'git clean handles being told what to clean, with -d' '
718 mkdir -p d1 d2 &&
719 touch d1/ut d2/ut &&
720 git clean -ffd */ut &&
721 test_path_is_missing d1/ut &&
722 test_path_is_missing d2/ut
725 test_expect_success 'git clean works if a glob is passed without -d' '
726 mkdir -p d1 d2 &&
727 touch d1/ut d2/ut &&
728 git clean -f "*ut" &&
729 test_path_is_missing d1/ut &&
730 test_path_is_missing d2/ut
733 test_expect_success 'git clean works if a glob is passed with -d' '
734 mkdir -p d1 d2 &&
735 touch d1/ut d2/ut &&
736 git clean -ffd "*ut" &&
737 test_path_is_missing d1/ut &&
738 test_path_is_missing d2/ut
741 test_expect_success MINGW 'handle clean & core.longpaths = false nicely' '
742 test_config core.longpaths false &&
743 a50=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
744 mkdir -p $a50$a50/$a50$a50/$a50$a50 &&
745 : >"$a50$a50/test.txt" 2>"$a50$a50/$a50$a50/$a50$a50/test.txt" &&
746 # create a temporary outside the working tree to hide from "git clean"
747 test_must_fail git clean -xdf 2>.git/err &&
748 # grepping for a strerror string is unportable but it is OK here with
749 # MINGW prereq
750 test_grep "too long" .git/err
753 test_expect_success 'clean untracked paths by pathspec' '
754 git init untracked &&
755 mkdir untracked/dir &&
756 echo >untracked/dir/file.txt &&
757 git -C untracked clean -f dir/file.txt &&
758 ls untracked/dir >actual &&
759 test_must_be_empty actual
762 test_expect_success 'avoid traversing into ignored directories' '
763 test_when_finished rm -f output error trace.* &&
764 test_create_repo avoid-traversing-deep-hierarchy &&
766 cd avoid-traversing-deep-hierarchy &&
768 mkdir -p untracked/subdir/with/a &&
769 >untracked/subdir/with/a/random-file.txt &&
771 GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
772 git clean -ffdxn -e untracked
773 ) &&
775 # Make sure we only visited into the top-level directory, and did
776 # not traverse into the "untracked" subdirectory since it was excluded
777 grep data.*read_directo.*directories-visited trace.output |
778 cut -d "|" -f 9 >trace.relevant &&
779 cat >trace.expect <<-EOF &&
780 ..directories-visited:1
782 test_cmp trace.expect trace.relevant
785 test_expect_success 'traverse into directories that may have ignored entries' '
786 test_when_finished rm -f output &&
787 test_create_repo need-to-traverse-into-hierarchy &&
789 cd need-to-traverse-into-hierarchy &&
790 mkdir -p modules/foobar/src/generated &&
791 > modules/foobar/src/generated/code.c &&
792 > modules/foobar/Makefile &&
793 echo "/modules/**/src/generated/" >.gitignore &&
795 git clean -fX modules/foobar >../output &&
797 grep Removing ../output &&
799 test_path_is_missing modules/foobar/src/generated/code.c &&
800 test_path_is_file modules/foobar/Makefile
804 test_done