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. 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 <avarab@gmail.com> Acked-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jan 17, 2023 at 14:57 UTC 6269f8eaad054a02517f5e03873726e84a032d8e
15 files changed +30 -58
apply.c
+1 -1
@@ -4105,7 +4105,7 @@ static int preimage_oid_in_gitlink_patch(struct patch *p, struct object_id *oid)
4105 static int build_fake_ancestor(struct apply_state *state, struct patch *list)
4106 {
4107 struct patch *patch;
4108 - struct index_state result = INDEX_STATE_INIT;
4108 + struct index_state result = INDEX_STATE_INIT(state->repo);
4109 struct lock_file lock = LOCK_INIT;
4110 int res;
4111
builtin/difftool.c
+1 -1
@@ -361,7 +361,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
361 struct hashmap symlinks2 = HASHMAP_INIT(pair_cmp, NULL);
362 struct hashmap_iter iter;
363 struct pair_entry *entry;
364 - struct index_state wtindex = INDEX_STATE_INIT;
364 + struct index_state wtindex = INDEX_STATE_INIT(the_repository);
365 struct checkout lstate, rstate;
366 int err = 0;
367 struct child_process cmd = CHILD_PROCESS_INIT;
builtin/sparse-checkout.c
+1 -1
@@ -217,7 +217,7 @@ static int update_working_directory(struct pattern_list *pl)
217 o.head_idx = -1;
218 o.src_index = r->index;
219 o.dst_index = r->index;
220 - index_state_init(&o.result);
220 + index_state_init(&o.result, r);
221 o.skip_sparse_checkout = 0;
222 o.pl = pl;
223
builtin/stash.c
+4 -4
@@ -1137,7 +1137,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
1137 int ret = 0;
1138 struct strbuf untracked_msg = STRBUF_INIT;
1139 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);
1141
1142 cp_upd_index.git_cmd = 1;
1143 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,
1176 {
1177 int ret = 0;
1178 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);
1180
1181 if (write_index_as_tree(&info->w_tree, &istate, the_repository->index_file,
1182 0, NULL)) {
@@ -1209,7 +1209,7 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
1209 int ret = 0;
1210 struct child_process cp_read_tree = CHILD_PROCESS_INIT;
1211 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);
1213 char *old_index_env = NULL, *old_repo_index_file;
1214
1215 remove_path(stash_index_path.buf);
@@ -1271,7 +1271,7 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps
1271 struct rev_info rev;
1272 struct child_process cp_upd_index = CHILD_PROCESS_INIT;
1273 struct strbuf diff_output = STRBUF_INIT;
1274 - struct index_state istate = INDEX_STATE_INIT;
1274 + struct index_state istate = INDEX_STATE_INIT(the_repository);
1275
1276 init_revisions(&rev, NULL);
1277 copy_pathspec(&rev.prune_data, ps);
builtin/worktree.c
+1 -1
@@ -923,7 +923,7 @@ static int unlock_worktree(int ac, const char **av, const char *prefix)
923
924 static void validate_no_submodules(const struct worktree *wt)
925 {
926 - struct index_state istate = INDEX_STATE_INIT;
926 + struct index_state istate = INDEX_STATE_INIT(the_repository);
927 struct strbuf path = STRBUF_INIT;
928 int i, found_submodules = 0;
929
cache.h
+6 -3
@@ -367,10 +367,13 @@ struct index_state {
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.
370 + * the end. The discard_index() will use its own "istate->repo" as the
371 + * "r" argument to index_state_init() in that case.
372 */
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);
377 void release_index(struct index_state *istate);
378
379 /* Name hashing */
fsmonitor-settings.c
-14
@@ -143,8 +143,6 @@ static void lookup_fsmonitor_settings(struct repository *r)
143
144 enum fsmonitor_mode fsm_settings__get_mode(struct repository *r)
145 {
146 - if (!r)
147 - r = the_repository;
146 if (!r->settings.fsmonitor)
147 lookup_fsmonitor_settings(r);
148
@@ -153,8 +151,6 @@ enum fsmonitor_mode fsm_settings__get_mode(struct repository *r)
151
152 const char *fsm_settings__get_hook_path(struct repository *r)
153 {
156 - if (!r)
157 - r = the_repository;
154 if (!r->settings.fsmonitor)
155 lookup_fsmonitor_settings(r);
156
@@ -174,8 +170,6 @@ void fsm_settings__set_ipc(struct repository *r)
170 * Caller requested IPC explicitly, so avoid (possibly
171 * recursive) config lookup.
172 */
177 - if (!r)
178 - r = the_repository;
173 if (!r->settings.fsmonitor)
174 r->settings.fsmonitor = alloc_settings();
175
@@ -197,8 +191,6 @@ void fsm_settings__set_hook(struct repository *r, const char *path)
191 * Caller requested hook explicitly, so avoid (possibly
192 * recursive) config lookup.
193 */
200 - if (!r)
201 - r = the_repository;
194 if (!r->settings.fsmonitor)
195 r->settings.fsmonitor = alloc_settings();
196
@@ -210,8 +202,6 @@ void fsm_settings__set_hook(struct repository *r, const char *path)
202
203 void fsm_settings__set_disabled(struct repository *r)
204 {
213 - if (!r)
214 - r = the_repository;
205 if (!r->settings.fsmonitor)
206 r->settings.fsmonitor = alloc_settings();
207
@@ -223,8 +213,6 @@ void fsm_settings__set_disabled(struct repository *r)
213 void fsm_settings__set_incompatible(struct repository *r,
214 enum fsmonitor_reason reason)
215 {
226 - if (!r)
227 - r = the_repository;
216 if (!r->settings.fsmonitor)
217 r->settings.fsmonitor = alloc_settings();
218
@@ -235,8 +223,6 @@ void fsm_settings__set_incompatible(struct repository *r,
223
224 enum fsmonitor_reason fsm_settings__get_reason(struct repository *r)
225 {
238 - if (!r)
239 - r = the_repository;
226 if (!r->settings.fsmonitor)
227 lookup_fsmonitor_settings(r);
228
fsmonitor.c
+1 -1
@@ -304,7 +304,7 @@ void refresh_fsmonitor(struct index_state *istate)
304 char *buf;
305 unsigned int i;
306 int is_trivial = 0;
307 - struct repository *r = istate->repo ? istate->repo : the_repository;
307 + struct repository *r = istate->repo;
308 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r);
309 enum fsmonitor_reason reason = fsm_settings__get_reason(r);
310
merge-recursive.c
+1 -1
@@ -412,7 +412,7 @@ static int unpack_trees_start(struct merge_options *opt,
412 {
413 int rc;
414 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);
416
417 memset(&opt->priv->unpack_opts, 0, sizeof(opt->priv->unpack_opts));
418 if (opt->priv->call_depth)
read-cache.c
+5 -12
@@ -2300,8 +2300,6 @@ static void set_new_index_sparsity(struct index_state *istate)
2300 * If the index's repo exists, mark it sparse according to
2301 * repo settings.
2302 */
2303 - if (!istate->repo)
2304 - return;
2303 prepare_repo_settings(istate->repo);
2304 if (!istate->repo->settings.command_requires_full_index &&
2305 is_sparse_index_allowed(istate, 0))
@@ -2330,8 +2328,6 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
2328 fd = open(path, O_RDONLY);
2329 if (fd < 0) {
2330 if (!must_exist && errno == ENOENT) {
2333 - if (!istate->repo)
2334 - istate->repo = the_repository;
2331 set_new_index_sparsity(istate);
2332 return 0;
2333 }
@@ -2433,9 +2429,6 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
2429 trace2_data_intmax("index", the_repository, "read/cache_nr",
2430 istate->cache_nr);
2431
2436 - if (!istate->repo)
2437 - istate->repo = the_repository;
2438 -
2432 /*
2433 * If the command explicitly requires a full index, force it
2434 * 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,
2494 release_index(split_index->base);
2495 else
2496 ALLOC_ARRAY(split_index->base, 1);
2504 - index_state_init(split_index->base);
2497 + index_state_init(split_index->base, istate->repo);
2498
2499 base_oid_hex = oid_to_hex(&split_index->base_oid);
2500 base_path = xstrfmt("%s/sharedindex.%s", gitdir, base_oid_hex);
@@ -2540,9 +2533,9 @@ int is_index_unborn(struct index_state *istate)
2533 return (!istate->cache_nr && !istate->timestamp.sec);
2534 }
2535
2543 -void index_state_init(struct index_state *istate)
2536 +void index_state_init(struct index_state *istate, struct repository *r)
2537 {
2545 - struct index_state blank = INDEX_STATE_INIT;
2538 + struct index_state blank = INDEX_STATE_INIT(r);
2539 memcpy(istate, &blank, sizeof(*istate));
2540 }
2541
@@ -2579,7 +2572,7 @@ void release_index(struct index_state *istate)
2572 void discard_index(struct index_state *istate)
2573 {
2574 release_index(istate);
2582 - index_state_init(istate);
2575 + index_state_init(istate, istate->repo);
2576 }
2577
2578 /*
@@ -2933,7 +2926,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
2926 int ieot_entries = 1;
2927 struct index_entry_offset_table *ieot = NULL;
2928 int nr, nr_threads;
2936 - struct repository *r = istate->repo ? istate->repo : the_repository;
2929 + struct repository *r = istate->repo;
2930
2931 f = hashfd(tempfile->fd, tempfile->filename.buf);
2932
repository.c
+6 -7
@@ -28,6 +28,8 @@ void initialize_the_repository(void)
28 the_repo.remote_state = remote_state_new();
29 the_repo.parsed_objects = parsed_object_pool_new();
30
31 + index_state_init(&the_index, the_repository);
32 +
33 repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
34 }
35
@@ -302,16 +304,13 @@ int repo_read_index(struct repository *repo)
304 {
305 int res;
306
307 + /* Complete the double-reference */
308 if (!repo->index) {
309 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) {
312 BUG("repo's index should point back at itself");
313 + }
314
315 res = read_index_from(repo->index, repo->index_file, repo->gitdir);
316
revision.c
+1 -1
@@ -1813,7 +1813,7 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1813 worktrees = get_worktrees();
1814 for (p = worktrees; *p; p++) {
1815 struct worktree *wt = *p;
1816 - struct index_state istate = INDEX_STATE_INIT;
1816 + struct index_state istate = INDEX_STATE_INIT(revs->repo);
1817
1818 if (wt->is_current)
1819 continue; /* current index already taken care of */
sparse-index.c
-9
@@ -128,9 +128,6 @@ int is_sparse_index_allowed(struct index_state *istate, int flags)
128 if (!core_apply_sparse_checkout || !core_sparse_checkout_cone)
129 return 0;
130
131 - if (!istate->repo)
132 - istate->repo = the_repository;
133 -
131 if (!(flags & SPARSE_INDEX_MEMORY_ONLY)) {
132 int test_env;
133
@@ -327,9 +324,6 @@ void expand_index(struct index_state *istate, struct pattern_list *pl)
324 pl = NULL;
325 }
326
330 - if (!istate->repo)
331 - istate->repo = the_repository;
332 -
327 /*
328 * A NULL pattern set indicates we are expanding a full index, so
329 * we use a special region name that indicates the full expansion.
@@ -552,9 +546,6 @@ void expand_to_path(struct index_state *istate,
546 if (!istate->sparse_index)
547 return;
548
555 - if (!istate->repo)
556 - istate->repo = the_repository;
557 -
549 in_expand_to_path = 1;
550
551 /*
split-index.c
+1 -1
@@ -91,7 +91,7 @@ void move_cache_to_base_index(struct index_state *istate)
91 }
92
93 ALLOC_ARRAY(si->base, 1);
94 - index_state_init(si->base);
94 + index_state_init(si->base, istate->repo);
95 si->base->version = istate->version;
96 /* zero timestamp disables racy test in ce_write_index() */
97 si->base->timestamp = istate->timestamp;
unpack-trees.c
+1 -1
@@ -1905,7 +1905,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
1905 populate_from_existing_patterns(o, &pl);
1906 }
1907
1908 - index_state_init(&o->result);
1908 + index_state_init(&o->result, o->src_index->repo);
1909 o->result.initialized = 1;
1910 o->result.timestamp.sec = o->src_index->timestamp.sec;
1911 o->result.timestamp.nsec = o->src_index->timestamp.nsec;