Thread (18 messages) 18 messages, 2 authors, 2025-07-07
STALE359d
Revisions (3)
  1. v1 [diff vs current]
  2. v2 current
  3. v3 [diff vs current]

[PATCH v2 1/4] apply: read in the index in --intent-to-add mode

From: Raymond E. Pasco <hidden>
Date: 2025-07-02 21:28:37
Subsystem: the rest · Maintainer: Linus Torvalds

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.

This causes the operation to clobber the old, correct index with a
new empty-tree index before writing intent-to-add entries to this
empty index; the final result is that the index now records every
existing file in the repository as deleted, which is incorrect.

This error can be corrected by first reading the index. The
update_index flag is correctly set if ita_only is true, because
this mode updates the index. However, if we merely gate the call
to read_apply_cache() behind update_index, then it will not be read
when state->apply is false, even if it must be checked. Therefore,
we instead read the index if it will be either checked or updated,
because reading the index is a prerequisite to either.

Reported-by: Ryan Hodges <redacted>
Original-patch-by: Johannes Altmanninger [off-list ref]
Signed-off-by: Raymond E. Pasco <redacted>
---
 apply.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/apply.c b/apply.c
index 8bbe6ed224..c8d4517c0a 100644
--- a/apply.c
+++ b/apply.c
@@ -4833,7 +4833,7 @@ static int apply_patch(struct apply_state *state,
 					       LOCK_DIE_ON_ERROR);
 	}
 
-	if (state->check_index && read_apply_cache(state) < 0) {
+	if ((state->check_index || state->update_index) && read_apply_cache(state) < 0) {
 		error(_("unable to read index file"));
 		res = -128;
 		goto end;
-- 
2.50.0.201.gfeb04032fb
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help