name-rev: refactor logic to see if a new candidate is a better name
[gitweb.git] / setup.c
diff --git a/setup.c b/setup.c
index d08730d94d488af6e4a631dac88cc222807878d6..5c7946d2b45f067049e11347cca1cedc9790e228 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -135,6 +135,7 @@ int path_inside_repo(const char *prefix, const char *path)
 int check_filename(const char *prefix, const char *arg)
 {
        const char *name;
+       char *to_free = NULL;
        struct stat st;
 
        if (starts_with(arg, ":/")) {
@@ -142,13 +143,17 @@ int check_filename(const char *prefix, const char *arg)
                        return 1;
                name = arg + 2;
        } else if (prefix)
-               name = prefix_filename(prefix, strlen(prefix), arg);
+               name = to_free = prefix_filename(prefix, arg);
        else
                name = arg;
-       if (!lstat(name, &st))
+       if (!lstat(name, &st)) {
+               free(to_free);
                return 1; /* file exists */
-       if (errno == ENOENT || errno == ENOTDIR)
+       }
+       if (errno == ENOENT || errno == ENOTDIR) {
+               free(to_free);
                return 0; /* file does not exist */
+       }
        die_errno("failed to stat '%s'", arg);
 }
 
@@ -254,7 +259,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 +536,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 +704,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 +814,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 +831,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 +849,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 +898,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);
@@ -934,7 +950,7 @@ const char *discover_git_directory(struct strbuf *gitdir)
                return NULL;
 
        cwd_len = dir.len;
-       if (setup_git_directory_gently_1(&dir, gitdir) <= 0) {
+       if (setup_git_directory_gently_1(&dir, gitdir, 0) <= 0) {
                strbuf_release(&dir);
                return NULL;
        }
@@ -994,7 +1010,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;