git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>,
	Jonathan Niedier <jrnieder@gmail.com>,
	Sverre Rabbelier <srabbelier@gmail.com>
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 7/9] setup: clean up setup_discovered_git_dir()
Date: Mon,  1 Nov 2010 13:26:32 +0700	[thread overview]
Message-ID: <1288592794-24221-7-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <AANLkTim-uW4Esk1bkPzNNGRb8svZoNwCUvXF3Fqb4QmR@mail.gmail.com>

If core.bare is true, discard the discovered worktree, move back to
original cwd.

This change may set GIT_DIR env var to ".git" via set_git_dir(). This
bothers alias handling because handle_alias() does

1. prefix = setup_git_directory_gently(..)
2. look up alias
3. chdir(prefix)

After step 1, GIT_DIR may be set to ".git" (relative to worktree), but
step 3 moves cwd back to original cwd. When the next
setup_git_directory_gently() is called by run_builtin(), it will
mistakenly find ".git" in original cwd.

As a workaround, teach set_git_dir() not to set to
".git". setup_git_env() will default to ".git" later on. Alias
handling code will need more work later.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 The alias breakage is new. It should be fixed by calling setup_git_*
 only once. No "chdir(prefix)" any more. And that fix will be needed
 before we can turn more git_dir to relative path.

 But for now the workaround is enough, I think.

 environment.c         |    6 +++-
 setup.c               |   71 ++++++++++++++++++++++++-------------------------
 t/t1510-repo-setup.sh |   24 ++++++++--------
 3 files changed, 51 insertions(+), 50 deletions(-)

diff --git a/environment.c b/environment.c
index d811049..15764ee 100644
--- a/environment.c
+++ b/environment.c
@@ -193,8 +193,10 @@ char *get_graft_file(void)
 
 int set_git_dir(const char *path)
 {
-	if (setenv(GIT_DIR_ENVIRONMENT, path, 1))
-		return error("Could not set GIT_DIR to '%s'", path);
+	if (strcmp(path, DEFAULT_GIT_DIR_ENVIRONMENT)) {
+		if (setenv(GIT_DIR_ENVIRONMENT, path, 1))
+			return error("Could not set GIT_DIR to '%s'", path);
+	}
 	setup_git_env();
 	return 0;
 }
diff --git a/setup.c b/setup.c
index a6cc044..2dd5f78 100644
--- a/setup.c
+++ b/setup.c
@@ -362,39 +362,26 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
 	return retval;
 }
 
-static int cwd_contains_git_dir(const char **gitfile_dirp)
+static const char *setup_discovered_git_dir(const char *gitdir,
+					    char *cwd, int offset, int len,
+					    int *nongit_ok)
 {
-	const char *gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
-	*gitfile_dirp = gitfile_dir;
-	if (gitfile_dir) {
-		if (set_git_dir(gitfile_dir))
-			die("Repository setup failed");
-		return 1;
-	}
-	if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) {
-		*gitfile_dirp = DEFAULT_GIT_DIR_ENVIRONMENT;
-		return 1;
-	}
-	return 0;
-}
+	if (check_repository_format_gently(gitdir, nongit_ok))
+		return NULL;
 
