commit-graph: implement git commit-graph read
[git] / builtin / commit-graph.c
1 #include "builtin.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "lockfile.h"
5 #include "parse-options.h"
6 #include "commit-graph.h"
7
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>]"),
12         NULL
13 };
14
15 static const char * const builtin_commit_graph_read_usage[] = {
16         N_("git commit-graph read [--object-dir <objdir>]"),
17         NULL
18 };
19
20 static const char * const builtin_commit_graph_write_usage[] = {
21         N_("git commit-graph write [--object-dir <objdir>]"),
22         NULL
23 };
24
25 static struct opts_commit_graph {
26         const char *obj_dir;
27 } opts;
28
29 static int graph_read(int argc, const char **argv)
30 {
31         struct commit_graph *graph = NULL;
32         char *graph_name;
33
34         static struct option builtin_commit_graph_read_options[] = {
35                 OPT_STRING(0, "object-dir", &opts.obj_dir,
36                         N_("dir"),
37                         N_("The object directory to store the graph")),
38                 OPT_END(),
39         };
40
41         argc = parse_options(argc, argv, NULL,
42                              builtin_commit_graph_read_options,
43                              builtin_commit_graph_read_usage, 0);
44
45         if (!opts.obj_dir)
46                 opts.obj_dir = get_object_directory();
47
48         graph_name = get_commit_graph_filename(opts.obj_dir);
49         graph = load_commit_graph_one(graph_name);
50
51         if (!graph)
52                 die("graph file %s does not exist", graph_name);
53         FREE_AND_NULL(graph_name);
54
55         printf("header: %08x %d %d %d %d\n",
56                 ntohl(*(uint32_t*)graph->data),
57                 *(unsigned char*)(graph->data + 4),
58                 *(unsigned char*)(graph->data + 5),
59                 *(unsigned char*)(graph->data + 6),
60                 *(unsigned char*)(graph->data + 7));
61         printf("num_commits: %u\n", graph->num_commits);
62         printf("chunks:");
63
64         if (graph->chunk_oid_fanout)
65                 printf(" oid_fanout");
66         if (graph->chunk_oid_lookup)
67                 printf(" oid_lookup");
68         if (graph->chunk_commit_data)
69                 printf(" commit_metadata");
70         if (graph->chunk_large_edges)
71                 printf(" large_edges");
72         printf("\n");
73
74         return 0;
75 }
76
77 static int graph_write(int argc, const char **argv)
78 {
79         static struct option builtin_commit_graph_write_options[] = {
80                 OPT_STRING(0, "object-dir", &opts.obj_dir,
81                         N_("dir"),
82                         N_("The object directory to store the graph")),
83                 OPT_END(),
84         };
85
86         argc = parse_options(argc, argv, NULL,
87                              builtin_commit_graph_write_options,
88                              builtin_commit_graph_write_usage, 0);
89
90         if (!opts.obj_dir)
91                 opts.obj_dir = get_object_directory();
92
93         write_commit_graph(opts.obj_dir);
94         return 0;
95 }
96
97 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
98 {
99         static struct option builtin_commit_graph_options[] = {
100                 OPT_STRING(0, "object-dir", &opts.obj_dir,
101                         N_("dir"),
102                         N_("The object directory to store the graph")),
103                 OPT_END(),
104         };
105
106         if (argc == 2 && !strcmp(argv[1], "-h"))
107                 usage_with_options(builtin_commit_graph_usage,
108                                    builtin_commit_graph_options);
109
110         git_config(git_default_config, NULL);
111         argc = parse_options(argc, argv, prefix,
112                              builtin_commit_graph_options,
113                              builtin_commit_graph_usage,
114                              PARSE_OPT_STOP_AT_NON_OPTION);
115
116         if (argc > 0) {
117                 if (!strcmp(argv[0], "read"))
118                         return graph_read(argc, argv);
119                 if (!strcmp(argv[0], "write"))
120                         return graph_write(argc, argv);
121         }
122
123         usage_with_options(builtin_commit_graph_usage,
124                            builtin_commit_graph_options);
125 }