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