git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 13/15] t/helper: use `hash_to_hex_algop()` to print hashes

From
Patrick Steinhardt <ps@pks.im>
Date
Aug 22, 2024, 06:35 UTC
Message-ID
<0a3c619e8425ac0d710f827be915931f003c00ad.1724308389.git.ps@pks.im>
In-Reply-To
<cover.1724308389.git.ps@pks.im>

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 <ps@pks.im>
---
 t/helper/test-reftable.c | 48 +++++++++-------------------------------
 1 file changed, 11 insertions(+), 37 deletions(-)
diff --git a/t/helper/test-reftable.c b/t/helper/test-reftable.c
index cb22d7537a9..234fb80010f 100644
--- a/t/helper/test-reftable.c
+++ b/t/helper/test-reftable.c
@@ -1,3 +1,6 @@
+#include "git-compat-util.h"
+#include "hash.h"
+#include "hex.h"
 #include "reftable/system.h"
 #include "reftable/reftable-error.h"
 #include "reftable/reftable-generic.h"
@@ -30,33 +33,12 @@ 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);
+	const struct git_hash_algo *algop;
 	int err;
 
 	reftable_table_init_ref_iter(tab, &it);
@@ -65,9 +47,9 @@ static int dump_table(struct reftable_table *tab)
 	if (err < 0)
 		return err;
 
-	while (1) {
-		char hex[GIT_MAX_HEXSZ + 1] = { 0 }; /* BUG */
+	algop = &hash_algos[hash_algo_by_id(reftable_table_hash_id(tab))];
 
+	while (1) {
 		err = reftable_iterator_next_ref(&it, &ref);
 		if (err > 0)
 			break;
@@ -80,15 +62,11 @@ static int dump_table(struct reftable_table *tab)
 			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);
+			printf("val 2 %s", hash_to_hex_algop(ref.value.val2.value, algop));
+			printf("(T %s)", hash_to_hex_algop(ref.value.val2.target_value, algop));
 			break;
 		case REFTABLE_REF_VAL1:
-			hex_format(hex, ref.value.val1, hash_len);
-			printf("val 1 %s", hex);
+			printf("val 1 %s", hash_to_hex_algop(ref.value.val1, algop));
 			break;
 		case REFTABLE_REF_DELETION:
 			printf("delete");
@@ -106,8 +84,6 @@ 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)
 			break;
@@ -126,10 +102,8 @@ static int dump_table(struct reftable_table *tab)
 			       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,
+			printf("%s => ", hash_to_hex_algop(log.value.update.old_hash, algop));
+			printf("%s\n\n%s\n}\n", hash_to_hex_algop(log.value.update.new_hash, algop),
 			       log.value.update.message ? log.value.update.message : "");
 			break;
 		}
-- 
2.46.0.164.g477ce5ccd6.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 36 of 40 in “reftable: drop generic `reftable_table` interface”
  1. 00/15 reftable: drop generic `reftable_table` interfacePatrick Steinhardt, Aug 14, 2024
  2. 01/15 reftable/merged: expose functions to initialize iteratorsPatrick Steinhardt, Aug 14, 2024
  3. 02/15 reftable/merged: rename `reftable_new_merged_table()`Patrick Steinhardt, Aug 14, 2024
  4. 03/15 reftable/merged: stop using generic tables in the merged tablePatrick Steinhardt, Aug 14, 2024
  5. 04/15 reftable/stack: open-code reading refsPatrick Steinhardt, Aug 14, 2024
  6. 05/15 reftable/iter: drop double-checking logicPatrick Steinhardt, Aug 14, 2024
  7. 06/15 reftable/generic: move generic iterator code into iterator interfacePatrick Steinhardt, Aug 14, 2024
  8. 07/15 reftable/dump: drop unused `compact_stack()`Patrick Steinhardt, Aug 14, 2024
  9. 08/15 t/helper: inline `reftable_dump_main()`Patrick Steinhardt, Aug 14, 2024
  10. 09/15 t/helper: inline `reftable_reader_print_file()`Patrick Steinhardt, Aug 14, 2024
  11. 10/15 t/helper: inline `reftable_stack_print_directory()`Patrick Steinhardt, Aug 14, 2024
  12. karthik nayakAug 20, 2024
  13. Patrick SteinhardtAug 20, 2024
  14. 11/15 t/helper: inline `reftable_table_print()`Patrick Steinhardt, Aug 14, 2024
  15. 12/15 t/helper: inline printing of reftable recordsPatrick Steinhardt, Aug 14, 2024
  16. 13/15 t/helper: use `hash_to_hex_algop()` to print hashesPatrick Steinhardt, Aug 14, 2024
  17. 14/15 t/helper: refactor to not use `struct reftable_table`Patrick Steinhardt, Aug 14, 2024
  18. 15/15 reftable/generic: drop interfacePatrick Steinhardt, Aug 14, 2024
  19. Patrick SteinhardtAug 14, 2024
  20. Junio C HamanoAug 14, 2024
  21. Patrick SteinhardtAug 15, 2024
  22. karthik nayakAug 20, 2024
  23. 00/15 reftable: drop generic `reftable_table` interfacePatrick Steinhardt, Aug 22, 2024
  24. 01/15 reftable/merged: expose functions to initialize iteratorsPatrick Steinhardt, Aug 22, 2024
  25. 02/15 reftable/merged: rename `reftable_new_merged_table()`Patrick Steinhardt, Aug 22, 2024
  26. 03/15 reftable/merged: stop using generic tables in the merged tablePatrick Steinhardt, Aug 22, 2024
  27. 04/15 reftable/stack: open-code reading refsPatrick Steinhardt, Aug 22, 2024
  28. 05/15 reftable/iter: drop double-checking logicPatrick Steinhardt, Aug 22, 2024
  29. 06/15 reftable/generic: move generic iterator code into iterator interfacePatrick Steinhardt, Aug 22, 2024
  30. 07/15 reftable/dump: drop unused `compact_stack()`Patrick Steinhardt, Aug 22, 2024
  31. 08/15 t/helper: inline `reftable_dump_main()`Patrick Steinhardt, Aug 22, 2024
  32. 09/15 t/helper: inline `reftable_reader_print_file()`Patrick Steinhardt, Aug 22, 2024
  33. 10/15 t/helper: inline `reftable_stack_print_directory()`Patrick Steinhardt, Aug 22, 2024
  34. 11/15 t/helper: inline `reftable_table_print()`Patrick Steinhardt, Aug 22, 2024
  35. 12/15 t/helper: inline printing of reftable recordsPatrick Steinhardt, Aug 22, 2024
  36. 13/15 t/helper: use `hash_to_hex_algop()` to print hashesPatrick Steinhardt, Aug 22, 2024
  37. 14/15 t/helper: refactor to not use `struct reftable_table`Patrick Steinhardt, Aug 22, 2024
  38. 15/15 reftable/generic: drop interfacePatrick Steinhardt, Aug 22, 2024
  39. karthik nayakAug 22, 2024
  40. Junio C HamanoAug 22, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.