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