statinfo: move stat_{data,validity} functions from cache/read-cache
[git.git] / commit-graph.c
blob843bdb458d02fbd2aa4721047aa376a1fe25891f
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.h"
16 #include "oid-array.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"
28 #include "wrapper.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 struct commit_graph_data *commit_graph_data_at(const struct commit *c)
133 unsigned int i, nth_slab;
134 struct commit_graph_data *data =
135 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
137 if (data)
138 return data;
140 nth_slab = c->index / commit_graph_data_slab.slab_size;
141 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
144 * commit-slab initializes elements with zero, overwrite this with
145 * COMMIT_NOT_FROM_GRAPH for graph_pos.
147 * We avoid initializing generation with checking if graph position
148 * is not COMMIT_NOT_FROM_GRAPH.
150 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
151 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
152 COMMIT_NOT_FROM_GRAPH;
155 return data;
159 * Should be used only while writing commit-graph as it compares
160 * generation value of commits by directly accessing commit-slab.
162 static int commit_gen_cmp(const void *va, const void *vb)
164 const struct commit *a = *(const struct commit **)va;
165 const struct commit *b = *(const struct commit **)vb;
167 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
168 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
169 /* lower generation commits first */
170 if (generation_a < generation_b)
171 return -1;
172 else if (generation_a > generation_b)
173 return 1;
175 /* use date as a heuristic when generations are equal */
176 if (a->date < b->date)
177 return -1;
178 else if (a->date > b->date)
179 return 1;
180 return 0;
183 char *get_commit_graph_filename(struct object_directory *obj_dir)
185 return xstrfmt("%s/info/commit-graph", obj_dir->path);
188 static char *get_split_graph_filename(struct object_directory *odb,
189 const char *oid_hex)
191 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
192 oid_hex);
195 char *get_commit_graph_chain_filename(struct object_directory *odb)
197 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
200 static struct commit_graph *alloc_commit_graph(void)
202 struct commit_graph *g = xcalloc(1, sizeof(*g));
204 return g;
207 extern int read_replace_refs;
209 static int commit_graph_compatible(struct repository *r)
211 if (!r->gitdir)
212 return 0;
214 if (read_replace_refs) {
215 prepare_replace_object(r);
216 if (hashmap_get_size(&r->objects->replace_map->map))
217 return 0;
220 prepare_commit_graft(r);
221 if (r->parsed_objects &&
222 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
223 return 0;
224 if (is_repository_shallow(r))
225 return 0;
227 return 1;
230 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
232 *fd = git_open(graph_file);
233 if (*fd < 0)
234 return 0;
235 if (fstat(*fd, st)) {
236 close(*fd);
237 return 0;
239 return 1;
242 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
243 int fd, struct stat *st,
244 struct object_directory *odb)
246 void *graph_map;
247 size_t graph_size;
248 struct commit_graph *ret;
250 graph_size = xsize_t(st->st_size);
252 if (graph_size < GRAPH_MIN_SIZE) {
253 close(fd);
254 error(_("commit-graph file is too small"));
255 return NULL;
257 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
258 close(fd);
259 prepare_repo_settings(r);
260 ret = parse_commit_graph(&r->settings, graph_map, graph_size);
262 if (ret)
263 ret->odb = odb;
264 else
265 munmap(graph_map, graph_size);
267 return ret;
270 static int verify_commit_graph_lite(struct commit_graph *g)
273 * Basic validation shared between parse_commit_graph()
274 * which'll be called every time the graph is used, and the
275 * much more expensive verify_commit_graph() used by
276 * "commit-graph verify".
278 * There should only be very basic checks here to ensure that
279 * we don't e.g. segfault in fill_commit_in_graph(), but
280 * because this is a very hot codepath nothing that e.g. loops
281 * over g->num_commits, or runs a checksum on the commit-graph
282 * itself.
284 if (!g->chunk_oid_fanout) {
285 error("commit-graph is missing the OID Fanout chunk");
286 return 1;
288 if (!g->chunk_oid_lookup) {
289 error("commit-graph is missing the OID Lookup chunk");
290 return 1;
292 if (!g->chunk_commit_data) {
293 error("commit-graph is missing the Commit Data chunk");
294 return 1;
297 return 0;
300 static int graph_read_oid_lookup(const unsigned char *chunk_start,
301 size_t chunk_size, void *data)
303 struct commit_graph *g = data;
304 g->chunk_oid_lookup = chunk_start;
305 g->num_commits = chunk_size / g->hash_len;
306 return 0;
309 static int graph_read_bloom_data(const unsigned char *chunk_start,
310 size_t chunk_size, void *data)
312 struct commit_graph *g = data;
313 uint32_t hash_version;
314 g->chunk_bloom_data = chunk_start;
315 hash_version = get_be32(chunk_start);
317 if (hash_version != 1)
318 return 0;
320 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
321 g->bloom_filter_settings->hash_version = hash_version;
322 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
323 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
324 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
326 return 0;
329 struct commit_graph *parse_commit_graph(struct repo_settings *s,
330 void *graph_map, size_t graph_size)
332 const unsigned char *data;
333 struct commit_graph *graph;
334 uint32_t graph_signature;
335 unsigned char graph_version, hash_version;
336 struct chunkfile *cf = NULL;
338 if (!graph_map)
339 return NULL;
341 if (graph_size < GRAPH_MIN_SIZE)
342 return NULL;
344 data = (const unsigned char *)graph_map;
346 graph_signature = get_be32(data);
347 if (graph_signature != GRAPH_SIGNATURE) {
348 error(_("commit-graph signature %X does not match signature %X"),
349 graph_signature, GRAPH_SIGNATURE);
350 return NULL;
353 graph_version = *(unsigned char*)(data + 4);
354 if (graph_version != GRAPH_VERSION) {
355 error(_("commit-graph version %X does not match version %X"),
356 graph_version, GRAPH_VERSION);
357 return NULL;
360 hash_version = *(unsigned char*)(data + 5);
361 if (hash_version != oid_version(the_hash_algo)) {
362 error(_("commit-graph hash version %X does not match version %X"),
363 hash_version, oid_version(the_hash_algo));
364 return NULL;
367 graph = alloc_commit_graph();
369 graph->hash_len = the_hash_algo->rawsz;
370 graph->num_chunks = *(unsigned char*)(data + 6);
371 graph->data = graph_map;
372 graph->data_len = graph_size;
374 if (graph_size < GRAPH_HEADER_SIZE +
375 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
376 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
377 error(_("commit-graph file is too small to hold %u chunks"),
378 graph->num_chunks);
379 free(graph);
380 return NULL;
383 cf = init_chunkfile(NULL);
385 if (read_table_of_contents(cf, graph->data, graph_size,
386 GRAPH_HEADER_SIZE, graph->num_chunks))
387 goto free_and_return;
389 pair_chunk(cf, GRAPH_CHUNKID_OIDFANOUT,
390 (const unsigned char **)&graph->chunk_oid_fanout);
391 read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph);
392 pair_chunk(cf, GRAPH_CHUNKID_DATA, &graph->chunk_commit_data);
393 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges);
394 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs);
396 if (s->commit_graph_generation_version >= 2) {
397 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
398 &graph->chunk_generation_data);
399 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
400 &graph->chunk_generation_data_overflow);
402 if (graph->chunk_generation_data)
403 graph->read_generation_data = 1;
406 if (s->commit_graph_read_changed_paths) {
407 pair_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
408 &graph->chunk_bloom_indexes);
409 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
410 graph_read_bloom_data, graph);
413 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
414 init_bloom_filters();
415 } else {
416 /* We need both the bloom chunks to exist together. Else ignore the data */
417 graph->chunk_bloom_indexes = NULL;
418 graph->chunk_bloom_data = NULL;
419 FREE_AND_NULL(graph->bloom_filter_settings);
422 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
424 if (verify_commit_graph_lite(graph))
425 goto free_and_return;
427 free_chunkfile(cf);
428 return graph;
430 free_and_return:
431 free_chunkfile(cf);
432 free(graph->bloom_filter_settings);
433 free(graph);
434 return NULL;
437 static struct commit_graph *load_commit_graph_one(struct repository *r,
438 const char *graph_file,
439 struct object_directory *odb)
442 struct stat st;
443 int fd;
444 struct commit_graph *g;
445 int open_ok = open_commit_graph(graph_file, &fd, &st);
447 if (!open_ok)
448 return NULL;
450 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
452 if (g)
453 g->filename = xstrdup(graph_file);
455 return g;
458 static struct commit_graph *load_commit_graph_v1(struct repository *r,
459 struct object_directory *odb)
461 char *graph_name = get_commit_graph_filename(odb);
462 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
463 free(graph_name);
465 return g;
468 static int add_graph_to_chain(struct commit_graph *g,
469 struct commit_graph *chain,
470 struct object_id *oids,
471 int n)
473 struct commit_graph *cur_g = chain;
475 if (n && !g->chunk_base_graphs) {
476 warning(_("commit-graph has no base graphs chunk"));
477 return 0;
480 while (n) {
481 n--;
483 if (!cur_g ||
484 !oideq(&oids[n], &cur_g->oid) ||
485 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
486 warning(_("commit-graph chain does not match"));
487 return 0;
490 cur_g = cur_g->base_graph;
493 g->base_graph = chain;
495 if (chain)
496 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
498 return 1;
501 static struct commit_graph *load_commit_graph_chain(struct repository *r,
502 struct object_directory *odb)
504 struct commit_graph *graph_chain = NULL;
505 struct strbuf line = STRBUF_INIT;
506 struct stat st;
507 struct object_id *oids;
508 int i = 0, valid = 1, count;
509 char *chain_name = get_commit_graph_chain_filename(odb);
510 FILE *fp;
511 int stat_res;
513 fp = fopen(chain_name, "r");
514 stat_res = stat(chain_name, &st);
515 free(chain_name);
517 if (!fp)
518 return NULL;
519 if (stat_res ||
520 st.st_size <= the_hash_algo->hexsz) {
521 fclose(fp);
522 return NULL;
525 count = st.st_size / (the_hash_algo->hexsz + 1);
526 CALLOC_ARRAY(oids, count);
528 prepare_alt_odb(r);
530 for (i = 0; i < count; i++) {
531 struct object_directory *odb;
533 if (strbuf_getline_lf(&line, fp) == EOF)
534 break;
536 if (get_oid_hex(line.buf, &oids[i])) {
537 warning(_("invalid commit-graph chain: line '%s' not a hash"),
538 line.buf);
539 valid = 0;
540 break;
543 valid = 0;
544 for (odb = r->objects->odb; odb; odb = odb->next) {
545 char *graph_name = get_split_graph_filename(odb, line.buf);
546 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
548 free(graph_name);
550 if (g) {
551 if (add_graph_to_chain(g, graph_chain, oids, i)) {
552 graph_chain = g;
553 valid = 1;
556 break;
560 if (!valid) {
561 warning(_("unable to find all commit-graph files"));
562 break;
566 free(oids);
567 fclose(fp);
568 strbuf_release(&line);
570 return graph_chain;
574 * returns 1 if and only if all graphs in the chain have
575 * corrected commit dates stored in the generation_data chunk.
577 static int validate_mixed_generation_chain(struct commit_graph *g)
579 int read_generation_data = 1;
580 struct commit_graph *p = g;
582 while (read_generation_data && p) {
583 read_generation_data = p->read_generation_data;
584 p = p->base_graph;
587 if (read_generation_data)
588 return 1;
590 while (g) {
591 g->read_generation_data = 0;
592 g = g->base_graph;
595 return 0;
598 struct commit_graph *read_commit_graph_one(struct repository *r,
599 struct object_directory *odb)
601 struct commit_graph *g = load_commit_graph_v1(r, odb);
603 if (!g)
604 g = load_commit_graph_chain(r, odb);
606 validate_mixed_generation_chain(g);
608 return g;
611 static void prepare_commit_graph_one(struct repository *r,
612 struct object_directory *odb)
615 if (r->objects->commit_graph)
616 return;
618 r->objects->commit_graph = read_commit_graph_one(r, odb);
622 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
624 * On the first invocation, this function attempts to load the commit
625 * graph if the_repository is configured to have one.
627 static int prepare_commit_graph(struct repository *r)
629 struct object_directory *odb;
632 * Early return if there is no git dir or if the commit graph is
633 * disabled.
635 * This must come before the "already attempted?" check below, because
636 * we want to disable even an already-loaded graph file.
638 if (!r->gitdir || r->commit_graph_disabled)
639 return 0;
641 if (r->objects->commit_graph_attempted)
642 return !!r->objects->commit_graph;
643 r->objects->commit_graph_attempted = 1;
645 prepare_repo_settings(r);
647 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
648 r->settings.core_commit_graph != 1)
650 * This repository is not configured to use commit graphs, so
651 * do not load one. (But report commit_graph_attempted anyway
652 * so that commit graph loading is not attempted again for this
653 * repository.)
655 return 0;
657 if (!commit_graph_compatible(r))
658 return 0;
660 prepare_alt_odb(r);
661 for (odb = r->objects->odb;
662 !r->objects->commit_graph && odb;
663 odb = odb->next)
664 prepare_commit_graph_one(r, odb);
665 return !!r->objects->commit_graph;
668 int generation_numbers_enabled(struct repository *r)
670 uint32_t first_generation;
671 struct commit_graph *g;
672 if (!prepare_commit_graph(r))
673 return 0;
675 g = r->objects->commit_graph;
677 if (!g->num_commits)
678 return 0;
680 first_generation = get_be32(g->chunk_commit_data +
681 g->hash_len + 8) >> 2;
683 return !!first_generation;
686 int corrected_commit_dates_enabled(struct repository *r)
688 struct commit_graph *g;
689 if (!prepare_commit_graph(r))
690 return 0;
692 g = r->objects->commit_graph;
694 if (!g->num_commits)
695 return 0;
697 return g->read_generation_data;
700 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
702 struct commit_graph *g = r->objects->commit_graph;
703 while (g) {
704 if (g->bloom_filter_settings)
705 return g->bloom_filter_settings;
706 g = g->base_graph;
708 return NULL;
711 static void close_commit_graph_one(struct commit_graph *g)
713 if (!g)
714 return;
716 clear_commit_graph_data_slab(&commit_graph_data_slab);
717 close_commit_graph_one(g->base_graph);
718 free_commit_graph(g);
721 void close_commit_graph(struct raw_object_store *o)
723 close_commit_graph_one(o->commit_graph);
724 o->commit_graph = NULL;
727 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
729 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
730 g->chunk_oid_lookup, g->hash_len, pos);
733 static void load_oid_from_graph(struct commit_graph *g,
734 uint32_t pos,
735 struct object_id *oid)
737 uint32_t lex_index;
739 while (g && pos < g->num_commits_in_base)
740 g = g->base_graph;
742 if (!g)
743 BUG("NULL commit-graph");
745 if (pos >= g->num_commits + g->num_commits_in_base)
746 die(_("invalid commit position. commit-graph is likely corrupt"));
748 lex_index = pos - g->num_commits_in_base;
750 oidread(oid, g->chunk_oid_lookup + g->hash_len * lex_index);
753 static struct commit_list **insert_parent_or_die(struct repository *r,
754 struct commit_graph *g,
755 uint32_t pos,
756 struct commit_list **pptr)
758 struct commit *c;
759 struct object_id oid;
761 if (pos >= g->num_commits + g->num_commits_in_base)
762 die("invalid parent position %"PRIu32, pos);
764 load_oid_from_graph(g, pos, &oid);
765 c = lookup_commit(r, &oid);
766 if (!c)
767 die(_("could not find commit %s"), oid_to_hex(&oid));
768 commit_graph_data_at(c)->graph_pos = pos;
769 return &commit_list_insert(c, pptr)->next;
772 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
774 const unsigned char *commit_data;
775 struct commit_graph_data *graph_data;
776 uint32_t lex_index, offset_pos;
777 uint64_t date_high, date_low, offset;
779 while (pos < g->num_commits_in_base)
780 g = g->base_graph;
782 if (pos >= g->num_commits + g->num_commits_in_base)
783 die(_("invalid commit position. commit-graph is likely corrupt"));
785 lex_index = pos - g->num_commits_in_base;
786 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
788 graph_data = commit_graph_data_at(item);
789 graph_data->graph_pos = pos;
791 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
792 date_low = get_be32(commit_data + g->hash_len + 12);
793 item->date = (timestamp_t)((date_high << 32) | date_low);
795 if (g->read_generation_data) {
796 offset = (timestamp_t)get_be32(g->chunk_generation_data + sizeof(uint32_t) * lex_index);
798 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
799 if (!g->chunk_generation_data_overflow)
800 die(_("commit-graph requires overflow generation data but has none"));
802 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
803 graph_data->generation = item->date + get_be64(g->chunk_generation_data_overflow + 8 * offset_pos);
804 } else
805 graph_data->generation = item->date + offset;
806 } else
807 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
809 if (g->topo_levels)
810 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
813 static inline void set_commit_tree(struct commit *c, struct tree *t)
815 c->maybe_tree = t;
818 static int fill_commit_in_graph(struct repository *r,
819 struct commit *item,
820 struct commit_graph *g, uint32_t pos)
822 uint32_t edge_value;
823 uint32_t *parent_data_ptr;
824 struct commit_list **pptr;
825 const unsigned char *commit_data;
826 uint32_t lex_index;
828 while (pos < g->num_commits_in_base)
829 g = g->base_graph;
831 fill_commit_graph_info(item, g, pos);
833 lex_index = pos - g->num_commits_in_base;
834 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
836 item->object.parsed = 1;
838 set_commit_tree(item, NULL);
840 pptr = &item->parents;
842 edge_value = get_be32(commit_data + g->hash_len);
843 if (edge_value == GRAPH_PARENT_NONE)
844 return 1;
845 pptr = insert_parent_or_die(r, g, edge_value, pptr);
847 edge_value = get_be32(commit_data + g->hash_len + 4);
848 if (edge_value == GRAPH_PARENT_NONE)
849 return 1;
850 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
851 pptr = insert_parent_or_die(r, g, edge_value, pptr);
852 return 1;
855 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
856 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
857 do {
858 edge_value = get_be32(parent_data_ptr);
859 pptr = insert_parent_or_die(r, g,
860 edge_value & GRAPH_EDGE_LAST_MASK,
861 pptr);
862 parent_data_ptr++;
863 } while (!(edge_value & GRAPH_LAST_EDGE));
865 return 1;
868 static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
870 struct commit_graph *cur_g = g;
871 uint32_t lex_index;
873 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
874 cur_g = cur_g->base_graph;
876 if (cur_g) {
877 *pos = lex_index + cur_g->num_commits_in_base;
878 return 1;
881 return 0;
884 static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
886 uint32_t graph_pos = commit_graph_position(item);
887 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
888 *pos = graph_pos;
889 return 1;
890 } else {
891 return search_commit_pos_in_graph(&item->object.oid, g, pos);
895 int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
896 uint32_t *pos)
898 if (!prepare_commit_graph(r))
899 return 0;
900 return find_commit_pos_in_graph(c, r->objects->commit_graph, pos);
903 struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
905 struct commit *commit;
906 uint32_t pos;
908 if (!prepare_commit_graph(repo))
909 return NULL;
910 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
911 return NULL;
912 if (!has_object(repo, id, 0))
913 return NULL;
915 commit = lookup_commit(repo, id);
916 if (!commit)
917 return NULL;
918 if (commit->object.parsed)
919 return commit;
921 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
922 return NULL;
924 return commit;
927 static int parse_commit_in_graph_one(struct repository *r,
928 struct commit_graph *g,
929 struct commit *item)
931 uint32_t pos;
933 if (item->object.parsed)
934 return 1;
936 if (find_commit_pos_in_graph(item, g, &pos))
937 return fill_commit_in_graph(r, item, g, pos);
939 return 0;
942 int parse_commit_in_graph(struct repository *r, struct commit *item)
944 static int checked_env = 0;
946 if (!checked_env &&
947 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
948 die("dying as requested by the '%s' variable on commit-graph parse!",
949 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
950 checked_env = 1;
952 if (!prepare_commit_graph(r))
953 return 0;
954 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
957 void load_commit_graph_info(struct repository *r, struct commit *item)
959 uint32_t pos;
960 if (repo_find_commit_pos_in_graph(r, item, &pos))
961 fill_commit_graph_info(item, r->objects->commit_graph, pos);
964 static struct tree *load_tree_for_commit(struct repository *r,
965 struct commit_graph *g,
966 struct commit *c)
968 struct object_id oid;
969 const unsigned char *commit_data;
970 uint32_t graph_pos = commit_graph_position(c);
972 while (graph_pos < g->num_commits_in_base)
973 g = g->base_graph;
975 commit_data = g->chunk_commit_data +
976 GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base);
978 oidread(&oid, commit_data);
979 set_commit_tree(c, lookup_tree(r, &oid));
981 return c->maybe_tree;
984 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
985 struct commit_graph *g,
986 const struct commit *c)
988 if (c->maybe_tree)
989 return c->maybe_tree;
990 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
991 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
993 return load_tree_for_commit(r, g, (struct commit *)c);
996 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
998 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
1001 struct packed_commit_list {
1002 struct commit **list;
1003 size_t nr;
1004 size_t alloc;
1007 struct write_commit_graph_context {
1008 struct repository *r;
1009 struct object_directory *odb;
1010 char *graph_name;
1011 struct oid_array oids;
1012 struct packed_commit_list commits;
1013 int num_extra_edges;
1014 int num_generation_data_overflows;
1015 unsigned long approx_nr_objects;
1016 struct progress *progress;
1017 int progress_done;
1018 uint64_t progress_cnt;
1020 char *base_graph_name;
1021 int num_commit_graphs_before;
1022 int num_commit_graphs_after;
1023 char **commit_graph_filenames_before;
1024 char **commit_graph_filenames_after;
1025 char **commit_graph_hash_after;
1026 uint32_t new_num_commits_in_base;
1027 struct commit_graph *new_base_graph;
1029 unsigned append:1,
1030 report_progress:1,
1031 split:1,
1032 changed_paths:1,
1033 order_by_pack:1,
1034 write_generation_data:1,
1035 trust_generation_numbers:1;
1037 struct topo_level_slab *topo_levels;
1038 const struct commit_graph_opts *opts;
1039 size_t total_bloom_filter_data_size;
1040 const struct bloom_filter_settings *bloom_settings;
1042 int count_bloom_filter_computed;
1043 int count_bloom_filter_not_computed;
1044 int count_bloom_filter_trunc_empty;
1045 int count_bloom_filter_trunc_large;
1048 static int write_graph_chunk_fanout(struct hashfile *f,
1049 void *data)
1051 struct write_commit_graph_context *ctx = data;
1052 int i, count = 0;
1053 struct commit **list = ctx->commits.list;
1056 * Write the first-level table (the list is sorted,
1057 * but we use a 256-entry lookup to be able to avoid
1058 * having to do eight extra binary search iterations).
1060 for (i = 0; i < 256; i++) {
1061 while (count < ctx->commits.nr) {
1062 if ((*list)->object.oid.hash[0] != i)
1063 break;
1064 display_progress(ctx->progress, ++ctx->progress_cnt);
1065 count++;
1066 list++;
1069 hashwrite_be32(f, count);
1072 return 0;
1075 static int write_graph_chunk_oids(struct hashfile *f,
1076 void *data)
1078 struct write_commit_graph_context *ctx = data;
1079 struct commit **list = ctx->commits.list;
1080 int count;
1081 for (count = 0; count < ctx->commits.nr; count++, list++) {
1082 display_progress(ctx->progress, ++ctx->progress_cnt);
1083 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1086 return 0;
1089 static const struct object_id *commit_to_oid(size_t index, const void *table)
1091 const struct commit * const *commits = table;
1092 return &commits[index]->object.oid;
1095 static int write_graph_chunk_data(struct hashfile *f,
1096 void *data)
1098 struct write_commit_graph_context *ctx = data;
1099 struct commit **list = ctx->commits.list;
1100 struct commit **last = ctx->commits.list + ctx->commits.nr;
1101 uint32_t num_extra_edges = 0;
1103 while (list < last) {
1104 struct commit_list *parent;
1105 struct object_id *tree;
1106 int edge_value;
1107 uint32_t packedDate[2];
1108 display_progress(ctx->progress, ++ctx->progress_cnt);
1110 if (repo_parse_commit_no_graph(ctx->r, *list))
1111 die(_("unable to parse commit %s"),
1112 oid_to_hex(&(*list)->object.oid));
1113 tree = get_commit_tree_oid(*list);
1114 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1116 parent = (*list)->parents;
1118 if (!parent)
1119 edge_value = GRAPH_PARENT_NONE;
1120 else {
1121 edge_value = oid_pos(&parent->item->object.oid,
1122 ctx->commits.list,
1123 ctx->commits.nr,
1124 commit_to_oid);
1126 if (edge_value >= 0)
1127 edge_value += ctx->new_num_commits_in_base;
1128 else if (ctx->new_base_graph) {
1129 uint32_t pos;
1130 if (find_commit_pos_in_graph(parent->item,
1131 ctx->new_base_graph,
1132 &pos))
1133 edge_value = pos;
1136 if (edge_value < 0)
1137 BUG("missing parent %s for commit %s",
1138 oid_to_hex(&parent->item->object.oid),
1139 oid_to_hex(&(*list)->object.oid));
1142 hashwrite_be32(f, edge_value);
1144 if (parent)
1145 parent = parent->next;
1147 if (!parent)
1148 edge_value = GRAPH_PARENT_NONE;
1149 else if (parent->next)
1150 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1151 else {
1152 edge_value = oid_pos(&parent->item->object.oid,
1153 ctx->commits.list,
1154 ctx->commits.nr,
1155 commit_to_oid);
1157 if (edge_value >= 0)
1158 edge_value += ctx->new_num_commits_in_base;
1159 else if (ctx->new_base_graph) {
1160 uint32_t pos;
1161 if (find_commit_pos_in_graph(parent->item,
1162 ctx->new_base_graph,
1163 &pos))
1164 edge_value = pos;
1167 if (edge_value < 0)
1168 BUG("missing parent %s for commit %s",
1169 oid_to_hex(&parent->item->object.oid),
1170 oid_to_hex(&(*list)->object.oid));
1173 hashwrite_be32(f, edge_value);
1175 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1176 do {
1177 num_extra_edges++;
1178 parent = parent->next;
1179 } while (parent);
1182 if (sizeof((*list)->date) > 4)
1183 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1184 else
1185 packedDate[0] = 0;
1187 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1189 packedDate[1] = htonl((*list)->date);
1190 hashwrite(f, packedDate, 8);
1192 list++;
1195 return 0;
1198 static int write_graph_chunk_generation_data(struct hashfile *f,
1199 void *data)
1201 struct write_commit_graph_context *ctx = data;
1202 int i, num_generation_data_overflows = 0;
1204 for (i = 0; i < ctx->commits.nr; i++) {
1205 struct commit *c = ctx->commits.list[i];
1206 timestamp_t offset;
1207 repo_parse_commit(ctx->r, c);
1208 offset = commit_graph_data_at(c)->generation - c->date;
1209 display_progress(ctx->progress, ++ctx->progress_cnt);
1211 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1212 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1213 num_generation_data_overflows++;
1216 hashwrite_be32(f, offset);
1219 return 0;
1222 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1223 void *data)
1225 struct write_commit_graph_context *ctx = data;
1226 int i;
1227 for (i = 0; i < ctx->commits.nr; i++) {
1228 struct commit *c = ctx->commits.list[i];
1229 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1230 display_progress(ctx->progress, ++ctx->progress_cnt);
1232 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1233 hashwrite_be32(f, offset >> 32);
1234 hashwrite_be32(f, (uint32_t) offset);
1238 return 0;
1241 static int write_graph_chunk_extra_edges(struct hashfile *f,
1242 void *data)
1244 struct write_commit_graph_context *ctx = data;
1245 struct commit **list = ctx->commits.list;
1246 struct commit **last = ctx->commits.list + ctx->commits.nr;
1247 struct commit_list *parent;
1249 while (list < last) {
1250 int num_parents = 0;
1252 display_progress(ctx->progress, ++ctx->progress_cnt);
1254 for (parent = (*list)->parents; num_parents < 3 && parent;
1255 parent = parent->next)
1256 num_parents++;
1258 if (num_parents <= 2) {
1259 list++;
1260 continue;
1263 /* Since num_parents > 2, this initializer is safe. */
1264 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1265 int edge_value = oid_pos(&parent->item->object.oid,
1266 ctx->commits.list,
1267 ctx->commits.nr,
1268 commit_to_oid);
1270 if (edge_value >= 0)
1271 edge_value += ctx->new_num_commits_in_base;
1272 else if (ctx->new_base_graph) {
1273 uint32_t pos;
1274 if (find_commit_pos_in_graph(parent->item,
1275 ctx->new_base_graph,
1276 &pos))
1277 edge_value = pos;
1280 if (edge_value < 0)
1281 BUG("missing parent %s for commit %s",
1282 oid_to_hex(&parent->item->object.oid),
1283 oid_to_hex(&(*list)->object.oid));
1284 else if (!parent->next)
1285 edge_value |= GRAPH_LAST_EDGE;
1287 hashwrite_be32(f, edge_value);
1290 list++;
1293 return 0;
1296 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1297 void *data)
1299 struct write_commit_graph_context *ctx = data;
1300 struct commit **list = ctx->commits.list;
1301 struct commit **last = ctx->commits.list + ctx->commits.nr;
1302 uint32_t cur_pos = 0;
1304 while (list < last) {
1305 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1306 size_t len = filter ? filter->len : 0;
1307 cur_pos += len;
1308 display_progress(ctx->progress, ++ctx->progress_cnt);
1309 hashwrite_be32(f, cur_pos);
1310 list++;
1313 return 0;
1316 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1318 struct json_writer jw = JSON_WRITER_INIT;
1320 jw_object_begin(&jw, 0);
1321 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1322 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1323 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1324 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1325 jw_end(&jw);
1327 trace2_data_json("bloom", ctx->r, "settings", &jw);
1329 jw_release(&jw);
1332 static int write_graph_chunk_bloom_data(struct hashfile *f,
1333 void *data)
1335 struct write_commit_graph_context *ctx = data;
1336 struct commit **list = ctx->commits.list;
1337 struct commit **last = ctx->commits.list + ctx->commits.nr;
1339 trace2_bloom_filter_settings(ctx);
1341 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1342 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1343 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1345 while (list < last) {
1346 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1347 size_t len = filter ? filter->len : 0;
1349 display_progress(ctx->progress, ++ctx->progress_cnt);
1350 if (len)
1351 hashwrite(f, filter->data, len * sizeof(unsigned char));
1352 list++;
1355 return 0;
1358 static int add_packed_commits(const struct object_id *oid,
1359 struct packed_git *pack,
1360 uint32_t pos,
1361 void *data)
1363 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1364 enum object_type type;
1365 off_t offset = nth_packed_object_offset(pack, pos);
1366 struct object_info oi = OBJECT_INFO_INIT;
1368 if (ctx->progress)
1369 display_progress(ctx->progress, ++ctx->progress_done);
1371 oi.typep = &type;
1372 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1373 die(_("unable to get type of object %s"), oid_to_hex(oid));
1375 if (type != OBJ_COMMIT)
1376 return 0;
1378 oid_array_append(&ctx->oids, oid);
1379 set_commit_pos(ctx->r, oid);
1381 return 0;
1384 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1386 struct commit_list *parent;
1387 for (parent = commit->parents; parent; parent = parent->next) {
1388 if (!(parent->item->object.flags & REACHABLE)) {
1389 oid_array_append(&ctx->oids, &parent->item->object.oid);
1390 parent->item->object.flags |= REACHABLE;
1395 static void close_reachable(struct write_commit_graph_context *ctx)
1397 int i;
1398 struct commit *commit;
1399 enum commit_graph_split_flags flags = ctx->opts ?
1400 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1402 if (ctx->report_progress)
1403 ctx->progress = start_delayed_progress(
1404 _("Loading known commits in commit graph"),
1405 ctx->oids.nr);
1406 for (i = 0; i < ctx->oids.nr; i++) {
1407 display_progress(ctx->progress, i + 1);
1408 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1409 if (commit)
1410 commit->object.flags |= REACHABLE;
1412 stop_progress(&ctx->progress);
1415 * As this loop runs, ctx->oids.nr may grow, but not more
1416 * than the number of missing commits in the reachable
1417 * closure.
1419 if (ctx->report_progress)
1420 ctx->progress = start_delayed_progress(
1421 _("Expanding reachable commits in commit graph"),
1423 for (i = 0; i < ctx->oids.nr; i++) {
1424 display_progress(ctx->progress, i + 1);
1425 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1427 if (!commit)
1428 continue;
1429 if (ctx->split) {
1430 if ((!repo_parse_commit(ctx->r, commit) &&
1431 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1432 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1433 add_missing_parents(ctx, commit);
1434 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1435 add_missing_parents(ctx, commit);
1437 stop_progress(&ctx->progress);
1439 if (ctx->report_progress)
1440 ctx->progress = start_delayed_progress(
1441 _("Clearing commit marks in commit graph"),
1442 ctx->oids.nr);
1443 for (i = 0; i < ctx->oids.nr; i++) {
1444 display_progress(ctx->progress, i + 1);
1445 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1447 if (commit)
1448 commit->object.flags &= ~REACHABLE;
1450 stop_progress(&ctx->progress);
1453 struct compute_generation_info {
1454 struct repository *r;
1455 struct packed_commit_list *commits;
1456 struct progress *progress;
1457 int progress_cnt;
1459 timestamp_t (*get_generation)(struct commit *c, void *data);
1460 void (*set_generation)(struct commit *c, timestamp_t gen, void *data);
1461 void *data;
1464 static timestamp_t compute_generation_from_max(struct commit *c,
1465 timestamp_t max_gen,
1466 int generation_version)
1468 switch (generation_version) {
1469 case 1: /* topological levels */
1470 if (max_gen > GENERATION_NUMBER_V1_MAX - 1)
1471 max_gen = GENERATION_NUMBER_V1_MAX - 1;
1472 return max_gen + 1;
1474 case 2: /* corrected commit date */
1475 if (c->date && c->date > max_gen)
1476 max_gen = c->date - 1;
1477 return max_gen + 1;
1479 default:
1480 BUG("attempting unimplemented version");
1484 static void compute_reachable_generation_numbers(
1485 struct compute_generation_info *info,
1486 int generation_version)
1488 int i;
1489 struct commit_list *list = NULL;
1491 for (i = 0; i < info->commits->nr; i++) {
1492 struct commit *c = info->commits->list[i];
1493 timestamp_t gen;
1494 repo_parse_commit(info->r, c);
1495 gen = info->get_generation(c, info->data);
1496 display_progress(info->progress, info->progress_cnt + 1);
1498 if (gen != GENERATION_NUMBER_ZERO && gen != GENERATION_NUMBER_INFINITY)
1499 continue;
1501 commit_list_insert(c, &list);
1502 while (list) {
1503 struct commit *current = list->item;
1504 struct commit_list *parent;
1505 int all_parents_computed = 1;
1506 uint32_t max_gen = 0;
1508 for (parent = current->parents; parent; parent = parent->next) {
1509 repo_parse_commit(info->r, parent->item);
1510 gen = info->get_generation(parent->item, info->data);
1512 if (gen == GENERATION_NUMBER_ZERO) {
1513 all_parents_computed = 0;
1514 commit_list_insert(parent->item, &list);
1515 break;
1518 if (gen > max_gen)
1519 max_gen = gen;
1522 if (all_parents_computed) {
1523 pop_commit(&list);
1524 gen = compute_generation_from_max(
1525 current, max_gen,
1526 generation_version);
1527 info->set_generation(current, gen, info->data);
1533 static timestamp_t get_topo_level(struct commit *c, void *data)
1535 struct write_commit_graph_context *ctx = data;
1536 return *topo_level_slab_at(ctx->topo_levels, c);
1539 static void set_topo_level(struct commit *c, timestamp_t t, void *data)
1541 struct write_commit_graph_context *ctx = data;
1542 *topo_level_slab_at(ctx->topo_levels, c) = (uint32_t)t;
1545 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1547 struct compute_generation_info info = {
1548 .r = ctx->r,
1549 .commits = &ctx->commits,
1550 .get_generation = get_topo_level,
1551 .set_generation = set_topo_level,
1552 .data = ctx,
1555 if (ctx->report_progress)
1556 info.progress = ctx->progress
1557 = start_delayed_progress(
1558 _("Computing commit graph topological levels"),
1559 ctx->commits.nr);
1561 compute_reachable_generation_numbers(&info, 1);
1563 stop_progress(&ctx->progress);
1566 static timestamp_t get_generation_from_graph_data(struct commit *c, void *data)
1568 return commit_graph_data_at(c)->generation;
1571 static void set_generation_v2(struct commit *c, timestamp_t t, void *data)
1573 struct commit_graph_data *g = commit_graph_data_at(c);
1574 g->generation = t;
1577 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1579 int i;
1580 struct compute_generation_info info = {
1581 .r = ctx->r,
1582 .commits = &ctx->commits,
1583 .get_generation = get_generation_from_graph_data,
1584 .set_generation = set_generation_v2,
1585 .data = ctx,
1588 if (ctx->report_progress)
1589 info.progress = ctx->progress
1590 = start_delayed_progress(
1591 _("Computing commit graph generation numbers"),
1592 ctx->commits.nr);
1594 if (!ctx->trust_generation_numbers) {
1595 for (i = 0; i < ctx->commits.nr; i++) {
1596 struct commit *c = ctx->commits.list[i];
1597 repo_parse_commit(ctx->r, c);
1598 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1602 compute_reachable_generation_numbers(&info, 2);
1604 for (i = 0; i < ctx->commits.nr; i++) {
1605 struct commit *c = ctx->commits.list[i];
1606 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1607 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1608 ctx->num_generation_data_overflows++;
1610 stop_progress(&ctx->progress);
1613 static void set_generation_in_graph_data(struct commit *c, timestamp_t t,
1614 void *data)
1616 commit_graph_data_at(c)->generation = t;
1620 * After this method, all commits reachable from those in the given
1621 * list will have non-zero, non-infinite generation numbers.
1623 void ensure_generations_valid(struct repository *r,
1624 struct commit **commits, size_t nr)
1626 int generation_version = get_configured_generation_version(r);
1627 struct packed_commit_list list = {
1628 .list = commits,
1629 .alloc = nr,
1630 .nr = nr,
1632 struct compute_generation_info info = {
1633 .r = r,
1634 .commits = &list,
1635 .get_generation = get_generation_from_graph_data,
1636 .set_generation = set_generation_in_graph_data,
1639 compute_reachable_generation_numbers(&info, generation_version);
1642 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1644 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1645 ctx->count_bloom_filter_computed);
1646 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1647 ctx->count_bloom_filter_not_computed);
1648 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1649 ctx->count_bloom_filter_trunc_empty);
1650 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1651 ctx->count_bloom_filter_trunc_large);
1654 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1656 int i;
1657 struct progress *progress = NULL;
1658 struct commit **sorted_commits;
1659 int max_new_filters;
1661 init_bloom_filters();
1663 if (ctx->report_progress)
1664 progress = start_delayed_progress(
1665 _("Computing commit changed paths Bloom filters"),
1666 ctx->commits.nr);
1668 DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1670 if (ctx->order_by_pack)
1671 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1672 else
1673 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1675 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1676 ctx->opts->max_new_filters : ctx->commits.nr;
1678 for (i = 0; i < ctx->commits.nr; i++) {
1679 enum bloom_filter_computed computed = 0;
1680 struct commit *c = sorted_commits[i];
1681 struct bloom_filter *filter = get_or_compute_bloom_filter(
1682 ctx->r,
1684 ctx->count_bloom_filter_computed < max_new_filters,
1685 ctx->bloom_settings,
1686 &computed);
1687 if (computed & BLOOM_COMPUTED) {
1688 ctx->count_bloom_filter_computed++;
1689 if (computed & BLOOM_TRUNC_EMPTY)
1690 ctx->count_bloom_filter_trunc_empty++;
1691 if (computed & BLOOM_TRUNC_LARGE)
1692 ctx->count_bloom_filter_trunc_large++;
1693 } else if (computed & BLOOM_NOT_COMPUTED)
1694 ctx->count_bloom_filter_not_computed++;
1695 ctx->total_bloom_filter_data_size += filter
1696 ? sizeof(unsigned char) * filter->len : 0;
1697 display_progress(progress, i + 1);
1700 if (trace2_is_enabled())
1701 trace2_bloom_filter_write_statistics(ctx);
1703 free(sorted_commits);
1704 stop_progress(&progress);
1707 struct refs_cb_data {
1708 struct oidset *commits;
1709 struct progress *progress;
1712 static int add_ref_to_set(const char *refname UNUSED,
1713 const struct object_id *oid,
1714 int flags UNUSED, void *cb_data)
1716 struct object_id peeled;
1717 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1719 if (!peel_iterated_oid(oid, &peeled))
1720 oid = &peeled;
1721 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1722 oidset_insert(data->commits, oid);
1724 display_progress(data->progress, oidset_size(data->commits));
1726 return 0;
1729 int write_commit_graph_reachable(struct object_directory *odb,
1730 enum commit_graph_write_flags flags,
1731 const struct commit_graph_opts *opts)
1733 struct oidset commits = OIDSET_INIT;
1734 struct refs_cb_data data;
1735 int result;
1737 memset(&data, 0, sizeof(data));
1738 data.commits = &commits;
1739 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1740 data.progress = start_delayed_progress(
1741 _("Collecting referenced commits"), 0);
1743 for_each_ref(add_ref_to_set, &data);
1745 stop_progress(&data.progress);
1747 result = write_commit_graph(odb, NULL, &commits,
1748 flags, opts);
1750 oidset_clear(&commits);
1751 return result;
1754 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1755 const struct string_list *pack_indexes)
1757 uint32_t i;
1758 struct strbuf progress_title = STRBUF_INIT;
1759 struct strbuf packname = STRBUF_INIT;
1760 int dirlen;
1761 int ret = 0;
1763 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1764 dirlen = packname.len;
1765 if (ctx->report_progress) {
1766 strbuf_addf(&progress_title,
1767 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1768 "Finding commits for commit graph in %"PRIuMAX" packs",
1769 pack_indexes->nr),
1770 (uintmax_t)pack_indexes->nr);
1771 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1772 ctx->progress_done = 0;
1774 for (i = 0; i < pack_indexes->nr; i++) {
1775 struct packed_git *p;
1776 strbuf_setlen(&packname, dirlen);
1777 strbuf_addstr(&packname, pack_indexes->items[i].string);
1778 p = add_packed_git(packname.buf, packname.len, 1);
1779 if (!p) {
1780 ret = error(_("error adding pack %s"), packname.buf);
1781 goto cleanup;
1783 if (open_pack_index(p)) {
1784 ret = error(_("error opening index for %s"), packname.buf);
1785 goto cleanup;
1787 for_each_object_in_pack(p, add_packed_commits, ctx,
1788 FOR_EACH_OBJECT_PACK_ORDER);
1789 close_pack(p);
1790 free(p);
1793 cleanup:
1794 stop_progress(&ctx->progress);
1795 strbuf_release(&progress_title);
1796 strbuf_release(&packname);
1798 return ret;
1801 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1802 struct oidset *commits)
1804 struct oidset_iter iter;
1805 struct object_id *oid;
1807 if (!oidset_size(commits))
1808 return 0;
1810 oidset_iter_init(commits, &iter);
1811 while ((oid = oidset_iter_next(&iter))) {
1812 oid_array_append(&ctx->oids, oid);
1815 return 0;
1818 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1820 if (ctx->report_progress)
1821 ctx->progress = start_delayed_progress(
1822 _("Finding commits for commit graph among packed objects"),
1823 ctx->approx_nr_objects);
1824 for_each_packed_object(add_packed_commits, ctx,
1825 FOR_EACH_OBJECT_PACK_ORDER);
1826 if (ctx->progress_done < ctx->approx_nr_objects)
1827 display_progress(ctx->progress, ctx->approx_nr_objects);
1828 stop_progress(&ctx->progress);
1831 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1833 uint32_t i;
1834 enum commit_graph_split_flags flags = ctx->opts ?
1835 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1837 ctx->num_extra_edges = 0;
1838 if (ctx->report_progress)
1839 ctx->progress = start_delayed_progress(
1840 _("Finding extra edges in commit graph"),
1841 ctx->oids.nr);
1842 oid_array_sort(&ctx->oids);
1843 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1844 unsigned int num_parents;
1846 display_progress(ctx->progress, i + 1);
1848 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1849 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1851 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1852 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1853 continue;
1855 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1856 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1857 else
1858 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1860 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1861 if (num_parents > 2)
1862 ctx->num_extra_edges += num_parents - 1;
1864 ctx->commits.nr++;
1866 stop_progress(&ctx->progress);
1869 static int write_graph_chunk_base_1(struct hashfile *f,
1870 struct commit_graph *g)
1872 int num = 0;
1874 if (!g)
1875 return 0;
1877 num = write_graph_chunk_base_1(f, g->base_graph);
1878 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1879 return num + 1;
1882 static int write_graph_chunk_base(struct hashfile *f,
1883 void *data)
1885 struct write_commit_graph_context *ctx = data;
1886 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1888 if (num != ctx->num_commit_graphs_after - 1) {
1889 error(_("failed to write correct number of base graph ids"));
1890 return -1;
1893 return 0;
1896 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1898 uint32_t i;
1899 int fd;
1900 struct hashfile *f;
1901 struct lock_file lk = LOCK_INIT;
1902 const unsigned hashsz = the_hash_algo->rawsz;
1903 struct strbuf progress_title = STRBUF_INIT;
1904 struct chunkfile *cf;
1905 unsigned char file_hash[GIT_MAX_RAWSZ];
1907 if (ctx->split) {
1908 struct strbuf tmp_file = STRBUF_INIT;
1910 strbuf_addf(&tmp_file,
1911 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1912 ctx->odb->path);
1913 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1914 } else {
1915 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1918 if (safe_create_leading_directories(ctx->graph_name)) {
1919 UNLEAK(ctx->graph_name);
1920 error(_("unable to create leading directories of %s"),
1921 ctx->graph_name);
1922 return -1;
1925 if (ctx->split) {
1926 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
1928 hold_lock_file_for_update_mode(&lk, lock_name,
1929 LOCK_DIE_ON_ERROR, 0444);
1930 free(lock_name);
1932 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1933 if (fd < 0) {
1934 error(_("unable to create temporary graph layer"));
1935 return -1;
1938 if (adjust_shared_perm(ctx->graph_name)) {
1939 error(_("unable to adjust shared permissions for '%s'"),
1940 ctx->graph_name);
1941 return -1;
1944 f = hashfd(fd, ctx->graph_name);
1945 } else {
1946 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1947 LOCK_DIE_ON_ERROR, 0444);
1948 fd = get_lock_file_fd(&lk);
1949 f = hashfd(fd, get_lock_file_path(&lk));
1952 cf = init_chunkfile(f);
1954 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
1955 write_graph_chunk_fanout);
1956 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, hashsz * ctx->commits.nr,
1957 write_graph_chunk_oids);
1958 add_chunk(cf, GRAPH_CHUNKID_DATA, (hashsz + 16) * ctx->commits.nr,
1959 write_graph_chunk_data);
1961 if (ctx->write_generation_data)
1962 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
1963 sizeof(uint32_t) * ctx->commits.nr,
1964 write_graph_chunk_generation_data);
1965 if (ctx->num_generation_data_overflows)
1966 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
1967 sizeof(timestamp_t) * ctx->num_generation_data_overflows,
1968 write_graph_chunk_generation_data_overflow);
1969 if (ctx->num_extra_edges)
1970 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
1971 4 * ctx->num_extra_edges,
1972 write_graph_chunk_extra_edges);
1973 if (ctx->changed_paths) {
1974 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
1975 sizeof(uint32_t) * ctx->commits.nr,
1976 write_graph_chunk_bloom_indexes);
1977 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
1978 sizeof(uint32_t) * 3
1979 + ctx->total_bloom_filter_data_size,
1980 write_graph_chunk_bloom_data);
1982 if (ctx->num_commit_graphs_after > 1)
1983 add_chunk(cf, GRAPH_CHUNKID_BASE,
1984 hashsz * (ctx->num_commit_graphs_after - 1),
1985 write_graph_chunk_base);
1987 hashwrite_be32(f, GRAPH_SIGNATURE);
1989 hashwrite_u8(f, GRAPH_VERSION);
1990 hashwrite_u8(f, oid_version(the_hash_algo));
1991 hashwrite_u8(f, get_num_chunks(cf));
1992 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1994 if (ctx->report_progress) {
1995 strbuf_addf(&progress_title,
1996 Q_("Writing out commit graph in %d pass",
1997 "Writing out commit graph in %d passes",
1998 get_num_chunks(cf)),
1999 get_num_chunks(cf));
2000 ctx->progress = start_delayed_progress(
2001 progress_title.buf,
2002 get_num_chunks(cf) * ctx->commits.nr);
2005 write_chunkfile(cf, ctx);
2007 stop_progress(&ctx->progress);
2008 strbuf_release(&progress_title);
2010 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
2011 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
2012 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
2014 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
2015 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
2016 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
2017 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
2020 close_commit_graph(ctx->r->objects);
2021 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
2022 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
2023 free_chunkfile(cf);
2025 if (ctx->split) {
2026 FILE *chainf = fdopen_lock_file(&lk, "w");
2027 char *final_graph_name;
2028 int result;
2030 close(fd);
2032 if (!chainf) {
2033 error(_("unable to open commit-graph chain file"));
2034 return -1;
2037 if (ctx->base_graph_name) {
2038 const char *dest;
2039 int idx = ctx->num_commit_graphs_after - 1;
2040 if (ctx->num_commit_graphs_after > 1)
2041 idx--;
2043 dest = ctx->commit_graph_filenames_after[idx];
2045 if (strcmp(ctx->base_graph_name, dest)) {
2046 result = rename(ctx->base_graph_name, dest);
2048 if (result) {
2049 error(_("failed to rename base commit-graph file"));
2050 return -1;
2053 } else {
2054 char *graph_name = get_commit_graph_filename(ctx->odb);
2055 unlink(graph_name);
2056 free(graph_name);
2059 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
2060 final_graph_name = get_split_graph_filename(ctx->odb,
2061 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2062 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
2064 result = rename(ctx->graph_name, final_graph_name);
2066 for (i = 0; i < ctx->num_commit_graphs_after; i++)
2067 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2069 if (result) {
2070 error(_("failed to rename temporary commit-graph file"));
2071 return -1;
2075 commit_lock_file(&lk);
2077 return 0;
2080 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2082 struct commit_graph *g;
2083 uint32_t num_commits;
2084 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2085 uint32_t i;
2087 int max_commits = 0;
2088 int size_mult = 2;
2090 if (ctx->opts) {
2091 max_commits = ctx->opts->max_commits;
2093 if (ctx->opts->size_multiple)
2094 size_mult = ctx->opts->size_multiple;
2096 flags = ctx->opts->split_flags;
2099 g = ctx->r->objects->commit_graph;
2100 num_commits = ctx->commits.nr;
2101 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2102 ctx->num_commit_graphs_after = 1;
2103 else
2104 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2106 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2107 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2108 while (g && (g->num_commits <= size_mult * num_commits ||
2109 (max_commits && num_commits > max_commits))) {
2110 if (g->odb != ctx->odb)
2111 break;
2113 num_commits += g->num_commits;
2114 g = g->base_graph;
2116 ctx->num_commit_graphs_after--;
2120 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2121 ctx->new_base_graph = g;
2122 else if (ctx->num_commit_graphs_after != 1)
2123 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2124 "should be 1 with --split=replace");
2126 if (ctx->num_commit_graphs_after == 2) {
2127 char *old_graph_name = get_commit_graph_filename(g->odb);
2129 if (!strcmp(g->filename, old_graph_name) &&
2130 g->odb != ctx->odb) {
2131 ctx->num_commit_graphs_after = 1;
2132 ctx->new_base_graph = NULL;
2135 free(old_graph_name);
2138 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2139 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2141 for (i = 0; i < ctx->num_commit_graphs_after &&
2142 i < ctx->num_commit_graphs_before; i++)
2143 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2145 i = ctx->num_commit_graphs_before - 1;
2146 g = ctx->r->objects->commit_graph;
2148 while (g) {
2149 if (i < ctx->num_commit_graphs_after)
2150 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2153 * If the topmost remaining layer has generation data chunk, the
2154 * resultant layer also has generation data chunk.
2156 if (i == ctx->num_commit_graphs_after - 2)
2157 ctx->write_generation_data = !!g->chunk_generation_data;
2159 i--;
2160 g = g->base_graph;
2164 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2165 struct commit_graph *g)
2167 uint32_t i;
2168 uint32_t offset = g->num_commits_in_base;
2170 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2172 for (i = 0; i < g->num_commits; i++) {
2173 struct object_id oid;
2174 struct commit *result;
2176 display_progress(ctx->progress, i + 1);
2178 load_oid_from_graph(g, i + offset, &oid);
2180 /* only add commits if they still exist in the repo */
2181 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2183 if (result) {
2184 ctx->commits.list[ctx->commits.nr] = result;
2185 ctx->commits.nr++;
2190 static int commit_compare(const void *_a, const void *_b)
2192 const struct commit *a = *(const struct commit **)_a;
2193 const struct commit *b = *(const struct commit **)_b;
2194 return oidcmp(&a->object.oid, &b->object.oid);
2197 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2199 uint32_t i, dedup_i = 0;
2201 if (ctx->report_progress)
2202 ctx->progress = start_delayed_progress(
2203 _("Scanning merged commits"),
2204 ctx->commits.nr);
2206 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2208 ctx->num_extra_edges = 0;
2209 for (i = 0; i < ctx->commits.nr; i++) {
2210 display_progress(ctx->progress, i + 1);
2212 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2213 &ctx->commits.list[i]->object.oid)) {
2215 * Silently ignore duplicates. These were likely
2216 * created due to a commit appearing in multiple
2217 * layers of the chain, which is unexpected but
2218 * not invalid. We should make sure there is a
2219 * unique copy in the new layer.
2221 } else {
2222 unsigned int num_parents;
2224 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2225 dedup_i++;
2227 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2228 if (num_parents > 2)
2229 ctx->num_extra_edges += num_parents - 1;
2233 ctx->commits.nr = dedup_i;
2235 stop_progress(&ctx->progress);
2238 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2240 struct commit_graph *g = ctx->r->objects->commit_graph;
2241 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2243 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2244 current_graph_number--;
2246 if (ctx->report_progress)
2247 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2249 merge_commit_graph(ctx, g);
2250 stop_progress(&ctx->progress);
2252 g = g->base_graph;
2255 if (g) {
2256 ctx->new_base_graph = g;
2257 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2260 if (ctx->new_base_graph)
2261 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2263 sort_and_scan_merged_commits(ctx);
2266 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2268 uint32_t i;
2269 time_t now = time(NULL);
2271 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2272 struct stat st;
2273 struct utimbuf updated_time;
2275 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2276 continue;
2278 updated_time.actime = st.st_atime;
2279 updated_time.modtime = now;
2280 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2284 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2286 struct strbuf path = STRBUF_INIT;
2287 DIR *dir;
2288 struct dirent *de;
2289 size_t dirnamelen;
2290 timestamp_t expire_time = time(NULL);
2292 if (ctx->opts && ctx->opts->expire_time)
2293 expire_time = ctx->opts->expire_time;
2294 if (!ctx->split) {
2295 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2296 unlink(chain_file_name);
2297 free(chain_file_name);
2298 ctx->num_commit_graphs_after = 0;
2301 strbuf_addstr(&path, ctx->odb->path);
2302 strbuf_addstr(&path, "/info/commit-graphs");
2303 dir = opendir(path.buf);
2305 if (!dir)
2306 goto out;
2308 strbuf_addch(&path, '/');
2309 dirnamelen = path.len;
2310 while ((de = readdir(dir)) != NULL) {
2311 struct stat st;
2312 uint32_t i, found = 0;
2314 strbuf_setlen(&path, dirnamelen);
2315 strbuf_addstr(&path, de->d_name);
2317 if (stat(path.buf, &st) < 0)
2318 continue;
2320 if (st.st_mtime > expire_time)
2321 continue;
2322 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2323 continue;
2325 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2326 if (!strcmp(ctx->commit_graph_filenames_after[i],
2327 path.buf)) {
2328 found = 1;
2329 break;
2333 if (!found)
2334 unlink(path.buf);
2337 out:
2338 if(dir)
2339 closedir(dir);
2340 strbuf_release(&path);
2343 int write_commit_graph(struct object_directory *odb,
2344 const struct string_list *const pack_indexes,
2345 struct oidset *commits,
2346 enum commit_graph_write_flags flags,
2347 const struct commit_graph_opts *opts)
2349 struct repository *r = the_repository;
2350 struct write_commit_graph_context *ctx;
2351 uint32_t i;
2352 int res = 0;
2353 int replace = 0;
2354 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2355 struct topo_level_slab topo_levels;
2357 prepare_repo_settings(r);
2358 if (!r->settings.core_commit_graph) {
2359 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2360 return 0;
2362 if (!commit_graph_compatible(r))
2363 return 0;
2365 CALLOC_ARRAY(ctx, 1);
2366 ctx->r = r;
2367 ctx->odb = odb;
2368 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2369 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2370 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2371 ctx->opts = opts;
2372 ctx->total_bloom_filter_data_size = 0;
2373 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2374 ctx->num_generation_data_overflows = 0;
2376 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2377 bloom_settings.bits_per_entry);
2378 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2379 bloom_settings.num_hashes);
2380 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2381 bloom_settings.max_changed_paths);
2382 ctx->bloom_settings = &bloom_settings;
2384 init_topo_level_slab(&topo_levels);
2385 ctx->topo_levels = &topo_levels;
2387 prepare_commit_graph(ctx->r);
2388 if (ctx->r->objects->commit_graph) {
2389 struct commit_graph *g = ctx->r->objects->commit_graph;
2391 while (g) {
2392 g->topo_levels = &topo_levels;
2393 g = g->base_graph;
2397 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2398 ctx->changed_paths = 1;
2399 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2400 struct commit_graph *g;
2402 g = ctx->r->objects->commit_graph;
2404 /* We have changed-paths already. Keep them in the next graph */
2405 if (g && g->chunk_bloom_data) {
2406 ctx->changed_paths = 1;
2407 ctx->bloom_settings = g->bloom_filter_settings;
2411 if (ctx->split) {
2412 struct commit_graph *g = ctx->r->objects->commit_graph;
2414 while (g) {
2415 ctx->num_commit_graphs_before++;
2416 g = g->base_graph;
2419 if (ctx->num_commit_graphs_before) {
2420 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2421 i = ctx->num_commit_graphs_before;
2422 g = ctx->r->objects->commit_graph;
2424 while (g) {
2425 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2426 g = g->base_graph;
2430 if (ctx->opts)
2431 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2434 ctx->approx_nr_objects = repo_approximate_object_count(the_repository);
2436 if (ctx->append && ctx->r->objects->commit_graph) {
2437 struct commit_graph *g = ctx->r->objects->commit_graph;
2438 for (i = 0; i < g->num_commits; i++) {
2439 struct object_id oid;
2440 oidread(&oid, g->chunk_oid_lookup + g->hash_len * i);
2441 oid_array_append(&ctx->oids, &oid);
2445 if (pack_indexes) {
2446 ctx->order_by_pack = 1;
2447 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2448 goto cleanup;
2451 if (commits) {
2452 if ((res = fill_oids_from_commits(ctx, commits)))
2453 goto cleanup;
2456 if (!pack_indexes && !commits) {
2457 ctx->order_by_pack = 1;
2458 fill_oids_from_all_packs(ctx);
2461 close_reachable(ctx);
2463 copy_oids_to_commits(ctx);
2465 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2466 error(_("too many commits to write graph"));
2467 res = -1;
2468 goto cleanup;
2471 if (!ctx->commits.nr && !replace)
2472 goto cleanup;
2474 if (ctx->split) {
2475 split_graph_merge_strategy(ctx);
2477 if (!replace)
2478 merge_commit_graphs(ctx);
2479 } else
2480 ctx->num_commit_graphs_after = 1;
2482 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2484 compute_topological_levels(ctx);
2485 if (ctx->write_generation_data)
2486 compute_generation_numbers(ctx);
2488 if (ctx->changed_paths)
2489 compute_bloom_filters(ctx);
2491 res = write_commit_graph_file(ctx);
2493 if (ctx->split)
2494 mark_commit_graphs(ctx);
2496 expire_commit_graphs(ctx);
2498 cleanup:
2499 free(ctx->graph_name);
2500 free(ctx->commits.list);
2501 oid_array_clear(&ctx->oids);
2502 clear_topo_level_slab(&topo_levels);
2504 if (ctx->commit_graph_filenames_after) {
2505 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2506 free(ctx->commit_graph_filenames_after[i]);
2507 free(ctx->commit_graph_hash_after[i]);
2510 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2511 free(ctx->commit_graph_filenames_before[i]);
2513 free(ctx->commit_graph_filenames_after);
2514 free(ctx->commit_graph_filenames_before);
2515 free(ctx->commit_graph_hash_after);
2518 free(ctx);
2520 return res;
2523 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2524 static int verify_commit_graph_error;
2526 __attribute__((format (printf, 1, 2)))
2527 static void graph_report(const char *fmt, ...)
2529 va_list ap;
2531 verify_commit_graph_error = 1;
2532 va_start(ap, fmt);
2533 vfprintf(stderr, fmt, ap);
2534 fprintf(stderr, "\n");
2535 va_end(ap);
2538 #define GENERATION_ZERO_EXISTS 1
2539 #define GENERATION_NUMBER_EXISTS 2
2541 static int commit_graph_checksum_valid(struct commit_graph *g)
2543 return hashfile_checksum_valid(g->data, g->data_len);
2546 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2548 uint32_t i, cur_fanout_pos = 0;
2549 struct object_id prev_oid, cur_oid;
2550 int generation_zero = 0;
2551 struct progress *progress = NULL;
2552 int local_error = 0;
2554 if (!g) {
2555 graph_report("no commit-graph file loaded");
2556 return 1;
2559 verify_commit_graph_error = verify_commit_graph_lite(g);
2560 if (verify_commit_graph_error)
2561 return verify_commit_graph_error;
2563 if (!commit_graph_checksum_valid(g)) {
2564 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2565 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2568 for (i = 0; i < g->num_commits; i++) {
2569 struct commit *graph_commit;
2571 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2573 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2574 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2575 oid_to_hex(&prev_oid),
2576 oid_to_hex(&cur_oid));
2578 oidcpy(&prev_oid, &cur_oid);
2580 while (cur_oid.hash[0] > cur_fanout_pos) {
2581 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2583 if (i != fanout_value)
2584 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2585 cur_fanout_pos, fanout_value, i);
2586 cur_fanout_pos++;
2589 graph_commit = lookup_commit(r, &cur_oid);
2590 if (!parse_commit_in_graph_one(r, g, graph_commit))
2591 graph_report(_("failed to parse commit %s from commit-graph"),
2592 oid_to_hex(&cur_oid));
2595 while (cur_fanout_pos < 256) {
2596 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2598 if (g->num_commits != fanout_value)
2599 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2600 cur_fanout_pos, fanout_value, i);
2602 cur_fanout_pos++;
2605 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2606 return verify_commit_graph_error;
2608 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2609 progress = start_progress(_("Verifying commits in commit graph"),
2610 g->num_commits);
2612 for (i = 0; i < g->num_commits; i++) {
2613 struct commit *graph_commit, *odb_commit;
2614 struct commit_list *graph_parents, *odb_parents;
2615 timestamp_t max_generation = 0;
2616 timestamp_t generation;
2618 display_progress(progress, i + 1);
2619 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2621 graph_commit = lookup_commit(r, &cur_oid);
2622 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2623 if (repo_parse_commit_internal(r, odb_commit, 0, 0)) {
2624 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2625 oid_to_hex(&cur_oid));
2626 continue;
2629 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2630 get_commit_tree_oid(odb_commit)))
2631 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2632 oid_to_hex(&cur_oid),
2633 oid_to_hex(get_commit_tree_oid(graph_commit)),
2634 oid_to_hex(get_commit_tree_oid(odb_commit)));
2636 graph_parents = graph_commit->parents;
2637 odb_parents = odb_commit->parents;
2639 while (graph_parents) {
2640 if (!odb_parents) {
2641 graph_report(_("commit-graph parent list for commit %s is too long"),
2642 oid_to_hex(&cur_oid));
2643 break;
2646 /* parse parent in case it is in a base graph */
2647 parse_commit_in_graph_one(r, g, graph_parents->item);
2649 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2650 graph_report(_("commit-graph parent for %s is %s != %s"),
2651 oid_to_hex(&cur_oid),
2652 oid_to_hex(&graph_parents->item->object.oid),
2653 oid_to_hex(&odb_parents->item->object.oid));
2655 generation = commit_graph_generation(graph_parents->item);
2656 if (generation > max_generation)
2657 max_generation = generation;
2659 graph_parents = graph_parents->next;
2660 odb_parents = odb_parents->next;
2663 if (odb_parents)
2664 graph_report(_("commit-graph parent list for commit %s terminates early"),
2665 oid_to_hex(&cur_oid));
2667 if (!commit_graph_generation(graph_commit)) {
2668 if (generation_zero == GENERATION_NUMBER_EXISTS)
2669 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2670 oid_to_hex(&cur_oid));
2671 generation_zero = GENERATION_ZERO_EXISTS;
2672 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2673 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2674 oid_to_hex(&cur_oid));
2676 if (generation_zero == GENERATION_ZERO_EXISTS)
2677 continue;
2680 * If we are using topological level and one of our parents has
2681 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2682 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2683 * in the following condition.
2685 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2686 max_generation--;
2688 generation = commit_graph_generation(graph_commit);
2689 if (generation < max_generation + 1)
2690 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2691 oid_to_hex(&cur_oid),
2692 generation,
2693 max_generation + 1);
2695 if (graph_commit->date != odb_commit->date)
2696 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2697 oid_to_hex(&cur_oid),
2698 graph_commit->date,
2699 odb_commit->date);
2701 stop_progress(&progress);
2703 local_error = verify_commit_graph_error;
2705 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2706 local_error |= verify_commit_graph(r, g->base_graph, flags);
2708 return local_error;
2711 void free_commit_graph(struct commit_graph *g)
2713 if (!g)
2714 return;
2715 if (g->data) {
2716 munmap((void *)g->data, g->data_len);
2717 g->data = NULL;
2719 free(g->filename);
2720 free(g->bloom_filter_settings);
2721 free(g);
2724 void disable_commit_graph(struct repository *r)
2726 r->commit_graph_disabled = 1;