commit-graph.c: introduce '--[no-]check-oids'
[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 verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
12         N_("git commit-graph write [--object-dir <objdir>] [--append] "
13            "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
14            "[--[no-]progress] [--[no-]check-oids] <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] [--[no-]progress]"),
20         NULL
21 };
22
23 static const char * const builtin_commit_graph_write_usage[] = {
24         N_("git commit-graph write [--object-dir <objdir>] [--append] "
25            "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
26            "[--[no-]progress] [--[no-]check-oids] <split options>"),
27         NULL
28 };
29
30 static struct opts_commit_graph {
31         const char *obj_dir;
32         int reachable;
33         int stdin_packs;
34         int stdin_commits;
35         int append;
36         int split;
37         int shallow;
38         int progress;
39         int check_oids;
40 } opts;
41
42 static struct object_directory *find_odb(struct repository *r,
43                                          const char *obj_dir)
44 {
45         struct object_directory *odb;
46         char *obj_dir_real = real_pathdup(obj_dir, 1);
47
48         prepare_alt_odb(r);
49         for (odb = r->objects->odb; odb; odb = odb->next) {
50                 if (!strcmp(obj_dir_real, real_path(odb->path)))
51                         break;
52         }
53
54         free(obj_dir_real);
55
56         if (!odb)
57                 die(_("could not find object directory matching %s"), obj_dir);
58         return odb;
59 }
60
61 static int graph_verify(int argc, const char **argv)
62 {
63         struct commit_graph *graph = NULL;
64         struct object_directory *odb = NULL;
65         char *graph_name;
66         int open_ok;
67         int fd;
68         struct stat st;
69         int flags = 0;
70
71         static struct option builtin_commit_graph_verify_options[] = {
72                 OPT_STRING(0, "object-dir", &opts.obj_dir,
73                            N_("dir"),
74                            N_("The object directory to store the graph")),
75                 OPT_BOOL(0, "shallow", &opts.shallow,
76                          N_("if the commit-graph is split, only verify the tip file")),
77                 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
78                 OPT_END(),
79         };
80
81         trace2_cmd_mode("verify");
82
83         opts.progress = isatty(2);
84         argc = parse_options(argc, argv, NULL,
85                              builtin_commit_graph_verify_options,
86                              builtin_commit_graph_verify_usage, 0);
87
88         if (!opts.obj_dir)
89                 opts.obj_dir = get_object_directory();
90         if (opts.shallow)
91                 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
92         if (opts.progress)
93                 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
94
95         odb = find_odb(the_repository, opts.obj_dir);
96         graph_name = get_commit_graph_filename(odb);
97         open_ok = open_commit_graph(graph_name, &fd, &st);
98         if (!open_ok && errno != ENOENT)
99                 die_errno(_("Could not open commit-graph '%s'"), graph_name);
100
101         FREE_AND_NULL(graph_name);
102
103         if (open_ok)
104                 graph = load_commit_graph_one_fd_st(fd, &st, odb);
105         else
106                 graph = read_commit_graph_one(the_repository, odb);
107
108         /* Return failure if open_ok predicted success */
109         if (!graph)
110                 return !!open_ok;
111
112         UNLEAK(graph);
113         return verify_commit_graph(the_repository, graph, flags);
114 }
115
116 extern int read_replace_refs;
117 static struct split_commit_graph_opts split_opts;
118
119 static int write_option_parse_split(const struct option *opt, const char *arg,
120                                     int unset)
121 {
122         enum commit_graph_split_flags *flags = opt->value;
123
124         opts.split = 1;
125         if (!arg)
126                 return 0;
127
128         if (!strcmp(arg, "no-merge"))
129                 *flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED;
130         else if (!strcmp(arg, "replace"))
131                 *flags = COMMIT_GRAPH_SPLIT_REPLACE;
132         else
133                 die(_("unrecognized --split argument, %s"), arg);
134
135         return 0;
136 }
137
138 static int graph_write(int argc, const char **argv)
139 {
140         struct string_list *pack_indexes = NULL;
141         struct oidset commits = OIDSET_INIT;
142         struct object_directory *odb = NULL;
143         struct string_list lines;
144         int result = 0;
145         enum commit_graph_write_flags flags = 0;
146
147         static struct option builtin_commit_graph_write_options[] = {
148                 OPT_STRING(0, "object-dir", &opts.obj_dir,
149                         N_("dir"),
150                         N_("The object directory to store the graph")),
151                 OPT_BOOL(0, "reachable", &opts.reachable,
152                         N_("start walk at all refs")),
153                 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
154                         N_("scan pack-indexes listed by stdin for commits")),
155                 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
156                         N_("start walk at commits listed by stdin")),
157                 OPT_BOOL(0, "append", &opts.append,
158                         N_("include all commits already in the commit-graph file")),
159                 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
160                 OPT_CALLBACK_F(0, "split", &split_opts.flags, NULL,
161                         N_("allow writing an incremental commit-graph file"),
162                         PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
163                         write_option_parse_split),
164                 OPT_BOOL(0, "check-oids", &opts.check_oids,
165                         N_("require OIDs to be commits")),
166                 OPT_INTEGER(0, "max-commits", &split_opts.max_commits,
167                         N_("maximum number of commits in a non-base split commit-graph")),
168                 OPT_INTEGER(0, "size-multiple", &split_opts.size_multiple,
169                         N_("maximum ratio between two levels of a split commit-graph")),
170                 OPT_EXPIRY_DATE(0, "expire-time", &split_opts.expire_time,
171                         N_("maximum number of commits in a non-base split commit-graph")),
172                 OPT_END(),
173         };
174
175         opts.progress = isatty(2);
176         opts.check_oids = 1;
177         split_opts.size_multiple = 2;
178         split_opts.max_commits = 0;
179         split_opts.expire_time = 0;
180
181         trace2_cmd_mode("write");
182
183         argc = parse_options(argc, argv, NULL,
184                              builtin_commit_graph_write_options,
185                              builtin_commit_graph_write_usage, 0);
186
187         if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
188                 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
189         if (!opts.obj_dir)
190                 opts.obj_dir = get_object_directory();
191         if (opts.append)
192                 flags |= COMMIT_GRAPH_WRITE_APPEND;
193         if (opts.split)
194                 flags |= COMMIT_GRAPH_WRITE_SPLIT;
195         if (opts.progress)
196                 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
197
198         read_replace_refs = 0;
199         odb = find_odb(the_repository, opts.obj_dir);
200
201         if (opts.reachable) {
202                 if (write_commit_graph_reachable(odb, flags, &split_opts))
203                         return 1;
204                 return 0;
205         }
206
207         string_list_init(&lines, 0);
208         if (opts.stdin_packs || opts.stdin_commits) {
209                 struct strbuf buf = STRBUF_INIT;
210
211                 while (strbuf_getline(&buf, stdin) != EOF)
212                         string_list_append(&lines, strbuf_detach(&buf, NULL));
213
214                 if (opts.stdin_packs)
215                         pack_indexes = &lines;
216                 if (opts.stdin_commits) {
217                         struct string_list_item *item;
218                         oidset_init(&commits, lines.nr);
219                         for_each_string_list_item(item, &lines) {
220                                 struct object_id oid;
221                                 const char *end;
222
223                                 if (parse_oid_hex(item->string, &oid, &end)) {
224                                         error(_("unexpected non-hex object ID: "
225                                                 "%s"), item->string);
226                                         return 1;
227                                 }
228
229                                 oidset_insert(&commits, &oid);
230                         }
231                         if (opts.check_oids)
232                                 flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS;
233                 }
234
235                 UNLEAK(buf);
236         }
237
238         if (write_commit_graph(odb,
239                                pack_indexes,
240                                opts.stdin_commits ? &commits : NULL,
241                                flags,
242                                &split_opts))
243                 result = 1;
244
245         UNLEAK(lines);
246         return result;
247 }
248
249 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
250 {
251         static struct option builtin_commit_graph_options[] = {
252                 OPT_STRING(0, "object-dir", &opts.obj_dir,
253                         N_("dir"),
254                         N_("The object directory to store the graph")),
255                 OPT_END(),
256         };
257
258         if (argc == 2 && !strcmp(argv[1], "-h"))
259                 usage_with_options(builtin_commit_graph_usage,
260                                    builtin_commit_graph_options);
261
262         git_config(git_default_config, NULL);
263         argc = parse_options(argc, argv, prefix,
264                              builtin_commit_graph_options,
265                              builtin_commit_graph_usage,
266                              PARSE_OPT_STOP_AT_NON_OPTION);
267
268         save_commit_buffer = 0;
269
270         if (argc > 0) {
271                 if (!strcmp(argv[0], "verify"))
272                         return graph_verify(argc, argv);
273                 if (!strcmp(argv[0], "write"))
274                         return graph_write(argc, argv);
275         }
276
277         usage_with_options(builtin_commit_graph_usage,
278                            builtin_commit_graph_options);
279 }