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