clone: factor out dir_exists() helper

Two parts of git-clone's setup logic check whether a directory exists, and they both call stat directly with the same scratch "struct stat" buffer. Let's pull that into a helper, which has a few advantages: - it makes the purpose of the stat calls more obvious - it makes it clear that we don't care about the information in "buf" remaining valid - if we later decide to make the check more robust (e.g., complaining about non-directories), we can do it in one place Note that we could just use file_exists() for this, which has identical code. But we specifically care about directories, so this future-proofs us against that function later getting more picky about seeing actual files. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jan 2, 2018 at 16:10 UTC f9e377adc0b1ed06e35d2c77a6c9f2687c5b950b
1 file changed +8 -3
builtin/clone.c
+8 -3
@@ -863,10 +863,15 @@ static void dissociate_from_references(void)
863 free(alternates);
864 }
865
866 +static int dir_exists(const char *path)
867 +{
868 + struct stat sb;
869 + return !stat(path, &sb);
870 +}
871 +
872 int cmd_clone(int argc, const char **argv, const char *prefix)
873 {
874 int is_bundle = 0, is_local;
869 - struct stat buf;
875 const char *repo_name, *repo, *work_tree, *git_dir;
876 char *path, *dir;
877 int dest_exists;
@@ -938,7 +943,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
943 dir = guess_dir_name(repo_name, is_bundle, option_bare);
944 strip_trailing_slashes(dir);
945
941 - dest_exists = !stat(dir, &buf);
946 + dest_exists = dir_exists(dir);
947 if (dest_exists && !is_empty_dir(dir))
948 die(_("destination path '%s' already exists and is not "
949 "an empty directory."), dir);
@@ -949,7 +954,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
954 work_tree = NULL;
955 else {
956 work_tree = getenv("GIT_WORK_TREE");
952 - if (work_tree && !stat(work_tree, &buf))
957 + if (work_tree && dir_exists(work_tree))
958 die(_("working tree '%s' already exists."), work_tree);
959 }
960