midx: stop duplicating info redundant with its owning source
Multi-pack indices store some information that is redundant with their owning source: - The locality bit that tracks whether the source is the primary object source or an alternate. - The object directory path the multi-pack index is located in. - The pointer to the owning parent directory. All of this information is already contained in `struct odb_source`. So now that we always have that struct available when loading a multi-pack index we have it readily accessible. Drop the redundant information and instead store a pointer to the object source. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Aug 11, 2025 at 15:46 UTC
7744936f374308d6fa3c6e317fb8fe0b685d0ef2
7 files changed
+36
-35
builtin/repack.c
+3
-2
@@ -223,9 +223,10 @@ static void mark_packs_for_deletion(struct existing_packs *existing,
223
static void remove_redundant_pack(const char *dir_name, const char *base_name)
224
{
225
struct strbuf buf = STRBUF_INIT;
226
- struct multi_pack_index *m = get_multi_pack_index(the_repository->objects->sources);
226
+ struct odb_source *source = the_repository->objects->sources;
227
+ struct multi_pack_index *m = get_multi_pack_index(source);
228
strbuf_addf(&buf, "%s.pack", base_name);
228
- if (m && m->local && midx_contains_pack(m, buf.buf))
229
+ if (m && source->local && midx_contains_pack(m, buf.buf))
230
clear_midx_file(the_repository);
231
strbuf_insertf(&buf, 0, "%s/", dir_name);
232
unlink_pack_path(buf.buf, 1);
midx-write.c
+5
-4
@@ -981,10 +981,11 @@ static int link_midx_to_chain(struct multi_pack_index *m)
981
for (i = 0; i < ARRAY_SIZE(midx_exts); i++) {
982
const unsigned char *hash = get_midx_checksum(m);
983
984
- get_midx_filename_ext(m->repo->hash_algo, &from, m->object_dir,
984
+ get_midx_filename_ext(m->source->odb->repo->hash_algo, &from,
985
+ m->source->path,
986
hash, midx_exts[i].non_split);
986
- get_split_midx_filename_ext(m->repo->hash_algo, &to,
987
- m->object_dir, hash,
987
+ get_split_midx_filename_ext(m->source->odb->repo->hash_algo, &to,
988
+ m->source->path, hash,
989
midx_exts[i].split);
990
991
if (link(from.buf, to.buf) < 0 && errno != ENOENT) {
@@ -1109,7 +1110,7 @@ static int write_midx_internal(struct odb_source *source,
1110
if (flags & MIDX_WRITE_BITMAP && load_midx_revindex(m)) {
1111
error(_("could not load reverse index for MIDX %s"),
1112
hash_to_hex_algop(get_midx_checksum(m),
1112
- m->repo->hash_algo));
1113
+ m->source->odb->repo->hash_algo));
1114
result = 1;
1115
goto cleanup;
1116
}
midx.c
+11
-10
@@ -26,7 +26,7 @@ int cmp_idx_or_pack_name(const char *idx_or_pack_name,
26
27
const unsigned char *get_midx_checksum(struct multi_pack_index *m)
28
{
29
- return m->data + m->data_len - m->repo->hash_algo->rawsz;
29
+ return m->data + m->data_len - m->source->odb->repo->hash_algo->rawsz;
30
}
31
32
void get_midx_filename(const struct git_hash_algo *hash_algo,
@@ -128,11 +128,10 @@ static struct multi_pack_index *load_multi_pack_index_one(struct odb_source *sou
128
midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0);
129
close(fd);
130
131
- FLEX_ALLOC_STR(m, object_dir, source->path);
131
+ CALLOC_ARRAY(m, 1);
132
m->data = midx_map;
133
m->data_len = midx_size;
134
- m->local = source->local;
135
- m->repo = r;
134
+ m->source = source;
135
136
m->signature = get_be32(m->data);
137
if (m->signature != MIDX_SIGNATURE)
@@ -446,7 +445,7 @@ static uint32_t midx_for_pack(struct multi_pack_index **_m,
445
int prepare_midx_pack(struct multi_pack_index *m,
446
uint32_t pack_int_id)
447
{
449
- struct repository *r = m->repo;
448
+ struct repository *r = m->source->odb->repo;
449
struct strbuf pack_name = STRBUF_INIT;
450
struct strbuf key = STRBUF_INIT;
451
struct packed_git *p;
@@ -458,7 +457,7 @@ int prepare_midx_pack(struct multi_pack_index *m,
457
if (m->packs[pack_int_id])
458
return 0;
459
461
- strbuf_addf(&pack_name, "%s/pack/%s", m->object_dir,
460
+ strbuf_addf(&pack_name, "%s/pack/%s", m->source->path,
461
m->pack_names[pack_int_id]);
462
463
/* pack_map holds the ".pack" name, but we have the .idx */
@@ -469,7 +468,8 @@ int prepare_midx_pack(struct multi_pack_index *m,
468
strhash(key.buf), key.buf,
469
struct packed_git, packmap_ent);
470
if (!p) {
472
- p = add_packed_git(r, pack_name.buf, pack_name.len, m->local);
471
+ p = add_packed_git(r, pack_name.buf, pack_name.len,
472
+ m->source->local);
473
if (p) {
474
install_packed_git(r, p);
475
list_add_tail(&p->mru, &r->objects->packed_git_mru);
@@ -528,7 +528,8 @@ int bsearch_one_midx(const struct object_id *oid, struct multi_pack_index *m,
528
uint32_t *result)
529
{
530
int ret = bsearch_hash(oid->hash, m->chunk_oid_fanout,
531
- m->chunk_oid_lookup, m->repo->hash_algo->rawsz,
531
+ m->chunk_oid_lookup,
532
+ m->source->odb->repo->hash_algo->rawsz,
533
result);
534
if (result)
535
*result += m->num_objects_in_base;
@@ -559,7 +560,7 @@ struct object_id *nth_midxed_object_oid(struct object_id *oid,
560
n = midx_for_object(&m, n);
561
562
oidread(oid, m->chunk_oid_lookup + st_mult(m->hash_len, n),
562
- m->repo->hash_algo);
563
+ m->source->odb->repo->hash_algo);
564
return oid;
565
}
566
@@ -734,7 +735,7 @@ int prepare_multi_pack_index_one(struct odb_source *source)
735
736
int midx_checksum_valid(struct multi_pack_index *m)
737
{
737
- return hashfile_checksum_valid(m->repo->hash_algo,
738
+ return hashfile_checksum_valid(m->source->odb->repo->hash_algo,
739
m->data, m->data_len);
740
}
741
midx.h
+2
-5
@@ -35,6 +35,8 @@ struct odb_source;
35
"GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL"
36
37
struct multi_pack_index {
38
+ struct odb_source *source;
39
+
40
const unsigned char *data;
41
size_t data_len;
42
@@ -50,7 +52,6 @@ struct multi_pack_index {
52
uint32_t num_objects;
53
int preferred_pack_idx;
54
53
- int local;
55
int has_chain;
56
57
const unsigned char *chunk_pack_names;
@@ -71,10 +72,6 @@ struct multi_pack_index {
72
73
const char **pack_names;
74
struct packed_git **packs;
74
-
75
- struct repository *repo;
76
-
77
- char object_dir[FLEX_ARRAY];
75
};
76
77
#define MIDX_PROGRESS (1 << 0)
pack-bitmap.c
+7
-6
@@ -216,7 +216,7 @@ static uint32_t bitmap_num_objects(struct bitmap_index *index)
216
static struct repository *bitmap_repo(struct bitmap_index *bitmap_git)
217
{
218
if (bitmap_is_midx(bitmap_git))
219
- return bitmap_git->midx->repo;
219
+ return bitmap_git->midx->source->odb->repo;
220
return bitmap_git->pack->repo;
221
}
222
@@ -418,13 +418,13 @@ char *midx_bitmap_filename(struct multi_pack_index *midx)
418
{
419
struct strbuf buf = STRBUF_INIT;
420
if (midx->has_chain)
421
- get_split_midx_filename_ext(midx->repo->hash_algo, &buf,
422
- midx->object_dir,
421
+ get_split_midx_filename_ext(midx->source->odb->repo->hash_algo, &buf,
422
+ midx->source->path,
423
get_midx_checksum(midx),
424
MIDX_EXT_BITMAP);
425
else
426
- get_midx_filename_ext(midx->repo->hash_algo, &buf,
427
- midx->object_dir, get_midx_checksum(midx),
426
+ get_midx_filename_ext(midx->source->odb->repo->hash_algo, &buf,
427
+ midx->source->path, get_midx_checksum(midx),
428
MIDX_EXT_BITMAP);
429
430
return strbuf_detach(&buf, NULL);
@@ -463,7 +463,8 @@ static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
463
464
if (bitmap_git->pack || bitmap_git->midx) {
465
struct strbuf buf = STRBUF_INIT;
466
- get_midx_filename(midx->repo->hash_algo, &buf, midx->object_dir);
466
+ get_midx_filename(midx->source->odb->repo->hash_algo, &buf,
467
+ midx->source->path);
468
trace2_data_string("bitmap", bitmap_repo(bitmap_git),
469
"ignoring extra midx bitmap file", buf.buf);
470
close(fd);
pack-revindex.c
+7
-7
@@ -379,25 +379,25 @@ int load_midx_revindex(struct multi_pack_index *m)
379
* not want to accidentally call munmap() in the middle of the
380
* MIDX.
381
*/
382
- trace2_data_string("load_midx_revindex", m->repo,
382
+ trace2_data_string("load_midx_revindex", m->source->odb->repo,
383
"source", "midx");
384
m->revindex_data = (const uint32_t *)m->chunk_revindex;
385
return 0;
386
}
387
388
- trace2_data_string("load_midx_revindex", m->repo,
388
+ trace2_data_string("load_midx_revindex", m->source->odb->repo,
389
"source", "rev");
390
391
if (m->has_chain)
392
- get_split_midx_filename_ext(m->repo->hash_algo, &revindex_name,
393
- m->object_dir, get_midx_checksum(m),
392
+ get_split_midx_filename_ext(m->source->odb->repo->hash_algo, &revindex_name,
393
+ m->source->path, get_midx_checksum(m),
394
MIDX_EXT_REV);
395
else
396
- get_midx_filename_ext(m->repo->hash_algo, &revindex_name,
397
- m->object_dir, get_midx_checksum(m),
396
+ get_midx_filename_ext(m->source->odb->repo->hash_algo, &revindex_name,
397
+ m->source->path, get_midx_checksum(m),
398
MIDX_EXT_REV);
399
400
- ret = load_revindex_from_disk(m->repo->hash_algo,
400
+ ret = load_revindex_from_disk(m->source->odb->repo->hash_algo,
401
revindex_name.buf,
402
m->num_objects,
403
&m->revindex_map,
t/helper/test-read-midx.c
+1
-1
@@ -66,7 +66,7 @@ static int read_midx_file(const char *object_dir, const char *checksum,
66
for (i = 0; i < m->num_packs; i++)
67
printf("%s\n", m->pack_names[i]);
68
69
- printf("object-dir: %s\n", m->object_dir);
69
+ printf("object-dir: %s\n", m->source->path);
70
71
if (show_objects) {
72
struct object_id oid;