Skip to content

Commit 42c424d

Browse files
pks-tgitster
authored andcommitted
t/helper: inline printing of reftable records
Move printing of reftable records into the "dump-reftable" helper. This follows the same reasoning as the preceding commit. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 64a5b7a commit 42c424d

File tree

4 files changed

+69
-144
lines changed

4 files changed

+69
-144
lines changed

reftable/record.c

-127
Original file line numberDiff line numberDiff line change
@@ -259,58 +259,6 @@ static void reftable_ref_record_copy_from(void *rec, const void *src_rec,
259259
}
260260
}
261261

262-
static char hexdigit(int c)
263-
{
264-
if (c <= 9)
265-
return '0' + c;
266-
return 'a' + (c - 10);
267-
}
268-
269-
static void hex_format(char *dest, const unsigned char *src, int hash_size)
270-
{
271-
assert(hash_size > 0);
272-
if (src) {
273-
int i = 0;
274-
for (i = 0; i < hash_size; i++) {
275-
dest[2 * i] = hexdigit(src[i] >> 4);
276-
dest[2 * i + 1] = hexdigit(src[i] & 0xf);
277-
}
278-
dest[2 * hash_size] = 0;
279-
}
280-
}
281-
282-
static void reftable_ref_record_print_sz(const struct reftable_ref_record *ref,
283-
int hash_size)
284-
{
285-
char hex[GIT_MAX_HEXSZ + 1] = { 0 }; /* BUG */
286-
printf("ref{%s(%" PRIu64 ") ", ref->refname, ref->update_index);
287-
switch (ref->value_type) {
288-
case REFTABLE_REF_SYMREF:
289-
printf("=> %s", ref->value.symref);
290-
break;
291-
case REFTABLE_REF_VAL2:
292-
hex_format(hex, ref->value.val2.value, hash_size);
293-
printf("val 2 %s", hex);
294-
hex_format(hex, ref->value.val2.target_value,
295-
hash_size);
296-
printf("(T %s)", hex);
297-
break;
298-
case REFTABLE_REF_VAL1:
299-
hex_format(hex, ref->value.val1, hash_size);
300-
printf("val 1 %s", hex);
301-
break;
302-
case REFTABLE_REF_DELETION:
303-
printf("delete");
304-
break;
305-
}
306-
printf("}\n");
307-
}
308-
309-
void reftable_ref_record_print(const struct reftable_ref_record *ref,
310-
uint32_t hash_id) {
311-
reftable_ref_record_print_sz(ref, hash_size(hash_id));
312-
}
313-
314262
static void reftable_ref_record_release_void(void *rec)
315263
{
316264
reftable_ref_record_release(rec);
@@ -480,12 +428,6 @@ static int reftable_ref_record_cmp_void(const void *_a, const void *_b)
480428
return strcmp(a->refname, b->refname);
481429
}
482430

483-
static void reftable_ref_record_print_void(const void *rec,
484-
int hash_size)
485-
{
486-
reftable_ref_record_print_sz((struct reftable_ref_record *) rec, hash_size);
487-
}
488-
489431
static struct reftable_record_vtable reftable_ref_record_vtable = {
490432
.key = &reftable_ref_record_key,
491433
.type = BLOCK_TYPE_REF,
@@ -497,7 +439,6 @@ static struct reftable_record_vtable reftable_ref_record_vtable = {
497439
.is_deletion = &reftable_ref_record_is_deletion_void,
498440
.equal = &reftable_ref_record_equal_void,
499441
.cmp = &reftable_ref_record_cmp_void,
500-
.print = &reftable_ref_record_print_void,
501442
};
502443

503444
static void reftable_obj_record_key(const void *r, struct strbuf *dest)
@@ -516,21 +457,6 @@ static void reftable_obj_record_release(void *rec)
516457
memset(obj, 0, sizeof(struct reftable_obj_record));
517458
}
518459

