Code

setup: clean up setup_discovered_git_dir()
[git.git] / setup.c
diff --git a/setup.c b/setup.c
index 276916052795c5ffa872b57a7e6723edc71dfdc4..3ee9c2eec842900dac6bf9717c1739b111f89893 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -243,9 +243,21 @@ void setup_work_tree(void)
        initialized = 1;
 }
 
-static int check_repository_format_gently(int *nongit_ok)
+static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
 {
-       git_config(check_repository_format_version, NULL);
+       char repo_config[PATH_MAX+1];
+
+       /*
+        * git_config() can't be used here because it calls git_pathdup()
+        * to get $GIT_CONFIG/config. That call will make setup_git_env()
+        * set git_dir to ".git".
+        *
+        * We are in gitdir setup, no git dir has been found useable yet.
+        * Use a gentler version of git_config() to check if this repo
+        * is a good one.
+        */
+       snprintf(repo_config, PATH_MAX, "%s/config", gitdir);
+       git_config_early(check_repository_format_version, NULL, repo_config);
        if (GIT_REPO_VERSION < repository_format_version) {
                if (!nongit_ok)
                        die ("Expected git repo version <= %d, found %d",
@@ -313,21 +325,130 @@ 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 (startup_info)
+               startup_info->setup_explicit = 1;
+       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(gitdirenv, nongit_ok))
+                       return NULL;
+               return retval;
+       }
+       if (check_repository_format_gently(gitdirenv, 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;
+}
+
+static const char *setup_discovered_git_dir(const char *gitdir,
+                                           char *cwd, int offset, int len,
+                                           int *nongit_ok)
+{
+       if (check_repository_format_gently(gitdir, nongit_ok))
+               return NULL;
+
+       /* #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(".");
+       if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT))
+               set_git_dir(gitdir);
+       inside_git_dir = 0;
+       inside_work_tree = 1;
+       if (offset == len)
+               return NULL;
+
+       /* Make "offset" point to past the '/', and add a '/' at the end */
+       offset++;
+       cwd[len++] = '/';
+       cwd[len] = 0;
+       return cwd + offset;
+}
+
+/* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
+static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongit_ok)
+{
+       int root_len;
+
+       if (check_repository_format_gently(".", nongit_ok))
+               return NULL;
+
+       inside_git_dir = 1;
+       inside_work_tree = 0;
+       if (offset != len) {
+               if (chdir(cwd))
+                       die_errno("Cannot come back to cwd");
+               root_len = offset_1st_component(cwd);
+               cwd[offset > root_len ? offset : root_len] = '\0';
+               set_git_dir(cwd);
+       }
+       else
+               set_git_dir(".");
+       return NULL;
+}
+
+static const char *setup_nongit(const char *cwd, int *nongit_ok)
+{
+       if (!nongit_ok)
+               die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT);
+       if (chdir(cwd))
+               die_errno("Cannot come back to cwd");
+       *nongit_ok = 1;
+       return NULL;
+}
+
+static dev_t get_device_or_die(const char *path, const char *prefix)
+{
+       struct stat buf;
+       if (stat(path, &buf))
+               die_errno("failed to stat '%s%s%s'",
+                               prefix ? prefix : "",
+                               prefix ? "/" : "", path);
+       return buf.st_dev;
+}
+
 /*
  * 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.
  */
-const char *setup_git_directory_gently(int *nongit_ok)
+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;
-       int len, offset, ceil_offset, root_len;
+       const char *gitdirenv, *ret;
+       char *gitfile;
+       int len, offset, ceil_offset;
        dev_t current_device = 0;
        int one_filesystem = 1;
-       struct stat buf;
 
        /*
         * Let's assume that we are in a git repository.
@@ -343,38 +464,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");
@@ -396,49 +487,35 @@ const char *setup_git_directory_gently(int *nongit_ok)
         */
        offset = len = strlen(cwd);
        one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
-       if (one_filesystem) {
-               if (stat(".", &buf))
-                       die_errno("failed to stat '.'");
-               current_device = buf.st_dev;
-       }
+       if (one_filesystem)
+               current_device = get_device_or_die(".", NULL);
        for (;;) {
-               gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
-               if (gitfile_dir) {
-                       if (set_git_dir(gitfile_dir))
-                               die("Repository setup failed");
-                       break;
+               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 (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
-                       break;
-               if (is_git_directory(".")) {
-                       inside_git_dir = 1;
-                       if (!work_tree_env)
-                               inside_work_tree = 0;
-                       if (offset != len) {
-                               root_len = offset_1st_component(cwd);
-                               cwd[offset > root_len ? offset : root_len] = '\0';
-                               set_git_dir(cwd);
-                       } else
-                               set_git_dir(".");
-                       check_repository_format_gently(nongit_ok);
-                       return NULL;
+
+               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);
+
                while (--offset > ceil_offset && cwd[offset] != '/');
-               if (offset <= ceil_offset) {
-                       if (nongit_ok) {
-                               if (chdir(cwd))
-                                       die_errno("Cannot come back to cwd");
-                               *nongit_ok = 1;
-                               return NULL;
-                       }
-                       die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT);
-               }
+               if (offset <= ceil_offset)
+                       return setup_nongit(cwd, nongit_ok);
                if (one_filesystem) {
-                       if (stat("..", &buf)) {
-                               cwd[offset] = '\0';
-                               die_errno("failed to stat '%s/..'", cwd);
-                       }
-                       if (buf.st_dev != current_device) {
+                       dev_t parent_device = get_device_or_die("..", cwd);
+                       if (parent_device != current_device) {
                                if (nongit_ok) {
                                        if (chdir(cwd))
                                                die_errno("Cannot come back to cwd");
@@ -455,22 +532,16 @@ const char *setup_git_directory_gently(int *nongit_ok)
                        die_errno("Cannot change to '%s/..'", cwd);
                }
        }
+}
 
-       inside_git_dir = 0;
-       if (!work_tree_env)
-               inside_work_tree = 1;
-       root_len = offset_1st_component(cwd);
-       git_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len);
-       if (check_repository_format_gently(nongit_ok))
-               return NULL;
-       if (offset == len)
-               return NULL;
+const char *setup_git_directory_gently(int *nongit_ok)
+{
+       const char *prefix;
 
-       /* Make "offset" point to past the '/', and add a '/' at the end */
-       offset++;
-       cwd[len++] = '/';
-       cwd[len] = 0;
-       return cwd + offset;
+       prefix = setup_git_directory_gently_1(nongit_ok);
+       if (startup_info)
+               startup_info->have_repository = !nongit_ok || !*nongit_ok;
+       return prefix;
 }
 
 int git_config_perm(const char *var, const char *value)
@@ -546,7 +617,7 @@ int check_repository_format_version(const char *var, const char *value, void *cb
 
 int check_repository_format(void)
 {
-       return check_repository_format_gently(NULL);
+       return check_repository_format_gently(get_git_dir(), NULL);
 }
 
 /*
@@ -560,7 +631,8 @@ 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) {
+       if ((!startup_info || startup_info->setup_explicit) &&
+           inside_work_tree < 0) {
                static char buffer[PATH_MAX + 1];
                char *rel;
                if (retval && chdir(retval))