Skip to content

Commit 3dd2f0d

Browse files
peffdscho
authored andcommitted
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 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 call 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 actual files.
1 parent 153b19e commit 3dd2f0d

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

builtin/clone.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -863,10 +863,15 @@ static void dissociate_from_references(void)
863863
free(alternates);
864864
}
865865

866+
static int dir_exists(const char *path)
867+
{
868+
struct stat sb;
869+
return !stat(path, &sb);
870+
}
871+
866872
int cmd_clone(int argc, const char **argv, const char *prefix)
867873
{
868874
int is_bundle = 0, is_local;
869-
struct stat buf;
870875
const char *repo_name, *repo, *work_tree, *git_dir;
871876
char *path, *dir;
872877
int dest_exists;
@@ -940,7 +945,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
940945
dir = guess_dir_name(repo_name, is_bundle, option_bare);
941946
strip_trailing_slashes(dir);
942947

943-
dest_exists = !stat(dir, &buf);
948+
dest_exists = dir_exists(dir);
944949
if (dest_exists && !is_empty_dir(dir))
945950
die(_("destination path '%s' already exists and is not "
946951
"an empty directory."), dir);
@@ -951,7 +956,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
951956
work_tree = NULL;
952957
else {
953958
work_tree = getenv("GIT_WORK_TREE");
954-
if (work_tree && !stat(work_tree, &buf))
959+
if (work_tree && dir_exists(work_tree))
955960
die(_("working tree '%s' already exists."), work_tree);
956961
}
957962

0 commit comments

Comments
 (0)