]> git.ipfire.org Git - thirdparty/git.git/commitdiff
reftable/dump: support dumping a table's block structure
authorPatrick Steinhardt <ps@pks.im>
Mon, 13 May 2024 08:18:13 +0000 (10:18 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 14 May 2024 00:02:38 +0000 (17:02 -0700)
We're about to introduce new configs that will allow users to have more
control over how exactly reftables are written. To verify that these
configs are effective we will need to take a peak into the actual blocks
written by the reftable backend.

Introduce a new mode to the dumping logic that prints out the block
structure. This logic can be invoked via `test-tool dump-reftables -b`.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
reftable/dump.c
reftable/reader.c
reftable/reftable-reader.h
t/t0613-reftable-write-options.sh [new file with mode: 0755]

index 586f3eb2883b62e641aedf4aedf02081f8ae77f4..41abbb8ecf888d1b4240c7a1c76d00209b19cc77 100644 (file)
@@ -48,6 +48,7 @@ static void print_help(void)
        printf("usage: dump [-cst] arg\n\n"
               "options: \n"
               "  -c compact\n"
+              "  -b dump blocks\n"
               "  -t dump table\n"
               "  -s dump stack\n"
               "  -6 sha256 hash format\n"
@@ -58,6 +59,7 @@ static void print_help(void)
 int reftable_dump_main(int argc, char *const *argv)
 {
        int err = 0;
+       int opt_dump_blocks = 0;
        int opt_dump_table = 0;
        int opt_dump_stack = 0;
        int opt_compact = 0;
@@ -67,6 +69,8 @@ int reftable_dump_main(int argc, char *const *argv)
        for (; argc > 1; argv++, argc--)
                if (*argv[1] != '-')
                        break;
+               else if (!strcmp("-b", argv[1]))
+                       opt_dump_blocks = 1;
                else if (!strcmp("-t", argv[1]))
                        opt_dump_table = 1;
                else if (!strcmp("-6", argv[1]))
@@ -88,7 +92,9 @@ int reftable_dump_main(int argc, char *const *argv)
 
        arg = argv[1];
 
-       if (opt_dump_table) {
+       if (opt_dump_blocks) {
+               err = reftable_reader_print_blocks(arg);
+       } else if (opt_dump_table) {
                err = reftable_reader_print_file(arg);
        } else if (opt_dump_stack) {
                err = reftable_stack_print_directory(arg, opt_hash_id);
index 481dff10d49b2fda49c13156b4f10e0dc0f3d276..f23c8523db4cf75985e3389401e7ce6032196d53 100644 (file)
@@ -856,3 +856,66 @@ done:
        reftable_reader_free(r);
        return err;
 }
+
+int reftable_reader_print_blocks(const char *tablename)
+{
+       struct {
+               const char *name;
+               int type;
+       } sections[] = {
+               {
+                       .name = "ref",
+                       .type = BLOCK_TYPE_REF,
+               },
+               {
+                       .name = "obj",
+                       .type = BLOCK_TYPE_OBJ,
+               },
+               {
+                       .name = "log",
+                       .type = BLOCK_TYPE_LOG,
+               },
+       };
+       struct reftable_block_source src = { 0 };
+       struct table_iter ti = TABLE_ITER_INIT;
+       struct reftable_reader *r = NULL;
+       size_t i;
+       int err;
+
+       err = reftable_block_source_from_file(&src, tablename);
+       if (err < 0)
+               goto done;
+
+       err = reftable_new_reader(&r, &src, tablename);
+       if (err < 0)
+               goto done;
+
+       printf("header:\n");
+       printf("  block_size: %d\n", r->block_size);
+
+       for (i = 0; i < ARRAY_SIZE(sections); i++) {
+               err = reader_start(r, &ti, sections[i].type, 0);
+               if (err < 0)
+                       goto done;
+               if (err > 0)
+                       continue;
+
+               printf("%s:\n", sections[i].name);
+
+               while (1) {
+                       printf("  - length: %u\n", ti.br.block_len);
+                       printf("    restarts: %u\n", ti.br.restart_count);
+
+                       err = table_iter_next_block(&ti);
+                       if (err < 0)
+                               goto done;
+                       if (err > 0)
+                               break;
+               }
+       }
+
+done:
+       reftable_reader_free(r);
+       table_iter_close(&ti);
+       return err;
+}
index 4a4bc2fdf85a542d68614cdfc500af108d4ad17c..4a04857773a69d575ec469e462d4c5e5698b767d 100644 (file)
@@ -97,5 +97,7 @@ void reftable_table_from_reader(struct reftable_table *tab,
 
 /* print table onto stdout for debugging. */
 int reftable_reader_print_file(const char *tablename);
+/* print blocks onto stdout for debugging. */
+int reftable_reader_print_blocks(const char *tablename);
 
 #endif
diff --git a/t/t0613-reftable-write-options.sh b/t/t0613-reftable-write-options.sh
new file mode 100755 (executable)
index 0000000..462980c
--- /dev/null
@@ -0,0 +1,102 @@
+#!/bin/sh
+
+test_description='reftable write options'
+
+GIT_TEST_DEFAULT_REF_FORMAT=reftable
+export GIT_TEST_DEFAULT_REF_FORMAT
+# Disable auto-compaction for all tests as we explicitly control repacking of
+# refs.
+GIT_TEST_REFTABLE_AUTOCOMPACTION=false
+export GIT_TEST_REFTABLE_AUTOCOMPACTION
+# Block sizes depend on the hash function, so we force SHA1 here.
+GIT_TEST_DEFAULT_HASH=sha1
+export GIT_TEST_DEFAULT_HASH
+# Block sizes also depend on the actual refs we write, so we force "master" to
+# be the default initial branch name.
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'default write options' '
+       test_when_finished "rm -rf repo" &&
+       git init repo &&
+       (
+               cd repo &&
+               test_commit initial &&
+               git pack-refs &&
+               cat >expect <<-EOF &&
+               header:
+                 block_size: 4096
+               ref:
+                 - length: 129
+                   restarts: 2
+               log:
+                 - length: 262
+                   restarts: 2
+               EOF
+               test-tool dump-reftable -b .git/reftable/*.ref >actual &&
+               test_cmp expect actual
+       )
+'
+
+test_expect_success 'disabled reflog writes no log blocks' '
+       test_config_global core.logAllRefUpdates false &&
+       test_when_finished "rm -rf repo" &&
+       git init repo &&
+       (
+               cd repo &&
+               test_commit initial &&
+               git pack-refs &&
+               cat >expect <<-EOF &&
+               header:
+                 block_size: 4096
+               ref:
+                 - length: 129
+                   restarts: 2
+               EOF
+               test-tool dump-reftable -b .git/reftable/*.ref >actual &&
+               test_cmp expect actual
+       )
+'
+
+test_expect_success 'many refs results in multiple blocks' '
+       test_when_finished "rm -rf repo" &&
+       git init repo &&
+       (
+               cd repo &&
+               test_commit initial &&
+               for i in $(test_seq 200)
+               do
+                       printf "update refs/heads/branch-%d HEAD\n" "$i" ||
+                       return 1
+               done >input &&
+               git update-ref --stdin <input &&
+               git pack-refs &&
+
+               cat >expect <<-EOF &&
+               header:
+                 block_size: 4096
+               ref:
+                 - length: 4049
+                   restarts: 11
+                 - length: 1136
+                   restarts: 3
+               log:
+                 - length: 4041
+                   restarts: 4
+                 - length: 4015
+                   restarts: 3
+                 - length: 4014
+                   restarts: 3
+                 - length: 4012
+                   restarts: 3
+                 - length: 3289
+                   restarts: 3
+               EOF
+               test-tool dump-reftable -b .git/reftable/*.ref >actual &&
+               test_cmp expect actual
+       )
+'
+
+test_done