Subject: Re: [PATCH 3/6] refs/files: sort reflogs returned by the reflog iterator

From: Junio C Hamano <[email-masked]>
Date: Mon, 19 Feb 2024 16:04:16 -0800

Patrick Steinhardt <[email-masked]> writes:

> We use a directory iterator to return reflogs via the reflog iterator.
> This iterator returns entries in the same order as readdir(3P) would and
> will thus yield reflogs with no discernible order.
>
> Set the new `DIR_ITERATOR_SORTED` flag that was introduced in the
> preceding commit so that the order is deterministic. While the effect of
> this can only been observed in a test tool, a subsequent commit will
> start to expose this functionality to users via a new `git reflog list`
> subcommand.
>
> Signed-off-by: Patrick Steinhardt <[email-masked]>
> ---
>  refs/files-backend.c           | 4 ++--
>  t/t0600-reffiles-backend.sh    | 4 ++--
>  t/t1405-main-ref-store.sh      | 2 +-
>  t/t1406-submodule-ref-store.sh | 2 +-
>  4 files changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/refs/files-backend.c b/refs/files-backend.c
> index 75dcc21ecb..2ffc63185f 100644
> --- a/refs/files-backend.c
> +++ b/refs/files-backend.c
> @@ -2193,7 +2193,7 @@ static struct ref_iterator *reflog_iterator_begin(struct ref_store *ref_store,
>  
>  	strbuf_addf(&sb, "%s/logs", gitdir);
>  
> -	diter = dir_iterator_begin(sb.buf, 0);
> +	diter = dir_iterator_begin(sb.buf, DIR_ITERATOR_SORTED);
>  	if (!diter) {
>  		strbuf_release(&sb);
>  		return empty_ref_iterator_begin();
> @@ -2202,7 +2202,7 @@ static struct ref_iterator *reflog_iterator_begin(struct ref_store *ref_store,
>  	CALLOC_ARRAY(iter, 1);
>  	ref_iterator = &iter->base;
>  
> -	base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable, 0);
> +	base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable, 1);

This caught my attention.  Once we apply this patch, the only way
base_ref_iterator_init() can receive 0 for its last parameter
(i.e. 'ordered') is via the merge_ref_iterator_begin() call in
files_reflog_iterator_begin() that passes 0 as 'ordered'.  If we
force files_reflog_iterator_begin() to ask for an ordered
merge_ref_iterator, then we will have no unordered ref iterators.
Am I reading the code right?