Subject: [PATCH 2/5] apply: read in the index in --intent-to-add mode

From: "Raymond E. Pasco" <[email-masked]>
Date: Sat, 10 May 2025 20:36:45 -0400

There are three main modes of operation for apply: applying only to the
worktree, applying to the worktree and index (--index), and applying
only to the index (--cached).

The --intent-to-add flag modifies the first of these modes, applying
only to the worktree, in a way which touches the index, because
intents to add are special index entries. However, it has not ever
worked correctly in any but the most trivial (empty repository)
cases, because the index was never read in (in apply, this is done
in read_apply_cache()) before writing to it.

The update_index flag is set in apply_patch() to mean that we
are touching the index at all, as opposed to the check_index flag
indicating --index mode.  Therefore, the reading of the index should
be gated by the update_index flag rather than the check_index flag,
so that we are prepared to work with the index before we begin adding
intents to add to it.

Reported-by: Ryan Hodges <[email-masked]>
Original-patch-by: Johannes Altmanninger <[email-masked]>
Signed-off-by: Raymond E. Pasco <[email-masked]>
---
 apply.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/apply.c b/apply.c
index 5e39cadde4..3bde54a04a 100644
--- a/apply.c
+++ b/apply.c
@@ -4837,7 +4837,7 @@ static int apply_patch(struct apply_state *state,
 					       LOCK_DIE_ON_ERROR);
 	}
 
-	if (state->check_index && read_apply_cache(state) < 0) {
+	if (state->update_index && read_apply_cache(state) < 0) {
 		error(_("unable to read index file"));
 		res = -128;
 		goto end;
-- 
2.49.0.1106.gc0efa3ba58