Merge branch 'es/trace-log-progress'
[git.git] / builtin / commit-graph.c
blob15fe60317c7846df93d9975dc991ba3a67bd0758
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"
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 "[--changed-paths] [--[no-]progress] <split options>"),
15 NULL
18 static const char * const builtin_commit_graph_verify_usage[] = {
19 N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
20 NULL
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 "[--changed-paths] [--[no-]progress] <split options>"),
27 NULL
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 int enable_changed_paths;
40 } opts;
42 static struct object_directory *find_odb(struct repository *r,
43 const char *obj_dir)
45 struct object_directory *odb;
46 char *obj_dir_real = real_pathdup(obj_dir, 1);
47 struct strbuf odb_path_real = STRBUF_INIT;
49 prepare_alt_odb(r);
50 for (odb = r->objects->odb; odb; odb = odb->next) {
51 strbuf_realpath(&odb_path_real, odb->path, 1);
52 if (!strcmp(obj_dir_real, odb_path_real.buf))
53 break;
56 free(obj_dir_real);
57 strbuf_release(&odb_path_real);
59 if (!odb)
60 die(_("could not find object directory matching %s"), obj_dir);
61 return odb;
64 static int graph_verify(int argc, const char **argv)
66 struct commit_graph *graph = NULL;
67 struct object_directory *odb = NULL;
68 char *graph_name;
69 int open_ok;
70 int fd;
71 struct stat st;
72 int flags = 0;
74 static struct option builtin_commit_graph_verify_options[] = {
75 OPT_STRING(0, "object-dir", &opts.obj_dir,
76 N_("dir"),
77 N_("The object directory to store the graph")),
78 OPT_BOOL(0, "shallow", &opts.shallow,
79 N_("if the commit-graph is split, only verify the tip file")),
80 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
81 OPT_END(),
84 trace2_cmd_mode("verify");
86 opts.progress = isatty(2);
87 argc = parse_options(argc, argv, NULL,
88 builtin_commit_graph_verify_options,
89 builtin_commit_graph_verify_usage, 0);
91 if (!opts.obj_dir)
92 opts.obj_dir = get_object_directory();
93 if (opts.shallow)
94 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
95 if (opts.progress)
96 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
98 odb = find_odb(the_repository, opts.obj_dir);
99 graph_name = get_commit_graph_filename(odb);
100 open_ok = open_commit_graph(graph_name, &fd, &st);
101 if (!open_ok && errno != ENOENT)
102 die_errno(_("Could not open commit-graph '%s'"), graph_name);
104 FREE_AND_NULL(graph_name);
106 if (open_ok)
107 graph = load_commit_graph_one_fd_st(fd, &st, odb);
108 else
109 graph = read_commit_graph_one(the_repository, odb);
111 /* Return failure if open_ok predicted success */
112 if (!graph)
113 return !!open_ok;
115 UNLEAK(graph);
116 return verify_commit_graph(the_repository, graph, flags);
119 extern int read_replace_refs;
120 static struct split_commit_graph_opts split_opts;
122 static int write_option_parse_split(const struct option *opt, const char *arg,
123 int unset)
125 enum commit_graph_split_flags *flags = opt->value;
127 opts.split = 1;
128 if (!arg)
129 return 0;
131 if (!strcmp(arg, "no-merge"))
132 *flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED;
133 else if (!strcmp(arg, "replace"))
134 *flags = COMMIT_GRAPH_SPLIT_REPLACE;
135 else
136 die(_("unrecognized --split argument, %s"), arg);
138 return 0;
141 static int graph_write(int argc, const char **argv)
143 struct string_list *pack_indexes = NULL;
144 struct oidset commits = OIDSET_INIT;
145 struct object_directory *odb = NULL;
146 struct string_list lines;
147 int result = 0;
148 enum commit_graph_write_flags flags = 0;
150 static struct option builtin_commit_graph_write_options[] = {
151 OPT_STRING(0, "object-dir", &opts.obj_dir,
152 N_("dir"),
153 N_("The object directory to store the graph")),
154 OPT_BOOL(0, "reachable", &opts.reachable,
155 N_("start walk at all refs")),
156 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
157 N_("scan pack-indexes listed by stdin for commits")),
158 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
159 N_("start walk at commits listed by stdin")),
160 OPT_BOOL(0, "append", &opts.append,
161 N_("include all commits already in the commit-graph file")),
162 OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
163 N_("enable computation for changed paths")),
164 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
165 OPT_CALLBACK_F(0, "split", &split_opts.flags, NULL,
166 N_("allow writing an incremental commit-graph file"),
167 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
168 write_option_parse_split),
169 OPT_INTEGER(0, "max-commits", &split_opts.max_commits,
170 N_("maximum number of commits in a non-base split commit-graph")),
171 OPT_INTEGER(0, "size-multiple", &split_opts.size_multiple,
172 N_("maximum ratio between two levels of a split commit-graph")),
173 OPT_EXPIRY_DATE(0, "expire-time", &split_opts.expire_time,
174 N_("only expire files older than a given date-time")),
175 OPT_END(),
178 opts.progress = isatty(2);
179 split_opts.size_multiple = 2;
180 split_opts.max_commits = 0;
181 split_opts.expire_time = 0;
183 trace2_cmd_mode("write");
185 argc = parse_options(argc, argv, NULL,
186 builtin_commit_graph_write_options,
187 builtin_commit_graph_write_usage, 0);
189 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
190 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
191 if (!opts.obj_dir)
192 opts.obj_dir = get_object_directory();
193 if (opts.append)
194 flags |= COMMIT_GRAPH_WRITE_APPEND;
195 if (opts.split)
196 flags |= COMMIT_GRAPH_WRITE_SPLIT;
197 if (opts.progress)
198 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
199 if (opts.enable_changed_paths ||
200 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
201 flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
203 read_replace_refs = 0;
204 odb = find_odb(the_repository, opts.obj_dir);
206 if (opts.reachable) {
207 if (write_commit_graph_reachable(odb, flags, &split_opts))
208 return 1;
209 return 0;
212 string_list_init(&lines, 0);
213 if (opts.stdin_packs || opts.stdin_commits) {
214 struct strbuf buf = STRBUF_INIT;
216 while (strbuf_getline(&buf, stdin) != EOF)
217 string_list_append(&lines, strbuf_detach(&buf, NULL));
219 if (opts.stdin_packs)
220 pack_indexes = &lines;
221 if (opts.stdin_commits) {
222 struct string_list_item *item;
223 oidset_init(&commits, lines.nr);
224 for_each_string_list_item(item, &lines) {
225 struct object_id oid;
226 const char *end;
228 if (parse_oid_hex(item->string, &oid, &end)) {
229 error(_("unexpected non-hex object ID: "
230 "%s"), item->string);
231 return 1;
234 oidset_insert(&commits, &oid);
236 flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS;
239 UNLEAK(buf);
242 if (write_commit_graph(odb,
243 pack_indexes,
244 opts.stdin_commits ? &commits : NULL,
245 flags,
246 &split_opts))
247 result = 1;
249 UNLEAK(lines);
250 return result;
253 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
255 static struct option builtin_commit_graph_options[] = {
256 OPT_STRING(0, "object-dir", &opts.obj_dir,
257 N_("dir"),
258 N_("The object directory to store the graph")),
259 OPT_END(),
262 if (argc == 2 && !strcmp(argv[1], "-h"))
263 usage_with_options(builtin_commit_graph_usage,
264 builtin_commit_graph_options);
266 git_config(git_default_config, NULL);
267 argc = parse_options(argc, argv, prefix,
268 builtin_commit_graph_options,
269 builtin_commit_graph_usage,
270 PARSE_OPT_STOP_AT_NON_OPTION);
272 save_commit_buffer = 0;
274 if (argc > 0) {
275 if (!strcmp(argv[0], "verify"))
276 return graph_verify(argc, argv);
277 if (!strcmp(argv[0], "write"))
278 return graph_write(argc, argv);
281 usage_with_options(builtin_commit_graph_usage,
282 builtin_commit_graph_options);