git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Nieder <jrnieder@gmail.com>
To: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Cc: Junio C Hamano <gitster@pobox.com>, git@vger.kernel.org
Subject: [PATCH 3/9] setup: split off $GIT_DIR-set case from setup_git_directory_gently
Date: Sat, 24 Jul 2010 06:19:44 -0500	[thread overview]
Message-ID: <20100724111944.GC16054@burratino> (raw)
In-Reply-To: <20100724111505.GC7150@burratino>

If $GIT_DIR is set, setup_git_directory_gently does not have
to do any repository discovery at all.  Split off a function
for the validation it still does do, in the hope that this will
make setup_git_directory_gently proper less daunting to read.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 setup.c |   69 +++++++++++++++++++++++++++++++++-----------------------------
 1 files changed, 37 insertions(+), 32 deletions(-)

diff --git a/setup.c b/setup.c
index 2769160..16bee6d 100644
--- a/setup.c
+++ b/setup.c
@@ -313,6 +313,41 @@ const char *read_gitfile_gently(const char *path)
 	return path;
 }
 
+static const char *setup_explicit_git_dir(const char *gitdirenv,
+				const char *work_tree_env, int *nongit_ok)
+{
+	static char buffer[1024 + 1];
+	const char *retval;
+
+	if (PATH_MAX - 40 < strlen(gitdirenv))
+		die("'$%s' too big", GIT_DIR_ENVIRONMENT);
+	if (!is_git_directory(gitdirenv)) {
+		if (nongit_ok) {
+			*nongit_ok = 1;
+			return NULL;
+		}
+		die("Not a git repository: '%s'", gitdirenv);
+	}
+	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 (check_repository_format_gently(nongit_ok))
+		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_errno ("Could not chdir to '%s'", work_tree_env);
+	strcat(buffer, "/");
+	return retval;
+}
+
 /*
  * We cannot decide in this function whether we are in the work tree or
  * not, since the config can only be read _after_ this function was called.
@@ -343,38 +378,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
 	 * validation.
 	 */
 	gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
-	if (gitdirenv) {
-		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) {
-				retval = set_work_tree(gitdirenv);
-				/* config may override worktree */
-				if (check_repository_format_gently(nongit_ok))
-					return NULL;
-				return retval;
-			}
-			if (check_repository_format_gently(nongit_ok))
-				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_errno ("Could not chdir to '%s'", work_tree_env);
-			strcat(buffer, "/");
-			return retval;
-		}
-		if (nongit_ok) {
-			*nongit_ok = 1;
-			return NULL;
-		}
-		die("Not a git repository: '%s'", gitdirenv);
-	}
+	if (gitdirenv)
+		return setup_explicit_git_dir(gitdirenv, work_tree_env, nongit_ok);
 
 	if (!getcwd(cwd, sizeof(cwd)-1))
 		die_errno("Unable to read current working directory");
-- 
1.7.2.rc3.593.g19611.dirty

  parent reply	other threads:[~2010-07-24 11:20 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-07-23 12:04 [PATCH 1/2] worktree setup: return to original cwd if prefix is set NULL Nguyễn Thái Ngọc Duy
2010-07-23 12:04 ` [PATCH 2/2] Revert "rehabilitate 'git index-pack' inside the object store" Nguyễn Thái Ngọc Duy
2010-07-23 14:38 ` [PATCH 1/2] worktree setup: return to original cwd if prefix is set NULL Ævar Arnfjörð Bjarmason
2010-07-24  0:50   ` Nguyen Thai Ngoc Duy
2010-07-24  1:16     ` Ævar Arnfjörð Bjarmason
2010-07-23 19:47       ` Nguyễn Thái Ngọc Duy
2010-07-24 11:15 ` [PATCH 0/9] setup_git_directory(): return to original cwd upon reaching .git Jonathan Nieder
2010-07-24 11:16   ` [PATCH 1/9] t1501 (rev-parse): clarify Jonathan Nieder
2010-07-24 11:18   ` [PATCH 2/9] tests: try git apply from subdir of toplevel Jonathan Nieder
2010-07-24 11:19   ` Jonathan Nieder [this message]
2010-07-24 12:15     ` [PATCH 3/9] setup: split off $GIT_DIR-set case from setup_git_directory_gently Nguyen Thai Ngoc Duy
2010-07-24 11:20   ` [PATCH 4/9] setup: split off a function to checks working dir for .git file Jonathan Nieder
2010-07-24 11:56     ` Nguyen Thai Ngoc Duy
2010-07-24 12:11       ` Jonathan Nieder
2010-07-24 11:25   ` [PATCH 5/9] setup: split off code to handle stumbling upon a repository Jonathan Nieder
2010-07-24 11:26   ` [PATCH 6/9] setup: split off a function to handle hitting ceiling in repo search Jonathan Nieder
2010-07-24 11:27   ` [PATCH 7/9] setup: split off get_device_or_die helper Jonathan Nieder
2010-07-24 11:29   ` [PATCH 8/9] setup: do not forget working dir from subdir of gitdir Jonathan Nieder
2010-07-24 11:30   ` [PATCH 9/9] Revert "rehabilitate 'git index-pack' inside the object store" Jonathan Nieder
2010-07-24 11:45   ` [PATCH 0/9] setup_git_directory(): return to original cwd upon reaching .git Nguyen Thai Ngoc Duy

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=20100724111944.GC16054@burratino \
    --to=jrnieder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=pclouds@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).