get_ref_dir(): use a strbuf to hold refname

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:
Michael Haggerty 2012-04-25 00:45:08 +02:00 коммит произвёл Junio C Hamano
Родитель d5fdae6737
Коммит 72b64b44e7
1 изменённых файлов: 26 добавлений и 28 удалений

54
refs.c
Просмотреть файл

@ -756,7 +756,7 @@ static void get_ref_dir(struct ref_cache *refs, const char *base,
const char *path; const char *path;
struct dirent *de; struct dirent *de;
int baselen; int baselen;
char *refname; struct strbuf refname;
if (*refs->name) if (*refs->name)
path = git_path_submodule(refs->name, "%s", base); path = git_path_submodule(refs->name, "%s", base);
@ -768,50 +768,48 @@ static void get_ref_dir(struct ref_cache *refs, const char *base,
return; return;
baselen = strlen(base); baselen = strlen(base);
refname = xmalloc(baselen + 257); strbuf_init(&refname, baselen + 257);
strbuf_add(&refname, base, baselen);
memcpy(refname, base, baselen); if (baselen && base[baselen-1] != '/') {
if (baselen && base[baselen-1] != '/') strbuf_addch(&refname, '/');
refname[baselen++] = '/'; baselen++;
}
while ((de = readdir(d)) != NULL) { while ((de = readdir(d)) != NULL) {
unsigned char sha1[20]; unsigned char sha1[20];
struct stat st; struct stat st;
int flag; int flag;
int namelen;
const char *refdir; const char *refdir;
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(refname + baselen, de->d_name, namelen+1); strbuf_addstr(&refname, de->d_name);
refdir = *refs->name refdir = *refs->name
? git_path_submodule(refs->name, "%s", refname) ? git_path_submodule(refs->name, "%s", refname.buf)
: git_path("%s", refname); : git_path("%s", refname.buf);
if (stat(refdir, &st) < 0) if (stat(refdir, &st) < 0) {
continue; ; /* silently ignore */
if (S_ISDIR(st.st_mode)) { } else if (S_ISDIR(st.st_mode)) {
get_ref_dir(refs, refname, dir); get_ref_dir(refs, refname.buf, dir);
continue; } else {
} if (*refs->name) {
if (*refs->name) { hashclr(sha1);
hashclr(sha1); flag = 0;
flag = 0; if (resolve_gitlink_ref(refs->name, refname.buf, sha1) < 0) {
if (resolve_gitlink_ref(refs->name, refname, sha1) < 0) { hashclr(sha1);
flag |= REF_ISBROKEN;
}
} else if (read_ref_full(refname.buf, sha1, 1, &flag)) {
hashclr(sha1); hashclr(sha1);
flag |= REF_ISBROKEN; flag |= REF_ISBROKEN;
} }
} else if (read_ref_full(refname, sha1, 1, &flag)) { add_ref(dir, create_ref_entry(refname.buf, sha1, flag, 1));
hashclr(sha1);
flag |= REF_ISBROKEN;
} }
add_ref(dir, create_ref_entry(refname, sha1, flag, 1)); strbuf_setlen(&refname, baselen);
} }
free(refname); strbuf_release(&refname);
closedir(d); closedir(d);
} }