read-cache: split out function to drop unmerged entries to stage 0

In `repo_read_index_unmerged()` we read the index and then drop any unmerged entries to stage 0. In a subsequent commit we'll want to perform this operation on arbitrary indexes, not only the one of the given repository. Prepare for this by splitting out the functionality into a new function that can act on an arbitrary index. While at it, fix a signedness mismatch when iterating through the index cache entries. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jul 1, 2026 at 13:35 UTC ca39c3511d89dd9c84a90b0a4dc394a950e08772
2 files changed +8 -5
read-cache-ll.h
+1
@@ -309,6 +309,7 @@ int write_locked_index(struct index_state *, struct lock_file *lock, unsigned fl
309 void discard_index(struct index_state *);
310 void move_index_extensions(struct index_state *dst, struct index_state *src);
311 int unmerged_index(const struct index_state *);
312 +int index_state_unmerged_to_stage0(struct index_state *istate);
313
314 /**
315 * Returns 1 if istate differs from tree, 0 otherwise. If tree is NULL,
read-cache.c
+7 -5
@@ -3403,13 +3403,15 @@ out:
3403 */
3404 int repo_read_index_unmerged(struct repository *repo)
3405 {
3406 - struct index_state *istate;
3407 - int i;
3406 + repo_read_index(repo);
3407 + return index_state_unmerged_to_stage0(repo->index);
3408 +}
3409 +
3410 +int index_state_unmerged_to_stage0(struct index_state *istate)
3411 +{
3412 int unmerged = 0;
3413
3410 - repo_read_index(repo);
3411 - istate = repo->index;
3412 - for (i = 0; i < istate->cache_nr; i++) {
3414 + for (unsigned int i = 0; i < istate->cache_nr; i++) {
3415 struct cache_entry *ce = istate->cache[i];
3416 struct cache_entry *new_ce;
3417 int len;