git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: git@vger.kernel.org, spearce@spearce.org, gitster@pobox.com
Subject: [PATCH] Fix setup_git_directory_gently() with relative GIT_DIR & GIT_WORK_TREE
Date: Wed, 17 Oct 2007 00:37:36 +0100 (BST)	[thread overview]
Message-ID: <Pine.LNX.4.64.0710170037030.25221@racer.site> (raw)


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>
---
 setup.c             |   13 ++++++++++++-
 t/t1501-worktree.sh |    9 +++++++++
 2 files changed, 21 insertions(+), 1 deletions(-)

diff --git a/setup.c b/setup.c
index 06004f1..145eca5 100644
--- 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;
diff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh
index 7322161..7ee3820 100755
--- a/t/t1501-worktree.sh
+++ b/t/t1501-worktree.sh
@@ -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
-- 
1.5.3.4.1223.ga973c

                 reply	other threads:[~2007-10-16 23:37 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.64.0710170037030.25221@racer.site \
    --to=johannes.schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=spearce@spearce.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).