t/helper: inline `reftable_dump_main()`

The printing functionality part of `reftable/dump.c` is really only used by our "dump-reftable" test helper. It is certainly not generic logic that is useful to anybody outside of Git, and the format it generates is quite specific. Still, parts of it are used in our test suite and the output may be useful to take a peek into reftable stacks, tables and blocks. So while it does not make sense to expose this as part of the reftable library, it does make sense to keep it around. Inline the `reftable_dump_main()` function into the "dump-reftable" test helper. This clarifies that its format is subject to change and not part of our public interface. Furthermore, this allows us to iterate on the implementation in subsequent patches. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 22, 2024 at 08:35 UTC 2b06b28fd65b6f706476df229aa8ca823c44b394
4 files changed +60 -89
Makefile
-1
@@ -2680,7 +2680,6 @@ REFTABLE_OBJS += reftable/tree.o
2680 REFTABLE_OBJS += reftable/writer.o
2681
2682 REFTABLE_TEST_OBJS += reftable/block_test.o
2683 -REFTABLE_TEST_OBJS += reftable/dump.o
2683 REFTABLE_TEST_OBJS += reftable/pq_test.o
2684 REFTABLE_TEST_OBJS += reftable/readwrite_test.o
2685 REFTABLE_TEST_OBJS += reftable/stack_test.o
reftable/dump.c deleted
-86
@@ -1,86 +0,0 @@
1 -/*
2 -Copyright 2020 Google LLC
3 -
4 -Use of this source code is governed by a BSD-style
5 -license that can be found in the LICENSE file or at
6 -https://developers.google.com/open-source/licenses/bsd
7 -*/
8 -
9 -#include "git-compat-util.h"
10 -#include "hash.h"
11 -
12 -#include "reftable-blocksource.h"
13 -#include "reftable-error.h"
14 -#include "reftable-record.h"
15 -#include "reftable-tests.h"
16 -#include "reftable-writer.h"
17 -#include "reftable-iterator.h"
18 -#include "reftable-reader.h"
19 -#include "reftable-stack.h"
20 -
21 -#include <stddef.h>
22 -#include <stdio.h>
23 -#include <stdlib.h>
24 -#include <unistd.h>
25 -#include <string.h>
26 -
27 -static void print_help(void)
28 -{
29 - printf("usage: dump [-st] arg\n\n"
30 - "options: \n"
31 - " -b dump blocks\n"
32 - " -t dump table\n"
33 - " -s dump stack\n"
34 - " -6 sha256 hash format\n"
35 - " -h this help\n"
36 - "\n");
37 -}
38 -
39 -int reftable_dump_main(int argc, char *const *argv)
40 -{
41 - int err = 0;
42 - int opt_dump_blocks = 0;
43 - int opt_dump_table = 0;
44 - int opt_dump_stack = 0;
45 - uint32_t opt_hash_id = GIT_SHA1_FORMAT_ID;
46 - const char *arg = NULL, *argv0 = argv[0];
47 -
48 - for (; argc > 1; argv++, argc--)
49 - if (*argv[1] != '-')
50 - break;
51 - else if (!strcmp("-b", argv[1]))
52 - opt_dump_blocks = 1;
53 - else if (!strcmp("-t", argv[1]))
54 - opt_dump_table = 1;
55 - else if (!strcmp("-6", argv[1]))
56 - opt_hash_id = GIT_SHA256_FORMAT_ID;
57 - else if (!strcmp("-s", argv[1]))
58 - opt_dump_stack = 1;
59 - else if (!strcmp("-?", argv[1]) || !strcmp("-h", argv[1])) {
60 - print_help();
61 - return 2;
62 - }
63 -
64 - if (argc != 2) {
65 - fprintf(stderr, "need argument\n");
66 - print_help();
67 - return 2;
68 - }
69 -
70 - arg = argv[1];
71 -
72 - if (opt_dump_blocks) {
73 - err = reftable_reader_print_blocks(arg);
74 - } else if (opt_dump_table) {
75 - err = reftable_reader_print_file(arg);
76 - } else if (opt_dump_stack) {
77 - err = reftable_stack_print_directory(arg, opt_hash_id);
78 - }
79 -
80 - if (err < 0) {
81 - fprintf(stderr, "%s: %s: %s\n", argv0, arg,
82 - reftable_error_str(err));
83 - return 1;
84 - }
85 - return 0;
86 -}
reftable/reftable-tests.h
-1
@@ -16,6 +16,5 @@ int record_test_main(int argc, const char **argv);
16 int readwrite_test_main(int argc, const char **argv);
17 int stack_test_main(int argc, const char **argv);
18 int tree_test_main(int argc, const char **argv);
19 -int reftable_dump_main(int argc, char *const *argv);
19
20 #endif
t/helper/test-reftable.c
+60 -1
@@ -1,4 +1,7 @@
1 #include "reftable/system.h"
2 +#include "reftable/reftable-error.h"
3 +#include "reftable/reftable-reader.h"
4 +#include "reftable/reftable-stack.h"
5 #include "reftable/reftable-tests.h"
6 #include "test-tool.h"
7
@@ -13,7 +16,63 @@ int cmd__reftable(int argc, const char **argv)
16 return 0;
17 }
18
19 +static void print_help(void)
20 +{
21 + printf("usage: dump [-st] arg\n\n"
22 + "options: \n"
23 + " -b dump blocks\n"
24 + " -t dump table\n"
25 + " -s dump stack\n"
26 + " -6 sha256 hash format\n"
27 + " -h this help\n"
28 + "\n");
29 +}
30 +
31 int cmd__dump_reftable(int argc, const char **argv)
32 {
18 - return reftable_dump_main(argc, (char *const *)argv);
33 + int err = 0;
34 + int opt_dump_blocks = 0;
35 + int opt_dump_table = 0;
36 + int opt_dump_stack = 0;
37 + uint32_t opt_hash_id = GIT_SHA1_FORMAT_ID;
38 + const char *arg = NULL, *argv0 = argv[0];
39 +
40 + for (; argc > 1; argv++, argc--)
41 + if (*argv[1] != '-')
42 + break;
43 + else if (!strcmp("-b", argv[1]))
44 + opt_dump_blocks = 1;
45 + else if (!strcmp("-t", argv[1]))
46 + opt_dump_table = 1;
47 + else if (!strcmp("-6", argv[1]))
48 + opt_hash_id = GIT_SHA256_FORMAT_ID;
49 + else if (!strcmp("-s", argv[1]))
50 + opt_dump_stack = 1;
51 + else if (!strcmp("-?", argv[1]) || !strcmp("-h", argv[1])) {
52 + print_help();
53 + return 2;
54 + }
55 +
56 + if (argc != 2) {
57 + fprintf(stderr, "need argument\n");
58 + print_help();
59 + return 2;
60 + }
61 +
62 + arg = argv[1];
63 +
64 + if (opt_dump_blocks) {
65 + err = reftable_reader_print_blocks(arg);
66 + } else if (opt_dump_table) {
67 + err = reftable_reader_print_file(arg);
68 + } else if (opt_dump_stack) {
69 + err = reftable_stack_print_directory(arg, opt_hash_id);
70 + }
71 +
72 + if (err < 0) {
73 + fprintf(stderr, "%s: %s: %s\n", argv0, arg,
74 + reftable_error_str(err));
75 + return 1;
76 + }
77 + return 0;
78 }