Skip to content

Commit ee4fb84

Browse files
max630gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ad35f61 commit ee4fb84

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

builtin/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ static int prepare_linked_checkout(const struct checkout_opts *opts,
865865

866866
if (!new->commit)
867867
die(_("no branch specified"));
868-
if (file_exists(path))
868+
if (file_exists(path) && !is_empty_dir(path))
869869
die(_("'%s' already exists"), path);
870870

871871
len = strlen(path);

t/t2025-checkout-to.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ test_expect_success 'checkout --to not updating paths' '
1313
'
1414

1515
test_expect_success 'checkout --to an existing worktree' '
16-
mkdir existing &&
16+
mkdir -p existing/subtree &&
1717
test_must_fail git checkout --detach --to existing master
1818
'
1919

20+
test_expect_success 'checkout --to an existing empty worktree' '
21+
mkdir existing_empty &&
22+
git checkout --detach --to existing_empty master
23+
'
24+
2025
test_expect_success 'checkout --to refuses to checkout locked branch' '
2126
test_must_fail git checkout --to zere master &&
2227
! test -d zere &&

0 commit comments

Comments
 (0)