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;
46 static struct option builtin_commit_graph_verify_options[] = {
47 OPT_STRING(0, "object-dir", &opts.obj_dir,
49 N_("The object directory to store the graph")),
53 argc = parse_options(argc, argv, NULL,
54 builtin_commit_graph_verify_options,
55 builtin_commit_graph_verify_usage, 0);
58 opts.obj_dir = get_object_directory();
60 graph_name = get_commit_graph_filename(opts.obj_dir);
61 graph = load_commit_graph_one(graph_name);
62 FREE_AND_NULL(graph_name);
67 return verify_commit_graph(the_repository, graph);
70 static int graph_read(int argc, const char **argv)
72 struct commit_graph *graph = NULL;
75 static struct option builtin_commit_graph_read_options[] = {
76 OPT_STRING(0, "object-dir", &opts.obj_dir,
78 N_("The object directory to store the graph")),
82 argc = parse_options(argc, argv, NULL,
83 builtin_commit_graph_read_options,
84 builtin_commit_graph_read_usage, 0);
87 opts.obj_dir = get_object_directory();
89 graph_name = get_commit_graph_filename(opts.obj_dir);
90 graph = load_commit_graph_one(graph_name);
94 die("graph file %s does not exist", graph_name);
97 FREE_AND_NULL(graph_name);
99 printf("header: %08x %d %d %d %d\n",
100 ntohl(*(uint32_t*)graph->data),
101 *(unsigned char*)(graph->data + 4),
102 *(unsigned char*)(graph->data + 5),
103 *(unsigned char*)(graph->data + 6),
104 *(unsigned char*)(graph->data + 7));
105 printf("num_commits: %u\n", graph->num_commits);
108 if (graph->chunk_oid_fanout)
109 printf(" oid_fanout");
110 if (graph->chunk_oid_lookup)
111 printf(" oid_lookup");
112 if (graph->chunk_commit_data)
113 printf(" commit_metadata");
114 if (graph->chunk_large_edges)
115 printf(" large_edges");
121 static int graph_write(int argc, const char **argv)
123 struct string_list *pack_indexes = NULL;
124 struct string_list *commit_hex = NULL;
125 struct string_list lines;
127 static struct option builtin_commit_graph_write_options[] = {
128 OPT_STRING(0, "object-dir", &opts.obj_dir,
130 N_("The object directory to store the graph")),
131 OPT_BOOL(0, "reachable", &opts.reachable,
132 N_("start walk at all refs")),
133 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
134 N_("scan pack-indexes listed by stdin for commits")),
135 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
136 N_("start walk at commits listed by stdin")),
137 OPT_BOOL(0, "append", &opts.append,
138 N_("include all commits already in the commit-graph file")),
142 argc = parse_options(argc, argv, NULL,
143 builtin_commit_graph_write_options,
144 builtin_commit_graph_write_usage, 0);
146 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
147 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
149 opts.obj_dir = get_object_directory();
151 if (opts.reachable) {
152 write_commit_graph_reachable(opts.obj_dir, opts.append);
156 string_list_init(&lines, 0);
157 if (opts.stdin_packs || opts.stdin_commits) {
158 struct strbuf buf = STRBUF_INIT;
160 while (strbuf_getline(&buf, stdin) != EOF)
161 string_list_append(&lines, strbuf_detach(&buf, NULL));
163 if (opts.stdin_packs)
164 pack_indexes = &lines;
165 if (opts.stdin_commits)
169 write_commit_graph(opts.obj_dir,
174 string_list_clear(&lines, 0);
178 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
180 static struct option builtin_commit_graph_options[] = {
181 OPT_STRING(0, "object-dir", &opts.obj_dir,
183 N_("The object directory to store the graph")),
187 if (argc == 2 && !strcmp(argv[1], "-h"))
188 usage_with_options(builtin_commit_graph_usage,
189 builtin_commit_graph_options);
191 git_config(git_default_config, NULL);
192 argc = parse_options(argc, argv, prefix,
193 builtin_commit_graph_options,
194 builtin_commit_graph_usage,
195 PARSE_OPT_STOP_AT_NON_OPTION);
198 if (!strcmp(argv[0], "read"))
199 return graph_read(argc, argv);
200 if (!strcmp(argv[0], "verify"))
201 return graph_verify(argc, argv);
202 if (!strcmp(argv[0], "write"))
203 return graph_write(argc, argv);
206 usage_with_options(builtin_commit_graph_usage,
207 builtin_commit_graph_options);