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]"),
14 N_("git commit-graph write [--object-dir <objdir>] [--append|--split] [--reachable|--stdin-packs|--stdin-commits] <split options>"),
18 static const char * const builtin_commit_graph_verify_usage[] = {
19 N_("git commit-graph verify [--object-dir <objdir>] [--shallow]"),
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] <split options>"),
33 static struct opts_commit_graph {
43 static int graph_verify(int argc, const char **argv)
45 struct commit_graph *graph = NULL;
52 static struct option builtin_commit_graph_verify_options[] = {
53 OPT_STRING(0, "object-dir", &opts.obj_dir,
55 N_("The object directory to store the graph")),
56 OPT_BOOL(0, "shallow", &opts.shallow,
57 N_("if the commit-graph is split, only verify the tip file")),
61 argc = parse_options(argc, argv, NULL,
62 builtin_commit_graph_verify_options,
63 builtin_commit_graph_verify_usage, 0);
66 opts.obj_dir = get_object_directory();
68 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
70 graph_name = get_commit_graph_filename(opts.obj_dir);
71 open_ok = open_commit_graph(graph_name, &fd, &st);
72 if (!open_ok && errno != ENOENT)
73 die_errno(_("Could not open commit-graph '%s'"), graph_name);
75 FREE_AND_NULL(graph_name);
78 graph = load_commit_graph_one_fd_st(fd, &st);
80 graph = read_commit_graph_one(the_repository, opts.obj_dir);
82 /* Return failure if open_ok predicted success */
87 return verify_commit_graph(the_repository, graph, flags);
90 static int graph_read(int argc, const char **argv)
92 struct commit_graph *graph = NULL;
98 static struct option builtin_commit_graph_read_options[] = {
99 OPT_STRING(0, "object-dir", &opts.obj_dir,
101 N_("The object directory to store the graph")),
105 argc = parse_options(argc, argv, NULL,
106 builtin_commit_graph_read_options,
107 builtin_commit_graph_read_usage, 0);
110 opts.obj_dir = get_object_directory();
112 graph_name = get_commit_graph_filename(opts.obj_dir);
114 open_ok = open_commit_graph(graph_name, &fd, &st);
116 die_errno(_("Could not open commit-graph '%s'"), graph_name);
118 graph = load_commit_graph_one_fd_st(fd, &st);
122 FREE_AND_NULL(graph_name);
124 printf("header: %08x %d %d %d %d\n",
125 ntohl(*(uint32_t*)graph->data),
126 *(unsigned char*)(graph->data + 4),
127 *(unsigned char*)(graph->data + 5),
128 *(unsigned char*)(graph->data + 6),
129 *(unsigned char*)(graph->data + 7));
130 printf("num_commits: %u\n", graph->num_commits);
133 if (graph->chunk_oid_fanout)
134 printf(" oid_fanout");
135 if (graph->chunk_oid_lookup)
136 printf(" oid_lookup");
137 if (graph->chunk_commit_data)
138 printf(" commit_metadata");
139 if (graph->chunk_extra_edges)
140 printf(" extra_edges");
148 extern int read_replace_refs;
149 static struct split_commit_graph_opts split_opts;
151 static int graph_write(int argc, const char **argv)
153 struct string_list *pack_indexes = NULL;
154 struct string_list *commit_hex = NULL;
155 struct string_list lines;
157 unsigned int flags = COMMIT_GRAPH_PROGRESS;
159 static struct option builtin_commit_graph_write_options[] = {
160 OPT_STRING(0, "object-dir", &opts.obj_dir,
162 N_("The object directory to store the graph")),
163 OPT_BOOL(0, "reachable", &opts.reachable,
164 N_("start walk at all refs")),
165 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
166 N_("scan pack-indexes listed by stdin for commits")),
167 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
168 N_("start walk at commits listed by stdin")),
169 OPT_BOOL(0, "append", &opts.append,
170 N_("include all commits already in the commit-graph file")),
171 OPT_BOOL(0, "split", &opts.split,
172 N_("allow writing an incremental commit-graph file")),
173 OPT_INTEGER(0, "max-commits", &split_opts.max_commits,
174 N_("maximum number of commits in a non-base split commit-graph")),
175 OPT_INTEGER(0, "size-multiple", &split_opts.size_multiple,
176 N_("maximum ratio between two levels of a split commit-graph")),
177 OPT_EXPIRY_DATE(0, "expire-time", &split_opts.expire_time,
178 N_("maximum number of commits in a non-base split commit-graph")),
182 split_opts.size_multiple = 2;
183 split_opts.max_commits = 0;
184 split_opts.expire_time = 0;
186 argc = parse_options(argc, argv, NULL,
187 builtin_commit_graph_write_options,
188 builtin_commit_graph_write_usage, 0);
190 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
191 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
193 opts.obj_dir = get_object_directory();
195 flags |= COMMIT_GRAPH_APPEND;
197 flags |= COMMIT_GRAPH_SPLIT;
199 read_replace_refs = 0;
201 if (opts.reachable) {
202 if (write_commit_graph_reachable(opts.obj_dir, flags, &split_opts))
207 string_list_init(&lines, 0);
208 if (opts.stdin_packs || opts.stdin_commits) {
209 struct strbuf buf = STRBUF_INIT;
211 while (strbuf_getline(&buf, stdin) != EOF)
212 string_list_append(&lines, strbuf_detach(&buf, NULL));
214 if (opts.stdin_packs)
215 pack_indexes = &lines;
216 if (opts.stdin_commits)
222 if (write_commit_graph(opts.obj_dir,
233 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
235 static struct option builtin_commit_graph_options[] = {
236 OPT_STRING(0, "object-dir", &opts.obj_dir,
238 N_("The object directory to store the graph")),
242 if (argc == 2 && !strcmp(argv[1], "-h"))
243 usage_with_options(builtin_commit_graph_usage,
244 builtin_commit_graph_options);
246 git_config(git_default_config, NULL);
247 argc = parse_options(argc, argv, prefix,
248 builtin_commit_graph_options,
249 builtin_commit_graph_usage,
250 PARSE_OPT_STOP_AT_NON_OPTION);
253 if (!strcmp(argv[0], "read"))
254 return graph_read(argc, argv);
255 if (!strcmp(argv[0], "verify"))
256 return graph_verify(argc, argv);
257 if (!strcmp(argv[0], "write"))
258 return graph_write(argc, argv);
261 usage_with_options(builtin_commit_graph_usage,
262 builtin_commit_graph_options);