]>
Commit | Line | Data |
---|---|---|
4ce58ee3 DS |
1 | #include "builtin.h" |
2 | #include "config.h" | |
f237c8b6 DS |
3 | #include "dir.h" |
4 | #include "lockfile.h" | |
4ce58ee3 | 5 | #include "parse-options.h" |
283e68c7 | 6 | #include "repository.h" |
f237c8b6 | 7 | #include "commit-graph.h" |
3da4b609 | 8 | #include "object-store.h" |
4ce58ee3 DS |
9 | |
10 | static char const * const builtin_commit_graph_usage[] = { | |
73716122 | 11 | N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"), |
4f027355 TB |
12 | N_("git commit-graph write [--object-dir <objdir>] [--append] " |
13 | "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] " | |
dbd5e0a1 | 14 | "[--[no-]progress] <split options>"), |
f237c8b6 DS |
15 | NULL |
16 | }; | |
17 | ||
283e68c7 | 18 | static const char * const builtin_commit_graph_verify_usage[] = { |
73716122 | 19 | N_("git commit-graph verify [--object-dir <objdir>] [--shallow] [--[no-]progress]"), |
283e68c7 DS |
20 | NULL |
21 | }; | |
22 | ||
f237c8b6 | 23 | static const char * const builtin_commit_graph_write_usage[] = { |
4f027355 TB |
24 | N_("git commit-graph write [--object-dir <objdir>] [--append] " |
25 | "[--split[=<strategy>]] [--reachable|--stdin-packs|--stdin-commits] " | |
dbd5e0a1 | 26 | "[--[no-]progress] <split options>"), |
4ce58ee3 DS |
27 | NULL |
28 | }; | |
29 | ||
30 | static struct opts_commit_graph { | |
31 | const char *obj_dir; | |
59fb8770 | 32 | int reachable; |
049d51a2 | 33 | int stdin_packs; |
3d5df01b | 34 | int stdin_commits; |
7547b95b | 35 | int append; |
135a7123 | 36 | int split; |
3da4b609 | 37 | int shallow; |
73716122 | 38 | int progress; |
4ce58ee3 DS |
39 | } opts; |
40 | ||
0bd52e27 TB |
41 | static struct object_directory *find_odb(struct repository *r, |
42 | const char *obj_dir) | |
43 | { | |
44 | struct object_directory *odb; | |
45 | char *obj_dir_real = real_pathdup(obj_dir, 1); | |
3d7747e3 | 46 | struct strbuf odb_path_real = STRBUF_INIT; |
0bd52e27 TB |
47 | |
48 | prepare_alt_odb(r); | |
49 | for (odb = r->objects->odb; odb; odb = odb->next) { | |
3d7747e3 AM |
50 | strbuf_realpath(&odb_path_real, odb->path, 1); |
51 | if (!strcmp(obj_dir_real, odb_path_real.buf)) | |
0bd52e27 TB |
52 | break; |
53 | } | |
54 | ||
55 | free(obj_dir_real); | |
3d7747e3 | 56 | strbuf_release(&odb_path_real); |
0bd52e27 TB |
57 | |
58 | if (!odb) | |
59 | die(_("could not find object directory matching %s"), obj_dir); | |
60 | return odb; | |
61 | } | |
62 | ||
283e68c7 DS |
63 | static int graph_verify(int argc, const char **argv) |
64 | { | |
65 | struct commit_graph *graph = NULL; | |
0bd52e27 | 66 | struct object_directory *odb = NULL; |
283e68c7 | 67 | char *graph_name; |
61df89c8 ÆAB |
68 | int open_ok; |
69 | int fd; | |
70 | struct stat st; | |
3da4b609 | 71 | int flags = 0; |
283e68c7 DS |
72 | |
73 | static struct option builtin_commit_graph_verify_options[] = { | |
74 | OPT_STRING(0, "object-dir", &opts.obj_dir, | |
75 | N_("dir"), | |
76 | N_("The object directory to store the graph")), | |
3da4b609 DS |
77 | OPT_BOOL(0, "shallow", &opts.shallow, |
78 | N_("if the commit-graph is split, only verify the tip file")), | |
73716122 | 79 | OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")), |
283e68c7 DS |
80 | OPT_END(), |
81 | }; | |
82 | ||
0bd7f578 GS |
83 | trace2_cmd_mode("verify"); |
84 | ||
73716122 | 85 | opts.progress = isatty(2); |
283e68c7 DS |
86 | argc = parse_options(argc, argv, NULL, |
87 | builtin_commit_graph_verify_options, | |
88 | builtin_commit_graph_verify_usage, 0); | |
89 | ||
90 | if (!opts.obj_dir) | |
91 | opts.obj_dir = get_object_directory(); | |
3da4b609 DS |
92 | if (opts.shallow) |
93 | flags |= COMMIT_GRAPH_VERIFY_SHALLOW; | |
73716122 GS |
94 | if (opts.progress) |
95 | flags |= COMMIT_GRAPH_WRITE_PROGRESS; | |
283e68c7 | 96 | |
0bd52e27 | 97 | odb = find_odb(the_repository, opts.obj_dir); |
ad2dd5bb | 98 | graph_name = get_commit_graph_filename(odb); |
61df89c8 | 99 | open_ok = open_commit_graph(graph_name, &fd, &st); |
3da4b609 | 100 | if (!open_ok && errno != ENOENT) |
7b8ce9c6 | 101 | die_errno(_("Could not open commit-graph '%s'"), graph_name); |
3da4b609 | 102 | |
283e68c7 DS |
103 | FREE_AND_NULL(graph_name); |
104 | ||
3da4b609 | 105 | if (open_ok) |
a7df60ca | 106 | graph = load_commit_graph_one_fd_st(fd, &st, odb); |
0bd52e27 | 107 | else |
13c24992 | 108 | graph = read_commit_graph_one(the_repository, odb); |
3da4b609 DS |
109 | |
110 | /* Return failure if open_ok predicted success */ | |
283e68c7 | 111 | if (!graph) |
3da4b609 | 112 | return !!open_ok; |
283e68c7 | 113 | |
0bfb48e6 | 114 | UNLEAK(graph); |
3da4b609 | 115 | return verify_commit_graph(the_repository, graph, flags); |
283e68c7 DS |
116 | } |
117 | ||
d6538246 | 118 | extern int read_replace_refs; |
c2bc6e6a | 119 | static struct split_commit_graph_opts split_opts; |
d6538246 | 120 | |
4f027355 TB |
121 | static int write_option_parse_split(const struct option *opt, const char *arg, |
122 | int unset) | |
123 | { | |
fdbde82f TB |
124 | enum commit_graph_split_flags *flags = opt->value; |
125 | ||
4f027355 TB |
126 | opts.split = 1; |
127 | if (!arg) | |
128 | return 0; | |
129 | ||
fdbde82f TB |
130 | if (!strcmp(arg, "no-merge")) |
131 | *flags = COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED; | |
8a6ac287 TB |
132 | else if (!strcmp(arg, "replace")) |
133 | *flags = COMMIT_GRAPH_SPLIT_REPLACE; | |
fdbde82f TB |
134 | else |
135 | die(_("unrecognized --split argument, %s"), arg); | |
4f027355 TB |
136 | |
137 | return 0; | |
138 | } | |
139 | ||
f237c8b6 DS |
140 | static int graph_write(int argc, const char **argv) |
141 | { | |
d88b14b3 | 142 | struct string_list *pack_indexes = NULL; |
6830c360 | 143 | struct oidset commits = OIDSET_INIT; |
0bd52e27 | 144 | struct object_directory *odb = NULL; |
d88b14b3 | 145 | struct string_list lines; |
e103f727 | 146 | int result = 0; |
73716122 | 147 | enum commit_graph_write_flags flags = 0; |
049d51a2 | 148 | |
f237c8b6 DS |
149 | static struct option builtin_commit_graph_write_options[] = { |
150 | OPT_STRING(0, "object-dir", &opts.obj_dir, | |
151 | N_("dir"), | |
152 | N_("The object directory to store the graph")), | |
59fb8770 DS |
153 | OPT_BOOL(0, "reachable", &opts.reachable, |
154 | N_("start walk at all refs")), | |
049d51a2 DS |
155 | OPT_BOOL(0, "stdin-packs", &opts.stdin_packs, |
156 | N_("scan pack-indexes listed by stdin for commits")), | |
3d5df01b DS |
157 | OPT_BOOL(0, "stdin-commits", &opts.stdin_commits, |
158 | N_("start walk at commits listed by stdin")), | |
7547b95b DS |
159 | OPT_BOOL(0, "append", &opts.append, |
160 | N_("include all commits already in the commit-graph file")), | |
73716122 | 161 | OPT_BOOL(0, "progress", &opts.progress, N_("force progress reporting")), |
4f027355 TB |
162 | OPT_CALLBACK_F(0, "split", &split_opts.flags, NULL, |
163 | N_("allow writing an incremental commit-graph file"), | |
164 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, | |
165 | write_option_parse_split), | |
c2bc6e6a DS |
166 | OPT_INTEGER(0, "max-commits", &split_opts.max_commits, |
167 | N_("maximum number of commits in a non-base split commit-graph")), | |
168 | OPT_INTEGER(0, "size-multiple", &split_opts.size_multiple, | |
169 | N_("maximum ratio between two levels of a split commit-graph")), | |
170 | OPT_EXPIRY_DATE(0, "expire-time", &split_opts.expire_time, | |
b09b785c | 171 | N_("only expire files older than a given date-time")), |
f237c8b6 DS |
172 | OPT_END(), |
173 | }; | |
174 | ||
73716122 | 175 | opts.progress = isatty(2); |
c2bc6e6a DS |
176 | split_opts.size_multiple = 2; |
177 | split_opts.max_commits = 0; | |
178 | split_opts.expire_time = 0; | |
179 | ||
0bd7f578 GS |
180 | trace2_cmd_mode("write"); |
181 | ||
f237c8b6 DS |
182 | argc = parse_options(argc, argv, NULL, |
183 | builtin_commit_graph_write_options, | |
184 | builtin_commit_graph_write_usage, 0); | |
185 | ||
59fb8770 DS |
186 | if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1) |
187 | die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs")); | |
f237c8b6 DS |
188 | if (!opts.obj_dir) |
189 | opts.obj_dir = get_object_directory(); | |
5af80394 | 190 | if (opts.append) |
39d88318 | 191 | flags |= COMMIT_GRAPH_WRITE_APPEND; |
135a7123 | 192 | if (opts.split) |
39d88318 | 193 | flags |= COMMIT_GRAPH_WRITE_SPLIT; |
73716122 GS |
194 | if (opts.progress) |
195 | flags |= COMMIT_GRAPH_WRITE_PROGRESS; | |
f237c8b6 | 196 | |
d6538246 | 197 | read_replace_refs = 0; |
0bd52e27 | 198 | odb = find_odb(the_repository, opts.obj_dir); |
d6538246 | 199 | |
c2bc6e6a | 200 | if (opts.reachable) { |
0bd52e27 | 201 | if (write_commit_graph_reachable(odb, flags, &split_opts)) |
c2bc6e6a DS |
202 | return 1; |
203 | return 0; | |
204 | } | |
59fb8770 | 205 | |
d88b14b3 | 206 | string_list_init(&lines, 0); |
3d5df01b | 207 | if (opts.stdin_packs || opts.stdin_commits) { |
049d51a2 | 208 | struct strbuf buf = STRBUF_INIT; |
d88b14b3 DS |
209 | |
210 | while (strbuf_getline(&buf, stdin) != EOF) | |
211 | string_list_append(&lines, strbuf_detach(&buf, NULL)); | |
212 | ||
213 | if (opts.stdin_packs) | |
214 | pack_indexes = &lines; | |
7c5c9b9c | 215 | if (opts.stdin_commits) { |
6830c360 TB |
216 | struct string_list_item *item; |
217 | oidset_init(&commits, lines.nr); | |
218 | for_each_string_list_item(item, &lines) { | |
219 | struct object_id oid; | |
220 | const char *end; | |
221 | ||
222 | if (parse_oid_hex(item->string, &oid, &end)) { | |
223 | error(_("unexpected non-hex object ID: " | |
224 | "%s"), item->string); | |
225 | return 1; | |
226 | } | |
227 | ||
228 | oidset_insert(&commits, &oid); | |
229 | } | |
7c5c9b9c SG |
230 | flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS; |
231 | } | |
0bfb48e6 MÅ |
232 | |
233 | UNLEAK(buf); | |
049d51a2 DS |
234 | } |
235 | ||
0bd52e27 | 236 | if (write_commit_graph(odb, |
e103f727 | 237 | pack_indexes, |
6830c360 | 238 | opts.stdin_commits ? &commits : NULL, |
c2bc6e6a DS |
239 | flags, |
240 | &split_opts)) | |
e103f727 | 241 | result = 1; |
049d51a2 | 242 | |
0bfb48e6 | 243 | UNLEAK(lines); |
e103f727 | 244 | return result; |
f237c8b6 | 245 | } |
4ce58ee3 DS |
246 | |
247 | int cmd_commit_graph(int argc, const char **argv, const char *prefix) | |
248 | { | |
249 | static struct option builtin_commit_graph_options[] = { | |
250 | OPT_STRING(0, "object-dir", &opts.obj_dir, | |
251 | N_("dir"), | |
252 | N_("The object directory to store the graph")), | |
253 | OPT_END(), | |
254 | }; | |
255 | ||
256 | if (argc == 2 && !strcmp(argv[1], "-h")) | |
257 | usage_with_options(builtin_commit_graph_usage, | |
258 | builtin_commit_graph_options); | |
259 | ||
260 | git_config(git_default_config, NULL); | |
261 | argc = parse_options(argc, argv, prefix, | |
262 | builtin_commit_graph_options, | |
263 | builtin_commit_graph_usage, | |
264 | PARSE_OPT_STOP_AT_NON_OPTION); | |
265 | ||
dd2e50a8 JK |
266 | save_commit_buffer = 0; |
267 | ||
f237c8b6 | 268 | if (argc > 0) { |
283e68c7 DS |
269 | if (!strcmp(argv[0], "verify")) |
270 | return graph_verify(argc, argv); | |
f237c8b6 DS |
271 | if (!strcmp(argv[0], "write")) |
272 | return graph_write(argc, argv); | |
273 | } | |
274 | ||
4ce58ee3 DS |
275 | usage_with_options(builtin_commit_graph_usage, |
276 | builtin_commit_graph_options); | |
277 | } |