The fifth batch
[alt-git.git] / commit-graph.c
blobc4c156ff52b55b6f2281eeeb95c16a50d83dce47
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "csum-file.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "lockfile.h"
7 #include "packfile.h"
8 #include "commit.h"
9 #include "object.h"
10 #include "refs.h"
11 #include "hash-lookup.h"
12 #include "commit-graph.h"
13 #include "object-file.h"
14 #include "object-store-ll.h"
15 #include "oid-array.h"
16 #include "path.h"
17 #include "alloc.h"
18 #include "hashmap.h"
19 #include "replace-object.h"
20 #include "progress.h"
21 #include "bloom.h"
22 #include "commit-slab.h"
23 #include "shallow.h"
24 #include "json-writer.h"
25 #include "trace2.h"
26 #include "tree.h"
27 #include "chunk-format.h"
29 void git_test_write_commit_graph_or_die(void)
31 int flags = 0;
32 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
33 return;
35 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
36 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
38 if (write_commit_graph_reachable(the_repository->objects->odb,
39 flags, NULL))
40 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
43 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
44 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
45 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
46 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
47 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
48 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
49 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
50 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
51 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
52 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
54 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
56 #define GRAPH_VERSION_1 0x1
57 #define GRAPH_VERSION GRAPH_VERSION_1
59 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
60 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
61 #define GRAPH_PARENT_NONE 0x70000000
63 #define GRAPH_LAST_EDGE 0x80000000
65 #define GRAPH_HEADER_SIZE 8
66 #define GRAPH_FANOUT_SIZE (4 * 256)
67 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
68 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
70 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
72 /* Remember to update object flag allocation in object.h */
73 #define REACHABLE (1u<<15)
75 define_commit_slab(topo_level_slab, uint32_t);
77 /* Keep track of the order in which commits are added to our list. */
78 define_commit_slab(commit_pos, int);
79 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
81 static void set_commit_pos(struct repository *r, const struct object_id *oid)
83 static int32_t max_pos;
84 struct commit *commit = lookup_commit(r, oid);
86 if (!commit)
87 return; /* should never happen, but be lenient */
89 *commit_pos_at(&commit_pos, commit) = max_pos++;
92 static int commit_pos_cmp(const void *va, const void *vb)
94 const struct commit *a = *(const struct commit **)va;
95 const struct commit *b = *(const struct commit **)vb;
96 return commit_pos_at(&commit_pos, a) -
97 commit_pos_at(&commit_pos, b);
100 define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
101 static struct commit_graph_data_slab commit_graph_data_slab =
102 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
104 static int get_configured_generation_version(struct repository *r)
106 int version = 2;
107 repo_config_get_int(r, "commitgraph.generationversion", &version);
108 return version;
111 uint32_t commit_graph_position(const struct commit *c)
113 struct commit_graph_data *data =
114 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
116 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
119 timestamp_t commit_graph_generation(const struct commit *c)
121 struct commit_graph_data *data =
122 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
124 if (data && data->generation)
125 return data->generation;
127 return GENERATION_NUMBER_INFINITY;
130 static timestamp_t commit_graph_generation_from_graph(const struct commit *c)
132 struct commit_graph_data *data =
133 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
135 if (!data || data->graph_pos == COMMIT_NOT_FROM_GRAPH)
136 return GENERATION_NUMBER_INFINITY;
137 return data->generation;
140 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
142 unsigned int i, nth_slab;
143 struct commit_graph_data *data =
144 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
146 if (data)
147 return data;
149 nth_slab = c->index / commit_graph_data_slab.slab_size;
150 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
153 * commit-slab initializes elements with zero, overwrite this with
154 * COMMIT_NOT_FROM_GRAPH for graph_pos.
156 * We avoid initializing generation with checking if graph position
157 * is not COMMIT_NOT_FROM_GRAPH.
159 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
160 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
161 COMMIT_NOT_FROM_GRAPH;
164 return data;
168 * Should be used only while writing commit-graph as it compares
169 * generation value of commits by directly accessing commit-slab.
171 static int commit_gen_cmp(const void *va, const void *vb)
173 const struct commit *a = *(const struct commit **)va;
174 const struct commit *b = *(const struct commit **)vb;
176 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
177 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
178 /* lower generation commits first */
179 if (generation_a < generation_b)
180 return -1;
181 else if (generation_a > generation_b)
182 return 1;
184 /* use date as a heuristic when generations are equal */
185 if (a->date < b->date)
186 return -1;
187 else if (a->date > b->date)
188 return 1;
189 return 0;
192 char *get_commit_graph_filename(struct object_directory *obj_dir)
194 return xstrfmt("%s/info/commit-graph", obj_dir->path);
197 static char *get_split_graph_filename(struct object_directory *odb,
198 const char *oid_hex)
200 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
201 oid_hex);
204 char *get_commit_graph_chain_filename(struct object_directory *odb)
206 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
209 static struct commit_graph *alloc_commit_graph(void)
211 struct commit_graph *g = xcalloc(1, sizeof(*g));
213 return g;
216 static int commit_graph_compatible(struct repository *r)
218 if (!r->gitdir)
219 return 0;
221 if (replace_refs_enabled(r)) {
222 prepare_replace_object(r);
223 if (hashmap_get_size(&r->objects->replace_map->map))
224 return 0;
227 prepare_commit_graft(r);
228 if (r->parsed_objects &&
229 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
230 return 0;
231 if (is_repository_shallow(r))
232 return 0;
234 return 1;
237 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
239 *fd = git_open(graph_file);
240 if (*fd < 0)
241 return 0;
242 if (fstat(*fd, st)) {
243 close(*fd);
244 return 0;
246 return 1;
249 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
250 int fd, struct stat *st,
251 struct object_directory *odb)
253 void *graph_map;
254 size_t graph_size;
255 struct commit_graph *ret;
257 graph_size = xsize_t(st->st_size);
259 if (graph_size < GRAPH_MIN_SIZE) {
260 close(fd);
261 error(_("commit-graph file is too small"));
262 return NULL;
264 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
265 close(fd);
266 prepare_repo_settings(r);
267 ret = parse_commit_graph(&r->settings, graph_map, graph_size);
269 if (ret)
270 ret->odb = odb;
271 else
272 munmap(graph_map, graph_size);
274 return ret;
277 static int graph_read_oid_fanout(const unsigned char *chunk_start,
278 size_t chunk_size, void *data)
280 struct commit_graph *g = data;
281 int i;
283 if (chunk_size != 256 * sizeof(uint32_t))
284 return error(_("commit-graph oid fanout chunk is wrong size"));
285 g->chunk_oid_fanout = (const uint32_t *)chunk_start;
286 g->num_commits = ntohl(g->chunk_oid_fanout[255]);
288 for (i = 0; i < 255; i++) {
289 uint32_t oid_fanout1 = ntohl(g->chunk_oid_fanout[i]);
290 uint32_t oid_fanout2 = ntohl(g->chunk_oid_fanout[i + 1]);
292 if (oid_fanout1 > oid_fanout2) {
293 error(_("commit-graph fanout values out of order"));
294 return 1;
298 return 0;
301 static int graph_read_oid_lookup(const unsigned char *chunk_start,
302 size_t chunk_size, void *data)
304 struct commit_graph *g = data;
305 g->chunk_oid_lookup = chunk_start;
306 if (chunk_size / g->hash_len != g->num_commits)
307 return error(_("commit-graph OID lookup chunk is the wrong size"));
308 return 0;
311 static int graph_read_commit_data(const unsigned char *chunk_start,
312 size_t chunk_size, void *data)
314 struct commit_graph *g = data;
315 if (chunk_size / GRAPH_DATA_WIDTH != g->num_commits)
316 return error(_("commit-graph commit data chunk is wrong size"));
317 g->chunk_commit_data = chunk_start;
318 return 0;
321 static int graph_read_generation_data(const unsigned char *chunk_start,
322 size_t chunk_size, void *data)
324 struct commit_graph *g = data;
325 if (chunk_size / sizeof(uint32_t) != g->num_commits)
326 return error(_("commit-graph generations chunk is wrong size"));
327 g->chunk_generation_data = chunk_start;
328 return 0;
331 static int graph_read_bloom_index(const unsigned char *chunk_start,
332 size_t chunk_size, void *data)
334 struct commit_graph *g = data;
335 if (chunk_size / 4 != g->num_commits) {
336 warning(_("commit-graph changed-path index chunk is too small"));
337 return -1;
339 g->chunk_bloom_indexes = chunk_start;
340 return 0;
343 static int graph_read_bloom_data(const unsigned char *chunk_start,
344 size_t chunk_size, void *data)
346 struct commit_graph *g = data;
347 uint32_t hash_version;
349 if (chunk_size < BLOOMDATA_CHUNK_HEADER_SIZE) {
350 warning(_("ignoring too-small changed-path chunk"
351 " (%"PRIuMAX" < %"PRIuMAX") in commit-graph file"),
352 (uintmax_t)chunk_size,
353 (uintmax_t)BLOOMDATA_CHUNK_HEADER_SIZE);
354 return -1;
357 g->chunk_bloom_data = chunk_start;
358 g->chunk_bloom_data_size = chunk_size;
359 hash_version = get_be32(chunk_start);
361 if (hash_version != 1)
362 return 0;
364 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
365 g->bloom_filter_settings->hash_version = hash_version;
366 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
367 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
368 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
370 return 0;
373 struct commit_graph *parse_commit_graph(struct repo_settings *s,
374 void *graph_map, size_t graph_size)
376 const unsigned char *data;
377 struct commit_graph *graph;
378 uint32_t graph_signature;
379 unsigned char graph_version, hash_version;
380 struct chunkfile *cf = NULL;
382 if (!graph_map)
383 return NULL;
385 if (graph_size < GRAPH_MIN_SIZE)
386 return NULL;
388 data = (const unsigned char *)graph_map;
390 graph_signature = get_be32(data);
391 if (graph_signature != GRAPH_SIGNATURE) {
392 error(_("commit-graph signature %X does not match signature %X"),
393 graph_signature, GRAPH_SIGNATURE);
394 return NULL;
397 graph_version = *(unsigned char*)(data + 4);
398 if (graph_version != GRAPH_VERSION) {
399 error(_("commit-graph version %X does not match version %X"),
400 graph_version, GRAPH_VERSION);
401 return NULL;
404 hash_version = *(unsigned char*)(data + 5);
405 if (hash_version != oid_version(the_hash_algo)) {
406 error(_("commit-graph hash version %X does not match version %X"),
407 hash_version, oid_version(the_hash_algo));
408 return NULL;
411 graph = alloc_commit_graph();
413 graph->hash_len = the_hash_algo->rawsz;
414 graph->num_chunks = *(unsigned char*)(data + 6);
415 graph->data = graph_map;
416 graph->data_len = graph_size;
418 if (graph_size < GRAPH_HEADER_SIZE +
419 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
420 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
421 error(_("commit-graph file is too small to hold %u chunks"),
422 graph->num_chunks);
423 free(graph);
424 return NULL;
427 cf = init_chunkfile(NULL);
429 if (read_table_of_contents(cf, graph->data, graph_size,
430 GRAPH_HEADER_SIZE, graph->num_chunks, 1))
431 goto free_and_return;
433 if (read_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, graph_read_oid_fanout, graph)) {
434 error(_("commit-graph required OID fanout chunk missing or corrupted"));
435 goto free_and_return;
437 if (read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph)) {
438 error(_("commit-graph required OID lookup chunk missing or corrupted"));
439 goto free_and_return;
441 if (read_chunk(cf, GRAPH_CHUNKID_DATA, graph_read_commit_data, graph)) {
442 error(_("commit-graph required commit data chunk missing or corrupted"));
443 goto free_and_return;
446 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges,
447 &graph->chunk_extra_edges_size);
448 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs,
449 &graph->chunk_base_graphs_size);
451 if (s->commit_graph_generation_version >= 2) {
452 read_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
453 graph_read_generation_data, graph);
454 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
455 &graph->chunk_generation_data_overflow,
456 &graph->chunk_generation_data_overflow_size);
458 if (graph->chunk_generation_data)
459 graph->read_generation_data = 1;
462 if (s->commit_graph_read_changed_paths) {
463 read_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
464 graph_read_bloom_index, graph);
465 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
466 graph_read_bloom_data, graph);
469 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
470 init_bloom_filters();
471 } else {
472 /* We need both the bloom chunks to exist together. Else ignore the data */
473 graph->chunk_bloom_indexes = NULL;
474 graph->chunk_bloom_data = NULL;
475 FREE_AND_NULL(graph->bloom_filter_settings);
478 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
480 free_chunkfile(cf);
481 return graph;
483 free_and_return:
484 free_chunkfile(cf);
485 free(graph->bloom_filter_settings);
486 free(graph);
487 return NULL;
490 static struct commit_graph *load_commit_graph_one(struct repository *r,
491 const char *graph_file,
492 struct object_directory *odb)
495 struct stat st;
496 int fd;
497 struct commit_graph *g;
498 int open_ok = open_commit_graph(graph_file, &fd, &st);
500 if (!open_ok)
501 return NULL;
503 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
505 if (g)
506 g->filename = xstrdup(graph_file);
508 return g;
511 static struct commit_graph *load_commit_graph_v1(struct repository *r,
512 struct object_directory *odb)
514 char *graph_name = get_commit_graph_filename(odb);
515 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
516 free(graph_name);
518 return g;
522 * returns 1 if and only if all graphs in the chain have
523 * corrected commit dates stored in the generation_data chunk.
525 static int validate_mixed_generation_chain(struct commit_graph *g)
527 int read_generation_data = 1;
528 struct commit_graph *p = g;
530 while (read_generation_data && p) {
531 read_generation_data = p->read_generation_data;
532 p = p->base_graph;
535 if (read_generation_data)
536 return 1;
538 while (g) {
539 g->read_generation_data = 0;
540 g = g->base_graph;
543 return 0;
546 static int add_graph_to_chain(struct commit_graph *g,
547 struct commit_graph *chain,
548 struct object_id *oids,
549 int n)
551 struct commit_graph *cur_g = chain;
553 if (n && !g->chunk_base_graphs) {
554 warning(_("commit-graph has no base graphs chunk"));
555 return 0;
558 if (g->chunk_base_graphs_size / g->hash_len < n) {
559 warning(_("commit-graph base graphs chunk is too small"));
560 return 0;
563 while (n) {
564 n--;
566 if (!cur_g ||
567 !oideq(&oids[n], &cur_g->oid) ||
568 !hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_len, n))) {
569 warning(_("commit-graph chain does not match"));
570 return 0;
573 cur_g = cur_g->base_graph;
576 if (chain) {
577 if (unsigned_add_overflows(chain->num_commits,
578 chain->num_commits_in_base)) {
579 warning(_("commit count in base graph too high: %"PRIuMAX),
580 (uintmax_t)chain->num_commits_in_base);
581 return 0;
583 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
586 g->base_graph = chain;
588 return 1;
591 int open_commit_graph_chain(const char *chain_file,
592 int *fd, struct stat *st)
594 *fd = git_open(chain_file);
595 if (*fd < 0)
596 return 0;
597 if (fstat(*fd, st)) {
598 close(*fd);
599 return 0;
601 if (st->st_size < the_hash_algo->hexsz) {
602 close(*fd);
603 if (!st->st_size) {
604 /* treat empty files the same as missing */
605 errno = ENOENT;
606 } else {
607 warning(_("commit-graph chain file too small"));
608 errno = EINVAL;
610 return 0;
612 return 1;
615 struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
616 int fd, struct stat *st,
617 int *incomplete_chain)
619 struct commit_graph *graph_chain = NULL;
620 struct strbuf line = STRBUF_INIT;
621 struct object_id *oids;
622 int i = 0, valid = 1, count;
623 FILE *fp = xfdopen(fd, "r");
625 count = st->st_size / (the_hash_algo->hexsz + 1);
626 CALLOC_ARRAY(oids, count);
628 prepare_alt_odb(r);
630 for (i = 0; i < count; i++) {
631 struct object_directory *odb;
633 if (strbuf_getline_lf(&line, fp) == EOF)
634 break;
636 if (get_oid_hex(line.buf, &oids[i])) {
637 warning(_("invalid commit-graph chain: line '%s' not a hash"),
638 line.buf);
639 valid = 0;
640 break;
643 valid = 0;
644 for (odb = r->objects->odb; odb; odb = odb->next) {
645 char *graph_name = get_split_graph_filename(odb, line.buf);
646 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
648 free(graph_name);
650 if (g) {
651 if (add_graph_to_chain(g, graph_chain, oids, i)) {
652 graph_chain = g;
653 valid = 1;
654 } else {
655 free_commit_graph(g);
658 break;
662 if (!valid) {
663 warning(_("unable to find all commit-graph files"));
664 break;
668 validate_mixed_generation_chain(graph_chain);
670 free(oids);
671 fclose(fp);
672 strbuf_release(&line);
674 *incomplete_chain = !valid;
675 return graph_chain;
678 static struct commit_graph *load_commit_graph_chain(struct repository *r,
679 struct object_directory *odb)
681 char *chain_file = get_commit_graph_chain_filename(odb);
682 struct stat st;
683 int fd;
684 struct commit_graph *g = NULL;
686 if (open_commit_graph_chain(chain_file, &fd, &st)) {
687 int incomplete;
688 /* ownership of fd is taken over by load function */
689 g = load_commit_graph_chain_fd_st(r, fd, &st, &incomplete);
692 free(chain_file);
693 return g;
696 struct commit_graph *read_commit_graph_one(struct repository *r,
697 struct object_directory *odb)
699 struct commit_graph *g = load_commit_graph_v1(r, odb);
701 if (!g)
702 g = load_commit_graph_chain(r, odb);
704 return g;
707 static void prepare_commit_graph_one(struct repository *r,
708 struct object_directory *odb)
711 if (r->objects->commit_graph)
712 return;
714 r->objects->commit_graph = read_commit_graph_one(r, odb);
718 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
720 * On the first invocation, this function attempts to load the commit
721 * graph if the_repository is configured to have one.
723 static int prepare_commit_graph(struct repository *r)
725 struct object_directory *odb;
728 * Early return if there is no git dir or if the commit graph is
729 * disabled.
731 * This must come before the "already attempted?" check below, because
732 * we want to disable even an already-loaded graph file.
734 if (!r->gitdir || r->commit_graph_disabled)
735 return 0;
737 if (r->objects->commit_graph_attempted)
738 return !!r->objects->commit_graph;
739 r->objects->commit_graph_attempted = 1;
741 prepare_repo_settings(r);
743 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
744 r->settings.core_commit_graph != 1)
746 * This repository is not configured to use commit graphs, so
747 * do not load one. (But report commit_graph_attempted anyway
748 * so that commit graph loading is not attempted again for this
749 * repository.)
751 return 0;
753 if (!commit_graph_compatible(r))
754 return 0;
756 prepare_alt_odb(r);
757 for (odb = r->objects->odb;
758 !r->objects->commit_graph && odb;
759 odb = odb->next)
760 prepare_commit_graph_one(r, odb);
761 return !!r->objects->commit_graph;
764 int generation_numbers_enabled(struct repository *r)
766 uint32_t first_generation;
767 struct commit_graph *g;
768 if (!prepare_commit_graph(r))
769 return 0;
771 g = r->objects->commit_graph;
773 if (!g->num_commits)
774 return 0;
776 first_generation = get_be32(g->chunk_commit_data +
777 g->hash_len + 8) >> 2;
779 return !!first_generation;
782 int corrected_commit_dates_enabled(struct repository *r)
784 struct commit_graph *g;
785 if (!prepare_commit_graph(r))
786 return 0;
788 g = r->objects->commit_graph;
790 if (!g->num_commits)
791 return 0;
793 return g->read_generation_data;
796 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
798 struct commit_graph *g = r->objects->commit_graph;
799 while (g) {
800 if (g->bloom_filter_settings)
801 return g->bloom_filter_settings;
802 g = g->base_graph;
804 return NULL;
807 void close_commit_graph(struct raw_object_store *o)
809 if (!o->commit_graph)
810 return;
812 clear_commit_graph_data_slab(&commit_graph_data_slab);
813 free_commit_graph(o->commit_graph);
814 o->commit_graph = NULL;
817 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
819 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
820 g->chunk_oid_lookup, g->hash_len, pos);
823 static void load_oid_from_graph(struct commit_graph *g,
824 uint32_t pos,
825 struct object_id *oid)
827 uint32_t lex_index;
829 while (g && pos < g->num_commits_in_base)
830 g = g->base_graph;
832 if (!g)
833 BUG("NULL commit-graph");
835 if (pos >= g->num_commits + g->num_commits_in_base)
836 die(_("invalid commit position. commit-graph is likely corrupt"));
838 lex_index = pos - g->num_commits_in_base;
840 oidread(oid, g->chunk_oid_lookup + st_mult(g->hash_len, lex_index));
843 static struct commit_list **insert_parent_or_die(struct repository *r,
844 struct commit_graph *g,
845 uint32_t pos,
846 struct commit_list **pptr)
848 struct commit *c;
849 struct object_id oid;
851 if (pos >= g->num_commits + g->num_commits_in_base)
852 die("invalid parent position %"PRIu32, pos);
854 load_oid_from_graph(g, pos, &oid);
855 c = lookup_commit(r, &oid);
856 if (!c)
857 die(_("could not find commit %s"), oid_to_hex(&oid));
858 commit_graph_data_at(c)->graph_pos = pos;
859 return &commit_list_insert(c, pptr)->next;
862 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
864 const unsigned char *commit_data;
865 struct commit_graph_data *graph_data;
866 uint32_t lex_index, offset_pos;
867 uint64_t date_high, date_low, offset;
869 while (pos < g->num_commits_in_base)
870 g = g->base_graph;
872 if (pos >= g->num_commits + g->num_commits_in_base)
873 die(_("invalid commit position. commit-graph is likely corrupt"));
875 lex_index = pos - g->num_commits_in_base;
876 commit_data = g->chunk_commit_data + st_mult(GRAPH_DATA_WIDTH, lex_index);
878 graph_data = commit_graph_data_at(item);
879 graph_data->graph_pos = pos;
881 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
882 date_low = get_be32(commit_data + g->hash_len + 12);
883 item->date = (timestamp_t)((date_high << 32) | date_low);
885 if (g->read_generation_data) {
886 offset = (timestamp_t)get_be32(g->chunk_generation_data + st_mult(sizeof(uint32_t), lex_index));
888 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
889 if (!g->chunk_generation_data_overflow)
890 die(_("commit-graph requires overflow generation data but has none"));
892 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
893 if (g->chunk_generation_data_overflow_size / sizeof(uint64_t) <= offset_pos)
894 die(_("commit-graph overflow generation data is too small"));
895 graph_data->generation = item->date +
896 get_be64(g->chunk_generation_data_overflow + sizeof(uint64_t) * offset_pos);
897 } else
898 graph_data->generation = item->date + offset;
899 } else
900 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
902 if (g->topo_levels)
903 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
906 static inline void set_commit_tree(struct commit *c, struct tree *t)
908 c->maybe_tree = t;
911 static int fill_commit_in_graph(struct repository *r,
912 struct commit *item,
913 struct commit_graph *g, uint32_t pos)
915 uint32_t edge_value;
916 uint32_t parent_data_pos;
917 struct commit_list **pptr;
918 const unsigned char *commit_data;
919 uint32_t lex_index;
921 while (pos < g->num_commits_in_base)
922 g = g->base_graph;
924 fill_commit_graph_info(item, g, pos);
926 lex_index = pos - g->num_commits_in_base;
927 commit_data = g->chunk_commit_data + st_mult(g->hash_len + 16, lex_index);
929 item->object.parsed = 1;
931 set_commit_tree(item, NULL);
933 pptr = &item->parents;
935 edge_value = get_be32(commit_data + g->hash_len);
936 if (edge_value == GRAPH_PARENT_NONE)
937 return 1;
938 pptr = insert_parent_or_die(r, g, edge_value, pptr);
940 edge_value = get_be32(commit_data + g->hash_len + 4);
941 if (edge_value == GRAPH_PARENT_NONE)
942 return 1;
943 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
944 pptr = insert_parent_or_die(r, g, edge_value, pptr);
945 return 1;
948 parent_data_pos = edge_value & GRAPH_EDGE_LAST_MASK;
949 do {
950 if (g->chunk_extra_edges_size / sizeof(uint32_t) <= parent_data_pos) {
951 error(_("commit-graph extra-edges pointer out of bounds"));
952 free_commit_list(item->parents);
953 item->parents = NULL;
954 item->object.parsed = 0;
955 return 0;
957 edge_value = get_be32(g->chunk_extra_edges +
958 sizeof(uint32_t) * parent_data_pos);
959 pptr = insert_parent_or_die(r, g,
960 edge_value & GRAPH_EDGE_LAST_MASK,
961 pptr);
962 parent_data_pos++;
963 } while (!(edge_value & GRAPH_LAST_EDGE));
965 return 1;
968 static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
970 struct commit_graph *cur_g = g;
971 uint32_t lex_index;
973 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
974 cur_g = cur_g->base_graph;
976 if (cur_g) {
977 *pos = lex_index + cur_g->num_commits_in_base;
978 return 1;
981 return 0;
984 static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
986 uint32_t graph_pos = commit_graph_position(item);
987 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
988 *pos = graph_pos;
989 return 1;
990 } else {
991 return search_commit_pos_in_graph(&item->object.oid, g, pos);
995 int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
996 uint32_t *pos)
998 if (!prepare_commit_graph(r))
999 return 0;
1000 return find_commit_pos_in_graph(c, r->objects->commit_graph, pos);
1003 struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
1005 static int commit_graph_paranoia = -1;
1006 struct commit *commit;
1007 uint32_t pos;
1009 if (commit_graph_paranoia == -1)
1010 commit_graph_paranoia = git_env_bool(GIT_COMMIT_GRAPH_PARANOIA, 0);
1012 if (!prepare_commit_graph(repo))
1013 return NULL;
1014 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
1015 return NULL;
1016 if (commit_graph_paranoia && !has_object(repo, id, 0))
1017 return NULL;
1019 commit = lookup_commit(repo, id);
1020 if (!commit)
1021 return NULL;
1022 if (commit->object.parsed)
1023 return commit;
1025 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
1026 return NULL;
1028 return commit;
1031 static int parse_commit_in_graph_one(struct repository *r,
1032 struct commit_graph *g,
1033 struct commit *item)
1035 uint32_t pos;
1037 if (item->object.parsed)
1038 return 1;
1040 if (find_commit_pos_in_graph(item, g, &pos))
1041 return fill_commit_in_graph(r, item, g, pos);
1043 return 0;
1046 int parse_commit_in_graph(struct repository *r, struct commit *item)
1048 static int checked_env = 0;
1050 if (!checked_env &&
1051 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
1052 die("dying as requested by the '%s' variable on commit-graph parse!",
1053 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
1054 checked_env = 1;
1056 if (!prepare_commit_graph(r))
1057 return 0;
1058 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
1061 void load_commit_graph_info(struct repository *r, struct commit *item)
1063 uint32_t pos;
1064 if (repo_find_commit_pos_in_graph(r, item, &pos))
1065 fill_commit_graph_info(item, r->objects->commit_graph, pos);
1068 static struct tree *load_tree_for_commit(struct repository *r,
1069 struct commit_graph *g,
1070 struct commit *c)
1072 struct object_id oid;
1073 const unsigned char *commit_data;
1074 uint32_t graph_pos = commit_graph_position(c);
1076 while (graph_pos < g->num_commits_in_base)
1077 g = g->base_graph;
1079 commit_data = g->chunk_commit_data +
1080 st_mult(GRAPH_DATA_WIDTH, graph_pos - g->num_commits_in_base);
1082 oidread(&oid, commit_data);
1083 set_commit_tree(c, lookup_tree(r, &oid));
1085 return c->maybe_tree;
1088 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
1089 struct commit_graph *g,
1090 const struct commit *c)
1092 if (c->maybe_tree)
1093 return c->maybe_tree;
1094 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
1095 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
1097 return load_tree_for_commit(r, g, (struct commit *)c);
1100 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
1102 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
1105 struct packed_commit_list {
1106 struct commit **list;
1107 size_t nr;
1108 size_t alloc;
1111 struct write_commit_graph_context {
1112 struct repository *r;
1113 struct object_directory *odb;
1114 char *graph_name;
1115 struct oid_array oids;
1116 struct packed_commit_list commits;
1117 int num_extra_edges;
1118 int num_generation_data_overflows;
1119 unsigned long approx_nr_objects;
1120 struct progress *progress;
1121 int progress_done;
1122 uint64_t progress_cnt;
1124 char *base_graph_name;
1125 int num_commit_graphs_before;
1126 int num_commit_graphs_after;
1127 char **commit_graph_filenames_before;
1128 char **commit_graph_filenames_after;
1129 char **commit_graph_hash_after;
1130 uint32_t new_num_commits_in_base;
1131 struct commit_graph *new_base_graph;
1133 unsigned append:1,
1134 report_progress:1,
1135 split:1,
1136 changed_paths:1,
1137 order_by_pack:1,
1138 write_generation_data:1,
1139 trust_generation_numbers:1;
1141 struct topo_level_slab *topo_levels;
1142 const struct commit_graph_opts *opts;
1143 size_t total_bloom_filter_data_size;
1144 const struct bloom_filter_settings *bloom_settings;
1146 int count_bloom_filter_computed;
1147 int count_bloom_filter_not_computed;
1148 int count_bloom_filter_trunc_empty;
1149 int count_bloom_filter_trunc_large;
1152 static int write_graph_chunk_fanout(struct hashfile *f,
1153 void *data)
1155 struct write_commit_graph_context *ctx = data;
1156 int i, count = 0;
1157 struct commit **list = ctx->commits.list;
1160 * Write the first-level table (the list is sorted,
1161 * but we use a 256-entry lookup to be able to avoid
1162 * having to do eight extra binary search iterations).
1164 for (i = 0; i < 256; i++) {
1165 while (count < ctx->commits.nr) {
1166 if ((*list)->object.oid.hash[0] != i)
1167 break;
1168 display_progress(ctx->progress, ++ctx->progress_cnt);
1169 count++;
1170 list++;
1173 hashwrite_be32(f, count);
1176 return 0;
1179 static int write_graph_chunk_oids(struct hashfile *f,
1180 void *data)
1182 struct write_commit_graph_context *ctx = data;
1183 struct commit **list = ctx->commits.list;
1184 int count;
1185 for (count = 0; count < ctx->commits.nr; count++, list++) {
1186 display_progress(ctx->progress, ++ctx->progress_cnt);
1187 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1190 return 0;
1193 static const struct object_id *commit_to_oid(size_t index, const void *table)
1195 const struct commit * const *commits = table;
1196 return &commits[index]->object.oid;
1199 static int write_graph_chunk_data(struct hashfile *f,
1200 void *data)
1202 struct write_commit_graph_context *ctx = data;
1203 struct commit **list = ctx->commits.list;
1204 struct commit **last = ctx->commits.list + ctx->commits.nr;
1205 uint32_t num_extra_edges = 0;
1207 while (list < last) {
1208 struct commit_list *parent;
1209 struct object_id *tree;
1210 int edge_value;
1211 uint32_t packedDate[2];
1212 display_progress(ctx->progress, ++ctx->progress_cnt);
1214 if (repo_parse_commit_no_graph(ctx->r, *list))
1215 die(_("unable to parse commit %s"),
1216 oid_to_hex(&(*list)->object.oid));
1217 tree = get_commit_tree_oid(*list);
1218 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1220 parent = (*list)->parents;
1222 if (!parent)
1223 edge_value = GRAPH_PARENT_NONE;
1224 else {
1225 edge_value = oid_pos(&parent->item->object.oid,
1226 ctx->commits.list,
1227 ctx->commits.nr,
1228 commit_to_oid);
1230 if (edge_value >= 0)
1231 edge_value += ctx->new_num_commits_in_base;
1232 else if (ctx->new_base_graph) {
1233 uint32_t pos;
1234 if (find_commit_pos_in_graph(parent->item,
1235 ctx->new_base_graph,
1236 &pos))
1237 edge_value = pos;
1240 if (edge_value < 0)
1241 BUG("missing parent %s for commit %s",
1242 oid_to_hex(&parent->item->object.oid),
1243 oid_to_hex(&(*list)->object.oid));
1246 hashwrite_be32(f, edge_value);
1248 if (parent)
1249 parent = parent->next;
1251 if (!parent)
1252 edge_value = GRAPH_PARENT_NONE;
1253 else if (parent->next)
1254 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1255 else {
1256 edge_value = oid_pos(&parent->item->object.oid,
1257 ctx->commits.list,
1258 ctx->commits.nr,
1259 commit_to_oid);
1261 if (edge_value >= 0)
1262 edge_value += ctx->new_num_commits_in_base;
1263 else if (ctx->new_base_graph) {
1264 uint32_t pos;
1265 if (find_commit_pos_in_graph(parent->item,
1266 ctx->new_base_graph,
1267 &pos))
1268 edge_value = pos;
1271 if (edge_value < 0)
1272 BUG("missing parent %s for commit %s",
1273 oid_to_hex(&parent->item->object.oid),
1274 oid_to_hex(&(*list)->object.oid));
1277 hashwrite_be32(f, edge_value);
1279 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1280 do {
1281 num_extra_edges++;
1282 parent = parent->next;
1283 } while (parent);
1286 if (sizeof((*list)->date) > 4)
1287 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1288 else
1289 packedDate[0] = 0;
1291 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1293 packedDate[1] = htonl((*list)->date);
1294 hashwrite(f, packedDate, 8);
1296 list++;
1299 return 0;
1302 static int write_graph_chunk_generation_data(struct hashfile *f,
1303 void *data)
1305 struct write_commit_graph_context *ctx = data;
1306 int i, num_generation_data_overflows = 0;
1308 for (i = 0; i < ctx->commits.nr; i++) {
1309 struct commit *c = ctx->commits.list[i];
1310 timestamp_t offset;
1311 repo_parse_commit(ctx->r, c);
1312 offset = commit_graph_data_at(c)->generation - c->date;
1313 display_progress(ctx->progress, ++ctx->progress_cnt);
1315 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1316 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1317 num_generation_data_overflows++;
1320 hashwrite_be32(f, offset);
1323 return 0;
1326 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1327 void *data)
1329 struct write_commit_graph_context *ctx = data;
1330 int i;
1331 for (i = 0; i < ctx->commits.nr; i++) {
1332 struct commit *c = ctx->commits.list[i];
1333 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1334 display_progress(ctx->progress, ++ctx->progress_cnt);
1336 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1337 hashwrite_be32(f, offset >> 32);
1338 hashwrite_be32(f, (uint32_t) offset);
1342 return 0;
1345 static int write_graph_chunk_extra_edges(struct hashfile *f,
1346 void *data)
1348 struct write_commit_graph_context *ctx = data;
1349 struct commit **list = ctx->commits.list;
1350 struct commit **last = ctx->commits.list + ctx->commits.nr;
1351 struct commit_list *parent;
1353 while (list < last) {
1354 int num_parents = 0;
1356 display_progress(ctx->progress, ++ctx->progress_cnt);
1358 for (parent = (*list)->parents; num_parents < 3 && parent;
1359 parent = parent->next)
1360 num_parents++;
1362 if (num_parents <= 2) {
1363 list++;
1364 continue;
1367 /* Since num_parents > 2, this initializer is safe. */
1368 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1369 int edge_value = oid_pos(&parent->item->object.oid,
1370 ctx->commits.list,
1371 ctx->commits.nr,
1372 commit_to_oid);
1374 if (edge_value >= 0)
1375 edge_value += ctx->new_num_commits_in_base;
1376 else if (ctx->new_base_graph) {
1377 uint32_t pos;
1378 if (find_commit_pos_in_graph(parent->item,
1379 ctx->new_base_graph,
1380 &pos))
1381 edge_value = pos;
1384 if (edge_value < 0)
1385 BUG("missing parent %s for commit %s",
1386 oid_to_hex(&parent->item->object.oid),
1387 oid_to_hex(&(*list)->object.oid));
1388 else if (!parent->next)
1389 edge_value |= GRAPH_LAST_EDGE;
1391 hashwrite_be32(f, edge_value);
1394 list++;
1397 return 0;
1400 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1401 void *data)
1403 struct write_commit_graph_context *ctx = data;
1404 struct commit **list = ctx->commits.list;
1405 struct commit **last = ctx->commits.list + ctx->commits.nr;
1406 uint32_t cur_pos = 0;
1408 while (list < last) {
1409 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1410 size_t len = filter ? filter->len : 0;
1411 cur_pos += len;
1412 display_progress(ctx->progress, ++ctx->progress_cnt);
1413 hashwrite_be32(f, cur_pos);
1414 list++;
1417 return 0;
1420 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1422 struct json_writer jw = JSON_WRITER_INIT;
1424 jw_object_begin(&jw, 0);
1425 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1426 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1427 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1428 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1429 jw_end(&jw);
1431 trace2_data_json("bloom", ctx->r, "settings", &jw);
1433 jw_release(&jw);
1436 static int write_graph_chunk_bloom_data(struct hashfile *f,
1437 void *data)
1439 struct write_commit_graph_context *ctx = data;
1440 struct commit **list = ctx->commits.list;
1441 struct commit **last = ctx->commits.list + ctx->commits.nr;
1443 trace2_bloom_filter_settings(ctx);
1445 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1446 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1447 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1449 while (list < last) {
1450 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1451 size_t len = filter ? filter->len : 0;
1453 display_progress(ctx->progress, ++ctx->progress_cnt);
1454 if (len)
1455 hashwrite(f, filter->data, len * sizeof(unsigned char));
1456 list++;
1459 return 0;
1462 static int add_packed_commits(const struct object_id *oid,
1463 struct packed_git *pack,
1464 uint32_t pos,
1465 void *data)
1467 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1468 enum object_type type;
1469 off_t offset = nth_packed_object_offset(pack, pos);
1470 struct object_info oi = OBJECT_INFO_INIT;
1472 if (ctx->progress)
1473 display_progress(ctx->progress, ++ctx->progress_done);
1475 oi.typep = &type;
1476 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1477 die(_("unable to get type of object %s"), oid_to_hex(oid));
1479 if (type != OBJ_COMMIT)
1480 return 0;
1482 oid_array_append(&ctx->oids, oid);
1483 set_commit_pos(ctx->r, oid);
1485 return 0;
1488 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1490 struct commit_list *parent;
1491 for (parent = commit->parents; parent; parent = parent->next) {
1492 if (!(parent->item->object.flags & REACHABLE)) {
1493 oid_array_append(&ctx->oids, &parent->item->object.oid);
1494 parent->item->object.flags |= REACHABLE;
1499 static void close_reachable(struct write_commit_graph_context *ctx)
1501 int i;
1502 struct commit *commit;
1503 enum commit_graph_split_flags flags = ctx->opts ?
1504 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1506 if (ctx->report_progress)
1507 ctx->progress = start_delayed_progress(
1508 _("Loading known commits in commit graph"),
1509 ctx->oids.nr);
1510 for (i = 0; i < ctx->oids.nr; i++) {
1511 display_progress(ctx->progress, i + 1);
1512 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1513 if (commit)
1514 commit->object.flags |= REACHABLE;
1516 stop_progress(&ctx->progress);
1519 * As this loop runs, ctx->oids.nr may grow, but not more
1520 * than the number of missing commits in the reachable
1521 * closure.
1523 if (ctx->report_progress)
1524 ctx->progress = start_delayed_progress(
1525 _("Expanding reachable commits in commit graph"),
1527 for (i = 0; i < ctx->oids.nr; i++) {
1528 display_progress(ctx->progress, i + 1);
1529 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1531 if (!commit)
1532 continue;
1533 if (ctx->split) {
1534 if ((!repo_parse_commit(ctx->r, commit) &&
1535 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1536 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1537 add_missing_parents(ctx, commit);
1538 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1539 add_missing_parents(ctx, commit);
1541 stop_progress(&ctx->progress);
1543 if (ctx->report_progress)
1544 ctx->progress = start_delayed_progress(
1545 _("Clearing commit marks in commit graph"),
1546 ctx->oids.nr);
1547 for (i = 0; i < ctx->oids.nr; i++) {
1548 display_progress(ctx->progress, i + 1);
1549 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1551 if (commit)
1552 commit->object.flags &= ~REACHABLE;
1554 stop_progress(&ctx->progress);
1557 struct compute_generation_info {
1558 struct repository *r;
1559 struct packed_commit_list *commits;
1560 struct progress *progress;
1561 int progress_cnt;
1563 timestamp_t (*get_generation)(struct commit *c, void *data);
1564 void (*set_generation)(struct commit *c, timestamp_t gen, void *data);
1565 void *data;
1568 static timestamp_t compute_generation_from_max(struct commit *c,
1569 timestamp_t max_gen,
1570 int generation_version)
1572 switch (generation_version) {
1573 case 1: /* topological levels */
1574 if (max_gen > GENERATION_NUMBER_V1_MAX - 1)
1575 max_gen = GENERATION_NUMBER_V1_MAX - 1;
1576 return max_gen + 1;
1578 case 2: /* corrected commit date */
1579 if (c->date && c->date > max_gen)
1580 max_gen = c->date - 1;
1581 return max_gen + 1;
1583 default:
1584 BUG("attempting unimplemented version");
1588 static void compute_reachable_generation_numbers(
1589 struct compute_generation_info *info,
1590 int generation_version)
1592 int i;
1593 struct commit_list *list = NULL;
1595 for (i = 0; i < info->commits->nr; i++) {
1596 struct commit *c = info->commits->list[i];
1597 timestamp_t gen;
1598 repo_parse_commit(info->r, c);
1599 gen = info->get_generation(c, info->data);
1600 display_progress(info->progress, info->progress_cnt + 1);
1602 if (gen != GENERATION_NUMBER_ZERO && gen != GENERATION_NUMBER_INFINITY)
1603 continue;
1605 commit_list_insert(c, &list);
1606 while (list) {
1607 struct commit *current = list->item;
1608 struct commit_list *parent;
1609 int all_parents_computed = 1;
1610 uint32_t max_gen = 0;
1612 for (parent = current->parents; parent; parent = parent->next) {
1613 repo_parse_commit(info->r, parent->item);
1614 gen = info->get_generation(parent->item, info->data);
1616 if (gen == GENERATION_NUMBER_ZERO) {
1617 all_parents_computed = 0;
1618 commit_list_insert(parent->item, &list);
1619 break;
1622 if (gen > max_gen)
1623 max_gen = gen;
1626 if (all_parents_computed) {
1627 pop_commit(&list);
1628 gen = compute_generation_from_max(
1629 current, max_gen,
1630 generation_version);
1631 info->set_generation(current, gen, info->data);
1637 static timestamp_t get_topo_level(struct commit *c, void *data)
1639 struct write_commit_graph_context *ctx = data;
1640 return *topo_level_slab_at(ctx->topo_levels, c);
1643 static void set_topo_level(struct commit *c, timestamp_t t, void *data)
1645 struct write_commit_graph_context *ctx = data;
1646 *topo_level_slab_at(ctx->topo_levels, c) = (uint32_t)t;
1649 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1651 struct compute_generation_info info = {
1652 .r = ctx->r,
1653 .commits = &ctx->commits,
1654 .get_generation = get_topo_level,
1655 .set_generation = set_topo_level,
1656 .data = ctx,
1659 if (ctx->report_progress)
1660 info.progress = ctx->progress
1661 = start_delayed_progress(
1662 _("Computing commit graph topological levels"),
1663 ctx->commits.nr);
1665 compute_reachable_generation_numbers(&info, 1);
1667 stop_progress(&ctx->progress);
1670 static timestamp_t get_generation_from_graph_data(struct commit *c,
1671 void *data UNUSED)
1673 return commit_graph_data_at(c)->generation;
1676 static void set_generation_v2(struct commit *c, timestamp_t t,
1677 void *data UNUSED)
1679 struct commit_graph_data *g = commit_graph_data_at(c);
1680 g->generation = t;
1683 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1685 int i;
1686 struct compute_generation_info info = {
1687 .r = ctx->r,
1688 .commits = &ctx->commits,
1689 .get_generation = get_generation_from_graph_data,
1690 .set_generation = set_generation_v2,
1693 if (ctx->report_progress)
1694 info.progress = ctx->progress
1695 = start_delayed_progress(
1696 _("Computing commit graph generation numbers"),
1697 ctx->commits.nr);
1699 if (!ctx->trust_generation_numbers) {
1700 for (i = 0; i < ctx->commits.nr; i++) {
1701 struct commit *c = ctx->commits.list[i];
1702 repo_parse_commit(ctx->r, c);
1703 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1707 compute_reachable_generation_numbers(&info, 2);
1709 for (i = 0; i < ctx->commits.nr; i++) {
1710 struct commit *c = ctx->commits.list[i];
1711 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1712 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1713 ctx->num_generation_data_overflows++;
1715 stop_progress(&ctx->progress);
1718 static void set_generation_in_graph_data(struct commit *c, timestamp_t t,
1719 void *data UNUSED)
1721 commit_graph_data_at(c)->generation = t;
1725 * After this method, all commits reachable from those in the given
1726 * list will have non-zero, non-infinite generation numbers.
1728 void ensure_generations_valid(struct repository *r,
1729 struct commit **commits, size_t nr)
1731 int generation_version = get_configured_generation_version(r);
1732 struct packed_commit_list list = {
1733 .list = commits,
1734 .alloc = nr,
1735 .nr = nr,
1737 struct compute_generation_info info = {
1738 .r = r,
1739 .commits = &list,
1740 .get_generation = get_generation_from_graph_data,
1741 .set_generation = set_generation_in_graph_data,
1744 compute_reachable_generation_numbers(&info, generation_version);
1747 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1749 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1750 ctx->count_bloom_filter_computed);
1751 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1752 ctx->count_bloom_filter_not_computed);
1753 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1754 ctx->count_bloom_filter_trunc_empty);
1755 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1756 ctx->count_bloom_filter_trunc_large);
1759 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1761 int i;
1762 struct progress *progress = NULL;
1763 struct commit **sorted_commits;
1764 int max_new_filters;
1766 init_bloom_filters();
1768 if (ctx->report_progress)
1769 progress = start_delayed_progress(
1770 _("Computing commit changed paths Bloom filters"),
1771 ctx->commits.nr);
1773 DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1775 if (ctx->order_by_pack)
1776 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1777 else
1778 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1780 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1781 ctx->opts->max_new_filters : ctx->commits.nr;
1783 for (i = 0; i < ctx->commits.nr; i++) {
1784 enum bloom_filter_computed computed = 0;
1785 struct commit *c = sorted_commits[i];
1786 struct bloom_filter *filter = get_or_compute_bloom_filter(
1787 ctx->r,
1789 ctx->count_bloom_filter_computed < max_new_filters,
1790 ctx->bloom_settings,
1791 &computed);
1792 if (computed & BLOOM_COMPUTED) {
1793 ctx->count_bloom_filter_computed++;
1794 if (computed & BLOOM_TRUNC_EMPTY)
1795 ctx->count_bloom_filter_trunc_empty++;
1796 if (computed & BLOOM_TRUNC_LARGE)
1797 ctx->count_bloom_filter_trunc_large++;
1798 } else if (computed & BLOOM_NOT_COMPUTED)
1799 ctx->count_bloom_filter_not_computed++;
1800 ctx->total_bloom_filter_data_size += filter
1801 ? sizeof(unsigned char) * filter->len : 0;
1802 display_progress(progress, i + 1);
1805 if (trace2_is_enabled())
1806 trace2_bloom_filter_write_statistics(ctx);
1808 free(sorted_commits);
1809 stop_progress(&progress);
1812 struct refs_cb_data {
1813 struct oidset *commits;
1814 struct progress *progress;
1817 static int add_ref_to_set(const char *refname UNUSED,
1818 const struct object_id *oid,
1819 int flags UNUSED, void *cb_data)
1821 struct object_id peeled;
1822 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1824 if (!peel_iterated_oid(oid, &peeled))
1825 oid = &peeled;
1826 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1827 oidset_insert(data->commits, oid);
1829 display_progress(data->progress, oidset_size(data->commits));
1831 return 0;
1834 int write_commit_graph_reachable(struct object_directory *odb,
1835 enum commit_graph_write_flags flags,
1836 const struct commit_graph_opts *opts)
1838 struct oidset commits = OIDSET_INIT;
1839 struct refs_cb_data data;
1840 int result;
1842 memset(&data, 0, sizeof(data));
1843 data.commits = &commits;
1844 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1845 data.progress = start_delayed_progress(
1846 _("Collecting referenced commits"), 0);
1848 refs_for_each_ref(get_main_ref_store(the_repository), add_ref_to_set,
1849 &data);
1851 stop_progress(&data.progress);
1853 result = write_commit_graph(odb, NULL, &commits,
1854 flags, opts);
1856 oidset_clear(&commits);
1857 return result;
1860 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1861 const struct string_list *pack_indexes)
1863 uint32_t i;
1864 struct strbuf progress_title = STRBUF_INIT;
1865 struct strbuf packname = STRBUF_INIT;
1866 int dirlen;
1867 int ret = 0;
1869 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1870 dirlen = packname.len;
1871 if (ctx->report_progress) {
1872 strbuf_addf(&progress_title,
1873 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1874 "Finding commits for commit graph in %"PRIuMAX" packs",
1875 pack_indexes->nr),
1876 (uintmax_t)pack_indexes->nr);
1877 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1878 ctx->progress_done = 0;
1880 for (i = 0; i < pack_indexes->nr; i++) {
1881 struct packed_git *p;
1882 strbuf_setlen(&packname, dirlen);
1883 strbuf_addstr(&packname, pack_indexes->items[i].string);
1884 p = add_packed_git(packname.buf, packname.len, 1);
1885 if (!p) {
1886 ret = error(_("error adding pack %s"), packname.buf);
1887 goto cleanup;
1889 if (open_pack_index(p)) {
1890 ret = error(_("error opening index for %s"), packname.buf);
1891 goto cleanup;
1893 for_each_object_in_pack(p, add_packed_commits, ctx,
1894 FOR_EACH_OBJECT_PACK_ORDER);
1895 close_pack(p);
1896 free(p);
1899 cleanup:
1900 stop_progress(&ctx->progress);
1901 strbuf_release(&progress_title);
1902 strbuf_release(&packname);
1904 return ret;
1907 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1908 struct oidset *commits)
1910 struct oidset_iter iter;
1911 struct object_id *oid;
1913 if (!oidset_size(commits))
1914 return 0;
1916 oidset_iter_init(commits, &iter);
1917 while ((oid = oidset_iter_next(&iter))) {
1918 oid_array_append(&ctx->oids, oid);
1921 return 0;
1924 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1926 if (ctx->report_progress)
1927 ctx->progress = start_delayed_progress(
1928 _("Finding commits for commit graph among packed objects"),
1929 ctx->approx_nr_objects);
1930 for_each_packed_object(add_packed_commits, ctx,
1931 FOR_EACH_OBJECT_PACK_ORDER);
1932 if (ctx->progress_done < ctx->approx_nr_objects)
1933 display_progress(ctx->progress, ctx->approx_nr_objects);
1934 stop_progress(&ctx->progress);
1937 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1939 uint32_t i;
1940 enum commit_graph_split_flags flags = ctx->opts ?
1941 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1943 ctx->num_extra_edges = 0;
1944 if (ctx->report_progress)
1945 ctx->progress = start_delayed_progress(
1946 _("Finding extra edges in commit graph"),
1947 ctx->oids.nr);
1948 oid_array_sort(&ctx->oids);
1949 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1950 unsigned int num_parents;
1952 display_progress(ctx->progress, i + 1);
1954 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1955 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1957 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1958 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1959 continue;
1961 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1962 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1963 else
1964 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1966 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1967 if (num_parents > 2)
1968 ctx->num_extra_edges += num_parents - 1;
1970 ctx->commits.nr++;
1972 stop_progress(&ctx->progress);
1975 static int write_graph_chunk_base_1(struct hashfile *f,
1976 struct commit_graph *g)
1978 int num = 0;
1980 if (!g)
1981 return 0;
1983 num = write_graph_chunk_base_1(f, g->base_graph);
1984 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1985 return num + 1;
1988 static int write_graph_chunk_base(struct hashfile *f,
1989 void *data)
1991 struct write_commit_graph_context *ctx = data;
1992 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1994 if (num != ctx->num_commit_graphs_after - 1) {
1995 error(_("failed to write correct number of base graph ids"));
1996 return -1;
1999 return 0;
2002 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
2004 uint32_t i;
2005 int fd;
2006 struct hashfile *f;
2007 struct lock_file lk = LOCK_INIT;
2008 const unsigned hashsz = the_hash_algo->rawsz;
2009 struct strbuf progress_title = STRBUF_INIT;
2010 struct chunkfile *cf;
2011 unsigned char file_hash[GIT_MAX_RAWSZ];
2013 if (ctx->split) {
2014 struct strbuf tmp_file = STRBUF_INIT;
2016 strbuf_addf(&tmp_file,
2017 "%s/info/commit-graphs/tmp_graph_XXXXXX",
2018 ctx->odb->path);
2019 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
2020 } else {
2021 ctx->graph_name = get_commit_graph_filename(ctx->odb);
2024 if (safe_create_leading_directories(ctx->graph_name)) {
2025 UNLEAK(ctx->graph_name);
2026 error(_("unable to create leading directories of %s"),
2027 ctx->graph_name);
2028 return -1;
2031 if (ctx->split) {
2032 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
2034 hold_lock_file_for_update_mode(&lk, lock_name,
2035 LOCK_DIE_ON_ERROR, 0444);
2036 free(lock_name);
2038 fd = git_mkstemp_mode(ctx->graph_name, 0444);
2039 if (fd < 0) {
2040 error(_("unable to create temporary graph layer"));
2041 return -1;
2044 if (adjust_shared_perm(ctx->graph_name)) {
2045 error(_("unable to adjust shared permissions for '%s'"),
2046 ctx->graph_name);
2047 return -1;
2050 f = hashfd(fd, ctx->graph_name);
2051 } else {
2052 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
2053 LOCK_DIE_ON_ERROR, 0444);
2054 fd = get_lock_file_fd(&lk);
2055 f = hashfd(fd, get_lock_file_path(&lk));
2058 cf = init_chunkfile(f);
2060 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
2061 write_graph_chunk_fanout);
2062 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, st_mult(hashsz, ctx->commits.nr),
2063 write_graph_chunk_oids);
2064 add_chunk(cf, GRAPH_CHUNKID_DATA, st_mult(hashsz + 16, ctx->commits.nr),
2065 write_graph_chunk_data);
2067 if (ctx->write_generation_data)
2068 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
2069 st_mult(sizeof(uint32_t), ctx->commits.nr),
2070 write_graph_chunk_generation_data);
2071 if (ctx->num_generation_data_overflows)
2072 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
2073 st_mult(sizeof(timestamp_t), ctx->num_generation_data_overflows),
2074 write_graph_chunk_generation_data_overflow);
2075 if (ctx->num_extra_edges)
2076 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
2077 st_mult(4, ctx->num_extra_edges),
2078 write_graph_chunk_extra_edges);
2079 if (ctx->changed_paths) {
2080 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
2081 st_mult(sizeof(uint32_t), ctx->commits.nr),
2082 write_graph_chunk_bloom_indexes);
2083 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
2084 st_add(sizeof(uint32_t) * 3,
2085 ctx->total_bloom_filter_data_size),
2086 write_graph_chunk_bloom_data);
2088 if (ctx->num_commit_graphs_after > 1)
2089 add_chunk(cf, GRAPH_CHUNKID_BASE,
2090 st_mult(hashsz, ctx->num_commit_graphs_after - 1),
2091 write_graph_chunk_base);
2093 hashwrite_be32(f, GRAPH_SIGNATURE);
2095 hashwrite_u8(f, GRAPH_VERSION);
2096 hashwrite_u8(f, oid_version(the_hash_algo));
2097 hashwrite_u8(f, get_num_chunks(cf));
2098 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
2100 if (ctx->report_progress) {
2101 strbuf_addf(&progress_title,
2102 Q_("Writing out commit graph in %d pass",
2103 "Writing out commit graph in %d passes",
2104 get_num_chunks(cf)),
2105 get_num_chunks(cf));
2106 ctx->progress = start_delayed_progress(
2107 progress_title.buf,
2108 st_mult(get_num_chunks(cf), ctx->commits.nr));
2111 write_chunkfile(cf, ctx);
2113 stop_progress(&ctx->progress);
2114 strbuf_release(&progress_title);
2116 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
2117 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
2118 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
2120 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
2121 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
2122 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
2123 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
2126 close_commit_graph(ctx->r->objects);
2127 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
2128 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
2129 free_chunkfile(cf);
2131 if (ctx->split) {
2132 FILE *chainf = fdopen_lock_file(&lk, "w");
2133 char *final_graph_name;
2134 int result;
2136 close(fd);
2138 if (!chainf) {
2139 error(_("unable to open commit-graph chain file"));
2140 return -1;
2143 if (ctx->base_graph_name) {
2144 const char *dest;
2145 int idx = ctx->num_commit_graphs_after - 1;
2146 if (ctx->num_commit_graphs_after > 1)
2147 idx--;
2149 dest = ctx->commit_graph_filenames_after[idx];
2151 if (strcmp(ctx->base_graph_name, dest)) {
2152 result = rename(ctx->base_graph_name, dest);
2154 if (result) {
2155 error(_("failed to rename base commit-graph file"));
2156 return -1;
2159 } else {
2160 char *graph_name = get_commit_graph_filename(ctx->odb);
2161 unlink(graph_name);
2162 free(graph_name);
2165 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2166 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
2167 final_graph_name = get_split_graph_filename(ctx->odb,
2168 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2169 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1]);
2170 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
2172 result = rename(ctx->graph_name, final_graph_name);
2174 for (i = 0; i < ctx->num_commit_graphs_after; i++)
2175 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2177 if (result) {
2178 error(_("failed to rename temporary commit-graph file"));
2179 return -1;
2183 commit_lock_file(&lk);
2185 return 0;
2188 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2190 struct commit_graph *g;
2191 uint32_t num_commits;
2192 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2193 uint32_t i;
2195 int max_commits = 0;
2196 int size_mult = 2;
2198 if (ctx->opts) {
2199 max_commits = ctx->opts->max_commits;
2201 if (ctx->opts->size_multiple)
2202 size_mult = ctx->opts->size_multiple;
2204 flags = ctx->opts->split_flags;
2207 g = ctx->r->objects->commit_graph;
2208 num_commits = ctx->commits.nr;
2209 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2210 ctx->num_commit_graphs_after = 1;
2211 else
2212 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2214 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2215 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2216 while (g && (g->num_commits <= st_mult(size_mult, num_commits) ||
2217 (max_commits && num_commits > max_commits))) {
2218 if (g->odb != ctx->odb)
2219 break;
2221 if (unsigned_add_overflows(num_commits, g->num_commits))
2222 die(_("cannot merge graphs with %"PRIuMAX", "
2223 "%"PRIuMAX" commits"),
2224 (uintmax_t)num_commits,
2225 (uintmax_t)g->num_commits);
2226 num_commits += g->num_commits;
2227 g = g->base_graph;
2229 ctx->num_commit_graphs_after--;
2233 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2234 ctx->new_base_graph = g;
2235 else if (ctx->num_commit_graphs_after != 1)
2236 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2237 "should be 1 with --split=replace");
2239 if (ctx->num_commit_graphs_after == 2) {
2240 char *old_graph_name = get_commit_graph_filename(g->odb);
2242 if (!strcmp(g->filename, old_graph_name) &&
2243 g->odb != ctx->odb) {
2244 ctx->num_commit_graphs_after = 1;
2245 ctx->new_base_graph = NULL;
2248 free(old_graph_name);
2251 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2252 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2254 for (i = 0; i < ctx->num_commit_graphs_after &&
2255 i < ctx->num_commit_graphs_before; i++)
2256 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2258 i = ctx->num_commit_graphs_before - 1;
2259 g = ctx->r->objects->commit_graph;
2261 while (g) {
2262 if (i < ctx->num_commit_graphs_after)
2263 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2266 * If the topmost remaining layer has generation data chunk, the
2267 * resultant layer also has generation data chunk.
2269 if (i == ctx->num_commit_graphs_after - 2)
2270 ctx->write_generation_data = !!g->chunk_generation_data;
2272 i--;
2273 g = g->base_graph;
2277 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2278 struct commit_graph *g)
2280 uint32_t i;
2281 uint32_t offset = g->num_commits_in_base;
2283 if (unsigned_add_overflows(ctx->commits.nr, g->num_commits))
2284 die(_("cannot merge graph %s, too many commits: %"PRIuMAX),
2285 oid_to_hex(&g->oid),
2286 (uintmax_t)st_add(ctx->commits.nr, g->num_commits));
2288 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2290 for (i = 0; i < g->num_commits; i++) {
2291 struct object_id oid;
2292 struct commit *result;
2294 display_progress(ctx->progress, i + 1);
2296 load_oid_from_graph(g, i + offset, &oid);
2298 /* only add commits if they still exist in the repo */
2299 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2301 if (result) {
2302 ctx->commits.list[ctx->commits.nr] = result;
2303 ctx->commits.nr++;
2308 static int commit_compare(const void *_a, const void *_b)
2310 const struct commit *a = *(const struct commit **)_a;
2311 const struct commit *b = *(const struct commit **)_b;
2312 return oidcmp(&a->object.oid, &b->object.oid);
2315 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2317 uint32_t i, dedup_i = 0;
2319 if (ctx->report_progress)
2320 ctx->progress = start_delayed_progress(
2321 _("Scanning merged commits"),
2322 ctx->commits.nr);
2324 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2326 ctx->num_extra_edges = 0;
2327 for (i = 0; i < ctx->commits.nr; i++) {
2328 display_progress(ctx->progress, i + 1);
2330 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2331 &ctx->commits.list[i]->object.oid)) {
2333 * Silently ignore duplicates. These were likely
2334 * created due to a commit appearing in multiple
2335 * layers of the chain, which is unexpected but
2336 * not invalid. We should make sure there is a
2337 * unique copy in the new layer.
2339 } else {
2340 unsigned int num_parents;
2342 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2343 dedup_i++;
2345 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2346 if (num_parents > 2)
2347 ctx->num_extra_edges += num_parents - 1;
2351 ctx->commits.nr = dedup_i;
2353 stop_progress(&ctx->progress);
2356 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2358 struct commit_graph *g = ctx->r->objects->commit_graph;
2359 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2361 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2362 current_graph_number--;
2364 if (ctx->report_progress)
2365 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2367 merge_commit_graph(ctx, g);
2368 stop_progress(&ctx->progress);
2370 g = g->base_graph;
2373 if (g) {
2374 ctx->new_base_graph = g;
2375 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2378 if (ctx->new_base_graph)
2379 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2381 sort_and_scan_merged_commits(ctx);
2384 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2386 uint32_t i;
2387 time_t now = time(NULL);
2389 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2390 struct stat st;
2391 struct utimbuf updated_time;
2393 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2394 continue;
2396 updated_time.actime = st.st_atime;
2397 updated_time.modtime = now;
2398 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2402 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2404 struct strbuf path = STRBUF_INIT;
2405 DIR *dir;
2406 struct dirent *de;
2407 size_t dirnamelen;
2408 timestamp_t expire_time = time(NULL);
2410 if (ctx->opts && ctx->opts->expire_time)
2411 expire_time = ctx->opts->expire_time;
2412 if (!ctx->split) {
2413 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2414 unlink(chain_file_name);
2415 free(chain_file_name);
2416 ctx->num_commit_graphs_after = 0;
2419 strbuf_addstr(&path, ctx->odb->path);
2420 strbuf_addstr(&path, "/info/commit-graphs");
2421 dir = opendir(path.buf);
2423 if (!dir)
2424 goto out;
2426 strbuf_addch(&path, '/');
2427 dirnamelen = path.len;
2428 while ((de = readdir(dir)) != NULL) {
2429 struct stat st;
2430 uint32_t i, found = 0;
2432 strbuf_setlen(&path, dirnamelen);
2433 strbuf_addstr(&path, de->d_name);
2435 if (stat(path.buf, &st) < 0)
2436 continue;
2438 if (st.st_mtime > expire_time)
2439 continue;
2440 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2441 continue;
2443 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2444 if (!strcmp(ctx->commit_graph_filenames_after[i],
2445 path.buf)) {
2446 found = 1;
2447 break;
2451 if (!found)
2452 unlink(path.buf);
2455 out:
2456 if(dir)
2457 closedir(dir);
2458 strbuf_release(&path);
2461 int write_commit_graph(struct object_directory *odb,
2462 const struct string_list *const pack_indexes,
2463 struct oidset *commits,
2464 enum commit_graph_write_flags flags,
2465 const struct commit_graph_opts *opts)
2467 struct repository *r = the_repository;
2468 struct write_commit_graph_context *ctx;
2469 uint32_t i;
2470 int res = 0;
2471 int replace = 0;
2472 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2473 struct topo_level_slab topo_levels;
2475 prepare_repo_settings(r);
2476 if (!r->settings.core_commit_graph) {
2477 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2478 return 0;
2480 if (!commit_graph_compatible(r))
2481 return 0;
2483 CALLOC_ARRAY(ctx, 1);
2484 ctx->r = r;
2485 ctx->odb = odb;
2486 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2487 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2488 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2489 ctx->opts = opts;
2490 ctx->total_bloom_filter_data_size = 0;
2491 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2492 ctx->num_generation_data_overflows = 0;
2494 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2495 bloom_settings.bits_per_entry);
2496 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2497 bloom_settings.num_hashes);
2498 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2499 bloom_settings.max_changed_paths);
2500 ctx->bloom_settings = &bloom_settings;
2502 init_topo_level_slab(&topo_levels);
2503 ctx->topo_levels = &topo_levels;
2505 prepare_commit_graph(ctx->r);
2506 if (ctx->r->objects->commit_graph) {
2507 struct commit_graph *g = ctx->r->objects->commit_graph;
2509 while (g) {
2510 g->topo_levels = &topo_levels;
2511 g = g->base_graph;
2515 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2516 ctx->changed_paths = 1;
2517 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2518 struct commit_graph *g;
2520 g = ctx->r->objects->commit_graph;
2522 /* We have changed-paths already. Keep them in the next graph */
2523 if (g && g->chunk_bloom_data) {
2524 ctx->changed_paths = 1;
2525 ctx->bloom_settings = g->bloom_filter_settings;
2529 if (ctx->split) {
2530 struct commit_graph *g = ctx->r->objects->commit_graph;
2532 while (g) {
2533 ctx->num_commit_graphs_before++;
2534 g = g->base_graph;
2537 if (ctx->num_commit_graphs_before) {
2538 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2539 i = ctx->num_commit_graphs_before;
2540 g = ctx->r->objects->commit_graph;
2542 while (g) {
2543 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2544 g = g->base_graph;
2548 if (ctx->opts)
2549 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2552 ctx->approx_nr_objects = repo_approximate_object_count(the_repository);
2554 if (ctx->append && ctx->r->objects->commit_graph) {
2555 struct commit_graph *g = ctx->r->objects->commit_graph;
2556 for (i = 0; i < g->num_commits; i++) {
2557 struct object_id oid;
2558 oidread(&oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2559 oid_array_append(&ctx->oids, &oid);
2563 if (pack_indexes) {
2564 ctx->order_by_pack = 1;
2565 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2566 goto cleanup;
2569 if (commits) {
2570 if ((res = fill_oids_from_commits(ctx, commits)))
2571 goto cleanup;
2574 if (!pack_indexes && !commits) {
2575 ctx->order_by_pack = 1;
2576 fill_oids_from_all_packs(ctx);
2579 close_reachable(ctx);
2581 copy_oids_to_commits(ctx);
2583 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2584 error(_("too many commits to write graph"));
2585 res = -1;
2586 goto cleanup;
2589 if (!ctx->commits.nr && !replace)
2590 goto cleanup;
2592 if (ctx->split) {
2593 split_graph_merge_strategy(ctx);
2595 if (!replace)
2596 merge_commit_graphs(ctx);
2597 } else
2598 ctx->num_commit_graphs_after = 1;
2600 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2602 compute_topological_levels(ctx);
2603 if (ctx->write_generation_data)
2604 compute_generation_numbers(ctx);
2606 if (ctx->changed_paths)
2607 compute_bloom_filters(ctx);
2609 res = write_commit_graph_file(ctx);
2611 if (ctx->split)
2612 mark_commit_graphs(ctx);
2614 expire_commit_graphs(ctx);
2616 cleanup:
2617 free(ctx->graph_name);
2618 free(ctx->base_graph_name);
2619 free(ctx->commits.list);
2620 oid_array_clear(&ctx->oids);
2621 clear_topo_level_slab(&topo_levels);
2623 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2624 free(ctx->commit_graph_filenames_before[i]);
2625 free(ctx->commit_graph_filenames_before);
2627 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2628 free(ctx->commit_graph_filenames_after[i]);
2629 free(ctx->commit_graph_hash_after[i]);
2631 free(ctx->commit_graph_filenames_after);
2632 free(ctx->commit_graph_hash_after);
2634 free(ctx);
2636 return res;
2639 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2640 static int verify_commit_graph_error;
2642 __attribute__((format (printf, 1, 2)))
2643 static void graph_report(const char *fmt, ...)
2645 va_list ap;
2647 verify_commit_graph_error = 1;
2648 va_start(ap, fmt);
2649 vfprintf(stderr, fmt, ap);
2650 fprintf(stderr, "\n");
2651 va_end(ap);
2654 static int commit_graph_checksum_valid(struct commit_graph *g)
2656 return hashfile_checksum_valid(g->data, g->data_len);
2659 static int verify_one_commit_graph(struct repository *r,
2660 struct commit_graph *g,
2661 struct progress *progress,
2662 uint64_t *seen)
2664 uint32_t i, cur_fanout_pos = 0;
2665 struct object_id prev_oid, cur_oid;
2666 struct commit *seen_gen_zero = NULL;
2667 struct commit *seen_gen_non_zero = NULL;
2669 if (!commit_graph_checksum_valid(g)) {
2670 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2671 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2674 for (i = 0; i < g->num_commits; i++) {
2675 struct commit *graph_commit;
2677 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2679 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2680 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2681 oid_to_hex(&prev_oid),
2682 oid_to_hex(&cur_oid));
2684 oidcpy(&prev_oid, &cur_oid);
2686 while (cur_oid.hash[0] > cur_fanout_pos) {
2687 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2689 if (i != fanout_value)
2690 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2691 cur_fanout_pos, fanout_value, i);
2692 cur_fanout_pos++;
2695 graph_commit = lookup_commit(r, &cur_oid);
2696 if (!parse_commit_in_graph_one(r, g, graph_commit))
2697 graph_report(_("failed to parse commit %s from commit-graph"),
2698 oid_to_hex(&cur_oid));
2701 while (cur_fanout_pos < 256) {
2702 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2704 if (g->num_commits != fanout_value)
2705 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2706 cur_fanout_pos, fanout_value, i);
2708 cur_fanout_pos++;
2711 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2712 return verify_commit_graph_error;
2714 for (i = 0; i < g->num_commits; i++) {
2715 struct commit *graph_commit, *odb_commit;
2716 struct commit_list *graph_parents, *odb_parents;
2717 timestamp_t max_generation = 0;
2718 timestamp_t generation;
2720 display_progress(progress, ++(*seen));
2721 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2723 graph_commit = lookup_commit(r, &cur_oid);
2724 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2725 if (repo_parse_commit_internal(r, odb_commit, 0, 0)) {
2726 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2727 oid_to_hex(&cur_oid));
2728 continue;
2731 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2732 get_commit_tree_oid(odb_commit)))
2733 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2734 oid_to_hex(&cur_oid),
2735 oid_to_hex(get_commit_tree_oid(graph_commit)),
2736 oid_to_hex(get_commit_tree_oid(odb_commit)));
2738 graph_parents = graph_commit->parents;
2739 odb_parents = odb_commit->parents;
2741 while (graph_parents) {
2742 if (!odb_parents) {
2743 graph_report(_("commit-graph parent list for commit %s is too long"),
2744 oid_to_hex(&cur_oid));
2745 break;
2748 /* parse parent in case it is in a base graph */
2749 parse_commit_in_graph_one(r, g, graph_parents->item);
2751 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2752 graph_report(_("commit-graph parent for %s is %s != %s"),
2753 oid_to_hex(&cur_oid),
2754 oid_to_hex(&graph_parents->item->object.oid),
2755 oid_to_hex(&odb_parents->item->object.oid));
2757 generation = commit_graph_generation_from_graph(graph_parents->item);
2758 if (generation > max_generation)
2759 max_generation = generation;
2761 graph_parents = graph_parents->next;
2762 odb_parents = odb_parents->next;
2765 if (odb_parents)
2766 graph_report(_("commit-graph parent list for commit %s terminates early"),
2767 oid_to_hex(&cur_oid));
2769 if (commit_graph_generation_from_graph(graph_commit))
2770 seen_gen_non_zero = graph_commit;
2771 else
2772 seen_gen_zero = graph_commit;
2774 if (seen_gen_zero)
2775 continue;
2778 * If we are using topological level and one of our parents has
2779 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2780 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2781 * in the following condition.
2783 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2784 max_generation--;
2786 generation = commit_graph_generation(graph_commit);
2787 if (generation < max_generation + 1)
2788 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2789 oid_to_hex(&cur_oid),
2790 generation,
2791 max_generation + 1);
2793 if (graph_commit->date != odb_commit->date)
2794 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2795 oid_to_hex(&cur_oid),
2796 graph_commit->date,
2797 odb_commit->date);
2800 if (seen_gen_zero && seen_gen_non_zero)
2801 graph_report(_("commit-graph has both zero and non-zero "
2802 "generations (e.g., commits '%s' and '%s')"),
2803 oid_to_hex(&seen_gen_zero->object.oid),
2804 oid_to_hex(&seen_gen_non_zero->object.oid));
2806 return verify_commit_graph_error;
2809 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2811 struct progress *progress = NULL;
2812 int local_error = 0;
2813 uint64_t seen = 0;
2815 if (!g) {
2816 graph_report("no commit-graph file loaded");
2817 return 1;
2820 if (flags & COMMIT_GRAPH_WRITE_PROGRESS) {
2821 uint64_t total = g->num_commits;
2822 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW))
2823 total += g->num_commits_in_base;
2825 progress = start_progress(_("Verifying commits in commit graph"),
2826 total);
2829 for (; g; g = g->base_graph) {
2830 local_error |= verify_one_commit_graph(r, g, progress, &seen);
2831 if (flags & COMMIT_GRAPH_VERIFY_SHALLOW)
2832 break;
2835 stop_progress(&progress);
2837 return local_error;
2840 void free_commit_graph(struct commit_graph *g)
2842 while (g) {
2843 struct commit_graph *next = g->base_graph;
2845 if (g->data)
2846 munmap((void *)g->data, g->data_len);
2847 free(g->filename);
2848 free(g->bloom_filter_settings);
2849 free(g);
2851 g = next;
2855 void disable_commit_graph(struct repository *r)
2857 r->commit_graph_disabled = 1;