Skip to content

Commit 1f39dd2

Browse files
pks-tgitster
authored andcommitted
t/helper: use hash_to_hex_algop() to print hashes
The "reftable" test helper uses a hand-crafted version to convert from a raw hash to its hex variant. This was done because this code used to be part of the reftable library, where we do not use most functions from the Git core. Now that the code is integrated into the "dump-reftable" helper though, that limitation went away. Let's thus use `hash_to_hex_algop()` instead. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 42c424d commit 1f39dd2

File tree

1 file changed

+11
-37
lines changed

1 file changed

+11
-37
lines changed

t/helper/test-reftable.c

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#include "git-compat-util.h"
2+
#include "hash.h"
3+
#include "hex.h"
14
#include "reftable/system.h"
25
#include "reftable/reftable-error.h"
36
#include "reftable/reftable-generic.h"
@@ -30,33 +33,12 @@ static void print_help(void)
3033
"\n");
3134
}
3235

33-
static char hexdigit(int c)
34-
{
35-
if (c <= 9)
36-
return '0' + c;
37-
return 'a' + (c - 10);
38-
}
39-
40-
static void hex_format(char *dest, const unsigned char *src, int hash_size)
41-
{
42-
assert(hash_size > 0);
43-
if (src) {
44-
int i = 0;
45-
for (i = 0; i < hash_size; i++) {
46-
dest[2 * i] = hexdigit(src[i] >> 4);
47-
dest[2 * i + 1] = hexdigit(src[i] & 0xf);
48-
}
49-
dest[2 * hash_size] = 0;
50-
}
51-
}
52-
5336
static int dump_table(struct reftable_table *tab)
5437
{
5538
struct reftable_iterator it = { NULL };
5639
struct reftable_ref_record ref = { NULL };
5740
struct reftable_log_record log = { NULL };
58-
uint32_t hash_id = reftable_table_hash_id(tab);
59-
int hash_len = hash_size(hash_id);
41+
const struct git_hash_algo *algop;
6042
int err;
6143

6244
reftable_table_init_ref_iter(tab, &it);
@@ -65,9 +47,9 @@ static int dump_table(struct reftable_table *tab)
6547
if (err < 0)
6648
return err;
6749

68-
while (1) {
69-
char hex[GIT_MAX_HEXSZ + 1] = { 0 }; /* BUG */
50+
algop = &hash_algos[hash_algo_by_id(reftable_table_hash_id(tab))];
7051

52+
while (1) {
7153
err = reftable_iterator_next_ref(&it, &ref);
7254
if (err > 0)
7355
break;
@@ -80,15 +62,11 @@ static int dump_table(struct reftable_table *tab)
8062
printf("=> %s", ref.value.symref);
8163
break;
8264
case REFTABLE_REF_VAL2:
83-
hex_format(hex, ref.value.val2.value, hash_len);
84-
printf("val 2 %s", hex);
85-
hex_format(hex, ref.value.val2.target_value,
86-
hash_len);
87-
printf("(T %s)", hex);
65+
printf("val 2 %s", hash_to_hex_algop(ref.value.val2.value, algop));
66+
printf("(T %s)", hash_to_hex_algop(ref.value.val2.target_value, algop));
8867
break;
8968
case REFTABLE_REF_VAL1:
90-
hex_format(hex, ref.value.val1, hash_len);
91-
printf("val 1 %s", hex);
69+
printf("val 1 %s", hash_to_hex_algop(ref.value.val1, algop));
9270
break;
9371
case REFTABLE_REF_DELETION:
9472
printf("delete");
@@ -106,8 +84,6 @@ static int dump_table(struct reftable_table *tab)
10684
return err;
10785

10886
while (1) {
109-
char hex[GIT_MAX_HEXSZ + 1] = { 0 };
110-
11187
err = reftable_iterator_next_log(&it, &log);
11288
if (err > 0)
11389
break;
@@ -126,10 +102,8 @@ static int dump_table(struct reftable_table *tab)
126102
log.value.update.email ? log.value.update.email : "",
127103
log.value.update.time,
128104
log.value.update.tz_offset);
129-
hex_format(hex, log.value.update.old_hash, hash_len);
130-
printf("%s => ", hex);
131-
hex_format(hex, log.value.update.new_hash, hash_len);
132-
printf("%s\n\n%s\n}\n", hex,
105+
printf("%s => ", hash_to_hex_algop(log.value.update.old_hash, algop));
106+
printf("%s\n\n%s\n}\n", hash_to_hex_algop(log.value.update.new_hash, algop),
133107
log.value.update.message ? log.value.update.message : "");
134108
break;
135109
}

0 commit comments

Comments
 (0)