interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / builtin / commit-graph.c

384 lines · 10825 bytes Raw Blame History
1#include "builtin.h"
2#include "commit.h"
3#include "config.h"
4#include "environment.h"
5#include "gettext.h"
6#include "hex.h"
7#include "parse-options.h"
8#include "commit-graph.h"
9#include "odb.h"
10#include "odb/source.h"
11#include "odb/source-files.h"
12#include "progress.h"
13#include "replace-object.h"
14#include "strbuf.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
27static const char * const builtin_commit_graph_verify_usage[] = {
28 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE,
29 NULL
30};
31
32static const char * const builtin_commit_graph_write_usage[] = {
33 BUILTIN_COMMIT_GRAPH_WRITE_USAGE,
34 NULL
35};
36
37static char const * const builtin_commit_graph_usage[] = {
38 BUILTIN_COMMIT_GRAPH_VERIFY_USAGE,
39 BUILTIN_COMMIT_GRAPH_WRITE_USAGE,
40 NULL,
41};
42
43struct 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};
54
55#define COMMIT_GRAPH_COMMON_OPTIONS(opts) \
56 OPT_STRING(0, "object-dir", &(opts)->obj_dir, \
57 N_("dir"), \
58 N_("the object directory to store the graph"))
59
60static int cmd_commit_graph_verify(int argc,
61 const char **argv,
62 const char *prefix,
63 struct repository *repo)
64{
65 struct opts_commit_graph opts = {
66 .progress = isatty(2),
67 };
68 struct commit_graph *graph = NULL;
69 struct odb_files_dir *dir = NULL;
70 char *graph_name;
71 char *chain_name;
72 enum { OPENED_NONE, OPENED_GRAPH, OPENED_CHAIN } opened = OPENED_NONE;
73 int fd;
74 struct stat st;
75 int flags = 0;
76 int incomplete_chain = 0;
77 int ret;
78 struct option 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 COMMIT_GRAPH_COMMON_OPTIONS(&opts),
84 OPT_END(),
85 };
86
87 trace2_cmd_mode("verify");
88
89 argc = parse_options(argc, argv, prefix,
90 options,
91 builtin_commit_graph_verify_usage, 0);
92 if (argc)
93 usage_with_options(builtin_commit_graph_verify_usage, options);
94
95 if (!opts.obj_dir)
96 opts.obj_dir = repo_get_object_directory(repo);
97 if (opts.shallow)
98 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
99 if (opts.progress)
100 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
101
102 dir = odb_source_files_find_dir(repo->objects, opts.obj_dir);
103 if (!dir)
104 die(_("could not find object directory matching %s"), opts.obj_dir);
105
106 graph_name = get_commit_graph_filename(dir->abspath);
107 chain_name = get_commit_graph_chain_filename(dir->abspath);
108 if (open_commit_graph(graph_name, &fd, &st))
109 opened = OPENED_GRAPH;
110 else if (errno != ENOENT)
111 die_errno(_("Could not open commit-graph '%s'"), graph_name);
112 else if (open_commit_graph_chain(chain_name, &fd, &st,
113 repo->hash_algo))
114 opened = OPENED_CHAIN;
115 else if (errno != ENOENT)
116 die_errno(_("could not open commit-graph chain '%s'"), chain_name);
117
118 FREE_AND_NULL(graph_name);
119 FREE_AND_NULL(chain_name);
120
121 if (opened == OPENED_NONE)
122 return 0;
123 else if (opened == OPENED_GRAPH)
124 graph = load_commit_graph_one_fd_st(repo, dir->abspath, fd, &st);
125 else
126 graph = load_commit_graph_chain_fd_st(repo->objects, fd, &st,
127 &incomplete_chain);
128
129 if (!graph)
130 return 1;
131
132 ret = verify_commit_graph(graph, flags);
133 free_commit_graph(graph);
134
135 if (incomplete_chain) {
136 error("one or more commit-graph chain files could not be loaded");
137 ret |= 1;
138 }
139
140 return ret;
141}
142
143struct commit_graph_write_config_data {
144 struct commit_graph_opts *write_opts;
145 struct opts_commit_graph *opts;
146};
147
148static int write_option_parse_split(const struct option *opt, const char *arg,
149 int unset)
150{
151 struct commit_graph_write_config_data *data = opt->value;
152
153 BUG_ON_OPT_NEG(unset);
154
155 data->opts->split = 1;
156 if (!arg)
157 return 0;
158
159 if (!strcmp(arg, "no-merge"))
160 data->write_opts->split_flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED;
161 else if (!strcmp(arg, "replace"))
162 data->write_opts->split_flags = COMMIT_GRAPH_SPLIT_REPLACE;
163 else
164 die(_("unrecognized --split argument, %s"), arg);
165
166 return 0;
167}
168
169static int read_one_commit(struct repository *repo,
170 struct oidset *commits,
171 struct progress *progress,
172 const char *hash)
173{
174 struct object *result;
175 struct object_id oid;
176 const char *end;
177
178 if (parse_oid_hex_algop(hash, &oid, &end, repo->hash_algo))
179 return error(_("unexpected non-hex object ID: %s"), hash);
180
181 result = deref_tag(repo, parse_object(repo, &oid),
182 NULL, 0);
183 if (!result)
184 return error(_("invalid object: %s"), hash);
185 else if (object_as_type(result, OBJ_COMMIT, 1))
186 oidset_insert(commits, &result->oid);
187
188 display_progress(progress, oidset_size(commits));
189
190 return 0;
191}
192
193static int write_option_max_new_filters(const struct option *opt,
194 const char *arg,
195 int unset)
196{
197 int *to = opt->value;
198 if (unset)
199 *to = -1;
200 else {
201 const char *s;
202 *to = strtol(arg, (char **)&s, 10);
203 if (*s)
204 return error(_("option `%s' expects a numerical value"),
205 "max-new-filters");
206 }
207 return 0;
208}
209
210static int git_commit_graph_write_config(const char *var, const char *value,
211 const struct config_context *ctx,
212 void *cb_data)
213{
214 struct commit_graph_write_config_data *data = cb_data;
215
216 if (!strcmp(var, "commitgraph.maxnewfilters"))
217 data->write_opts->max_new_filters = git_config_int(var, value, ctx->kvi);
218 else if (!strcmp(var, "commitgraph.changedpaths"))
219 data->opts->enable_changed_paths = git_config_bool(var, value) ? 1 : -1;
220 /*
221 * No need to fall-back to 'git_default_config', since this was already
222 * called in 'cmd_commit_graph()'.
223 */
224 return 0;
225}
226
227static int cmd_commit_graph_write(int argc,
228 const char **argv,
229 const char *prefix,
230 struct repository *repo)
231{
232 struct commit_graph_opts write_opts = {
233 .size_multiple = 2,
234 .max_new_filters = -1,
235 };
236 struct opts_commit_graph opts = {
237 .progress = isatty(2),
238 .enable_changed_paths = -1,
239 };
240 struct commit_graph_write_config_data config_data = {
241 .write_opts = &write_opts,
242 .opts = &opts,
243 };
244 struct string_list pack_indexes = STRING_LIST_INIT_DUP;
245 struct strbuf buf = STRBUF_INIT;
246 struct oidset commits = OIDSET_INIT;
247 struct odb_files_dir *dir = NULL;
248 int result = 0;
249 enum commit_graph_write_flags flags = 0;
250 struct progress *progress = NULL;
251 struct option options[] = {
252 OPT_BOOL(0, "reachable", &opts.reachable,
253 N_("start walk at all refs")),
254 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
255 N_("scan pack-indexes listed by stdin for commits")),
256 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
257 N_("start walk at commits listed by stdin")),
258 OPT_BOOL(0, "append", &opts.append,
259 N_("include all commits already in the commit-graph file")),
260 OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
261 N_("enable computation for changed paths")),
262 OPT_CALLBACK_F(0, "split", &config_data, NULL,
263 N_("allow writing an incremental commit-graph file"),
264 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
265 write_option_parse_split),
266 OPT_INTEGER(0, "max-commits", &write_opts.max_commits,
267 N_("maximum number of commits in a non-base split commit-graph")),
268 OPT_INTEGER(0, "size-multiple", &write_opts.size_multiple,
269 N_("maximum ratio between two levels of a split commit-graph")),
270 OPT_EXPIRY_DATE(0, "expire-time", &write_opts.expire_time,
271 N_("only expire files older than a given date-time")),
272 OPT_CALLBACK_F(0, "max-new-filters", &write_opts.max_new_filters,
273 NULL, N_("maximum number of changed-path Bloom filters to compute"),
274 0, write_option_max_new_filters),
275 OPT_BOOL(0, "progress", &opts.progress,
276 N_("force progress reporting")),
277 COMMIT_GRAPH_COMMON_OPTIONS(&opts),
278 OPT_END(),
279 };
280
281 trace2_cmd_mode("write");
282
283 repo_config(repo, git_commit_graph_write_config, &config_data);
284
285 argc = parse_options(argc, argv, prefix,
286 options,
287 builtin_commit_graph_write_usage, 0);
288 if (argc)
289 usage_with_options(builtin_commit_graph_write_usage, options);
290
291 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
292 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
293 if (!opts.obj_dir)
294 opts.obj_dir = repo_get_object_directory(repo);
295 if (opts.append)
296 flags |= COMMIT_GRAPH_WRITE_APPEND;
297 if (opts.split)
298 flags |= COMMIT_GRAPH_WRITE_SPLIT;
299 if (opts.progress)
300 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
301 if (!opts.enable_changed_paths)
302 flags |= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS;
303 if (opts.enable_changed_paths == 1 ||
304 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
305 flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
306
307 dir = odb_source_files_find_dir(repo->objects, opts.obj_dir);
308 if (!dir)
309 die(_("could not find object directory matching %s"), opts.obj_dir);
310
311 if (opts.reachable) {
312 if (write_commit_graph_reachable(repo, dir->abspath, flags, &write_opts))
313 result = 1;
314 goto cleanup;
315 }
316
317 if (opts.stdin_packs) {
318 struct strbuf packname = STRBUF_INIT;
319 size_t dirlen;
320
321 strbuf_addf(&packname, "%s/pack/", dir->abspath);
322 dirlen = packname.len;
323
324 while (strbuf_getline(&buf, stdin) != EOF) {
325 strbuf_setlen(&packname, dirlen);
326 strbuf_addbuf(&packname, &buf);
327 string_list_append(&pack_indexes, packname.buf);
328 }
329
330 strbuf_release(&packname);
331 } else if (opts.stdin_commits) {
332 oidset_init(&commits, 0);
333 if (opts.progress)
334 progress = start_delayed_progress(
335 repo,
336 _("Collecting commits from input"), 0);
337
338 while (strbuf_getline(&buf, stdin) != EOF) {
339 if (read_one_commit(repo, &commits, progress, buf.buf)) {
340 result = 1;
341 stop_progress(&progress);
342 goto cleanup;
343 }
344 }
345
346 stop_progress(&progress);
347 }
348
349 if (write_commit_graph(repo, dir->abspath,
350 opts.stdin_packs ? &pack_indexes : NULL,
351 opts.stdin_commits ? &commits : NULL,
352 flags,
353 &write_opts))
354 result = 1;
355
356cleanup:
357 string_list_clear(&pack_indexes, 0);
358 strbuf_release(&buf);
359 oidset_clear(&commits);
360 return result;
361}
362
363int cmd_commit_graph(int argc,
364 const char **argv,
365 const char *prefix,
366 struct repository *repo)
367{
368 parse_opt_subcommand_fn *fn = NULL;
369 struct option options[] = {
370 OPT_SUBCOMMAND("verify", &fn, cmd_commit_graph_verify),
371 OPT_SUBCOMMAND("write", &fn, cmd_commit_graph_write),
372 OPT_END(),
373 };
374
375 repo_config(repo, git_default_config, NULL);
376
377 disable_replace_refs();
378 save_commit_buffer = 0;
379
380 argc = parse_options(argc, argv, prefix, options,
381 builtin_commit_graph_usage, 0);
382
383 return fn(argc, argv, prefix, repo);
384}