for-each-ref: add new option to include root refs

The git-for-each-ref(1) command doesn't provide a way to print root refs
i.e pseudorefs and HEAD with the regular "refs/" prefixed refs.

This commit adds a new option "--include-root-refs" to
git-for-each-ref(1). When used this would also print pseudorefs and HEAD
for the current worktree.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Karthik Nayak
2024-02-23 11:01:12 +01:00
committed by Junio C Hamano
parent 810f7a1aac
commit 33d15b5435
6 changed files with 79 additions and 11 deletions

View File

@ -2622,6 +2622,12 @@ static int for_each_fullref_in_pattern(struct ref_filter *filter,
each_ref_fn cb,
void *cb_data)
{
if (filter->kind == FILTER_REFS_KIND_MASK) {
/* In this case, we want to print all refs including root refs. */
return refs_for_each_include_root_refs(get_main_ref_store(the_repository),
cb, cb_data);
}
if (!filter->match_as_path) {
/*
* in this case, the patterns are applied after
@ -2744,6 +2750,9 @@ static int ref_kind_from_refname(const char *refname)
return ref_kind[i].kind;
}
if (is_pseudoref(get_main_ref_store(the_repository), refname))
return FILTER_REFS_PSEUDOREFS;
return FILTER_REFS_OTHERS;
}
@ -2775,7 +2784,16 @@ static struct ref_array_item *apply_ref_filter(const char *refname, const struct
/* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
kind = filter_ref_kind(filter, refname);
if (!(kind & filter->kind))
/*
* Generally HEAD refs are printed with special description denoting a rebase,
* detached state and so forth. This is useful when only printing the HEAD ref
* But when it is being printed along with other pseudorefs, it makes sense to
* keep the formatting consistent. So we mask the type to act like a pseudoref.
*/
if (filter->kind == FILTER_REFS_KIND_MASK && kind == FILTER_REFS_DETACHED_HEAD)
kind = FILTER_REFS_PSEUDOREFS;
else if (!(kind & filter->kind))
return NULL;
if (!filter_pattern_match(filter, refname))
@ -3043,7 +3061,13 @@ static int do_filter_refs(struct ref_filter *filter, unsigned int type, each_ref
ret = for_each_fullref_in("refs/tags/", fn, cb_data);
else if (filter->kind & FILTER_REFS_REGULAR)
ret = for_each_fullref_in_pattern(filter, fn, cb_data);
if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
/*
* When printing all ref types, HEAD is already included,
* so we don't want to print HEAD again.
*/
if (!ret && (filter->kind != FILTER_REFS_KIND_MASK) &&
(filter->kind & FILTER_REFS_DETACHED_HEAD))
head_ref(fn, cb_data);
}