Skip to content

Commit 6269f8e

Browse files
avargitster
authored andcommitted
treewide: always have a valid "index_state.repo" member
When the "repo" member was added to "the_index" in [1] the repo_read_index() was made to populate it, but the unpopulated "the_index" variable didn't get the same treatment. Let's do that in initialize_the_repository() when we set it up, and likewise for all of the current callers initialized an empty "struct index_state". This simplifies code that needs to deal with "the_index" or a custom "struct index_state", we no longer need to second-guess this part of the "index_state" deep in the stack. A recent example of such second-guessing is the "istate->repo ? istate->repo : the_repository" code in [2]. We can now simply use "istate->repo". We're doing this by making use of the INDEX_STATE_INIT() macro (and corresponding function) added in [3], which now have mandatory "repo" arguments. Because we now call index_state_init() in repository.c's initialize_the_repository() we don't need to handle the case where we have a "repo->index" whose "repo" member doesn't match the "repo" we're setting up, i.e. the "Complete the double-reference" code in repo_read_index() being altered here. That logic was originally added in [1], and was working around the lack of what we now have in initialize_the_repository(). For "fsmonitor-settings.c" we can remove the initialization of a NULL "r" argument to "the_repository". This was added back in [4], and was needed at the time for callers that would pass us the "r" from an "istate->repo". Before this change such a change to "fsmonitor-settings.c" would segfault all over the test suite (e.g. in t0002-gitfile.sh). This change has wider eventual implications for "fsmonitor-settings.c". The reason the other lazy loading behavior in it is required (starting with "if (!r->settings.fsmonitor) ..." is because of the previously passed "r" being "NULL". I have other local changes on top of this which move its configuration reading to "prepare_repo_settings()" in "repo-settings.c", as we could now start to rely on it being called for our "r". But let's leave all of that for now, and narrowly remove this particular part of the lazy-loading. 1. 1fd9ae5 (repository: add repo reference to index_state, 2021-01-23) 2. ee1f0c2 (read-cache: add index.skipHash config option, 2023-01-06) 3. 2f6b1eb (cache API: add a "INDEX_STATE_INIT" macro/function, add release_index(), 2023-01-12) 4. 1e0ea5c (fsmonitor: config settings are repository-specific, 2022-03-25) Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Acked-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dc71be4 commit 6269f8e

15 files changed

+30
-58
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 = INDEX_STATE_INIT;
4108+
struct index_state result = INDEX_STATE_INIT(state->repo);
41094109
struct lock_file lock = LOCK_INIT;
41104110
int res;
41114111

builtin/difftool.c

Lines changed: 1 addition & 1 deletion
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 = INDEX_STATE_INIT;
364+
struct index_state wtindex = INDEX_STATE_INIT(the_repository);
365365
struct checkout lstate, rstate;
366366
int err = 0;
367367
struct child_process cmd = CHILD_PROCESS_INIT;

builtin/sparse-checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +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);
220+
index_state_init(&o.result, r);
221221
o.skip_sparse_checkout = 0;
222222
o.pl = pl;
223223

builtin/stash.c

Lines changed: 4 additions & 4 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 = INDEX_STATE_INIT;
1140+
struct index_state istate = INDEX_STATE_INIT(the_repository);
11411141

