midx: compute paths via their source

With the preceding commits we started to always have the object database source available when we load, write or access multi-pack indices. With this in place we can change how MIDX paths are computed so that we don't have to pass in the combination of a hash algorithm and object directory anymore, but only the object database source. Refactor the code accordingly. 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 13296ac909d53e14712f89a7f4fda94dd0465479
5 files changed +62 -75
midx-write.c
+24 -28
@@ -26,9 +26,9 @@
26 #define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
27
28 extern int midx_checksum_valid(struct multi_pack_index *m);
29 -extern void clear_midx_files_ext(const char *object_dir, const char *ext,
29 +extern void clear_midx_files_ext(struct odb_source *source, const char *ext,
30 const char *keep_hash);
31 -extern void clear_incremental_midx_files_ext(const char *object_dir,
31 +extern void clear_incremental_midx_files_ext(struct odb_source *source,
32 const char *ext,
33 const char **keep_hashes,
34 uint32_t hashes_nr);
@@ -112,6 +112,7 @@ struct write_midx_context {
112 struct string_list *to_include;
113
114 struct repository *repo;
115 + struct odb_source *source;
116 };
117
118 static int should_include_pack(const struct write_midx_context *ctx,
@@ -648,7 +649,6 @@ static uint32_t *midx_pack_order(struct write_midx_context *ctx)
649 }
650
651 static void write_midx_reverse_index(struct write_midx_context *ctx,
651 - const char *object_dir,
652 unsigned char *midx_hash)
653 {
654 struct strbuf buf = STRBUF_INIT;
@@ -657,11 +657,10 @@ static void write_midx_reverse_index(struct write_midx_context *ctx,
657 trace2_region_enter("midx", "write_midx_reverse_index", ctx->repo);
658
659 if (ctx->incremental)
660 - get_split_midx_filename_ext(ctx->repo->hash_algo, &buf,
661 - object_dir, midx_hash,
662 - MIDX_EXT_REV);
660 + get_split_midx_filename_ext(ctx->source, &buf,
661 + midx_hash, MIDX_EXT_REV);
662 else
664 - get_midx_filename_ext(ctx->repo->hash_algo, &buf, object_dir,
663 + get_midx_filename_ext(ctx->source, &buf,
664 midx_hash, MIDX_EXT_REV);
665
666 tmp_file = write_rev_file_order(ctx->repo, NULL, ctx->pack_order,
@@ -836,7 +835,6 @@ static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr
835 }
836
837 static int write_midx_bitmap(struct write_midx_context *ctx,
839 - const char *object_dir,
838 const unsigned char *midx_hash,
839 struct packing_data *pdata,
840 struct commit **commits,
@@ -852,12 +850,11 @@ static int write_midx_bitmap(struct write_midx_context *ctx,
850 trace2_region_enter("midx", "write_midx_bitmap", ctx->repo);
851
852 if (ctx->incremental)
855 - get_split_midx_filename_ext(ctx->repo->hash_algo, &bitmap_name,
856 - object_dir, midx_hash,
857 - MIDX_EXT_BITMAP);
853 + get_split_midx_filename_ext(ctx->source, &bitmap_name,
854 + midx_hash, MIDX_EXT_BITMAP);
855 else
859 - get_midx_filename_ext(ctx->repo->hash_algo, &bitmap_name,
860 - object_dir, midx_hash, MIDX_EXT_BITMAP);
856 + get_midx_filename_ext(ctx->source, &bitmap_name,
857 + midx_hash, MIDX_EXT_BITMAP);
858
859 if (flags & MIDX_WRITE_BITMAP_HASH_CACHE)
860 options |= BITMAP_OPT_HASH_CACHE;
@@ -981,11 +978,9 @@ static int link_midx_to_chain(struct multi_pack_index *m)
978 for (i = 0; i < ARRAY_SIZE(midx_exts); i++) {
979 const unsigned char *hash = get_midx_checksum(m);
980
984 - get_midx_filename_ext(m->source->odb->repo->hash_algo, &from,
985 - m->source->path,
981 + get_midx_filename_ext(m->source, &from,
982 hash, midx_exts[i].non_split);
987 - get_split_midx_filename_ext(m->source->odb->repo->hash_algo, &to,
988 - m->source->path, hash,
983 + get_split_midx_filename_ext(m->source, &to, hash,
984 midx_exts[i].split);
985
986 if (link(from.buf, to.buf) < 0 && errno != ENOENT) {
@@ -1023,16 +1018,16 @@ static void clear_midx_files(struct odb_source *source,
1018 uint32_t i, j;
1019
1020 for (i = 0; i < ARRAY_SIZE(exts); i++) {
1026 - clear_incremental_midx_files_ext(source->path, exts[i],
1021 + clear_incremental_midx_files_ext(source, exts[i],
1022 hashes, hashes_nr);
1023 for (j = 0; j < hashes_nr; j++)
1029 - clear_midx_files_ext(source->path, exts[i], hashes[j]);
1024 + clear_midx_files_ext(source, exts[i], hashes[j]);
1025 }
1026
1027 if (incremental)
1033 - get_midx_filename(source->odb->repo->hash_algo, &buf, source->path);
1028 + get_midx_filename(source, &buf);
1029 else
1035 - get_midx_chain_filename(&buf, source->path);
1030 + get_midx_chain_filename(source, &buf);
1031
1032 if (unlink(buf.buf) && errno != ENOENT)
1033 die_errno(_("failed to clear multi-pack-index at %s"), buf.buf);
@@ -1065,6 +1060,7 @@ static int write_midx_internal(struct odb_source *source,
1060 trace2_region_enter("midx", "write_midx_internal", r);
1061
1062 ctx.repo = r;
1063 + ctx.source = source;
1064
1065 ctx.incremental = !!(flags & MIDX_WRITE_INCREMENTAL);
1066
@@ -1073,7 +1069,7 @@ static int write_midx_internal(struct odb_source *source,
1069 "%s/pack/multi-pack-index.d/tmp_midx_XXXXXX",
1070 source->path);
1071 else
1076 - get_midx_filename(r->hash_algo, &midx_name, source->path);
1072 + get_midx_filename(source, &midx_name);
1073 if (safe_create_leading_directories(r, midx_name.buf))
1074 die_errno(_("unable to create leading directories of %s"),
1075 midx_name.buf);
@@ -1153,7 +1149,7 @@ static int write_midx_internal(struct odb_source *source,
1149 * corresponding bitmap (or one wasn't requested).
1150 */
1151 if (!want_bitmap)
1156 - clear_midx_files_ext(source->path, "bitmap", NULL);
1152 + clear_midx_files_ext(source, "bitmap", NULL);
1153 goto cleanup;
1154 }
1155 }
@@ -1321,7 +1317,7 @@ static int write_midx_internal(struct odb_source *source,
1317 if (ctx.incremental) {
1318 struct strbuf lock_name = STRBUF_INIT;
1319
1324 - get_midx_chain_filename(&lock_name, source->path);
1320 + get_midx_chain_filename(source, &lock_name);
1321 hold_lock_file_for_update(&lk, lock_name.buf, LOCK_DIE_ON_ERROR);
1322 strbuf_release(&lock_name);
1323
@@ -1384,7 +1380,7 @@ static int write_midx_internal(struct odb_source *source,
1380
1381 if (flags & MIDX_WRITE_REV_INDEX &&
1382 git_env_bool("GIT_TEST_MIDX_WRITE_REV", 0))
1387 - write_midx_reverse_index(&ctx, source->path, midx_hash);
1383 + write_midx_reverse_index(&ctx, midx_hash);
1384
1385 if (flags & MIDX_WRITE_BITMAP) {
1386 struct packing_data pdata;
@@ -1407,7 +1403,7 @@ static int write_midx_internal(struct odb_source *source,
1403 FREE_AND_NULL(ctx.entries);
1404 ctx.entries_nr = 0;
1405
1410 - if (write_midx_bitmap(&ctx, source->path,
1406 + if (write_midx_bitmap(&ctx,
1407 midx_hash, &pdata, commits, commits_nr,
1408 flags) < 0) {
1409 error(_("could not write multi-pack bitmap"));
@@ -1440,8 +1436,8 @@ static int write_midx_internal(struct odb_source *source,
1436 if (link_midx_to_chain(ctx.base_midx) < 0)
1437 return -1;
1438
1443 - get_split_midx_filename_ext(r->hash_algo, &final_midx_name,
1444 - source->path, midx_hash, MIDX_EXT_MIDX);
1439 + get_split_midx_filename_ext(source, &final_midx_name,
1440 + midx_hash, MIDX_EXT_MIDX);
1441
1442 if (rename_tempfile(&incr, final_midx_name.buf) < 0) {
1443 error_errno(_("unable to rename new multi-pack-index layer"));
midx.c
+25 -29
@@ -16,9 +16,9 @@
16 #define MIDX_PACK_ERROR ((void *)(intptr_t)-1)
17
18 int midx_checksum_valid(struct multi_pack_index *m);
19 -void clear_midx_files_ext(const char *object_dir, const char *ext,
19 +void clear_midx_files_ext(struct odb_source *source, const char *ext,
20 const char *keep_hash);
21 -void clear_incremental_midx_files_ext(const char *object_dir, const char *ext,
21 +void clear_incremental_midx_files_ext(struct odb_source *source, const char *ext,
22 char **keep_hashes,
23 uint32_t hashes_nr);
24 int cmp_idx_or_pack_name(const char *idx_or_pack_name,
@@ -29,19 +29,17 @@ const unsigned char *get_midx_checksum(struct multi_pack_index *m)
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,
33 - struct strbuf *out, const char *object_dir)
32 +void get_midx_filename(struct odb_source *source, struct strbuf *out)
33 {
35 - get_midx_filename_ext(hash_algo, out, object_dir, NULL, NULL);
34 + get_midx_filename_ext(source, out, NULL, NULL);
35 }
36
38 -void get_midx_filename_ext(const struct git_hash_algo *hash_algo,
39 - struct strbuf *out, const char *object_dir,
37 +void get_midx_filename_ext(struct odb_source *source, struct strbuf *out,
38 const unsigned char *hash, const char *ext)
39 {
42 - strbuf_addf(out, "%s/pack/multi-pack-index", object_dir);
40 + strbuf_addf(out, "%s/pack/multi-pack-index", source->path);
41 if (ext)
44 - strbuf_addf(out, "-%s.%s", hash_to_hex_algop(hash, hash_algo), ext);
42 + strbuf_addf(out, "-%s.%s", hash_to_hex_algop(hash, source->odb->repo->hash_algo), ext);
43 }
44
45 static int midx_read_oid_fanout(const unsigned char *chunk_start,
@@ -222,24 +220,23 @@ cleanup_fail:
220 return NULL;
221 }
222
225 -void get_midx_chain_dirname(struct strbuf *buf, const char *object_dir)
223 +void get_midx_chain_dirname(struct odb_source *source, struct strbuf *buf)
224 {
227 - strbuf_addf(buf, "%s/pack/multi-pack-index.d", object_dir);
225 + strbuf_addf(buf, "%s/pack/multi-pack-index.d", source->path);
226 }
227
230 -void get_midx_chain_filename(struct strbuf *buf, const char *object_dir)
228 +void get_midx_chain_filename(struct odb_source *source, struct strbuf *buf)
229 {
232 - get_midx_chain_dirname(buf, object_dir);
230 + get_midx_chain_dirname(source, buf);
231 strbuf_addstr(buf, "/multi-pack-index-chain");
232 }
233
236 -void get_split_midx_filename_ext(const struct git_hash_algo *hash_algo,
237 - struct strbuf *buf, const char *object_dir,
234 +void get_split_midx_filename_ext(struct odb_source *source, struct strbuf *buf,
235 const unsigned char *hash, const char *ext)
236 {
240 - get_midx_chain_dirname(buf, object_dir);
237 + get_midx_chain_dirname(source, buf);
238 strbuf_addf(buf, "/multi-pack-index-%s.%s",
242 - hash_to_hex_algop(hash, hash_algo), ext);
239 + hash_to_hex_algop(hash, source->odb->repo->hash_algo), ext);
240 }
241
242 static int open_multi_pack_index_chain(const struct git_hash_algo *hash_algo,
@@ -326,7 +323,7 @@ static struct multi_pack_index *load_midx_chain_fd_st(struct odb_source *source,
323 valid = 0;
324
325 strbuf_reset(&buf);
329 - get_split_midx_filename_ext(hash_algo, &buf, source->path,
326 + get_split_midx_filename_ext(source, &buf,
327 layer.hash, MIDX_EXT_MIDX);
328 m = load_multi_pack_index_one(source, buf.buf);
329
@@ -358,7 +355,7 @@ static struct multi_pack_index *load_multi_pack_index_chain(struct odb_source *s
355 int fd;
356 struct multi_pack_index *m = NULL;
357
361 - get_midx_chain_filename(&chain_file, source->path);
358 + get_midx_chain_filename(source, &chain_file);
359 if (open_multi_pack_index_chain(source->odb->repo->hash_algo, chain_file.buf, &fd, &st)) {
360 int incomplete;
361 /* ownership of fd is taken over by load function */
@@ -374,8 +371,7 @@ struct multi_pack_index *load_multi_pack_index(struct odb_source *source)
371 struct strbuf midx_name = STRBUF_INIT;
372 struct multi_pack_index *m;
373
377 - get_midx_filename(source->odb->repo->hash_algo, &midx_name,
378 - source->path);
374 + get_midx_filename(source, &midx_name);
375
376 m = load_multi_pack_index_one(source, midx_name.buf);
377 if (!m)
@@ -762,7 +758,7 @@ static void clear_midx_file_ext(const char *full_path, size_t full_path_len UNUS
758 die_errno(_("failed to remove %s"), full_path);
759 }
760
765 -void clear_midx_files_ext(const char *object_dir, const char *ext,
761 +void clear_midx_files_ext(struct odb_source *source, const char *ext,
762 const char *keep_hash)
763 {
764 struct clear_midx_data data;
@@ -776,7 +772,7 @@ void clear_midx_files_ext(const char *object_dir, const char *ext,
772 }
773 data.ext = ext;
774
779 - for_each_file_in_pack_dir(object_dir,
775 + for_each_file_in_pack_dir(source->path,
776 clear_midx_file_ext,
777 &data);
778
@@ -785,7 +781,7 @@ void clear_midx_files_ext(const char *object_dir, const char *ext,
781 free(data.keep);
782 }
783
788 -void clear_incremental_midx_files_ext(const char *object_dir, const char *ext,
784 +void clear_incremental_midx_files_ext(struct odb_source *source, const char *ext,
785 char **keep_hashes,
786 uint32_t hashes_nr)
787 {
@@ -801,7 +797,7 @@ void clear_incremental_midx_files_ext(const char *object_dir, const char *ext,
797 data.keep_nr = hashes_nr;
798 data.ext = ext;
799
804 - for_each_file_in_pack_subdir(object_dir, "multi-pack-index.d",
800 + for_each_file_in_pack_subdir(source->path, "multi-pack-index.d",
801 clear_midx_file_ext, &data);
802
803 for (i = 0; i < hashes_nr; i++)
@@ -813,7 +809,7 @@ void clear_midx_file(struct repository *r)
809 {
810 struct strbuf midx = STRBUF_INIT;
811
816 - get_midx_filename(r->hash_algo, &midx, r->objects->sources->path);
812 + get_midx_filename(r->objects->sources, &midx);
813
814 if (r->objects) {
815 struct odb_source *source;
@@ -828,8 +824,8 @@ void clear_midx_file(struct repository *r)
824 if (remove_path(midx.buf))
825 die(_("failed to clear multi-pack-index at %s"), midx.buf);
826
831 - clear_midx_files_ext(r->objects->sources->path, MIDX_EXT_BITMAP, NULL);
832 - clear_midx_files_ext(r->objects->sources->path, MIDX_EXT_REV, NULL);
827 + clear_midx_files_ext(r->objects->sources, MIDX_EXT_BITMAP, NULL);
828 + clear_midx_files_ext(r->objects->sources, MIDX_EXT_REV, NULL);
829
830 strbuf_release(&midx);
831 }
@@ -888,7 +884,7 @@ int verify_midx_file(struct odb_source *source, unsigned flags)
884 struct stat sb;
885 struct strbuf filename = STRBUF_INIT;
886
891 - get_midx_filename(r->hash_algo, &filename, source->path);
887 + get_midx_filename(source, &filename);
888
889 if (!stat(filename.buf, &sb)) {
890 error(_("multi-pack-index file exists, but failed to parse"));
midx.h
+5 -8
@@ -86,15 +86,12 @@ struct multi_pack_index {
86 #define MIDX_EXT_MIDX "midx"
87
88 const unsigned char *get_midx_checksum(struct multi_pack_index *m);
89 -void get_midx_filename(const struct git_hash_algo *hash_algo,
90 - struct strbuf *out, const char *object_dir);
91 -void get_midx_filename_ext(const struct git_hash_algo *hash_algo,
92 - struct strbuf *out, const char *object_dir,
89 +void get_midx_filename(struct odb_source *source, struct strbuf *out);
90 +void get_midx_filename_ext(struct odb_source *source, struct strbuf *out,
91 const unsigned char *hash, const char *ext);
94 -void get_midx_chain_dirname(struct strbuf *buf, const char *object_dir);
95 -void get_midx_chain_filename(struct strbuf *buf, const char *object_dir);
96 -void get_split_midx_filename_ext(const struct git_hash_algo *hash_algo,
97 - struct strbuf *buf, const char *object_dir,
92 +void get_midx_chain_dirname(struct odb_source *source, struct strbuf *out);
93 +void get_midx_chain_filename(struct odb_source *source, struct strbuf *out);
94 +void get_split_midx_filename_ext(struct odb_source *source, struct strbuf *buf,
95 const unsigned char *hash, const char *ext);
96
97 struct multi_pack_index *load_multi_pack_index(struct odb_source *source);
pack-bitmap.c
+4 -6
@@ -418,13 +418,12 @@ 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->source->odb->repo->hash_algo, &buf,
422 - midx->source->path,
421 + get_split_midx_filename_ext(midx->source, &buf,
422 get_midx_checksum(midx),
423 MIDX_EXT_BITMAP);
424 else
426 - get_midx_filename_ext(midx->source->odb->repo->hash_algo, &buf,
427 - midx->source->path, get_midx_checksum(midx),
425 + get_midx_filename_ext(midx->source, &buf,
426 + get_midx_checksum(midx),
427 MIDX_EXT_BITMAP);
428
429 return strbuf_detach(&buf, NULL);
@@ -463,8 +462,7 @@ static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
462
463 if (bitmap_git->pack || bitmap_git->midx) {
464 struct strbuf buf = STRBUF_INIT;
466 - get_midx_filename(midx->source->odb->repo->hash_algo, &buf,
467 - midx->source->path);
465 + get_midx_filename(midx->source, &buf);
466 trace2_data_string("bitmap", bitmap_repo(bitmap_git),
467 "ignoring extra midx bitmap file", buf.buf);
468 close(fd);
pack-revindex.c
+4 -4
@@ -389,12 +389,12 @@ int load_midx_revindex(struct multi_pack_index *m)
389 "source", "rev");
390
391 if (m->has_chain)
392 - get_split_midx_filename_ext(m->source->odb->repo->hash_algo, &revindex_name,
393 - m->source->path, get_midx_checksum(m),
392 + get_split_midx_filename_ext(m->source, &revindex_name,
393 + get_midx_checksum(m),
394 MIDX_EXT_REV);
395 else
396 - get_midx_filename_ext(m->source->odb->repo->hash_algo, &revindex_name,
397 - m->source->path, get_midx_checksum(m),
396 + get_midx_filename_ext(m->source, &revindex_name,
397 + get_midx_checksum(m),
398 MIDX_EXT_REV);
399
400 ret = load_revindex_from_disk(m->source->odb->repo->hash_algo,