Skip to content

Commit f5b69ee

Browse files
committed
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
2 parents 2b5bf70 + 2b49d97 commit f5b69ee

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

Documentation/git-apply.adoc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,14 @@ OPTIONS
7575
tree. If `--check` is in effect, merely check that it would
7676
apply cleanly to the index entry.
7777

78+
-N::
7879
--intent-to-add::
7980
When applying the patch only to the working tree, mark new
8081
files to be added to the index later (see `--intent-to-add`
81-
option in linkgit:git-add[1]). This option is ignored unless
82-
running in a Git repository and `--index` is not specified.
83-
Note that `--index` could be implied by other options such
84-
as `--cached` or `--3way`.
82+
option in linkgit:git-add[1]). This option is ignored if
83+
`--index` or `--cached` are used, and has no effect outside a Git
84+
repository. Note that `--index` could be implied by other options
85+
such as `--3way`.
8586

8687
-3::
8788
--3way::

apply.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4565,7 +4565,7 @@ static int create_file(struct apply_state *state, struct patch *patch)
45654565

45664566
if (patch->conflicted_threeway)
45674567
return add_conflicted_stages_file(state, patch);
4568-
else if (state->update_index)
4568+
else if (state->check_index || (state->ita_only && patch->is_new > 0))
45694569
return add_index_file(state, path, mode, buf, size);
45704570
return 0;
45714571
}
@@ -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;

t/t4140-apply-ita.sh

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,25 @@ test_description='git apply of i-t-a file'
77
test_expect_success setup '
88
test_write_lines 1 2 3 4 5 >blueprint &&
99
10+
cat blueprint >committed-file &&
11+
git add committed-file &&
12+
git commit -m "commit" &&
13+
1014
cat blueprint >test-file &&
1115
git add -N test-file &&
1216
git diff >creation-patch &&
1317
grep "new file mode 100644" creation-patch &&
1418
1519
rm -f test-file &&
1620
git diff >deletion-patch &&
17-
grep "deleted file mode 100644" deletion-patch
21+
grep "deleted file mode 100644" deletion-patch &&
22+
23+
git rm -f test-file &&
24+
test_write_lines 6 >>committed-file &&
25+
cat blueprint >test-file &&
26+
git add -N test-file &&
27+
git diff >complex-patch &&
28+
git restore committed-file
1829
'
1930

2031
test_expect_success 'apply creation patch to ita path (--cached)' '
@@ -53,4 +64,22 @@ test_expect_success 'apply deletion patch to ita path (--index)' '
5364
git ls-files --stage --error-unmatch test-file
5465
'
5566

67+
test_expect_success 'apply creation patch to existing index with -N' '
68+
git rm -f test-file &&
69+
cat blueprint >index-file &&
70+
git add index-file &&
71+
git apply -N creation-patch &&
72+
73+
git ls-files --stage --error-unmatch index-file &&
74+
git ls-files --stage --error-unmatch test-file
75+
'
76+
77+
test_expect_success 'apply complex patch with -N' '
78+
git rm -f test-file index-file &&
79+
git apply -N complex-patch &&
80+
81+
git ls-files --stage --error-unmatch test-file &&
82+
git diff | grep "a/committed-file"
83+
'
84+
5685
test_done

0 commit comments

Comments
 (0)