]> git.ipfire.org Git - thirdparty/git.git/blob - commit-graph.c
treewide: be explicit about dependence on oid-array.h
[thirdparty/git.git] / commit-graph.c
1 #include "cache.h"
2 #include "config.h"
3 #include "gettext.h"
4 #include "hex.h"
5 #include "lockfile.h"
6 #include "pack.h"
7 #include "packfile.h"
8 #include "commit.h"
9 #include "object.h"
10 #include "refs.h"
11 #include "revision.h"
12 #include "hash-lookup.h"
13 #include "commit-graph.h"
14 #include "object-store.h"
15 #include "oid-array.h"
16 #include "alloc.h"
17 #include "hashmap.h"
18 #include "replace-object.h"
19 #include "progress.h"
20 #include "bloom.h"
21 #include "commit-slab.h"
22 #include "shallow.h"
23 #include "json-writer.h"
24 #include "trace2.h"
25 #include "chunk-format.h"
26 #include "wrapper.h"
27
28 void git_test_write_commit_graph_or_die(void)
29 {
30 int flags = 0;
31 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
32 return;
33
34 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
35 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
36
37 if (write_commit_graph_reachable(the_repository->objects->odb,
38 flags, NULL))
39 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
40 }
41
42 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
43 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
44 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
45 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
46 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
47 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
48 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
49 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
50 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
51 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
52
53 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
54
55 #define GRAPH_VERSION_1 0x1
56 #define GRAPH_VERSION GRAPH_VERSION_1
57
58 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
59 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
60 #define GRAPH_PARENT_NONE 0x70000000
61
62 #define GRAPH_LAST_EDGE 0x80000000
63
64 #define GRAPH_HEADER_SIZE 8
65 #define GRAPH_FANOUT_SIZE (4 * 256)
66 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
67 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
68
69 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
70
71 /* Remember to update object flag allocation in object.h */
72 #define REACHABLE (1u<<15)
73
74 define_commit_slab(topo_level_slab, uint32_t);
75
76 /* Keep track of the order in which commits are added to our list. */
77 define_commit_slab(commit_pos, int);
78 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
79
80 static void set_commit_pos(struct repository *r, const struct object_id *oid)
81 {
82 static int32_t max_pos;
83 struct commit *commit = lookup_commit(r, oid);
84
85 if (!commit)
86 return; /* should never happen, but be lenient */
87
88 *commit_pos_at(&commit_pos, commit) = max_pos++;
89 }
90
91 static int commit_pos_cmp(const void *va, const void *vb)
92 {
93 const struct commit *a = *(const struct commit **)va;
94 const struct commit *b = *(const struct commit **)vb;
95 return commit_pos_at(&commit_pos, a) -
96 commit_pos_at(&commit_pos, b);
97 }
98
99 define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
100 static struct commit_graph_data_slab commit_graph_data_slab =
101 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
102
103 static int get_configured_generation_version(struct repository *r)
104 {
105 int version = 2;
106 repo_config_get_int(r, "commitgraph.generationversion", &version);
107 return version;
108 }
109
110 uint32_t commit_graph_position(const struct commit *c)
111 {
112 struct commit_graph_data *data =
113 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
114
115 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
116 }
117
118 timestamp_t commit_graph_generation(const struct commit *c)
119 {
120 struct commit_graph_data *data =
121 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
122
123 if (!data)
124 return GENERATION_NUMBER_INFINITY;
125 else if (data->graph_pos == COMMIT_NOT_FROM_GRAPH)
126 return GENERATION_NUMBER_INFINITY;
127
128 return data->generation;
129 }
130
131 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
132 {
133 unsigned int i, nth_slab;
134 struct commit_graph_data *data =
135 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
136
137 if (data)
138 return data;
139
140 nth_slab = c->index / commit_graph_data_slab.slab_size;
141 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
142
143 /*
144 * commit-slab initializes elements with zero, overwrite this with
145 * COMMIT_NOT_FROM_GRAPH for graph_pos.
146 *
147 * We avoid initializing generation with checking if graph position
148 * is not COMMIT_NOT_FROM_GRAPH.
149 */
150 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
151 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
152 COMMIT_NOT_FROM_GRAPH;
153 }
154
155 return data;
156 }
157
158 /*
159 * Should be used only while writing commit-graph as it compares
160 * generation value of commits by directly accessing commit-slab.
161 */
162 static int commit_gen_cmp(const void *va, const void *vb)
163 {
164 const struct commit *a = *(const struct commit **)va;
165 const struct commit *b = *(const struct commit **)vb;
166
167 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
168 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
169 /* lower generation commits first */
170 if (generation_a < generation_b)
171 return -1;
172 else if (generation_a > generation_b)
173 return 1;
174
175 /* use date as a heuristic when generations are equal */
176 if (a->date < b->date)
177 return -1;
178 else if (a->date > b->date)
179 return 1;
180 return 0;
181 }
182
183 char *get_commit_graph_filename(struct object_directory *obj_dir)
184 {
185 return xstrfmt("%s/info/commit-graph", obj_dir->path);
186 }
187
188 static char *get_split_graph_filename(struct object_directory *odb,
189 const char *oid_hex)
190 {
191 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
192 oid_hex);
193 }
194
195 char *get_commit_graph_chain_filename(struct object_directory *odb)
196 {
197 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
198 }
199
200 static struct commit_graph *alloc_commit_graph(void)
201 {
202 struct commit_graph *g = xcalloc(1, sizeof(*g));
203
204 return g;
205 }
206
207 extern int read_replace_refs;
208
209 static int commit_graph_compatible(struct repository *r)
210 {
211 if (!r->gitdir)
212 return 0;
213
214 if (read_replace_refs) {
215 prepare_replace_object(r);
216 if (hashmap_get_size(&r->objects->replace_map->map))
217 return 0;
218 }
219
220 prepare_commit_graft(r);
221 if (r->parsed_objects &&
222 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
223 return 0;
224 if (is_repository_shallow(r))
225 return 0;
226
227 return 1;
228 }
229
230 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
231 {
232 *fd = git_open(graph_file);
233 if (*fd < 0)
234 return 0;
235 if (fstat(*fd, st)) {
236 close(*fd);
237 return 0;
238 }
239 return 1;
240 }
241
242 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
243 int fd, struct stat *st,
244 struct object_directory *odb)
245 {
246 void *graph_map;
247 size_t graph_size;
248 struct commit_graph *ret;
249
250 graph_size = xsize_t(st->st_size);
251
252 if (graph_size < GRAPH_MIN_SIZE) {
253 close(fd);
254 error(_("commit-graph file is too small"));
255 return NULL;
256 }
257 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
258 close(fd);
259 prepare_repo_settings(r);
260 ret = parse_commit_graph(&r->settings, graph_map, graph_size);
261
262 if (ret)
263 ret->odb = odb;
264 else
265 munmap(graph_map, graph_size);
266
267 return ret;
268 }
269
270 static int verify_commit_graph_lite(struct commit_graph *g)
271 {
272 /*
273 * Basic validation shared between parse_commit_graph()
274 * which'll be called every time the graph is used, and the
275 * much more expensive verify_commit_graph() used by
276 * "commit-graph verify".
277 *
278 * There should only be very basic checks here to ensure that
279 * we don't e.g. segfault in fill_commit_in_graph(), but
280 * because this is a very hot codepath nothing that e.g. loops
281 * over g->num_commits, or runs a checksum on the commit-graph
282 * itself.
283 */
284 if (!g->chunk_oid_fanout) {
285 error("commit-graph is missing the OID Fanout chunk");
286 return 1;
287 }
288 if (!g->chunk_oid_lookup) {
289 error("commit-graph is missing the OID Lookup chunk");
290 return 1;
291 }
292 if (!g->chunk_commit_data) {
293 error("commit-graph is missing the Commit Data chunk");
294 return 1;
295 }
296
297 return 0;
298 }
299
300 static int graph_read_oid_lookup(const unsigned char *chunk_start,
301 size_t chunk_size, void *data)
302 {
303 struct commit_graph *g = data;
304 g->chunk_oid_lookup = chunk_start;
305 g->num_commits = chunk_size / g->hash_len;
306 return 0;
307 }
308
309 static int graph_read_bloom_data(const unsigned char *chunk_start,
310 size_t chunk_size, void *data)
311 {
312 struct commit_graph *g = data;
313 uint32_t hash_version;
314 g->chunk_bloom_data = chunk_start;
315 hash_version = get_be32(chunk_start);
316
317 if (hash_version != 1)
318 return 0;
319
320 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
321 g->bloom_filter_settings->hash_version = hash_version;
322 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
323 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
324 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
325
326 return 0;
327 }
328
329 struct commit_graph *parse_commit_graph(struct repo_settings *s,
330 void *graph_map, size_t graph_size)
331 {
332 const unsigned char *data;
333 struct commit_graph *graph;
334 uint32_t graph_signature;
335 unsigned char graph_version, hash_version;
336 struct chunkfile *cf = NULL;
337
338 if (!graph_map)
339 return NULL;
340
341 if (graph_size < GRAPH_MIN_SIZE)
342 return NULL;
343
344 data = (const unsigned char *)graph_map;
345
346 graph_signature = get_be32(data);
347 if (graph_signature != GRAPH_SIGNATURE) {
348 error(_("commit-graph signature %X does not match signature %X"),
349 graph_signature, GRAPH_SIGNATURE);
350 return NULL;
351 }
352
353 graph_version = *(unsigned char*)(data + 4);
354 if (graph_version != GRAPH_VERSION) {
355 error(_("commit-graph version %X does not match version %X"),
356 graph_version, GRAPH_VERSION);
357 return NULL;
358 }
359
360 hash_version = *(unsigned char*)(data + 5);
361 if (hash_version != oid_version(the_hash_algo)) {
362 error(_("commit-graph hash version %X does not match version %X"),
363 hash_version, oid_version(the_hash_algo));
364 return NULL;
365 }
366
367 graph = alloc_commit_graph();
368
369 graph->hash_len = the_hash_algo->rawsz;
370 graph->num_chunks = *(unsigned char*)(data + 6);
371 graph->data = graph_map;
372 graph->data_len = graph_size;
373
374 if (graph_size < GRAPH_HEADER_SIZE +
375 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
376 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
377 error(_("commit-graph file is too small to hold %u chunks"),
378 graph->num_chunks);
379 free(graph);
380 return NULL;
381 }
382
383 cf = init_chunkfile(NULL);
384
385 if (read_table_of_contents(cf, graph->data, graph_size,
386 GRAPH_HEADER_SIZE, graph->num_chunks))
387 goto free_and_return;
388
389 pair_chunk(cf, GRAPH_CHUNKID_OIDFANOUT,
390 (const unsigned char **)&graph->chunk_oid_fanout);
391 read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph);
392 pair_chunk(cf, GRAPH_CHUNKID_DATA, &graph->chunk_commit_data);
393 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges);
394 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs);
395
396 if (s->commit_graph_generation_version >= 2) {
397 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
398 &graph->chunk_generation_data);
399 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
400 &graph->chunk_generation_data_overflow);
401
402 if (graph->chunk_generation_data)
403 graph->read_generation_data = 1;
404 }
405
406 if (s->commit_graph_read_changed_paths) {
407 pair_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
408 &graph->chunk_bloom_indexes);
409 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
410 graph_read_bloom_data, graph);
411 }
412
413 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
414 init_bloom_filters();
415 } else {
416 /* We need both the bloom chunks to exist together. Else ignore the data */
417 graph->chunk_bloom_indexes = NULL;
418 graph->chunk_bloom_data = NULL;
419 FREE_AND_NULL(graph->bloom_filter_settings);
420 }
421
422 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
423
424 if (verify_commit_graph_lite(graph))
425 goto free_and_return;
426
427 free_chunkfile(cf);
428 return graph;
429
430 free_and_return:
431 free_chunkfile(cf);
432 free(graph->bloom_filter_settings);
433 free(graph);
434 return NULL;
435 }
436
437 static struct commit_graph *load_commit_graph_one(struct repository *r,
438 const char *graph_file,
439 struct object_directory *odb)
440 {
441
442 struct stat st;
443 int fd;
444 struct commit_graph *g;
445 int open_ok = open_commit_graph(graph_file, &fd, &st);
446
447 if (!open_ok)
448 return NULL;
449
450 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
451
452 if (g)
453 g->filename = xstrdup(graph_file);
454
455 return g;
456 }
457
458 static struct commit_graph *load_commit_graph_v1(struct repository *r,
459 struct object_directory *odb)
460 {
461 char *graph_name = get_commit_graph_filename(odb);
462 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
463 free(graph_name);
464
465 return g;
466 }
467
468 static int add_graph_to_chain(struct commit_graph *g,
469 struct commit_graph *chain,
470 struct object_id *oids,
471 int n)
472 {
473 struct commit_graph *cur_g = chain;
474
475 if (n && !g->chunk_base_graphs) {
476 warning(_("commit-graph has no base graphs chunk"));
477 return 0;
478 }
479
480 while (n) {
481 n--;
482
483 if (!cur_g ||
484 !oideq(&oids[n], &cur_g->oid) ||
485 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
486 warning(_("commit-graph chain does not match"));
487 return 0;
488 }
489
490 cur_g = cur_g->base_graph;
491 }
492
493 g->base_graph = chain;
494
495 if (chain)
496 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
497
498 return 1;
499 }
500
501 static struct commit_graph *load_commit_graph_chain(struct repository *r,
502 struct object_directory *odb)
503 {
504 struct commit_graph *graph_chain = NULL;
505 struct strbuf line = STRBUF_INIT;
506 struct stat st;
507 struct object_id *oids;
508 int i = 0, valid = 1, count;
509 char *chain_name = get_commit_graph_chain_filename(odb);
510 FILE *fp;
511 int stat_res;
512
513 fp = fopen(chain_name, "r");
514 stat_res = stat(chain_name, &st);
515 free(chain_name);
516
517 if (!fp)
518 return NULL;
519 if (stat_res ||
520 st.st_size <= the_hash_algo->hexsz) {
521 fclose(fp);
522 return NULL;
523 }
524
525 count = st.st_size / (the_hash_algo->hexsz + 1);
526 CALLOC_ARRAY(oids, count);
527
528 prepare_alt_odb(r);
529
530 for (i = 0; i < count; i++) {
531 struct object_directory *odb;
532
533 if (strbuf_getline_lf(&line, fp) == EOF)
534 break;
535
536 if (get_oid_hex(line.buf, &oids[i])) {
537 warning(_("invalid commit-graph chain: line '%s' not a hash"),
538 line.buf);
539 valid = 0;
540 break;
541 }
542
543 valid = 0;
544 for (odb = r->objects->odb; odb; odb = odb->next) {
545 char *graph_name = get_split_graph_filename(odb, line.buf);
546 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
547
548 free(graph_name);
549
550 if (g) {
551 if (add_graph_to_chain(g, graph_chain, oids, i)) {
552 graph_chain = g;
553 valid = 1;
554 }
555
556 break;
557 }
558 }
559
560 if (!valid) {
561 warning(_("unable to find all commit-graph files"));
562 break;
563 }
564 }
565
566 free(oids);
567 fclose(fp);
568 strbuf_release(&line);
569
570 return graph_chain;
571 }
572
573 /*
574 * returns 1 if and only if all graphs in the chain have
575 * corrected commit dates stored in the generation_data chunk.
576 */
577 static int validate_mixed_generation_chain(struct commit_graph *g)
578 {
579 int read_generation_data = 1;
580 struct commit_graph *p = g;
581
582 while (read_generation_data && p) {
583 read_generation_data = p->read_generation_data;
584 p = p->base_graph;
585 }
586
587 if (read_generation_data)
588 return 1;
589
590 while (g) {
591 g->read_generation_data = 0;
592 g = g->base_graph;
593 }
594
595 return 0;
596 }
597
598 struct commit_graph *read_commit_graph_one(struct repository *r,
599 struct object_directory *odb)
600 {
601 struct commit_graph *g = load_commit_graph_v1(r, odb);
602
603 if (!g)
604 g = load_commit_graph_chain(r, odb);
605
606 validate_mixed_generation_chain(g);
607
608 return g;
609 }
610
611 static void prepare_commit_graph_one(struct repository *r,
612 struct object_directory *odb)
613 {
614
615 if (r->objects->commit_graph)
616 return;
617
618 r->objects->commit_graph = read_commit_graph_one(r, odb);
619 }
620
621 /*
622 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
623 *
624 * On the first invocation, this function attempts to load the commit
625 * graph if the_repository is configured to have one.
626 */
627 static int prepare_commit_graph(struct repository *r)
628 {
629 struct object_directory *odb;
630
631 /*
632 * Early return if there is no git dir or if the commit graph is
633 * disabled.
634 *
635 * This must come before the "already attempted?" check below, because
636 * we want to disable even an already-loaded graph file.
637 */
638 if (!r->gitdir || r->commit_graph_disabled)
639 return 0;
640
641 if (r->objects->commit_graph_attempted)
642 return !!r->objects->commit_graph;
643 r->objects->commit_graph_attempted = 1;
644
645 prepare_repo_settings(r);
646
647 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
648 r->settings.core_commit_graph != 1)
649 /*
650 * This repository is not configured to use commit graphs, so
651 * do not load one. (But report commit_graph_attempted anyway
652 * so that commit graph loading is not attempted again for this
653 * repository.)
654 */
655 return 0;
656
657 if (!commit_graph_compatible(r))
658 return 0;
659
660 prepare_alt_odb(r);
661 for (odb = r->objects->odb;
662 !r->objects->commit_graph && odb;
663 odb = odb->next)
664 prepare_commit_graph_one(r, odb);
665 return !!r->objects->commit_graph;
666 }
667
668 int generation_numbers_enabled(struct repository *r)
669 {
670 uint32_t first_generation;
671 struct commit_graph *g;
672 if (!prepare_commit_graph(r))
673 return 0;
674
675 g = r->objects->commit_graph;
676
677 if (!g->num_commits)
678 return 0;
679
680 first_generation = get_be32(g->chunk_commit_data +
681 g->hash_len + 8) >> 2;
682
683 return !!first_generation;
684 }
685
686 int corrected_commit_dates_enabled(struct repository *r)
687 {
688 struct commit_graph *g;
689 if (!prepare_commit_graph(r))
690 return 0;
691
692 g = r->objects->commit_graph;
693
694 if (!g->num_commits)
695 return 0;
696
697 return g->read_generation_data;
698 }
699
700 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
701 {
702 struct commit_graph *g = r->objects->commit_graph;
703 while (g) {
704 if (g->bloom_filter_settings)
705 return g->bloom_filter_settings;
706 g = g->base_graph;
707 }
708 return NULL;
709 }
710
711 static void close_commit_graph_one(struct commit_graph *g)
712 {
713 if (!g)
714 return;
715
716 clear_commit_graph_data_slab(&commit_graph_data_slab);
717 close_commit_graph_one(g->base_graph);
718 free_commit_graph(g);
719 }
720
721 void close_commit_graph(struct raw_object_store *o)
722 {
723 close_commit_graph_one(o->commit_graph);
724 o->commit_graph = NULL;
725 }
726
727 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
728 {
729 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
730 g->chunk_oid_lookup, g->hash_len, pos);
731 }
732
733 static void load_oid_from_graph(struct commit_graph *g,
734 uint32_t pos,
735 struct object_id *oid)
736 {
737 uint32_t lex_index;
738
739 while (g && pos < g->num_commits_in_base)
740 g = g->base_graph;
741
742 if (!g)
743 BUG("NULL commit-graph");
744
745 if (pos >= g->num_commits + g->num_commits_in_base)
746 die(_("invalid commit position. commit-graph is likely corrupt"));
747
748 lex_index = pos - g->num_commits_in_base;
749
750 oidread(oid, g->chunk_oid_lookup + g->hash_len * lex_index);
751 }
752
753 static struct commit_list **insert_parent_or_die(struct repository *r,
754 struct commit_graph *g,
755 uint32_t pos,
756 struct commit_list **pptr)
757 {
758 struct commit *c;
759 struct object_id oid;
760
761 if (pos >= g->num_commits + g->num_commits_in_base)
762 die("invalid parent position %"PRIu32, pos);
763
764 load_oid_from_graph(g, pos, &oid);
765 c = lookup_commit(r, &oid);
766 if (!c)
767 die(_("could not find commit %s"), oid_to_hex(&oid));
768 commit_graph_data_at(c)->graph_pos = pos;
769 return &commit_list_insert(c, pptr)->next;
770 }
771
772 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
773 {
774 const unsigned char *commit_data;
775 struct commit_graph_data *graph_data;
776 uint32_t lex_index, offset_pos;
777 uint64_t date_high, date_low, offset;
778
779 while (pos < g->num_commits_in_base)
780 g = g->base_graph;
781
782 if (pos >= g->num_commits + g->num_commits_in_base)
783 die(_("invalid commit position. commit-graph is likely corrupt"));
784
785 lex_index = pos - g->num_commits_in_base;
786 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
787
788 graph_data = commit_graph_data_at(item);
789 graph_data->graph_pos = pos;
790
791 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
792 date_low = get_be32(commit_data + g->hash_len + 12);
793 item->date = (timestamp_t)((date_high << 32) | date_low);
794
795 if (g->read_generation_data) {
796 offset = (timestamp_t)get_be32(g->chunk_generation_data + sizeof(uint32_t) * lex_index);
797
798 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
799 if (!g->chunk_generation_data_overflow)
800 die(_("commit-graph requires overflow generation data but has none"));
801
802 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
803 graph_data->generation = item->date + get_be64(g->chunk_generation_data_overflow + 8 * offset_pos);
804 } else
805 graph_data->generation = item->date + offset;
806 } else
807 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
808
809 if (g->topo_levels)
810 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
811 }
812
813 static inline void set_commit_tree(struct commit *c, struct tree *t)
814 {
815 c->maybe_tree = t;
816 }
817
818 static int fill_commit_in_graph(struct repository *r,
819 struct commit *item,
820 struct commit_graph *g, uint32_t pos)
821 {
822 uint32_t edge_value;
823 uint32_t *parent_data_ptr;
824 struct commit_list **pptr;
825 const unsigned char *commit_data;
826 uint32_t lex_index;
827
828 while (pos < g->num_commits_in_base)
829 g = g->base_graph;
830
831 fill_commit_graph_info(item, g, pos);
832
833 lex_index = pos - g->num_commits_in_base;
834 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
835
836 item->object.parsed = 1;
837
838 set_commit_tree(item, NULL);
839
840 pptr = &item->parents;
841
842 edge_value = get_be32(commit_data + g->hash_len);
843 if (edge_value == GRAPH_PARENT_NONE)
844 return 1;
845 pptr = insert_parent_or_die(r, g, edge_value, pptr);
846
847 edge_value = get_be32(commit_data + g->hash_len + 4);
848 if (edge_value == GRAPH_PARENT_NONE)
849 return 1;
850 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
851 pptr = insert_parent_or_die(r, g, edge_value, pptr);
852 return 1;
853 }
854
855 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
856 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
857 do {
858 edge_value = get_be32(parent_data_ptr);
859 pptr = insert_parent_or_die(r, g,
860 edge_value & GRAPH_EDGE_LAST_MASK,
861 pptr);
862 parent_data_ptr++;
863 } while (!(edge_value & GRAPH_LAST_EDGE));
864
865 return 1;
866 }
867
868 static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
869 {
870 struct commit_graph *cur_g = g;
871 uint32_t lex_index;
872
873 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
874 cur_g = cur_g->base_graph;
875
876 if (cur_g) {
877 *pos = lex_index + cur_g->num_commits_in_base;
878 return 1;
879 }
880
881 return 0;
882 }
883
884 static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
885 {
886 uint32_t graph_pos = commit_graph_position(item);
887 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
888 *pos = graph_pos;
889 return 1;
890 } else {
891 return search_commit_pos_in_graph(&item->object.oid, g, pos);
892 }
893 }
894
895 int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
896 uint32_t *pos)
897 {
898 if (!prepare_commit_graph(r))
899 return 0;
900 return find_commit_pos_in_graph(c, r->objects->commit_graph, pos);
901 }
902
903 struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
904 {
905 struct commit *commit;
906 uint32_t pos;
907
908 if (!prepare_commit_graph(repo))
909 return NULL;
910 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
911 return NULL;
912 if (!has_object(repo, id, 0))
913 return NULL;
914
915 commit = lookup_commit(repo, id);
916 if (!commit)
917 return NULL;
918 if (commit->object.parsed)
919 return commit;
920
921 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
922 return NULL;
923
924 return commit;
925 }
926
927 static int parse_commit_in_graph_one(struct repository *r,
928 struct commit_graph *g,
929 struct commit *item)
930 {
931 uint32_t pos;
932
933 if (item->object.parsed)
934 return 1;
935
936 if (find_commit_pos_in_graph(item, g, &pos))
937 return fill_commit_in_graph(r, item, g, pos);
938
939 return 0;
940 }
941
942 int parse_commit_in_graph(struct repository *r, struct commit *item)
943 {
944 static int checked_env = 0;
945
946 if (!checked_env &&
947 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
948 die("dying as requested by the '%s' variable on commit-graph parse!",
949 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
950 checked_env = 1;
951
952 if (!prepare_commit_graph(r))
953 return 0;
954 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
955 }
956
957 void load_commit_graph_info(struct repository *r, struct commit *item)
958 {
959 uint32_t pos;
960 if (repo_find_commit_pos_in_graph(r, item, &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 DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1602
1603 if (ctx->order_by_pack)
1604 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1605 else
1606 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1607
1608 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1609 ctx->opts->max_new_filters : ctx->commits.nr;
1610
1611 for (i = 0; i < ctx->commits.nr; i++) {
1612 enum bloom_filter_computed computed = 0;
1613 struct commit *c = sorted_commits[i];
1614 struct bloom_filter *filter = get_or_compute_bloom_filter(
1615 ctx->r,
1616 c,
1617 ctx->count_bloom_filter_computed < max_new_filters,
1618 ctx->bloom_settings,
1619 &computed);
1620 if (computed & BLOOM_COMPUTED) {
1621 ctx->count_bloom_filter_computed++;
1622 if (computed & BLOOM_TRUNC_EMPTY)
1623 ctx->count_bloom_filter_trunc_empty++;
1624 if (computed & BLOOM_TRUNC_LARGE)
1625 ctx->count_bloom_filter_trunc_large++;
1626 } else if (computed & BLOOM_NOT_COMPUTED)
1627 ctx->count_bloom_filter_not_computed++;
1628 ctx->total_bloom_filter_data_size += filter
1629 ? sizeof(unsigned char) * filter->len : 0;
1630 display_progress(progress, i + 1);
1631 }
1632
1633 if (trace2_is_enabled())
1634 trace2_bloom_filter_write_statistics(ctx);
1635
1636 free(sorted_commits);
1637 stop_progress(&progress);
1638 }
1639
1640 struct refs_cb_data {
1641 struct oidset *commits;
1642 struct progress *progress;
1643 };
1644
1645 static int add_ref_to_set(const char *refname UNUSED,
1646 const struct object_id *oid,
1647 int flags UNUSED, void *cb_data)
1648 {
1649 struct object_id peeled;
1650 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1651
1652 if (!peel_iterated_oid(oid, &peeled))
1653 oid = &peeled;
1654 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1655 oidset_insert(data->commits, oid);
1656
1657 display_progress(data->progress, oidset_size(data->commits));
1658
1659 return 0;
1660 }
1661
1662 int write_commit_graph_reachable(struct object_directory *odb,
1663 enum commit_graph_write_flags flags,
1664 const struct commit_graph_opts *opts)
1665 {
1666 struct oidset commits = OIDSET_INIT;
1667 struct refs_cb_data data;
1668 int result;
1669
1670 memset(&data, 0, sizeof(data));
1671 data.commits = &commits;
1672 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1673 data.progress = start_delayed_progress(
1674 _("Collecting referenced commits"), 0);
1675
1676 for_each_ref(add_ref_to_set, &data);
1677
1678 stop_progress(&data.progress);
1679
1680 result = write_commit_graph(odb, NULL, &commits,
1681 flags, opts);
1682
1683 oidset_clear(&commits);
1684 return result;
1685 }
1686
1687 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1688 const struct string_list *pack_indexes)
1689 {
1690 uint32_t i;
1691 struct strbuf progress_title = STRBUF_INIT;
1692 struct strbuf packname = STRBUF_INIT;
1693 int dirlen;
1694 int ret = 0;
1695
1696 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1697 dirlen = packname.len;
1698 if (ctx->report_progress) {
1699 strbuf_addf(&progress_title,
1700 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1701 "Finding commits for commit graph in %"PRIuMAX" packs",
1702 pack_indexes->nr),
1703 (uintmax_t)pack_indexes->nr);
1704 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1705 ctx->progress_done = 0;
1706 }
1707 for (i = 0; i < pack_indexes->nr; i++) {
1708 struct packed_git *p;
1709 strbuf_setlen(&packname, dirlen);
1710 strbuf_addstr(&packname, pack_indexes->items[i].string);
1711 p = add_packed_git(packname.buf, packname.len, 1);
1712 if (!p) {
1713 ret = error(_("error adding pack %s"), packname.buf);
1714 goto cleanup;
1715 }
1716 if (open_pack_index(p)) {
1717 ret = error(_("error opening index for %s"), packname.buf);
1718 goto cleanup;
1719 }
1720 for_each_object_in_pack(p, add_packed_commits, ctx,
1721 FOR_EACH_OBJECT_PACK_ORDER);
1722 close_pack(p);
1723 free(p);
1724 }
1725
1726 cleanup:
1727 stop_progress(&ctx->progress);
1728 strbuf_release(&progress_title);
1729 strbuf_release(&packname);
1730
1731 return ret;
1732 }
1733
1734 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1735 struct oidset *commits)
1736 {
1737 struct oidset_iter iter;
1738 struct object_id *oid;
1739
1740 if (!oidset_size(commits))
1741 return 0;
1742
1743 oidset_iter_init(commits, &iter);
1744 while ((oid = oidset_iter_next(&iter))) {
1745 oid_array_append(&ctx->oids, oid);
1746 }
1747
1748 return 0;
1749 }
1750
1751 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1752 {
1753 if (ctx->report_progress)
1754 ctx->progress = start_delayed_progress(
1755 _("Finding commits for commit graph among packed objects"),
1756 ctx->approx_nr_objects);
1757 for_each_packed_object(add_packed_commits, ctx,
1758 FOR_EACH_OBJECT_PACK_ORDER);
1759 if (ctx->progress_done < ctx->approx_nr_objects)
1760 display_progress(ctx->progress, ctx->approx_nr_objects);
1761 stop_progress(&ctx->progress);
1762 }
1763
1764 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1765 {
1766 uint32_t i;
1767 enum commit_graph_split_flags flags = ctx->opts ?
1768 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1769
1770 ctx->num_extra_edges = 0;
1771 if (ctx->report_progress)
1772 ctx->progress = start_delayed_progress(
1773 _("Finding extra edges in commit graph"),
1774 ctx->oids.nr);
1775 oid_array_sort(&ctx->oids);
1776 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1777 unsigned int num_parents;
1778
1779 display_progress(ctx->progress, i + 1);
1780
1781 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1782 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1783
1784 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1785 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1786 continue;
1787
1788 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1789 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1790 else
1791 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1792
1793 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1794 if (num_parents > 2)
1795 ctx->num_extra_edges += num_parents - 1;
1796
1797 ctx->commits.nr++;
1798 }
1799 stop_progress(&ctx->progress);
1800 }
1801
1802 static int write_graph_chunk_base_1(struct hashfile *f,
1803 struct commit_graph *g)
1804 {
1805 int num = 0;
1806
1807 if (!g)
1808 return 0;
1809
1810 num = write_graph_chunk_base_1(f, g->base_graph);
1811 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1812 return num + 1;
1813 }
1814
1815 static int write_graph_chunk_base(struct hashfile *f,
1816 void *data)
1817 {
1818 struct write_commit_graph_context *ctx = data;
1819 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1820
1821 if (num != ctx->num_commit_graphs_after - 1) {
1822 error(_("failed to write correct number of base graph ids"));
1823 return -1;
1824 }
1825
1826 return 0;
1827 }
1828
1829 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1830 {
1831 uint32_t i;
1832 int fd;
1833 struct hashfile *f;
1834 struct lock_file lk = LOCK_INIT;
1835 const unsigned hashsz = the_hash_algo->rawsz;
1836 struct strbuf progress_title = STRBUF_INIT;
1837 struct chunkfile *cf;
1838 unsigned char file_hash[GIT_MAX_RAWSZ];
1839
1840 if (ctx->split) {
1841 struct strbuf tmp_file = STRBUF_INIT;
1842
1843 strbuf_addf(&tmp_file,
1844 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1845 ctx->odb->path);
1846 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1847 } else {
1848 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1849 }
1850
1851 if (safe_create_leading_directories(ctx->graph_name)) {
1852 UNLEAK(ctx->graph_name);
1853 error(_("unable to create leading directories of %s"),
1854 ctx->graph_name);
1855 return -1;
1856 }
1857
1858 if (ctx->split) {
1859 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
1860
1861 hold_lock_file_for_update_mode(&lk, lock_name,
1862 LOCK_DIE_ON_ERROR, 0444);
1863 free(lock_name);
1864
1865 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1866 if (fd < 0) {
1867 error(_("unable to create temporary graph layer"));
1868 return -1;
1869 }
1870
1871 if (adjust_shared_perm(ctx->graph_name)) {
1872 error(_("unable to adjust shared permissions for '%s'"),
1873 ctx->graph_name);
1874 return -1;
1875 }
1876
1877 f = hashfd(fd, ctx->graph_name);
1878 } else {
1879 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1880 LOCK_DIE_ON_ERROR, 0444);
1881 fd = get_lock_file_fd(&lk);
1882 f = hashfd(fd, get_lock_file_path(&lk));
1883 }
1884
1885 cf = init_chunkfile(f);
1886
1887 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
1888 write_graph_chunk_fanout);
1889 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, hashsz * ctx->commits.nr,
1890 write_graph_chunk_oids);
1891 add_chunk(cf, GRAPH_CHUNKID_DATA, (hashsz + 16) * ctx->commits.nr,
1892 write_graph_chunk_data);
1893
1894 if (ctx->write_generation_data)
1895 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
1896 sizeof(uint32_t) * ctx->commits.nr,
1897 write_graph_chunk_generation_data);
1898 if (ctx->num_generation_data_overflows)
1899 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
1900 sizeof(timestamp_t) * ctx->num_generation_data_overflows,
1901 write_graph_chunk_generation_data_overflow);
1902 if (ctx->num_extra_edges)
1903 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
1904 4 * ctx->num_extra_edges,
1905 write_graph_chunk_extra_edges);
1906 if (ctx->changed_paths) {
1907 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
1908 sizeof(uint32_t) * ctx->commits.nr,
1909 write_graph_chunk_bloom_indexes);
1910 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
1911 sizeof(uint32_t) * 3
1912 + ctx->total_bloom_filter_data_size,
1913 write_graph_chunk_bloom_data);
1914 }
1915 if (ctx->num_commit_graphs_after > 1)
1916 add_chunk(cf, GRAPH_CHUNKID_BASE,
1917 hashsz * (ctx->num_commit_graphs_after - 1),
1918 write_graph_chunk_base);
1919
1920 hashwrite_be32(f, GRAPH_SIGNATURE);
1921
1922 hashwrite_u8(f, GRAPH_VERSION);
1923 hashwrite_u8(f, oid_version(the_hash_algo));
1924 hashwrite_u8(f, get_num_chunks(cf));
1925 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1926
1927 if (ctx->report_progress) {
1928 strbuf_addf(&progress_title,
1929 Q_("Writing out commit graph in %d pass",
1930 "Writing out commit graph in %d passes",
1931 get_num_chunks(cf)),
1932 get_num_chunks(cf));
1933 ctx->progress = start_delayed_progress(
1934 progress_title.buf,
1935 get_num_chunks(cf) * ctx->commits.nr);
1936 }
1937
1938 write_chunkfile(cf, ctx);
1939
1940 stop_progress(&ctx->progress);
1941 strbuf_release(&progress_title);
1942
1943 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1944 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1945 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
1946
1947 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1948 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1949 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1950 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1951 }
1952
1953 close_commit_graph(ctx->r->objects);
1954 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
1955 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1956 free_chunkfile(cf);
1957
1958 if (ctx->split) {
1959 FILE *chainf = fdopen_lock_file(&lk, "w");
1960 char *final_graph_name;
1961 int result;
1962
1963 close(fd);
1964
1965 if (!chainf) {
1966 error(_("unable to open commit-graph chain file"));
1967 return -1;
1968 }
1969
1970 if (ctx->base_graph_name) {
1971 const char *dest;
1972 int idx = ctx->num_commit_graphs_after - 1;
1973 if (ctx->num_commit_graphs_after > 1)
1974 idx--;
1975
1976 dest = ctx->commit_graph_filenames_after[idx];
1977
1978 if (strcmp(ctx->base_graph_name, dest)) {
1979 result = rename(ctx->base_graph_name, dest);
1980
1981 if (result) {
1982 error(_("failed to rename base commit-graph file"));
1983 return -1;
1984 }
1985 }
1986 } else {
1987 char *graph_name = get_commit_graph_filename(ctx->odb);
1988 unlink(graph_name);
1989 free(graph_name);
1990 }
1991
1992 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
1993 final_graph_name = get_split_graph_filename(ctx->odb,
1994 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1995 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1996
1997 result = rename(ctx->graph_name, final_graph_name);
1998
1999 for (i = 0; i < ctx->num_commit_graphs_after; i++)
2000 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2001
2002 if (result) {
2003 error(_("failed to rename temporary commit-graph file"));
2004 return -1;
2005 }
2006 }
2007
2008 commit_lock_file(&lk);
2009
2010 return 0;
2011 }
2012
2013 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2014 {
2015 struct commit_graph *g;
2016 uint32_t num_commits;
2017 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2018 uint32_t i;
2019
2020 int max_commits = 0;
2021 int size_mult = 2;
2022
2023 if (ctx->opts) {
2024 max_commits = ctx->opts->max_commits;
2025
2026 if (ctx->opts->size_multiple)
2027 size_mult = ctx->opts->size_multiple;
2028
2029 flags = ctx->opts->split_flags;
2030 }
2031
2032 g = ctx->r->objects->commit_graph;
2033 num_commits = ctx->commits.nr;
2034 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2035 ctx->num_commit_graphs_after = 1;
2036 else
2037 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2038
2039 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2040 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2041 while (g && (g->num_commits <= size_mult * num_commits ||
2042 (max_commits && num_commits > max_commits))) {
2043 if (g->odb != ctx->odb)
2044 break;
2045
2046 num_commits += g->num_commits;
2047 g = g->base_graph;
2048
2049 ctx->num_commit_graphs_after--;
2050 }
2051 }
2052
2053 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2054 ctx->new_base_graph = g;
2055 else if (ctx->num_commit_graphs_after != 1)
2056 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2057 "should be 1 with --split=replace");
2058
2059 if (ctx->num_commit_graphs_after == 2) {
2060 char *old_graph_name = get_commit_graph_filename(g->odb);
2061
2062 if (!strcmp(g->filename, old_graph_name) &&
2063 g->odb != ctx->odb) {
2064 ctx->num_commit_graphs_after = 1;
2065 ctx->new_base_graph = NULL;
2066 }
2067
2068 free(old_graph_name);
2069 }
2070
2071 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2072 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2073
2074 for (i = 0; i < ctx->num_commit_graphs_after &&
2075 i < ctx->num_commit_graphs_before; i++)
2076 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2077
2078 i = ctx->num_commit_graphs_before - 1;
2079 g = ctx->r->objects->commit_graph;
2080
2081 while (g) {
2082 if (i < ctx->num_commit_graphs_after)
2083 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2084
2085 /*
2086 * If the topmost remaining layer has generation data chunk, the
2087 * resultant layer also has generation data chunk.
2088 */
2089 if (i == ctx->num_commit_graphs_after - 2)
2090 ctx->write_generation_data = !!g->chunk_generation_data;
2091
2092 i--;
2093 g = g->base_graph;
2094 }
2095 }
2096
2097 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2098 struct commit_graph *g)
2099 {
2100 uint32_t i;
2101 uint32_t offset = g->num_commits_in_base;
2102
2103 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2104
2105 for (i = 0; i < g->num_commits; i++) {
2106 struct object_id oid;
2107 struct commit *result;
2108
2109 display_progress(ctx->progress, i + 1);
2110
2111 load_oid_from_graph(g, i + offset, &oid);
2112
2113 /* only add commits if they still exist in the repo */
2114 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2115
2116 if (result) {
2117 ctx->commits.list[ctx->commits.nr] = result;
2118 ctx->commits.nr++;
2119 }
2120 }
2121 }
2122
2123 static int commit_compare(const void *_a, const void *_b)
2124 {
2125 const struct commit *a = *(const struct commit **)_a;
2126 const struct commit *b = *(const struct commit **)_b;
2127 return oidcmp(&a->object.oid, &b->object.oid);
2128 }
2129
2130 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2131 {
2132 uint32_t i, dedup_i = 0;
2133
2134 if (ctx->report_progress)
2135 ctx->progress = start_delayed_progress(
2136 _("Scanning merged commits"),
2137 ctx->commits.nr);
2138
2139 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2140
2141 ctx->num_extra_edges = 0;
2142 for (i = 0; i < ctx->commits.nr; i++) {
2143 display_progress(ctx->progress, i + 1);
2144
2145 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2146 &ctx->commits.list[i]->object.oid)) {
2147 /*
2148 * Silently ignore duplicates. These were likely
2149 * created due to a commit appearing in multiple
2150 * layers of the chain, which is unexpected but
2151 * not invalid. We should make sure there is a
2152 * unique copy in the new layer.
2153 */
2154 } else {
2155 unsigned int num_parents;
2156
2157 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2158 dedup_i++;
2159
2160 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2161 if (num_parents > 2)
2162 ctx->num_extra_edges += num_parents - 1;
2163 }
2164 }
2165
2166 ctx->commits.nr = dedup_i;
2167
2168 stop_progress(&ctx->progress);
2169 }
2170
2171 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2172 {
2173 struct commit_graph *g = ctx->r->objects->commit_graph;
2174 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2175
2176 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2177 current_graph_number--;
2178
2179 if (ctx->report_progress)
2180 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2181
2182 merge_commit_graph(ctx, g);
2183 stop_progress(&ctx->progress);
2184
2185 g = g->base_graph;
2186 }
2187
2188 if (g) {
2189 ctx->new_base_graph = g;
2190 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2191 }
2192
2193 if (ctx->new_base_graph)
2194 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2195
2196 sort_and_scan_merged_commits(ctx);
2197 }
2198
2199 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2200 {
2201 uint32_t i;
2202 time_t now = time(NULL);
2203
2204 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2205 struct stat st;
2206 struct utimbuf updated_time;
2207
2208 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2209 continue;
2210
2211 updated_time.actime = st.st_atime;
2212 updated_time.modtime = now;
2213 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2214 }
2215 }
2216
2217 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2218 {
2219 struct strbuf path = STRBUF_INIT;
2220 DIR *dir;
2221 struct dirent *de;
2222 size_t dirnamelen;
2223 timestamp_t expire_time = time(NULL);
2224
2225 if (ctx->opts && ctx->opts->expire_time)
2226 expire_time = ctx->opts->expire_time;
2227 if (!ctx->split) {
2228 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2229 unlink(chain_file_name);
2230 free(chain_file_name);
2231 ctx->num_commit_graphs_after = 0;
2232 }
2233
2234 strbuf_addstr(&path, ctx->odb->path);
2235 strbuf_addstr(&path, "/info/commit-graphs");
2236 dir = opendir(path.buf);
2237
2238 if (!dir)
2239 goto out;
2240
2241 strbuf_addch(&path, '/');
2242 dirnamelen = path.len;
2243 while ((de = readdir(dir)) != NULL) {
2244 struct stat st;
2245 uint32_t i, found = 0;
2246
2247 strbuf_setlen(&path, dirnamelen);
2248 strbuf_addstr(&path, de->d_name);
2249
2250 if (stat(path.buf, &st) < 0)
2251 continue;
2252
2253 if (st.st_mtime > expire_time)
2254 continue;
2255 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2256 continue;
2257
2258 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2259 if (!strcmp(ctx->commit_graph_filenames_after[i],
2260 path.buf)) {
2261 found = 1;
2262 break;
2263 }
2264 }
2265
2266 if (!found)
2267 unlink(path.buf);
2268 }
2269
2270 out:
2271 if(dir)
2272 closedir(dir);
2273 strbuf_release(&path);
2274 }
2275
2276 int write_commit_graph(struct object_directory *odb,
2277 const struct string_list *const pack_indexes,
2278 struct oidset *commits,
2279 enum commit_graph_write_flags flags,
2280 const struct commit_graph_opts *opts)
2281 {
2282 struct repository *r = the_repository;
2283 struct write_commit_graph_context *ctx;
2284 uint32_t i;
2285 int res = 0;
2286 int replace = 0;
2287 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2288 struct topo_level_slab topo_levels;
2289
2290 prepare_repo_settings(r);
2291 if (!r->settings.core_commit_graph) {
2292 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2293 return 0;
2294 }
2295 if (!commit_graph_compatible(r))
2296 return 0;
2297
2298 CALLOC_ARRAY(ctx, 1);
2299 ctx->r = r;
2300 ctx->odb = odb;
2301 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2302 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2303 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2304 ctx->opts = opts;
2305 ctx->total_bloom_filter_data_size = 0;
2306 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2307 ctx->num_generation_data_overflows = 0;
2308
2309 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2310 bloom_settings.bits_per_entry);
2311 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2312 bloom_settings.num_hashes);
2313 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2314 bloom_settings.max_changed_paths);
2315 ctx->bloom_settings = &bloom_settings;
2316
2317 init_topo_level_slab(&topo_levels);
2318 ctx->topo_levels = &topo_levels;
2319
2320 prepare_commit_graph(ctx->r);
2321 if (ctx->r->objects->commit_graph) {
2322 struct commit_graph *g = ctx->r->objects->commit_graph;
2323
2324 while (g) {
2325 g->topo_levels = &topo_levels;
2326 g = g->base_graph;
2327 }
2328 }
2329
2330 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2331 ctx->changed_paths = 1;
2332 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2333 struct commit_graph *g;
2334
2335 g = ctx->r->objects->commit_graph;
2336
2337 /* We have changed-paths already. Keep them in the next graph */
2338 if (g && g->chunk_bloom_data) {
2339 ctx->changed_paths = 1;
2340 ctx->bloom_settings = g->bloom_filter_settings;
2341 }
2342 }
2343
2344 if (ctx->split) {
2345 struct commit_graph *g = ctx->r->objects->commit_graph;
2346
2347 while (g) {
2348 ctx->num_commit_graphs_before++;
2349 g = g->base_graph;
2350 }
2351
2352 if (ctx->num_commit_graphs_before) {
2353 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2354 i = ctx->num_commit_graphs_before;
2355 g = ctx->r->objects->commit_graph;
2356
2357 while (g) {
2358 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2359 g = g->base_graph;
2360 }
2361 }
2362
2363 if (ctx->opts)
2364 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2365 }
2366
2367 ctx->approx_nr_objects = repo_approximate_object_count(the_repository);
2368
2369 if (ctx->append && ctx->r->objects->commit_graph) {
2370 struct commit_graph *g = ctx->r->objects->commit_graph;
2371 for (i = 0; i < g->num_commits; i++) {
2372 struct object_id oid;
2373 oidread(&oid, g->chunk_oid_lookup + g->hash_len * i);
2374 oid_array_append(&ctx->oids, &oid);
2375 }
2376 }
2377
2378 if (pack_indexes) {
2379 ctx->order_by_pack = 1;
2380 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2381 goto cleanup;
2382 }
2383
2384 if (commits) {
2385 if ((res = fill_oids_from_commits(ctx, commits)))
2386 goto cleanup;
2387 }
2388
2389 if (!pack_indexes && !commits) {
2390 ctx->order_by_pack = 1;
2391 fill_oids_from_all_packs(ctx);
2392 }
2393
2394 close_reachable(ctx);
2395
2396 copy_oids_to_commits(ctx);
2397
2398 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2399 error(_("too many commits to write graph"));
2400 res = -1;
2401 goto cleanup;
2402 }
2403
2404 if (!ctx->commits.nr && !replace)
2405 goto cleanup;
2406
2407 if (ctx->split) {
2408 split_graph_merge_strategy(ctx);
2409
2410 if (!replace)
2411 merge_commit_graphs(ctx);
2412 } else
2413 ctx->num_commit_graphs_after = 1;
2414
2415 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2416
2417 compute_topological_levels(ctx);
2418 if (ctx->write_generation_data)
2419 compute_generation_numbers(ctx);
2420
2421 if (ctx->changed_paths)
2422 compute_bloom_filters(ctx);
2423
2424 res = write_commit_graph_file(ctx);
2425
2426 if (ctx->split)
2427 mark_commit_graphs(ctx);
2428
2429 expire_commit_graphs(ctx);
2430
2431 cleanup:
2432 free(ctx->graph_name);
2433 free(ctx->commits.list);
2434 oid_array_clear(&ctx->oids);
2435 clear_topo_level_slab(&topo_levels);
2436
2437 if (ctx->commit_graph_filenames_after) {
2438 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2439 free(ctx->commit_graph_filenames_after[i]);
2440 free(ctx->commit_graph_hash_after[i]);
2441 }
2442
2443 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2444 free(ctx->commit_graph_filenames_before[i]);
2445
2446 free(ctx->commit_graph_filenames_after);
2447 free(ctx->commit_graph_filenames_before);
2448 free(ctx->commit_graph_hash_after);
2449 }
2450
2451 free(ctx);
2452
2453 return res;
2454 }
2455
2456 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2457 static int verify_commit_graph_error;
2458
2459 __attribute__((format (printf, 1, 2)))
2460 static void graph_report(const char *fmt, ...)
2461 {
2462 va_list ap;
2463
2464 verify_commit_graph_error = 1;
2465 va_start(ap, fmt);
2466 vfprintf(stderr, fmt, ap);
2467 fprintf(stderr, "\n");
2468 va_end(ap);
2469 }
2470
2471 #define GENERATION_ZERO_EXISTS 1
2472 #define GENERATION_NUMBER_EXISTS 2
2473
2474 static int commit_graph_checksum_valid(struct commit_graph *g)
2475 {
2476 return hashfile_checksum_valid(g->data, g->data_len);
2477 }
2478
2479 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2480 {
2481 uint32_t i, cur_fanout_pos = 0;
2482 struct object_id prev_oid, cur_oid;
2483 int generation_zero = 0;
2484 struct progress *progress = NULL;
2485 int local_error = 0;
2486
2487 if (!g) {
2488 graph_report("no commit-graph file loaded");
2489 return 1;
2490 }
2491
2492 verify_commit_graph_error = verify_commit_graph_lite(g);
2493 if (verify_commit_graph_error)
2494 return verify_commit_graph_error;
2495
2496 if (!commit_graph_checksum_valid(g)) {
2497 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2498 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2499 }
2500
2501 for (i = 0; i < g->num_commits; i++) {
2502 struct commit *graph_commit;
2503
2504 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2505
2506 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2507 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2508 oid_to_hex(&prev_oid),
2509 oid_to_hex(&cur_oid));
2510
2511 oidcpy(&prev_oid, &cur_oid);
2512
2513 while (cur_oid.hash[0] > cur_fanout_pos) {
2514 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2515
2516 if (i != fanout_value)
2517 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2518 cur_fanout_pos, fanout_value, i);
2519 cur_fanout_pos++;
2520 }
2521
2522 graph_commit = lookup_commit(r, &cur_oid);
2523 if (!parse_commit_in_graph_one(r, g, graph_commit))
2524 graph_report(_("failed to parse commit %s from commit-graph"),
2525 oid_to_hex(&cur_oid));
2526 }
2527
2528 while (cur_fanout_pos < 256) {
2529 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2530
2531 if (g->num_commits != fanout_value)
2532 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2533 cur_fanout_pos, fanout_value, i);
2534
2535 cur_fanout_pos++;
2536 }
2537
2538 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2539 return verify_commit_graph_error;
2540
2541 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2542 progress = start_progress(_("Verifying commits in commit graph"),
2543 g->num_commits);
2544
2545 for (i = 0; i < g->num_commits; i++) {
2546 struct commit *graph_commit, *odb_commit;
2547 struct commit_list *graph_parents, *odb_parents;
2548 timestamp_t max_generation = 0;
2549 timestamp_t generation;
2550
2551 display_progress(progress, i + 1);
2552 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2553
2554 graph_commit = lookup_commit(r, &cur_oid);
2555 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2556 if (repo_parse_commit_internal(r, odb_commit, 0, 0)) {
2557 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2558 oid_to_hex(&cur_oid));
2559 continue;
2560 }
2561
2562 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2563 get_commit_tree_oid(odb_commit)))
2564 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2565 oid_to_hex(&cur_oid),
2566 oid_to_hex(get_commit_tree_oid(graph_commit)),
2567 oid_to_hex(get_commit_tree_oid(odb_commit)));
2568
2569 graph_parents = graph_commit->parents;
2570 odb_parents = odb_commit->parents;
2571
2572 while (graph_parents) {
2573 if (!odb_parents) {
2574 graph_report(_("commit-graph parent list for commit %s is too long"),
2575 oid_to_hex(&cur_oid));
2576 break;
2577 }
2578
2579 /* parse parent in case it is in a base graph */
2580 parse_commit_in_graph_one(r, g, graph_parents->item);
2581
2582 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2583 graph_report(_("commit-graph parent for %s is %s != %s"),
2584 oid_to_hex(&cur_oid),
2585 oid_to_hex(&graph_parents->item->object.oid),
2586 oid_to_hex(&odb_parents->item->object.oid));
2587
2588 generation = commit_graph_generation(graph_parents->item);
2589 if (generation > max_generation)
2590 max_generation = generation;
2591
2592 graph_parents = graph_parents->next;
2593 odb_parents = odb_parents->next;
2594 }
2595
2596 if (odb_parents)
2597 graph_report(_("commit-graph parent list for commit %s terminates early"),
2598 oid_to_hex(&cur_oid));
2599
2600 if (!commit_graph_generation(graph_commit)) {
2601 if (generation_zero == GENERATION_NUMBER_EXISTS)
2602 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2603 oid_to_hex(&cur_oid));
2604 generation_zero = GENERATION_ZERO_EXISTS;
2605 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2606 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2607 oid_to_hex(&cur_oid));
2608
2609 if (generation_zero == GENERATION_ZERO_EXISTS)
2610 continue;
2611
2612 /*
2613 * If we are using topological level and one of our parents has
2614 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2615 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2616 * in the following condition.
2617 */
2618 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2619 max_generation--;
2620
2621 generation = commit_graph_generation(graph_commit);
2622 if (generation < max_generation + 1)
2623 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2624 oid_to_hex(&cur_oid),
2625 generation,
2626 max_generation + 1);
2627
2628 if (graph_commit->date != odb_commit->date)
2629 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2630 oid_to_hex(&cur_oid),
2631 graph_commit->date,
2632 odb_commit->date);
2633 }
2634 stop_progress(&progress);
2635
2636 local_error = verify_commit_graph_error;
2637
2638 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2639 local_error |= verify_commit_graph(r, g->base_graph, flags);
2640
2641 return local_error;
2642 }
2643
2644 void free_commit_graph(struct commit_graph *g)
2645 {
2646 if (!g)
2647 return;
2648 if (g->data) {
2649 munmap((void *)g->data, g->data_len);
2650 g->data = NULL;
2651 }
2652 free(g->filename);
2653 free(g->bloom_filter_settings);
2654 free(g);
2655 }
2656
2657 void disable_commit_graph(struct repository *r)
2658 {
2659 r->commit_graph_disabled = 1;
2660 }