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