Skip to content

Commit fcf3d68

Browse files
ttaylorrgitster
authored andcommitted
midx: split get_midx_checksum() by adding get_midx_hash()
When trying to print out, say, the hexadecimal representation of a MIDX's hash, our code will do something like: hash_to_hex_algop(get_midx_checksum(m), m->source->odb->repo->hash_algo); , which is both cumbersome and repetitive. In fact, all but a handful of callers to `get_midx_checksum()` do exactly the above. Reduce the repetitive nature of calling `get_midx_checksum()` 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), introduce `get_midx_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 3eea3c8 commit fcf3d68

File tree

6 files changed

+20
-16
lines changed

6 files changed

+20
-16
lines changed

midx-write.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ static int link_midx_to_chain(struct multi_pack_index *m)
955955
}
956956

957957
for (i = 0; i < ARRAY_SIZE(midx_exts); i++) {
958-
const unsigned char *hash = get_midx_checksum(m);
958+
const unsigned char *hash = get_midx_hash(m);
959959

960960
get_midx_filename_ext(m->source, &from,
961961
hash, midx_exts[i].non_split);
@@ -1086,8 +1086,7 @@ static int write_midx_internal(struct odb_source *source,
10861086
while (m) {
10871087
if (flags & MIDX_WRITE_BITMAP && load_midx_revindex(m)) {
10881088
error(_("could not load reverse index for MIDX %s"),
1089-
hash_to_hex_algop(get_midx_checksum(m),
1090-
m->source->odb->repo->hash_algo));
1089+
get_midx_checksum(m));
10911090
goto cleanup;
10921091
}
10931092
ctx.num_multi_pack_indexes_before++;
@@ -1445,8 +1444,7 @@ static int write_midx_internal(struct odb_source *source,
14451444
for (uint32_t i = 0; i < ctx.num_multi_pack_indexes_before; i++) {
14461445
uint32_t j = ctx.num_multi_pack_indexes_before - i - 1;
14471446

1448-
keep_hashes[j] = xstrdup(hash_to_hex_algop(get_midx_checksum(m),
1449-
r->hash_algo));
1447+
keep_hashes[j] = xstrdup(get_midx_checksum(m));
14501448
m = m->base_midx;
14511449
}
14521450

midx.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ 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 unsigned char *get_midx_checksum(const struct multi_pack_index *m)
27+
const char *get_midx_checksum(const struct multi_pack_index *m)
28+
{
29+
return hash_to_hex_algop(get_midx_hash(m),
30+
m->source->odb->repo->hash_algo);
31+
}
32+
33+
const unsigned char *get_midx_hash(const struct multi_pack_index *m)
2834
{
2935
return m->data + m->data_len - m->source->odb->repo->hash_algo->rawsz;
3036
}

midx.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ struct multi_pack_index {
8585
#define MIDX_EXT_BITMAP "bitmap"
8686
#define MIDX_EXT_MIDX "midx"
8787

88-
const unsigned char *get_midx_checksum(const struct multi_pack_index *m);
88+
const char *get_midx_checksum(const struct multi_pack_index *m) /* static buffer */;
89+
const unsigned char *get_midx_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,
9192
const unsigned char *hash, const char *ext);

pack-bitmap.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,11 @@ char *midx_bitmap_filename(struct multi_pack_index *midx)
441441
struct strbuf buf = STRBUF_INIT;
442442
if (midx->has_chain)
443443
get_split_midx_filename_ext(midx->source, &buf,
444-
get_midx_checksum(midx),
444+
get_midx_hash(midx),
445445
MIDX_EXT_BITMAP);
446446
else
447447
get_midx_filename_ext(midx->source, &buf,
448-
get_midx_checksum(midx),
448+
get_midx_hash(midx),
449449
MIDX_EXT_BITMAP);
450450

451451
return strbuf_detach(&buf, NULL);
@@ -502,7 +502,7 @@ static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
502502
if (load_bitmap_header(bitmap_git) < 0)
503503
goto cleanup;
504504

505-
if (!hasheq(get_midx_checksum(bitmap_git->midx), bitmap_git->checksum,
505+
if (!hasheq(get_midx_hash(bitmap_git->midx), bitmap_git->checksum,
506506
bitmap_repo(bitmap_git)->hash_algo)) {
507507
error(_("checksum doesn't match in MIDX and bitmap"));
508508
goto cleanup;
@@ -2820,8 +2820,7 @@ void test_bitmap_walk(struct rev_info *revs)
28202820

28212821
if (bitmap_is_midx(found))
28222822
fprintf_ln(stderr, "Located via MIDX '%s'.",
2823-
hash_to_hex_algop(get_midx_checksum(found->midx),
2824-
revs->repo->hash_algo));
2823+
get_midx_checksum(found->midx));
28252824
else
28262825
fprintf_ln(stderr, "Located via pack '%s'.",
28272826
hash_to_hex_algop(found->pack->hash,

pack-revindex.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,11 @@ int load_midx_revindex(struct multi_pack_index *m)
390390

391391
if (m->has_chain)
392392
get_split_midx_filename_ext(m->source, &revindex_name,
393-
get_midx_checksum(m),
393+
get_midx_hash(m),
394394
MIDX_EXT_REV);
395395
else
396396
get_midx_filename_ext(m->source, &revindex_name,
397-
get_midx_checksum(m),
397+
get_midx_hash(m),
398398
MIDX_EXT_REV);
399399

400400
ret = load_revindex_from_disk(m->source->odb->repo->hash_algo,

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(get_midx_checksum(m)), checksum))
37+
while (m && strcmp(get_midx_checksum(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(get_midx_checksum(m)));
97+
printf("%s\n", get_midx_checksum(m));
9898

9999
close_midx(m);
100100
return 0;

0 commit comments

Comments
 (0)