builtin/bundle: have unbundle check for repo before opening its bundle
[git/gitster.git] / reftable / dump.c
blobdd65d9e8bb72a6cf6eaac08a5b3b2315f59881cc
1 /*
2 Copyright 2020 Google LLC
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 */
9 #include "git-compat-util.h"
10 #include "hash.h"
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"
21 #include <stddef.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
27 static int compact_stack(const char *stackdir)
29 struct reftable_stack *stack = NULL;
30 struct reftable_write_options opts = { 0 };
32 int err = reftable_new_stack(&stack, stackdir, &opts);
33 if (err < 0)
34 goto done;
36 err = reftable_stack_compact_all(stack, NULL);
37 if (err < 0)
38 goto done;
39 done:
40 if (stack) {
41 reftable_stack_destroy(stack);
43 return err;
46 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"
55 " -h this help\n"
56 "\n");
59 int reftable_dump_main(int argc, char *const *argv)
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;
66 uint32_t opt_hash_id = GIT_SHA1_FORMAT_ID;
67 const char *arg = NULL, *argv0 = argv[0];
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]))
77 opt_hash_id = GIT_SHA256_FORMAT_ID;
78 else if (!strcmp("-s", argv[1]))
79 opt_dump_stack = 1;
80 else if (!strcmp("-c", argv[1]))
81 opt_compact = 1;
82 else if (!strcmp("-?", argv[1]) || !strcmp("-h", argv[1])) {
83 print_help();
84 return 2;
87 if (argc != 2) {
88 fprintf(stderr, "need argument\n");
89 print_help();
90 return 2;
93 arg = argv[1];
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);
101 } else if (opt_compact) {
102 err = compact_stack(arg);
105 if (err < 0) {
106 fprintf(stderr, "%s: %s: %s\n", argv0, arg,
107 reftable_error_str(err));
108 return 1;
110 return 0;