checkout: do not fail if target is an empty directory

Non-recursive checkout creates empty directpries in place of submodules. If then I try to "checkout --to" submodules there, it refuses to do so, because directory already exists. Fix by allowing checking out to empty directory. Add test and modify the existing one so that it uses non-empty directory. Signed-off-by: Max Kirillov <max@max630.net> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Max Kirillov committed Nov 30, 2014 at 15:24 UTC ee4fb8435e2186323c6a91d26d1572da12b5d033
2 files changed +7 -2
builtin/checkout.c
+1 -1
@@ -865,7 +865,7 @@ static int prepare_linked_checkout(const struct checkout_opts *opts,
865
866 if (!new->commit)
867 die(_("no branch specified"));
868 - if (file_exists(path))
868 + if (file_exists(path) && !is_empty_dir(path))
869 die(_("'%s' already exists"), path);
870
871 len = strlen(path);
t/t2025-checkout-to.sh
+6 -1
@@ -13,10 +13,15 @@ test_expect_success 'checkout --to not updating paths' '
13 '
14
15 test_expect_success 'checkout --to an existing worktree' '
16 - mkdir existing &&
16 + mkdir -p existing/subtree &&
17 test_must_fail git checkout --detach --to existing master
18 '
19
20 +test_expect_success 'checkout --to an existing empty worktree' '
21 + mkdir existing_empty &&
22 + git checkout --detach --to existing_empty master
23 +'
24 +
25 test_expect_success 'checkout --to refuses to checkout locked branch' '
26 test_must_fail git checkout --to zere master &&
27 ! test -d zere &&