5 #include "parse-options.h"
6 #include "commit-graph.h"
8 static char const * const builtin_commit_graph_usage[] = {
9 N_("git commit-graph [--object-dir <objdir>]"),
10 N_("git commit-graph read [--object-dir <objdir>]"),
11 N_("git commit-graph write [--object-dir <objdir>] [--stdin-packs]"),
15 static const char * const builtin_commit_graph_read_usage[] = {
16 N_("git commit-graph read [--object-dir <objdir>]"),
20 static const char * const builtin_commit_graph_write_usage[] = {
21 N_("git commit-graph write [--object-dir <objdir>] [--stdin-packs]"),
25 static struct opts_commit_graph {
30 static int graph_read(int argc, const char **argv)
32 struct commit_graph *graph = NULL;
35 static struct option builtin_commit_graph_read_options[] = {
36 OPT_STRING(0, "object-dir", &opts.obj_dir,
38 N_("The object directory to store the graph")),
42 argc = parse_options(argc, argv, NULL,
43 builtin_commit_graph_read_options,
44 builtin_commit_graph_read_usage, 0);
47 opts.obj_dir = get_object_directory();
49 graph_name = get_commit_graph_filename(opts.obj_dir);
50 graph = load_commit_graph_one(graph_name);
53 die("graph file %s does not exist", graph_name);
54 FREE_AND_NULL(graph_name);
56 printf("header: %08x %d %d %d %d\n",
57 ntohl(*(uint32_t*)graph->data),
58 *(unsigned char*)(graph->data + 4),
59 *(unsigned char*)(graph->data + 5),
60 *(unsigned char*)(graph->data + 6),
61 *(unsigned char*)(graph->data + 7));
62 printf("num_commits: %u\n", graph->num_commits);
65 if (graph->chunk_oid_fanout)
66 printf(" oid_fanout");
67 if (graph->chunk_oid_lookup)
68 printf(" oid_lookup");
69 if (graph->chunk_commit_data)
70 printf(" commit_metadata");
71 if (graph->chunk_large_edges)
72 printf(" large_edges");
78 static int graph_write(int argc, const char **argv)
80 const char **pack_indexes = NULL;
82 const char **lines = NULL;
86 static struct option builtin_commit_graph_write_options[] = {
87 OPT_STRING(0, "object-dir", &opts.obj_dir,
89 N_("The object directory to store the graph")),
90 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
91 N_("scan pack-indexes listed by stdin for commits")),
95 argc = parse_options(argc, argv, NULL,
96 builtin_commit_graph_write_options,
97 builtin_commit_graph_write_usage, 0);
100 opts.obj_dir = get_object_directory();
102 if (opts.stdin_packs) {
103 struct strbuf buf = STRBUF_INIT;
106 ALLOC_ARRAY(lines, lines_alloc);
108 while (strbuf_getline(&buf, stdin) != EOF) {
109 ALLOC_GROW(lines, lines_nr + 1, lines_alloc);
110 lines[lines_nr++] = strbuf_detach(&buf, NULL);
113 pack_indexes = lines;
117 write_commit_graph(opts.obj_dir,
124 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
126 static struct option builtin_commit_graph_options[] = {
127 OPT_STRING(0, "object-dir", &opts.obj_dir,
129 N_("The object directory to store the graph")),
133 if (argc == 2 && !strcmp(argv[1], "-h"))
134 usage_with_options(builtin_commit_graph_usage,
135 builtin_commit_graph_options);
137 git_config(git_default_config, NULL);
138 argc = parse_options(argc, argv, prefix,
139 builtin_commit_graph_options,
140 builtin_commit_graph_usage,
141 PARSE_OPT_STOP_AT_NON_OPTION);
144 if (!strcmp(argv[0], "read"))
145 return graph_read(argc, argv);
146 if (!strcmp(argv[0], "write"))
147 return graph_write(argc, argv);
150 usage_with_options(builtin_commit_graph_usage,
151 builtin_commit_graph_options);