builtin/multi-pack-index.c: disable top-level --[no-]progress
[git/debian.git] / builtin / commit-graph.c
blob067587a0fd841121e653dfbb2bbfabab0bd9875f
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_END()
56 static struct option *add_common_options(struct option *to)
58 return parse_options_concat(common_opts, to);
61 static struct object_directory *find_odb(struct repository *r,
62 const char *obj_dir)
64 struct object_directory *odb;
65 char *obj_dir_real = real_pathdup(obj_dir, 1);
66 struct strbuf odb_path_real = STRBUF_INIT;
68 prepare_alt_odb(r);
69 for (odb = r->objects->odb; odb; odb = odb->next) {
70 strbuf_realpath(&odb_path_real, odb->path, 1);
71 if (!strcmp(obj_dir_real, odb_path_real.buf))
72 break;
75 free(obj_dir_real);
76 strbuf_release(&odb_path_real);
78 if (!odb)
79 die(_("could not find object directory matching %s"), obj_dir);
80 return odb;
83 static int graph_verify(int argc, const char **argv)
85 struct commit_graph *graph = NULL;
86 struct object_directory *odb = NULL;
87 char *graph_name;
88 int open_ok;
89 int fd;
90 struct stat st;
91 int flags = 0;
93 static struct option builtin_commit_graph_verify_options[] = {
94 OPT_BOOL(0, "shallow", &opts.shallow,
95 N_("if the commit-graph is split, only verify the tip file")),
96 OPT_BOOL(0, "progress", &opts.progress,
97 N_("force progress reporting")),
98 OPT_END(),
100 struct option *options = add_common_options(builtin_commit_graph_verify_options);
102 trace2_cmd_mode("verify");
104 opts.progress = isatty(2);
105 argc = parse_options(argc, argv, NULL,
106 options,
107 builtin_commit_graph_verify_usage, 0);
108 if (argc)
109 usage_with_options(builtin_commit_graph_verify_usage, options);
111 if (!opts.obj_dir)
112 opts.obj_dir = get_object_directory();
113 if (opts.shallow)
114 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
115 if (opts.progress)
116 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
118 odb = find_odb(the_repository, opts.obj_dir);
119 graph_name = get_commit_graph_filename(odb);
120 open_ok = open_commit_graph(graph_name, &fd, &st);
121 if (!open_ok && errno != ENOENT)
122 die_errno(_("Could not open commit-graph '%s'"), graph_name);
124 FREE_AND_NULL(graph_name);
125 FREE_AND_NULL(options);
127 if (open_ok)
128 graph = load_commit_graph_one_fd_st(the_repository, fd, &st, odb);
129 else
130 graph = read_commit_graph_one(the_repository, odb);
132 /* Return failure if open_ok predicted success */
133 if (!graph)
134 return !!open_ok;
136 UNLEAK(graph);
137 return verify_commit_graph(the_repository, graph, flags);
140 extern int read_replace_refs;
141 static struct commit_graph_opts write_opts;
143 static int write_option_parse_split(const struct option *opt, const char *arg,
144 int unset)
146 enum commit_graph_split_flags *flags = opt->value;
148 BUG_ON_OPT_NEG(unset);
150 opts.split = 1;
151 if (!arg)
152 return 0;
154 if (!strcmp(arg, "no-merge"))
155 *flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED;
156 else if (!strcmp(arg, "replace"))
157 *flags = COMMIT_GRAPH_SPLIT_REPLACE;
158 else
159 die(_("unrecognized --split argument, %s"), arg);
161 return 0;
164 static int read_one_commit(struct oidset *commits, struct progress *progress,
165 const char *hash)
167 struct object *result;
168 struct object_id oid;
169 const char *end;
171 if (parse_oid_hex(hash, &oid, &end))
172 return error(_("unexpected non-hex object ID: %s"), hash);
174 result = deref_tag(the_repository, parse_object(the_repository, &oid),
175 NULL, 0);
176 if (!result)
177 return error(_("invalid object: %s"), hash);
178 else if (object_as_type(result, OBJ_COMMIT, 1))
179 oidset_insert(commits, &result->oid);
181 display_progress(progress, oidset_size(commits));
183 return 0;
186 static int write_option_max_new_filters(const struct option *opt,
187 const char *arg,
188 int unset)
190 int *to = opt->value;
191 if (unset)
192 *to = -1;
193 else {
194 const char *s;
195 *to = strtol(arg, (char **)&s, 10);
196 if (*s)
197 return error(_("%s expects a numerical value"),
198 optname(opt, opt->flags));
200 return 0;
203 static int git_commit_graph_write_config(const char *var, const char *value,
204 void *cb)
206 if (!strcmp(var, "commitgraph.maxnewfilters"))
207 write_opts.max_new_filters = git_config_int(var, value);
209 * No need to fall-back to 'git_default_config', since this was already
210 * called in 'cmd_commit_graph()'.
212 return 0;
215 static int graph_write(int argc, const char **argv)
217 struct string_list pack_indexes = STRING_LIST_INIT_NODUP;
218 struct strbuf buf = STRBUF_INIT;
219 struct oidset commits = OIDSET_INIT;
220 struct object_directory *odb = NULL;
221 int result = 0;
222 enum commit_graph_write_flags flags = 0;
223 struct progress *progress = NULL;
225 static struct option builtin_commit_graph_write_options[] = {
226 OPT_BOOL(0, "reachable", &opts.reachable,
227 N_("start walk at all refs")),
228 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
229 N_("scan pack-indexes listed by stdin for commits")),
230 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
231 N_("start walk at commits listed by stdin")),
232 OPT_BOOL(0, "append", &opts.append,
233 N_("include all commits already in the commit-graph file")),
234 OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
235 N_("enable computation for changed paths")),
236 OPT_CALLBACK_F(0, "split", &write_opts.split_flags, NULL,
237 N_("allow writing an incremental commit-graph file"),
238 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
239 write_option_parse_split),
240 OPT_INTEGER(0, "max-commits", &write_opts.max_commits,
241 N_("maximum number of commits in a non-base split commit-graph")),
242 OPT_INTEGER(0, "size-multiple", &write_opts.size_multiple,
243 N_("maximum ratio between two levels of a split commit-graph")),
244 OPT_EXPIRY_DATE(0, "expire-time", &write_opts.expire_time,
245 N_("only expire files older than a given date-time")),
246 OPT_CALLBACK_F(0, "max-new-filters", &write_opts.max_new_filters,
247 NULL, N_("maximum number of changed-path Bloom filters to compute"),
248 0, write_option_max_new_filters),
249 OPT_BOOL(0, "progress", &opts.progress,
250 N_("force progress reporting")),
251 OPT_END(),
253 struct option *options = add_common_options(builtin_commit_graph_write_options);
255 opts.progress = isatty(2);
256 opts.enable_changed_paths = -1;
257 write_opts.size_multiple = 2;
258 write_opts.max_commits = 0;
259 write_opts.expire_time = 0;
260 write_opts.max_new_filters = -1;
262 trace2_cmd_mode("write");
264 git_config(git_commit_graph_write_config, &opts);
266 argc = parse_options(argc, argv, NULL,
267 options,
268 builtin_commit_graph_write_usage, 0);
269 if (argc)
270 usage_with_options(builtin_commit_graph_write_usage, options);
272 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
273 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
274 if (!opts.obj_dir)
275 opts.obj_dir = get_object_directory();
276 if (opts.append)
277 flags |= COMMIT_GRAPH_WRITE_APPEND;
278 if (opts.split)
279 flags |= COMMIT_GRAPH_WRITE_SPLIT;
280 if (opts.progress)
281 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
282 if (!opts.enable_changed_paths)
283 flags |= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS;
284 if (opts.enable_changed_paths == 1 ||
285 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
286 flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
288 read_replace_refs = 0;
289 odb = find_odb(the_repository, opts.obj_dir);
291 if (opts.reachable) {
292 if (write_commit_graph_reachable(odb, flags, &write_opts))
293 return 1;
294 return 0;
297 if (opts.stdin_packs) {
298 while (strbuf_getline(&buf, stdin) != EOF)
299 string_list_append(&pack_indexes,
300 strbuf_detach(&buf, NULL));
301 } else if (opts.stdin_commits) {
302 oidset_init(&commits, 0);
303 if (opts.progress)
304 progress = start_delayed_progress(
305 _("Collecting commits from input"), 0);
307 while (strbuf_getline(&buf, stdin) != EOF) {
308 if (read_one_commit(&commits, progress, buf.buf)) {
309 result = 1;
310 goto cleanup;
314 stop_progress(&progress);
317 if (write_commit_graph(odb,
318 opts.stdin_packs ? &pack_indexes : NULL,
319 opts.stdin_commits ? &commits : NULL,
320 flags,
321 &write_opts))
322 result = 1;
324 cleanup:
325 FREE_AND_NULL(options);
326 string_list_clear(&pack_indexes, 0);
327 strbuf_release(&buf);
328 return result;
331 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
333 struct option *builtin_commit_graph_options = common_opts;
335 git_config(git_default_config, NULL);
336 argc = parse_options(argc, argv, prefix,
337 builtin_commit_graph_options,
338 builtin_commit_graph_usage,
339 PARSE_OPT_STOP_AT_NON_OPTION);
340 if (!argc)
341 goto usage;
343 save_commit_buffer = 0;
345 if (!strcmp(argv[0], "verify"))
346 return graph_verify(argc, argv);
347 else if (argc && !strcmp(argv[0], "write"))
348 return graph_write(argc, argv);
350 error(_("unrecognized subcommand: %s"), argv[0]);
351 usage:
352 usage_with_options(builtin_commit_graph_usage,
353 builtin_commit_graph_options);