wt-status: read HEAD and ORIG_HEAD via the refdb
[alt-git.git] / commit-graph.c
blobacac9bf6e1d94d08ec2410bd2f76d95a5c3aa8a5
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 graph_read_oid_fanout(const unsigned char *chunk_start,
279 size_t chunk_size, void *data)
281 struct commit_graph *g = data;
282 int i;
284 if (chunk_size != 256 * sizeof(uint32_t))
285 return error(_("commit-graph oid fanout chunk is wrong size"));
286 g->chunk_oid_fanout = (const uint32_t *)chunk_start;
287 g->num_commits = ntohl(g->chunk_oid_fanout[255]);
289 for (i = 0; i < 255; i++) {
290 uint32_t oid_fanout1 = ntohl(g->chunk_oid_fanout[i]);
291 uint32_t oid_fanout2 = ntohl(g->chunk_oid_fanout[i + 1]);
293 if (oid_fanout1 > oid_fanout2) {
294 error(_("commit-graph fanout values out of order"));
295 return 1;
299 return 0;
302 static int graph_read_oid_lookup(const unsigned char *chunk_start,
303 size_t chunk_size, void *data)
305 struct commit_graph *g = data;
306 g->chunk_oid_lookup = chunk_start;
307 if (chunk_size / g->hash_len != g->num_commits)
308 return error(_("commit-graph OID lookup chunk is the wrong size"));
309 return 0;
312 static int graph_read_commit_data(const unsigned char *chunk_start,
313 size_t chunk_size, void *data)
315 struct commit_graph *g = data;
316 if (chunk_size / GRAPH_DATA_WIDTH != g->num_commits)
317 return error(_("commit-graph commit data chunk is wrong size"));
318 g->chunk_commit_data = chunk_start;
319 return 0;
322 static int graph_read_generation_data(const unsigned char *chunk_start,
323 size_t chunk_size, void *data)
325 struct commit_graph *g = data;
326 if (chunk_size / sizeof(uint32_t) != g->num_commits)
327 return error(_("commit-graph generations chunk is wrong size"));
328 g->chunk_generation_data = chunk_start;
329 return 0;
332 static int graph_read_bloom_index(const unsigned char *chunk_start,
333 size_t chunk_size, void *data)
335 struct commit_graph *g = data;
336 if (chunk_size / 4 != g->num_commits) {
337 warning(_("commit-graph changed-path index chunk is too small"));
338 return -1;
340 g->chunk_bloom_indexes = chunk_start;
341 return 0;
344 static int graph_read_bloom_data(const unsigned char *chunk_start,
345 size_t chunk_size, void *data)
347 struct commit_graph *g = data;
348 uint32_t hash_version;
350 if (chunk_size < BLOOMDATA_CHUNK_HEADER_SIZE) {
351 warning(_("ignoring too-small changed-path chunk"
352 " (%"PRIuMAX" < %"PRIuMAX") in commit-graph file"),
353 (uintmax_t)chunk_size,
354 (uintmax_t)BLOOMDATA_CHUNK_HEADER_SIZE);
355 return -1;
358 g->chunk_bloom_data = chunk_start;
359 g->chunk_bloom_data_size = chunk_size;
360 hash_version = get_be32(chunk_start);
362 if (hash_version != 1)
363 return 0;
365 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
366 g->bloom_filter_settings->hash_version = hash_version;
367 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
368 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
369 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
371 return 0;
374 struct commit_graph *parse_commit_graph(struct repo_settings *s,
375 void *graph_map, size_t graph_size)
377 const unsigned char *data;
378 struct commit_graph *graph;
379 uint32_t graph_signature;
380 unsigned char graph_version, hash_version;
381 struct chunkfile *cf = NULL;
383 if (!graph_map)
384 return NULL;
386 if (graph_size < GRAPH_MIN_SIZE)
387 return NULL;
389 data = (const unsigned char *)graph_map;
391 graph_signature = get_be32(data);
392 if (graph_signature != GRAPH_SIGNATURE) {
393 error(_("commit-graph signature %X does not match signature %X"),
394 graph_signature, GRAPH_SIGNATURE);
395 return NULL;
398 graph_version = *(unsigned char*)(data + 4);
399 if (graph_version != GRAPH_VERSION) {
400 error(_("commit-graph version %X does not match version %X"),
401 graph_version, GRAPH_VERSION);
402 return NULL;
405 hash_version = *(unsigned char*)(data + 5);
406 if (hash_version != oid_version(the_hash_algo)) {
407 error(_("commit-graph hash version %X does not match version %X"),
408 hash_version, oid_version(the_hash_algo));
409 return NULL;
412 graph = alloc_commit_graph();
414 graph->hash_len = the_hash_algo->rawsz;
415 graph->num_chunks = *(unsigned char*)(data + 6);
416 graph->data = graph_map;
417 graph->data_len = graph_size;
419 if (graph_size < GRAPH_HEADER_SIZE +
420 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
421 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
422 error(_("commit-graph file is too small to hold %u chunks"),
423 graph->num_chunks);
424 free(graph);
425 return NULL;
428 cf = init_chunkfile(NULL);
430 if (read_table_of_contents(cf, graph->data, graph_size,
431 GRAPH_HEADER_SIZE, graph->num_chunks, 1))
432 goto free_and_return;
434 if (read_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, graph_read_oid_fanout, graph)) {
435 error(_("commit-graph required OID fanout chunk missing or corrupted"));
436 goto free_and_return;
438 if (read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph)) {
439 error(_("commit-graph required OID lookup chunk missing or corrupted"));
440 goto free_and_return;
442 if (read_chunk(cf, GRAPH_CHUNKID_DATA, graph_read_commit_data, graph)) {
443 error(_("commit-graph required commit data chunk missing or corrupted"));
444 goto free_and_return;
447 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges,
448 &graph->chunk_extra_edges_size);
449 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs,
450 &graph->chunk_base_graphs_size);
452 if (s->commit_graph_generation_version >= 2) {
453 read_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
454 graph_read_generation_data, graph);
455 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
456 &graph->chunk_generation_data_overflow,
457 &graph->chunk_generation_data_overflow_size);
459 if (graph->chunk_generation_data)
460 graph->read_generation_data = 1;
463 if (s->commit_graph_read_changed_paths) {
464 read_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
465 graph_read_bloom_index, graph);
466 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
467 graph_read_bloom_data, graph);
470 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
471 init_bloom_filters();
472 } else {
473 /* We need both the bloom chunks to exist together. Else ignore the data */
474 graph->chunk_bloom_indexes = NULL;
475 graph->chunk_bloom_data = NULL;
476 FREE_AND_NULL(graph->bloom_filter_settings);
479 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
481 free_chunkfile(cf);
482 return graph;
484 free_and_return:
485 free_chunkfile(cf);
486 free(graph->bloom_filter_settings);
487 free(graph);
488 return NULL;
491 static struct commit_graph *load_commit_graph_one(struct repository *r,
492 const char *graph_file,
493 struct object_directory *odb)
496 struct stat st;
497 int fd;
498 struct commit_graph *g;
499 int open_ok = open_commit_graph(graph_file, &fd, &st);
501 if (!open_ok)
502 return NULL;
504 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
506 if (g)
507 g->filename = xstrdup(graph_file);
509 return g;
512 static struct commit_graph *load_commit_graph_v1(struct repository *r,
513 struct object_directory *odb)
515 char *graph_name = get_commit_graph_filename(odb);
516 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
517 free(graph_name);
519 return g;
523 * returns 1 if and only if all graphs in the chain have
524 * corrected commit dates stored in the generation_data chunk.
526 static int validate_mixed_generation_chain(struct commit_graph *g)
528 int read_generation_data = 1;
529 struct commit_graph *p = g;
531 while (read_generation_data && p) {
532 read_generation_data = p->read_generation_data;
533 p = p->base_graph;
536 if (read_generation_data)
537 return 1;
539 while (g) {
540 g->read_generation_data = 0;
541 g = g->base_graph;
544 return 0;
547 static int add_graph_to_chain(struct commit_graph *g,
548 struct commit_graph *chain,
549 struct object_id *oids,
550 int n)
552 struct commit_graph *cur_g = chain;
554 if (n && !g->chunk_base_graphs) {
555 warning(_("commit-graph has no base graphs chunk"));
556 return 0;
559 if (g->chunk_base_graphs_size / g->hash_len < n) {
560 warning(_("commit-graph base graphs chunk is too small"));
561 return 0;
564 while (n) {
565 n--;
567 if (!cur_g ||
568 !oideq(&oids[n], &cur_g->oid) ||
569 !hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_len, n))) {
570 warning(_("commit-graph chain does not match"));
571 return 0;
574 cur_g = cur_g->base_graph;
577 if (chain) {
578 if (unsigned_add_overflows(chain->num_commits,
579 chain->num_commits_in_base)) {
580 warning(_("commit count in base graph too high: %"PRIuMAX),
581 (uintmax_t)chain->num_commits_in_base);
582 return 0;
584 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
587 g->base_graph = chain;
589 return 1;
592 int open_commit_graph_chain(const char *chain_file,
593 int *fd, struct stat *st)
595 *fd = git_open(chain_file);
596 if (*fd < 0)
597 return 0;
598 if (fstat(*fd, st)) {
599 close(*fd);
600 return 0;
602 if (st->st_size < the_hash_algo->hexsz) {
603 close(*fd);
604 if (!st->st_size) {
605 /* treat empty files the same as missing */
606 errno = ENOENT;
607 } else {
608 warning(_("commit-graph chain file too small"));
609 errno = EINVAL;
611 return 0;
613 return 1;
616 struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
617 int fd, struct stat *st,
618 int *incomplete_chain)
620 struct commit_graph *graph_chain = NULL;
621 struct strbuf line = STRBUF_INIT;
622 struct object_id *oids;
623 int i = 0, valid = 1, count;
624 FILE *fp = xfdopen(fd, "r");
626 count = st->st_size / (the_hash_algo->hexsz + 1);
627 CALLOC_ARRAY(oids, count);
629 prepare_alt_odb(r);
631 for (i = 0; i < count; i++) {
632 struct object_directory *odb;
634 if (strbuf_getline_lf(&line, fp) == EOF)
635 break;
637 if (get_oid_hex(line.buf, &oids[i])) {
638 warning(_("invalid commit-graph chain: line '%s' not a hash"),
639 line.buf);
640 valid = 0;
641 break;
644 valid = 0;
645 for (odb = r->objects->odb; odb; odb = odb->next) {
646 char *graph_name = get_split_graph_filename(odb, line.buf);
647 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
649 free(graph_name);
651 if (g) {
652 if (add_graph_to_chain(g, graph_chain, oids, i)) {
653 graph_chain = g;
654 valid = 1;
655 } else {
656 free_commit_graph(g);
659 break;
663 if (!valid) {
664 warning(_("unable to find all commit-graph files"));
665 break;
669 validate_mixed_generation_chain(graph_chain);
671 free(oids);
672 fclose(fp);
673 strbuf_release(&line);
675 *incomplete_chain = !valid;
676 return graph_chain;
679 static struct commit_graph *load_commit_graph_chain(struct repository *r,
680 struct object_directory *odb)
682 char *chain_file = get_commit_graph_chain_filename(odb);
683 struct stat st;
684 int fd;
685 struct commit_graph *g = NULL;
687 if (open_commit_graph_chain(chain_file, &fd, &st)) {
688 int incomplete;
689 /* ownership of fd is taken over by load function */
690 g = load_commit_graph_chain_fd_st(r, fd, &st, &incomplete);
693 free(chain_file);
694 return g;
697 struct commit_graph *read_commit_graph_one(struct repository *r,
698 struct object_directory *odb)
700 struct commit_graph *g = load_commit_graph_v1(r, odb);
702 if (!g)
703 g = load_commit_graph_chain(r, odb);
705 return g;
708 static void prepare_commit_graph_one(struct repository *r,
709 struct object_directory *odb)
712 if (r->objects->commit_graph)
713 return;
715 r->objects->commit_graph = read_commit_graph_one(r, odb);
719 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
721 * On the first invocation, this function attempts to load the commit
722 * graph if the_repository is configured to have one.
724 static int prepare_commit_graph(struct repository *r)
726 struct object_directory *odb;
729 * Early return if there is no git dir or if the commit graph is
730 * disabled.
732 * This must come before the "already attempted?" check below, because
733 * we want to disable even an already-loaded graph file.
735 if (!r->gitdir || r->commit_graph_disabled)
736 return 0;
738 if (r->objects->commit_graph_attempted)
739 return !!r->objects->commit_graph;
740 r->objects->commit_graph_attempted = 1;
742 prepare_repo_settings(r);
744 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
745 r->settings.core_commit_graph != 1)
747 * This repository is not configured to use commit graphs, so
748 * do not load one. (But report commit_graph_attempted anyway
749 * so that commit graph loading is not attempted again for this
750 * repository.)
752 return 0;
754 if (!commit_graph_compatible(r))
755 return 0;
757 prepare_alt_odb(r);
758 for (odb = r->objects->odb;
759 !r->objects->commit_graph && odb;
760 odb = odb->next)
761 prepare_commit_graph_one(r, odb);
762 return !!r->objects->commit_graph;
765 int generation_numbers_enabled(struct repository *r)
767 uint32_t first_generation;
768 struct commit_graph *g;
769 if (!prepare_commit_graph(r))
770 return 0;
772 g = r->objects->commit_graph;
774 if (!g->num_commits)
775 return 0;
777 first_generation = get_be32(g->chunk_commit_data +
778 g->hash_len + 8) >> 2;
780 return !!first_generation;
783 int corrected_commit_dates_enabled(struct repository *r)
785 struct commit_graph *g;
786 if (!prepare_commit_graph(r))
787 return 0;
789 g = r->objects->commit_graph;
791 if (!g->num_commits)
792 return 0;
794 return g->read_generation_data;
797 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
799 struct commit_graph *g = r->objects->commit_graph;
800 while (g) {
801 if (g->bloom_filter_settings)
802 return g->bloom_filter_settings;
803 g = g->base_graph;
805 return NULL;
808 void close_commit_graph(struct raw_object_store *o)
810 clear_commit_graph_data_slab(&commit_graph_data_slab);
811 free_commit_graph(o->commit_graph);
812 o->commit_graph = NULL;
815 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
817 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
818 g->chunk_oid_lookup, g->hash_len, pos);
821 static void load_oid_from_graph(struct commit_graph *g,
822 uint32_t pos,
823 struct object_id *oid)
825 uint32_t lex_index;
827 while (g && pos < g->num_commits_in_base)
828 g = g->base_graph;
830 if (!g)
831 BUG("NULL commit-graph");
833 if (pos >= g->num_commits + g->num_commits_in_base)
834 die(_("invalid commit position. commit-graph is likely corrupt"));
836 lex_index = pos - g->num_commits_in_base;
838 oidread(oid, g->chunk_oid_lookup + st_mult(g->hash_len, lex_index));
841 static struct commit_list **insert_parent_or_die(struct repository *r,
842 struct commit_graph *g,
843 uint32_t pos,
844 struct commit_list **pptr)
846 struct commit *c;
847 struct object_id oid;
849 if (pos >= g->num_commits + g->num_commits_in_base)
850 die("invalid parent position %"PRIu32, pos);
852 load_oid_from_graph(g, pos, &oid);
853 c = lookup_commit(r, &oid);
854 if (!c)
855 die(_("could not find commit %s"), oid_to_hex(&oid));
856 commit_graph_data_at(c)->graph_pos = pos;
857 return &commit_list_insert(c, pptr)->next;
860 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
862 const unsigned char *commit_data;
863 struct commit_graph_data *graph_data;
864 uint32_t lex_index, offset_pos;
865 uint64_t date_high, date_low, offset;
867 while (pos < g->num_commits_in_base)
868 g = g->base_graph;
870 if (pos >= g->num_commits + g->num_commits_in_base)
871 die(_("invalid commit position. commit-graph is likely corrupt"));
873 lex_index = pos - g->num_commits_in_base;
874 commit_data = g->chunk_commit_data + st_mult(GRAPH_DATA_WIDTH, lex_index);
876 graph_data = commit_graph_data_at(item);
877 graph_data->graph_pos = pos;
879 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
880 date_low = get_be32(commit_data + g->hash_len + 12);
881 item->date = (timestamp_t)((date_high << 32) | date_low);
883 if (g->read_generation_data) {
884 offset = (timestamp_t)get_be32(g->chunk_generation_data + st_mult(sizeof(uint32_t), lex_index));
886 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
887 if (!g->chunk_generation_data_overflow)
888 die(_("commit-graph requires overflow generation data but has none"));
890 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
891 if (g->chunk_generation_data_overflow_size / sizeof(uint64_t) <= offset_pos)
892 die(_("commit-graph overflow generation data is too small"));
893 graph_data->generation = item->date +
894 get_be64(g->chunk_generation_data_overflow + sizeof(uint64_t) * offset_pos);
895 } else
896 graph_data->generation = item->date + offset;
897 } else
898 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
900 if (g->topo_levels)
901 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
904 static inline void set_commit_tree(struct commit *c, struct tree *t)
906 c->maybe_tree = t;
909 static int fill_commit_in_graph(struct repository *r,
910 struct commit *item,
911 struct commit_graph *g, uint32_t pos)
913 uint32_t edge_value;
914 uint32_t parent_data_pos;
915 struct commit_list **pptr;
916 const unsigned char *commit_data;
917 uint32_t lex_index;
919 while (pos < g->num_commits_in_base)
920 g = g->base_graph;
922 fill_commit_graph_info(item, g, pos);
924 lex_index = pos - g->num_commits_in_base;
925 commit_data = g->chunk_commit_data + st_mult(g->hash_len + 16, lex_index);
927 item->object.parsed = 1;
929 set_commit_tree(item, NULL);
931 pptr = &item->parents;
933 edge_value = get_be32(commit_data + g->hash_len);
934 if (edge_value == GRAPH_PARENT_NONE)
935 return 1;
936 pptr = insert_parent_or_die(r, g, edge_value, pptr);
938 edge_value = get_be32(commit_data + g->hash_len + 4);
939 if (edge_value == GRAPH_PARENT_NONE)
940 return 1;
941 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
942 pptr = insert_parent_or_die(r, g, edge_value, pptr);
943 return 1;
946 parent_data_pos = edge_value & GRAPH_EDGE_LAST_MASK;
947 do {
948 if (g->chunk_extra_edges_size / sizeof(uint32_t) <= parent_data_pos) {
949 error(_("commit-graph extra-edges pointer out of bounds"));
950 free_commit_list(item->parents);
951 item->parents = NULL;
952 item->object.parsed = 0;
953 return 0;
955 edge_value = get_be32(g->chunk_extra_edges +
956 sizeof(uint32_t) * parent_data_pos);
957 pptr = insert_parent_or_die(r, g,
958 edge_value & GRAPH_EDGE_LAST_MASK,
959 pptr);
960 parent_data_pos++;
961 } while (!(edge_value & GRAPH_LAST_EDGE));
963 return 1;
966 static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
968 struct commit_graph *cur_g = g;
969 uint32_t lex_index;
971 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
972 cur_g = cur_g->base_graph;
974 if (cur_g) {
975 *pos = lex_index + cur_g->num_commits_in_base;
976 return 1;
979 return 0;
982 static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
984 uint32_t graph_pos = commit_graph_position(item);
985 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
986 *pos = graph_pos;
987 return 1;
988 } else {
989 return search_commit_pos_in_graph(&item->object.oid, g, pos);
993 int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
994 uint32_t *pos)
996 if (!prepare_commit_graph(r))
997 return 0;
998 return find_commit_pos_in_graph(c, r->objects->commit_graph, pos);
1001 struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
1003 static int commit_graph_paranoia = -1;
1004 struct commit *commit;
1005 uint32_t pos;
1007 if (commit_graph_paranoia == -1)
1008 commit_graph_paranoia = git_env_bool(GIT_COMMIT_GRAPH_PARANOIA, 1);
1010 if (!prepare_commit_graph(repo))
1011 return NULL;
1012 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
1013 return NULL;
1014 if (commit_graph_paranoia && !has_object(repo, id, 0))
1015 return NULL;
1017 commit = lookup_commit(repo, id);
1018 if (!commit)
1019 return NULL;
1020 if (commit->object.parsed)
1021 return commit;
1023 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
1024 return NULL;
1026 return commit;
1029 static int parse_commit_in_graph_one(struct repository *r,
1030 struct commit_graph *g,
1031 struct commit *item)
1033 uint32_t pos;
1035 if (item->object.parsed)
1036 return 1;
1038 if (find_commit_pos_in_graph(item, g, &pos))
1039 return fill_commit_in_graph(r, item, g, pos);
1041 return 0;
1044 int parse_commit_in_graph(struct repository *r, struct commit *item)
1046 static int checked_env = 0;
1048 if (!checked_env &&
1049 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
1050 die("dying as requested by the '%s' variable on commit-graph parse!",
1051 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
1052 checked_env = 1;
1054 if (!prepare_commit_graph(r))
1055 return 0;
1056 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
1059 void load_commit_graph_info(struct repository *r, struct commit *item)
1061 uint32_t pos;
1062 if (repo_find_commit_pos_in_graph(r, item, &pos))
1063 fill_commit_graph_info(item, r->objects->commit_graph, pos);
1066 static struct tree *load_tree_for_commit(struct repository *r,
1067 struct commit_graph *g,
1068 struct commit *c)
1070 struct object_id oid;
1071 const unsigned char *commit_data;
1072 uint32_t graph_pos = commit_graph_position(c);
1074 while (graph_pos < g->num_commits_in_base)
1075 g = g->base_graph;
1077 commit_data = g->chunk_commit_data +
1078 st_mult(GRAPH_DATA_WIDTH, graph_pos - g->num_commits_in_base);
1080 oidread(&oid, commit_data);
1081 set_commit_tree(c, lookup_tree(r, &oid));
1083 return c->maybe_tree;
1086 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
1087 struct commit_graph *g,
1088 const struct commit *c)
1090 if (c->maybe_tree)
1091 return c->maybe_tree;
1092 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
1093 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
1095 return load_tree_for_commit(r, g, (struct commit *)c);
1098 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
1100 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
1103 struct packed_commit_list {
1104 struct commit **list;
1105 size_t nr;
1106 size_t alloc;
1109 struct write_commit_graph_context {
1110 struct repository *r;
1111 struct object_directory *odb;
1112 char *graph_name;
1113 struct oid_array oids;
1114 struct packed_commit_list commits;
1115 int num_extra_edges;
1116 int num_generation_data_overflows;
1117 unsigned long approx_nr_objects;
1118 struct progress *progress;
1119 int progress_done;
1120 uint64_t progress_cnt;
1122 char *base_graph_name;
1123 int num_commit_graphs_before;
1124 int num_commit_graphs_after;
1125 char **commit_graph_filenames_before;
1126 char **commit_graph_filenames_after;
1127 char **commit_graph_hash_after;
1128 uint32_t new_num_commits_in_base;
1129 struct commit_graph *new_base_graph;
1131 unsigned append:1,
1132 report_progress:1,
1133 split:1,
1134 changed_paths:1,
1135 order_by_pack:1,
1136 write_generation_data:1,
1137 trust_generation_numbers:1;
1139 struct topo_level_slab *topo_levels;
1140 const struct commit_graph_opts *opts;
1141 size_t total_bloom_filter_data_size;
1142 const struct bloom_filter_settings *bloom_settings;
1144 int count_bloom_filter_computed;
1145 int count_bloom_filter_not_computed;
1146 int count_bloom_filter_trunc_empty;
1147 int count_bloom_filter_trunc_large;
1150 static int write_graph_chunk_fanout(struct hashfile *f,
1151 void *data)
1153 struct write_commit_graph_context *ctx = data;
1154 int i, count = 0;
1155 struct commit **list = ctx->commits.list;
1158 * Write the first-level table (the list is sorted,
1159 * but we use a 256-entry lookup to be able to avoid
1160 * having to do eight extra binary search iterations).
1162 for (i = 0; i < 256; i++) {
1163 while (count < ctx->commits.nr) {
1164 if ((*list)->object.oid.hash[0] != i)
1165 break;
1166 display_progress(ctx->progress, ++ctx->progress_cnt);
1167 count++;
1168 list++;
1171 hashwrite_be32(f, count);
1174 return 0;
1177 static int write_graph_chunk_oids(struct hashfile *f,
1178 void *data)
1180 struct write_commit_graph_context *ctx = data;
1181 struct commit **list = ctx->commits.list;
1182 int count;
1183 for (count = 0; count < ctx->commits.nr; count++, list++) {
1184 display_progress(ctx->progress, ++ctx->progress_cnt);
1185 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1188 return 0;
1191 static const struct object_id *commit_to_oid(size_t index, const void *table)
1193 const struct commit * const *commits = table;
1194 return &commits[index]->object.oid;
1197 static int write_graph_chunk_data(struct hashfile *f,
1198 void *data)
1200 struct write_commit_graph_context *ctx = data;
1201 struct commit **list = ctx->commits.list;
1202 struct commit **last = ctx->commits.list + ctx->commits.nr;
1203 uint32_t num_extra_edges = 0;
1205 while (list < last) {
1206 struct commit_list *parent;
1207 struct object_id *tree;
1208 int edge_value;
1209 uint32_t packedDate[2];
1210 display_progress(ctx->progress, ++ctx->progress_cnt);
1212 if (repo_parse_commit_no_graph(ctx->r, *list))
1213 die(_("unable to parse commit %s"),
1214 oid_to_hex(&(*list)->object.oid));
1215 tree = get_commit_tree_oid(*list);
1216 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1218 parent = (*list)->parents;
1220 if (!parent)
1221 edge_value = GRAPH_PARENT_NONE;
1222 else {
1223 edge_value = oid_pos(&parent->item->object.oid,
1224 ctx->commits.list,
1225 ctx->commits.nr,
1226 commit_to_oid);
1228 if (edge_value >= 0)
1229 edge_value += ctx->new_num_commits_in_base;
1230 else if (ctx->new_base_graph) {
1231 uint32_t pos;
1232 if (find_commit_pos_in_graph(parent->item,
1233 ctx->new_base_graph,
1234 &pos))
1235 edge_value = pos;
1238 if (edge_value < 0)
1239 BUG("missing parent %s for commit %s",
1240 oid_to_hex(&parent->item->object.oid),
1241 oid_to_hex(&(*list)->object.oid));
1244 hashwrite_be32(f, edge_value);
1246 if (parent)
1247 parent = parent->next;
1249 if (!parent)
1250 edge_value = GRAPH_PARENT_NONE;
1251 else if (parent->next)
1252 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1253 else {
1254 edge_value = oid_pos(&parent->item->object.oid,
1255 ctx->commits.list,
1256 ctx->commits.nr,
1257 commit_to_oid);
1259 if (edge_value >= 0)
1260 edge_value += ctx->new_num_commits_in_base;
1261 else if (ctx->new_base_graph) {
1262 uint32_t pos;
1263 if (find_commit_pos_in_graph(parent->item,
1264 ctx->new_base_graph,
1265 &pos))
1266 edge_value = pos;
1269 if (edge_value < 0)
1270 BUG("missing parent %s for commit %s",
1271 oid_to_hex(&parent->item->object.oid),
1272 oid_to_hex(&(*list)->object.oid));
1275 hashwrite_be32(f, edge_value);
1277 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1278 do {
1279 num_extra_edges++;
1280 parent = parent->next;
1281 } while (parent);
1284 if (sizeof((*list)->date) > 4)
1285 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1286 else
1287 packedDate[0] = 0;
1289 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1291 packedDate[1] = htonl((*list)->date);
1292 hashwrite(f, packedDate, 8);
1294 list++;
1297 return 0;
1300 static int write_graph_chunk_generation_data(struct hashfile *f,
1301 void *data)
1303 struct write_commit_graph_context *ctx = data;
1304 int i, num_generation_data_overflows = 0;
1306 for (i = 0; i < ctx->commits.nr; i++) {
1307 struct commit *c = ctx->commits.list[i];
1308 timestamp_t offset;
1309 repo_parse_commit(ctx->r, c);
1310 offset = commit_graph_data_at(c)->generation - c->date;
1311 display_progress(ctx->progress, ++ctx->progress_cnt);
1313 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1314 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1315 num_generation_data_overflows++;
1318 hashwrite_be32(f, offset);
1321 return 0;
1324 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1325 void *data)
1327 struct write_commit_graph_context *ctx = data;
1328 int i;
1329 for (i = 0; i < ctx->commits.nr; i++) {
1330 struct commit *c = ctx->commits.list[i];
1331 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1332 display_progress(ctx->progress, ++ctx->progress_cnt);
1334 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1335 hashwrite_be32(f, offset >> 32);
1336 hashwrite_be32(f, (uint32_t) offset);
1340 return 0;
1343 static int write_graph_chunk_extra_edges(struct hashfile *f,
1344 void *data)
1346 struct write_commit_graph_context *ctx = data;
1347 struct commit **list = ctx->commits.list;
1348 struct commit **last = ctx->commits.list + ctx->commits.nr;
1349 struct commit_list *parent;
1351 while (list < last) {
1352 int num_parents = 0;
1354 display_progress(ctx->progress, ++ctx->progress_cnt);
1356 for (parent = (*list)->parents; num_parents < 3 && parent;
1357 parent = parent->next)
1358 num_parents++;
1360 if (num_parents <= 2) {
1361 list++;
1362 continue;
1365 /* Since num_parents > 2, this initializer is safe. */
1366 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1367 int edge_value = oid_pos(&parent->item->object.oid,
1368 ctx->commits.list,
1369 ctx->commits.nr,
1370 commit_to_oid);
1372 if (edge_value >= 0)
1373 edge_value += ctx->new_num_commits_in_base;
1374 else if (ctx->new_base_graph) {
1375 uint32_t pos;
1376 if (find_commit_pos_in_graph(parent->item,
1377 ctx->new_base_graph,
1378 &pos))
1379 edge_value = pos;
1382 if (edge_value < 0)
1383 BUG("missing parent %s for commit %s",
1384 oid_to_hex(&parent->item->object.oid),
1385 oid_to_hex(&(*list)->object.oid));
1386 else if (!parent->next)
1387 edge_value |= GRAPH_LAST_EDGE;
1389 hashwrite_be32(f, edge_value);
1392 list++;
1395 return 0;
1398 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1399 void *data)
1401 struct write_commit_graph_context *ctx = data;
1402 struct commit **list = ctx->commits.list;
1403 struct commit **last = ctx->commits.list + ctx->commits.nr;
1404 uint32_t cur_pos = 0;
1406 while (list < last) {
1407 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1408 size_t len = filter ? filter->len : 0;
1409 cur_pos += len;
1410 display_progress(ctx->progress, ++ctx->progress_cnt);
1411 hashwrite_be32(f, cur_pos);
1412 list++;
1415 return 0;
1418 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1420 struct json_writer jw = JSON_WRITER_INIT;
1422 jw_object_begin(&jw, 0);
1423 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1424 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1425 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1426 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1427 jw_end(&jw);
1429 trace2_data_json("bloom", ctx->r, "settings", &jw);
1431 jw_release(&jw);
1434 static int write_graph_chunk_bloom_data(struct hashfile *f,
1435 void *data)
1437 struct write_commit_graph_context *ctx = data;
1438 struct commit **list = ctx->commits.list;
1439 struct commit **last = ctx->commits.list + ctx->commits.nr;
1441 trace2_bloom_filter_settings(ctx);
1443 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1444 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1445 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1447 while (list < last) {
1448 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1449 size_t len = filter ? filter->len : 0;
1451 display_progress(ctx->progress, ++ctx->progress_cnt);
1452 if (len)
1453 hashwrite(f, filter->data, len * sizeof(unsigned char));
1454 list++;
1457 return 0;
1460 static int add_packed_commits(const struct object_id *oid,
1461 struct packed_git *pack,
1462 uint32_t pos,
1463 void *data)
1465 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1466 enum object_type type;
1467 off_t offset = nth_packed_object_offset(pack, pos);
1468 struct object_info oi = OBJECT_INFO_INIT;
1470 if (ctx->progress)
1471 display_progress(ctx->progress, ++ctx->progress_done);
1473 oi.typep = &type;
1474 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1475 die(_("unable to get type of object %s"), oid_to_hex(oid));
1477 if (type != OBJ_COMMIT)
1478 return 0;
1480 oid_array_append(&ctx->oids, oid);
1481 set_commit_pos(ctx->r, oid);
1483 return 0;
1486 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1488 struct commit_list *parent;
1489 for (parent = commit->parents; parent; parent = parent->next) {
1490 if (!(parent->item->object.flags & REACHABLE)) {
1491 oid_array_append(&ctx->oids, &parent->item->object.oid);
1492 parent->item->object.flags |= REACHABLE;
1497 static void close_reachable(struct write_commit_graph_context *ctx)
1499 int i;
1500 struct commit *commit;
1501 enum commit_graph_split_flags flags = ctx->opts ?
1502 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1504 if (ctx->report_progress)
1505 ctx->progress = start_delayed_progress(
1506 _("Loading known commits in commit graph"),
1507 ctx->oids.nr);
1508 for (i = 0; i < ctx->oids.nr; i++) {
1509 display_progress(ctx->progress, i + 1);
1510 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1511 if (commit)
1512 commit->object.flags |= REACHABLE;
1514 stop_progress(&ctx->progress);
1517 * As this loop runs, ctx->oids.nr may grow, but not more
1518 * than the number of missing commits in the reachable
1519 * closure.
1521 if (ctx->report_progress)
1522 ctx->progress = start_delayed_progress(
1523 _("Expanding reachable commits in commit graph"),
1525 for (i = 0; i < ctx->oids.nr; i++) {
1526 display_progress(ctx->progress, i + 1);
1527 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1529 if (!commit)
1530 continue;
1531 if (ctx->split) {
1532 if ((!repo_parse_commit(ctx->r, commit) &&
1533 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1534 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1535 add_missing_parents(ctx, commit);
1536 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1537 add_missing_parents(ctx, commit);
1539 stop_progress(&ctx->progress);
1541 if (ctx->report_progress)
1542 ctx->progress = start_delayed_progress(
1543 _("Clearing commit marks in commit graph"),
1544 ctx->oids.nr);
1545 for (i = 0; i < ctx->oids.nr; i++) {
1546 display_progress(ctx->progress, i + 1);
1547 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1549 if (commit)
1550 commit->object.flags &= ~REACHABLE;
1552 stop_progress(&ctx->progress);
1555 struct compute_generation_info {
1556 struct repository *r;
1557 struct packed_commit_list *commits;
1558 struct progress *progress;
1559 int progress_cnt;
1561 timestamp_t (*get_generation)(struct commit *c, void *data);
1562 void (*set_generation)(struct commit *c, timestamp_t gen, void *data);
1563 void *data;
1566 static timestamp_t compute_generation_from_max(struct commit *c,
1567 timestamp_t max_gen,
1568 int generation_version)
1570 switch (generation_version) {
1571 case 1: /* topological levels */
1572 if (max_gen > GENERATION_NUMBER_V1_MAX - 1)
1573 max_gen = GENERATION_NUMBER_V1_MAX - 1;
1574 return max_gen + 1;
1576 case 2: /* corrected commit date */
1577 if (c->date && c->date > max_gen)
1578 max_gen = c->date - 1;
1579 return max_gen + 1;
1581 default:
1582 BUG("attempting unimplemented version");
1586 static void compute_reachable_generation_numbers(
1587 struct compute_generation_info *info,
1588 int generation_version)
1590 int i;
1591 struct commit_list *list = NULL;
1593 for (i = 0; i < info->commits->nr; i++) {
1594 struct commit *c = info->commits->list[i];
1595 timestamp_t gen;
1596 repo_parse_commit(info->r, c);
1597 gen = info->get_generation(c, info->data);
1598 display_progress(info->progress, info->progress_cnt + 1);
1600 if (gen != GENERATION_NUMBER_ZERO && gen != GENERATION_NUMBER_INFINITY)
1601 continue;
1603 commit_list_insert(c, &list);
1604 while (list) {
1605 struct commit *current = list->item;
1606 struct commit_list *parent;
1607 int all_parents_computed = 1;
1608 uint32_t max_gen = 0;
1610 for (parent = current->parents; parent; parent = parent->next) {
1611 repo_parse_commit(info->r, parent->item);
1612 gen = info->get_generation(parent->item, info->data);
1614 if (gen == GENERATION_NUMBER_ZERO) {
1615 all_parents_computed = 0;
1616 commit_list_insert(parent->item, &list);
1617 break;
1620 if (gen > max_gen)
1621 max_gen = gen;
1624 if (all_parents_computed) {
1625 pop_commit(&list);
1626 gen = compute_generation_from_max(
1627 current, max_gen,
1628 generation_version);
1629 info->set_generation(current, gen, info->data);
1635 static timestamp_t get_topo_level(struct commit *c, void *data)
1637 struct write_commit_graph_context *ctx = data;
1638 return *topo_level_slab_at(ctx->topo_levels, c);
1641 static void set_topo_level(struct commit *c, timestamp_t t, void *data)
1643 struct write_commit_graph_context *ctx = data;
1644 *topo_level_slab_at(ctx->topo_levels, c) = (uint32_t)t;
1647 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1649 struct compute_generation_info info = {
1650 .r = ctx->r,
1651 .commits = &ctx->commits,
1652 .get_generation = get_topo_level,
1653 .set_generation = set_topo_level,
1654 .data = ctx,
1657 if (ctx->report_progress)
1658 info.progress = ctx->progress
1659 = start_delayed_progress(
1660 _("Computing commit graph topological levels"),
1661 ctx->commits.nr);
1663 compute_reachable_generation_numbers(&info, 1);
1665 stop_progress(&ctx->progress);
1668 static timestamp_t get_generation_from_graph_data(struct commit *c,
1669 void *data UNUSED)
1671 return commit_graph_data_at(c)->generation;
1674 static void set_generation_v2(struct commit *c, timestamp_t t,
1675 void *data UNUSED)
1677 struct commit_graph_data *g = commit_graph_data_at(c);
1678 g->generation = t;
1681 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1683 int i;
1684 struct compute_generation_info info = {
1685 .r = ctx->r,
1686 .commits = &ctx->commits,
1687 .get_generation = get_generation_from_graph_data,
1688 .set_generation = set_generation_v2,
1691 if (ctx->report_progress)
1692 info.progress = ctx->progress
1693 = start_delayed_progress(
1694 _("Computing commit graph generation numbers"),
1695 ctx->commits.nr);
1697 if (!ctx->trust_generation_numbers) {
1698 for (i = 0; i < ctx->commits.nr; i++) {
1699 struct commit *c = ctx->commits.list[i];
1700 repo_parse_commit(ctx->r, c);
1701 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1705 compute_reachable_generation_numbers(&info, 2);
1707 for (i = 0; i < ctx->commits.nr; i++) {
1708 struct commit *c = ctx->commits.list[i];
1709 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1710 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1711 ctx->num_generation_data_overflows++;
1713 stop_progress(&ctx->progress);
1716 static void set_generation_in_graph_data(struct commit *c, timestamp_t t,
1717 void *data UNUSED)
1719 commit_graph_data_at(c)->generation = t;
1723 * After this method, all commits reachable from those in the given
1724 * list will have non-zero, non-infinite generation numbers.
1726 void ensure_generations_valid(struct repository *r,
1727 struct commit **commits, size_t nr)
1729 int generation_version = get_configured_generation_version(r);
1730 struct packed_commit_list list = {
1731 .list = commits,
1732 .alloc = nr,
1733 .nr = nr,
1735 struct compute_generation_info info = {
1736 .r = r,
1737 .commits = &list,
1738 .get_generation = get_generation_from_graph_data,
1739 .set_generation = set_generation_in_graph_data,
1742 compute_reachable_generation_numbers(&info, generation_version);
1745 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1747 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1748 ctx->count_bloom_filter_computed);
1749 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1750 ctx->count_bloom_filter_not_computed);
1751 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1752 ctx->count_bloom_filter_trunc_empty);
1753 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1754 ctx->count_bloom_filter_trunc_large);
1757 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1759 int i;
1760 struct progress *progress = NULL;
1761 struct commit **sorted_commits;
1762 int max_new_filters;
1764 init_bloom_filters();
1766 if (ctx->report_progress)
1767 progress = start_delayed_progress(
1768 _("Computing commit changed paths Bloom filters"),
1769 ctx->commits.nr);
1771 DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1773 if (ctx->order_by_pack)
1774 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1775 else
1776 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1778 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1779 ctx->opts->max_new_filters : ctx->commits.nr;
1781 for (i = 0; i < ctx->commits.nr; i++) {
1782 enum bloom_filter_computed computed = 0;
1783 struct commit *c = sorted_commits[i];
1784 struct bloom_filter *filter = get_or_compute_bloom_filter(
1785 ctx->r,
1787 ctx->count_bloom_filter_computed < max_new_filters,
1788 ctx->bloom_settings,
1789 &computed);
1790 if (computed & BLOOM_COMPUTED) {
1791 ctx->count_bloom_filter_computed++;
1792 if (computed & BLOOM_TRUNC_EMPTY)
1793 ctx->count_bloom_filter_trunc_empty++;
1794 if (computed & BLOOM_TRUNC_LARGE)
1795 ctx->count_bloom_filter_trunc_large++;
1796 } else if (computed & BLOOM_NOT_COMPUTED)
1797 ctx->count_bloom_filter_not_computed++;
1798 ctx->total_bloom_filter_data_size += filter
1799 ? sizeof(unsigned char) * filter->len : 0;
1800 display_progress(progress, i + 1);
1803 if (trace2_is_enabled())
1804 trace2_bloom_filter_write_statistics(ctx);
1806 free(sorted_commits);
1807 stop_progress(&progress);
1810 struct refs_cb_data {
1811 struct oidset *commits;
1812 struct progress *progress;
1815 static int add_ref_to_set(const char *refname UNUSED,
1816 const struct object_id *oid,
1817 int flags UNUSED, void *cb_data)
1819 struct object_id peeled;
1820 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1822 if (!peel_iterated_oid(oid, &peeled))
1823 oid = &peeled;
1824 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1825 oidset_insert(data->commits, oid);
1827 display_progress(data->progress, oidset_size(data->commits));
1829 return 0;
1832 int write_commit_graph_reachable(struct object_directory *odb,
1833 enum commit_graph_write_flags flags,
1834 const struct commit_graph_opts *opts)
1836 struct oidset commits = OIDSET_INIT;
1837 struct refs_cb_data data;
1838 int result;
1840 memset(&data, 0, sizeof(data));
1841 data.commits = &commits;
1842 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1843 data.progress = start_delayed_progress(
1844 _("Collecting referenced commits"), 0);
1846 for_each_ref(add_ref_to_set, &data);
1848 stop_progress(&data.progress);
1850 result = write_commit_graph(odb, NULL, &commits,
1851 flags, opts);
1853 oidset_clear(&commits);
1854 return result;
1857 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1858 const struct string_list *pack_indexes)
1860 uint32_t i;
1861 struct strbuf progress_title = STRBUF_INIT;
1862 struct strbuf packname = STRBUF_INIT;
1863 int dirlen;
1864 int ret = 0;
1866 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1867 dirlen = packname.len;
1868 if (ctx->report_progress) {
1869 strbuf_addf(&progress_title,
1870 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1871 "Finding commits for commit graph in %"PRIuMAX" packs",
1872 pack_indexes->nr),
1873 (uintmax_t)pack_indexes->nr);
1874 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1875 ctx->progress_done = 0;
1877 for (i = 0; i < pack_indexes->nr; i++) {
1878 struct packed_git *p;
1879 strbuf_setlen(&packname, dirlen);
1880 strbuf_addstr(&packname, pack_indexes->items[i].string);
1881 p = add_packed_git(packname.buf, packname.len, 1);
1882 if (!p) {
1883 ret = error(_("error adding pack %s"), packname.buf);
1884 goto cleanup;
1886 if (open_pack_index(p)) {
1887 ret = error(_("error opening index for %s"), packname.buf);
1888 goto cleanup;
1890 for_each_object_in_pack(p, add_packed_commits, ctx,
1891 FOR_EACH_OBJECT_PACK_ORDER);
1892 close_pack(p);
1893 free(p);
1896 cleanup:
1897 stop_progress(&ctx->progress);
1898 strbuf_release(&progress_title);
1899 strbuf_release(&packname);
1901 return ret;
1904 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1905 struct oidset *commits)
1907 struct oidset_iter iter;
1908 struct object_id *oid;
1910 if (!oidset_size(commits))
1911 return 0;
1913 oidset_iter_init(commits, &iter);
1914 while ((oid = oidset_iter_next(&iter))) {
1915 oid_array_append(&ctx->oids, oid);
1918 return 0;
1921 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1923 if (ctx->report_progress)
1924 ctx->progress = start_delayed_progress(
1925 _("Finding commits for commit graph among packed objects"),
1926 ctx->approx_nr_objects);
1927 for_each_packed_object(add_packed_commits, ctx,
1928 FOR_EACH_OBJECT_PACK_ORDER);
1929 if (ctx->progress_done < ctx->approx_nr_objects)
1930 display_progress(ctx->progress, ctx->approx_nr_objects);
1931 stop_progress(&ctx->progress);
1934 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1936 uint32_t i;
1937 enum commit_graph_split_flags flags = ctx->opts ?
1938 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1940 ctx->num_extra_edges = 0;
1941 if (ctx->report_progress)
1942 ctx->progress = start_delayed_progress(
1943 _("Finding extra edges in commit graph"),
1944 ctx->oids.nr);
1945 oid_array_sort(&ctx->oids);
1946 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1947 unsigned int num_parents;
1949 display_progress(ctx->progress, i + 1);
1951 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1952 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1954 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1955 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1956 continue;
1958 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1959 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1960 else
1961 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1963 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1964 if (num_parents > 2)
1965 ctx->num_extra_edges += num_parents - 1;
1967 ctx->commits.nr++;
1969 stop_progress(&ctx->progress);
1972 static int write_graph_chunk_base_1(struct hashfile *f,
1973 struct commit_graph *g)
1975 int num = 0;
1977 if (!g)
1978 return 0;
1980 num = write_graph_chunk_base_1(f, g->base_graph);
1981 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1982 return num + 1;
1985 static int write_graph_chunk_base(struct hashfile *f,
1986 void *data)
1988 struct write_commit_graph_context *ctx = data;
1989 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1991 if (num != ctx->num_commit_graphs_after - 1) {
1992 error(_("failed to write correct number of base graph ids"));
1993 return -1;
1996 return 0;
1999 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
2001 uint32_t i;
2002 int fd;
2003 struct hashfile *f;
2004 struct lock_file lk = LOCK_INIT;
2005 const unsigned hashsz = the_hash_algo->rawsz;
2006 struct strbuf progress_title = STRBUF_INIT;
2007 struct chunkfile *cf;
2008 unsigned char file_hash[GIT_MAX_RAWSZ];
2010 if (ctx->split) {
2011 struct strbuf tmp_file = STRBUF_INIT;
2013 strbuf_addf(&tmp_file,
2014 "%s/info/commit-graphs/tmp_graph_XXXXXX",
2015 ctx->odb->path);
2016 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
2017 } else {
2018 ctx->graph_name = get_commit_graph_filename(ctx->odb);
2021 if (safe_create_leading_directories(ctx->graph_name)) {
2022 UNLEAK(ctx->graph_name);
2023 error(_("unable to create leading directories of %s"),
2024 ctx->graph_name);
2025 return -1;
2028 if (ctx->split) {
2029 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
2031 hold_lock_file_for_update_mode(&lk, lock_name,
2032 LOCK_DIE_ON_ERROR, 0444);
2033 free(lock_name);
2035 fd = git_mkstemp_mode(ctx->graph_name, 0444);
2036 if (fd < 0) {
2037 error(_("unable to create temporary graph layer"));
2038 return -1;
2041 if (adjust_shared_perm(ctx->graph_name)) {
2042 error(_("unable to adjust shared permissions for '%s'"),
2043 ctx->graph_name);
2044 return -1;
2047 f = hashfd(fd, ctx->graph_name);
2048 } else {
2049 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
2050 LOCK_DIE_ON_ERROR, 0444);
2051 fd = get_lock_file_fd(&lk);
2052 f = hashfd(fd, get_lock_file_path(&lk));
2055 cf = init_chunkfile(f);
2057 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
2058 write_graph_chunk_fanout);
2059 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, st_mult(hashsz, ctx->commits.nr),
2060 write_graph_chunk_oids);
2061 add_chunk(cf, GRAPH_CHUNKID_DATA, st_mult(hashsz + 16, ctx->commits.nr),
2062 write_graph_chunk_data);
2064 if (ctx->write_generation_data)
2065 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
2066 st_mult(sizeof(uint32_t), ctx->commits.nr),
2067 write_graph_chunk_generation_data);
2068 if (ctx->num_generation_data_overflows)
2069 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
2070 st_mult(sizeof(timestamp_t), ctx->num_generation_data_overflows),
2071 write_graph_chunk_generation_data_overflow);
2072 if (ctx->num_extra_edges)
2073 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
2074 st_mult(4, ctx->num_extra_edges),
2075 write_graph_chunk_extra_edges);
2076 if (ctx->changed_paths) {
2077 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
2078 st_mult(sizeof(uint32_t), ctx->commits.nr),
2079 write_graph_chunk_bloom_indexes);
2080 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
2081 st_add(sizeof(uint32_t) * 3,
2082 ctx->total_bloom_filter_data_size),
2083 write_graph_chunk_bloom_data);
2085 if (ctx->num_commit_graphs_after > 1)
2086 add_chunk(cf, GRAPH_CHUNKID_BASE,
2087 st_mult(hashsz, ctx->num_commit_graphs_after - 1),
2088 write_graph_chunk_base);
2090 hashwrite_be32(f, GRAPH_SIGNATURE);
2092 hashwrite_u8(f, GRAPH_VERSION);
2093 hashwrite_u8(f, oid_version(the_hash_algo));
2094 hashwrite_u8(f, get_num_chunks(cf));
2095 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
2097 if (ctx->report_progress) {
2098 strbuf_addf(&progress_title,
2099 Q_("Writing out commit graph in %d pass",
2100 "Writing out commit graph in %d passes",
2101 get_num_chunks(cf)),
2102 get_num_chunks(cf));
2103 ctx->progress = start_delayed_progress(
2104 progress_title.buf,
2105 st_mult(get_num_chunks(cf), ctx->commits.nr));
2108 write_chunkfile(cf, ctx);
2110 stop_progress(&ctx->progress);
2111 strbuf_release(&progress_title);
2113 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
2114 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
2115 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
2117 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
2118 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
2119 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
2120 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
2123 close_commit_graph(ctx->r->objects);
2124 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
2125 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
2126 free_chunkfile(cf);
2128 if (ctx->split) {
2129 FILE *chainf = fdopen_lock_file(&lk, "w");
2130 char *final_graph_name;
2131 int result;
2133 close(fd);
2135 if (!chainf) {
2136 error(_("unable to open commit-graph chain file"));
2137 return -1;
2140 if (ctx->base_graph_name) {
2141 const char *dest;
2142 int idx = ctx->num_commit_graphs_after - 1;
2143 if (ctx->num_commit_graphs_after > 1)
2144 idx--;
2146 dest = ctx->commit_graph_filenames_after[idx];
2148 if (strcmp(ctx->base_graph_name, dest)) {
2149 result = rename(ctx->base_graph_name, dest);
2151 if (result) {
2152 error(_("failed to rename base commit-graph file"));
2153 return -1;
2156 } else {
2157 char *graph_name = get_commit_graph_filename(ctx->odb);
2158 unlink(graph_name);
2159 free(graph_name);
2162 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2163 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
2164 final_graph_name = get_split_graph_filename(ctx->odb,
2165 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2166 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1]);
2167 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
2169 result = rename(ctx->graph_name, final_graph_name);
2171 for (i = 0; i < ctx->num_commit_graphs_after; i++)
2172 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2174 if (result) {
2175 error(_("failed to rename temporary commit-graph file"));
2176 return -1;
2180 commit_lock_file(&lk);
2182 return 0;
2185 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2187 struct commit_graph *g;
2188 uint32_t num_commits;
2189 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2190 uint32_t i;
2192 int max_commits = 0;
2193 int size_mult = 2;
2195 if (ctx->opts) {
2196 max_commits = ctx->opts->max_commits;
2198 if (ctx->opts->size_multiple)
2199 size_mult = ctx->opts->size_multiple;
2201 flags = ctx->opts->split_flags;
2204 g = ctx->r->objects->commit_graph;
2205 num_commits = ctx->commits.nr;
2206 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2207 ctx->num_commit_graphs_after = 1;
2208 else
2209 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2211 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2212 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2213 while (g && (g->num_commits <= st_mult(size_mult, num_commits) ||
2214 (max_commits && num_commits > max_commits))) {
2215 if (g->odb != ctx->odb)
2216 break;
2218 if (unsigned_add_overflows(num_commits, g->num_commits))
2219 die(_("cannot merge graphs with %"PRIuMAX", "
2220 "%"PRIuMAX" commits"),
2221 (uintmax_t)num_commits,
2222 (uintmax_t)g->num_commits);
2223 num_commits += g->num_commits;
2224 g = g->base_graph;
2226 ctx->num_commit_graphs_after--;
2230 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2231 ctx->new_base_graph = g;
2232 else if (ctx->num_commit_graphs_after != 1)
2233 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2234 "should be 1 with --split=replace");
2236 if (ctx->num_commit_graphs_after == 2) {
2237 char *old_graph_name = get_commit_graph_filename(g->odb);
2239 if (!strcmp(g->filename, old_graph_name) &&
2240 g->odb != ctx->odb) {
2241 ctx->num_commit_graphs_after = 1;
2242 ctx->new_base_graph = NULL;
2245 free(old_graph_name);
2248 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2249 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2251 for (i = 0; i < ctx->num_commit_graphs_after &&
2252 i < ctx->num_commit_graphs_before; i++)
2253 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2255 i = ctx->num_commit_graphs_before - 1;
2256 g = ctx->r->objects->commit_graph;
2258 while (g) {
2259 if (i < ctx->num_commit_graphs_after)
2260 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2263 * If the topmost remaining layer has generation data chunk, the
2264 * resultant layer also has generation data chunk.
2266 if (i == ctx->num_commit_graphs_after - 2)
2267 ctx->write_generation_data = !!g->chunk_generation_data;
2269 i--;
2270 g = g->base_graph;
2274 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2275 struct commit_graph *g)
2277 uint32_t i;
2278 uint32_t offset = g->num_commits_in_base;
2280 if (unsigned_add_overflows(ctx->commits.nr, g->num_commits))
2281 die(_("cannot merge graph %s, too many commits: %"PRIuMAX),
2282 oid_to_hex(&g->oid),
2283 (uintmax_t)st_add(ctx->commits.nr, g->num_commits));
2285 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2287 for (i = 0; i < g->num_commits; i++) {
2288 struct object_id oid;
2289 struct commit *result;
2291 display_progress(ctx->progress, i + 1);
2293 load_oid_from_graph(g, i + offset, &oid);
2295 /* only add commits if they still exist in the repo */
2296 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2298 if (result) {
2299 ctx->commits.list[ctx->commits.nr] = result;
2300 ctx->commits.nr++;
2305 static int commit_compare(const void *_a, const void *_b)
2307 const struct commit *a = *(const struct commit **)_a;
2308 const struct commit *b = *(const struct commit **)_b;
2309 return oidcmp(&a->object.oid, &b->object.oid);
2312 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2314 uint32_t i, dedup_i = 0;
2316 if (ctx->report_progress)
2317 ctx->progress = start_delayed_progress(
2318 _("Scanning merged commits"),
2319 ctx->commits.nr);
2321 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2323 ctx->num_extra_edges = 0;
2324 for (i = 0; i < ctx->commits.nr; i++) {
2325 display_progress(ctx->progress, i + 1);
2327 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2328 &ctx->commits.list[i]->object.oid)) {
2330 * Silently ignore duplicates. These were likely
2331 * created due to a commit appearing in multiple
2332 * layers of the chain, which is unexpected but
2333 * not invalid. We should make sure there is a
2334 * unique copy in the new layer.
2336 } else {
2337 unsigned int num_parents;
2339 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2340 dedup_i++;
2342 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2343 if (num_parents > 2)
2344 ctx->num_extra_edges += num_parents - 1;
2348 ctx->commits.nr = dedup_i;
2350 stop_progress(&ctx->progress);
2353 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2355 struct commit_graph *g = ctx->r->objects->commit_graph;
2356 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2358 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2359 current_graph_number--;
2361 if (ctx->report_progress)
2362 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2364 merge_commit_graph(ctx, g);
2365 stop_progress(&ctx->progress);
2367 g = g->base_graph;
2370 if (g) {
2371 ctx->new_base_graph = g;
2372 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2375 if (ctx->new_base_graph)
2376 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2378 sort_and_scan_merged_commits(ctx);
2381 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2383 uint32_t i;
2384 time_t now = time(NULL);
2386 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2387 struct stat st;
2388 struct utimbuf updated_time;
2390 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2391 continue;
2393 updated_time.actime = st.st_atime;
2394 updated_time.modtime = now;
2395 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2399 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2401 struct strbuf path = STRBUF_INIT;
2402 DIR *dir;
2403 struct dirent *de;
2404 size_t dirnamelen;
2405 timestamp_t expire_time = time(NULL);
2407 if (ctx->opts && ctx->opts->expire_time)
2408 expire_time = ctx->opts->expire_time;
2409 if (!ctx->split) {
2410 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2411 unlink(chain_file_name);
2412 free(chain_file_name);
2413 ctx->num_commit_graphs_after = 0;
2416 strbuf_addstr(&path, ctx->odb->path);
2417 strbuf_addstr(&path, "/info/commit-graphs");
2418 dir = opendir(path.buf);
2420 if (!dir)
2421 goto out;
2423 strbuf_addch(&path, '/');
2424 dirnamelen = path.len;
2425 while ((de = readdir(dir)) != NULL) {
2426 struct stat st;
2427 uint32_t i, found = 0;
2429 strbuf_setlen(&path, dirnamelen);
2430 strbuf_addstr(&path, de->d_name);
2432 if (stat(path.buf, &st) < 0)
2433 continue;
2435 if (st.st_mtime > expire_time)
2436 continue;
2437 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2438 continue;
2440 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2441 if (!strcmp(ctx->commit_graph_filenames_after[i],
2442 path.buf)) {
2443 found = 1;
2444 break;
2448 if (!found)
2449 unlink(path.buf);
2452 out:
2453 if(dir)
2454 closedir(dir);
2455 strbuf_release(&path);
2458 int write_commit_graph(struct object_directory *odb,
2459 const struct string_list *const pack_indexes,
2460 struct oidset *commits,
2461 enum commit_graph_write_flags flags,
2462 const struct commit_graph_opts *opts)
2464 struct repository *r = the_repository;
2465 struct write_commit_graph_context *ctx;
2466 uint32_t i;
2467 int res = 0;
2468 int replace = 0;
2469 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2470 struct topo_level_slab topo_levels;
2472 prepare_repo_settings(r);
2473 if (!r->settings.core_commit_graph) {
2474 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2475 return 0;
2477 if (!commit_graph_compatible(r))
2478 return 0;
2480 CALLOC_ARRAY(ctx, 1);
2481 ctx->r = r;
2482 ctx->odb = odb;
2483 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2484 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2485 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2486 ctx->opts = opts;
2487 ctx->total_bloom_filter_data_size = 0;
2488 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2489 ctx->num_generation_data_overflows = 0;
2491 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2492 bloom_settings.bits_per_entry);
2493 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2494 bloom_settings.num_hashes);
2495 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2496 bloom_settings.max_changed_paths);
2497 ctx->bloom_settings = &bloom_settings;
2499 init_topo_level_slab(&topo_levels);
2500 ctx->topo_levels = &topo_levels;
2502 prepare_commit_graph(ctx->r);
2503 if (ctx->r->objects->commit_graph) {
2504 struct commit_graph *g = ctx->r->objects->commit_graph;
2506 while (g) {
2507 g->topo_levels = &topo_levels;
2508 g = g->base_graph;
2512 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2513 ctx->changed_paths = 1;
2514 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2515 struct commit_graph *g;
2517 g = ctx->r->objects->commit_graph;
2519 /* We have changed-paths already. Keep them in the next graph */
2520 if (g && g->chunk_bloom_data) {
2521 ctx->changed_paths = 1;
2522 ctx->bloom_settings = g->bloom_filter_settings;
2526 if (ctx->split) {
2527 struct commit_graph *g = ctx->r->objects->commit_graph;
2529 while (g) {
2530 ctx->num_commit_graphs_before++;
2531 g = g->base_graph;
2534 if (ctx->num_commit_graphs_before) {
2535 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2536 i = ctx->num_commit_graphs_before;
2537 g = ctx->r->objects->commit_graph;
2539 while (g) {
2540 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2541 g = g->base_graph;
2545 if (ctx->opts)
2546 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2549 ctx->approx_nr_objects = repo_approximate_object_count(the_repository);
2551 if (ctx->append && ctx->r->objects->commit_graph) {
2552 struct commit_graph *g = ctx->r->objects->commit_graph;
2553 for (i = 0; i < g->num_commits; i++) {
2554 struct object_id oid;
2555 oidread(&oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2556 oid_array_append(&ctx->oids, &oid);
2560 if (pack_indexes) {
2561 ctx->order_by_pack = 1;
2562 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2563 goto cleanup;
2566 if (commits) {
2567 if ((res = fill_oids_from_commits(ctx, commits)))
2568 goto cleanup;
2571 if (!pack_indexes && !commits) {
2572 ctx->order_by_pack = 1;
2573 fill_oids_from_all_packs(ctx);
2576 close_reachable(ctx);
2578 copy_oids_to_commits(ctx);
2580 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2581 error(_("too many commits to write graph"));
2582 res = -1;
2583 goto cleanup;
2586 if (!ctx->commits.nr && !replace)
2587 goto cleanup;
2589 if (ctx->split) {
2590 split_graph_merge_strategy(ctx);
2592 if (!replace)
2593 merge_commit_graphs(ctx);
2594 } else
2595 ctx->num_commit_graphs_after = 1;
2597 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2599 compute_topological_levels(ctx);
2600 if (ctx->write_generation_data)
2601 compute_generation_numbers(ctx);
2603 if (ctx->changed_paths)
2604 compute_bloom_filters(ctx);
2606 res = write_commit_graph_file(ctx);
2608 if (ctx->split)
2609 mark_commit_graphs(ctx);
2611 expire_commit_graphs(ctx);
2613 cleanup:
2614 free(ctx->graph_name);
2615 free(ctx->base_graph_name);
2616 free(ctx->commits.list);
2617 oid_array_clear(&ctx->oids);
2618 clear_topo_level_slab(&topo_levels);
2620 if (ctx->commit_graph_filenames_after) {
2621 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2622 free(ctx->commit_graph_filenames_after[i]);
2623 free(ctx->commit_graph_hash_after[i]);
2626 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2627 free(ctx->commit_graph_filenames_before[i]);
2629 free(ctx->commit_graph_filenames_after);
2630 free(ctx->commit_graph_filenames_before);
2631 free(ctx->commit_graph_hash_after);
2634 free(ctx);
2636 return res;
2639 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2640 static int verify_commit_graph_error;
2642 __attribute__((format (printf, 1, 2)))
2643 static void graph_report(const char *fmt, ...)
2645 va_list ap;
2647 verify_commit_graph_error = 1;
2648 va_start(ap, fmt);
2649 vfprintf(stderr, fmt, ap);
2650 fprintf(stderr, "\n");
2651 va_end(ap);
2654 static int commit_graph_checksum_valid(struct commit_graph *g)
2656 return hashfile_checksum_valid(g->data, g->data_len);
2659 static int verify_one_commit_graph(struct repository *r,
2660 struct commit_graph *g,
2661 struct progress *progress,
2662 uint64_t *seen)
2664 uint32_t i, cur_fanout_pos = 0;
2665 struct object_id prev_oid, cur_oid;
2666 struct commit *seen_gen_zero = NULL;
2667 struct commit *seen_gen_non_zero = NULL;
2669 if (!commit_graph_checksum_valid(g)) {
2670 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2671 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2674 for (i = 0; i < g->num_commits; i++) {
2675 struct commit *graph_commit;
2677 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2679 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2680 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2681 oid_to_hex(&prev_oid),
2682 oid_to_hex(&cur_oid));
2684 oidcpy(&prev_oid, &cur_oid);
2686 while (cur_oid.hash[0] > cur_fanout_pos) {
2687 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2689 if (i != fanout_value)
2690 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2691 cur_fanout_pos, fanout_value, i);
2692 cur_fanout_pos++;
2695 graph_commit = lookup_commit(r, &cur_oid);
2696 if (!parse_commit_in_graph_one(r, g, graph_commit))
2697 graph_report(_("failed to parse commit %s from commit-graph"),
2698 oid_to_hex(&cur_oid));
2701 while (cur_fanout_pos < 256) {
2702 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2704 if (g->num_commits != fanout_value)
2705 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2706 cur_fanout_pos, fanout_value, i);
2708 cur_fanout_pos++;
2711 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2712 return verify_commit_graph_error;
2714 for (i = 0; i < g->num_commits; i++) {
2715 struct commit *graph_commit, *odb_commit;
2716 struct commit_list *graph_parents, *odb_parents;
2717 timestamp_t max_generation = 0;
2718 timestamp_t generation;
2720 display_progress(progress, ++(*seen));
2721 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2723 graph_commit = lookup_commit(r, &cur_oid);
2724 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2725 if (repo_parse_commit_internal(r, odb_commit, 0, 0)) {
2726 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2727 oid_to_hex(&cur_oid));
2728 continue;
2731 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2732 get_commit_tree_oid(odb_commit)))
2733 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2734 oid_to_hex(&cur_oid),
2735 oid_to_hex(get_commit_tree_oid(graph_commit)),
2736 oid_to_hex(get_commit_tree_oid(odb_commit)));
2738 graph_parents = graph_commit->parents;
2739 odb_parents = odb_commit->parents;
2741 while (graph_parents) {
2742 if (!odb_parents) {
2743 graph_report(_("commit-graph parent list for commit %s is too long"),
2744 oid_to_hex(&cur_oid));
2745 break;
2748 /* parse parent in case it is in a base graph */
2749 parse_commit_in_graph_one(r, g, graph_parents->item);
2751 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2752 graph_report(_("commit-graph parent for %s is %s != %s"),
2753 oid_to_hex(&cur_oid),
2754 oid_to_hex(&graph_parents->item->object.oid),
2755 oid_to_hex(&odb_parents->item->object.oid));
2757 generation = commit_graph_generation_from_graph(graph_parents->item);
2758 if (generation > max_generation)
2759 max_generation = generation;
2761 graph_parents = graph_parents->next;
2762 odb_parents = odb_parents->next;
2765 if (odb_parents)
2766 graph_report(_("commit-graph parent list for commit %s terminates early"),
2767 oid_to_hex(&cur_oid));
2769 if (commit_graph_generation_from_graph(graph_commit))
2770 seen_gen_non_zero = graph_commit;
2771 else
2772 seen_gen_zero = graph_commit;
2774 if (seen_gen_zero)
2775 continue;
2778 * If we are using topological level and one of our parents has
2779 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2780 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2781 * in the following condition.
2783 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2784 max_generation--;
2786 generation = commit_graph_generation(graph_commit);
2787 if (generation < max_generation + 1)
2788 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2789 oid_to_hex(&cur_oid),
2790 generation,
2791 max_generation + 1);
2793 if (graph_commit->date != odb_commit->date)
2794 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2795 oid_to_hex(&cur_oid),
2796 graph_commit->date,
2797 odb_commit->date);
2800 if (seen_gen_zero && seen_gen_non_zero)
2801 graph_report(_("commit-graph has both zero and non-zero "
2802 "generations (e.g., commits '%s' and '%s')"),
2803 oid_to_hex(&seen_gen_zero->object.oid),
2804 oid_to_hex(&seen_gen_non_zero->object.oid));
2806 return verify_commit_graph_error;
2809 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2811 struct progress *progress = NULL;
2812 int local_error = 0;
2813 uint64_t seen = 0;
2815 if (!g) {
2816 graph_report("no commit-graph file loaded");
2817 return 1;
2820 if (flags & COMMIT_GRAPH_WRITE_PROGRESS) {
2821 uint64_t total = g->num_commits;
2822 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW))
2823 total += g->num_commits_in_base;
2825 progress = start_progress(_("Verifying commits in commit graph"),
2826 total);
2829 for (; g; g = g->base_graph) {
2830 local_error |= verify_one_commit_graph(r, g, progress, &seen);
2831 if (flags & COMMIT_GRAPH_VERIFY_SHALLOW)
2832 break;
2835 stop_progress(&progress);
2837 return local_error;
2840 void free_commit_graph(struct commit_graph *g)
2842 while (g) {
2843 struct commit_graph *next = g->base_graph;
2845 if (g->data)
2846 munmap((void *)g->data, g->data_len);
2847 free(g->filename);
2848 free(g->bloom_filter_settings);
2849 free(g);
2851 g = next;
2855 void disable_commit_graph(struct repository *r)
2857 r->commit_graph_disabled = 1;