-static const char *setup_discovered_git_dir(const char *work_tree_env,
-					    const char *gitdir,
-					    int offset, int len,
-					    char *cwd, int *nongit_ok)
-{
-	int root_len;
-	char *work_tree;
+	/* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
+	if (is_bare_repository_cfg > 0) {
+		set_git_dir(offset == len ? gitdir : make_absolute_path(gitdir));
+		if (chdir(cwd))
+			die_errno("Could not come back to cwd");
+		return NULL;
+	}
 
+	/* #0, #1, #5, #8, #9, #12, #13 */
+	set_git_work_tree(".");
+	set_git_dir(gitdir);
 	inside_git_dir = 0;
-	if (!work_tree_env)
-		inside_work_tree = 1;
-	root_len = offset_1st_component(cwd);
-	work_tree = xstrndup(cwd, offset > root_len ? offset : root_len);
-	set_git_work_tree(work_tree);
-	free(work_tree);
-	if (check_repository_format_gently(gitdir, nongit_ok))
-		return NULL;
+	inside_work_tree = 1;
 	if (offset == len)
 		return NULL;
 
@@ -456,8 +443,8 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
 	const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
 	const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
 	static char cwd[PATH_MAX+1];
-	const char *gitdirenv;
-	const char *gitfile_dir;
+	const char *gitdirenv, *ret;
+	char *gitfile;
 	int len, offset, ceil_offset;
 	dev_t current_device = 0;
 	int one_filesystem = 1;
@@ -502,11 +489,23 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
 	if (one_filesystem)
 		current_device = get_device_or_die(".", NULL);
 	for (;;) {
-		if (cwd_contains_git_dir(&gitfile_dir))
-			return setup_discovered_git_dir(work_tree_env,
-							gitfile_dir,
-							offset, len,
-							cwd, nongit_ok);
+		gitfile = (char*)read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
+		if (gitfile)
+			gitdirenv = gitfile = xstrdup(gitfile);
+		else {
+			if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
+				gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT;
+		}
+
+		if (gitdirenv) {
+			ret = setup_discovered_git_dir(gitdirenv,
+						       cwd, offset, len,
+						       nongit_ok);
+			free(gitfile);
+			return ret;
+		}
+		free(gitfile);
+
 		if (is_git_directory("."))
 			return setup_bare_git_dir(cwd, offset, len, nongit_ok);
 
diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh
index 629ba34..d492996 100755
--- a/t/t1510-repo-setup.sh
+++ b/t/t1510-repo-setup.sh
@@ -2343,7 +2343,7 @@ EOF
 	test_repo 16
 '
 
-test_expect_failure '#16.2: in subdir' '
+test_expect_success '#16.2: in subdir' '
 	cat >16/sub/expected <<EOF &&
 setup: git_dir: $TRASH_DIRECTORY/16/.git
 setup: worktree: (null)
@@ -2471,7 +2471,7 @@ EOF
 	test_repo 17
 '
 
-test_expect_failure '#17.2: in subdir' '
+test_expect_success '#17.2: in subdir' '
 	cat >17/sub/expected <<EOF &&
 setup: git_dir: $TRASH_DIRECTORY/17/.git
 setup: worktree: (null)
@@ -2936,7 +2936,7 @@ EOF
 	test_repo 20
 '
 
-test_expect_failure '#20.2: in subdir' '
+test_expect_success '#20.2: in subdir' '
 	cat >20/sub/expected <<EOF &&
 setup: git_dir: $TRASH_DIRECTORY/20/.git
 setup: worktree: (null)
@@ -3065,7 +3065,7 @@ EOF
 	test_repo 21
 '
 
-test_expect_failure '#21.2: in subdir' '
+test_expect_success '#21.2: in subdir' '
 	cat >21/sub/expected <<EOF &&
 setup: git_dir: $TRASH_DIRECTORY/21/.git
 setup: worktree: (null)
@@ -3703,7 +3703,7 @@ test_expect_success '#24: setup' '
 	cd ..
 '
 
-test_expect_failure '#24: at root' '
+test_expect_success '#24: at root' '
 	cat >24/expected <<EOF &&
 setup: git_dir: $TRASH_DIRECTORY/24.git
 setup: worktree: (null)
@@ -3713,7 +3713,7 @@ EOF
 	test_repo 24
 '
 
-test_expect_failure '#24: in subdir' '
+test_expect_success '#24: in subdir' '
 	cat >24/sub/expected <<EOF &&
 setup: git_dir: $TRASH_DIRECTORY/24.git
 setup: worktree: (null)
@@ -3752,7 +3752,7 @@ test_expect_success '#25: setup' '
 	cd ..
 '
 
-test_expect_failure '#25: at root' '
+test_expect_success '#25: at root' '
 	cat >25/expected <<EOF &&
 setup: git_dir: $TRASH_DIRECTORY/25.git
 setup: worktree: (null)
@@ -3762,7 +3762,7 @@ EOF
 	test_repo 25
 '
 
-test_expect_failure '#25: in subdir' '
+test_expect_success '#25: in subdir' '
 	cat >25/sub/expected <<EOF &&
 setup: git_dir: $TRASH_DIRECTORY/25.git
 setup: worktree: (null)
@@ -4137,7 +4137,7 @@ test_expect_success '#28: setup' '
 	cd ..
 '
 
-test_expect_failure '#28: at root' '
+test_expect_success '#28: at root' '
 	cat >28/expected <<EOF &&
 setup: git_dir: $TRASH_DIRECTORY/28.git
 setup: worktree: (null)
@@ -4147,7 +4147,7 @@ EOF
 	test_repo 28
 '
 
-test_expect_failure '#28: in subdir' '
+test_expect_success '#28: in subdir' '
 	cat >28/sub/expected <<EOF &&
 setup: git_dir: $TRASH_DIRECTORY/28.git
 setup: worktree: (null)
@@ -4186,7 +4186,7 @@ test_expect_success '#29: setup' '
 	cd ..
 '
 
-test_expect_failure '#29: at root' '
+test_expect_success '#29: at root' '
 	cat >29/expected <<EOF &&
 setup: git_dir: $TRASH_DIRECTORY/29.git
 setup: worktree: (null)
@@ -4196,7 +4196,7 @@ EOF
 	test_repo 29
 '
 
-test_expect_failure '#29: in subdir' '
+test_expect_success '#29: in subdir' '
 	cat >29/sub/expected <<EOF &&
 setup: git_dir: $TRASH_DIRECTORY/29.git
 setup: worktree: (null)
-- 
1.7.3.2.210.g045198

  parent reply	other threads:[~2010-11-01  6:27 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-29  6:48 [PATCH 00/42] repo setup test cases and fixes Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 01/42] builtins: print setup info if repo is found Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 02/42] Add t1510 and basic rules that run repo setup Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 03/42] t1510: setup case #0 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 04/42] t1510: setup case #1 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 05/42] t1510: setup case #2 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 06/42] t1510: setup case #3 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 07/42] t1510: setup case #4 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 08/42] t1510: setup case #5 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 09/42] t1510: setup case #6 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 10/42] t1510: setup case #7 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 11/42] t1510: setup case #8 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 12/42] t1510: setup case #9 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 13/42] t1510: setup case #10 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 14/42] t1510: setup case #11 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 15/42] t1510: setup case #12 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 16/42] t1510: setup case #13 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 17/42] t1510: setup case #14 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 18/42] t1510: setup case #15 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 19/42] t1510: setup case #16 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 20/42] t1510: setup case #17 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 21/42] t1510: setup case #18 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 22/42] t1510: setup case #19 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 23/42] t1510: setup case #20 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 24/42] t1510: setup case #21 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 25/42] t1510: setup case #22 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 26/42] t1510: setup case #23 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 27/42] t1510: setup case #24 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 28/42] t1510: setup case #25 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 29/42] t1510: setup case #26 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 30/42] t1510: setup case #27 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 31/42] t1510: setup case #28 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 32/42] t1510: setup case #29 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 33/42] t1510: setup case #30 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 34/42] t1510: setup case #31 Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 35/42] rev-parse: prints --git-dir relative to user's cwd Nguyễn Thái Ngọc Duy
