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