Commit 7d170a4b by Vicent Martí

Merge pull request #231 from Romain-Geissler/discovery-path-v2

[Discovery path] Fix and tests
parents f9213015 efcc87c9
...@@ -380,7 +380,7 @@ int gitfo_mkdir_recurs(const char *path, int mode) ...@@ -380,7 +380,7 @@ int gitfo_mkdir_recurs(const char *path, int mode)
if (root_path_offset > 0) if (root_path_offset > 0)
pp += root_path_offset; /* On Windows, will skip the drive name (eg. C: or D:) */ pp += root_path_offset; /* On Windows, will skip the drive name (eg. C: or D:) */
while (error == GIT_SUCCESS && (sp = strchr(pp, '/')) != 0) { while (error == GIT_SUCCESS && (sp = strchr(pp, '/')) != NULL) {
if (sp != pp && gitfo_isdir(path_copy) < GIT_SUCCESS) { if (sp != pp && gitfo_isdir(path_copy) < GIT_SUCCESS) {
*sp = 0; *sp = 0;
error = gitfo_mkdir(path_copy, mode); error = gitfo_mkdir(path_copy, mode);
...@@ -395,8 +395,11 @@ int gitfo_mkdir_recurs(const char *path, int mode) ...@@ -395,8 +395,11 @@ int gitfo_mkdir_recurs(const char *path, int mode)
pp = sp + 1; pp = sp + 1;
} }
if (*(pp - 1) != '/' && error == GIT_SUCCESS) if (*pp != '\0' && error == GIT_SUCCESS) {
error = gitfo_mkdir(path, mode); error = gitfo_mkdir(path, mode);
if (errno == EEXIST)
error = GIT_SUCCESS;
}
free(path_copy); free(path_copy);
......
...@@ -330,7 +330,7 @@ static int abspath(char *buffer_out, size_t size, const char *path) ...@@ -330,7 +330,7 @@ static int abspath(char *buffer_out, size_t size, const char *path)
return GIT_SUCCESS; return GIT_SUCCESS;
} }
static dev_t retrieve_device(dev_t *device_out, const char *path) static int retrieve_device(dev_t *device_out, const char *path)
{ {
struct stat path_info; struct stat path_info;
...@@ -369,8 +369,7 @@ static int retrieve_ceiling_directories_offset(const char *path, const char *cei ...@@ -369,8 +369,7 @@ static int retrieve_ceiling_directories_offset(const char *path, const char *cei
strncpy(buf, ceil, len); strncpy(buf, ceil, len);
buf[len] = '\0'; buf[len] = '\0';
gitfo_posixify_path(buf); if (abspath(buf2, sizeof(buf2), buf) < GIT_SUCCESS)
if (gitfo_prettify_dir_path(buf2, sizeof(buf2), buf, NULL) < GIT_SUCCESS)
continue; continue;
len = strlen(buf2); len = strlen(buf2);
...@@ -410,15 +409,22 @@ static int read_gitfile(char *path_out, size_t size, const char *file_path, cons ...@@ -410,15 +409,22 @@ static int read_gitfile(char *path_out, size_t size, const char *file_path, cons
end_offset = strlen(data) - 1; end_offset = strlen(data) - 1;
for (;data[end_offset] != '\r' && data[end_offset] != '\n'; --end_offset); for (;data[end_offset] == '\r' || data[end_offset] == '\n'; --end_offset);
data[end_offset] = '\0'; data[end_offset + 1] = '\0';
if (GIT_FILE_CONTENT_PREFIX_LENGTH == end_offset) { if (GIT_FILE_CONTENT_PREFIX_LENGTH == end_offset + 1) {
gitfo_free_buf(&file); gitfo_free_buf(&file);
return git__throw(GIT_ENOTFOUND, "No path in git file `%s`", file_path); return git__throw(GIT_ENOTFOUND, "No path in git file `%s`", file_path);
} }
error = gitfo_prettify_dir_path(path_out, size, data + GIT_FILE_CONTENT_PREFIX_LENGTH, base_path); error = gitfo_prettify_dir_path(path_out, size, data + GIT_FILE_CONTENT_PREFIX_LENGTH, base_path);
if (error == GIT_SUCCESS) {
end_offset = strlen(path_out);
if (end_offset > 0 && path_out[end_offset - 1] == '/')
path_out[end_offset - 1 ] = '\0';
}
gitfo_free_buf(&file); gitfo_free_buf(&file);
return error; return error;
...@@ -521,12 +527,6 @@ int git_repository_discover(char *repository_path, size_t size, const char *star ...@@ -521,12 +527,6 @@ int git_repository_discover(char *repository_path, size_t size, const char *star
git_repository__free_dirs(&repo); git_repository__free_dirs(&repo);
//nothing has been found, lets try the parent directory
if (bare_path[ceiling_offset] == '\0') {
error = git__throw(GIT_ENOTAREPO,"Not a git repository (or any of the parent directories): %s", start_path);
goto cleanup;
}
if (git__dirname_r(normal_path, sizeof(normal_path), bare_path) < GIT_SUCCESS) if (git__dirname_r(normal_path, sizeof(normal_path), bare_path) < GIT_SUCCESS)
goto cleanup; goto cleanup;
...@@ -547,6 +547,13 @@ int git_repository_discover(char *repository_path, size_t size, const char *star ...@@ -547,6 +547,13 @@ int git_repository_discover(char *repository_path, size_t size, const char *star
strcpy(bare_path, normal_path); strcpy(bare_path, normal_path);
git__joinpath(normal_path, bare_path, DOT_GIT); git__joinpath(normal_path, bare_path, DOT_GIT);
//nothing has been found, lets try the parent directory
if (bare_path[ceiling_offset] == '\0') {
error = git__throw(GIT_ENOTAREPO,"Not a git repository (or any of the parent directories): %s", start_path);
goto cleanup;
}
} }
if (size < (strlen(found_path) + 1) * sizeof(char)) { if (size < (strlen(found_path) + 1) * sizeof(char)) {
......
...@@ -268,6 +268,142 @@ BEGIN_TEST(empty0, "test if a repository is empty or not") ...@@ -268,6 +268,142 @@ BEGIN_TEST(empty0, "test if a repository is empty or not")
git_repository_free(repo_empty); git_repository_free(repo_empty);
END_TEST END_TEST
#define DISCOVER_FOLDER TEST_RESOURCES "/discover.git"
#define SUB_REPOSITORY_FOLDER_NAME "sub_repo"
#define SUB_REPOSITORY_FOLDER DISCOVER_FOLDER "/" SUB_REPOSITORY_FOLDER_NAME
#define SUB_REPOSITORY_FOLDER_SUB SUB_REPOSITORY_FOLDER "/sub"
#define SUB_REPOSITORY_FOLDER_SUB_SUB SUB_REPOSITORY_FOLDER_SUB "/subsub"
#define SUB_REPOSITORY_FOLDER_SUB_SUB_SUB SUB_REPOSITORY_FOLDER_SUB_SUB "/subsubsub"
#define REPOSITORY_ALTERNATE_FOLDER DISCOVER_FOLDER "/alternate_sub_repo"
#define REPOSITORY_ALTERNATE_FOLDER_SUB REPOSITORY_ALTERNATE_FOLDER "/sub"
#define REPOSITORY_ALTERNATE_FOLDER_SUB_SUB REPOSITORY_ALTERNATE_FOLDER_SUB "/subsub"
#define REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB REPOSITORY_ALTERNATE_FOLDER_SUB_SUB "/subsubsub"
#define ALTERNATE_MALFORMED_FOLDER1 DISCOVER_FOLDER "/alternate_malformed_repo1"
#define ALTERNATE_MALFORMED_FOLDER2 DISCOVER_FOLDER "/alternate_malformed_repo2"
#define ALTERNATE_MALFORMED_FOLDER3 DISCOVER_FOLDER "/alternate_malformed_repo3"
#define ALTERNATE_NOT_FOUND_FOLDER DISCOVER_FOLDER "/alternate_not_found_repo"
static int ensure_repository_discover(const char *start_path, const char *ceiling_dirs, const char *expected_path)
{
int error;
char found_path[GIT_PATH_MAX];
error = git_repository_discover(found_path, sizeof(found_path), start_path, 0, ceiling_dirs);
//across_fs is always 0 as we can't automate the filesystem change tests
if (error < GIT_SUCCESS)
return error;
return strcmp(found_path, expected_path) ? GIT_ERROR : GIT_SUCCESS;
}
static int write_file(const char *path, const char *content)
{
int error;
git_file file;
if (gitfo_exists(path) == GIT_SUCCESS) {
error = gitfo_unlink(path);
if (error < GIT_SUCCESS)
return error;
}
file = gitfo_creat_force(path, 0644);
if (file < GIT_SUCCESS)
return file;
error = gitfo_write(file, (void*)content, strlen(content) * sizeof(char));
gitfo_close(file);
return error;
}
//no check is performed on ceiling_dirs length, so be sure it's long enough
static int append_ceiling_dir(char *ceiling_dirs, const char *path)
{
int len = strlen(ceiling_dirs);
int error;
error = gitfo_prettify_dir_path(ceiling_dirs + len + (len ? 1 : 0), GIT_PATH_MAX, path, NULL);
if (error < GIT_SUCCESS)
return git__rethrow(error, "Failed to append ceiling directory.");
if (len)
ceiling_dirs[len] = ':';
return GIT_SUCCESS;
}
BEGIN_TEST(discover0, "test discover")
git_repository *repo;
char ceiling_dirs[GIT_PATH_MAX * 2] = "";
char repository_path[GIT_PATH_MAX];
char sub_repository_path[GIT_PATH_MAX];
char found_path[GIT_PATH_MAX];
int mode = 0755;
rmdir_recurs(DISCOVER_FOLDER);
must_pass(append_ceiling_dir(ceiling_dirs,TEST_RESOURCES));
gitfo_mkdir_recurs(DISCOVER_FOLDER, mode);
must_be_true(git_repository_discover(repository_path, sizeof(repository_path), DISCOVER_FOLDER, 0, ceiling_dirs) == GIT_ENOTAREPO);
must_pass(git_repository_init(&repo, DISCOVER_FOLDER, 1));
must_pass(git_repository_discover(repository_path, sizeof(repository_path), DISCOVER_FOLDER, 0, ceiling_dirs));
must_pass(git_repository_init(&repo, SUB_REPOSITORY_FOLDER, 0));
must_pass(gitfo_mkdir_recurs(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, mode));
must_pass(git_repository_discover(sub_repository_path, sizeof(sub_repository_path), SUB_REPOSITORY_FOLDER, 0, ceiling_dirs));
must_pass(gitfo_mkdir_recurs(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, mode));
must_pass(ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB, ceiling_dirs, sub_repository_path));
must_pass(ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB, ceiling_dirs, sub_repository_path));
must_pass(ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, ceiling_dirs, sub_repository_path));
must_pass(gitfo_mkdir_recurs(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, mode));
must_pass(write_file(REPOSITORY_ALTERNATE_FOLDER "/" DOT_GIT, "gitdir: ../" SUB_REPOSITORY_FOLDER_NAME "/" DOT_GIT));
must_pass(write_file(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB "/" DOT_GIT, "gitdir: ../../../" SUB_REPOSITORY_FOLDER_NAME "/" DOT_GIT));
must_pass(write_file(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB "/" DOT_GIT, "gitdir: ../../../../"));
must_pass(ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, sub_repository_path));
must_pass(ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, sub_repository_path));
must_pass(ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, sub_repository_path));
must_pass(ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, repository_path));
must_pass(gitfo_mkdir_recurs(ALTERNATE_MALFORMED_FOLDER1, mode));
must_pass(write_file(ALTERNATE_MALFORMED_FOLDER1 "/" DOT_GIT, "Anything but not gitdir:"));
must_pass(gitfo_mkdir_recurs(ALTERNATE_MALFORMED_FOLDER2, mode));
must_pass(write_file(ALTERNATE_MALFORMED_FOLDER2 "/" DOT_GIT, "gitdir:"));
must_pass(gitfo_mkdir_recurs(ALTERNATE_MALFORMED_FOLDER3, mode));
must_pass(write_file(ALTERNATE_MALFORMED_FOLDER3 "/" DOT_GIT, "gitdir: \n\n\n"));
must_pass(gitfo_mkdir_recurs(ALTERNATE_NOT_FOUND_FOLDER, mode));
must_pass(write_file(ALTERNATE_NOT_FOUND_FOLDER "/" DOT_GIT, "gitdir: a_repository_that_surely_does_not_exist"));
must_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER1, 0, ceiling_dirs));
must_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER2, 0, ceiling_dirs));
must_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER3, 0, ceiling_dirs));
must_be_true(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_NOT_FOUND_FOLDER, 0, ceiling_dirs) == GIT_ENOTAREPO);
must_pass(append_ceiling_dir(ceiling_dirs, SUB_REPOSITORY_FOLDER));
//this must pass as ceiling_directories cannot predent the current
//working directory to be checked
must_pass(git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER, 0, ceiling_dirs));
must_be_true(git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB, 0, ceiling_dirs) == GIT_ENOTAREPO);
must_be_true(git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB_SUB, 0, ceiling_dirs) == GIT_ENOTAREPO);
must_be_true(git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, 0, ceiling_dirs) == GIT_ENOTAREPO);
//.gitfile redirection should not be affected by ceiling directories
must_pass(ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, sub_repository_path));
must_pass(ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, sub_repository_path));
must_pass(ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, sub_repository_path));
must_pass(ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, repository_path));
rmdir_recurs(DISCOVER_FOLDER);
END_TEST
BEGIN_SUITE(repository) BEGIN_SUITE(repository)
ADD_TEST(odb0); ADD_TEST(odb0);
ADD_TEST(odb1); ADD_TEST(odb1);
...@@ -278,5 +414,6 @@ BEGIN_SUITE(repository) ...@@ -278,5 +414,6 @@ BEGIN_SUITE(repository)
ADD_TEST(open1); ADD_TEST(open1);
ADD_TEST(open2); ADD_TEST(open2);
ADD_TEST(empty0); ADD_TEST(empty0);
ADD_TEST(discover0);
END_SUITE END_SUITE
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment