Skip to content

Commit 6e86f67

Browse files
ttaylorrgitster
authored andcommitted
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>
1 parent de811c2 commit 6e86f67

5 files changed

Lines changed: 12 additions & 8 deletions

File tree

midx-write.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,8 +1151,7 @@ static int write_midx_internal(struct odb_source *source,
11511151
while (m) {
11521152
if (flags & MIDX_WRITE_BITMAP && load_midx_revindex(m)) {
11531153
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));
11561155
goto cleanup;
11571156
}
11581157
ctx.num_multi_pack_indexes_before++;
@@ -1520,8 +1519,7 @@ static int write_midx_internal(struct odb_source *source,
15201519
for (uint32_t i = 0; i < ctx.num_multi_pack_indexes_before; i++) {
15211520
uint32_t j = ctx.num_multi_pack_indexes_before - i - 1;
15221521

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));
15251523
m = m->base_midx;
15261524
}
15271525

midx.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ void clear_incremental_midx_files_ext(struct odb_source *source, const char *ext
2424
int cmp_idx_or_pack_name(const char *idx_or_pack_name,
2525
const char *idx_name);
2626

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+
2733
const unsigned char *midx_get_checksum_hash(const struct multi_pack_index *m)
2834
{
2935
return m->data + m->data_len - m->source->odb->repo->hash_algo->rawsz;

midx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ struct multi_pack_index {
8585
#define MIDX_EXT_BITMAP "bitmap"
8686
#define MIDX_EXT_MIDX "midx"
8787

88+
const char *midx_get_checksum_hex(const struct multi_pack_index *m) /* static buffer */;
8889
const unsigned char *midx_get_checksum_hash(const struct multi_pack_index *m);
8990
void get_midx_filename(struct odb_source *source, struct strbuf *out);
9091
void get_midx_filename_ext(struct odb_source *source, struct strbuf *out,

pack-bitmap.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2819,8 +2819,7 @@ void test_bitmap_walk(struct rev_info *revs)
28192819

28202820
if (bitmap_is_midx(found))
28212821
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));
28242823
else
28252824
fprintf_ln(stderr, "Located via pack '%s'.",
28262825
hash_to_hex_algop(found->pack->hash,

t/helper/test-read-midx.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static int read_midx_file(const char *object_dir, const char *checksum,
3434
return 1;
3535

3636
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))
3838
m = m->base_midx;
3939
if (!m)
4040
return 1;
@@ -94,7 +94,7 @@ static int read_midx_checksum(const char *object_dir)
9494
m = setup_midx(object_dir);
9595
if (!m)
9696
return 1;
97-
printf("%s\n", hash_to_hex(midx_get_checksum_hash(m)));
97+
printf("%s\n", midx_get_checksum_hex(m));
9898

9999
close_midx(m);
100100
return 0;

0 commit comments

Comments
 (0)