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

[PATCH 1/2] worktree setup: return to original cwd if prefix is set NULL

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jul 23, 2010, 12:04 UTC
Message-ID
<1279886651-14590-1-git-send-email-pclouds@gmail.com>

Commit a672ea6 (rehabilitate 'git index-pack' inside the object store - 2008-10-20) worked around a fault in setup_git_directory_gently(). When walking up from inside a git repository, we will return NULL as prefix.

Prefix and cwd should be consistent. That is if cwd is moved, prefix reflects that. If prefix is NULL, cwd is unmoved. In this case, to retain current behavior, we move cwd back, with an eye on gitdir because gitdir may be relative to cwd.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 setup.c             |    7 +++++++
 t/t1501-worktree.sh |   21 +++++++++++++++++++++
 2 files changed, 28 insertions(+), 0 deletions(-)
diff --git a/setup.c b/setup.c
index 87c21f0..33eb253 100644
--- a/setup.c
+++ b/setup.c
@@ -413,6 +413,13 @@ const char *setup_git_directory_gently(int *nongit_ok)
 			if (!work_tree_env)
 				inside_work_tree = 0;
 			if (offset != len) {
+				/*
+				 * The returned prefix in this code path is NULL
+				 * chdir() back to match NULL prefix, i.e. unmoved cwd
+				 */
+				if (chdir(cwd))
+					die_errno("Cannot come back to cwd");
+
 				root_len = offset_1st_component(cwd);
 				cwd[offset > root_len ? offset : root_len] = '\0';
 				set_git_dir(cwd);
diff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh
index bd8b607..edd81ce 100755
--- a/t/t1501-worktree.sh
+++ b/t/t1501-worktree.sh
@@ -33,6 +33,27 @@ mkdir -p work/sub/dir || exit 1
 mkdir -p work2 || exit 1
 mv .git repo.git || exit 1
 
+cat >repo.git/objects/patch <<\EOF
+diff one one
+--- one
++++ one
+@@ -1 +1,2 @@
+ 1
++2
+EOF
+
+test_expect_success 'cwd is unchanged when prefix is NULL (from within a repo)' '
+	(
+		cd repo.git/objects &&
+		echo 1 > one &&
+		cp one expected &&
+		echo 2 >> expected &&
+		git apply patch &&
+		test_cmp expected one &&
+		rm one expected patch
+	)
+'
+
 say "core.worktree = relative path"
 GIT_DIR=repo.git
 GIT_CONFIG="$(pwd)"/$GIT_DIR/config
-- 
1.7.1.rc1.69.g24c2f7
Next: Nguyễn Thái Ngọc Duy
Message 1 of 20 in “worktree setup: return to original cwd if prefix is set NULL”
  1. 1/2 worktree setup: return to original cwd if prefix is set NULLNguyễn Thái Ngọc Duy, Jul 23, 2010
  2. 2/2 Revert "rehabilitate 'git index-pack' inside the object store"Nguyễn Thái Ngọc Duy, Jul 23, 2010
  3. Ævar Arnfjörð BjarmasonJul 23, 2010
  4. Nguyen Thai Ngoc DuyJul 24, 2010
  5. Ævar Arnfjörð BjarmasonJul 24, 2010
  6. 1/2 worktree setup: return to original cwd if prefix is set NULLNguyễn Thái Ngọc Duy, Jul 23, 2010
  7. 0/9 setup_git_directory(): return to original cwd upon reaching .gitJonathan Nieder, Jul 24, 2010
  8. 1/9 t1501 (rev-parse): clarifyJonathan Nieder, Jul 24, 2010
  9. 2/9 tests: try git apply from subdir of toplevelJonathan Nieder, Jul 24, 2010
  10. 3/9 setup: split off $GIT_DIR-set case from setup_git_directory_gentlyJonathan Nieder, Jul 24, 2010
  11. Nguyen Thai Ngoc DuyJul 24, 2010
  12. 4/9 setup: split off a function to checks working dir for .git fileJonathan Nieder, Jul 24, 2010
  13. Nguyen Thai Ngoc DuyJul 24, 2010
  14. Jonathan NiederJul 24, 2010
  15. 5/9 setup: split off code to handle stumbling upon a repositoryJonathan Nieder, Jul 24, 2010
  16. 6/9 setup: split off a function to handle hitting ceiling in repo searchJonathan Nieder, Jul 24, 2010
  17. 7/9 setup: split off get_device_or_die helperJonathan Nieder, Jul 24, 2010
  18. 8/9 setup: do not forget working dir from subdir of gitdirJonathan Nieder, Jul 24, 2010
  19. 9/9 Revert "rehabilitate 'git index-pack' inside the object store"Jonathan Nieder, Jul 24, 2010
  20. Nguyen Thai Ngoc DuyJul 24, 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.