Code

for-each-reflog: fix case for empty log directory
authorJunio C Hamano <junkio@cox.net>
Wed, 7 Feb 2007 17:18:57 +0000 (09:18 -0800)
committerJunio C Hamano <junkio@cox.net>
Wed, 7 Feb 2007 17:18:57 +0000 (09:18 -0800)
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>
refs.c

diff --git a/refs.c b/refs.c
index 03e8dfec9fcb2c9fffc7c809048fa90a0e02b0af..7e07fc4cb5b6bc32f5c75211aff636421f9e14d5 100644 (file)
--- 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;
 }