Commit 24b9c4b6 by Edward Thomson

repo: honor GIT_COMMON_DIR when respecting env

When the repository is opened with `GIT_REPOSITORY_OPEN_FROM_ENV`, honor
the `GIT_COMMON_DIR` environment variable.
parent b41c3dfc
...@@ -191,11 +191,23 @@ void git_repository_free(git_repository *repo) ...@@ -191,11 +191,23 @@ void git_repository_free(git_repository *repo)
} }
/* Check if we have a separate commondir (e.g. we have a worktree) */ /* Check if we have a separate commondir (e.g. we have a worktree) */
static int lookup_commondir(bool *separate, git_str *commondir, git_str *repository_path) static int lookup_commondir(
bool *separate,
git_str *commondir,
git_str *repository_path,
uint32_t flags)
{ {
git_str common_link = GIT_STR_INIT; git_str common_link = GIT_STR_INIT;
int error; int error;
/* Environment variable overrides configuration */
if ((flags & GIT_REPOSITORY_OPEN_FROM_ENV)) {
error = git__getenv(commondir, "GIT_COMMON_DIR");
if (!error || error != GIT_ENOTFOUND)
goto done;
}
/* /*
* If there's no commondir file, the repository path is the * If there's no commondir file, the repository path is the
* common path, but it needs a trailing slash. * common path, but it needs a trailing slash.
...@@ -222,12 +234,11 @@ static int lookup_commondir(bool *separate, git_str *commondir, git_str *reposit ...@@ -222,12 +234,11 @@ static int lookup_commondir(bool *separate, git_str *commondir, git_str *reposit
git_str_swap(commondir, &common_link); git_str_swap(commondir, &common_link);
} }
git_str_dispose(&common_link);
/* Make sure the commondir path always has a trailing slash */ /* Make sure the commondir path always has a trailing slash */
error = git_fs_path_prettify_dir(commondir, commondir->ptr, NULL); error = git_fs_path_prettify_dir(commondir, commondir->ptr, NULL);
done: done:
git_str_dispose(&common_link);
return error; return error;
} }
...@@ -252,14 +263,19 @@ GIT_INLINE(int) validate_repo_path(git_str *path) ...@@ -252,14 +263,19 @@ GIT_INLINE(int) validate_repo_path(git_str *path)
* *
* Open a repository object from its path * Open a repository object from its path
*/ */
static int is_valid_repository_path(bool *out, git_str *repository_path, git_str *common_path) static int is_valid_repository_path(
bool *out,
git_str *repository_path,
git_str *common_path,
uint32_t flags)
{ {
bool separate_commondir = false; bool separate_commondir = false;
int error; int error;
*out = false; *out = false;
if ((error = lookup_commondir(&separate_commondir, common_path, repository_path)) < 0) if ((error = lookup_commondir(&separate_commondir,
common_path, repository_path, flags)) < 0)
return error; return error;
/* Ensure HEAD file exists */ /* Ensure HEAD file exists */
...@@ -742,7 +758,7 @@ static int find_repo_traverse( ...@@ -742,7 +758,7 @@ static int find_repo_traverse(
break; break;
if (S_ISDIR(st.st_mode)) { if (S_ISDIR(st.st_mode)) {
if ((error = is_valid_repository_path(&is_valid, &path, &common_link)) < 0) if ((error = is_valid_repository_path(&is_valid, &path, &common_link, flags)) < 0)
goto out; goto out;
if (is_valid) { if (is_valid) {
...@@ -759,7 +775,7 @@ static int find_repo_traverse( ...@@ -759,7 +775,7 @@ static int find_repo_traverse(
} }
} else if (S_ISREG(st.st_mode) && git__suffixcmp(path.ptr, "/" DOT_GIT) == 0) { } else if (S_ISREG(st.st_mode) && git__suffixcmp(path.ptr, "/" DOT_GIT) == 0) {
if ((error = read_gitfile(&repo_link, path.ptr)) < 0 || if ((error = read_gitfile(&repo_link, path.ptr)) < 0 ||
(error = is_valid_repository_path(&is_valid, &repo_link, &common_link)) < 0) (error = is_valid_repository_path(&is_valid, &repo_link, &common_link, flags)) < 0)
goto out; goto out;
if (is_valid) { if (is_valid) {
...@@ -934,7 +950,7 @@ int git_repository_open_bare( ...@@ -934,7 +950,7 @@ int git_repository_open_bare(
git_config *config; git_config *config;
if ((error = git_fs_path_prettify_dir(&path, bare_path, NULL)) < 0 || if ((error = git_fs_path_prettify_dir(&path, bare_path, NULL)) < 0 ||
(error = is_valid_repository_path(&is_valid, &path, &common_path)) < 0) (error = is_valid_repository_path(&is_valid, &path, &common_path, 0)) < 0)
return error; return error;
if (!is_valid) { if (!is_valid) {
...@@ -2668,7 +2684,7 @@ int git_repository_init_ext( ...@@ -2668,7 +2684,7 @@ int git_repository_init_ext(
wd = (opts->flags & GIT_REPOSITORY_INIT_BARE) ? NULL : git_str_cstr(&wd_path); wd = (opts->flags & GIT_REPOSITORY_INIT_BARE) ? NULL : git_str_cstr(&wd_path);
if ((error = is_valid_repository_path(&is_valid, &repo_path, &common_path)) < 0) if ((error = is_valid_repository_path(&is_valid, &repo_path, &common_path, opts->flags)) < 0)
goto out; goto out;
if (is_valid) { if (is_valid) {
......
...@@ -295,3 +295,23 @@ void test_repo_env__work_tree(void) ...@@ -295,3 +295,23 @@ void test_repo_env__work_tree(void)
git_repository_free(repo); git_repository_free(repo);
cl_setenv("GIT_WORK_TREE", NULL); cl_setenv("GIT_WORK_TREE", NULL);
} }
void test_repo_env__commondir(void)
{
git_repository *repo;
const char *test_path;
cl_fixture_sandbox("attr");
cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
cl_fixture_sandbox("testrepo.git");
cl_git_pass(p_rename("testrepo.git", "test_commondir"));
test_path = cl_git_sandbox_path(1, "test_commondir", NULL);
cl_setenv("GIT_COMMON_DIR", test_path);
cl_git_pass(git_repository_open_ext(&repo, "attr", GIT_REPOSITORY_OPEN_FROM_ENV, NULL));
cl_assert_equal_s(test_path, git_repository_commondir(repo));
git_repository_free(repo);
cl_setenv("GIT_COMMON_DIR", NULL);
}
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