@matheusbsbs / matheus-bagliano / commits / dd5c8af176
Fork 0

Fix setup_git_directory_gently() with relative GIT_DIR & GIT_WORK_TREE

There are a few programs, such as config and diff, which allow running without a git repository. Therefore, they have to call setup_git_directory_gently(). However, when GIT_DIR and GIT_WORK_TREE were set, and the current directory was a subdirectory of the work tree, setup_git_directory_gently() would return a bogus NULL prefix. This patch fixes that. Noticed by REPLeffect on IRC. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Johannes Schindelin committed Oct 17, 2007 at 00:37 UTC dd5c8af176bb935a0b01a7dc2d5e022565c3aac3
2 files changed +21 -1
setup.c
+12 -1
@@ -227,9 +227,20 @@ const char *setup_git_directory_gently(int *nongit_ok)
227 if (PATH_MAX - 40 < strlen(gitdirenv))
228 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
229 if (is_git_directory(gitdirenv)) {
230 + static char buffer[1024 + 1];
231 + const char *retval;
232 +
233 if (!work_tree_env)
234 return set_work_tree(gitdirenv);
232 - return NULL;
235 + retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
236 + get_git_work_tree());
237 + if (!retval || !*retval)
238 + return NULL;
239 + set_git_dir(make_absolute_path(gitdirenv));
240 + if (chdir(work_tree_env) < 0)
241 + die ("Could not chdir to %s", work_tree_env);
242 + strcat(buffer, "/");
243 + return retval;
244 }
245 if (nongit_ok) {
246 *nongit_ok = 1;
t/t1501-worktree.sh
+9
@@ -103,4 +103,13 @@ test_expect_success 'repo finds its work tree from work tree, too' '
103 test sub/dir/tracked = "$(git ls-files)")
104 '
105
106 +test_expect_success '_gently() groks relative GIT_DIR & GIT_WORK_TREE' '
107 + cd repo.git/work/sub/dir &&
108 + GIT_DIR=../../.. GIT_WORK_TREE=../.. GIT_PAGER= \
109 + git diff --exit-code tracked &&
110 + echo changed > tracked &&
111 + ! GIT_DIR=../../.. GIT_WORK_TREE=../.. GIT_PAGER= \
112 + git diff --exit-code tracked
113 +'
114 +
115 test_done