5 #include "parse-options.h"
6 #include "repository.h"
7 #include "commit-graph.h"
9 static char const * const builtin_commit_graph_usage[] = {
10 N_("git commit-graph [--object-dir <objdir>]"),
11 N_("git commit-graph read [--object-dir <objdir>]"),
12 N_("git commit-graph verify [--object-dir <objdir>]"),
13 N_("git commit-graph write [--object-dir <objdir>] [--append] [--stdin-packs|--stdin-commits]"),
17 static const char * const builtin_commit_graph_verify_usage[] = {
18 N_("git commit-graph verify [--object-dir <objdir>]"),
22 static const char * const builtin_commit_graph_read_usage[] = {
23 N_("git commit-graph read [--object-dir <objdir>]"),
27 static const char * const builtin_commit_graph_write_usage[] = {
28 N_("git commit-graph write [--object-dir <objdir>] [--append] [--stdin-packs|--stdin-commits]"),
32 static struct opts_commit_graph {
40 static int graph_verify(int argc, const char **argv)
42 struct commit_graph *graph = NULL;
45 static struct option builtin_commit_graph_verify_options[] = {
46 OPT_STRING(0, "object-dir", &opts.obj_dir,
48 N_("The object directory to store the graph")),
52 argc = parse_options(argc, argv, NULL,
53 builtin_commit_graph_verify_options,
54 builtin_commit_graph_verify_usage, 0);
57 opts.obj_dir = get_object_directory();
59 graph_name = get_commit_graph_filename(opts.obj_dir);
60 graph = load_commit_graph_one(graph_name);
61 FREE_AND_NULL(graph_name);
66 return verify_commit_graph(the_repository, graph);
69 static int graph_read(int argc, const char **argv)
71 struct commit_graph *graph = NULL;
74 static struct option builtin_commit_graph_read_options[] = {
75 OPT_STRING(0, "object-dir", &opts.obj_dir,
77 N_("The object directory to store the graph")),
81 argc = parse_options(argc, argv, NULL,
82 builtin_commit_graph_read_options,
83 builtin_commit_graph_read_usage, 0);
86 opts.obj_dir = get_object_directory();
88 graph_name = get_commit_graph_filename(opts.obj_dir);
89 graph = load_commit_graph_one(graph_name);
93 die("graph file %s does not exist", graph_name);
96 FREE_AND_NULL(graph_name);
98 printf("header: %08x %d %d %d %d\n",
99 ntohl(*(uint32_t*)graph->data),
100 *(unsigned char*)(graph->data + 4),
101 *(unsigned char*)(graph->data + 5),
102 *(unsigned char*)(graph->data + 6),
103 *(unsigned char*)(graph->data + 7));
104 printf("num_commits: %u\n", graph->num_commits);
107 if (graph->chunk_oid_fanout)
108 printf(" oid_fanout");
109 if (graph->chunk_oid_lookup)
110 printf(" oid_lookup");
111 if (graph->chunk_commit_data)
112 printf(" commit_metadata");
113 if (graph->chunk_large_edges)
114 printf(" large_edges");
120 static int graph_write(int argc, const char **argv)
122 struct string_list *pack_indexes = NULL;
123 struct string_list *commit_hex = NULL;
124 struct string_list lines;
126 static struct option builtin_commit_graph_write_options[] = {
127 OPT_STRING(0, "object-dir", &opts.obj_dir,
129 N_("The object directory to store the graph")),
130 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
131 N_("scan pack-indexes listed by stdin for commits")),
132 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
133 N_("start walk at commits listed by stdin")),
134 OPT_BOOL(0, "append", &opts.append,
135 N_("include all commits already in the commit-graph file")),
139 argc = parse_options(argc, argv, NULL,
140 builtin_commit_graph_write_options,
141 builtin_commit_graph_write_usage, 0);
143 if (opts.stdin_packs && opts.stdin_commits)
144 die(_("cannot use both --stdin-commits and --stdin-packs"));
146 opts.obj_dir = get_object_directory();
148 string_list_init(&lines, 0);
149 if (opts.stdin_packs || opts.stdin_commits) {
150 struct strbuf buf = STRBUF_INIT;
152 while (strbuf_getline(&buf, stdin) != EOF)
153 string_list_append(&lines, strbuf_detach(&buf, NULL));
155 if (opts.stdin_packs)
156 pack_indexes = &lines;
157 if (opts.stdin_commits)
161 write_commit_graph(opts.obj_dir,
166 string_list_clear(&lines, 0);
170 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
172 static struct option builtin_commit_graph_options[] = {
173 OPT_STRING(0, "object-dir", &opts.obj_dir,
175 N_("The object directory to store the graph")),
179 if (argc == 2 && !strcmp(argv[1], "-h"))
180 usage_with_options(builtin_commit_graph_usage,
181 builtin_commit_graph_options);
183 git_config(git_default_config, NULL);
184 argc = parse_options(argc, argv, prefix,
185 builtin_commit_graph_options,
186 builtin_commit_graph_usage,
187 PARSE_OPT_STOP_AT_NON_OPTION);
190 if (!strcmp(argv[0], "read"))
191 return graph_read(argc, argv);
192 if (!strcmp(argv[0], "verify"))
193 return graph_verify(argc, argv);
194 if (!strcmp(argv[0], "write"))
195 return graph_write(argc, argv);
198 usage_with_options(builtin_commit_graph_usage,
199 builtin_commit_graph_options);