git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/4] clone: factor out dir_exists() helper

From
Jeff King <peff@peff.net>
Date
Jan 2, 2018, 21:10 UTC
Message-ID
<20180102211014.GC22556@sigill.intra.peff.net>
In-Reply-To
<20180102210753.GA10430@sigill.intra.peff.net>

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>
---
 builtin/clone.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/builtin/clone.c b/builtin/clone.c
index 2da71db107..04b0d7283f 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -863,10 +863,15 @@ static void dissociate_from_references(void)
 	free(alternates);
 }
 
+static int dir_exists(const char *path)
+{
+	struct stat sb;
+	return !stat(path, &sb);
+}
+
 int cmd_clone(int argc, const char **argv, const char *prefix)
 {
 	int is_bundle = 0, is_local;
-	struct stat buf;
 	const char *repo_name, *repo, *work_tree, *git_dir;
 	char *path, *dir;
 	int dest_exists;
@@ -938,7 +943,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 		dir = guess_dir_name(repo_name, is_bundle, option_bare);
 	strip_trailing_slashes(dir);
 
-	dest_exists = !stat(dir, &buf);
+	dest_exists = dir_exists(dir);
 	if (dest_exists && !is_empty_dir(dir))
 		die(_("destination path '%s' already exists and is not "
 			"an empty directory."), dir);
@@ -949,7 +954,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 		work_tree = NULL;
 	else {
 		work_tree = getenv("GIT_WORK_TREE");
-		if (work_tree && !stat(work_tree, &buf))
+		if (work_tree && dir_exists(work_tree))
 			die(_("working tree '%s' already exists."), work_tree);
 	}
 
-- 
2.16.0.rc0.384.gc477e89267
Previous: Jeff KingNext: Junio C Hamano
Message 7 of 16 in “Git removes existing folder when cancelling clone”
  1. Stephan JanssenJan 2, 2018
  2. Robert P. J. DayJan 2, 2018
  3. Jeff KingJan 2, 2018
  4. 0/4 Git removes existing folder when cancelling cloneJeff King, Jan 2, 2018
  5. 1/4 t5600: fix outdated comment about unborn HEADJeff King, Jan 2, 2018
  6. 2/4 t5600: modernize styleJeff King, Jan 2, 2018
  7. 3/4 clone: factor out dir_exists() helperJeff King, Jan 2, 2018
  8. Junio C HamanoJan 4, 2018
  9. Jeff KingJan 4, 2018
  10. Jeff KingJan 5, 2018
  11. Junio C HamanoJan 5, 2018
  12. 4/4 clone: do not clean up directories we didn't createJeff King, Jan 2, 2018
  13. Eric SunshineJan 2, 2018
  14. Jeff KingJan 2, 2018
  15. Junio C HamanoJan 5, 2018
  16. Junio C HamanoJan 4, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.