Skip to content

Commit 01017dc

Browse files
dschogitster
authored andcommitted
setup_git_directory_gently_1(): avoid die()ing
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1a6ec1e commit 01017dc

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

setup.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,8 @@ enum discovery_result {
825825
GIT_DIR_BARE,
826826
/* these are errors */
827827
GIT_DIR_HIT_CEILING = -1,
828-
GIT_DIR_HIT_MOUNT_POINT = -2
828+
GIT_DIR_HIT_MOUNT_POINT = -2,
829+
GIT_DIR_INVALID_GITFILE = -3
829830
};
830831

831832
/*
@@ -842,7 +843,8 @@ enum discovery_result {
842843
* is relative to `dir` (i.e. *not* necessarily the cwd).
843844
*/
844845
static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
845-
struct strbuf *gitdir)
846+
struct strbuf *gitdir,
847+
int die_on_error)
846848
{
847849
const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
848850
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,
890892
if (one_filesystem)
891893
current_device = get_device_or_die(dir->buf, NULL, 0);
892894
for (;;) {
893-
int offset = dir->len;
895+
int offset = dir->len, error_code = 0;
894896

895897
if (offset > min_offset)
896898
strbuf_addch(dir, '/');
897899
strbuf_addstr(dir, DEFAULT_GIT_DIR_ENVIRONMENT);
898-
gitdirenv = read_gitfile(dir->buf);
899-
if (!gitdirenv && is_git_directory(dir->buf))
900-
gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT;
900+
gitdirenv = read_gitfile_gently(dir->buf, die_on_error ?
901+
NULL : &error_code);
902+
if (!gitdirenv) {
903+
if (die_on_error ||
904+
error_code == READ_GITFILE_ERR_NOT_A_FILE) {
905+
if (is_git_directory(dir->buf))
906+
gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT;
907+
} else if (error_code != READ_GITFILE_ERR_STAT_FAILED)
908+
return GIT_DIR_INVALID_GITFILE;
909+
}
901910
strbuf_setlen(dir, offset);
902911
if (gitdirenv) {
903912
strbuf_addstr(gitdir, gitdirenv);
@@ -934,7 +943,7 @@ const char *discover_git_directory(struct strbuf *gitdir)
934943
return NULL;
935944

936945
cwd_len = dir.len;
937-
if (setup_git_directory_gently_1(&dir, gitdir) <= 0) {
946+
if (setup_git_directory_gently_1(&dir, gitdir, 0) <= 0) {
938947
strbuf_release(&dir);
939948
return NULL;
940949
}
@@ -994,7 +1003,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
9941003
die_errno(_("Unable to read current working directory"));
9951004
strbuf_addbuf(&dir, &cwd);
9961005

997-
switch (setup_git_directory_gently_1(&dir, &gitdir)) {
1006+
switch (setup_git_directory_gently_1(&dir, &gitdir, 1)) {
9981007
case GIT_DIR_NONE:
9991008
prefix = NULL;
10001009
break;

0 commit comments

Comments
 (0)