Skip to content

Commit 73f69f2

Browse files
committed
Merge branch 'ab/cache-api-cleanup' into ab/cache-api-cleanup-users
* ab/cache-api-cleanup: cache API: add a "INDEX_STATE_INIT" macro/function, add release_index() read-cache.c: refactor set_new_index_sparsity() for subsequent commit sparse-index API: BUG() out on NULL ensure_full_index() sparse-index.c: expand_to_path() can assume non-NULL "istate" builtin/difftool.c: { 0 }-initialize rather than using memset()
2 parents a7caae2 + 2f6b1eb commit 73f69f2

13 files changed

+64
-41
lines changed

apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4105,7 +4105,7 @@ static int preimage_oid_in_gitlink_patch(struct patch *p, struct object_id *oid)
41054105
static int build_fake_ancestor(struct apply_state *state, struct patch *list)
41064106
{
41074107
struct patch *patch;
4108-
struct index_state result = { NULL };
4108+
struct index_state result = INDEX_STATE_INIT;
41094109
struct lock_file lock = LOCK_INIT;
41104110
int res;
41114111

builtin/difftool.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
361361
struct hashmap symlinks2 = HASHMAP_INIT(pair_cmp, NULL);
362362
struct hashmap_iter iter;
363363
struct pair_entry *entry;
364-
struct index_state wtindex;
364+
struct index_state wtindex = INDEX_STATE_INIT;
365365
struct checkout lstate, rstate;
366366
int err = 0;
367367
struct child_process cmd = CHILD_PROCESS_INIT;
@@ -387,8 +387,6 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
387387
mkdir(ldir.buf, 0700);
388388
mkdir(rdir.buf, 0700);
389389

390-
memset(&wtindex, 0, sizeof(wtindex));
391-
392390
memset(&lstate, 0, sizeof(lstate));
393391
lstate.base_dir = lbase_dir = xstrdup(ldir.buf);
394392
lstate.base_dir_len = ldir.len;

builtin/sparse-checkout.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ static int update_working_directory(struct pattern_list *pl)
217217
o.head_idx = -1;
218218
o.src_index = r->index;
219219
o.dst_index = r->index;
220+
index_state_init(&o.result);
220221
o.skip_sparse_checkout = 0;
221222
o.pl = pl;
222223

builtin/stash.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
11371137
int ret = 0;
11381138
struct strbuf untracked_msg = STRBUF_INIT;
11391139
struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1140-
struct index_state istate = { NULL };
1140+
struct index_state istate = INDEX_STATE_INIT;
11411141

11421142
cp_upd_index.git_cmd = 1;
11431143
strvec_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
@@ -1165,7 +1165,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
11651165
}
11661166

11671167
done:
1168-
discard_index(&istate);
1168+
release_index(&istate);
11691169
strbuf_release(&untracked_msg);
11701170
remove_path(stash_index_path.buf);
11711171
return ret;
@@ -1176,7 +1176,7 @@ static int stash_staged(struct stash_info *info, struct strbuf *out_patch,
11761176
{
11771177
int ret = 0;
11781178
struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
1179-
struct index_state istate = { NULL };
1179+
struct index_state istate = INDEX_STATE_INIT;
11801180

11811181
if (write_index_as_tree(&info->w_tree, &istate, the_repository->index_file,
11821182
0, NULL)) {
@@ -1199,7 +1199,7 @@ static int stash_staged(struct stash_info *info, struct strbuf *out_patch,
11991199
}
12001200

12011201
done:
1202-
discard_index(&istate);
1202+
release_index(&istate);
12031203
return ret;
12041204
}
12051205

@@ -1209,7 +1209,7 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
12091209
int ret = 0;
12101210
struct child_process cp_read_tree = CHILD_PROCESS_INIT;
12111211
struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
1212-
struct index_state istate = { NULL };
1212+
struct index_state istate = INDEX_STATE_INIT;
12131213
char *old_index_env = NULL, *old_repo_index_file;
12141214

12151215
remove_path(stash_index_path.buf);
@@ -1260,7 +1260,7 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
12601260
}
12611261

12621262
done:
1263-
discard_index(&istate);
1263+
release_index(&istate);
12641264
remove_path(stash_index_path.buf);
12651265
return ret;
12661266
}
@@ -1271,7 +1271,7 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps
12711271
struct rev_info rev;
12721272
struct child_process cp_upd_index = CHILD_PROCESS_INIT;
12731273
struct strbuf diff_output = STRBUF_INIT;
1274-
struct index_state istate = { NULL };
1274+
struct index_state istate = INDEX_STATE_INIT;
12751275

