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