519-
static void reftable_obj_record_print(const void *rec, int hash_size)
520-
{
521-
const struct reftable_obj_record *obj = rec;
522-
char hex[GIT_MAX_HEXSZ + 1] = { 0 };
523-
struct strbuf offset_str = STRBUF_INIT;
524-
int i;
525-
526-
for (i = 0; i < obj->offset_len; i++)
527-
strbuf_addf(&offset_str, "%" PRIu64 " ", obj->offsets[i]);
528-
hex_format(hex, obj->hash_prefix, obj->hash_prefix_len);
529-
printf("prefix %s (len %d), offsets [%s]\n",
530-
hex, obj->hash_prefix_len, offset_str.buf);
531-
strbuf_release(&offset_str);
532-
}
533-
534460
static void reftable_obj_record_copy_from(void *rec, const void *src_rec,
535461
int hash_size)
536462
{
@@ -701,41 +627,8 @@ static struct reftable_record_vtable reftable_obj_record_vtable = {
701627
.is_deletion = &not_a_deletion,
702628
.equal = &reftable_obj_record_equal_void,
703629
.cmp = &reftable_obj_record_cmp_void,
704-
.print = &reftable_obj_record_print,
705630
};
706631

707-
static void reftable_log_record_print_sz(struct reftable_log_record *log,
708-
int hash_size)
709-
{
710-
char hex[GIT_MAX_HEXSZ + 1] = { 0 };
711-
712-
switch (log->value_type) {
713-
case REFTABLE_LOG_DELETION:
714-
printf("log{%s(%" PRIu64 ") delete\n", log->refname,
715-
log->update_index);
716-
break;
717-
case REFTABLE_LOG_UPDATE:
718-
printf("log{%s(%" PRIu64 ") %s <%s> %" PRIu64 " %04d\n",
719-
log->refname, log->update_index,
720-
log->value.update.name ? log->value.update.name : "",
721-
log->value.update.email ? log->value.update.email : "",
722-
log->value.update.time,
723-
log->value.update.tz_offset);
724-
hex_format(hex, log->value.update.old_hash, hash_size);
725-
printf("%s => ", hex);
726-
hex_format(hex, log->value.update.new_hash, hash_size);
727-
printf("%s\n\n%s\n}\n", hex,
728-
log->value.update.message ? log->value.update.message : "");
729-
break;
730-
}
731-
}
732-
733-
void reftable_log_record_print(struct reftable_log_record *log,
734-
uint32_t hash_id)
735-
{
736-
reftable_log_record_print_sz(log, hash_size(hash_id));
737-
}
738-
739632
static void reftable_log_record_key(const void *r, struct strbuf *dest)
740633
{
741634
const struct reftable_log_record *rec =
@@ -1039,11 +932,6 @@ static int reftable_log_record_is_deletion_void(const void *p)
1039932
(const struct reftable_log_record *)p);
1040933
}
1041934

1042-
static void reftable_log_record_print_void(const void *rec, int hash_size)
1043-
{
1044-
reftable_log_record_print_sz((struct reftable_log_record*)rec, hash_size);
1045-
}
1046-
1047935
static struct reftable_record_vtable reftable_log_record_vtable = {
1048936
.key = &reftable_log_record_key,
1049937
.type = BLOCK_TYPE_LOG,
@@ -1055,7 +943,6 @@ static struct reftable_record_vtable reftable_log_record_vtable = {
1055943
.is_deletion = &reftable_log_record_is_deletion_void,
1056944
.equal = &reftable_log_record_equal_void,
1057945
.cmp = &reftable_log_record_cmp_void,
1058-
.print = &reftable_log_record_print_void,
1059946
};
1060947

1061948
static void reftable_index_record_key(const void *r, struct strbuf *dest)
@@ -1137,13 +1024,6 @@ static int reftable_index_record_cmp(const void *_a, const void *_b)
11371024
return strbuf_cmp(&a->last_key, &b->last_key);
11381025
}
11391026

1140-
static void reftable_index_record_print(const void *rec, int hash_size)
1141-
{
1142-
const struct reftable_index_record *idx = rec;
1143-
/* TODO: escape null chars? */
1144-
printf("\"%s\" %" PRIu64 "\n", idx->last_key.buf, idx->offset);
1145-
}
1146-
11471027
static struct reftable_record_vtable reftable_index_record_vtable = {
11481028
.key = &reftable_index_record_key,
11491029
.type = BLOCK_TYPE_INDEX,
@@ -1155,7 +1035,6 @@ static struct reftable_record_vtable reftable_index_record_vtable = {
11551035
.is_deletion = &not_a_deletion,
11561036
.equal = &reftable_index_record_equal,
11571037
.cmp = &reftable_index_record_cmp,
1158-
.print = &reftable_index_record_print,
11591038
};
11601039

11611040
void reftable_record_key(struct reftable_record *rec, struct strbuf *dest)
@@ -1334,9 +1213,3 @@ void reftable_record_init(struct reftable_record *rec, uint8_t typ)
13341213
BUG("unhandled record type");
13351214
}
13361215
}
1337-
1338-
void reftable_record_print(struct reftable_record *rec, int hash_size)
1339-
{
1340-
printf("'%c': ", rec->type);
1341-
reftable_record_vtable(rec)->print(reftable_record_data(rec), hash_size);
1342-
}

reftable/record.h

