]>
Commit | Line | Data |
---|---|---|
08fd81c9 DS |
1 | #include "cache.h" |
2 | #include "config.h" | |
33286dcd | 3 | #include "dir.h" |
08fd81c9 DS |
4 | #include "git-compat-util.h" |
5 | #include "lockfile.h" | |
6 | #include "pack.h" | |
7 | #include "packfile.h" | |
8 | #include "commit.h" | |
9 | #include "object.h" | |
59fb8770 | 10 | #include "refs.h" |
08fd81c9 DS |
11 | #include "revision.h" |
12 | #include "sha1-lookup.h" | |
13 | #include "commit-graph.h" | |
b10edb2d | 14 | #include "object-store.h" |
96af91d4 | 15 | #include "alloc.h" |
d6538246 DS |
16 | #include "hashmap.h" |
17 | #include "replace-object.h" | |
7b0f2292 | 18 | #include "progress.h" |
08fd81c9 DS |
19 | |
20 | #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */ | |
21 | #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */ | |
22 | #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */ | |
23 | #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */ | |
5af7417b | 24 | #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */ |
118bd570 | 25 | #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */ |
08fd81c9 | 26 | |
c1665998 | 27 | #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16) |
08fd81c9 DS |
28 | |
29 | #define GRAPH_VERSION_1 0x1 | |
30 | #define GRAPH_VERSION GRAPH_VERSION_1 | |
31 | ||
5af7417b | 32 | #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000 |
08fd81c9 DS |
33 | #define GRAPH_EDGE_LAST_MASK 0x7fffffff |
34 | #define GRAPH_PARENT_NONE 0x70000000 | |
35 | ||
36 | #define GRAPH_LAST_EDGE 0x80000000 | |
37 | ||
0e3b97cc | 38 | #define GRAPH_HEADER_SIZE 8 |
08fd81c9 DS |
39 | #define GRAPH_FANOUT_SIZE (4 * 256) |
40 | #define GRAPH_CHUNKLOOKUP_WIDTH 12 | |
0e3b97cc | 41 | #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \ |
c1665998 | 42 | + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) |
08fd81c9 | 43 | |
cb99a34e DS |
44 | /* Remember to update object flag allocation in object.h */ |
45 | #define REACHABLE (1u<<15) | |
46 | ||
ad2dd5bb | 47 | char *get_commit_graph_filename(struct object_directory *odb) |
08fd81c9 | 48 | { |
ad2dd5bb | 49 | return xstrfmt("%s/info/commit-graph", odb->path); |
08fd81c9 DS |
50 | } |
51 | ||
ad2dd5bb | 52 | static char *get_split_graph_filename(struct object_directory *odb, |
5c84b339 DS |
53 | const char *oid_hex) |
54 | { | |
ad2dd5bb TB |
55 | return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path, |
56 | oid_hex); | |
5c84b339 DS |
57 | } |
58 | ||
ad2dd5bb | 59 | static char *get_chain_filename(struct object_directory *odb) |
5c84b339 | 60 | { |
ad2dd5bb | 61 | return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path); |
08fd81c9 DS |
62 | } |
63 | ||
c1665998 | 64 | static uint8_t oid_version(void) |
65 | { | |
66 | return 1; | |
67 | } | |
68 | ||
2a2e32bd DS |
69 | static struct commit_graph *alloc_commit_graph(void) |
70 | { | |
71 | struct commit_graph *g = xcalloc(1, sizeof(*g)); | |
2a2e32bd DS |
72 | |
73 | return g; | |
74 | } | |
75 | ||
d6538246 DS |
76 | extern int read_replace_refs; |
77 | ||
78 | static int commit_graph_compatible(struct repository *r) | |
79 | { | |
5cef295f DS |
80 | if (!r->gitdir) |
81 | return 0; | |
82 | ||
d6538246 DS |
83 | if (read_replace_refs) { |
84 | prepare_replace_object(r); | |
85 | if (hashmap_get_size(&r->objects->replace_map->map)) | |
86 | return 0; | |
87 | } | |
88 | ||
20fd6d57 DS |
89 | prepare_commit_graft(r); |
90 | if (r->parsed_objects && r->parsed_objects->grafts_nr) | |
91 | return 0; | |
92 | if (is_repository_shallow(r)) | |
93 | return 0; | |
94 | ||
d6538246 DS |
95 | return 1; |
96 | } | |
97 | ||
61df89c8 ÆAB |
98 | int open_commit_graph(const char *graph_file, int *fd, struct stat *st) |
99 | { | |
100 | *fd = git_open(graph_file); | |
101 | if (*fd < 0) | |
102 | return 0; | |
103 | if (fstat(*fd, st)) { | |
104 | close(*fd); | |
105 | return 0; | |
106 | } | |
107 | return 1; | |
108 | } | |
109 | ||
a7df60ca TB |
110 | struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st, |
111 | struct object_directory *odb) | |
2a2e32bd DS |
112 | { |
113 | void *graph_map; | |
2a2e32bd | 114 | size_t graph_size; |
aa658574 | 115 | struct commit_graph *ret; |
2a2e32bd | 116 | |
61df89c8 | 117 | graph_size = xsize_t(st->st_size); |
2a2e32bd DS |
118 | |
119 | if (graph_size < GRAPH_MIN_SIZE) { | |
120 | close(fd); | |
67a530fa | 121 | error(_("commit-graph file is too small")); |
61df89c8 | 122 | return NULL; |
2a2e32bd DS |
123 | } |
124 | graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0); | |
c8828530 JK |
125 | close(fd); |
126 | ret = parse_commit_graph(graph_map, graph_size); | |
aa658574 | 127 | |
a7df60ca TB |
128 | if (ret) |
129 | ret->odb = odb; | |
c8828530 | 130 | else |
aa658574 | 131 | munmap(graph_map, graph_size); |
aa658574 JS |
132 | |
133 | return ret; | |
134 | } | |
135 | ||
2ac138d5 ÆAB |
136 | static int verify_commit_graph_lite(struct commit_graph *g) |
137 | { | |
138 | /* | |
139 | * Basic validation shared between parse_commit_graph() | |
140 | * which'll be called every time the graph is used, and the | |
141 | * much more expensive verify_commit_graph() used by | |
142 | * "commit-graph verify". | |
143 | * | |
144 | * There should only be very basic checks here to ensure that | |
145 | * we don't e.g. segfault in fill_commit_in_graph(), but | |
7a9ce026 | 146 | e because this is a very hot codepath nothing that e.g. loops |
2ac138d5 ÆAB |
147 | * over g->num_commits, or runs a checksum on the commit-graph |
148 | * itself. | |
149 | */ | |
150 | if (!g->chunk_oid_fanout) { | |
151 | error("commit-graph is missing the OID Fanout chunk"); | |
152 | return 1; | |
153 | } | |
154 | if (!g->chunk_oid_lookup) { | |
155 | error("commit-graph is missing the OID Lookup chunk"); | |
156 | return 1; | |
157 | } | |
158 | if (!g->chunk_commit_data) { | |
159 | error("commit-graph is missing the Commit Data chunk"); | |
160 | return 1; | |
161 | } | |
162 | ||
163 | return 0; | |
164 | } | |
165 | ||
c8828530 | 166 | struct commit_graph *parse_commit_graph(void *graph_map, size_t graph_size) |
aa658574 JS |
167 | { |
168 | const unsigned char *data, *chunk_lookup; | |
169 | uint32_t i; | |
170 | struct commit_graph *graph; | |
171 | uint64_t last_chunk_offset; | |
172 | uint32_t last_chunk_id; | |
173 | uint32_t graph_signature; | |
174 | unsigned char graph_version, hash_version; | |
175 | ||
176 | if (!graph_map) | |
177 | return NULL; | |
178 | ||
179 | if (graph_size < GRAPH_MIN_SIZE) | |
180 | return NULL; | |
181 | ||
2a2e32bd DS |
182 | data = (const unsigned char *)graph_map; |
183 | ||
184 | graph_signature = get_be32(data); | |
185 | if (graph_signature != GRAPH_SIGNATURE) { | |
93b4405f | 186 | error(_("commit-graph signature %X does not match signature %X"), |
2a2e32bd | 187 | graph_signature, GRAPH_SIGNATURE); |
aa658574 | 188 | return NULL; |
2a2e32bd DS |
189 | } |
190 | ||
191 | graph_version = *(unsigned char*)(data + 4); | |
192 | if (graph_version != GRAPH_VERSION) { | |
93b4405f | 193 | error(_("commit-graph version %X does not match version %X"), |
2a2e32bd | 194 | graph_version, GRAPH_VERSION); |
aa658574 | 195 | return NULL; |
2a2e32bd DS |
196 | } |
197 | ||
198 | hash_version = *(unsigned char*)(data + 5); | |
c1665998 | 199 | if (hash_version != oid_version()) { |
93b4405f | 200 | error(_("commit-graph hash version %X does not match version %X"), |
c1665998 | 201 | hash_version, oid_version()); |
aa658574 | 202 | return NULL; |
2a2e32bd DS |
203 | } |
204 | ||
205 | graph = alloc_commit_graph(); | |
206 | ||
c1665998 | 207 | graph->hash_len = the_hash_algo->rawsz; |
2a2e32bd | 208 | graph->num_chunks = *(unsigned char*)(data + 6); |
2a2e32bd DS |
209 | graph->data = graph_map; |
210 | graph->data_len = graph_size; | |
211 | ||
212 | last_chunk_id = 0; | |
213 | last_chunk_offset = 8; | |
214 | chunk_lookup = data + 8; | |
215 | for (i = 0; i < graph->num_chunks; i++) { | |
d2b86fba JS |
216 | uint32_t chunk_id; |
217 | uint64_t chunk_offset; | |
2a2e32bd DS |
218 | int chunk_repeated = 0; |
219 | ||
d2b86fba JS |
220 | if (data + graph_size - chunk_lookup < |
221 | GRAPH_CHUNKLOOKUP_WIDTH) { | |
93b4405f | 222 | error(_("commit-graph chunk lookup table entry missing; file may be incomplete")); |
d2b86fba JS |
223 | free(graph); |
224 | return NULL; | |
225 | } | |
226 | ||
227 | chunk_id = get_be32(chunk_lookup + 0); | |
228 | chunk_offset = get_be64(chunk_lookup + 4); | |
229 | ||
2a2e32bd DS |
230 | chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH; |
231 | ||
c1665998 | 232 | if (chunk_offset > graph_size - the_hash_algo->rawsz) { |
93b4405f | 233 | error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32), |
2a2e32bd | 234 | (uint32_t)chunk_offset); |
aa658574 JS |
235 | free(graph); |
236 | return NULL; | |
2a2e32bd DS |
237 | } |
238 | ||
239 | switch (chunk_id) { | |
240 | case GRAPH_CHUNKID_OIDFANOUT: | |
241 | if (graph->chunk_oid_fanout) | |
242 | chunk_repeated = 1; | |
243 | else | |
244 | graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset); | |
245 | break; | |
246 | ||
247 | case GRAPH_CHUNKID_OIDLOOKUP: | |
248 | if (graph->chunk_oid_lookup) | |
249 | chunk_repeated = 1; | |
250 | else | |
251 | graph->chunk_oid_lookup = data + chunk_offset; | |
252 | break; | |
253 | ||
254 | case GRAPH_CHUNKID_DATA: | |
255 | if (graph->chunk_commit_data) | |
256 | chunk_repeated = 1; | |
257 | else | |
258 | graph->chunk_commit_data = data + chunk_offset; | |
259 | break; | |
260 | ||
5af7417b SG |
261 | case GRAPH_CHUNKID_EXTRAEDGES: |
262 | if (graph->chunk_extra_edges) | |
2a2e32bd DS |
263 | chunk_repeated = 1; |
264 | else | |
5af7417b | 265 | graph->chunk_extra_edges = data + chunk_offset; |
2a2e32bd | 266 | break; |
118bd570 DS |
267 | |
268 | case GRAPH_CHUNKID_BASE: | |
269 | if (graph->chunk_base_graphs) | |
270 | chunk_repeated = 1; | |
271 | else | |
272 | graph->chunk_base_graphs = data + chunk_offset; | |
2a2e32bd DS |
273 | } |
274 | ||
275 | if (chunk_repeated) { | |
93b4405f | 276 | error(_("commit-graph chunk id %08x appears multiple times"), chunk_id); |
aa658574 JS |
277 | free(graph); |
278 | return NULL; | |
2a2e32bd DS |
279 | } |
280 | ||
281 | if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP) | |
282 | { | |
283 | graph->num_commits = (chunk_offset - last_chunk_offset) | |
284 | / graph->hash_len; | |
285 | } | |
286 | ||
287 | last_chunk_id = chunk_id; | |
288 | last_chunk_offset = chunk_offset; | |
289 | } | |
290 | ||
118bd570 DS |
291 | hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len); |
292 | ||
98552f25 JS |
293 | if (verify_commit_graph_lite(graph)) { |
294 | free(graph); | |
2ac138d5 | 295 | return NULL; |
98552f25 | 296 | } |
2ac138d5 | 297 | |
2a2e32bd | 298 | return graph; |
2a2e32bd DS |
299 | } |
300 | ||
a7df60ca TB |
301 | static struct commit_graph *load_commit_graph_one(const char *graph_file, |
302 | struct object_directory *odb) | |
61df89c8 ÆAB |
303 | { |
304 | ||
305 | struct stat st; | |
306 | int fd; | |
6c622f9f | 307 | struct commit_graph *g; |
61df89c8 ÆAB |
308 | int open_ok = open_commit_graph(graph_file, &fd, &st); |
309 | ||
310 | if (!open_ok) | |
311 | return NULL; | |
312 | ||
a7df60ca | 313 | g = load_commit_graph_one_fd_st(fd, &st, odb); |
6c622f9f DS |
314 | |
315 | if (g) | |
316 | g->filename = xstrdup(graph_file); | |
317 | ||
318 | return g; | |
61df89c8 ÆAB |
319 | } |
320 | ||
13c24992 TB |
321 | static struct commit_graph *load_commit_graph_v1(struct repository *r, |
322 | struct object_directory *odb) | |
5c84b339 | 323 | { |
ad2dd5bb | 324 | char *graph_name = get_commit_graph_filename(odb); |
a7df60ca | 325 | struct commit_graph *g = load_commit_graph_one(graph_name, odb); |
5c84b339 DS |
326 | free(graph_name); |
327 | ||
328 | return g; | |
329 | } | |
330 | ||
331 | static int add_graph_to_chain(struct commit_graph *g, | |
332 | struct commit_graph *chain, | |
333 | struct object_id *oids, | |
334 | int n) | |
335 | { | |
336 | struct commit_graph *cur_g = chain; | |
337 | ||
118bd570 DS |
338 | if (n && !g->chunk_base_graphs) { |
339 | warning(_("commit-graph has no base graphs chunk")); | |
340 | return 0; | |
341 | } | |
342 | ||
5c84b339 DS |
343 | while (n) { |
344 | n--; | |
118bd570 DS |
345 | |
346 | if (!cur_g || | |
347 | !oideq(&oids[n], &cur_g->oid) || | |
348 | !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) { | |
349 | warning(_("commit-graph chain does not match")); | |
350 | return 0; | |
351 | } | |
352 | ||
5c84b339 DS |
353 | cur_g = cur_g->base_graph; |
354 | } | |
355 | ||
356 | g->base_graph = chain; | |
357 | ||
358 | if (chain) | |
359 | g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base; | |
360 | ||
361 | return 1; | |
362 | } | |
363 | ||
13c24992 TB |
364 | static struct commit_graph *load_commit_graph_chain(struct repository *r, |
365 | struct object_directory *odb) | |
5c84b339 DS |
366 | { |
367 | struct commit_graph *graph_chain = NULL; | |
368 | struct strbuf line = STRBUF_INIT; | |
369 | struct stat st; | |
370 | struct object_id *oids; | |
371 | int i = 0, valid = 1, count; | |
ad2dd5bb | 372 | char *chain_name = get_chain_filename(odb); |
5c84b339 DS |
373 | FILE *fp; |
374 | int stat_res; | |
375 | ||
376 | fp = fopen(chain_name, "r"); | |
377 | stat_res = stat(chain_name, &st); | |
378 | free(chain_name); | |
379 | ||
380 | if (!fp || | |
381 | stat_res || | |
382 | st.st_size <= the_hash_algo->hexsz) | |
383 | return NULL; | |
384 | ||
385 | count = st.st_size / (the_hash_algo->hexsz + 1); | |
386 | oids = xcalloc(count, sizeof(struct object_id)); | |
387 | ||
c523035c DS |
388 | prepare_alt_odb(r); |
389 | ||
390 | for (i = 0; i < count; i++) { | |
391 | struct object_directory *odb; | |
5c84b339 DS |
392 | |
393 | if (strbuf_getline_lf(&line, fp) == EOF) | |
394 | break; | |
395 | ||
396 | if (get_oid_hex(line.buf, &oids[i])) { | |
397 | warning(_("invalid commit-graph chain: line '%s' not a hash"), | |
398 | line.buf); | |
399 | valid = 0; | |
400 | break; | |
401 | } | |
402 | ||
c523035c DS |
403 | valid = 0; |
404 | for (odb = r->objects->odb; odb; odb = odb->next) { | |
ad2dd5bb | 405 | char *graph_name = get_split_graph_filename(odb, line.buf); |
a7df60ca | 406 | struct commit_graph *g = load_commit_graph_one(graph_name, odb); |
5c84b339 | 407 | |
c523035c DS |
408 | free(graph_name); |
409 | ||
410 | if (g) { | |
c523035c DS |
411 | if (add_graph_to_chain(g, graph_chain, oids, i)) { |
412 | graph_chain = g; | |
413 | valid = 1; | |
414 | } | |
415 | ||
416 | break; | |
417 | } | |
418 | } | |
419 | ||
420 | if (!valid) { | |
421 | warning(_("unable to find all commit-graph files")); | |
422 | break; | |
423 | } | |
5c84b339 DS |
424 | } |
425 | ||
426 | free(oids); | |
427 | fclose(fp); | |
0aa6bce7 | 428 | strbuf_release(&line); |
5c84b339 DS |
429 | |
430 | return graph_chain; | |
431 | } | |
432 | ||
13c24992 TB |
433 | struct commit_graph *read_commit_graph_one(struct repository *r, |
434 | struct object_directory *odb) | |
5c84b339 | 435 | { |
13c24992 | 436 | struct commit_graph *g = load_commit_graph_v1(r, odb); |
5c84b339 DS |
437 | |
438 | if (!g) | |
13c24992 | 439 | g = load_commit_graph_chain(r, odb); |
5c84b339 DS |
440 | |
441 | return g; | |
61df89c8 ÆAB |
442 | } |
443 | ||
13c24992 TB |
444 | static void prepare_commit_graph_one(struct repository *r, |
445 | struct object_directory *odb) | |
177722b3 | 446 | { |
177722b3 | 447 | |
dade47c0 | 448 | if (r->objects->commit_graph) |
177722b3 DS |
449 | return; |
450 | ||
13c24992 | 451 | r->objects->commit_graph = read_commit_graph_one(r, odb); |
177722b3 DS |
452 | } |
453 | ||
5faf357b JT |
454 | /* |
455 | * Return 1 if commit_graph is non-NULL, and 0 otherwise. | |
456 | * | |
15beaaa3 | 457 | * On the first invocation, this function attempts to load the commit |
5faf357b JT |
458 | * graph if the_repository is configured to have one. |
459 | */ | |
dade47c0 | 460 | static int prepare_commit_graph(struct repository *r) |
177722b3 | 461 | { |
263db403 | 462 | struct object_directory *odb; |
dade47c0 | 463 | |
6abada18 JK |
464 | /* |
465 | * This must come before the "already attempted?" check below, because | |
466 | * we want to disable even an already-loaded graph file. | |
467 | */ | |
468 | if (r->commit_graph_disabled) | |
469 | return 0; | |
43d35618 | 470 | |
dade47c0 JT |
471 | if (r->objects->commit_graph_attempted) |
472 | return !!r->objects->commit_graph; | |
473 | r->objects->commit_graph_attempted = 1; | |
474 | ||
fbab552a JK |
475 | if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD, 0)) |
476 | die("dying as requested by the '%s' variable on commit-graph load!", | |
477 | GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD); | |
478 | ||
7211b9e7 DS |
479 | prepare_repo_settings(r); |
480 | ||
859fdc0c | 481 | if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) && |
7211b9e7 | 482 | r->settings.core_commit_graph != 1) |
dade47c0 JT |
483 | /* |
484 | * This repository is not configured to use commit graphs, so | |
485 | * do not load one. (But report commit_graph_attempted anyway | |
486 | * so that commit graph loading is not attempted again for this | |
487 | * repository.) | |
488 | */ | |
5faf357b JT |
489 | return 0; |
490 | ||
d6538246 DS |
491 | if (!commit_graph_compatible(r)) |
492 | return 0; | |
493 | ||
dade47c0 | 494 | prepare_alt_odb(r); |
f0eaf638 | 495 | for (odb = r->objects->odb; |
263db403 JK |
496 | !r->objects->commit_graph && odb; |
497 | odb = odb->next) | |
13c24992 | 498 | prepare_commit_graph_one(r, odb); |
dade47c0 | 499 | return !!r->objects->commit_graph; |
177722b3 DS |
500 | } |
501 | ||
6cc01743 DS |
502 | int generation_numbers_enabled(struct repository *r) |
503 | { | |
504 | uint32_t first_generation; | |
505 | struct commit_graph *g; | |
506 | if (!prepare_commit_graph(r)) | |
507 | return 0; | |
508 | ||
509 | g = r->objects->commit_graph; | |
510 | ||
511 | if (!g->num_commits) | |
512 | return 0; | |
513 | ||
514 | first_generation = get_be32(g->chunk_commit_data + | |
515 | g->hash_len + 8) >> 2; | |
516 | ||
517 | return !!first_generation; | |
518 | } | |
519 | ||
d4f4d60f DS |
520 | static void close_commit_graph_one(struct commit_graph *g) |
521 | { | |
522 | if (!g) | |
523 | return; | |
524 | ||
525 | close_commit_graph_one(g->base_graph); | |
526 | free_commit_graph(g); | |
527 | } | |
528 | ||
c3a3a964 | 529 | void close_commit_graph(struct raw_object_store *o) |
177722b3 | 530 | { |
d4f4d60f | 531 | close_commit_graph_one(o->commit_graph); |
c3a3a964 | 532 | o->commit_graph = NULL; |
177722b3 DS |
533 | } |
534 | ||
535 | static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos) | |
536 | { | |
537 | return bsearch_hash(oid->hash, g->chunk_oid_fanout, | |
538 | g->chunk_oid_lookup, g->hash_len, pos); | |
539 | } | |
540 | ||
d4f4d60f DS |
541 | static void load_oid_from_graph(struct commit_graph *g, |
542 | uint32_t pos, | |
543 | struct object_id *oid) | |
544 | { | |
545 | uint32_t lex_index; | |
546 | ||
547 | while (g && pos < g->num_commits_in_base) | |
548 | g = g->base_graph; | |
549 | ||
550 | if (!g) | |
551 | BUG("NULL commit-graph"); | |
552 | ||
553 | if (pos >= g->num_commits + g->num_commits_in_base) | |
554 | die(_("invalid commit position. commit-graph is likely corrupt")); | |
555 | ||
556 | lex_index = pos - g->num_commits_in_base; | |
557 | ||
558 | hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index); | |
559 | } | |
560 | ||
4f542b7a SB |
561 | static struct commit_list **insert_parent_or_die(struct repository *r, |
562 | struct commit_graph *g, | |
d4f4d60f | 563 | uint32_t pos, |
177722b3 DS |
564 | struct commit_list **pptr) |
565 | { | |
566 | struct commit *c; | |
567 | struct object_id oid; | |
96af91d4 | 568 | |
d4f4d60f DS |
569 | if (pos >= g->num_commits + g->num_commits_in_base) |
570 | die("invalid parent position %"PRIu32, pos); | |
53614b13 | 571 | |
d4f4d60f | 572 | load_oid_from_graph(g, pos, &oid); |
4f542b7a | 573 | c = lookup_commit(r, &oid); |
177722b3 | 574 | if (!c) |
4f5b532d | 575 | die(_("could not find commit %s"), oid_to_hex(&oid)); |
177722b3 DS |
576 | c->graph_pos = pos; |
577 | return &commit_list_insert(c, pptr)->next; | |
578 | } | |
579 | ||
e2838d85 DS |
580 | static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos) |
581 | { | |
d4f4d60f DS |
582 | const unsigned char *commit_data; |
583 | uint32_t lex_index; | |
584 | ||
585 | while (pos < g->num_commits_in_base) | |
586 | g = g->base_graph; | |
587 | ||
588 | lex_index = pos - g->num_commits_in_base; | |
589 | commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index; | |
e2838d85 DS |
590 | item->graph_pos = pos; |
591 | item->generation = get_be32(commit_data + g->hash_len + 8) >> 2; | |
592 | } | |
593 | ||
a133c40b NTND |
594 | static inline void set_commit_tree(struct commit *c, struct tree *t) |
595 | { | |
596 | c->maybe_tree = t; | |
597 | } | |
598 | ||
4f542b7a SB |
599 | static int fill_commit_in_graph(struct repository *r, |
600 | struct commit *item, | |
601 | struct commit_graph *g, uint32_t pos) | |
177722b3 | 602 | { |
177722b3 DS |
603 | uint32_t edge_value; |
604 | uint32_t *parent_data_ptr; | |
605 | uint64_t date_low, date_high; | |
606 | struct commit_list **pptr; | |
d4f4d60f DS |
607 | const unsigned char *commit_data; |
608 | uint32_t lex_index; | |
177722b3 | 609 | |
d4f4d60f DS |
610 | while (pos < g->num_commits_in_base) |
611 | g = g->base_graph; | |
612 | ||
613 | if (pos >= g->num_commits + g->num_commits_in_base) | |
614 | die(_("invalid commit position. commit-graph is likely corrupt")); | |
615 | ||
616 | /* | |
617 | * Store the "full" position, but then use the | |
618 | * "local" position for the rest of the calculation. | |
619 | */ | |
177722b3 | 620 | item->graph_pos = pos; |
d4f4d60f DS |
621 | lex_index = pos - g->num_commits_in_base; |
622 | ||
623 | commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index; | |
624 | ||
625 | item->object.parsed = 1; | |
177722b3 | 626 | |
a133c40b | 627 | set_commit_tree(item, NULL); |
177722b3 DS |
628 | |
629 | date_high = get_be32(commit_data + g->hash_len + 8) & 0x3; | |
630 | date_low = get_be32(commit_data + g->hash_len + 12); | |
631 | item->date = (timestamp_t)((date_high << 32) | date_low); | |
632 | ||
83073cc9 DS |
633 | item->generation = get_be32(commit_data + g->hash_len + 8) >> 2; |
634 | ||
177722b3 DS |
635 | pptr = &item->parents; |
636 | ||
637 | edge_value = get_be32(commit_data + g->hash_len); | |
638 | if (edge_value == GRAPH_PARENT_NONE) | |
639 | return 1; | |
4f542b7a | 640 | pptr = insert_parent_or_die(r, g, edge_value, pptr); |
177722b3 DS |
641 | |
642 | edge_value = get_be32(commit_data + g->hash_len + 4); | |
643 | if (edge_value == GRAPH_PARENT_NONE) | |
644 | return 1; | |
5af7417b | 645 | if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) { |
4f542b7a | 646 | pptr = insert_parent_or_die(r, g, edge_value, pptr); |
177722b3 DS |
647 | return 1; |
648 | } | |
649 | ||
5af7417b | 650 | parent_data_ptr = (uint32_t*)(g->chunk_extra_edges + |
177722b3 DS |
651 | 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK)); |
652 | do { | |
653 | edge_value = get_be32(parent_data_ptr); | |
4f542b7a | 654 | pptr = insert_parent_or_die(r, g, |
177722b3 DS |
655 | edge_value & GRAPH_EDGE_LAST_MASK, |
656 | pptr); | |
657 | parent_data_ptr++; | |
658 | } while (!(edge_value & GRAPH_LAST_EDGE)); | |
659 | ||
660 | return 1; | |
661 | } | |
662 | ||
e2838d85 DS |
663 | static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos) |
664 | { | |
665 | if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) { | |
666 | *pos = item->graph_pos; | |
667 | return 1; | |
668 | } else { | |
d4f4d60f DS |
669 | struct commit_graph *cur_g = g; |
670 | uint32_t lex_index; | |
671 | ||
672 | while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index)) | |
673 | cur_g = cur_g->base_graph; | |
674 | ||
675 | if (cur_g) { | |
676 | *pos = lex_index + cur_g->num_commits_in_base; | |
677 | return 1; | |
678 | } | |
679 | ||
680 | return 0; | |
e2838d85 DS |
681 | } |
682 | } | |
683 | ||
4f542b7a SB |
684 | static int parse_commit_in_graph_one(struct repository *r, |
685 | struct commit_graph *g, | |
686 | struct commit *item) | |
177722b3 | 687 | { |
e2838d85 DS |
688 | uint32_t pos; |
689 | ||
177722b3 DS |
690 | if (item->object.parsed) |
691 | return 1; | |
ee797053 DS |
692 | |
693 | if (find_commit_in_graph(item, g, &pos)) | |
4f542b7a | 694 | return fill_commit_in_graph(r, item, g, pos); |
ee797053 DS |
695 | |
696 | return 0; | |
697 | } | |
698 | ||
dade47c0 | 699 | int parse_commit_in_graph(struct repository *r, struct commit *item) |
ee797053 | 700 | { |
dade47c0 | 701 | if (!prepare_commit_graph(r)) |
ee797053 | 702 | return 0; |
4f542b7a | 703 | return parse_commit_in_graph_one(r, r->objects->commit_graph, item); |
177722b3 DS |
704 | } |
705 | ||
dade47c0 | 706 | void load_commit_graph_info(struct repository *r, struct commit *item) |
e2838d85 DS |
707 | { |
708 | uint32_t pos; | |
dade47c0 | 709 | if (!prepare_commit_graph(r)) |
e2838d85 | 710 | return; |
dade47c0 JT |
711 | if (find_commit_in_graph(item, r->objects->commit_graph, &pos)) |
712 | fill_commit_graph_info(item, r->objects->commit_graph, pos); | |
e2838d85 DS |
713 | } |
714 | ||
4f542b7a SB |
715 | static struct tree *load_tree_for_commit(struct repository *r, |
716 | struct commit_graph *g, | |
717 | struct commit *c) | |
7b8a21db DS |
718 | { |
719 | struct object_id oid; | |
d4f4d60f DS |
720 | const unsigned char *commit_data; |
721 | ||
722 | while (c->graph_pos < g->num_commits_in_base) | |
723 | g = g->base_graph; | |
724 | ||
725 | commit_data = g->chunk_commit_data + | |
726 | GRAPH_DATA_WIDTH * (c->graph_pos - g->num_commits_in_base); | |
7b8a21db DS |
727 | |
728 | hashcpy(oid.hash, commit_data); | |
a133c40b | 729 | set_commit_tree(c, lookup_tree(r, &oid)); |
7b8a21db DS |
730 | |
731 | return c->maybe_tree; | |
732 | } | |
733 | ||
4f542b7a SB |
734 | static struct tree *get_commit_tree_in_graph_one(struct repository *r, |
735 | struct commit_graph *g, | |
0cbef8f8 | 736 | const struct commit *c) |
7b8a21db DS |
737 | { |
738 | if (c->maybe_tree) | |
739 | return c->maybe_tree; | |
740 | if (c->graph_pos == COMMIT_NOT_FROM_GRAPH) | |
0cbef8f8 DS |
741 | BUG("get_commit_tree_in_graph_one called from non-commit-graph commit"); |
742 | ||
4f542b7a | 743 | return load_tree_for_commit(r, g, (struct commit *)c); |
0cbef8f8 | 744 | } |
7b8a21db | 745 | |
dade47c0 | 746 | struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c) |
0cbef8f8 | 747 | { |
4f542b7a | 748 | return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c); |
7b8a21db DS |
749 | } |
750 | ||
c9905bea DS |
751 | struct packed_commit_list { |
752 | struct commit **list; | |
753 | int nr; | |
754 | int alloc; | |
755 | }; | |
756 | ||
757 | struct packed_oid_list { | |
758 | struct object_id *list; | |
759 | int nr; | |
760 | int alloc; | |
761 | }; | |
762 | ||
763 | struct write_commit_graph_context { | |
764 | struct repository *r; | |
0bd52e27 | 765 | struct object_directory *odb; |
c9905bea DS |
766 | char *graph_name; |
767 | struct packed_oid_list oids; | |
768 | struct packed_commit_list commits; | |
769 | int num_extra_edges; | |
770 | unsigned long approx_nr_objects; | |
771 | struct progress *progress; | |
772 | int progress_done; | |
773 | uint64_t progress_cnt; | |
6c622f9f DS |
774 | |
775 | char *base_graph_name; | |
776 | int num_commit_graphs_before; | |
777 | int num_commit_graphs_after; | |
778 | char **commit_graph_filenames_before; | |
779 | char **commit_graph_filenames_after; | |
780 | char **commit_graph_hash_after; | |
781 | uint32_t new_num_commits_in_base; | |
782 | struct commit_graph *new_base_graph; | |
783 | ||
c9905bea | 784 | unsigned append:1, |
6c622f9f | 785 | report_progress:1, |
7c5c9b9c SG |
786 | split:1, |
787 | check_oids:1; | |
c2bc6e6a DS |
788 | |
789 | const struct split_commit_graph_opts *split_opts; | |
c9905bea DS |
790 | }; |
791 | ||
08fd81c9 | 792 | static void write_graph_chunk_fanout(struct hashfile *f, |
c9905bea | 793 | struct write_commit_graph_context *ctx) |
08fd81c9 DS |
794 | { |
795 | int i, count = 0; | |
c9905bea | 796 | struct commit **list = ctx->commits.list; |
08fd81c9 DS |
797 | |
798 | /* | |
799 | * Write the first-level table (the list is sorted, | |
800 | * but we use a 256-entry lookup to be able to avoid | |
801 | * having to do eight extra binary search iterations). | |
802 | */ | |
803 | for (i = 0; i < 256; i++) { | |
c9905bea | 804 | while (count < ctx->commits.nr) { |
08fd81c9 DS |
805 | if ((*list)->object.oid.hash[0] != i) |
806 | break; | |
c9905bea | 807 | display_progress(ctx->progress, ++ctx->progress_cnt); |
08fd81c9 DS |
808 | count++; |
809 | list++; | |
810 | } | |
811 | ||
812 | hashwrite_be32(f, count); | |
813 | } | |
814 | } | |
815 | ||
816 | static void write_graph_chunk_oids(struct hashfile *f, int hash_len, | |
c9905bea | 817 | struct write_commit_graph_context *ctx) |
08fd81c9 | 818 | { |
c9905bea | 819 | struct commit **list = ctx->commits.list; |
08fd81c9 | 820 | int count; |
c9905bea DS |
821 | for (count = 0; count < ctx->commits.nr; count++, list++) { |
822 | display_progress(ctx->progress, ++ctx->progress_cnt); | |
08fd81c9 | 823 | hashwrite(f, (*list)->object.oid.hash, (int)hash_len); |
53035c4f | 824 | } |
08fd81c9 DS |
825 | } |
826 | ||
827 | static const unsigned char *commit_to_sha1(size_t index, void *table) | |
828 | { | |
829 | struct commit **commits = table; | |
830 | return commits[index]->object.oid.hash; | |
831 | } | |
832 | ||
833 | static void write_graph_chunk_data(struct hashfile *f, int hash_len, | |
c9905bea | 834 | struct write_commit_graph_context *ctx) |
08fd81c9 | 835 | { |
c9905bea DS |
836 | struct commit **list = ctx->commits.list; |
837 | struct commit **last = ctx->commits.list + ctx->commits.nr; | |
08fd81c9 DS |
838 | uint32_t num_extra_edges = 0; |
839 | ||
840 | while (list < last) { | |
841 | struct commit_list *parent; | |
806278de | 842 | struct object_id *tree; |
08fd81c9 DS |
843 | int edge_value; |
844 | uint32_t packedDate[2]; | |
c9905bea | 845 | display_progress(ctx->progress, ++ctx->progress_cnt); |
08fd81c9 | 846 | |
16749b8d TB |
847 | if (parse_commit_no_graph(*list)) |
848 | die(_("unable to parse commit %s"), | |
849 | oid_to_hex(&(*list)->object.oid)); | |
806278de | 850 | tree = get_commit_tree_oid(*list); |
806278de | 851 | hashwrite(f, tree->hash, hash_len); |
08fd81c9 DS |
852 | |
853 | parent = (*list)->parents; | |
854 | ||
855 | if (!parent) | |
856 | edge_value = GRAPH_PARENT_NONE; | |
857 | else { | |
858 | edge_value = sha1_pos(parent->item->object.oid.hash, | |
c9905bea DS |
859 | ctx->commits.list, |
860 | ctx->commits.nr, | |
08fd81c9 DS |
861 | commit_to_sha1); |
862 | ||
6c622f9f DS |
863 | if (edge_value >= 0) |
864 | edge_value += ctx->new_num_commits_in_base; | |
8a6ac287 | 865 | else if (ctx->new_base_graph) { |
6c622f9f DS |
866 | uint32_t pos; |
867 | if (find_commit_in_graph(parent->item, | |
868 | ctx->new_base_graph, | |
869 | &pos)) | |
870 | edge_value = pos; | |
871 | } | |
872 | ||
08fd81c9 | 873 | if (edge_value < 0) |
cce99cd8 DS |
874 | BUG("missing parent %s for commit %s", |
875 | oid_to_hex(&parent->item->object.oid), | |
876 | oid_to_hex(&(*list)->object.oid)); | |
08fd81c9 DS |
877 | } |
878 | ||
879 | hashwrite_be32(f, edge_value); | |
880 | ||
881 | if (parent) | |
882 | parent = parent->next; | |
883 | ||
884 | if (!parent) | |
885 | edge_value = GRAPH_PARENT_NONE; | |
886 | else if (parent->next) | |
5af7417b | 887 | edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges; |
08fd81c9 DS |
888 | else { |
889 | edge_value = sha1_pos(parent->item->object.oid.hash, | |
c9905bea DS |
890 | ctx->commits.list, |
891 | ctx->commits.nr, | |
08fd81c9 | 892 | commit_to_sha1); |
6c622f9f DS |
893 | |
894 | if (edge_value >= 0) | |
895 | edge_value += ctx->new_num_commits_in_base; | |
8a6ac287 | 896 | else if (ctx->new_base_graph) { |
6c622f9f DS |
897 | uint32_t pos; |
898 | if (find_commit_in_graph(parent->item, | |
899 | ctx->new_base_graph, | |
900 | &pos)) | |
901 | edge_value = pos; | |
902 | } | |
903 | ||
08fd81c9 | 904 | if (edge_value < 0) |
cce99cd8 DS |
905 | BUG("missing parent %s for commit %s", |
906 | oid_to_hex(&parent->item->object.oid), | |
907 | oid_to_hex(&(*list)->object.oid)); | |
08fd81c9 DS |
908 | } |
909 | ||
910 | hashwrite_be32(f, edge_value); | |
911 | ||
5af7417b | 912 | if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) { |
08fd81c9 DS |
913 | do { |
914 | num_extra_edges++; | |
915 | parent = parent->next; | |
916 | } while (parent); | |
917 | } | |
918 | ||
919 | if (sizeof((*list)->date) > 4) | |
920 | packedDate[0] = htonl(((*list)->date >> 32) & 0x3); | |
921 | else | |
922 | packedDate[0] = 0; | |
923 | ||
3258c663 DS |
924 | packedDate[0] |= htonl((*list)->generation << 2); |
925 | ||
08fd81c9 DS |
926 | packedDate[1] = htonl((*list)->date); |
927 | hashwrite(f, packedDate, 8); | |
928 | ||
929 | list++; | |
930 | } | |
931 | } | |
932 | ||
5af7417b | 933 | static void write_graph_chunk_extra_edges(struct hashfile *f, |
c9905bea | 934 | struct write_commit_graph_context *ctx) |
08fd81c9 | 935 | { |
c9905bea DS |
936 | struct commit **list = ctx->commits.list; |
937 | struct commit **last = ctx->commits.list + ctx->commits.nr; | |
08fd81c9 DS |
938 | struct commit_list *parent; |
939 | ||
940 | while (list < last) { | |
941 | int num_parents = 0; | |
53035c4f | 942 | |
c9905bea | 943 | display_progress(ctx->progress, ++ctx->progress_cnt); |
53035c4f | 944 | |
08fd81c9 DS |
945 | for (parent = (*list)->parents; num_parents < 3 && parent; |
946 | parent = parent->next) | |
947 | num_parents++; | |
948 | ||
949 | if (num_parents <= 2) { | |
950 | list++; | |
951 | continue; | |
952 | } | |
953 | ||
954 | /* Since num_parents > 2, this initializer is safe. */ | |
955 | for (parent = (*list)->parents->next; parent; parent = parent->next) { | |
956 | int edge_value = sha1_pos(parent->item->object.oid.hash, | |
c9905bea DS |
957 | ctx->commits.list, |
958 | ctx->commits.nr, | |
08fd81c9 DS |
959 | commit_to_sha1); |
960 | ||
6c622f9f DS |
961 | if (edge_value >= 0) |
962 | edge_value += ctx->new_num_commits_in_base; | |
8a6ac287 | 963 | else if (ctx->new_base_graph) { |
6c622f9f DS |
964 | uint32_t pos; |
965 | if (find_commit_in_graph(parent->item, | |
966 | ctx->new_base_graph, | |
967 | &pos)) | |
968 | edge_value = pos; | |
969 | } | |
970 | ||
08fd81c9 | 971 | if (edge_value < 0) |
cce99cd8 DS |
972 | BUG("missing parent %s for commit %s", |
973 | oid_to_hex(&parent->item->object.oid), | |
974 | oid_to_hex(&(*list)->object.oid)); | |
08fd81c9 DS |
975 | else if (!parent->next) |
976 | edge_value |= GRAPH_LAST_EDGE; | |
977 | ||
978 | hashwrite_be32(f, edge_value); | |
979 | } | |
980 | ||
981 | list++; | |
982 | } | |
983 | } | |
984 | ||
3cbc6ed3 | 985 | static int oid_compare(const void *_a, const void *_b) |
08fd81c9 DS |
986 | { |
987 | const struct object_id *a = (const struct object_id *)_a; | |
988 | const struct object_id *b = (const struct object_id *)_b; | |
989 | return oidcmp(a, b); | |
990 | } | |
991 | ||
08fd81c9 DS |
992 | static int add_packed_commits(const struct object_id *oid, |
993 | struct packed_git *pack, | |
994 | uint32_t pos, | |
995 | void *data) | |
996 | { | |
c9905bea | 997 | struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data; |
08fd81c9 DS |
998 | enum object_type type; |
999 | off_t offset = nth_packed_object_offset(pack, pos); | |
1000 | struct object_info oi = OBJECT_INFO_INIT; | |
1001 | ||
c9905bea DS |
1002 | if (ctx->progress) |
1003 | display_progress(ctx->progress, ++ctx->progress_done); | |
7b0f2292 | 1004 | |
08fd81c9 | 1005 | oi.typep = &type; |
c9905bea | 1006 | if (packed_object_info(ctx->r, pack, offset, &oi) < 0) |
4f5b532d | 1007 | die(_("unable to get type of object %s"), oid_to_hex(oid)); |
08fd81c9 DS |
1008 | |
1009 | if (type != OBJ_COMMIT) | |
1010 | return 0; | |
1011 | ||
c9905bea DS |
1012 | ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc); |
1013 | oidcpy(&(ctx->oids.list[ctx->oids.nr]), oid); | |
1014 | ctx->oids.nr++; | |
08fd81c9 DS |
1015 | |
1016 | return 0; | |
1017 | } | |
1018 | ||
c9905bea | 1019 | static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit) |
4f2542b4 DS |
1020 | { |
1021 | struct commit_list *parent; | |
1022 | for (parent = commit->parents; parent; parent = parent->next) { | |
cb99a34e | 1023 | if (!(parent->item->object.flags & REACHABLE)) { |
c9905bea DS |
1024 | ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc); |
1025 | oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid)); | |
1026 | ctx->oids.nr++; | |
cb99a34e | 1027 | parent->item->object.flags |= REACHABLE; |
4f2542b4 DS |
1028 | } |
1029 | } | |
1030 | } | |
1031 | ||
c9905bea | 1032 | static void close_reachable(struct write_commit_graph_context *ctx) |
4f2542b4 | 1033 | { |
49bbc57a | 1034 | int i; |
4f2542b4 | 1035 | struct commit *commit; |
8a6ac287 TB |
1036 | enum commit_graph_split_flags flags = ctx->split_opts ? |
1037 | ctx->split_opts->flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED; | |
4f2542b4 | 1038 | |
c9905bea DS |
1039 | if (ctx->report_progress) |
1040 | ctx->progress = start_delayed_progress( | |
1041 | _("Loading known commits in commit graph"), | |
1042 | ctx->oids.nr); | |
1043 | for (i = 0; i < ctx->oids.nr; i++) { | |
1044 | display_progress(ctx->progress, i + 1); | |
1045 | commit = lookup_commit(ctx->r, &ctx->oids.list[i]); | |
4f2542b4 | 1046 | if (commit) |
cb99a34e | 1047 | commit->object.flags |= REACHABLE; |
4f2542b4 | 1048 | } |
c9905bea | 1049 | stop_progress(&ctx->progress); |
4f2542b4 DS |
1050 | |
1051 | /* | |
c9905bea | 1052 | * As this loop runs, ctx->oids.nr may grow, but not more |
4f2542b4 DS |
1053 | * than the number of missing commits in the reachable |
1054 | * closure. | |
1055 | */ | |
c9905bea DS |
1056 | if (ctx->report_progress) |
1057 | ctx->progress = start_delayed_progress( | |
1058 | _("Expanding reachable commits in commit graph"), | |
67fa6aac | 1059 | 0); |
c9905bea DS |
1060 | for (i = 0; i < ctx->oids.nr; i++) { |
1061 | display_progress(ctx->progress, i + 1); | |
1062 | commit = lookup_commit(ctx->r, &ctx->oids.list[i]); | |
4f2542b4 | 1063 | |
6c622f9f DS |
1064 | if (!commit) |
1065 | continue; | |
1066 | if (ctx->split) { | |
8a6ac287 TB |
1067 | if ((!parse_commit(commit) && |
1068 | commit->graph_pos == COMMIT_NOT_FROM_GRAPH) || | |
1069 | flags == COMMIT_GRAPH_SPLIT_REPLACE) | |
6c622f9f DS |
1070 | add_missing_parents(ctx, commit); |
1071 | } else if (!parse_commit_no_graph(commit)) | |
c9905bea | 1072 | add_missing_parents(ctx, commit); |
4f2542b4 | 1073 | } |
c9905bea | 1074 | stop_progress(&ctx->progress); |
4f2542b4 | 1075 | |
c9905bea DS |
1076 | if (ctx->report_progress) |
1077 | ctx->progress = start_delayed_progress( | |
1078 | _("Clearing commit marks in commit graph"), | |
1079 | ctx->oids.nr); | |
1080 | for (i = 0; i < ctx->oids.nr; i++) { | |
1081 | display_progress(ctx->progress, i + 1); | |
1082 | commit = lookup_commit(ctx->r, &ctx->oids.list[i]); | |
4f2542b4 DS |
1083 | |
1084 | if (commit) | |
cb99a34e | 1085 | commit->object.flags &= ~REACHABLE; |
4f2542b4 | 1086 | } |
c9905bea | 1087 | stop_progress(&ctx->progress); |
4f2542b4 DS |
1088 | } |
1089 | ||
c9905bea | 1090 | static void compute_generation_numbers(struct write_commit_graph_context *ctx) |
3258c663 DS |
1091 | { |
1092 | int i; | |
1093 | struct commit_list *list = NULL; | |
1094 | ||
c9905bea | 1095 | if (ctx->report_progress) |
ecc08690 | 1096 | ctx->progress = start_delayed_progress( |
c9905bea DS |
1097 | _("Computing commit graph generation numbers"), |
1098 | ctx->commits.nr); | |
1099 | for (i = 0; i < ctx->commits.nr; i++) { | |
1100 | display_progress(ctx->progress, i + 1); | |
1101 | if (ctx->commits.list[i]->generation != GENERATION_NUMBER_INFINITY && | |
1102 | ctx->commits.list[i]->generation != GENERATION_NUMBER_ZERO) | |
3258c663 DS |
1103 | continue; |
1104 | ||
c9905bea | 1105 | commit_list_insert(ctx->commits.list[i], &list); |
3258c663 DS |
1106 | while (list) { |
1107 | struct commit *current = list->item; | |
1108 | struct commit_list *parent; | |
1109 | int all_parents_computed = 1; | |
1110 | uint32_t max_generation = 0; | |
1111 | ||
1112 | for (parent = current->parents; parent; parent = parent->next) { | |
1113 | if (parent->item->generation == GENERATION_NUMBER_INFINITY || | |
1114 | parent->item->generation == GENERATION_NUMBER_ZERO) { | |
1115 | all_parents_computed = 0; | |
1116 | commit_list_insert(parent->item, &list); | |
1117 | break; | |
1118 | } else if (parent->item->generation > max_generation) { | |
1119 | max_generation = parent->item->generation; | |
1120 | } | |
1121 | } | |
1122 | ||
1123 | if (all_parents_computed) { | |
1124 | current->generation = max_generation + 1; | |
1125 | pop_commit(&list); | |
1126 | ||
1127 | if (current->generation > GENERATION_NUMBER_MAX) | |
1128 | current->generation = GENERATION_NUMBER_MAX; | |
1129 | } | |
1130 | } | |
1131 | } | |
c9905bea | 1132 | stop_progress(&ctx->progress); |
3258c663 DS |
1133 | } |
1134 | ||
6830c360 TB |
1135 | static int add_ref_to_set(const char *refname, |
1136 | const struct object_id *oid, | |
1137 | int flags, void *cb_data) | |
59fb8770 | 1138 | { |
6830c360 | 1139 | struct oidset *commits = (struct oidset *)cb_data; |
59fb8770 | 1140 | |
6830c360 | 1141 | oidset_insert(commits, oid); |
59fb8770 DS |
1142 | return 0; |
1143 | } | |
1144 | ||
0bd52e27 | 1145 | int write_commit_graph_reachable(struct object_directory *odb, |
39d88318 | 1146 | enum commit_graph_write_flags flags, |
c2bc6e6a | 1147 | const struct split_commit_graph_opts *split_opts) |
59fb8770 | 1148 | { |
6830c360 | 1149 | struct oidset commits = OIDSET_INIT; |
e103f727 | 1150 | int result; |
59fb8770 | 1151 | |
6830c360 TB |
1152 | for_each_ref(add_ref_to_set, &commits); |
1153 | result = write_commit_graph(odb, NULL, &commits, | |
c2bc6e6a | 1154 | flags, split_opts); |
f4dbdfc4 | 1155 | |
6830c360 | 1156 | oidset_clear(&commits); |
e103f727 | 1157 | return result; |
59fb8770 DS |
1158 | } |
1159 | ||
ef5b83f2 DS |
1160 | static int fill_oids_from_packs(struct write_commit_graph_context *ctx, |
1161 | struct string_list *pack_indexes) | |
08fd81c9 | 1162 | { |
ef5b83f2 | 1163 | uint32_t i; |
28944739 | 1164 | struct strbuf progress_title = STRBUF_INIT; |
ef5b83f2 DS |
1165 | struct strbuf packname = STRBUF_INIT; |
1166 | int dirlen; | |
08fd81c9 | 1167 | |
0bd52e27 | 1168 | strbuf_addf(&packname, "%s/pack/", ctx->odb->path); |
ef5b83f2 DS |
1169 | dirlen = packname.len; |
1170 | if (ctx->report_progress) { | |
1171 | strbuf_addf(&progress_title, | |
1172 | Q_("Finding commits for commit graph in %d pack", | |
1173 | "Finding commits for commit graph in %d packs", | |
1174 | pack_indexes->nr), | |
1175 | pack_indexes->nr); | |
1176 | ctx->progress = start_delayed_progress(progress_title.buf, 0); | |
1177 | ctx->progress_done = 0; | |
7547b95b | 1178 | } |
ef5b83f2 DS |
1179 | for (i = 0; i < pack_indexes->nr; i++) { |
1180 | struct packed_git *p; | |
1181 | strbuf_setlen(&packname, dirlen); | |
1182 | strbuf_addstr(&packname, pack_indexes->items[i].string); | |
1183 | p = add_packed_git(packname.buf, packname.len, 1); | |
1184 | if (!p) { | |
1185 | error(_("error adding pack %s"), packname.buf); | |
1186 | return -1; | |
7547b95b | 1187 | } |
ef5b83f2 DS |
1188 | if (open_pack_index(p)) { |
1189 | error(_("error opening index for %s"), packname.buf); | |
1190 | return -1; | |
1191 | } | |
1192 | for_each_object_in_pack(p, add_packed_commits, ctx, | |
1193 | FOR_EACH_OBJECT_PACK_ORDER); | |
1194 | close_pack(p); | |
1195 | free(p); | |
7547b95b DS |
1196 | } |
1197 | ||
ef5b83f2 | 1198 | stop_progress(&ctx->progress); |
0aa6bce7 | 1199 | strbuf_release(&progress_title); |
ef5b83f2 DS |
1200 | strbuf_release(&packname); |
1201 | ||
1202 | return 0; | |
1203 | } | |
1204 | ||
6830c360 TB |
1205 | static int fill_oids_from_commits(struct write_commit_graph_context *ctx, |
1206 | struct oidset *commits) | |
4c9efe85 | 1207 | { |
6830c360 | 1208 | uint32_t i = 0; |
4c9efe85 | 1209 | struct strbuf progress_title = STRBUF_INIT; |
6830c360 TB |
1210 | struct oidset_iter iter; |
1211 | struct object_id *oid; | |
1212 | ||
1213 | if (!oidset_size(commits)) | |
1214 | return 0; | |
4c9efe85 DS |
1215 | |
1216 | if (ctx->report_progress) { | |
1217 | strbuf_addf(&progress_title, | |
1218 | Q_("Finding commits for commit graph from %d ref", | |
1219 | "Finding commits for commit graph from %d refs", | |
6830c360 TB |
1220 | oidset_size(commits)), |
1221 | oidset_size(commits)); | |
4c9efe85 DS |
1222 | ctx->progress = start_delayed_progress( |
1223 | progress_title.buf, | |
6830c360 | 1224 | oidset_size(commits)); |
3d5df01b | 1225 | } |
6830c360 TB |
1226 | |
1227 | oidset_iter_init(commits, &iter); | |
1228 | while ((oid = oidset_iter_next(&iter))) { | |
4c9efe85 DS |
1229 | struct commit *result; |
1230 | ||
6830c360 TB |
1231 | display_progress(ctx->progress, ++i); |
1232 | ||
1233 | result = lookup_commit_reference_gently(ctx->r, oid, 1); | |
1234 | if (result) { | |
4c9efe85 DS |
1235 | ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc); |
1236 | oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid)); | |
1237 | ctx->oids.nr++; | |
7c5c9b9c SG |
1238 | } else if (ctx->check_oids) { |
1239 | error(_("invalid commit object id: %s"), | |
6830c360 | 1240 | oid_to_hex(oid)); |
7c5c9b9c | 1241 | return -1; |
3d5df01b DS |
1242 | } |
1243 | } | |
6830c360 | 1244 | |
4c9efe85 DS |
1245 | stop_progress(&ctx->progress); |
1246 | strbuf_release(&progress_title); | |
7c5c9b9c SG |
1247 | |
1248 | return 0; | |
4c9efe85 | 1249 | } |
3d5df01b | 1250 | |
b2c83060 DS |
1251 | static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx) |
1252 | { | |
1253 | if (ctx->report_progress) | |
1254 | ctx->progress = start_delayed_progress( | |
1255 | _("Finding commits for commit graph among packed objects"), | |
1256 | ctx->approx_nr_objects); | |
1257 | for_each_packed_object(add_packed_commits, ctx, | |
1258 | FOR_EACH_OBJECT_PACK_ORDER); | |
1259 | if (ctx->progress_done < ctx->approx_nr_objects) | |
1260 | display_progress(ctx->progress, ctx->approx_nr_objects); | |
1261 | stop_progress(&ctx->progress); | |
1262 | } | |
049d51a2 | 1263 | |
014e3440 DS |
1264 | static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx) |
1265 | { | |
1266 | uint32_t i, count_distinct = 1; | |
08fd81c9 | 1267 | |
014e3440 DS |
1268 | if (ctx->report_progress) |
1269 | ctx->progress = start_delayed_progress( | |
890226cc | 1270 | _("Counting distinct commits in commit graph"), |
014e3440 DS |
1271 | ctx->oids.nr); |
1272 | display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */ | |
3cbc6ed3 | 1273 | QSORT(ctx->oids.list, ctx->oids.nr, oid_compare); |
014e3440 DS |
1274 | |
1275 | for (i = 1; i < ctx->oids.nr; i++) { | |
1276 | display_progress(ctx->progress, i + 1); | |
6c622f9f DS |
1277 | if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i])) { |
1278 | if (ctx->split) { | |
1279 | struct commit *c = lookup_commit(ctx->r, &ctx->oids.list[i]); | |
1280 | ||
1281 | if (!c || c->graph_pos != COMMIT_NOT_FROM_GRAPH) | |
1282 | continue; | |
1283 | } | |
1284 | ||
08fd81c9 | 1285 | count_distinct++; |
6c622f9f | 1286 | } |
08fd81c9 | 1287 | } |
014e3440 | 1288 | stop_progress(&ctx->progress); |
08fd81c9 | 1289 | |
014e3440 DS |
1290 | return count_distinct; |
1291 | } | |
08fd81c9 | 1292 | |
f998d542 DS |
1293 | static void copy_oids_to_commits(struct write_commit_graph_context *ctx) |
1294 | { | |
1295 | uint32_t i; | |
8a6ac287 TB |
1296 | enum commit_graph_split_flags flags = ctx->split_opts ? |
1297 | ctx->split_opts->flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED; | |
08fd81c9 | 1298 | |
f998d542 DS |
1299 | ctx->num_extra_edges = 0; |
1300 | if (ctx->report_progress) | |
1301 | ctx->progress = start_delayed_progress( | |
890226cc | 1302 | _("Finding extra edges in commit graph"), |
f998d542 DS |
1303 | ctx->oids.nr); |
1304 | for (i = 0; i < ctx->oids.nr; i++) { | |
689a146c RS |
1305 | unsigned int num_parents; |
1306 | ||
f998d542 DS |
1307 | display_progress(ctx->progress, i + 1); |
1308 | if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i])) | |
08fd81c9 DS |
1309 | continue; |
1310 | ||
6c622f9f | 1311 | ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc); |
f998d542 | 1312 | ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.list[i]); |
6c622f9f | 1313 | |
8a6ac287 | 1314 | if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE && |
6c622f9f DS |
1315 | ctx->commits.list[ctx->commits.nr]->graph_pos != COMMIT_NOT_FROM_GRAPH) |
1316 | continue; | |
1317 | ||
8a6ac287 TB |
1318 | if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE) |
1319 | parse_commit(ctx->commits.list[ctx->commits.nr]); | |
1320 | else | |
1321 | parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]); | |
08fd81c9 | 1322 | |
689a146c | 1323 | num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents); |
08fd81c9 | 1324 | if (num_parents > 2) |
f998d542 | 1325 | ctx->num_extra_edges += num_parents - 1; |
08fd81c9 | 1326 | |
f998d542 | 1327 | ctx->commits.nr++; |
08fd81c9 | 1328 | } |
f998d542 DS |
1329 | stop_progress(&ctx->progress); |
1330 | } | |
08fd81c9 | 1331 | |
6c622f9f DS |
1332 | static int write_graph_chunk_base_1(struct hashfile *f, |
1333 | struct commit_graph *g) | |
1334 | { | |
1335 | int num = 0; | |
1336 | ||
1337 | if (!g) | |
1338 | return 0; | |
1339 | ||
1340 | num = write_graph_chunk_base_1(f, g->base_graph); | |
1341 | hashwrite(f, g->oid.hash, the_hash_algo->rawsz); | |
1342 | return num + 1; | |
1343 | } | |
1344 | ||
1345 | static int write_graph_chunk_base(struct hashfile *f, | |
1346 | struct write_commit_graph_context *ctx) | |
1347 | { | |
1348 | int num = write_graph_chunk_base_1(f, ctx->new_base_graph); | |
1349 | ||
1350 | if (num != ctx->num_commit_graphs_after - 1) { | |
1351 | error(_("failed to write correct number of base graph ids")); | |
1352 | return -1; | |
1353 | } | |
1354 | ||
1355 | return 0; | |
1356 | } | |
1357 | ||
238def57 | 1358 | static int write_commit_graph_file(struct write_commit_graph_context *ctx) |
08fd81c9 | 1359 | { |
238def57 | 1360 | uint32_t i; |
6c622f9f | 1361 | int fd; |
08fd81c9 | 1362 | struct hashfile *f; |
08fd81c9 | 1363 | struct lock_file lk = LOCK_INIT; |
6c622f9f DS |
1364 | uint32_t chunk_ids[6]; |
1365 | uint64_t chunk_offsets[6]; | |
c1665998 | 1366 | const unsigned hashsz = the_hash_algo->rawsz; |
28944739 | 1367 | struct strbuf progress_title = STRBUF_INIT; |
144354b0 | 1368 | int num_chunks = 3; |
6c622f9f DS |
1369 | struct object_id file_hash; |
1370 | ||
1371 | if (ctx->split) { | |
1372 | struct strbuf tmp_file = STRBUF_INIT; | |
1373 | ||
1374 | strbuf_addf(&tmp_file, | |
1375 | "%s/info/commit-graphs/tmp_graph_XXXXXX", | |
0bd52e27 | 1376 | ctx->odb->path); |
6c622f9f DS |
1377 | ctx->graph_name = strbuf_detach(&tmp_file, NULL); |
1378 | } else { | |
ad2dd5bb | 1379 | ctx->graph_name = get_commit_graph_filename(ctx->odb); |
6c622f9f | 1380 | } |
238def57 | 1381 | |
238def57 DS |
1382 | if (safe_create_leading_directories(ctx->graph_name)) { |
1383 | UNLEAK(ctx->graph_name); | |
1384 | error(_("unable to create leading directories of %s"), | |
1385 | ctx->graph_name); | |
1386 | return -1; | |
f4dbdfc4 | 1387 | } |
08fd81c9 | 1388 | |
6c622f9f | 1389 | if (ctx->split) { |
ad2dd5bb | 1390 | char *lock_name = get_chain_filename(ctx->odb); |
08fd81c9 | 1391 | |
6c622f9f | 1392 | hold_lock_file_for_update(&lk, lock_name, LOCK_DIE_ON_ERROR); |
08fd81c9 | 1393 | |
6c622f9f DS |
1394 | fd = git_mkstemp_mode(ctx->graph_name, 0444); |
1395 | if (fd < 0) { | |
a2d57e22 | 1396 | error(_("unable to create temporary graph layer")); |
6c622f9f DS |
1397 | return -1; |
1398 | } | |
1399 | ||
1400 | f = hashfd(fd, ctx->graph_name); | |
1401 | } else { | |
1402 | hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR); | |
1403 | fd = lk.tempfile->fd; | |
1404 | f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf); | |
1405 | } | |
08fd81c9 DS |
1406 | |
1407 | chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT; | |
1408 | chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP; | |
1409 | chunk_ids[2] = GRAPH_CHUNKID_DATA; | |
144354b0 DS |
1410 | if (ctx->num_extra_edges) { |
1411 | chunk_ids[num_chunks] = GRAPH_CHUNKID_EXTRAEDGES; | |
1412 | num_chunks++; | |
1413 | } | |
6c622f9f DS |
1414 | if (ctx->num_commit_graphs_after > 1) { |
1415 | chunk_ids[num_chunks] = GRAPH_CHUNKID_BASE; | |
1416 | num_chunks++; | |
1417 | } | |
144354b0 DS |
1418 | |
1419 | chunk_ids[num_chunks] = 0; | |
08fd81c9 DS |
1420 | |
1421 | chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH; | |
1422 | chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE; | |
238def57 DS |
1423 | chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr; |
1424 | chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr; | |
144354b0 DS |
1425 | |
1426 | num_chunks = 3; | |
1427 | if (ctx->num_extra_edges) { | |
1428 | chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] + | |
1429 | 4 * ctx->num_extra_edges; | |
1430 | num_chunks++; | |
1431 | } | |
6c622f9f DS |
1432 | if (ctx->num_commit_graphs_after > 1) { |
1433 | chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] + | |
1434 | hashsz * (ctx->num_commit_graphs_after - 1); | |
1435 | num_chunks++; | |
1436 | } | |
144354b0 DS |
1437 | |
1438 | hashwrite_be32(f, GRAPH_SIGNATURE); | |
1439 | ||
1440 | hashwrite_u8(f, GRAPH_VERSION); | |
1441 | hashwrite_u8(f, oid_version()); | |
1442 | hashwrite_u8(f, num_chunks); | |
6c622f9f | 1443 | hashwrite_u8(f, ctx->num_commit_graphs_after - 1); |
08fd81c9 DS |
1444 | |
1445 | for (i = 0; i <= num_chunks; i++) { | |
1446 | uint32_t chunk_write[3]; | |
1447 | ||
1448 | chunk_write[0] = htonl(chunk_ids[i]); | |
1449 | chunk_write[1] = htonl(chunk_offsets[i] >> 32); | |
1450 | chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff); | |
1451 | hashwrite(f, chunk_write, 12); | |
1452 | } | |
1453 | ||
238def57 | 1454 | if (ctx->report_progress) { |
28944739 ÆAB |
1455 | strbuf_addf(&progress_title, |
1456 | Q_("Writing out commit graph in %d pass", | |
1457 | "Writing out commit graph in %d passes", | |
1458 | num_chunks), | |
1459 | num_chunks); | |
238def57 | 1460 | ctx->progress = start_delayed_progress( |
28944739 | 1461 | progress_title.buf, |
238def57 | 1462 | num_chunks * ctx->commits.nr); |
28944739 | 1463 | } |
238def57 DS |
1464 | write_graph_chunk_fanout(f, ctx); |
1465 | write_graph_chunk_oids(f, hashsz, ctx); | |
1466 | write_graph_chunk_data(f, hashsz, ctx); | |
1467 | if (ctx->num_extra_edges) | |
1468 | write_graph_chunk_extra_edges(f, ctx); | |
6c622f9f DS |
1469 | if (ctx->num_commit_graphs_after > 1 && |
1470 | write_graph_chunk_base(f, ctx)) { | |
1471 | return -1; | |
1472 | } | |
238def57 | 1473 | stop_progress(&ctx->progress); |
28944739 | 1474 | strbuf_release(&progress_title); |
08fd81c9 | 1475 | |
6c622f9f DS |
1476 | if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) { |
1477 | char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid)); | |
ad2dd5bb | 1478 | char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash); |
6c622f9f DS |
1479 | |
1480 | free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]); | |
1481 | free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]); | |
1482 | ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name; | |
1483 | ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash; | |
1484 | } | |
1485 | ||
c3a3a964 | 1486 | close_commit_graph(ctx->r->objects); |
6c622f9f DS |
1487 | finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC); |
1488 | ||
1489 | if (ctx->split) { | |
1490 | FILE *chainf = fdopen_lock_file(&lk, "w"); | |
1491 | char *final_graph_name; | |
1492 | int result; | |
1493 | ||
1494 | close(fd); | |
1495 | ||
1496 | if (!chainf) { | |
1497 | error(_("unable to open commit-graph chain file")); | |
1498 | return -1; | |
1499 | } | |
1500 | ||
1501 | if (ctx->base_graph_name) { | |
8a6ac287 TB |
1502 | const char *dest; |
1503 | int idx = ctx->num_commit_graphs_after - 1; | |
1504 | if (ctx->num_commit_graphs_after > 1) | |
1505 | idx--; | |
1506 | ||
1507 | dest = ctx->commit_graph_filenames_after[idx]; | |
6c622f9f | 1508 | |
135a7123 DS |
1509 | if (strcmp(ctx->base_graph_name, dest)) { |
1510 | result = rename(ctx->base_graph_name, dest); | |
1511 | ||
1512 | if (result) { | |
1513 | error(_("failed to rename base commit-graph file")); | |
1514 | return -1; | |
1515 | } | |
6c622f9f DS |
1516 | } |
1517 | } else { | |
ad2dd5bb | 1518 | char *graph_name = get_commit_graph_filename(ctx->odb); |
6c622f9f DS |
1519 | unlink(graph_name); |
1520 | } | |
1521 | ||
1522 | ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash)); | |
ad2dd5bb | 1523 | final_graph_name = get_split_graph_filename(ctx->odb, |
6c622f9f DS |
1524 | ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]); |
1525 | ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name; | |
1526 | ||
1527 | result = rename(ctx->graph_name, final_graph_name); | |
1528 | ||
1529 | for (i = 0; i < ctx->num_commit_graphs_after; i++) | |
1530 | fprintf(lk.tempfile->fp, "%s\n", ctx->commit_graph_hash_after[i]); | |
1531 | ||
1532 | if (result) { | |
1533 | error(_("failed to rename temporary commit-graph file")); | |
1534 | return -1; | |
1535 | } | |
1536 | } | |
1537 | ||
08fd81c9 DS |
1538 | commit_lock_file(&lk); |
1539 | ||
238def57 DS |
1540 | return 0; |
1541 | } | |
1542 | ||
1771be90 DS |
1543 | static void split_graph_merge_strategy(struct write_commit_graph_context *ctx) |
1544 | { | |
8da02ce6 AH |
1545 | struct commit_graph *g; |
1546 | uint32_t num_commits; | |
fdbde82f | 1547 | enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED; |
1771be90 DS |
1548 | uint32_t i; |
1549 | ||
c2bc6e6a DS |
1550 | int max_commits = 0; |
1551 | int size_mult = 2; | |
1552 | ||
1553 | if (ctx->split_opts) { | |
1554 | max_commits = ctx->split_opts->max_commits; | |
63020f17 DS |
1555 | |
1556 | if (ctx->split_opts->size_multiple) | |
1557 | size_mult = ctx->split_opts->size_multiple; | |
fdbde82f TB |
1558 | |
1559 | flags = ctx->split_opts->flags; | |
c2bc6e6a DS |
1560 | } |
1561 | ||
1771be90 | 1562 | g = ctx->r->objects->commit_graph; |
8da02ce6 | 1563 | num_commits = ctx->commits.nr; |
8a6ac287 TB |
1564 | if (flags == COMMIT_GRAPH_SPLIT_REPLACE) |
1565 | ctx->num_commit_graphs_after = 1; | |
1566 | else | |
1567 | ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1; | |
1771be90 | 1568 | |
8a6ac287 TB |
1569 | if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED && |
1570 | flags != COMMIT_GRAPH_SPLIT_REPLACE) { | |
fdbde82f TB |
1571 | while (g && (g->num_commits <= size_mult * num_commits || |
1572 | (max_commits && num_commits > max_commits))) { | |
1573 | if (g->odb != ctx->odb) | |
1574 | break; | |
c523035c | 1575 | |
fdbde82f TB |
1576 | num_commits += g->num_commits; |
1577 | g = g->base_graph; | |
1771be90 | 1578 | |
fdbde82f TB |
1579 | ctx->num_commit_graphs_after--; |
1580 | } | |
1771be90 DS |
1581 | } |
1582 | ||
8a6ac287 TB |
1583 | if (flags != COMMIT_GRAPH_SPLIT_REPLACE) |
1584 | ctx->new_base_graph = g; | |
1585 | else if (ctx->num_commit_graphs_after != 1) | |
1586 | BUG("split_graph_merge_strategy: num_commit_graphs_after " | |
1587 | "should be 1 with --split=replace"); | |
1771be90 | 1588 | |
c523035c | 1589 | if (ctx->num_commit_graphs_after == 2) { |
ad2dd5bb | 1590 | char *old_graph_name = get_commit_graph_filename(g->odb); |
c523035c DS |
1591 | |
1592 | if (!strcmp(g->filename, old_graph_name) && | |
ad2dd5bb | 1593 | g->odb != ctx->odb) { |
c523035c DS |
1594 | ctx->num_commit_graphs_after = 1; |
1595 | ctx->new_base_graph = NULL; | |
1596 | } | |
1597 | ||
1598 | free(old_graph_name); | |
1599 | } | |
1600 | ||
b78a556a TB |
1601 | CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after); |
1602 | CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after); | |
1771be90 DS |
1603 | |
1604 | for (i = 0; i < ctx->num_commit_graphs_after && | |
1605 | i < ctx->num_commit_graphs_before; i++) | |
1606 | ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]); | |
1607 | ||
1608 | i = ctx->num_commit_graphs_before - 1; | |
1609 | g = ctx->r->objects->commit_graph; | |
1610 | ||
1611 | while (g) { | |
1612 | if (i < ctx->num_commit_graphs_after) | |
1613 | ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid)); | |
1614 | ||
1615 | i--; | |
1616 | g = g->base_graph; | |
1617 | } | |
1618 | } | |
1619 | ||
1620 | static void merge_commit_graph(struct write_commit_graph_context *ctx, | |
1621 | struct commit_graph *g) | |
1622 | { | |
1623 | uint32_t i; | |
1624 | uint32_t offset = g->num_commits_in_base; | |
1625 | ||
1626 | ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc); | |
1627 | ||
1628 | for (i = 0; i < g->num_commits; i++) { | |
1629 | struct object_id oid; | |
1630 | struct commit *result; | |
1631 | ||
1632 | display_progress(ctx->progress, i + 1); | |
1633 | ||
1634 | load_oid_from_graph(g, i + offset, &oid); | |
1635 | ||
1636 | /* only add commits if they still exist in the repo */ | |
1637 | result = lookup_commit_reference_gently(ctx->r, &oid, 1); | |
1638 | ||
1639 | if (result) { | |
1640 | ctx->commits.list[ctx->commits.nr] = result; | |
1641 | ctx->commits.nr++; | |
1642 | } | |
1643 | } | |
1644 | } | |
1645 | ||
1646 | static int commit_compare(const void *_a, const void *_b) | |
1647 | { | |
1648 | const struct commit *a = *(const struct commit **)_a; | |
1649 | const struct commit *b = *(const struct commit **)_b; | |
1650 | return oidcmp(&a->object.oid, &b->object.oid); | |
1651 | } | |
1652 | ||
1653 | static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx) | |
1654 | { | |
689a146c | 1655 | uint32_t i; |
1771be90 DS |
1656 | |
1657 | if (ctx->report_progress) | |
1658 | ctx->progress = start_delayed_progress( | |
1659 | _("Scanning merged commits"), | |
1660 | ctx->commits.nr); | |
1661 | ||
1662 | QSORT(ctx->commits.list, ctx->commits.nr, commit_compare); | |
1663 | ||
1664 | ctx->num_extra_edges = 0; | |
1665 | for (i = 0; i < ctx->commits.nr; i++) { | |
1666 | display_progress(ctx->progress, i); | |
1667 | ||
1668 | if (i && oideq(&ctx->commits.list[i - 1]->object.oid, | |
1669 | &ctx->commits.list[i]->object.oid)) { | |
1670 | die(_("unexpected duplicate commit id %s"), | |
1671 | oid_to_hex(&ctx->commits.list[i]->object.oid)); | |
1672 | } else { | |
689a146c | 1673 | unsigned int num_parents; |
1771be90 | 1674 | |
689a146c | 1675 | num_parents = commit_list_count(ctx->commits.list[i]->parents); |
1771be90 | 1676 | if (num_parents > 2) |
a35bea40 | 1677 | ctx->num_extra_edges += num_parents - 1; |
1771be90 DS |
1678 | } |
1679 | } | |
1680 | ||
1681 | stop_progress(&ctx->progress); | |
1682 | } | |
1683 | ||
1684 | static void merge_commit_graphs(struct write_commit_graph_context *ctx) | |
1685 | { | |
1686 | struct commit_graph *g = ctx->r->objects->commit_graph; | |
1687 | uint32_t current_graph_number = ctx->num_commit_graphs_before; | |
1688 | struct strbuf progress_title = STRBUF_INIT; | |
1689 | ||
1690 | while (g && current_graph_number >= ctx->num_commit_graphs_after) { | |
1691 | current_graph_number--; | |
1692 | ||
1693 | if (ctx->report_progress) { | |
1694 | strbuf_addstr(&progress_title, _("Merging commit-graph")); | |
1695 | ctx->progress = start_delayed_progress(progress_title.buf, 0); | |
1696 | } | |
1697 | ||
1698 | merge_commit_graph(ctx, g); | |
1699 | stop_progress(&ctx->progress); | |
1700 | strbuf_release(&progress_title); | |
1701 | ||
1702 | g = g->base_graph; | |
1703 | } | |
1704 | ||
1705 | if (g) { | |
1706 | ctx->new_base_graph = g; | |
1707 | ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base; | |
1708 | } | |
1709 | ||
1710 | if (ctx->new_base_graph) | |
1711 | ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename); | |
1712 | ||
1713 | sort_and_scan_merged_commits(ctx); | |
1714 | } | |
1715 | ||
8d84097f DS |
1716 | static void mark_commit_graphs(struct write_commit_graph_context *ctx) |
1717 | { | |
1718 | uint32_t i; | |
1719 | time_t now = time(NULL); | |
1720 | ||
1721 | for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) { | |
1722 | struct stat st; | |
1723 | struct utimbuf updated_time; | |
1724 | ||
1725 | stat(ctx->commit_graph_filenames_before[i], &st); | |
1726 | ||
1727 | updated_time.actime = st.st_atime; | |
1728 | updated_time.modtime = now; | |
1729 | utime(ctx->commit_graph_filenames_before[i], &updated_time); | |
1730 | } | |
1731 | } | |
1732 | ||
1733 | static void expire_commit_graphs(struct write_commit_graph_context *ctx) | |
1734 | { | |
1735 | struct strbuf path = STRBUF_INIT; | |
1736 | DIR *dir; | |
1737 | struct dirent *de; | |
1738 | size_t dirnamelen; | |
c2bc6e6a DS |
1739 | timestamp_t expire_time = time(NULL); |
1740 | ||
1741 | if (ctx->split_opts && ctx->split_opts->expire_time) | |
1742 | expire_time -= ctx->split_opts->expire_time; | |
ba41112a | 1743 | if (!ctx->split) { |
ad2dd5bb | 1744 | char *chain_file_name = get_chain_filename(ctx->odb); |
ba41112a DS |
1745 | unlink(chain_file_name); |
1746 | free(chain_file_name); | |
1747 | ctx->num_commit_graphs_after = 0; | |
1748 | } | |
8d84097f | 1749 | |
0bd52e27 | 1750 | strbuf_addstr(&path, ctx->odb->path); |
8d84097f DS |
1751 | strbuf_addstr(&path, "/info/commit-graphs"); |
1752 | dir = opendir(path.buf); | |
1753 | ||
0aa6bce7 RS |
1754 | if (!dir) |
1755 | goto out; | |
8d84097f DS |
1756 | |
1757 | strbuf_addch(&path, '/'); | |
1758 | dirnamelen = path.len; | |
1759 | while ((de = readdir(dir)) != NULL) { | |
1760 | struct stat st; | |
1761 | uint32_t i, found = 0; | |
1762 | ||
1763 | strbuf_setlen(&path, dirnamelen); | |
1764 | strbuf_addstr(&path, de->d_name); | |
1765 | ||
1766 | stat(path.buf, &st); | |
1767 | ||
1768 | if (st.st_mtime > expire_time) | |
1769 | continue; | |
1770 | if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph")) | |
1771 | continue; | |
1772 | ||
1773 | for (i = 0; i < ctx->num_commit_graphs_after; i++) { | |
1774 | if (!strcmp(ctx->commit_graph_filenames_after[i], | |
1775 | path.buf)) { | |
1776 | found = 1; | |
1777 | break; | |
1778 | } | |
1779 | } | |
1780 | ||
1781 | if (!found) | |
1782 | unlink(path.buf); | |
8d84097f | 1783 | } |
0aa6bce7 RS |
1784 | |
1785 | out: | |
1786 | strbuf_release(&path); | |
8d84097f DS |
1787 | } |
1788 | ||
0bd52e27 | 1789 | int write_commit_graph(struct object_directory *odb, |
238def57 | 1790 | struct string_list *pack_indexes, |
6830c360 | 1791 | struct oidset *commits, |
39d88318 | 1792 | enum commit_graph_write_flags flags, |
c2bc6e6a | 1793 | const struct split_commit_graph_opts *split_opts) |
238def57 DS |
1794 | { |
1795 | struct write_commit_graph_context *ctx; | |
1796 | uint32_t i, count_distinct = 0; | |
e103f727 | 1797 | int res = 0; |
8a6ac287 | 1798 | int replace = 0; |
08fd81c9 | 1799 | |
d6538246 | 1800 | if (!commit_graph_compatible(the_repository)) |
e103f727 | 1801 | return 0; |
d6538246 | 1802 | |
c9905bea DS |
1803 | ctx = xcalloc(1, sizeof(struct write_commit_graph_context)); |
1804 | ctx->r = the_repository; | |
0bd52e27 | 1805 | ctx->odb = odb; |
39d88318 SG |
1806 | ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0; |
1807 | ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0; | |
1808 | ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0; | |
7c5c9b9c | 1809 | ctx->check_oids = flags & COMMIT_GRAPH_WRITE_CHECK_OIDS ? 1 : 0; |
c2bc6e6a | 1810 | ctx->split_opts = split_opts; |
6c622f9f DS |
1811 | |
1812 | if (ctx->split) { | |
1813 | struct commit_graph *g; | |
1814 | prepare_commit_graph(ctx->r); | |
1815 | ||
1816 | g = ctx->r->objects->commit_graph; | |
1817 | ||
1818 | while (g) { | |
1819 | ctx->num_commit_graphs_before++; | |
1820 | g = g->base_graph; | |
1821 | } | |
1822 | ||
1823 | if (ctx->num_commit_graphs_before) { | |
1824 | ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before); | |
1825 | i = ctx->num_commit_graphs_before; | |
1826 | g = ctx->r->objects->commit_graph; | |
1827 | ||
1828 | while (g) { | |
1829 | ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename); | |
1830 | g = g->base_graph; | |
1831 | } | |
1832 | } | |
8a6ac287 TB |
1833 | |
1834 | if (ctx->split_opts) | |
1835 | replace = ctx->split_opts->flags & COMMIT_GRAPH_SPLIT_REPLACE; | |
6c622f9f | 1836 | } |
c9905bea DS |
1837 | |
1838 | ctx->approx_nr_objects = approximate_object_count(); | |
1839 | ctx->oids.alloc = ctx->approx_nr_objects / 32; | |
1840 | ||
c2bc6e6a DS |
1841 | if (ctx->split && split_opts && ctx->oids.alloc > split_opts->max_commits) |
1842 | ctx->oids.alloc = split_opts->max_commits; | |
1771be90 | 1843 | |
c9905bea | 1844 | if (ctx->append) { |
13c24992 | 1845 | prepare_commit_graph_one(ctx->r, ctx->odb); |
c9905bea DS |
1846 | if (ctx->r->objects->commit_graph) |
1847 | ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits; | |
7547b95b DS |
1848 | } |
1849 | ||
c9905bea DS |
1850 | if (ctx->oids.alloc < 1024) |
1851 | ctx->oids.alloc = 1024; | |
1852 | ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc); | |
1853 | ||
1854 | if (ctx->append && ctx->r->objects->commit_graph) { | |
1855 | struct commit_graph *g = ctx->r->objects->commit_graph; | |
1856 | for (i = 0; i < g->num_commits; i++) { | |
1857 | const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i; | |
1858 | hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash); | |
7547b95b DS |
1859 | } |
1860 | } | |
1861 | ||
049d51a2 | 1862 | if (pack_indexes) { |
ef5b83f2 DS |
1863 | if ((res = fill_oids_from_packs(ctx, pack_indexes))) |
1864 | goto cleanup; | |
3d5df01b DS |
1865 | } |
1866 | ||
6830c360 TB |
1867 | if (commits) { |
1868 | if ((res = fill_oids_from_commits(ctx, commits))) | |
7c5c9b9c SG |
1869 | goto cleanup; |
1870 | } | |
3d5df01b | 1871 | |
6830c360 | 1872 | if (!pack_indexes && !commits) |
b2c83060 | 1873 | fill_oids_from_all_packs(ctx); |
049d51a2 | 1874 | |
c9905bea | 1875 | close_reachable(ctx); |
08fd81c9 | 1876 | |
014e3440 | 1877 | count_distinct = count_distinct_commits(ctx); |
08fd81c9 | 1878 | |
e103f727 DS |
1879 | if (count_distinct >= GRAPH_EDGE_LAST_MASK) { |
1880 | error(_("the commit graph format cannot write %d commits"), count_distinct); | |
1881 | res = -1; | |
1882 | goto cleanup; | |
1883 | } | |
08fd81c9 | 1884 | |
c9905bea DS |
1885 | ctx->commits.alloc = count_distinct; |
1886 | ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc); | |
08fd81c9 | 1887 | |
f998d542 | 1888 | copy_oids_to_commits(ctx); |
08fd81c9 | 1889 | |
c9905bea | 1890 | if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) { |
e103f727 DS |
1891 | error(_("too many commits to write graph")); |
1892 | res = -1; | |
1893 | goto cleanup; | |
1894 | } | |
08fd81c9 | 1895 | |
8a6ac287 | 1896 | if (!ctx->commits.nr && !replace) |
6c622f9f DS |
1897 | goto cleanup; |
1898 | ||
1771be90 DS |
1899 | if (ctx->split) { |
1900 | split_graph_merge_strategy(ctx); | |
1901 | ||
8a6ac287 TB |
1902 | if (!replace) |
1903 | merge_commit_graphs(ctx); | |
1771be90 | 1904 | } else |
6c622f9f DS |
1905 | ctx->num_commit_graphs_after = 1; |
1906 | ||
c9905bea | 1907 | compute_generation_numbers(ctx); |
08fd81c9 | 1908 | |
238def57 | 1909 | res = write_commit_graph_file(ctx); |
08fd81c9 | 1910 | |
ba41112a | 1911 | if (ctx->split) |
8d84097f | 1912 | mark_commit_graphs(ctx); |
ba41112a DS |
1913 | |
1914 | expire_commit_graphs(ctx); | |
8d84097f | 1915 | |
e103f727 | 1916 | cleanup: |
238def57 | 1917 | free(ctx->graph_name); |
c9905bea DS |
1918 | free(ctx->commits.list); |
1919 | free(ctx->oids.list); | |
6c622f9f DS |
1920 | |
1921 | if (ctx->commit_graph_filenames_after) { | |
1922 | for (i = 0; i < ctx->num_commit_graphs_after; i++) { | |
1923 | free(ctx->commit_graph_filenames_after[i]); | |
1924 | free(ctx->commit_graph_hash_after[i]); | |
1925 | } | |
1926 | ||
1927 | for (i = 0; i < ctx->num_commit_graphs_before; i++) | |
1928 | free(ctx->commit_graph_filenames_before[i]); | |
1929 | ||
1930 | free(ctx->commit_graph_filenames_after); | |
1931 | free(ctx->commit_graph_filenames_before); | |
1932 | free(ctx->commit_graph_hash_after); | |
1933 | } | |
1934 | ||
c9905bea | 1935 | free(ctx); |
e103f727 DS |
1936 | |
1937 | return res; | |
08fd81c9 | 1938 | } |
283e68c7 | 1939 | |
41df0e30 | 1940 | #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2 |
283e68c7 DS |
1941 | static int verify_commit_graph_error; |
1942 | ||
1943 | static void graph_report(const char *fmt, ...) | |
1944 | { | |
1945 | va_list ap; | |
1946 | ||
1947 | verify_commit_graph_error = 1; | |
1948 | va_start(ap, fmt); | |
1949 | vfprintf(stderr, fmt, ap); | |
1950 | fprintf(stderr, "\n"); | |
1951 | va_end(ap); | |
1952 | } | |
1953 | ||
1373e547 DS |
1954 | #define GENERATION_ZERO_EXISTS 1 |
1955 | #define GENERATION_NUMBER_EXISTS 2 | |
1956 | ||
3da4b609 | 1957 | int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags) |
283e68c7 | 1958 | { |
9bda8467 | 1959 | uint32_t i, cur_fanout_pos = 0; |
41df0e30 | 1960 | struct object_id prev_oid, cur_oid, checksum; |
1373e547 | 1961 | int generation_zero = 0; |
41df0e30 DS |
1962 | struct hashfile *f; |
1963 | int devnull; | |
1f7f557f | 1964 | struct progress *progress = NULL; |
3da4b609 | 1965 | int local_error = 0; |
9bda8467 | 1966 | |
283e68c7 DS |
1967 | if (!g) { |
1968 | graph_report("no commit-graph file loaded"); | |
1969 | return 1; | |
1970 | } | |
1971 | ||
2ac138d5 | 1972 | verify_commit_graph_error = verify_commit_graph_lite(g); |
9bda8467 DS |
1973 | if (verify_commit_graph_error) |
1974 | return verify_commit_graph_error; | |
1975 | ||
41df0e30 DS |
1976 | devnull = open("/dev/null", O_WRONLY); |
1977 | f = hashfd(devnull, NULL); | |
1978 | hashwrite(f, g->data, g->data_len - g->hash_len); | |
1979 | finalize_hashfile(f, checksum.hash, CSUM_CLOSE); | |
67947c34 | 1980 | if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) { |
41df0e30 DS |
1981 | graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt")); |
1982 | verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH; | |
1983 | } | |
1984 | ||
9bda8467 | 1985 | for (i = 0; i < g->num_commits; i++) { |
2e3c0737 DS |
1986 | struct commit *graph_commit; |
1987 | ||
9bda8467 DS |
1988 | hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i); |
1989 | ||
1990 | if (i && oidcmp(&prev_oid, &cur_oid) >= 0) | |
93b4405f | 1991 | graph_report(_("commit-graph has incorrect OID order: %s then %s"), |
9bda8467 DS |
1992 | oid_to_hex(&prev_oid), |
1993 | oid_to_hex(&cur_oid)); | |
1994 | ||
1995 | oidcpy(&prev_oid, &cur_oid); | |
1996 | ||
1997 | while (cur_oid.hash[0] > cur_fanout_pos) { | |
1998 | uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos); | |
1999 | ||
2000 | if (i != fanout_value) | |
93b4405f | 2001 | graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"), |
9bda8467 DS |
2002 | cur_fanout_pos, fanout_value, i); |
2003 | cur_fanout_pos++; | |
2004 | } | |
2e3c0737 | 2005 | |
82952964 | 2006 | graph_commit = lookup_commit(r, &cur_oid); |
4f542b7a | 2007 | if (!parse_commit_in_graph_one(r, g, graph_commit)) |
93b4405f | 2008 | graph_report(_("failed to parse commit %s from commit-graph"), |
2e3c0737 | 2009 | oid_to_hex(&cur_oid)); |
9bda8467 DS |
2010 | } |
2011 | ||
2012 | while (cur_fanout_pos < 256) { | |
2013 | uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos); | |
2014 | ||
2015 | if (g->num_commits != fanout_value) | |
93b4405f | 2016 | graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"), |
9bda8467 DS |
2017 | cur_fanout_pos, fanout_value, i); |
2018 | ||
2019 | cur_fanout_pos++; | |
2020 | } | |
2021 | ||
41df0e30 | 2022 | if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH) |
96af91d4 DS |
2023 | return verify_commit_graph_error; |
2024 | ||
73716122 GS |
2025 | if (flags & COMMIT_GRAPH_WRITE_PROGRESS) |
2026 | progress = start_progress(_("Verifying commits in commit graph"), | |
2027 | g->num_commits); | |
2028 | ||
96af91d4 | 2029 | for (i = 0; i < g->num_commits; i++) { |
2e3c0737 | 2030 | struct commit *graph_commit, *odb_commit; |
53614b13 | 2031 | struct commit_list *graph_parents, *odb_parents; |
1373e547 | 2032 | uint32_t max_generation = 0; |
96af91d4 | 2033 | |
1f7f557f | 2034 | display_progress(progress, i + 1); |
96af91d4 DS |
2035 | hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i); |
2036 | ||
82952964 | 2037 | graph_commit = lookup_commit(r, &cur_oid); |
a378509e | 2038 | odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r)); |
96af91d4 | 2039 | if (parse_commit_internal(odb_commit, 0, 0)) { |
93b4405f | 2040 | graph_report(_("failed to parse commit %s from object database for commit-graph"), |
96af91d4 DS |
2041 | oid_to_hex(&cur_oid)); |
2042 | continue; | |
2043 | } | |
2e3c0737 | 2044 | |
4f542b7a | 2045 | if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid, |
2e3c0737 | 2046 | get_commit_tree_oid(odb_commit))) |
93b4405f | 2047 | graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"), |
2e3c0737 DS |
2048 | oid_to_hex(&cur_oid), |
2049 | oid_to_hex(get_commit_tree_oid(graph_commit)), | |
2050 | oid_to_hex(get_commit_tree_oid(odb_commit))); | |
53614b13 DS |
2051 | |
2052 | graph_parents = graph_commit->parents; | |
2053 | odb_parents = odb_commit->parents; | |
2054 | ||
2055 | while (graph_parents) { | |
2056 | if (odb_parents == NULL) { | |
93b4405f | 2057 | graph_report(_("commit-graph parent list for commit %s is too long"), |
53614b13 DS |
2058 | oid_to_hex(&cur_oid)); |
2059 | break; | |
2060 | } | |
2061 | ||
3da4b609 DS |
2062 | /* parse parent in case it is in a base graph */ |
2063 | parse_commit_in_graph_one(r, g, graph_parents->item); | |
2064 | ||
9001dc2a | 2065 | if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid)) |
93b4405f | 2066 | graph_report(_("commit-graph parent for %s is %s != %s"), |
53614b13 DS |
2067 | oid_to_hex(&cur_oid), |
2068 | oid_to_hex(&graph_parents->item->object.oid), | |
2069 | oid_to_hex(&odb_parents->item->object.oid)); | |
2070 | ||
1373e547 DS |
2071 | if (graph_parents->item->generation > max_generation) |
2072 | max_generation = graph_parents->item->generation; | |
2073 | ||
53614b13 DS |
2074 | graph_parents = graph_parents->next; |
2075 | odb_parents = odb_parents->next; | |
2076 | } | |
2077 | ||
2078 | if (odb_parents != NULL) | |
93b4405f | 2079 | graph_report(_("commit-graph parent list for commit %s terminates early"), |
53614b13 | 2080 | oid_to_hex(&cur_oid)); |
1373e547 DS |
2081 | |
2082 | if (!graph_commit->generation) { | |
2083 | if (generation_zero == GENERATION_NUMBER_EXISTS) | |
93b4405f | 2084 | graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"), |
1373e547 DS |
2085 | oid_to_hex(&cur_oid)); |
2086 | generation_zero = GENERATION_ZERO_EXISTS; | |
2087 | } else if (generation_zero == GENERATION_ZERO_EXISTS) | |
93b4405f | 2088 | graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"), |
1373e547 DS |
2089 | oid_to_hex(&cur_oid)); |
2090 | ||
2091 | if (generation_zero == GENERATION_ZERO_EXISTS) | |
2092 | continue; | |
2093 | ||
2094 | /* | |
2095 | * If one of our parents has generation GENERATION_NUMBER_MAX, then | |
2096 | * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid | |
2097 | * extra logic in the following condition. | |
2098 | */ | |
2099 | if (max_generation == GENERATION_NUMBER_MAX) | |
2100 | max_generation--; | |
2101 | ||
2102 | if (graph_commit->generation != max_generation + 1) | |
93b4405f | 2103 | graph_report(_("commit-graph generation for commit %s is %u != %u"), |
1373e547 DS |
2104 | oid_to_hex(&cur_oid), |
2105 | graph_commit->generation, | |
2106 | max_generation + 1); | |
88968ebf DS |
2107 | |
2108 | if (graph_commit->date != odb_commit->date) | |
93b4405f | 2109 | graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime), |
88968ebf DS |
2110 | oid_to_hex(&cur_oid), |
2111 | graph_commit->date, | |
2112 | odb_commit->date); | |
96af91d4 | 2113 | } |
1f7f557f | 2114 | stop_progress(&progress); |
96af91d4 | 2115 | |
3da4b609 DS |
2116 | local_error = verify_commit_graph_error; |
2117 | ||
2118 | if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph) | |
2119 | local_error |= verify_commit_graph(r, g->base_graph, flags); | |
2120 | ||
2121 | return local_error; | |
283e68c7 | 2122 | } |
c3756d5b JT |
2123 | |
2124 | void free_commit_graph(struct commit_graph *g) | |
2125 | { | |
2126 | if (!g) | |
2127 | return; | |
c8828530 | 2128 | if (g->data) { |
c3756d5b JT |
2129 | munmap((void *)g->data, g->data_len); |
2130 | g->data = NULL; | |
c3756d5b | 2131 | } |
6c622f9f | 2132 | free(g->filename); |
c3756d5b JT |
2133 | free(g); |
2134 | } | |
6abada18 JK |
2135 | |
2136 | void disable_commit_graph(struct repository *r) | |
2137 | { | |
2138 | r->commit_graph_disabled = 1; | |
2139 | } |