commit-graph: build graph from starting commits
[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>] [--stdin-packs|--stdin-commits]"),
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>] [--stdin-packs|--stdin-commits]"),
22         NULL
23 };
24
25 static struct opts_commit_graph {
26         const char *obj_dir;
27         int stdin_packs;
28         int stdin_commits;
29 } opts;
30
31 static int graph_read(int argc, const char **argv)
32 {
33         struct commit_graph *graph = NULL;
34         char *graph_name;
35
36         static struct option builtin_commit_graph_read_options[] = {
37                 OPT_STRING(0, "object-dir", &opts.obj_dir,
38                         N_("dir"),
39                         N_("The object directory to store the graph")),
40                 OPT_END(),
41         };
42
43         argc = parse_options(argc, argv, NULL,
44                              builtin_commit_graph_read_options,
45                              builtin_commit_graph_read_usage, 0);
46
47         if (!opts.obj_dir)
48                 opts.obj_dir = get_object_directory();
49
50         graph_name = get_commit_graph_filename(opts.obj_dir);
51         graph = load_commit_graph_one(graph_name);
52
53         if (!graph)
54                 die("graph file %s does not exist", graph_name);
55         FREE_AND_NULL(graph_name);
56
57         printf("header: %08x %d %d %d %d\n",
58                 ntohl(*(uint32_t*)graph->data),
59                 *(unsigned char*)(graph->data + 4),
60                 *(unsigned char*)(graph->data + 5),
61                 *(unsigned char*)(graph->data + 6),
62                 *(unsigned char*)(graph->data + 7));
63         printf("num_commits: %u\n", graph->num_commits);
64         printf("chunks:");
65
66         if (graph->chunk_oid_fanout)
67                 printf(" oid_fanout");
68         if (graph->chunk_oid_lookup)
69                 printf(" oid_lookup");
70         if (graph->chunk_commit_data)
71                 printf(" commit_metadata");
72         if (graph->chunk_large_edges)
73                 printf(" large_edges");
74         printf("\n");
75
76         return 0;
77 }
78
79 static int graph_write(int argc, const char **argv)
80 {
81         const char **pack_indexes = NULL;
82         int packs_nr = 0;
83         const char **commit_hex = NULL;
84         int commits_nr = 0;
85         const char **lines = NULL;
86         int lines_nr = 0;
87         int lines_alloc = 0;
88
89         static struct option builtin_commit_graph_write_options[] = {
90                 OPT_STRING(0, "object-dir", &opts.obj_dir,
91                         N_("dir"),
92                         N_("The object directory to store the graph")),
93                 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
94                         N_("scan pack-indexes listed by stdin for commits")),
95                 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
96                         N_("start walk at commits listed by stdin")),
97                 OPT_END(),
98         };
99
100         argc = parse_options(argc, argv, NULL,
101                              builtin_commit_graph_write_options,
102                              builtin_commit_graph_write_usage, 0);
103
104         if (opts.stdin_packs && opts.stdin_commits)
105                 die(_("cannot use both --stdin-commits and --stdin-packs"));
106         if (!opts.obj_dir)
107                 opts.obj_dir = get_object_directory();
108
109         if (opts.stdin_packs || opts.stdin_commits) {
110                 struct strbuf buf = STRBUF_INIT;
111                 lines_nr = 0;
112                 lines_alloc = 128;
113                 ALLOC_ARRAY(lines, lines_alloc);
114
115                 while (strbuf_getline(&buf, stdin) != EOF) {
116                         ALLOC_GROW(lines, lines_nr + 1, lines_alloc);
117                         lines[lines_nr++] = strbuf_detach(&buf, NULL);
118                 }
119
120                 if (opts.stdin_packs) {
121                         pack_indexes = lines;
122                         packs_nr = lines_nr;
123                 }
124                 if (opts.stdin_commits) {
125                         commit_hex = lines;
126                         commits_nr = lines_nr;
127                 }
128         }
129
130         write_commit_graph(opts.obj_dir,
131                            pack_indexes,
132                            packs_nr,
133                            commit_hex,
134                            commits_nr);
135
136         return 0;
137 }
138
139 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
140 {
141         static struct option builtin_commit_graph_options[] = {
142                 OPT_STRING(0, "object-dir", &opts.obj_dir,
143                         N_("dir"),
144                         N_("The object directory to store the graph")),
145                 OPT_END(),
146         };
147
148         if (argc == 2 && !strcmp(argv[1], "-h"))
149                 usage_with_options(builtin_commit_graph_usage,
150                                    builtin_commit_graph_options);
151
152         git_config(git_default_config, NULL);
153         argc = parse_options(argc, argv, prefix,
154                              builtin_commit_graph_options,
155                              builtin_commit_graph_usage,
156                              PARSE_OPT_STOP_AT_NON_OPTION);
157
158         if (argc > 0) {
159                 if (!strcmp(argv[0], "read"))
160                         return graph_read(argc, argv);
161                 if (!strcmp(argv[0], "write"))
162                         return graph_write(argc, argv);
163         }
164
165         usage_with_options(builtin_commit_graph_usage,
166                            builtin_commit_graph_options);
167 }