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

[PATCH] Preserve cwd in setup_git_directory()

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jul 24, 2008, 03:14 UTC
Message-ID
<20080724031441.GA26072@laptop>

When GIT_DIR is not set, cwd is used to determine where .git is. If core.worktree is set, setup_git_directory() needs to jump back to the original cwd in order to calculate worktree, this leads to incorrect .git location later in setup_work_tree().

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 setup.c             |    4 ++++
 t/t1501-worktree.sh |   13 ++++++++++++-
 2 files changed, 16 insertions(+), 1 deletions(-)
diff --git a/setup.c b/setup.c
index 6cf9094..ef58761 100644
--- a/setup.c
+++ b/setup.c
@@ -577,10 +577,14 @@ const char *setup_git_directory(void)
 	/* If the work tree is not the default one, recompute prefix */
 	if (inside_work_tree < 0) {
 		static char buffer[PATH_MAX + 1];
+		static char cwd[PATH_MAX + 1];
 		char *rel;
+		getcwd(cwd, PATH_MAX);
 		if (retval && chdir(retval))
 			die ("Could not jump back into original cwd");
 		rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
+		if (retval && chdir(cwd))
+			die ("Could not jump back into original cwd");
 		return rel && *rel ? strcat(rel, "/") : NULL;
 	}
 
diff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh
index 2ee88d8..64f8dea 100755
--- a/t/t1501-worktree.sh
+++ b/t/t1501-worktree.sh
@@ -29,7 +29,18 @@ test_rev_parse() {
 }
 
 mkdir -p work/sub/dir || exit 1
-mv .git repo.git || exit 1
+
+git config core.worktree "$(pwd)"/work
+mv .git work || exit 1
+test_expect_success '--git-dir with relative .git' '
+	(
+	MYPWD="$(pwd)"
+	cd work/sub/dir &&
+	test "$MYPWD"/work/.git = "$(git rev-parse --git-dir)"
+	)
+'
+
+mv work/.git repo.git || exit 1
 
 say "core.worktree = relative path"
 GIT_DIR=repo.git
-- 
1.5.5.GIT
Next: Johannes Sixt
Message 1 of 9 in “Preserve cwd in setup_git_directory()”
  1. Preserve cwd in setup_git_directory()Nguyễn Thái Ngọc Duy, Jul 24, 2008
  2. Johannes SixtJul 24, 2008
  3. Preserve cwd in setup_git_directory()Nguyen Thai Ngoc Duy, Jul 24, 2008
  4. Johannes SchindelinJul 24, 2008
  5. Nguyen Thai Ngoc DuyJul 24, 2008
  6. Johannes SchindelinJul 24, 2008
  7. Nguyen Thai Ngoc DuyJul 24, 2008
  8. Geoff RussellJul 25, 2008
  9. Johannes SchindelinJul 25, 2008

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.