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