at commit 3cb9185f

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