THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
5.8KB

  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. static char const * const builtin_commit_graph_usage[] = {
  9. N_("git commit-graph [--object-dir <objdir>]"),
  10. N_("git commit-graph read [--object-dir <objdir>]"),
  11. N_("git commit-graph verify [--object-dir <objdir>]"),
  12. N_("git commit-graph write [--object-dir <objdir>] [--append] [--reachable|--stdin-packs|--stdin-commits]"),
  13. NULL
  14. };
  15. static const char * const builtin_commit_graph_verify_usage[] = {
  16. N_("git commit-graph verify [--object-dir <objdir>]"),
  17. NULL
  18. };
  19. static const char * const builtin_commit_graph_read_usage[] = {
  20. N_("git commit-graph read [--object-dir <objdir>]"),
  21. NULL
  22. };
  23. static const char * const builtin_commit_graph_write_usage[] = {
  24. N_("git commit-graph write [--object-dir <objdir>] [--append] [--reachable|--stdin-packs|--stdin-commits]"),
  25. NULL
  26. };
  27. static struct opts_commit_graph {
  28. const char *obj_dir;
  29. int reachable;
  30. int stdin_packs;
  31. int stdin_commits;
  32. int append;
  33. } opts;
  34. static int graph_verify(int argc, const char **argv)
  35. {
  36. struct commit_graph *graph = NULL;
  37. char *graph_name;
  38. int open_ok;
  39. int fd;
  40. struct stat st;
  41. static struct option builtin_commit_graph_verify_options[] = {
  42. OPT_STRING(0, "object-dir", &opts.obj_dir,
  43. N_("dir"),
  44. N_("The object directory to store the graph")),
  45. OPT_END(),
  46. };
  47. argc = parse_options(argc, argv, NULL,
  48. builtin_commit_graph_verify_options,
  49. builtin_commit_graph_verify_usage, 0);
  50. if (!opts.obj_dir)
  51. opts.obj_dir = get_object_directory();
  52. graph_name = get_commit_graph_filename(opts.obj_dir);
  53. open_ok = open_commit_graph(graph_name, &fd, &st);
  54. if (!open_ok)
  55. return 0;
  56. graph = load_commit_graph_one_fd_st(graph_name, fd, &st);
  57. FREE_AND_NULL(graph_name);
  58. if (!graph)
  59. return 1;
  60. UNLEAK(graph);
  61. return verify_commit_graph(the_repository, graph);
  62. }
  63. static int graph_read(int argc, const char **argv)
  64. {
  65. struct commit_graph *graph = NULL;
  66. char *graph_name;
  67. int open_ok;
  68. int fd;
  69. struct stat st;
  70. static struct option builtin_commit_graph_read_options[] = {
  71. OPT_STRING(0, "object-dir", &opts.obj_dir,
  72. N_("dir"),
  73. N_("The object directory to store the graph")),
  74. OPT_END(),
  75. };
  76. argc = parse_options(argc, argv, NULL,
  77. builtin_commit_graph_read_options,
  78. builtin_commit_graph_read_usage, 0);
  79. if (!opts.obj_dir)
  80. opts.obj_dir = get_object_directory();
  81. graph_name = get_commit_graph_filename(opts.obj_dir);
  82. open_ok = open_commit_graph(graph_name, &fd, &st);
  83. if (!open_ok)
  84. die_errno(_("Could not open commit-graph '%s'"), graph_name);
  85. graph = load_commit_graph_one_fd_st(graph_name, fd, &st);
  86. if (!graph)
  87. return 1;
  88. FREE_AND_NULL(graph_name);
  89. printf("header: %08x %d %d %d %d\n",
  90. ntohl(*(uint32_t*)graph->data),
  91. *(unsigned char*)(graph->data + 4),
  92. *(unsigned char*)(graph->data + 5),
  93. *(unsigned char*)(graph->data + 6),
  94. *(unsigned char*)(graph->data + 7));
  95. printf("num_commits: %u\n", graph->num_commits);
  96. printf("chunks:");
  97. if (graph->chunk_oid_fanout)
  98. printf(" oid_fanout");
  99. if (graph->chunk_oid_lookup)
  100. printf(" oid_lookup");
  101. if (graph->chunk_commit_data)
  102. printf(" commit_metadata");
  103. if (graph->chunk_extra_edges)
  104. printf(" extra_edges");
  105. printf("\n");
  106. UNLEAK(graph);
  107. return 0;
  108. }
  109. extern int read_replace_refs;
  110. static int graph_write(int argc, const char **argv)
  111. {
  112. struct string_list *pack_indexes = NULL;
  113. struct string_list *commit_hex = NULL;
  114. struct string_list lines;
  115. static struct option builtin_commit_graph_write_options[] = {
  116. OPT_STRING(0, "object-dir", &opts.obj_dir,
  117. N_("dir"),
  118. N_("The object directory to store the graph")),
  119. OPT_BOOL(0, "reachable", &opts.reachable,
  120. N_("start walk at all refs")),
  121. OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
  122. N_("scan pack-indexes listed by stdin for commits")),
  123. OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
  124. N_("start walk at commits listed by stdin")),
  125. OPT_BOOL(0, "append", &opts.append,
  126. N_("include all commits already in the commit-graph file")),
  127. OPT_END(),
  128. };
  129. argc = parse_options(argc, argv, NULL,
  130. builtin_commit_graph_write_options,
  131. builtin_commit_graph_write_usage, 0);
  132. if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
  133. die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
  134. if (!opts.obj_dir)
  135. opts.obj_dir = get_object_directory();
  136. read_replace_refs = 0;
  137. if (opts.reachable) {
  138. write_commit_graph_reachable(opts.obj_dir, opts.append, 1);
  139. return 0;
  140. }
  141. string_list_init(&lines, 0);
  142. if (opts.stdin_packs || opts.stdin_commits) {
  143. struct strbuf buf = STRBUF_INIT;
  144. while (strbuf_getline(&buf, stdin) != EOF)
  145. string_list_append(&lines, strbuf_detach(&buf, NULL));
  146. if (opts.stdin_packs)
  147. pack_indexes = &lines;
  148. if (opts.stdin_commits)
  149. commit_hex = &lines;
  150. UNLEAK(buf);
  151. }
  152. write_commit_graph(opts.obj_dir,
  153. pack_indexes,
  154. commit_hex,
  155. opts.append,
  156. 1);
  157. UNLEAK(lines);
  158. return 0;
  159. }
  160. int cmd_commit_graph(int argc, const char **argv, const char *prefix)
  161. {
  162. static struct option builtin_commit_graph_options[] = {
  163. OPT_STRING(0, "object-dir", &opts.obj_dir,
  164. N_("dir"),
  165. N_("The object directory to store the graph")),
  166. OPT_END(),
  167. };
  168. if (argc == 2 && !strcmp(argv[1], "-h"))
  169. usage_with_options(builtin_commit_graph_usage,
  170. builtin_commit_graph_options);
  171. git_config(git_default_config, NULL);
  172. argc = parse_options(argc, argv, prefix,
  173. builtin_commit_graph_options,
  174. builtin_commit_graph_usage,
  175. PARSE_OPT_STOP_AT_NON_OPTION);
  176. if (argc > 0) {
  177. if (!strcmp(argv[0], "read"))
  178. return graph_read(argc, argv);
  179. if (!strcmp(argv[0], "verify"))
  180. return graph_verify(argc, argv);
  181. if (!strcmp(argv[0], "write"))
  182. return graph_write(argc, argv);
  183. }
  184. usage_with_options(builtin_commit_graph_usage,
  185. builtin_commit_graph_options);
  186. }