Subject: [PATCH 5/7] setup: rework core.worktree

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Wed, 27 Oct 2010 21:58:14 +0700

Now that core.worktree can only be effective inside
setup_explicit_git_dir, move the code up from setup_git_directory().
---
 setup.c |   48 +++++++++++++++++-------------------------------
 1 files changed, 17 insertions(+), 31 deletions(-)

diff --git a/setup.c b/setup.c
index e8273c3..1a4765d 100644
--- a/setup.c
+++ b/setup.c
@@ -323,7 +323,9 @@ const char *read_gitfile_gently(const char *path)
 }
 
 static const char *setup_explicit_git_dir(const char *gitdirenv,
-				const char *work_tree_env, int *nongit_ok)
+					  const char *work_tree_env,
+					  char *cwd, int len,
+					  int *nongit_ok)
 {
 	static char buffer[1024 + 1];
 	const char *retval, *gitfile;
@@ -340,23 +342,21 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
 	}
 	if (gitfile)
 		set_git_dir(gitfile);
-	if (!work_tree_env) {
-		retval = set_work_tree(gitdirenv);
-		/* config may override worktree */
-		if (check_repository_format_gently(nongit_ok))
-			return NULL;
-		return retval;
-	}
+	if (!work_tree_env)
+		set_work_tree(gitdirenv);
+
+	/* config may override worktree */
 	if (check_repository_format_gently(nongit_ok))
 		return NULL;
+
 	retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
-			get_git_work_tree());
+				  get_git_work_tree());
 	if (!retval || !*retval)
 		return NULL;
 	if (!gitfile)
 		set_git_dir(make_absolute_path(gitdirenv));
-	if (chdir(work_tree_env) < 0)
-		die_errno ("Could not chdir to '%s'", work_tree_env);
+	if (chdir(get_git_work_tree()) < 0)
+		die_errno ("Could not chdir to '%s'", get_git_work_tree());
 	strcat(buffer, "/");
 	return retval;
 }
@@ -454,6 +454,10 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
 	if (nongit_ok)
 		*nongit_ok = 0;
 
+	if (!getcwd(cwd, sizeof(cwd)-1))
+		die_errno("Unable to read current working directory");
+	offset = len = strlen(cwd);
+
 	/*
 	 * If GIT_DIR is set explicitly, we're not going
 	 * to do any discovery, but we still do repository
@@ -462,7 +466,7 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
 	gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
 	if (gitdirenv) {
 		has_git_dir_env = 1;
-		return setup_explicit_git_dir(gitdirenv, work_tree_env, nongit_o, nongit_ok);
+		return setup_explicit_git_dir(gitdirenv, work_tree_env, cwd, len, nongit_ok);
 	}
 	else {
 		/* prevent get_git_work_tree() from using it because GIT_DIR is not set */
@@ -470,9 +474,6 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
 			unsetenv(GIT_WORK_TREE_ENVIRONMENT);
 	}
 
-	if (!getcwd(cwd, sizeof(cwd)-1))
-		die_errno("Unable to read current working directory");
-
 	ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs);
 	if (ceil_offset < 0 && has_dos_drive_prefix(cwd))
 		ceil_offset = 1;
@@ -488,7 +489,6 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
 	 * - ../../.git/
 	 *   etc.
 	 */
-	offset = len = strlen(cwd);
 	one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
 	if (one_filesystem)
 		current_device = get_device_or_die(".", NULL);
@@ -623,19 +623,5 @@ int check_repository_format(void)
  */
 const char *setup_git_directory(void)
 {
-	const char *retval = setup_git_directory_gently(NULL);
-
-	/* If the work tree is not the default one, recompute prefix */
-	if (inside_work_tree < 0) {
-		static char buffer[PATH_MAX + 1];
-		char *rel;
-		if (retval && chdir(retval))
-			die_errno ("Could not jump back into original cwd");
-		rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
-		if (rel && *rel && chdir(get_git_work_tree()))
-			die_errno ("Could not jump to working directory");
-		return rel && *rel ? strcat(rel, "/") : NULL;
-	}
-
-	return retval;
+	return setup_git_directory_gently(NULL);
 }
-- 
1.7.0.2.445.gcbdb3