-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ void reftable_record_init(struct reftable_record *rec, uint8_t typ);
136136
/* see struct record_vtable */
137137
int reftable_record_cmp(struct reftable_record *a, struct reftable_record *b);
138138
int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size);
139-
void reftable_record_print(struct reftable_record *rec, int hash_size);
140139
void reftable_record_key(struct reftable_record *rec, struct strbuf *dest);
141140
void reftable_record_copy_from(struct reftable_record *rec,
142141
struct reftable_record *src, int hash_size);

reftable/reftable-record.h

-8
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ const unsigned char *reftable_ref_record_val2(const struct reftable_ref_record *
6060
/* returns whether 'ref' represents a deletion */
6161
int reftable_ref_record_is_deletion(const struct reftable_ref_record *ref);
6262

63-
/* prints a reftable_ref_record onto stdout. Useful for debugging. */
64-
void reftable_ref_record_print(const struct reftable_ref_record *ref,
65-
uint32_t hash_id);
66-
6763
/* frees and nulls all pointer values inside `ref`. */
6864
void reftable_ref_record_release(struct reftable_ref_record *ref);
6965

@@ -111,8 +107,4 @@ void reftable_log_record_release(struct reftable_log_record *log);
111107
int reftable_log_record_equal(const struct reftable_log_record *a,
112108
const struct reftable_log_record *b, int hash_size);
113109

114-
/* dumps a reftable_log_record on stdout, for debugging/testing. */
115-
void reftable_log_record_print(struct reftable_log_record *log,
116-
uint32_t hash_id);
117-
118110
#endif

t/helper/test-reftable.c

+69-8
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,33 @@ static void print_help(void)
3030
"\n");
3131
}
3232

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+
3353
static int dump_table(struct reftable_table *tab)
3454
{
3555
struct reftable_iterator it = { NULL };
3656
struct reftable_ref_record ref = { NULL };
3757
struct reftable_log_record log = { NULL };
3858
uint32_t hash_id = reftable_table_hash_id(tab);
59+
int hash_len = hash_size(hash_id);
3960
int err;
4061

4162
reftable_table_init_ref_iter(tab, &it);
@@ -45,14 +66,35 @@ static int dump_table(struct reftable_table *tab)
4566
return err;
4667

4768
while (1) {
69+
char hex[GIT_MAX_HEXSZ + 1] = { 0 }; /* BUG */
70+
4871
err = reftable_iterator_next_ref(&it, &ref);
49-
if (err > 0) {
72+
if (err > 0)
5073
break;
51-
}
52-
if (err < 0) {
74+
if (err < 0)
5375
return err;
76+
77+
printf("ref{%s(%" PRIu64 ") ", ref.refname, ref.update_index);
78+
switch (ref.value_type) {
79+
case REFTABLE_REF_SYMREF:
80+
printf("=> %s", ref.value.symref);
81+
break;
82+
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);
88+
break;
89+
case REFTABLE_REF_VAL1:
90+
hex_format(hex, ref.value.val1, hash_len);
91+
printf("val 1 %s", hex);
92+
break;
93+
case REFTABLE_REF_DELETION:
94+
printf("delete");
95+
break;
5496
}
55-
reftable_ref_record_print(&ref, hash_id);
97+
printf("}\n");
5698
}
5799
reftable_iterator_destroy(&it);
58100
reftable_ref_record_release(&ref);
@@ -64,14 +106,33 @@ static int dump_table(struct reftable_table *tab)
64106
return err;
65107

66108
while (1) {
109+
char hex[GIT_MAX_HEXSZ + 1] = { 0 };
110+
67111
err = reftable_iterator_next_log(&it, &log);
68-
if (err > 0) {
112+
if (err > 0)
69113
break;
70-
}
71-
if (err < 0) {
114+
if (err < 0)
72115
return err;
116+
117+
switch (log.value_type) {
118+
case REFTABLE_LOG_DELETION:
119+
printf("log{%s(%" PRIu64 ") delete\n", log.refname,
120+
log.update_index);
121+
break;
122+
case REFTABLE_LOG_UPDATE:
123+
printf("log{%s(%" PRIu64 ") %s <%s> %" PRIu64 " %04d\n",
124+
log.refname, log.update_index,
125+
log.value.update.name ? log.value.update.name : "",
126+
log.value.update.email ? log.value.update.email : "",
127+
log.value.update.time,
128+
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,
133+
log.value.update.message ? log.value.update.message : "");
134+
break;
73135
}
74-
reftable_log_record_print(&log, hash_id);
75136
}
76137
reftable_iterator_destroy(&it);
77138
reftable_log_record_release(&log);

0 commit comments

Comments
 (0)