The eleventh batch
[git/gitster.git] / builtin / refs.c
blob24978a7b7b081ac6ed8ca99016f201d34c0639f8
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "config.h"
4 #include "fsck.h"
5 #include "parse-options.h"
6 #include "refs.h"
7 #include "strbuf.h"
9 #define REFS_MIGRATE_USAGE \
10 N_("git refs migrate --ref-format=<format> [--dry-run]")
12 #define REFS_VERIFY_USAGE \
13 N_("git refs verify [--strict] [--verbose]")
15 static int cmd_refs_migrate(int argc, const char **argv, const char *prefix)
17 const char * const migrate_usage[] = {
18 REFS_MIGRATE_USAGE,
19 NULL,
21 const char *format_str = NULL;
22 enum ref_storage_format format;
23 unsigned int flags = 0;
24 struct option options[] = {
25 OPT_STRING_F(0, "ref-format", &format_str, N_("format"),
26 N_("specify the reference format to convert to"),
27 PARSE_OPT_NONEG),
28 OPT_BIT(0, "dry-run", &flags,
29 N_("perform a non-destructive dry-run"),
30 REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN),
31 OPT_END(),
33 struct strbuf errbuf = STRBUF_INIT;
34 int err;
36 argc = parse_options(argc, argv, prefix, options, migrate_usage, 0);
37 if (argc)
38 usage(_("too many arguments"));
39 if (!format_str)
40 usage(_("missing --ref-format=<format>"));
42 format = ref_storage_format_by_name(format_str);
43 if (format == REF_STORAGE_FORMAT_UNKNOWN) {
44 err = error(_("unknown ref storage format '%s'"), format_str);
45 goto out;
48 if (the_repository->ref_storage_format == format) {
49 err = error(_("repository already uses '%s' format"),
50 ref_storage_format_to_name(format));
51 goto out;
54 if (repo_migrate_ref_storage_format(the_repository, format, flags, &errbuf) < 0) {
55 err = error("%s", errbuf.buf);
56 goto out;
59 err = 0;
61 out:
62 strbuf_release(&errbuf);
63 return err;
66 static int cmd_refs_verify(int argc, const char **argv, const char *prefix)
68 struct fsck_options fsck_refs_options = FSCK_REFS_OPTIONS_DEFAULT;
69 const char * const verify_usage[] = {
70 REFS_VERIFY_USAGE,
71 NULL,
73 struct option options[] = {
74 OPT_BOOL(0, "verbose", &fsck_refs_options.verbose, N_("be verbose")),
75 OPT_BOOL(0, "strict", &fsck_refs_options.strict, N_("enable strict checking")),
76 OPT_END(),
78 int ret;
80 argc = parse_options(argc, argv, prefix, options, verify_usage, 0);
81 if (argc)
82 usage(_("'git refs verify' takes no arguments"));
84 git_config(git_fsck_config, &fsck_refs_options);
85 prepare_repo_settings(the_repository);
87 ret = refs_fsck(get_main_ref_store(the_repository), &fsck_refs_options);
89 fsck_options_clear(&fsck_refs_options);
90 return ret;
93 int cmd_refs(int argc,
94 const char **argv,
95 const char *prefix,
96 struct repository *repo UNUSED)
98 const char * const refs_usage[] = {
99 REFS_MIGRATE_USAGE,
100 REFS_VERIFY_USAGE,
101 NULL,
103 parse_opt_subcommand_fn *fn = NULL;
104 struct option opts[] = {
105 OPT_SUBCOMMAND("migrate", &fn, cmd_refs_migrate),
106 OPT_SUBCOMMAND("verify", &fn, cmd_refs_verify),
107 OPT_END(),
110 argc = parse_options(argc, argv, prefix, opts, refs_usage, 0);
111 return fn(argc, argv, prefix);