git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Cc: karthik nayak <karthik.188@gmail.com>,
	Justin Tobler <jltobler@gmail.com>
Subject: [PATCH v3 12/15] t/helper: inline printing of reftable records
Date: Thu, 22 Aug 2024 08:35:18 +0200	[thread overview]
Message-ID: <ee22a08e11e74abadb0daa05d1023f46d689e2ed.1724308389.git.ps@pks.im> (raw)
In-Reply-To: <cover.1724308389.git.ps@pks.im>

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>
---
 reftable/record.c          | 127 -------------------------------------
 reftable/record.h          |   1 -
 reftable/reftable-record.h |   8 ---
 t/helper/test-reftable.c   |  77 +++++++++++++++++++---
 4 files changed, 69 insertions(+), 144 deletions(-)

diff --git a/reftable/record.c b/reftable/record.c
index a2cba5ef747..e26bd4bc8d9 100644
--- a/reftable/record.c
+++ b/reftable/record.c
@@ -259,58 +259,6 @@ static void reftable_ref_record_copy_from(void *rec, const void *src_rec,
 	}
 }
 
-static char hexdigit(int c)
-{
-	if (c <= 9)
-		return '0' + c;
-	return 'a' + (c - 10);
-}
-
-static void hex_format(char *dest, const unsigned char *src, int hash_size)
-{
-	assert(hash_size > 0);
-	if (src) {
-		int i = 0;
-		for (i = 0; i < hash_size; i++) {
-			dest[2 * i] = hexdigit(src[i] >> 4);
-			dest[2 * i + 1] = hexdigit(src[i] & 0xf);
-		}
-		dest[2 * hash_size] = 0;
-	}
-}
-
-static void reftable_ref_record_print_sz(const struct reftable_ref_record *ref,
-					 int hash_size)
-{
-	char hex[GIT_MAX_HEXSZ + 1] = { 0 }; /* BUG */
-	printf("ref{%s(%" PRIu64 ") ", ref->refname, ref->update_index);
-	switch (ref->value_type) {
-	case REFTABLE_REF_SYMREF:
-		printf("=> %s", ref->value.symref);
-		break;
-	case REFTABLE_REF_VAL2:
-		hex_format(hex, ref->value.val2.value, hash_size);
-		printf("val 2 %s", hex);
-		hex_format(hex, ref->value.val2.target_value,
-			   hash_size);
-		printf("(T %s)", hex);
-		break;
-	case REFTABLE_REF_VAL1:
-		hex_format(hex, ref->value.val1, hash_size);
-		printf("val 1 %s", hex);
-		break;
-	case REFTABLE_REF_DELETION:
-		printf("delete");
-		break;
-	}
-	printf("}\n");
-}
-
-void reftable_ref_record_print(const struct reftable_ref_record *ref,
-			       uint32_t hash_id) {
-	reftable_ref_record_print_sz(ref, hash_size(hash_id));
-}
-
 static void reftable_ref_record_release_void(void *rec)
 {
 	reftable_ref_record_release(rec);
@@ -480,12 +428,6 @@ static int reftable_ref_record_cmp_void(const void *_a, const void *_b)
 	return strcmp(a->refname, b->refname);
 }
 
-static void reftable_ref_record_print_void(const void *rec,
-					   int hash_size)
-{
-	reftable_ref_record_print_sz((struct reftable_ref_record *) rec, hash_size);
-}
-
 static struct reftable_record_vtable reftable_ref_record_vtable = {
 	.key = &reftable_ref_record_key,
 	.type = BLOCK_TYPE_REF,
@@ -497,7 +439,6 @@ static struct reftable_record_vtable reftable_ref_record_vtable = {
 	.is_deletion = &reftable_ref_record_is_deletion_void,
 	.equal = &reftable_ref_record_equal_void,
 	.cmp = &reftable_ref_record_cmp_void,
-	.print = &reftable_ref_record_print_void,
 };
 
 static void reftable_obj_record_key(const void *r, struct strbuf *dest)
@@ -516,21 +457,6 @@ static void reftable_obj_record_release(void *rec)
 	memset(obj, 0, sizeof(struct reftable_obj_record));
 }
 
