Subject: [PATCH] setup_explicit_git_dir: adjust relative $GIT_WORK_TREE after moving cwd

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Sun, 26 Dec 2010 18:46:54 +0700

setup_explicit_git_dir() can move cwd. If $GIT_WORK_TREE is relative
to original cwd, then the subsequent git processes will take wrong
worktree.

Instead of making $GIT_WORK_TREE absolute too, we just say "." and let
subsequent git processes handle it.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 This one is on top of nd/setup. If nd/setup does not make it in the
 next release, I can still make similar patch for master, but I don't
 think this happens in real life, therefore not really urgent to fix.

 There are only few cases where git spawns more git processes.
 For those that do that, setup_work_tree() is likely called by git.c
 already, which is what the first patch is for.

 setup.c             |    7 +++++++
 t/t1501-worktree.sh |   12 ++++++++++++
 2 files changed, 19 insertions(+), 0 deletions(-)

diff --git a/setup.c b/setup.c
index 3d73269..10b8f16 100644
--- a/setup.c
+++ b/setup.c
@@ -392,6 +392,13 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
 		set_git_dir(make_absolute_path(gitdirenv));
 		if (chdir(worktree))
 			die_errno("Could not chdir to '%s'", worktree);
+		/*
+		 * Make sure subsequent git processes find correct worktree
+		 * if $GIT_WORK_TREE is set relative
+		 */
+		if (work_tree_env)
+			setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
+
 		cwd[len++] = '/';
 		cwd[len] = '\0';
 		free(gitfile);
diff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh
index 1f3b50d..fa35c3e 100755
--- a/t/t1501-worktree.sh
+++ b/t/t1501-worktree.sh
@@ -351,4 +351,16 @@ test_expect_success 'relative $GIT_WORK_TREE and git subprocesses' '
 	)
 '
 
+test_expect_success 'relative $GIT_WORK_TREE and git subprocesses (2)' '
+	(
+	cd repo.git/work/sub &&
+	GIT_DIR=../.. &&
+	GIT_WORK_TREE=.. &&
+	export GIT_DIR GIT_WORK_TREE &&
+	test-subprocess rev-parse --show-toplevel >actual &&
+	echo "$TRASH_DIRECTORY/repo.git/work" >expected &&
+	test_cmp expected actual
+	)
+'
+
 test_done
-- 
1.7.3.4.878.g439c7