@matheusbsbs / matheus-bagliano / commits / 72183cb297
Fork 0

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 <szeder@ira.uka.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Jan 16, 2009 at 16:37 UTC 72183cb297f614dc2df1c0ed08afac1be27ec35a
2 files changed +12 -1
setup.c
+5 -1
@@ -456,7 +456,11 @@ const char *setup_git_directory_gently(int *nongit_ok)
456 inside_git_dir = 1;
457 if (!work_tree_env)
458 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);
464 check_repository_format_gently(nongit_ok);
465 return NULL;
466 }
t/t1501-worktree.sh
+7
@@ -92,6 +92,13 @@ cd sub/dir || exit 1
92 test_rev_parse 'in repo.git/sub/dir' false true true sub/dir/
93 cd ../../../.. || exit 1
94
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 +
102 test_expect_success 'repo finds its work tree' '
103 (cd repo.git &&
104 : > work/sub/dir/untracked &&