do_for_each_reflog(): use a strbuf to hold logfile name
This simplifies the bookkeeping and allows an (artificial) restriction on refname component length to be removed. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
93c603fcb7
commit
989c0e5d02
60
refs.c
60
refs.c
@ -2242,57 +2242,59 @@ int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_dat
|
|||||||
return for_each_recent_reflog_ent(refname, fn, 0, cb_data);
|
return for_each_recent_reflog_ent(refname, fn, 0, cb_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int do_for_each_reflog(const char *base, each_ref_fn fn, void *cb_data)
|
/*
|
||||||
|
* Call fn for each reflog in the namespace indicated by name. name
|
||||||
|
* must be empty or end with '/'. Name will be used as a scratch
|
||||||
|
* space, but its contents will be restored before return.
|
||||||
|
*/
|
||||||
|
static int do_for_each_reflog(struct strbuf *name, each_ref_fn fn, void *cb_data)
|
||||||
{
|
{
|
||||||
DIR *d = opendir(git_path("logs/%s", base));
|
DIR *d = opendir(git_path("logs/%s", name->buf));
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
struct dirent *de;
|
struct dirent *de;
|
||||||
int baselen;
|
int oldlen = name->len;
|
||||||
char *log;
|
|
||||||
|
|
||||||
if (!d)
|
if (!d)
|
||||||
return *base ? errno : 0;
|
return name->len ? errno : 0;
|
||||||
|
|
||||||
baselen = strlen(base);
|
|
||||||
log = xmalloc(baselen + 257);
|
|
||||||
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;
|
|
||||||
|
|
||||||
if (de->d_name[0] == '.')
|
if (de->d_name[0] == '.')
|
||||||
continue;
|
continue;
|
||||||
namelen = strlen(de->d_name);
|
|
||||||
if (namelen > 255)
|
|
||||||
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);
|
strbuf_addstr(name, de->d_name);
|
||||||
if (stat(git_path("logs/%s", log), &st) < 0)
|
if (stat(git_path("logs/%s", name->buf), &st) < 0) {
|
||||||
continue;
|
; /* silently ignore */
|
||||||
if (S_ISDIR(st.st_mode)) {
|
|
||||||
retval = do_for_each_reflog(log, fn, cb_data);
|
|
||||||
} else {
|
} else {
|
||||||
unsigned char sha1[20];
|
if (S_ISDIR(st.st_mode)) {
|
||||||
if (read_ref_full(log, sha1, 0, NULL))
|
strbuf_addch(name, '/');
|
||||||
retval = error("bad ref for %s", log);
|
retval = do_for_each_reflog(name, fn, cb_data);
|
||||||
else
|
} else {
|
||||||
retval = fn(log, sha1, 0, cb_data);
|
unsigned char sha1[20];
|
||||||
|
if (read_ref_full(name->buf, sha1, 0, NULL))
|
||||||
|
retval = error("bad ref for %s", name->buf);
|
||||||
|
else
|
||||||
|
retval = fn(name->buf, sha1, 0, cb_data);
|
||||||
|
}
|
||||||
|
if (retval)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if (retval)
|
strbuf_setlen(name, oldlen);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
free(log);
|
|
||||||
closedir(d);
|
closedir(d);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
int for_each_reflog(each_ref_fn fn, void *cb_data)
|
int for_each_reflog(each_ref_fn fn, void *cb_data)
|
||||||
{
|
{
|
||||||
return do_for_each_reflog("", fn, cb_data);
|
int retval;
|
||||||
|
struct strbuf name;
|
||||||
|
strbuf_init(&name, PATH_MAX);
|
||||||
|
retval = do_for_each_reflog(&name, fn, cb_data);
|
||||||
|
strbuf_release(&name);
|
||||||
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
int update_ref(const char *action, const char *refname,
|
int update_ref(const char *action, const char *refname,
|
||||||
|
Loading…
Reference in New Issue
Block a user