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] [--reachable|--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] [--reachable|--stdin-packs|--stdin-commits]"),
32 static struct opts_commit_graph {
41 static int graph_verify(int argc, const char **argv)
43 struct commit_graph *graph = NULL;
49 static struct option builtin_commit_graph_verify_options[] = {
50 OPT_STRING(0, "object-dir", &opts.obj_dir,
52 N_("The object directory to store the graph")),
56 argc = parse_options(argc, argv, NULL,
57 builtin_commit_graph_verify_options,
58 builtin_commit_graph_verify_usage, 0);
61 opts.obj_dir = get_object_directory();
63 graph_name = get_commit_graph_filename(opts.obj_dir);
64 open_ok = open_commit_graph(graph_name, &fd, &st);
67 graph = load_commit_graph_one_fd_st(fd, &st);
68 FREE_AND_NULL(graph_name);
74 return verify_commit_graph(the_repository, graph);
77 static int graph_read(int argc, const char **argv)
79 struct commit_graph *graph = NULL;
85 static struct option builtin_commit_graph_read_options[] = {
86 OPT_STRING(0, "object-dir", &opts.obj_dir,
88 N_("The object directory to store the graph")),
92 argc = parse_options(argc, argv, NULL,
93 builtin_commit_graph_read_options,
94 builtin_commit_graph_read_usage, 0);
97 opts.obj_dir = get_object_directory();
99 graph_name = get_commit_graph_filename(opts.obj_dir);
101 open_ok = open_commit_graph(graph_name, &fd, &st);
103 die_errno(_("Could not open commit-graph '%s'"), graph_name);
105 graph = load_commit_graph_one_fd_st(fd, &st);
109 FREE_AND_NULL(graph_name);
111 printf("header: %08x %d %d %d %d\n",
112 ntohl(*(uint32_t*)graph->data),
113 *(unsigned char*)(graph->data + 4),
114 *(unsigned char*)(graph->data + 5),
115 *(unsigned char*)(graph->data + 6),
116 *(unsigned char*)(graph->data + 7));
117 printf("num_commits: %u\n", graph->num_commits);
120 if (graph->chunk_oid_fanout)
121 printf(" oid_fanout");
122 if (graph->chunk_oid_lookup)
123 printf(" oid_lookup");
124 if (graph->chunk_commit_data)
125 printf(" commit_metadata");
126 if (graph->chunk_extra_edges)
127 printf(" extra_edges");
135 extern int read_replace_refs;
137 static int graph_write(int argc, const char **argv)
139 struct string_list *pack_indexes = NULL;
140 struct string_list *commit_hex = NULL;
141 struct string_list lines;
143 static struct option builtin_commit_graph_write_options[] = {
144 OPT_STRING(0, "object-dir", &opts.obj_dir,
146 N_("The object directory to store the graph")),
147 OPT_BOOL(0, "reachable", &opts.reachable,
148 N_("start walk at all refs")),
149 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
150 N_("scan pack-indexes listed by stdin for commits")),
151 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
152 N_("start walk at commits listed by stdin")),
153 OPT_BOOL(0, "append", &opts.append,
154 N_("include all commits already in the commit-graph file")),
158 argc = parse_options(argc, argv, NULL,
159 builtin_commit_graph_write_options,
160 builtin_commit_graph_write_usage, 0);
162 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
163 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
165 opts.obj_dir = get_object_directory();
167 read_replace_refs = 0;
169 if (opts.reachable) {
170 write_commit_graph_reachable(opts.obj_dir, opts.append, 1);
174 string_list_init(&lines, 0);
175 if (opts.stdin_packs || opts.stdin_commits) {
176 struct strbuf buf = STRBUF_INIT;
178 while (strbuf_getline(&buf, stdin) != EOF)
179 string_list_append(&lines, strbuf_detach(&buf, NULL));
181 if (opts.stdin_packs)
182 pack_indexes = &lines;
183 if (opts.stdin_commits)
189 write_commit_graph(opts.obj_dir,
199 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
201 static struct option builtin_commit_graph_options[] = {
202 OPT_STRING(0, "object-dir", &opts.obj_dir,
204 N_("The object directory to store the graph")),
208 if (argc == 2 && !strcmp(argv[1], "-h"))
209 usage_with_options(builtin_commit_graph_usage,
210 builtin_commit_graph_options);
212 git_config(git_default_config, NULL);
213 argc = parse_options(argc, argv, prefix,
214 builtin_commit_graph_options,
215 builtin_commit_graph_usage,
216 PARSE_OPT_STOP_AT_NON_OPTION);
219 if (!strcmp(argv[0], "read"))
220 return graph_read(argc, argv);
221 if (!strcmp(argv[0], "verify"))
222 return graph_verify(argc, argv);
223 if (!strcmp(argv[0], "write"))
224 return graph_write(argc, argv);
227 usage_with_options(builtin_commit_graph_usage,
228 builtin_commit_graph_options);