Skip to content

Commit 8102d10

Browse files
ferdinandybgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2fd5555 commit 8102d10

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

refs.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ int refs_read_ref_full(struct ref_store *refs, const char *refname,
8383

8484
int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid);
8585

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+
*/
8697
int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
8798
struct strbuf *referent);
8899

refs/files-backend.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -596,10 +596,9 @@ static int files_read_symbolic_ref(struct ref_store *ref_store, const char *refn
596596
unsigned int type;
597597

598598
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;
603602
}
604603

605604
int parse_loose_ref_contents(const struct git_hash_algo *algop,

refs/refs-internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,11 @@ struct ref_storage_be {
673673

674674
ref_iterator_begin_fn *iterator_begin;
675675
read_raw_ref_fn *read_raw_ref;
676+
677+
/*
678+
* Please refer to `refs_read_symbolic_ref()` for the expected
679+
* behaviour.
680+
*/
676681
read_symbolic_ref_fn *read_symbolic_ref;
677682

678683
reflog_iterator_begin_fn *reflog_iterator_begin;

refs/reftable-backend.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,10 +830,12 @@ static int reftable_be_read_symbolic_ref(struct ref_store *ref_store,
830830
return ret;
831831

832832
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)
834836
strbuf_addstr(referent, ref.value.symref);
835837
else
836-
ret = -1;
838+
ret = NOT_A_SYMREF;
837839

838840
reftable_ref_record_release(&ref);
839841
return ret;

0 commit comments

Comments
 (0)