Skip to content

Commit e07c1e1

Browse files
committed
Merge pull request libgit2#2880 from ethomson/mkdir_root
Ensure we can make a repo at the root of the filesystem
2 parents 5a2a577 + 9cb5b0f commit e07c1e1

File tree

5 files changed

+65
-17
lines changed

5 files changed

+65
-17
lines changed

appveyor.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ branches:
33
only:
44
- master
55
environment:
6+
GITTEST_INVASIVE_FILESYSTEM: 1
7+
68
matrix:
79
- GENERATOR: "Visual Studio 11"
810
ARCH: 32

src/fileops.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ int git_futils_mkdir_withperf(
330330
{
331331
int error = -1;
332332
git_buf make_path = GIT_BUF_INIT;
333-
ssize_t root = 0, min_root_len;
333+
ssize_t root = 0, min_root_len, root_len;
334334
char lastch = '/', *tail;
335335
struct stat st;
336336

@@ -343,22 +343,29 @@ int git_futils_mkdir_withperf(
343343
goto done;
344344
}
345345

346-
/* remove trailing slashes on path */
347-
while (make_path.ptr[make_path.size - 1] == '/') {
348-
make_path.size--;
349-
make_path.ptr[make_path.size] = '\0';
350-
}
346+
/* Trim trailing slashes (except the root) */
347+
if ((root_len = git_path_root(make_path.ptr)) < 0)
348+
root_len = 0;
349+
else
350+
root_len++;
351+
352+
while (make_path.size > (size_t)root_len &&
353+
make_path.ptr[make_path.size - 1] == '/')
354+
make_path.ptr[--make_path.size] = '\0';
351355

352356
/* if we are not supposed to made the last element, truncate it */
353357
if ((flags & GIT_MKDIR_SKIP_LAST2) != 0) {
354-
git_buf_rtruncate_at_char(&make_path, '/');
358+
git_path_dirname_r(&make_path, make_path.ptr);
355359
flags |= GIT_MKDIR_SKIP_LAST;
356360
}
357-
if ((flags & GIT_MKDIR_SKIP_LAST) != 0)
358-
git_buf_rtruncate_at_char(&make_path, '/');
361+
if ((flags & GIT_MKDIR_SKIP_LAST) != 0) {
362+
git_path_dirname_r(&make_path, make_path.ptr);
363+
}
359364

360-
/* if nothing left after truncation, then we're done! */
361-
if (!make_path.size) {
365+
/* We were either given the root path (or trimmed it to
366+
* the root), we don't have anything to do.
367+
*/
368+
if (make_path.size <= (size_t)root_len) {
362369
error = 0;
363370
goto done;
364371
}

src/win32/posix_w32.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,8 @@ int p_stat(const char* path, struct stat* buf)
448448
git_win32_path path_w;
449449
int len;
450450

451-
if ((len = git_win32_path_from_utf8(path_w, path)) < 0)
452-
return -1;
453-
454-
git_win32__path_trim_end(path_w, len);
455-
456-
if (lstat_w(path_w, buf, false) < 0)
451+
if ((len = git_win32_path_from_utf8(path_w, path)) < 0 ||
452+
lstat_w(path_w, buf, false) < 0)
457453
return -1;
458454

459455
/* The item is a symbolic link or mount point. No need to iterate

tests/core/stat.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,20 @@ void test_core_stat__0(void)
9595
cl_assert_error(ENOTDIR);
9696
}
9797

98+
void test_core_stat__root(void)
99+
{
100+
const char *sandbox = clar_sandbox_path();
101+
git_buf root = GIT_BUF_INIT;
102+
int root_len;
103+
struct stat st;
104+
105+
root_len = git_path_root(sandbox);
106+
cl_assert(root_len >= 0);
107+
108+
git_buf_set(&root, sandbox, root_len+1);
109+
110+
cl_must_pass(p_stat(root.ptr, &st));
111+
cl_assert(S_ISDIR(st.st_mode));
112+
113+
git_buf_free(&root);
114+
}

tests/repo/init.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,3 +714,29 @@ void test_repo_init__init_with_initial_commit(void)
714714

715715
git_index_free(index);
716716
}
717+
718+
void test_repo_init__at_filesystem_root(void)
719+
{
720+
git_repository *repo;
721+
const char *sandbox = clar_sandbox_path();
722+
git_buf root = GIT_BUF_INIT;
723+
int root_len;
724+
725+
if (!cl_getenv("GITTEST_INVASIVE_FILESYSTEM"))
726+
cl_skip();
727+
728+
root_len = git_path_root(sandbox);
729+
cl_assert(root_len >= 0);
730+
731+
git_buf_put(&root, sandbox, root_len+1);
732+
git_buf_joinpath(&root, root.ptr, "libgit2_test_dir");
733+
734+
cl_assert(!git_path_exists(root.ptr));
735+
736+
cl_git_pass(git_repository_init(&repo, root.ptr, 0));
737+
cl_assert(git_path_isdir(root.ptr));
738+
cl_git_pass(git_futils_rmdir_r(root.ptr, NULL, GIT_RMDIR_REMOVE_FILES));
739+
740+
git_buf_free(&root);
741+
git_repository_free(repo);
742+
}

0 commit comments

Comments
 (0)