gpg-interface: drop pointless config_error_nonbool() checks
[alt-git.git] / builtin / commit-graph.c
blob45d035af6007a2a64833015dbe58300493a55f80
1 #include "builtin.h"
2 #include "commit.h"
3 #include "config.h"
4 #include "dir.h"
5 #include "environment.h"
6 #include "gettext.h"
7 #include "hex.h"
8 #include "lockfile.h"
9 #include "parse-options.h"
10 #include "repository.h"
11 #include "commit-graph.h"
12 #include "object-store-ll.h"
13 #include "progress.h"
14 #include "replace-object.h"
15 #include "tag.h"
16 #include "trace2.h"
18 #define BUILTIN_COMMIT_GRAPH_VERIFY_USAGE \
19 N_("git commit-graph verify [--object-dir <dir>] [--shallow] [--[no-]progress]")
21 #define BUILTIN_COMMIT_GRAPH_WRITE_USAGE \
22 N_("git commit-graph write [--object-dir <dir>] [--append]\n" \
23 " [--split[=<strategy>]] [--reachable | --stdin-packs | --stdin-commits]\n" \
24 " [--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress]\n" \
25 " <split options>")
27 static const char * builtin_commit_graph_verify_usage[] = {
28 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE,
29 NULL
32 static const char * builtin_commit_graph_write_usage[] = {
33 BUILTIN_COMMIT_GRAPH_WRITE_USAGE,
34 NULL
37 static char const * const builtin_commit_graph_usage[] = {
38 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE,
39 BUILTIN_COMMIT_GRAPH_WRITE_USAGE,
40 NULL,
43 static struct opts_commit_graph {
44 const char *obj_dir;
45 int reachable;
46 int stdin_packs;
47 int stdin_commits;
48 int append;
49 int split;
50 int shallow;
51 int progress;
52 int enable_changed_paths;
53 } opts;
55 static struct option common_opts[] = {
56 OPT_STRING(0, "object-dir", &opts.obj_dir,
57 N_("dir"),
58 N_("the object directory to store the graph")),
59 OPT_END()
62 static struct option *add_common_options(struct option *to)
64 return parse_options_concat(common_opts, to);
67 static int graph_verify(int argc, const char **argv, const char *prefix)
69 struct commit_graph *graph = NULL;
70 struct object_directory *odb = NULL;
71 char *graph_name;
72 char *chain_name;
73 enum { OPENED_NONE, OPENED_GRAPH, OPENED_CHAIN } opened = OPENED_NONE;
74 int fd;
75 struct stat st;
76 int flags = 0;
77 int incomplete_chain = 0;
78 int ret;
80 static struct option builtin_commit_graph_verify_options[] = {
81 OPT_BOOL(0, "shallow", &opts.shallow,
82 N_("if the commit-graph is split, only verify the tip file")),
83 OPT_BOOL(0, "progress", &opts.progress,
84 N_("force progress reporting")),
85 OPT_END(),
87 struct option *options = add_common_options(builtin_commit_graph_verify_options);
89 trace2_cmd_mode("verify");
91 opts.progress = isatty(2);
92 argc = parse_options(argc, argv, prefix,
93 options,
94 builtin_commit_graph_verify_usage, 0);
95 if (argc)
96 usage_with_options(builtin_commit_graph_verify_usage, options);
98 if (!opts.obj_dir)
99 opts.obj_dir = get_object_directory();
100 if (opts.shallow)
101 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
102 if (opts.progress)
103 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
105 odb = find_odb(the_repository, opts.obj_dir);
106 graph_name = get_commit_graph_filename(odb);
107 chain_name = get_commit_graph_chain_filename(odb);
108 if (open_commit_graph(graph_name, &fd, &st))
109 opened = OPENED_GRAPH;
110 else if (errno != ENOENT)
111 die_errno(_("Could not open commit-graph '%s'"), graph_name);
112 else if (open_commit_graph_chain(chain_name, &fd, &st))
113 opened = OPENED_CHAIN;
114 else if (errno != ENOENT)
115 die_errno(_("could not open commit-graph chain '%s'"), chain_name);
117 FREE_AND_NULL(graph_name);
118 FREE_AND_NULL(chain_name);
119 FREE_AND_NULL(options);
121 if (opened == OPENED_NONE)
122 return 0;
123 else if (opened == OPENED_GRAPH)
124 graph = load_commit_graph_one_fd_st(the_repository, fd, &st, odb);
125 else
126 graph = load_commit_graph_chain_fd_st(the_repository, fd, &st,
127 &incomplete_chain);
129 if (!graph)
130 return 1;
132 ret = verify_commit_graph(the_repository, graph, flags);
133 free_commit_graph(graph);
135 if (incomplete_chain) {
136 error("one or more commit-graph chain files could not be loaded");
137 ret |= 1;
140 return ret;
143 extern int read_replace_refs;
144 static struct commit_graph_opts write_opts;
146 static int write_option_parse_split(const struct option *opt, const char *arg,
147 int unset)
149 enum commit_graph_split_flags *flags = opt->value;
151 BUG_ON_OPT_NEG(unset);
153 opts.split = 1;
154 if (!arg)
155 return 0;
157 if (!strcmp(arg, "no-merge"))
158 *flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED;
159 else if (!strcmp(arg, "replace"))
160 *flags = COMMIT_GRAPH_SPLIT_REPLACE;
161 else
162 die(_("unrecognized --split argument, %s"), arg);
164 return 0;
167 static int read_one_commit(struct oidset *commits, struct progress *progress,
168 const char *hash)
170 struct object *result;
171 struct object_id oid;
172 const char *end;
174 if (parse_oid_hex(hash, &oid, &end))
175 return error(_("unexpected non-hex object ID: %s"), hash);
177 result = deref_tag(the_repository, parse_object(the_repository, &oid),
178 NULL, 0);
179 if (!result)
180 return error(_("invalid object: %s"), hash);
181 else if (object_as_type(result, OBJ_COMMIT, 1))
182 oidset_insert(commits, &result->oid);
184 display_progress(progress, oidset_size(commits));
186 return 0;
189 static int write_option_max_new_filters(const struct option *opt,
190 const char *arg,
191 int unset)
193 int *to = opt->value;
194 if (unset)
195 *to = -1;
196 else {
197 const char *s;
198 *to = strtol(arg, (char **)&s, 10);
199 if (*s)
200 return error(_("option `%s' expects a numerical value"),
201 "max-new-filters");
203 return 0;
206 static int git_commit_graph_write_config(const char *var, const char *value,
207 const struct config_context *ctx,
208 void *cb UNUSED)
210 if (!strcmp(var, "commitgraph.maxnewfilters"))
211 write_opts.max_new_filters = git_config_int(var, value, ctx->kvi);
213 * No need to fall-back to 'git_default_config', since this was already
214 * called in 'cmd_commit_graph()'.
216 return 0;
219 static int graph_write(int argc, const char **argv, const char *prefix)
221 struct string_list pack_indexes = STRING_LIST_INIT_DUP;
222 struct strbuf buf = STRBUF_INIT;
223 struct oidset commits = OIDSET_INIT;
224 struct object_directory *odb = NULL;
225 int result = 0;
226 enum commit_graph_write_flags flags = 0;
227 struct progress *progress = NULL;
229 static struct option builtin_commit_graph_write_options[] = {
230 OPT_BOOL(0, "reachable", &opts.reachable,
231 N_("start walk at all refs")),
232 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
233 N_("scan pack-indexes listed by stdin for commits")),
234 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
235 N_("start walk at commits listed by stdin")),
236 OPT_BOOL(0, "append", &opts.append,
237 N_("include all commits already in the commit-graph file")),
238 OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
239 N_("enable computation for changed paths")),
240 OPT_CALLBACK_F(0, "split", &write_opts.split_flags, NULL,
241 N_("allow writing an incremental commit-graph file"),
242 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
243 write_option_parse_split),
244 OPT_INTEGER(0, "max-commits", &write_opts.max_commits,
245 N_("maximum number of commits in a non-base split commit-graph")),
246 OPT_INTEGER(0, "size-multiple", &write_opts.size_multiple,
247 N_("maximum ratio between two levels of a split commit-graph")),
248 OPT_EXPIRY_DATE(0, "expire-time", &write_opts.expire_time,
249 N_("only expire files older than a given date-time")),
250 OPT_CALLBACK_F(0, "max-new-filters", &write_opts.max_new_filters,
251 NULL, N_("maximum number of changed-path Bloom filters to compute"),
252 0, write_option_max_new_filters),
253 OPT_BOOL(0, "progress", &opts.progress,
254 N_("force progress reporting")),
255 OPT_END(),
257 struct option *options = add_common_options(builtin_commit_graph_write_options);
259 opts.progress = isatty(2);
260 opts.enable_changed_paths = -1;
261 write_opts.size_multiple = 2;
262 write_opts.max_commits = 0;
263 write_opts.expire_time = 0;
264 write_opts.max_new_filters = -1;
266 trace2_cmd_mode("write");
268 git_config(git_commit_graph_write_config, &opts);
270 argc = parse_options(argc, argv, prefix,
271 options,
272 builtin_commit_graph_write_usage, 0);
273 if (argc)
274 usage_with_options(builtin_commit_graph_write_usage, options);
276 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
277 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
278 if (!opts.obj_dir)
279 opts.obj_dir = get_object_directory();
280 if (opts.append)
281 flags |= COMMIT_GRAPH_WRITE_APPEND;
282 if (opts.split)
283 flags |= COMMIT_GRAPH_WRITE_SPLIT;
284 if (opts.progress)
285 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
286 if (!opts.enable_changed_paths)
287 flags |= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS;
288 if (opts.enable_changed_paths == 1 ||
289 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
290 flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
292 odb = find_odb(the_repository, opts.obj_dir);
294 if (opts.reachable) {
295 if (write_commit_graph_reachable(odb, flags, &write_opts))
296 result = 1;
297 goto cleanup;
300 if (opts.stdin_packs) {
301 while (strbuf_getline(&buf, stdin) != EOF)
302 string_list_append_nodup(&pack_indexes,
303 strbuf_detach(&buf, NULL));
304 } else if (opts.stdin_commits) {
305 oidset_init(&commits, 0);
306 if (opts.progress)
307 progress = start_delayed_progress(
308 _("Collecting commits from input"), 0);
310 while (strbuf_getline(&buf, stdin) != EOF) {
311 if (read_one_commit(&commits, progress, buf.buf)) {
312 result = 1;
313 goto cleanup;
317 stop_progress(&progress);
320 if (write_commit_graph(odb,
321 opts.stdin_packs ? &pack_indexes : NULL,
322 opts.stdin_commits ? &commits : NULL,
323 flags,
324 &write_opts))
325 result = 1;
327 cleanup:
328 FREE_AND_NULL(options);
329 string_list_clear(&pack_indexes, 0);
330 strbuf_release(&buf);
331 oidset_clear(&commits);
332 return result;
335 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
337 parse_opt_subcommand_fn *fn = NULL;
338 struct option builtin_commit_graph_options[] = {
339 OPT_SUBCOMMAND("verify", &fn, graph_verify),
340 OPT_SUBCOMMAND("write", &fn, graph_write),
341 OPT_END(),
343 struct option *options = parse_options_concat(builtin_commit_graph_options, common_opts);
345 git_config(git_default_config, NULL);
347 disable_replace_refs();
348 save_commit_buffer = 0;
350 argc = parse_options(argc, argv, prefix, options,
351 builtin_commit_graph_usage, 0);
352 FREE_AND_NULL(options);
354 return fn(argc, argv, prefix);