-static void reftable_obj_record_print(const void *rec, int hash_size)
-{
-	const struct reftable_obj_record *obj = rec;
-	char hex[GIT_MAX_HEXSZ + 1] = { 0 };
-	struct strbuf offset_str = STRBUF_INIT;
-	int i;
-
-	for (i = 0; i < obj->offset_len; i++)
-		strbuf_addf(&offset_str, "%" PRIu64 " ", obj->offsets[i]);
-	hex_format(hex, obj->hash_prefix, obj->hash_prefix_len);
-	printf("prefix %s (len %d), offsets [%s]\n",
-	       hex, obj->hash_prefix_len, offset_str.buf);
-	strbuf_release(&offset_str);
-}
-
 static void reftable_obj_record_copy_from(void *rec, const void *src_rec,
 					  int hash_size)
 {
@@ -701,41 +627,8 @@ static struct reftable_record_vtable reftable_obj_record_vtable = {
 	.is_deletion = &not_a_deletion,
 	.equal = &reftable_obj_record_equal_void,
 	.cmp = &reftable_obj_record_cmp_void,
-	.print = &reftable_obj_record_print,
 };
 
-static void reftable_log_record_print_sz(struct reftable_log_record *log,
-					 int hash_size)
-{
-	char hex[GIT_MAX_HEXSZ + 1] = { 0 };
-
-	switch (log->value_type) {
-	case REFTABLE_LOG_DELETION:
-		printf("log{%s(%" PRIu64 ") delete\n", log->refname,
-		       log->update_index);
-		break;
-	case REFTABLE_LOG_UPDATE:
-		printf("log{%s(%" PRIu64 ") %s <%s> %" PRIu64 " %04d\n",
-		       log->refname, log->update_index,
-		       log->value.update.name ? log->value.update.name : "",
-		       log->value.update.email ? log->value.update.email : "",
-		       log->value.update.time,
-		       log->value.update.tz_offset);
-		hex_format(hex, log->value.update.old_hash, hash_size);
-		printf("%s => ", hex);
-		hex_format(hex, log->value.update.new_hash, hash_size);
-		printf("%s\n\n%s\n}\n", hex,
-		       log->value.update.message ? log->value.update.message : "");
-		break;
-	}
-}
-
-void reftable_log_record_print(struct reftable_log_record *log,
-				      uint32_t hash_id)
-{
-	reftable_log_record_print_sz(log, hash_size(hash_id));
-}
-
 static void reftable_log_record_key(const void *r, struct strbuf *dest)
 {
 	const struct reftable_log_record *rec =
@@ -1039,11 +932,6 @@ static int reftable_log_record_is_deletion_void(const void *p)
 		(const struct reftable_log_record *)p);
 }
 
-static void reftable_log_record_print_void(const void *rec, int hash_size)
-{
-	reftable_log_record_print_sz((struct reftable_log_record*)rec, hash_size);
-}
-
 static struct reftable_record_vtable reftable_log_record_vtable = {
 	.key = &reftable_log_record_key,
 	.type = BLOCK_TYPE_LOG,
@@ -1055,7 +943,6 @@ static struct reftable_record_vtable reftable_log_record_vtable = {
 	.is_deletion = &reftable_log_record_is_deletion_void,
 	.equal = &reftable_log_record_equal_void,
 	.cmp = &reftable_log_record_cmp_void,
-	.print = &reftable_log_record_print_void,
 };
 
 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)
 	return strbuf_cmp(&a->last_key, &b->last_key);
 }
 
