Subject: [PATCH 1/3] worktree setup: calculate prefix even if no worktree is found

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Mon, 8 Mar 2010 23:50:31 +0700

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.  We should have returned a
correct prefix anyway because cwd has been moved.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 I missed index-pack in my RUN_SETUP_GENTLY series. So three more patches
 on top.

 setup.c              |   19 ++++++++-----------
 t/t1500-rev-parse.sh |    2 +-
 t/t1501-worktree.sh  |    5 +++++
 3 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/setup.c b/setup.c
index bb3648c..8796c6f 100644
--- a/setup.c
+++ b/setup.c
@@ -392,13 +392,16 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
 	offset = len = strlen(cwd);
 	for (;;) {
 		gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
-		if (gitfile_dir) {
-			if (set_git_dir(gitfile_dir))
+		if (gitfile_dir || is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) {
+			if (gitfile_dir && set_git_dir(gitfile_dir))
 				die("Repository setup failed");
+			inside_git_dir = 0;
+			if (!work_tree_env)
+				inside_work_tree = 1;
+			root_len = offset_1st_component(cwd);
+			git_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len);
 			break;
 		}
-		if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
-			break;
 		if (is_git_directory(".")) {
 			inside_git_dir = 1;
 			if (!work_tree_env)
@@ -409,8 +412,7 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
 				set_git_dir(cwd);
 			} else
 				set_git_dir(".");
-			check_repository_format_gently(nongit_ok);
-			return NULL;
+			break;
 		}
 		while (--offset > ceil_offset && cwd[offset] != '/');
 		if (offset <= ceil_offset) {
@@ -426,11 +428,6 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
 			die_errno("Cannot change to '%s/..'", cwd);
 	}
 
-	inside_git_dir = 0;
-	if (!work_tree_env)
-		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))
 		return NULL;
 	if (offset == len)
diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh
index 48ee077..4ee92c7 100755
--- a/t/t1500-rev-parse.sh
+++ b/t/t1500-rev-parse.sh
@@ -42,7 +42,7 @@ test_rev_parse toplevel false false true '' .git
 cd .git || exit 1
 test_rev_parse .git/ false true false '' .
 cd objects || exit 1
-test_rev_parse .git/objects/ false true false '' "$ROOT/.git"
+test_rev_parse .git/objects/ false true false 'objects/' "$ROOT/.git"
 cd ../.. || exit 1
 
 mkdir -p sub/dir || exit 1
diff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh
index 9df3012..c72b578 100755
--- a/t/t1501-worktree.sh
+++ b/t/t1501-worktree.sh
@@ -28,6 +28,11 @@ test_rev_parse() {
 	[ $# -eq 0 ] && return
 }
 
+cd .git/objects || exit 1
+say "subdirectory inside .git"
+test_rev_parse 'inside'       false true false objects/
+cd ../.. || exit 1
+
 EMPTY_TREE=$(git write-tree)
 mkdir -p work/sub/dir || exit 1
 mv .git repo.git || exit 1
-- 
1.7.0.1.384.g6abcaa