]> git.ipfire.org Git - thirdparty/git.git/blob - commit-graph.c
Merge branch 'es/add-doc-list-short-form-of-all-in-synopsis'
[thirdparty/git.git] / commit-graph.c
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "gettext.h"
4 #include "hex.h"
5 #include "lockfile.h"
6 #include "pack.h"
7 #include "packfile.h"
8 #include "commit.h"
9 #include "object.h"
10 #include "refs.h"
11 #include "revision.h"
12 #include "hash-lookup.h"
13 #include "commit-graph.h"
14 #include "object-file.h"
15 #include "object-store-ll.h"
16 #include "oid-array.h"
17 #include "path.h"
18 #include "alloc.h"
19 #include "hashmap.h"
20 #include "replace-object.h"
21 #include "progress.h"
22 #include "bloom.h"
23 #include "commit-slab.h"
24 #include "shallow.h"
25 #include "json-writer.h"
26 #include "trace2.h"
27 #include "tree.h"
28 #include "chunk-format.h"
29
30 void git_test_write_commit_graph_or_die(void)
31 {
32 int flags = 0;
33 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
34 return;
35
36 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
37 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
38
39 if (write_commit_graph_reachable(the_repository->objects->odb,
40 flags, NULL))
41 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
42 }
43
44 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
45 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
46 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
47 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
48 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
49 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
50 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
51 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
52 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
53 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
54
55 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
56
57 #define GRAPH_VERSION_1 0x1
58 #define GRAPH_VERSION GRAPH_VERSION_1
59
60 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
61 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
62 #define GRAPH_PARENT_NONE 0x70000000
63
64 #define GRAPH_LAST_EDGE 0x80000000
65
66 #define GRAPH_HEADER_SIZE 8
67 #define GRAPH_FANOUT_SIZE (4 * 256)
68 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
69 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
70
71 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
72
73 /* Remember to update object flag allocation in object.h */
74 #define REACHABLE (1u<<15)
75
76 define_commit_slab(topo_level_slab, uint32_t);
77
78 /* Keep track of the order in which commits are added to our list. */
79 define_commit_slab(commit_pos, int);
80 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
81
82 static void set_commit_pos(struct repository *r, const struct object_id *oid)
83 {
84 static int32_t max_pos;
85 struct commit *commit = lookup_commit(r, oid);
86
87 if (!commit)
88 return; /* should never happen, but be lenient */
89
90 *commit_pos_at(&commit_pos, commit) = max_pos++;
91 }
92
93 static int commit_pos_cmp(const void *va, const void *vb)
94 {
95 const struct commit *a = *(const struct commit **)va;
96 const struct commit *b = *(const struct commit **)vb;
97 return commit_pos_at(&commit_pos, a) -
98 commit_pos_at(&commit_pos, b);
99 }
100
101 define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
102 static struct commit_graph_data_slab commit_graph_data_slab =
103 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
104
105 static int get_configured_generation_version(struct repository *r)
106 {
107 int version = 2;
108 repo_config_get_int(r, "commitgraph.generationversion", &version);
109 return version;
110 }
111
112 uint32_t commit_graph_position(const struct commit *c)
113 {
114 struct commit_graph_data *data =
115 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
116
117 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
118 }
119
120 timestamp_t commit_graph_generation(const struct commit *c)
121 {
122 struct commit_graph_data *data =
123 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
124
125 if (data && data->generation)
126 return data->generation;
127
128 return GENERATION_NUMBER_INFINITY;
129 }
130
131 static timestamp_t commit_graph_generation_from_graph(const struct commit *c)
132 {
133 struct commit_graph_data *data =
134 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
135
136 if (!data || data->graph_pos == COMMIT_NOT_FROM_GRAPH)
137 return GENERATION_NUMBER_INFINITY;
138 return data->generation;
139 }
140
141 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
142 {
143 unsigned int i, nth_slab;
144 struct commit_graph_data *data =
145 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
146
147 if (data)
148 return data;
149
150 nth_slab = c->index / commit_graph_data_slab.slab_size;
151 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
152
153 /*
154 * commit-slab initializes elements with zero, overwrite this with
155 * COMMIT_NOT_FROM_GRAPH for graph_pos.
156 *
157 * We avoid initializing generation with checking if graph position
158 * is not COMMIT_NOT_FROM_GRAPH.
159 */
160 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
161 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
162 COMMIT_NOT_FROM_GRAPH;
163 }
164
165 return data;
166 }
167
168 /*
169 * Should be used only while writing commit-graph as it compares
170 * generation value of commits by directly accessing commit-slab.
171 */
172 static int commit_gen_cmp(const void *va, const void *vb)
173 {
174 const struct commit *a = *(const struct commit **)va;
175 const struct commit *b = *(const struct commit **)vb;
176
177 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
178 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
179 /* lower generation commits first */
180 if (generation_a < generation_b)
181 return -1;
182 else if (generation_a > generation_b)
183 return 1;
184
185 /* use date as a heuristic when generations are equal */
186 if (a->date < b->date)
187 return -1;
188 else if (a->date > b->date)
189 return 1;
190 return 0;
191 }
192
193 char *get_commit_graph_filename(struct object_directory *obj_dir)
194 {
195 return xstrfmt("%s/info/commit-graph", obj_dir->path);
196 }
197
198 static char *get_split_graph_filename(struct object_directory *odb,
199 const char *oid_hex)
200 {
201 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
202 oid_hex);
203 }
204
205 char *get_commit_graph_chain_filename(struct object_directory *odb)
206 {
207 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
208 }
209
210 static struct commit_graph *alloc_commit_graph(void)
211 {
212 struct commit_graph *g = xcalloc(1, sizeof(*g));
213
214 return g;
215 }
216
217 static int commit_graph_compatible(struct repository *r)
218 {
219 if (!r->gitdir)
220 return 0;
221
222 if (replace_refs_enabled(r)) {
223 prepare_replace_object(r);
224 if (hashmap_get_size(&r->objects->replace_map->map))
225 return 0;
226 }
227
228 prepare_commit_graft(r);
229 if (r->parsed_objects &&
230 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
231 return 0;
232 if (is_repository_shallow(r))
233 return 0;
234
235 return 1;
236 }
237
238 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
239 {
240 *fd = git_open(graph_file);
241 if (*fd < 0)
242 return 0;
243 if (fstat(*fd, st)) {
244 close(*fd);
245 return 0;
246 }
247 return 1;
248 }
249
250 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
251 int fd, struct stat *st,
252 struct object_directory *odb)
253 {
254 void *graph_map;
255 size_t graph_size;
256 struct commit_graph *ret;
257
258 graph_size = xsize_t(st->st_size);
259
260 if (graph_size < GRAPH_MIN_SIZE) {
261 close(fd);
262 error(_("commit-graph file is too small"));
263 return NULL;
264 }
265 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
266 close(fd);
267 prepare_repo_settings(r);
268 ret = parse_commit_graph(&r->settings, graph_map, graph_size);
269
270 if (ret)
271 ret->odb = odb;
272 else
273 munmap(graph_map, graph_size);
274
275 return ret;
276 }
277
278 static int graph_read_oid_fanout(const unsigned char *chunk_start,
279 size_t chunk_size, void *data)
280 {
281 struct commit_graph *g = data;
282 int i;
283
284 if (chunk_size != 256 * sizeof(uint32_t))
285 return error(_("commit-graph oid fanout chunk is wrong size"));
286 g->chunk_oid_fanout = (const uint32_t *)chunk_start;
287 g->num_commits = ntohl(g->chunk_oid_fanout[255]);
288
289 for (i = 0; i < 255; i++) {
290 uint32_t oid_fanout1 = ntohl(g->chunk_oid_fanout[i]);
291 uint32_t oid_fanout2 = ntohl(g->chunk_oid_fanout[i + 1]);
292
293 if (oid_fanout1 > oid_fanout2) {
294 error(_("commit-graph fanout values out of order"));
295 return 1;
296 }
297 }
298
299 return 0;
300 }
301
302 static int graph_read_oid_lookup(const unsigned char *chunk_start,
303 size_t chunk_size, void *data)
304 {
305 struct commit_graph *g = data;
306 g->chunk_oid_lookup = chunk_start;
307 if (chunk_size / g->hash_len != g->num_commits)
308 return error(_("commit-graph OID lookup chunk is the wrong size"));
309 return 0;
310 }
311
312 static int graph_read_commit_data(const unsigned char *chunk_start,
313 size_t chunk_size, void *data)
314 {
315 struct commit_graph *g = data;
316 if (chunk_size / GRAPH_DATA_WIDTH != g->num_commits)
317 return error(_("commit-graph commit data chunk is wrong size"));
318 g->chunk_commit_data = chunk_start;
319 return 0;
320 }
321
322 static int graph_read_generation_data(const unsigned char *chunk_start,
323 size_t chunk_size, void *data)
324 {
325 struct commit_graph *g = data;
326 if (chunk_size / sizeof(uint32_t) != g->num_commits)
327 return error(_("commit-graph generations chunk is wrong size"));
328 g->chunk_generation_data = chunk_start;
329 return 0;
330 }
331
332 static int graph_read_bloom_index(const unsigned char *chunk_start,
333 size_t chunk_size, void *data)
334 {
335 struct commit_graph *g = data;
336 if (chunk_size / 4 != g->num_commits) {
337 warning(_("commit-graph changed-path index chunk is too small"));
338 return -1;
339 }
340 g->chunk_bloom_indexes = chunk_start;
341 return 0;
342 }
343
344 static int graph_read_bloom_data(const unsigned char *chunk_start,
345 size_t chunk_size, void *data)
346 {
347 struct commit_graph *g = data;
348 uint32_t hash_version;
349
350 if (chunk_size < BLOOMDATA_CHUNK_HEADER_SIZE) {
351 warning(_("ignoring too-small changed-path chunk"
352 " (%"PRIuMAX" < %"PRIuMAX") in commit-graph file"),
353 (uintmax_t)chunk_size,
354 (uintmax_t)BLOOMDATA_CHUNK_HEADER_SIZE);
355 return -1;
356 }
357
358 g->chunk_bloom_data = chunk_start;
359 g->chunk_bloom_data_size = chunk_size;
360 hash_version = get_be32(chunk_start);
361
362 if (hash_version != 1)
363 return 0;
364
365 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
366 g->bloom_filter_settings->hash_version = hash_version;
367 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
368 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
369 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
370
371 return 0;
372 }
373
374 struct commit_graph *parse_commit_graph(struct repo_settings *s,
375 void *graph_map, size_t graph_size)
376 {
377 const unsigned char *data;
378 struct commit_graph *graph;
379 uint32_t graph_signature;
380 unsigned char graph_version, hash_version;
381 struct chunkfile *cf = NULL;
382
383 if (!graph_map)
384 return NULL;
385
386 if (graph_size < GRAPH_MIN_SIZE)
387 return NULL;
388
389 data = (const unsigned char *)graph_map;
390
391 graph_signature = get_be32(data);
392 if (graph_signature != GRAPH_SIGNATURE) {
393 error(_("commit-graph signature %X does not match signature %X"),
394 graph_signature, GRAPH_SIGNATURE);
395 return NULL;
396 }
397
398 graph_version = *(unsigned char*)(data + 4);
399 if (graph_version != GRAPH_VERSION) {
400 error(_("commit-graph version %X does not match version %X"),
401 graph_version, GRAPH_VERSION);
402 return NULL;
403 }
404
405 hash_version = *(unsigned char*)(data + 5);
406 if (hash_version != oid_version(the_hash_algo)) {
407 error(_("commit-graph hash version %X does not match version %X"),
408 hash_version, oid_version(the_hash_algo));
409 return NULL;
410 }
411
412 graph = alloc_commit_graph();
413
414 graph->hash_len = the_hash_algo->rawsz;
415 graph->num_chunks = *(unsigned char*)(data + 6);
416 graph->data = graph_map;
417 graph->data_len = graph_size;
418
419 if (graph_size < GRAPH_HEADER_SIZE +
420 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
421 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
422 error(_("commit-graph file is too small to hold %u chunks"),
423 graph->num_chunks);
424 free(graph);
425 return NULL;
426 }
427
428 cf = init_chunkfile(NULL);
429
430 if (read_table_of_contents(cf, graph->data, graph_size,
431 GRAPH_HEADER_SIZE, graph->num_chunks, 1))
432 goto free_and_return;
433
434 if (read_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, graph_read_oid_fanout, graph)) {
435 error(_("commit-graph required OID fanout chunk missing or corrupted"));
436 goto free_and_return;
437 }
438 if (read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph)) {
439 error(_("commit-graph required OID lookup chunk missing or corrupted"));
440 goto free_and_return;
441 }
442 if (read_chunk(cf, GRAPH_CHUNKID_DATA, graph_read_commit_data, graph)) {
443 error(_("commit-graph required commit data chunk missing or corrupted"));
444 goto free_and_return;
445 }
446
447 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges,
448 &graph->chunk_extra_edges_size);
449 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs,
450 &graph->chunk_base_graphs_size);
451
452 if (s->commit_graph_generation_version >= 2) {
453 read_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
454 graph_read_generation_data, graph);
455 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
456 &graph->chunk_generation_data_overflow,
457 &graph->chunk_generation_data_overflow_size);
458
459 if (graph->chunk_generation_data)
460 graph->read_generation_data = 1;
461 }
462
463 if (s->commit_graph_read_changed_paths) {
464 read_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
465 graph_read_bloom_index, graph);
466 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
467 graph_read_bloom_data, graph);
468 }
469
470 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
471 init_bloom_filters();
472 } else {
473 /* We need both the bloom chunks to exist together. Else ignore the data */
474 graph->chunk_bloom_indexes = NULL;
475 graph->chunk_bloom_data = NULL;
476 FREE_AND_NULL(graph->bloom_filter_settings);
477 }
478
479 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
480
481 free_chunkfile(cf);
482 return graph;
483
484 free_and_return:
485 free_chunkfile(cf);
486 free(graph->bloom_filter_settings);
487 free(graph);
488 return NULL;
489 }
490
491 static struct commit_graph *load_commit_graph_one(struct repository *r,
492 const char *graph_file,
493 struct object_directory *odb)
494 {
495
496 struct stat st;
497 int fd;
498 struct commit_graph *g;
499 int open_ok = open_commit_graph(graph_file, &fd, &st);
500
501 if (!open_ok)
502 return NULL;
503
504 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
505
506 if (g)
507 g->filename = xstrdup(graph_file);
508
509 return g;
510 }
511
512 static struct commit_graph *load_commit_graph_v1(struct repository *r,
513 struct object_directory *odb)
514 {
515 char *graph_name = get_commit_graph_filename(odb);
516 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
517 free(graph_name);
518
519 return g;
520 }
521
522 /*
523 * returns 1 if and only if all graphs in the chain have
524 * corrected commit dates stored in the generation_data chunk.
525 */
526 static int validate_mixed_generation_chain(struct commit_graph *g)
527 {
528 int read_generation_data = 1;
529 struct commit_graph *p = g;
530
531 while (read_generation_data && p) {
532 read_generation_data = p->read_generation_data;
533 p = p->base_graph;
534 }
535
536 if (read_generation_data)
537 return 1;
538
539 while (g) {
540 g->read_generation_data = 0;
541 g = g->base_graph;
542 }
543
544 return 0;
545 }
546
547 static int add_graph_to_chain(struct commit_graph *g,
548 struct commit_graph *chain,
549 struct object_id *oids,
550 int n)
551 {
552 struct commit_graph *cur_g = chain;
553
554 if (n && !g->chunk_base_graphs) {
555 warning(_("commit-graph has no base graphs chunk"));
556 return 0;
557 }
558
559 if (g->chunk_base_graphs_size / g->hash_len < n) {
560 warning(_("commit-graph base graphs chunk is too small"));
561 return 0;
562 }
563
564 while (n) {
565 n--;
566
567 if (!cur_g ||
568 !oideq(&oids[n], &cur_g->oid) ||
569 !hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_len, n))) {
570 warning(_("commit-graph chain does not match"));
571 return 0;
572 }
573
574 cur_g = cur_g->base_graph;
575 }
576
577 if (chain) {
578 if (unsigned_add_overflows(chain->num_commits,
579 chain->num_commits_in_base)) {
580 warning(_("commit count in base graph too high: %"PRIuMAX),
581 (uintmax_t)chain->num_commits_in_base);
582 return 0;
583 }
584 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
585 }
586
587 g->base_graph = chain;
588
589 return 1;
590 }
591
592 int open_commit_graph_chain(const char *chain_file,
593 int *fd, struct stat *st)
594 {
595 *fd = git_open(chain_file);
596 if (*fd < 0)
597 return 0;
598 if (fstat(*fd, st)) {
599 close(*fd);
600 return 0;
601 }
602 if (st->st_size < the_hash_algo->hexsz) {
603 close(*fd);
604 if (!st->st_size) {
605 /* treat empty files the same as missing */
606 errno = ENOENT;
607 } else {
608 warning(_("commit-graph chain file too small"));
609 errno = EINVAL;
610 }
611 return 0;
612 }
613 return 1;
614 }
615
616 struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
617 int fd, struct stat *st,
618 int *incomplete_chain)
619 {
620 struct commit_graph *graph_chain = NULL;
621 struct strbuf line = STRBUF_INIT;
622 struct object_id *oids;
623 int i = 0, valid = 1, count;
624 FILE *fp = xfdopen(fd, "r");
625
626 count = st->st_size / (the_hash_algo->hexsz + 1);
627 CALLOC_ARRAY(oids, count);
628
629 prepare_alt_odb(r);
630
631 for (i = 0; i < count; i++) {
632 struct object_directory *odb;
633
634 if (strbuf_getline_lf(&line, fp) == EOF)
635 break;
636
637 if (get_oid_hex(line.buf, &oids[i])) {
638 warning(_("invalid commit-graph chain: line '%s' not a hash"),
639 line.buf);
640 valid = 0;
641 break;
642 }
643
644 valid = 0;
645 for (odb = r->objects->odb; odb; odb = odb->next) {
646 char *graph_name = get_split_graph_filename(odb, line.buf);
647 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
648
649 free(graph_name);
650
651 if (g) {
652 if (add_graph_to_chain(g, graph_chain, oids, i)) {
653 graph_chain = g;
654 valid = 1;
655 } else {
656 free_commit_graph(g);
657 }
658
659 break;
660 }
661 }
662
663 if (!valid) {
664 warning(_("unable to find all commit-graph files"));
665 break;
666 }
667 }
668
669 validate_mixed_generation_chain(graph_chain);
670
671 free(oids);
672 fclose(fp);
673 strbuf_release(&line);
674
675 *incomplete_chain = !valid;
676 return graph_chain;
677 }
678
679 static struct commit_graph *load_commit_graph_chain(struct repository *r,
680 struct object_directory *odb)
681 {
682 char *chain_file = get_commit_graph_chain_filename(odb);
683 struct stat st;
684 int fd;
685 struct commit_graph *g = NULL;
686
687 if (open_commit_graph_chain(chain_file, &fd, &st)) {
688 int incomplete;
689 /* ownership of fd is taken over by load function */
690 g = load_commit_graph_chain_fd_st(r, fd, &st, &incomplete);
691 }
692
693 free(chain_file);
694 return g;
695 }
696
697 struct commit_graph *read_commit_graph_one(struct repository *r,
698 struct object_directory *odb)
699 {
700 struct commit_graph *g = load_commit_graph_v1(r, odb);
701
702 if (!g)
703 g = load_commit_graph_chain(r, odb);
704
705 return g;
706 }
707
708 static void prepare_commit_graph_one(struct repository *r,
709 struct object_directory *odb)
710 {
711
712 if (r->objects->commit_graph)
713 return;
714
715 r->objects->commit_graph = read_commit_graph_one(r, odb);
716 }
717
718 /*
719 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
720 *
721 * On the first invocation, this function attempts to load the commit
722 * graph if the_repository is configured to have one.
723 */
724 static int prepare_commit_graph(struct repository *r)
725 {
726 struct object_directory *odb;
727
728 /*
729 * Early return if there is no git dir or if the commit graph is
730 * disabled.
731 *
732 * This must come before the "already attempted?" check below, because
733 * we want to disable even an already-loaded graph file.
734 */
735 if (!r->gitdir || r->commit_graph_disabled)
736 return 0;
737
738 if (r->objects->commit_graph_attempted)
739 return !!r->objects->commit_graph;
740 r->objects->commit_graph_attempted = 1;
741
742 prepare_repo_settings(r);
743
744 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
745 r->settings.core_commit_graph != 1)
746 /*
747 * This repository is not configured to use commit graphs, so
748 * do not load one. (But report commit_graph_attempted anyway
749 * so that commit graph loading is not attempted again for this
750 * repository.)
751 */
752 return 0;
753
754 if (!commit_graph_compatible(r))
755 return 0;
756
757 prepare_alt_odb(r);
758 for (odb = r->objects->odb;
759 !r->objects->commit_graph && odb;
760 odb = odb->next)
761 prepare_commit_graph_one(r, odb);
762 return !!r->objects->commit_graph;
763 }
764
765 int generation_numbers_enabled(struct repository *r)
766 {
767 uint32_t first_generation;
768 struct commit_graph *g;
769 if (!prepare_commit_graph(r))
770 return 0;
771
772 g = r->objects->commit_graph;
773
774 if (!g->num_commits)
775 return 0;
776
777 first_generation = get_be32(g->chunk_commit_data +
778 g->hash_len + 8) >> 2;
779
780 return !!first_generation;
781 }
782
783 int corrected_commit_dates_enabled(struct repository *r)
784 {
785 struct commit_graph *g;
786 if (!prepare_commit_graph(r))
787 return 0;
788
789 g = r->objects->commit_graph;
790
791 if (!g->num_commits)
792 return 0;
793
794 return g->read_generation_data;
795 }
796
797 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
798 {
799 struct commit_graph *g = r->objects->commit_graph;
800 while (g) {
801 if (g->bloom_filter_settings)
802 return g->bloom_filter_settings;
803 g = g->base_graph;
804 }
805 return NULL;
806 }
807
808 void close_commit_graph(struct raw_object_store *o)
809 {
810 clear_commit_graph_data_slab(&commit_graph_data_slab);
811 free_commit_graph(o->commit_graph);
812 o->commit_graph = NULL;
813 }
814
815 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
816 {
817 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
818 g->chunk_oid_lookup, g->hash_len, pos);
819 }
820
821 static void load_oid_from_graph(struct commit_graph *g,
822 uint32_t pos,
823 struct object_id *oid)
824 {
825 uint32_t lex_index;
826
827 while (g && pos < g->num_commits_in_base)
828 g = g->base_graph;
829
830 if (!g)
831 BUG("NULL commit-graph");
832
833 if (pos >= g->num_commits + g->num_commits_in_base)
834 die(_("invalid commit position. commit-graph is likely corrupt"));
835
836 lex_index = pos - g->num_commits_in_base;
837
838 oidread(oid, g->chunk_oid_lookup + st_mult(g->hash_len, lex_index));
839 }
840
841 static struct commit_list **insert_parent_or_die(struct repository *r,
842 struct commit_graph *g,
843 uint32_t pos,
844 struct commit_list **pptr)
845 {
846 struct commit *c;
847 struct object_id oid;
848
849 if (pos >= g->num_commits + g->num_commits_in_base)
850 die("invalid parent position %"PRIu32, pos);
851
852 load_oid_from_graph(g, pos, &oid);
853 c = lookup_commit(r, &oid);
854 if (!c)
855 die(_("could not find commit %s"), oid_to_hex(&oid));
856 commit_graph_data_at(c)->graph_pos = pos;
857 return &commit_list_insert(c, pptr)->next;
858 }
859
860 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
861 {
862 const unsigned char *commit_data;
863 struct commit_graph_data *graph_data;
864 uint32_t lex_index, offset_pos;
865 uint64_t date_high, date_low, offset;
866
867 while (pos < g->num_commits_in_base)
868 g = g->base_graph;
869
870 if (pos >= g->num_commits + g->num_commits_in_base)
871 die(_("invalid commit position. commit-graph is likely corrupt"));
872
873 lex_index = pos - g->num_commits_in_base;
874 commit_data = g->chunk_commit_data + st_mult(GRAPH_DATA_WIDTH, lex_index);
875
876 graph_data = commit_graph_data_at(item);
877 graph_data->graph_pos = pos;
878
879 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
880 date_low = get_be32(commit_data + g->hash_len + 12);
881 item->date = (timestamp_t)((date_high << 32) | date_low);
882
883 if (g->read_generation_data) {
884 offset = (timestamp_t)get_be32(g->chunk_generation_data + st_mult(sizeof(uint32_t), lex_index));
885
886 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
887 if (!g->chunk_generation_data_overflow)
888 die(_("commit-graph requires overflow generation data but has none"));
889
890 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
891 if (g->chunk_generation_data_overflow_size / sizeof(uint64_t) <= offset_pos)
892 die(_("commit-graph overflow generation data is too small"));
893 graph_data->generation = item->date +
894 get_be64(g->chunk_generation_data_overflow + sizeof(uint64_t) * offset_pos);
895 } else
896 graph_data->generation = item->date + offset;
897 } else
898 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
899
900 if (g->topo_levels)
901 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
902 }
903
904 static inline void set_commit_tree(struct commit *c, struct tree *t)
905 {
906 c->maybe_tree = t;
907 }
908
909 static int fill_commit_in_graph(struct repository *r,
910 struct commit *item,
911 struct commit_graph *g, uint32_t pos)
912 {
913 uint32_t edge_value;
914 uint32_t parent_data_pos;
915 struct commit_list **pptr;
916 const unsigned char *commit_data;
917 uint32_t lex_index;
918
919 while (pos < g->num_commits_in_base)
920 g = g->base_graph;
921
922 fill_commit_graph_info(item, g, pos);
923
924 lex_index = pos - g->num_commits_in_base;
925 commit_data = g->chunk_commit_data + st_mult(g->hash_len + 16, lex_index);
926
927 item->object.parsed = 1;
928
929 set_commit_tree(item, NULL);
930
931 pptr = &item->parents;
932
933 edge_value = get_be32(commit_data + g->hash_len);
934 if (edge_value == GRAPH_PARENT_NONE)
935 return 1;
936 pptr = insert_parent_or_die(r, g, edge_value, pptr);
937
938 edge_value = get_be32(commit_data + g->hash_len + 4);
939 if (edge_value == GRAPH_PARENT_NONE)
940 return 1;
941 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
942 pptr = insert_parent_or_die(r, g, edge_value, pptr);
943 return 1;
944 }
945
946 parent_data_pos = edge_value & GRAPH_EDGE_LAST_MASK;
947 do {
948 if (g->chunk_extra_edges_size / sizeof(uint32_t) <= parent_data_pos) {
949 error(_("commit-graph extra-edges pointer out of bounds"));
950 free_commit_list(item->parents);
951 item->parents = NULL;
952 item->object.parsed = 0;
953 return 0;
954 }
955 edge_value = get_be32(g->chunk_extra_edges +
956 sizeof(uint32_t) * parent_data_pos);
957 pptr = insert_parent_or_die(r, g,
958 edge_value & GRAPH_EDGE_LAST_MASK,
959 pptr);
960 parent_data_pos++;
961 } while (!(edge_value & GRAPH_LAST_EDGE));
962
963 return 1;
964 }
965
966 static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
967 {
968 struct commit_graph *cur_g = g;
969 uint32_t lex_index;
970
971 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
972 cur_g = cur_g->base_graph;
973
974 if (cur_g) {
975 *pos = lex_index + cur_g->num_commits_in_base;
976 return 1;
977 }
978
979 return 0;
980 }
981
982 static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
983 {
984 uint32_t graph_pos = commit_graph_position(item);
985 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
986 *pos = graph_pos;
987 return 1;
988 } else {
989 return search_commit_pos_in_graph(&item->object.oid, g, pos);
990 }
991 }
992
993 int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
994 uint32_t *pos)
995 {
996 if (!prepare_commit_graph(r))
997 return 0;
998 return find_commit_pos_in_graph(c, r->objects->commit_graph, pos);
999 }
1000
1001 struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
1002 {
1003 static int commit_graph_paranoia = -1;
1004 struct commit *commit;
1005 uint32_t pos;
1006
1007 if (commit_graph_paranoia == -1)
1008 commit_graph_paranoia = git_env_bool(GIT_COMMIT_GRAPH_PARANOIA, 0);
1009
1010 if (!prepare_commit_graph(repo))
1011 return NULL;
1012 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
1013 return NULL;
1014 if (commit_graph_paranoia && !has_object(repo, id, 0))
1015 return NULL;
1016
1017 commit = lookup_commit(repo, id);
1018 if (!commit)
1019 return NULL;
1020 if (commit->object.parsed)
1021 return commit;
1022
1023 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
1024 return NULL;
1025
1026 return commit;
1027 }
1028
1029 static int parse_commit_in_graph_one(struct repository *r,
1030 struct commit_graph *g,
1031 struct commit *item)
1032 {
1033 uint32_t pos;
1034
1035 if (item->object.parsed)
1036 return 1;
1037
1038 if (find_commit_pos_in_graph(item, g, &pos))
1039 return fill_commit_in_graph(r, item, g, pos);
1040
1041 return 0;
1042 }
1043
1044 int parse_commit_in_graph(struct repository *r, struct commit *item)
1045 {
1046 static int checked_env = 0;
1047
1048 if (!checked_env &&
1049 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
1050 die("dying as requested by the '%s' variable on commit-graph parse!",
1051 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
1052 checked_env = 1;
1053
1054 if (!prepare_commit_graph(r))
1055 return 0;
1056 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
1057 }
1058
1059 void load_commit_graph_info(struct repository *r, struct commit *item)
1060 {
1061 uint32_t pos;
1062 if (repo_find_commit_pos_in_graph(r, item, &pos))
1063 fill_commit_graph_info(item, r->objects->commit_graph, pos);
1064 }
1065
1066 static struct tree *load_tree_for_commit(struct repository *r,
1067 struct commit_graph *g,
1068 struct commit *c)
1069 {
1070 struct object_id oid;
1071 const unsigned char *commit_data;
1072 uint32_t graph_pos = commit_graph_position(c);
1073
1074 while (graph_pos < g->num_commits_in_base)
1075 g = g->base_graph;
1076
1077 commit_data = g->chunk_commit_data +
1078 st_mult(GRAPH_DATA_WIDTH, graph_pos - g->num_commits_in_base);
1079
1080 oidread(&oid, commit_data);
1081 set_commit_tree(c, lookup_tree(r, &oid));
1082
1083 return c->maybe_tree;
1084 }
1085
1086 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
1087 struct commit_graph *g,
1088 const struct commit *c)
1089 {
1090 if (c->maybe_tree)
1091 return c->maybe_tree;
1092 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
1093 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
1094
1095 return load_tree_for_commit(r, g, (struct commit *)c);
1096 }
1097
1098 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
1099 {
1100 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
1101 }
1102
1103 struct packed_commit_list {
1104 struct commit **list;
1105 size_t nr;
1106 size_t alloc;
1107 };
1108
1109 struct write_commit_graph_context {
1110 struct repository *r;
1111 struct object_directory *odb;
1112 char *graph_name;
1113 struct oid_array oids;
1114 struct packed_commit_list commits;
1115 int num_extra_edges;
1116 int num_generation_data_overflows;
1117 unsigned long approx_nr_objects;
1118 struct progress *progress;
1119 int progress_done;
1120 uint64_t progress_cnt;
1121
1122 char *base_graph_name;
1123 int num_commit_graphs_before;
1124 int num_commit_graphs_after;
1125 char **commit_graph_filenames_before;
1126 char **commit_graph_filenames_after;
1127 char **commit_graph_hash_after;
1128 uint32_t new_num_commits_in_base;
1129 struct commit_graph *new_base_graph;
1130
1131 unsigned append:1,
1132 report_progress:1,
1133 split:1,
1134 changed_paths:1,
1135 order_by_pack:1,
1136 write_generation_data:1,
1137 trust_generation_numbers:1;
1138
1139 struct topo_level_slab *topo_levels;
1140 const struct commit_graph_opts *opts;
1141 size_t total_bloom_filter_data_size;
1142 const struct bloom_filter_settings *bloom_settings;
1143
1144 int count_bloom_filter_computed;
1145 int count_bloom_filter_not_computed;
1146 int count_bloom_filter_trunc_empty;
1147 int count_bloom_filter_trunc_large;
1148 };
1149
1150 static int write_graph_chunk_fanout(struct hashfile *f,
1151 void *data)
1152 {
1153 struct write_commit_graph_context *ctx = data;
1154 int i, count = 0;
1155 struct commit **list = ctx->commits.list;
1156
1157 /*
1158 * Write the first-level table (the list is sorted,
1159 * but we use a 256-entry lookup to be able to avoid
1160 * having to do eight extra binary search iterations).
1161 */
1162 for (i = 0; i < 256; i++) {
1163 while (count < ctx->commits.nr) {
1164 if ((*list)->object.oid.hash[0] != i)
1165 break;
1166 display_progress(ctx->progress, ++ctx->progress_cnt);
1167 count++;
1168 list++;
1169 }
1170
1171 hashwrite_be32(f, count);
1172 }
1173
1174 return 0;
1175 }
1176
1177 static int write_graph_chunk_oids(struct hashfile *f,
1178 void *data)
1179 {
1180 struct write_commit_graph_context *ctx = data;
1181 struct commit **list = ctx->commits.list;
1182 int count;
1183 for (count = 0; count < ctx->commits.nr; count++, list++) {
1184 display_progress(ctx->progress, ++ctx->progress_cnt);
1185 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1186 }
1187
1188 return 0;
1189 }
1190
1191 static const struct object_id *commit_to_oid(size_t index, const void *table)
1192 {
1193 const struct commit * const *commits = table;
1194 return &commits[index]->object.oid;
1195 }
1196
1197 static int write_graph_chunk_data(struct hashfile *f,
1198 void *data)
1199 {
1200 struct write_commit_graph_context *ctx = data;
1201 struct commit **list = ctx->commits.list;
1202 struct commit **last = ctx->commits.list + ctx->commits.nr;
1203 uint32_t num_extra_edges = 0;
1204
1205 while (list < last) {
1206 struct commit_list *parent;
1207 struct object_id *tree;
1208 int edge_value;
1209 uint32_t packedDate[2];
1210 display_progress(ctx->progress, ++ctx->progress_cnt);
1211
1212 if (repo_parse_commit_no_graph(ctx->r, *list))
1213 die(_("unable to parse commit %s"),
1214 oid_to_hex(&(*list)->object.oid));
1215 tree = get_commit_tree_oid(*list);
1216 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1217
1218 parent = (*list)->parents;
1219
1220 if (!parent)
1221 edge_value = GRAPH_PARENT_NONE;
1222 else {
1223 edge_value = oid_pos(&parent->item->object.oid,
1224 ctx->commits.list,
1225 ctx->commits.nr,
1226 commit_to_oid);
1227
1228 if (edge_value >= 0)
1229 edge_value += ctx->new_num_commits_in_base;
1230 else if (ctx->new_base_graph) {
1231 uint32_t pos;
1232 if (find_commit_pos_in_graph(parent->item,
1233 ctx->new_base_graph,
1234 &pos))
1235 edge_value = pos;
1236 }
1237
1238 if (edge_value < 0)
1239 BUG("missing parent %s for commit %s",
1240 oid_to_hex(&parent->item->object.oid),
1241 oid_to_hex(&(*list)->object.oid));
1242 }
1243
1244 hashwrite_be32(f, edge_value);
1245
1246 if (parent)
1247 parent = parent->next;
1248
1249 if (!parent)
1250 edge_value = GRAPH_PARENT_NONE;
1251 else if (parent->next)
1252 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1253 else {
1254 edge_value = oid_pos(&parent->item->object.oid,
1255 ctx->commits.list,
1256 ctx->commits.nr,
1257 commit_to_oid);
1258
1259 if (edge_value >= 0)
1260 edge_value += ctx->new_num_commits_in_base;
1261 else if (ctx->new_base_graph) {
1262 uint32_t pos;
1263 if (find_commit_pos_in_graph(parent->item,
1264 ctx->new_base_graph,
1265 &pos))
1266 edge_value = pos;
1267 }
1268
1269 if (edge_value < 0)
1270 BUG("missing parent %s for commit %s",
1271 oid_to_hex(&parent->item->object.oid),
1272 oid_to_hex(&(*list)->object.oid));
1273 }
1274
1275 hashwrite_be32(f, edge_value);
1276
1277 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1278 do {
1279 num_extra_edges++;
1280 parent = parent->next;
1281 } while (parent);
1282 }
1283
1284 if (sizeof((*list)->date) > 4)
1285 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1286 else
1287 packedDate[0] = 0;
1288
1289 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1290
1291 packedDate[1] = htonl((*list)->date);
1292 hashwrite(f, packedDate, 8);
1293
1294 list++;
1295 }
1296
1297 return 0;
1298 }
1299
1300 static int write_graph_chunk_generation_data(struct hashfile *f,
1301 void *data)
1302 {
1303 struct write_commit_graph_context *ctx = data;
1304 int i, num_generation_data_overflows = 0;
1305
1306 for (i = 0; i < ctx->commits.nr; i++) {
1307 struct commit *c = ctx->commits.list[i];
1308 timestamp_t offset;
1309 repo_parse_commit(ctx->r, c);
1310 offset = commit_graph_data_at(c)->generation - c->date;
1311 display_progress(ctx->progress, ++ctx->progress_cnt);
1312
1313 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1314 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1315 num_generation_data_overflows++;
1316 }
1317
1318 hashwrite_be32(f, offset);
1319 }
1320
1321 return 0;
1322 }
1323
1324 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1325 void *data)
1326 {
1327 struct write_commit_graph_context *ctx = data;
1328 int i;
1329 for (i = 0; i < ctx->commits.nr; i++) {
1330 struct commit *c = ctx->commits.list[i];
1331 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1332 display_progress(ctx->progress, ++ctx->progress_cnt);
1333
1334 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1335 hashwrite_be32(f, offset >> 32);
1336 hashwrite_be32(f, (uint32_t) offset);
1337 }
1338 }
1339
1340 return 0;
1341 }
1342
1343 static int write_graph_chunk_extra_edges(struct hashfile *f,
1344 void *data)
1345 {
1346 struct write_commit_graph_context *ctx = data;
1347 struct commit **list = ctx->commits.list;
1348 struct commit **last = ctx->commits.list + ctx->commits.nr;
1349 struct commit_list *parent;
1350
1351 while (list < last) {
1352 int num_parents = 0;
1353
1354 display_progress(ctx->progress, ++ctx->progress_cnt);
1355
1356 for (parent = (*list)->parents; num_parents < 3 && parent;
1357 parent = parent->next)
1358 num_parents++;
1359
1360 if (num_parents <= 2) {
1361 list++;
1362 continue;
1363 }
1364
1365 /* Since num_parents > 2, this initializer is safe. */
1366 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1367 int edge_value = oid_pos(&parent->item->object.oid,
1368 ctx->commits.list,
1369 ctx->commits.nr,
1370 commit_to_oid);
1371
1372 if (edge_value >= 0)
1373 edge_value += ctx->new_num_commits_in_base;
1374 else if (ctx->new_base_graph) {
1375 uint32_t pos;
1376 if (find_commit_pos_in_graph(parent->item,
1377 ctx->new_base_graph,
1378 &pos))
1379 edge_value = pos;
1380 }
1381
1382 if (edge_value < 0)
1383 BUG("missing parent %s for commit %s",
1384 oid_to_hex(&parent->item->object.oid),
1385 oid_to_hex(&(*list)->object.oid));
1386 else if (!parent->next)
1387 edge_value |= GRAPH_LAST_EDGE;
1388
1389 hashwrite_be32(f, edge_value);
1390 }
1391
1392 list++;
1393 }
1394
1395 return 0;
1396 }
1397
1398 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1399 void *data)
1400 {
1401 struct write_commit_graph_context *ctx = data;
1402 struct commit **list = ctx->commits.list;
1403 struct commit **last = ctx->commits.list + ctx->commits.nr;
1404 uint32_t cur_pos = 0;
1405
1406 while (list < last) {
1407 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1408 size_t len = filter ? filter->len : 0;
1409 cur_pos += len;
1410 display_progress(ctx->progress, ++ctx->progress_cnt);
1411 hashwrite_be32(f, cur_pos);
1412 list++;
1413 }
1414
1415 return 0;
1416 }
1417
1418 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1419 {
1420 struct json_writer jw = JSON_WRITER_INIT;
1421
1422 jw_object_begin(&jw, 0);
1423 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1424 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1425 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1426 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1427 jw_end(&jw);
1428
1429 trace2_data_json("bloom", ctx->r, "settings", &jw);
1430
1431 jw_release(&jw);
1432 }
1433
1434 static int write_graph_chunk_bloom_data(struct hashfile *f,
1435 void *data)
1436 {
1437 struct write_commit_graph_context *ctx = data;
1438 struct commit **list = ctx->commits.list;
1439 struct commit **last = ctx->commits.list + ctx->commits.nr;
1440
1441 trace2_bloom_filter_settings(ctx);
1442
1443 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1444 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1445 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1446
1447 while (list < last) {
1448 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1449 size_t len = filter ? filter->len : 0;
1450
1451 display_progress(ctx->progress, ++ctx->progress_cnt);
1452 if (len)
1453 hashwrite(f, filter->data, len * sizeof(unsigned char));
1454 list++;
1455 }
1456
1457 return 0;
1458 }
1459
1460 static int add_packed_commits(const struct object_id *oid,
1461 struct packed_git *pack,
1462 uint32_t pos,
1463 void *data)
1464 {
1465 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1466 enum object_type type;
1467 off_t offset = nth_packed_object_offset(pack, pos);
1468 struct object_info oi = OBJECT_INFO_INIT;
1469
1470 if (ctx->progress)
1471 display_progress(ctx->progress, ++ctx->progress_done);
1472
1473 oi.typep = &type;
1474 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1475 die(_("unable to get type of object %s"), oid_to_hex(oid));
1476
1477 if (type != OBJ_COMMIT)
1478 return 0;
1479
1480 oid_array_append(&ctx->oids, oid);
1481 set_commit_pos(ctx->r, oid);
1482
1483 return 0;
1484 }
1485
1486 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1487 {
1488 struct commit_list *parent;
1489 for (parent = commit->parents; parent; parent = parent->next) {
1490 if (!(parent->item->object.flags & REACHABLE)) {
1491 oid_array_append(&ctx->oids, &parent->item->object.oid);
1492 parent->item->object.flags |= REACHABLE;
1493 }
1494 }
1495 }
1496
1497 static void close_reachable(struct write_commit_graph_context *ctx)
1498 {
1499 int i;
1500 struct commit *commit;
1501 enum commit_graph_split_flags flags = ctx->opts ?
1502 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1503
1504 if (ctx->report_progress)
1505 ctx->progress = start_delayed_progress(
1506 _("Loading known commits in commit graph"),
1507 ctx->oids.nr);
1508 for (i = 0; i < ctx->oids.nr; i++) {
1509 display_progress(ctx->progress, i + 1);
1510 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1511 if (commit)
1512 commit->object.flags |= REACHABLE;
1513 }
1514 stop_progress(&ctx->progress);
1515
1516 /*
1517 * As this loop runs, ctx->oids.nr may grow, but not more
1518 * than the number of missing commits in the reachable
1519 * closure.
1520 */
1521 if (ctx->report_progress)
1522 ctx->progress = start_delayed_progress(
1523 _("Expanding reachable commits in commit graph"),
1524 0);
1525 for (i = 0; i < ctx->oids.nr; i++) {
1526 display_progress(ctx->progress, i + 1);
1527 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1528
1529 if (!commit)
1530 continue;
1531 if (ctx->split) {
1532 if ((!repo_parse_commit(ctx->r, commit) &&
1533 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1534 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1535 add_missing_parents(ctx, commit);
1536 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1537 add_missing_parents(ctx, commit);
1538 }
1539 stop_progress(&ctx->progress);
1540
1541 if (ctx->report_progress)
1542 ctx->progress = start_delayed_progress(
1543 _("Clearing commit marks in commit graph"),
1544 ctx->oids.nr);
1545 for (i = 0; i < ctx->oids.nr; i++) {
1546 display_progress(ctx->progress, i + 1);
1547 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1548
1549 if (commit)
1550 commit->object.flags &= ~REACHABLE;
1551 }
1552 stop_progress(&ctx->progress);
1553 }
1554
1555 struct compute_generation_info {
1556 struct repository *r;
1557 struct packed_commit_list *commits;
1558 struct progress *progress;
1559 int progress_cnt;
1560
1561 timestamp_t (*get_generation)(struct commit *c, void *data);
1562 void (*set_generation)(struct commit *c, timestamp_t gen, void *data);
1563 void *data;
1564 };
1565
1566 static timestamp_t compute_generation_from_max(struct commit *c,
1567 timestamp_t max_gen,
1568 int generation_version)
1569 {
1570 switch (generation_version) {
1571 case 1: /* topological levels */
1572 if (max_gen > GENERATION_NUMBER_V1_MAX - 1)
1573 max_gen = GENERATION_NUMBER_V1_MAX - 1;
1574 return max_gen + 1;
1575
1576 case 2: /* corrected commit date */
1577 if (c->date && c->date > max_gen)
1578 max_gen = c->date - 1;
1579 return max_gen + 1;
1580
1581 default:
1582 BUG("attempting unimplemented version");
1583 }
1584 }
1585
1586 static void compute_reachable_generation_numbers(
1587 struct compute_generation_info *info,
1588 int generation_version)
1589 {
1590 int i;
1591 struct commit_list *list = NULL;
1592
1593 for (i = 0; i < info->commits->nr; i++) {
1594 struct commit *c = info->commits->list[i];
1595 timestamp_t gen;
1596 repo_parse_commit(info->r, c);
1597 gen = info->get_generation(c, info->data);
1598 display_progress(info->progress, info->progress_cnt + 1);
1599
1600 if (gen != GENERATION_NUMBER_ZERO && gen != GENERATION_NUMBER_INFINITY)
1601 continue;
1602
1603 commit_list_insert(c, &list);
1604 while (list) {
1605 struct commit *current = list->item;
1606 struct commit_list *parent;
1607 int all_parents_computed = 1;
1608 uint32_t max_gen = 0;
1609
1610 for (parent = current->parents; parent; parent = parent->next) {
1611 repo_parse_commit(info->r, parent->item);
1612 gen = info->get_generation(parent->item, info->data);
1613
1614 if (gen == GENERATION_NUMBER_ZERO) {
1615 all_parents_computed = 0;
1616 commit_list_insert(parent->item, &list);
1617 break;
1618 }
1619
1620 if (gen > max_gen)
1621 max_gen = gen;
1622 }
1623
1624 if (all_parents_computed) {
1625 pop_commit(&list);
1626 gen = compute_generation_from_max(
1627 current, max_gen,
1628 generation_version);
1629 info->set_generation(current, gen, info->data);
1630 }
1631 }
1632 }
1633 }
1634
1635 static timestamp_t get_topo_level(struct commit *c, void *data)
1636 {
1637 struct write_commit_graph_context *ctx = data;
1638 return *topo_level_slab_at(ctx->topo_levels, c);
1639 }
1640
1641 static void set_topo_level(struct commit *c, timestamp_t t, void *data)
1642 {
1643 struct write_commit_graph_context *ctx = data;
1644 *topo_level_slab_at(ctx->topo_levels, c) = (uint32_t)t;
1645 }
1646
1647 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1648 {
1649 struct compute_generation_info info = {
1650 .r = ctx->r,
1651 .commits = &ctx->commits,
1652 .get_generation = get_topo_level,
1653 .set_generation = set_topo_level,
1654 .data = ctx,
1655 };
1656
1657 if (ctx->report_progress)
1658 info.progress = ctx->progress
1659 = start_delayed_progress(
1660 _("Computing commit graph topological levels"),
1661 ctx->commits.nr);
1662
1663 compute_reachable_generation_numbers(&info, 1);
1664
1665 stop_progress(&ctx->progress);
1666 }
1667
1668 static timestamp_t get_generation_from_graph_data(struct commit *c,
1669 void *data UNUSED)
1670 {
1671 return commit_graph_data_at(c)->generation;
1672 }
1673
1674 static void set_generation_v2(struct commit *c, timestamp_t t,
1675 void *data UNUSED)
1676 {
1677 struct commit_graph_data *g = commit_graph_data_at(c);
1678 g->generation = t;
1679 }
1680
1681 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1682 {
1683 int i;
1684 struct compute_generation_info info = {
1685 .r = ctx->r,
1686 .commits = &ctx->commits,
1687 .get_generation = get_generation_from_graph_data,
1688 .set_generation = set_generation_v2,
1689 };
1690
1691 if (ctx->report_progress)
1692 info.progress = ctx->progress
1693 = start_delayed_progress(
1694 _("Computing commit graph generation numbers"),
1695 ctx->commits.nr);
1696
1697 if (!ctx->trust_generation_numbers) {
1698 for (i = 0; i < ctx->commits.nr; i++) {
1699 struct commit *c = ctx->commits.list[i];
1700 repo_parse_commit(ctx->r, c);
1701 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1702 }
1703 }
1704
1705 compute_reachable_generation_numbers(&info, 2);
1706
1707 for (i = 0; i < ctx->commits.nr; i++) {
1708 struct commit *c = ctx->commits.list[i];
1709 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1710 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1711 ctx->num_generation_data_overflows++;
1712 }
1713 stop_progress(&ctx->progress);
1714 }
1715
1716 static void set_generation_in_graph_data(struct commit *c, timestamp_t t,
1717 void *data UNUSED)
1718 {
1719 commit_graph_data_at(c)->generation = t;
1720 }
1721
1722 /*
1723 * After this method, all commits reachable from those in the given
1724 * list will have non-zero, non-infinite generation numbers.
1725 */
1726 void ensure_generations_valid(struct repository *r,
1727 struct commit **commits, size_t nr)
1728 {
1729 int generation_version = get_configured_generation_version(r);
1730 struct packed_commit_list list = {
1731 .list = commits,
1732 .alloc = nr,
1733 .nr = nr,
1734 };
1735 struct compute_generation_info info = {
1736 .r = r,
1737 .commits = &list,
1738 .get_generation = get_generation_from_graph_data,
1739 .set_generation = set_generation_in_graph_data,
1740 };
1741
1742 compute_reachable_generation_numbers(&info, generation_version);
1743 }
1744
1745 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1746 {
1747 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1748 ctx->count_bloom_filter_computed);
1749 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1750 ctx->count_bloom_filter_not_computed);
1751 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1752 ctx->count_bloom_filter_trunc_empty);
1753 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1754 ctx->count_bloom_filter_trunc_large);
1755 }
1756
1757 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1758 {
1759 int i;
1760 struct progress *progress = NULL;
1761 struct commit **sorted_commits;
1762 int max_new_filters;
1763
1764 init_bloom_filters();
1765
1766 if (ctx->report_progress)
1767 progress = start_delayed_progress(
1768 _("Computing commit changed paths Bloom filters"),
1769 ctx->commits.nr);
1770
1771 DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1772
1773 if (ctx->order_by_pack)
1774 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1775 else
1776 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1777
1778 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1779 ctx->opts->max_new_filters : ctx->commits.nr;
1780
1781 for (i = 0; i < ctx->commits.nr; i++) {
1782 enum bloom_filter_computed computed = 0;
1783 struct commit *c = sorted_commits[i];
1784 struct bloom_filter *filter = get_or_compute_bloom_filter(
1785 ctx->r,
1786 c,
1787 ctx->count_bloom_filter_computed < max_new_filters,
1788 ctx->bloom_settings,
1789 &computed);
1790 if (computed & BLOOM_COMPUTED) {
1791 ctx->count_bloom_filter_computed++;
1792 if (computed & BLOOM_TRUNC_EMPTY)
1793 ctx->count_bloom_filter_trunc_empty++;
1794 if (computed & BLOOM_TRUNC_LARGE)
1795 ctx->count_bloom_filter_trunc_large++;
1796 } else if (computed & BLOOM_NOT_COMPUTED)
1797 ctx->count_bloom_filter_not_computed++;
1798 ctx->total_bloom_filter_data_size += filter
1799 ? sizeof(unsigned char) * filter->len : 0;
1800 display_progress(progress, i + 1);
1801 }
1802
1803 if (trace2_is_enabled())
1804 trace2_bloom_filter_write_statistics(ctx);
1805
1806 free(sorted_commits);
1807 stop_progress(&progress);
1808 }
1809
1810 struct refs_cb_data {
1811 struct oidset *commits;
1812 struct progress *progress;
1813 };
1814
1815 static int add_ref_to_set(const char *refname UNUSED,
1816 const struct object_id *oid,
1817 int flags UNUSED, void *cb_data)
1818 {
1819 struct object_id peeled;
1820 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1821
1822 if (!peel_iterated_oid(oid, &peeled))
1823 oid = &peeled;
1824 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1825 oidset_insert(data->commits, oid);
1826
1827 display_progress(data->progress, oidset_size(data->commits));
1828
1829 return 0;
1830 }
1831
1832 int write_commit_graph_reachable(struct object_directory *odb,
1833 enum commit_graph_write_flags flags,
1834 const struct commit_graph_opts *opts)
1835 {
1836 struct oidset commits = OIDSET_INIT;
1837 struct refs_cb_data data;
1838 int result;
1839
1840 memset(&data, 0, sizeof(data));
1841 data.commits = &commits;
1842 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1843 data.progress = start_delayed_progress(
1844 _("Collecting referenced commits"), 0);
1845
1846 for_each_ref(add_ref_to_set, &data);
1847
1848 stop_progress(&data.progress);
1849
1850 result = write_commit_graph(odb, NULL, &commits,
1851 flags, opts);
1852
1853 oidset_clear(&commits);
1854 return result;
1855 }
1856
1857 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1858 const struct string_list *pack_indexes)
1859 {
1860 uint32_t i;
1861 struct strbuf progress_title = STRBUF_INIT;
1862 struct strbuf packname = STRBUF_INIT;
1863 int dirlen;
1864 int ret = 0;
1865
1866 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1867 dirlen = packname.len;
1868 if (ctx->report_progress) {
1869 strbuf_addf(&progress_title,
1870 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1871 "Finding commits for commit graph in %"PRIuMAX" packs",
1872 pack_indexes->nr),
1873 (uintmax_t)pack_indexes->nr);
1874 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1875 ctx->progress_done = 0;
1876 }
1877 for (i = 0; i < pack_indexes->nr; i++) {
1878 struct packed_git *p;
1879 strbuf_setlen(&packname, dirlen);
1880 strbuf_addstr(&packname, pack_indexes->items[i].string);
1881 p = add_packed_git(packname.buf, packname.len, 1);
1882 if (!p) {
1883 ret = error(_("error adding pack %s"), packname.buf);
1884 goto cleanup;
1885 }
1886 if (open_pack_index(p)) {
1887 ret = error(_("error opening index for %s"), packname.buf);
1888 goto cleanup;
1889 }
1890 for_each_object_in_pack(p, add_packed_commits, ctx,
1891 FOR_EACH_OBJECT_PACK_ORDER);
1892 close_pack(p);
1893 free(p);
1894 }
1895
1896 cleanup:
1897 stop_progress(&ctx->progress);
1898 strbuf_release(&progress_title);
1899 strbuf_release(&packname);
1900
1901 return ret;
1902 }
1903
1904 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1905 struct oidset *commits)
1906 {
1907 struct oidset_iter iter;
1908 struct object_id *oid;
1909
1910 if (!oidset_size(commits))
1911 return 0;
1912
1913 oidset_iter_init(commits, &iter);
1914 while ((oid = oidset_iter_next(&iter))) {
1915 oid_array_append(&ctx->oids, oid);
1916 }
1917
1918 return 0;
1919 }
1920
1921 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1922 {
1923 if (ctx->report_progress)
1924 ctx->progress = start_delayed_progress(
1925 _("Finding commits for commit graph among packed objects"),
1926 ctx->approx_nr_objects);
1927 for_each_packed_object(add_packed_commits, ctx,
1928 FOR_EACH_OBJECT_PACK_ORDER);
1929 if (ctx->progress_done < ctx->approx_nr_objects)
1930 display_progress(ctx->progress, ctx->approx_nr_objects);
1931 stop_progress(&ctx->progress);
1932 }
1933
1934 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1935 {
1936 uint32_t i;
1937 enum commit_graph_split_flags flags = ctx->opts ?
1938 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1939
1940 ctx->num_extra_edges = 0;
1941 if (ctx->report_progress)
1942 ctx->progress = start_delayed_progress(
1943 _("Finding extra edges in commit graph"),
1944 ctx->oids.nr);
1945 oid_array_sort(&ctx->oids);
1946 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1947 unsigned int num_parents;
1948
1949 display_progress(ctx->progress, i + 1);
1950
1951 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1952 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1953
1954 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1955 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1956 continue;
1957
1958 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1959 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1960 else
1961 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1962
1963 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1964 if (num_parents > 2)
1965 ctx->num_extra_edges += num_parents - 1;
1966
1967 ctx->commits.nr++;
1968 }
1969 stop_progress(&ctx->progress);
1970 }
1971
1972 static int write_graph_chunk_base_1(struct hashfile *f,
1973 struct commit_graph *g)
1974 {
1975 int num = 0;
1976
1977 if (!g)
1978 return 0;
1979
1980 num = write_graph_chunk_base_1(f, g->base_graph);
1981 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1982 return num + 1;
1983 }
1984
1985 static int write_graph_chunk_base(struct hashfile *f,
1986 void *data)
1987 {
1988 struct write_commit_graph_context *ctx = data;
1989 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1990
1991 if (num != ctx->num_commit_graphs_after - 1) {
1992 error(_("failed to write correct number of base graph ids"));
1993 return -1;
1994 }
1995
1996 return 0;
1997 }
1998
1999 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
2000 {
2001 uint32_t i;
2002 int fd;
2003 struct hashfile *f;
2004 struct lock_file lk = LOCK_INIT;
2005 const unsigned hashsz = the_hash_algo->rawsz;
2006 struct strbuf progress_title = STRBUF_INIT;
2007 struct chunkfile *cf;
2008 unsigned char file_hash[GIT_MAX_RAWSZ];
2009
2010 if (ctx->split) {
2011 struct strbuf tmp_file = STRBUF_INIT;
2012
2013 strbuf_addf(&tmp_file,
2014 "%s/info/commit-graphs/tmp_graph_XXXXXX",
2015 ctx->odb->path);
2016 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
2017 } else {
2018 ctx->graph_name = get_commit_graph_filename(ctx->odb);
2019 }
2020
2021 if (safe_create_leading_directories(ctx->graph_name)) {
2022 UNLEAK(ctx->graph_name);
2023 error(_("unable to create leading directories of %s"),
2024 ctx->graph_name);
2025 return -1;
2026 }
2027
2028 if (ctx->split) {
2029 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
2030
2031 hold_lock_file_for_update_mode(&lk, lock_name,
2032 LOCK_DIE_ON_ERROR, 0444);
2033 free(lock_name);
2034
2035 fd = git_mkstemp_mode(ctx->graph_name, 0444);
2036 if (fd < 0) {
2037 error(_("unable to create temporary graph layer"));
2038 return -1;
2039 }
2040
2041 if (adjust_shared_perm(ctx->graph_name)) {
2042 error(_("unable to adjust shared permissions for '%s'"),
2043 ctx->graph_name);
2044 return -1;
2045 }
2046
2047 f = hashfd(fd, ctx->graph_name);
2048 } else {
2049 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
2050 LOCK_DIE_ON_ERROR, 0444);
2051 fd = get_lock_file_fd(&lk);
2052 f = hashfd(fd, get_lock_file_path(&lk));
2053 }
2054
2055 cf = init_chunkfile(f);
2056
2057 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
2058 write_graph_chunk_fanout);
2059 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, st_mult(hashsz, ctx->commits.nr),
2060 write_graph_chunk_oids);
2061 add_chunk(cf, GRAPH_CHUNKID_DATA, st_mult(hashsz + 16, ctx->commits.nr),
2062 write_graph_chunk_data);
2063
2064 if (ctx->write_generation_data)
2065 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
2066 st_mult(sizeof(uint32_t), ctx->commits.nr),
2067 write_graph_chunk_generation_data);
2068 if (ctx->num_generation_data_overflows)
2069 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
2070 st_mult(sizeof(timestamp_t), ctx->num_generation_data_overflows),
2071 write_graph_chunk_generation_data_overflow);
2072 if (ctx->num_extra_edges)
2073 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
2074 st_mult(4, ctx->num_extra_edges),
2075 write_graph_chunk_extra_edges);
2076 if (ctx->changed_paths) {
2077 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
2078 st_mult(sizeof(uint32_t), ctx->commits.nr),
2079 write_graph_chunk_bloom_indexes);
2080 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
2081 st_add(sizeof(uint32_t) * 3,
2082 ctx->total_bloom_filter_data_size),
2083 write_graph_chunk_bloom_data);
2084 }
2085 if (ctx->num_commit_graphs_after > 1)
2086 add_chunk(cf, GRAPH_CHUNKID_BASE,
2087 st_mult(hashsz, ctx->num_commit_graphs_after - 1),
2088 write_graph_chunk_base);
2089
2090 hashwrite_be32(f, GRAPH_SIGNATURE);
2091
2092 hashwrite_u8(f, GRAPH_VERSION);
2093 hashwrite_u8(f, oid_version(the_hash_algo));
2094 hashwrite_u8(f, get_num_chunks(cf));
2095 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
2096
2097 if (ctx->report_progress) {
2098 strbuf_addf(&progress_title,
2099 Q_("Writing out commit graph in %d pass",
2100 "Writing out commit graph in %d passes",
2101 get_num_chunks(cf)),
2102 get_num_chunks(cf));
2103 ctx->progress = start_delayed_progress(
2104 progress_title.buf,
2105 st_mult(get_num_chunks(cf), ctx->commits.nr));
2106 }
2107
2108 write_chunkfile(cf, ctx);
2109
2110 stop_progress(&ctx->progress);
2111 strbuf_release(&progress_title);
2112
2113 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
2114 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
2115 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
2116
2117 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
2118 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
2119 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
2120 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
2121 }
2122
2123 close_commit_graph(ctx->r->objects);
2124 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
2125 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
2126 free_chunkfile(cf);
2127
2128 if (ctx->split) {
2129 FILE *chainf = fdopen_lock_file(&lk, "w");
2130 char *final_graph_name;
2131 int result;
2132
2133 close(fd);
2134
2135 if (!chainf) {
2136 error(_("unable to open commit-graph chain file"));
2137 return -1;
2138 }
2139
2140 if (ctx->base_graph_name) {
2141 const char *dest;
2142 int idx = ctx->num_commit_graphs_after - 1;
2143 if (ctx->num_commit_graphs_after > 1)
2144 idx--;
2145
2146 dest = ctx->commit_graph_filenames_after[idx];
2147
2148 if (strcmp(ctx->base_graph_name, dest)) {
2149 result = rename(ctx->base_graph_name, dest);
2150
2151 if (result) {
2152 error(_("failed to rename base commit-graph file"));
2153 return -1;
2154 }
2155 }
2156 } else {
2157 char *graph_name = get_commit_graph_filename(ctx->odb);
2158 unlink(graph_name);
2159 free(graph_name);
2160 }
2161
2162 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2163 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
2164 final_graph_name = get_split_graph_filename(ctx->odb,
2165 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2166 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1]);
2167 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
2168
2169 result = rename(ctx->graph_name, final_graph_name);
2170
2171 for (i = 0; i < ctx->num_commit_graphs_after; i++)
2172 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2173
2174 if (result) {
2175 error(_("failed to rename temporary commit-graph file"));
2176 return -1;
2177 }
2178 }
2179
2180 commit_lock_file(&lk);
2181
2182 return 0;
2183 }
2184
2185 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2186 {
2187 struct commit_graph *g;
2188 uint32_t num_commits;
2189 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2190 uint32_t i;
2191
2192 int max_commits = 0;
2193 int size_mult = 2;
2194
2195 if (ctx->opts) {
2196 max_commits = ctx->opts->max_commits;
2197
2198 if (ctx->opts->size_multiple)
2199 size_mult = ctx->opts->size_multiple;
2200
2201 flags = ctx->opts->split_flags;
2202 }
2203
2204 g = ctx->r->objects->commit_graph;
2205 num_commits = ctx->commits.nr;
2206 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2207 ctx->num_commit_graphs_after = 1;
2208 else
2209 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2210
2211 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2212 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2213 while (g && (g->num_commits <= st_mult(size_mult, num_commits) ||
2214 (max_commits && num_commits > max_commits))) {
2215 if (g->odb != ctx->odb)
2216 break;
2217
2218 if (unsigned_add_overflows(num_commits, g->num_commits))
2219 die(_("cannot merge graphs with %"PRIuMAX", "
2220 "%"PRIuMAX" commits"),
2221 (uintmax_t)num_commits,
2222 (uintmax_t)g->num_commits);
2223 num_commits += g->num_commits;
2224 g = g->base_graph;
2225
2226 ctx->num_commit_graphs_after--;
2227 }
2228 }
2229
2230 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2231 ctx->new_base_graph = g;
2232 else if (ctx->num_commit_graphs_after != 1)
2233 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2234 "should be 1 with --split=replace");
2235
2236 if (ctx->num_commit_graphs_after == 2) {
2237 char *old_graph_name = get_commit_graph_filename(g->odb);
2238
2239 if (!strcmp(g->filename, old_graph_name) &&
2240 g->odb != ctx->odb) {
2241 ctx->num_commit_graphs_after = 1;
2242 ctx->new_base_graph = NULL;
2243 }
2244
2245 free(old_graph_name);
2246 }
2247
2248 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2249 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2250
2251 for (i = 0; i < ctx->num_commit_graphs_after &&
2252 i < ctx->num_commit_graphs_before; i++)
2253 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2254
2255 i = ctx->num_commit_graphs_before - 1;
2256 g = ctx->r->objects->commit_graph;
2257
2258 while (g) {
2259 if (i < ctx->num_commit_graphs_after)
2260 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2261
2262 /*
2263 * If the topmost remaining layer has generation data chunk, the
2264 * resultant layer also has generation data chunk.
2265 */
2266 if (i == ctx->num_commit_graphs_after - 2)
2267 ctx->write_generation_data = !!g->chunk_generation_data;
2268
2269 i--;
2270 g = g->base_graph;
2271 }
2272 }
2273
2274 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2275 struct commit_graph *g)
2276 {
2277 uint32_t i;
2278 uint32_t offset = g->num_commits_in_base;
2279
2280 if (unsigned_add_overflows(ctx->commits.nr, g->num_commits))
2281 die(_("cannot merge graph %s, too many commits: %"PRIuMAX),
2282 oid_to_hex(&g->oid),
2283 (uintmax_t)st_add(ctx->commits.nr, g->num_commits));
2284
2285 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2286
2287 for (i = 0; i < g->num_commits; i++) {
2288 struct object_id oid;
2289 struct commit *result;
2290
2291 display_progress(ctx->progress, i + 1);
2292
2293 load_oid_from_graph(g, i + offset, &oid);
2294
2295 /* only add commits if they still exist in the repo */
2296 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2297
2298 if (result) {
2299 ctx->commits.list[ctx->commits.nr] = result;
2300 ctx->commits.nr++;
2301 }
2302 }
2303 }
2304
2305 static int commit_compare(const void *_a, const void *_b)
2306 {
2307 const struct commit *a = *(const struct commit **)_a;
2308 const struct commit *b = *(const struct commit **)_b;
2309 return oidcmp(&a->object.oid, &b->object.oid);
2310 }
2311
2312 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2313 {
2314 uint32_t i, dedup_i = 0;
2315
2316 if (ctx->report_progress)
2317 ctx->progress = start_delayed_progress(
2318 _("Scanning merged commits"),
2319 ctx->commits.nr);
2320
2321 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2322
2323 ctx->num_extra_edges = 0;
2324 for (i = 0; i < ctx->commits.nr; i++) {
2325 display_progress(ctx->progress, i + 1);
2326
2327 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2328 &ctx->commits.list[i]->object.oid)) {
2329 /*
2330 * Silently ignore duplicates. These were likely
2331 * created due to a commit appearing in multiple
2332 * layers of the chain, which is unexpected but
2333 * not invalid. We should make sure there is a
2334 * unique copy in the new layer.
2335 */
2336 } else {
2337 unsigned int num_parents;
2338
2339 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2340 dedup_i++;
2341
2342 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2343 if (num_parents > 2)
2344 ctx->num_extra_edges += num_parents - 1;
2345 }
2346 }
2347
2348 ctx->commits.nr = dedup_i;
2349
2350 stop_progress(&ctx->progress);
2351 }
2352
2353 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2354 {
2355 struct commit_graph *g = ctx->r->objects->commit_graph;
2356 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2357
2358 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2359 current_graph_number--;
2360
2361 if (ctx->report_progress)
2362 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2363
2364 merge_commit_graph(ctx, g);
2365 stop_progress(&ctx->progress);
2366
2367 g = g->base_graph;
2368 }
2369
2370 if (g) {
2371 ctx->new_base_graph = g;
2372 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2373 }
2374
2375 if (ctx->new_base_graph)
2376 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2377
2378 sort_and_scan_merged_commits(ctx);
2379 }
2380
2381 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2382 {
2383 uint32_t i;
2384 time_t now = time(NULL);
2385
2386 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2387 struct stat st;
2388 struct utimbuf updated_time;
2389
2390 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2391 continue;
2392
2393 updated_time.actime = st.st_atime;
2394 updated_time.modtime = now;
2395 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2396 }
2397 }
2398
2399 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2400 {
2401 struct strbuf path = STRBUF_INIT;
2402 DIR *dir;
2403 struct dirent *de;
2404 size_t dirnamelen;
2405 timestamp_t expire_time = time(NULL);
2406
2407 if (ctx->opts && ctx->opts->expire_time)
2408 expire_time = ctx->opts->expire_time;
2409 if (!ctx->split) {
2410 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2411 unlink(chain_file_name);
2412 free(chain_file_name);
2413 ctx->num_commit_graphs_after = 0;
2414 }
2415
2416 strbuf_addstr(&path, ctx->odb->path);
2417 strbuf_addstr(&path, "/info/commit-graphs");
2418 dir = opendir(path.buf);
2419
2420 if (!dir)
2421 goto out;
2422
2423 strbuf_addch(&path, '/');
2424 dirnamelen = path.len;
2425 while ((de = readdir(dir)) != NULL) {
2426 struct stat st;
2427 uint32_t i, found = 0;
2428
2429 strbuf_setlen(&path, dirnamelen);
2430 strbuf_addstr(&path, de->d_name);
2431
2432 if (stat(path.buf, &st) < 0)
2433 continue;
2434
2435 if (st.st_mtime > expire_time)
2436 continue;
2437 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2438 continue;
2439
2440 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2441 if (!strcmp(ctx->commit_graph_filenames_after[i],
2442 path.buf)) {
2443 found = 1;
2444 break;
2445 }
2446 }
2447
2448 if (!found)
2449 unlink(path.buf);
2450 }
2451
2452 out:
2453 if(dir)
2454 closedir(dir);
2455 strbuf_release(&path);
2456 }
2457
2458 int write_commit_graph(struct object_directory *odb,
2459 const struct string_list *const pack_indexes,
2460 struct oidset *commits,
2461 enum commit_graph_write_flags flags,
2462 const struct commit_graph_opts *opts)
2463 {
2464 struct repository *r = the_repository;
2465 struct write_commit_graph_context *ctx;
2466 uint32_t i;
2467 int res = 0;
2468 int replace = 0;
2469 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2470 struct topo_level_slab topo_levels;
2471
2472 prepare_repo_settings(r);
2473 if (!r->settings.core_commit_graph) {
2474 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2475 return 0;
2476 }
2477 if (!commit_graph_compatible(r))
2478 return 0;
2479
2480 CALLOC_ARRAY(ctx, 1);
2481 ctx->r = r;
2482 ctx->odb = odb;
2483 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2484 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2485 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2486 ctx->opts = opts;
2487 ctx->total_bloom_filter_data_size = 0;
2488 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2489 ctx->num_generation_data_overflows = 0;
2490
2491 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2492 bloom_settings.bits_per_entry);
2493 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2494 bloom_settings.num_hashes);
2495 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2496 bloom_settings.max_changed_paths);
2497 ctx->bloom_settings = &bloom_settings;
2498
2499 init_topo_level_slab(&topo_levels);
2500 ctx->topo_levels = &topo_levels;
2501
2502 prepare_commit_graph(ctx->r);
2503 if (ctx->r->objects->commit_graph) {
2504 struct commit_graph *g = ctx->r->objects->commit_graph;
2505
2506 while (g) {
2507 g->topo_levels = &topo_levels;
2508 g = g->base_graph;
2509 }
2510 }
2511
2512 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2513 ctx->changed_paths = 1;
2514 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2515 struct commit_graph *g;
2516
2517 g = ctx->r->objects->commit_graph;
2518
2519 /* We have changed-paths already. Keep them in the next graph */
2520 if (g && g->chunk_bloom_data) {
2521 ctx->changed_paths = 1;
2522 ctx->bloom_settings = g->bloom_filter_settings;
2523 }
2524 }
2525
2526 if (ctx->split) {
2527 struct commit_graph *g = ctx->r->objects->commit_graph;
2528
2529 while (g) {
2530 ctx->num_commit_graphs_before++;
2531 g = g->base_graph;
2532 }
2533
2534 if (ctx->num_commit_graphs_before) {
2535 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2536 i = ctx->num_commit_graphs_before;
2537 g = ctx->r->objects->commit_graph;
2538
2539 while (g) {
2540 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2541 g = g->base_graph;
2542 }
2543 }
2544
2545 if (ctx->opts)
2546 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2547 }
2548
2549 ctx->approx_nr_objects = repo_approximate_object_count(the_repository);
2550
2551 if (ctx->append && ctx->r->objects->commit_graph) {
2552 struct commit_graph *g = ctx->r->objects->commit_graph;
2553 for (i = 0; i < g->num_commits; i++) {
2554 struct object_id oid;
2555 oidread(&oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2556 oid_array_append(&ctx->oids, &oid);
2557 }
2558 }
2559
2560 if (pack_indexes) {
2561 ctx->order_by_pack = 1;
2562 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2563 goto cleanup;
2564 }
2565
2566 if (commits) {
2567 if ((res = fill_oids_from_commits(ctx, commits)))
2568 goto cleanup;
2569 }
2570
2571 if (!pack_indexes && !commits) {
2572 ctx->order_by_pack = 1;
2573 fill_oids_from_all_packs(ctx);
2574 }
2575
2576 close_reachable(ctx);
2577
2578 copy_oids_to_commits(ctx);
2579
2580 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2581 error(_("too many commits to write graph"));
2582 res = -1;
2583 goto cleanup;
2584 }
2585
2586 if (!ctx->commits.nr && !replace)
2587 goto cleanup;
2588
2589 if (ctx->split) {
2590 split_graph_merge_strategy(ctx);
2591
2592 if (!replace)
2593 merge_commit_graphs(ctx);
2594 } else
2595 ctx->num_commit_graphs_after = 1;
2596
2597 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2598
2599 compute_topological_levels(ctx);
2600 if (ctx->write_generation_data)
2601 compute_generation_numbers(ctx);
2602
2603 if (ctx->changed_paths)
2604 compute_bloom_filters(ctx);
2605
2606 res = write_commit_graph_file(ctx);
2607
2608 if (ctx->split)
2609 mark_commit_graphs(ctx);
2610
2611 expire_commit_graphs(ctx);
2612
2613 cleanup:
2614 free(ctx->graph_name);
2615 free(ctx->base_graph_name);
2616 free(ctx->commits.list);
2617 oid_array_clear(&ctx->oids);
2618 clear_topo_level_slab(&topo_levels);
2619
2620 if (ctx->commit_graph_filenames_after) {
2621 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2622 free(ctx->commit_graph_filenames_after[i]);
2623 free(ctx->commit_graph_hash_after[i]);
2624 }
2625
2626 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2627 free(ctx->commit_graph_filenames_before[i]);
2628
2629 free(ctx->commit_graph_filenames_after);
2630 free(ctx->commit_graph_filenames_before);
2631 free(ctx->commit_graph_hash_after);
2632 }
2633
2634 free(ctx);
2635
2636 return res;
2637 }
2638
2639 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2640 static int verify_commit_graph_error;
2641
2642 __attribute__((format (printf, 1, 2)))
2643 static void graph_report(const char *fmt, ...)
2644 {
2645 va_list ap;
2646
2647 verify_commit_graph_error = 1;
2648 va_start(ap, fmt);
2649 vfprintf(stderr, fmt, ap);
2650 fprintf(stderr, "\n");
2651 va_end(ap);
2652 }
2653
2654 static int commit_graph_checksum_valid(struct commit_graph *g)
2655 {
2656 return hashfile_checksum_valid(g->data, g->data_len);
2657 }
2658
2659 static int verify_one_commit_graph(struct repository *r,
2660 struct commit_graph *g,
2661 struct progress *progress,
2662 uint64_t *seen)
2663 {
2664 uint32_t i, cur_fanout_pos = 0;
2665 struct object_id prev_oid, cur_oid;
2666 struct commit *seen_gen_zero = NULL;
2667 struct commit *seen_gen_non_zero = NULL;
2668
2669 if (!commit_graph_checksum_valid(g)) {
2670 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2671 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2672 }
2673
2674 for (i = 0; i < g->num_commits; i++) {
2675 struct commit *graph_commit;
2676
2677 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2678
2679 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2680 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2681 oid_to_hex(&prev_oid),
2682 oid_to_hex(&cur_oid));
2683
2684 oidcpy(&prev_oid, &cur_oid);
2685
2686 while (cur_oid.hash[0] > cur_fanout_pos) {
2687 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2688
2689 if (i != fanout_value)
2690 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2691 cur_fanout_pos, fanout_value, i);
2692 cur_fanout_pos++;
2693 }
2694
2695 graph_commit = lookup_commit(r, &cur_oid);
2696 if (!parse_commit_in_graph_one(r, g, graph_commit))
2697 graph_report(_("failed to parse commit %s from commit-graph"),
2698 oid_to_hex(&cur_oid));
2699 }
2700
2701 while (cur_fanout_pos < 256) {
2702 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2703
2704 if (g->num_commits != fanout_value)
2705 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2706 cur_fanout_pos, fanout_value, i);
2707
2708 cur_fanout_pos++;
2709 }
2710
2711 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2712 return verify_commit_graph_error;
2713
2714 for (i = 0; i < g->num_commits; i++) {
2715 struct commit *graph_commit, *odb_commit;
2716 struct commit_list *graph_parents, *odb_parents;
2717 timestamp_t max_generation = 0;
2718 timestamp_t generation;
2719
2720 display_progress(progress, ++(*seen));
2721 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2722
2723 graph_commit = lookup_commit(r, &cur_oid);
2724 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2725 if (repo_parse_commit_internal(r, odb_commit, 0, 0)) {
2726 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2727 oid_to_hex(&cur_oid));
2728 continue;
2729 }
2730
2731 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2732 get_commit_tree_oid(odb_commit)))
2733 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2734 oid_to_hex(&cur_oid),
2735 oid_to_hex(get_commit_tree_oid(graph_commit)),
2736 oid_to_hex(get_commit_tree_oid(odb_commit)));
2737
2738 graph_parents = graph_commit->parents;
2739 odb_parents = odb_commit->parents;
2740
2741 while (graph_parents) {
2742 if (!odb_parents) {
2743 graph_report(_("commit-graph parent list for commit %s is too long"),
2744 oid_to_hex(&cur_oid));
2745 break;
2746 }
2747
2748 /* parse parent in case it is in a base graph */
2749 parse_commit_in_graph_one(r, g, graph_parents->item);
2750
2751 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2752 graph_report(_("commit-graph parent for %s is %s != %s"),
2753 oid_to_hex(&cur_oid),
2754 oid_to_hex(&graph_parents->item->object.oid),
2755 oid_to_hex(&odb_parents->item->object.oid));
2756
2757 generation = commit_graph_generation_from_graph(graph_parents->item);
2758 if (generation > max_generation)
2759 max_generation = generation;
2760
2761 graph_parents = graph_parents->next;
2762 odb_parents = odb_parents->next;
2763 }
2764
2765 if (odb_parents)
2766 graph_report(_("commit-graph parent list for commit %s terminates early"),
2767 oid_to_hex(&cur_oid));
2768
2769 if (commit_graph_generation_from_graph(graph_commit))
2770 seen_gen_non_zero = graph_commit;
2771 else
2772 seen_gen_zero = graph_commit;
2773
2774 if (seen_gen_zero)
2775 continue;
2776
2777 /*
2778 * If we are using topological level and one of our parents has
2779 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2780 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2781 * in the following condition.
2782 */
2783 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2784 max_generation--;
2785
2786 generation = commit_graph_generation(graph_commit);
2787 if (generation < max_generation + 1)
2788 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2789 oid_to_hex(&cur_oid),
2790 generation,
2791 max_generation + 1);
2792
2793 if (graph_commit->date != odb_commit->date)
2794 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2795 oid_to_hex(&cur_oid),
2796 graph_commit->date,
2797 odb_commit->date);
2798 }
2799
2800 if (seen_gen_zero && seen_gen_non_zero)
2801 graph_report(_("commit-graph has both zero and non-zero "
2802 "generations (e.g., commits '%s' and '%s')"),
2803 oid_to_hex(&seen_gen_zero->object.oid),
2804 oid_to_hex(&seen_gen_non_zero->object.oid));
2805
2806 return verify_commit_graph_error;
2807 }
2808
2809 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2810 {
2811 struct progress *progress = NULL;
2812 int local_error = 0;
2813 uint64_t seen = 0;
2814
2815 if (!g) {
2816 graph_report("no commit-graph file loaded");
2817 return 1;
2818 }
2819
2820 if (flags & COMMIT_GRAPH_WRITE_PROGRESS) {
2821 uint64_t total = g->num_commits;
2822 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW))
2823 total += g->num_commits_in_base;
2824
2825 progress = start_progress(_("Verifying commits in commit graph"),
2826 total);
2827 }
2828
2829 for (; g; g = g->base_graph) {
2830 local_error |= verify_one_commit_graph(r, g, progress, &seen);
2831 if (flags & COMMIT_GRAPH_VERIFY_SHALLOW)
2832 break;
2833 }
2834
2835 stop_progress(&progress);
2836
2837 return local_error;
2838 }
2839
2840 void free_commit_graph(struct commit_graph *g)
2841 {
2842 while (g) {
2843 struct commit_graph *next = g->base_graph;
2844
2845 if (g->data)
2846 munmap((void *)g->data, g->data_len);
2847 free(g->filename);
2848 free(g->bloom_filter_settings);
2849 free(g);
2850
2851 g = next;
2852 }
2853 }
2854
2855 void disable_commit_graph(struct repository *r)
2856 {
2857 r->commit_graph_disabled = 1;
2858 }