-static void reftable_index_record_print(const void *rec, int hash_size)
-{
-	const struct reftable_index_record *idx = rec;
-	/* TODO: escape null chars? */
-	printf("\"%s\" %" PRIu64 "\n", idx->last_key.buf, idx->offset);
-}
-
 static struct reftable_record_vtable reftable_index_record_vtable = {
 	.key = &reftable_index_record_key,
 	.type = BLOCK_TYPE_INDEX,
@@ -1155,7 +1035,6 @@ static struct reftable_record_vtable reftable_index_record_vtable = {
 	.is_deletion = &not_a_deletion,
 	.equal = &reftable_index_record_equal,
 	.cmp = &reftable_index_record_cmp,
-	.print = &reftable_index_record_print,
 };
 
 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)
 		BUG("unhandled record type");
 	}
 }
-
-void reftable_record_print(struct reftable_record *rec, int hash_size)
-{
-	printf("'%c': ", rec->type);
-	reftable_record_vtable(rec)->print(reftable_record_data(rec), hash_size);
-}
diff --git a/reftable/record.h b/reftable/record.h
index d778133e6ec..5003bacdb0c 100644
--- a/reftable/record.h
+++ b/reftable/record.h
@@ -136,7 +136,6 @@ void reftable_record_init(struct reftable_record *rec, uint8_t typ);
 /* see struct record_vtable */
 int reftable_record_cmp(struct reftable_record *a, struct reftable_record *b);
 int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size);
-void reftable_record_print(struct reftable_record *rec, int hash_size);
 void reftable_record_key(struct reftable_record *rec, struct strbuf *dest);
 void reftable_record_copy_from(struct reftable_record *rec,
 			       struct reftable_record *src, int hash_size);
diff --git a/reftable/reftable-record.h b/reftable/reftable-record.h
index ff486eb1f75..2d42463c581 100644
--- a/reftable/reftable-record.h
+++ b/reftable/reftable-record.h
@@ -60,10 +60,6 @@ const unsigned char *reftable_ref_record_val2(const struct reftable_ref_record *
 /* returns whether 'ref' represents a deletion */
 int reftable_ref_record_is_deletion(const struct reftable_ref_record *ref);
 
-/* prints a reftable_ref_record onto stdout. Useful for debugging. */
-void reftable_ref_record_print(const struct reftable_ref_record *ref,
-			       uint32_t hash_id);
-
 /* frees and nulls all pointer values inside `ref`. */
 void reftable_ref_record_release(struct reftable_ref_record *ref);
 
@@ -111,8 +107,4 @@ void reftable_log_record_release(struct reftable_log_record *log);
 int reftable_log_record_equal(const struct reftable_log_record *a,
 			      const struct reftable_log_record *b, int hash_size);
 
-/* dumps a reftable_log_record on stdout, for debugging/testing. */
-void reftable_log_record_print(struct reftable_log_record *log,
-			       uint32_t hash_id);
-
 #endif
diff --git a/t/helper/test-reftable.c b/t/helper/test-reftable.c
index 82159fa51f4..cb22d7537a9 100644
--- a/t/helper/test-reftable.c
+++ b/t/helper/test-reftable.c
@@ -30,12 +30,33 @@ static void print_help(void)
 	       "\n");
 }
 
