Skip to content

Commit dd5c8af

Browse files
dschospearce
authored andcommitted
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 <[email protected]> Signed-off-by: Shawn O. Pearce <[email protected]>
1 parent 33c8d38 commit dd5c8af

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

setup.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,20 @@ const char *setup_git_directory_gently(int *nongit_ok)
227227
if (PATH_MAX - 40 < strlen(gitdirenv))
228228
die("'$%s' too big", GIT_DIR_ENVIRONMENT);
229229
if (is_git_directory(gitdirenv)) {
230+
static char buffer[1024 + 1];
231+
const char *retval;
232+
230233
if (!work_tree_env)
231234
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;
233244
}
234245
if (nongit_ok) {
235246
*nongit_ok = 1;

t/t1501-worktree.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,13 @@ test_expect_success 'repo finds its work tree from work tree, too' '
103103
test sub/dir/tracked = "$(git ls-files)")
104104
'
105105

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+
106115
test_done

0 commit comments

Comments
 (0)