]> git.ipfire.org Git - thirdparty/git.git/commitdiff
setup_git_directory_gently_1(): avoid die()ing
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Mon, 13 Mar 2017 20:11:22 +0000 (21:11 +0100)
committerJunio C Hamano <gitster@pobox.com>
Tue, 14 Mar 2017 21:24:16 +0000 (14:24 -0700)
This function now has a new caller in addition to setup_git_directory():
the newly introduced discover_git_directory(). That function wants to
discover the current .git/ directory, and in case of a corrupted one
simply pretend that there is none to be found.

Example: if a stale .git file exists in the parent directory, and the
user calls `git -p init`, we want Git to simply *not* read any
repository config for the pager (instead of aborting with a message that
the .git file is corrupt).

Let's actually pretend that there was no GIT_DIR to be found in that case
when being called from discover_git_directory(), but keep the previous
behavior (i.e. to die()) for the setup_git_directory() case.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
setup.c

diff --git a/setup.c b/setup.c
index d08730d94d488af6e4a631dac88cc222807878d6..f26094e9ade1f4c8871f80cdc458f40b1448e68e 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -825,7 +825,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 +843,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 +892,21 @@ 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) {
+                               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 +943,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 +1003,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;