Skip to content

Commit

Permalink
refs: keep track of unresolved reference value in iterators
Browse files Browse the repository at this point in the history
Since ref iterators do not hold onto the direct value of a reference
without resolving it, the only way to get ahold of a direct value of a
symbolic ref is to make a separate call to refs_read_symbolic_ref.

To make accessing the direct value of a symbolic ref more efficient,
let's save the direct value of the ref in the iterators for both the
files backend and the reftable backend.

Signed-off-by: John Cai <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
john-cai authored and gitster committed Aug 9, 2024
1 parent 39bf06a commit cfd9715
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 5 deletions.
15 changes: 12 additions & 3 deletions refs/files-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,12 @@ static void loose_fill_ref_dir_regular_file(struct files_ref_store *refs,
{
struct object_id oid;
int flag;
const char *referent = refs_resolve_ref_unsafe(&refs->base,
refname,
RESOLVE_REF_READING,
&oid, &flag);

if (!refs_resolve_ref_unsafe(&refs->base, refname, RESOLVE_REF_READING,
&oid, &flag)) {
if (!referent) {
oidclr(&oid, the_repository->hash_algo);
flag |= REF_ISBROKEN;
} else if (is_null_oid(&oid)) {
Expand All @@ -268,7 +271,11 @@ static void loose_fill_ref_dir_regular_file(struct files_ref_store *refs,
oidclr(&oid, the_repository->hash_algo);
flag |= REF_BAD_NAME | REF_ISBROKEN;
}
add_entry_to_dir(dir, create_ref_entry(refname, &oid, flag));

if (!(flag & REF_ISSYMREF))
referent = NULL;

add_entry_to_dir(dir, create_ref_entry(refname, referent, &oid, flag));
}

/*
Expand Down Expand Up @@ -886,6 +893,8 @@ static int files_ref_iterator_advance(struct ref_iterator *ref_iterator)
iter->base.refname = iter->iter0->refname;
iter->base.oid = iter->iter0->oid;
iter->base.flags = iter->iter0->flags;
iter->base.referent = iter->iter0->referent;

return ITER_OK;
}

Expand Down
2 changes: 2 additions & 0 deletions refs/iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void base_ref_iterator_init(struct ref_iterator *iter,
{
iter->vtable = vtable;
iter->refname = NULL;
iter->referent = NULL;
iter->oid = NULL;
iter->flags = 0;
}
Expand Down Expand Up @@ -199,6 +200,7 @@ static int merge_ref_iterator_advance(struct ref_iterator *ref_iterator)
}

if (selection & ITER_YIELD_CURRENT) {
iter->base.referent = (*iter->current)->referent;
iter->base.refname = (*iter->current)->refname;
iter->base.oid = (*iter->current)->oid;
iter->base.flags = (*iter->current)->flags;
Expand Down
5 changes: 5 additions & 0 deletions refs/ref-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ struct ref_dir *get_ref_dir(struct ref_entry *entry)
}

struct ref_entry *create_ref_entry(const char *refname,
const char *referent,
const struct object_id *oid, int flag)
{
struct ref_entry *ref;

FLEX_ALLOC_STR(ref, name, refname);
oidcpy(&ref->u.value.oid, oid);
ref->flag = flag;
ref->u.value.referent = xstrdup_or_null(referent);

return ref;
}

Expand All @@ -66,6 +69,7 @@ static void free_ref_entry(struct ref_entry *entry)
*/
clear_ref_dir(&entry->u.subdir);
}
free(entry->u.value.referent);
free(entry);
}

Expand Down Expand Up @@ -431,6 +435,7 @@ static int cache_ref_iterator_advance(struct ref_iterator *ref_iterator)
level->index = -1;
} else {
iter->base.refname = entry->name;
iter->base.referent = entry->u.value.referent;
iter->base.oid = &entry->u.value.oid;
iter->base.flags = entry->flag;
return ITER_OK;
Expand Down
2 changes: 2 additions & 0 deletions refs/ref-cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct ref_value {
* referred to by the last reference in the symlink chain.
*/
struct object_id oid;
char *referent;
};

/*
Expand Down Expand Up @@ -173,6 +174,7 @@ struct ref_entry *create_dir_entry(struct ref_cache *cache,
const char *dirname, size_t len);

struct ref_entry *create_ref_entry(const char *refname,
const char *referent,
const struct object_id *oid, int flag);

/*
Expand Down
1 change: 1 addition & 0 deletions refs/refs-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ enum do_for_each_ref_flags {
struct ref_iterator {
struct ref_iterator_vtable *vtable;
const char *refname;
const char *referent;
const struct object_id *oid;
unsigned int flags;
};
Expand Down
10 changes: 8 additions & 2 deletions refs/reftable-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ static int reftable_ref_iterator_advance(struct ref_iterator *ref_iterator)
struct reftable_ref_iterator *iter =
(struct reftable_ref_iterator *)ref_iterator;
struct reftable_ref_store *refs = iter->refs;
const char *referent = NULL;

while (!iter->err) {
int flags = 0;
Expand Down Expand Up @@ -494,8 +495,12 @@ static int reftable_ref_iterator_advance(struct ref_iterator *ref_iterator)
the_repository->hash_algo);
break;
case REFTABLE_REF_SYMREF:
if (!refs_resolve_ref_unsafe(&iter->refs->base, iter->ref.refname,
RESOLVE_REF_READING, &iter->oid, &flags))
referent = refs_resolve_ref_unsafe(&iter->refs->base,
iter->ref.refname,
RESOLVE_REF_READING,
&iter->oid,
&flags);
if (!referent)
oidclr(&iter->oid, the_repository->hash_algo);
break;
default:
Expand Down Expand Up @@ -523,6 +528,7 @@ static int reftable_ref_iterator_advance(struct ref_iterator *ref_iterator)
continue;

iter->base.refname = iter->ref.refname;
iter->base.referent = referent;
iter->base.oid = &iter->oid;
iter->base.flags = flags;

Expand Down

0 comments on commit cfd9715

Please sign in to comment.