Skip to content

Commit 14954b7

Browse files
matheustavaresgitster
authored andcommitted
clone: extract function from copy_or_link_directory
Extract dir creation code snippet from copy_or_link_directory to its own function named mkdir_if_missing. This change will help to remove copy_or_link_directory's explicit recursion, which will be done in a following patch. Also makes the code more readable. Signed-off-by: Matheus Tavares <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 68c7c59 commit 14954b7

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

builtin/clone.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,21 @@ static void copy_alternates(struct strbuf *src, const char *src_repo)
391391
fclose(in);
392392
}
393393

394+
static void mkdir_if_missing(const char *pathname, mode_t mode)
395+
{
396+
struct stat st;
397+
398+
if (!mkdir(pathname, mode))
399+
return;
400+
401+
if (errno != EEXIST)
402+
die_errno(_("failed to create directory '%s'"), pathname);
403+
else if (stat(pathname, &st))
404+
die_errno(_("failed to stat '%s'"), pathname);
405+
else if (!S_ISDIR(st.st_mode))
406+
die(_("%s exists and is not a directory"), pathname);
407+
}
408+
394409
static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
395410
const char *src_repo, int src_baselen)
396411
{
@@ -403,14 +418,7 @@ static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
403418
if (!dir)
404419
die_errno(_("failed to open '%s'"), src->buf);
405420

406-
if (mkdir(dest->buf, 0777)) {
407-
if (errno != EEXIST)
408-
die_errno(_("failed to create directory '%s'"), dest->buf);
409-
else if (stat(dest->buf, &buf))
410-
die_errno(_("failed to stat '%s'"), dest->buf);
411-
else if (!S_ISDIR(buf.st_mode))
412-
die(_("%s exists and is not a directory"), dest->buf);
413-
}
421+
mkdir_if_missing(dest->buf, 0777);
414422

415423
strbuf_addch(src, '/');
416424
src_len = src->len;

0 commit comments

Comments
 (0)