]> git.ipfire.org Git - thirdparty/git.git/commitdiff
git clone: don't clone into non-empty directory
authorBen Wijen <ben@wijen.net>
Fri, 10 Jul 2020 08:47:32 +0000 (10:47 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 10 Jul 2020 18:43:29 +0000 (11:43 -0700)
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 d45420c1 (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 <ben@wijen.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/clone.c
t/t5601-clone.sh

index 2a8e3aaaed367cc094d8277bd2d1aa6ccb9210c4..e470193bb83b580c0bc56c7769d579b3bf3e61d8 100644 (file)
@@ -946,7 +946,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
        int is_bundle = 0, is_local;
        const char *repo_name, *repo, *work_tree, *git_dir;
        char *path, *dir, *display_repo = NULL;
-       int dest_exists;
+       int dest_exists, real_dest_exists = 0;
        const struct ref *refs, *remote_head;
        const struct ref *remote_head_points_at;
        const struct ref *our_head_points_at;
@@ -1021,6 +1021,14 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
                die(_("destination path '%s' already exists and is not "
                        "an empty directory."), dir);
 
+       if (real_git_dir) {
+               real_dest_exists = path_exists(real_git_dir);
+               if (real_dest_exists && !is_empty_dir(real_git_dir))
+                       die(_("repository path '%s' already exists and is not "
+                               "an empty directory."), real_git_dir);
+       }
+
+
        strbuf_addf(&reflog_msg, "clone: from %s",
                    display_repo ? display_repo : repo);
        free(display_repo);
@@ -1057,7 +1065,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
        }
 
        if (real_git_dir) {
-               if (path_exists(real_git_dir))
+               if (real_dest_exists)
                        junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
                junk_git_dir = real_git_dir;
        } else {
index 84ea2a3eb707a7405d7fc3c5267f8bfff06abd18..eb9a093e25fadce8f8e6964e1e4b208256419cad 100755 (executable)
@@ -271,7 +271,9 @@ test_expect_success 'fetch from gitfile parent' '
 
 test_expect_success 'clone separate gitdir where target already exists' '
        rm -rf dst &&
-       test_must_fail git clone --separate-git-dir realgitdir src dst
+       echo foo=bar >>realgitdir/config &&
+       test_must_fail git clone --separate-git-dir realgitdir src dst &&
+       grep foo=bar realgitdir/config
 '
 
 test_expect_success 'clone --reference from original' '