Git fork
at reftables-rust 362 lines 10 kB view raw
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 26static const char * const builtin_commit_graph_verify_usage[] = { 27 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE, 28 NULL 29}; 30 31static const char * const builtin_commit_graph_write_usage[] = { 32 BUILTIN_COMMIT_GRAPH_WRITE_USAGE, 33 NULL 34}; 35 36static char const * const builtin_commit_graph_usage[] = { 37 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE, 38 BUILTIN_COMMIT_GRAPH_WRITE_USAGE, 39 NULL, 40}; 41 42static 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 54static 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 61static struct option *add_common_options(struct option *to) 62{ 63 return parse_options_concat(common_opts, to); 64} 65 66static 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 144extern int read_replace_refs; 145static struct commit_graph_opts write_opts; 146 147static 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 168static 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 190static 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 207static 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 /* 214 * No need to fall-back to 'git_default_config', since this was already 215 * called in 'cmd_commit_graph()'. 216 */ 217 return 0; 218} 219 220static int graph_write(int argc, const char **argv, const char *prefix, 221 struct repository *repo UNUSED) 222{ 223 struct string_list pack_indexes = STRING_LIST_INIT_DUP; 224 struct strbuf buf = STRBUF_INIT; 225 struct oidset commits = OIDSET_INIT; 226 struct odb_source *source = NULL; 227 int result = 0; 228 enum commit_graph_write_flags flags = 0; 229 struct progress *progress = NULL; 230 231 static struct option builtin_commit_graph_write_options[] = { 232 OPT_BOOL(0, "reachable", &opts.reachable, 233 N_("start walk at all refs")), 234 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs, 235 N_("scan pack-indexes listed by stdin for commits")), 236 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits, 237 N_("start walk at commits listed by stdin")), 238 OPT_BOOL(0, "append", &opts.append, 239 N_("include all commits already in the commit-graph file")), 240 OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths, 241 N_("enable computation for changed paths")), 242 OPT_CALLBACK_F(0, "split", &write_opts.split_flags, NULL, 243 N_("allow writing an incremental commit-graph file"), 244 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, 245 write_option_parse_split), 246 OPT_INTEGER(0, "max-commits", &write_opts.max_commits, 247 N_("maximum number of commits in a non-base split commit-graph")), 248 OPT_INTEGER(0, "size-multiple", &write_opts.size_multiple, 249 N_("maximum ratio between two levels of a split commit-graph")), 250 OPT_EXPIRY_DATE(0, "expire-time", &write_opts.expire_time, 251 N_("only expire files older than a given date-time")), 252 OPT_CALLBACK_F(0, "max-new-filters", &write_opts.max_new_filters, 253 NULL, N_("maximum number of changed-path Bloom filters to compute"), 254 0, write_option_max_new_filters), 255 OPT_BOOL(0, "progress", &opts.progress, 256 N_("force progress reporting")), 257 OPT_END(), 258 }; 259 struct option *options = add_common_options(builtin_commit_graph_write_options); 260 261 opts.progress = isatty(2); 262 opts.enable_changed_paths = -1; 263 write_opts.size_multiple = 2; 264 write_opts.max_commits = 0; 265 write_opts.expire_time = 0; 266 write_opts.max_new_filters = -1; 267 268 trace2_cmd_mode("write"); 269 270 repo_config(the_repository, git_commit_graph_write_config, &opts); 271 272 argc = parse_options(argc, argv, prefix, 273 options, 274 builtin_commit_graph_write_usage, 0); 275 if (argc) 276 usage_with_options(builtin_commit_graph_write_usage, options); 277 278 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1) 279 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs")); 280 if (!opts.obj_dir) 281 opts.obj_dir = repo_get_object_directory(the_repository); 282 if (opts.append) 283 flags |= COMMIT_GRAPH_WRITE_APPEND; 284 if (opts.split) 285 flags |= COMMIT_GRAPH_WRITE_SPLIT; 286 if (opts.progress) 287 flags |= COMMIT_GRAPH_WRITE_PROGRESS; 288 if (!opts.enable_changed_paths) 289 flags |= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS; 290 if (opts.enable_changed_paths == 1 || 291 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0)) 292 flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS; 293 294 source = odb_find_source_or_die(the_repository->objects, opts.obj_dir); 295 296 if (opts.reachable) { 297 if (write_commit_graph_reachable(source, flags, &write_opts)) 298 result = 1; 299 goto cleanup; 300 } 301 302 if (opts.stdin_packs) { 303 while (strbuf_getline(&buf, stdin) != EOF) 304 string_list_append_nodup(&pack_indexes, 305 strbuf_detach(&buf, NULL)); 306 } else if (opts.stdin_commits) { 307 oidset_init(&commits, 0); 308 if (opts.progress) 309 progress = start_delayed_progress( 310 the_repository, 311 _("Collecting commits from input"), 0); 312 313 while (strbuf_getline(&buf, stdin) != EOF) { 314 if (read_one_commit(&commits, progress, buf.buf)) { 315 result = 1; 316 stop_progress(&progress); 317 goto cleanup; 318 } 319 } 320 321 stop_progress(&progress); 322 } 323 324 if (write_commit_graph(source, 325 opts.stdin_packs ? &pack_indexes : NULL, 326 opts.stdin_commits ? &commits : NULL, 327 flags, 328 &write_opts)) 329 result = 1; 330 331cleanup: 332 FREE_AND_NULL(options); 333 string_list_clear(&pack_indexes, 0); 334 strbuf_release(&buf); 335 oidset_clear(&commits); 336 return result; 337} 338 339int cmd_commit_graph(int argc, 340 const char **argv, 341 const char *prefix, 342 struct repository *repo) 343{ 344 parse_opt_subcommand_fn *fn = NULL; 345 struct option builtin_commit_graph_options[] = { 346 OPT_SUBCOMMAND("verify", &fn, graph_verify), 347 OPT_SUBCOMMAND("write", &fn, graph_write), 348 OPT_END(), 349 }; 350 struct option *options = parse_options_concat(builtin_commit_graph_options, common_opts); 351 352 repo_config(the_repository, git_default_config, NULL); 353 354 disable_replace_refs(); 355 save_commit_buffer = 0; 356 357 argc = parse_options(argc, argv, prefix, options, 358 builtin_commit_graph_usage, 0); 359 FREE_AND_NULL(options); 360 361 return fn(argc, argv, prefix, repo); 362}