do_for_each_reflog(): return early on error

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Michael Haggerty 2012-04-25 00:45:13 +02:00 committed by Junio C Hamano
parent 9f2fb4a373
commit 93c603fcb7

70
refs.c
View File

@ -2246,47 +2246,47 @@ static int do_for_each_reflog(const char *base, each_ref_fn fn, void *cb_data)
{ {
DIR *d = opendir(git_path("logs/%s", base)); DIR *d = opendir(git_path("logs/%s", base));
int retval = 0; int retval = 0;
struct dirent *de;
int baselen;
char *log;
if (d) { if (!d)
struct dirent *de; return *base ? errno : 0;
int baselen = strlen(base);
char *log = xmalloc(baselen + 257);
memcpy(log, base, baselen); baselen = strlen(base);
if (baselen && base[baselen-1] != '/') log = xmalloc(baselen + 257);
log[baselen++] = '/'; memcpy(log, base, baselen);
if (baselen && base[baselen-1] != '/')
log[baselen++] = '/';
while ((de = readdir(d)) != NULL) { while ((de = readdir(d)) != NULL) {
struct stat st; struct stat st;
int namelen; int namelen;
if (de->d_name[0] == '.') if (de->d_name[0] == '.')
continue; continue;
namelen = strlen(de->d_name); namelen = strlen(de->d_name);
if (namelen > 255) if (namelen > 255)
continue; continue;
if (has_extension(de->d_name, ".lock")) if (has_extension(de->d_name, ".lock"))
continue; continue;
memcpy(log + baselen, de->d_name, namelen+1); memcpy(log + baselen, de->d_name, namelen+1);
if (stat(git_path("logs/%s", log), &st) < 0) if (stat(git_path("logs/%s", log), &st) < 0)
continue; continue;
if (S_ISDIR(st.st_mode)) { if (S_ISDIR(st.st_mode)) {
retval = do_for_each_reflog(log, fn, cb_data); retval = do_for_each_reflog(log, fn, cb_data);
} else { } else {
unsigned char sha1[20]; unsigned char sha1[20];
if (read_ref_full(log, sha1, 0, NULL)) if (read_ref_full(log, sha1, 0, NULL))
retval = error("bad ref for %s", log); retval = error("bad ref for %s", log);
else else
retval = fn(log, sha1, 0, cb_data); retval = fn(log, sha1, 0, cb_data);
}
if (retval)
break;
} }
free(log); if (retval)
closedir(d); break;
} }
else if (*base) free(log);
return errno; closedir(d);
return retval; return retval;
} }