Subject: Re: [PATCH] ls-files: conditionally leave index sparse

From: Junio C Hamano <[email-masked]>
Date: Tue, 26 Aug 2025 12:00:18 -0700

"Derrick Stolee via GitGitGadget" <[email-masked]> writes:

>  	for (i = 0; i < repo->index->cache_nr; i++) {
>  		const struct cache_entry *ce = repo->index->cache[i];
>  		struct stat st;
>  		int stat_err;
>  
> +		if (S_ISSPARSEDIR(ce->ce_mode) && !show_sparse_dirs) {
> +			/*
> +			 * This is the first time we've hit a sparse dir,
> +			 * so expansion will leave the first 'i' entries
> +			 * alone.
> +			 */

In other words,

 (1) we know that the original index entries are sorted

 (2) we are looking at a single directory entry that is sparse, say
     "D/", and ensure_full_index() will expand it (and other later
     entries in the current index).

 (3) we assume that the contents of "D/" will never sort before the
     original location where "D/" used to sit, iow, we do not have
     to rewind to the beginning of index->cache[] array and skip
     what we have already processed.

Having bitten by the index sort order number of times, I just wanted
to make sure everybody's assumption is on the same page.

> +			ensure_full_index(repo->index);
> +			ce = repo->index->cache[i];

and there is no need to say "again" (redo this round of the loop)
here, as grabbing ce was the only thing the loop did, and we just
replaced the entry for the originally folded "D/" with one for the
first subpath in "D/".  Sounds sensible.