Fix setup_git_directory_gently() with relative GIT_DIR & GIT_WORK_TREE
authorJohannes Schindelin <Johannes.Schindelin@gmx.de>
Tue, 16 Oct 2007 23:37:36 +0000 (00:37 +0100)
committerShawn O. Pearce <spearce@spearce.org>
Wed, 17 Oct 2007 00:18:04 +0000 (20:18 -0400)
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>
setup.c
t/t1501-worktree.sh
diff --git a/setup.c b/setup.c
index 06004f15874fbcbab50eafd272c5898a34fcdcfe..145eca50f41d811c4c8fcb21ed2604e6b2971aba 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -227,9 +227,20 @@ const char *setup_git_directory_gently(int *nongit_ok)
                if (PATH_MAX - 40 < strlen(gitdirenv))
                        die("'$%s' too big", GIT_DIR_ENVIRONMENT);
                if (is_git_directory(gitdirenv)) {
+                       static char buffer[1024 + 1];
+                       const char *retval;
+
                        if (!work_tree_env)
                                return set_work_tree(gitdirenv);
-                       return NULL;
+                       retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
+                                       get_git_work_tree());
+                       if (!retval || !*retval)
+                               return NULL;
+                       set_git_dir(make_absolute_path(gitdirenv));
+                       if (chdir(work_tree_env) < 0)
+                               die ("Could not chdir to %s", work_tree_env);
+                       strcat(buffer, "/");
+                       return retval;
                }
                if (nongit_ok) {
                        *nongit_ok = 1;
index 732216184ffc255a385aac81ba970edfc6a9e83a..7ee3820ce97b6c26c9465685a7bc64a962aad3cb 100755 (executable)
@@ -103,4 +103,13 @@ test_expect_success 'repo finds its work tree from work tree, too' '
         test sub/dir/tracked = "$(git ls-files)")
 '
 
+test_expect_success '_gently() groks relative GIT_DIR & GIT_WORK_TREE' '
+       cd repo.git/work/sub/dir &&
+       GIT_DIR=../../.. GIT_WORK_TREE=../.. GIT_PAGER= \
+               git diff --exit-code tracked &&
+       echo changed > tracked &&
+       ! GIT_DIR=../../.. GIT_WORK_TREE=../.. GIT_PAGER= \
+               git diff --exit-code tracked
+'
+
 test_done