5 #include "parse-options.h"
6 #include "repository.h"
7 #include "commit-graph.h"
8 #include "object-store.h"
12 static char const * const builtin_commit_graph_usage[] = {
13 N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
14 N_("git commit-graph write [--object-dir <objdir>] [--append] "
15 "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
16 "[--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress] "
21 static const char * const builtin_commit_graph_verify_usage[] = {
22 N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
26 static const char * const builtin_commit_graph_write_usage[] = {
27 N_("git commit-graph write [--object-dir <objdir>] [--append] "
28 "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
29 "[--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress] "
34 static struct opts_commit_graph {
43 int enable_changed_paths;
46 static struct object_directory *find_odb(struct repository *r,
49 struct object_directory *odb;
50 char *obj_dir_real = real_pathdup(obj_dir, 1);
51 struct strbuf odb_path_real = STRBUF_INIT;
54 for (odb = r->objects->odb; odb; odb = odb->next) {
55 strbuf_realpath(&odb_path_real, odb->path, 1);
56 if (!strcmp(obj_dir_real, odb_path_real.buf))
61 strbuf_release(&odb_path_real);
64 die(_("could not find object directory matching %s"), obj_dir);
68 static int graph_verify(int argc, const char **argv)
70 struct commit_graph *graph = NULL;
71 struct object_directory *odb = NULL;
78 static struct option builtin_commit_graph_verify_options[] = {
79 OPT_STRING(0, "object-dir", &opts.obj_dir,
81 N_("The object directory to store the graph")),
82 OPT_BOOL(0, "shallow", &opts.shallow,
83 N_("if the commit-graph is split, only verify the tip file")),
84 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
88 trace2_cmd_mode("verify");
90 opts.progress = isatty(2);
91 argc = parse_options(argc, argv, NULL,
92 builtin_commit_graph_verify_options,
93 builtin_commit_graph_verify_usage, 0);
96 opts.obj_dir = get_object_directory();
98 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
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);
111 graph = load_commit_graph_one_fd_st(the_repository, fd, &st, odb);
113 graph = read_commit_graph_one(the_repository, odb);
115 /* Return failure if open_ok predicted success */
120 return verify_commit_graph(the_repository, graph, flags);
123 extern int read_replace_refs;
124 static struct commit_graph_opts write_opts;
126 static int write_option_parse_split(const struct option *opt, const char *arg,
129 enum commit_graph_split_flags *flags = opt->value;
135 if (!strcmp(arg, "no-merge"))
136 *flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED;
137 else if (!strcmp(arg, "replace"))
138 *flags = COMMIT_GRAPH_SPLIT_REPLACE;
140 die(_("unrecognized --split argument, %s"), arg);
145 static int read_one_commit(struct oidset *commits, struct progress *progress,
148 struct object *result;
149 struct object_id oid;
152 if (parse_oid_hex(hash, &oid, &end))
153 return error(_("unexpected non-hex object ID: %s"), hash);
155 result = deref_tag(the_repository, parse_object(the_repository, &oid),
158 return error(_("invalid object: %s"), hash);
159 else if (object_as_type(result, OBJ_COMMIT, 1))
160 oidset_insert(commits, &result->oid);
162 display_progress(progress, oidset_size(commits));
167 static int write_option_max_new_filters(const struct option *opt,
171 int *to = opt->value;
176 *to = strtol(arg, (char **)&s, 10);
178 return error(_("%s expects a numerical value"),
179 optname(opt, opt->flags));
184 static int graph_write(int argc, const char **argv)
186 struct string_list pack_indexes = STRING_LIST_INIT_NODUP;
187 struct strbuf buf = STRBUF_INIT;
188 struct oidset commits = OIDSET_INIT;
189 struct object_directory *odb = NULL;
191 enum commit_graph_write_flags flags = 0;
192 struct progress *progress = NULL;
194 static struct option builtin_commit_graph_write_options[] = {
195 OPT_STRING(0, "object-dir", &opts.obj_dir,
197 N_("The object directory to store the graph")),
198 OPT_BOOL(0, "reachable", &opts.reachable,
199 N_("start walk at all refs")),
200 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
201 N_("scan pack-indexes listed by stdin for commits")),
202 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
203 N_("start walk at commits listed by stdin")),
204 OPT_BOOL(0, "append", &opts.append,
205 N_("include all commits already in the commit-graph file")),
206 OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
207 N_("enable computation for changed paths")),
208 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
209 OPT_CALLBACK_F(0, "split", &write_opts.split_flags, NULL,
210 N_("allow writing an incremental commit-graph file"),
211 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
212 write_option_parse_split),
213 OPT_INTEGER(0, "max-commits", &write_opts.max_commits,
214 N_("maximum number of commits in a non-base split commit-graph")),
215 OPT_INTEGER(0, "size-multiple", &write_opts.size_multiple,
216 N_("maximum ratio between two levels of a split commit-graph")),
217 OPT_EXPIRY_DATE(0, "expire-time", &write_opts.expire_time,
218 N_("only expire files older than a given date-time")),
219 OPT_CALLBACK_F(0, "max-new-filters", &write_opts.max_new_filters,
220 NULL, N_("maximum number of changed-path Bloom filters to compute"),
221 0, write_option_max_new_filters),
225 opts.progress = isatty(2);
226 opts.enable_changed_paths = -1;
227 write_opts.size_multiple = 2;
228 write_opts.max_commits = 0;
229 write_opts.expire_time = 0;
230 write_opts.max_new_filters = -1;
232 trace2_cmd_mode("write");
234 argc = parse_options(argc, argv, NULL,
235 builtin_commit_graph_write_options,
236 builtin_commit_graph_write_usage, 0);
238 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
239 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
241 opts.obj_dir = get_object_directory();
243 flags |= COMMIT_GRAPH_WRITE_APPEND;
245 flags |= COMMIT_GRAPH_WRITE_SPLIT;
247 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
248 if (!opts.enable_changed_paths)
249 flags |= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS;
250 if (opts.enable_changed_paths == 1 ||
251 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
252 flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
254 read_replace_refs = 0;
255 odb = find_odb(the_repository, opts.obj_dir);
257 if (opts.reachable) {
258 if (write_commit_graph_reachable(odb, flags, &write_opts))
263 if (opts.stdin_packs) {
264 while (strbuf_getline(&buf, stdin) != EOF)
265 string_list_append(&pack_indexes,
266 strbuf_detach(&buf, NULL));
267 } else if (opts.stdin_commits) {
268 oidset_init(&commits, 0);
270 progress = start_delayed_progress(
271 _("Collecting commits from input"), 0);
273 while (strbuf_getline(&buf, stdin) != EOF) {
274 if (read_one_commit(&commits, progress, buf.buf)) {
280 stop_progress(&progress);
283 if (write_commit_graph(odb,
284 opts.stdin_packs ? &pack_indexes : NULL,
285 opts.stdin_commits ? &commits : NULL,
291 string_list_clear(&pack_indexes, 0);
292 strbuf_release(&buf);
296 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
298 static struct option builtin_commit_graph_options[] = {
299 OPT_STRING(0, "object-dir", &opts.obj_dir,
301 N_("The object directory to store the graph")),
305 if (argc == 2 && !strcmp(argv[1], "-h"))
306 usage_with_options(builtin_commit_graph_usage,
307 builtin_commit_graph_options);
309 git_config(git_default_config, NULL);
310 argc = parse_options(argc, argv, prefix,
311 builtin_commit_graph_options,
312 builtin_commit_graph_usage,
313 PARSE_OPT_STOP_AT_NON_OPTION);
315 save_commit_buffer = 0;
318 if (!strcmp(argv[0], "verify"))
319 return graph_verify(argc, argv);
320 if (!strcmp(argv[0], "write"))
321 return graph_write(argc, argv);
324 usage_with_options(builtin_commit_graph_usage,
325 builtin_commit_graph_options);