commit-graph.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #include "builtin.h"
  2. #include "config.h"
  3. #include "dir.h"
  4. #include "lockfile.h"
  5. #include "parse-options.h"
  6. #include "repository.h"
  7. #include "commit-graph.h"
  8. #include "object-store.h"
  9. #include "progress.h"
  10. #include "tag.h"
  11. static char const * const builtin_commit_graph_usage[] = {
  12. N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
  13. N_("git commit-graph write [--object-dir <objdir>] [--append] "
  14. "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
  15. "[--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress] "
  16. "<split options>"),
  17. NULL
  18. };
  19. static const char * const builtin_commit_graph_verify_usage[] = {
  20. N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
  21. NULL
  22. };
  23. static const char * const builtin_commit_graph_write_usage[] = {
  24. N_("git commit-graph write [--object-dir <objdir>] [--append] "
  25. "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
  26. "[--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress] "
  27. "<split options>"),
  28. NULL
  29. };
  30. static struct opts_commit_graph {
  31. const char *obj_dir;
  32. int reachable;
  33. int stdin_packs;
  34. int stdin_commits;
  35. int append;
  36. int split;
  37. int shallow;
  38. int progress;
  39. int enable_changed_paths;
  40. } opts;
  41. static struct object_directory *find_odb(struct repository *r,
  42. const char *obj_dir)
  43. {
  44. struct object_directory *odb;
  45. char *obj_dir_real = real_pathdup(obj_dir, 1);
  46. struct strbuf odb_path_real = STRBUF_INIT;
  47. prepare_alt_odb(r);
  48. for (odb = r->objects->odb; odb; odb = odb->next) {
  49. strbuf_realpath(&odb_path_real, odb->path, 1);
  50. if (!strcmp(obj_dir_real, odb_path_real.buf))
  51. break;
  52. }
  53. free(obj_dir_real);
  54. strbuf_release(&odb_path_real);
  55. if (!odb)
  56. die(_("could not find object directory matching %s"), obj_dir);
  57. return odb;
  58. }
  59. static int graph_verify(int argc, const char **argv)
  60. {
  61. struct commit_graph *graph = NULL;
  62. struct object_directory *odb = NULL;
  63. char *graph_name;
  64. int open_ok;
  65. int fd;
  66. struct stat st;
  67. int flags = 0;
  68. static struct option builtin_commit_graph_verify_options[] = {
  69. OPT_STRING(0, "object-dir", &opts.obj_dir,
  70. N_("dir"),
  71. N_("The object directory to store the graph")),
  72. OPT_BOOL(0, "shallow", &opts.shallow,
  73. N_("if the commit-graph is split, only verify the tip file")),
  74. OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
  75. OPT_END(),
  76. };
  77. trace2_cmd_mode("verify");
  78. opts.progress = isatty(2);
  79. argc = parse_options(argc, argv, NULL,
  80. builtin_commit_graph_verify_options,
  81. builtin_commit_graph_verify_usage, 0);
  82. if (!opts.obj_dir)
  83. opts.obj_dir = get_object_directory();
  84. if (opts.shallow)
  85. flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
  86. if (opts.progress)
  87. flags |= COMMIT_GRAPH_WRITE_PROGRESS;
  88. odb = find_odb(the_repository, opts.obj_dir);
  89. graph_name = get_commit_graph_filename(odb);
  90. open_ok = open_commit_graph(graph_name, &fd, &st);
  91. if (!open_ok && errno != ENOENT)
  92. die_errno(_("Could not open commit-graph '%s'"), graph_name);
  93. FREE_AND_NULL(graph_name);
  94. if (open_ok)
  95. graph = load_commit_graph_one_fd_st(the_repository, fd, &st, odb);
  96. else
  97. graph = read_commit_graph_one(the_repository, odb);
  98. /* Return failure if open_ok predicted success */
  99. if (!graph)
  100. return !!open_ok;
  101. UNLEAK(graph);
  102. return verify_commit_graph(the_repository, graph, flags);
  103. }
  104. extern int read_replace_refs;
  105. static struct commit_graph_opts write_opts;
  106. static int write_option_parse_split(const struct option *opt, const char *arg,
  107. int unset)
  108. {
  109. enum commit_graph_split_flags *flags = opt->value;
  110. BUG_ON_OPT_NEG(unset);
  111. opts.split = 1;
  112. if (!arg)
  113. return 0;
  114. if (!strcmp(arg, "no-merge"))
  115. *flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED;
  116. else if (!strcmp(arg, "replace"))
  117. *flags = COMMIT_GRAPH_SPLIT_REPLACE;
  118. else
  119. die(_("unrecognized --split argument, %s"), arg);
  120. return 0;
  121. }
  122. static int read_one_commit(struct oidset *commits, struct progress *progress,
  123. const char *hash)
  124. {
  125. struct object *result;
  126. struct object_id oid;
  127. const char *end;
  128. if (parse_oid_hex(hash, &oid, &end))
  129. return error(_("unexpected non-hex object ID: %s"), hash);
  130. result = deref_tag(the_repository, parse_object(the_repository, &oid),
  131. NULL, 0);
  132. if (!result)
  133. return error(_("invalid object: %s"), hash);
  134. else if (object_as_type(result, OBJ_COMMIT, 1))
  135. oidset_insert(commits, &result->oid);
  136. display_progress(progress, oidset_size(commits));
  137. return 0;
  138. }
  139. static int write_option_max_new_filters(const struct option *opt,
  140. const char *arg,
  141. int unset)
  142. {
  143. int *to = opt->value;
  144. if (unset)
  145. *to = -1;
  146. else {
  147. const char *s;
  148. *to = strtol(arg, (char **)&s, 10);
  149. if (*s)
  150. return error(_("%s expects a numerical value"),
  151. optname(opt, opt->flags));
  152. }
  153. return 0;
  154. }
  155. static int git_commit_graph_write_config(const char *var, const char *value,
  156. void *cb)
  157. {
  158. if (!strcmp(var, "commitgraph.maxnewfilters"))
  159. write_opts.max_new_filters = git_config_int(var, value);
  160. /*
  161. * No need to fall-back to 'git_default_config', since this was already
  162. * called in 'cmd_commit_graph()'.
  163. */
  164. return 0;
  165. }
  166. static int graph_write(int argc, const char **argv)
  167. {
  168. struct string_list pack_indexes = STRING_LIST_INIT_NODUP;
  169. struct strbuf buf = STRBUF_INIT;
  170. struct oidset commits = OIDSET_INIT;
  171. struct object_directory *odb = NULL;
  172. int result = 0;
  173. enum commit_graph_write_flags flags = 0;
  174. struct progress *progress = NULL;
  175. static struct option builtin_commit_graph_write_options[] = {
  176. OPT_STRING(0, "object-dir", &opts.obj_dir,
  177. N_("dir"),
  178. N_("The object directory to store the graph")),
  179. OPT_BOOL(0, "reachable", &opts.reachable,
  180. N_("start walk at all refs")),
  181. OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
  182. N_("scan pack-indexes listed by stdin for commits")),
  183. OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
  184. N_("start walk at commits listed by stdin")),
  185. OPT_BOOL(0, "append", &opts.append,
  186. N_("include all commits already in the commit-graph file")),
  187. OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
  188. N_("enable computation for changed paths")),
  189. OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
  190. OPT_CALLBACK_F(0, "split", &write_opts.split_flags, NULL,
  191. N_("allow writing an incremental commit-graph file"),
  192. PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
  193. write_option_parse_split),
  194. OPT_INTEGER(0, "max-commits", &write_opts.max_commits,
  195. N_("maximum number of commits in a non-base split commit-graph")),
  196. OPT_INTEGER(0, "size-multiple", &write_opts.size_multiple,
  197. N_("maximum ratio between two levels of a split commit-graph")),
  198. OPT_EXPIRY_DATE(0, "expire-time", &write_opts.expire_time,
  199. N_("only expire files older than a given date-time")),
  200. OPT_CALLBACK_F(0, "max-new-filters", &write_opts.max_new_filters,
  201. NULL, N_("maximum number of changed-path Bloom filters to compute"),
  202. 0, write_option_max_new_filters),
  203. OPT_END(),
  204. };
  205. opts.progress = isatty(2);
  206. opts.enable_changed_paths = -1;
  207. write_opts.size_multiple = 2;
  208. write_opts.max_commits = 0;
  209. write_opts.expire_time = 0;
  210. write_opts.max_new_filters = -1;
  211. trace2_cmd_mode("write");
  212. git_config(git_commit_graph_write_config, &opts);
  213. argc = parse_options(argc, argv, NULL,
  214. builtin_commit_graph_write_options,
  215. builtin_commit_graph_write_usage, 0);
  216. if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
  217. die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
  218. if (!opts.obj_dir)
  219. opts.obj_dir = get_object_directory();
  220. if (opts.append)
  221. flags |= COMMIT_GRAPH_WRITE_APPEND;
  222. if (opts.split)
  223. flags |= COMMIT_GRAPH_WRITE_SPLIT;
  224. if (opts.progress)
  225. flags |= COMMIT_GRAPH_WRITE_PROGRESS;
  226. if (!opts.enable_changed_paths)
  227. flags |= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS;
  228. if (opts.enable_changed_paths == 1 ||
  229. git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
  230. flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
  231. read_replace_refs = 0;
  232. odb = find_odb(the_repository, opts.obj_dir);
  233. if (opts.reachable) {
  234. if (write_commit_graph_reachable(odb, flags, &write_opts))
  235. return 1;
  236. return 0;
  237. }
  238. if (opts.stdin_packs) {
  239. while (strbuf_getline(&buf, stdin) != EOF)
  240. string_list_append(&pack_indexes,
  241. strbuf_detach(&buf, NULL));
  242. } else if (opts.stdin_commits) {
  243. oidset_init(&commits, 0);
  244. if (opts.progress)
  245. progress = start_delayed_progress(
  246. _("Collecting commits from input"), 0);
  247. while (strbuf_getline(&buf, stdin) != EOF) {
  248. if (read_one_commit(&commits, progress, buf.buf)) {
  249. result = 1;
  250. goto cleanup;
  251. }
  252. }
  253. stop_progress(&progress);
  254. }
  255. if (write_commit_graph(odb,
  256. opts.stdin_packs ? &pack_indexes : NULL,
  257. opts.stdin_commits ? &commits : NULL,
  258. flags,
  259. &write_opts))
  260. result = 1;
  261. cleanup:
  262. string_list_clear(&pack_indexes, 0);
  263. strbuf_release(&buf);
  264. return result;
  265. }
  266. int cmd_commit_graph(int argc, const char **argv, const char *prefix)
  267. {
  268. static struct option builtin_commit_graph_options[] = {
  269. OPT_STRING(0, "object-dir", &opts.obj_dir,
  270. N_("dir"),
  271. N_("The object directory to store the graph")),
  272. OPT_END(),
  273. };
  274. if (argc == 2 && !strcmp(argv[1], "-h"))
  275. usage_with_options(builtin_commit_graph_usage,
  276. builtin_commit_graph_options);
  277. git_config(git_default_config, NULL);
  278. argc = parse_options(argc, argv, prefix,
  279. builtin_commit_graph_options,
  280. builtin_commit_graph_usage,
  281. PARSE_OPT_STOP_AT_NON_OPTION);
  282. save_commit_buffer = 0;
  283. if (argc > 0) {
  284. if (!strcmp(argv[0], "verify"))
  285. return graph_verify(argc, argv);
  286. if (!strcmp(argv[0], "write"))
  287. return graph_write(argc, argv);
  288. }
  289. usage_with_options(builtin_commit_graph_usage,
  290. builtin_commit_graph_options);
  291. }