ref-cache: rename `add_ref()` to `add_ref_entry()`
[gitweb.git] / setup.c
diff --git a/setup.c b/setup.c
index 27a31de33fd84fd4b6d3a3adbd071e573c759f9f..64f922a9378cc48c72b9173e7c45296bdb885678 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -254,7 +254,7 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)
                if (!is_absolute_path(data.buf))
                        strbuf_addf(&path, "%s/", gitdir);
                strbuf_addbuf(&path, &data);
-               strbuf_addstr(sb, real_path(path.buf));
+               strbuf_add_real_path(sb, path.buf);
                ret = 1;
        } else {
                strbuf_addstr(sb, gitdir);
@@ -531,6 +531,7 @@ const char *read_gitfile_gently(const char *path, int *return_error_code)
        ssize_t len;
 
        if (stat(path, &st)) {
+               /* NEEDSWORK: discern between ENOENT vs other errors */
                error_code = READ_GITFILE_ERR_STAT_FAILED;
                goto cleanup_return;
        }
@@ -698,7 +699,7 @@ static const char *setup_discovered_git_dir(const char *gitdir,
        /* --work-tree is set without --git-dir; use discovered one */
        if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
                if (offset != cwd->len && !is_absolute_path(gitdir))
-                       gitdir = real_pathdup(gitdir);
+                       gitdir = real_pathdup(gitdir, 1);
                if (chdir(cwd->buf))
                        die_errno("Could not come back to cwd");
                return setup_explicit_git_dir(gitdir, cwd, nongit_ok);
@@ -808,7 +809,7 @@ static int canonicalize_ceiling_entry(struct string_list_item *item,
                /* Keep entry but do not canonicalize it */
                return 1;
        } else {
-               char *real_path = real_pathdup(ceil);
+               char *real_path = real_pathdup(ceil, 0);
                if (!real_path) {
                        return 0;
                }
@@ -825,7 +826,8 @@ enum discovery_result {
        GIT_DIR_BARE,
        /* these are errors */
        GIT_DIR_HIT_CEILING = -1,
-       GIT_DIR_HIT_MOUNT_POINT = -2
+       GIT_DIR_HIT_MOUNT_POINT = -2,
+       GIT_DIR_INVALID_GITFILE = -3
 };
 
 /*
@@ -842,7 +844,8 @@ enum discovery_result {
  * is relative to `dir` (i.e. *not* necessarily the cwd).
  */
 static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
-                                                         struct strbuf *gitdir)
+                                                         struct strbuf *gitdir,
+                                                         int die_on_error)
 {
        const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
        struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
@@ -890,14 +893,22 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
        if (one_filesystem)
                current_device = get_device_or_die(dir->buf, NULL, 0);
        for (;;) {
-               int offset = dir->len;
+               int offset = dir->len, error_code = 0;
 
                if (offset > min_offset)
                        strbuf_addch(dir, '/');
                strbuf_addstr(dir, DEFAULT_GIT_DIR_ENVIRONMENT);
-               gitdirenv = read_gitfile(dir->buf);
-               if (!gitdirenv && is_git_directory(dir->buf))
-                       gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT;
+               gitdirenv = read_gitfile_gently(dir->buf, die_on_error ?
+                                               NULL : &error_code);
+               if (!gitdirenv) {
+                       if (die_on_error ||
+                           error_code == READ_GITFILE_ERR_NOT_A_FILE) {
+                               /* NEEDSWORK: fail if .git is not file nor dir */
+                               if (is_git_directory(dir->buf))
+                                       gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT;
+                       } else if (error_code != READ_GITFILE_ERR_STAT_FAILED)
+                               return GIT_DIR_INVALID_GITFILE;
+               }
                strbuf_setlen(dir, offset);
                if (gitdirenv) {
                        strbuf_addstr(gitdir, gitdirenv);
@@ -924,6 +935,49 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
        }
 }
 
+const char *discover_git_directory(struct strbuf *gitdir)
+{
+       struct strbuf dir = STRBUF_INIT, err = STRBUF_INIT;
+       size_t gitdir_offset = gitdir->len, cwd_len;
+       struct repository_format candidate;
+
+       if (strbuf_getcwd(&dir))
+               return NULL;
+
+       cwd_len = dir.len;
+       if (setup_git_directory_gently_1(&dir, gitdir, 0) <= 0) {
+               strbuf_release(&dir);
+               return NULL;
+       }
+
+       /*
+        * The returned gitdir is relative to dir, and if dir does not reflect
+        * the current working directory, we simply make the gitdir absolute.
+        */
+       if (dir.len < cwd_len && !is_absolute_path(gitdir->buf + gitdir_offset)) {
+               /* Avoid a trailing "/." */
+               if (!strcmp(".", gitdir->buf + gitdir_offset))
+                       strbuf_setlen(gitdir, gitdir_offset);
+               else
+                       strbuf_addch(&dir, '/');
+               strbuf_insert(gitdir, gitdir_offset, dir.buf, dir.len);
+       }
+
+       strbuf_reset(&dir);
+       strbuf_addf(&dir, "%s/config", gitdir->buf + gitdir_offset);
+       read_repository_format(&candidate, dir.buf);
+       strbuf_release(&dir);
+
+       if (verify_repository_format(&candidate, &err) < 0) {
+               warning("ignoring git dir '%s': %s",
+                       gitdir->buf + gitdir_offset, err.buf);
+               strbuf_release(&err);
+               return NULL;
+       }
+
+       return gitdir->buf + gitdir_offset;
+}
+
 const char *setup_git_directory_gently(int *nongit_ok)
 {
        static struct strbuf cwd = STRBUF_INIT;
@@ -951,7 +1005,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
                die_errno(_("Unable to read current working directory"));
        strbuf_addbuf(&dir, &cwd);
 
-       switch (setup_git_directory_gently_1(&dir, &gitdir)) {
+       switch (setup_git_directory_gently_1(&dir, &gitdir, 1)) {
        case GIT_DIR_NONE:
                prefix = NULL;
                break;