midx: check size of pack names chunk
[alt-git.git] / commit-graph.c
blobb217e19194e1b2fdbd1a29e537525e2e0ee0e867
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "gettext.h"
4 #include "hex.h"
5 #include "lockfile.h"
6 #include "pack.h"
7 #include "packfile.h"
8 #include "commit.h"
9 #include "object.h"
10 #include "refs.h"
11 #include "revision.h"
12 #include "hash-lookup.h"
13 #include "commit-graph.h"
14 #include "object-file.h"
15 #include "object-store-ll.h"
16 #include "oid-array.h"
17 #include "path.h"
18 #include "alloc.h"
19 #include "hashmap.h"
20 #include "replace-object.h"
21 #include "progress.h"
22 #include "bloom.h"
23 #include "commit-slab.h"
24 #include "shallow.h"
25 #include "json-writer.h"
26 #include "trace2.h"
27 #include "tree.h"
28 #include "chunk-format.h"
30 void git_test_write_commit_graph_or_die(void)
32 int flags = 0;
33 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
34 return;
36 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
37 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
39 if (write_commit_graph_reachable(the_repository->objects->odb,
40 flags, NULL))
41 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
44 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
45 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
46 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
47 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
48 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
49 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
50 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
51 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
52 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
53 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
55 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
57 #define GRAPH_VERSION_1 0x1
58 #define GRAPH_VERSION GRAPH_VERSION_1
60 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
61 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
62 #define GRAPH_PARENT_NONE 0x70000000
64 #define GRAPH_LAST_EDGE 0x80000000
66 #define GRAPH_HEADER_SIZE 8
67 #define GRAPH_FANOUT_SIZE (4 * 256)
68 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
69 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
71 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
73 /* Remember to update object flag allocation in object.h */
74 #define REACHABLE (1u<<15)
76 define_commit_slab(topo_level_slab, uint32_t);
78 /* Keep track of the order in which commits are added to our list. */
79 define_commit_slab(commit_pos, int);
80 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
82 static void set_commit_pos(struct repository *r, const struct object_id *oid)
84 static int32_t max_pos;
85 struct commit *commit = lookup_commit(r, oid);
87 if (!commit)
88 return; /* should never happen, but be lenient */
90 *commit_pos_at(&commit_pos, commit) = max_pos++;
93 static int commit_pos_cmp(const void *va, const void *vb)
95 const struct commit *a = *(const struct commit **)va;
96 const struct commit *b = *(const struct commit **)vb;
97 return commit_pos_at(&commit_pos, a) -
98 commit_pos_at(&commit_pos, b);
101 define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
102 static struct commit_graph_data_slab commit_graph_data_slab =
103 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
105 static int get_configured_generation_version(struct repository *r)
107 int version = 2;
108 repo_config_get_int(r, "commitgraph.generationversion", &version);
109 return version;
112 uint32_t commit_graph_position(const struct commit *c)
114 struct commit_graph_data *data =
115 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
117 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
120 timestamp_t commit_graph_generation(const struct commit *c)
122 struct commit_graph_data *data =
123 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
125 if (data && data->generation)
126 return data->generation;
128 return GENERATION_NUMBER_INFINITY;
131 static timestamp_t commit_graph_generation_from_graph(const struct commit *c)
133 struct commit_graph_data *data =
134 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
136 if (!data || data->graph_pos == COMMIT_NOT_FROM_GRAPH)
137 return GENERATION_NUMBER_INFINITY;
138 return data->generation;
141 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
143 unsigned int i, nth_slab;
144 struct commit_graph_data *data =
145 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
147 if (data)
148 return data;
150 nth_slab = c->index / commit_graph_data_slab.slab_size;
151 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
154 * commit-slab initializes elements with zero, overwrite this with
155 * COMMIT_NOT_FROM_GRAPH for graph_pos.
157 * We avoid initializing generation with checking if graph position
158 * is not COMMIT_NOT_FROM_GRAPH.
160 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
161 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
162 COMMIT_NOT_FROM_GRAPH;
165 return data;
169 * Should be used only while writing commit-graph as it compares
170 * generation value of commits by directly accessing commit-slab.
172 static int commit_gen_cmp(const void *va, const void *vb)
174 const struct commit *a = *(const struct commit **)va;
175 const struct commit *b = *(const struct commit **)vb;
177 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
178 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
179 /* lower generation commits first */
180 if (generation_a < generation_b)
181 return -1;
182 else if (generation_a > generation_b)
183 return 1;
185 /* use date as a heuristic when generations are equal */
186 if (a->date < b->date)
187 return -1;
188 else if (a->date > b->date)
189 return 1;
190 return 0;
193 char *get_commit_graph_filename(struct object_directory *obj_dir)
195 return xstrfmt("%s/info/commit-graph", obj_dir->path);
198 static char *get_split_graph_filename(struct object_directory *odb,
199 const char *oid_hex)
201 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
202 oid_hex);
205 char *get_commit_graph_chain_filename(struct object_directory *odb)
207 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
210 static struct commit_graph *alloc_commit_graph(void)
212 struct commit_graph *g = xcalloc(1, sizeof(*g));
214 return g;
217 static int commit_graph_compatible(struct repository *r)
219 if (!r->gitdir)
220 return 0;
222 if (replace_refs_enabled(r)) {
223 prepare_replace_object(r);
224 if (hashmap_get_size(&r->objects->replace_map->map))
225 return 0;
228 prepare_commit_graft(r);
229 if (r->parsed_objects &&
230 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
231 return 0;
232 if (is_repository_shallow(r))
233 return 0;
235 return 1;
238 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
240 *fd = git_open(graph_file);
241 if (*fd < 0)
242 return 0;
243 if (fstat(*fd, st)) {
244 close(*fd);
245 return 0;
247 return 1;
250 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
251 int fd, struct stat *st,
252 struct object_directory *odb)
254 void *graph_map;
255 size_t graph_size;
256 struct commit_graph *ret;
258 graph_size = xsize_t(st->st_size);
260 if (graph_size < GRAPH_MIN_SIZE) {
261 close(fd);
262 error(_("commit-graph file is too small"));
263 return NULL;
265 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
266 close(fd);
267 prepare_repo_settings(r);
268 ret = parse_commit_graph(&r->settings, graph_map, graph_size);
270 if (ret)
271 ret->odb = odb;
272 else
273 munmap(graph_map, graph_size);
275 return ret;
278 static int verify_commit_graph_lite(struct commit_graph *g)
280 int i;
283 * Basic validation shared between parse_commit_graph()
284 * which'll be called every time the graph is used, and the
285 * much more expensive verify_commit_graph() used by
286 * "commit-graph verify".
288 * There should only be very basic checks here to ensure that
289 * we don't e.g. segfault in fill_commit_in_graph(), but
290 * because this is a very hot codepath nothing that e.g. loops
291 * over g->num_commits, or runs a checksum on the commit-graph
292 * itself.
294 if (!g->chunk_oid_fanout) {
295 error("commit-graph is missing the OID Fanout chunk");
296 return 1;
298 if (!g->chunk_oid_lookup) {
299 error("commit-graph is missing the OID Lookup chunk");
300 return 1;
302 if (!g->chunk_commit_data) {
303 error("commit-graph is missing the Commit Data chunk");
304 return 1;
307 for (i = 0; i < 255; i++) {
308 uint32_t oid_fanout1 = ntohl(g->chunk_oid_fanout[i]);
309 uint32_t oid_fanout2 = ntohl(g->chunk_oid_fanout[i + 1]);
311 if (oid_fanout1 > oid_fanout2) {
312 error("commit-graph fanout values out of order");
313 return 1;
316 if (ntohl(g->chunk_oid_fanout[255]) != g->num_commits) {
317 error("commit-graph oid table and fanout disagree on size");
318 return 1;
321 return 0;
324 static int graph_read_oid_fanout(const unsigned char *chunk_start,
325 size_t chunk_size, void *data)
327 struct commit_graph *g = data;
328 if (chunk_size != 256 * sizeof(uint32_t))
329 return error("commit-graph oid fanout chunk is wrong size");
330 g->chunk_oid_fanout = (const uint32_t *)chunk_start;
331 return 0;
334 static int graph_read_oid_lookup(const unsigned char *chunk_start,
335 size_t chunk_size, void *data)
337 struct commit_graph *g = data;
338 g->chunk_oid_lookup = chunk_start;
339 g->num_commits = chunk_size / g->hash_len;
340 return 0;
343 static int graph_read_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;
348 g->chunk_bloom_data = chunk_start;
349 hash_version = get_be32(chunk_start);
351 if (hash_version != 1)
352 return 0;
354 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
355 g->bloom_filter_settings->hash_version = hash_version;
356 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
357 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
358 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
360 return 0;
363 struct commit_graph *parse_commit_graph(struct repo_settings *s,
364 void *graph_map, size_t graph_size)
366 const unsigned char *data;
367 struct commit_graph *graph;
368 uint32_t graph_signature;
369 unsigned char graph_version, hash_version;
370 struct chunkfile *cf = NULL;
372 if (!graph_map)
373 return NULL;
375 if (graph_size < GRAPH_MIN_SIZE)
376 return NULL;
378 data = (const unsigned char *)graph_map;
380 graph_signature = get_be32(data);
381 if (graph_signature != GRAPH_SIGNATURE) {
382 error(_("commit-graph signature %X does not match signature %X"),
383 graph_signature, GRAPH_SIGNATURE);
384 return NULL;
387 graph_version = *(unsigned char*)(data + 4);
388 if (graph_version != GRAPH_VERSION) {
389 error(_("commit-graph version %X does not match version %X"),
390 graph_version, GRAPH_VERSION);
391 return NULL;
394 hash_version = *(unsigned char*)(data + 5);
395 if (hash_version != oid_version(the_hash_algo)) {
396 error(_("commit-graph hash version %X does not match version %X"),
397 hash_version, oid_version(the_hash_algo));
398 return NULL;
401 graph = alloc_commit_graph();
403 graph->hash_len = the_hash_algo->rawsz;
404 graph->num_chunks = *(unsigned char*)(data + 6);
405 graph->data = graph_map;
406 graph->data_len = graph_size;
408 if (graph_size < GRAPH_HEADER_SIZE +
409 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
410 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
411 error(_("commit-graph file is too small to hold %u chunks"),
412 graph->num_chunks);
413 free(graph);
414 return NULL;
417 cf = init_chunkfile(NULL);
419 if (read_table_of_contents(cf, graph->data, graph_size,
420 GRAPH_HEADER_SIZE, graph->num_chunks))
421 goto free_and_return;
423 read_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, graph_read_oid_fanout, graph);
424 read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph);
425 pair_chunk_unsafe(cf, GRAPH_CHUNKID_DATA, &graph->chunk_commit_data);
426 pair_chunk_unsafe(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges);
427 pair_chunk_unsafe(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs);
429 if (s->commit_graph_generation_version >= 2) {
430 pair_chunk_unsafe(cf, GRAPH_CHUNKID_GENERATION_DATA,
431 &graph->chunk_generation_data);
432 pair_chunk_unsafe(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
433 &graph->chunk_generation_data_overflow);
435 if (graph->chunk_generation_data)
436 graph->read_generation_data = 1;
439 if (s->commit_graph_read_changed_paths) {
440 pair_chunk_unsafe(cf, GRAPH_CHUNKID_BLOOMINDEXES,
441 &graph->chunk_bloom_indexes);
442 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
443 graph_read_bloom_data, graph);
446 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
447 init_bloom_filters();
448 } else {
449 /* We need both the bloom chunks to exist together. Else ignore the data */
450 graph->chunk_bloom_indexes = NULL;
451 graph->chunk_bloom_data = NULL;
452 FREE_AND_NULL(graph->bloom_filter_settings);
455 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
457 if (verify_commit_graph_lite(graph))
458 goto free_and_return;
460 free_chunkfile(cf);
461 return graph;
463 free_and_return:
464 free_chunkfile(cf);
465 free(graph->bloom_filter_settings);
466 free(graph);
467 return NULL;
470 static struct commit_graph *load_commit_graph_one(struct repository *r,
471 const char *graph_file,
472 struct object_directory *odb)
475 struct stat st;
476 int fd;
477 struct commit_graph *g;
478 int open_ok = open_commit_graph(graph_file, &fd, &st);
480 if (!open_ok)
481 return NULL;
483 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
485 if (g)
486 g->filename = xstrdup(graph_file);
488 return g;
491 static struct commit_graph *load_commit_graph_v1(struct repository *r,
492 struct object_directory *odb)
494 char *graph_name = get_commit_graph_filename(odb);
495 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
496 free(graph_name);
498 return g;
502 * returns 1 if and only if all graphs in the chain have
503 * corrected commit dates stored in the generation_data chunk.
505 static int validate_mixed_generation_chain(struct commit_graph *g)
507 int read_generation_data = 1;
508 struct commit_graph *p = g;
510 while (read_generation_data && p) {
511 read_generation_data = p->read_generation_data;
512 p = p->base_graph;
515 if (read_generation_data)
516 return 1;
518 while (g) {
519 g->read_generation_data = 0;
520 g = g->base_graph;
523 return 0;
526 static int add_graph_to_chain(struct commit_graph *g,
527 struct commit_graph *chain,
528 struct object_id *oids,
529 int n)
531 struct commit_graph *cur_g = chain;
533 if (n && !g->chunk_base_graphs) {
534 warning(_("commit-graph has no base graphs chunk"));
535 return 0;
538 while (n) {
539 n--;
541 if (!cur_g ||
542 !oideq(&oids[n], &cur_g->oid) ||
543 !hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_len, n))) {
544 warning(_("commit-graph chain does not match"));
545 return 0;
548 cur_g = cur_g->base_graph;
551 g->base_graph = chain;
553 if (chain) {
554 if (unsigned_add_overflows(chain->num_commits,
555 chain->num_commits_in_base)) {
556 warning(_("commit count in base graph too high: %"PRIuMAX),
557 (uintmax_t)chain->num_commits_in_base);
558 return 0;
560 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
563 return 1;
566 int open_commit_graph_chain(const char *chain_file,
567 int *fd, struct stat *st)
569 *fd = git_open(chain_file);
570 if (*fd < 0)
571 return 0;
572 if (fstat(*fd, st)) {
573 close(*fd);
574 return 0;
576 if (st->st_size < the_hash_algo->hexsz) {
577 close(*fd);
578 if (!st->st_size) {
579 /* treat empty files the same as missing */
580 errno = ENOENT;
581 } else {
582 warning("commit-graph chain file too small");
583 errno = EINVAL;
585 return 0;
587 return 1;
590 struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
591 int fd, struct stat *st,
592 int *incomplete_chain)
594 struct commit_graph *graph_chain = NULL;
595 struct strbuf line = STRBUF_INIT;
596 struct object_id *oids;
597 int i = 0, valid = 1, count;
598 FILE *fp = xfdopen(fd, "r");
600 count = st->st_size / (the_hash_algo->hexsz + 1);
601 CALLOC_ARRAY(oids, count);
603 prepare_alt_odb(r);
605 for (i = 0; i < count; i++) {
606 struct object_directory *odb;
608 if (strbuf_getline_lf(&line, fp) == EOF)
609 break;
611 if (get_oid_hex(line.buf, &oids[i])) {
612 warning(_("invalid commit-graph chain: line '%s' not a hash"),
613 line.buf);
614 valid = 0;
615 break;
618 valid = 0;
619 for (odb = r->objects->odb; odb; odb = odb->next) {
620 char *graph_name = get_split_graph_filename(odb, line.buf);
621 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
623 free(graph_name);
625 if (g) {
626 if (add_graph_to_chain(g, graph_chain, oids, i)) {
627 graph_chain = g;
628 valid = 1;
631 break;
635 if (!valid) {
636 warning(_("unable to find all commit-graph files"));
637 break;
641 validate_mixed_generation_chain(graph_chain);
643 free(oids);
644 fclose(fp);
645 strbuf_release(&line);
647 *incomplete_chain = !valid;
648 return graph_chain;
651 static struct commit_graph *load_commit_graph_chain(struct repository *r,
652 struct object_directory *odb)
654 char *chain_file = get_commit_graph_chain_filename(odb);
655 struct stat st;
656 int fd;
657 struct commit_graph *g = NULL;
659 if (open_commit_graph_chain(chain_file, &fd, &st)) {
660 int incomplete;
661 /* ownership of fd is taken over by load function */
662 g = load_commit_graph_chain_fd_st(r, fd, &st, &incomplete);
665 free(chain_file);
666 return g;
669 struct commit_graph *read_commit_graph_one(struct repository *r,
670 struct object_directory *odb)
672 struct commit_graph *g = load_commit_graph_v1(r, odb);
674 if (!g)
675 g = load_commit_graph_chain(r, odb);
677 return g;
680 static void prepare_commit_graph_one(struct repository *r,
681 struct object_directory *odb)
684 if (r->objects->commit_graph)
685 return;
687 r->objects->commit_graph = read_commit_graph_one(r, odb);
691 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
693 * On the first invocation, this function attempts to load the commit
694 * graph if the_repository is configured to have one.
696 static int prepare_commit_graph(struct repository *r)
698 struct object_directory *odb;
701 * Early return if there is no git dir or if the commit graph is
702 * disabled.
704 * This must come before the "already attempted?" check below, because
705 * we want to disable even an already-loaded graph file.
707 if (!r->gitdir || r->commit_graph_disabled)
708 return 0;
710 if (r->objects->commit_graph_attempted)
711 return !!r->objects->commit_graph;
712 r->objects->commit_graph_attempted = 1;
714 prepare_repo_settings(r);
716 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
717 r->settings.core_commit_graph != 1)
719 * This repository is not configured to use commit graphs, so
720 * do not load one. (But report commit_graph_attempted anyway
721 * so that commit graph loading is not attempted again for this
722 * repository.)
724 return 0;
726 if (!commit_graph_compatible(r))
727 return 0;
729 prepare_alt_odb(r);
730 for (odb = r->objects->odb;
731 !r->objects->commit_graph && odb;
732 odb = odb->next)
733 prepare_commit_graph_one(r, odb);
734 return !!r->objects->commit_graph;
737 int generation_numbers_enabled(struct repository *r)
739 uint32_t first_generation;
740 struct commit_graph *g;
741 if (!prepare_commit_graph(r))
742 return 0;
744 g = r->objects->commit_graph;
746 if (!g->num_commits)
747 return 0;
749 first_generation = get_be32(g->chunk_commit_data +
750 g->hash_len + 8) >> 2;
752 return !!first_generation;
755 int corrected_commit_dates_enabled(struct repository *r)
757 struct commit_graph *g;
758 if (!prepare_commit_graph(r))
759 return 0;
761 g = r->objects->commit_graph;
763 if (!g->num_commits)
764 return 0;
766 return g->read_generation_data;
769 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
771 struct commit_graph *g = r->objects->commit_graph;
772 while (g) {
773 if (g->bloom_filter_settings)
774 return g->bloom_filter_settings;
775 g = g->base_graph;
777 return NULL;
780 static void close_commit_graph_one(struct commit_graph *g)
782 if (!g)
783 return;
785 clear_commit_graph_data_slab(&commit_graph_data_slab);
786 close_commit_graph_one(g->base_graph);
787 free_commit_graph(g);
790 void close_commit_graph(struct raw_object_store *o)
792 close_commit_graph_one(o->commit_graph);
793 o->commit_graph = NULL;
796 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
798 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
799 g->chunk_oid_lookup, g->hash_len, pos);
802 static void load_oid_from_graph(struct commit_graph *g,
803 uint32_t pos,
804 struct object_id *oid)
806 uint32_t lex_index;
808 while (g && pos < g->num_commits_in_base)
809 g = g->base_graph;
811 if (!g)
812 BUG("NULL commit-graph");
814 if (pos >= g->num_commits + g->num_commits_in_base)
815 die(_("invalid commit position. commit-graph is likely corrupt"));
817 lex_index = pos - g->num_commits_in_base;
819 oidread(oid, g->chunk_oid_lookup + st_mult(g->hash_len, lex_index));
822 static struct commit_list **insert_parent_or_die(struct repository *r,
823 struct commit_graph *g,
824 uint32_t pos,
825 struct commit_list **pptr)
827 struct commit *c;
828 struct object_id oid;
830 if (pos >= g->num_commits + g->num_commits_in_base)
831 die("invalid parent position %"PRIu32, pos);
833 load_oid_from_graph(g, pos, &oid);
834 c = lookup_commit(r, &oid);
835 if (!c)
836 die(_("could not find commit %s"), oid_to_hex(&oid));
837 commit_graph_data_at(c)->graph_pos = pos;
838 return &commit_list_insert(c, pptr)->next;
841 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
843 const unsigned char *commit_data;
844 struct commit_graph_data *graph_data;
845 uint32_t lex_index, offset_pos;
846 uint64_t date_high, date_low, offset;
848 while (pos < g->num_commits_in_base)
849 g = g->base_graph;
851 if (pos >= g->num_commits + g->num_commits_in_base)
852 die(_("invalid commit position. commit-graph is likely corrupt"));
854 lex_index = pos - g->num_commits_in_base;
855 commit_data = g->chunk_commit_data + st_mult(GRAPH_DATA_WIDTH, lex_index);
857 graph_data = commit_graph_data_at(item);
858 graph_data->graph_pos = pos;
860 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
861 date_low = get_be32(commit_data + g->hash_len + 12);
862 item->date = (timestamp_t)((date_high << 32) | date_low);
864 if (g->read_generation_data) {
865 offset = (timestamp_t)get_be32(g->chunk_generation_data + st_mult(sizeof(uint32_t), lex_index));
867 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
868 if (!g->chunk_generation_data_overflow)
869 die(_("commit-graph requires overflow generation data but has none"));
871 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
872 graph_data->generation = item->date + get_be64(g->chunk_generation_data_overflow + st_mult(8, offset_pos));
873 } else
874 graph_data->generation = item->date + offset;
875 } else
876 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
878 if (g->topo_levels)
879 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
882 static inline void set_commit_tree(struct commit *c, struct tree *t)
884 c->maybe_tree = t;
887 static int fill_commit_in_graph(struct repository *r,
888 struct commit *item,
889 struct commit_graph *g, uint32_t pos)
891 uint32_t edge_value;
892 uint32_t *parent_data_ptr;
893 struct commit_list **pptr;
894 const unsigned char *commit_data;
895 uint32_t lex_index;
897 while (pos < g->num_commits_in_base)
898 g = g->base_graph;
900 fill_commit_graph_info(item, g, pos);
902 lex_index = pos - g->num_commits_in_base;
903 commit_data = g->chunk_commit_data + st_mult(g->hash_len + 16, lex_index);
905 item->object.parsed = 1;
907 set_commit_tree(item, NULL);
909 pptr = &item->parents;
911 edge_value = get_be32(commit_data + g->hash_len);
912 if (edge_value == GRAPH_PARENT_NONE)
913 return 1;
914 pptr = insert_parent_or_die(r, g, edge_value, pptr);
916 edge_value = get_be32(commit_data + g->hash_len + 4);
917 if (edge_value == GRAPH_PARENT_NONE)
918 return 1;
919 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
920 pptr = insert_parent_or_die(r, g, edge_value, pptr);
921 return 1;
924 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
925 st_mult(4, edge_value & GRAPH_EDGE_LAST_MASK));
926 do {
927 edge_value = get_be32(parent_data_ptr);
928 pptr = insert_parent_or_die(r, g,
929 edge_value & GRAPH_EDGE_LAST_MASK,
930 pptr);
931 parent_data_ptr++;
932 } while (!(edge_value & GRAPH_LAST_EDGE));
934 return 1;
937 static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
939 struct commit_graph *cur_g = g;
940 uint32_t lex_index;
942 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
943 cur_g = cur_g->base_graph;
945 if (cur_g) {
946 *pos = lex_index + cur_g->num_commits_in_base;
947 return 1;
950 return 0;
953 static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
955 uint32_t graph_pos = commit_graph_position(item);
956 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
957 *pos = graph_pos;
958 return 1;
959 } else {
960 return search_commit_pos_in_graph(&item->object.oid, g, pos);
964 int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
965 uint32_t *pos)
967 if (!prepare_commit_graph(r))
968 return 0;
969 return find_commit_pos_in_graph(c, r->objects->commit_graph, pos);
972 struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
974 struct commit *commit;
975 uint32_t pos;
977 if (!prepare_commit_graph(repo))
978 return NULL;
979 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
980 return NULL;
981 if (!has_object(repo, id, 0))
982 return NULL;
984 commit = lookup_commit(repo, id);
985 if (!commit)
986 return NULL;
987 if (commit->object.parsed)
988 return commit;
990 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
991 return NULL;
993 return commit;
996 static int parse_commit_in_graph_one(struct repository *r,
997 struct commit_graph *g,
998 struct commit *item)
1000 uint32_t pos;
1002 if (item->object.parsed)
1003 return 1;
1005 if (find_commit_pos_in_graph(item, g, &pos))
1006 return fill_commit_in_graph(r, item, g, pos);
1008 return 0;
1011 int parse_commit_in_graph(struct repository *r, struct commit *item)
1013 static int checked_env = 0;
1015 if (!checked_env &&
1016 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
1017 die("dying as requested by the '%s' variable on commit-graph parse!",
1018 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
1019 checked_env = 1;
1021 if (!prepare_commit_graph(r))
1022 return 0;
1023 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
1026 void load_commit_graph_info(struct repository *r, struct commit *item)
1028 uint32_t pos;
1029 if (repo_find_commit_pos_in_graph(r, item, &pos))
1030 fill_commit_graph_info(item, r->objects->commit_graph, pos);
1033 static struct tree *load_tree_for_commit(struct repository *r,
1034 struct commit_graph *g,
1035 struct commit *c)
1037 struct object_id oid;
1038 const unsigned char *commit_data;
1039 uint32_t graph_pos = commit_graph_position(c);
1041 while (graph_pos < g->num_commits_in_base)
1042 g = g->base_graph;
1044 commit_data = g->chunk_commit_data +
1045 st_mult(GRAPH_DATA_WIDTH, graph_pos - g->num_commits_in_base);
1047 oidread(&oid, commit_data);
1048 set_commit_tree(c, lookup_tree(r, &oid));
1050 return c->maybe_tree;
1053 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
1054 struct commit_graph *g,
1055 const struct commit *c)
1057 if (c->maybe_tree)
1058 return c->maybe_tree;
1059 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
1060 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
1062 return load_tree_for_commit(r, g, (struct commit *)c);
1065 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
1067 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
1070 struct packed_commit_list {
1071 struct commit **list;
1072 size_t nr;
1073 size_t alloc;
1076 struct write_commit_graph_context {
1077 struct repository *r;
1078 struct object_directory *odb;
1079 char *graph_name;
1080 struct oid_array oids;
1081 struct packed_commit_list commits;
1082 int num_extra_edges;
1083 int num_generation_data_overflows;
1084 unsigned long approx_nr_objects;
1085 struct progress *progress;
1086 int progress_done;
1087 uint64_t progress_cnt;
1089 char *base_graph_name;
1090 int num_commit_graphs_before;
1091 int num_commit_graphs_after;
1092 char **commit_graph_filenames_before;
1093 char **commit_graph_filenames_after;
1094 char **commit_graph_hash_after;
1095 uint32_t new_num_commits_in_base;
1096 struct commit_graph *new_base_graph;
1098 unsigned append:1,
1099 report_progress:1,
1100 split:1,
1101 changed_paths:1,
1102 order_by_pack:1,
1103 write_generation_data:1,
1104 trust_generation_numbers:1;
1106 struct topo_level_slab *topo_levels;
1107 const struct commit_graph_opts *opts;
1108 size_t total_bloom_filter_data_size;
1109 const struct bloom_filter_settings *bloom_settings;
1111 int count_bloom_filter_computed;
1112 int count_bloom_filter_not_computed;
1113 int count_bloom_filter_trunc_empty;
1114 int count_bloom_filter_trunc_large;
1117 static int write_graph_chunk_fanout(struct hashfile *f,
1118 void *data)
1120 struct write_commit_graph_context *ctx = data;
1121 int i, count = 0;
1122 struct commit **list = ctx->commits.list;
1125 * Write the first-level table (the list is sorted,
1126 * but we use a 256-entry lookup to be able to avoid
1127 * having to do eight extra binary search iterations).
1129 for (i = 0; i < 256; i++) {
1130 while (count < ctx->commits.nr) {
1131 if ((*list)->object.oid.hash[0] != i)
1132 break;
1133 display_progress(ctx->progress, ++ctx->progress_cnt);
1134 count++;
1135 list++;
1138 hashwrite_be32(f, count);
1141 return 0;
1144 static int write_graph_chunk_oids(struct hashfile *f,
1145 void *data)
1147 struct write_commit_graph_context *ctx = data;
1148 struct commit **list = ctx->commits.list;
1149 int count;
1150 for (count = 0; count < ctx->commits.nr; count++, list++) {
1151 display_progress(ctx->progress, ++ctx->progress_cnt);
1152 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1155 return 0;
1158 static const struct object_id *commit_to_oid(size_t index, const void *table)
1160 const struct commit * const *commits = table;
1161 return &commits[index]->object.oid;
1164 static int write_graph_chunk_data(struct hashfile *f,
1165 void *data)
1167 struct write_commit_graph_context *ctx = data;
1168 struct commit **list = ctx->commits.list;
1169 struct commit **last = ctx->commits.list + ctx->commits.nr;
1170 uint32_t num_extra_edges = 0;
1172 while (list < last) {
1173 struct commit_list *parent;
1174 struct object_id *tree;
1175 int edge_value;
1176 uint32_t packedDate[2];
1177 display_progress(ctx->progress, ++ctx->progress_cnt);
1179 if (repo_parse_commit_no_graph(ctx->r, *list))
1180 die(_("unable to parse commit %s"),
1181 oid_to_hex(&(*list)->object.oid));
1182 tree = get_commit_tree_oid(*list);
1183 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1185 parent = (*list)->parents;
1187 if (!parent)
1188 edge_value = GRAPH_PARENT_NONE;
1189 else {
1190 edge_value = oid_pos(&parent->item->object.oid,
1191 ctx->commits.list,
1192 ctx->commits.nr,
1193 commit_to_oid);
1195 if (edge_value >= 0)
1196 edge_value += ctx->new_num_commits_in_base;
1197 else if (ctx->new_base_graph) {
1198 uint32_t pos;
1199 if (find_commit_pos_in_graph(parent->item,
1200 ctx->new_base_graph,
1201 &pos))
1202 edge_value = pos;
1205 if (edge_value < 0)
1206 BUG("missing parent %s for commit %s",
1207 oid_to_hex(&parent->item->object.oid),
1208 oid_to_hex(&(*list)->object.oid));
1211 hashwrite_be32(f, edge_value);
1213 if (parent)
1214 parent = parent->next;
1216 if (!parent)
1217 edge_value = GRAPH_PARENT_NONE;
1218 else if (parent->next)
1219 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1220 else {
1221 edge_value = oid_pos(&parent->item->object.oid,
1222 ctx->commits.list,
1223 ctx->commits.nr,
1224 commit_to_oid);
1226 if (edge_value >= 0)
1227 edge_value += ctx->new_num_commits_in_base;
1228 else if (ctx->new_base_graph) {
1229 uint32_t pos;
1230 if (find_commit_pos_in_graph(parent->item,
1231 ctx->new_base_graph,
1232 &pos))
1233 edge_value = pos;
1236 if (edge_value < 0)
1237 BUG("missing parent %s for commit %s",
1238 oid_to_hex(&parent->item->object.oid),
1239 oid_to_hex(&(*list)->object.oid));
1242 hashwrite_be32(f, edge_value);
1244 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1245 do {
1246 num_extra_edges++;
1247 parent = parent->next;
1248 } while (parent);
1251 if (sizeof((*list)->date) > 4)
1252 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1253 else
1254 packedDate[0] = 0;
1256 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1258 packedDate[1] = htonl((*list)->date);
1259 hashwrite(f, packedDate, 8);
1261 list++;
1264 return 0;
1267 static int write_graph_chunk_generation_data(struct hashfile *f,
1268 void *data)
1270 struct write_commit_graph_context *ctx = data;
1271 int i, num_generation_data_overflows = 0;
1273 for (i = 0; i < ctx->commits.nr; i++) {
1274 struct commit *c = ctx->commits.list[i];
1275 timestamp_t offset;
1276 repo_parse_commit(ctx->r, c);
1277 offset = commit_graph_data_at(c)->generation - c->date;
1278 display_progress(ctx->progress, ++ctx->progress_cnt);
1280 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1281 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1282 num_generation_data_overflows++;
1285 hashwrite_be32(f, offset);
1288 return 0;
1291 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1292 void *data)
1294 struct write_commit_graph_context *ctx = data;
1295 int i;
1296 for (i = 0; i < ctx->commits.nr; i++) {
1297 struct commit *c = ctx->commits.list[i];
1298 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1299 display_progress(ctx->progress, ++ctx->progress_cnt);
1301 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1302 hashwrite_be32(f, offset >> 32);
1303 hashwrite_be32(f, (uint32_t) offset);
1307 return 0;
1310 static int write_graph_chunk_extra_edges(struct hashfile *f,
1311 void *data)
1313 struct write_commit_graph_context *ctx = data;
1314 struct commit **list = ctx->commits.list;
1315 struct commit **last = ctx->commits.list + ctx->commits.nr;
1316 struct commit_list *parent;
1318 while (list < last) {
1319 int num_parents = 0;
1321 display_progress(ctx->progress, ++ctx->progress_cnt);
1323 for (parent = (*list)->parents; num_parents < 3 && parent;
1324 parent = parent->next)
1325 num_parents++;
1327 if (num_parents <= 2) {
1328 list++;
1329 continue;
1332 /* Since num_parents > 2, this initializer is safe. */
1333 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1334 int edge_value = oid_pos(&parent->item->object.oid,
1335 ctx->commits.list,
1336 ctx->commits.nr,
1337 commit_to_oid);
1339 if (edge_value >= 0)
1340 edge_value += ctx->new_num_commits_in_base;
1341 else if (ctx->new_base_graph) {
1342 uint32_t pos;
1343 if (find_commit_pos_in_graph(parent->item,
1344 ctx->new_base_graph,
1345 &pos))
1346 edge_value = pos;
1349 if (edge_value < 0)
1350 BUG("missing parent %s for commit %s",
1351 oid_to_hex(&parent->item->object.oid),
1352 oid_to_hex(&(*list)->object.oid));
1353 else if (!parent->next)
1354 edge_value |= GRAPH_LAST_EDGE;
1356 hashwrite_be32(f, edge_value);
1359 list++;
1362 return 0;
1365 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1366 void *data)
1368 struct write_commit_graph_context *ctx = data;
1369 struct commit **list = ctx->commits.list;
1370 struct commit **last = ctx->commits.list + ctx->commits.nr;
1371 uint32_t cur_pos = 0;
1373 while (list < last) {
1374 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1375 size_t len = filter ? filter->len : 0;
1376 cur_pos += len;
1377 display_progress(ctx->progress, ++ctx->progress_cnt);
1378 hashwrite_be32(f, cur_pos);
1379 list++;
1382 return 0;
1385 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1387 struct json_writer jw = JSON_WRITER_INIT;
1389 jw_object_begin(&jw, 0);
1390 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1391 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1392 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1393 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1394 jw_end(&jw);
1396 trace2_data_json("bloom", ctx->r, "settings", &jw);
1398 jw_release(&jw);
1401 static int write_graph_chunk_bloom_data(struct hashfile *f,
1402 void *data)
1404 struct write_commit_graph_context *ctx = data;
1405 struct commit **list = ctx->commits.list;
1406 struct commit **last = ctx->commits.list + ctx->commits.nr;
1408 trace2_bloom_filter_settings(ctx);
1410 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1411 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1412 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1414 while (list < last) {
1415 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1416 size_t len = filter ? filter->len : 0;
1418 display_progress(ctx->progress, ++ctx->progress_cnt);
1419 if (len)
1420 hashwrite(f, filter->data, len * sizeof(unsigned char));
1421 list++;
1424 return 0;
1427 static int add_packed_commits(const struct object_id *oid,
1428 struct packed_git *pack,
1429 uint32_t pos,
1430 void *data)
1432 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1433 enum object_type type;
1434 off_t offset = nth_packed_object_offset(pack, pos);
1435 struct object_info oi = OBJECT_INFO_INIT;
1437 if (ctx->progress)
1438 display_progress(ctx->progress, ++ctx->progress_done);
1440 oi.typep = &type;
1441 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1442 die(_("unable to get type of object %s"), oid_to_hex(oid));
1444 if (type != OBJ_COMMIT)
1445 return 0;
1447 oid_array_append(&ctx->oids, oid);
1448 set_commit_pos(ctx->r, oid);
1450 return 0;
1453 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1455 struct commit_list *parent;
1456 for (parent = commit->parents; parent; parent = parent->next) {
1457 if (!(parent->item->object.flags & REACHABLE)) {
1458 oid_array_append(&ctx->oids, &parent->item->object.oid);
1459 parent->item->object.flags |= REACHABLE;
1464 static void close_reachable(struct write_commit_graph_context *ctx)
1466 int i;
1467 struct commit *commit;
1468 enum commit_graph_split_flags flags = ctx->opts ?
1469 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1471 if (ctx->report_progress)
1472 ctx->progress = start_delayed_progress(
1473 _("Loading known commits in commit graph"),
1474 ctx->oids.nr);
1475 for (i = 0; i < ctx->oids.nr; i++) {
1476 display_progress(ctx->progress, i + 1);
1477 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1478 if (commit)
1479 commit->object.flags |= REACHABLE;
1481 stop_progress(&ctx->progress);
1484 * As this loop runs, ctx->oids.nr may grow, but not more
1485 * than the number of missing commits in the reachable
1486 * closure.
1488 if (ctx->report_progress)
1489 ctx->progress = start_delayed_progress(
1490 _("Expanding reachable commits in commit graph"),
1492 for (i = 0; i < ctx->oids.nr; i++) {
1493 display_progress(ctx->progress, i + 1);
1494 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1496 if (!commit)
1497 continue;
1498 if (ctx->split) {
1499 if ((!repo_parse_commit(ctx->r, commit) &&
1500 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1501 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1502 add_missing_parents(ctx, commit);
1503 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1504 add_missing_parents(ctx, commit);
1506 stop_progress(&ctx->progress);
1508 if (ctx->report_progress)
1509 ctx->progress = start_delayed_progress(
1510 _("Clearing commit marks in commit graph"),
1511 ctx->oids.nr);
1512 for (i = 0; i < ctx->oids.nr; i++) {
1513 display_progress(ctx->progress, i + 1);
1514 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1516 if (commit)
1517 commit->object.flags &= ~REACHABLE;
1519 stop_progress(&ctx->progress);
1522 struct compute_generation_info {
1523 struct repository *r;
1524 struct packed_commit_list *commits;
1525 struct progress *progress;
1526 int progress_cnt;
1528 timestamp_t (*get_generation)(struct commit *c, void *data);
1529 void (*set_generation)(struct commit *c, timestamp_t gen, void *data);
1530 void *data;
1533 static timestamp_t compute_generation_from_max(struct commit *c,
1534 timestamp_t max_gen,
1535 int generation_version)
1537 switch (generation_version) {
1538 case 1: /* topological levels */
1539 if (max_gen > GENERATION_NUMBER_V1_MAX - 1)
1540 max_gen = GENERATION_NUMBER_V1_MAX - 1;
1541 return max_gen + 1;
1543 case 2: /* corrected commit date */
1544 if (c->date && c->date > max_gen)
1545 max_gen = c->date - 1;
1546 return max_gen + 1;
1548 default:
1549 BUG("attempting unimplemented version");
1553 static void compute_reachable_generation_numbers(
1554 struct compute_generation_info *info,
1555 int generation_version)
1557 int i;
1558 struct commit_list *list = NULL;
1560 for (i = 0; i < info->commits->nr; i++) {
1561 struct commit *c = info->commits->list[i];
1562 timestamp_t gen;
1563 repo_parse_commit(info->r, c);
1564 gen = info->get_generation(c, info->data);
1565 display_progress(info->progress, info->progress_cnt + 1);
1567 if (gen != GENERATION_NUMBER_ZERO && gen != GENERATION_NUMBER_INFINITY)
1568 continue;
1570 commit_list_insert(c, &list);
1571 while (list) {
1572 struct commit *current = list->item;
1573 struct commit_list *parent;
1574 int all_parents_computed = 1;
1575 uint32_t max_gen = 0;
1577 for (parent = current->parents; parent; parent = parent->next) {
1578 repo_parse_commit(info->r, parent->item);
1579 gen = info->get_generation(parent->item, info->data);
1581 if (gen == GENERATION_NUMBER_ZERO) {
1582 all_parents_computed = 0;
1583 commit_list_insert(parent->item, &list);
1584 break;
1587 if (gen > max_gen)
1588 max_gen = gen;
1591 if (all_parents_computed) {
1592 pop_commit(&list);
1593 gen = compute_generation_from_max(
1594 current, max_gen,
1595 generation_version);
1596 info->set_generation(current, gen, info->data);
1602 static timestamp_t get_topo_level(struct commit *c, void *data)
1604 struct write_commit_graph_context *ctx = data;
1605 return *topo_level_slab_at(ctx->topo_levels, c);
1608 static void set_topo_level(struct commit *c, timestamp_t t, void *data)
1610 struct write_commit_graph_context *ctx = data;
1611 *topo_level_slab_at(ctx->topo_levels, c) = (uint32_t)t;
1614 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1616 struct compute_generation_info info = {
1617 .r = ctx->r,
1618 .commits = &ctx->commits,
1619 .get_generation = get_topo_level,
1620 .set_generation = set_topo_level,
1621 .data = ctx,
1624 if (ctx->report_progress)
1625 info.progress = ctx->progress
1626 = start_delayed_progress(
1627 _("Computing commit graph topological levels"),
1628 ctx->commits.nr);
1630 compute_reachable_generation_numbers(&info, 1);
1632 stop_progress(&ctx->progress);
1635 static timestamp_t get_generation_from_graph_data(struct commit *c,
1636 void *data UNUSED)
1638 return commit_graph_data_at(c)->generation;
1641 static void set_generation_v2(struct commit *c, timestamp_t t,
1642 void *data UNUSED)
1644 struct commit_graph_data *g = commit_graph_data_at(c);
1645 g->generation = t;
1648 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1650 int i;
1651 struct compute_generation_info info = {
1652 .r = ctx->r,
1653 .commits = &ctx->commits,
1654 .get_generation = get_generation_from_graph_data,
1655 .set_generation = set_generation_v2,
1658 if (ctx->report_progress)
1659 info.progress = ctx->progress
1660 = start_delayed_progress(
1661 _("Computing commit graph generation numbers"),
1662 ctx->commits.nr);
1664 if (!ctx->trust_generation_numbers) {
1665 for (i = 0; i < ctx->commits.nr; i++) {
1666 struct commit *c = ctx->commits.list[i];
1667 repo_parse_commit(ctx->r, c);
1668 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1672 compute_reachable_generation_numbers(&info, 2);
1674 for (i = 0; i < ctx->commits.nr; i++) {
1675 struct commit *c = ctx->commits.list[i];
1676 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1677 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1678 ctx->num_generation_data_overflows++;
1680 stop_progress(&ctx->progress);
1683 static void set_generation_in_graph_data(struct commit *c, timestamp_t t,
1684 void *data UNUSED)
1686 commit_graph_data_at(c)->generation = t;
1690 * After this method, all commits reachable from those in the given
1691 * list will have non-zero, non-infinite generation numbers.
1693 void ensure_generations_valid(struct repository *r,
1694 struct commit **commits, size_t nr)
1696 int generation_version = get_configured_generation_version(r);
1697 struct packed_commit_list list = {
1698 .list = commits,
1699 .alloc = nr,
1700 .nr = nr,
1702 struct compute_generation_info info = {
1703 .r = r,
1704 .commits = &list,
1705 .get_generation = get_generation_from_graph_data,
1706 .set_generation = set_generation_in_graph_data,
1709 compute_reachable_generation_numbers(&info, generation_version);
1712 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1714 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1715 ctx->count_bloom_filter_computed);
1716 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1717 ctx->count_bloom_filter_not_computed);
1718 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1719 ctx->count_bloom_filter_trunc_empty);
1720 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1721 ctx->count_bloom_filter_trunc_large);
1724 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1726 int i;
1727 struct progress *progress = NULL;
1728 struct commit **sorted_commits;
1729 int max_new_filters;
1731 init_bloom_filters();
1733 if (ctx->report_progress)
1734 progress = start_delayed_progress(
1735 _("Computing commit changed paths Bloom filters"),
1736 ctx->commits.nr);
1738 DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1740 if (ctx->order_by_pack)
1741 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1742 else
1743 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1745 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1746 ctx->opts->max_new_filters : ctx->commits.nr;
1748 for (i = 0; i < ctx->commits.nr; i++) {
1749 enum bloom_filter_computed computed = 0;
1750 struct commit *c = sorted_commits[i];
1751 struct bloom_filter *filter = get_or_compute_bloom_filter(
1752 ctx->r,
1754 ctx->count_bloom_filter_computed < max_new_filters,
1755 ctx->bloom_settings,
1756 &computed);
1757 if (computed & BLOOM_COMPUTED) {
1758 ctx->count_bloom_filter_computed++;
1759 if (computed & BLOOM_TRUNC_EMPTY)
1760 ctx->count_bloom_filter_trunc_empty++;
1761 if (computed & BLOOM_TRUNC_LARGE)
1762 ctx->count_bloom_filter_trunc_large++;
1763 } else if (computed & BLOOM_NOT_COMPUTED)
1764 ctx->count_bloom_filter_not_computed++;
1765 ctx->total_bloom_filter_data_size += filter
1766 ? sizeof(unsigned char) * filter->len : 0;
1767 display_progress(progress, i + 1);
1770 if (trace2_is_enabled())
1771 trace2_bloom_filter_write_statistics(ctx);
1773 free(sorted_commits);
1774 stop_progress(&progress);
1777 struct refs_cb_data {
1778 struct oidset *commits;
1779 struct progress *progress;
1782 static int add_ref_to_set(const char *refname UNUSED,
1783 const struct object_id *oid,
1784 int flags UNUSED, void *cb_data)
1786 struct object_id peeled;
1787 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1789 if (!peel_iterated_oid(oid, &peeled))
1790 oid = &peeled;
1791 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1792 oidset_insert(data->commits, oid);
1794 display_progress(data->progress, oidset_size(data->commits));
1796 return 0;
1799 int write_commit_graph_reachable(struct object_directory *odb,
1800 enum commit_graph_write_flags flags,
1801 const struct commit_graph_opts *opts)
1803 struct oidset commits = OIDSET_INIT;
1804 struct refs_cb_data data;
1805 int result;
1807 memset(&data, 0, sizeof(data));
1808 data.commits = &commits;
1809 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1810 data.progress = start_delayed_progress(
1811 _("Collecting referenced commits"), 0);
1813 for_each_ref(add_ref_to_set, &data);
1815 stop_progress(&data.progress);
1817 result = write_commit_graph(odb, NULL, &commits,
1818 flags, opts);
1820 oidset_clear(&commits);
1821 return result;
1824 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1825 const struct string_list *pack_indexes)
1827 uint32_t i;
1828 struct strbuf progress_title = STRBUF_INIT;
1829 struct strbuf packname = STRBUF_INIT;
1830 int dirlen;
1831 int ret = 0;
1833 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1834 dirlen = packname.len;
1835 if (ctx->report_progress) {
1836 strbuf_addf(&progress_title,
1837 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1838 "Finding commits for commit graph in %"PRIuMAX" packs",
1839 pack_indexes->nr),
1840 (uintmax_t)pack_indexes->nr);
1841 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1842 ctx->progress_done = 0;
1844 for (i = 0; i < pack_indexes->nr; i++) {
1845 struct packed_git *p;
1846 strbuf_setlen(&packname, dirlen);
1847 strbuf_addstr(&packname, pack_indexes->items[i].string);
1848 p = add_packed_git(packname.buf, packname.len, 1);
1849 if (!p) {
1850 ret = error(_("error adding pack %s"), packname.buf);
1851 goto cleanup;
1853 if (open_pack_index(p)) {
1854 ret = error(_("error opening index for %s"), packname.buf);
1855 goto cleanup;
1857 for_each_object_in_pack(p, add_packed_commits, ctx,
1858 FOR_EACH_OBJECT_PACK_ORDER);
1859 close_pack(p);
1860 free(p);
1863 cleanup:
1864 stop_progress(&ctx->progress);
1865 strbuf_release(&progress_title);
1866 strbuf_release(&packname);
1868 return ret;
1871 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1872 struct oidset *commits)
1874 struct oidset_iter iter;
1875 struct object_id *oid;
1877 if (!oidset_size(commits))
1878 return 0;
1880 oidset_iter_init(commits, &iter);
1881 while ((oid = oidset_iter_next(&iter))) {
1882 oid_array_append(&ctx->oids, oid);
1885 return 0;
1888 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1890 if (ctx->report_progress)
1891 ctx->progress = start_delayed_progress(
1892 _("Finding commits for commit graph among packed objects"),
1893 ctx->approx_nr_objects);
1894 for_each_packed_object(add_packed_commits, ctx,
1895 FOR_EACH_OBJECT_PACK_ORDER);
1896 if (ctx->progress_done < ctx->approx_nr_objects)
1897 display_progress(ctx->progress, ctx->approx_nr_objects);
1898 stop_progress(&ctx->progress);
1901 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1903 uint32_t i;
1904 enum commit_graph_split_flags flags = ctx->opts ?
1905 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1907 ctx->num_extra_edges = 0;
1908 if (ctx->report_progress)
1909 ctx->progress = start_delayed_progress(
1910 _("Finding extra edges in commit graph"),
1911 ctx->oids.nr);
1912 oid_array_sort(&ctx->oids);
1913 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1914 unsigned int num_parents;
1916 display_progress(ctx->progress, i + 1);
1918 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1919 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1921 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1922 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1923 continue;
1925 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1926 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1927 else
1928 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1930 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1931 if (num_parents > 2)
1932 ctx->num_extra_edges += num_parents - 1;
1934 ctx->commits.nr++;
1936 stop_progress(&ctx->progress);
1939 static int write_graph_chunk_base_1(struct hashfile *f,
1940 struct commit_graph *g)
1942 int num = 0;
1944 if (!g)
1945 return 0;
1947 num = write_graph_chunk_base_1(f, g->base_graph);
1948 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1949 return num + 1;
1952 static int write_graph_chunk_base(struct hashfile *f,
1953 void *data)
1955 struct write_commit_graph_context *ctx = data;
1956 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1958 if (num != ctx->num_commit_graphs_after - 1) {
1959 error(_("failed to write correct number of base graph ids"));
1960 return -1;
1963 return 0;
1966 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1968 uint32_t i;
1969 int fd;
1970 struct hashfile *f;
1971 struct lock_file lk = LOCK_INIT;
1972 const unsigned hashsz = the_hash_algo->rawsz;
1973 struct strbuf progress_title = STRBUF_INIT;
1974 struct chunkfile *cf;
1975 unsigned char file_hash[GIT_MAX_RAWSZ];
1977 if (ctx->split) {
1978 struct strbuf tmp_file = STRBUF_INIT;
1980 strbuf_addf(&tmp_file,
1981 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1982 ctx->odb->path);
1983 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1984 } else {
1985 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1988 if (safe_create_leading_directories(ctx->graph_name)) {
1989 UNLEAK(ctx->graph_name);
1990 error(_("unable to create leading directories of %s"),
1991 ctx->graph_name);
1992 return -1;
1995 if (ctx->split) {
1996 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
1998 hold_lock_file_for_update_mode(&lk, lock_name,
1999 LOCK_DIE_ON_ERROR, 0444);
2000 free(lock_name);
2002 fd = git_mkstemp_mode(ctx->graph_name, 0444);
2003 if (fd < 0) {
2004 error(_("unable to create temporary graph layer"));
2005 return -1;
2008 if (adjust_shared_perm(ctx->graph_name)) {
2009 error(_("unable to adjust shared permissions for '%s'"),
2010 ctx->graph_name);
2011 return -1;
2014 f = hashfd(fd, ctx->graph_name);
2015 } else {
2016 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
2017 LOCK_DIE_ON_ERROR, 0444);
2018 fd = get_lock_file_fd(&lk);
2019 f = hashfd(fd, get_lock_file_path(&lk));
2022 cf = init_chunkfile(f);
2024 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
2025 write_graph_chunk_fanout);
2026 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, st_mult(hashsz, ctx->commits.nr),
2027 write_graph_chunk_oids);
2028 add_chunk(cf, GRAPH_CHUNKID_DATA, st_mult(hashsz + 16, ctx->commits.nr),
2029 write_graph_chunk_data);
2031 if (ctx->write_generation_data)
2032 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
2033 st_mult(sizeof(uint32_t), ctx->commits.nr),
2034 write_graph_chunk_generation_data);
2035 if (ctx->num_generation_data_overflows)
2036 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
2037 st_mult(sizeof(timestamp_t), ctx->num_generation_data_overflows),
2038 write_graph_chunk_generation_data_overflow);
2039 if (ctx->num_extra_edges)
2040 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
2041 st_mult(4, ctx->num_extra_edges),
2042 write_graph_chunk_extra_edges);
2043 if (ctx->changed_paths) {
2044 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
2045 st_mult(sizeof(uint32_t), ctx->commits.nr),
2046 write_graph_chunk_bloom_indexes);
2047 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
2048 st_add(sizeof(uint32_t) * 3,
2049 ctx->total_bloom_filter_data_size),
2050 write_graph_chunk_bloom_data);
2052 if (ctx->num_commit_graphs_after > 1)
2053 add_chunk(cf, GRAPH_CHUNKID_BASE,
2054 st_mult(hashsz, ctx->num_commit_graphs_after - 1),
2055 write_graph_chunk_base);
2057 hashwrite_be32(f, GRAPH_SIGNATURE);
2059 hashwrite_u8(f, GRAPH_VERSION);
2060 hashwrite_u8(f, oid_version(the_hash_algo));
2061 hashwrite_u8(f, get_num_chunks(cf));
2062 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
2064 if (ctx->report_progress) {
2065 strbuf_addf(&progress_title,
2066 Q_("Writing out commit graph in %d pass",
2067 "Writing out commit graph in %d passes",
2068 get_num_chunks(cf)),
2069 get_num_chunks(cf));
2070 ctx->progress = start_delayed_progress(
2071 progress_title.buf,
2072 st_mult(get_num_chunks(cf), ctx->commits.nr));
2075 write_chunkfile(cf, ctx);
2077 stop_progress(&ctx->progress);
2078 strbuf_release(&progress_title);
2080 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
2081 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
2082 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
2084 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
2085 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
2086 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
2087 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
2090 close_commit_graph(ctx->r->objects);
2091 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
2092 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
2093 free_chunkfile(cf);
2095 if (ctx->split) {
2096 FILE *chainf = fdopen_lock_file(&lk, "w");
2097 char *final_graph_name;
2098 int result;
2100 close(fd);
2102 if (!chainf) {
2103 error(_("unable to open commit-graph chain file"));
2104 return -1;
2107 if (ctx->base_graph_name) {
2108 const char *dest;
2109 int idx = ctx->num_commit_graphs_after - 1;
2110 if (ctx->num_commit_graphs_after > 1)
2111 idx--;
2113 dest = ctx->commit_graph_filenames_after[idx];
2115 if (strcmp(ctx->base_graph_name, dest)) {
2116 result = rename(ctx->base_graph_name, dest);
2118 if (result) {
2119 error(_("failed to rename base commit-graph file"));
2120 return -1;
2123 } else {
2124 char *graph_name = get_commit_graph_filename(ctx->odb);
2125 unlink(graph_name);
2126 free(graph_name);
2129 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
2130 final_graph_name = get_split_graph_filename(ctx->odb,
2131 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2132 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
2134 result = rename(ctx->graph_name, final_graph_name);
2136 for (i = 0; i < ctx->num_commit_graphs_after; i++)
2137 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2139 if (result) {
2140 error(_("failed to rename temporary commit-graph file"));
2141 return -1;
2145 commit_lock_file(&lk);
2147 return 0;
2150 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2152 struct commit_graph *g;
2153 uint32_t num_commits;
2154 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2155 uint32_t i;
2157 int max_commits = 0;
2158 int size_mult = 2;
2160 if (ctx->opts) {
2161 max_commits = ctx->opts->max_commits;
2163 if (ctx->opts->size_multiple)
2164 size_mult = ctx->opts->size_multiple;
2166 flags = ctx->opts->split_flags;
2169 g = ctx->r->objects->commit_graph;
2170 num_commits = ctx->commits.nr;
2171 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2172 ctx->num_commit_graphs_after = 1;
2173 else
2174 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2176 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2177 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2178 while (g && (g->num_commits <= st_mult(size_mult, num_commits) ||
2179 (max_commits && num_commits > max_commits))) {
2180 if (g->odb != ctx->odb)
2181 break;
2183 if (unsigned_add_overflows(num_commits, g->num_commits))
2184 die(_("cannot merge graphs with %"PRIuMAX", "
2185 "%"PRIuMAX" commits"),
2186 (uintmax_t)num_commits,
2187 (uintmax_t)g->num_commits);
2188 num_commits += g->num_commits;
2189 g = g->base_graph;
2191 ctx->num_commit_graphs_after--;
2195 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2196 ctx->new_base_graph = g;
2197 else if (ctx->num_commit_graphs_after != 1)
2198 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2199 "should be 1 with --split=replace");
2201 if (ctx->num_commit_graphs_after == 2) {
2202 char *old_graph_name = get_commit_graph_filename(g->odb);
2204 if (!strcmp(g->filename, old_graph_name) &&
2205 g->odb != ctx->odb) {
2206 ctx->num_commit_graphs_after = 1;
2207 ctx->new_base_graph = NULL;
2210 free(old_graph_name);
2213 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2214 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2216 for (i = 0; i < ctx->num_commit_graphs_after &&
2217 i < ctx->num_commit_graphs_before; i++)
2218 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2220 i = ctx->num_commit_graphs_before - 1;
2221 g = ctx->r->objects->commit_graph;
2223 while (g) {
2224 if (i < ctx->num_commit_graphs_after)
2225 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2228 * If the topmost remaining layer has generation data chunk, the
2229 * resultant layer also has generation data chunk.
2231 if (i == ctx->num_commit_graphs_after - 2)
2232 ctx->write_generation_data = !!g->chunk_generation_data;
2234 i--;
2235 g = g->base_graph;
2239 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2240 struct commit_graph *g)
2242 uint32_t i;
2243 uint32_t offset = g->num_commits_in_base;
2245 if (unsigned_add_overflows(ctx->commits.nr, g->num_commits))
2246 die(_("cannot merge graph %s, too many commits: %"PRIuMAX),
2247 oid_to_hex(&g->oid),
2248 (uintmax_t)st_add(ctx->commits.nr, g->num_commits));
2250 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2252 for (i = 0; i < g->num_commits; i++) {
2253 struct object_id oid;
2254 struct commit *result;
2256 display_progress(ctx->progress, i + 1);
2258 load_oid_from_graph(g, i + offset, &oid);
2260 /* only add commits if they still exist in the repo */
2261 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2263 if (result) {
2264 ctx->commits.list[ctx->commits.nr] = result;
2265 ctx->commits.nr++;
2270 static int commit_compare(const void *_a, const void *_b)
2272 const struct commit *a = *(const struct commit **)_a;
2273 const struct commit *b = *(const struct commit **)_b;
2274 return oidcmp(&a->object.oid, &b->object.oid);
2277 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2279 uint32_t i, dedup_i = 0;
2281 if (ctx->report_progress)
2282 ctx->progress = start_delayed_progress(
2283 _("Scanning merged commits"),
2284 ctx->commits.nr);
2286 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2288 ctx->num_extra_edges = 0;
2289 for (i = 0; i < ctx->commits.nr; i++) {
2290 display_progress(ctx->progress, i + 1);
2292 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2293 &ctx->commits.list[i]->object.oid)) {
2295 * Silently ignore duplicates. These were likely
2296 * created due to a commit appearing in multiple
2297 * layers of the chain, which is unexpected but
2298 * not invalid. We should make sure there is a
2299 * unique copy in the new layer.
2301 } else {
2302 unsigned int num_parents;
2304 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2305 dedup_i++;
2307 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2308 if (num_parents > 2)
2309 ctx->num_extra_edges += num_parents - 1;
2313 ctx->commits.nr = dedup_i;
2315 stop_progress(&ctx->progress);
2318 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2320 struct commit_graph *g = ctx->r->objects->commit_graph;
2321 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2323 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2324 current_graph_number--;
2326 if (ctx->report_progress)
2327 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2329 merge_commit_graph(ctx, g);
2330 stop_progress(&ctx->progress);
2332 g = g->base_graph;
2335 if (g) {
2336 ctx->new_base_graph = g;
2337 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2340 if (ctx->new_base_graph)
2341 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2343 sort_and_scan_merged_commits(ctx);
2346 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2348 uint32_t i;
2349 time_t now = time(NULL);
2351 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2352 struct stat st;
2353 struct utimbuf updated_time;
2355 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2356 continue;
2358 updated_time.actime = st.st_atime;
2359 updated_time.modtime = now;
2360 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2364 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2366 struct strbuf path = STRBUF_INIT;
2367 DIR *dir;
2368 struct dirent *de;
2369 size_t dirnamelen;
2370 timestamp_t expire_time = time(NULL);
2372 if (ctx->opts && ctx->opts->expire_time)
2373 expire_time = ctx->opts->expire_time;
2374 if (!ctx->split) {
2375 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2376 unlink(chain_file_name);
2377 free(chain_file_name);
2378 ctx->num_commit_graphs_after = 0;
2381 strbuf_addstr(&path, ctx->odb->path);
2382 strbuf_addstr(&path, "/info/commit-graphs");
2383 dir = opendir(path.buf);
2385 if (!dir)
2386 goto out;
2388 strbuf_addch(&path, '/');
2389 dirnamelen = path.len;
2390 while ((de = readdir(dir)) != NULL) {
2391 struct stat st;
2392 uint32_t i, found = 0;
2394 strbuf_setlen(&path, dirnamelen);
2395 strbuf_addstr(&path, de->d_name);
2397 if (stat(path.buf, &st) < 0)
2398 continue;
2400 if (st.st_mtime > expire_time)
2401 continue;
2402 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2403 continue;
2405 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2406 if (!strcmp(ctx->commit_graph_filenames_after[i],
2407 path.buf)) {
2408 found = 1;
2409 break;
2413 if (!found)
2414 unlink(path.buf);
2417 out:
2418 if(dir)
2419 closedir(dir);
2420 strbuf_release(&path);
2423 int write_commit_graph(struct object_directory *odb,
2424 const struct string_list *const pack_indexes,
2425 struct oidset *commits,
2426 enum commit_graph_write_flags flags,
2427 const struct commit_graph_opts *opts)
2429 struct repository *r = the_repository;
2430 struct write_commit_graph_context *ctx;
2431 uint32_t i;
2432 int res = 0;
2433 int replace = 0;
2434 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2435 struct topo_level_slab topo_levels;
2437 prepare_repo_settings(r);
2438 if (!r->settings.core_commit_graph) {
2439 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2440 return 0;
2442 if (!commit_graph_compatible(r))
2443 return 0;
2445 CALLOC_ARRAY(ctx, 1);
2446 ctx->r = r;
2447 ctx->odb = odb;
2448 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2449 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2450 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2451 ctx->opts = opts;
2452 ctx->total_bloom_filter_data_size = 0;
2453 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2454 ctx->num_generation_data_overflows = 0;
2456 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2457 bloom_settings.bits_per_entry);
2458 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2459 bloom_settings.num_hashes);
2460 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2461 bloom_settings.max_changed_paths);
2462 ctx->bloom_settings = &bloom_settings;
2464 init_topo_level_slab(&topo_levels);
2465 ctx->topo_levels = &topo_levels;
2467 prepare_commit_graph(ctx->r);
2468 if (ctx->r->objects->commit_graph) {
2469 struct commit_graph *g = ctx->r->objects->commit_graph;
2471 while (g) {
2472 g->topo_levels = &topo_levels;
2473 g = g->base_graph;
2477 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2478 ctx->changed_paths = 1;
2479 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2480 struct commit_graph *g;
2482 g = ctx->r->objects->commit_graph;
2484 /* We have changed-paths already. Keep them in the next graph */
2485 if (g && g->chunk_bloom_data) {
2486 ctx->changed_paths = 1;
2487 ctx->bloom_settings = g->bloom_filter_settings;
2491 if (ctx->split) {
2492 struct commit_graph *g = ctx->r->objects->commit_graph;
2494 while (g) {
2495 ctx->num_commit_graphs_before++;
2496 g = g->base_graph;
2499 if (ctx->num_commit_graphs_before) {
2500 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2501 i = ctx->num_commit_graphs_before;
2502 g = ctx->r->objects->commit_graph;
2504 while (g) {
2505 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2506 g = g->base_graph;
2510 if (ctx->opts)
2511 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2514 ctx->approx_nr_objects = repo_approximate_object_count(the_repository);
2516 if (ctx->append && ctx->r->objects->commit_graph) {
2517 struct commit_graph *g = ctx->r->objects->commit_graph;
2518 for (i = 0; i < g->num_commits; i++) {
2519 struct object_id oid;
2520 oidread(&oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2521 oid_array_append(&ctx->oids, &oid);
2525 if (pack_indexes) {
2526 ctx->order_by_pack = 1;
2527 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2528 goto cleanup;
2531 if (commits) {
2532 if ((res = fill_oids_from_commits(ctx, commits)))
2533 goto cleanup;
2536 if (!pack_indexes && !commits) {
2537 ctx->order_by_pack = 1;
2538 fill_oids_from_all_packs(ctx);
2541 close_reachable(ctx);
2543 copy_oids_to_commits(ctx);
2545 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2546 error(_("too many commits to write graph"));
2547 res = -1;
2548 goto cleanup;
2551 if (!ctx->commits.nr && !replace)
2552 goto cleanup;
2554 if (ctx->split) {
2555 split_graph_merge_strategy(ctx);
2557 if (!replace)
2558 merge_commit_graphs(ctx);
2559 } else
2560 ctx->num_commit_graphs_after = 1;
2562 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2564 compute_topological_levels(ctx);
2565 if (ctx->write_generation_data)
2566 compute_generation_numbers(ctx);
2568 if (ctx->changed_paths)
2569 compute_bloom_filters(ctx);
2571 res = write_commit_graph_file(ctx);
2573 if (ctx->split)
2574 mark_commit_graphs(ctx);
2576 expire_commit_graphs(ctx);
2578 cleanup:
2579 free(ctx->graph_name);
2580 free(ctx->commits.list);
2581 oid_array_clear(&ctx->oids);
2582 clear_topo_level_slab(&topo_levels);
2584 if (ctx->commit_graph_filenames_after) {
2585 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2586 free(ctx->commit_graph_filenames_after[i]);
2587 free(ctx->commit_graph_hash_after[i]);
2590 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2591 free(ctx->commit_graph_filenames_before[i]);
2593 free(ctx->commit_graph_filenames_after);
2594 free(ctx->commit_graph_filenames_before);
2595 free(ctx->commit_graph_hash_after);
2598 free(ctx);
2600 return res;
2603 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2604 static int verify_commit_graph_error;
2606 __attribute__((format (printf, 1, 2)))
2607 static void graph_report(const char *fmt, ...)
2609 va_list ap;
2611 verify_commit_graph_error = 1;
2612 va_start(ap, fmt);
2613 vfprintf(stderr, fmt, ap);
2614 fprintf(stderr, "\n");
2615 va_end(ap);
2618 static int commit_graph_checksum_valid(struct commit_graph *g)
2620 return hashfile_checksum_valid(g->data, g->data_len);
2623 static int verify_one_commit_graph(struct repository *r,
2624 struct commit_graph *g,
2625 struct progress *progress,
2626 uint64_t *seen)
2628 uint32_t i, cur_fanout_pos = 0;
2629 struct object_id prev_oid, cur_oid;
2630 struct commit *seen_gen_zero = NULL;
2631 struct commit *seen_gen_non_zero = NULL;
2633 verify_commit_graph_error = verify_commit_graph_lite(g);
2634 if (verify_commit_graph_error)
2635 return verify_commit_graph_error;
2637 if (!commit_graph_checksum_valid(g)) {
2638 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2639 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2642 for (i = 0; i < g->num_commits; i++) {
2643 struct commit *graph_commit;
2645 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2647 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2648 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2649 oid_to_hex(&prev_oid),
2650 oid_to_hex(&cur_oid));
2652 oidcpy(&prev_oid, &cur_oid);
2654 while (cur_oid.hash[0] > cur_fanout_pos) {
2655 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2657 if (i != fanout_value)
2658 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2659 cur_fanout_pos, fanout_value, i);
2660 cur_fanout_pos++;
2663 graph_commit = lookup_commit(r, &cur_oid);
2664 if (!parse_commit_in_graph_one(r, g, graph_commit))
2665 graph_report(_("failed to parse commit %s from commit-graph"),
2666 oid_to_hex(&cur_oid));
2669 while (cur_fanout_pos < 256) {
2670 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2672 if (g->num_commits != fanout_value)
2673 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2674 cur_fanout_pos, fanout_value, i);
2676 cur_fanout_pos++;
2679 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2680 return verify_commit_graph_error;
2682 for (i = 0; i < g->num_commits; i++) {
2683 struct commit *graph_commit, *odb_commit;
2684 struct commit_list *graph_parents, *odb_parents;
2685 timestamp_t max_generation = 0;
2686 timestamp_t generation;
2688 display_progress(progress, ++(*seen));
2689 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2691 graph_commit = lookup_commit(r, &cur_oid);
2692 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2693 if (repo_parse_commit_internal(r, odb_commit, 0, 0)) {
2694 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2695 oid_to_hex(&cur_oid));
2696 continue;
2699 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2700 get_commit_tree_oid(odb_commit)))
2701 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2702 oid_to_hex(&cur_oid),
2703 oid_to_hex(get_commit_tree_oid(graph_commit)),
2704 oid_to_hex(get_commit_tree_oid(odb_commit)));
2706 graph_parents = graph_commit->parents;
2707 odb_parents = odb_commit->parents;
2709 while (graph_parents) {
2710 if (!odb_parents) {
2711 graph_report(_("commit-graph parent list for commit %s is too long"),
2712 oid_to_hex(&cur_oid));
2713 break;
2716 /* parse parent in case it is in a base graph */
2717 parse_commit_in_graph_one(r, g, graph_parents->item);
2719 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2720 graph_report(_("commit-graph parent for %s is %s != %s"),
2721 oid_to_hex(&cur_oid),
2722 oid_to_hex(&graph_parents->item->object.oid),
2723 oid_to_hex(&odb_parents->item->object.oid));
2725 generation = commit_graph_generation_from_graph(graph_parents->item);
2726 if (generation > max_generation)
2727 max_generation = generation;
2729 graph_parents = graph_parents->next;
2730 odb_parents = odb_parents->next;
2733 if (odb_parents)
2734 graph_report(_("commit-graph parent list for commit %s terminates early"),
2735 oid_to_hex(&cur_oid));
2737 if (commit_graph_generation_from_graph(graph_commit))
2738 seen_gen_non_zero = graph_commit;
2739 else
2740 seen_gen_zero = graph_commit;
2742 if (seen_gen_zero)
2743 continue;
2746 * If we are using topological level and one of our parents has
2747 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2748 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2749 * in the following condition.
2751 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2752 max_generation--;
2754 generation = commit_graph_generation(graph_commit);
2755 if (generation < max_generation + 1)
2756 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2757 oid_to_hex(&cur_oid),
2758 generation,
2759 max_generation + 1);
2761 if (graph_commit->date != odb_commit->date)
2762 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2763 oid_to_hex(&cur_oid),
2764 graph_commit->date,
2765 odb_commit->date);
2768 if (seen_gen_zero && seen_gen_non_zero)
2769 graph_report(_("commit-graph has both zero and non-zero "
2770 "generations (e.g., commits '%s' and '%s')"),
2771 oid_to_hex(&seen_gen_zero->object.oid),
2772 oid_to_hex(&seen_gen_non_zero->object.oid));
2774 return verify_commit_graph_error;
2777 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2779 struct progress *progress = NULL;
2780 int local_error = 0;
2781 uint64_t seen = 0;
2783 if (!g) {
2784 graph_report("no commit-graph file loaded");
2785 return 1;
2788 if (flags & COMMIT_GRAPH_WRITE_PROGRESS) {
2789 uint64_t total = g->num_commits;
2790 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW))
2791 total += g->num_commits_in_base;
2793 progress = start_progress(_("Verifying commits in commit graph"),
2794 total);
2797 for (; g; g = g->base_graph) {
2798 local_error |= verify_one_commit_graph(r, g, progress, &seen);
2799 if (flags & COMMIT_GRAPH_VERIFY_SHALLOW)
2800 break;
2803 stop_progress(&progress);
2805 return local_error;
2808 void free_commit_graph(struct commit_graph *g)
2810 if (!g)
2811 return;
2812 if (g->data) {
2813 munmap((void *)g->data, g->data_len);
2814 g->data = NULL;
2816 free(g->filename);
2817 free(g->bloom_filter_settings);
2818 free(g);
2821 void disable_commit_graph(struct repository *r)
2823 r->commit_graph_disabled = 1;