commit-graph: return with errors during write
[git.git] / builtin / commit-graph.c
blob2a1c4d701fcb50605b46c441dd16729f8b7c6605
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"
9 static char const * const builtin_commit_graph_usage[] = {
10 N_("git commit-graph [--object-dir <objdir>]"),
11 N_("git commit-graph read [--object-dir <objdir>]"),
12 N_("git commit-graph verify [--object-dir <objdir>]"),
13 N_("git commit-graph write [--object-dir <objdir>] [--append] [--reachable|--stdin-packs|--stdin-commits]"),
14 NULL
17 static const char * const builtin_commit_graph_verify_usage[] = {
18 N_("git commit-graph verify [--object-dir <objdir>]"),
19 NULL
22 static const char * const builtin_commit_graph_read_usage[] = {
23 N_("git commit-graph read [--object-dir <objdir>]"),
24 NULL
27 static const char * const builtin_commit_graph_write_usage[] = {
28 N_("git commit-graph write [--object-dir <objdir>] [--append] [--reachable|--stdin-packs|--stdin-commits]"),
29 NULL
32 static struct opts_commit_graph {
33 const char *obj_dir;
34 int reachable;
35 int stdin_packs;
36 int stdin_commits;
37 int append;
38 } opts;
41 static int graph_verify(int argc, const char **argv)
43 struct commit_graph *graph = NULL;
44 char *graph_name;
45 int open_ok;
46 int fd;
47 struct stat st;
49 static struct option builtin_commit_graph_verify_options[] = {
50 OPT_STRING(0, "object-dir", &opts.obj_dir,
51 N_("dir"),
52 N_("The object directory to store the graph")),
53 OPT_END(),
56 argc = parse_options(argc, argv, NULL,
57 builtin_commit_graph_verify_options,
58 builtin_commit_graph_verify_usage, 0);
60 if (!opts.obj_dir)
61 opts.obj_dir = get_object_directory();
63 graph_name = get_commit_graph_filename(opts.obj_dir);
64 open_ok = open_commit_graph(graph_name, &fd, &st);
65 if (!open_ok && errno == ENOENT)
66 return 0;
67 if (!open_ok)
68 die_errno(_("Could not open commit-graph '%s'"), graph_name);
69 graph = load_commit_graph_one_fd_st(fd, &st);
70 FREE_AND_NULL(graph_name);
72 if (!graph)
73 return 1;
75 UNLEAK(graph);
76 return verify_commit_graph(the_repository, graph);
79 static int graph_read(int argc, const char **argv)
81 struct commit_graph *graph = NULL;
82 char *graph_name;
83 int open_ok;
84 int fd;
85 struct stat st;
87 static struct option builtin_commit_graph_read_options[] = {
88 OPT_STRING(0, "object-dir", &opts.obj_dir,
89 N_("dir"),
90 N_("The object directory to store the graph")),
91 OPT_END(),
94 argc = parse_options(argc, argv, NULL,
95 builtin_commit_graph_read_options,
96 builtin_commit_graph_read_usage, 0);
98 if (!opts.obj_dir)
99 opts.obj_dir = get_object_directory();
101 graph_name = get_commit_graph_filename(opts.obj_dir);
103 open_ok = open_commit_graph(graph_name, &fd, &st);
104 if (!open_ok)
105 die_errno(_("Could not open commit-graph '%s'"), graph_name);
107 graph = load_commit_graph_one_fd_st(fd, &st);
108 if (!graph)
109 return 1;
111 FREE_AND_NULL(graph_name);
113 printf("header: %08x %d %d %d %d\n",
114 ntohl(*(uint32_t*)graph->data),
115 *(unsigned char*)(graph->data + 4),
116 *(unsigned char*)(graph->data + 5),
117 *(unsigned char*)(graph->data + 6),
118 *(unsigned char*)(graph->data + 7));
119 printf("num_commits: %u\n", graph->num_commits);
120 printf("chunks:");
122 if (graph->chunk_oid_fanout)
123 printf(" oid_fanout");
124 if (graph->chunk_oid_lookup)
125 printf(" oid_lookup");
126 if (graph->chunk_commit_data)
127 printf(" commit_metadata");
128 if (graph->chunk_extra_edges)
129 printf(" extra_edges");
130 printf("\n");
132 UNLEAK(graph);
134 return 0;
137 extern int read_replace_refs;
139 static int graph_write(int argc, const char **argv)
141 struct string_list *pack_indexes = NULL;
142 struct string_list *commit_hex = NULL;
143 struct string_list lines;
144 int result = 0;
146 static struct option builtin_commit_graph_write_options[] = {
147 OPT_STRING(0, "object-dir", &opts.obj_dir,
148 N_("dir"),
149 N_("The object directory to store the graph")),
150 OPT_BOOL(0, "reachable", &opts.reachable,
151 N_("start walk at all refs")),
152 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
153 N_("scan pack-indexes listed by stdin for commits")),
154 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
155 N_("start walk at commits listed by stdin")),
156 OPT_BOOL(0, "append", &opts.append,
157 N_("include all commits already in the commit-graph file")),
158 OPT_END(),
161 argc = parse_options(argc, argv, NULL,
162 builtin_commit_graph_write_options,
163 builtin_commit_graph_write_usage, 0);
165 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
166 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
167 if (!opts.obj_dir)
168 opts.obj_dir = get_object_directory();
170 read_replace_refs = 0;
172 if (opts.reachable)
173 return write_commit_graph_reachable(opts.obj_dir, opts.append, 1);
175 string_list_init(&lines, 0);
176 if (opts.stdin_packs || opts.stdin_commits) {
177 struct strbuf buf = STRBUF_INIT;
179 while (strbuf_getline(&buf, stdin) != EOF)
180 string_list_append(&lines, strbuf_detach(&buf, NULL));
182 if (opts.stdin_packs)
183 pack_indexes = &lines;
184 if (opts.stdin_commits)
185 commit_hex = &lines;
187 UNLEAK(buf);
190 if (write_commit_graph(opts.obj_dir,
191 pack_indexes,
192 commit_hex,
193 opts.append,
195 result = 1;
197 UNLEAK(lines);
198 return result;
201 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
203 static struct option builtin_commit_graph_options[] = {
204 OPT_STRING(0, "object-dir", &opts.obj_dir,
205 N_("dir"),
206 N_("The object directory to store the graph")),
207 OPT_END(),
210 if (argc == 2 && !strcmp(argv[1], "-h"))
211 usage_with_options(builtin_commit_graph_usage,
212 builtin_commit_graph_options);
214 git_config(git_default_config, NULL);
215 argc = parse_options(argc, argv, prefix,
216 builtin_commit_graph_options,
217 builtin_commit_graph_usage,
218 PARSE_OPT_STOP_AT_NON_OPTION);
220 if (argc > 0) {
221 if (!strcmp(argv[0], "read"))
222 return graph_read(argc, argv);
223 if (!strcmp(argv[0], "verify"))
224 return graph_verify(argc, argv);
225 if (!strcmp(argv[0], "write"))
226 return graph_write(argc, argv);
229 usage_with_options(builtin_commit_graph_usage,
230 builtin_commit_graph_options);