Skip to content

Commit e3cc41b

Browse files
jupedgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Raymond E. Pasco <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7cfde3f commit e3cc41b

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

apply.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3740,18 +3740,29 @@ static int check_preimage(struct apply_state *state,
37403740

37413741
#define EXISTS_IN_INDEX 1
37423742
#define EXISTS_IN_WORKTREE 2
3743+
#define EXISTS_IN_INDEX_AS_ITA 3
37433744

37443745
static int check_to_create(struct apply_state *state,
37453746
const char *new_name,
37463747
int ok_if_exists)
37473748
{
37483749
struct stat nst;
37493750

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+
}
37553766
}
37563767

37573768
if (state->cached)
@@ -3938,6 +3949,9 @@ static int check_patch(struct apply_state *state, struct patch *patch)
39383949
case EXISTS_IN_INDEX:
39393950
return error(_("%s: already exists in index"), new_name);
39403951
break;
3952+
case EXISTS_IN_INDEX_AS_ITA:
3953+
return error(_("%s: does not match index"), new_name);
3954+
break;
39413955
case EXISTS_IN_WORKTREE:
39423956
return error(_("%s: already exists in working directory"),
39433957
new_name);

0 commit comments

Comments
 (0)