Raw
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "config.h"
4 #include "fsck.h"
5 #include "pack-refs.h"
6 #include "parse-options.h"
7 #include "refs.h"
8 #include "strbuf.h"
9 #include "worktree.h"
10 #include "for-each-ref.h"
11 #include "refs/refs-internal.h"
12
13 #define REFS_MIGRATE_USAGE \
14 N_("git refs migrate --ref-format=<format> [--no-reflog] [--dry-run]")
15
16 #define REFS_VERIFY_USAGE \
17 N_("git refs verify [--strict] [--verbose]")
18
19 #define REFS_EXISTS_USAGE \
20 N_("git refs exists <ref>")
21
22 #define REFS_OPTIMIZE_USAGE \
23 N_("git refs optimize " PACK_REFS_OPTS)
24
25 static int cmd_refs_migrate(int argc, const char **argv, const char *prefix,
26 struct repository *repo UNUSED)
27 {
28 const char * const migrate_usage[] = {
29 REFS_MIGRATE_USAGE,
30 NULL,
31 };
32 const char *format_str = NULL;
33 enum ref_storage_format format;
34 unsigned int flags = 0;
35 struct option options[] = {
36 OPT_STRING_F(0, "ref-format", &format_str, N_("format"),
37 N_("specify the reference format to convert to"),
38 PARSE_OPT_NONEG),
39 OPT_BIT(0, "dry-run", &flags,
40 N_("perform a non-destructive dry-run"),
41 REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN),
42 OPT_BIT(0, "no-reflog", &flags,
43 N_("drop reflogs entirely during the migration"),
44 REPO_MIGRATE_REF_STORAGE_FORMAT_SKIP_REFLOG),
45 OPT_END(),
46 };
47 struct strbuf errbuf = STRBUF_INIT;
48 int err;
49
50 argc = parse_options(argc, argv, prefix, options, migrate_usage, 0);
51 if (argc)
52 usage(_("too many arguments"));
53 if (!format_str)
54 usage(_("missing --ref-format=<format>"));
55
56 format = ref_storage_format_by_name(format_str);
57 if (format == REF_STORAGE_FORMAT_UNKNOWN) {
58 err = error(_("unknown ref storage format '%s'"), format_str);
59 goto out;
60 }
61
62 if (the_repository->ref_storage_format == format) {
63 err = error(_("repository already uses '%s' format"),
64 ref_storage_format_to_name(format));
65 goto out;
66 }
67
68 if (repo_migrate_ref_storage_format(the_repository, format, flags, &errbuf) < 0) {
69 err = error("%s", errbuf.buf);
70 goto out;
71 }
72
73 err = 0;
74
75 out:
76 strbuf_release(&errbuf);
77 return err;
78 }
79
80 static int cmd_refs_verify(int argc, const char **argv, const char *prefix,
81 struct repository *repo)
82 {
83 struct fsck_options fsck_refs_options;
84 struct worktree **worktrees;
85 const char * const verify_usage[] = {
86 REFS_VERIFY_USAGE,
87 NULL,
88 };
89 struct option options[] = {
90 OPT_BOOL(0, "verbose", &fsck_refs_options.verbose, N_("be verbose")),
91 OPT_BOOL(0, "strict", &fsck_refs_options.strict, N_("enable strict checking")),
92 OPT_END(),
93 };
94 int ret = 0;
95
96 fsck_options_init(&fsck_refs_options, repo, FSCK_OPTIONS_REFS);
97
98 argc = parse_options(argc, argv, prefix, options, verify_usage, 0);
99 if (argc)
100 usage(_("'git refs verify' takes no arguments"));
101
102 repo_config(the_repository, git_fsck_config, &fsck_refs_options);
103 prepare_repo_settings(the_repository);
104
105 worktrees = get_worktrees_without_reading_head();
106 for (size_t i = 0; worktrees[i]; i++)
107 ret |= refs_fsck(get_worktree_ref_store(worktrees[i]),
108 &fsck_refs_options, worktrees[i]);
109
110 fsck_options_clear(&fsck_refs_options);
111 free_worktrees(worktrees);
112 return ret;
113 }
114
115 static int cmd_refs_list(int argc, const char **argv, const char *prefix,
116 struct repository *repo)
117 {
118 static char const * const refs_list_usage[] = {
119 N_("git refs list " COMMON_USAGE_FOR_EACH_REF),
120 NULL
121 };
122
123 return for_each_ref_core(argc, argv, prefix, repo, refs_list_usage);
124 }
125
126 static int cmd_refs_exists(int argc, const char **argv, const char *prefix,
127 struct repository *repo UNUSED)
128 {
129 struct strbuf unused_referent = STRBUF_INIT;
130 struct object_id unused_oid;
131 unsigned int unused_type;
132 int failure_errno = 0;
133 const char *ref;
134 int ret = 0;
135 const char * const exists_usage[] = {
136 REFS_EXISTS_USAGE,
137 NULL,
138 };
139 struct option options[] = {
140 OPT_END(),
141 };
142
143 argc = parse_options(argc, argv, prefix, options, exists_usage, 0);
144 if (argc != 1)
145 die(_("'git refs exists' requires a reference"));
146
147 ref = *argv++;
148 if (refs_read_raw_ref(get_main_ref_store(the_repository), ref,
149 &unused_oid, &unused_referent, &unused_type,
150 &failure_errno)) {
151 if (failure_errno == ENOENT || failure_errno == EISDIR) {
152 error(_("reference does not exist"));
153 ret = 2;
154 } else {
155 errno = failure_errno;
156 error_errno(_("failed to look up reference"));
157 ret = 1;
158 }
159
160 goto out;
161 }
162
163 out:
164 strbuf_release(&unused_referent);
165 return ret;
166 }
167
168 static int cmd_refs_optimize(int argc, const char **argv, const char *prefix,
169 struct repository *repo)
170 {
171 static char const * const refs_optimize_usage[] = {
172 REFS_OPTIMIZE_USAGE,
173 NULL
174 };
175
176 return pack_refs_core(argc, argv, prefix, repo, refs_optimize_usage);
177 }
178
179 int cmd_refs(int argc,
180 const char **argv,
181 const char *prefix,
182 struct repository *repo)
183 {
184 const char * const refs_usage[] = {
185 REFS_MIGRATE_USAGE,
186 REFS_VERIFY_USAGE,
187 "git refs list " COMMON_USAGE_FOR_EACH_REF,
188 REFS_EXISTS_USAGE,
189 REFS_OPTIMIZE_USAGE,
190 NULL,
191 };
192 parse_opt_subcommand_fn *fn = NULL;
193 struct option opts[] = {
194 OPT_SUBCOMMAND("migrate", &fn, cmd_refs_migrate),
195 OPT_SUBCOMMAND("verify", &fn, cmd_refs_verify),
196 OPT_SUBCOMMAND("list", &fn, cmd_refs_list),
197 OPT_SUBCOMMAND("exists", &fn, cmd_refs_exists),
198 OPT_SUBCOMMAND("optimize", &fn, cmd_refs_optimize),
199 OPT_END(),
200 };
201
202 argc = parse_options(argc, argv, prefix, opts, refs_usage, 0);
203 return fn(argc, argv, prefix, repo);
204 }