get_ref_dir(): require that the dirname argument ends in '/'

This removes some conditional code and makes it consistent with the
way that direntry names are stored.  Please note that this function is
never used on the top-level .git directory; it is always called for
directories at level .git/refs or deeper.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Michael Haggerty 2012-04-25 00:45:10 +02:00 committed by Junio C Hamano
parent 66a3d20b8f
commit abc390989f
1 changed files with 7 additions and 7 deletions

14
refs.c
View File

@ -749,13 +749,17 @@ void add_packed_ref(const char *refname, const unsigned char *sha1)
create_ref_entry(refname, sha1, REF_ISPACKED, 1)); create_ref_entry(refname, sha1, REF_ISPACKED, 1));
} }


/*
* Read the loose references for refs from the namespace dirname.
* dirname must end with '/'.
*/
static void get_ref_dir(struct ref_cache *refs, const char *dirname, static void get_ref_dir(struct ref_cache *refs, const char *dirname,
struct ref_dir *dir) struct ref_dir *dir)
{ {
DIR *d; DIR *d;
const char *path; const char *path;
struct dirent *de; struct dirent *de;
int dirnamelen; int dirnamelen = strlen(dirname);
struct strbuf refname; struct strbuf refname;


if (*refs->name) if (*refs->name)
@ -767,13 +771,8 @@ static void get_ref_dir(struct ref_cache *refs, const char *dirname,
if (!d) if (!d)
return; return;


dirnamelen = strlen(dirname);
strbuf_init(&refname, dirnamelen + 257); strbuf_init(&refname, dirnamelen + 257);
strbuf_add(&refname, dirname, dirnamelen); strbuf_add(&refname, dirname, dirnamelen);
if (dirnamelen && dirname[dirnamelen-1] != '/') {
strbuf_addch(&refname, '/');
dirnamelen++;
}


while ((de = readdir(d)) != NULL) { while ((de = readdir(d)) != NULL) {
unsigned char sha1[20]; unsigned char sha1[20];
@ -792,6 +791,7 @@ static void get_ref_dir(struct ref_cache *refs, const char *dirname,
if (stat(refdir, &st) < 0) { if (stat(refdir, &st) < 0) {
; /* silently ignore */ ; /* silently ignore */
} else if (S_ISDIR(st.st_mode)) { } else if (S_ISDIR(st.st_mode)) {
strbuf_addch(&refname, '/');
get_ref_dir(refs, refname.buf, dir); get_ref_dir(refs, refname.buf, dir);
} else { } else {
if (*refs->name) { if (*refs->name) {
@ -816,7 +816,7 @@ static void get_ref_dir(struct ref_cache *refs, const char *dirname,
static struct ref_dir *get_loose_refs(struct ref_cache *refs) static struct ref_dir *get_loose_refs(struct ref_cache *refs)
{ {
if (!refs->did_loose) { if (!refs->did_loose) {
get_ref_dir(refs, "refs", &refs->loose); get_ref_dir(refs, "refs/", &refs->loose);
refs->did_loose = 1; refs->did_loose = 1;
} }
return &refs->loose; return &refs->loose;