replay: remove progress and info output
[alt-git.git] / commit-graph.c
blobc2b782af3b649fbf3deea7d90c959e10f951a003
1 #include "git-compat-util.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-file.h"
15 #include "object-store-ll.h"
16 #include "oid-array.h"
17 #include "path.h"
18 #include "alloc.h"
19 #include "hashmap.h"
20 #include "replace-object.h"
21 #include "progress.h"
22 #include "bloom.h"
23 #include "commit-slab.h"
24 #include "shallow.h"
25 #include "json-writer.h"
26 #include "trace2.h"
27 #include "tree.h"
28 #include "chunk-format.h"
30 void git_test_write_commit_graph_or_die(void)
32 int flags = 0;
33 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
34 return;
36 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
37 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
39 if (write_commit_graph_reachable(the_repository->objects->odb,
40 flags, NULL))
41 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
44 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
45 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
46 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
47 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
48 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
49 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
50 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
51 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
52 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
53 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
55 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
57 #define GRAPH_VERSION_1 0x1
58 #define GRAPH_VERSION GRAPH_VERSION_1
60 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
61 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
62 #define GRAPH_PARENT_NONE 0x70000000
64 #define GRAPH_LAST_EDGE 0x80000000
66 #define GRAPH_HEADER_SIZE 8
67 #define GRAPH_FANOUT_SIZE (4 * 256)
68 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
69 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
71 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
73 /* Remember to update object flag allocation in object.h */
74 #define REACHABLE (1u<<15)
76 define_commit_slab(topo_level_slab, uint32_t);
78 /* Keep track of the order in which commits are added to our list. */
79 define_commit_slab(commit_pos, int);
80 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
82 static void set_commit_pos(struct repository *r, const struct object_id *oid)
84 static int32_t max_pos;
85 struct commit *commit = lookup_commit(r, oid);
87 if (!commit)
88 return; /* should never happen, but be lenient */
90 *commit_pos_at(&commit_pos, commit) = max_pos++;
93 static int commit_pos_cmp(const void *va, const void *vb)
95 const struct commit *a = *(const struct commit **)va;
96 const struct commit *b = *(const struct commit **)vb;
97 return commit_pos_at(&commit_pos, a) -
98 commit_pos_at(&commit_pos, b);
101 define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
102 static struct commit_graph_data_slab commit_graph_data_slab =
103 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
105 static int get_configured_generation_version(struct repository *r)
107 int version = 2;
108 repo_config_get_int(r, "commitgraph.generationversion", &version);
109 return version;
112 uint32_t commit_graph_position(const struct commit *c)
114 struct commit_graph_data *data =
115 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
117 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
120 timestamp_t commit_graph_generation(const struct commit *c)
122 struct commit_graph_data *data =
123 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
125 if (data && data->generation)
126 return data->generation;
128 return GENERATION_NUMBER_INFINITY;
131 static timestamp_t commit_graph_generation_from_graph(const struct commit *c)
133 struct commit_graph_data *data =
134 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
136 if (!data || data->graph_pos == COMMIT_NOT_FROM_GRAPH)
137 return GENERATION_NUMBER_INFINITY;
138 return data->generation;
141 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
143 unsigned int i, nth_slab;
144 struct commit_graph_data *data =
145 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
147 if (data)
148 return data;
150 nth_slab = c->index / commit_graph_data_slab.slab_size;
151 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
154 * commit-slab initializes elements with zero, overwrite this with
155 * COMMIT_NOT_FROM_GRAPH for graph_pos.
157 * We avoid initializing generation with checking if graph position
158 * is not COMMIT_NOT_FROM_GRAPH.
160 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
161 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
162 COMMIT_NOT_FROM_GRAPH;
165 return data;
169 * Should be used only while writing commit-graph as it compares
170 * generation value of commits by directly accessing commit-slab.
172 static int commit_gen_cmp(const void *va, const void *vb)
174 const struct commit *a = *(const struct commit **)va;
175 const struct commit *b = *(const struct commit **)vb;
177 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
178 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
179 /* lower generation commits first */
180 if (generation_a < generation_b)
181 return -1;
182 else if (generation_a > generation_b)
183 return 1;
185 /* use date as a heuristic when generations are equal */
186 if (a->date < b->date)
187 return -1;
188 else if (a->date > b->date)
189 return 1;
190 return 0;
193 char *get_commit_graph_filename(struct object_directory *obj_dir)
195 return xstrfmt("%s/info/commit-graph", obj_dir->path);
198 static char *get_split_graph_filename(struct object_directory *odb,
199 const char *oid_hex)
201 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
202 oid_hex);
205 char *get_commit_graph_chain_filename(struct object_directory *odb)
207 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
210 static struct commit_graph *alloc_commit_graph(void)
212 struct commit_graph *g = xcalloc(1, sizeof(*g));
214 return g;
217 static int commit_graph_compatible(struct repository *r)
219 if (!r->gitdir)
220 return 0;
222 if (replace_refs_enabled(r)) {
223 prepare_replace_object(r);
224 if (hashmap_get_size(&r->objects->replace_map->map))
225 return 0;
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;
235 return 1;
238 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
240 *fd = git_open(graph_file);
241 if (*fd < 0)
242 return 0;
243 if (fstat(*fd, st)) {
244 close(*fd);
245 return 0;
247 return 1;
250 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
251 int fd, struct stat *st,
252 struct object_directory *odb)
254 void *graph_map;
255 size_t graph_size;
256 struct commit_graph *ret;
258 graph_size = xsize_t(st->st_size);
260 if (graph_size < GRAPH_MIN_SIZE) {
261 close(fd);
262 error(_("commit-graph file is too small"));
263 return NULL;
265 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
266 close(fd);
267 prepare_repo_settings(r);
268 ret = parse_commit_graph(&r->settings, graph_map, graph_size);
270 if (ret)
271 ret->odb = odb;
272 else
273 munmap(graph_map, graph_size);
275 return ret;
278 static int verify_commit_graph_lite(struct commit_graph *g)
280 int i;
283 * Basic validation shared between parse_commit_graph()
284 * which'll be called every time the graph is used, and the
285 * much more expensive verify_commit_graph() used by
286 * "commit-graph verify".
288 * There should only be very basic checks here to ensure that
289 * we don't e.g. segfault in fill_commit_in_graph(), but
290 * because this is a very hot codepath nothing that e.g. loops
291 * over g->num_commits, or runs a checksum on the commit-graph
292 * itself.
294 if (!g->chunk_oid_fanout) {
295 error("commit-graph is missing the OID Fanout chunk");
296 return 1;
298 if (!g->chunk_oid_lookup) {
299 error("commit-graph is missing the OID Lookup chunk");
300 return 1;
302 if (!g->chunk_commit_data) {
303 error("commit-graph is missing the Commit Data chunk");
304 return 1;
307 for (i = 0; i < 255; i++) {
308 uint32_t oid_fanout1 = ntohl(g->chunk_oid_fanout[i]);
309 uint32_t oid_fanout2 = ntohl(g->chunk_oid_fanout[i + 1]);
311 if (oid_fanout1 > oid_fanout2) {
312 error("commit-graph fanout values out of order");
313 return 1;
316 if (ntohl(g->chunk_oid_fanout[255]) != g->num_commits) {
317 error("commit-graph oid table and fanout disagree on size");
318 return 1;
321 return 0;
324 static int graph_read_oid_fanout(const unsigned char *chunk_start,
325 size_t chunk_size, void *data)
327 struct commit_graph *g = data;
328 if (chunk_size != 256 * sizeof(uint32_t))
329 return error("commit-graph oid fanout chunk is wrong size");
330 g->chunk_oid_fanout = (const uint32_t *)chunk_start;
331 return 0;
334 static int graph_read_oid_lookup(const unsigned char *chunk_start,
335 size_t chunk_size, void *data)
337 struct commit_graph *g = data;
338 g->chunk_oid_lookup = chunk_start;
339 g->num_commits = chunk_size / g->hash_len;
340 return 0;
343 static int graph_read_commit_data(const unsigned char *chunk_start,
344 size_t chunk_size, void *data)
346 struct commit_graph *g = data;
347 if (chunk_size != g->num_commits * GRAPH_DATA_WIDTH)
348 return error("commit-graph commit data chunk is wrong size");
349 g->chunk_commit_data = chunk_start;
350 return 0;
353 static int graph_read_generation_data(const unsigned char *chunk_start,
354 size_t chunk_size, void *data)
356 struct commit_graph *g = data;
357 if (chunk_size != g->num_commits * sizeof(uint32_t))
358 return error("commit-graph generations chunk is wrong size");
359 g->chunk_generation_data = chunk_start;
360 return 0;
363 static int graph_read_bloom_index(const unsigned char *chunk_start,
364 size_t chunk_size, void *data)
366 struct commit_graph *g = data;
367 if (chunk_size != g->num_commits * 4) {
368 warning("commit-graph changed-path index chunk is too small");
369 return -1;
371 g->chunk_bloom_indexes = chunk_start;
372 return 0;
375 static int graph_read_bloom_data(const unsigned char *chunk_start,
376 size_t chunk_size, void *data)
378 struct commit_graph *g = data;
379 uint32_t hash_version;
381 if (chunk_size < BLOOMDATA_CHUNK_HEADER_SIZE) {
382 warning("ignoring too-small changed-path chunk"
383 " (%"PRIuMAX" < %"PRIuMAX") in commit-graph file",
384 (uintmax_t)chunk_size,
385 (uintmax_t)BLOOMDATA_CHUNK_HEADER_SIZE);
386 return -1;
389 g->chunk_bloom_data = chunk_start;
390 g->chunk_bloom_data_size = chunk_size;
391 hash_version = get_be32(chunk_start);
393 if (hash_version != 1)
394 return 0;
396 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
397 g->bloom_filter_settings->hash_version = hash_version;
398 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
399 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
400 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
402 return 0;
405 struct commit_graph *parse_commit_graph(struct repo_settings *s,
406 void *graph_map, size_t graph_size)
408 const unsigned char *data;
409 struct commit_graph *graph;
410 uint32_t graph_signature;
411 unsigned char graph_version, hash_version;
412 struct chunkfile *cf = NULL;
414 if (!graph_map)
415 return NULL;
417 if (graph_size < GRAPH_MIN_SIZE)
418 return NULL;
420 data = (const unsigned char *)graph_map;
422 graph_signature = get_be32(data);
423 if (graph_signature != GRAPH_SIGNATURE) {
424 error(_("commit-graph signature %X does not match signature %X"),
425 graph_signature, GRAPH_SIGNATURE);
426 return NULL;
429 graph_version = *(unsigned char*)(data + 4);
430 if (graph_version != GRAPH_VERSION) {
431 error(_("commit-graph version %X does not match version %X"),
432 graph_version, GRAPH_VERSION);
433 return NULL;
436 hash_version = *(unsigned char*)(data + 5);
437 if (hash_version != oid_version(the_hash_algo)) {
438 error(_("commit-graph hash version %X does not match version %X"),
439 hash_version, oid_version(the_hash_algo));
440 return NULL;
443 graph = alloc_commit_graph();
445 graph->hash_len = the_hash_algo->rawsz;
446 graph->num_chunks = *(unsigned char*)(data + 6);
447 graph->data = graph_map;
448 graph->data_len = graph_size;
450 if (graph_size < GRAPH_HEADER_SIZE +
451 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
452 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
453 error(_("commit-graph file is too small to hold %u chunks"),
454 graph->num_chunks);
455 free(graph);
456 return NULL;
459 cf = init_chunkfile(NULL);
461 if (read_table_of_contents(cf, graph->data, graph_size,
462 GRAPH_HEADER_SIZE, graph->num_chunks, 1))
463 goto free_and_return;
465 read_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, graph_read_oid_fanout, graph);
466 read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph);
467 read_chunk(cf, GRAPH_CHUNKID_DATA, graph_read_commit_data, graph);
468 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges,
469 &graph->chunk_extra_edges_size);
470 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs,
471 &graph->chunk_base_graphs_size);
473 if (s->commit_graph_generation_version >= 2) {
474 read_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
475 graph_read_generation_data, graph);
476 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
477 &graph->chunk_generation_data_overflow,
478 &graph->chunk_generation_data_overflow_size);
480 if (graph->chunk_generation_data)
481 graph->read_generation_data = 1;
484 if (s->commit_graph_read_changed_paths) {
485 read_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
486 graph_read_bloom_index, graph);
487 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
488 graph_read_bloom_data, graph);
491 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
492 init_bloom_filters();
493 } else {
494 /* We need both the bloom chunks to exist together. Else ignore the data */
495 graph->chunk_bloom_indexes = NULL;
496 graph->chunk_bloom_data = NULL;
497 FREE_AND_NULL(graph->bloom_filter_settings);
500 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
502 if (verify_commit_graph_lite(graph))
503 goto free_and_return;
505 free_chunkfile(cf);
506 return graph;
508 free_and_return:
509 free_chunkfile(cf);
510 free(graph->bloom_filter_settings);
511 free(graph);
512 return NULL;
515 static struct commit_graph *load_commit_graph_one(struct repository *r,
516 const char *graph_file,
517 struct object_directory *odb)
520 struct stat st;
521 int fd;
522 struct commit_graph *g;
523 int open_ok = open_commit_graph(graph_file, &fd, &st);
525 if (!open_ok)
526 return NULL;
528 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
530 if (g)
531 g->filename = xstrdup(graph_file);
533 return g;
536 static struct commit_graph *load_commit_graph_v1(struct repository *r,
537 struct object_directory *odb)
539 char *graph_name = get_commit_graph_filename(odb);
540 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
541 free(graph_name);
543 return g;
547 * returns 1 if and only if all graphs in the chain have
548 * corrected commit dates stored in the generation_data chunk.
550 static int validate_mixed_generation_chain(struct commit_graph *g)
552 int read_generation_data = 1;
553 struct commit_graph *p = g;
555 while (read_generation_data && p) {
556 read_generation_data = p->read_generation_data;
557 p = p->base_graph;
560 if (read_generation_data)
561 return 1;
563 while (g) {
564 g->read_generation_data = 0;
565 g = g->base_graph;
568 return 0;
571 static int add_graph_to_chain(struct commit_graph *g,
572 struct commit_graph *chain,
573 struct object_id *oids,
574 int n)
576 struct commit_graph *cur_g = chain;
578 if (n && !g->chunk_base_graphs) {
579 warning(_("commit-graph has no base graphs chunk"));
580 return 0;
583 if (g->chunk_base_graphs_size / g->hash_len < n) {
584 warning(_("commit-graph base graphs chunk is too small"));
585 return 0;
588 while (n) {
589 n--;
591 if (!cur_g ||
592 !oideq(&oids[n], &cur_g->oid) ||
593 !hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_len, n))) {
594 warning(_("commit-graph chain does not match"));
595 return 0;
598 cur_g = cur_g->base_graph;
601 if (chain) {
602 if (unsigned_add_overflows(chain->num_commits,
603 chain->num_commits_in_base)) {
604 warning(_("commit count in base graph too high: %"PRIuMAX),
605 (uintmax_t)chain->num_commits_in_base);
606 return 0;
608 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
611 g->base_graph = chain;
613 return 1;
616 int open_commit_graph_chain(const char *chain_file,
617 int *fd, struct stat *st)
619 *fd = git_open(chain_file);
620 if (*fd < 0)
621 return 0;
622 if (fstat(*fd, st)) {
623 close(*fd);
624 return 0;
626 if (st->st_size < the_hash_algo->hexsz) {
627 close(*fd);
628 if (!st->st_size) {
629 /* treat empty files the same as missing */
630 errno = ENOENT;
631 } else {
632 warning("commit-graph chain file too small");
633 errno = EINVAL;
635 return 0;
637 return 1;
640 struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
641 int fd, struct stat *st,
642 int *incomplete_chain)
644 struct commit_graph *graph_chain = NULL;
645 struct strbuf line = STRBUF_INIT;
646 struct object_id *oids;
647 int i = 0, valid = 1, count;
648 FILE *fp = xfdopen(fd, "r");
650 count = st->st_size / (the_hash_algo->hexsz + 1);
651 CALLOC_ARRAY(oids, count);
653 prepare_alt_odb(r);
655 for (i = 0; i < count; i++) {
656 struct object_directory *odb;
658 if (strbuf_getline_lf(&line, fp) == EOF)
659 break;
661 if (get_oid_hex(line.buf, &oids[i])) {
662 warning(_("invalid commit-graph chain: line '%s' not a hash"),
663 line.buf);
664 valid = 0;
665 break;
668 valid = 0;
669 for (odb = r->objects->odb; odb; odb = odb->next) {
670 char *graph_name = get_split_graph_filename(odb, line.buf);
671 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
673 free(graph_name);
675 if (g) {
676 if (add_graph_to_chain(g, graph_chain, oids, i)) {
677 graph_chain = g;
678 valid = 1;
679 } else {
680 free_commit_graph(g);
683 break;
687 if (!valid) {
688 warning(_("unable to find all commit-graph files"));
689 break;
693 validate_mixed_generation_chain(graph_chain);
695 free(oids);
696 fclose(fp);
697 strbuf_release(&line);
699 *incomplete_chain = !valid;
700 return graph_chain;
703 static struct commit_graph *load_commit_graph_chain(struct repository *r,
704 struct object_directory *odb)
706 char *chain_file = get_commit_graph_chain_filename(odb);
707 struct stat st;
708 int fd;
709 struct commit_graph *g = NULL;
711 if (open_commit_graph_chain(chain_file, &fd, &st)) {
712 int incomplete;
713 /* ownership of fd is taken over by load function */
714 g = load_commit_graph_chain_fd_st(r, fd, &st, &incomplete);
717 free(chain_file);
718 return g;
721 struct commit_graph *read_commit_graph_one(struct repository *r,
722 struct object_directory *odb)
724 struct commit_graph *g = load_commit_graph_v1(r, odb);
726 if (!g)
727 g = load_commit_graph_chain(r, odb);
729 return g;
732 static void prepare_commit_graph_one(struct repository *r,
733 struct object_directory *odb)
736 if (r->objects->commit_graph)
737 return;
739 r->objects->commit_graph = read_commit_graph_one(r, odb);
743 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
745 * On the first invocation, this function attempts to load the commit
746 * graph if the_repository is configured to have one.
748 static int prepare_commit_graph(struct repository *r)
750 struct object_directory *odb;
753 * Early return if there is no git dir or if the commit graph is
754 * disabled.
756 * This must come before the "already attempted?" check below, because
757 * we want to disable even an already-loaded graph file.
759 if (!r->gitdir || r->commit_graph_disabled)
760 return 0;
762 if (r->objects->commit_graph_attempted)
763 return !!r->objects->commit_graph;
764 r->objects->commit_graph_attempted = 1;
766 prepare_repo_settings(r);
768 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
769 r->settings.core_commit_graph != 1)
771 * This repository is not configured to use commit graphs, so
772 * do not load one. (But report commit_graph_attempted anyway
773 * so that commit graph loading is not attempted again for this
774 * repository.)
776 return 0;
778 if (!commit_graph_compatible(r))
779 return 0;
781 prepare_alt_odb(r);
782 for (odb = r->objects->odb;
783 !r->objects->commit_graph && odb;
784 odb = odb->next)
785 prepare_commit_graph_one(r, odb);
786 return !!r->objects->commit_graph;
789 int generation_numbers_enabled(struct repository *r)
791 uint32_t first_generation;
792 struct commit_graph *g;
793 if (!prepare_commit_graph(r))
794 return 0;
796 g = r->objects->commit_graph;
798 if (!g->num_commits)
799 return 0;
801 first_generation = get_be32(g->chunk_commit_data +
802 g->hash_len + 8) >> 2;
804 return !!first_generation;
807 int corrected_commit_dates_enabled(struct repository *r)
809 struct commit_graph *g;
810 if (!prepare_commit_graph(r))
811 return 0;
813 g = r->objects->commit_graph;
815 if (!g->num_commits)
816 return 0;
818 return g->read_generation_data;
821 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
823 struct commit_graph *g = r->objects->commit_graph;
824 while (g) {
825 if (g->bloom_filter_settings)
826 return g->bloom_filter_settings;
827 g = g->base_graph;
829 return NULL;
832 void close_commit_graph(struct raw_object_store *o)
834 clear_commit_graph_data_slab(&commit_graph_data_slab);
835 free_commit_graph(o->commit_graph);
836 o->commit_graph = NULL;
839 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
841 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
842 g->chunk_oid_lookup, g->hash_len, pos);
845 static void load_oid_from_graph(struct commit_graph *g,
846 uint32_t pos,
847 struct object_id *oid)
849 uint32_t lex_index;
851 while (g && pos < g->num_commits_in_base)
852 g = g->base_graph;
854 if (!g)
855 BUG("NULL commit-graph");
857 if (pos >= g->num_commits + g->num_commits_in_base)
858 die(_("invalid commit position. commit-graph is likely corrupt"));
860 lex_index = pos - g->num_commits_in_base;
862 oidread(oid, g->chunk_oid_lookup + st_mult(g->hash_len, lex_index));
865 static struct commit_list **insert_parent_or_die(struct repository *r,
866 struct commit_graph *g,
867 uint32_t pos,
868 struct commit_list **pptr)
870 struct commit *c;
871 struct object_id oid;
873 if (pos >= g->num_commits + g->num_commits_in_base)
874 die("invalid parent position %"PRIu32, pos);
876 load_oid_from_graph(g, pos, &oid);
877 c = lookup_commit(r, &oid);
878 if (!c)
879 die(_("could not find commit %s"), oid_to_hex(&oid));
880 commit_graph_data_at(c)->graph_pos = pos;
881 return &commit_list_insert(c, pptr)->next;
884 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
886 const unsigned char *commit_data;
887 struct commit_graph_data *graph_data;
888 uint32_t lex_index, offset_pos;
889 uint64_t date_high, date_low, offset;
891 while (pos < g->num_commits_in_base)
892 g = g->base_graph;
894 if (pos >= g->num_commits + g->num_commits_in_base)
895 die(_("invalid commit position. commit-graph is likely corrupt"));
897 lex_index = pos - g->num_commits_in_base;
898 commit_data = g->chunk_commit_data + st_mult(GRAPH_DATA_WIDTH, lex_index);
900 graph_data = commit_graph_data_at(item);
901 graph_data->graph_pos = pos;
903 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
904 date_low = get_be32(commit_data + g->hash_len + 12);
905 item->date = (timestamp_t)((date_high << 32) | date_low);
907 if (g->read_generation_data) {
908 offset = (timestamp_t)get_be32(g->chunk_generation_data + st_mult(sizeof(uint32_t), lex_index));
910 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
911 if (!g->chunk_generation_data_overflow)
912 die(_("commit-graph requires overflow generation data but has none"));
914 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
915 if (g->chunk_generation_data_overflow_size / sizeof(uint64_t) <= offset_pos)
916 die(_("commit-graph overflow generation data is too small"));
917 graph_data->generation = item->date +
918 get_be64(g->chunk_generation_data_overflow + sizeof(uint64_t) * offset_pos);
919 } else
920 graph_data->generation = item->date + offset;
921 } else
922 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
924 if (g->topo_levels)
925 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
928 static inline void set_commit_tree(struct commit *c, struct tree *t)
930 c->maybe_tree = t;
933 static int fill_commit_in_graph(struct repository *r,
934 struct commit *item,
935 struct commit_graph *g, uint32_t pos)
937 uint32_t edge_value;
938 uint32_t parent_data_pos;
939 struct commit_list **pptr;
940 const unsigned char *commit_data;
941 uint32_t lex_index;
943 while (pos < g->num_commits_in_base)
944 g = g->base_graph;
946 fill_commit_graph_info(item, g, pos);
948 lex_index = pos - g->num_commits_in_base;
949 commit_data = g->chunk_commit_data + st_mult(g->hash_len + 16, lex_index);
951 item->object.parsed = 1;
953 set_commit_tree(item, NULL);
955 pptr = &item->parents;
957 edge_value = get_be32(commit_data + g->hash_len);
958 if (edge_value == GRAPH_PARENT_NONE)
959 return 1;
960 pptr = insert_parent_or_die(r, g, edge_value, pptr);
962 edge_value = get_be32(commit_data + g->hash_len + 4);
963 if (edge_value == GRAPH_PARENT_NONE)
964 return 1;
965 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
966 pptr = insert_parent_or_die(r, g, edge_value, pptr);
967 return 1;
970 parent_data_pos = edge_value & GRAPH_EDGE_LAST_MASK;
971 do {
972 if (g->chunk_extra_edges_size / sizeof(uint32_t) <= parent_data_pos) {
973 error("commit-graph extra-edges pointer out of bounds");
974 free_commit_list(item->parents);
975 item->parents = NULL;
976 item->object.parsed = 0;
977 return 0;
979 edge_value = get_be32(g->chunk_extra_edges +
980 sizeof(uint32_t) * parent_data_pos);
981 pptr = insert_parent_or_die(r, g,
982 edge_value & GRAPH_EDGE_LAST_MASK,
983 pptr);
984 parent_data_pos++;
985 } while (!(edge_value & GRAPH_LAST_EDGE));
987 return 1;
990 static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
992 struct commit_graph *cur_g = g;
993 uint32_t lex_index;
995 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
996 cur_g = cur_g->base_graph;
998 if (cur_g) {
999 *pos = lex_index + cur_g->num_commits_in_base;
1000 return 1;
1003 return 0;
1006 static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
1008 uint32_t graph_pos = commit_graph_position(item);
1009 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
1010 *pos = graph_pos;
1011 return 1;
1012 } else {
1013 return search_commit_pos_in_graph(&item->object.oid, g, pos);
1017 int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
1018 uint32_t *pos)
1020 if (!prepare_commit_graph(r))
1021 return 0;
1022 return find_commit_pos_in_graph(c, r->objects->commit_graph, pos);
1025 struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
1027 struct commit *commit;
1028 uint32_t pos;
1030 if (!prepare_commit_graph(repo))
1031 return NULL;
1032 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
1033 return NULL;
1034 if (!has_object(repo, id, 0))
1035 return NULL;
1037 commit = lookup_commit(repo, id);
1038 if (!commit)
1039 return NULL;
1040 if (commit->object.parsed)
1041 return commit;
1043 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
1044 return NULL;
1046 return commit;
1049 static int parse_commit_in_graph_one(struct repository *r,
1050 struct commit_graph *g,
1051 struct commit *item)
1053 uint32_t pos;
1055 if (item->object.parsed)
1056 return 1;
1058 if (find_commit_pos_in_graph(item, g, &pos))
1059 return fill_commit_in_graph(r, item, g, pos);
1061 return 0;
1064 int parse_commit_in_graph(struct repository *r, struct commit *item)
1066 static int checked_env = 0;
1068 if (!checked_env &&
1069 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
1070 die("dying as requested by the '%s' variable on commit-graph parse!",
1071 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
1072 checked_env = 1;
1074 if (!prepare_commit_graph(r))
1075 return 0;
1076 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
1079 void load_commit_graph_info(struct repository *r, struct commit *item)
1081 uint32_t pos;
1082 if (repo_find_commit_pos_in_graph(r, item, &pos))
1083 fill_commit_graph_info(item, r->objects->commit_graph, pos);
1086 static struct tree *load_tree_for_commit(struct repository *r,
1087 struct commit_graph *g,
1088 struct commit *c)
1090 struct object_id oid;
1091 const unsigned char *commit_data;
1092 uint32_t graph_pos = commit_graph_position(c);
1094 while (graph_pos < g->num_commits_in_base)
1095 g = g->base_graph;
1097 commit_data = g->chunk_commit_data +
1098 st_mult(GRAPH_DATA_WIDTH, graph_pos - g->num_commits_in_base);
1100 oidread(&oid, commit_data);
1101 set_commit_tree(c, lookup_tree(r, &oid));
1103 return c->maybe_tree;
1106 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
1107 struct commit_graph *g,
1108 const struct commit *c)
1110 if (c->maybe_tree)
1111 return c->maybe_tree;
1112 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
1113 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
1115 return load_tree_for_commit(r, g, (struct commit *)c);
1118 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
1120 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
1123 struct packed_commit_list {
1124 struct commit **list;
1125 size_t nr;
1126 size_t alloc;
1129 struct write_commit_graph_context {
1130 struct repository *r;
1131 struct object_directory *odb;
1132 char *graph_name;
1133 struct oid_array oids;
1134 struct packed_commit_list commits;
1135 int num_extra_edges;
1136 int num_generation_data_overflows;
1137 unsigned long approx_nr_objects;
1138 struct progress *progress;
1139 int progress_done;
1140 uint64_t progress_cnt;
1142 char *base_graph_name;
1143 int num_commit_graphs_before;
1144 int num_commit_graphs_after;
1145 char **commit_graph_filenames_before;
1146 char **commit_graph_filenames_after;
1147 char **commit_graph_hash_after;
1148 uint32_t new_num_commits_in_base;
1149 struct commit_graph *new_base_graph;
1151 unsigned append:1,
1152 report_progress:1,
1153 split:1,
1154 changed_paths:1,
1155 order_by_pack:1,
1156 write_generation_data:1,
1157 trust_generation_numbers:1;
1159 struct topo_level_slab *topo_levels;
1160 const struct commit_graph_opts *opts;
1161 size_t total_bloom_filter_data_size;
1162 const struct bloom_filter_settings *bloom_settings;
1164 int count_bloom_filter_computed;
1165 int count_bloom_filter_not_computed;
1166 int count_bloom_filter_trunc_empty;
1167 int count_bloom_filter_trunc_large;
1170 static int write_graph_chunk_fanout(struct hashfile *f,
1171 void *data)
1173 struct write_commit_graph_context *ctx = data;
1174 int i, count = 0;
1175 struct commit **list = ctx->commits.list;
1178 * Write the first-level table (the list is sorted,
1179 * but we use a 256-entry lookup to be able to avoid
1180 * having to do eight extra binary search iterations).
1182 for (i = 0; i < 256; i++) {
1183 while (count < ctx->commits.nr) {
1184 if ((*list)->object.oid.hash[0] != i)
1185 break;
1186 display_progress(ctx->progress, ++ctx->progress_cnt);
1187 count++;
1188 list++;
1191 hashwrite_be32(f, count);
1194 return 0;
1197 static int write_graph_chunk_oids(struct hashfile *f,
1198 void *data)
1200 struct write_commit_graph_context *ctx = data;
1201 struct commit **list = ctx->commits.list;
1202 int count;
1203 for (count = 0; count < ctx->commits.nr; count++, list++) {
1204 display_progress(ctx->progress, ++ctx->progress_cnt);
1205 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1208 return 0;
1211 static const struct object_id *commit_to_oid(size_t index, const void *table)
1213 const struct commit * const *commits = table;
1214 return &commits[index]->object.oid;
1217 static int write_graph_chunk_data(struct hashfile *f,
1218 void *data)
1220 struct write_commit_graph_context *ctx = data;
1221 struct commit **list = ctx->commits.list;
1222 struct commit **last = ctx->commits.list + ctx->commits.nr;
1223 uint32_t num_extra_edges = 0;
1225 while (list < last) {
1226 struct commit_list *parent;
1227 struct object_id *tree;
1228 int edge_value;
1229 uint32_t packedDate[2];
1230 display_progress(ctx->progress, ++ctx->progress_cnt);
1232 if (repo_parse_commit_no_graph(ctx->r, *list))
1233 die(_("unable to parse commit %s"),
1234 oid_to_hex(&(*list)->object.oid));
1235 tree = get_commit_tree_oid(*list);
1236 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1238 parent = (*list)->parents;
1240 if (!parent)
1241 edge_value = GRAPH_PARENT_NONE;
1242 else {
1243 edge_value = oid_pos(&parent->item->object.oid,
1244 ctx->commits.list,
1245 ctx->commits.nr,
1246 commit_to_oid);
1248 if (edge_value >= 0)
1249 edge_value += ctx->new_num_commits_in_base;
1250 else if (ctx->new_base_graph) {
1251 uint32_t pos;
1252 if (find_commit_pos_in_graph(parent->item,
1253 ctx->new_base_graph,
1254 &pos))
1255 edge_value = pos;
1258 if (edge_value < 0)
1259 BUG("missing parent %s for commit %s",
1260 oid_to_hex(&parent->item->object.oid),
1261 oid_to_hex(&(*list)->object.oid));
1264 hashwrite_be32(f, edge_value);
1266 if (parent)
1267 parent = parent->next;
1269 if (!parent)
1270 edge_value = GRAPH_PARENT_NONE;
1271 else if (parent->next)
1272 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1273 else {
1274 edge_value = oid_pos(&parent->item->object.oid,
1275 ctx->commits.list,
1276 ctx->commits.nr,
1277 commit_to_oid);
1279 if (edge_value >= 0)
1280 edge_value += ctx->new_num_commits_in_base;
1281 else if (ctx->new_base_graph) {
1282 uint32_t pos;
1283 if (find_commit_pos_in_graph(parent->item,
1284 ctx->new_base_graph,
1285 &pos))
1286 edge_value = pos;
1289 if (edge_value < 0)
1290 BUG("missing parent %s for commit %s",
1291 oid_to_hex(&parent->item->object.oid),
1292 oid_to_hex(&(*list)->object.oid));
1295 hashwrite_be32(f, edge_value);
1297 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1298 do {
1299 num_extra_edges++;
1300 parent = parent->next;
1301 } while (parent);
1304 if (sizeof((*list)->date) > 4)
1305 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1306 else
1307 packedDate[0] = 0;
1309 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1311 packedDate[1] = htonl((*list)->date);
1312 hashwrite(f, packedDate, 8);
1314 list++;
1317 return 0;
1320 static int write_graph_chunk_generation_data(struct hashfile *f,
1321 void *data)
1323 struct write_commit_graph_context *ctx = data;
1324 int i, num_generation_data_overflows = 0;
1326 for (i = 0; i < ctx->commits.nr; i++) {
1327 struct commit *c = ctx->commits.list[i];
1328 timestamp_t offset;
1329 repo_parse_commit(ctx->r, c);
1330 offset = commit_graph_data_at(c)->generation - c->date;
1331 display_progress(ctx->progress, ++ctx->progress_cnt);
1333 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1334 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1335 num_generation_data_overflows++;
1338 hashwrite_be32(f, offset);
1341 return 0;
1344 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1345 void *data)
1347 struct write_commit_graph_context *ctx = data;
1348 int i;
1349 for (i = 0; i < ctx->commits.nr; i++) {
1350 struct commit *c = ctx->commits.list[i];
1351 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1352 display_progress(ctx->progress, ++ctx->progress_cnt);
1354 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1355 hashwrite_be32(f, offset >> 32);
1356 hashwrite_be32(f, (uint32_t) offset);
1360 return 0;
1363 static int write_graph_chunk_extra_edges(struct hashfile *f,
1364 void *data)
1366 struct write_commit_graph_context *ctx = data;
1367 struct commit **list = ctx->commits.list;
1368 struct commit **last = ctx->commits.list + ctx->commits.nr;
1369 struct commit_list *parent;
1371 while (list < last) {
1372 int num_parents = 0;
1374 display_progress(ctx->progress, ++ctx->progress_cnt);
1376 for (parent = (*list)->parents; num_parents < 3 && parent;
1377 parent = parent->next)
1378 num_parents++;
1380 if (num_parents <= 2) {
1381 list++;
1382 continue;
1385 /* Since num_parents > 2, this initializer is safe. */
1386 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1387 int edge_value = oid_pos(&parent->item->object.oid,
1388 ctx->commits.list,
1389 ctx->commits.nr,
1390 commit_to_oid);
1392 if (edge_value >= 0)
1393 edge_value += ctx->new_num_commits_in_base;
1394 else if (ctx->new_base_graph) {
1395 uint32_t pos;
1396 if (find_commit_pos_in_graph(parent->item,
1397 ctx->new_base_graph,
1398 &pos))
1399 edge_value = pos;
1402 if (edge_value < 0)
1403 BUG("missing parent %s for commit %s",
1404 oid_to_hex(&parent->item->object.oid),
1405 oid_to_hex(&(*list)->object.oid));
1406 else if (!parent->next)
1407 edge_value |= GRAPH_LAST_EDGE;
1409 hashwrite_be32(f, edge_value);
1412 list++;
1415 return 0;
1418 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1419 void *data)
1421 struct write_commit_graph_context *ctx = data;
1422 struct commit **list = ctx->commits.list;
1423 struct commit **last = ctx->commits.list + ctx->commits.nr;
1424 uint32_t cur_pos = 0;
1426 while (list < last) {
1427 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1428 size_t len = filter ? filter->len : 0;
1429 cur_pos += len;
1430 display_progress(ctx->progress, ++ctx->progress_cnt);
1431 hashwrite_be32(f, cur_pos);
1432 list++;
1435 return 0;
1438 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1440 struct json_writer jw = JSON_WRITER_INIT;
1442 jw_object_begin(&jw, 0);
1443 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1444 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1445 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1446 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1447 jw_end(&jw);
1449 trace2_data_json("bloom", ctx->r, "settings", &jw);
1451 jw_release(&jw);
1454 static int write_graph_chunk_bloom_data(struct hashfile *f,
1455 void *data)
1457 struct write_commit_graph_context *ctx = data;
1458 struct commit **list = ctx->commits.list;
1459 struct commit **last = ctx->commits.list + ctx->commits.nr;
1461 trace2_bloom_filter_settings(ctx);
1463 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1464 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1465 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1467 while (list < last) {
1468 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1469 size_t len = filter ? filter->len : 0;
1471 display_progress(ctx->progress, ++ctx->progress_cnt);
1472 if (len)
1473 hashwrite(f, filter->data, len * sizeof(unsigned char));
1474 list++;
1477 return 0;
1480 static int add_packed_commits(const struct object_id *oid,
1481 struct packed_git *pack,
1482 uint32_t pos,
1483 void *data)
1485 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1486 enum object_type type;
1487 off_t offset = nth_packed_object_offset(pack, pos);
1488 struct object_info oi = OBJECT_INFO_INIT;
1490 if (ctx->progress)
1491 display_progress(ctx->progress, ++ctx->progress_done);
1493 oi.typep = &type;
1494 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1495 die(_("unable to get type of object %s"), oid_to_hex(oid));
1497 if (type != OBJ_COMMIT)
1498 return 0;
1500 oid_array_append(&ctx->oids, oid);
1501 set_commit_pos(ctx->r, oid);
1503 return 0;
1506 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1508 struct commit_list *parent;
1509 for (parent = commit->parents; parent; parent = parent->next) {
1510 if (!(parent->item->object.flags & REACHABLE)) {
1511 oid_array_append(&ctx->oids, &parent->item->object.oid);
1512 parent->item->object.flags |= REACHABLE;
1517 static void close_reachable(struct write_commit_graph_context *ctx)
1519 int i;
1520 struct commit *commit;
1521 enum commit_graph_split_flags flags = ctx->opts ?
1522 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1524 if (ctx->report_progress)
1525 ctx->progress = start_delayed_progress(
1526 _("Loading known commits in commit graph"),
1527 ctx->oids.nr);
1528 for (i = 0; i < ctx->oids.nr; i++) {
1529 display_progress(ctx->progress, i + 1);
1530 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1531 if (commit)
1532 commit->object.flags |= REACHABLE;
1534 stop_progress(&ctx->progress);
1537 * As this loop runs, ctx->oids.nr may grow, but not more
1538 * than the number of missing commits in the reachable
1539 * closure.
1541 if (ctx->report_progress)
1542 ctx->progress = start_delayed_progress(
1543 _("Expanding reachable commits in commit graph"),
1545 for (i = 0; i < ctx->oids.nr; i++) {
1546 display_progress(ctx->progress, i + 1);
1547 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1549 if (!commit)
1550 continue;
1551 if (ctx->split) {
1552 if ((!repo_parse_commit(ctx->r, commit) &&
1553 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1554 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1555 add_missing_parents(ctx, commit);
1556 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1557 add_missing_parents(ctx, commit);
1559 stop_progress(&ctx->progress);
1561 if (ctx->report_progress)
1562 ctx->progress = start_delayed_progress(
1563 _("Clearing commit marks in commit graph"),
1564 ctx->oids.nr);
1565 for (i = 0; i < ctx->oids.nr; i++) {
1566 display_progress(ctx->progress, i + 1);
1567 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1569 if (commit)
1570 commit->object.flags &= ~REACHABLE;
1572 stop_progress(&ctx->progress);
1575 struct compute_generation_info {
1576 struct repository *r;
1577 struct packed_commit_list *commits;
1578 struct progress *progress;
1579 int progress_cnt;
1581 timestamp_t (*get_generation)(struct commit *c, void *data);
1582 void (*set_generation)(struct commit *c, timestamp_t gen, void *data);
1583 void *data;
1586 static timestamp_t compute_generation_from_max(struct commit *c,
1587 timestamp_t max_gen,
1588 int generation_version)
1590 switch (generation_version) {
1591 case 1: /* topological levels */
1592 if (max_gen > GENERATION_NUMBER_V1_MAX - 1)
1593 max_gen = GENERATION_NUMBER_V1_MAX - 1;
1594 return max_gen + 1;
1596 case 2: /* corrected commit date */
1597 if (c->date && c->date > max_gen)
1598 max_gen = c->date - 1;
1599 return max_gen + 1;
1601 default:
1602 BUG("attempting unimplemented version");
1606 static void compute_reachable_generation_numbers(
1607 struct compute_generation_info *info,
1608 int generation_version)
1610 int i;
1611 struct commit_list *list = NULL;
1613 for (i = 0; i < info->commits->nr; i++) {
1614 struct commit *c = info->commits->list[i];
1615 timestamp_t gen;
1616 repo_parse_commit(info->r, c);
1617 gen = info->get_generation(c, info->data);
1618 display_progress(info->progress, info->progress_cnt + 1);
1620 if (gen != GENERATION_NUMBER_ZERO && gen != GENERATION_NUMBER_INFINITY)
1621 continue;
1623 commit_list_insert(c, &list);
1624 while (list) {
1625 struct commit *current = list->item;
1626 struct commit_list *parent;
1627 int all_parents_computed = 1;
1628 uint32_t max_gen = 0;
1630 for (parent = current->parents; parent; parent = parent->next) {
1631 repo_parse_commit(info->r, parent->item);
1632 gen = info->get_generation(parent->item, info->data);
1634 if (gen == GENERATION_NUMBER_ZERO) {
1635 all_parents_computed = 0;
1636 commit_list_insert(parent->item, &list);
1637 break;
1640 if (gen > max_gen)
1641 max_gen = gen;
1644 if (all_parents_computed) {
1645 pop_commit(&list);
1646 gen = compute_generation_from_max(
1647 current, max_gen,
1648 generation_version);
1649 info->set_generation(current, gen, info->data);
1655 static timestamp_t get_topo_level(struct commit *c, void *data)
1657 struct write_commit_graph_context *ctx = data;
1658 return *topo_level_slab_at(ctx->topo_levels, c);
1661 static void set_topo_level(struct commit *c, timestamp_t t, void *data)
1663 struct write_commit_graph_context *ctx = data;
1664 *topo_level_slab_at(ctx->topo_levels, c) = (uint32_t)t;
1667 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1669 struct compute_generation_info info = {
1670 .r = ctx->r,
1671 .commits = &ctx->commits,
1672 .get_generation = get_topo_level,
1673 .set_generation = set_topo_level,
1674 .data = ctx,
1677 if (ctx->report_progress)
1678 info.progress = ctx->progress
1679 = start_delayed_progress(
1680 _("Computing commit graph topological levels"),
1681 ctx->commits.nr);
1683 compute_reachable_generation_numbers(&info, 1);
1685 stop_progress(&ctx->progress);
1688 static timestamp_t get_generation_from_graph_data(struct commit *c,
1689 void *data UNUSED)
1691 return commit_graph_data_at(c)->generation;
1694 static void set_generation_v2(struct commit *c, timestamp_t t,
1695 void *data UNUSED)
1697 struct commit_graph_data *g = commit_graph_data_at(c);
1698 g->generation = t;
1701 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1703 int i;
1704 struct compute_generation_info info = {
1705 .r = ctx->r,
1706 .commits = &ctx->commits,
1707 .get_generation = get_generation_from_graph_data,
1708 .set_generation = set_generation_v2,
1711 if (ctx->report_progress)
1712 info.progress = ctx->progress
1713 = start_delayed_progress(
1714 _("Computing commit graph generation numbers"),
1715 ctx->commits.nr);
1717 if (!ctx->trust_generation_numbers) {
1718 for (i = 0; i < ctx->commits.nr; i++) {
1719 struct commit *c = ctx->commits.list[i];
1720 repo_parse_commit(ctx->r, c);
1721 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1725 compute_reachable_generation_numbers(&info, 2);
1727 for (i = 0; i < ctx->commits.nr; i++) {
1728 struct commit *c = ctx->commits.list[i];
1729 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1730 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1731 ctx->num_generation_data_overflows++;
1733 stop_progress(&ctx->progress);
1736 static void set_generation_in_graph_data(struct commit *c, timestamp_t t,
1737 void *data UNUSED)
1739 commit_graph_data_at(c)->generation = t;
1743 * After this method, all commits reachable from those in the given
1744 * list will have non-zero, non-infinite generation numbers.
1746 void ensure_generations_valid(struct repository *r,
1747 struct commit **commits, size_t nr)
1749 int generation_version = get_configured_generation_version(r);
1750 struct packed_commit_list list = {
1751 .list = commits,
1752 .alloc = nr,
1753 .nr = nr,
1755 struct compute_generation_info info = {
1756 .r = r,
1757 .commits = &list,
1758 .get_generation = get_generation_from_graph_data,
1759 .set_generation = set_generation_in_graph_data,
1762 compute_reachable_generation_numbers(&info, generation_version);
1765 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1767 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1768 ctx->count_bloom_filter_computed);
1769 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1770 ctx->count_bloom_filter_not_computed);
1771 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1772 ctx->count_bloom_filter_trunc_empty);
1773 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1774 ctx->count_bloom_filter_trunc_large);
1777 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1779 int i;
1780 struct progress *progress = NULL;
1781 struct commit **sorted_commits;
1782 int max_new_filters;
1784 init_bloom_filters();
1786 if (ctx->report_progress)
1787 progress = start_delayed_progress(
1788 _("Computing commit changed paths Bloom filters"),
1789 ctx->commits.nr);
1791 DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1793 if (ctx->order_by_pack)
1794 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1795 else
1796 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1798 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1799 ctx->opts->max_new_filters : ctx->commits.nr;
1801 for (i = 0; i < ctx->commits.nr; i++) {
1802 enum bloom_filter_computed computed = 0;
1803 struct commit *c = sorted_commits[i];
1804 struct bloom_filter *filter = get_or_compute_bloom_filter(
1805 ctx->r,
1807 ctx->count_bloom_filter_computed < max_new_filters,
1808 ctx->bloom_settings,
1809 &computed);
1810 if (computed & BLOOM_COMPUTED) {
1811 ctx->count_bloom_filter_computed++;
1812 if (computed & BLOOM_TRUNC_EMPTY)
1813 ctx->count_bloom_filter_trunc_empty++;
1814 if (computed & BLOOM_TRUNC_LARGE)
1815 ctx->count_bloom_filter_trunc_large++;
1816 } else if (computed & BLOOM_NOT_COMPUTED)
1817 ctx->count_bloom_filter_not_computed++;
1818 ctx->total_bloom_filter_data_size += filter
1819 ? sizeof(unsigned char) * filter->len : 0;
1820 display_progress(progress, i + 1);
1823 if (trace2_is_enabled())
1824 trace2_bloom_filter_write_statistics(ctx);
1826 free(sorted_commits);
1827 stop_progress(&progress);
1830 struct refs_cb_data {
1831 struct oidset *commits;
1832 struct progress *progress;
1835 static int add_ref_to_set(const char *refname UNUSED,
1836 const struct object_id *oid,
1837 int flags UNUSED, void *cb_data)
1839 struct object_id peeled;
1840 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1842 if (!peel_iterated_oid(oid, &peeled))
1843 oid = &peeled;
1844 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1845 oidset_insert(data->commits, oid);
1847 display_progress(data->progress, oidset_size(data->commits));
1849 return 0;
1852 int write_commit_graph_reachable(struct object_directory *odb,
1853 enum commit_graph_write_flags flags,
1854 const struct commit_graph_opts *opts)
1856 struct oidset commits = OIDSET_INIT;
1857 struct refs_cb_data data;
1858 int result;
1860 memset(&data, 0, sizeof(data));
1861 data.commits = &commits;
1862 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1863 data.progress = start_delayed_progress(
1864 _("Collecting referenced commits"), 0);
1866 for_each_ref(add_ref_to_set, &data);
1868 stop_progress(&data.progress);
1870 result = write_commit_graph(odb, NULL, &commits,
1871 flags, opts);
1873 oidset_clear(&commits);
1874 return result;
1877 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1878 const struct string_list *pack_indexes)
1880 uint32_t i;
1881 struct strbuf progress_title = STRBUF_INIT;
1882 struct strbuf packname = STRBUF_INIT;
1883 int dirlen;
1884 int ret = 0;
1886 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1887 dirlen = packname.len;
1888 if (ctx->report_progress) {
1889 strbuf_addf(&progress_title,
1890 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1891 "Finding commits for commit graph in %"PRIuMAX" packs",
1892 pack_indexes->nr),
1893 (uintmax_t)pack_indexes->nr);
1894 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1895 ctx->progress_done = 0;
1897 for (i = 0; i < pack_indexes->nr; i++) {
1898 struct packed_git *p;
1899 strbuf_setlen(&packname, dirlen);
1900 strbuf_addstr(&packname, pack_indexes->items[i].string);
1901 p = add_packed_git(packname.buf, packname.len, 1);
1902 if (!p) {
1903 ret = error(_("error adding pack %s"), packname.buf);
1904 goto cleanup;
1906 if (open_pack_index(p)) {
1907 ret = error(_("error opening index for %s"), packname.buf);
1908 goto cleanup;
1910 for_each_object_in_pack(p, add_packed_commits, ctx,
1911 FOR_EACH_OBJECT_PACK_ORDER);
1912 close_pack(p);
1913 free(p);
1916 cleanup:
1917 stop_progress(&ctx->progress);
1918 strbuf_release(&progress_title);
1919 strbuf_release(&packname);
1921 return ret;
1924 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1925 struct oidset *commits)
1927 struct oidset_iter iter;
1928 struct object_id *oid;
1930 if (!oidset_size(commits))
1931 return 0;
1933 oidset_iter_init(commits, &iter);
1934 while ((oid = oidset_iter_next(&iter))) {
1935 oid_array_append(&ctx->oids, oid);
1938 return 0;
1941 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1943 if (ctx->report_progress)
1944 ctx->progress = start_delayed_progress(
1945 _("Finding commits for commit graph among packed objects"),
1946 ctx->approx_nr_objects);
1947 for_each_packed_object(add_packed_commits, ctx,
1948 FOR_EACH_OBJECT_PACK_ORDER);
1949 if (ctx->progress_done < ctx->approx_nr_objects)
1950 display_progress(ctx->progress, ctx->approx_nr_objects);
1951 stop_progress(&ctx->progress);
1954 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1956 uint32_t i;
1957 enum commit_graph_split_flags flags = ctx->opts ?
1958 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1960 ctx->num_extra_edges = 0;
1961 if (ctx->report_progress)
1962 ctx->progress = start_delayed_progress(
1963 _("Finding extra edges in commit graph"),
1964 ctx->oids.nr);
1965 oid_array_sort(&ctx->oids);
1966 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1967 unsigned int num_parents;
1969 display_progress(ctx->progress, i + 1);
1971 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1972 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1974 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1975 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1976 continue;
1978 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1979 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1980 else
1981 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1983 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1984 if (num_parents > 2)
1985 ctx->num_extra_edges += num_parents - 1;
1987 ctx->commits.nr++;
1989 stop_progress(&ctx->progress);
1992 static int write_graph_chunk_base_1(struct hashfile *f,
1993 struct commit_graph *g)
1995 int num = 0;
1997 if (!g)
1998 return 0;
2000 num = write_graph_chunk_base_1(f, g->base_graph);
2001 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
2002 return num + 1;
2005 static int write_graph_chunk_base(struct hashfile *f,
2006 void *data)
2008 struct write_commit_graph_context *ctx = data;
2009 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
2011 if (num != ctx->num_commit_graphs_after - 1) {
2012 error(_("failed to write correct number of base graph ids"));
2013 return -1;
2016 return 0;
2019 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
2021 uint32_t i;
2022 int fd;
2023 struct hashfile *f;
2024 struct lock_file lk = LOCK_INIT;
2025 const unsigned hashsz = the_hash_algo->rawsz;
2026 struct strbuf progress_title = STRBUF_INIT;
2027 struct chunkfile *cf;
2028 unsigned char file_hash[GIT_MAX_RAWSZ];
2030 if (ctx->split) {
2031 struct strbuf tmp_file = STRBUF_INIT;
2033 strbuf_addf(&tmp_file,
2034 "%s/info/commit-graphs/tmp_graph_XXXXXX",
2035 ctx->odb->path);
2036 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
2037 } else {
2038 ctx->graph_name = get_commit_graph_filename(ctx->odb);
2041 if (safe_create_leading_directories(ctx->graph_name)) {
2042 UNLEAK(ctx->graph_name);
2043 error(_("unable to create leading directories of %s"),
2044 ctx->graph_name);
2045 return -1;
2048 if (ctx->split) {
2049 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
2051 hold_lock_file_for_update_mode(&lk, lock_name,
2052 LOCK_DIE_ON_ERROR, 0444);
2053 free(lock_name);
2055 fd = git_mkstemp_mode(ctx->graph_name, 0444);
2056 if (fd < 0) {
2057 error(_("unable to create temporary graph layer"));
2058 return -1;
2061 if (adjust_shared_perm(ctx->graph_name)) {
2062 error(_("unable to adjust shared permissions for '%s'"),
2063 ctx->graph_name);
2064 return -1;
2067 f = hashfd(fd, ctx->graph_name);
2068 } else {
2069 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
2070 LOCK_DIE_ON_ERROR, 0444);
2071 fd = get_lock_file_fd(&lk);
2072 f = hashfd(fd, get_lock_file_path(&lk));
2075 cf = init_chunkfile(f);
2077 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
2078 write_graph_chunk_fanout);
2079 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, st_mult(hashsz, ctx->commits.nr),
2080 write_graph_chunk_oids);
2081 add_chunk(cf, GRAPH_CHUNKID_DATA, st_mult(hashsz + 16, ctx->commits.nr),
2082 write_graph_chunk_data);
2084 if (ctx->write_generation_data)
2085 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
2086 st_mult(sizeof(uint32_t), ctx->commits.nr),
2087 write_graph_chunk_generation_data);
2088 if (ctx->num_generation_data_overflows)
2089 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
2090 st_mult(sizeof(timestamp_t), ctx->num_generation_data_overflows),
2091 write_graph_chunk_generation_data_overflow);
2092 if (ctx->num_extra_edges)
2093 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
2094 st_mult(4, ctx->num_extra_edges),
2095 write_graph_chunk_extra_edges);
2096 if (ctx->changed_paths) {
2097 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
2098 st_mult(sizeof(uint32_t), ctx->commits.nr),
2099 write_graph_chunk_bloom_indexes);
2100 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
2101 st_add(sizeof(uint32_t) * 3,
2102 ctx->total_bloom_filter_data_size),
2103 write_graph_chunk_bloom_data);
2105 if (ctx->num_commit_graphs_after > 1)
2106 add_chunk(cf, GRAPH_CHUNKID_BASE,
2107 st_mult(hashsz, ctx->num_commit_graphs_after - 1),
2108 write_graph_chunk_base);
2110 hashwrite_be32(f, GRAPH_SIGNATURE);
2112 hashwrite_u8(f, GRAPH_VERSION);
2113 hashwrite_u8(f, oid_version(the_hash_algo));
2114 hashwrite_u8(f, get_num_chunks(cf));
2115 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
2117 if (ctx->report_progress) {
2118 strbuf_addf(&progress_title,
2119 Q_("Writing out commit graph in %d pass",
2120 "Writing out commit graph in %d passes",
2121 get_num_chunks(cf)),
2122 get_num_chunks(cf));
2123 ctx->progress = start_delayed_progress(
2124 progress_title.buf,
2125 st_mult(get_num_chunks(cf), ctx->commits.nr));
2128 write_chunkfile(cf, ctx);
2130 stop_progress(&ctx->progress);
2131 strbuf_release(&progress_title);
2133 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
2134 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
2135 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
2137 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
2138 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
2139 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
2140 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
2143 close_commit_graph(ctx->r->objects);
2144 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
2145 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
2146 free_chunkfile(cf);
2148 if (ctx->split) {
2149 FILE *chainf = fdopen_lock_file(&lk, "w");
2150 char *final_graph_name;
2151 int result;
2153 close(fd);
2155 if (!chainf) {
2156 error(_("unable to open commit-graph chain file"));
2157 return -1;
2160 if (ctx->base_graph_name) {
2161 const char *dest;
2162 int idx = ctx->num_commit_graphs_after - 1;
2163 if (ctx->num_commit_graphs_after > 1)
2164 idx--;
2166 dest = ctx->commit_graph_filenames_after[idx];
2168 if (strcmp(ctx->base_graph_name, dest)) {
2169 result = rename(ctx->base_graph_name, dest);
2171 if (result) {
2172 error(_("failed to rename base commit-graph file"));
2173 return -1;
2176 } else {
2177 char *graph_name = get_commit_graph_filename(ctx->odb);
2178 unlink(graph_name);
2179 free(graph_name);
2182 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2183 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
2184 final_graph_name = get_split_graph_filename(ctx->odb,
2185 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2186 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1]);
2187 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
2189 result = rename(ctx->graph_name, final_graph_name);
2191 for (i = 0; i < ctx->num_commit_graphs_after; i++)
2192 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2194 if (result) {
2195 error(_("failed to rename temporary commit-graph file"));
2196 return -1;
2200 commit_lock_file(&lk);
2202 return 0;
2205 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2207 struct commit_graph *g;
2208 uint32_t num_commits;
2209 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2210 uint32_t i;
2212 int max_commits = 0;
2213 int size_mult = 2;
2215 if (ctx->opts) {
2216 max_commits = ctx->opts->max_commits;
2218 if (ctx->opts->size_multiple)
2219 size_mult = ctx->opts->size_multiple;
2221 flags = ctx->opts->split_flags;
2224 g = ctx->r->objects->commit_graph;
2225 num_commits = ctx->commits.nr;
2226 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2227 ctx->num_commit_graphs_after = 1;
2228 else
2229 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2231 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2232 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2233 while (g && (g->num_commits <= st_mult(size_mult, num_commits) ||
2234 (max_commits && num_commits > max_commits))) {
2235 if (g->odb != ctx->odb)
2236 break;
2238 if (unsigned_add_overflows(num_commits, g->num_commits))
2239 die(_("cannot merge graphs with %"PRIuMAX", "
2240 "%"PRIuMAX" commits"),
2241 (uintmax_t)num_commits,
2242 (uintmax_t)g->num_commits);
2243 num_commits += g->num_commits;
2244 g = g->base_graph;
2246 ctx->num_commit_graphs_after--;
2250 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2251 ctx->new_base_graph = g;
2252 else if (ctx->num_commit_graphs_after != 1)
2253 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2254 "should be 1 with --split=replace");
2256 if (ctx->num_commit_graphs_after == 2) {
2257 char *old_graph_name = get_commit_graph_filename(g->odb);
2259 if (!strcmp(g->filename, old_graph_name) &&
2260 g->odb != ctx->odb) {
2261 ctx->num_commit_graphs_after = 1;
2262 ctx->new_base_graph = NULL;
2265 free(old_graph_name);
2268 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2269 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2271 for (i = 0; i < ctx->num_commit_graphs_after &&
2272 i < ctx->num_commit_graphs_before; i++)
2273 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2275 i = ctx->num_commit_graphs_before - 1;
2276 g = ctx->r->objects->commit_graph;
2278 while (g) {
2279 if (i < ctx->num_commit_graphs_after)
2280 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2283 * If the topmost remaining layer has generation data chunk, the
2284 * resultant layer also has generation data chunk.
2286 if (i == ctx->num_commit_graphs_after - 2)
2287 ctx->write_generation_data = !!g->chunk_generation_data;
2289 i--;
2290 g = g->base_graph;
2294 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2295 struct commit_graph *g)
2297 uint32_t i;
2298 uint32_t offset = g->num_commits_in_base;
2300 if (unsigned_add_overflows(ctx->commits.nr, g->num_commits))
2301 die(_("cannot merge graph %s, too many commits: %"PRIuMAX),
2302 oid_to_hex(&g->oid),
2303 (uintmax_t)st_add(ctx->commits.nr, g->num_commits));
2305 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2307 for (i = 0; i < g->num_commits; i++) {
2308 struct object_id oid;
2309 struct commit *result;
2311 display_progress(ctx->progress, i + 1);
2313 load_oid_from_graph(g, i + offset, &oid);
2315 /* only add commits if they still exist in the repo */
2316 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2318 if (result) {
2319 ctx->commits.list[ctx->commits.nr] = result;
2320 ctx->commits.nr++;
2325 static int commit_compare(const void *_a, const void *_b)
2327 const struct commit *a = *(const struct commit **)_a;
2328 const struct commit *b = *(const struct commit **)_b;
2329 return oidcmp(&a->object.oid, &b->object.oid);
2332 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2334 uint32_t i, dedup_i = 0;
2336 if (ctx->report_progress)
2337 ctx->progress = start_delayed_progress(
2338 _("Scanning merged commits"),
2339 ctx->commits.nr);
2341 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2343 ctx->num_extra_edges = 0;
2344 for (i = 0; i < ctx->commits.nr; i++) {
2345 display_progress(ctx->progress, i + 1);
2347 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2348 &ctx->commits.list[i]->object.oid)) {
2350 * Silently ignore duplicates. These were likely
2351 * created due to a commit appearing in multiple
2352 * layers of the chain, which is unexpected but
2353 * not invalid. We should make sure there is a
2354 * unique copy in the new layer.
2356 } else {
2357 unsigned int num_parents;
2359 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2360 dedup_i++;
2362 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2363 if (num_parents > 2)
2364 ctx->num_extra_edges += num_parents - 1;
2368 ctx->commits.nr = dedup_i;
2370 stop_progress(&ctx->progress);
2373 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2375 struct commit_graph *g = ctx->r->objects->commit_graph;
2376 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2378 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2379 current_graph_number--;
2381 if (ctx->report_progress)
2382 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2384 merge_commit_graph(ctx, g);
2385 stop_progress(&ctx->progress);
2387 g = g->base_graph;
2390 if (g) {
2391 ctx->new_base_graph = g;
2392 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2395 if (ctx->new_base_graph)
2396 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2398 sort_and_scan_merged_commits(ctx);
2401 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2403 uint32_t i;
2404 time_t now = time(NULL);
2406 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2407 struct stat st;
2408 struct utimbuf updated_time;
2410 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2411 continue;
2413 updated_time.actime = st.st_atime;
2414 updated_time.modtime = now;
2415 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2419 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2421 struct strbuf path = STRBUF_INIT;
2422 DIR *dir;
2423 struct dirent *de;
2424 size_t dirnamelen;
2425 timestamp_t expire_time = time(NULL);
2427 if (ctx->opts && ctx->opts->expire_time)
2428 expire_time = ctx->opts->expire_time;
2429 if (!ctx->split) {
2430 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2431 unlink(chain_file_name);
2432 free(chain_file_name);
2433 ctx->num_commit_graphs_after = 0;
2436 strbuf_addstr(&path, ctx->odb->path);
2437 strbuf_addstr(&path, "/info/commit-graphs");
2438 dir = opendir(path.buf);
2440 if (!dir)
2441 goto out;
2443 strbuf_addch(&path, '/');
2444 dirnamelen = path.len;
2445 while ((de = readdir(dir)) != NULL) {
2446 struct stat st;
2447 uint32_t i, found = 0;
2449 strbuf_setlen(&path, dirnamelen);
2450 strbuf_addstr(&path, de->d_name);
2452 if (stat(path.buf, &st) < 0)
2453 continue;
2455 if (st.st_mtime > expire_time)
2456 continue;
2457 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2458 continue;
2460 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2461 if (!strcmp(ctx->commit_graph_filenames_after[i],
2462 path.buf)) {
2463 found = 1;
2464 break;
2468 if (!found)
2469 unlink(path.buf);
2472 out:
2473 if(dir)
2474 closedir(dir);
2475 strbuf_release(&path);
2478 int write_commit_graph(struct object_directory *odb,
2479 const struct string_list *const pack_indexes,
2480 struct oidset *commits,
2481 enum commit_graph_write_flags flags,
2482 const struct commit_graph_opts *opts)
2484 struct repository *r = the_repository;
2485 struct write_commit_graph_context *ctx;
2486 uint32_t i;
2487 int res = 0;
2488 int replace = 0;
2489 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2490 struct topo_level_slab topo_levels;
2492 prepare_repo_settings(r);
2493 if (!r->settings.core_commit_graph) {
2494 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2495 return 0;
2497 if (!commit_graph_compatible(r))
2498 return 0;
2500 CALLOC_ARRAY(ctx, 1);
2501 ctx->r = r;
2502 ctx->odb = odb;
2503 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2504 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2505 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2506 ctx->opts = opts;
2507 ctx->total_bloom_filter_data_size = 0;
2508 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2509 ctx->num_generation_data_overflows = 0;
2511 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2512 bloom_settings.bits_per_entry);
2513 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2514 bloom_settings.num_hashes);
2515 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2516 bloom_settings.max_changed_paths);
2517 ctx->bloom_settings = &bloom_settings;
2519 init_topo_level_slab(&topo_levels);
2520 ctx->topo_levels = &topo_levels;
2522 prepare_commit_graph(ctx->r);
2523 if (ctx->r->objects->commit_graph) {
2524 struct commit_graph *g = ctx->r->objects->commit_graph;
2526 while (g) {
2527 g->topo_levels = &topo_levels;
2528 g = g->base_graph;
2532 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2533 ctx->changed_paths = 1;
2534 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2535 struct commit_graph *g;
2537 g = ctx->r->objects->commit_graph;
2539 /* We have changed-paths already. Keep them in the next graph */
2540 if (g && g->chunk_bloom_data) {
2541 ctx->changed_paths = 1;
2542 ctx->bloom_settings = g->bloom_filter_settings;
2546 if (ctx->split) {
2547 struct commit_graph *g = ctx->r->objects->commit_graph;
2549 while (g) {
2550 ctx->num_commit_graphs_before++;
2551 g = g->base_graph;
2554 if (ctx->num_commit_graphs_before) {
2555 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2556 i = ctx->num_commit_graphs_before;
2557 g = ctx->r->objects->commit_graph;
2559 while (g) {
2560 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2561 g = g->base_graph;
2565 if (ctx->opts)
2566 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2569 ctx->approx_nr_objects = repo_approximate_object_count(the_repository);
2571 if (ctx->append && ctx->r->objects->commit_graph) {
2572 struct commit_graph *g = ctx->r->objects->commit_graph;
2573 for (i = 0; i < g->num_commits; i++) {
2574 struct object_id oid;
2575 oidread(&oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2576 oid_array_append(&ctx->oids, &oid);
2580 if (pack_indexes) {
2581 ctx->order_by_pack = 1;
2582 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2583 goto cleanup;
2586 if (commits) {
2587 if ((res = fill_oids_from_commits(ctx, commits)))
2588 goto cleanup;
2591 if (!pack_indexes && !commits) {
2592 ctx->order_by_pack = 1;
2593 fill_oids_from_all_packs(ctx);
2596 close_reachable(ctx);
2598 copy_oids_to_commits(ctx);
2600 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2601 error(_("too many commits to write graph"));
2602 res = -1;
2603 goto cleanup;
2606 if (!ctx->commits.nr && !replace)
2607 goto cleanup;
2609 if (ctx->split) {
2610 split_graph_merge_strategy(ctx);
2612 if (!replace)
2613 merge_commit_graphs(ctx);
2614 } else
2615 ctx->num_commit_graphs_after = 1;
2617 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2619 compute_topological_levels(ctx);
2620 if (ctx->write_generation_data)
2621 compute_generation_numbers(ctx);
2623 if (ctx->changed_paths)
2624 compute_bloom_filters(ctx);
2626 res = write_commit_graph_file(ctx);
2628 if (ctx->split)
2629 mark_commit_graphs(ctx);
2631 expire_commit_graphs(ctx);
2633 cleanup:
2634 free(ctx->graph_name);
2635 free(ctx->base_graph_name);
2636 free(ctx->commits.list);
2637 oid_array_clear(&ctx->oids);
2638 clear_topo_level_slab(&topo_levels);
2640 if (ctx->commit_graph_filenames_after) {
2641 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2642 free(ctx->commit_graph_filenames_after[i]);
2643 free(ctx->commit_graph_hash_after[i]);
2646 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2647 free(ctx->commit_graph_filenames_before[i]);
2649 free(ctx->commit_graph_filenames_after);
2650 free(ctx->commit_graph_filenames_before);
2651 free(ctx->commit_graph_hash_after);
2654 free(ctx);
2656 return res;
2659 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2660 static int verify_commit_graph_error;
2662 __attribute__((format (printf, 1, 2)))
2663 static void graph_report(const char *fmt, ...)
2665 va_list ap;
2667 verify_commit_graph_error = 1;
2668 va_start(ap, fmt);
2669 vfprintf(stderr, fmt, ap);
2670 fprintf(stderr, "\n");
2671 va_end(ap);
2674 static int commit_graph_checksum_valid(struct commit_graph *g)
2676 return hashfile_checksum_valid(g->data, g->data_len);
2679 static int verify_one_commit_graph(struct repository *r,
2680 struct commit_graph *g,
2681 struct progress *progress,
2682 uint64_t *seen)
2684 uint32_t i, cur_fanout_pos = 0;
2685 struct object_id prev_oid, cur_oid;
2686 struct commit *seen_gen_zero = NULL;
2687 struct commit *seen_gen_non_zero = NULL;
2689 verify_commit_graph_error = verify_commit_graph_lite(g);
2690 if (verify_commit_graph_error)
2691 return verify_commit_graph_error;
2693 if (!commit_graph_checksum_valid(g)) {
2694 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2695 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2698 for (i = 0; i < g->num_commits; i++) {
2699 struct commit *graph_commit;
2701 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2703 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2704 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2705 oid_to_hex(&prev_oid),
2706 oid_to_hex(&cur_oid));
2708 oidcpy(&prev_oid, &cur_oid);
2710 while (cur_oid.hash[0] > cur_fanout_pos) {
2711 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2713 if (i != fanout_value)
2714 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2715 cur_fanout_pos, fanout_value, i);
2716 cur_fanout_pos++;
2719 graph_commit = lookup_commit(r, &cur_oid);
2720 if (!parse_commit_in_graph_one(r, g, graph_commit))
2721 graph_report(_("failed to parse commit %s from commit-graph"),
2722 oid_to_hex(&cur_oid));
2725 while (cur_fanout_pos < 256) {
2726 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2728 if (g->num_commits != fanout_value)
2729 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2730 cur_fanout_pos, fanout_value, i);
2732 cur_fanout_pos++;
2735 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2736 return verify_commit_graph_error;
2738 for (i = 0; i < g->num_commits; i++) {
2739 struct commit *graph_commit, *odb_commit;
2740 struct commit_list *graph_parents, *odb_parents;
2741 timestamp_t max_generation = 0;
2742 timestamp_t generation;
2744 display_progress(progress, ++(*seen));
2745 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2747 graph_commit = lookup_commit(r, &cur_oid);
2748 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2749 if (repo_parse_commit_internal(r, odb_commit, 0, 0)) {
2750 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2751 oid_to_hex(&cur_oid));
2752 continue;
2755 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2756 get_commit_tree_oid(odb_commit)))
2757 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2758 oid_to_hex(&cur_oid),
2759 oid_to_hex(get_commit_tree_oid(graph_commit)),
2760 oid_to_hex(get_commit_tree_oid(odb_commit)));
2762 graph_parents = graph_commit->parents;
2763 odb_parents = odb_commit->parents;
2765 while (graph_parents) {
2766 if (!odb_parents) {
2767 graph_report(_("commit-graph parent list for commit %s is too long"),
2768 oid_to_hex(&cur_oid));
2769 break;
2772 /* parse parent in case it is in a base graph */
2773 parse_commit_in_graph_one(r, g, graph_parents->item);
2775 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2776 graph_report(_("commit-graph parent for %s is %s != %s"),
2777 oid_to_hex(&cur_oid),
2778 oid_to_hex(&graph_parents->item->object.oid),
2779 oid_to_hex(&odb_parents->item->object.oid));
2781 generation = commit_graph_generation_from_graph(graph_parents->item);
2782 if (generation > max_generation)
2783 max_generation = generation;
2785 graph_parents = graph_parents->next;
2786 odb_parents = odb_parents->next;
2789 if (odb_parents)
2790 graph_report(_("commit-graph parent list for commit %s terminates early"),
2791 oid_to_hex(&cur_oid));
2793 if (commit_graph_generation_from_graph(graph_commit))
2794 seen_gen_non_zero = graph_commit;
2795 else
2796 seen_gen_zero = graph_commit;
2798 if (seen_gen_zero)
2799 continue;
2802 * If we are using topological level and one of our parents has
2803 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2804 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2805 * in the following condition.
2807 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2808 max_generation--;
2810 generation = commit_graph_generation(graph_commit);
2811 if (generation < max_generation + 1)
2812 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2813 oid_to_hex(&cur_oid),
2814 generation,
2815 max_generation + 1);
2817 if (graph_commit->date != odb_commit->date)
2818 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2819 oid_to_hex(&cur_oid),
2820 graph_commit->date,
2821 odb_commit->date);
2824 if (seen_gen_zero && seen_gen_non_zero)
2825 graph_report(_("commit-graph has both zero and non-zero "
2826 "generations (e.g., commits '%s' and '%s')"),
2827 oid_to_hex(&seen_gen_zero->object.oid),
2828 oid_to_hex(&seen_gen_non_zero->object.oid));
2830 return verify_commit_graph_error;
2833 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2835 struct progress *progress = NULL;
2836 int local_error = 0;
2837 uint64_t seen = 0;
2839 if (!g) {
2840 graph_report("no commit-graph file loaded");
2841 return 1;
2844 if (flags & COMMIT_GRAPH_WRITE_PROGRESS) {
2845 uint64_t total = g->num_commits;
2846 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW))
2847 total += g->num_commits_in_base;
2849 progress = start_progress(_("Verifying commits in commit graph"),
2850 total);
2853 for (; g; g = g->base_graph) {
2854 local_error |= verify_one_commit_graph(r, g, progress, &seen);
2855 if (flags & COMMIT_GRAPH_VERIFY_SHALLOW)
2856 break;
2859 stop_progress(&progress);
2861 return local_error;
2864 void free_commit_graph(struct commit_graph *g)
2866 while (g) {
2867 struct commit_graph *next = g->base_graph;
2869 if (g->data)
2870 munmap((void *)g->data, g->data_len);
2871 free(g->filename);
2872 free(g->bloom_filter_settings);
2873 free(g);
2875 g = next;
2879 void disable_commit_graph(struct repository *r)
2881 r->commit_graph_disabled = 1;