@@ -455,6 +455,148 @@ test_expect_success 'nested git work tree' '
455455 ! test -d bar
456456'
457457
458+ test_expect_failure ' 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 &&
463+ garbage
464+ EOF
465+ cp -r almost_git/.git/ almost_bare_git &&
466+ mkdir almost_submodule/ &&
467+ cat >almost_submodule/.git <<-\EOF &&
468+ garbage
469+ EOF
470+ test_when_finished "rm -rf almost_*" &&
471+ ## This will fail due to die("Invalid gitfile format: %s", path); in
472+ ## setup.c:read_gitfile.
473+ git clean -f -d &&
474+ test_path_is_missing almost_git &&
475+ test_path_is_missing almost_bare_git &&
476+ test_path_is_missing almost_submodule
477+ '
478+
479+ test_expect_success ' should not clean submodules' '
480+ rm -fr repo to_clean sub1 sub2 &&
481+ mkdir repo to_clean &&
482+ (
483+ cd repo &&
484+ git init &&
485+ test_commit msg hello.world
486+ ) &&
487+ git submodule add ./repo/.git sub1 &&
488+ git commit -m "sub1" &&
489+ git branch before_sub2 &&
490+ git submodule add ./repo/.git sub2 &&
491+ git commit -m "sub2" &&
492+ git checkout before_sub2 &&
493+ >to_clean/should_clean.this &&
494+ git clean -f -d &&
495+ test_path_is_file repo/.git/index &&
496+ test_path_is_file repo/hello.world &&
497+ test_path_is_file sub1/.git &&
498+ test_path_is_file sub1/hello.world &&
499+ test_path_is_file sub2/.git &&
500+ test_path_is_file sub2/hello.world &&
501+ test_path_is_missing to_clean
502+ '
503+
504+ test_expect_failure ' should avoid cleaning possible submodules' '
505+ rm -fr to_clean possible_sub1 &&
506+ mkdir to_clean possible_sub1 &&
507+ test_when_finished "rm -rf possible_sub*" &&
508+ echo "gitdir: foo" >possible_sub1/.git &&
509+ >possible_sub1/hello.world &&
510+ chmod 0 possible_sub1/.git &&
511+ >to_clean/should_clean.this &&
512+ git clean -f -d &&
513+ test_path_is_file possible_sub1/.git &&
514+ test_path_is_file possible_sub1/hello.world &&
515+ test_path_is_missing to_clean
516+ '
517+
518+ test_expect_failure ' nested (empty) git should be kept' '
519+ rm -fr empty_repo to_clean &&
520+ git init empty_repo &&
521+ mkdir to_clean &&
522+ >to_clean/should_clean.this &&
523+ git clean -f -d &&
524+ test_path_is_file empty_repo/.git/HEAD &&
525+ test_path_is_missing to_clean
526+ '
527+
528+ test_expect_success ' nested bare repositories should be cleaned' '
529+ rm -fr bare1 bare2 subdir &&
530+ git init --bare bare1 &&
531+ git clone --local --bare . bare2 &&
532+ mkdir subdir &&
533+ cp -r bare2 subdir/bare3 &&
534+ git clean -f -d &&
535+ test_path_is_missing bare1 &&
536+ test_path_is_missing bare2 &&
537+ test_path_is_missing subdir
538+ '
539+
540+ test_expect_success ' nested (empty) bare repositories should be cleaned even when in .git' '
541+ rm -fr strange_bare &&
542+ mkdir strange_bare &&
543+ git init --bare strange_bare/.git &&
544+ git clean -f -d &&
545+ test_path_is_missing strange_bare
546+ '
547+
548+ test_expect_failure ' nested (non-empty) bare repositories should be cleaned even when in .git' '
549+ rm -fr strange_bare &&
550+ mkdir strange_bare &&
551+ git clone --local --bare . strange_bare/.git &&
552+ git clean -f -d &&
553+ test_path_is_missing strange_bare
554+ '
555+
556+ test_expect_success ' giving path in nested git work tree will remove it' '
557+ rm -fr repo &&
558+ mkdir repo &&
559+ (
560+ cd repo &&
561+ git init &&
562+ mkdir -p bar/baz &&
563+ test_commit msg bar/baz/hello.world
564+ ) &&
565+ git clean -f -d repo/bar/baz &&
566+ test_path_is_file repo/.git/HEAD &&
567+ test_path_is_dir repo/bar/ &&
568+ test_path_is_missing repo/bar/baz
569+ '
570+
571+ test_expect_success ' giving path to nested .git will not remove it' '
572+ rm -fr repo &&
573+ mkdir repo untracked &&
574+ (
575+ cd repo &&
576+ git init &&
577+ test_commit msg hello.world
578+ ) &&
579+ git clean -f -d repo/.git &&
580+ test_path_is_file repo/.git/HEAD &&
581+ test_path_is_dir repo/.git/refs &&
582+ test_path_is_dir repo/.git/objects &&
583+ test_path_is_dir untracked/
584+ '
585+
586+ test_expect_success ' giving path to nested .git/ will remove contents' '
587+ rm -fr repo untracked &&
588+ mkdir repo untracked &&
589+ (
590+ cd repo &&
591+ git init &&
592+ test_commit msg hello.world
593+ ) &&
594+ git clean -f -d repo/.git/ &&
595+ test_path_is_dir repo/.git &&
596+ test_dir_is_empty repo/.git &&
597+ test_path_is_dir untracked/
598+ '
599+
458600test_expect_success ' force removal of nested git work tree' '
459601 rm -fr foo bar baz &&
460602 mkdir -p foo bar baz/boo &&
0 commit comments