Skip to content

Commit 57391a9

Browse files
jupedgitster
authored andcommitted
apply: read in the index in --intent-to-add mode
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, since its introduction in cff5dc0 (apply: add --intent-to-add, 2018-05-26), it has not worked correctly in any but the most trivial (empty repository) cases, because the index is 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 flag causes the index to be updated. 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 due to being in --index or --cached mode. 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 <[email protected]> Original-patch-by: Johannes Altmanninger <[email protected]> Signed-off-by: Raymond E. Pasco <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 16bd9f2 commit 57391a9

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4833,7 +4833,7 @@ static int apply_patch(struct apply_state *state,
48334833
LOCK_DIE_ON_ERROR);
48344834
}
48354835

4836-
if (state->check_index && read_apply_cache(state) < 0) {
4836+
if ((state->check_index || state->update_index) && read_apply_cache(state) < 0) {
48374837
error(_("unable to read index file"));
48384838
res = -128;
48394839
goto end;

0 commit comments

Comments
 (0)