apply: make i-t-a entries never match worktree
By definition, an intent-to-add index entry can never match the worktree, because worktrees have no concept of intent-to-add entries. Therefore, "apply --index" should always fail on intent-to-add paths. Because check_preimage() calls verify_index_match(), it already fails for patches other than creation patches, which check_preimage() ignores. This patch adds a check to check_preimage()'s rough equivalent for creation patches, check_to_create(). Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Raymond E. Pasco <ray@ameretat.dev> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Raymond E. Pasco committed
Aug 8, 2020 at 03:49 UTC
e3cc41b4f939a64c74b6d4a2d59f6efe006c4e4b
1 file changed
+19
-5
apply.c
+19
-5
@@ -3740,6 +3740,7 @@ static int check_preimage(struct apply_state *state,
3740
3741
#define EXISTS_IN_INDEX 1
3742
#define EXISTS_IN_WORKTREE 2
3743
+#define EXISTS_IN_INDEX_AS_ITA 3
3744
3745
static int check_to_create(struct apply_state *state,
3746
const char *new_name,
@@ -3747,11 +3748,21 @@ static int check_to_create(struct apply_state *state,
3748
{
3749
struct stat nst;
3750
3750
- if (state->check_index && !ok_if_exists) {
3751
- int pos = index_name_pos(state->repo->index, new_name, strlen(new_name));
3752
- if (pos >= 0 &&
3753
- !(state->repo->index->cache[pos]->ce_flags & CE_INTENT_TO_ADD))
3754
- return EXISTS_IN_INDEX;
3751
+ if (state->check_index && (!ok_if_exists || !state->cached)) {
3752
+ int pos;
3753
+
3754
+ pos = index_name_pos(state->repo->index, new_name, strlen(new_name));
3755
+ if (pos >= 0) {
3756
+ struct cache_entry *ce = state->repo->index->cache[pos];
3757
+
3758
+ /* allow ITA, as they do not yet exist in the index */
3759
+ if (!ok_if_exists && !(ce->ce_flags & CE_INTENT_TO_ADD))
3760
+ return EXISTS_IN_INDEX;
3761
+
3762
+ /* ITA entries can never match working tree files */
3763
+ if (!state->cached && (ce->ce_flags & CE_INTENT_TO_ADD))
3764
+ return EXISTS_IN_INDEX_AS_ITA;
3765
+ }
3766
}
3767
3768
if (state->cached)
@@ -3938,6 +3949,9 @@ static int check_patch(struct apply_state *state, struct patch *patch)
3949
case EXISTS_IN_INDEX:
3950
return error(_("%s: already exists in index"), new_name);
3951
break;
3952
+ case EXISTS_IN_INDEX_AS_ITA:
3953
+ return error(_("%s: does not match index"), new_name);
3954
+ break;
3955
case EXISTS_IN_WORKTREE:
3956
return error(_("%s: already exists in working directory"),
3957
new_name);