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>] [--file=<hash>]"),
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>] [--file=<hash>]"),
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         const char *graph_file;
28 } opts;
29
30 static int graph_read(int argc, const char **argv)
31 {
32         struct commit_graph *graph = 0;
33         struct strbuf full_path = STRBUF_INIT;
34
35         static struct option builtin_commit_graph_read_options[] = {
36                 { OPTION_STRING, 'o', "object-dir", &opts.obj_dir,
37                         N_("dir"),
38                         N_("The object directory to store the graph") },
39                 { OPTION_STRING, 'H', "file", &opts.graph_file,
40                         N_("file"),
41                         N_("The filename for a specific commit graph file in the object directory."),
42                         PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
43                 OPT_END(),
44         };
45
46         argc = parse_options(argc, argv, NULL,
47                              builtin_commit_graph_read_options,
48                              builtin_commit_graph_read_usage, 0);
49
50         if (!opts.obj_dir)
51                 opts.obj_dir = get_object_directory();
52
53         if (!opts.graph_file)
54                 die("no graph hash specified");
55
56         strbuf_addf(&full_path, "%s/info/%s", opts.obj_dir, opts.graph_file);
57         graph = load_commit_graph_one(full_path.buf);
58
59         if (!graph)
60                 die("graph file %s does not exist", full_path.buf);
61
62         printf("header: %08x %d %d %d %d\n",
63                 ntohl(*(uint32_t*)graph->data),
64                 *(unsigned char*)(graph->data + 4),
65                 *(unsigned char*)(graph->data + 5),
66                 *(unsigned char*)(graph->data + 6),
67                 *(unsigned char*)(graph->data + 7));
68         printf("num_commits: %u\n", graph->num_commits);
69         printf("chunks:");
70
71         if (graph->chunk_oid_fanout)
72                 printf(" oid_fanout");
73         if (graph->chunk_oid_lookup)
74                 printf(" oid_lookup");
75         if (graph->chunk_commit_data)
76                 printf(" commit_metadata");
77         if (graph->chunk_large_edges)
78                 printf(" large_edges");
79         printf("\n");
80
81         return 0;
82 }
83
84 static int graph_write(int argc, const char **argv)
85 {
86         char *graph_name;
87
88         static struct option builtin_commit_graph_write_options[] = {
89                 { OPTION_STRING, 'o', "object-dir", &opts.obj_dir,
90                         N_("dir"),
91                         N_("The object directory to store the graph") },
92                 OPT_END(),
93         };
94
95         argc = parse_options(argc, argv, NULL,
96                              builtin_commit_graph_write_options,
97                              builtin_commit_graph_write_usage, 0);
98
99         if (!opts.obj_dir)
100                 opts.obj_dir = get_object_directory();
101
102         graph_name = write_commit_graph(opts.obj_dir);
103
104         if (graph_name) {
105                 printf("%s\n", graph_name);
106                 FREE_AND_NULL(graph_name);
107         }
108
109         return 0;
110 }
111
112 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
113 {
114         static struct option builtin_commit_graph_options[] = {
115                 { OPTION_STRING, 'o', "object-dir", &opts.obj_dir,
116                         N_("dir"),
117                         N_("The object directory to store the graph") },
118                 OPT_END(),
119         };
120
121         if (argc == 2 && !strcmp(argv[1], "-h"))
122                 usage_with_options(builtin_commit_graph_usage,
123                                    builtin_commit_graph_options);
124
125         git_config(git_default_config, NULL);
126         argc = parse_options(argc, argv, prefix,
127                              builtin_commit_graph_options,
128                              builtin_commit_graph_usage,
129                              PARSE_OPT_STOP_AT_NON_OPTION);
130
131         if (argc > 0) {
132                 if (!strcmp(argv[0], "read"))
133                         return graph_read(argc, argv);
134                 if (!strcmp(argv[0], "write"))
135                         return graph_write(argc, argv);
136         }
137
138         usage_with_options(builtin_commit_graph_usage,
139                            builtin_commit_graph_options);
140 }