reftable/dump: support dumping a table's block structure
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>
Patrick Steinhardt committed
May 13, 2024 at 10:18 UTC
fcf341890ef583f519bc2746940c048bb8261d3d
4 files changed
+174
-1
reftable/dump.c
+7
-1
@@ -48,6 +48,7 @@ static void print_help(void)
48
printf("usage: dump [-cst] arg\n\n"
49
"options: \n"
50
" -c compact\n"
51
+ " -b dump blocks\n"
52
" -t dump table\n"
53
" -s dump stack\n"
54
" -6 sha256 hash format\n"
@@ -58,6 +59,7 @@ static void print_help(void)
59
int reftable_dump_main(int argc, char *const *argv)
60
{
61
int err = 0;
62
+ int opt_dump_blocks = 0;
63
int opt_dump_table = 0;
64
int opt_dump_stack = 0;
65
int opt_compact = 0;
@@ -67,6 +69,8 @@ int reftable_dump_main(int argc, char *const *argv)
69
for (; argc > 1; argv++, argc--)
70
if (*argv[1] != '-')
71
break;
72
+ else if (!strcmp("-b", argv[1]))
73
+ opt_dump_blocks = 1;
74
else if (!strcmp("-t", argv[1]))
75
opt_dump_table = 1;
76
else if (!strcmp("-6", argv[1]))
@@ -88,7 +92,9 @@ int reftable_dump_main(int argc, char *const *argv)
92
93
arg = argv[1];
94
91
- if (opt_dump_table) {
95
+ if (opt_dump_blocks) {
96
+ err = reftable_reader_print_blocks(arg);
97
+ } else if (opt_dump_table) {
98
err = reftable_reader_print_file(arg);
99
} else if (opt_dump_stack) {
100
err = reftable_stack_print_directory(arg, opt_hash_id);
reftable/reader.c
+63
@@ -856,3 +856,66 @@ done:
856
reftable_reader_free(r);
857
return err;
858
}
859
+
860
+int reftable_reader_print_blocks(const char *tablename)
861
+{
862
+ struct {
863
+ const char *name;
864
+ int type;
865
+ } sections[] = {
866
+ {
867
+ .name = "ref",
868
+ .type = BLOCK_TYPE_REF,
869
+ },
870
+ {
871
+ .name = "obj",
872
+ .type = BLOCK_TYPE_OBJ,
873
+ },
874
+ {
875
+ .name = "log",
876
+ .type = BLOCK_TYPE_LOG,
877
+ },
878
+ };
879
+ struct reftable_block_source src = { 0 };
880
+ struct table_iter ti = TABLE_ITER_INIT;
881
+ struct reftable_reader *r = NULL;
882
+ size_t i;
883
+ int err;
884
+
885
+ err = reftable_block_source_from_file(&src, tablename);
886
+ if (err < 0)
887
+ goto done;
888
+
889
+ err = reftable_new_reader(&r, &src, tablename);
890
+ if (err < 0)
891
+ goto done;
892
+
893
+ printf("header:\n");
894
+ printf(" block_size: %d\n", r->block_size);
895
+
896
+ for (i = 0; i < ARRAY_SIZE(sections); i++) {
897
+ err = reader_start(r, &ti, sections[i].type, 0);
898
+ if (err < 0)
899
+ goto done;
900
+ if (err > 0)
901
+ continue;
902
+
903
+ printf("%s:\n", sections[i].name);
904
+
905
+ while (1) {
906
+ printf(" - length: %u\n", ti.br.block_len);
907
+ printf(" restarts: %u\n", ti.br.restart_count);
908
+
909
+ err = table_iter_next_block(&ti);
910
+ if (err < 0)
911
+ goto done;
912
+ if (err > 0)
913
+ break;
914
+ }
915
+ }
916
+
917
+done:
918
+ reftable_reader_free(r);
919
+ table_iter_close(&ti);
920
+ return err;
921
+}
reftable/reftable-reader.h
+2
@@ -97,5 +97,7 @@ void reftable_table_from_reader(struct reftable_table *tab,
97
98
/* print table onto stdout for debugging. */
99
int reftable_reader_print_file(const char *tablename);
100
+/* print blocks onto stdout for debugging. */
101
+int reftable_reader_print_blocks(const char *tablename);
102
103
#endif
t/t0613-reftable-write-options.sh
new
+102
@@ -0,0 +1,102 @@
1
+#!/bin/sh
2
+
3
+test_description='reftable write options'
4
+
5
+GIT_TEST_DEFAULT_REF_FORMAT=reftable
6
+export GIT_TEST_DEFAULT_REF_FORMAT
7
+# Disable auto-compaction for all tests as we explicitly control repacking of
8
+# refs.
9
+GIT_TEST_REFTABLE_AUTOCOMPACTION=false
10
+export GIT_TEST_REFTABLE_AUTOCOMPACTION
11
+# Block sizes depend on the hash function, so we force SHA1 here.
12
+GIT_TEST_DEFAULT_HASH=sha1
13
+export GIT_TEST_DEFAULT_HASH
14
+# Block sizes also depend on the actual refs we write, so we force "master" to
15
+# be the default initial branch name.
16
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
17
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
18
+
19
+. ./test-lib.sh
20
+
21
+test_expect_success 'default write options' '
22
+ test_when_finished "rm -rf repo" &&
23
+ git init repo &&
24
+ (
25
+ cd repo &&
26
+ test_commit initial &&
27
+ git pack-refs &&
28
+ cat >expect <<-EOF &&
29
+ header:
30
+ block_size: 4096
31
+ ref:
32
+ - length: 129
33
+ restarts: 2
34
+ log:
35
+ - length: 262
36
+ restarts: 2
37
+ EOF
38
+ test-tool dump-reftable -b .git/reftable/*.ref >actual &&
39
+ test_cmp expect actual
40
+ )
41
+'
42
+
43
+test_expect_success 'disabled reflog writes no log blocks' '
44
+ test_config_global core.logAllRefUpdates false &&
45
+ test_when_finished "rm -rf repo" &&
46
+ git init repo &&
47
+ (
48
+ cd repo &&
49
+ test_commit initial &&
50
+ git pack-refs &&
51
+ cat >expect <<-EOF &&
52
+ header:
53
+ block_size: 4096
54
+ ref:
55
+ - length: 129
56
+ restarts: 2
57
+ EOF
58
+ test-tool dump-reftable -b .git/reftable/*.ref >actual &&
59
+ test_cmp expect actual
60
+ )
61
+'
62
+
63
+test_expect_success 'many refs results in multiple blocks' '
64
+ test_when_finished "rm -rf repo" &&
65
+ git init repo &&
66
+ (
67
+ cd repo &&
68
+ test_commit initial &&
69
+ for i in $(test_seq 200)
70
+ do
71
+ printf "update refs/heads/branch-%d HEAD\n" "$i" ||
72
+ return 1
73
+ done >input &&
74
+ git update-ref --stdin <input &&
75
+ git pack-refs &&
76
+
77
+ cat >expect <<-EOF &&
78
+ header:
79
+ block_size: 4096
80
+ ref:
81
+ - length: 4049
82
+ restarts: 11
83
+ - length: 1136
84
+ restarts: 3
85
+ log:
86
+ - length: 4041
87
+ restarts: 4
88
+ - length: 4015
89
+ restarts: 3
90
+ - length: 4014
91
+ restarts: 3
92
+ - length: 4012
93
+ restarts: 3
94
+ - length: 3289
95
+ restarts: 3
96
+ EOF
97
+ test-tool dump-reftable -b .git/reftable/*.ref >actual &&
98
+ test_cmp expect actual
99
+ )
100
+'
101
+
102
+test_done