Skip to content

Commit fcf3418

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c22d75b commit fcf3418

File tree

4 files changed

+174
-1
lines changed

4 files changed

+174
-1
lines changed

reftable/dump.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ static void print_help(void)
4848
printf("usage: dump [-cst] arg\n\n"
4949
"options: \n"
5050
" -c compact\n"
51+
" -b dump blocks\n"
5152
" -t dump table\n"
5253
" -s dump stack\n"
5354
" -6 sha256 hash format\n"
@@ -58,6 +59,7 @@ static void print_help(void)
5859
int reftable_dump_main(int argc, char *const *argv)
5960
{
6061
int err = 0;
62+
int opt_dump_blocks = 0;
6163
int opt_dump_table = 0;
6264
int opt_dump_stack = 0;
6365
int opt_compact = 0;
@@ -67,6 +69,8 @@ int reftable_dump_main(int argc, char *const *argv)
6769
for (; argc > 1; argv++, argc--)
6870
if (*argv[1] != '-')
6971
break;
72+
else if (!strcmp("-b", argv[1]))
73+
opt_dump_blocks = 1;
7074
else if (!strcmp("-t", argv[1]))
7175
opt_dump_table = 1;
7276
else if (!strcmp("-6", argv[1]))
@@ -88,7 +92,9 @@ int reftable_dump_main(int argc, char *const *argv)
8892

8993
arg = argv[1];
9094

91-
if (opt_dump_table) {
95+
if (opt_dump_blocks) {
96+
err = reftable_reader_print_blocks(arg);
97+
} else if (opt_dump_table) {
9298
err = reftable_reader_print_file(arg);
9399
} else if (opt_dump_stack) {
94100
err = reftable_stack_print_directory(arg, opt_hash_id);

reftable/reader.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,3 +856,66 @@ int reftable_reader_print_file(const char *tablename)
856856
reftable_reader_free(r);
857857
return err;
858858
}
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,7 @@ void reftable_table_from_reader(struct reftable_table *tab,
9797

9898
/* print table onto stdout for debugging. */
9999
int reftable_reader_print_file(const char *tablename);
100+
/* print blocks onto stdout for debugging. */
101+
int reftable_reader_print_blocks(const char *tablename);
100102

101103
#endif

t/t0613-reftable-write-options.sh

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)