Skip to content

Commit 762f8f2

Browse files
KarthikNayakgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bd39788 commit 762f8f2

File tree

3 files changed

+59
-35
lines changed

3 files changed

+59
-35
lines changed

midx.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir
131131
m->data = midx_map;
132132
m->data_len = midx_size;
133133
m->local = local;
134+
m->repo = the_repository;
134135

135136
m->signature = get_be32(m->data);
136137
if (m->signature != MIDX_SIGNATURE)

midx.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ struct multi_pack_index {
7171

7272
const char **pack_names;
7373
struct packed_git **packs;
74+
75+
struct repository *repo;
76+
7477
char object_dir[FLEX_ARRAY];
7578
};
7679

pack-bitmap.c

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#define USE_THE_REPOSITORY_VARIABLE
2-
31
#include "git-compat-util.h"
42
#include "commit.h"
53
#include "gettext.h"
@@ -177,12 +175,21 @@ static uint32_t bitmap_num_objects(struct bitmap_index *index)
177175
return index->pack->num_objects;
178176
}
179177

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+
180185
static int load_bitmap_header(struct bitmap_index *index)
181186
{
182187
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;
184191

185-
if (index->map_size < header_size + the_hash_algo->rawsz)
192+
if (index->map_size < header_size + hash_algo->rawsz)
186193
return error(_("corrupted bitmap index (too small)"));
187194

188195
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)
196203
{
197204
uint32_t flags = ntohs(header->options);
198205
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;
200207

201208
if ((flags & BITMAP_OPT_FULL_DAG) == 0)
202209
BUG("unsupported options for bitmap index file "
@@ -409,7 +416,7 @@ static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
409416
if (bitmap_git->pack || bitmap_git->midx) {
410417
struct strbuf buf = STRBUF_INIT;
411418
get_midx_filename(&buf, midx->object_dir);
412-
trace2_data_string("bitmap", the_repository,
419+
trace2_data_string("bitmap", bitmap_repo(bitmap_git),
413420
"ignoring extra midx bitmap file", buf.buf);
414421
close(fd);
415422
strbuf_release(&buf);
@@ -427,7 +434,7 @@ static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
427434
goto cleanup;
428435

429436
if (!hasheq(get_midx_checksum(bitmap_git->midx), bitmap_git->checksum,
430-
the_repository->hash_algo)) {
437+
bitmap_repo(bitmap_git)->hash_algo)) {
431438
error(_("checksum doesn't match in MIDX and bitmap"));
432439
goto cleanup;
433440
}
@@ -438,7 +445,9 @@ static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
438445
}
439446

440447
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)) {
442451
warning(_("could not open pack %s"),
443452
bitmap_git->midx->pack_names[i]);
444453
goto cleanup;
@@ -492,8 +501,9 @@ static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git
492501
}
493502

494503
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);
497507
close(fd);
498508
return -1;
499509
}
@@ -518,8 +528,8 @@ static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git
518528
return -1;
519529
}
520530

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);
523533
return 0;
524534
}
525535

@@ -649,7 +659,7 @@ struct bitmap_index *prepare_bitmap_git(struct repository *r)
649659

650660
struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
651661
{
652-
struct repository *r = the_repository;
662+
struct repository *r = midx->repo;
653663
struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
654664

655665
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,
12131223
{
12141224
struct bitmap_boundary_cb cb;
12151225
struct object_list *root;
1226+
struct repository *repo;
12161227
unsigned int i;
12171228
unsigned int tmp_blobs, tmp_trees, tmp_tags;
12181229
int any_missing = 0;
@@ -1222,6 +1233,8 @@ static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git,
12221233
cb.base = bitmap_new();
12231234
object_array_init(&cb.boundary);
12241235

1236+
repo = bitmap_repo(bitmap_git);
1237+
12251238
revs->ignore_missing_links = 1;
12261239

12271240
if (bitmap_git->pseudo_merges.nr) {
@@ -1280,19 +1293,19 @@ static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git,
12801293
* revision walk to (a) OR in any bitmaps that are UNINTERESTING
12811294
* between the tips and boundary, and (b) record the boundary.
12821295
*/
1283-
trace2_region_enter("pack-bitmap", "boundary-prepare", the_repository);
1296+
trace2_region_enter("pack-bitmap", "boundary-prepare", repo);
12841297
if (prepare_revision_walk(revs))
12851298
die("revision walk setup failed");
1286-
trace2_region_leave("pack-bitmap", "boundary-prepare", the_repository);
1299+
trace2_region_leave("pack-bitmap", "boundary-prepare", repo);
12871300

1288-
trace2_region_enter("pack-bitmap", "boundary-traverse", the_repository);
1301+
trace2_region_enter("pack-bitmap", "boundary-traverse", repo);
12891302
revs->boundary = 1;
12901303
traverse_commit_list_filtered(revs,
12911304
show_boundary_commit,
12921305
show_boundary_object,
12931306
&cb, NULL);
12941307
revs->boundary = 0;
1295-
trace2_region_leave("pack-bitmap", "boundary-traverse", the_repository);
1308+
trace2_region_leave("pack-bitmap", "boundary-traverse", repo);
12961309

12971310
revs->blob_objects = tmp_blobs;
12981311
revs->tree_objects = tmp_trees;
@@ -1304,7 +1317,7 @@ static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git,
13041317
/*
13051318
* Then add the boundary commit(s) as fill-in traversal tips.
13061319
*/
1307-
trace2_region_enter("pack-bitmap", "boundary-fill-in", the_repository);
1320+
trace2_region_enter("pack-bitmap", "boundary-fill-in", repo);
13081321
for (i = 0; i < cb.boundary.nr; i++) {
13091322
struct object *obj = cb.boundary.objects[i].item;
13101323
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,
13141327
}
13151328
if (revs->pending.nr)
13161329
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);
13181331

13191332
cleanup:
13201333
object_array_clear(&cb.boundary);
@@ -1718,7 +1731,8 @@ static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
17181731
ofs = pack_pos_to_offset(pack, pos);
17191732
}
17201733

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) {
17221736
struct object_id oid;
17231737
nth_bitmap_object_oid(bitmap_git, &oid,
17241738
pack_pos_to_index(pack, pos));
@@ -1727,7 +1741,8 @@ static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
17271741
} else {
17281742
struct eindex *eindex = &bitmap_git->ext_index;
17291743
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)
17311746
die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
17321747
}
17331748

