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] [--[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] [--[no-]progress] <split options>"),
26 static struct opts_commit_graph {
37 static struct object_directory *find_odb(struct repository *r,
40 struct object_directory *odb;
41 char *obj_dir_real = real_pathdup(obj_dir, 1);
44 for (odb = r->objects->odb; odb; odb = odb->next) {
45 if (!strcmp(obj_dir_real, real_path(odb->path)))
52 die(_("could not find object directory matching %s"), obj_dir);
56 static int graph_verify(int argc, const char **argv)
58 struct commit_graph *graph = NULL;
59 struct object_directory *odb = NULL;
66 static struct option builtin_commit_graph_verify_options[] = {
67 OPT_STRING(0, "object-dir", &opts.obj_dir,
69 N_("The object directory to store the graph")),
70 OPT_BOOL(0, "shallow", &opts.shallow,
71 N_("if the commit-graph is split, only verify the tip file")),
72 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
76 trace2_cmd_mode("verify");
78 opts.progress = isatty(2);
79 argc = parse_options(argc, argv, NULL,
80 builtin_commit_graph_verify_options,
81 builtin_commit_graph_verify_usage, 0);
84 opts.obj_dir = get_object_directory();
86 flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
88 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
90 odb = find_odb(the_repository, opts.obj_dir);
91 graph_name = get_commit_graph_filename(odb);
92 open_ok = open_commit_graph(graph_name, &fd, &st);
93 if (!open_ok && errno != ENOENT)
94 die_errno(_("Could not open commit-graph '%s'"), graph_name);
96 FREE_AND_NULL(graph_name);
99 graph = load_commit_graph_one_fd_st(fd, &st, odb);
101 graph = read_commit_graph_one(the_repository, odb);
103 /* Return failure if open_ok predicted success */
108 return verify_commit_graph(the_repository, graph, flags);
111 extern int read_replace_refs;
112 static struct split_commit_graph_opts split_opts;
114 static int graph_write(int argc, const char **argv)
116 struct string_list *pack_indexes = NULL;
117 struct string_list *commit_hex = NULL;
118 struct object_directory *odb = NULL;
119 struct string_list lines;
121 enum commit_graph_write_flags flags = 0;
123 static struct option builtin_commit_graph_write_options[] = {
124 OPT_STRING(0, "object-dir", &opts.obj_dir,
126 N_("The object directory to store the graph")),
127 OPT_BOOL(0, "reachable", &opts.reachable,
128 N_("start walk at all refs")),
129 OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
130 N_("scan pack-indexes listed by stdin for commits")),
131 OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
132 N_("start walk at commits listed by stdin")),
133 OPT_BOOL(0, "append", &opts.append,
134 N_("include all commits already in the commit-graph file")),
135 OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")),
136 OPT_BOOL(0, "split", &opts.split,
137 N_("allow writing an incremental commit-graph file")),
138 OPT_INTEGER(0, "max-commits", &split_opts.max_commits,
139 N_("maximum number of commits in a non-base split commit-graph")),
140 OPT_INTEGER(0, "size-multiple", &split_opts.size_multiple,
141 N_("maximum ratio between two levels of a split commit-graph")),
142 OPT_EXPIRY_DATE(0, "expire-time", &split_opts.expire_time,
143 N_("maximum number of commits in a non-base split commit-graph")),
147 opts.progress = isatty(2);
148 split_opts.size_multiple = 2;
149 split_opts.max_commits = 0;
150 split_opts.expire_time = 0;
152 trace2_cmd_mode("write");
154 argc = parse_options(argc, argv, NULL,
155 builtin_commit_graph_write_options,
156 builtin_commit_graph_write_usage, 0);
158 if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
159 die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
161 opts.obj_dir = get_object_directory();
163 flags |= COMMIT_GRAPH_WRITE_APPEND;
165 flags |= COMMIT_GRAPH_WRITE_SPLIT;
167 flags |= COMMIT_GRAPH_WRITE_PROGRESS;
169 read_replace_refs = 0;
170 odb = find_odb(the_repository, opts.obj_dir);
172 if (opts.reachable) {
173 if (write_commit_graph_reachable(odb, flags, &split_opts))
178 string_list_init(&lines, 0);
179 if (opts.stdin_packs || opts.stdin_commits) {
180 struct strbuf buf = STRBUF_INIT;
182 while (strbuf_getline(&buf, stdin) != EOF)
183 string_list_append(&lines, strbuf_detach(&buf, NULL));
185 if (opts.stdin_packs)
186 pack_indexes = &lines;
187 if (opts.stdin_commits) {
189 flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS;
195 if (write_commit_graph(odb,
206 int cmd_commit_graph(int argc, const char **argv, const char *prefix)
208 static struct option builtin_commit_graph_options[] = {
209 OPT_STRING(0, "object-dir", &opts.obj_dir,
211 N_("The object directory to store the graph")),
215 if (argc == 2 && !strcmp(argv[1], "-h"))
216 usage_with_options(builtin_commit_graph_usage,
217 builtin_commit_graph_options);
219 git_config(git_default_config, NULL);
220 argc = parse_options(argc, argv, prefix,
221 builtin_commit_graph_options,
222 builtin_commit_graph_usage,
223 PARSE_OPT_STOP_AT_NON_OPTION);
225 save_commit_buffer = 0;
228 if (!strcmp(argv[0], "verify"))
229 return graph_verify(argc, argv);
230 if (!strcmp(argv[0], "write"))
231 return graph_write(argc, argv);
234 usage_with_options(builtin_commit_graph_usage,
235 builtin_commit_graph_options);