refs: split off reading loose ref data in separate function

This prepares for handling FETCH_HEAD (which is not a regular ref)
separately from the ref backend.

Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Han-Wen Nienhuys 2020-08-19 14:27:55 +00:00 committed by Junio C Hamano
parent 878e727637
commit e39620f07e
2 changed files with 33 additions and 23 deletions

View File

@ -360,7 +360,6 @@ static int files_read_raw_ref(struct ref_store *ref_store,
struct strbuf sb_path = STRBUF_INIT; struct strbuf sb_path = STRBUF_INIT;
const char *path; const char *path;
const char *buf; const char *buf;
const char *p;
struct stat st; struct stat st;
int fd; int fd;
int ret = -1; int ret = -1;
@ -465,29 +464,8 @@ stat_ref:
close(fd); close(fd);
strbuf_rtrim(&sb_contents); strbuf_rtrim(&sb_contents);
buf = sb_contents.buf; buf = sb_contents.buf;
if (skip_prefix(buf, "ref:", &buf)) {
while (isspace(*buf))
buf++;
strbuf_reset(referent); ret = parse_loose_ref_contents(buf, oid, referent, type);
strbuf_addstr(referent, buf);
*type |= REF_ISSYMREF;
ret = 0;
goto out;
}
/*
* Please note that FETCH_HEAD has additional
* data after the sha.
*/
if (parse_oid_hex(buf, oid, &p) ||
(*p != '\0' && !isspace(*p))) {
*type |= REF_ISBROKEN;
errno = EINVAL;
goto out;
}
ret = 0;
out: out:
save_errno = errno; save_errno = errno;
@ -497,6 +475,32 @@ out:
return ret; return ret;
} }
int parse_loose_ref_contents(const char *buf, struct object_id *oid,
struct strbuf *referent, unsigned int *type)
{
const char *p;
if (skip_prefix(buf, "ref:", &buf)) {
while (isspace(*buf))
buf++;
strbuf_reset(referent);
strbuf_addstr(referent, buf);
*type |= REF_ISSYMREF;
return 0;
}
/*
* FETCH_HEAD has additional data after the sha.
*/
if (parse_oid_hex(buf, oid, &p) ||
(*p != '\0' && !isspace(*p))) {
*type |= REF_ISBROKEN;
errno = EINVAL;
return -1;
}
return 0;
}
static void unlock_ref(struct ref_lock *lock) static void unlock_ref(struct ref_lock *lock)
{ {
rollback_lock_file(&lock->lk); rollback_lock_file(&lock->lk);

View File

@ -674,6 +674,12 @@ struct ref_store {
const struct ref_storage_be *be; const struct ref_storage_be *be;
}; };
/*
* Parse contents of a loose ref file.
*/
int parse_loose_ref_contents(const char *buf, struct object_id *oid,
struct strbuf *referent, unsigned int *type);
/* /*
* Fill in the generic part of refs and add it to our collection of * Fill in the generic part of refs and add it to our collection of
* reference stores. * reference stores.