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