Commit b41c3dfc by Edward Thomson

repo: honor GIT_WORK_TREE environment variable

When the repository is opened with `GIT_REPOSITORY_OPEN_FROM_ENV`, honor
the `GIT_WORK_TREE` environment variable.
parent 54a22f53
......@@ -337,20 +337,43 @@ static int load_config_data(git_repository *repo, const git_config *config)
return 0;
}
static int load_workdir(git_repository *repo, git_config *config, git_str *parent_path)
static int load_workdir(
git_repository *repo,
git_config *config,
git_str *parent_path)
{
int error;
git_config_entry *ce;
git_config_entry *ce = NULL;
git_str worktree = GIT_STR_INIT;
git_str path = GIT_STR_INIT;
git_str workdir_env = GIT_STR_INIT;
const char *value = NULL;
int error;
if (repo->is_bare)
return 0;
if ((error = git_config__lookup_entry(
&ce, config, "core.worktree", false)) < 0)
/* Environment variables are preferred */
if (repo->use_env) {
error = git__getenv(&workdir_env, "GIT_WORK_TREE");
if (error == 0)
value = workdir_env.ptr;
else if (error == GIT_ENOTFOUND)
error = 0;
else
goto cleanup;
}
/* Examine configuration values if necessary */
if (!value) {
if ((error = git_config__lookup_entry(&ce, config,
"core.worktree", false)) < 0)
return error;
if (ce && ce->value)
value = ce->value;
}
if (repo->is_worktree) {
char *gitlink = git_worktree__read_link(repo->gitdir, GIT_GITDIR_FILE);
if (!gitlink) {
......@@ -367,17 +390,21 @@ static int load_workdir(git_repository *repo, git_config *config, git_str *paren
}
repo->workdir = git_str_detach(&worktree);
} else if (value) {
if (!*value) {
git_error_set(GIT_ERROR_NET, "working directory cannot be set to empty path");
error = -1;
goto cleanup;
}
else if (ce && ce->value) {
if ((error = git_fs_path_prettify_dir(
&worktree, ce->value, repo->gitdir)) < 0)
if ((error = git_fs_path_prettify_dir(&worktree,
value, repo->gitdir)) < 0)
goto cleanup;
repo->workdir = git_str_detach(&worktree);
}
else if (parent_path && git_fs_path_isdir(parent_path->ptr))
} else if (parent_path && git_fs_path_isdir(parent_path->ptr)) {
repo->workdir = git_str_detach(parent_path);
else {
} else {
if (git_fs_path_dirname_r(&worktree, repo->gitdir) < 0 ||
git_fs_path_to_dir(&worktree) < 0) {
error = -1;
......@@ -388,8 +415,10 @@ static int load_workdir(git_repository *repo, git_config *config, git_str *paren
}
GIT_ERROR_CHECK_ALLOC(repo->workdir);
cleanup:
git_str_dispose(&path);
git_str_dispose(&workdir_env);
git_config_entry_free(ce);
return error;
}
......
......@@ -31,6 +31,8 @@ void test_repo_env__cleanup(void)
if (git_fs_path_isdir("peeled.git"))
git_futils_rmdir_r("peeled.git", NULL, GIT_RMDIR_REMOVE_FILES);
cl_fixture_cleanup("test_workdir");
clear_git_env();
}
......@@ -275,3 +277,21 @@ void test_repo_env__open(void)
clear_git_env();
}
void test_repo_env__work_tree(void)
{
git_repository *repo;
const char *test_path;
cl_fixture_sandbox("attr");
cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
cl_must_pass(p_mkdir("test_workdir", 0777));
test_path = cl_git_sandbox_path(1, "test_workdir", NULL);
cl_setenv("GIT_WORK_TREE", 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_workdir(repo));
git_repository_free(repo);
cl_setenv("GIT_WORK_TREE", 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