@@ -920,10 +920,14 @@ int verify_repository_format(const struct repository_format *format,
920920void read_gitfile_error_die (int error_code , const char * path , const char * dir )
921921{
922922 switch (error_code ) {
923- case READ_GITFILE_ERR_STAT_FAILED :
924- case READ_GITFILE_ERR_NOT_A_FILE :
923+ case READ_GITFILE_ERR_MISSING :
924+ case READ_GITFILE_ERR_IS_A_DIR :
925925 /* non-fatal; follow return path */
926926 break ;
927+ case READ_GITFILE_ERR_STAT_FAILED :
928+ die (_ ("error reading '%s'" ), path );
929+ case READ_GITFILE_ERR_NOT_A_FILE :
930+ die (_ ("not a regular file: '%s'" ), path );
927931 case READ_GITFILE_ERR_OPEN_FAILED :
928932 die_errno (_ ("error opening '%s'" ), path );
929933 case READ_GITFILE_ERR_TOO_LARGE :
@@ -964,8 +968,14 @@ const char *read_gitfile_gently(const char *path, int *return_error_code)
964968 static struct strbuf realpath = STRBUF_INIT ;
965969
966970 if (stat (path , & st )) {
967- /* NEEDSWORK: discern between ENOENT vs other errors */
968- error_code = READ_GITFILE_ERR_STAT_FAILED ;
971+ if (errno == ENOENT || errno == ENOTDIR )
972+ error_code = READ_GITFILE_ERR_MISSING ;
973+ else
974+ error_code = READ_GITFILE_ERR_STAT_FAILED ;
975+ goto cleanup_return ;
976+ }
977+ if (S_ISDIR (st .st_mode )) {
978+ error_code = READ_GITFILE_ERR_IS_A_DIR ;
969979 goto cleanup_return ;
970980 }
971981 if (!S_ISREG (st .st_mode )) {
@@ -1601,20 +1611,31 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
16011611 if (offset > min_offset )
16021612 strbuf_addch (dir , '/' );
16031613 strbuf_addstr (dir , DEFAULT_GIT_DIR_ENVIRONMENT );
1604- gitdirenv = read_gitfile_gently (dir -> buf , die_on_error ?
1605- NULL : & error_code );
1614+ gitdirenv = read_gitfile_gently (dir -> buf , & error_code );
16061615 if (!gitdirenv ) {
1607- if (die_on_error ||
1608- error_code == READ_GITFILE_ERR_NOT_A_FILE ) {
1609- /* NEEDSWORK: fail if .git is not file nor dir */
1616+ switch (error_code ) {
1617+ case READ_GITFILE_ERR_MISSING :
1618+ /* no .git in this directory, move on */
1619+ break ;
1620+ case READ_GITFILE_ERR_IS_A_DIR :
16101621 if (is_git_directory (dir -> buf )) {
16111622 gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT ;
16121623 gitdir_path = xstrdup (dir -> buf );
16131624 }
1614- } else if (error_code != READ_GITFILE_ERR_STAT_FAILED )
1615- return GIT_DIR_INVALID_GITFILE ;
1616- } else
1625+ /*
1626+ * NEEDSWORK: should we catch a directory .git
1627+ * that is not a git directory here?
1628+ */
1629+ break ;
1630+ default :
1631+ if (die_on_error || error_code == READ_GITFILE_ERR_NOT_A_FILE )
1632+ read_gitfile_error_die (error_code , dir -> buf , NULL );
1633+ else
1634+ return GIT_DIR_INVALID_GITFILE ;
1635+ }
1636+ } else {
16171637 gitfile = xstrdup (dir -> buf );
1638+ }
16181639 /*
16191640 * Earlier, we tentatively added DEFAULT_GIT_DIR_ENVIRONMENT
16201641 * to check that directory for a repository.
0 commit comments