refs: add pseudorefs array and iteration functions

Define const array 'pseudorefs' with the names of the pseudorefs that
are documented in gitrevisions.1, and add functions for_each_pseudoref()
and refs_for_each_pseudoref() for iterating over them.

The functions process the pseudorefs in the same way as head_ref() and
refs_head_ref() process HEAD, invoking an each_ref_fn callback on each
pseudoref that exists.

This is in preparation for adding pseudorefs to log decorations.

Signed-off-by: Andy Koppe <andy.koppe@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Andy Koppe
2023-10-23 23:11:41 +01:00
committed by Junio C Hamano
parent c6bcc49d69
commit 3a5c981df9
2 changed files with 47 additions and 0 deletions

42
refs.c
View File

@ -65,6 +65,21 @@ static unsigned char refname_disposition[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4
};
/*
* List of documented pseudorefs. This needs to be kept in sync with the list
* in Documentation/revisions.txt.
*/
static const char *const pseudorefs[] = {
"FETCH_HEAD",
"ORIG_HEAD",
"MERGE_HEAD",
"REBASE_HEAD",
"CHERRY_PICK_HEAD",
"REVERT_HEAD",
"BISECT_HEAD",
"AUTO_MERGE",
};
struct ref_namespace_info ref_namespace[] = {
[NAMESPACE_HEAD] = {
.ref = "HEAD",
@ -1549,6 +1564,33 @@ int head_ref(each_ref_fn fn, void *cb_data)
return refs_head_ref(get_main_ref_store(the_repository), fn, cb_data);
}
int refs_for_each_pseudoref(struct ref_store *refs,
each_ref_fn fn, void *cb_data)
{
int i;
for (i = 0; i < ARRAY_SIZE(pseudorefs); i++) {
struct object_id oid;
int flag;
if (refs_resolve_ref_unsafe(refs, pseudorefs[i],
RESOLVE_REF_READING, &oid, &flag)) {
int ret = fn(pseudorefs[i], &oid, flag, cb_data);
if (ret)
return ret;
}
}
return 0;
}
int for_each_pseudoref(each_ref_fn fn, void *cb_data)
{
return refs_for_each_pseudoref(get_main_ref_store(the_repository),
fn, cb_data);
}
struct ref_iterator *refs_ref_iterator_begin(
struct ref_store *refs,
const char *prefix,

5
refs.h
View File

@ -320,6 +320,8 @@ typedef int each_repo_ref_fn(struct repository *r,
*/
int refs_head_ref(struct ref_store *refs,
each_ref_fn fn, void *cb_data);
int refs_for_each_pseudoref(struct ref_store *refs,
each_ref_fn fn, void *cb_data);
int refs_for_each_ref(struct ref_store *refs,
each_ref_fn fn, void *cb_data);
int refs_for_each_ref_in(struct ref_store *refs, const char *prefix,
@ -334,6 +336,9 @@ int refs_for_each_remote_ref(struct ref_store *refs,
/* just iterates the head ref. */
int head_ref(each_ref_fn fn, void *cb_data);
/* iterates pseudorefs. */
int for_each_pseudoref(each_ref_fn fn, void *cb_data);
/* iterates all refs. */
int for_each_ref(each_ref_fn fn, void *cb_data);