11421142
cp_upd_index.git_cmd = 1;
11431143
strvec_pushl(&cp_upd_index.args, "update-index", "-z", "--add",
@@ -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 = INDEX_STATE_INIT;
1179+
struct index_state istate = INDEX_STATE_INIT(the_repository);
11801180

11811181
if (write_index_as_tree(&info->w_tree, &istate, the_repository->index_file,
11821182
0, NULL)) {
@@ -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 = INDEX_STATE_INIT;
1212+
struct index_state istate = INDEX_STATE_INIT(the_repository);
12131213
char *old_index_env = NULL, *old_repo_index_file;
12141214

12151215
remove_path(stash_index_path.buf);
@@ -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 = INDEX_STATE_INIT;
1274+
struct index_state istate = INDEX_STATE_INIT(the_repository);
12751275

12761276
init_revisions(&rev, NULL);
12771277
copy_pathspec(&rev.prune_data, ps);

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 = INDEX_STATE_INIT;
926+
struct index_state istate = INDEX_STATE_INIT(the_repository);
927927
struct strbuf path = STRBUF_INIT;
928928
int i, found_submodules = 0;
929929

cache.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,13 @@ struct index_state {
367367
* If the variable won't be used again, use release_index() to free()
368368
* its resources. If it needs to be used again use discard_index(),
369369
* which does the same thing, but will use use index_state_init() at
370-
* the end.
370+
* the end. The discard_index() will use its own "istate->repo" as the
371+
* "r" argument to index_state_init() in that case.
371372
*/
372-
#define INDEX_STATE_INIT { 0 }
373-
void index_state_init(struct index_state *istate);
373+
#define INDEX_STATE_INIT(r) { \
374+
.repo = (r), \
375+
}
376+
void index_state_init(struct index_state *istate, struct repository *r);
374377
void release_index(struct index_state *istate);
375378

376379
/* Name hashing */

fsmonitor-settings.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,6 @@ static void lookup_fsmonitor_settings(struct repository *r)
143143

144144
enum fsmonitor_mode fsm_settings__get_mode(struct repository *r)
145145
{
146-
if (!r)
147-
r = the_repository;
148146
if (!r->settings.fsmonitor)
149147
lookup_fsmonitor_settings(r);
150148

@@ -153,8 +151,6 @@ enum fsmonitor_mode fsm_settings__get_mode(struct repository *r)
153151

154152
const char *fsm_settings__get_hook_path(struct repository *r)
155153
{
156-
if (!r)
157-
r = the_repository;
158154
if (!r->settings.fsmonitor)
159155
lookup_fsmonitor_settings(r);
160156

@@ -174,8 +170,6 @@ void fsm_settings__set_ipc(struct repository *r)
174170
* Caller requested IPC explicitly, so avoid (possibly
175171
* recursive) config lookup.
176172
*/
177-
if (!r)
178-
r = the_repository;
179173
if (!r->settings.fsmonitor)
180174
r->settings.fsmonitor = alloc_settings();
181175

@@ -197,8 +191,6 @@ void fsm_settings__set_hook(struct repository *r, const char *path)
197191
* Caller requested hook explicitly, so avoid (possibly
198192
* recursive) config lookup.
199193
*/
200-
if (!r)
201-
r = the_repository;
202194
if (!r->settings.fsmonitor)
203195
r->settings.fsmonitor = alloc_settings();
204196

@@ -210,8 +202,6 @@ void fsm_settings__set_hook(struct repository *r, const char *path)
210202

211203
void fsm_settings__set_disabled(struct repository *r)
212204
{
213-
if (!r)
214-
r = the_repository;
215205
if (!r->settings.fsmonitor)
216206
r->settings.fsmonitor = alloc_settings();
217207

@@ -223,8 +213,6 @@ void fsm_settings__set_disabled(struct repository *r)
223213
void fsm_settings__set_incompatible(struct repository *r,
224214
enum fsmonitor_reason reason)
225215
{
226-
if (!r)
227-
r = the_repository;
228216
if (!r->settings.fsmonitor)
229217
r->settings.fsmonitor = alloc_settings();
230218

@@ -235,8 +223,6 @@ void fsm_settings__set_incompatible(struct repository *r,
235223

236224
enum fsmonitor_reason fsm_settings__get_reason(struct repository *r)
237225
{
238-
if (!r)
239-
r = the_repository;
240226
if (!r->settings.fsmonitor)
241227
lookup_fsmonitor_settings(r);
242228

fsmonitor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ void refresh_fsmonitor(struct index_state *istate)
304304
char *buf;
305305
unsigned int i;
306306
int is_trivial = 0;
307-
struct repository *r = istate->repo ? istate->repo : the_repository;
307+
struct repository *r = istate->repo;
308308
enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r);
309309
enum fsmonitor_reason reason = fsm_settings__get_reason(r);
310310

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 = INDEX_STATE_INIT;
415+
struct index_state tmp_index = INDEX_STATE_INIT(opt->repo);
416416

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

read-cache.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,8 +2300,6 @@ 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-
return;
23052303
prepare_repo_settings(istate->repo);
23062304
if (!istate->repo->settings.command_requires_full_index &&
23072305
is_sparse_index_allowed(istate, 0))
@@ -2330,8 +2328,6 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
23302328
fd = open(path, O_RDONLY);
23312329
if (fd < 0) {
23322330
if (!must_exist && errno == ENOENT) {
2333-
if (!istate->repo)
2334-
istate->repo = the_repository;
23352331
set_new_index_sparsity(istate);
23362332
return 0;
23372333
}
@@ -2433,9 +2429,6 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
24332429
trace2_data_intmax("index", the_repository, "read/cache_nr",
24342430
istate->cache_nr);
24352431

2436-
if (!istate->repo)
2437-
istate->repo = the_repository;
2438-
24392432
/*
24402433
* If the command explicitly requires a full index, force it
24412434
* to be full. Otherwise, correct the sparsity based on repository
@@ -2501,7 +2494,7 @@ int read_index_from(struct index_state *istate, const char *path,
25012494
release_index(split_index->base);
25022495
else
25032496
ALLOC_ARRAY(split_index->base, 1);
2504-
index_state_init(split_index->base);
2497+
index_state_init(split_index->base, istate->repo);
25052498

25062499
base_oid_hex = oid_to_hex(&split_index->base_oid);
25072500
base_path = xstrfmt("%s/sharedindex.%s", gitdir, base_oid_hex);
@@ -2540,9 +2533,9 @@ int is_index_unborn(struct index_state *istate)
25402533
return (!istate->cache_nr && !istate->timestamp.sec);
25412534
}
25422535

2543-
void index_state_init(struct index_state *istate)
2536+
void index_state_init(struct index_state *istate, struct repository *r)
25442537
{
2545-
struct index_state blank = INDEX_STATE_INIT;
2538+
struct index_state blank = INDEX_STATE_INIT(r);
25462539
memcpy(istate, &blank, sizeof(*istate));
25472540
}
25482541

@@ -2579,7 +2572,7 @@ void release_index(struct index_state *istate)
25792572
void discard_index(struct index_state *istate)
25802573
{
25812574
release_index(istate);
2582-
index_state_init(istate);
2575+
index_state_init(istate, istate->repo);
25832576
}
25842577

25852578
/*
@@ -2933,7 +2926,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
29332926
int ieot_entries = 1;
29342927
struct index_entry_offset_table *ieot = NULL;
29352928
int nr, nr_threads;
2936-
struct repository *r = istate->repo ? istate->repo : the_repository;
2929+
struct repository *r = istate->repo;
29372930

29382931
f = hashfd(tempfile->fd, tempfile->filename.buf);
29392932

repository.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ void initialize_the_repository(void)
2828
the_repo.remote_state = remote_state_new();
2929
the_repo.parsed_objects = parsed_object_pool_new();
3030

31+
index_state_init(&the_index, the_repository);
32+
3133
repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
3234
}
3335

@@ -302,16 +304,13 @@ int repo_read_index(struct repository *repo)
302304
{
303305
int res;
304306

307+
/* Complete the double-reference */
305308
if (!repo->index) {
306309
ALLOC_ARRAY(repo->index, 1);
307-
index_state_init(repo->index);
308-
}
309-
310-
/* Complete the double-reference */
311-
if (!repo->index->repo)
312-
repo->index->repo = repo;
313-
else if (repo->index->repo != repo)
310+
index_state_init(repo->index, repo);
311+
} else if (repo->index->repo != repo) {
314312
BUG("repo's index should point back at itself");
313+
}
315314

316315
res = read_index_from(repo->index, repo->index_file, repo->gitdir);
317316

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 = INDEX_STATE_INIT;
1816+
struct index_state istate = INDEX_STATE_INIT(revs->repo);
18171817

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

sparse-index.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,6 @@ int is_sparse_index_allowed(struct index_state *istate, int flags)
128128
if (!core_apply_sparse_checkout || !core_sparse_checkout_cone)
129129
return 0;
130130

131-
if (!istate->repo)
132-
istate->repo = the_repository;
133-
134131
if (!(flags & SPARSE_INDEX_MEMORY_ONLY)) {
135132
int test_env;
136133

@@ -327,9 +324,6 @@ void expand_index(struct index_state *istate, struct pattern_list *pl)
327324
pl = NULL;
328325
}
329326

330-
if (!istate->repo)
331-
istate->repo = the_repository;
332-
333327
/*
334328
* A NULL pattern set indicates we are expanding a full index, so
335329
* we use a special region name that indicates the full expansion.
@@ -552,9 +546,6 @@ void expand_to_path(struct index_state *istate,
552546
if (!istate->sparse_index)
553547
return;
554548

555-
if (!istate->repo)
556-
istate->repo = the_repository;
557-
558549
in_expand_to_path = 1;
559550

560551
/*

split-index.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void move_cache_to_base_index(struct index_state *istate)
9191
}
9292

9393
ALLOC_ARRAY(si->base, 1);
94-
index_state_init(si->base);
94+
index_state_init(si->base, istate->repo);
9595
si->base->version = istate->version;
9696
/* zero timestamp disables racy test in ce_write_index() */
9797
si->base->timestamp = istate->timestamp;

unpack-trees.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1905,7 +1905,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
19051905
populate_from_existing_patterns(o, &pl);
19061906
}
19071907

1908-
index_state_init(&o->result);
1908+
index_state_init(&o->result, o->src_index->repo);
19091909
o->result.initialized = 1;
19101910
o->result.timestamp.sec = o->src_index->timestamp.sec;
19111911
o->result.timestamp.nsec = o->src_index->timestamp.nsec;

0 commit comments

Comments
 (0)