Merge branch 'rp/apply-intent-to-add-fix'

"git apply -N" should start from the current index and register
only new files, but it instead started from an empty index, which
has been corrected.

* rp/apply-intent-to-add-fix:
  apply docs: clarify wording for --intent-to-add
  t4140: test apply --intent-to-add interactions
  apply: only write intents to add for new files
  apply: read in the index in --intent-to-add mode
This commit is contained in:
Junio C Hamano
2025-07-14 11:19:29 -07:00
3 changed files with 37 additions and 7 deletions

View File

@@ -75,13 +75,14 @@ OPTIONS
tree. If `--check` is in effect, merely check that it would
apply cleanly to the index entry.
-N::
--intent-to-add::
When applying the patch only to the working tree, mark new
files to be added to the index later (see `--intent-to-add`
option in linkgit:git-add[1]). This option is ignored unless
running in a Git repository and `--index` is not specified.
Note that `--index` could be implied by other options such
as `--cached` or `--3way`.
option in linkgit:git-add[1]). This option is ignored if
`--index` or `--cached` are used, and has no effect outside a Git
repository. Note that `--index` could be implied by other options
such as `--3way`.
-3::
--3way::

View File

@@ -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;
}
@@ -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;

View File

@@ -7,6 +7,10 @@ test_description='git apply of i-t-a file'
test_expect_success setup '
test_write_lines 1 2 3 4 5 >blueprint &&
cat blueprint >committed-file &&
git add committed-file &&
git commit -m "commit" &&
cat blueprint >test-file &&
git add -N test-file &&
git diff >creation-patch &&
@@ -14,7 +18,14 @@ test_expect_success setup '
rm -f test-file &&
git diff >deletion-patch &&
grep "deleted file mode 100644" deletion-patch
grep "deleted file mode 100644" deletion-patch &&
git rm -f test-file &&
test_write_lines 6 >>committed-file &&
cat blueprint >test-file &&
git add -N test-file &&
git diff >complex-patch &&
git restore committed-file
'
test_expect_success 'apply creation patch to ita path (--cached)' '
@@ -53,4 +64,22 @@ test_expect_success 'apply deletion patch to ita path (--index)' '
git ls-files --stage --error-unmatch test-file
'
test_expect_success 'apply creation patch to existing index with -N' '
git rm -f test-file &&
cat blueprint >index-file &&
git add index-file &&
git apply -N creation-patch &&
git ls-files --stage --error-unmatch index-file &&
git ls-files --stage --error-unmatch test-file
'
test_expect_success 'apply complex patch with -N' '
git rm -f test-file index-file &&
git apply -N complex-patch &&
git ls-files --stage --error-unmatch test-file &&
git diff | grep "a/committed-file"
'
test_done