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