+static char hexdigit(int c)
+{
+	if (c <= 9)
+		return '0' + c;
+	return 'a' + (c - 10);
+}
+
+static void hex_format(char *dest, const unsigned char *src, int hash_size)
+{
+	assert(hash_size > 0);
+	if (src) {
+		int i = 0;
+		for (i = 0; i < hash_size; i++) {
+			dest[2 * i] = hexdigit(src[i] >> 4);
+			dest[2 * i + 1] = hexdigit(src[i] & 0xf);
+		}
+		dest[2 * hash_size] = 0;
+	}
+}
+
 static int dump_table(struct reftable_table *tab)
 {
 	struct reftable_iterator it = { NULL };
 	struct reftable_ref_record ref = { NULL };
 	struct reftable_log_record log = { NULL };
 	uint32_t hash_id = reftable_table_hash_id(tab);
+	int hash_len = hash_size(hash_id);
 	int err;
 
 	reftable_table_init_ref_iter(tab, &it);
@@ -45,14 +66,35 @@ static int dump_table(struct reftable_table *tab)
 		return err;
 
 	while (1) {
+		char hex[GIT_MAX_HEXSZ + 1] = { 0 }; /* BUG */
+
 		err = reftable_iterator_next_ref(&it, &ref);
-		if (err > 0) {
+		if (err > 0)
 			break;
-		}
-		if (err < 0) {
+		if (err < 0)
 			return err;
+
+		printf("ref{%s(%" PRIu64 ") ", ref.refname, ref.update_index);
+		switch (ref.value_type) {
+		case REFTABLE_REF_SYMREF:
+			printf("=> %s", ref.value.symref);
+			break;
+		case REFTABLE_REF_VAL2:
+			hex_format(hex, ref.value.val2.value, hash_len);
+			printf("val 2 %s", hex);
+			hex_format(hex, ref.value.val2.target_value,
+				   hash_len);
+			printf("(T %s)", hex);
+			break;
+		case REFTABLE_REF_VAL1:
+			hex_format(hex, ref.value.val1, hash_len);
+			printf("val 1 %s", hex);
+			break;
+		case REFTABLE_REF_DELETION:
+			printf("delete");
+			break;
 		}
-		reftable_ref_record_print(&ref, hash_id);
+		printf("}\n");
 	}
 	reftable_iterator_destroy(&it);
 	reftable_ref_record_release(&ref);
@@ -64,14 +106,33 @@ static int dump_table(struct reftable_table *tab)
 		return err;
 
 	while (1) {
+		char hex[GIT_MAX_HEXSZ + 1] = { 0 };
+
 		err = reftable_iterator_next_log(&it, &log);
-		if (err > 0) {
+		if (err > 0)
 			break;
-		}
-		if (err < 0) {
+		if (err < 0)
 			return err;
+
+		switch (log.value_type) {
+		case REFTABLE_LOG_DELETION:
+			printf("log{%s(%" PRIu64 ") delete\n", log.refname,
+			       log.update_index);
+			break;
+		case REFTABLE_LOG_UPDATE:
+			printf("log{%s(%" PRIu64 ") %s <%s> %" PRIu64 " %04d\n",
+			       log.refname, log.update_index,
+			       log.value.update.name ? log.value.update.name : "",
+			       log.value.update.email ? log.value.update.email : "",
+			       log.value.update.time,
+			       log.value.update.tz_offset);
+			hex_format(hex, log.value.update.old_hash, hash_len);
+			printf("%s => ", hex);
+			hex_format(hex, log.value.update.new_hash, hash_len);
+			printf("%s\n\n%s\n}\n", hex,
+			       log.value.update.message ? log.value.update.message : "");
+			break;
 		}
-		reftable_log_record_print(&log, hash_id);
 	}
 	reftable_iterator_destroy(&it);
 	reftable_log_record_release(&log);
-- 
2.46.0.164.g477ce5ccd6.dirty


  parent reply	other threads:[~2024-08-22  6:35 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-14 13:22 [PATCH v2 00/15] reftable: drop generic `reftable_table` interface Patrick Steinhardt
2024-08-14 13:22 ` [PATCH v2 01/15] reftable/merged: expose functions to initialize iterators Patrick Steinhardt
2024-08-14 13:22 ` [PATCH v2 02/15] reftable/merged: rename `reftable_new_merged_table()` Patrick Steinhardt
2024-08-14 13:22 ` [PATCH v2 03/15] reftable/merged: stop using generic tables in the merged table Patrick Steinhardt
2024-08-14 13:22 ` [PATCH v2 04/15] reftable/stack: open-code reading refs Patrick Steinhardt
2024-08-14 13:22 ` [PATCH v2 05/15] reftable/iter: drop double-checking logic Patrick Steinhardt
2024-08-14 13:23 ` [PATCH v2 06/15] reftable/generic: move generic iterator code into iterator interface Patrick Steinhardt
2024-08-14 13:23 ` [PATCH v2 07/15] reftable/dump: drop unused `compact_stack()` Patrick Steinhardt
2024-08-14 13:23 ` [PATCH v2 08/15] t/helper: inline `reftable_dump_main()` Patrick Steinhardt
2024-08-14 13:23 ` [PATCH v2 09/15] t/helper: inline `reftable_reader_print_file()` Patrick Steinhardt
2024-08-14 13:23 ` [PATCH v2 10/15] t/helper: inline `reftable_stack_print_directory()` Patrick Steinhardt
2024-08-20  7:34   ` karthik nayak
2024-08-20  8:06     ` Patrick Steinhardt
2024-08-14 13:23 ` [PATCH v2 11/15] t/helper: inline `reftable_table_print()` Patrick Steinhardt
2024-08-14 13:23 ` [PATCH v2 12/15] t/helper: inline printing of reftable records Patrick Steinhardt
2024-08-14 13:23 ` [PATCH v2 13/15] t/helper: use `hash_to_hex_algop()` to print hashes Patrick Steinhardt
2024-08-14 13:23 ` [PATCH v2 14/15] t/helper: refactor to not use `struct reftable_table` Patrick Steinhardt
2024-08-14 13:23 ` [PATCH v2 15/15] reftable/generic: drop interface Patrick Steinhardt
2024-08-14 13:31 ` [PATCH v2 00/15] reftable: drop generic `reftable_table` interface Patrick Steinhardt
2024-08-14 17:21   ` Junio C Hamano
2024-08-15  5:29     ` Patrick Steinhardt
2024-08-20  7:38 ` karthik nayak
2024-08-22  6:34 ` [PATCH v3 " Patrick Steinhardt
2024-08-22  6:34   ` [PATCH v3 01/15] reftable/merged: expose functions to initialize iterators Patrick Steinhardt
2024-08-22  6:34   ` [PATCH v3 02/15] reftable/merged: rename `reftable_new_merged_table()` Patrick Steinhardt
2024-08-22  6:34   ` [PATCH v3 03/15] reftable/merged: stop using generic tables in the merged table Patrick Steinhardt
2024-08-22  6:34   ` [PATCH v3 04/15] reftable/stack: open-code reading refs Patrick Steinhardt
2024-08-22  6:34   ` [PATCH v3 05/15] reftable/iter: drop double-checking logic Patrick Steinhardt
2024-08-22  6:34   ` [PATCH v3 06/15] reftable/generic: move generic iterator code into iterator interface Patrick Steinhardt
2024-08-22  6:35   ` [PATCH v3 07/15] reftable/dump: drop unused `compact_stack()` Patrick Steinhardt
2024-08-22  6:35   ` [PATCH v3 08/15] t/helper: inline `reftable_dump_main()` Patrick Steinhardt
2024-08-22  6:35   ` [PATCH v3 09/15] t/helper: inline `reftable_reader_print_file()` Patrick Steinhardt
2024-08-22  6:35   ` [PATCH v3 10/15] t/helper: inline `reftable_stack_print_directory()` Patrick Steinhardt
2024-08-22  6:35   ` [PATCH v3 11/15] t/helper: inline `reftable_table_print()` Patrick Steinhardt
2024-08-22  6:35   ` Patrick Steinhardt [this message]
2024-08-22  6:35   ` [PATCH v3 13/15] t/helper: use `hash_to_hex_algop()` to print hashes Patrick Steinhardt
2024-08-22  6:35   ` [PATCH v3 14/15] t/helper: refactor to not use `struct reftable_table` Patrick Steinhardt
2024-08-22  6:35   ` [PATCH v3 15/15] reftable/generic: drop interface Patrick Steinhardt
2024-08-22  8:03   ` [PATCH v3 00/15] reftable: drop generic `reftable_table` interface karthik nayak
2024-08-22 17:11   ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ee22a08e11e74abadb0daa05d1023f46d689e2ed.1724308389.git.ps@pks.im \
    --to=ps@pks.im \
    --cc=git@vger.kernel.org \
    --cc=jltobler@gmail.com \
    --cc=karthik.188@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).