repository: add repo reference to index_state

It will be helpful to add behavior to index operations that might trigger an object lookup. Since each index belongs to a specific repository, add a 'repo' pointer to struct index_state that allows access to this repository. Add a BUG() statement if the repo already has an index, and the index already has a repo, but somehow the index points to a different repo. This will prevent future changes from needing to pass an additional 'struct repository *repo' parameter and instead rely only on the 'struct index_state *istate' parameter. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jan 23, 2021 at 19:58 UTC 1fd9ae517c45294a01466aad0d38f99c7893d814
2 files changed +7
cache.h
+1
@@ -328,6 +328,7 @@ struct index_state {
328 struct ewah_bitmap *fsmonitor_dirty;
329 struct mem_pool *ce_mem_pool;
330 struct progress *progress;
331 + struct repository *repo;
332 };
333
334 /* Name hashing */
repository.c
+6
@@ -264,6 +264,12 @@ int repo_read_index(struct repository *repo)
264 if (!repo->index)
265 repo->index = xcalloc(1, sizeof(*repo->index));
266
267 + /* Complete the double-reference */
268 + if (!repo->index->repo)
269 + repo->index->repo = repo;
270 + else if (repo->index->repo != repo)
271 + BUG("repo's index should point back at itself");
272 +
273 return read_index_from(repo->index, repo->index_file, repo->gitdir);
274 }
275