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

[PATCH] for-each-reflog: fix case for empty log directory

From
Junio C Hamano <junkio@cox.net>
Date
Feb 7, 2007, 17:21 UTC
Message-ID
<7v4ppx993f.fsf@assigned-by-dhcp.cox.net>

When we remove the last reflog in a directory, opendir() would succeed and we would iterate over its dirents, expecting retval to be initialized to zero and setting it to non-zero only upon seeing an error. If the directory is empty, oops!, we do not have anybody that touches retval.

The problem is because we initialize retval to errno even on success from opendir(), which would leave the errno unmolested.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---
 * This bug made "git fsck --full" in fully gc'ed repository to
   report dangling commits because it did not fully walk the
   reflogs.
 refs.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
diff --git a/refs.c b/refs.c
index 03e8dfe..7e07fc4 100644
--- a/refs.c
+++ b/refs.c
@@ -1206,7 +1206,7 @@ int for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data)
 static int do_for_each_reflog(const char *base, each_ref_fn fn, void *cb_data)
 {
 	DIR *dir = opendir(git_path("logs/%s", base));
-	int retval = errno;
+	int retval = 0;
 
 	if (dir) {
 		struct dirent *de;
@@ -1246,6 +1246,8 @@ static int do_for_each_reflog(const char *base, each_ref_fn fn, void *cb_data)
 		free(log);
 		closedir(dir);
 	}
+	else
+		return errno;
 	return retval;
 }
 
-- 
1.5.0.rc3.185.g302d
Message 1 of 1 in “for-each-reflog: fix case for empty log directory”
  1. for-each-reflog: fix case for empty log directoryJunio C Hamano, Feb 7, 2007

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.