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