refs: standardize output of refs_read_symbolic_ref

When the symbolic reference we want to read with refs_read_symbolic_ref is actually not a symbolic reference, the files and the reftable backends return different values (1 and -1 respectively). Standardize the returned values so that 0 is success, -1 is a generic error and -2 is that the reference was actually non-symbolic. Signed-off-by: Bence Ferdinandy <bence@ferdinandy.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Bence Ferdinandy committed Nov 22, 2024 at 13:28 UTC 8102d10ff8317c1e0ba5afb3a41b6a2bc523ff97
4 files changed +23 -6
refs.h
+11
@@ -83,6 +83,17 @@ int refs_read_ref_full(struct ref_store *refs, const char *refname,
83
84 int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid);
85
86 +#define NOT_A_SYMREF -2
87 +
88 +/*
89 + * Read the symbolic ref named "refname" and write its immediate referent into
90 + * the provided buffer. Referent is left empty if "refname" is not a symbolic
91 + * ref. It does not resolve the symbolic reference recursively in case the
92 + * target is also a symbolic ref.
93 + *
94 + * Returns 0 on success, -2 if the "refname" is not a symbolic ref,
95 + * -1 otherwise.
96 + */
97 int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
98 struct strbuf *referent);
99
refs/files-backend.c
+3 -4
@@ -596,10 +596,9 @@ static int files_read_symbolic_ref(struct ref_store *ref_store, const char *refn
596 unsigned int type;
597
598 ret = read_ref_internal(ref_store, refname, &oid, referent, &type, &failure_errno, 1);
599 - if (ret)
600 - return ret;
601 -
602 - return !(type & REF_ISSYMREF);
599 + if (!ret && !(type & REF_ISSYMREF))
600 + return NOT_A_SYMREF;
601 + return ret;
602 }
603
604 int parse_loose_ref_contents(const struct git_hash_algo *algop,
refs/refs-internal.h
+5
@@ -673,6 +673,11 @@ struct ref_storage_be {
673
674 ref_iterator_begin_fn *iterator_begin;
675 read_raw_ref_fn *read_raw_ref;
676 +
677 + /*
678 + * Please refer to `refs_read_symbolic_ref()` for the expected
679 + * behaviour.
680 + */
681 read_symbolic_ref_fn *read_symbolic_ref;
682
683 reflog_iterator_begin_fn *reflog_iterator_begin;
refs/reftable-backend.c
+4 -2
@@ -830,10 +830,12 @@ static int reftable_be_read_symbolic_ref(struct ref_store *ref_store,
830 return ret;
831
832 ret = reftable_stack_read_ref(stack, refname, &ref);
833 - if (ret == 0 && ref.value_type == REFTABLE_REF_SYMREF)
833 + if (ret)
834 + ret = -1;
835 + else if (ref.value_type == REFTABLE_REF_SYMREF)
836 strbuf_addstr(referent, ref.value.symref);
837 else
836 - ret = -1;
838 + ret = NOT_A_SYMREF;
839
840 reftable_ref_record_release(&ref);
841 return ret;