git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 09/17] refs: generalize `refs_for_each_fullref_in_prefixes()`

From
Karthik Nayak <karthik.188@gmail.com>
Date
Feb 23, 2026, 09:06 UTC
Message-ID
<CAOLa=ZSKF1WoE_2q5oEjU8MOh=_0s--cPwf0R8xnQQF8=9dWDw@mail.gmail.com>
In-Reply-To
<20260220-pks-refs-for-each-unification-v1-9-17170bd99de1@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
Show 18 quoted lines
> The function `refs_for_each_fullref_in_prefixes()` can be used to
> iterate over all references part of any of the user-provided prefixes.
> In contrast to the `prefix` parameter of `refs_for_each_ref_ext()` it
> knows to handle the case well where multiple of the passed-in prefixes
> start with a common prefix by computing longest common prefixes and then
> iterating over those.
>
> While we could move this logic into `refs_for_each_ref_ext()`, this one
> feels somewhat special as we perform multiple iterations. But what we
> _can_ do is to generalize how this function works: instead of accepting
> only a small handful of parameters, we can have it accept the full
> options structure.
>
> One obvious exception is that the caller must not provide a prefix via
> the options. But this case can be easily detected.
>
> Refactor the code accordingly.
>

I've attempted the refactoring that this series is doing a couple of times, I always got stuck up on how to integrate this function with the rest. This is a novel approach.

