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 opts.progress = isatty(2);
64 argc = parse_options(argc, argv, NULL,
65 builtin_commit_graph_verify_options,
66 builtin_commit_graph_verify_usage, 0);
69 opts.obj_dir = get_object_directory();
71 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
73 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
75 graph_name = get_commit_graph_filename(opts.obj_dir);
76 open_ok = open_commit_graph(graph_name, &fd, &st);
77 if (!open_ok && errno != ENOENT)
78 die_errno(_("Could not open commit-graph '%s'"), graph_name);
80 FREE_AND_NULL(graph_name);
83 graph = load_commit_graph_one_fd_st(fd, &st);
85 graph = read_commit_graph_one(the_repository, opts.obj_dir);
87 /* Return failure if open_ok predicted success */
92 return verify_commit_graph(the_repository, graph, flags);
95 static int graph_read(int argc, const char **argv)
97 struct commit_graph *graph = NULL;
103 static struct option builtin_commit_graph_read_options[] = {
104 OPT_STRING(0, "object-dir", &opts.obj_dir,
106 N_("The object directory to store the graph")),
110 argc = parse_options(argc, argv, NULL,
111 builtin_commit_graph_read_options,
112 builtin_commit_graph_read_usage, 0);
115 opts.obj_dir = get_object_directory();
117 graph_name = get_commit_graph_filename(opts.obj_dir);
119 open_ok = open_commit_graph(graph_name, &fd, &st);
121 die_errno(_("Could not open commit-graph '%s'"), graph_name);
123 graph = load_commit_graph_one_fd_st(fd, &st);
127 FREE_AND_NULL(graph_name);
129 printf("header: %08x %d %d %d %d\n",
130 ntohl(*(uint32_t*)graph->data),
131 *(unsigned char*)(graph->data + 4),
132 *(unsigned char*)(graph->data + 5),
133 *(unsigned char*)(graph->data + 6),
134 *(unsigned char*)(graph->data + 7));
135 printf("num_commits: %u\n", graph->num_commits);
138 if (graph->chunk_oid_fanout)
139 printf(" oid_fanout");
140 if (graph->chunk_oid_lookup)
141 printf(" oid_lookup");
142 if (graph->chunk_commit_data)
143 printf(" commit_metadata");
144 if (graph->chunk_extra_edges)
145 printf(" extra_edges");
153 extern int read_replace_refs;
154 static struct split_commit_graph_opts split_opts;
156 static int graph_write(int argc, const char **argv)
158 struct string_list *pack_indexes = NULL;
159 struct string_list *commit_hex = NULL;
160 struct string_list lines;
162 enum commit_graph_write_flags flags = 0;
164 static struct option builtin_commit_graph_write_options[] = {
165 OPT_STRING(0, "object-dir", &opts.obj_dir,
167 N_("The object directory to store the graph")),
168 OPT_BOOL(0, "reachable", &opts.reachable,
169 N_("start walk at all refs")),
170 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
171 N_("scan pack-indexes listed by stdin for commits")),
172 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
173 N_("start walk at commits listed by stdin")),
174 OPT_BOOL(0, "append", &opts.append,
175 N_("include all commits already in the commit-graph file")),
176 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
177 OPT_BOOL(0, "split", &opts.split,
178 N_("allow writing an incremental commit-graph file")),
179 OPT_INTEGER(0, "max-commits", &split_opts.max_commits,
180 N_("maximum number of commits in a non-base split commit-graph")),
181 OPT_INTEGER(0, "size-multiple", &split_opts.size_multiple,
182 N_("maximum ratio between two levels of a split commit-graph")),
183 OPT_EXPIRY_DATE(0, "expire-time", &split_opts.expire_time,
184 N_("maximum number of commits in a non-base split commit-graph")),
188 opts.progress = isatty(2);
189 split_opts.size_multiple = 2;
190 split_opts.max_commits = 0;
191 split_opts.expire_time = 0;
193 argc = parse_options(argc, argv, NULL,
194 builtin_commit_graph_write_options,
195 builtin_commit_graph_write_usage, 0);
197 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
198 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
200 opts.obj_dir = get_object_directory();
202 flags |= COMMIT_GRAPH_WRITE_APPEND;
204 flags |= COMMIT_GRAPH_WRITE_SPLIT;
206 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
208 read_replace_refs = 0;
210 if (opts.reachable) {
211 if (write_commit_graph_reachable(opts.obj_dir, flags, &split_opts))
216 string_list_init(&lines, 0);
217 if (opts.stdin_packs || opts.stdin_commits) {
218 struct strbuf buf = STRBUF_INIT;
220 while (strbuf_getline(&buf, stdin) != EOF)
221 string_list_append(&lines, strbuf_detach(&buf, NULL));
223 if (opts.stdin_packs)
224 pack_indexes = &lines;
225 if (opts.stdin_commits) {
227 flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS;
233 if (write_commit_graph(opts.obj_dir,
244 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
246 static struct option builtin_commit_graph_options[] = {
247 OPT_STRING(0, "object-dir", &opts.obj_dir,
249 N_("The object directory to store the graph")),
253 if (argc == 2 && !strcmp(argv[1], "-h"))
254 usage_with_options(builtin_commit_graph_usage,
255 builtin_commit_graph_options);
257 git_config(git_default_config, NULL);
258 argc = parse_options(argc, argv, prefix,
259 builtin_commit_graph_options,
260 builtin_commit_graph_usage,
261 PARSE_OPT_STOP_AT_NON_OPTION);
263 save_commit_buffer = 0;
266 if (!strcmp(argv[0], "read"))
267 return graph_read(argc, argv);
268 if (!strcmp(argv[0], "verify"))
269 return graph_verify(argc, argv);
270 if (!strcmp(argv[0], "write"))
271 return graph_write(argc, argv);
274 usage_with_options(builtin_commit_graph_usage,
275 builtin_commit_graph_options);