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