Skip to content

Commit cd475b3

Browse files
pks-tgitster
authored andcommitted
refs: add ability for backends to special-case reading of symbolic refs
Reading of symbolic and non-symbolic references is currently treated the same in reference backends: we always call `refs_read_raw_ref()` and then decide based on the returned flags what type it is. This has one downside though: symbolic references may be treated different from normal references in a backend from normal references. The packed-refs backend for example doesn't even know about symbolic references, and as a result it is pointless to even ask it for one. There are cases where we really only care about whether a reference is symbolic or not, but don't care about whether it exists at all or may be a non-symbolic reference. But it is not possible to optimize for this case right now, and as a consequence we will always first check for a loose reference to exist, and if it doesn't, we'll query the packed-refs backend for a known-to-not-be-symbolic reference. This is inefficient and requires us to search all packed references even though we know to not care for the result at all. Introduce a new function `refs_read_symbolic_ref()` which allows us to fix this case. This function will only ever return symbolic references and can thus optimize for the scenario layed out above. By default, if the backend doesn't provide an implementation for it, we just use the old code path and fall back to `read_raw_ref()`. But in case the backend provides its own, more efficient implementation, we will use that one instead. Note that this function is explicitly designed to not distinguish between missing references and non-symbolic references. If it did, we'd be forced to always search the packed-refs backend to see whether the symbolic reference the user asked for really doesn't exist, or if it exists as a non-symbolic reference. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8e55634 commit cd475b3

File tree

6 files changed

+39
-0
lines changed

6 files changed

+39
-0
lines changed

refs.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,23 @@ int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,
16721672
type, failure_errno);
16731673
}
16741674

1675+
int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
1676+
struct strbuf *referent)
1677+
{
1678+
struct object_id oid;
1679+
int ret, failure_errno = 0;
1680+
unsigned int type = 0;
1681+
1682+
if (ref_store->be->read_symbolic_ref)
1683+
return ref_store->be->read_symbolic_ref(ref_store, refname, referent);
1684+
1685+
ret = refs_read_raw_ref(ref_store, refname, &oid, referent, &type, &failure_errno);
1686+
if (ret || !(type & REF_ISSYMREF))
1687+
return -1;
1688+
1689+
return 0;
1690+
}
1691+
16751692
const char *refs_resolve_ref_unsafe(struct ref_store *refs,
16761693
const char *refname,
16771694
int resolve_flags,

refs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ int read_ref_full(const char *refname, int resolve_flags,
8282
struct object_id *oid, int *flags);
8383
int read_ref(const char *refname, struct object_id *oid);
8484

85+
int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
86+
struct strbuf *referent);
87+
8588
/*
8689
* Return 0 if a reference named refname could be created without
8790
* conflicting with the name of an existing reference. Otherwise,

refs/debug.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ struct ref_storage_be refs_be_debug = {
435435

436436
debug_ref_iterator_begin,
437437
debug_read_raw_ref,
438+
NULL,
438439

439440
debug_reflog_iterator_begin,
440441
debug_for_each_reflog_ent,

refs/files-backend.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3286,6 +3286,7 @@ struct ref_storage_be refs_be_files = {
32863286

32873287
files_ref_iterator_begin,
32883288
files_read_raw_ref,
3289+
NULL,
32893290

32903291
files_reflog_iterator_begin,
32913292
files_for_each_reflog_ent,

refs/packed-backend.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,6 +1684,7 @@ struct ref_storage_be refs_be_packed = {
16841684

16851685
packed_ref_iterator_begin,
16861686
packed_read_raw_ref,
1687+
NULL,
16871688

16881689
packed_reflog_iterator_begin,
16891690
packed_for_each_reflog_ent,

refs/refs-internal.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,21 @@ typedef int read_raw_ref_fn(struct ref_store *ref_store, const char *refname,
649649
struct object_id *oid, struct strbuf *referent,
650650
unsigned int *type, int *failure_errno);
651651

652+
/*
653+
* Read a symbolic reference from the specified reference store. This function
654+
* is optional: if not implemented by a backend, then `read_raw_ref_fn` is used
655+
* to read the symbolcic reference instead. It is intended to be implemented
656+
* only in case the backend can optimize the reading of symbolic references.
657+
*
658+
* Return 0 on success, or -1 on failure. `referent` will be set to the target
659+
* of the symbolic reference on success. This function explicitly does not
660+
* distinguish between error cases and the reference not being a symbolic
661+
* reference to allow backends to optimize this operation in case symbolic and
662+
* non-symbolic references are treated differently.
663+
*/
664+
typedef int read_symbolic_ref_fn(struct ref_store *ref_store, const char *refname,
665+
struct strbuf *referent);
666+
652667
struct ref_storage_be {
653668
struct ref_storage_be *next;
654669
const char *name;
@@ -668,6 +683,7 @@ struct ref_storage_be {
668683

669684
ref_iterator_begin_fn *iterator_begin;
670685
read_raw_ref_fn *read_raw_ref;
686+
read_symbolic_ref_fn *read_symbolic_ref;
671687

672688
reflog_iterator_begin_fn *reflog_iterator_begin;
673689
for_each_reflog_ent_fn *for_each_reflog_ent;

0 commit comments

Comments
 (0)