The eleventh batch
[git/gitster.git] / builtin / commit-graph.c
blob7c991db6eb48ad6e935727a079abac02bb358f8a
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "commit.h"
4 #include "config.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "parse-options.h"
8 #include "commit-graph.h"
9 #include "object-store-ll.h"
10 #include "progress.h"
11 #include "replace-object.h"
12 #include "strbuf.h"
13 #include "tag.h"
14 #include "trace2.h"
16 #define BUILTIN_COMMIT_GRAPH_VERIFY_USAGE \
17 N_("git commit-graph verify [--object-dir <dir>] [--shallow] [--[no-]progress]")
19 #define BUILTIN_COMMIT_GRAPH_WRITE_USAGE \
20 N_("git commit-graph write [--object-dir <dir>] [--append]\n" \
21 " [--split[=<strategy>]] [--reachable | --stdin-packs | --stdin-commits]\n" \
22 " [--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress]\n" \
23 " <split-options>")
25 static const char * builtin_commit_graph_verify_usage[] = {
26 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE,
27 NULL
30 static const char * builtin_commit_graph_write_usage[] = {
31 BUILTIN_COMMIT_GRAPH_WRITE_USAGE,
32 NULL
35 static char const * const builtin_commit_graph_usage[] = {
36 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE,
37 BUILTIN_COMMIT_GRAPH_WRITE_USAGE,
38 NULL,
41 static struct opts_commit_graph {
42 const char *obj_dir;
43 int reachable;
44 int stdin_packs;
45 int stdin_commits;
46 int append;
47 int split;
48 int shallow;
49 int progress;
50 int enable_changed_paths;
51 } opts;
53 static struct option common_opts[] = {
54 OPT_STRING(0, "object-dir", &opts.obj_dir,
55 N_("dir"),
56 N_("the object directory to store the graph")),
57 OPT_END()
60 static struct option *add_common_options(struct option *to)
62 return parse_options_concat(common_opts, to);
65 static int graph_verify(int argc, const char **argv, const char *prefix)
67 struct commit_graph *graph = NULL;
68 struct object_directory *odb = NULL;
69 char *graph_name;
70 char *chain_name;
71 enum { OPENED_NONE, OPENED_GRAPH, OPENED_CHAIN } opened = OPENED_NONE;
72 int fd;
73 struct stat st;
74 int flags = 0;
75 int incomplete_chain = 0;
76 int ret;
78 static struct option builtin_commit_graph_verify_options[] = {
79 OPT_BOOL(0, "shallow", &opts.shallow,
80 N_("if the commit-graph is split, only verify the tip file")),
81 OPT_BOOL(0, "progress", &opts.progress,
82 N_("force progress reporting")),
83 OPT_END(),
85 struct option *options = add_common_options(builtin_commit_graph_verify_options);
87 trace2_cmd_mode("verify");
89 opts.progress = isatty(2);
90 argc = parse_options(argc, argv, prefix,
91 options,
92 builtin_commit_graph_verify_usage, 0);
93 if (argc)
94 usage_with_options(builtin_commit_graph_verify_usage, options);
96 if (!opts.obj_dir)
97 opts.obj_dir = repo_get_object_directory(the_repository);
98 if (opts.shallow)
99 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
100 if (opts.progress)
101 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
103 odb = find_odb(the_repository, opts.obj_dir);
104 graph_name = get_commit_graph_filename(odb);
105 chain_name = get_commit_graph_chain_filename(odb);
106 if (open_commit_graph(graph_name, &fd, &st))
107 opened = OPENED_GRAPH;
108 else if (errno != ENOENT)
109 die_errno(_("Could not open commit-graph '%s'"), graph_name);
110 else if (open_commit_graph_chain(chain_name, &fd, &st))
111 opened = OPENED_CHAIN;
112 else if (errno != ENOENT)
113 die_errno(_("could not open commit-graph chain '%s'"), chain_name);
115 FREE_AND_NULL(graph_name);
116 FREE_AND_NULL(chain_name);
117 FREE_AND_NULL(options);
119 if (opened == OPENED_NONE)
120 return 0;
121 else if (opened == OPENED_GRAPH)
122 graph = load_commit_graph_one_fd_st(the_repository, fd, &st, odb);
123 else
124 graph = load_commit_graph_chain_fd_st(the_repository, fd, &st,
125 &incomplete_chain);
127 if (!graph)
128 return 1;
130 ret = verify_commit_graph(the_repository, graph, flags);
131 free_commit_graph(graph);
133 if (incomplete_chain) {
134 error("one or more commit-graph chain files could not be loaded");
135 ret |= 1;
138 return ret;
141 extern int read_replace_refs;
142 static struct commit_graph_opts write_opts;
144 static int write_option_parse_split(const struct option *opt, const char *arg,
145 int unset)
147 enum commit_graph_split_flags *flags = opt->value;
149 BUG_ON_OPT_NEG(unset);
151 opts.split = 1;
152 if (!arg)
153 return 0;
155 if (!strcmp(arg, "no-merge"))
156 *flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED;
157 else if (!strcmp(arg, "replace"))
158 *flags = COMMIT_GRAPH_SPLIT_REPLACE;
159 else
160 die(_("unrecognized --split argument, %s"), arg);
162 return 0;
165 static int read_one_commit(struct oidset *commits, struct progress *progress,
166 const char *hash)
168 struct object *result;
169 struct object_id oid;
170 const char *end;
172 if (parse_oid_hex(hash, &oid, &end))
173 return error(_("unexpected non-hex object ID: %s"), hash);
175 result = deref_tag(the_repository, parse_object(the_repository, &oid),
176 NULL, 0);
177 if (!result)
178 return error(_("invalid object: %s"), hash);
179 else if (object_as_type(result, OBJ_COMMIT, 1))
180 oidset_insert(commits, &result->oid);
182 display_progress(progress, oidset_size(commits));
184 return 0;
187 static int write_option_max_new_filters(const struct option *opt,
188 const char *arg,
189 int unset)
191 int *to = opt->value;
192 if (unset)
193 *to = -1;
194 else {
195 const char *s;
196 *to = strtol(arg, (char **)&s, 10);
197 if (*s)
198 return error(_("option `%s' expects a numerical value"),
199 "max-new-filters");
201 return 0;
204 static int git_commit_graph_write_config(const char *var, const char *value,
205 const struct config_context *ctx,
206 void *cb UNUSED)
208 if (!strcmp(var, "commitgraph.maxnewfilters"))
209 write_opts.max_new_filters = git_config_int(var, value, ctx->kvi);
211 * No need to fall-back to 'git_default_config', since this was already
212 * called in 'cmd_commit_graph()'.
214 return 0;
217 static int graph_write(int argc, const char **argv, const char *prefix)
219 struct string_list pack_indexes = STRING_LIST_INIT_DUP;
220 struct strbuf buf = STRBUF_INIT;
221 struct oidset commits = OIDSET_INIT;
222 struct object_directory *odb = NULL;
223 int result = 0;
224 enum commit_graph_write_flags flags = 0;
225 struct progress *progress = NULL;
227 static struct option builtin_commit_graph_write_options[] = {
228 OPT_BOOL(0, "reachable", &opts.reachable,
229 N_("start walk at all refs")),
230 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
231 N_("scan pack-indexes listed by stdin for commits")),
232 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
233 N_("start walk at commits listed by stdin")),
234 OPT_BOOL(0, "append", &opts.append,
235 N_("include all commits already in the commit-graph file")),
236 OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
237 N_("enable computation for changed paths")),
238 OPT_CALLBACK_F(0, "split", &write_opts.split_flags, NULL,
239 N_("allow writing an incremental commit-graph file"),
240 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
241 write_option_parse_split),
242 OPT_INTEGER(0, "max-commits", &write_opts.max_commits,
243 N_("maximum number of commits in a non-base split commit-graph")),
244 OPT_INTEGER(0, "size-multiple", &write_opts.size_multiple,
245 N_("maximum ratio between two levels of a split commit-graph")),
246 OPT_EXPIRY_DATE(0, "expire-time", &write_opts.expire_time,
247 N_("only expire files older than a given date-time")),
248 OPT_CALLBACK_F(0, "max-new-filters", &write_opts.max_new_filters,
249 NULL, N_("maximum number of changed-path Bloom filters to compute"),
250 0, write_option_max_new_filters),
251 OPT_BOOL(0, "progress", &opts.progress,
252 N_("force progress reporting")),
253 OPT_END(),
255 struct option *options = add_common_options(builtin_commit_graph_write_options);
257 opts.progress = isatty(2);
258 opts.enable_changed_paths = -1;
259 write_opts.size_multiple = 2;
260 write_opts.max_commits = 0;
261 write_opts.expire_time = 0;
262 write_opts.max_new_filters = -1;
264 trace2_cmd_mode("write");
266 git_config(git_commit_graph_write_config, &opts);
268 argc = parse_options(argc, argv, prefix,
269 options,
270 builtin_commit_graph_write_usage, 0);
271 if (argc)
272 usage_with_options(builtin_commit_graph_write_usage, options);
274 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
275 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
276 if (!opts.obj_dir)
277 opts.obj_dir = repo_get_object_directory(the_repository);
278 if (opts.append)
279 flags |= COMMIT_GRAPH_WRITE_APPEND;
280 if (opts.split)
281 flags |= COMMIT_GRAPH_WRITE_SPLIT;
282 if (opts.progress)
283 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
284 if (!opts.enable_changed_paths)
285 flags |= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS;
286 if (opts.enable_changed_paths == 1 ||
287 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
288 flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
290 odb = find_odb(the_repository, opts.obj_dir);
292 if (opts.reachable) {
293 if (write_commit_graph_reachable(odb, flags, &write_opts))
294 result = 1;
295 goto cleanup;
298 if (opts.stdin_packs) {
299 while (strbuf_getline(&buf, stdin) != EOF)
300 string_list_append_nodup(&pack_indexes,
301 strbuf_detach(&buf, NULL));
302 } else if (opts.stdin_commits) {
303 oidset_init(&commits, 0);
304 if (opts.progress)
305 progress = start_delayed_progress(
306 _("Collecting commits from input"), 0);
308 while (strbuf_getline(&buf, stdin) != EOF) {
309 if (read_one_commit(&commits, progress, buf.buf)) {
310 result = 1;
311 goto cleanup;
315 stop_progress(&progress);
318 if (write_commit_graph(odb,
319 opts.stdin_packs ? &pack_indexes : NULL,
320 opts.stdin_commits ? &commits : NULL,
321 flags,
322 &write_opts))
323 result = 1;
325 cleanup:
326 FREE_AND_NULL(options);
327 string_list_clear(&pack_indexes, 0);
328 strbuf_release(&buf);
329 oidset_clear(&commits);
330 return result;
333 int cmd_commit_graph(int argc,
334 const char **argv,
335 const char *prefix,
336 struct repository *repo UNUSED)
338 parse_opt_subcommand_fn *fn = NULL;
339 struct option builtin_commit_graph_options[] = {
340 OPT_SUBCOMMAND("verify", &fn, graph_verify),
341 OPT_SUBCOMMAND("write", &fn, graph_write),
342 OPT_END(),
344 struct option *options = parse_options_concat(builtin_commit_graph_options, common_opts);
346 git_config(git_default_config, NULL);
348 disable_replace_refs();
349 save_commit_buffer = 0;
351 argc = parse_options(argc, argv, prefix, options,
352 builtin_commit_graph_usage, 0);
353 FREE_AND_NULL(options);
355 return fn(argc, argv, prefix);