Sync with 'maint'
[alt-git.git] / commit-graph.c
blob93c075552a4c5d7308007362006cd44d16897e15
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "config.h"
5 #include "csum-file.h"
6 #include "gettext.h"
7 #include "hex.h"
8 #include "lockfile.h"
9 #include "packfile.h"
10 #include "commit.h"
11 #include "object.h"
12 #include "refs.h"
13 #include "hash-lookup.h"
14 #include "commit-graph.h"
15 #include "object-file.h"
16 #include "object-store-ll.h"
17 #include "oid-array.h"
18 #include "path.h"
19 #include "alloc.h"
20 #include "hashmap.h"
21 #include "replace-object.h"
22 #include "progress.h"
23 #include "bloom.h"
24 #include "commit-slab.h"
25 #include "shallow.h"
26 #include "json-writer.h"
27 #include "trace2.h"
28 #include "tree.h"
29 #include "chunk-format.h"
31 void git_test_write_commit_graph_or_die(void)
33 int flags = 0;
34 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
35 return;
37 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
38 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
40 if (write_commit_graph_reachable(the_repository->objects->odb,
41 flags, NULL))
42 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
45 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
46 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
47 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
48 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
49 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
50 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
51 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
52 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
53 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
54 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
56 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
58 #define GRAPH_VERSION_1 0x1
59 #define GRAPH_VERSION GRAPH_VERSION_1
61 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
62 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
63 #define GRAPH_PARENT_NONE 0x70000000
65 #define GRAPH_LAST_EDGE 0x80000000
67 #define GRAPH_HEADER_SIZE 8
68 #define GRAPH_FANOUT_SIZE (4 * 256)
69 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
70 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
72 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
74 /* Remember to update object flag allocation in object.h */
75 #define REACHABLE (1u<<15)
77 define_commit_slab(topo_level_slab, uint32_t);
79 /* Keep track of the order in which commits are added to our list. */
80 define_commit_slab(commit_pos, int);
81 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
83 static void set_commit_pos(struct repository *r, const struct object_id *oid)
85 static int32_t max_pos;
86 struct commit *commit = lookup_commit(r, oid);
88 if (!commit)
89 return; /* should never happen, but be lenient */
91 *commit_pos_at(&commit_pos, commit) = max_pos++;
94 static int commit_pos_cmp(const void *va, const void *vb)
96 const struct commit *a = *(const struct commit **)va;
97 const struct commit *b = *(const struct commit **)vb;
98 return commit_pos_at(&commit_pos, a) -
99 commit_pos_at(&commit_pos, b);
102 define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
103 static struct commit_graph_data_slab commit_graph_data_slab =
104 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
106 static int get_configured_generation_version(struct repository *r)
108 int version = 2;
109 repo_config_get_int(r, "commitgraph.generationversion", &version);
110 return version;
113 uint32_t commit_graph_position(const struct commit *c)
115 struct commit_graph_data *data =
116 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
118 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
121 timestamp_t commit_graph_generation(const struct commit *c)
123 struct commit_graph_data *data =
124 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
126 if (data && data->generation)
127 return data->generation;
129 return GENERATION_NUMBER_INFINITY;
132 static timestamp_t commit_graph_generation_from_graph(const struct commit *c)
134 struct commit_graph_data *data =
135 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
137 if (!data || data->graph_pos == COMMIT_NOT_FROM_GRAPH)
138 return GENERATION_NUMBER_INFINITY;
139 return data->generation;
142 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
144 unsigned int i, nth_slab;
145 struct commit_graph_data *data =
146 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
148 if (data)
149 return data;
151 nth_slab = c->index / commit_graph_data_slab.slab_size;
152 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
155 * commit-slab initializes elements with zero, overwrite this with
156 * COMMIT_NOT_FROM_GRAPH for graph_pos.
158 * We avoid initializing generation with checking if graph position
159 * is not COMMIT_NOT_FROM_GRAPH.
161 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
162 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
163 COMMIT_NOT_FROM_GRAPH;
166 return data;
170 * Should be used only while writing commit-graph as it compares
171 * generation value of commits by directly accessing commit-slab.
173 static int commit_gen_cmp(const void *va, const void *vb)
175 const struct commit *a = *(const struct commit **)va;
176 const struct commit *b = *(const struct commit **)vb;
178 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
179 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
180 /* lower generation commits first */
181 if (generation_a < generation_b)
182 return -1;
183 else if (generation_a > generation_b)
184 return 1;
186 /* use date as a heuristic when generations are equal */
187 if (a->date < b->date)
188 return -1;
189 else if (a->date > b->date)
190 return 1;
191 return 0;
194 char *get_commit_graph_filename(struct object_directory *obj_dir)
196 return xstrfmt("%s/info/commit-graph", obj_dir->path);
199 static char *get_split_graph_filename(struct object_directory *odb,
200 const char *oid_hex)
202 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
203 oid_hex);
206 char *get_commit_graph_chain_filename(struct object_directory *odb)
208 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
211 static struct commit_graph *alloc_commit_graph(void)
213 struct commit_graph *g = xcalloc(1, sizeof(*g));
215 return g;
218 static int commit_graph_compatible(struct repository *r)
220 if (!r->gitdir)
221 return 0;
223 if (replace_refs_enabled(r)) {
224 prepare_replace_object(r);
225 if (hashmap_get_size(&r->objects->replace_map->map))
226 return 0;
229 prepare_commit_graft(r);
230 if (r->parsed_objects &&
231 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
232 return 0;
233 if (is_repository_shallow(r))
234 return 0;
236 return 1;
239 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
241 *fd = git_open(graph_file);
242 if (*fd < 0)
243 return 0;
244 if (fstat(*fd, st)) {
245 close(*fd);
246 return 0;
248 return 1;
251 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
252 int fd, struct stat *st,
253 struct object_directory *odb)
255 void *graph_map;
256 size_t graph_size;
257 struct commit_graph *ret;
259 graph_size = xsize_t(st->st_size);
261 if (graph_size < GRAPH_MIN_SIZE) {
262 close(fd);
263 error(_("commit-graph file is too small"));
264 return NULL;
266 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
267 close(fd);
268 prepare_repo_settings(r);
269 ret = parse_commit_graph(&r->settings, graph_map, graph_size);
271 if (ret)
272 ret->odb = odb;
273 else
274 munmap(graph_map, graph_size);
276 return ret;
279 static int graph_read_oid_fanout(const unsigned char *chunk_start,
280 size_t chunk_size, void *data)
282 struct commit_graph *g = data;
283 int i;
285 if (chunk_size != 256 * sizeof(uint32_t))
286 return error(_("commit-graph oid fanout chunk is wrong size"));
287 g->chunk_oid_fanout = (const uint32_t *)chunk_start;
288 g->num_commits = ntohl(g->chunk_oid_fanout[255]);
290 for (i = 0; i < 255; i++) {
291 uint32_t oid_fanout1 = ntohl(g->chunk_oid_fanout[i]);
292 uint32_t oid_fanout2 = ntohl(g->chunk_oid_fanout[i + 1]);
294 if (oid_fanout1 > oid_fanout2) {
295 error(_("commit-graph fanout values out of order"));
296 return 1;
300 return 0;
303 static int graph_read_oid_lookup(const unsigned char *chunk_start,
304 size_t chunk_size, void *data)
306 struct commit_graph *g = data;
307 g->chunk_oid_lookup = chunk_start;
308 if (chunk_size / g->hash_len != g->num_commits)
309 return error(_("commit-graph OID lookup chunk is the wrong size"));
310 return 0;
313 static int graph_read_commit_data(const unsigned char *chunk_start,
314 size_t chunk_size, void *data)
316 struct commit_graph *g = data;
317 if (chunk_size / GRAPH_DATA_WIDTH != g->num_commits)
318 return error(_("commit-graph commit data chunk is wrong size"));
319 g->chunk_commit_data = chunk_start;
320 return 0;
323 static int graph_read_generation_data(const unsigned char *chunk_start,
324 size_t chunk_size, void *data)
326 struct commit_graph *g = data;
327 if (chunk_size / sizeof(uint32_t) != g->num_commits)
328 return error(_("commit-graph generations chunk is wrong size"));
329 g->chunk_generation_data = chunk_start;
330 return 0;
333 static int graph_read_bloom_index(const unsigned char *chunk_start,
334 size_t chunk_size, void *data)
336 struct commit_graph *g = data;
337 if (chunk_size / 4 != g->num_commits) {
338 warning(_("commit-graph changed-path index chunk is too small"));
339 return -1;
341 g->chunk_bloom_indexes = chunk_start;
342 return 0;
345 static int graph_read_bloom_data(const unsigned char *chunk_start,
346 size_t chunk_size, void *data)
348 struct commit_graph *g = data;
349 uint32_t hash_version;
351 if (chunk_size < BLOOMDATA_CHUNK_HEADER_SIZE) {
352 warning(_("ignoring too-small changed-path chunk"
353 " (%"PRIuMAX" < %"PRIuMAX") in commit-graph file"),
354 (uintmax_t)chunk_size,
355 (uintmax_t)BLOOMDATA_CHUNK_HEADER_SIZE);
356 return -1;
359 g->chunk_bloom_data = chunk_start;
360 g->chunk_bloom_data_size = chunk_size;
361 hash_version = get_be32(chunk_start);
363 if (hash_version != 1)
364 return 0;
366 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
367 g->bloom_filter_settings->hash_version = hash_version;
368 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
369 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
370 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
372 return 0;
375 struct commit_graph *parse_commit_graph(struct repo_settings *s,
376 void *graph_map, size_t graph_size)
378 const unsigned char *data;
379 struct commit_graph *graph;
380 uint32_t graph_signature;
381 unsigned char graph_version, hash_version;
382 struct chunkfile *cf = NULL;
384 if (!graph_map)
385 return NULL;
387 if (graph_size < GRAPH_MIN_SIZE)
388 return NULL;
390 data = (const unsigned char *)graph_map;
392 graph_signature = get_be32(data);
393 if (graph_signature != GRAPH_SIGNATURE) {
394 error(_("commit-graph signature %X does not match signature %X"),
395 graph_signature, GRAPH_SIGNATURE);
396 return NULL;
399 graph_version = *(unsigned char*)(data + 4);
400 if (graph_version != GRAPH_VERSION) {
401 error(_("commit-graph version %X does not match version %X"),
402 graph_version, GRAPH_VERSION);
403 return NULL;
406 hash_version = *(unsigned char*)(data + 5);
407 if (hash_version != oid_version(the_hash_algo)) {
408 error(_("commit-graph hash version %X does not match version %X"),
409 hash_version, oid_version(the_hash_algo));
410 return NULL;
413 graph = alloc_commit_graph();
415 graph->hash_len = the_hash_algo->rawsz;
416 graph->num_chunks = *(unsigned char*)(data + 6);
417 graph->data = graph_map;
418 graph->data_len = graph_size;
420 if (graph_size < GRAPH_HEADER_SIZE +
421 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
422 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
423 error(_("commit-graph file is too small to hold %u chunks"),
424 graph->num_chunks);
425 free(graph);
426 return NULL;
429 cf = init_chunkfile(NULL);
431 if (read_table_of_contents(cf, graph->data, graph_size,
432 GRAPH_HEADER_SIZE, graph->num_chunks, 1))
433 goto free_and_return;
435 if (read_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, graph_read_oid_fanout, graph)) {
436 error(_("commit-graph required OID fanout chunk missing or corrupted"));
437 goto free_and_return;
439 if (read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph)) {
440 error(_("commit-graph required OID lookup chunk missing or corrupted"));
441 goto free_and_return;
443 if (read_chunk(cf, GRAPH_CHUNKID_DATA, graph_read_commit_data, graph)) {
444 error(_("commit-graph required commit data chunk missing or corrupted"));
445 goto free_and_return;
448 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges,
449 &graph->chunk_extra_edges_size);
450 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs,
451 &graph->chunk_base_graphs_size);
453 if (s->commit_graph_generation_version >= 2) {
454 read_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
455 graph_read_generation_data, graph);
456 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
457 &graph->chunk_generation_data_overflow,
458 &graph->chunk_generation_data_overflow_size);
460 if (graph->chunk_generation_data)
461 graph->read_generation_data = 1;
464 if (s->commit_graph_read_changed_paths) {
465 read_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
466 graph_read_bloom_index, graph);
467 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
468 graph_read_bloom_data, graph);
471 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
472 init_bloom_filters();
473 } else {
474 /* We need both the bloom chunks to exist together. Else ignore the data */
475 graph->chunk_bloom_indexes = NULL;
476 graph->chunk_bloom_data = NULL;
477 FREE_AND_NULL(graph->bloom_filter_settings);
480 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len,
481 the_repository->hash_algo);
483 free_chunkfile(cf);
484 return graph;
486 free_and_return:
487 free_chunkfile(cf);
488 free(graph->bloom_filter_settings);
489 free(graph);
490 return NULL;
493 static struct commit_graph *load_commit_graph_one(struct repository *r,
494 const char *graph_file,
495 struct object_directory *odb)
498 struct stat st;
499 int fd;
500 struct commit_graph *g;
501 int open_ok = open_commit_graph(graph_file, &fd, &st);
503 if (!open_ok)
504 return NULL;
506 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
508 if (g)
509 g->filename = xstrdup(graph_file);
511 return g;
514 static struct commit_graph *load_commit_graph_v1(struct repository *r,
515 struct object_directory *odb)
517 char *graph_name = get_commit_graph_filename(odb);
518 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
519 free(graph_name);
521 return g;
525 * returns 1 if and only if all graphs in the chain have
526 * corrected commit dates stored in the generation_data chunk.
528 static int validate_mixed_generation_chain(struct commit_graph *g)
530 int read_generation_data = 1;
531 struct commit_graph *p = g;
533 while (read_generation_data && p) {
534 read_generation_data = p->read_generation_data;
535 p = p->base_graph;
538 if (read_generation_data)
539 return 1;
541 while (g) {
542 g->read_generation_data = 0;
543 g = g->base_graph;
546 return 0;
549 static int add_graph_to_chain(struct commit_graph *g,
550 struct commit_graph *chain,
551 struct object_id *oids,
552 int n)
554 struct commit_graph *cur_g = chain;
556 if (n && !g->chunk_base_graphs) {
557 warning(_("commit-graph has no base graphs chunk"));
558 return 0;
561 if (g->chunk_base_graphs_size / g->hash_len < n) {
562 warning(_("commit-graph base graphs chunk is too small"));
563 return 0;
566 while (n) {
567 n--;
569 if (!cur_g ||
570 !oideq(&oids[n], &cur_g->oid) ||
571 !hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_len, n),
572 the_repository->hash_algo)) {
573 warning(_("commit-graph chain does not match"));
574 return 0;
577 cur_g = cur_g->base_graph;
580 if (chain) {
581 if (unsigned_add_overflows(chain->num_commits,
582 chain->num_commits_in_base)) {
583 warning(_("commit count in base graph too high: %"PRIuMAX),
584 (uintmax_t)chain->num_commits_in_base);
585 return 0;
587 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
590 g->base_graph = chain;
592 return 1;
595 int open_commit_graph_chain(const char *chain_file,
596 int *fd, struct stat *st)
598 *fd = git_open(chain_file);
599 if (*fd < 0)
600 return 0;
601 if (fstat(*fd, st)) {
602 close(*fd);
603 return 0;
605 if (st->st_size < the_hash_algo->hexsz) {
606 close(*fd);
607 if (!st->st_size) {
608 /* treat empty files the same as missing */
609 errno = ENOENT;
610 } else {
611 warning(_("commit-graph chain file too small"));
612 errno = EINVAL;
614 return 0;
616 return 1;
619 struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
620 int fd, struct stat *st,
621 int *incomplete_chain)
623 struct commit_graph *graph_chain = NULL;
624 struct strbuf line = STRBUF_INIT;
625 struct object_id *oids;
626 int i = 0, valid = 1, count;
627 FILE *fp = xfdopen(fd, "r");
629 count = st->st_size / (the_hash_algo->hexsz + 1);
630 CALLOC_ARRAY(oids, count);
632 prepare_alt_odb(r);
634 for (i = 0; i < count; i++) {
635 struct object_directory *odb;
637 if (strbuf_getline_lf(&line, fp) == EOF)
638 break;
640 if (get_oid_hex(line.buf, &oids[i])) {
641 warning(_("invalid commit-graph chain: line '%s' not a hash"),
642 line.buf);
643 valid = 0;
644 break;
647 valid = 0;
648 for (odb = r->objects->odb; odb; odb = odb->next) {
649 char *graph_name = get_split_graph_filename(odb, line.buf);
650 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
652 free(graph_name);
654 if (g) {
655 if (add_graph_to_chain(g, graph_chain, oids, i)) {
656 graph_chain = g;
657 valid = 1;
658 } else {
659 free_commit_graph(g);
662 break;
666 if (!valid) {
667 warning(_("unable to find all commit-graph files"));
668 break;
672 validate_mixed_generation_chain(graph_chain);
674 free(oids);
675 fclose(fp);
676 strbuf_release(&line);
678 *incomplete_chain = !valid;
679 return graph_chain;
682 static struct commit_graph *load_commit_graph_chain(struct repository *r,
683 struct object_directory *odb)
685 char *chain_file = get_commit_graph_chain_filename(odb);
686 struct stat st;
687 int fd;
688 struct commit_graph *g = NULL;
690 if (open_commit_graph_chain(chain_file, &fd, &st)) {
691 int incomplete;
692 /* ownership of fd is taken over by load function */
693 g = load_commit_graph_chain_fd_st(r, fd, &st, &incomplete);
696 free(chain_file);
697 return g;
700 struct commit_graph *read_commit_graph_one(struct repository *r,
701 struct object_directory *odb)
703 struct commit_graph *g = load_commit_graph_v1(r, odb);
705 if (!g)
706 g = load_commit_graph_chain(r, odb);
708 return g;
711 static void prepare_commit_graph_one(struct repository *r,
712 struct object_directory *odb)
715 if (r->objects->commit_graph)
716 return;
718 r->objects->commit_graph = read_commit_graph_one(r, odb);
722 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
724 * On the first invocation, this function attempts to load the commit
725 * graph if the_repository is configured to have one.
727 static int prepare_commit_graph(struct repository *r)
729 struct object_directory *odb;
732 * Early return if there is no git dir or if the commit graph is
733 * disabled.
735 * This must come before the "already attempted?" check below, because
736 * we want to disable even an already-loaded graph file.
738 if (!r->gitdir || r->commit_graph_disabled)
739 return 0;
741 if (r->objects->commit_graph_attempted)
742 return !!r->objects->commit_graph;
743 r->objects->commit_graph_attempted = 1;
745 prepare_repo_settings(r);
747 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
748 r->settings.core_commit_graph != 1)
750 * This repository is not configured to use commit graphs, so
751 * do not load one. (But report commit_graph_attempted anyway
752 * so that commit graph loading is not attempted again for this
753 * repository.)
755 return 0;
757 if (!commit_graph_compatible(r))
758 return 0;
760 prepare_alt_odb(r);
761 for (odb = r->objects->odb;
762 !r->objects->commit_graph && odb;
763 odb = odb->next)
764 prepare_commit_graph_one(r, odb);
765 return !!r->objects->commit_graph;
768 int generation_numbers_enabled(struct repository *r)
770 uint32_t first_generation;
771 struct commit_graph *g;
772 if (!prepare_commit_graph(r))
773 return 0;
775 g = r->objects->commit_graph;
777 if (!g->num_commits)
778 return 0;
780 first_generation = get_be32(g->chunk_commit_data +
781 g->hash_len + 8) >> 2;
783 return !!first_generation;
786 int corrected_commit_dates_enabled(struct repository *r)
788 struct commit_graph *g;
789 if (!prepare_commit_graph(r))
790 return 0;
792 g = r->objects->commit_graph;
794 if (!g->num_commits)
795 return 0;
797 return g->read_generation_data;
800 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
802 struct commit_graph *g = r->objects->commit_graph;
803 while (g) {
804 if (g->bloom_filter_settings)
805 return g->bloom_filter_settings;
806 g = g->base_graph;
808 return NULL;
811 void close_commit_graph(struct raw_object_store *o)
813 if (!o->commit_graph)
814 return;
816 clear_commit_graph_data_slab(&commit_graph_data_slab);
817 free_commit_graph(o->commit_graph);
818 o->commit_graph = NULL;
821 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
823 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
824 g->chunk_oid_lookup, g->hash_len, pos);
827 static void load_oid_from_graph(struct commit_graph *g,
828 uint32_t pos,
829 struct object_id *oid)
831 uint32_t lex_index;
833 while (g && pos < g->num_commits_in_base)
834 g = g->base_graph;
836 if (!g)
837 BUG("NULL commit-graph");
839 if (pos >= g->num_commits + g->num_commits_in_base)
840 die(_("invalid commit position. commit-graph is likely corrupt"));
842 lex_index = pos - g->num_commits_in_base;
844 oidread(oid, g->chunk_oid_lookup + st_mult(g->hash_len, lex_index),
845 the_repository->hash_algo);
848 static struct commit_list **insert_parent_or_die(struct repository *r,
849 struct commit_graph *g,
850 uint32_t pos,
851 struct commit_list **pptr)
853 struct commit *c;
854 struct object_id oid;
856 if (pos >= g->num_commits + g->num_commits_in_base)
857 die("invalid parent position %"PRIu32, pos);
859 load_oid_from_graph(g, pos, &oid);
860 c = lookup_commit(r, &oid);
861 if (!c)
862 die(_("could not find commit %s"), oid_to_hex(&oid));
863 commit_graph_data_at(c)->graph_pos = pos;
864 return &commit_list_insert(c, pptr)->next;
867 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
869 const unsigned char *commit_data;
870 struct commit_graph_data *graph_data;
871 uint32_t lex_index, offset_pos;
872 uint64_t date_high, date_low, offset;
874 while (pos < g->num_commits_in_base)
875 g = g->base_graph;
877 if (pos >= g->num_commits + g->num_commits_in_base)
878 die(_("invalid commit position. commit-graph is likely corrupt"));
880 lex_index = pos - g->num_commits_in_base;
881 commit_data = g->chunk_commit_data + st_mult(GRAPH_DATA_WIDTH, lex_index);
883 graph_data = commit_graph_data_at(item);
884 graph_data->graph_pos = pos;
886 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
887 date_low = get_be32(commit_data + g->hash_len + 12);
888 item->date = (timestamp_t)((date_high << 32) | date_low);
890 if (g->read_generation_data) {
891 offset = (timestamp_t)get_be32(g->chunk_generation_data + st_mult(sizeof(uint32_t), lex_index));
893 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
894 if (!g->chunk_generation_data_overflow)
895 die(_("commit-graph requires overflow generation data but has none"));
897 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
898 if (g->chunk_generation_data_overflow_size / sizeof(uint64_t) <= offset_pos)
899 die(_("commit-graph overflow generation data is too small"));
900 graph_data->generation = item->date +
901 get_be64(g->chunk_generation_data_overflow + sizeof(uint64_t) * offset_pos);
902 } else
903 graph_data->generation = item->date + offset;
904 } else
905 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
907 if (g->topo_levels)
908 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
911 static inline void set_commit_tree(struct commit *c, struct tree *t)
913 c->maybe_tree = t;
916 static int fill_commit_in_graph(struct repository *r,
917 struct commit *item,
918 struct commit_graph *g, uint32_t pos)
920 uint32_t edge_value;
921 uint32_t parent_data_pos;
922 struct commit_list **pptr;
923 const unsigned char *commit_data;
924 uint32_t lex_index;
926 while (pos < g->num_commits_in_base)
927 g = g->base_graph;
929 fill_commit_graph_info(item, g, pos);
931 lex_index = pos - g->num_commits_in_base;
932 commit_data = g->chunk_commit_data + st_mult(g->hash_len + 16, lex_index);
934 item->object.parsed = 1;
936 set_commit_tree(item, NULL);
938 pptr = &item->parents;
940 edge_value = get_be32(commit_data + g->hash_len);
941 if (edge_value == GRAPH_PARENT_NONE)
942 return 1;
943 pptr = insert_parent_or_die(r, g, edge_value, pptr);
945 edge_value = get_be32(commit_data + g->hash_len + 4);
946 if (edge_value == GRAPH_PARENT_NONE)
947 return 1;
948 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
949 pptr = insert_parent_or_die(r, g, edge_value, pptr);
950 return 1;
953 parent_data_pos = edge_value & GRAPH_EDGE_LAST_MASK;
954 do {
955 if (g->chunk_extra_edges_size / sizeof(uint32_t) <= parent_data_pos) {
956 error(_("commit-graph extra-edges pointer out of bounds"));
957 free_commit_list(item->parents);
958 item->parents = NULL;
959 item->object.parsed = 0;
960 return 0;
962 edge_value = get_be32(g->chunk_extra_edges +
963 sizeof(uint32_t) * parent_data_pos);
964 pptr = insert_parent_or_die(r, g,
965 edge_value & GRAPH_EDGE_LAST_MASK,
966 pptr);
967 parent_data_pos++;
968 } while (!(edge_value & GRAPH_LAST_EDGE));
970 return 1;
973 static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
975 struct commit_graph *cur_g = g;
976 uint32_t lex_index;
978 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
979 cur_g = cur_g->base_graph;
981 if (cur_g) {
982 *pos = lex_index + cur_g->num_commits_in_base;
983 return 1;
986 return 0;
989 static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
991 uint32_t graph_pos = commit_graph_position(item);
992 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
993 *pos = graph_pos;
994 return 1;
995 } else {
996 return search_commit_pos_in_graph(&item->object.oid, g, pos);
1000 int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
1001 uint32_t *pos)
1003 if (!prepare_commit_graph(r))
1004 return 0;
1005 return find_commit_pos_in_graph(c, r->objects->commit_graph, pos);
1008 struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
1010 static int commit_graph_paranoia = -1;
1011 struct commit *commit;
1012 uint32_t pos;
1014 if (commit_graph_paranoia == -1)
1015 commit_graph_paranoia = git_env_bool(GIT_COMMIT_GRAPH_PARANOIA, 0);
1017 if (!prepare_commit_graph(repo))
1018 return NULL;
1019 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
1020 return NULL;
1021 if (commit_graph_paranoia && !has_object(repo, id, 0))
1022 return NULL;
1024 commit = lookup_commit(repo, id);
1025 if (!commit)
1026 return NULL;
1027 if (commit->object.parsed)
1028 return commit;
1030 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
1031 return NULL;
1033 return commit;
1036 static int parse_commit_in_graph_one(struct repository *r,
1037 struct commit_graph *g,
1038 struct commit *item)
1040 uint32_t pos;
1042 if (item->object.parsed)
1043 return 1;
1045 if (find_commit_pos_in_graph(item, g, &pos))
1046 return fill_commit_in_graph(r, item, g, pos);
1048 return 0;
1051 int parse_commit_in_graph(struct repository *r, struct commit *item)
1053 static int checked_env = 0;
1055 if (!checked_env &&
1056 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
1057 die("dying as requested by the '%s' variable on commit-graph parse!",
1058 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
1059 checked_env = 1;
1061 if (!prepare_commit_graph(r))
1062 return 0;
1063 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
1066 void load_commit_graph_info(struct repository *r, struct commit *item)
1068 uint32_t pos;
1069 if (repo_find_commit_pos_in_graph(r, item, &pos))
1070 fill_commit_graph_info(item, r->objects->commit_graph, pos);
1073 static struct tree *load_tree_for_commit(struct repository *r,
1074 struct commit_graph *g,
1075 struct commit *c)
1077 struct object_id oid;
1078 const unsigned char *commit_data;
1079 uint32_t graph_pos = commit_graph_position(c);
1081 while (graph_pos < g->num_commits_in_base)
1082 g = g->base_graph;
1084 commit_data = g->chunk_commit_data +
1085 st_mult(GRAPH_DATA_WIDTH, graph_pos - g->num_commits_in_base);
1087 oidread(&oid, commit_data, the_repository->hash_algo);
1088 set_commit_tree(c, lookup_tree(r, &oid));
1090 return c->maybe_tree;
1093 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
1094 struct commit_graph *g,
1095 const struct commit *c)
1097 if (c->maybe_tree)
1098 return c->maybe_tree;
1099 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
1100 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
1102 return load_tree_for_commit(r, g, (struct commit *)c);
1105 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
1107 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
1110 struct packed_commit_list {
1111 struct commit **list;
1112 size_t nr;
1113 size_t alloc;
1116 struct write_commit_graph_context {
1117 struct repository *r;
1118 struct object_directory *odb;
1119 char *graph_name;
1120 struct oid_array oids;
1121 struct packed_commit_list commits;
1122 int num_extra_edges;
1123 int num_generation_data_overflows;
1124 unsigned long approx_nr_objects;
1125 struct progress *progress;
1126 int progress_done;
1127 uint64_t progress_cnt;
1129 char *base_graph_name;
1130 int num_commit_graphs_before;
1131 int num_commit_graphs_after;
1132 char **commit_graph_filenames_before;
1133 char **commit_graph_filenames_after;
1134 char **commit_graph_hash_after;
1135 uint32_t new_num_commits_in_base;
1136 struct commit_graph *new_base_graph;
1138 unsigned append:1,
1139 report_progress:1,
1140 split:1,
1141 changed_paths:1,
1142 order_by_pack:1,
1143 write_generation_data:1,
1144 trust_generation_numbers:1;
1146 struct topo_level_slab *topo_levels;
1147 const struct commit_graph_opts *opts;
1148 size_t total_bloom_filter_data_size;
1149 const struct bloom_filter_settings *bloom_settings;
1151 int count_bloom_filter_computed;
1152 int count_bloom_filter_not_computed;
1153 int count_bloom_filter_trunc_empty;
1154 int count_bloom_filter_trunc_large;
1157 static int write_graph_chunk_fanout(struct hashfile *f,
1158 void *data)
1160 struct write_commit_graph_context *ctx = data;
1161 int i, count = 0;
1162 struct commit **list = ctx->commits.list;
1165 * Write the first-level table (the list is sorted,
1166 * but we use a 256-entry lookup to be able to avoid
1167 * having to do eight extra binary search iterations).
1169 for (i = 0; i < 256; i++) {
1170 while (count < ctx->commits.nr) {
1171 if ((*list)->object.oid.hash[0] != i)
1172 break;
1173 display_progress(ctx->progress, ++ctx->progress_cnt);
1174 count++;
1175 list++;
1178 hashwrite_be32(f, count);
1181 return 0;
1184 static int write_graph_chunk_oids(struct hashfile *f,
1185 void *data)
1187 struct write_commit_graph_context *ctx = data;
1188 struct commit **list = ctx->commits.list;
1189 int count;
1190 for (count = 0; count < ctx->commits.nr; count++, list++) {
1191 display_progress(ctx->progress, ++ctx->progress_cnt);
1192 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1195 return 0;
1198 static const struct object_id *commit_to_oid(size_t index, const void *table)
1200 const struct commit * const *commits = table;
1201 return &commits[index]->object.oid;
1204 static int write_graph_chunk_data(struct hashfile *f,
1205 void *data)
1207 struct write_commit_graph_context *ctx = data;
1208 struct commit **list = ctx->commits.list;
1209 struct commit **last = ctx->commits.list + ctx->commits.nr;
1210 uint32_t num_extra_edges = 0;
1212 while (list < last) {
1213 struct commit_list *parent;
1214 struct object_id *tree;
1215 int edge_value;
1216 uint32_t packedDate[2];
1217 display_progress(ctx->progress, ++ctx->progress_cnt);
1219 if (repo_parse_commit_no_graph(ctx->r, *list))
1220 die(_("unable to parse commit %s"),
1221 oid_to_hex(&(*list)->object.oid));
1222 tree = get_commit_tree_oid(*list);
1223 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1225 parent = (*list)->parents;
1227 if (!parent)
1228 edge_value = GRAPH_PARENT_NONE;
1229 else {
1230 edge_value = oid_pos(&parent->item->object.oid,
1231 ctx->commits.list,
1232 ctx->commits.nr,
1233 commit_to_oid);
1235 if (edge_value >= 0)
1236 edge_value += ctx->new_num_commits_in_base;
1237 else if (ctx->new_base_graph) {
1238 uint32_t pos;
1239 if (find_commit_pos_in_graph(parent->item,
1240 ctx->new_base_graph,
1241 &pos))
1242 edge_value = pos;
1245 if (edge_value < 0)
1246 BUG("missing parent %s for commit %s",
1247 oid_to_hex(&parent->item->object.oid),
1248 oid_to_hex(&(*list)->object.oid));
1251 hashwrite_be32(f, edge_value);
1253 if (parent)
1254 parent = parent->next;
1256 if (!parent)
1257 edge_value = GRAPH_PARENT_NONE;
1258 else if (parent->next)
1259 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1260 else {
1261 edge_value = oid_pos(&parent->item->object.oid,
1262 ctx->commits.list,
1263 ctx->commits.nr,
1264 commit_to_oid);
1266 if (edge_value >= 0)
1267 edge_value += ctx->new_num_commits_in_base;
1268 else if (ctx->new_base_graph) {
1269 uint32_t pos;
1270 if (find_commit_pos_in_graph(parent->item,
1271 ctx->new_base_graph,
1272 &pos))
1273 edge_value = pos;
1276 if (edge_value < 0)
1277 BUG("missing parent %s for commit %s",
1278 oid_to_hex(&parent->item->object.oid),
1279 oid_to_hex(&(*list)->object.oid));
1282 hashwrite_be32(f, edge_value);
1284 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1285 do {
1286 num_extra_edges++;
1287 parent = parent->next;
1288 } while (parent);
1291 if (sizeof((*list)->date) > 4)
1292 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1293 else
1294 packedDate[0] = 0;
1296 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1298 packedDate[1] = htonl((*list)->date);
1299 hashwrite(f, packedDate, 8);
1301 list++;
1304 return 0;
1307 static int write_graph_chunk_generation_data(struct hashfile *f,
1308 void *data)
1310 struct write_commit_graph_context *ctx = data;
1311 int i, num_generation_data_overflows = 0;
1313 for (i = 0; i < ctx->commits.nr; i++) {
1314 struct commit *c = ctx->commits.list[i];
1315 timestamp_t offset;
1316 repo_parse_commit(ctx->r, c);
1317 offset = commit_graph_data_at(c)->generation - c->date;
1318 display_progress(ctx->progress, ++ctx->progress_cnt);
1320 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1321 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1322 num_generation_data_overflows++;
1325 hashwrite_be32(f, offset);
1328 return 0;
1331 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1332 void *data)
1334 struct write_commit_graph_context *ctx = data;
1335 int i;
1336 for (i = 0; i < ctx->commits.nr; i++) {
1337 struct commit *c = ctx->commits.list[i];
1338 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1339 display_progress(ctx->progress, ++ctx->progress_cnt);
1341 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1342 hashwrite_be32(f, offset >> 32);
1343 hashwrite_be32(f, (uint32_t) offset);
1347 return 0;
1350 static int write_graph_chunk_extra_edges(struct hashfile *f,
1351 void *data)
1353 struct write_commit_graph_context *ctx = data;
1354 struct commit **list = ctx->commits.list;
1355 struct commit **last = ctx->commits.list + ctx->commits.nr;
1356 struct commit_list *parent;
1358 while (list < last) {
1359 int num_parents = 0;
1361 display_progress(ctx->progress, ++ctx->progress_cnt);
1363 for (parent = (*list)->parents; num_parents < 3 && parent;
1364 parent = parent->next)
1365 num_parents++;
1367 if (num_parents <= 2) {
1368 list++;
1369 continue;
1372 /* Since num_parents > 2, this initializer is safe. */
1373 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1374 int edge_value = oid_pos(&parent->item->object.oid,
1375 ctx->commits.list,
1376 ctx->commits.nr,
1377 commit_to_oid);
1379 if (edge_value >= 0)
1380 edge_value += ctx->new_num_commits_in_base;
1381 else if (ctx->new_base_graph) {
1382 uint32_t pos;
1383 if (find_commit_pos_in_graph(parent->item,
1384 ctx->new_base_graph,
1385 &pos))
1386 edge_value = pos;
1389 if (edge_value < 0)
1390 BUG("missing parent %s for commit %s",
1391 oid_to_hex(&parent->item->object.oid),
1392 oid_to_hex(&(*list)->object.oid));
1393 else if (!parent->next)
1394 edge_value |= GRAPH_LAST_EDGE;
1396 hashwrite_be32(f, edge_value);
1399 list++;
1402 return 0;
1405 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1406 void *data)
1408 struct write_commit_graph_context *ctx = data;
1409 struct commit **list = ctx->commits.list;
1410 struct commit **last = ctx->commits.list + ctx->commits.nr;
1411 uint32_t cur_pos = 0;
1413 while (list < last) {
1414 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1415 size_t len = filter ? filter->len : 0;
1416 cur_pos += len;
1417 display_progress(ctx->progress, ++ctx->progress_cnt);
1418 hashwrite_be32(f, cur_pos);
1419 list++;
1422 return 0;
1425 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1427 struct json_writer jw = JSON_WRITER_INIT;
1429 jw_object_begin(&jw, 0);
1430 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1431 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1432 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1433 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1434 jw_end(&jw);
1436 trace2_data_json("bloom", ctx->r, "settings", &jw);
1438 jw_release(&jw);
1441 static int write_graph_chunk_bloom_data(struct hashfile *f,
1442 void *data)
1444 struct write_commit_graph_context *ctx = data;
1445 struct commit **list = ctx->commits.list;
1446 struct commit **last = ctx->commits.list + ctx->commits.nr;
1448 trace2_bloom_filter_settings(ctx);
1450 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1451 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1452 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1454 while (list < last) {
1455 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1456 size_t len = filter ? filter->len : 0;
1458 display_progress(ctx->progress, ++ctx->progress_cnt);
1459 if (len)
1460 hashwrite(f, filter->data, len * sizeof(unsigned char));
1461 list++;
1464 return 0;
1467 static int add_packed_commits(const struct object_id *oid,
1468 struct packed_git *pack,
1469 uint32_t pos,
1470 void *data)
1472 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1473 enum object_type type;
1474 off_t offset = nth_packed_object_offset(pack, pos);
1475 struct object_info oi = OBJECT_INFO_INIT;
1477 if (ctx->progress)
1478 display_progress(ctx->progress, ++ctx->progress_done);
1480 oi.typep = &type;
1481 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1482 die(_("unable to get type of object %s"), oid_to_hex(oid));
1484 if (type != OBJ_COMMIT)
1485 return 0;
1487 oid_array_append(&ctx->oids, oid);
1488 set_commit_pos(ctx->r, oid);
1490 return 0;
1493 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1495 struct commit_list *parent;
1496 for (parent = commit->parents; parent; parent = parent->next) {
1497 if (!(parent->item->object.flags & REACHABLE)) {
1498 oid_array_append(&ctx->oids, &parent->item->object.oid);
1499 parent->item->object.flags |= REACHABLE;
1504 static void close_reachable(struct write_commit_graph_context *ctx)
1506 int i;
1507 struct commit *commit;
1508 enum commit_graph_split_flags flags = ctx->opts ?
1509 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1511 if (ctx->report_progress)
1512 ctx->progress = start_delayed_progress(
1513 _("Loading known commits in commit graph"),
1514 ctx->oids.nr);
1515 for (i = 0; i < ctx->oids.nr; i++) {
1516 display_progress(ctx->progress, i + 1);
1517 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1518 if (commit)
1519 commit->object.flags |= REACHABLE;
1521 stop_progress(&ctx->progress);
1524 * As this loop runs, ctx->oids.nr may grow, but not more
1525 * than the number of missing commits in the reachable
1526 * closure.
1528 if (ctx->report_progress)
1529 ctx->progress = start_delayed_progress(
1530 _("Expanding reachable commits in commit graph"),
1532 for (i = 0; i < ctx->oids.nr; i++) {
1533 display_progress(ctx->progress, i + 1);
1534 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1536 if (!commit)
1537 continue;
1538 if (ctx->split) {
1539 if ((!repo_parse_commit(ctx->r, commit) &&
1540 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1541 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1542 add_missing_parents(ctx, commit);
1543 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1544 add_missing_parents(ctx, commit);
1546 stop_progress(&ctx->progress);
1548 if (ctx->report_progress)
1549 ctx->progress = start_delayed_progress(
1550 _("Clearing commit marks in commit graph"),
1551 ctx->oids.nr);
1552 for (i = 0; i < ctx->oids.nr; i++) {
1553 display_progress(ctx->progress, i + 1);
1554 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1556 if (commit)
1557 commit->object.flags &= ~REACHABLE;
1559 stop_progress(&ctx->progress);
1562 struct compute_generation_info {
1563 struct repository *r;
1564 struct packed_commit_list *commits;
1565 struct progress *progress;
1566 int progress_cnt;
1568 timestamp_t (*get_generation)(struct commit *c, void *data);
1569 void (*set_generation)(struct commit *c, timestamp_t gen, void *data);
1570 void *data;
1573 static timestamp_t compute_generation_from_max(struct commit *c,
1574 timestamp_t max_gen,
1575 int generation_version)
1577 switch (generation_version) {
1578 case 1: /* topological levels */
1579 if (max_gen > GENERATION_NUMBER_V1_MAX - 1)
1580 max_gen = GENERATION_NUMBER_V1_MAX - 1;
1581 return max_gen + 1;
1583 case 2: /* corrected commit date */
1584 if (c->date && c->date > max_gen)
1585 max_gen = c->date - 1;
1586 return max_gen + 1;
1588 default:
1589 BUG("attempting unimplemented version");
1593 static void compute_reachable_generation_numbers(
1594 struct compute_generation_info *info,
1595 int generation_version)
1597 int i;
1598 struct commit_list *list = NULL;
1600 for (i = 0; i < info->commits->nr; i++) {
1601 struct commit *c = info->commits->list[i];
1602 timestamp_t gen;
1603 repo_parse_commit(info->r, c);
1604 gen = info->get_generation(c, info->data);
1605 display_progress(info->progress, ++info->progress_cnt);
1607 if (gen != GENERATION_NUMBER_ZERO && gen != GENERATION_NUMBER_INFINITY)
1608 continue;
1610 commit_list_insert(c, &list);
1611 while (list) {
1612 struct commit *current = list->item;
1613 struct commit_list *parent;
1614 int all_parents_computed = 1;
1615 uint32_t max_gen = 0;
1617 for (parent = current->parents; parent; parent = parent->next) {
1618 repo_parse_commit(info->r, parent->item);
1619 gen = info->get_generation(parent->item, info->data);
1621 if (gen == GENERATION_NUMBER_ZERO) {
1622 all_parents_computed = 0;
1623 commit_list_insert(parent->item, &list);
1624 break;
1627 if (gen > max_gen)
1628 max_gen = gen;
1631 if (all_parents_computed) {
1632 pop_commit(&list);
1633 gen = compute_generation_from_max(
1634 current, max_gen,
1635 generation_version);
1636 info->set_generation(current, gen, info->data);
1642 static timestamp_t get_topo_level(struct commit *c, void *data)
1644 struct write_commit_graph_context *ctx = data;
1645 return *topo_level_slab_at(ctx->topo_levels, c);
1648 static void set_topo_level(struct commit *c, timestamp_t t, void *data)
1650 struct write_commit_graph_context *ctx = data;
1651 *topo_level_slab_at(ctx->topo_levels, c) = (uint32_t)t;
1654 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1656 struct compute_generation_info info = {
1657 .r = ctx->r,
1658 .commits = &ctx->commits,
1659 .get_generation = get_topo_level,
1660 .set_generation = set_topo_level,
1661 .data = ctx,
1664 if (ctx->report_progress)
1665 info.progress = ctx->progress
1666 = start_delayed_progress(
1667 _("Computing commit graph topological levels"),
1668 ctx->commits.nr);
1670 compute_reachable_generation_numbers(&info, 1);
1672 stop_progress(&ctx->progress);
1675 static timestamp_t get_generation_from_graph_data(struct commit *c,
1676 void *data UNUSED)
1678 return commit_graph_data_at(c)->generation;
1681 static void set_generation_v2(struct commit *c, timestamp_t t,
1682 void *data UNUSED)
1684 struct commit_graph_data *g = commit_graph_data_at(c);
1685 g->generation = t;
1688 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1690 int i;
1691 struct compute_generation_info info = {
1692 .r = ctx->r,
1693 .commits = &ctx->commits,
1694 .get_generation = get_generation_from_graph_data,
1695 .set_generation = set_generation_v2,
1698 if (ctx->report_progress)
1699 info.progress = ctx->progress
1700 = start_delayed_progress(
1701 _("Computing commit graph generation numbers"),
1702 ctx->commits.nr);
1704 if (!ctx->trust_generation_numbers) {
1705 for (i = 0; i < ctx->commits.nr; i++) {
1706 struct commit *c = ctx->commits.list[i];
1707 repo_parse_commit(ctx->r, c);
1708 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1712 compute_reachable_generation_numbers(&info, 2);
1714 for (i = 0; i < ctx->commits.nr; i++) {
1715 struct commit *c = ctx->commits.list[i];
1716 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1717 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1718 ctx->num_generation_data_overflows++;
1720 stop_progress(&ctx->progress);
1723 static void set_generation_in_graph_data(struct commit *c, timestamp_t t,
1724 void *data UNUSED)
1726 commit_graph_data_at(c)->generation = t;
1730 * After this method, all commits reachable from those in the given
1731 * list will have non-zero, non-infinite generation numbers.
1733 void ensure_generations_valid(struct repository *r,
1734 struct commit **commits, size_t nr)
1736 int generation_version = get_configured_generation_version(r);
1737 struct packed_commit_list list = {
1738 .list = commits,
1739 .alloc = nr,
1740 .nr = nr,
1742 struct compute_generation_info info = {
1743 .r = r,
1744 .commits = &list,
1745 .get_generation = get_generation_from_graph_data,
1746 .set_generation = set_generation_in_graph_data,
1749 compute_reachable_generation_numbers(&info, generation_version);
1752 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1754 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1755 ctx->count_bloom_filter_computed);
1756 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1757 ctx->count_bloom_filter_not_computed);
1758 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1759 ctx->count_bloom_filter_trunc_empty);
1760 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1761 ctx->count_bloom_filter_trunc_large);
1764 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1766 int i;
1767 struct progress *progress = NULL;
1768 struct commit **sorted_commits;
1769 int max_new_filters;
1771 init_bloom_filters();
1773 if (ctx->report_progress)
1774 progress = start_delayed_progress(
1775 _("Computing commit changed paths Bloom filters"),
1776 ctx->commits.nr);
1778 DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1780 if (ctx->order_by_pack)
1781 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1782 else
1783 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1785 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1786 ctx->opts->max_new_filters : ctx->commits.nr;
1788 for (i = 0; i < ctx->commits.nr; i++) {
1789 enum bloom_filter_computed computed = 0;
1790 struct commit *c = sorted_commits[i];
1791 struct bloom_filter *filter = get_or_compute_bloom_filter(
1792 ctx->r,
1794 ctx->count_bloom_filter_computed < max_new_filters,
1795 ctx->bloom_settings,
1796 &computed);
1797 if (computed & BLOOM_COMPUTED) {
1798 ctx->count_bloom_filter_computed++;
1799 if (computed & BLOOM_TRUNC_EMPTY)
1800 ctx->count_bloom_filter_trunc_empty++;
1801 if (computed & BLOOM_TRUNC_LARGE)
1802 ctx->count_bloom_filter_trunc_large++;
1803 } else if (computed & BLOOM_NOT_COMPUTED)
1804 ctx->count_bloom_filter_not_computed++;
1805 ctx->total_bloom_filter_data_size += filter
1806 ? sizeof(unsigned char) * filter->len : 0;
1807 display_progress(progress, i + 1);
1810 if (trace2_is_enabled())
1811 trace2_bloom_filter_write_statistics(ctx);
1813 free(sorted_commits);
1814 stop_progress(&progress);
1817 struct refs_cb_data {
1818 struct oidset *commits;
1819 struct progress *progress;
1822 static int add_ref_to_set(const char *refname UNUSED,
1823 const struct object_id *oid,
1824 int flags UNUSED, void *cb_data)
1826 struct object_id peeled;
1827 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1829 if (!peel_iterated_oid(the_repository, oid, &peeled))
1830 oid = &peeled;
1831 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1832 oidset_insert(data->commits, oid);
1834 display_progress(data->progress, oidset_size(data->commits));
1836 return 0;
1839 int write_commit_graph_reachable(struct object_directory *odb,
1840 enum commit_graph_write_flags flags,
1841 const struct commit_graph_opts *opts)
1843 struct oidset commits = OIDSET_INIT;
1844 struct refs_cb_data data;
1845 int result;
1847 memset(&data, 0, sizeof(data));
1848 data.commits = &commits;
1849 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1850 data.progress = start_delayed_progress(
1851 _("Collecting referenced commits"), 0);
1853 refs_for_each_ref(get_main_ref_store(the_repository), add_ref_to_set,
1854 &data);
1856 stop_progress(&data.progress);
1858 result = write_commit_graph(odb, NULL, &commits,
1859 flags, opts);
1861 oidset_clear(&commits);
1862 return result;
1865 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1866 const struct string_list *pack_indexes)
1868 uint32_t i;
1869 struct strbuf progress_title = STRBUF_INIT;
1870 struct strbuf packname = STRBUF_INIT;
1871 int dirlen;
1872 int ret = 0;
1874 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1875 dirlen = packname.len;
1876 if (ctx->report_progress) {
1877 strbuf_addf(&progress_title,
1878 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1879 "Finding commits for commit graph in %"PRIuMAX" packs",
1880 pack_indexes->nr),
1881 (uintmax_t)pack_indexes->nr);
1882 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1883 ctx->progress_done = 0;
1885 for (i = 0; i < pack_indexes->nr; i++) {
1886 struct packed_git *p;
1887 strbuf_setlen(&packname, dirlen);
1888 strbuf_addstr(&packname, pack_indexes->items[i].string);
1889 p = add_packed_git(packname.buf, packname.len, 1);
1890 if (!p) {
1891 ret = error(_("error adding pack %s"), packname.buf);
1892 goto cleanup;
1894 if (open_pack_index(p)) {
1895 ret = error(_("error opening index for %s"), packname.buf);
1896 goto cleanup;
1898 for_each_object_in_pack(p, add_packed_commits, ctx,
1899 FOR_EACH_OBJECT_PACK_ORDER);
1900 close_pack(p);
1901 free(p);
1904 cleanup:
1905 stop_progress(&ctx->progress);
1906 strbuf_release(&progress_title);
1907 strbuf_release(&packname);
1909 return ret;
1912 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1913 struct oidset *commits)
1915 struct oidset_iter iter;
1916 struct object_id *oid;
1918 if (!oidset_size(commits))
1919 return 0;
1921 oidset_iter_init(commits, &iter);
1922 while ((oid = oidset_iter_next(&iter))) {
1923 oid_array_append(&ctx->oids, oid);
1926 return 0;
1929 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1931 if (ctx->report_progress)
1932 ctx->progress = start_delayed_progress(
1933 _("Finding commits for commit graph among packed objects"),
1934 ctx->approx_nr_objects);
1935 for_each_packed_object(add_packed_commits, ctx,
1936 FOR_EACH_OBJECT_PACK_ORDER);
1937 if (ctx->progress_done < ctx->approx_nr_objects)
1938 display_progress(ctx->progress, ctx->approx_nr_objects);
1939 stop_progress(&ctx->progress);
1942 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1944 uint32_t i;
1945 enum commit_graph_split_flags flags = ctx->opts ?
1946 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1948 ctx->num_extra_edges = 0;
1949 if (ctx->report_progress)
1950 ctx->progress = start_delayed_progress(
1951 _("Finding extra edges in commit graph"),
1952 ctx->oids.nr);
1953 oid_array_sort(&ctx->oids);
1954 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1955 unsigned int num_parents;
1957 display_progress(ctx->progress, i + 1);
1959 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1960 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1962 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1963 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1964 continue;
1966 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1967 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1968 else
1969 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1971 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1972 if (num_parents > 2)
1973 ctx->num_extra_edges += num_parents - 1;
1975 ctx->commits.nr++;
1977 stop_progress(&ctx->progress);
1980 static int write_graph_chunk_base_1(struct hashfile *f,
1981 struct commit_graph *g)
1983 int num = 0;
1985 if (!g)
1986 return 0;
1988 num = write_graph_chunk_base_1(f, g->base_graph);
1989 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1990 return num + 1;
1993 static int write_graph_chunk_base(struct hashfile *f,
1994 void *data)
1996 struct write_commit_graph_context *ctx = data;
1997 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1999 if (num != ctx->num_commit_graphs_after - 1) {
2000 error(_("failed to write correct number of base graph ids"));
2001 return -1;
2004 return 0;
2007 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
2009 uint32_t i;
2010 struct hashfile *f;
2011 struct tempfile *graph_layer; /* when ctx->split is non-zero */
2012 struct lock_file lk = LOCK_INIT;
2013 const unsigned hashsz = the_hash_algo->rawsz;
2014 struct strbuf progress_title = STRBUF_INIT;
2015 struct chunkfile *cf;
2016 unsigned char file_hash[GIT_MAX_RAWSZ];
2018 if (ctx->split) {
2019 struct strbuf tmp_file = STRBUF_INIT;
2021 strbuf_addf(&tmp_file,
2022 "%s/info/commit-graphs/tmp_graph_XXXXXX",
2023 ctx->odb->path);
2024 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
2025 } else {
2026 ctx->graph_name = get_commit_graph_filename(ctx->odb);
2029 if (safe_create_leading_directories(ctx->graph_name)) {
2030 UNLEAK(ctx->graph_name);
2031 error(_("unable to create leading directories of %s"),
2032 ctx->graph_name);
2033 return -1;
2036 if (ctx->split) {
2037 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
2039 hold_lock_file_for_update_mode(&lk, lock_name,
2040 LOCK_DIE_ON_ERROR, 0444);
2041 free(lock_name);
2043 graph_layer = mks_tempfile_m(ctx->graph_name, 0444);
2044 if (!graph_layer) {
2045 error(_("unable to create temporary graph layer"));
2046 return -1;
2049 if (adjust_shared_perm(get_tempfile_path(graph_layer))) {
2050 error(_("unable to adjust shared permissions for '%s'"),
2051 get_tempfile_path(graph_layer));
2052 return -1;
2055 f = hashfd(get_tempfile_fd(graph_layer), get_tempfile_path(graph_layer));
2056 } else {
2057 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
2058 LOCK_DIE_ON_ERROR, 0444);
2059 f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk));
2062 cf = init_chunkfile(f);
2064 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
2065 write_graph_chunk_fanout);
2066 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, st_mult(hashsz, ctx->commits.nr),
2067 write_graph_chunk_oids);
2068 add_chunk(cf, GRAPH_CHUNKID_DATA, st_mult(hashsz + 16, ctx->commits.nr),
2069 write_graph_chunk_data);
2071 if (ctx->write_generation_data)
2072 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
2073 st_mult(sizeof(uint32_t), ctx->commits.nr),
2074 write_graph_chunk_generation_data);
2075 if (ctx->num_generation_data_overflows)
2076 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
2077 st_mult(sizeof(timestamp_t), ctx->num_generation_data_overflows),
2078 write_graph_chunk_generation_data_overflow);
2079 if (ctx->num_extra_edges)
2080 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
2081 st_mult(4, ctx->num_extra_edges),
2082 write_graph_chunk_extra_edges);
2083 if (ctx->changed_paths) {
2084 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
2085 st_mult(sizeof(uint32_t), ctx->commits.nr),
2086 write_graph_chunk_bloom_indexes);
2087 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
2088 st_add(sizeof(uint32_t) * 3,
2089 ctx->total_bloom_filter_data_size),
2090 write_graph_chunk_bloom_data);
2092 if (ctx->num_commit_graphs_after > 1)
2093 add_chunk(cf, GRAPH_CHUNKID_BASE,
2094 st_mult(hashsz, ctx->num_commit_graphs_after - 1),
2095 write_graph_chunk_base);
2097 hashwrite_be32(f, GRAPH_SIGNATURE);
2099 hashwrite_u8(f, GRAPH_VERSION);
2100 hashwrite_u8(f, oid_version(the_hash_algo));
2101 hashwrite_u8(f, get_num_chunks(cf));
2102 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
2104 if (ctx->report_progress) {
2105 strbuf_addf(&progress_title,
2106 Q_("Writing out commit graph in %d pass",
2107 "Writing out commit graph in %d passes",
2108 get_num_chunks(cf)),
2109 get_num_chunks(cf));
2110 ctx->progress = start_delayed_progress(
2111 progress_title.buf,
2112 st_mult(get_num_chunks(cf), ctx->commits.nr));
2115 write_chunkfile(cf, ctx);
2117 stop_progress(&ctx->progress);
2118 strbuf_release(&progress_title);
2120 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
2121 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
2122 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
2124 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
2125 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
2126 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
2127 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
2130 close_commit_graph(ctx->r->objects);
2131 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
2132 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
2133 free_chunkfile(cf);
2135 if (ctx->split) {
2136 FILE *chainf = fdopen_lock_file(&lk, "w");
2137 char *final_graph_name;
2138 int result;
2140 if (!chainf) {
2141 error(_("unable to open commit-graph chain file"));
2142 return -1;
2145 if (ctx->base_graph_name) {
2146 const char *dest;
2147 int idx = ctx->num_commit_graphs_after - 1;
2148 if (ctx->num_commit_graphs_after > 1)
2149 idx--;
2151 dest = ctx->commit_graph_filenames_after[idx];
2153 if (strcmp(ctx->base_graph_name, dest)) {
2154 result = rename(ctx->base_graph_name, dest);
2156 if (result) {
2157 error(_("failed to rename base commit-graph file"));
2158 return -1;
2161 } else {
2162 char *graph_name = get_commit_graph_filename(ctx->odb);
2163 unlink(graph_name);
2164 free(graph_name);
2167 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2168 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
2169 final_graph_name = get_split_graph_filename(ctx->odb,
2170 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2171 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1]);
2172 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
2174 result = rename_tempfile(&graph_layer, final_graph_name);
2176 for (i = 0; i < ctx->num_commit_graphs_after; i++)
2177 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2179 if (result) {
2180 error(_("failed to rename temporary commit-graph file"));
2181 return -1;
2185 commit_lock_file(&lk);
2187 return 0;
2190 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2192 struct commit_graph *g;
2193 uint32_t num_commits;
2194 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2195 uint32_t i;
2197 int max_commits = 0;
2198 int size_mult = 2;
2200 if (ctx->opts) {
2201 max_commits = ctx->opts->max_commits;
2203 if (ctx->opts->size_multiple)
2204 size_mult = ctx->opts->size_multiple;
2206 flags = ctx->opts->split_flags;
2209 g = ctx->r->objects->commit_graph;
2210 num_commits = ctx->commits.nr;
2211 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2212 ctx->num_commit_graphs_after = 1;
2213 else
2214 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2216 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2217 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2218 while (g && (g->num_commits <= st_mult(size_mult, num_commits) ||
2219 (max_commits && num_commits > max_commits))) {
2220 if (g->odb != ctx->odb)
2221 break;
2223 if (unsigned_add_overflows(num_commits, g->num_commits))
2224 die(_("cannot merge graphs with %"PRIuMAX", "
2225 "%"PRIuMAX" commits"),
2226 (uintmax_t)num_commits,
2227 (uintmax_t)g->num_commits);
2228 num_commits += g->num_commits;
2229 g = g->base_graph;
2231 ctx->num_commit_graphs_after--;
2235 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2236 ctx->new_base_graph = g;
2237 else if (ctx->num_commit_graphs_after != 1)
2238 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2239 "should be 1 with --split=replace");
2241 if (ctx->num_commit_graphs_after == 2) {
2242 char *old_graph_name = get_commit_graph_filename(g->odb);
2244 if (!strcmp(g->filename, old_graph_name) &&
2245 g->odb != ctx->odb) {
2246 ctx->num_commit_graphs_after = 1;
2247 ctx->new_base_graph = NULL;
2250 free(old_graph_name);
2253 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2254 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2256 for (i = 0; i < ctx->num_commit_graphs_after &&
2257 i < ctx->num_commit_graphs_before; i++)
2258 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2260 i = ctx->num_commit_graphs_before - 1;
2261 g = ctx->r->objects->commit_graph;
2263 while (g) {
2264 if (i < ctx->num_commit_graphs_after)
2265 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2268 * If the topmost remaining layer has generation data chunk, the
2269 * resultant layer also has generation data chunk.
2271 if (i == ctx->num_commit_graphs_after - 2)
2272 ctx->write_generation_data = !!g->chunk_generation_data;
2274 i--;
2275 g = g->base_graph;
2279 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2280 struct commit_graph *g)
2282 uint32_t i;
2283 uint32_t offset = g->num_commits_in_base;
2285 if (unsigned_add_overflows(ctx->commits.nr, g->num_commits))
2286 die(_("cannot merge graph %s, too many commits: %"PRIuMAX),
2287 oid_to_hex(&g->oid),
2288 (uintmax_t)st_add(ctx->commits.nr, g->num_commits));
2290 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2292 for (i = 0; i < g->num_commits; i++) {
2293 struct object_id oid;
2294 struct commit *result;
2296 display_progress(ctx->progress, i + 1);
2298 load_oid_from_graph(g, i + offset, &oid);
2300 /* only add commits if they still exist in the repo */
2301 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2303 if (result) {
2304 ctx->commits.list[ctx->commits.nr] = result;
2305 ctx->commits.nr++;
2310 static int commit_compare(const void *_a, const void *_b)
2312 const struct commit *a = *(const struct commit **)_a;
2313 const struct commit *b = *(const struct commit **)_b;
2314 return oidcmp(&a->object.oid, &b->object.oid);
2317 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2319 uint32_t i, dedup_i = 0;
2321 if (ctx->report_progress)
2322 ctx->progress = start_delayed_progress(
2323 _("Scanning merged commits"),
2324 ctx->commits.nr);
2326 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2328 ctx->num_extra_edges = 0;
2329 for (i = 0; i < ctx->commits.nr; i++) {
2330 display_progress(ctx->progress, i + 1);
2332 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2333 &ctx->commits.list[i]->object.oid)) {
2335 * Silently ignore duplicates. These were likely
2336 * created due to a commit appearing in multiple
2337 * layers of the chain, which is unexpected but
2338 * not invalid. We should make sure there is a
2339 * unique copy in the new layer.
2341 } else {
2342 unsigned int num_parents;
2344 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2345 dedup_i++;
2347 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2348 if (num_parents > 2)
2349 ctx->num_extra_edges += num_parents - 1;
2353 ctx->commits.nr = dedup_i;
2355 stop_progress(&ctx->progress);
2358 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2360 struct commit_graph *g = ctx->r->objects->commit_graph;
2361 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2363 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2364 current_graph_number--;
2366 if (ctx->report_progress)
2367 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2369 merge_commit_graph(ctx, g);
2370 stop_progress(&ctx->progress);
2372 g = g->base_graph;
2375 if (g) {
2376 ctx->new_base_graph = g;
2377 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2380 if (ctx->new_base_graph)
2381 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2383 sort_and_scan_merged_commits(ctx);
2386 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2388 uint32_t i;
2389 time_t now = time(NULL);
2391 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2392 struct stat st;
2393 struct utimbuf updated_time;
2395 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2396 continue;
2398 updated_time.actime = st.st_atime;
2399 updated_time.modtime = now;
2400 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2404 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2406 struct strbuf path = STRBUF_INIT;
2407 DIR *dir;
2408 struct dirent *de;
2409 size_t dirnamelen;
2410 timestamp_t expire_time = time(NULL);
2412 if (ctx->opts && ctx->opts->expire_time)
2413 expire_time = ctx->opts->expire_time;
2414 if (!ctx->split) {
2415 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2416 unlink(chain_file_name);
2417 free(chain_file_name);
2418 ctx->num_commit_graphs_after = 0;
2421 strbuf_addstr(&path, ctx->odb->path);
2422 strbuf_addstr(&path, "/info/commit-graphs");
2423 dir = opendir(path.buf);
2425 if (!dir)
2426 goto out;
2428 strbuf_addch(&path, '/');
2429 dirnamelen = path.len;
2430 while ((de = readdir(dir)) != NULL) {
2431 struct stat st;
2432 uint32_t i, found = 0;
2434 strbuf_setlen(&path, dirnamelen);
2435 strbuf_addstr(&path, de->d_name);
2437 if (stat(path.buf, &st) < 0)
2438 continue;
2440 if (st.st_mtime > expire_time)
2441 continue;
2442 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2443 continue;
2445 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2446 if (!strcmp(ctx->commit_graph_filenames_after[i],
2447 path.buf)) {
2448 found = 1;
2449 break;
2453 if (!found)
2454 unlink(path.buf);
2457 out:
2458 if(dir)
2459 closedir(dir);
2460 strbuf_release(&path);
2463 int write_commit_graph(struct object_directory *odb,
2464 const struct string_list *const pack_indexes,
2465 struct oidset *commits,
2466 enum commit_graph_write_flags flags,
2467 const struct commit_graph_opts *opts)
2469 struct repository *r = the_repository;
2470 struct write_commit_graph_context *ctx;
2471 uint32_t i;
2472 int res = 0;
2473 int replace = 0;
2474 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2475 struct topo_level_slab topo_levels;
2477 prepare_repo_settings(r);
2478 if (!r->settings.core_commit_graph) {
2479 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2480 return 0;
2482 if (!commit_graph_compatible(r))
2483 return 0;
2485 CALLOC_ARRAY(ctx, 1);
2486 ctx->r = r;
2487 ctx->odb = odb;
2488 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2489 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2490 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2491 ctx->opts = opts;
2492 ctx->total_bloom_filter_data_size = 0;
2493 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2494 ctx->num_generation_data_overflows = 0;
2496 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2497 bloom_settings.bits_per_entry);
2498 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2499 bloom_settings.num_hashes);
2500 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2501 bloom_settings.max_changed_paths);
2502 ctx->bloom_settings = &bloom_settings;
2504 init_topo_level_slab(&topo_levels);
2505 ctx->topo_levels = &topo_levels;
2507 prepare_commit_graph(ctx->r);
2508 if (ctx->r->objects->commit_graph) {
2509 struct commit_graph *g = ctx->r->objects->commit_graph;
2511 while (g) {
2512 g->topo_levels = &topo_levels;
2513 g = g->base_graph;
2517 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2518 ctx->changed_paths = 1;
2519 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2520 struct commit_graph *g;
2522 g = ctx->r->objects->commit_graph;
2524 /* We have changed-paths already. Keep them in the next graph */
2525 if (g && g->chunk_bloom_data) {
2526 ctx->changed_paths = 1;
2527 ctx->bloom_settings = g->bloom_filter_settings;
2531 if (ctx->split) {
2532 struct commit_graph *g = ctx->r->objects->commit_graph;
2534 while (g) {
2535 ctx->num_commit_graphs_before++;
2536 g = g->base_graph;
2539 if (ctx->num_commit_graphs_before) {
2540 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2541 i = ctx->num_commit_graphs_before;
2542 g = ctx->r->objects->commit_graph;
2544 while (g) {
2545 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2546 g = g->base_graph;
2550 if (ctx->opts)
2551 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2554 ctx->approx_nr_objects = repo_approximate_object_count(the_repository);
2556 if (ctx->append && ctx->r->objects->commit_graph) {
2557 struct commit_graph *g = ctx->r->objects->commit_graph;
2558 for (i = 0; i < g->num_commits; i++) {
2559 struct object_id oid;
2560 oidread(&oid, g->chunk_oid_lookup + st_mult(g->hash_len, i),
2561 the_repository->hash_algo);
2562 oid_array_append(&ctx->oids, &oid);
2566 if (pack_indexes) {
2567 ctx->order_by_pack = 1;
2568 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2569 goto cleanup;
2572 if (commits) {
2573 if ((res = fill_oids_from_commits(ctx, commits)))
2574 goto cleanup;
2577 if (!pack_indexes && !commits) {
2578 ctx->order_by_pack = 1;
2579 fill_oids_from_all_packs(ctx);
2582 close_reachable(ctx);
2584 copy_oids_to_commits(ctx);
2586 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2587 error(_("too many commits to write graph"));
2588 res = -1;
2589 goto cleanup;
2592 if (!ctx->commits.nr && !replace)
2593 goto cleanup;
2595 if (ctx->split) {
2596 split_graph_merge_strategy(ctx);
2598 if (!replace)
2599 merge_commit_graphs(ctx);
2600 } else
2601 ctx->num_commit_graphs_after = 1;
2603 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2605 compute_topological_levels(ctx);
2606 if (ctx->write_generation_data)
2607 compute_generation_numbers(ctx);
2609 if (ctx->changed_paths)
2610 compute_bloom_filters(ctx);
2612 res = write_commit_graph_file(ctx);
2614 if (ctx->split)
2615 mark_commit_graphs(ctx);
2617 expire_commit_graphs(ctx);
2619 cleanup:
2620 free(ctx->graph_name);
2621 free(ctx->base_graph_name);
2622 free(ctx->commits.list);
2623 oid_array_clear(&ctx->oids);
2624 clear_topo_level_slab(&topo_levels);
2626 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2627 free(ctx->commit_graph_filenames_before[i]);
2628 free(ctx->commit_graph_filenames_before);
2630 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2631 free(ctx->commit_graph_filenames_after[i]);
2632 free(ctx->commit_graph_hash_after[i]);
2634 free(ctx->commit_graph_filenames_after);
2635 free(ctx->commit_graph_hash_after);
2637 free(ctx);
2639 return res;
2642 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2643 static int verify_commit_graph_error;
2645 __attribute__((format (printf, 1, 2)))
2646 static void graph_report(const char *fmt, ...)
2648 va_list ap;
2650 verify_commit_graph_error = 1;
2651 va_start(ap, fmt);
2652 vfprintf(stderr, fmt, ap);
2653 fprintf(stderr, "\n");
2654 va_end(ap);
2657 static int commit_graph_checksum_valid(struct commit_graph *g)
2659 return hashfile_checksum_valid(g->data, g->data_len);
2662 static int verify_one_commit_graph(struct repository *r,
2663 struct commit_graph *g,
2664 struct progress *progress,
2665 uint64_t *seen)
2667 uint32_t i, cur_fanout_pos = 0;
2668 struct object_id prev_oid, cur_oid;
2669 struct commit *seen_gen_zero = NULL;
2670 struct commit *seen_gen_non_zero = NULL;
2672 if (!commit_graph_checksum_valid(g)) {
2673 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2674 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2677 for (i = 0; i < g->num_commits; i++) {
2678 struct commit *graph_commit;
2680 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i),
2681 the_repository->hash_algo);
2683 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2684 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2685 oid_to_hex(&prev_oid),
2686 oid_to_hex(&cur_oid));
2688 oidcpy(&prev_oid, &cur_oid);
2690 while (cur_oid.hash[0] > cur_fanout_pos) {
2691 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2693 if (i != fanout_value)
2694 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2695 cur_fanout_pos, fanout_value, i);
2696 cur_fanout_pos++;
2699 graph_commit = lookup_commit(r, &cur_oid);
2700 if (!parse_commit_in_graph_one(r, g, graph_commit))
2701 graph_report(_("failed to parse commit %s from commit-graph"),
2702 oid_to_hex(&cur_oid));
2705 while (cur_fanout_pos < 256) {
2706 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2708 if (g->num_commits != fanout_value)
2709 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2710 cur_fanout_pos, fanout_value, i);
2712 cur_fanout_pos++;
2715 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2716 return verify_commit_graph_error;
2718 for (i = 0; i < g->num_commits; i++) {
2719 struct commit *graph_commit, *odb_commit;
2720 struct commit_list *graph_parents, *odb_parents;
2721 timestamp_t max_generation = 0;
2722 timestamp_t generation;
2724 display_progress(progress, ++(*seen));
2725 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i),
2726 the_repository->hash_algo);
2728 graph_commit = lookup_commit(r, &cur_oid);
2729 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2730 if (repo_parse_commit_internal(r, odb_commit, 0, 0)) {
2731 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2732 oid_to_hex(&cur_oid));
2733 continue;
2736 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2737 get_commit_tree_oid(odb_commit)))
2738 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2739 oid_to_hex(&cur_oid),
2740 oid_to_hex(get_commit_tree_oid(graph_commit)),
2741 oid_to_hex(get_commit_tree_oid(odb_commit)));
2743 graph_parents = graph_commit->parents;
2744 odb_parents = odb_commit->parents;
2746 while (graph_parents) {
2747 if (!odb_parents) {
2748 graph_report(_("commit-graph parent list for commit %s is too long"),
2749 oid_to_hex(&cur_oid));
2750 break;
2753 /* parse parent in case it is in a base graph */
2754 parse_commit_in_graph_one(r, g, graph_parents->item);
2756 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2757 graph_report(_("commit-graph parent for %s is %s != %s"),
2758 oid_to_hex(&cur_oid),
2759 oid_to_hex(&graph_parents->item->object.oid),
2760 oid_to_hex(&odb_parents->item->object.oid));
2762 generation = commit_graph_generation_from_graph(graph_parents->item);
2763 if (generation > max_generation)
2764 max_generation = generation;
2766 graph_parents = graph_parents->next;
2767 odb_parents = odb_parents->next;
2770 if (odb_parents)
2771 graph_report(_("commit-graph parent list for commit %s terminates early"),
2772 oid_to_hex(&cur_oid));
2774 if (commit_graph_generation_from_graph(graph_commit))
2775 seen_gen_non_zero = graph_commit;
2776 else
2777 seen_gen_zero = graph_commit;
2779 if (seen_gen_zero)
2780 continue;
2783 * If we are using topological level and one of our parents has
2784 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2785 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2786 * in the following condition.
2788 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2789 max_generation--;
2791 generation = commit_graph_generation(graph_commit);
2792 if (generation < max_generation + 1)
2793 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2794 oid_to_hex(&cur_oid),
2795 generation,
2796 max_generation + 1);
2798 if (graph_commit->date != odb_commit->date)
2799 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2800 oid_to_hex(&cur_oid),
2801 graph_commit->date,
2802 odb_commit->date);
2805 if (seen_gen_zero && seen_gen_non_zero)
2806 graph_report(_("commit-graph has both zero and non-zero "
2807 "generations (e.g., commits '%s' and '%s')"),
2808 oid_to_hex(&seen_gen_zero->object.oid),
2809 oid_to_hex(&seen_gen_non_zero->object.oid));
2811 return verify_commit_graph_error;
2814 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2816 struct progress *progress = NULL;
2817 int local_error = 0;
2818 uint64_t seen = 0;
2820 if (!g) {
2821 graph_report("no commit-graph file loaded");
2822 return 1;
2825 if (flags & COMMIT_GRAPH_WRITE_PROGRESS) {
2826 uint64_t total = g->num_commits;
2827 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW))
2828 total += g->num_commits_in_base;
2830 progress = start_progress(_("Verifying commits in commit graph"),
2831 total);
2834 for (; g; g = g->base_graph) {
2835 local_error |= verify_one_commit_graph(r, g, progress, &seen);
2836 if (flags & COMMIT_GRAPH_VERIFY_SHALLOW)
2837 break;
2840 stop_progress(&progress);
2842 return local_error;
2845 void free_commit_graph(struct commit_graph *g)
2847 while (g) {
2848 struct commit_graph *next = g->base_graph;
2850 if (g->data)
2851 munmap((void *)g->data, g->data_len);
2852 free(g->filename);
2853 free(g->bloom_filter_settings);
2854 free(g);
2856 g = next;
2860 void disable_commit_graph(struct repository *r)
2862 r->commit_graph_disabled = 1;