In setup.c:setup_git_directory_gently(), the case that fail when dir->buf is neither a file nor a directory is currently marked as NEEDSWORK. Add two new READ_GITFILE_ERR types to handle this case more explicitly: * READ_GITFILE_ERR_NOT_A_FILE_OR_DIR: path exist, but path is neither a file nor directory * READ_GITFILE_ERR_IS_DIR: path exist, and it is a directory. If dir->buf is neither a file nor a directory, then depending on die_or_error, either read_gitfile_gently() will die, or this function will return GIT_DIR_INVALID_GIT_FILE. To make old use of READ_GITFILE_ERR_NOT_A_FILE still works, Add READ_GITFILE_ERR_NOT_A_FILE(err) macro, which has the same effect as `err == READ_GITFILE_ERR_NOT_A_FILE` in the origin code. Also add die message for READ_GITFILE_ERR_NOT_A_FILE_OR_DIR in read_gitfile_error_die(). Signed-off-by: Lidong Yan <502024330056@xxxxxxxxxxxxxxxx> --- setup.c | 11 +++++++---- setup.h | 18 +++++++++++------- worktree.c | 4 ++-- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/setup.c b/setup.c index f93bd6a24a..b18c43d93b 100644 --- a/setup.c +++ b/setup.c @@ -893,9 +893,11 @@ void read_gitfile_error_die(int error_code, const char *path, const char *dir) { switch (error_code) { case READ_GITFILE_ERR_STAT_FAILED: - case READ_GITFILE_ERR_NOT_A_FILE: + case READ_GITFILE_ERR_IS_DIR: /* non-fatal; follow return path */ break; + case READ_GITFILE_ERR_NOT_A_FILE_OR_DIR: + die(_("'%s' is not a file or directory"), path); case READ_GITFILE_ERR_OPEN_FAILED: die_errno(_("error opening '%s'"), path); case READ_GITFILE_ERR_TOO_LARGE: @@ -941,7 +943,9 @@ const char *read_gitfile_gently(const char *path, int *return_error_code) goto cleanup_return; } if (!S_ISREG(st.st_mode)) { - error_code = READ_GITFILE_ERR_NOT_A_FILE; + error_code = S_ISDIR(st.st_mode) ? + READ_GITFILE_ERR_IS_DIR : + READ_GITFILE_ERR_NOT_A_FILE_OR_DIR; goto cleanup_return; } if (st.st_size > max_file_size) { @@ -1499,8 +1503,7 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir, 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 */ + error_code == READ_GITFILE_ERR_IS_DIR) { if (is_git_directory(dir->buf)) { gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT; gitdir_path = xstrdup(dir->buf); diff --git a/setup.h b/setup.h index 18dc3b7368..7f246ad248 100644 --- a/setup.h +++ b/setup.h @@ -29,13 +29,17 @@ int is_git_directory(const char *path); int is_nonbare_repository_dir(struct strbuf *path); #define READ_GITFILE_ERR_STAT_FAILED 1 -#define READ_GITFILE_ERR_NOT_A_FILE 2 -#define READ_GITFILE_ERR_OPEN_FAILED 3 -#define READ_GITFILE_ERR_READ_FAILED 4 -#define READ_GITFILE_ERR_INVALID_FORMAT 5 -#define READ_GITFILE_ERR_NO_PATH 6 -#define READ_GITFILE_ERR_NOT_A_REPO 7 -#define READ_GITFILE_ERR_TOO_LARGE 8 +#define READ_GITFILE_ERR_IS_DIR 2 +#define READ_GITFILE_ERR_NOT_A_FILE_OR_DIR 3 +#define READ_GITFILE_ERR_OPEN_FAILED 4 +#define READ_GITFILE_ERR_READ_FAILED 5 +#define READ_GITFILE_ERR_INVALID_FORMAT 6 +#define READ_GITFILE_ERR_NO_PATH 7 +#define READ_GITFILE_ERR_NOT_A_REPO 8 +#define READ_GITFILE_ERR_TOO_LARGE 9 +#define READ_GITFILE_ERR_NOT_A_FILE(x) \ + ((x) == READ_GITFILE_ERR_NOT_A_FILE_OR_DIR || \ + (x) == READ_GITFILE_ERR_IS_DIR) void read_gitfile_error_die(int error_code, const char *path, const char *dir); const char *read_gitfile_gently(const char *path, int *return_error_code); #define read_gitfile(path) read_gitfile_gently((path), NULL) diff --git a/worktree.c b/worktree.c index c34b9eb74e..3f0e9748ec 100644 --- a/worktree.c +++ b/worktree.c @@ -646,7 +646,7 @@ static void repair_gitfile(struct worktree *wt, } } - if (err == READ_GITFILE_ERR_NOT_A_FILE) + if (READ_GITFILE_ERR_NOT_A_FILE(err)) fn(1, wt->path, _(".git is not a file"), cb_data); else if (err) repair = _(".git file broken"); @@ -826,7 +826,7 @@ void repair_worktree_at_path(const char *path, strbuf_addstr(&backlink, dotgit_contents); strbuf_realpath_forgiving(&backlink, backlink.buf, 0); } - } else if (err == READ_GITFILE_ERR_NOT_A_FILE) { + } else if (READ_GITFILE_ERR_NOT_A_FILE(err)) { fn(1, dotgit.buf, _("unable to locate repository; .git is not a file"), cb_data); goto done; } else if (err == READ_GITFILE_ERR_NOT_A_REPO) { -- 2.50.0-rc1