midx: introduce `midx_get_checksum_hex()`

When trying to print out, say, the hexadecimal representation of a MIDX's hash, our code will do something like: hash_to_hex_algop(midx_get_checksum_hash(m), m->source->odb->repo->hash_algo); , which is both cumbersome and repetitive. In fact, all but a handful of callers to `midx_get_checksum_hash()` do exactly the above. Reduce the repetitive nature of calling `midx_get_checksum_hash()` by having it return a pointer into a static buffer containing the above result. For the handful of callers that do need to compare the raw bytes and don't want to deal with an encoded copy (e.g., because they are passing it to hasheq() or similar), they may still rely on `midx_get_checksum_hash()` which returns the raw bytes. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Feb 24, 2026 at 13:59 UTC 6e86f679248580bec5c105b37217862f3022b504
5 files changed +12 -8
midx-write.c
+2 -4
@@ -1151,8 +1151,7 @@ static int write_midx_internal(struct odb_source *source,
1151 while (m) {
1152 if (flags & MIDX_WRITE_BITMAP && load_midx_revindex(m)) {
1153 error(_("could not load reverse index for MIDX %s"),
1154 - hash_to_hex_algop(midx_get_checksum_hash(m),
1155 - m->source->odb->repo->hash_algo));
1154 + midx_get_checksum_hex(m));
1155 goto cleanup;
1156 }
1157 ctx.num_multi_pack_indexes_before++;
@@ -1520,8 +1519,7 @@ static int write_midx_internal(struct odb_source *source,
1519 for (uint32_t i = 0; i < ctx.num_multi_pack_indexes_before; i++) {
1520 uint32_t j = ctx.num_multi_pack_indexes_before - i - 1;
1521
1523 - keep_hashes[j] = xstrdup(hash_to_hex_algop(midx_get_checksum_hash(m),
1524 - r->hash_algo));
1522 + keep_hashes[j] = xstrdup(midx_get_checksum_hex(m));
1523 m = m->base_midx;
1524 }
1525
midx.c
+6
@@ -24,6 +24,12 @@ void clear_incremental_midx_files_ext(struct odb_source *source, const char *ext
24 int cmp_idx_or_pack_name(const char *idx_or_pack_name,
25 const char *idx_name);
26
27 +const char *midx_get_checksum_hex(const struct multi_pack_index *m)
28 +{
29 + return hash_to_hex_algop(midx_get_checksum_hash(m),
30 + m->source->odb->repo->hash_algo);
31 +}
32 +
33 const unsigned char *midx_get_checksum_hash(const struct multi_pack_index *m)
34 {
35 return m->data + m->data_len - m->source->odb->repo->hash_algo->rawsz;
midx.h
+1
@@ -85,6 +85,7 @@ struct multi_pack_index {
85 #define MIDX_EXT_BITMAP "bitmap"
86 #define MIDX_EXT_MIDX "midx"
87
88 +const char *midx_get_checksum_hex(const struct multi_pack_index *m) /* static buffer */;
89 const unsigned char *midx_get_checksum_hash(const struct multi_pack_index *m);
90 void get_midx_filename(struct odb_source *source, struct strbuf *out);
91 void get_midx_filename_ext(struct odb_source *source, struct strbuf *out,
pack-bitmap.c
+1 -2
@@ -2819,8 +2819,7 @@ void test_bitmap_walk(struct rev_info *revs)
2819
2820 if (bitmap_is_midx(found))
2821 fprintf_ln(stderr, "Located via MIDX '%s'.",
2822 - hash_to_hex_algop(midx_get_checksum_hash(found->midx),
2823 - revs->repo->hash_algo));
2822 + midx_get_checksum_hex(found->midx));
2823 else
2824 fprintf_ln(stderr, "Located via pack '%s'.",
2825 hash_to_hex_algop(found->pack->hash,
t/helper/test-read-midx.c
+2 -2
@@ -34,7 +34,7 @@ static int read_midx_file(const char *object_dir, const char *checksum,
34 return 1;
35
36 if (checksum) {
37 - while (m && strcmp(hash_to_hex(midx_get_checksum_hash(m)), checksum))
37 + while (m && strcmp(midx_get_checksum_hex(m), checksum))
38 m = m->base_midx;
39 if (!m)
40 return 1;
@@ -94,7 +94,7 @@ static int read_midx_checksum(const char *object_dir)
94 m = setup_midx(object_dir);
95 if (!m)
96 return 1;
97 - printf("%s\n", hash_to_hex(midx_get_checksum_hash(m)));
97 + printf("%s\n", midx_get_checksum_hex(m));
98
99 close_midx(m);
100 return 0;