2010-10-29 17:06   ` Sverre Rabbelier
2010-10-30  4:42     ` Nguyen Thai Ngoc Duy
2010-10-30  7:09       ` Jonathan Nieder
2010-10-29  6:48 ` [PATCH 36/42] Add git_config_early() Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 37/42] Use git_config_early() instead of git_config() during repo setup Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 38/42] Remove all logic from get_git_work_tree() Nguyễn Thái Ngọc Duy
2010-10-29 17:09   ` Sverre Rabbelier
2010-10-30  4:38     ` Nguyen Thai Ngoc Duy
2010-11-01  6:26     ` [PATCH 1/9] git-rev-parse.txt: clarify --git-dir Nguyễn Thái Ngọc Duy
2010-11-01  6:26     ` [PATCH 2/9] rev-parse: prints --git-dir relative to user's cwd Nguyễn Thái Ngọc Duy
2010-11-01  6:26     ` [PATCH 3/9] Add git_config_early() Nguyễn Thái Ngọc Duy
2010-11-01  6:26     ` [PATCH 4/9] Use git_config_early() instead of git_config() during repo setup Nguyễn Thái Ngọc Duy
2010-11-01  6:26     ` [PATCH 5/9] setup: limit get_git_work_tree()'s to explicit setup case only Nguyễn Thái Ngọc Duy
2010-11-01  6:26     ` [PATCH 6/9] setup: clean up setup_bare_git_dir() Nguyễn Thái Ngọc Duy
2010-11-01  6:26     ` Nguyễn Thái Ngọc Duy [this message]
2010-11-01  6:26     ` [PATCH 8/9] setup: rework setup_explicit_git_dir() Nguyễn Thái Ngọc Duy
2010-11-01  6:26     ` [PATCH 9/9] Remove all logic from get_git_work_tree() Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 39/42] setup: clean up setup_bare_git_dir() Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 40/42] setup: clean up setup_discovered_git_dir() Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 41/42] setup: rework setup_explicit_git_dir() Nguyễn Thái Ngọc Duy
2010-10-29  6:48 ` [PATCH 42/42] t1510: all failed tests are now fixed Nguyễn Thái Ngọc Duy
2010-10-29  8:29   ` Nguyen Thai Ngoc Duy
2010-10-29 17:04 ` [PATCH 00/42] repo setup test cases and fixes Sverre Rabbelier
2010-10-29 17:09   ` Jonathan Nieder

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

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

  git send-email \
    --in-reply-to=1288592794-24221-7-git-send-email-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jrnieder@gmail.com \
    --cc=srabbelier@gmail.com \
    /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.
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 NNTP newsgroup(s).