commit-graph: emit trace2 cmd_mode for each sub-command
[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 "repository.h"
7 #include "commit-graph.h"
8 #include "object-store.h"
9
10 static char const * const builtin_commit_graph_usage[] = {
11         N_("git commit-graph [--object-dir <objdir>]"),
12         N_("git commit-graph read [--object-dir <objdir>]"),
13         N_("git commit-graph verify [--object-dir <objdir>] [--shallow]"),
14         N_("git commit-graph write [--object-dir <objdir>] [--append|--split] [--reachable|--stdin-packs|--stdin-commits] <split options>"),
15         NULL
16 };
17
18 static const char * const builtin_commit_graph_verify_usage[] = {
19         N_("git commit-graph verify [--object-dir <objdir>] [--shallow]"),
20         NULL
21 };
22
23 static const char * const builtin_commit_graph_read_usage[] = {
24         N_("git commit-graph read [--object-dir <objdir>]"),
25         NULL
26 };
27
28 static const char * const builtin_commit_graph_write_usage[] = {
29         N_("git commit-graph write [--object-dir <objdir>] [--append|--split] [--reachable|--stdin-packs|--stdin-commits] <split options>"),
30         NULL
31 };
32
33 static struct opts_commit_graph {
34         const char *obj_dir;
35         int reachable;
36         int stdin_packs;
37         int stdin_commits;
38         int append;
39         int split;
40         int shallow;
41 } opts;
42
43 static int graph_verify(int argc, const char **argv)
44 {
45         struct commit_graph *graph = NULL;
46         char *graph_name;
47         int open_ok;
48         int fd;
49         struct stat st;
50         int flags = 0;
51
52         static struct option builtin_commit_graph_verify_options[] = {
53                 OPT_STRING(0, "object-dir", &opts.obj_dir,
54                            N_("dir"),
55                            N_("The object directory to store the graph")),
56                 OPT_BOOL(0, "shallow", &opts.shallow,
57                          N_("if the commit-graph is split, only verify the tip file")),
58                 OPT_END(),
59         };
60
61         trace2_cmd_mode("verify");
62
63         argc = parse_options(argc, argv, NULL,
64                              builtin_commit_graph_verify_options,
65                              builtin_commit_graph_verify_usage, 0);
66
67         if (!opts.obj_dir)
68                 opts.obj_dir = get_object_directory();
69         if (opts.shallow)
70                 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
71
72         graph_name = get_commit_graph_filename(opts.obj_dir);
73         open_ok = open_commit_graph(graph_name, &fd, &st);
74         if (!open_ok && errno != ENOENT)
75                 die_errno(_("Could not open commit-graph '%s'"), graph_name);
76
77         FREE_AND_NULL(graph_name);
78
79         if (open_ok)
80                 graph = load_commit_graph_one_fd_st(fd, &st);
81          else
82                 graph = read_commit_graph_one(the_repository, opts.obj_dir);
83
84         /* Return failure if open_ok predicted success */
85         if (!graph)
86                 return !!open_ok;
87
88         UNLEAK(graph);
89         return verify_commit_graph(the_repository, graph, flags);
90 }
91
92 static int graph_read(int argc, const char **argv)
93 {
94         struct commit_graph *graph = NULL;
95         char *graph_name;
96         int open_ok;
97         int fd;
98         struct stat st;
99
100         static struct option builtin_commit_graph_read_options[] = {
101                 OPT_STRING(0, "object-dir", &opts.obj_dir,
102                         N_("dir"),
103                         N_("The object directory to store the graph")),
104                 OPT_END(),
105         };
106
107         trace2_cmd_mode("read");
108
109         argc = parse_options(argc, argv, NULL,
110                              builtin_commit_graph_read_options,
111                              builtin_commit_graph_read_usage, 0);
112
113         if (!opts.obj_dir)
114                 opts.obj_dir = get_object_directory();
115
116         graph_name = get_commit_graph_filename(opts.obj_dir);
117
118         open_ok = open_commit_graph(graph_name, &fd, &st);
119         if (!open_ok)
120                 die_errno(_("Could not open commit-graph '%s'"), graph_name);
121
122         graph = load_commit_graph_one_fd_st(fd, &st);
123         if (!graph)
124                 return 1;
125
126         FREE_AND_NULL(graph_name);
127
128         printf("header: %08x %d %d %d %d\n",
129                 ntohl(*(uint32_t*)graph->data),
130                 *(unsigned char*)(graph->data + 4),
131                 *(unsigned char*)(graph->data + 5),
132                 *(unsigned char*)(graph->data + 6),
133                 *(unsigned char*)(graph->data + 7));
134         printf("num_commits: %u\n", graph->num_commits);
135         printf("chunks:");
136
137         if (graph->chunk_oid_fanout)
138                 printf(" oid_fanout");
139         if (graph->chunk_oid_lookup)
140                 printf(" oid_lookup");
141         if (graph->chunk_commit_data)
142                 printf(" commit_metadata");
143         if (graph->chunk_extra_edges)
144                 printf(" extra_edges");
145         printf("\n");
146
147         UNLEAK(graph);
148
149         return 0;
150 }
151
152 extern int read_replace_refs;
153 static struct split_commit_graph_opts split_opts;
154
155 static int graph_write(int argc, const char **argv)
156 {
157         struct string_list *pack_indexes = NULL;
158         struct string_list *commit_hex = NULL;
159         struct string_list lines;
160         int result = 0;
161         enum commit_graph_write_flags flags = COMMIT_GRAPH_WRITE_PROGRESS;
162
163         static struct option builtin_commit_graph_write_options[] = {
164                 OPT_STRING(0, "object-dir", &opts.obj_dir,
165                         N_("dir"),
166                         N_("The object directory to store the graph")),
167                 OPT_BOOL(0, "reachable", &opts.reachable,
168                         N_("start walk at all refs")),
169                 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
170                         N_("scan pack-indexes listed by stdin for commits")),
171                 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
172                         N_("start walk at commits listed by stdin")),
173                 OPT_BOOL(0, "append", &opts.append,
174                         N_("include all commits already in the commit-graph file")),
175                 OPT_BOOL(0, "split", &opts.split,
176                         N_("allow writing an incremental commit-graph file")),
177                 OPT_INTEGER(0, "max-commits", &split_opts.max_commits,
178                         N_("maximum number of commits in a non-base split commit-graph")),
179                 OPT_INTEGER(0, "size-multiple", &split_opts.size_multiple,
180                         N_("maximum ratio between two levels of a split commit-graph")),
181                 OPT_EXPIRY_DATE(0, "expire-time", &split_opts.expire_time,
182                         N_("maximum number of commits in a non-base split commit-graph")),
183                 OPT_END(),
184         };
185
186         split_opts.size_multiple = 2;
187         split_opts.max_commits = 0;
188         split_opts.expire_time = 0;
189
190         trace2_cmd_mode("write");
191
192         argc = parse_options(argc, argv, NULL,
193                              builtin_commit_graph_write_options,
194                              builtin_commit_graph_write_usage, 0);
195
196         if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
197                 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
198         if (!opts.obj_dir)
199                 opts.obj_dir = get_object_directory();
200         if (opts.append)
201                 flags |= COMMIT_GRAPH_WRITE_APPEND;
202         if (opts.split)
203                 flags |= COMMIT_GRAPH_WRITE_SPLIT;
204
205         read_replace_refs = 0;
206
207         if (opts.reachable) {
208                 if (write_commit_graph_reachable(opts.obj_dir, flags, &split_opts))
209                         return 1;
210                 return 0;
211         }
212
213         string_list_init(&lines, 0);
214         if (opts.stdin_packs || opts.stdin_commits) {
215                 struct strbuf buf = STRBUF_INIT;
216
217                 while (strbuf_getline(&buf, stdin) != EOF)
218                         string_list_append(&lines, strbuf_detach(&buf, NULL));
219
220                 if (opts.stdin_packs)
221                         pack_indexes = &lines;
222                 if (opts.stdin_commits) {
223                         commit_hex = &lines;
224                         flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS;
225                 }
226
227                 UNLEAK(buf);
228         }
229
230         if (write_commit_graph(opts.obj_dir,
231                                pack_indexes,
232                                commit_hex,
233                                flags,
234                                &split_opts))
235                 result = 1;
236
237         UNLEAK(lines);
238         return result;
239 }
240
241 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
242 {
243         static struct option builtin_commit_graph_options[] = {
244                 OPT_STRING(0, "object-dir", &opts.obj_dir,
245                         N_("dir"),
246                         N_("The object directory to store the graph")),
247                 OPT_END(),
248         };
249
250         if (argc == 2 && !strcmp(argv[1], "-h"))
251                 usage_with_options(builtin_commit_graph_usage,
252                                    builtin_commit_graph_options);
253
254         git_config(git_default_config, NULL);
255         argc = parse_options(argc, argv, prefix,
256                              builtin_commit_graph_options,
257                              builtin_commit_graph_usage,
258                              PARSE_OPT_STOP_AT_NON_OPTION);
259
260         if (argc > 0) {
261                 if (!strcmp(argv[0], "read"))
262                         return graph_read(argc, argv);
263                 if (!strcmp(argv[0], "verify"))
264                         return graph_verify(argc, argv);
265                 if (!strcmp(argv[0], "write"))
266                         return graph_write(argc, argv);
267         }
268
269         usage_with_options(builtin_commit_graph_usage,
270                            builtin_commit_graph_options);
271 }