Merge branch 'ab/make-tags-cleanup'
[alt-git.git] / builtin / commit-graph.c
blob0386f5c7755901aeb33ca41ade143a98d8a78ec7
1 #include "builtin.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "lockfile.h"
5 #include "parse-options.h"
6 #include "repository.h"
7 #include "commit-graph.h"
8 #include "object-store.h"
9 #include "progress.h"
10 #include "tag.h"
12 #define BUILTIN_COMMIT_GRAPH_VERIFY_USAGE \
13 N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]")
15 #define BUILTIN_COMMIT_GRAPH_WRITE_USAGE \
16 N_("git commit-graph write [--object-dir <objdir>] [--append] " \
17 "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] " \
18 "[--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress] " \
19 "<split options>")
21 static const char * builtin_commit_graph_verify_usage[] = {
22 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE,
23 NULL
26 static const char * builtin_commit_graph_write_usage[] = {
27 BUILTIN_COMMIT_GRAPH_WRITE_USAGE,
28 NULL
31 static char const * const builtin_commit_graph_usage[] = {
32 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE,
33 BUILTIN_COMMIT_GRAPH_WRITE_USAGE,
34 NULL,
37 static struct opts_commit_graph {
38 const char *obj_dir;
39 int reachable;
40 int stdin_packs;
41 int stdin_commits;
42 int append;
43 int split;
44 int shallow;
45 int progress;
46 int enable_changed_paths;
47 } opts;
49 static struct option common_opts[] = {
50 OPT_STRING(0, "object-dir", &opts.obj_dir,
51 N_("dir"),
52 N_("the object directory to store the graph")),
53 OPT_BOOL(0, "progress", &opts.progress,
54 N_("force progress reporting")),
55 OPT_END()
58 static struct option *add_common_options(struct option *to)
60 return parse_options_concat(common_opts, to);
63 static int graph_verify(int argc, const char **argv)
65 struct commit_graph *graph = NULL;
66 struct object_directory *odb = NULL;
67 char *graph_name;
68 int open_ok;
69 int fd;
70 struct stat st;
71 int flags = 0;
73 static struct option builtin_commit_graph_verify_options[] = {
74 OPT_BOOL(0, "shallow", &opts.shallow,
75 N_("if the commit-graph is split, only verify the tip file")),
76 OPT_END(),
78 struct option *options = add_common_options(builtin_commit_graph_verify_options);
80 trace2_cmd_mode("verify");
82 opts.progress = isatty(2);
83 argc = parse_options(argc, argv, NULL,
84 options,
85 builtin_commit_graph_verify_usage, 0);
86 if (argc)
87 usage_with_options(builtin_commit_graph_verify_usage, options);
89 if (!opts.obj_dir)
90 opts.obj_dir = get_object_directory();
91 if (opts.shallow)
92 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
93 if (opts.progress)
94 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
96 odb = find_odb(the_repository, opts.obj_dir);
97 graph_name = get_commit_graph_filename(odb);
98 open_ok = open_commit_graph(graph_name, &fd, &st);
99 if (!open_ok && errno != ENOENT)
100 die_errno(_("Could not open commit-graph '%s'"), graph_name);
102 FREE_AND_NULL(graph_name);
103 FREE_AND_NULL(options);
105 if (open_ok)
106 graph = load_commit_graph_one_fd_st(the_repository, fd, &st, odb);
107 else
108 graph = read_commit_graph_one(the_repository, odb);
110 /* Return failure if open_ok predicted success */
111 if (!graph)
112 return !!open_ok;
114 UNLEAK(graph);
115 return verify_commit_graph(the_repository, graph, flags);
118 extern int read_replace_refs;
119 static struct commit_graph_opts write_opts;
121 static int write_option_parse_split(const struct option *opt, const char *arg,
122 int unset)
124 enum commit_graph_split_flags *flags = opt->value;
126 BUG_ON_OPT_NEG(unset);
128 opts.split = 1;
129 if (!arg)
130 return 0;
132 if (!strcmp(arg, "no-merge"))
133 *flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED;
134 else if (!strcmp(arg, "replace"))
135 *flags = COMMIT_GRAPH_SPLIT_REPLACE;
136 else
137 die(_("unrecognized --split argument, %s"), arg);
139 return 0;
142 static int read_one_commit(struct oidset *commits, struct progress *progress,
143 const char *hash)
145 struct object *result;
146 struct object_id oid;
147 const char *end;
149 if (parse_oid_hex(hash, &oid, &end))
150 return error(_("unexpected non-hex object ID: %s"), hash);
152 result = deref_tag(the_repository, parse_object(the_repository, &oid),
153 NULL, 0);
154 if (!result)
155 return error(_("invalid object: %s"), hash);
156 else if (object_as_type(result, OBJ_COMMIT, 1))
157 oidset_insert(commits, &result->oid);
159 display_progress(progress, oidset_size(commits));
161 return 0;
164 static int write_option_max_new_filters(const struct option *opt,
165 const char *arg,
166 int unset)
168 int *to = opt->value;
169 if (unset)
170 *to = -1;
171 else {
172 const char *s;
173 *to = strtol(arg, (char **)&s, 10);
174 if (*s)
175 return error(_("%s expects a numerical value"),
176 optname(opt, opt->flags));
178 return 0;
181 static int git_commit_graph_write_config(const char *var, const char *value,
182 void *cb)
184 if (!strcmp(var, "commitgraph.maxnewfilters"))
185 write_opts.max_new_filters = git_config_int(var, value);
187 * No need to fall-back to 'git_default_config', since this was already
188 * called in 'cmd_commit_graph()'.
190 return 0;
193 static int graph_write(int argc, const char **argv)
195 struct string_list pack_indexes = STRING_LIST_INIT_NODUP;
196 struct strbuf buf = STRBUF_INIT;
197 struct oidset commits = OIDSET_INIT;
198 struct object_directory *odb = NULL;
199 int result = 0;
200 enum commit_graph_write_flags flags = 0;
201 struct progress *progress = NULL;
203 static struct option builtin_commit_graph_write_options[] = {
204 OPT_BOOL(0, "reachable", &opts.reachable,
205 N_("start walk at all refs")),
206 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
207 N_("scan pack-indexes listed by stdin for commits")),
208 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
209 N_("start walk at commits listed by stdin")),
210 OPT_BOOL(0, "append", &opts.append,
211 N_("include all commits already in the commit-graph file")),
212 OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
213 N_("enable computation for changed paths")),
214 OPT_CALLBACK_F(0, "split", &write_opts.split_flags, NULL,
215 N_("allow writing an incremental commit-graph file"),
216 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
217 write_option_parse_split),
218 OPT_INTEGER(0, "max-commits", &write_opts.max_commits,
219 N_("maximum number of commits in a non-base split commit-graph")),
220 OPT_INTEGER(0, "size-multiple", &write_opts.size_multiple,
221 N_("maximum ratio between two levels of a split commit-graph")),
222 OPT_EXPIRY_DATE(0, "expire-time", &write_opts.expire_time,
223 N_("only expire files older than a given date-time")),
224 OPT_CALLBACK_F(0, "max-new-filters", &write_opts.max_new_filters,
225 NULL, N_("maximum number of changed-path Bloom filters to compute"),
226 0, write_option_max_new_filters),
227 OPT_END(),
229 struct option *options = add_common_options(builtin_commit_graph_write_options);
231 opts.progress = isatty(2);
232 opts.enable_changed_paths = -1;
233 write_opts.size_multiple = 2;
234 write_opts.max_commits = 0;
235 write_opts.expire_time = 0;
236 write_opts.max_new_filters = -1;
238 trace2_cmd_mode("write");
240 git_config(git_commit_graph_write_config, &opts);
242 argc = parse_options(argc, argv, NULL,
243 options,
244 builtin_commit_graph_write_usage, 0);
245 if (argc)
246 usage_with_options(builtin_commit_graph_write_usage, options);
248 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
249 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
250 if (!opts.obj_dir)
251 opts.obj_dir = get_object_directory();
252 if (opts.append)
253 flags |= COMMIT_GRAPH_WRITE_APPEND;
254 if (opts.split)
255 flags |= COMMIT_GRAPH_WRITE_SPLIT;
256 if (opts.progress)
257 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
258 if (!opts.enable_changed_paths)
259 flags |= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS;
260 if (opts.enable_changed_paths == 1 ||
261 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
262 flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
264 read_replace_refs = 0;
265 odb = find_odb(the_repository, opts.obj_dir);
267 if (opts.reachable) {
268 if (write_commit_graph_reachable(odb, flags, &write_opts))
269 return 1;
270 return 0;
273 if (opts.stdin_packs) {
274 while (strbuf_getline(&buf, stdin) != EOF)
275 string_list_append(&pack_indexes,
276 strbuf_detach(&buf, NULL));
277 } else if (opts.stdin_commits) {
278 oidset_init(&commits, 0);
279 if (opts.progress)
280 progress = start_delayed_progress(
281 _("Collecting commits from input"), 0);
283 while (strbuf_getline(&buf, stdin) != EOF) {
284 if (read_one_commit(&commits, progress, buf.buf)) {
285 result = 1;
286 goto cleanup;
290 stop_progress(&progress);
293 if (write_commit_graph(odb,
294 opts.stdin_packs ? &pack_indexes : NULL,
295 opts.stdin_commits ? &commits : NULL,
296 flags,
297 &write_opts))
298 result = 1;
300 cleanup:
301 FREE_AND_NULL(options);
302 string_list_clear(&pack_indexes, 0);
303 strbuf_release(&buf);
304 return result;
307 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
309 struct option *builtin_commit_graph_options = common_opts;
311 git_config(git_default_config, NULL);
312 argc = parse_options(argc, argv, prefix,
313 builtin_commit_graph_options,
314 builtin_commit_graph_usage,
315 PARSE_OPT_STOP_AT_NON_OPTION);
316 if (!argc)
317 goto usage;
319 save_commit_buffer = 0;
321 if (!strcmp(argv[0], "verify"))
322 return graph_verify(argc, argv);
323 else if (argc && !strcmp(argv[0], "write"))
324 return graph_write(argc, argv);
326 error(_("unrecognized subcommand: %s"), argv[0]);
327 usage:
328 usage_with_options(builtin_commit_graph_usage,
329 builtin_commit_graph_options);