git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 2/2] setup: fix NEEDSWORK in setup_git_directory_gently()

From
Lidong Yan <yldhome2d2@gmail.com>
Date
Jun 15, 2025, 14:46 UTC
Message-ID
<20250615144604.1447302-3-502024330056@smail.nju.edu.cn>
In-Reply-To
<20250615144604.1447302-1-502024330056@smail.nju.edu.cn>

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@smail.nju.edu.cn>
---
 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
Previous: Lidong YanNext: Junio C Hamano
Message 13 of 16 in “git.c: remove the_repository dependence in run_builtin()”
  1. git.c: remove the_repository dependence in run_builtin()Lidong Yan, Jun 12, 2025
  2. Junio C HamanoJun 12, 2025
  3. lidongyanJun 13, 2025
  4. Junio C HamanoJun 13, 2025
  5. git.c: remove the_repository dependence in run_builtin()Lidong Yan, Jun 14, 2025
  6. Junio C HamanoJun 14, 2025
  7. Lidong YanJun 15, 2025
  8. Junio C HamanoJun 16, 2025
  9. Lidong YanJun 16, 2025
  10. Junio C HamanoJun 16, 2025
  11. 0/2 small fixes for git.c and setup.cLidong Yan, Jun 15, 2025
  12. 1/2 git.c: remove the_repository dependence in run_builtin()Lidong Yan, Jun 15, 2025
  13. 2/2 setup: fix NEEDSWORK in setup_git_directory_gently()Lidong Yan, Jun 15, 2025
  14. Junio C HamanoJun 16, 2025
  15. Lidong YanJun 16, 2025
  16. git.c: remove the_repository dependence in run_builtin()Lidong Yan, Jun 16, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.