refs API: make refs_read_raw_ref() not set errno

Add a "failure_errno" to refs_read_raw_ref(), his allows
refs_werrres_ref_unsafe() to pass along its "failure_errno", as a
first step before its own callers are migrated to pass it further up
the chain.

We are leaving out out the refs_read_special_head() in
refs_read_raw_ref() for now, as noted in a subsequent commit moving it
to "failure_errno" will require some special consideration.

Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Han-Wen Nienhuys 2021-10-16 11:39:09 +02:00 коммит произвёл Junio C Hamano
Родитель ef18119dec
Коммит 8b72fea7e9
4 изменённых файлов: 29 добавлений и 18 удалений

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

@ -1666,17 +1666,18 @@ done:
return result; return result;
} }
int refs_read_raw_ref(struct ref_store *ref_store, int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,
const char *refname, struct object_id *oid, struct object_id *oid, struct strbuf *referent,
struct strbuf *referent, unsigned int *type) unsigned int *type, int *failure_errno)
{ {
assert(failure_errno);
if (!strcmp(refname, "FETCH_HEAD") || !strcmp(refname, "MERGE_HEAD")) { if (!strcmp(refname, "FETCH_HEAD") || !strcmp(refname, "MERGE_HEAD")) {
return refs_read_special_head(ref_store, refname, oid, referent, return refs_read_special_head(ref_store, refname, oid, referent,
type); type);
} }
return ref_store->be->read_raw_ref(ref_store, refname, oid, referent, return ref_store->be->read_raw_ref(ref_store, refname, oid, referent,
type, &errno); type, failure_errno);
} }
const char *refs_werrres_ref_unsafe(struct ref_store *refs, const char *refs_werrres_ref_unsafe(struct ref_store *refs,
@ -1720,9 +1721,8 @@ const char *refs_werrres_ref_unsafe(struct ref_store *refs,
for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) { for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) {
unsigned int read_flags = 0; unsigned int read_flags = 0;
errno = 0; if (refs_read_raw_ref(refs, refname, oid, &sb_refname,
if (refs_read_raw_ref(refs, refname, &read_flags, failure_errno)) {
oid, &sb_refname, &read_flags)) {
*flags |= read_flags; *flags |= read_flags;
if (errno) if (errno)
*failure_errno = errno; *failure_errno = errno;
@ -2240,6 +2240,13 @@ int refs_verify_refname_available(struct ref_store *refs,
strbuf_grow(&dirname, strlen(refname) + 1); strbuf_grow(&dirname, strlen(refname) + 1);
for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) { for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
/*
* Just saying "Is a directory" when we e.g. can't
* lock some multi-level ref isn't very informative,
* the user won't be told *what* is a directory, so
* let's not use strerror() below.
*/
int ignore_errno;
/* Expand dirname to the new prefix, not including the trailing slash: */ /* Expand dirname to the new prefix, not including the trailing slash: */
strbuf_add(&dirname, refname + dirname.len, slash - refname - dirname.len); strbuf_add(&dirname, refname + dirname.len, slash - refname - dirname.len);
@ -2251,7 +2258,8 @@ int refs_verify_refname_available(struct ref_store *refs,
if (skip && string_list_has_string(skip, dirname.buf)) if (skip && string_list_has_string(skip, dirname.buf))
continue; continue;
if (!refs_read_raw_ref(refs, dirname.buf, &oid, &referent, &type)) { if (!refs_read_raw_ref(refs, dirname.buf, &oid, &referent,
&type, &ignore_errno)) {
strbuf_addf(err, _("'%s' exists; cannot create '%s'"), strbuf_addf(err, _("'%s' exists; cannot create '%s'"),
dirname.buf, refname); dirname.buf, refname);
goto cleanup; goto cleanup;

Просмотреть файл

@ -381,10 +381,11 @@ stat_ref:
goto out; goto out;
if (lstat(path, &st) < 0) { if (lstat(path, &st) < 0) {
int ignore_errno;
if (errno != ENOENT) if (errno != ENOENT)
goto out; goto out;
if (refs_read_raw_ref(refs->packed_ref_store, refname, if (refs_read_raw_ref(refs->packed_ref_store, refname, oid,
oid, referent, type)) { referent, type, &ignore_errno)) {
errno = ENOENT; errno = ENOENT;
goto out; goto out;
} }
@ -418,13 +419,14 @@ stat_ref:
/* Is it a directory? */ /* Is it a directory? */
if (S_ISDIR(st.st_mode)) { if (S_ISDIR(st.st_mode)) {
int ignore_errno;
/* /*
* Even though there is a directory where the loose * Even though there is a directory where the loose
* ref is supposed to be, there could still be a * ref is supposed to be, there could still be a
* packed ref: * packed ref:
*/ */
if (refs_read_raw_ref(refs->packed_ref_store, refname, if (refs_read_raw_ref(refs->packed_ref_store, refname, oid,
oid, referent, type)) { referent, type, &ignore_errno)) {
errno = EISDIR; errno = EISDIR;
goto out; goto out;
} }

Просмотреть файл

@ -1347,6 +1347,7 @@ int is_packed_transaction_needed(struct ref_store *ref_store,
ret = 0; ret = 0;
for (i = 0; i < transaction->nr; i++) { for (i = 0; i < transaction->nr; i++) {
struct ref_update *update = transaction->updates[i]; struct ref_update *update = transaction->updates[i];
int failure_errno;
unsigned int type; unsigned int type;
struct object_id oid; struct object_id oid;
@ -1357,9 +1358,9 @@ int is_packed_transaction_needed(struct ref_store *ref_store,
*/ */
continue; continue;
if (!refs_read_raw_ref(ref_store, update->refname, if (!refs_read_raw_ref(ref_store, update->refname, &oid,
&oid, &referent, &type) || &referent, &type, &failure_errno) ||
errno != ENOENT) { failure_errno != ENOENT) {
/* /*
* We have to actually delete that reference * We have to actually delete that reference
* -> this transaction is needed. * -> this transaction is needed.

Просмотреть файл

@ -149,9 +149,9 @@ struct ref_update {
const char refname[FLEX_ARRAY]; const char refname[FLEX_ARRAY];
}; };
int refs_read_raw_ref(struct ref_store *ref_store, int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,
const char *refname, struct object_id *oid, struct object_id *oid, struct strbuf *referent,
struct strbuf *referent, unsigned int *type); unsigned int *type, int *failure_errno);
/* /*
* Write an error to `err` and return a nonzero value iff the same * Write an error to `err` and return a nonzero value iff the same