Show 114 quoted lines
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
>  ls-refs.c    | 11 +++++++----
>  ref-filter.c | 11 +++++++----
>  refs.c       | 39 +++++++++++++++------------------------
>  refs.h       | 16 +++++-----------
>  4 files changed, 34 insertions(+), 43 deletions(-)
>
> diff --git a/ls-refs.c b/ls-refs.c
> index 8641281b86..9759826ca7 100644
> --- a/ls-refs.c
> +++ b/ls-refs.c
> @@ -160,6 +160,7 @@ static int ls_refs_config(const char *var, const char *value,
>
>  int ls_refs(struct repository *r, struct packet_reader *request)
>  {
> +	struct refs_for_each_ref_options opts = { 0 };
>  	struct ls_refs_data data;
>
>  	memset(&data, 0, sizeof(data));
> @@ -201,10 +202,12 @@ int ls_refs(struct repository *r, struct packet_reader *request)
>  	send_possibly_unborn_head(&data);
>  	if (!data.prefixes.nr)
>  		strvec_push(&data.prefixes, "");
> -	refs_for_each_fullref_in_prefixes(get_main_ref_store(r),
> -					  get_git_namespace(), data.prefixes.v,
> -					  hidden_refs_to_excludes(&data.hidden_refs),
> -					  send_ref, &data);
> +
> +	opts.exclude_patterns = hidden_refs_to_excludes(&data.hidden_refs);
> +	opts.namespace = get_git_namespace();
> +
> +	refs_for_each_ref_in_prefixes(get_main_ref_store(r), data.prefixes.v,
> +				      &opts, send_ref, &data);
>  	packet_fflush(stdout);
>  	strvec_clear(&data.prefixes);
>  	strbuf_release(&data.buf);
> diff --git a/ref-filter.c b/ref-filter.c
> index 049e845a19..7c682e0a33 100644
> --- a/ref-filter.c
> +++ b/ref-filter.c
> @@ -2807,6 +2807,10 @@ static int for_each_fullref_in_pattern(struct ref_filter *filter,
>  				       refs_for_each_cb cb,
>  				       void *cb_data)
>  {
> +	struct refs_for_each_ref_options opts = {
> +		.exclude_patterns = filter->exclude.v,
> +	};
> +
>  	if (filter->kind & FILTER_REFS_ROOT_REFS) {
>  		/* In this case, we want to print all refs including root refs. */
>  		return for_each_fullref_with_seek(filter, cb, cb_data,
> @@ -2836,10 +2840,9 @@ static int for_each_fullref_in_pattern(struct ref_filter *filter,
>  		return for_each_fullref_with_seek(filter, cb, cb_data, 0);
>  	}
>
> -	return refs_for_each_fullref_in_prefixes(get_main_ref_store(the_repository),
> -						 NULL, filter->name_patterns,
> -						 filter->exclude.v,
> -						 cb, cb_data);
> +	return refs_for_each_ref_in_prefixes(get_main_ref_store(the_repository),
> +					     filter->name_patterns, &opts,
> +					     cb, cb_data);
>  }
>
>  /*
> diff --git a/refs.c b/refs.c
> index 99994879d9..20d34faeb5 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -2029,40 +2029,31 @@ static void find_longest_prefixes(struct string_list *out,
>  	strbuf_release(&prefix);
>  }
>
> -int refs_for_each_fullref_in_prefixes(struct ref_store *ref_store,
> -				      const char *namespace,
> -				      const char **patterns,
> -				      const char **exclude_patterns,
> -				      refs_for_each_cb fn, void *cb_data)
> +int refs_for_each_ref_in_prefixes(struct ref_store *ref_store,
> +				  const char **prefixes,
> +				  const struct refs_for_each_ref_options *opts,
> +				  refs_for_each_cb cb, void *cb_data)
>  {
> -	struct strvec namespaced_exclude_patterns = STRVEC_INIT;
> -	struct string_list prefixes = STRING_LIST_INIT_DUP;
> +	struct string_list longest_prefixes = STRING_LIST_INIT_DUP;
>  	struct string_list_item *prefix;
> -	struct strbuf buf = STRBUF_INIT;
> -	int ret = 0, namespace_len;
> +	int ret = 0;
>
> -	find_longest_prefixes(&prefixes, patterns);
> +	if (opts->prefix)
> +		BUG("refs_for_each_ref_in_prefixes called with specific prefix");
>
> -	if (namespace)
> -		strbuf_addstr(&buf, namespace);
> -	namespace_len = buf.len;
> +	find_longest_prefixes(&longest_prefixes, prefixes);
>
> -	exclude_patterns = get_namespaced_exclude_patterns(exclude_patterns,
> -							   namespace,
> -							   &namespaced_exclude_patterns);
> +	for_each_string_list_item(prefix, &longest_prefixes) {
> +		struct refs_for_each_ref_options prefix_opts = *opts;
> +		prefix_opts.prefix = prefix->string;
>
> -	for_each_string_list_item(prefix, &prefixes) {
> -		strbuf_addstr(&buf, prefix->string);
> -		ret = refs_for_each_fullref_in(ref_store, buf.buf,
> -					       exclude_patterns, fn, cb_data);
> +		ret = refs_for_each_ref_ext(ref_store, cb, cb_data,
> +					    &prefix_opts);

Okay so it still calls `refs_for_each_ref_ext()` on the longest prefix. Makes sense.

[snip]
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 21 of 34 in “refs: unify `refs_for_each_*()` functions”
  1. 00/17 refs: unify `refs_for_each_*()` functionsPatrick Steinhardt, Feb 20, 2026
  2. 01/17 refs: move `refs_head_ref_namespaced()`Patrick Steinhardt, Feb 20, 2026
  3. Karthik NayakFeb 23, 2026
  4. Patrick SteinhardtFeb 23, 2026
  5. 02/17 refs: move `do_for_each_ref_flags` further upPatrick Steinhardt, Feb 20, 2026
  6. 03/17 refs: rename `do_for_each_ref_flags`Patrick Steinhardt, Feb 20, 2026
  7. 04/17 refs: rename `each_ref_fn`Patrick Steinhardt, Feb 20, 2026
  8. Karthik NayakFeb 23, 2026
  9. 05/17 refs: remove unused `refs_for_each_include_root_ref()`Patrick Steinhardt, Feb 20, 2026
  10. Oswald BuddenhagenFeb 20, 2026
  11. Patrick SteinhardtFeb 20, 2026
  12. 06/17 refs: introduce `refs_for_each_ref_ext`Patrick Steinhardt, Feb 20, 2026
  13. Karthik NayakFeb 23, 2026
  14. 07/17 refs: speed up `refs_for_each_glob_ref_in()`Patrick Steinhardt, Feb 20, 2026
  15. Karthik NayakFeb 23, 2026
  16. Patrick SteinhardtFeb 23, 2026
  17. 08/17 refs: generalize `refs_for_each_namespaced_ref()`Patrick Steinhardt, Feb 20, 2026
  18. Karthik NayakFeb 23, 2026
  19. Patrick SteinhardtFeb 23, 2026
  20. 09/17 refs: generalize `refs_for_each_fullref_in_prefixes()`Patrick Steinhardt, Feb 20, 2026
  21. Karthik NayakFeb 23, 2026
  22. 10/17 refs: improve verification for-each-ref optionsPatrick Steinhardt, Feb 20, 2026
  23. Karthik NayakFeb 23, 2026
  24. 11/17 refs: replace `refs_for_each_ref_in()`Patrick Steinhardt, Feb 20, 2026
  25. Karthik NayakFeb 23, 2026
  26. Patrick SteinhardtFeb 23, 2026
  27. Karthik NayakFeb 23, 2026
  28. 12/17 refs: replace `refs_for_each_rawref()`Patrick Steinhardt, Feb 20, 2026
  29. 13/17 refs: replace `refs_for_each_rawref_in()`Patrick Steinhardt, Feb 20, 2026
  30. 14/17 refs: replace `refs_for_each_glob_ref_in()`Patrick Steinhardt, Feb 20, 2026
  31. 15/17 refs: replace `refs_for_each_glob_ref()`Patrick Steinhardt, Feb 20, 2026
  32. 16/17 refs: replace `refs_for_each_namespaced_ref()`Patrick Steinhardt, Feb 20, 2026
  33. 17/17 refs: replace `refs_for_each_fullref_in()`Patrick Steinhardt, Feb 20, 2026
  34. Karthik NayakFeb 23, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.