midx: add repository to `multi_pack_index` struct

The `multi_pack_index` struct represents the MIDX for a repository. Here, we add a pointer to the repository in this struct, allowing direct use of the repository variable without relying on the global `the_repository` struct. With this addition, we can determine the repository associated with a `bitmap_index` struct. A `bitmap_index` points to either a `packed_git` or a `multi_pack_index`, both of which have direct repository references. To support this, we introduce a static helper function, `bitmap_repo`, in `pack-bitmap.c`, which retrieves a repository given a `bitmap_index`. With this, we clear up all usages of `the_repository` within `pack-bitmap.c` and also remove the `USE_THE_REPOSITORY_VARIABLE` definition. Bringing us another step closer to remove all global variable usage. Although this change also opens up the potential to clean up `midx.c`, doing so would require additional refactoring to pass the repository struct to functions where the MIDX struct is created: a task better suited for future patches. Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Dec 3, 2024 at 15:44 UTC e106040722c746346c3c40d3790789d9e196f60d
3 files changed +59 -35
midx.c
+1
@@ -131,6 +131,7 @@ static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir
131 m->data = midx_map;
132 m->data_len = midx_size;
133 m->local = local;
134 + m->repo = the_repository;
135
136 m->signature = get_be32(m->data);
137 if (m->signature != MIDX_SIGNATURE)
midx.h
+3
@@ -71,6 +71,9 @@ struct multi_pack_index {
71
72 const char **pack_names;
73 struct packed_git **packs;
74 +
75 + struct repository *repo;
76 +
77 char object_dir[FLEX_ARRAY];
78 };
79
pack-bitmap.c
+55 -35
@@ -1,5 +1,3 @@
1 -#define USE_THE_REPOSITORY_VARIABLE
2 -
1 #include "git-compat-util.h"
2 #include "commit.h"
3 #include "gettext.h"
@@ -177,12 +175,21 @@ static uint32_t bitmap_num_objects(struct bitmap_index *index)
175 return index->pack->num_objects;
176 }
177
178 +static struct repository *bitmap_repo(struct bitmap_index *bitmap_git)
179 +{
180 + if (bitmap_is_midx(bitmap_git))
181 + return bitmap_git->midx->repo;
182 + return bitmap_git->pack->repo;
183 +}
184 +
185 static int load_bitmap_header(struct bitmap_index *index)
186 {
187 struct bitmap_disk_header *header = (void *)index->map;
183 - size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
188 + const struct git_hash_algo *hash_algo = bitmap_repo(index)->hash_algo;
189 +
190 + size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + hash_algo->rawsz;
191
185 - if (index->map_size < header_size + the_hash_algo->rawsz)
192 + if (index->map_size < header_size + hash_algo->rawsz)
193 return error(_("corrupted bitmap index (too small)"));
194
195 if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
@@ -196,7 +203,7 @@ static int load_bitmap_header(struct bitmap_index *index)
203 {
204 uint32_t flags = ntohs(header->options);
205 size_t cache_size = st_mult(bitmap_num_objects(index), sizeof(uint32_t));
199 - unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
206 + unsigned char *index_end = index->map + index->map_size - hash_algo->rawsz;
207
208 if ((flags & BITMAP_OPT_FULL_DAG) == 0)
209 BUG("unsupported options for bitmap index file "
@@ -409,7 +416,7 @@ static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
416 if (bitmap_git->pack || bitmap_git->midx) {
417 struct strbuf buf = STRBUF_INIT;
418 get_midx_filename(&buf, midx->object_dir);
412 - trace2_data_string("bitmap", the_repository,
419 + trace2_data_string("bitmap", bitmap_repo(bitmap_git),
420 "ignoring extra midx bitmap file", buf.buf);
421 close(fd);
422 strbuf_release(&buf);
@@ -427,7 +434,7 @@ static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
434 goto cleanup;
435
436 if (!hasheq(get_midx_checksum(bitmap_git->midx), bitmap_git->checksum,
430 - the_repository->hash_algo)) {
437 + bitmap_repo(bitmap_git)->hash_algo)) {
438 error(_("checksum doesn't match in MIDX and bitmap"));
439 goto cleanup;
440 }
@@ -438,7 +445,9 @@ static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
445 }
446
447 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
441 - if (prepare_midx_pack(the_repository, bitmap_git->midx, i)) {
448 + if (prepare_midx_pack(bitmap_repo(bitmap_git),
449 + bitmap_git->midx,
450 + i)) {
451 warning(_("could not open pack %s"),
452 bitmap_git->midx->pack_names[i]);
453 goto cleanup;
@@ -492,8 +501,9 @@ static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git
501 }
502
503 if (bitmap_git->pack || bitmap_git->midx) {
495 - trace2_data_string("bitmap", the_repository,
496 - "ignoring extra bitmap file", packfile->pack_name);
504 + trace2_data_string("bitmap", bitmap_repo(bitmap_git),
505 + "ignoring extra bitmap file",
506 + packfile->pack_name);
507 close(fd);
508 return -1;
509 }
@@ -518,8 +528,8 @@ static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git
528 return -1;
529 }
530
521 - trace2_data_string("bitmap", the_repository, "opened bitmap file",
522 - packfile->pack_name);
531 + trace2_data_string("bitmap", bitmap_repo(bitmap_git),
532 + "opened bitmap file", packfile->pack_name);
533 return 0;
534 }
535
@@ -649,7 +659,7 @@ struct bitmap_index *prepare_bitmap_git(struct repository *r)
659
660 struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
661 {
652 - struct repository *r = the_repository;
662 + struct repository *r = midx->repo;
663 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
664
665 if (!open_midx_bitmap_1(bitmap_git, midx) && !load_bitmap(r, bitmap_git))
@@ -1213,6 +1223,7 @@ static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git,
1223 {
1224 struct bitmap_boundary_cb cb;
1225 struct object_list *root;
1226 + struct repository *repo;
1227 unsigned int i;
1228 unsigned int tmp_blobs, tmp_trees, tmp_tags;
1229 int any_missing = 0;
@@ -1222,6 +1233,8 @@ static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git,
1233 cb.base = bitmap_new();
1234 object_array_init(&cb.boundary);
1235
1236 + repo = bitmap_repo(bitmap_git);
1237 +
1238 revs->ignore_missing_links = 1;
1239
1240 if (bitmap_git->pseudo_merges.nr) {
@@ -1280,19 +1293,19 @@ static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git,
1293 * revision walk to (a) OR in any bitmaps that are UNINTERESTING
1294 * between the tips and boundary, and (b) record the boundary.
1295 */
1283 - trace2_region_enter("pack-bitmap", "boundary-prepare", the_repository);
1296 + trace2_region_enter("pack-bitmap", "boundary-prepare", repo);
1297 if (prepare_revision_walk(revs))
1298 die("revision walk setup failed");
1286 - trace2_region_leave("pack-bitmap", "boundary-prepare", the_repository);
1299 + trace2_region_leave("pack-bitmap", "boundary-prepare", repo);
1300
1288 - trace2_region_enter("pack-bitmap", "boundary-traverse", the_repository);
1301 + trace2_region_enter("pack-bitmap", "boundary-traverse", repo);
1302 revs->boundary = 1;
1303 traverse_commit_list_filtered(revs,
1304 show_boundary_commit,
1305 show_boundary_object,
1306 &cb, NULL);
1307 revs->boundary = 0;
1295 - trace2_region_leave("pack-bitmap", "boundary-traverse", the_repository);
1308 + trace2_region_leave("pack-bitmap", "boundary-traverse", repo);
1309
1310 revs->blob_objects = tmp_blobs;
1311 revs->tree_objects = tmp_trees;
@@ -1304,7 +1317,7 @@ static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git,
1317 /*
1318 * Then add the boundary commit(s) as fill-in traversal tips.
1319 */
1307 - trace2_region_enter("pack-bitmap", "boundary-fill-in", the_repository);
1320 + trace2_region_enter("pack-bitmap", "boundary-fill-in", repo);
1321 for (i = 0; i < cb.boundary.nr; i++) {
1322 struct object *obj = cb.boundary.objects[i].item;
1323 if (bitmap_walk_contains(bitmap_git, cb.base, &obj->oid))
@@ -1314,7 +1327,7 @@ static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git,
1327 }
1328 if (revs->pending.nr)
1329 cb.base = fill_in_bitmap(bitmap_git, revs, cb.base, NULL);
1317 - trace2_region_leave("pack-bitmap", "boundary-fill-in", the_repository);
1330 + trace2_region_leave("pack-bitmap", "boundary-fill-in", repo);
1331
1332 cleanup:
1333 object_array_clear(&cb.boundary);
@@ -1718,7 +1731,8 @@ static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1731 ofs = pack_pos_to_offset(pack, pos);
1732 }
1733
1721 - if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
1734 + if (packed_object_info(bitmap_repo(bitmap_git), pack, ofs,
1735 + &oi) < 0) {
1736 struct object_id oid;
1737 nth_bitmap_object_oid(bitmap_git, &oid,
1738 pack_pos_to_index(pack, pos));
@@ -1727,7 +1741,8 @@ static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1741 } else {
1742 struct eindex *eindex = &bitmap_git->ext_index;
1743 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
1730 - if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1744 + if (oid_object_info_extended(bitmap_repo(bitmap_git), &obj->oid,
1745 + &oi, 0) < 0)
1746 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1747 }
1748
@@ -1889,7 +1904,8 @@ static void filter_packed_objects_from_bitmap(struct bitmap_index *bitmap_git,
1904 bitmap_unset(result, i);
1905
1906 for (i = 0; i < eindex->count; ++i) {
1892 - if (has_object_pack(the_repository, &eindex->objects[i]->oid))
1907 + if (has_object_pack(bitmap_repo(bitmap_git),
1908 + &eindex->objects[i]->oid))
1909 bitmap_unset(result, objects_nr + i);
1910 }
1911 }
@@ -1907,6 +1923,7 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1923 struct bitmap *haves_bitmap = NULL;
1924
1925 struct bitmap_index *bitmap_git;
1926 + struct repository *repo;
1927
1928 /*
1929 * We can't do pathspec limiting with bitmaps, because we don't know
@@ -1980,18 +1997,20 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1997 if (!use_boundary_traversal)
1998 object_array_clear(&revs->pending);
1999
2000 + repo = bitmap_repo(bitmap_git);
2001 +
2002 if (haves) {
2003 if (use_boundary_traversal) {
1985 - trace2_region_enter("pack-bitmap", "haves/boundary", the_repository);
2004 + trace2_region_enter("pack-bitmap", "haves/boundary", repo);
2005 haves_bitmap = find_boundary_objects(bitmap_git, revs, haves);
1987 - trace2_region_leave("pack-bitmap", "haves/boundary", the_repository);
2006 + trace2_region_leave("pack-bitmap", "haves/boundary", repo);
2007 } else {
1989 - trace2_region_enter("pack-bitmap", "haves/classic", the_repository);
2008 + trace2_region_enter("pack-bitmap", "haves/classic", repo);
2009 revs->ignore_missing_links = 1;
2010 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
2011 reset_revision_walk();
2012 revs->ignore_missing_links = 0;
1994 - trace2_region_leave("pack-bitmap", "haves/classic", the_repository);
2013 + trace2_region_leave("pack-bitmap", "haves/classic", repo);
2014 }
2015
2016 if (!haves_bitmap)
@@ -2025,17 +2044,17 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
2044 object_list_free(&wants);
2045 object_list_free(&haves);
2046
2028 - trace2_data_intmax("bitmap", the_repository, "pseudo_merges_satisfied",
2047 + trace2_data_intmax("bitmap", repo, "pseudo_merges_satisfied",
2048 pseudo_merges_satisfied_nr);
2030 - trace2_data_intmax("bitmap", the_repository, "pseudo_merges_cascades",
2049 + trace2_data_intmax("bitmap", repo, "pseudo_merges_cascades",
2050 pseudo_merges_cascades_nr);
2032 - trace2_data_intmax("bitmap", the_repository, "bitmap/hits",
2051 + trace2_data_intmax("bitmap", repo, "bitmap/hits",
2052 existing_bitmaps_hits_nr);
2034 - trace2_data_intmax("bitmap", the_repository, "bitmap/misses",
2053 + trace2_data_intmax("bitmap", repo, "bitmap/misses",
2054 existing_bitmaps_misses_nr);
2036 - trace2_data_intmax("bitmap", the_repository, "bitmap/roots_with_bitmap",
2055 + trace2_data_intmax("bitmap", repo, "bitmap/roots_with_bitmap",
2056 roots_with_bitmaps_nr);
2038 - trace2_data_intmax("bitmap", the_repository, "bitmap/roots_without_bitmap",
2057 + trace2_data_intmax("bitmap", repo, "bitmap/roots_without_bitmap",
2058 roots_without_bitmaps_nr);
2059
2060 return bitmap_git;
@@ -2256,7 +2275,7 @@ void reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
2275 struct bitmap **reuse_out,
2276 int multi_pack_reuse)
2277 {
2259 - struct repository *r = the_repository;
2278 + struct repository *r = bitmap_repo(bitmap_git);
2279 struct bitmapped_pack *packs = NULL;
2280 struct bitmap *result = bitmap_git->result;
2281 struct bitmap *reuse;
@@ -2792,7 +2811,7 @@ int rebuild_bitmap(const uint32_t *reposition,
2811 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
2812 struct packing_data *mapping)
2813 {
2795 - struct repository *r = the_repository;
2814 + struct repository *r = bitmap_repo(bitmap_git);
2815 uint32_t i, num_objects;
2816 uint32_t *reposition;
2817
@@ -2948,7 +2967,8 @@ static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
2967 st_add(bitmap_num_objects(bitmap_git), i)))
2968 continue;
2969
2951 - if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
2970 + if (oid_object_info_extended(bitmap_repo(bitmap_git), &obj->oid,
2971 + &oi, 0) < 0)
2972 die(_("unable to get disk usage of '%s'"),
2973 oid_to_hex(&obj->oid));
2974