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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Aug 13, 2018 at 18:14 UTC
1b5c6c1e5309e8be24ad4958baa2553a003ee45e
1 file changed
+25
-21
apply.c
+25
-21
@@ -3385,7 +3385,8 @@ static int verify_index_match(struct apply_state *state,
3385
return -1;
3386
return 0;
3387
}
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);
3390
}
3391
3392
#define SUBMODULE_PATCH_WITHOUT_INDEX 1
@@ -3518,14 +3519,14 @@ static int load_current(struct apply_state *state,
3519
if (!patch->is_new)
3520
BUG("patch to %s is not a creation", patch->old_name);
3521
3521
- pos = cache_name_pos(name, strlen(name));
3522
+ pos = index_name_pos(state->repo->index, name, strlen(name));
3523
if (pos < 0)
3524
return error(_("%s: does not exist in index"), name);
3524
- ce = active_cache[pos];
3525
+ ce = state->repo->index->cache[pos];
3526
if (lstat(name, &st)) {
3527
if (errno != ENOENT)
3528
return error_errno("%s", name);
3528
- if (checkout_target(&the_index, ce, &st))
3529
+ if (checkout_target(state->repo->index, ce, &st))
3530
return -1;
3531
}
3532
if (verify_index_match(state, ce, &st))
@@ -3687,15 +3688,16 @@ static int check_preimage(struct apply_state *state,
3688
}
3689
3690
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));
3693
if (pos < 0) {
3694
if (patch->is_new < 0)
3695
goto is_new;
3696
return error(_("%s: does not exist in index"), old_name);
3697
}
3696
- *ce = active_cache[pos];
3698
+ *ce = state->repo->index->cache[pos];
3699
if (stat_ret < 0) {
3698
- if (checkout_target(&the_index, *ce, st))
3700
+ if (checkout_target(state->repo->index, *ce, st))
3701
return -1;
3702
}
3703
if (!state->cached && verify_index_match(state, *ce, st))
@@ -3742,7 +3744,7 @@ static int check_to_create(struct apply_state *state,
3744
struct stat nst;
3745
3746
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 &&
3748
!ok_if_exists)
3749
return EXISTS_IN_INDEX;
3750
if (state->cached)
@@ -3831,7 +3833,8 @@ static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *na
3833
if (state->check_index) {
3834
struct cache_entry *ce;
3835
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);
3838
if (ce && S_ISLNK(ce->ce_mode))
3839
return 1;
3840
} else {
@@ -4006,9 +4009,10 @@ static int check_patch_list(struct apply_state *state, struct patch *patch)
4009
static int read_apply_cache(struct apply_state *state)
4010
{
4011
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());
4014
else
4011
- return read_cache();
4015
+ return read_index(state->repo->index);
4016
}
4017
4018
/* 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,
4023
4024
if (read_apply_cache(state) < 0)
4025
return -1;
4022
- pos = cache_name_pos(path, strlen(path));
4026
+ pos = index_name_pos(state->repo->index, path, strlen(path));
4027
if (pos < 0)
4028
return -1;
4025
- oidcpy(oid, &active_cache[pos]->oid);
4029
+ oidcpy(oid, &state->repo->index->cache[pos]->oid);
4030
return 0;
4031
}
4032
@@ -4250,7 +4254,7 @@ static void patch_stats(struct apply_state *state, struct patch *patch)
4254
static int remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)
4255
{
4256
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)
4258
return error(_("unable to remove %s from index"), patch->old_name);
4259
}
4260
if (!state->cached) {
@@ -4271,7 +4275,7 @@ static int add_index_file(struct apply_state *state,
4275
struct cache_entry *ce;
4276
int namelen = strlen(path);
4277
4274
- ce = make_empty_cache_entry(&the_index, namelen);
4278
+ ce = make_empty_cache_entry(state->repo->index, namelen);
4279
memcpy(ce->name, path, namelen);
4280
ce->ce_mode = create_ce_mode(mode);
4281
ce->ce_flags = create_ce_flags(0);
@@ -4303,7 +4307,7 @@ static int add_index_file(struct apply_state *state,
4307
"for newly created file %s"), path);
4308
}
4309
}
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) {
4311
discard_cache_entry(ce);
4312
return error(_("unable to add cache entry for %s"), path);
4313
}
@@ -4341,7 +4345,7 @@ static int try_create_file(struct apply_state *state, const char *path,
4345
if (fd < 0)
4346
return 1;
4347
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)) {
4349
size = nbuf.len;
4350
buf = nbuf.buf;
4351
}
@@ -4438,17 +4442,17 @@ static int add_conflicted_stages_file(struct apply_state *state,
4442
namelen = strlen(patch->new_name);
4443
mode = patch->new_mode ? patch->new_mode : (S_IFREG | 0644);
4444
4441
- remove_file_from_cache(patch->new_name);
4445
+ remove_file_from_index(state->repo->index, patch->new_name);
4446
for (stage = 1; stage < 4; stage++) {
4447
if (is_null_oid(&patch->threeway_stage[stage - 1]))
4448
continue;
4445
- ce = make_empty_cache_entry(&the_index, namelen);
4449
+ ce = make_empty_cache_entry(state->repo->index, namelen);
4450
memcpy(ce->name, patch->new_name, namelen);
4451
ce->ce_mode = create_ce_mode(mode);
4452
ce->ce_flags = create_ce_flags(stage);
4453
ce->ce_namelen = namelen;
4454
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) {
4456
discard_cache_entry(ce);
4457
return error(_("unable to add cache entry for %s"),
4458
patch->new_name);
@@ -4897,7 +4901,7 @@ int apply_all_patches(struct apply_state *state,
4901
}
4902
4903
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);
4905
if (res) {
4906
error(_("Unable to write new index file"));
4907
res = -128;