Skip to content

Commit 1b5c6c1

Browse files
pcloudsgitster
authored andcommitted
apply.c: remove implicit dependency on the_index
Use apply_state->repo->index instead of the_index (in most cases, unless we need to use a temporary index in some functions). Let the callers (am and apply) tell us what to use, instead of always assuming to operate on the_index. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 82ea77e commit 1b5c6c1

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

apply.c

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3385,7 +3385,8 @@ static int verify_index_match(struct apply_state *state,
33853385
return -1;
33863386
return 0;
33873387
}
3388-
return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
3388+
return ie_match_stat(state->repo->index, ce, st,
3389+
CE_MATCH_IGNORE_VALID | CE_MATCH_IGNORE_SKIP_WORKTREE);
33893390
}
33903391

33913392
#define SUBMODULE_PATCH_WITHOUT_INDEX 1
@@ -3518,14 +3519,14 @@ static int load_current(struct apply_state *state,
35183519
if (!patch->is_new)
35193520
BUG("patch to %s is not a creation", patch->old_name);
35203521

3521-
pos = cache_name_pos(name, strlen(name));
3522+
pos = index_name_pos(state->repo->index, name, strlen(name));
35223523
if (pos < 0)
35233524
return error(_("%s: does not exist in index"), name);
3524-
ce = active_cache[pos];
3525+
ce = state->repo->index->cache[pos];
35253526
if (lstat(name, &st)) {
35263527
if (errno != ENOENT)
35273528
return error_errno("%s", name);
3528-
if (checkout_target(&the_index, ce, &st))
3529+
if (checkout_target(state->repo->index, ce, &st))
35293530
return -1;
35303531
}
35313532
if (verify_index_match(state, ce, &st))
@@ -3687,15 +3688,16 @@ static int check_preimage(struct apply_state *state,
36873688
}
36883689

36893690
if (state->check_index && !previous) {
3690-
int pos = cache_name_pos(old_name, strlen(old_name));
3691+
int pos = index_name_pos(state->repo->index, old_name,
3692+
strlen(old_name));
36913693
if (pos < 0) {
36923694
if (patch->is_new < 0)
36933695
goto is_new;
36943696
return error(_("%s: does not exist in index"), old_name);
36953697
}
3696-
*ce = active_cache[pos];
3698+
*ce = state->repo->index->cache[pos];
36973699
if (stat_ret < 0) {
3698-
if (checkout_target(&the_index, *ce, st))
3700+
if (checkout_target(state->repo->index, *ce, st))
36993701
return -1;
37003702
}
37013703
if (!state->cached && verify_index_match(state, *ce, st))
@@ -3742,7 +3744,7 @@ static int check_to_create(struct apply_state *state,
37423744
struct stat nst;
37433745

37443746
if (state->check_index &&
3745-
cache_name_pos(new_name, strlen(new_name)) >= 0 &&
3747+
index_name_pos(state->repo->index, new_name, strlen(new_name)) >= 0 &&
37463748
!ok_if_exists)
37473749
return EXISTS_IN_INDEX;
37483750
if (state->cached)
@@ -3831,7 +3833,8 @@ static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *na
38313833
if (state->check_index) {
38323834
struct cache_entry *ce;
38333835

3834-
ce = cache_file_exists(name->buf, name->len, ignore_case);
3836+
ce = index_file_exists(state->repo->index, name->buf,
3837+
name->len, ignore_case);
38353838
if (ce && S_ISLNK(ce->ce_mode))
38363839
return 1;
38373840
} else {
@@ -4006,9 +4009,10 @@ static int check_patch_list(struct apply_state *state, struct patch *patch)
40064009
static int read_apply_cache(struct apply_state *state)
40074010
{
40084011
if (state->index_file)
4009-
return read_cache_from(state->index_file);
4012+
return read_index_from(state->repo->index, state->index_file,
4013+
get_git_dir());
40104014
else
4011-
return read_cache();
4015+
return read_index(state->repo->index);
40124016
}
40134017

40144018
/* This function tries to read the object name from the current index */
@@ -4019,10 +4023,10 @@ static int get_current_oid(struct apply_state *state, const char *path,
40194023

40204024
if (read_apply_cache(state) < 0)
40214025
return -1;
4022-
pos = cache_name_pos(path, strlen(path));
4026+
pos = index_name_pos(state->repo->index, path, strlen(path));
40234027
if (pos < 0)
40244028
return -1;
4025-
oidcpy(oid, &active_cache[pos]->oid);
4029+
oidcpy(oid, &state->repo->index->cache[pos]->oid);
40264030
return 0;
40274031
}
40284032

@@ -4250,7 +4254,7 @@ static void patch_stats(struct apply_state *state, struct patch *patch)
42504254
static int remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)
42514255
{
42524256
if (state->update_index && !state->ita_only) {
4253-
if (remove_file_from_cache(patch->old_name) < 0)
4257+
if (remove_file_from_index(state->repo->index, patch->old_name) < 0)
42544258
return error(_("unable to remove %s from index"), patch->old_name);
42554259
}
42564260
if (!state->cached) {
@@ -4271,7 +4275,7 @@ static int add_index_file(struct apply_state *state,
42714275
struct cache_entry *ce;
42724276
int namelen = strlen(path);
42734277

4274-
ce = make_empty_cache_entry(&the_index, namelen);
4278+
ce = make_empty_cache_entry(state->repo->index, namelen);
42754279
memcpy(ce->name, path, namelen);
42764280
ce->ce_mode = create_ce_mode(mode);
42774281
ce->ce_flags = create_ce_flags(0);
@@ -4303,7 +4307,7 @@ static int add_index_file(struct apply_state *state,
43034307
"for newly created file %s"), path);
43044308
}
43054309
}
4306-
if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {
4310+
if (add_index_entry(state->repo->index, ce, ADD_CACHE_OK_TO_ADD) < 0) {
43074311
discard_cache_entry(ce);
43084312
return error(_("unable to add cache entry for %s"), path);
43094313
}
@@ -4341,7 +4345,7 @@ static int try_create_file(struct apply_state *state, const char *path,
43414345
if (fd < 0)
43424346
return 1;
43434347

4344-
if (convert_to_working_tree(&the_index, path, buf, size, &nbuf)) {
4348+
if (convert_to_working_tree(state->repo->index, path, buf, size, &nbuf)) {
43454349
size = nbuf.len;
43464350
buf = nbuf.buf;
43474351
}
@@ -4438,17 +4442,17 @@ static int add_conflicted_stages_file(struct apply_state *state,
44384442
namelen = strlen(patch->new_name);
44394443
mode = patch->new_mode ? patch->new_mode : (S_IFREG | 0644);
44404444

4441-
remove_file_from_cache(patch->new_name);
4445+
remove_file_from_index(state->repo->index, patch->new_name);
44424446
for (stage = 1; stage < 4; stage++) {
44434447
if (is_null_oid(&patch->threeway_stage[stage - 1]))
44444448
continue;
4445-
ce = make_empty_cache_entry(&the_index, namelen);
4449+
ce = make_empty_cache_entry(state->repo->index, namelen);
44464450
memcpy(ce->name, patch->new_name, namelen);
44474451
ce->ce_mode = create_ce_mode(mode);
44484452
ce->ce_flags = create_ce_flags(stage);
44494453
ce->ce_namelen = namelen;
44504454
oidcpy(&ce->oid, &patch->threeway_stage[stage - 1]);
4451-
if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {
4455+
if (add_index_entry(state->repo->index, ce, ADD_CACHE_OK_TO_ADD) < 0) {
44524456
discard_cache_entry(ce);
44534457
return error(_("unable to add cache entry for %s"),
44544458
patch->new_name);
@@ -4897,7 +4901,7 @@ int apply_all_patches(struct apply_state *state,
48974901
}
48984902

48994903
if (state->update_index) {
4900-
res = write_locked_index(&the_index, &state->lock_file, COMMIT_LOCK);
4904+
res = write_locked_index(state->repo->index, &state->lock_file, COMMIT_LOCK);
49014905
if (res) {
49024906
error(_("Unable to write new index file"));
49034907
res = -128;

0 commit comments

Comments
 (0)