5 #include "parse-options.h"
6 #include "repository.h"
7 #include "commit-graph.h"
8 #include "object-store.h"
10 static char const * const builtin_commit_graph_usage[] = {
11 N_("git commit-graph [--object-dir <objdir>]"),
12 N_("git commit-graph read [--object-dir <objdir>]"),
13 N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
14 N_("git commit-graph write [--object-dir <objdir>] [--append|--split] [--reachable|--stdin-packs|--stdin-commits] [--[no-]progress] <split options>"),
18 static const char * const builtin_commit_graph_verify_usage[] = {
19 N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
23 static const char * const builtin_commit_graph_read_usage[] = {
24 N_("git commit-graph read [--object-dir <objdir>]"),
28 static const char * const builtin_commit_graph_write_usage[] = {
29 N_("git commit-graph write [--object-dir <objdir>] [--append|--split] [--reachable|--stdin-packs|--stdin-commits] [--[no-]progress] <split options>"),
33 static struct opts_commit_graph {
44 static int graph_verify(int argc, const char **argv)
46 struct commit_graph *graph = NULL;
53 static struct option builtin_commit_graph_verify_options[] = {
54 OPT_STRING(0, "object-dir", &opts.obj_dir,
56 N_("The object directory to store the graph")),
57 OPT_BOOL(0, "shallow", &opts.shallow,
58 N_("if the commit-graph is split, only verify the tip file")),
59 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
63 trace2_cmd_mode("verify");
65 opts.progress = isatty(2);
66 argc = parse_options(argc, argv, NULL,
67 builtin_commit_graph_verify_options,
68 builtin_commit_graph_verify_usage, 0);
71 opts.obj_dir = get_object_directory();
73 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
75 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
77 graph_name = get_commit_graph_filename(opts.obj_dir);
78 open_ok = open_commit_graph(graph_name, &fd, &st);
79 if (!open_ok && errno != ENOENT)
80 die_errno(_("Could not open commit-graph '%s'"), graph_name);
82 FREE_AND_NULL(graph_name);
85 graph = load_commit_graph_one_fd_st(fd, &st);
87 graph = read_commit_graph_one(the_repository, opts.obj_dir);
89 /* Return failure if open_ok predicted success */
94 return verify_commit_graph(the_repository, graph, flags);
97 static int graph_read(int argc, const char **argv)
99 struct commit_graph *graph = NULL;
105 static struct option builtin_commit_graph_read_options[] = {
106 OPT_STRING(0, "object-dir", &opts.obj_dir,
108 N_("The object directory to store the graph")),
112 trace2_cmd_mode("read");
114 argc = parse_options(argc, argv, NULL,
115 builtin_commit_graph_read_options,
116 builtin_commit_graph_read_usage, 0);
119 opts.obj_dir = get_object_directory();
121 graph_name = get_commit_graph_filename(opts.obj_dir);
123 open_ok = open_commit_graph(graph_name, &fd, &st);
125 die_errno(_("Could not open commit-graph '%s'"), graph_name);
127 graph = load_commit_graph_one_fd_st(fd, &st);
131 FREE_AND_NULL(graph_name);
133 printf("header: %08x %d %d %d %d\n",
134 ntohl(*(uint32_t*)graph->data),
135 *(unsigned char*)(graph->data + 4),
136 *(unsigned char*)(graph->data + 5),
137 *(unsigned char*)(graph->data + 6),
138 *(unsigned char*)(graph->data + 7));
139 printf("num_commits: %u\n", graph->num_commits);
142 if (graph->chunk_oid_fanout)
143 printf(" oid_fanout");
144 if (graph->chunk_oid_lookup)
145 printf(" oid_lookup");
146 if (graph->chunk_commit_data)
147 printf(" commit_metadata");
148 if (graph->chunk_extra_edges)
149 printf(" extra_edges");
157 extern int read_replace_refs;
158 static struct split_commit_graph_opts split_opts;
160 static int graph_write(int argc, const char **argv)
162 struct string_list *pack_indexes = NULL;
163 struct string_list *commit_hex = NULL;
164 struct string_list lines;
166 enum commit_graph_write_flags flags = 0;
168 static struct option builtin_commit_graph_write_options[] = {
169 OPT_STRING(0, "object-dir", &opts.obj_dir,
171 N_("The object directory to store the graph")),
172 OPT_BOOL(0, "reachable", &opts.reachable,
173 N_("start walk at all refs")),
174 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
175 N_("scan pack-indexes listed by stdin for commits")),
176 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
177 N_("start walk at commits listed by stdin")),
178 OPT_BOOL(0, "append", &opts.append,
179 N_("include all commits already in the commit-graph file")),
180 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
181 OPT_BOOL(0, "split", &opts.split,
182 N_("allow writing an incremental commit-graph file")),
183 OPT_INTEGER(0, "max-commits", &split_opts.max_commits,
184 N_("maximum number of commits in a non-base split commit-graph")),
185 OPT_INTEGER(0, "size-multiple", &split_opts.size_multiple,
186 N_("maximum ratio between two levels of a split commit-graph")),
187 OPT_EXPIRY_DATE(0, "expire-time", &split_opts.expire_time,
188 N_("maximum number of commits in a non-base split commit-graph")),
192 opts.progress = isatty(2);
193 split_opts.size_multiple = 2;
194 split_opts.max_commits = 0;
195 split_opts.expire_time = 0;
197 trace2_cmd_mode("write");
199 argc = parse_options(argc, argv, NULL,
200 builtin_commit_graph_write_options,
201 builtin_commit_graph_write_usage, 0);
203 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
204 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
206 opts.obj_dir = get_object_directory();
208 flags |= COMMIT_GRAPH_WRITE_APPEND;
210 flags |= COMMIT_GRAPH_WRITE_SPLIT;
212 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
214 read_replace_refs = 0;
216 if (opts.reachable) {
217 if (write_commit_graph_reachable(opts.obj_dir, flags, &split_opts))
222 string_list_init(&lines, 0);
223 if (opts.stdin_packs || opts.stdin_commits) {
224 struct strbuf buf = STRBUF_INIT;
226 while (strbuf_getline(&buf, stdin) != EOF)
227 string_list_append(&lines, strbuf_detach(&buf, NULL));
229 if (opts.stdin_packs)
230 pack_indexes = &lines;
231 if (opts.stdin_commits) {
233 flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS;
239 if (write_commit_graph(opts.obj_dir,
250 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
252 static struct option builtin_commit_graph_options[] = {
253 OPT_STRING(0, "object-dir", &opts.obj_dir,
255 N_("The object directory to store the graph")),
259 if (argc == 2 && !strcmp(argv[1], "-h"))
260 usage_with_options(builtin_commit_graph_usage,
261 builtin_commit_graph_options);
263 git_config(git_default_config, NULL);
264 argc = parse_options(argc, argv, prefix,
265 builtin_commit_graph_options,
266 builtin_commit_graph_usage,
267 PARSE_OPT_STOP_AT_NON_OPTION);
269 save_commit_buffer = 0;
272 if (!strcmp(argv[0], "read"))
273 return graph_read(argc, argv);
274 if (!strcmp(argv[0], "verify"))
275 return graph_verify(argc, argv);
276 if (!strcmp(argv[0], "write"))
277 return graph_write(argc, argv);
280 usage_with_options(builtin_commit_graph_usage,
281 builtin_commit_graph_options);