summaryrefslogtreecommitdiff
path: root/setup.c
diff options
context:
space:
mode:
authorJonathan Nieder <jrnieder@gmail.com>2010-07-24 06:19:44 -0500
committerJunio C Hamano <gitster@pobox.com>2010-07-25 22:08:13 -0700
commite4e303479b62e474c52ee1f9edd265d7333117d9 (patch)
tree24deeb75d501804172cf3f758d005f2a8ac6e429 /setup.c
parent9f41a91a7d100055fbd527f35b8cddae0bd91cac (diff)
downloadgit-e4e303479b62e474c52ee1f9edd265d7333117d9.tar.gz
setup: split off $GIT_DIR-set case from setup_git_directory_gently
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'setup.c')
-rw-r--r--setup.c69
1 files changed, 37 insertions, 32 deletions
diff --git a/setup.c b/setup.c
index 2769160527..16bee6d485 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");