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|--split] [--reachable|--stdin-packs|--stdin-commits] [--changed-paths] [--[no-]progress] <split options>"),
16 static const char * const builtin_commit_graph_verify_usage[] = {
17 N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"),
21 static const char * const builtin_commit_graph_write_usage[] = {
22 N_("git commit-graph write [--object-dir <objdir>] [--append|--split] [--reachable|--stdin-packs|--stdin-commits] [--changed-paths] [--[no-]progress] <split options>"),
26 static struct opts_commit_graph {
35 int enable_changed_paths;
38 static struct object_directory *find_odb(struct repository *r,
41 struct object_directory *odb;
42 char *obj_dir_real = real_pathdup(obj_dir, 1);
43 struct strbuf odb_path_real = STRBUF_INIT;
46 for (odb = r->objects->odb; odb; odb = odb->next) {
47 strbuf_realpath(&odb_path_real, odb->path, 1);
48 if (!strcmp(obj_dir_real, odb_path_real.buf))
53 strbuf_release(&odb_path_real);
56 die(_("could not find object directory matching %s"), obj_dir);
60 static int graph_verify(int argc, const char **argv)
62 struct commit_graph *graph = NULL;
63 struct object_directory *odb = NULL;
70 static struct option builtin_commit_graph_verify_options[] = {
71 OPT_STRING(0, "object-dir", &opts.obj_dir,
73 N_("The object directory to store the graph")),
74 OPT_BOOL(0, "shallow", &opts.shallow,
75 N_("if the commit-graph is split, only verify the tip file")),
76 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
80 trace2_cmd_mode("verify");
82 opts.progress = isatty(2);
83 argc = parse_options(argc, argv, NULL,
84 builtin_commit_graph_verify_options,
85 builtin_commit_graph_verify_usage, 0);
88 opts.obj_dir = get_object_directory();
90 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
92 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
94 odb = find_odb(the_repository, opts.obj_dir);
95 graph_name = get_commit_graph_filename(odb);
96 open_ok = open_commit_graph(graph_name, &fd, &st);
97 if (!open_ok && errno != ENOENT)
98 die_errno(_("Could not open commit-graph '%s'"), graph_name);
100 FREE_AND_NULL(graph_name);
103 graph = load_commit_graph_one_fd_st(fd, &st, odb);
105 graph = read_commit_graph_one(the_repository, odb);
107 /* Return failure if open_ok predicted success */
112 return verify_commit_graph(the_repository, graph, flags);
115 extern int read_replace_refs;
116 static struct split_commit_graph_opts split_opts;
118 static int graph_write(int argc, const char **argv)
120 struct string_list *pack_indexes = NULL;
121 struct string_list *commit_hex = NULL;
122 struct object_directory *odb = NULL;
123 struct string_list lines;
125 enum commit_graph_write_flags flags = 0;
127 static struct option builtin_commit_graph_write_options[] = {
128 OPT_STRING(0, "object-dir", &opts.obj_dir,
130 N_("The object directory to store the graph")),
131 OPT_BOOL(0, "reachable", &opts.reachable,
132 N_("start walk at all refs")),
133 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
134 N_("scan pack-indexes listed by stdin for commits")),
135 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
136 N_("start walk at commits listed by stdin")),
137 OPT_BOOL(0, "append", &opts.append,
138 N_("include all commits already in the commit-graph file")),
139 OPT_BOOL(0, "changed-paths", &opts.enable_changed_paths,
140 N_("enable computation for changed paths")),
141 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
142 OPT_BOOL(0, "split", &opts.split,
143 N_("allow writing an incremental commit-graph file")),
144 OPT_INTEGER(0, "max-commits", &split_opts.max_commits,
145 N_("maximum number of commits in a non-base split commit-graph")),
146 OPT_INTEGER(0, "size-multiple", &split_opts.size_multiple,
147 N_("maximum ratio between two levels of a split commit-graph")),
148 OPT_EXPIRY_DATE(0, "expire-time", &split_opts.expire_time,
149 N_("maximum number of commits in a non-base split commit-graph")),
153 opts.progress = isatty(2);
154 opts.enable_changed_paths = -1;
155 split_opts.size_multiple = 2;
156 split_opts.max_commits = 0;
157 split_opts.expire_time = 0;
159 trace2_cmd_mode("write");
161 argc = parse_options(argc, argv, NULL,
162 builtin_commit_graph_write_options,
163 builtin_commit_graph_write_usage, 0);
165 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
166 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
168 opts.obj_dir = get_object_directory();
170 flags |= COMMIT_GRAPH_WRITE_APPEND;
172 flags |= COMMIT_GRAPH_WRITE_SPLIT;
174 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
175 if (!opts.enable_changed_paths)
176 flags |= COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS;
177 if (opts.enable_changed_paths == 1 ||
178 git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
179 flags |= COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
181 read_replace_refs = 0;
182 odb = find_odb(the_repository, opts.obj_dir);
184 if (opts.reachable) {
185 if (write_commit_graph_reachable(odb, flags, &split_opts))
190 string_list_init(&lines, 0);
191 if (opts.stdin_packs || opts.stdin_commits) {
192 struct strbuf buf = STRBUF_INIT;
194 while (strbuf_getline(&buf, stdin) != EOF)
195 string_list_append(&lines, strbuf_detach(&buf, NULL));
197 if (opts.stdin_packs)
198 pack_indexes = &lines;
199 if (opts.stdin_commits) {
201 flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS;
207 if (write_commit_graph(odb,
218 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
220 static struct option builtin_commit_graph_options[] = {
221 OPT_STRING(0, "object-dir", &opts.obj_dir,
223 N_("The object directory to store the graph")),
227 if (argc == 2 && !strcmp(argv[1], "-h"))
228 usage_with_options(builtin_commit_graph_usage,
229 builtin_commit_graph_options);
231 git_config(git_default_config, NULL);
232 argc = parse_options(argc, argv, prefix,
233 builtin_commit_graph_options,
234 builtin_commit_graph_usage,
235 PARSE_OPT_STOP_AT_NON_OPTION);
237 save_commit_buffer = 0;
240 if (!strcmp(argv[0], "verify"))
241 return graph_verify(argc, argv);
242 if (!strcmp(argv[0], "write"))
243 return graph_write(argc, argv);
246 usage_with_options(builtin_commit_graph_usage,
247 builtin_commit_graph_options);