Skip to content

Commit dfaa209

Browse files
bwijengitster
authored andcommitted
git clone: don't clone into non-empty directory
When using git clone with --separate-git-dir realgitdir and realgitdir already exists, it's content is destroyed. So, make sure we don't clone into an existing non-empty directory. When d45420c (clone: do not clean up directories we didn't create, 2018-01-02) tightened the clean-up procedure after a failed cloning into an empty directory, it assumed that the existing directory given is an empty one so it is OK to keep that directory, while running the clean-up procedure that is designed to remove everything in it (since there won't be any, anyway). Check and make sure that the $GIT_DIR is empty even cloning into an existing repository. Signed-off-by: Ben Wijen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a08a83d commit dfaa209

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

builtin/clone.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
946946
int is_bundle = 0, is_local;
947947
const char *repo_name, *repo, *work_tree, *git_dir;
948948
char *path, *dir, *display_repo = NULL;
949-
int dest_exists;
949+
int dest_exists, real_dest_exists = 0;
950950
const struct ref *refs, *remote_head;
951951
const struct ref *remote_head_points_at;
952952
const struct ref *our_head_points_at;
@@ -1021,6 +1021,14 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
10211021
die(_("destination path '%s' already exists and is not "
10221022
"an empty directory."), dir);
10231023

1024+
if (real_git_dir) {
1025+
real_dest_exists = path_exists(real_git_dir);
1026+
if (real_dest_exists && !is_empty_dir(real_git_dir))
1027+
die(_("repository path '%s' already exists and is not "
1028+
"an empty directory."), real_git_dir);
1029+
}
1030+
1031+
10241032
strbuf_addf(&reflog_msg, "clone: from %s",
10251033
display_repo ? display_repo : repo);
10261034
free(display_repo);
@@ -1057,7 +1065,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
10571065
}
10581066

10591067
if (real_git_dir) {
1060-
if (path_exists(real_git_dir))
1068+
if (real_dest_exists)
10611069
junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
10621070
junk_git_dir = real_git_dir;
10631071
} else {

t/t5601-clone.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,9 @@ test_expect_success 'fetch from gitfile parent' '
271271

272272
test_expect_success 'clone separate gitdir where target already exists' '
273273
rm -rf dst &&
274-
test_must_fail git clone --separate-git-dir realgitdir src dst
274+
echo foo=bar >>realgitdir/config &&
275+
test_must_fail git clone --separate-git-dir realgitdir src dst &&
276+
grep foo=bar realgitdir/config
275277
'
276278

277279
test_expect_success 'clone --reference from original' '

0 commit comments

Comments
 (0)