]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/commit-graph.c
Merge branch 'js/t5563-portability-fix'
[thirdparty/git.git] / builtin / commit-graph.c
1 #include "builtin.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "hex.h"
5 #include "lockfile.h"
6 #include "parse-options.h"
7 #include "repository.h"
8 #include "commit-graph.h"
9 #include "object-store.h"
10 #include "progress.h"
11 #include "replace-object.h"
12 #include "tag.h"
13
14 #define BUILTIN_COMMIT_GRAPH_VERIFY_USAGE \
15 N_("git commit-graph verify [--object-dir <dir>] [--shallow] [--[no-]progress]")
16
17 #define BUILTIN_COMMIT_GRAPH_WRITE_USAGE \
18 N_("git commit-graph write [--object-dir <dir>] [--append]\n" \
19 " [--split[=<strategy>]] [--reachable | --stdin-packs | --stdin-commits]\n" \
20 " [--changed-paths] [--[no-]max-new-filters <n>] [--[no-]progress]\n" \
21 " <split options>")
22
23 static const char * builtin_commit_graph_verify_usage[] = {
24 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE,
25 NULL
26 };
27
28 static const char * builtin_commit_graph_write_usage[] = {
29 BUILTIN_COMMIT_GRAPH_WRITE_USAGE,
30 NULL
31 };
32
33 static char const * const builtin_commit_graph_usage[] = {
34 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE,
35 BUILTIN_COMMIT_GRAPH_WRITE_USAGE,
36 NULL,
37 };
38
39 static struct opts_commit_graph {
40 const char *obj_dir;
41 int reachable;
42 int stdin_packs;
43 int stdin_commits;
44 int append;
45 int split;
46 int shallow;
47 int progress;
48 int enable_changed_paths;
49 } opts;
50
51 static struct option common_opts[] = {
52 OPT_STRING(0, "object-dir", &opts.obj_dir,
53 N_("dir"),
54 N_("the object directory to store the graph")),
55 OPT_END()
56 };
57
58 static struct option *add_common_options(struct option *to)
59 {
60 return parse_options_concat(common_opts, to);
61 }
62
63 static int graph_verify(int argc, const char **argv, const char *prefix)
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 int ret;
73
74 static struct option builtin_commit_graph_verify_options[] = {
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,
78 N_("force progress reporting")),
79 OPT_END(),
80 };
81 struct option *options = add_common_options(builtin_commit_graph_verify_options);
82
83 trace2_cmd_mode("verify");
84
85 opts.progress = isatty(2);
86 argc = parse_options(argc, argv, prefix,
87 options,
88 builtin_commit_graph_verify_usage, 0);
89 if (argc)
90 usage_with_options(builtin_commit_graph_verify_usage, options);
91
92 if (!opts.obj_dir)
93 opts.obj_dir = get_object_directory();
94 if (opts.shallow)
95 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
96 if (opts.progress)
97 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
98
99 odb = find_odb(the_repository, opts.obj_dir);
100 graph_name = get_commit_graph_filename(odb);
101 open_ok = open_commit_graph(graph_name, &fd, &st);
102 if (!open_ok && errno != ENOENT)
103 die_errno(_("Could not open commit-graph '%s'"), graph_name);
104
105 FREE_AND_NULL(graph_name);
106 FREE_AND_NULL(options);
107
108 if (open_ok)
109 graph = load_commit_graph_one_fd_st(the_repository, fd, &st, odb);
110 else
111 graph = read_commit_graph_one(the_repository, odb);
112
113 /* Return failure if open_ok predicted success */
114 if (!graph)
115 return !!open_ok;
116
117 ret = verify_commit_graph(the_repository, graph, flags);
118 free_commit_graph(graph);
119 return ret;
120 }
121
122 extern int read_replace_refs;
123 static struct commit_graph_opts write_opts;
124
125 static int write_option_parse_split(const struct option *opt, const char *arg,
126 int unset)
127 {
128 enum commit_graph_split_flags *flags = opt->value;
129
130 BUG_ON_OPT_NEG(unset);
131
132 opts.split = 1;
133 if (!arg)
134 return 0;
135
136 if (!strcmp(arg, "no-merge"))
137 *flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED;
138 else if (!strcmp(arg, "replace"))
139 *flags = COMMIT_GRAPH_SPLIT_REPLACE;
140 else
141 die(_("unrecognized --split argument, %s"), arg);
142
143 return 0;
144 }
145
146 static int read_one_commit(struct oidset *commits, struct progress *progress,
147 const char *hash)
148 {
149 struct object *result;
150 struct object_id oid;
151 const char *end;
152
153 if (parse_oid_hex(hash, &oid, &end))
154 return error(_("unexpected non-hex object ID: %s"), hash);
155
156 result = deref_tag(the_repository, parse_object(the_repository, &oid),
157 NULL, 0);
158 if (!result)
159 return error(_("invalid object: %s"), hash);
160 else if (object_as_type(result, OBJ_COMMIT, 1))
161 oidset_insert(commits, &result->oid);
162
163 display_progress(progress, oidset_size(commits));
164
165 return 0;
166 }
167
168 static int write_option_max_new_filters(const struct option *opt,
169 const char *arg,
170 int unset)
171 {
172 int *to = opt->value;
173 if (unset)
174 *to = -1;
175 else {
176 const char *s;
177 *to = strtol(arg, (char **)&s, 10);
178 if (*s)
179 return error(_("option `%s' expects a numerical value"),
180 "max-new-filters");
181 }
182 return 0;
183 }
184
185 static int git_commit_graph_write_config(const char *var, const char *value,
186 void *cb UNUSED)
187 {
188 if (!strcmp(var, "commitgraph.maxnewfilters"))
189 write_opts.max_new_filters = git_config_int(var, value);
190 /*
191 * No need to fall-back to 'git_default_config', since this was already
192 * called in 'cmd_commit_graph()'.
193 */
194 return 0;
195 }
196
197 static int graph_write(int argc, const char **argv, const char *prefix)
198 {
199 struct string_list pack_indexes = STRING_LIST_INIT_DUP;
200 struct strbuf buf = STRBUF_INIT;
201 struct oidset commits = OIDSET_INIT;
202 struct object_directory *odb = NULL;
203 int result = 0;
204 enum commit_graph_write_flags flags = 0;
205 struct progress *progress = NULL;
206
207 static struct option builtin_commit_graph_write_options[] = {
208 OPT_BOOL(0, "reachable", &opts.reachable,
209 N_("start walk at all refs")),
210 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
211 N_("scan pack-indexes listed by stdin for commits")),
212 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
213 N_("start walk at commits listed by stdin")),
214 OPT_BOOL(0, "append", &opts.append,
215 N_("include all commits already in the commit-graph file")),
216 OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
217 N_("enable computation for changed paths")),
218 OPT_CALLBACK_F(0, "split", &write_opts.split_flags, NULL,
219 N_("allow writing an incremental commit-graph file"),
220 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
221 write_option_parse_split),
222 OPT_INTEGER(0, "max-commits", &write_opts.max_commits,
223 N_("maximum number of commits in a non-base split commit-graph")),
224 OPT_INTEGER(0, "size-multiple", &write_opts.size_multiple,
225 N_("maximum ratio between two levels of a split commit-graph")),
226 OPT_EXPIRY_DATE(0, "expire-time", &write_opts.expire_time,
227 N_("only expire files older than a given date-time")),
228 OPT_CALLBACK_F(0, "max-new-filters", &write_opts.max_new_filters,
229 NULL, N_("maximum number of changed-path Bloom filters to compute"),
230 0, write_option_max_new_filters),
231 OPT_BOOL(0, "progress", &opts.progress,
232 N_("force progress reporting")),
233 OPT_END(),
234 };
235 struct option *options = add_common_options(builtin_commit_graph_write_options);
236
237 opts.progress = isatty(2);
238 opts.enable_changed_paths = -1;
239 write_opts.size_multiple = 2;
240 write_opts.max_commits = 0;
241 write_opts.expire_time = 0;
242 write_opts.max_new_filters = -1;
243
244 trace2_cmd_mode("write");
245
246 git_config(git_commit_graph_write_config, &opts);
247
248 argc = parse_options(argc, argv, prefix,
249 options,
250 builtin_commit_graph_write_usage, 0);
251 if (argc)
252 usage_with_options(builtin_commit_graph_write_usage, options);
253
254 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
255 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
256 if (!opts.obj_dir)
257 opts.obj_dir = get_object_directory();
258 if (opts.append)
259 flags |= COMMIT_GRAPH_WRITE_APPEND;
260 if (opts.split)
261 flags |= COMMIT_GRAPH_WRITE_SPLIT;
262 if (opts.progress)
263 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
264 if (!opts.enable_changed_paths)
265 flags |= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS;
266 if (opts.enable_changed_paths == 1 ||
267 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
268 flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
269
270 odb = find_odb(the_repository, opts.obj_dir);
271
272 if (opts.reachable) {
273 if (write_commit_graph_reachable(odb, flags, &write_opts))
274 result = 1;
275 goto cleanup;
276 }
277
278 if (opts.stdin_packs) {
279 while (strbuf_getline(&buf, stdin) != EOF)
280 string_list_append_nodup(&pack_indexes,
281 strbuf_detach(&buf, NULL));
282 } else if (opts.stdin_commits) {
283 oidset_init(&commits, 0);
284 if (opts.progress)
285 progress = start_delayed_progress(
286 _("Collecting commits from input"), 0);
287
288 while (strbuf_getline(&buf, stdin) != EOF) {
289 if (read_one_commit(&commits, progress, buf.buf)) {
290 result = 1;
291 goto cleanup;
292 }
293 }
294
295 stop_progress(&progress);
296 }
297
298 if (write_commit_graph(odb,
299 opts.stdin_packs ? &pack_indexes : NULL,
300 opts.stdin_commits ? &commits : NULL,
301 flags,
302 &write_opts))
303 result = 1;
304
305 cleanup:
306 FREE_AND_NULL(options);
307 string_list_clear(&pack_indexes, 0);
308 strbuf_release(&buf);
309 return result;
310 }
311
312 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
313 {
314 parse_opt_subcommand_fn *fn = NULL;
315 struct option builtin_commit_graph_options[] = {
316 OPT_SUBCOMMAND("verify", &fn, graph_verify),
317 OPT_SUBCOMMAND("write", &fn, graph_write),
318 OPT_END(),
319 };
320 struct option *options = parse_options_concat(builtin_commit_graph_options, common_opts);
321
322 git_config(git_default_config, NULL);
323
324 read_replace_refs = 0;
325 save_commit_buffer = 0;
326
327 argc = parse_options(argc, argv, prefix, options,
328 builtin_commit_graph_usage, 0);
329 FREE_AND_NULL(options);
330
331 return fn(argc, argv, prefix);
332 }