Subject: [PATCH 2/7] unset: discard GIT_WORK_TREE if GIT_DIR is not set

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Wed, 27 Oct 2010 21:58:11 +0700

GIT_WORK_TREE can only be used together with GIT_DIR. Unfortunately
get_git_work_tree() checks that env variable unconditionally. As a
work around, discard the variable.
---
 setup.c |   23 +++++++++++------------
 1 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/setup.c b/setup.c
index 2e7387d..992a944 100644
--- a/setup.c
+++ b/setup.c
@@ -364,14 +364,12 @@ static int cwd_contains_git_dir(const char **gitfile_dirp)
 	return is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT);
 }
 
-static const char *setup_discovered_git_dir(const char *work_tree_env,
-		int offset, int len, char *cwd, int *nongit_ok)
+static const char *setup_discovered_git_dir(int offset, int len, char *cwd, int *nongit_ok)
 {
 	int root_len;
 
 	inside_git_dir = 0;
-	if (!work_tree_env)
-		inside_work_tree = 1;
+	inside_work_tree = 1;
 	root_len = offset_1st_component(cwd);
 	git_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len);
 	if (check_repository_format_gently(nongit_ok))
@@ -386,14 +384,12 @@ static const char *setup_discovered_git_dir(const char *work_tree_env,
 	return cwd + offset;
 }
 
-static const char *setup_bare_git_dir(const char *work_tree_env,
-		int offset, int len, char *cwd, int *nongit_ok)
+static const char *setup_bare_git_dir(int offset, int len, char *cwd, int *nongit_ok)
 {
 	int root_len;
 
 	inside_git_dir = 1;
-	if (!work_tree_env)
-		inside_work_tree = 0;
+	inside_work_tree = 0;
 	if (offset != len) {
 		if (chdir(cwd))
 			die_errno("Cannot come back to cwd");
@@ -457,6 +453,11 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
 	gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
 	if (gitdirenv)
 		return setup_explicit_git_dir(gitdirenv, work_tree_env, nongit_ok);
+	else {
+		/* prevent get_git_work_tree() from using it because GIT_DIR is not set */
+		if (work_tree_env)
+			unsetenv(GIT_WORK_TREE_ENVIRONMENT);
+	}
 
 	if (!getcwd(cwd, sizeof(cwd)-1))
 		die_errno("Unable to read current working directory");
@@ -482,11 +483,9 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
 		current_device = get_device_or_die(".", NULL);
 	for (;;) {
 		if (cwd_contains_git_dir(&gitfile_dir))
-			return setup_discovered_git_dir(work_tree_env, offset,
-							len, cwd, nongit_ok);
+			return setup_discovered_git_dir(offset, len, cwd, nongit_ok);
 		if (is_git_directory("."))
-			return setup_bare_git_dir(work_tree_env, offset,
-							len, cwd, nongit_ok);
+			return setup_bare_git_dir(offset, len, cwd, nongit_ok);
 		while (--offset > ceil_offset && cwd[offset] != '/');
 		if (offset <= ceil_offset)
 			return setup_nongit(cwd, nongit_ok);
-- 
1.7.0.2.445.gcbdb3