checkout: do not fail if target is an empty directory
authorMax Kirillov <max@max630.net>
Sun, 30 Nov 2014 08:24:58 +0000 (15:24 +0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 1 Dec 2014 19:00:19 +0000 (11:00 -0800)
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>
builtin/checkout.c
t/t2025-checkout-to.sh
index 8c5276cf443f5d1e84cd5fd34e5f899afd3e58b6..953b76370be91563d4ec5a0ded3394d91b7d699b 100644 (file)
@@ -865,7 +865,7 @@ static int prepare_linked_checkout(const struct checkout_opts *opts,
 
        if (!new->commit)
                die(_("no branch specified"));
-       if (file_exists(path))
+       if (file_exists(path) && !is_empty_dir(path))
                die(_("'%s' already exists"), path);
 
        len = strlen(path);
index eddd3251f1faede8cbfb0c9f6db501f0facfe063..915b506b6c91066ebf4217aad66e38a2b0298125 100755 (executable)
@@ -13,10 +13,15 @@ test_expect_success 'checkout --to not updating paths' '
 '
 
 test_expect_success 'checkout --to an existing worktree' '
-       mkdir existing &&
+       mkdir -p existing/subtree &&
        test_must_fail git checkout --detach --to existing master
 '
 
+test_expect_success 'checkout --to an existing empty worktree' '
+       mkdir existing_empty &&
+       git checkout --detach --to existing_empty master
+'
+
 test_expect_success 'checkout --to refuses to checkout locked branch' '
        test_must_fail git checkout --to zere master &&
        ! test -d zere &&