object: add object_array initializer helper function
[git/gitster.git] / builtin / commit-graph.c
bloba3d00fa232b51a0ed6581a2638ca65bf50db3023
1 #include "builtin.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "environment.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "lockfile.h"
8 #include "parse-options.h"
9 #include "repository.h"
10 #include "commit-graph.h"
11 #include "object-store.h"
12 #include "progress.h"
13 #include "replace-object.h"
14 #include "tag.h"
15 #include "trace2.h"
17 #define BUILTIN_COMMIT_GRAPH_VERIFY_USAGE \
18 N_("git commit-graph verify [--object-dir <dir>] [--shallow] [--[no-]progress]")
20 #define BUILTIN_COMMIT_GRAPH_WRITE_USAGE \
21 N_("git commit-graph write [--object-dir <dir>] [--append]\n" \
22 " [--split[=<strategy>]] [--reachable | --stdin-packs | --stdin-commits]\n" \
23 " [--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress]\n" \
24 " <split options>")
26 static const char * builtin_commit_graph_verify_usage[] = {
27 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE,
28 NULL
31 static const char * builtin_commit_graph_write_usage[] = {
32 BUILTIN_COMMIT_GRAPH_WRITE_USAGE,
33 NULL
36 static char const * const builtin_commit_graph_usage[] = {
37 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE,
38 BUILTIN_COMMIT_GRAPH_WRITE_USAGE,
39 NULL,
42 static struct opts_commit_graph {
43 const char *obj_dir;
44 int reachable;
45 int stdin_packs;
46 int stdin_commits;
47 int append;
48 int split;
49 int shallow;
50 int progress;
51 int enable_changed_paths;
52 } opts;
54 static struct option common_opts[] = {
55 OPT_STRING(0, "object-dir", &opts.obj_dir,
56 N_("dir"),
57 N_("the object directory to store the graph")),
58 OPT_END()
61 static struct option *add_common_options(struct option *to)
63 return parse_options_concat(common_opts, to);
66 static int graph_verify(int argc, const char **argv, const char *prefix)
68 struct commit_graph *graph = NULL;
69 struct object_directory *odb = NULL;
70 char *graph_name;
71 int open_ok;
72 int fd;
73 struct stat st;
74 int flags = 0;
75 int ret;
77 static struct option builtin_commit_graph_verify_options[] = {
78 OPT_BOOL(0, "shallow", &opts.shallow,
79 N_("if the commit-graph is split, only verify the tip file")),
80 OPT_BOOL(0, "progress", &opts.progress,
81 N_("force progress reporting")),
82 OPT_END(),
84 struct option *options = add_common_options(builtin_commit_graph_verify_options);
86 trace2_cmd_mode("verify");
88 opts.progress = isatty(2);
89 argc = parse_options(argc, argv, prefix,
90 options,
91 builtin_commit_graph_verify_usage, 0);
92 if (argc)
93 usage_with_options(builtin_commit_graph_verify_usage, options);
95 if (!opts.obj_dir)
96 opts.obj_dir = get_object_directory();
97 if (opts.shallow)
98 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
99 if (opts.progress)
100 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
102 odb = find_odb(the_repository, opts.obj_dir);
103 graph_name = get_commit_graph_filename(odb);
104 open_ok = open_commit_graph(graph_name, &fd, &st);
105 if (!open_ok && errno != ENOENT)
106 die_errno(_("Could not open commit-graph '%s'"), graph_name);
108 FREE_AND_NULL(graph_name);
109 FREE_AND_NULL(options);
111 if (open_ok)
112 graph = load_commit_graph_one_fd_st(the_repository, fd, &st, odb);
113 else
114 graph = read_commit_graph_one(the_repository, odb);
116 /* Return failure if open_ok predicted success */
117 if (!graph)
118 return !!open_ok;
120 ret = verify_commit_graph(the_repository, graph, flags);
121 free_commit_graph(graph);
122 return ret;
125 extern int read_replace_refs;
126 static struct commit_graph_opts write_opts;
128 static int write_option_parse_split(const struct option *opt, const char *arg,
129 int unset)
131 enum commit_graph_split_flags *flags = opt->value;
133 BUG_ON_OPT_NEG(unset);
135 opts.split = 1;
136 if (!arg)
137 return 0;
139 if (!strcmp(arg, "no-merge"))
140 *flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED;
141 else if (!strcmp(arg, "replace"))
142 *flags = COMMIT_GRAPH_SPLIT_REPLACE;
143 else
144 die(_("unrecognized --split argument, %s"), arg);
146 return 0;
149 static int read_one_commit(struct oidset *commits, struct progress *progress,
150 const char *hash)
152 struct object *result;
153 struct object_id oid;
154 const char *end;
156 if (parse_oid_hex(hash, &oid, &end))
157 return error(_("unexpected non-hex object ID: %s"), hash);
159 result = deref_tag(the_repository, parse_object(the_repository, &oid),
160 NULL, 0);
161 if (!result)
162 return error(_("invalid object: %s"), hash);
163 else if (object_as_type(result, OBJ_COMMIT, 1))
164 oidset_insert(commits, &result->oid);
166 display_progress(progress, oidset_size(commits));
168 return 0;
171 static int write_option_max_new_filters(const struct option *opt,
172 const char *arg,
173 int unset)
175 int *to = opt->value;
176 if (unset)
177 *to = -1;
178 else {
179 const char *s;
180 *to = strtol(arg, (char **)&s, 10);
181 if (*s)
182 return error(_("option `%s' expects a numerical value"),
183 "max-new-filters");
185 return 0;
188 static int git_commit_graph_write_config(const char *var, const char *value,
189 void *cb UNUSED)
191 if (!strcmp(var, "commitgraph.maxnewfilters"))
192 write_opts.max_new_filters = git_config_int(var, value);
194 * No need to fall-back to 'git_default_config', since this was already
195 * called in 'cmd_commit_graph()'.
197 return 0;
200 static int graph_write(int argc, const char **argv, const char *prefix)
202 struct string_list pack_indexes = STRING_LIST_INIT_DUP;
203 struct strbuf buf = STRBUF_INIT;
204 struct oidset commits = OIDSET_INIT;
205 struct object_directory *odb = NULL;
206 int result = 0;
207 enum commit_graph_write_flags flags = 0;
208 struct progress *progress = NULL;
210 static struct option builtin_commit_graph_write_options[] = {
211 OPT_BOOL(0, "reachable", &opts.reachable,
212 N_("start walk at all refs")),
213 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
214 N_("scan pack-indexes listed by stdin for commits")),
215 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
216 N_("start walk at commits listed by stdin")),
217 OPT_BOOL(0, "append", &opts.append,
218 N_("include all commits already in the commit-graph file")),
219 OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
220 N_("enable computation for changed paths")),
221 OPT_CALLBACK_F(0, "split", &write_opts.split_flags, NULL,
222 N_("allow writing an incremental commit-graph file"),
223 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
224 write_option_parse_split),
225 OPT_INTEGER(0, "max-commits", &write_opts.max_commits,
226 N_("maximum number of commits in a non-base split commit-graph")),
227 OPT_INTEGER(0, "size-multiple", &write_opts.size_multiple,
228 N_("maximum ratio between two levels of a split commit-graph")),
229 OPT_EXPIRY_DATE(0, "expire-time", &write_opts.expire_time,
230 N_("only expire files older than a given date-time")),
231 OPT_CALLBACK_F(0, "max-new-filters", &write_opts.max_new_filters,
232 NULL, N_("maximum number of changed-path Bloom filters to compute"),
233 0, write_option_max_new_filters),
234 OPT_BOOL(0, "progress", &opts.progress,
235 N_("force progress reporting")),
236 OPT_END(),
238 struct option *options = add_common_options(builtin_commit_graph_write_options);
240 opts.progress = isatty(2);
241 opts.enable_changed_paths = -1;
242 write_opts.size_multiple = 2;
243 write_opts.max_commits = 0;
244 write_opts.expire_time = 0;
245 write_opts.max_new_filters = -1;
247 trace2_cmd_mode("write");
249 git_config(git_commit_graph_write_config, &opts);
251 argc = parse_options(argc, argv, prefix,
252 options,
253 builtin_commit_graph_write_usage, 0);
254 if (argc)
255 usage_with_options(builtin_commit_graph_write_usage, options);
257 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
258 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
259 if (!opts.obj_dir)
260 opts.obj_dir = get_object_directory();
261 if (opts.append)
262 flags |= COMMIT_GRAPH_WRITE_APPEND;
263 if (opts.split)
264 flags |= COMMIT_GRAPH_WRITE_SPLIT;
265 if (opts.progress)
266 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
267 if (!opts.enable_changed_paths)
268 flags |= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS;
269 if (opts.enable_changed_paths == 1 ||
270 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
271 flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
273 odb = find_odb(the_repository, opts.obj_dir);
275 if (opts.reachable) {
276 if (write_commit_graph_reachable(odb, flags, &write_opts))
277 result = 1;
278 goto cleanup;
281 if (opts.stdin_packs) {
282 while (strbuf_getline(&buf, stdin) != EOF)
283 string_list_append_nodup(&pack_indexes,
284 strbuf_detach(&buf, NULL));
285 } else if (opts.stdin_commits) {
286 oidset_init(&commits, 0);
287 if (opts.progress)
288 progress = start_delayed_progress(
289 _("Collecting commits from input"), 0);
291 while (strbuf_getline(&buf, stdin) != EOF) {
292 if (read_one_commit(&commits, progress, buf.buf)) {
293 result = 1;
294 goto cleanup;
298 stop_progress(&progress);
301 if (write_commit_graph(odb,
302 opts.stdin_packs ? &pack_indexes : NULL,
303 opts.stdin_commits ? &commits : NULL,
304 flags,
305 &write_opts))
306 result = 1;
308 cleanup:
309 FREE_AND_NULL(options);
310 string_list_clear(&pack_indexes, 0);
311 strbuf_release(&buf);
312 return result;
315 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
317 parse_opt_subcommand_fn *fn = NULL;
318 struct option builtin_commit_graph_options[] = {
319 OPT_SUBCOMMAND("verify", &fn, graph_verify),
320 OPT_SUBCOMMAND("write", &fn, graph_write),
321 OPT_END(),
323 struct option *options = parse_options_concat(builtin_commit_graph_options, common_opts);
325 git_config(git_default_config, NULL);
327 read_replace_refs = 0;
328 save_commit_buffer = 0;
330 argc = parse_options(argc, argv, prefix, options,
331 builtin_commit_graph_usage, 0);
332 FREE_AND_NULL(options);
334 return fn(argc, argv, prefix);