blob: ce936b4e1887ca982c9d814a014b469cbb49e52e [file] [log] [blame]
Han-Wen Nienhuyse7931682021-10-07 20:25:14 +00001/*
2Copyright 2020 Google LLC
3
4Use of this source code is governed by a BSD-style
5license that can be found in the LICENSE file or at
6https://developers.google.com/open-source/licenses/bsd
7*/
8
9#include "git-compat-util.h"
Elijah Newrend1cbe1e2023-04-22 20:17:20 +000010#include "hash-ll.h"
Han-Wen Nienhuyse7931682021-10-07 20:25:14 +000011
12#include "reftable-blocksource.h"
13#include "reftable-error.h"
14#include "reftable-merged.h"
15#include "reftable-record.h"
16#include "reftable-tests.h"
17#include "reftable-writer.h"
18#include "reftable-iterator.h"
19#include "reftable-reader.h"
20#include "reftable-stack.h"
21#include "reftable-generic.h"
22
23#include <stddef.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <string.h>
28
29static int compact_stack(const char *stackdir)
30{
31 struct reftable_stack *stack = NULL;
32 struct reftable_write_options cfg = { 0 };
33
34 int err = reftable_new_stack(&stack, stackdir, cfg);
35 if (err < 0)
36 goto done;
37
38 err = reftable_stack_compact_all(stack, NULL);
39 if (err < 0)
40 goto done;
41done:
42 if (stack) {
43 reftable_stack_destroy(stack);
44 }
45 return err;
46}
47
48static void print_help(void)
49{
50 printf("usage: dump [-cst] arg\n\n"
51 "options: \n"
52 " -c compact\n"
53 " -t dump table\n"
54 " -s dump stack\n"
55 " -6 sha256 hash format\n"
56 " -h this help\n"
57 "\n");
58}
59
60int reftable_dump_main(int argc, char *const *argv)
61{
62 int err = 0;
63 int opt_dump_table = 0;
64 int opt_dump_stack = 0;
65 int opt_compact = 0;
66 uint32_t opt_hash_id = GIT_SHA1_FORMAT_ID;
67 const char *arg = NULL, *argv0 = argv[0];
68
69 for (; argc > 1; argv++, argc--)
70 if (*argv[1] != '-')
71 break;
72 else if (!strcmp("-t", argv[1]))
73 opt_dump_table = 1;
74 else if (!strcmp("-6", argv[1]))
75 opt_hash_id = GIT_SHA256_FORMAT_ID;
76 else if (!strcmp("-s", argv[1]))
77 opt_dump_stack = 1;
78 else if (!strcmp("-c", argv[1]))
79 opt_compact = 1;
80 else if (!strcmp("-?", argv[1]) || !strcmp("-h", argv[1])) {
81 print_help();
82 return 2;
83 }
84
85 if (argc != 2) {
86 fprintf(stderr, "need argument\n");
87 print_help();
88 return 2;
89 }
90
91 arg = argv[1];
92
93 if (opt_dump_table) {
94 err = reftable_reader_print_file(arg);
95 } else if (opt_dump_stack) {
96 err = reftable_stack_print_directory(arg, opt_hash_id);
97 } else if (opt_compact) {
98 err = compact_stack(arg);
99 }
100
101 if (err < 0) {
102 fprintf(stderr, "%s: %s: %s\n", argv0, arg,
103 reftable_error_str(err));
104 return 1;
105 }
106 return 0;
107}