Code

setup: split off $GIT_DIR-set case from setup_git_directory_gently
authorJonathan Nieder <jrnieder@gmail.com>
Sat, 24 Jul 2010 11:19:44 +0000 (06:19 -0500)
committerJunio C Hamano <gitster@pobox.com>
Mon, 26 Jul 2010 05:08:13 +0000 (22:08 -0700)
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>
setup.c

diff --git a/setup.c b/setup.c
index 276916052795c5ffa872b57a7e6723edc71dfdc4..16bee6d485349602db8aa5de28b997e7ef82a731 100644 (file)
--- 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");