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>

Han-Wen Nienhuys committed Oct 16, 2021 at 11:39 UTC 8b72fea7e91b5900c3a35891df1d8ea16d4b2bee
4 files changed +29 -18
refs.c
+16 -8
@@ -1666,17 +1666,18 @@ done:
1666 return result;
1667 }
1668
1669 -int refs_read_raw_ref(struct ref_store *ref_store,
1670 - const char *refname, struct object_id *oid,
1671 - struct strbuf *referent, unsigned int *type)
1669 +int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,
1670 + struct object_id *oid, struct strbuf *referent,
1671 + unsigned int *type, int *failure_errno)
1672 {
1673 + assert(failure_errno);
1674 if (!strcmp(refname, "FETCH_HEAD") || !strcmp(refname, "MERGE_HEAD")) {
1675 return refs_read_special_head(ref_store, refname, oid, referent,
1676 type);
1677 }
1678
1679 return ref_store->be->read_raw_ref(ref_store, refname, oid, referent,
1679 - type, &errno);
1680 + type, failure_errno);
1681 }
1682
1683 const char *refs_werrres_ref_unsafe(struct ref_store *refs,
@@ -1720,9 +1721,8 @@ const char *refs_werrres_ref_unsafe(struct ref_store *refs,
1721 for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) {
1722 unsigned int read_flags = 0;
1723
1723 - errno = 0;
1724 - if (refs_read_raw_ref(refs, refname,
1725 - oid, &sb_refname, &read_flags)) {
1724 + if (refs_read_raw_ref(refs, refname, oid, &sb_refname,
1725 + &read_flags, failure_errno)) {
1726 *flags |= read_flags;
1727 if (errno)
1728 *failure_errno = errno;
@@ -2240,6 +2240,13 @@ int refs_verify_refname_available(struct ref_store *refs,
2240
2241 strbuf_grow(&dirname, strlen(refname) + 1);
2242 for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
2243 + /*
2244 + * Just saying "Is a directory" when we e.g. can't
2245 + * lock some multi-level ref isn't very informative,
2246 + * the user won't be told *what* is a directory, so
2247 + * let's not use strerror() below.
2248 + */
2249 + int ignore_errno;
2250 /* Expand dirname to the new prefix, not including the trailing slash: */
2251 strbuf_add(&dirname, refname + dirname.len, slash - refname - dirname.len);
2252
@@ -2251,7 +2258,8 @@ int refs_verify_refname_available(struct ref_store *refs,
2258 if (skip && string_list_has_string(skip, dirname.buf))
2259 continue;
2260
2254 - if (!refs_read_raw_ref(refs, dirname.buf, &oid, &referent, &type)) {
2261 + if (!refs_read_raw_ref(refs, dirname.buf, &oid, &referent,
2262 + &type, &ignore_errno)) {
2263 strbuf_addf(err, _("'%s' exists; cannot create '%s'"),
2264 dirname.buf, refname);
2265 goto cleanup;
refs/files-backend.c
+6 -4
@@ -381,10 +381,11 @@ stat_ref:
381 goto out;
382
383 if (lstat(path, &st) < 0) {
384 + int ignore_errno;
385 if (errno != ENOENT)
386 goto out;
386 - if (refs_read_raw_ref(refs->packed_ref_store, refname,
387 - oid, referent, type)) {
387 + if (refs_read_raw_ref(refs->packed_ref_store, refname, oid,
388 + referent, type, &ignore_errno)) {
389 errno = ENOENT;
390 goto out;
391 }
@@ -418,13 +419,14 @@ stat_ref:
419
420 /* Is it a directory? */
421 if (S_ISDIR(st.st_mode)) {
422 + int ignore_errno;
423 /*
424 * Even though there is a directory where the loose
425 * ref is supposed to be, there could still be a
426 * packed ref:
427 */
426 - if (refs_read_raw_ref(refs->packed_ref_store, refname,
427 - oid, referent, type)) {
428 + if (refs_read_raw_ref(refs->packed_ref_store, refname, oid,
429 + referent, type, &ignore_errno)) {
430 errno = EISDIR;
431 goto out;
432 }
refs/packed-backend.c
+4 -3
@@ -1347,6 +1347,7 @@ int is_packed_transaction_needed(struct ref_store *ref_store,
1347 ret = 0;
1348 for (i = 0; i < transaction->nr; i++) {
1349 struct ref_update *update = transaction->updates[i];
1350 + int failure_errno;
1351 unsigned int type;
1352 struct object_id oid;
1353
@@ -1357,9 +1358,9 @@ int is_packed_transaction_needed(struct ref_store *ref_store,
1358 */
1359 continue;
1360
1360 - if (!refs_read_raw_ref(ref_store, update->refname,
1361 - &oid, &referent, &type) ||
1362 - errno != ENOENT) {
1361 + if (!refs_read_raw_ref(ref_store, update->refname, &oid,
1362 + &referent, &type, &failure_errno) ||
1363 + failure_errno != ENOENT) {
1364 /*
1365 * We have to actually delete that reference
1366 * -> this transaction is needed.
refs/refs-internal.h
+3 -3
@@ -149,9 +149,9 @@ struct ref_update {
149 const char refname[FLEX_ARRAY];
150 };
151
152 -int refs_read_raw_ref(struct ref_store *ref_store,
153 - const char *refname, struct object_id *oid,
154 - struct strbuf *referent, unsigned int *type);
152 +int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,
153 + struct object_id *oid, struct strbuf *referent,
154 + unsigned int *type, int *failure_errno);
155
156 /*
157 * Write an error to `err` and return a nonzero value iff the same