From 6269f8eaad054a02517f5e03873726e84a032d8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Tue, 17 Jan 2023 14:57:00 +0100 Subject: treewide: always have a valid "index_state.repo" member MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. 1fd9ae517c4 (repository: add repo reference to index_state, 2021-01-23) 2. ee1f0c242ef (read-cache: add index.skipHash config option, 2023-01-06) 3. 2f6b1eb794e (cache API: add a "INDEX_STATE_INIT" macro/function, add release_index(), 2023-01-12) 4. 1e0ea5c4316 (fsmonitor: config settings are repository-specific, 2022-03-25) Signed-off-by: Ævar Arnfjörð Bjarmason Acked-by: Derrick Stolee Signed-off-by: Junio C Hamano --- read-cache.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) (limited to 'read-cache.c') diff --git a/read-cache.c b/read-cache.c index d191741f60..7bd12afb38 100644 --- a/read-cache.c +++ b/read-cache.c @@ -2300,8 +2300,6 @@ static void set_new_index_sparsity(struct index_state *istate) * If the index's repo exists, mark it sparse according to * repo settings. */ - if (!istate->repo) - return; prepare_repo_settings(istate->repo); if (!istate->repo->settings.command_requires_full_index && is_sparse_index_allowed(istate, 0)) @@ -2330,8 +2328,6 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist) fd = open(path, O_RDONLY); if (fd < 0) { if (!must_exist && errno == ENOENT) { - if (!istate->repo) - istate->repo = the_repository; set_new_index_sparsity(istate); return 0; } @@ -2433,9 +2429,6 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist) trace2_data_intmax("index", the_repository, "read/cache_nr", istate->cache_nr); - if (!istate->repo) - istate->repo = the_repository; - /* * If the command explicitly requires a full index, force it * 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, release_index(split_index->base); else ALLOC_ARRAY(split_index->base, 1); - index_state_init(split_index->base); + index_state_init(split_index->base, istate->repo); base_oid_hex = oid_to_hex(&split_index->base_oid); base_path = xstrfmt("%s/sharedindex.%s", gitdir, base_oid_hex); @@ -2540,9 +2533,9 @@ int is_index_unborn(struct index_state *istate) return (!istate->cache_nr && !istate->timestamp.sec); } -void index_state_init(struct index_state *istate) +void index_state_init(struct index_state *istate, struct repository *r) { - struct index_state blank = INDEX_STATE_INIT; + struct index_state blank = INDEX_STATE_INIT(r); memcpy(istate, &blank, sizeof(*istate)); } @@ -2579,7 +2572,7 @@ void release_index(struct index_state *istate) void discard_index(struct index_state *istate) { release_index(istate); - index_state_init(istate); + index_state_init(istate, istate->repo); } /* @@ -2933,7 +2926,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile, int ieot_entries = 1; struct index_entry_offset_table *ieot = NULL; int nr, nr_threads; - struct repository *r = istate->repo ? istate->repo : the_repository; + struct repository *r = istate->repo; f = hashfd(tempfile->fd, tempfile->filename.buf); -- cgit v1.2.3