commit-graph: detect read errors when verifying graph chain
[git.git] / builtin / commit-graph.c
blob50c15d9bfe7dfbaad388463f7a28fd293607a7cc
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 ret;
79 static struct option builtin_commit_graph_verify_options[] = {
80 OPT_BOOL(0, "shallow", &opts.shallow,
81 N_("if the commit-graph is split, only verify the tip file")),
82 OPT_BOOL(0, "progress", &opts.progress,
83 N_("force progress reporting")),
84 OPT_END(),
86 struct option *options = add_common_options(builtin_commit_graph_verify_options);
88 trace2_cmd_mode("verify");
90 opts.progress = isatty(2);
91 argc = parse_options(argc, argv, prefix,
92 options,
93 builtin_commit_graph_verify_usage, 0);
94 if (argc)
95 usage_with_options(builtin_commit_graph_verify_usage, options);
97 if (!opts.obj_dir)
98 opts.obj_dir = get_object_directory();
99 if (opts.shallow)
100 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
101 if (opts.progress)
102 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
104 odb = find_odb(the_repository, opts.obj_dir);
105 graph_name = get_commit_graph_filename(odb);
106 chain_name = get_commit_graph_chain_filename(odb);
107 if (open_commit_graph(graph_name, &fd, &st))
108 opened = OPENED_GRAPH;
109 else if (errno != ENOENT)
110 die_errno(_("Could not open commit-graph '%s'"), graph_name);
111 else if (open_commit_graph_chain(chain_name, &fd, &st))
112 opened = OPENED_CHAIN;
113 else if (errno != ENOENT)
114 die_errno(_("could not open commit-graph chain '%s'"), chain_name);
116 FREE_AND_NULL(graph_name);
117 FREE_AND_NULL(chain_name);
118 FREE_AND_NULL(options);
120 if (opened == OPENED_NONE)
121 return 0;
122 else if (opened == OPENED_GRAPH)
123 graph = load_commit_graph_one_fd_st(the_repository, fd, &st, odb);
124 else
125 graph = load_commit_graph_chain_fd_st(the_repository, fd, &st);
127 if (!graph)
128 return 1;
130 ret = verify_commit_graph(the_repository, graph, flags);
131 free_commit_graph(graph);
132 return ret;
135 extern int read_replace_refs;
136 static struct commit_graph_opts write_opts;
138 static int write_option_parse_split(const struct option *opt, const char *arg,
139 int unset)
141 enum commit_graph_split_flags *flags = opt->value;
143 BUG_ON_OPT_NEG(unset);
145 opts.split = 1;
146 if (!arg)
147 return 0;
149 if (!strcmp(arg, "no-merge"))
150 *flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED;
151 else if (!strcmp(arg, "replace"))
152 *flags = COMMIT_GRAPH_SPLIT_REPLACE;
153 else
154 die(_("unrecognized --split argument, %s"), arg);
156 return 0;
159 static int read_one_commit(struct oidset *commits, struct progress *progress,
160 const char *hash)
162 struct object *result;
163 struct object_id oid;
164 const char *end;
166 if (parse_oid_hex(hash, &oid, &end))
167 return error(_("unexpected non-hex object ID: %s"), hash);
169 result = deref_tag(the_repository, parse_object(the_repository, &oid),
170 NULL, 0);
171 if (!result)
172 return error(_("invalid object: %s"), hash);
173 else if (object_as_type(result, OBJ_COMMIT, 1))
174 oidset_insert(commits, &result->oid);
176 display_progress(progress, oidset_size(commits));
178 return 0;
181 static int write_option_max_new_filters(const struct option *opt,
182 const char *arg,
183 int unset)
185 int *to = opt->value;
186 if (unset)
187 *to = -1;
188 else {
189 const char *s;
190 *to = strtol(arg, (char **)&s, 10);
191 if (*s)
192 return error(_("option `%s' expects a numerical value"),
193 "max-new-filters");
195 return 0;
198 static int git_commit_graph_write_config(const char *var, const char *value,
199 const struct config_context *ctx,
200 void *cb UNUSED)
202 if (!strcmp(var, "commitgraph.maxnewfilters"))
203 write_opts.max_new_filters = git_config_int(var, value, ctx->kvi);
205 * No need to fall-back to 'git_default_config', since this was already
206 * called in 'cmd_commit_graph()'.
208 return 0;
211 static int graph_write(int argc, const char **argv, const char *prefix)
213 struct string_list pack_indexes = STRING_LIST_INIT_DUP;
214 struct strbuf buf = STRBUF_INIT;
215 struct oidset commits = OIDSET_INIT;
216 struct object_directory *odb = NULL;
217 int result = 0;
218 enum commit_graph_write_flags flags = 0;
219 struct progress *progress = NULL;
221 static struct option builtin_commit_graph_write_options[] = {
222 OPT_BOOL(0, "reachable", &opts.reachable,
223 N_("start walk at all refs")),
224 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
225 N_("scan pack-indexes listed by stdin for commits")),
226 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
227 N_("start walk at commits listed by stdin")),
228 OPT_BOOL(0, "append", &opts.append,
229 N_("include all commits already in the commit-graph file")),
230 OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
231 N_("enable computation for changed paths")),
232 OPT_CALLBACK_F(0, "split", &write_opts.split_flags, NULL,
233 N_("allow writing an incremental commit-graph file"),
234 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
235 write_option_parse_split),
236 OPT_INTEGER(0, "max-commits", &write_opts.max_commits,
237 N_("maximum number of commits in a non-base split commit-graph")),
238 OPT_INTEGER(0, "size-multiple", &write_opts.size_multiple,
239 N_("maximum ratio between two levels of a split commit-graph")),
240 OPT_EXPIRY_DATE(0, "expire-time", &write_opts.expire_time,
241 N_("only expire files older than a given date-time")),
242 OPT_CALLBACK_F(0, "max-new-filters", &write_opts.max_new_filters,
243 NULL, N_("maximum number of changed-path Bloom filters to compute"),
244 0, write_option_max_new_filters),
245 OPT_BOOL(0, "progress", &opts.progress,
246 N_("force progress reporting")),
247 OPT_END(),
249 struct option *options = add_common_options(builtin_commit_graph_write_options);
251 opts.progress = isatty(2);
252 opts.enable_changed_paths = -1;
253 write_opts.size_multiple = 2;
254 write_opts.max_commits = 0;
255 write_opts.expire_time = 0;
256 write_opts.max_new_filters = -1;
258 trace2_cmd_mode("write");
260 git_config(git_commit_graph_write_config, &opts);
262 argc = parse_options(argc, argv, prefix,
263 options,
264 builtin_commit_graph_write_usage, 0);
265 if (argc)
266 usage_with_options(builtin_commit_graph_write_usage, options);
268 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
269 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
270 if (!opts.obj_dir)
271 opts.obj_dir = get_object_directory();
272 if (opts.append)
273 flags |= COMMIT_GRAPH_WRITE_APPEND;
274 if (opts.split)
275 flags |= COMMIT_GRAPH_WRITE_SPLIT;
276 if (opts.progress)
277 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
278 if (!opts.enable_changed_paths)
279 flags |= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS;
280 if (opts.enable_changed_paths == 1 ||
281 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
282 flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
284 odb = find_odb(the_repository, opts.obj_dir);
286 if (opts.reachable) {
287 if (write_commit_graph_reachable(odb, flags, &write_opts))
288 result = 1;
289 goto cleanup;
292 if (opts.stdin_packs) {
293 while (strbuf_getline(&buf, stdin) != EOF)
294 string_list_append_nodup(&pack_indexes,
295 strbuf_detach(&buf, NULL));
296 } else if (opts.stdin_commits) {
297 oidset_init(&commits, 0);
298 if (opts.progress)
299 progress = start_delayed_progress(
300 _("Collecting commits from input"), 0);
302 while (strbuf_getline(&buf, stdin) != EOF) {
303 if (read_one_commit(&commits, progress, buf.buf)) {
304 result = 1;
305 goto cleanup;
309 stop_progress(&progress);
312 if (write_commit_graph(odb,
313 opts.stdin_packs ? &pack_indexes : NULL,
314 opts.stdin_commits ? &commits : NULL,
315 flags,
316 &write_opts))
317 result = 1;
319 cleanup:
320 FREE_AND_NULL(options);
321 string_list_clear(&pack_indexes, 0);
322 strbuf_release(&buf);
323 return result;
326 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
328 parse_opt_subcommand_fn *fn = NULL;
329 struct option builtin_commit_graph_options[] = {
330 OPT_SUBCOMMAND("verify", &fn, graph_verify),
331 OPT_SUBCOMMAND("write", &fn, graph_write),
332 OPT_END(),
334 struct option *options = parse_options_concat(builtin_commit_graph_options, common_opts);
336 git_config(git_default_config, NULL);
338 disable_replace_refs();
339 save_commit_buffer = 0;
341 argc = parse_options(argc, argv, prefix, options,
342 builtin_commit_graph_usage, 0);
343 FREE_AND_NULL(options);
345 return fn(argc, argv, prefix);