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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 1, 2022 at 10:33 UTC cd475b3b03809b1b1c664e0dca9f16f815456719
6 files changed +39
refs.c
+17
@@ -1672,6 +1672,23 @@ int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,
1672 type, failure_errno);
1673 }
1674
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 +
1692 const char *refs_resolve_ref_unsafe(struct ref_store *refs,
1693 const char *refname,
1694 int resolve_flags,
refs.h
+3
@@ -82,6 +82,9 @@ int read_ref_full(const char *refname, int resolve_flags,
82 struct object_id *oid, int *flags);
83 int read_ref(const char *refname, struct object_id *oid);
84
85 +int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
86 + struct strbuf *referent);
87 +
88 /*
89 * Return 0 if a reference named refname could be created without
90 * conflicting with the name of an existing reference. Otherwise,
refs/debug.c
+1
@@ -435,6 +435,7 @@ struct ref_storage_be refs_be_debug = {
435
436 debug_ref_iterator_begin,
437 debug_read_raw_ref,
438 + NULL,
439
440 debug_reflog_iterator_begin,
441 debug_for_each_reflog_ent,
refs/files-backend.c
+1
@@ -3286,6 +3286,7 @@ struct ref_storage_be refs_be_files = {
3286
3287 files_ref_iterator_begin,
3288 files_read_raw_ref,
3289 + NULL,
3290
3291 files_reflog_iterator_begin,
3292 files_for_each_reflog_ent,
refs/packed-backend.c
+1
@@ -1684,6 +1684,7 @@ struct ref_storage_be refs_be_packed = {
1684
1685 packed_ref_iterator_begin,
1686 packed_read_raw_ref,
1687 + NULL,
1688
1689 packed_reflog_iterator_begin,
1690 packed_for_each_reflog_ent,
refs/refs-internal.h
+16
@@ -649,6 +649,21 @@ typedef int read_raw_ref_fn(struct ref_store *ref_store, const char *refname,
649 struct object_id *oid, struct strbuf *referent,
650 unsigned int *type, int *failure_errno);
651
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 +
667 struct ref_storage_be {
668 struct ref_storage_be *next;
669 const char *name;
@@ -668,6 +683,7 @@ struct ref_storage_be {
683
684 ref_iterator_begin_fn *iterator_begin;
685 read_raw_ref_fn *read_raw_ref;
686 + read_symbolic_ref_fn *read_symbolic_ref;
687
688 reflog_iterator_begin_fn *reflog_iterator_begin;
689 for_each_reflog_ent_fn *for_each_reflog_ent;