12761276
init_revisions(&rev, NULL);
12771277
copy_pathspec(&rev.prune_data, ps);
@@ -1319,7 +1319,7 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps
13191319
}
13201320

13211321
done:
1322-
discard_index(&istate);
1322+
release_index(&istate);
13231323
release_revisions(&rev);
13241324
strbuf_release(&diff_output);
13251325
remove_path(stash_index_path.buf);

builtin/worktree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ static int unlock_worktree(int ac, const char **av, const char *prefix)
923923

924924
static void validate_no_submodules(const struct worktree *wt)
925925
{
926-
struct index_state istate = { NULL };
926+
struct index_state istate = INDEX_STATE_INIT;
927927
struct strbuf path = STRBUF_INIT;
928928
int i, found_submodules = 0;
929929

cache.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,19 @@ struct index_state {
360360
struct pattern_list *sparse_checkout_patterns;
361361
};
362362

363+
/**
364+
* A "struct index_state istate" must be initialized with
365+
* INDEX_STATE_INIT or the corresponding index_state_init().
366+
*
367+
* If the variable won't be used again, use release_index() to free()
368+
* its resources. If it needs to be used again use discard_index(),
369+
* which does the same thing, but will use use index_state_init() at
370+
* the end.
371+
*/
372+
#define INDEX_STATE_INIT { 0 }
373+
void index_state_init(struct index_state *istate);
374+
void release_index(struct index_state *istate);
375+
363376
/* Name hashing */
364377
int test_lazy_init_name_hash(struct index_state *istate, int try_threaded);
365378
void add_name_hash(struct index_state *istate, struct cache_entry *ce);

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ static int unpack_trees_start(struct merge_options *opt,
412412
{
413413
int rc;
414414
struct tree_desc t[3];
415-
struct index_state tmp_index = { NULL };
415+
struct index_state tmp_index = INDEX_STATE_INIT;
416416

417417
memset(&opt->priv->unpack_opts, 0, sizeof(opt->priv->unpack_opts));
418418
if (opt->priv->call_depth)

read-cache.c

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,12 +2300,12 @@ static void set_new_index_sparsity(struct index_state *istate)
23002300
* If the index's repo exists, mark it sparse according to
23012301
* repo settings.
23022302
*/
2303-
if (istate->repo) {
2304-
prepare_repo_settings(istate->repo);
2305-
if (!istate->repo->settings.command_requires_full_index &&
2306-
is_sparse_index_allowed(istate, 0))
2307-
istate->sparse_index = 1;
2308-
}
2303+
if (!istate->repo)
2304+
return;
2305+
prepare_repo_settings(istate->repo);
2306+
if (!istate->repo->settings.command_requires_full_index &&
2307+
is_sparse_index_allowed(istate, 0))
2308+
istate->sparse_index = 1;
23092309
}
23102310

23112311
/* remember to discard_cache() before reading a different cache! */
@@ -2498,9 +2498,10 @@ int read_index_from(struct index_state *istate, const char *path,
24982498

24992499
trace_performance_enter();
25002500
if (split_index->base)
2501-
discard_index(split_index->base);
2501+
release_index(split_index->base);
25022502
else
2503-
CALLOC_ARRAY(split_index->base, 1);
2503+
ALLOC_ARRAY(split_index->base, 1);
2504+
index_state_init(split_index->base);
25042505

25052506
base_oid_hex = oid_to_hex(&split_index->base_oid);
25062507
base_path = xstrfmt("%s/sharedindex.%s", gitdir, base_oid_hex);
@@ -2539,7 +2540,13 @@ int is_index_unborn(struct index_state *istate)
25392540
return (!istate->cache_nr && !istate->timestamp.sec);
25402541
}
25412542

2542-
void discard_index(struct index_state *istate)
2543+
void index_state_init(struct index_state *istate)
2544+
{
2545+
struct index_state blank = INDEX_STATE_INIT;
2546+
memcpy(istate, &blank, sizeof(*istate));
2547+
}
2548+
2549+
void release_index(struct index_state *istate)
25432550
{
25442551
/*
25452552
* Cache entries in istate->cache[] should have been allocated
@@ -2551,20 +2558,12 @@ void discard_index(struct index_state *istate)
25512558
validate_cache_entries(istate);
25522559

25532560
resolve_undo_clear_index(istate);
2554-
istate->cache_nr = 0;
2555-
istate->cache_changed = 0;
2556-
istate->timestamp.sec = 0;
2557-
istate->timestamp.nsec = 0;
25582561
free_name_hash(istate);
25592562
cache_tree_free(&(istate->cache_tree));
2560-
istate->initialized = 0;
2561-
istate->fsmonitor_has_run_once = 0;
2562-
FREE_AND_NULL(istate->fsmonitor_last_update);
2563-
FREE_AND_NULL(istate->cache);
2564-
istate->cache_alloc = 0;
2563+
free(istate->fsmonitor_last_update);
2564+
free(istate->cache);
25652565
discard_split_index(istate);
25662566
free_untracked_cache(istate->untracked);
2567-
istate->untracked = NULL;
25682567

25692568
if (istate->sparse_checkout_patterns) {
25702569
clear_pattern_list(istate->sparse_checkout_patterns);
@@ -2577,6 +2576,12 @@ void discard_index(struct index_state *istate)
25772576
}
25782577
}
25792578

2579+
void discard_index(struct index_state *istate)
2580+
{
2581+
release_index(istate);
2582+
index_state_init(istate);
2583+
}
2584+
25802585
/*
25812586
* Validate the cache entries of this index.
25822587
* All cache entries associated with this index

repository.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,10 @@ int repo_read_index(struct repository *repo)
302302
{
303303
int res;
304304

305-
if (!repo->index)
306-
CALLOC_ARRAY(repo->index, 1);
305+
if (!repo->index) {
306+
ALLOC_ARRAY(repo->index, 1);
307+
index_state_init(repo->index);
308+
}
307309

308310
/* Complete the double-reference */
309311
if (!repo->index->repo)

revision.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1813,7 +1813,7 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
18131813
worktrees = get_worktrees();
18141814
for (p = worktrees; *p; p++) {
18151815
struct worktree *wt = *p;
1816-
struct index_state istate = { NULL };
1816+
struct index_state istate = INDEX_STATE_INIT;
18171817

18181818
if (wt->is_current)
18191819
continue; /* current index already taken care of */

sparse-index.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ void expand_index(struct index_state *istate, struct pattern_list *pl)
299299
* If the index is already full, then keep it full. We will convert
300300
* it to a sparse index on write, if possible.
301301
*/
302-
if (!istate || istate->sparse_index == INDEX_EXPANDED)
302+
if (istate->sparse_index == INDEX_EXPANDED)
303303
return;
304304

305305
/*
@@ -424,6 +424,8 @@ void expand_index(struct index_state *istate, struct pattern_list *pl)
424424

425425
void ensure_full_index(struct index_state *istate)
426426
{
427+
if (!istate)
428+
BUG("ensure_full_index() must get an index!");
427429
expand_index(istate, NULL);
428430
}
429431

@@ -547,7 +549,7 @@ void expand_to_path(struct index_state *istate,
547549
if (in_expand_to_path)
548550
return;
549551

550-
if (!istate || !istate->sparse_index)
552+
if (!istate->sparse_index)
551553
return;
552554

553555
if (!istate->repo)

split-index.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ void move_cache_to_base_index(struct index_state *istate)
9090
mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool);
9191
}
9292

93-
CALLOC_ARRAY(si->base, 1);
93+
ALLOC_ARRAY(si->base, 1);
94+
index_state_init(si->base);
9495
si->base->version = istate->version;
9596
/* zero timestamp disables racy test in ce_write_index() */
9697
si->base->timestamp = istate->timestamp;

unpack-trees.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,7 +1880,8 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
18801880
prepare_repo_settings(repo);
18811881
if (repo->settings.command_requires_full_index) {
18821882
ensure_full_index(o->src_index);
1883-
ensure_full_index(o->dst_index);
1883+
if (o->dst_index)
1884+
ensure_full_index(o->dst_index);
18841885
}
18851886

18861887
if (o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED &&
@@ -1904,7 +1905,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
19041905
populate_from_existing_patterns(o, &pl);
19051906
}
19061907

1907-
memset(&o->result, 0, sizeof(o->result));
1908+
index_state_init(&o->result);
19081909
o->result.initialized = 1;
19091910
o->result.timestamp.sec = o->src_index->timestamp.sec;
19101911
o->result.timestamp.nsec = o->src_index->timestamp.nsec;

0 commit comments

Comments
 (0)