@@ -1889,7 +1904,8 @@ static void filter_packed_objects_from_bitmap(struct bitmap_index *bitmap_git,
18891904
bitmap_unset(result, i);
18901905

18911906
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))
18931909
bitmap_unset(result, objects_nr + i);
18941910
}
18951911
}
@@ -1907,6 +1923,7 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
19071923
struct bitmap *haves_bitmap = NULL;
19081924

19091925
struct bitmap_index *bitmap_git;
1926+
struct repository *repo;
19101927

19111928
/*
19121929
* 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,
19801997
if (!use_boundary_traversal)
19811998
object_array_clear(&revs->pending);
19821999

2000+
repo = bitmap_repo(bitmap_git);
2001+
19832002
if (haves) {
19842003
if (use_boundary_traversal) {
1985-
trace2_region_enter("pack-bitmap", "haves/boundary", the_repository);
2004+
trace2_region_enter("pack-bitmap", "haves/boundary", repo);
19862005
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);
19882007
} else {
1989-
trace2_region_enter("pack-bitmap", "haves/classic", the_repository);
2008+
trace2_region_enter("pack-bitmap", "haves/classic", repo);
19902009
revs->ignore_missing_links = 1;
19912010
haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
19922011
reset_revision_walk();
19932012
revs->ignore_missing_links = 0;
1994-
trace2_region_leave("pack-bitmap", "haves/classic", the_repository);
2013+
trace2_region_leave("pack-bitmap", "haves/classic", repo);
19952014
}
19962015

19972016
if (!haves_bitmap)
@@ -2025,17 +2044,17 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
20252044
object_list_free(&wants);
20262045
object_list_free(&haves);
20272046

2028-
trace2_data_intmax("bitmap", the_repository, "pseudo_merges_satisfied",
2047+
trace2_data_intmax("bitmap", repo, "pseudo_merges_satisfied",
20292048
pseudo_merges_satisfied_nr);
2030-
trace2_data_intmax("bitmap", the_repository, "pseudo_merges_cascades",
2049+
trace2_data_intmax("bitmap", repo, "pseudo_merges_cascades",
20312050
pseudo_merges_cascades_nr);
2032-
trace2_data_intmax("bitmap", the_repository, "bitmap/hits",
2051+
trace2_data_intmax("bitmap", repo, "bitmap/hits",
20332052
existing_bitmaps_hits_nr);
2034-
trace2_data_intmax("bitmap", the_repository, "bitmap/misses",
2053+
trace2_data_intmax("bitmap", repo, "bitmap/misses",
20352054
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",
20372056
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",
20392058
roots_without_bitmaps_nr);
20402059

20412060
return bitmap_git;
@@ -2256,7 +2275,7 @@ void reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
22562275
struct bitmap **reuse_out,
22572276
int multi_pack_reuse)
22582277
{
2259-
struct repository *r = the_repository;
2278+
struct repository *r = bitmap_repo(bitmap_git);
22602279
struct bitmapped_pack *packs = NULL;
22612280
struct bitmap *result = bitmap_git->result;
22622281
struct bitmap *reuse;
@@ -2792,7 +2811,7 @@ int rebuild_bitmap(const uint32_t *reposition,
27922811
uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
27932812
struct packing_data *mapping)
27942813
{
2795-
struct repository *r = the_repository;
2814+
struct repository *r = bitmap_repo(bitmap_git);
27962815
uint32_t i, num_objects;
27972816
uint32_t *reposition;
27982817

@@ -2948,7 +2967,8 @@ static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
29482967
st_add(bitmap_num_objects(bitmap_git), i)))
29492968
continue;
29502969

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)
29522972
die(_("unable to get disk usage of '%s'"),
29532973
oid_to_hex(&obj->oid));
29542974

0 commit comments

Comments
 (0)