Skip to content

Commit 9f1c873

Browse files
committed
checkout: don't recreate previous directory
Don't bother trying to recreate the previously created directory during checkout, for a modest reduction in the number of stats.
1 parent 1ca7fa9 commit 9f1c873

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

src/checkout.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ typedef struct {
6868
size_t total_steps;
6969
size_t completed_steps;
7070
git_checkout_perfdata perfdata;
71+
git_buf last_mkdir;
7172
} checkout_data;
7273

7374
typedef struct {
@@ -1312,9 +1313,24 @@ static int checkout_mkdir(
13121313
static int mkpath2file(
13131314
checkout_data *data, const char *path, unsigned int mode)
13141315
{
1315-
return checkout_mkdir(
1316-
data, path, git_repository_workdir(data->repo), mode,
1317-
GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR);
1316+
git_buf *mkdir_path = &data->tmp;
1317+
int error;
1318+
1319+
if ((error = git_buf_sets(mkdir_path, path)) < 0)
1320+
return error;
1321+
1322+
git_buf_rtruncate_at_char(mkdir_path, '/');
1323+
1324+
if (data->last_mkdir.size && mkdir_path->size == data->last_mkdir.size &&
1325+
memcmp(mkdir_path->ptr, data->last_mkdir.ptr, mkdir_path->size) == 0)
1326+
return 0;
1327+
1328+
if ((error = checkout_mkdir(
1329+
data, mkdir_path->ptr, data->opts.target_directory, mode,
1330+
GIT_MKDIR_PATH | GIT_MKDIR_VERIFY_DIR)) == 0)
1331+
git_buf_swap(&data->last_mkdir, mkdir_path);
1332+
1333+
return error;
13181334
}
13191335

13201336
static int buffer_to_file(

src/fileops.c

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -352,26 +352,40 @@ int git_futils_mkdir_withperf(
352352
*tail = '\0';
353353
st.st_mode = 0;
354354

355-
/* make directory */
356-
perfdata->mkdir_calls++;
355+
/* See what's going on with this path component */
356+
perfdata->stat_calls++;
357+
358+
if (p_lstat(make_path.ptr, &st) < 0) {
359+
perfdata->mkdir_calls++;
357360

358-
if (p_mkdir(make_path.ptr, mode) < 0) {
359-
int tmp_errno = giterr_system_last();
361+
if (errno != ENOENT || p_mkdir(make_path.ptr, mode) < 0) {
362+
giterr_set(GITERR_OS, "Failed to make directory '%s'", make_path.ptr);
363+
error = GIT_EEXISTS;
364+
goto done;
365+
}
366+
367+
giterr_clear();
368+
} else {
369+
/* with exclusive create, existing dir is an error */
370+
if ((flags & GIT_MKDIR_EXCL) != 0) {
371+
giterr_set(GITERR_INVALID, "Failed to make directory '%s': directory exists", make_path.ptr);
372+
error = GIT_EEXISTS;
373+
goto done;
374+
}
360375

361-
/* ignore error if not at end or if directory already exists */
362-
if (lastch == '\0') {
376+
if (S_ISLNK(st.st_mode)) {
363377
perfdata->stat_calls++;
364378

365-
if (p_stat(make_path.ptr, &st) < 0 || !S_ISDIR(st.st_mode)) {
366-
giterr_system_set(tmp_errno);
379+
/* Re-stat the target, make sure it's a directory */
380+
if (p_stat(make_path.ptr, &st) < 0) {
367381
giterr_set(GITERR_OS, "Failed to make directory '%s'", make_path.ptr);
382+
error = GIT_EEXISTS;
368383
goto done;
369384
}
370385
}
371386

372-
/* with exclusive create, existing dir is an error */
373-
if ((flags & GIT_MKDIR_EXCL) != 0) {
374-
giterr_set(GITERR_OS, "Directory already exists '%s'", make_path.ptr);
387+
if (!S_ISDIR(st.st_mode)) {
388+
giterr_set(GITERR_INVALID, "Failed to make directory '%s': directory exists", make_path.ptr);
375389
error = GIT_EEXISTS;
376390
goto done;
377391
}

0 commit comments

Comments
 (0)