summaryrefslogtreecommitdiff
path: root/environment.c
diff options
context:
space:
mode:
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>2015-06-26 17:37:35 +0700
committerJunio C Hamano <gitster@pobox.com>2015-06-26 11:52:26 -0700
commitd95138e695d99d32dcad528a2a7974f434c51e79 (patch)
treeff4744d9543239af00876c8cf32b1bf5fcd2eb10 /environment.c
parent282616c72d1d08a77ca4fe1186cb708c38408d87 (diff)
downloadgit-d95138e695d99d32dcad528a2a7974f434c51e79.tar.gz
setup: set env $GIT_WORK_TREE when work tree is set, like $GIT_DIRnd/export-worktree
In the test case, we run setup_git_dir_gently() the first time to read $GIT_DIR/config so that we can resolve aliases. We'll enter setup_discovered_git_dir() and may or may not call set_git_dir() near the end of the function, depending on whether the detected git dir is ".git" or not. This set_git_dir() will set env var $GIT_DIR. For normal repo, git dir detected via setup_discovered_git_dir() will be ".git", and set_git_dir() is not called. If .git file is used however, the git dir can't be ".git" and set_git_dir() is called and $GIT_DIR set. This is the key of this problem. If we expand an alias (or autocorrect command names), then setup_git_dir_gently() is run the second time. If $GIT_DIR is not set in the first run, we run the same setup_discovered_git_dir() as before. Nothing to see. If it is, however, we'll enter setup_explicit_git_dir() this time. This is where the "fun" is. If $GIT_WORK_TREE is not set but $GIT_DIR is, you are supposed to be at the root level of the worktree. But if you are in a subdir "foo/bar" (real worktree's top is "foo"), this rule bites you: your detected worktree is now "foo/bar", even though the first run correctly detected worktree as "foo". You get "internal error: work tree has already been set" as a result. Bottom line is, when $GIT_DIR is set, $GIT_WORK_TREE should be set too unless there's no work tree. But setting $GIT_WORK_TREE inside set_git_dir() may backfire. We don't know at that point if work tree is already configured by the caller. So set it when work tree is detected. It does not harm if $GIT_WORK_TREE is set while $GIT_DIR is not. Reported-by: Bjørnar Snoksrud <snoksrud@gmail.com> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'environment.c')
-rw-r--r--environment.c2
1 files changed, 2 insertions, 0 deletions
diff --git a/environment.c b/environment.c
index 9daa0ba4a3..36fbba57fc 100644
--- a/environment.c
+++ b/environment.c
@@ -211,6 +211,8 @@ void set_git_work_tree(const char *new_work_tree)
}
git_work_tree_initialized = 1;
work_tree = xstrdup(real_path(new_work_tree));
+ if (setenv(GIT_WORK_TREE_ENVIRONMENT, work_tree, 1))
+ die("could not set GIT_WORK_TREE to '%s'", work_tree);
}
const char *get_git_work_tree(void)