git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Dec 26, 2010, 11:46 UTC
Message-ID
<1293364014-8463-2-git-send-email-pclouds@gmail.com>
In-Reply-To
<1293364014-8463-1-git-send-email-pclouds@gmail.com>

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 <pclouds@gmail.com>
---
 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
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 4 of 9 in “t1501: avoid bashisms”
  1. 1/2 t1501: avoid bashismsNguyễn Thái Ngọc Duy, Dec 25, 2010
  2. 2/2 setup_work_tree: adjust relative $GIT_WORK_TREE after moving cwdNguyễn Thái Ngọc Duy, Dec 25, 2010
  3. setup_work_tree: adjust relative $GIT_WORK_TREE after moving cwdNguyễn Thái Ngọc Duy, Dec 26, 2010
  4. setup_explicit_git_dir: adjust relative $GIT_WORK_TREE after moving cwdNguyễn Thái Ngọc Duy, Dec 26, 2010
  5. setup_work_tree: adjust relative $GIT_WORK_TREE after moving cwdNguyễn Thái Ngọc Duy, Dec 27, 2010
  6. Andreas SchwabDec 25, 2010
  7. Nguyen Thai Ngoc DuyDec 25, 2010
  8. Nguyen Thai Ngoc DuyDec 25, 2010
  9. Junio C HamanoDec 26, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.