Merge branch 'dl/test-must-fail-fixes-5'
[git/raj.git] / builtin / commit-graph.c
blobf6797e2a9fb3210192dc65a044b084458ab632d6
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 #include "progress.h"
10 #include "tag.h"
12 static char const * const builtin_commit_graph_usage[] = {
13 N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
14 N_("git commit-graph write [--object-dir <objdir>] [--append] "
15 "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
16 "[--changed-paths] [--[no-]progress] <split options>"),
17 NULL
20 static const char * const builtin_commit_graph_verify_usage[] = {
21 N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
22 NULL
25 static const char * const builtin_commit_graph_write_usage[] = {
26 N_("git commit-graph write [--object-dir <objdir>] [--append] "
27 "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] "
28 "[--changed-paths] [--[no-]progress] <split options>"),
29 NULL
32 static struct opts_commit_graph {
33 const char *obj_dir;
34 int reachable;
35 int stdin_packs;
36 int stdin_commits;
37 int append;
38 int split;
39 int shallow;
40 int progress;
41 int enable_changed_paths;
42 } opts;
44 static struct object_directory *find_odb(struct repository *r,
45 const char *obj_dir)
47 struct object_directory *odb;
48 char *obj_dir_real = real_pathdup(obj_dir, 1);
49 struct strbuf odb_path_real = STRBUF_INIT;
51 prepare_alt_odb(r);
52 for (odb = r->objects->odb; odb; odb = odb->next) {
53 strbuf_realpath(&odb_path_real, odb->path, 1);
54 if (!strcmp(obj_dir_real, odb_path_real.buf))
55 break;
58 free(obj_dir_real);
59 strbuf_release(&odb_path_real);
61 if (!odb)
62 die(_("could not find object directory matching %s"), obj_dir);
63 return odb;
66 static int graph_verify(int argc, const char **argv)
68 struct commit_graph *graph = NULL;
69 struct object_directory *odb = NULL;
70 char *graph_name;
71 int open_ok;
72 int fd;
73 struct stat st;
74 int flags = 0;
76 static struct option builtin_commit_graph_verify_options[] = {
77 OPT_STRING(0, "object-dir", &opts.obj_dir,
78 N_("dir"),
79 N_("The object directory to store the graph")),
80 OPT_BOOL(0, "shallow", &opts.shallow,
81 N_("if the commit-graph is split, only verify the tip file")),
82 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
83 OPT_END(),
86 trace2_cmd_mode("verify");
88 opts.progress = isatty(2);
89 argc = parse_options(argc, argv, NULL,
90 builtin_commit_graph_verify_options,
91 builtin_commit_graph_verify_usage, 0);
93 if (!opts.obj_dir)
94 opts.obj_dir = get_object_directory();
95 if (opts.shallow)
96 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
97 if (opts.progress)
98 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
100 odb = find_odb(the_repository, opts.obj_dir);
101 graph_name = get_commit_graph_filename(odb);
102 open_ok = open_commit_graph(graph_name, &fd, &st);
103 if (!open_ok && errno != ENOENT)
104 die_errno(_("Could not open commit-graph '%s'"), graph_name);
106 FREE_AND_NULL(graph_name);
108 if (open_ok)
109 graph = load_commit_graph_one_fd_st(fd, &st, odb);
110 else
111 graph = read_commit_graph_one(the_repository, odb);
113 /* Return failure if open_ok predicted success */
114 if (!graph)
115 return !!open_ok;
117 UNLEAK(graph);
118 return verify_commit_graph(the_repository, graph, flags);
121 extern int read_replace_refs;
122 static struct split_commit_graph_opts split_opts;
124 static int write_option_parse_split(const struct option *opt, const char *arg,
125 int unset)
127 enum commit_graph_split_flags *flags = opt->value;
129 opts.split = 1;
130 if (!arg)
131 return 0;
133 if (!strcmp(arg, "no-merge"))
134 *flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED;
135 else if (!strcmp(arg, "replace"))
136 *flags = COMMIT_GRAPH_SPLIT_REPLACE;
137 else
138 die(_("unrecognized --split argument, %s"), arg);
140 return 0;
143 static int read_one_commit(struct oidset *commits, struct progress *progress,
144 const char *hash)
146 struct object *result;
147 struct object_id oid;
148 const char *end;
150 if (parse_oid_hex(hash, &oid, &end))
151 return error(_("unexpected non-hex object ID: %s"), hash);
153 result = deref_tag(the_repository, parse_object(the_repository, &oid),
154 NULL, 0);
155 if (!result)
156 return error(_("invalid object: %s"), hash);
157 else if (object_as_type(result, OBJ_COMMIT, 1))
158 oidset_insert(commits, &result->oid);
160 display_progress(progress, oidset_size(commits));
162 return 0;
165 static int graph_write(int argc, const char **argv)
167 struct string_list pack_indexes = STRING_LIST_INIT_NODUP;
168 struct strbuf buf = STRBUF_INIT;
169 struct oidset commits = OIDSET_INIT;
170 struct object_directory *odb = NULL;
171 int result = 0;
172 enum commit_graph_write_flags flags = 0;
173 struct progress *progress = NULL;
175 static struct option builtin_commit_graph_write_options[] = {
176 OPT_STRING(0, "object-dir", &opts.obj_dir,
177 N_("dir"),
178 N_("The object directory to store the graph")),
179 OPT_BOOL(0, "reachable", &opts.reachable,
180 N_("start walk at all refs")),
181 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
182 N_("scan pack-indexes listed by stdin for commits")),
183 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
184 N_("start walk at commits listed by stdin")),
185 OPT_BOOL(0, "append", &opts.append,
186 N_("include all commits already in the commit-graph file")),
187 OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
188 N_("enable computation for changed paths")),
189 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
190 OPT_CALLBACK_F(0, "split", &split_opts.flags, NULL,
191 N_("allow writing an incremental commit-graph file"),
192 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
193 write_option_parse_split),
194 OPT_INTEGER(0, "max-commits", &split_opts.max_commits,
195 N_("maximum number of commits in a non-base split commit-graph")),
196 OPT_INTEGER(0, "size-multiple", &split_opts.size_multiple,
197 N_("maximum ratio between two levels of a split commit-graph")),
198 OPT_EXPIRY_DATE(0, "expire-time", &split_opts.expire_time,
199 N_("only expire files older than a given date-time")),
200 OPT_END(),
203 opts.progress = isatty(2);
204 split_opts.size_multiple = 2;
205 split_opts.max_commits = 0;
206 split_opts.expire_time = 0;
208 trace2_cmd_mode("write");
210 argc = parse_options(argc, argv, NULL,
211 builtin_commit_graph_write_options,
212 builtin_commit_graph_write_usage, 0);
214 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
215 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
216 if (!opts.obj_dir)
217 opts.obj_dir = get_object_directory();
218 if (opts.append)
219 flags |= COMMIT_GRAPH_WRITE_APPEND;
220 if (opts.split)
221 flags |= COMMIT_GRAPH_WRITE_SPLIT;
222 if (opts.progress)
223 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
224 if (opts.enable_changed_paths ||
225 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
226 flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
228 read_replace_refs = 0;
229 odb = find_odb(the_repository, opts.obj_dir);
231 if (opts.reachable) {
232 if (write_commit_graph_reachable(odb, flags, &split_opts))
233 return 1;
234 return 0;
237 if (opts.stdin_packs) {
238 while (strbuf_getline(&buf, stdin) != EOF)
239 string_list_append(&pack_indexes,
240 strbuf_detach(&buf, NULL));
241 } else if (opts.stdin_commits) {
242 oidset_init(&commits, 0);
243 if (opts.progress)
244 progress = start_delayed_progress(
245 _("Collecting commits from input"), 0);
247 while (strbuf_getline(&buf, stdin) != EOF) {
248 if (read_one_commit(&commits, progress, buf.buf)) {
249 result = 1;
250 goto cleanup;
257 if (write_commit_graph(odb,
258 opts.stdin_packs ? &pack_indexes : NULL,
259 opts.stdin_commits ? &commits : NULL,
260 flags,
261 &split_opts))
262 result = 1;
264 cleanup:
265 string_list_clear(&pack_indexes, 0);
266 strbuf_release(&buf);
267 if (progress)
268 stop_progress(&progress);
269 return result;
272 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
274 static struct option builtin_commit_graph_options[] = {
275 OPT_STRING(0, "object-dir", &opts.obj_dir,
276 N_("dir"),
277 N_("The object directory to store the graph")),
278 OPT_END(),
281 if (argc == 2 && !strcmp(argv[1], "-h"))
282 usage_with_options(builtin_commit_graph_usage,
283 builtin_commit_graph_options);
285 git_config(git_default_config, NULL);
286 argc = parse_options(argc, argv, prefix,
287 builtin_commit_graph_options,
288 builtin_commit_graph_usage,
289 PARSE_OPT_STOP_AT_NON_OPTION);
291 save_commit_buffer = 0;
293 if (argc > 0) {
294 if (!strcmp(argv[0], "verify"))
295 return graph_verify(argc, argv);
296 if (!strcmp(argv[0], "write"))
297 return graph_write(argc, argv);
300 usage_with_options(builtin_commit_graph_usage,
301 builtin_commit_graph_options);