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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 22, 2024 at 08:35 UTC 42c424d69de22cbb298eb85c48b0a6be9ce4ca96
4 files changed +69 -144
reftable/record.c
-127
@@ -259,58 +259,6 @@ static void reftable_ref_record_copy_from(void *rec, const void *src_rec,
259 }
260 }
261
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 -
262 static void reftable_ref_record_release_void(void *rec)
263 {
264 reftable_ref_record_release(rec);
@@ -480,12 +428,6 @@ static int reftable_ref_record_cmp_void(const void *_a, const void *_b)
428 return strcmp(a->refname, b->refname);
429 }
430
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 -
431 static struct reftable_record_vtable reftable_ref_record_vtable = {
432 .key = &reftable_ref_record_key,
433 .type = BLOCK_TYPE_REF,
@@ -497,7 +439,6 @@ static struct reftable_record_vtable reftable_ref_record_vtable = {
439 .is_deletion = &reftable_ref_record_is_deletion_void,
440 .equal = &reftable_ref_record_equal_void,
441 .cmp = &reftable_ref_record_cmp_void,
500 - .print = &reftable_ref_record_print_void,
442 };
443
444 static void reftable_obj_record_key(const void *r, struct strbuf *dest)
@@ -516,21 +457,6 @@ static void reftable_obj_record_release(void *rec)
457 memset(obj, 0, sizeof(struct reftable_obj_record));
458 }
459
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 -
460 static void reftable_obj_record_copy_from(void *rec, const void *src_rec,
461 int hash_size)
462 {
@@ -701,41 +627,8 @@ static struct reftable_record_vtable reftable_obj_record_vtable = {
627 .is_deletion = &not_a_deletion,
628 .equal = &reftable_obj_record_equal_void,
629 .cmp = &reftable_obj_record_cmp_void,
704 - .print = &reftable_obj_record_print,
630 };
631
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 -
632 static void reftable_log_record_key(const void *r, struct strbuf *dest)
633 {
634 const struct reftable_log_record *rec =
@@ -1039,11 +932,6 @@ static int reftable_log_record_is_deletion_void(const void *p)
932 (const struct reftable_log_record *)p);
933 }
934
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 -
935 static struct reftable_record_vtable reftable_log_record_vtable = {
936 .key = &reftable_log_record_key,
937 .type = BLOCK_TYPE_LOG,
@@ -1055,7 +943,6 @@ static struct reftable_record_vtable reftable_log_record_vtable = {
943 .is_deletion = &reftable_log_record_is_deletion_void,
944 .equal = &reftable_log_record_equal_void,
945 .cmp = &reftable_log_record_cmp_void,
1058 - .print = &reftable_log_record_print_void,
946 };
947
948 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)
1024 return strbuf_cmp(&a->last_key, &b->last_key);
1025 }
1026
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 -
1027 static struct reftable_record_vtable reftable_index_record_vtable = {
1028 .key = &reftable_index_record_key,
1029 .type = BLOCK_TYPE_INDEX,
@@ -1155,7 +1035,6 @@ static struct reftable_record_vtable reftable_index_record_vtable = {
1035 .is_deletion = &not_a_deletion,
1036 .equal = &reftable_index_record_equal,
1037 .cmp = &reftable_index_record_cmp,
1158 - .print = &reftable_index_record_print,
1038 };
1039
1040 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)
1213 BUG("unhandled record type");
1214 }
1215 }
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
@@ -136,7 +136,6 @@ void reftable_record_init(struct reftable_record *rec, uint8_t typ);
136 /* see struct record_vtable */
137 int reftable_record_cmp(struct reftable_record *a, struct reftable_record *b);
138 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);
139 void reftable_record_key(struct reftable_record *rec, struct strbuf *dest);
140 void reftable_record_copy_from(struct reftable_record *rec,
141 struct reftable_record *src, int hash_size);
reftable/reftable-record.h
-8
@@ -60,10 +60,6 @@ const unsigned char *reftable_ref_record_val2(const struct reftable_ref_record *
60 /* returns whether 'ref' represents a deletion */
61 int reftable_ref_record_is_deletion(const struct reftable_ref_record *ref);
62
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 -
63 /* frees and nulls all pointer values inside `ref`. */
64 void reftable_ref_record_release(struct reftable_ref_record *ref);
65
@@ -111,8 +107,4 @@ void reftable_log_record_release(struct reftable_log_record *log);
107 int reftable_log_record_equal(const struct reftable_log_record *a,
108 const struct reftable_log_record *b, int hash_size);
109
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 -
110 #endif
t/helper/test-reftable.c
+69 -8
@@ -30,12 +30,33 @@ static void print_help(void)
30 "\n");
31 }
32
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 +
53 static int dump_table(struct reftable_table *tab)
54 {
55 struct reftable_iterator it = { NULL };
56 struct reftable_ref_record ref = { NULL };
57 struct reftable_log_record log = { NULL };
58 uint32_t hash_id = reftable_table_hash_id(tab);
59 + int hash_len = hash_size(hash_id);
60 int err;
61
62 reftable_table_init_ref_iter(tab, &it);
@@ -45,14 +66,35 @@ static int dump_table(struct reftable_table *tab)
66 return err;
67
68 while (1) {
69 + char hex[GIT_MAX_HEXSZ + 1] = { 0 }; /* BUG */
70 +
71 err = reftable_iterator_next_ref(&it, &ref);
49 - if (err > 0) {
72 + if (err > 0)
73 break;
51 - }
52 - if (err < 0) {
74 + if (err < 0)
75 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;
96 }
55 - reftable_ref_record_print(&ref, hash_id);
97 + printf("}\n");
98 }
99 reftable_iterator_destroy(&it);
100 reftable_ref_record_release(&ref);
@@ -64,14 +106,33 @@ static int dump_table(struct reftable_table *tab)
106 return err;
107
108 while (1) {
109 + char hex[GIT_MAX_HEXSZ + 1] = { 0 };
110 +
111 err = reftable_iterator_next_log(&it, &log);
68 - if (err > 0) {
112 + if (err > 0)
113 break;
70 - }
71 - if (err < 0) {
114 + if (err < 0)
115 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;
135 }
74 - reftable_log_record_print(&log, hash_id);
136 }
137 reftable_iterator_destroy(&it);
138 reftable_log_record_release(&log);