Skip to content

Commit 01033de

Browse files
hanwengitster
authored andcommitted
reftable: add print functions to the record types
This isn't used per se, but it is useful for debugging, especially Windows CI failures. Signed-off-by: Han-Wen Nienhuys <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 66c0dab commit 01033de

File tree

3 files changed

+95
-15
lines changed

3 files changed

+95
-15
lines changed

reftable/record.c

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ static void hex_format(char *dest, uint8_t *src, int hash_size)
255255
}
256256
}
257257

258-
void reftable_ref_record_print(const struct reftable_ref_record *ref,
259-
uint32_t hash_id)
258+
static void reftable_ref_record_print_sz(const struct reftable_ref_record *ref,
259+
int hash_size)
260260
{
261261
char hex[GIT_MAX_HEXSZ + 1] = { 0 }; /* BUG */
262262
printf("ref{%s(%" PRIu64 ") ", ref->refname, ref->update_index);
@@ -265,14 +265,14 @@ void reftable_ref_record_print(const struct reftable_ref_record *ref,
265265
printf("=> %s", ref->value.symref);
266266
break;
267267
case REFTABLE_REF_VAL2:
268-
hex_format(hex, ref->value.val2.value, hash_size(hash_id));
268+
hex_format(hex, ref->value.val2.value, hash_size);
269269
printf("val 2 %s", hex);
270270
hex_format(hex, ref->value.val2.target_value,
271-
hash_size(hash_id));
271+
hash_size);
272272
printf("(T %s)", hex);
273273
break;
274274
case REFTABLE_REF_VAL1:
275-
hex_format(hex, ref->value.val1, hash_size(hash_id));
275+
hex_format(hex, ref->value.val1, hash_size);
276276
printf("val 1 %s", hex);
277277
break;
278278
case REFTABLE_REF_DELETION:
@@ -282,6 +282,11 @@ void reftable_ref_record_print(const struct reftable_ref_record *ref,
282282
printf("}\n");
283283
}
284284

285+
void reftable_ref_record_print(const struct reftable_ref_record *ref,
286+
uint32_t hash_id) {
287+
reftable_ref_record_print_sz(ref, hash_size(hash_id));
288+
}
289+
285290
static void reftable_ref_record_release_void(void *rec)
286291
{
287292
reftable_ref_record_release(rec);
@@ -443,6 +448,12 @@ static int reftable_ref_record_equal_void(const void *a,
443448
return reftable_ref_record_equal(ra, rb, hash_size);
444449
}
445450

451+
static void reftable_ref_record_print_void(const void *rec,
452+
int hash_size)
453+
{
454+
reftable_ref_record_print_sz((struct reftable_ref_record *) rec, hash_size);
455+
}
456+
446457
static struct reftable_record_vtable reftable_ref_record_vtable = {
447458
.key = &reftable_ref_record_key,
448459
.type = BLOCK_TYPE_REF,
@@ -453,6 +464,7 @@ static struct reftable_record_vtable reftable_ref_record_vtable = {
453464
.release = &reftable_ref_record_release_void,
454465
.is_deletion = &reftable_ref_record_is_deletion_void,
455466
.equal = &reftable_ref_record_equal_void,
467+
.print = &reftable_ref_record_print_void,
456468
};
457469

458470
static void reftable_obj_record_key(const void *r, struct strbuf *dest)
@@ -471,6 +483,21 @@ static void reftable_obj_record_release(void *rec)
471483
memset(obj, 0, sizeof(struct reftable_obj_record));
472484
}
473485

486+
static void reftable_obj_record_print(const void *rec, int hash_size)
487+
{
488+
const struct reftable_obj_record *obj = rec;
489+
char hex[GIT_MAX_HEXSZ + 1] = { 0 };
490+
struct strbuf offset_str = STRBUF_INIT;
491+
int i;
492+
493+
for (i = 0; i < obj->offset_len; i++)
494+
strbuf_addf(&offset_str, "%" PRIu64 " ", obj->offsets[i]);
495+
hex_format(hex, obj->hash_prefix, obj->hash_prefix_len);
496+
printf("prefix %s (len %d), offsets [%s]\n",
497+
hex, obj->hash_prefix_len, offset_str.buf);
498+
strbuf_release(&offset_str);
499+
}
500+
474501
static void reftable_obj_record_copy_from(void *rec, const void *src_rec,
475502
int hash_size)
476503
{
@@ -617,31 +644,41 @@ static struct reftable_record_vtable reftable_obj_record_vtable = {
617644
.release = &reftable_obj_record_release,
618645
.is_deletion = &not_a_deletion,
619646
.equal = &reftable_obj_record_equal_void,
647+
.print = &reftable_obj_record_print,
620648
};
621649

622-
void reftable_log_record_print(struct reftable_log_record *log,
623-
uint32_t hash_id)
650+
static void reftable_log_record_print_sz(struct reftable_log_record *log,
651+
int hash_size)
624652
{
625653
char hex[GIT_MAX_HEXSZ + 1] = { 0 };
626654

627655
switch (log->value_type) {
628656
case REFTABLE_LOG_DELETION:
629-
printf("log{%s(%" PRIu64 ") delete", log->refname,
657+
printf("log{%s(%" PRIu64 ") delete\n", log->refname,
630658
log->update_index);
631659
break;
632660
case REFTABLE_LOG_UPDATE:
633661
printf("log{%s(%" PRIu64 ") %s <%s> %" PRIu64 " %04d\n",
634-
log->refname, log->update_index, log->value.update.name,
635-
log->value.update.email, log->value.update.time,
662+
log->refname, log->update_index,
663+
log->value.update.name ? log->value.update.name : "",
664+
log->value.update.email ? log->value.update.email : "",
665+
log->value.update.time,
636666
log->value.update.tz_offset);
637-
hex_format(hex, log->value.update.old_hash, hash_size(hash_id));
667+
hex_format(hex, log->value.update.old_hash, hash_size);
638668
printf("%s => ", hex);
639-
hex_format(hex, log->value.update.new_hash, hash_size(hash_id));
640-
printf("%s\n\n%s\n}\n", hex, log->value.update.message);
669+
hex_format(hex, log->value.update.new_hash, hash_size);
670+
printf("%s\n\n%s\n}\n", hex,
671+
log->value.update.message ? log->value.update.message : "");
641672
break;
642673
}
643674
}
644675

676+
void reftable_log_record_print(struct reftable_log_record *log,
677+
uint32_t hash_id)
678+
{
679+
reftable_log_record_print_sz(log, hash_size(hash_id));
680+
}
681+
645682
static void reftable_log_record_key(const void *r, struct strbuf *dest)
646683
{
647684
const struct reftable_log_record *rec =
@@ -959,6 +996,11 @@ static int reftable_log_record_is_deletion_void(const void *p)
959996
(const struct reftable_log_record *)p);
960997
}
961998

999+
static void reftable_log_record_print_void(const void *rec, int hash_size)
1000+
{
1001+
reftable_log_record_print_sz((struct reftable_log_record*)rec, hash_size);
1002+
}
1003+
9621004
static struct reftable_record_vtable reftable_log_record_vtable = {
9631005
.key = &reftable_log_record_key,
9641006
.type = BLOCK_TYPE_LOG,
@@ -968,7 +1010,8 @@ static struct reftable_record_vtable reftable_log_record_vtable = {
9681010
.decode = &reftable_log_record_decode,
9691011
.release = &reftable_log_record_release_void,
9701012
.is_deletion = &reftable_log_record_is_deletion_void,
971-
.equal = &reftable_log_record_equal_void
1013+
.equal = &reftable_log_record_equal_void,
1014+
.print = &reftable_log_record_print_void,
9721015
};
9731016

9741017
static void reftable_index_record_key(const void *r, struct strbuf *dest)
@@ -1043,6 +1086,13 @@ static int reftable_index_record_equal(const void *a, const void *b, int hash_si
10431086
return ia->offset == ib->offset && !strbuf_cmp(&ia->last_key, &ib->last_key);
10441087
}
10451088

1089+
static void reftable_index_record_print(const void *rec, int hash_size)
1090+
{
1091+
const struct reftable_index_record *idx = rec;
1092+
/* TODO: escape null chars? */
1093+
printf("\"%s\" %" PRIu64 "\n", idx->last_key.buf, idx->offset);
1094+
}
1095+
10461096
static struct reftable_record_vtable reftable_index_record_vtable = {
10471097
.key = &reftable_index_record_key,
10481098
.type = BLOCK_TYPE_INDEX,
@@ -1053,6 +1103,7 @@ static struct reftable_record_vtable reftable_index_record_vtable = {
10531103
.release = &reftable_index_record_release,
10541104
.is_deletion = &not_a_deletion,
10551105
.equal = &reftable_index_record_equal,
1106+
.print = &reftable_index_record_print,
10561107
};
10571108

10581109
void reftable_record_key(struct reftable_record *rec, struct strbuf *dest)
@@ -1255,3 +1306,9 @@ struct reftable_record reftable_new_record(uint8_t typ)
12551306
}
12561307
return clean;
12571308
}
1309+
1310+
void reftable_record_print(struct reftable_record *rec, int hash_size)
1311+
{
1312+
printf("'%c': ", rec->type);
1313+
reftable_record_vtable(rec)->print(reftable_record_data(rec), hash_size);
1314+
}

reftable/record.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ struct reftable_record_vtable {
6161

6262
/* Are two records equal? This assumes they have the same type. Returns 0 for non-equal. */
6363
int (*equal)(const void *a, const void *b, int hash_size);
64+
65+
/* Print on stdout, for debugging. */
66+
void (*print)(const void *rec, int hash_size);
6467
};
6568

6669
/* returns true for recognized block types. Block start with the block type. */
@@ -112,6 +115,7 @@ struct reftable_record {
112115

113116
/* see struct record_vtable */
114117
int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size);
118+
void reftable_record_print(struct reftable_record *rec, int hash_size);
115119
void reftable_record_key(struct reftable_record *rec, struct strbuf *dest);
116120
uint8_t reftable_record_type(struct reftable_record *rec);
117121
void reftable_record_copy_from(struct reftable_record *rec,

reftable/record_test.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ static void test_copy(struct reftable_record *rec)
2525
/* do it twice to catch memory leaks */
2626
reftable_record_copy_from(&copy, rec, GIT_SHA1_RAWSZ);
2727
EXPECT(reftable_record_equal(rec, &copy, GIT_SHA1_RAWSZ));
28+
29+
puts("testing print coverage:\n");
30+
reftable_record_print(&copy, GIT_SHA1_RAWSZ);
31+
2832
reftable_record_release(&copy);
2933
}
3034

@@ -176,7 +180,8 @@ static void test_reftable_log_record_equal(void)
176180
static void test_reftable_log_record_roundtrip(void)
177181
{
178182
int i;
179-
struct reftable_log_record in[2] = {
183+
184+
struct reftable_log_record in[] = {
180185
{
181186
.refname = xstrdup("refs/heads/master"),
182187
.update_index = 42,
@@ -197,10 +202,24 @@ static void test_reftable_log_record_roundtrip(void)
197202
.refname = xstrdup("refs/heads/master"),
198203
.update_index = 22,
199204
.value_type = REFTABLE_LOG_DELETION,
205+
},
206+
{
207+
.refname = xstrdup("branch"),
208+
.update_index = 33,
209+
.value_type = REFTABLE_LOG_UPDATE,
210+
.value = {
211+
.update = {
212+
.old_hash = reftable_malloc(GIT_SHA1_RAWSZ),
213+
.new_hash = reftable_malloc(GIT_SHA1_RAWSZ),
214+
/* rest of fields left empty. */
215+
},
216+
},
200217
}
201218
};
202219
set_test_hash(in[0].value.update.new_hash, 1);
203220
set_test_hash(in[0].value.update.old_hash, 2);
221+
set_test_hash(in[2].value.update.new_hash, 3);
222+
set_test_hash(in[2].value.update.old_hash, 4);
204223
for (i = 0; i < ARRAY_SIZE(in); i++) {
205224
struct reftable_record rec = { .type = BLOCK_TYPE_LOG };
206225
struct strbuf key = STRBUF_INIT;

0 commit comments

Comments
 (0)