Skip to content

Commit 72183cb

Browse files
szedergitster
authored andcommitted
Fix gitdir detection when in subdir of gitdir
If the current working directory is a subdirectory of the gitdir (e.g. <repo>/.git/refs/), then setup_git_directory_gently() will climb its parent directories until it finds itself in a gitdir. However, no matter how many parent directories it climbs, it sets 'GIT_DIR_ENVIRONMENT' to ".", which is obviously wrong. This behaviour affected at least 'git rev-parse --git-dir' and hence caused some errors in bash completion (e.g. customized command prompt when on a detached head and completion of refs). To fix this, we set the absolute path of the found gitdir instead. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bf474e2 commit 72183cb

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

setup.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,11 @@ const char *setup_git_directory_gently(int *nongit_ok)
456456
inside_git_dir = 1;
457457
if (!work_tree_env)
458458
inside_work_tree = 0;
459-
setenv(GIT_DIR_ENVIRONMENT, ".", 1);
459+
if (offset != len) {
460+
cwd[offset] = '\0';
461+
setenv(GIT_DIR_ENVIRONMENT, cwd, 1);
462+
} else
463+
setenv(GIT_DIR_ENVIRONMENT, ".", 1);
460464
check_repository_format_gently(nongit_ok);
461465
return NULL;
462466
}

t/t1501-worktree.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ cd sub/dir || exit 1
9292
test_rev_parse 'in repo.git/sub/dir' false true true sub/dir/
9393
cd ../../../.. || exit 1
9494

95+
test_expect_success 'detecting gitdir when cwd is in a subdir of gitdir' '
96+
(expected=$(pwd)/repo.git &&
97+
cd repo.git/refs &&
98+
unset GIT_DIR &&
99+
test "$expected" = "$(git rev-parse --git-dir)")
100+
'
101+
95102
test_expect_success 'repo finds its work tree' '
96103
(cd repo.git &&
97104
: > work/sub/dir/untracked &&

0 commit comments

Comments
 (0)