]> git.ipfire.org Git - thirdparty/git.git/commitdiff
apply: only write intents to add for new files
authorRaymond E. Pasco <ray@ameretat.dev>
Mon, 7 Jul 2025 12:12:31 +0000 (08:12 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 7 Jul 2025 13:41:10 +0000 (06:41 -0700)
In the "apply only to files" mode (i.e., neither --index nor --cached
mode), the index should not be touched except to record intents to
add when --intent-to-add is on. Because having --intent-to-add on sets
update_index, to indicate that we may touch the index, we can't rely
only on that flag in create_file() (which is called to write both new
files and updated files) to decide whether to write an index entry;
if we did, we would write an index entry for every file being patched
(which would moreover be an intent-to-add entry despite not being a
new file, because we are going to turn on the CE_INTENT_TO_ADD flag
in add_index_entry() if we enter it here and ita_only is true).

To decide whether to touch the index, we need to check the
specific reason the index would be updated, rather than merely
their aggregate in the update_index flag. Because we have already
entered write_out_results() and are performing writes, we know that
state->apply is true. If state->check_index is additionally true, we
are in --index or --cached mode, which updates the index and should
always write, whereas if we are merely in ita_only mode we must only
write if the patch is a new file creation patch.

Signed-off-by: Raymond E. Pasco <ray@ameretat.dev>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
apply.c

diff --git a/apply.c b/apply.c
index c8d4517c0a299f9d2164b429a5a4bf561b7659f6..8637ad4c9fc9f07ab726886b8dc632bef09c4856 100644 (file)
--- a/apply.c
+++ b/apply.c
@@ -4565,7 +4565,7 @@ static int create_file(struct apply_state *state, struct patch *patch)
 
        if (patch->conflicted_threeway)
                return add_conflicted_stages_file(state, patch);
-       else if (state->update_index)
+       else if (state->check_index || (state->ita_only && patch->is_new > 0))
                return add_index_file(state, path, mode, buf, size);
        return 0;
 }