]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/commit-graph.c
commit-graph: tighten chain size check
[thirdparty/git.git] / builtin / commit-graph.c
CommitLineData
4ce58ee3 1#include "builtin.h"
88e4e183 2#include "commit.h"
4ce58ee3 3#include "config.h"
f237c8b6 4#include "dir.h"
32a8f510 5#include "environment.h"
f394e093 6#include "gettext.h"
41771fa4 7#include "hex.h"
f237c8b6 8#include "lockfile.h"
4ce58ee3 9#include "parse-options.h"
283e68c7 10#include "repository.h"
f237c8b6 11#include "commit-graph.h"
a034e910 12#include "object-store-ll.h"
5b6653e5 13#include "progress.h"
cbeab747 14#include "replace-object.h"
2f00c355 15#include "tag.h"
74ea5c95 16#include "trace2.h"
4ce58ee3 17
8757b35d 18#define BUILTIN_COMMIT_GRAPH_VERIFY_USAGE \
f6a8ef07 19 N_("git commit-graph verify [--object-dir <dir>] [--shallow] [--[no-]progress]")
8757b35d
ÆAB
20
21#define BUILTIN_COMMIT_GRAPH_WRITE_USAGE \
f6a8ef07 22 N_("git commit-graph write [--object-dir <dir>] [--append]\n" \
e2f4e7e8 23 " [--split[=<strategy>]] [--reachable | --stdin-packs | --stdin-commits]\n" \
5af8b61c
ÆAB
24 " [--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress]\n" \
25 " <split options>")
8757b35d
ÆAB
26
27static const char * builtin_commit_graph_verify_usage[] = {
28 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE,
f237c8b6
DS
29 NULL
30};
31
8757b35d
ÆAB
32static const char * builtin_commit_graph_write_usage[] = {
33 BUILTIN_COMMIT_GRAPH_WRITE_USAGE,
283e68c7
DS
34 NULL
35};
36
8757b35d
ÆAB
37static char const * const builtin_commit_graph_usage[] = {
38 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE,
39 BUILTIN_COMMIT_GRAPH_WRITE_USAGE,
40 NULL,
4ce58ee3
DS
41};
42
43static struct opts_commit_graph {
44 const char *obj_dir;
59fb8770 45 int reachable;
049d51a2 46 int stdin_packs;
3d5df01b 47 int stdin_commits;
7547b95b 48 int append;
135a7123 49 int split;
3da4b609 50 int shallow;
73716122 51 int progress;
d38e07b8 52 int enable_changed_paths;
4ce58ee3
DS
53} opts;
54
84e4484f
ÆAB
55static struct option common_opts[] = {
56 OPT_STRING(0, "object-dir", &opts.obj_dir,
57 N_("dir"),
58 N_("the object directory to store the graph")),
84e4484f
ÆAB
59 OPT_END()
60};
61
62static struct option *add_common_options(struct option *to)
63{
64 return parse_options_concat(common_opts, to);
65}
66
1c3b0517 67static int graph_verify(int argc, const char **argv, const char *prefix)
283e68c7
DS
68{
69 struct commit_graph *graph = NULL;
0bd52e27 70 struct object_directory *odb = NULL;
283e68c7 71 char *graph_name;
47d06bb0
JK
72 char *chain_name;
73 enum { OPENED_NONE, OPENED_GRAPH, OPENED_CHAIN } opened = OPENED_NONE;
61df89c8
ÆAB
74 int fd;
75 struct stat st;
3da4b609 76 int flags = 0;
e8ed0a8a 77 int ret;
283e68c7
DS
78
79 static struct option builtin_commit_graph_verify_options[] = {
3da4b609
DS
80 OPT_BOOL(0, "shallow", &opts.shallow,
81 N_("if the commit-graph is split, only verify the tip file")),
d5fdf307
TB
82 OPT_BOOL(0, "progress", &opts.progress,
83 N_("force progress reporting")),
283e68c7
DS
84 OPT_END(),
85 };
84e4484f 86 struct option *options = add_common_options(builtin_commit_graph_verify_options);
283e68c7 87
0bd7f578
GS
88 trace2_cmd_mode("verify");
89
73716122 90 opts.progress = isatty(2);
ecd2d3ef 91 argc = parse_options(argc, argv, prefix,
84e4484f 92 options,
283e68c7 93 builtin_commit_graph_verify_usage, 0);
6d209a01
ÆAB
94 if (argc)
95 usage_with_options(builtin_commit_graph_verify_usage, options);
283e68c7
DS
96
97 if (!opts.obj_dir)
98 opts.obj_dir = get_object_directory();
3da4b609
DS
99 if (opts.shallow)
100 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
73716122
GS
101 if (opts.progress)
102 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
283e68c7 103
0bd52e27 104 odb = find_odb(the_repository, opts.obj_dir);
ad2dd5bb 105 graph_name = get_commit_graph_filename(odb);
47d06bb0
JK
106 chain_name = get_commit_graph_chain_filename(odb);
107 if (open_commit_graph(graph_name, &fd, &st))
108 opened = OPENED_GRAPH;
109 else if (errno != ENOENT)
7b8ce9c6 110 die_errno(_("Could not open commit-graph '%s'"), graph_name);
47d06bb0
JK
111 else if (open_commit_graph_chain(chain_name, &fd, &st))
112 opened = OPENED_CHAIN;
113 else if (errno != ENOENT)
114 die_errno(_("could not open commit-graph chain '%s'"), chain_name);
3da4b609 115
283e68c7 116 FREE_AND_NULL(graph_name);
47d06bb0 117 FREE_AND_NULL(chain_name);
84e4484f 118 FREE_AND_NULL(options);
283e68c7 119
47d06bb0
JK
120 if (opened == OPENED_NONE)
121 return 0;
122 else if (opened == OPENED_GRAPH)
ab14d067 123 graph = load_commit_graph_one_fd_st(the_repository, fd, &st, odb);
0bd52e27 124 else
47d06bb0 125 graph = load_commit_graph_chain_fd_st(the_repository, fd, &st);
3da4b609 126
283e68c7 127 if (!graph)
47d06bb0 128 return 1;
283e68c7 129
e8ed0a8a
ÆAB
130 ret = verify_commit_graph(the_repository, graph, flags);
131 free_commit_graph(graph);
132 return ret;
283e68c7
DS
133}
134
d6538246 135extern int read_replace_refs;
98bb7961 136static struct commit_graph_opts write_opts;
d6538246 137
4f027355
TB
138static int write_option_parse_split(const struct option *opt, const char *arg,
139 int unset)
140{
fdbde82f
TB
141 enum commit_graph_split_flags *flags = opt->value;
142
8d2aa8df
JK
143 BUG_ON_OPT_NEG(unset);
144
4f027355
TB
145 opts.split = 1;
146 if (!arg)
147 return 0;
148
fdbde82f
TB
149 if (!strcmp(arg, "no-merge"))
150 *flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED;
8a6ac287
TB
151 else if (!strcmp(arg, "replace"))
152 *flags = COMMIT_GRAPH_SPLIT_REPLACE;
fdbde82f
TB
153 else
154 die(_("unrecognized --split argument, %s"), arg);
4f027355
TB
155
156 return 0;
157}
158
5b6653e5
TB
159static int read_one_commit(struct oidset *commits, struct progress *progress,
160 const char *hash)
fa8953cb 161{
2f00c355 162 struct object *result;
fa8953cb
TB
163 struct object_id oid;
164 const char *end;
165
166 if (parse_oid_hex(hash, &oid, &end))
167 return error(_("unexpected non-hex object ID: %s"), hash);
168
2f00c355
TB
169 result = deref_tag(the_repository, parse_object(the_repository, &oid),
170 NULL, 0);
171 if (!result)
172 return error(_("invalid object: %s"), hash);
6da43d93 173 else if (object_as_type(result, OBJ_COMMIT, 1))
2f00c355 174 oidset_insert(commits, &result->oid);
5b6653e5
TB
175
176 display_progress(progress, oidset_size(commits));
177
fa8953cb
TB
178 return 0;
179}
180
809e0327
TB
181static int write_option_max_new_filters(const struct option *opt,
182 const char *arg,
183 int unset)
184{
185 int *to = opt->value;
186 if (unset)
187 *to = -1;
188 else {
189 const char *s;
190 *to = strtol(arg, (char **)&s, 10);
191 if (*s)
13d9fcec
ÆAB
192 return error(_("option `%s' expects a numerical value"),
193 "max-new-filters");
809e0327
TB
194 }
195 return 0;
196}
197
d356d5de 198static int git_commit_graph_write_config(const char *var, const char *value,
8868b1eb 199 const struct config_context *ctx,
5cf88fd8 200 void *cb UNUSED)
d356d5de
TB
201{
202 if (!strcmp(var, "commitgraph.maxnewfilters"))
8868b1eb 203 write_opts.max_new_filters = git_config_int(var, value, ctx->kvi);
d356d5de
TB
204 /*
205 * No need to fall-back to 'git_default_config', since this was already
206 * called in 'cmd_commit_graph()'.
207 */
208 return 0;
209}
210
1c3b0517 211static int graph_write(int argc, const char **argv, const char *prefix)
f237c8b6 212{
4a047908 213 struct string_list pack_indexes = STRING_LIST_INIT_DUP;
fa8953cb 214 struct strbuf buf = STRBUF_INIT;
6830c360 215 struct oidset commits = OIDSET_INIT;
0bd52e27 216 struct object_directory *odb = NULL;
e103f727 217 int result = 0;
73716122 218 enum commit_graph_write_flags flags = 0;
5b6653e5 219 struct progress *progress = NULL;
049d51a2 220
f237c8b6 221 static struct option builtin_commit_graph_write_options[] = {
59fb8770
DS
222 OPT_BOOL(0, "reachable", &opts.reachable,
223 N_("start walk at all refs")),
049d51a2
DS
224 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
225 N_("scan pack-indexes listed by stdin for commits")),
3d5df01b
DS
226 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
227 N_("start walk at commits listed by stdin")),
7547b95b
DS
228 OPT_BOOL(0, "append", &opts.append,
229 N_("include all commits already in the commit-graph file")),
d38e07b8
GS
230 OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
231 N_("enable computation for changed paths")),
98bb7961 232 OPT_CALLBACK_F(0, "split", &write_opts.split_flags, NULL,
4f027355
TB
233 N_("allow writing an incremental commit-graph file"),
234 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
235 write_option_parse_split),
98bb7961 236 OPT_INTEGER(0, "max-commits", &write_opts.max_commits,
c2bc6e6a 237 N_("maximum number of commits in a non-base split commit-graph")),
98bb7961 238 OPT_INTEGER(0, "size-multiple", &write_opts.size_multiple,
c2bc6e6a 239 N_("maximum ratio between two levels of a split commit-graph")),
98bb7961 240 OPT_EXPIRY_DATE(0, "expire-time", &write_opts.expire_time,
b09b785c 241 N_("only expire files older than a given date-time")),
809e0327
TB
242 OPT_CALLBACK_F(0, "max-new-filters", &write_opts.max_new_filters,
243 NULL, N_("maximum number of changed-path Bloom filters to compute"),
244 0, write_option_max_new_filters),
d5fdf307
TB
245 OPT_BOOL(0, "progress", &opts.progress,
246 N_("force progress reporting")),
f237c8b6
DS
247 OPT_END(),
248 };
84e4484f 249 struct option *options = add_common_options(builtin_commit_graph_write_options);
f237c8b6 250
73716122 251 opts.progress = isatty(2);
0087a87b 252 opts.enable_changed_paths = -1;
98bb7961
TB
253 write_opts.size_multiple = 2;
254 write_opts.max_commits = 0;
255 write_opts.expire_time = 0;
809e0327 256 write_opts.max_new_filters = -1;
c2bc6e6a 257
0bd7f578
GS
258 trace2_cmd_mode("write");
259
d356d5de
TB
260 git_config(git_commit_graph_write_config, &opts);
261
ecd2d3ef 262 argc = parse_options(argc, argv, prefix,
84e4484f 263 options,
f237c8b6 264 builtin_commit_graph_write_usage, 0);
6d209a01
ÆAB
265 if (argc)
266 usage_with_options(builtin_commit_graph_write_usage, options);
f237c8b6 267
59fb8770
DS
268 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
269 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
f237c8b6
DS
270 if (!opts.obj_dir)
271 opts.obj_dir = get_object_directory();
5af80394 272 if (opts.append)
39d88318 273 flags |= COMMIT_GRAPH_WRITE_APPEND;
135a7123 274 if (opts.split)
39d88318 275 flags |= COMMIT_GRAPH_WRITE_SPLIT;
73716122
GS
276 if (opts.progress)
277 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
0087a87b
DS
278 if (!opts.enable_changed_paths)
279 flags |= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS;
280 if (opts.enable_changed_paths == 1 ||
d5b873c8 281 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
d38e07b8 282 flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
f237c8b6 283
0bd52e27 284 odb = find_odb(the_repository, opts.obj_dir);
d6538246 285
c2bc6e6a 286 if (opts.reachable) {
98bb7961 287 if (write_commit_graph_reachable(odb, flags, &write_opts))
9d01cfed
ÆAB
288 result = 1;
289 goto cleanup;
c2bc6e6a 290 }
59fb8770 291
fa8953cb 292 if (opts.stdin_packs) {
d88b14b3 293 while (strbuf_getline(&buf, stdin) != EOF)
4a047908
ÆAB
294 string_list_append_nodup(&pack_indexes,
295 strbuf_detach(&buf, NULL));
fa8953cb
TB
296 } else if (opts.stdin_commits) {
297 oidset_init(&commits, 0);
5b6653e5
TB
298 if (opts.progress)
299 progress = start_delayed_progress(
300 _("Collecting commits from input"), 0);
fa8953cb
TB
301
302 while (strbuf_getline(&buf, stdin) != EOF) {
5b6653e5 303 if (read_one_commit(&commits, progress, buf.buf)) {
fa8953cb
TB
304 result = 1;
305 goto cleanup;
6830c360 306 }
7c5c9b9c 307 }
5b6653e5 308
862aead2 309 stop_progress(&progress);
049d51a2
DS
310 }
311
0bd52e27 312 if (write_commit_graph(odb,
fa8953cb 313 opts.stdin_packs ? &pack_indexes : NULL,
6830c360 314 opts.stdin_commits ? &commits : NULL,
c2bc6e6a 315 flags,
98bb7961 316 &write_opts))
e103f727 317 result = 1;
049d51a2 318
fa8953cb 319cleanup:
84e4484f 320 FREE_AND_NULL(options);
fa8953cb
TB
321 string_list_clear(&pack_indexes, 0);
322 strbuf_release(&buf);
e103f727 323 return result;
f237c8b6 324}
4ce58ee3
DS
325
326int cmd_commit_graph(int argc, const char **argv, const char *prefix)
327{
1c3b0517
SG
328 parse_opt_subcommand_fn *fn = NULL;
329 struct option builtin_commit_graph_options[] = {
330 OPT_SUBCOMMAND("verify", &fn, graph_verify),
331 OPT_SUBCOMMAND("write", &fn, graph_write),
332 OPT_END(),
333 };
334 struct option *options = parse_options_concat(builtin_commit_graph_options, common_opts);
4ce58ee3 335
4ce58ee3 336 git_config(git_default_config, NULL);
4ce58ee3 337
d24eda4e 338 disable_replace_refs();
dd2e50a8
JK
339 save_commit_buffer = 0;
340
1c3b0517
SG
341 argc = parse_options(argc, argv, prefix, options,
342 builtin_commit_graph_usage, 0);
343 FREE_AND_NULL(options);
f237c8b6 344
1c3b0517 345 return fn(argc, argv, prefix);
4ce58ee3 346}