]> git.ipfire.org Git - thirdparty/git.git/commitdiff
midx: introduce `midx_get_checksum_hex()`
authorTaylor Blau <me@ttaylorr.com>
Wed, 14 Jan 2026 19:54:24 +0000 (14:54 -0500)
committerJunio C Hamano <gitster@pobox.com>
Wed, 14 Jan 2026 20:52:55 +0000 (12:52 -0800)
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>
midx-write.c
midx.c
midx.h
pack-bitmap.c
t/helper/test-read-midx.c

index 73d33752ef11199fd3da6980e76bd37d98f7489e..13171d7e9c4ae03dacf4fcb41564ae42654b2225 100644 (file)
@@ -1151,8 +1151,7 @@ static int write_midx_internal(struct odb_source *source,
                while (m) {
                        if (flags & MIDX_WRITE_BITMAP && load_midx_revindex(m)) {
                                error(_("could not load reverse index for MIDX %s"),
-                                     hash_to_hex_algop(midx_get_checksum_hash(m),
-                                                       m->source->odb->repo->hash_algo));
+                                     midx_get_checksum_hex(m));
                                goto cleanup;
                        }
                        ctx.num_multi_pack_indexes_before++;
@@ -1520,8 +1519,7 @@ static int write_midx_internal(struct odb_source *source,
                for (uint32_t i = 0; i < ctx.num_multi_pack_indexes_before; i++) {
                        uint32_t j = ctx.num_multi_pack_indexes_before - i - 1;
 
-                       keep_hashes[j] = xstrdup(hash_to_hex_algop(midx_get_checksum_hash(m),
-                                                                  r->hash_algo));
+                       keep_hashes[j] = xstrdup(midx_get_checksum_hex(m));
                        m = m->base_midx;
                }
 
diff --git a/midx.c b/midx.c
index 554bdfc505baea06e042f286f7b3a623c175a2f9..19ef230d3fd259038a857f5834dc0580c2b869e5 100644 (file)
--- a/midx.c
+++ b/midx.c
@@ -24,6 +24,12 @@ void clear_incremental_midx_files_ext(struct odb_source *source, const char *ext
 int cmp_idx_or_pack_name(const char *idx_or_pack_name,
                         const char *idx_name);
 
+const char *midx_get_checksum_hex(const struct multi_pack_index *m)
+{
+       return hash_to_hex_algop(midx_get_checksum_hash(m),
+                                m->source->odb->repo->hash_algo);
+}
+
 const unsigned char *midx_get_checksum_hash(const struct multi_pack_index *m)
 {
        return m->data + m->data_len - m->source->odb->repo->hash_algo->rawsz;
diff --git a/midx.h b/midx.h
index 62d6105195f0fb05d617a8a66e999aa4f9f04c68..a39bcc9d03fc9c5c21673908e6378d54b251fa6d 100644 (file)
--- a/midx.h
+++ b/midx.h
@@ -85,6 +85,7 @@ struct multi_pack_index {
 #define MIDX_EXT_BITMAP "bitmap"
 #define MIDX_EXT_MIDX "midx"
 
+const char *midx_get_checksum_hex(const struct multi_pack_index *m) /* static buffer */;
 const unsigned char *midx_get_checksum_hash(const struct multi_pack_index *m);
 void get_midx_filename(struct odb_source *source, struct strbuf *out);
 void get_midx_filename_ext(struct odb_source *source, struct strbuf *out,
index 90ae63e6804ca141056be87cde353b3891942d16..b688531fd2ffa329dcd04aff51781e088494f5b5 100644 (file)
@@ -2820,8 +2820,7 @@ void test_bitmap_walk(struct rev_info *revs)
 
                if (bitmap_is_midx(found))
                        fprintf_ln(stderr, "Located via MIDX '%s'.",
-                                  hash_to_hex_algop(midx_get_checksum_hash(found->midx),
-                                                    revs->repo->hash_algo));
+                                  midx_get_checksum_hex(found->midx));
                else
                        fprintf_ln(stderr, "Located via pack '%s'.",
                                   hash_to_hex_algop(found->pack->hash,
index b8fefb1a1245e21c09a2c23285a1d14f3cd5daf0..9d42c587564182b7499e3464b023dfd44c41a715 100644 (file)
@@ -34,7 +34,7 @@ static int read_midx_file(const char *object_dir, const char *checksum,
                return 1;
 
        if (checksum) {
-               while (m && strcmp(hash_to_hex(midx_get_checksum_hash(m)), checksum))
+               while (m && strcmp(midx_get_checksum_hex(m), checksum))
                        m = m->base_midx;
                if (!m)
                        return 1;
@@ -94,7 +94,7 @@ static int read_midx_checksum(const char *object_dir)
        m = setup_midx(object_dir);
        if (!m)
                return 1;
-       printf("%s\n", hash_to_hex(midx_get_checksum_hash(m)));
+       printf("%s\n", midx_get_checksum_hex(m));
 
        close_midx(m);
        return 0;