Merge branch 'fc/vimdiff-layout-vimdiff3-fix' into maint
[git.git] / commit-graph.c
bloba487d49c3e48a5eea0bee945d07cd2e37723994c
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "lockfile.h"
4 #include "pack.h"
5 #include "packfile.h"
6 #include "commit.h"
7 #include "object.h"
8 #include "refs.h"
9 #include "revision.h"
10 #include "hash-lookup.h"
11 #include "commit-graph.h"
12 #include "object-store.h"
13 #include "alloc.h"
14 #include "hashmap.h"
15 #include "replace-object.h"
16 #include "progress.h"
17 #include "bloom.h"
18 #include "commit-slab.h"
19 #include "shallow.h"
20 #include "json-writer.h"
21 #include "trace2.h"
22 #include "chunk-format.h"
24 void git_test_write_commit_graph_or_die(void)
26 int flags = 0;
27 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
28 return;
30 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
31 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
33 if (write_commit_graph_reachable(the_repository->objects->odb,
34 flags, NULL))
35 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
38 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
39 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
40 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
41 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
42 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
43 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
44 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
45 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
46 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
47 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
49 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
51 #define GRAPH_VERSION_1 0x1
52 #define GRAPH_VERSION GRAPH_VERSION_1
54 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
55 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
56 #define GRAPH_PARENT_NONE 0x70000000
58 #define GRAPH_LAST_EDGE 0x80000000
60 #define GRAPH_HEADER_SIZE 8
61 #define GRAPH_FANOUT_SIZE (4 * 256)
62 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
63 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
65 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
67 /* Remember to update object flag allocation in object.h */
68 #define REACHABLE (1u<<15)
70 define_commit_slab(topo_level_slab, uint32_t);
72 /* Keep track of the order in which commits are added to our list. */
73 define_commit_slab(commit_pos, int);
74 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
76 static void set_commit_pos(struct repository *r, const struct object_id *oid)
78 static int32_t max_pos;
79 struct commit *commit = lookup_commit(r, oid);
81 if (!commit)
82 return; /* should never happen, but be lenient */
84 *commit_pos_at(&commit_pos, commit) = max_pos++;
87 static int commit_pos_cmp(const void *va, const void *vb)
89 const struct commit *a = *(const struct commit **)va;
90 const struct commit *b = *(const struct commit **)vb;
91 return commit_pos_at(&commit_pos, a) -
92 commit_pos_at(&commit_pos, b);
95 define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
96 static struct commit_graph_data_slab commit_graph_data_slab =
97 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
99 static int get_configured_generation_version(struct repository *r)
101 int version = 2;
102 repo_config_get_int(r, "commitgraph.generationversion", &version);
103 return version;
106 uint32_t commit_graph_position(const struct commit *c)
108 struct commit_graph_data *data =
109 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
111 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
114 timestamp_t commit_graph_generation(const struct commit *c)
116 struct commit_graph_data *data =
117 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
119 if (!data)
120 return GENERATION_NUMBER_INFINITY;
121 else if (data->graph_pos == COMMIT_NOT_FROM_GRAPH)
122 return GENERATION_NUMBER_INFINITY;
124 return data->generation;
127 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
129 unsigned int i, nth_slab;
130 struct commit_graph_data *data =
131 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
133 if (data)
134 return data;
136 nth_slab = c->index / commit_graph_data_slab.slab_size;
137 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
140 * commit-slab initializes elements with zero, overwrite this with
141 * COMMIT_NOT_FROM_GRAPH for graph_pos.
143 * We avoid initializing generation with checking if graph position
144 * is not COMMIT_NOT_FROM_GRAPH.
146 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
147 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
148 COMMIT_NOT_FROM_GRAPH;
151 return data;
155 * Should be used only while writing commit-graph as it compares
156 * generation value of commits by directly accessing commit-slab.
158 static int commit_gen_cmp(const void *va, const void *vb)
160 const struct commit *a = *(const struct commit **)va;
161 const struct commit *b = *(const struct commit **)vb;
163 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
164 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
165 /* lower generation commits first */
166 if (generation_a < generation_b)
167 return -1;
168 else if (generation_a > generation_b)
169 return 1;
171 /* use date as a heuristic when generations are equal */
172 if (a->date < b->date)
173 return -1;
174 else if (a->date > b->date)
175 return 1;
176 return 0;
179 char *get_commit_graph_filename(struct object_directory *obj_dir)
181 return xstrfmt("%s/info/commit-graph", obj_dir->path);
184 static char *get_split_graph_filename(struct object_directory *odb,
185 const char *oid_hex)
187 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
188 oid_hex);
191 char *get_commit_graph_chain_filename(struct object_directory *odb)
193 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
196 static struct commit_graph *alloc_commit_graph(void)
198 struct commit_graph *g = xcalloc(1, sizeof(*g));
200 return g;
203 extern int read_replace_refs;
205 static int commit_graph_compatible(struct repository *r)
207 if (!r->gitdir)
208 return 0;
210 if (read_replace_refs) {
211 prepare_replace_object(r);
212 if (hashmap_get_size(&r->objects->replace_map->map))
213 return 0;
216 prepare_commit_graft(r);
217 if (r->parsed_objects &&
218 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
219 return 0;
220 if (is_repository_shallow(r))
221 return 0;
223 return 1;
226 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
228 *fd = git_open(graph_file);
229 if (*fd < 0)
230 return 0;
231 if (fstat(*fd, st)) {
232 close(*fd);
233 return 0;
235 return 1;
238 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
239 int fd, struct stat *st,
240 struct object_directory *odb)
242 void *graph_map;
243 size_t graph_size;
244 struct commit_graph *ret;
246 graph_size = xsize_t(st->st_size);
248 if (graph_size < GRAPH_MIN_SIZE) {
249 close(fd);
250 error(_("commit-graph file is too small"));
251 return NULL;
253 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
254 close(fd);
255 ret = parse_commit_graph(r, graph_map, graph_size);
257 if (ret)
258 ret->odb = odb;
259 else
260 munmap(graph_map, graph_size);
262 return ret;
265 static int verify_commit_graph_lite(struct commit_graph *g)
268 * Basic validation shared between parse_commit_graph()
269 * which'll be called every time the graph is used, and the
270 * much more expensive verify_commit_graph() used by
271 * "commit-graph verify".
273 * There should only be very basic checks here to ensure that
274 * we don't e.g. segfault in fill_commit_in_graph(), but
275 * because this is a very hot codepath nothing that e.g. loops
276 * over g->num_commits, or runs a checksum on the commit-graph
277 * itself.
279 if (!g->chunk_oid_fanout) {
280 error("commit-graph is missing the OID Fanout chunk");
281 return 1;
283 if (!g->chunk_oid_lookup) {
284 error("commit-graph is missing the OID Lookup chunk");
285 return 1;
287 if (!g->chunk_commit_data) {
288 error("commit-graph is missing the Commit Data chunk");
289 return 1;
292 return 0;
295 static int graph_read_oid_lookup(const unsigned char *chunk_start,
296 size_t chunk_size, void *data)
298 struct commit_graph *g = data;
299 g->chunk_oid_lookup = chunk_start;
300 g->num_commits = chunk_size / g->hash_len;
301 return 0;
304 static int graph_read_bloom_data(const unsigned char *chunk_start,
305 size_t chunk_size, void *data)
307 struct commit_graph *g = data;
308 uint32_t hash_version;
309 g->chunk_bloom_data = chunk_start;
310 hash_version = get_be32(chunk_start);
312 if (hash_version != 1)
313 return 0;
315 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
316 g->bloom_filter_settings->hash_version = hash_version;
317 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
318 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
319 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
321 return 0;
324 struct commit_graph *parse_commit_graph(struct repository *r,
325 void *graph_map, size_t graph_size)
327 const unsigned char *data;
328 struct commit_graph *graph;
329 uint32_t graph_signature;
330 unsigned char graph_version, hash_version;
331 struct chunkfile *cf = NULL;
333 if (!graph_map)
334 return NULL;
336 if (graph_size < GRAPH_MIN_SIZE)
337 return NULL;
339 data = (const unsigned char *)graph_map;
341 graph_signature = get_be32(data);
342 if (graph_signature != GRAPH_SIGNATURE) {
343 error(_("commit-graph signature %X does not match signature %X"),
344 graph_signature, GRAPH_SIGNATURE);
345 return NULL;
348 graph_version = *(unsigned char*)(data + 4);
349 if (graph_version != GRAPH_VERSION) {
350 error(_("commit-graph version %X does not match version %X"),
351 graph_version, GRAPH_VERSION);
352 return NULL;
355 hash_version = *(unsigned char*)(data + 5);
356 if (hash_version != oid_version(the_hash_algo)) {
357 error(_("commit-graph hash version %X does not match version %X"),
358 hash_version, oid_version(the_hash_algo));
359 return NULL;
362 prepare_repo_settings(r);
364 graph = alloc_commit_graph();
366 graph->hash_len = the_hash_algo->rawsz;
367 graph->num_chunks = *(unsigned char*)(data + 6);
368 graph->data = graph_map;
369 graph->data_len = graph_size;
371 if (graph_size < GRAPH_HEADER_SIZE +
372 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
373 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
374 error(_("commit-graph file is too small to hold %u chunks"),
375 graph->num_chunks);
376 free(graph);
377 return NULL;
380 cf = init_chunkfile(NULL);
382 if (read_table_of_contents(cf, graph->data, graph_size,
383 GRAPH_HEADER_SIZE, graph->num_chunks))
384 goto free_and_return;
386 pair_chunk(cf, GRAPH_CHUNKID_OIDFANOUT,
387 (const unsigned char **)&graph->chunk_oid_fanout);
388 read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph);
389 pair_chunk(cf, GRAPH_CHUNKID_DATA, &graph->chunk_commit_data);
390 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges);
391 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs);
393 if (get_configured_generation_version(r) >= 2) {
394 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
395 &graph->chunk_generation_data);
396 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
397 &graph->chunk_generation_data_overflow);
399 if (graph->chunk_generation_data)
400 graph->read_generation_data = 1;
403 if (r->settings.commit_graph_read_changed_paths) {
404 pair_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
405 &graph->chunk_bloom_indexes);
406 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
407 graph_read_bloom_data, graph);
410 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
411 init_bloom_filters();
412 } else {
413 /* We need both the bloom chunks to exist together. Else ignore the data */
414 graph->chunk_bloom_indexes = NULL;
415 graph->chunk_bloom_data = NULL;
416 FREE_AND_NULL(graph->bloom_filter_settings);
419 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
421 if (verify_commit_graph_lite(graph))
422 goto free_and_return;
424 free_chunkfile(cf);
425 return graph;
427 free_and_return:
428 free_chunkfile(cf);
429 free(graph->bloom_filter_settings);
430 free(graph);
431 return NULL;
434 static struct commit_graph *load_commit_graph_one(struct repository *r,
435 const char *graph_file,
436 struct object_directory *odb)
439 struct stat st;
440 int fd;
441 struct commit_graph *g;
442 int open_ok = open_commit_graph(graph_file, &fd, &st);
444 if (!open_ok)
445 return NULL;
447 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
449 if (g)
450 g->filename = xstrdup(graph_file);
452 return g;
455 static struct commit_graph *load_commit_graph_v1(struct repository *r,
456 struct object_directory *odb)
458 char *graph_name = get_commit_graph_filename(odb);
459 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
460 free(graph_name);
462 return g;
465 static int add_graph_to_chain(struct commit_graph *g,
466 struct commit_graph *chain,
467 struct object_id *oids,
468 int n)
470 struct commit_graph *cur_g = chain;
472 if (n && !g->chunk_base_graphs) {
473 warning(_("commit-graph has no base graphs chunk"));
474 return 0;
477 while (n) {
478 n--;
480 if (!cur_g ||
481 !oideq(&oids[n], &cur_g->oid) ||
482 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
483 warning(_("commit-graph chain does not match"));
484 return 0;
487 cur_g = cur_g->base_graph;
490 g->base_graph = chain;
492 if (chain)
493 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
495 return 1;
498 static struct commit_graph *load_commit_graph_chain(struct repository *r,
499 struct object_directory *odb)
501 struct commit_graph *graph_chain = NULL;
502 struct strbuf line = STRBUF_INIT;
503 struct stat st;
504 struct object_id *oids;
505 int i = 0, valid = 1, count;
506 char *chain_name = get_commit_graph_chain_filename(odb);
507 FILE *fp;
508 int stat_res;
510 fp = fopen(chain_name, "r");
511 stat_res = stat(chain_name, &st);
512 free(chain_name);
514 if (!fp)
515 return NULL;
516 if (stat_res ||
517 st.st_size <= the_hash_algo->hexsz) {
518 fclose(fp);
519 return NULL;
522 count = st.st_size / (the_hash_algo->hexsz + 1);
523 CALLOC_ARRAY(oids, count);
525 prepare_alt_odb(r);
527 for (i = 0; i < count; i++) {
528 struct object_directory *odb;
530 if (strbuf_getline_lf(&line, fp) == EOF)
531 break;
533 if (get_oid_hex(line.buf, &oids[i])) {
534 warning(_("invalid commit-graph chain: line '%s' not a hash"),
535 line.buf);
536 valid = 0;
537 break;
540 valid = 0;
541 for (odb = r->objects->odb; odb; odb = odb->next) {
542 char *graph_name = get_split_graph_filename(odb, line.buf);
543 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
545 free(graph_name);
547 if (g) {
548 if (add_graph_to_chain(g, graph_chain, oids, i)) {
549 graph_chain = g;
550 valid = 1;
553 break;
557 if (!valid) {
558 warning(_("unable to find all commit-graph files"));
559 break;
563 free(oids);
564 fclose(fp);
565 strbuf_release(&line);
567 return graph_chain;
571 * returns 1 if and only if all graphs in the chain have
572 * corrected commit dates stored in the generation_data chunk.
574 static int validate_mixed_generation_chain(struct commit_graph *g)
576 int read_generation_data = 1;
577 struct commit_graph *p = g;
579 while (read_generation_data && p) {
580 read_generation_data = p->read_generation_data;
581 p = p->base_graph;
584 if (read_generation_data)
585 return 1;
587 while (g) {
588 g->read_generation_data = 0;
589 g = g->base_graph;
592 return 0;
595 struct commit_graph *read_commit_graph_one(struct repository *r,
596 struct object_directory *odb)
598 struct commit_graph *g = load_commit_graph_v1(r, odb);
600 if (!g)
601 g = load_commit_graph_chain(r, odb);
603 validate_mixed_generation_chain(g);
605 return g;
608 static void prepare_commit_graph_one(struct repository *r,
609 struct object_directory *odb)
612 if (r->objects->commit_graph)
613 return;
615 r->objects->commit_graph = read_commit_graph_one(r, odb);
619 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
621 * On the first invocation, this function attempts to load the commit
622 * graph if the_repository is configured to have one.
624 static int prepare_commit_graph(struct repository *r)
626 struct object_directory *odb;
629 * Early return if there is no git dir or if the commit graph is
630 * disabled.
632 * This must come before the "already attempted?" check below, because
633 * we want to disable even an already-loaded graph file.
635 if (!r->gitdir || r->commit_graph_disabled)
636 return 0;
638 if (r->objects->commit_graph_attempted)
639 return !!r->objects->commit_graph;
640 r->objects->commit_graph_attempted = 1;
642 prepare_repo_settings(r);
644 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
645 r->settings.core_commit_graph != 1)
647 * This repository is not configured to use commit graphs, so
648 * do not load one. (But report commit_graph_attempted anyway
649 * so that commit graph loading is not attempted again for this
650 * repository.)
652 return 0;
654 if (!commit_graph_compatible(r))
655 return 0;
657 prepare_alt_odb(r);
658 for (odb = r->objects->odb;
659 !r->objects->commit_graph && odb;
660 odb = odb->next)
661 prepare_commit_graph_one(r, odb);
662 return !!r->objects->commit_graph;
665 int generation_numbers_enabled(struct repository *r)
667 uint32_t first_generation;
668 struct commit_graph *g;
669 if (!prepare_commit_graph(r))
670 return 0;
672 g = r->objects->commit_graph;
674 if (!g->num_commits)
675 return 0;
677 first_generation = get_be32(g->chunk_commit_data +
678 g->hash_len + 8) >> 2;
680 return !!first_generation;
683 int corrected_commit_dates_enabled(struct repository *r)
685 struct commit_graph *g;
686 if (!prepare_commit_graph(r))
687 return 0;
689 g = r->objects->commit_graph;
691 if (!g->num_commits)
692 return 0;
694 return g->read_generation_data;
697 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
699 struct commit_graph *g = r->objects->commit_graph;
700 while (g) {
701 if (g->bloom_filter_settings)
702 return g->bloom_filter_settings;
703 g = g->base_graph;
705 return NULL;
708 static void close_commit_graph_one(struct commit_graph *g)
710 if (!g)
711 return;
713 clear_commit_graph_data_slab(&commit_graph_data_slab);
714 close_commit_graph_one(g->base_graph);
715 free_commit_graph(g);
718 void close_commit_graph(struct raw_object_store *o)
720 close_commit_graph_one(o->commit_graph);
721 o->commit_graph = NULL;
724 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
726 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
727 g->chunk_oid_lookup, g->hash_len, pos);
730 static void load_oid_from_graph(struct commit_graph *g,
731 uint32_t pos,
732 struct object_id *oid)
734 uint32_t lex_index;
736 while (g && pos < g->num_commits_in_base)
737 g = g->base_graph;
739 if (!g)
740 BUG("NULL commit-graph");
742 if (pos >= g->num_commits + g->num_commits_in_base)
743 die(_("invalid commit position. commit-graph is likely corrupt"));
745 lex_index = pos - g->num_commits_in_base;
747 oidread(oid, g->chunk_oid_lookup + g->hash_len * lex_index);
750 static struct commit_list **insert_parent_or_die(struct repository *r,
751 struct commit_graph *g,
752 uint32_t pos,
753 struct commit_list **pptr)
755 struct commit *c;
756 struct object_id oid;
758 if (pos >= g->num_commits + g->num_commits_in_base)
759 die("invalid parent position %"PRIu32, pos);
761 load_oid_from_graph(g, pos, &oid);
762 c = lookup_commit(r, &oid);
763 if (!c)
764 die(_("could not find commit %s"), oid_to_hex(&oid));
765 commit_graph_data_at(c)->graph_pos = pos;
766 return &commit_list_insert(c, pptr)->next;
769 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
771 const unsigned char *commit_data;
772 struct commit_graph_data *graph_data;
773 uint32_t lex_index, offset_pos;
774 uint64_t date_high, date_low, offset;
776 while (pos < g->num_commits_in_base)
777 g = g->base_graph;
779 if (pos >= g->num_commits + g->num_commits_in_base)
780 die(_("invalid commit position. commit-graph is likely corrupt"));
782 lex_index = pos - g->num_commits_in_base;
783 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
785 graph_data = commit_graph_data_at(item);
786 graph_data->graph_pos = pos;
788 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
789 date_low = get_be32(commit_data + g->hash_len + 12);
790 item->date = (timestamp_t)((date_high << 32) | date_low);
792 if (g->read_generation_data) {
793 offset = (timestamp_t)get_be32(g->chunk_generation_data + sizeof(uint32_t) * lex_index);
795 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
796 if (!g->chunk_generation_data_overflow)
797 die(_("commit-graph requires overflow generation data but has none"));
799 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
800 graph_data->generation = item->date + get_be64(g->chunk_generation_data_overflow + 8 * offset_pos);
801 } else
802 graph_data->generation = item->date + offset;
803 } else
804 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
806 if (g->topo_levels)
807 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
810 static inline void set_commit_tree(struct commit *c, struct tree *t)
812 c->maybe_tree = t;
815 static int fill_commit_in_graph(struct repository *r,
816 struct commit *item,
817 struct commit_graph *g, uint32_t pos)
819 uint32_t edge_value;
820 uint32_t *parent_data_ptr;
821 struct commit_list **pptr;
822 const unsigned char *commit_data;
823 uint32_t lex_index;
825 while (pos < g->num_commits_in_base)
826 g = g->base_graph;
828 fill_commit_graph_info(item, g, pos);
830 lex_index = pos - g->num_commits_in_base;
831 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
833 item->object.parsed = 1;
835 set_commit_tree(item, NULL);
837 pptr = &item->parents;
839 edge_value = get_be32(commit_data + g->hash_len);
840 if (edge_value == GRAPH_PARENT_NONE)
841 return 1;
842 pptr = insert_parent_or_die(r, g, edge_value, pptr);
844 edge_value = get_be32(commit_data + g->hash_len + 4);
845 if (edge_value == GRAPH_PARENT_NONE)
846 return 1;
847 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
848 pptr = insert_parent_or_die(r, g, edge_value, pptr);
849 return 1;
852 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
853 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
854 do {
855 edge_value = get_be32(parent_data_ptr);
856 pptr = insert_parent_or_die(r, g,
857 edge_value & GRAPH_EDGE_LAST_MASK,
858 pptr);
859 parent_data_ptr++;
860 } while (!(edge_value & GRAPH_LAST_EDGE));
862 return 1;
865 static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
867 struct commit_graph *cur_g = g;
868 uint32_t lex_index;
870 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
871 cur_g = cur_g->base_graph;
873 if (cur_g) {
874 *pos = lex_index + cur_g->num_commits_in_base;
875 return 1;
878 return 0;
881 static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
883 uint32_t graph_pos = commit_graph_position(item);
884 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
885 *pos = graph_pos;
886 return 1;
887 } else {
888 return search_commit_pos_in_graph(&item->object.oid, g, pos);
892 int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
893 uint32_t *pos)
895 if (!prepare_commit_graph(r))
896 return 0;
897 return find_commit_pos_in_graph(c, r->objects->commit_graph, pos);
900 struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
902 struct commit *commit;
903 uint32_t pos;
905 if (!repo->objects->commit_graph)
906 return NULL;
907 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
908 return NULL;
909 if (!has_object(repo, id, 0))
910 return NULL;
912 commit = lookup_commit(repo, id);
913 if (!commit)
914 return NULL;
915 if (commit->object.parsed)
916 return commit;
918 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
919 return NULL;
921 return commit;
924 static int parse_commit_in_graph_one(struct repository *r,
925 struct commit_graph *g,
926 struct commit *item)
928 uint32_t pos;
930 if (item->object.parsed)
931 return 1;
933 if (find_commit_pos_in_graph(item, g, &pos))
934 return fill_commit_in_graph(r, item, g, pos);
936 return 0;
939 int parse_commit_in_graph(struct repository *r, struct commit *item)
941 static int checked_env = 0;
943 if (!checked_env &&
944 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
945 die("dying as requested by the '%s' variable on commit-graph parse!",
946 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
947 checked_env = 1;
949 if (!prepare_commit_graph(r))
950 return 0;
951 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
954 void load_commit_graph_info(struct repository *r, struct commit *item)
956 uint32_t pos;
957 if (repo_find_commit_pos_in_graph(r, item, &pos))
958 fill_commit_graph_info(item, r->objects->commit_graph, pos);
961 static struct tree *load_tree_for_commit(struct repository *r,
962 struct commit_graph *g,
963 struct commit *c)
965 struct object_id oid;
966 const unsigned char *commit_data;
967 uint32_t graph_pos = commit_graph_position(c);
969 while (graph_pos < g->num_commits_in_base)
970 g = g->base_graph;
972 commit_data = g->chunk_commit_data +
973 GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base);
975 oidread(&oid, commit_data);
976 set_commit_tree(c, lookup_tree(r, &oid));
978 return c->maybe_tree;
981 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
982 struct commit_graph *g,
983 const struct commit *c)
985 if (c->maybe_tree)
986 return c->maybe_tree;
987 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
988 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
990 return load_tree_for_commit(r, g, (struct commit *)c);
993 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
995 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
998 struct packed_commit_list {
999 struct commit **list;
1000 size_t nr;
1001 size_t alloc;
1004 struct write_commit_graph_context {
1005 struct repository *r;
1006 struct object_directory *odb;
1007 char *graph_name;
1008 struct oid_array oids;
1009 struct packed_commit_list commits;
1010 int num_extra_edges;
1011 int num_generation_data_overflows;
1012 unsigned long approx_nr_objects;
1013 struct progress *progress;
1014 int progress_done;
1015 uint64_t progress_cnt;
1017 char *base_graph_name;
1018 int num_commit_graphs_before;
1019 int num_commit_graphs_after;
1020 char **commit_graph_filenames_before;
1021 char **commit_graph_filenames_after;
1022 char **commit_graph_hash_after;
1023 uint32_t new_num_commits_in_base;
1024 struct commit_graph *new_base_graph;
1026 unsigned append:1,
1027 report_progress:1,
1028 split:1,
1029 changed_paths:1,
1030 order_by_pack:1,
1031 write_generation_data:1,
1032 trust_generation_numbers:1;
1034 struct topo_level_slab *topo_levels;
1035 const struct commit_graph_opts *opts;
1036 size_t total_bloom_filter_data_size;
1037 const struct bloom_filter_settings *bloom_settings;
1039 int count_bloom_filter_computed;
1040 int count_bloom_filter_not_computed;
1041 int count_bloom_filter_trunc_empty;
1042 int count_bloom_filter_trunc_large;
1045 static int write_graph_chunk_fanout(struct hashfile *f,
1046 void *data)
1048 struct write_commit_graph_context *ctx = data;
1049 int i, count = 0;
1050 struct commit **list = ctx->commits.list;
1053 * Write the first-level table (the list is sorted,
1054 * but we use a 256-entry lookup to be able to avoid
1055 * having to do eight extra binary search iterations).
1057 for (i = 0; i < 256; i++) {
1058 while (count < ctx->commits.nr) {
1059 if ((*list)->object.oid.hash[0] != i)
1060 break;
1061 display_progress(ctx->progress, ++ctx->progress_cnt);
1062 count++;
1063 list++;
1066 hashwrite_be32(f, count);
1069 return 0;
1072 static int write_graph_chunk_oids(struct hashfile *f,
1073 void *data)
1075 struct write_commit_graph_context *ctx = data;
1076 struct commit **list = ctx->commits.list;
1077 int count;
1078 for (count = 0; count < ctx->commits.nr; count++, list++) {
1079 display_progress(ctx->progress, ++ctx->progress_cnt);
1080 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1083 return 0;
1086 static const struct object_id *commit_to_oid(size_t index, const void *table)
1088 const struct commit * const *commits = table;
1089 return &commits[index]->object.oid;
1092 static int write_graph_chunk_data(struct hashfile *f,
1093 void *data)
1095 struct write_commit_graph_context *ctx = data;
1096 struct commit **list = ctx->commits.list;
1097 struct commit **last = ctx->commits.list + ctx->commits.nr;
1098 uint32_t num_extra_edges = 0;
1100 while (list < last) {
1101 struct commit_list *parent;
1102 struct object_id *tree;
1103 int edge_value;
1104 uint32_t packedDate[2];
1105 display_progress(ctx->progress, ++ctx->progress_cnt);
1107 if (repo_parse_commit_no_graph(ctx->r, *list))
1108 die(_("unable to parse commit %s"),
1109 oid_to_hex(&(*list)->object.oid));
1110 tree = get_commit_tree_oid(*list);
1111 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1113 parent = (*list)->parents;
1115 if (!parent)
1116 edge_value = GRAPH_PARENT_NONE;
1117 else {
1118 edge_value = oid_pos(&parent->item->object.oid,
1119 ctx->commits.list,
1120 ctx->commits.nr,
1121 commit_to_oid);
1123 if (edge_value >= 0)
1124 edge_value += ctx->new_num_commits_in_base;
1125 else if (ctx->new_base_graph) {
1126 uint32_t pos;
1127 if (find_commit_pos_in_graph(parent->item,
1128 ctx->new_base_graph,
1129 &pos))
1130 edge_value = pos;
1133 if (edge_value < 0)
1134 BUG("missing parent %s for commit %s",
1135 oid_to_hex(&parent->item->object.oid),
1136 oid_to_hex(&(*list)->object.oid));
1139 hashwrite_be32(f, edge_value);
1141 if (parent)
1142 parent = parent->next;
1144 if (!parent)
1145 edge_value = GRAPH_PARENT_NONE;
1146 else if (parent->next)
1147 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1148 else {
1149 edge_value = oid_pos(&parent->item->object.oid,
1150 ctx->commits.list,
1151 ctx->commits.nr,
1152 commit_to_oid);
1154 if (edge_value >= 0)
1155 edge_value += ctx->new_num_commits_in_base;
1156 else if (ctx->new_base_graph) {
1157 uint32_t pos;
1158 if (find_commit_pos_in_graph(parent->item,
1159 ctx->new_base_graph,
1160 &pos))
1161 edge_value = pos;
1164 if (edge_value < 0)
1165 BUG("missing parent %s for commit %s",
1166 oid_to_hex(&parent->item->object.oid),
1167 oid_to_hex(&(*list)->object.oid));
1170 hashwrite_be32(f, edge_value);
1172 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1173 do {
1174 num_extra_edges++;
1175 parent = parent->next;
1176 } while (parent);
1179 if (sizeof((*list)->date) > 4)
1180 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1181 else
1182 packedDate[0] = 0;
1184 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1186 packedDate[1] = htonl((*list)->date);
1187 hashwrite(f, packedDate, 8);
1189 list++;
1192 return 0;
1195 static int write_graph_chunk_generation_data(struct hashfile *f,
1196 void *data)
1198 struct write_commit_graph_context *ctx = data;
1199 int i, num_generation_data_overflows = 0;
1201 for (i = 0; i < ctx->commits.nr; i++) {
1202 struct commit *c = ctx->commits.list[i];
1203 timestamp_t offset;
1204 repo_parse_commit(ctx->r, c);
1205 offset = commit_graph_data_at(c)->generation - c->date;
1206 display_progress(ctx->progress, ++ctx->progress_cnt);
1208 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1209 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1210 num_generation_data_overflows++;
1213 hashwrite_be32(f, offset);
1216 return 0;
1219 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1220 void *data)
1222 struct write_commit_graph_context *ctx = data;
1223 int i;
1224 for (i = 0; i < ctx->commits.nr; i++) {
1225 struct commit *c = ctx->commits.list[i];
1226 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1227 display_progress(ctx->progress, ++ctx->progress_cnt);
1229 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1230 hashwrite_be32(f, offset >> 32);
1231 hashwrite_be32(f, (uint32_t) offset);
1235 return 0;
1238 static int write_graph_chunk_extra_edges(struct hashfile *f,
1239 void *data)
1241 struct write_commit_graph_context *ctx = data;
1242 struct commit **list = ctx->commits.list;
1243 struct commit **last = ctx->commits.list + ctx->commits.nr;
1244 struct commit_list *parent;
1246 while (list < last) {
1247 int num_parents = 0;
1249 display_progress(ctx->progress, ++ctx->progress_cnt);
1251 for (parent = (*list)->parents; num_parents < 3 && parent;
1252 parent = parent->next)
1253 num_parents++;
1255 if (num_parents <= 2) {
1256 list++;
1257 continue;
1260 /* Since num_parents > 2, this initializer is safe. */
1261 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1262 int edge_value = oid_pos(&parent->item->object.oid,
1263 ctx->commits.list,
1264 ctx->commits.nr,
1265 commit_to_oid);
1267 if (edge_value >= 0)
1268 edge_value += ctx->new_num_commits_in_base;
1269 else if (ctx->new_base_graph) {
1270 uint32_t pos;
1271 if (find_commit_pos_in_graph(parent->item,
1272 ctx->new_base_graph,
1273 &pos))
1274 edge_value = pos;
1277 if (edge_value < 0)
1278 BUG("missing parent %s for commit %s",
1279 oid_to_hex(&parent->item->object.oid),
1280 oid_to_hex(&(*list)->object.oid));
1281 else if (!parent->next)
1282 edge_value |= GRAPH_LAST_EDGE;
1284 hashwrite_be32(f, edge_value);
1287 list++;
1290 return 0;
1293 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1294 void *data)
1296 struct write_commit_graph_context *ctx = data;
1297 struct commit **list = ctx->commits.list;
1298 struct commit **last = ctx->commits.list + ctx->commits.nr;
1299 uint32_t cur_pos = 0;
1301 while (list < last) {
1302 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1303 size_t len = filter ? filter->len : 0;
1304 cur_pos += len;
1305 display_progress(ctx->progress, ++ctx->progress_cnt);
1306 hashwrite_be32(f, cur_pos);
1307 list++;
1310 return 0;
1313 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1315 struct json_writer jw = JSON_WRITER_INIT;
1317 jw_object_begin(&jw, 0);
1318 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1319 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1320 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1321 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1322 jw_end(&jw);
1324 trace2_data_json("bloom", ctx->r, "settings", &jw);
1326 jw_release(&jw);
1329 static int write_graph_chunk_bloom_data(struct hashfile *f,
1330 void *data)
1332 struct write_commit_graph_context *ctx = data;
1333 struct commit **list = ctx->commits.list;
1334 struct commit **last = ctx->commits.list + ctx->commits.nr;
1336 trace2_bloom_filter_settings(ctx);
1338 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1339 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1340 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1342 while (list < last) {
1343 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1344 size_t len = filter ? filter->len : 0;
1346 display_progress(ctx->progress, ++ctx->progress_cnt);
1347 if (len)
1348 hashwrite(f, filter->data, len * sizeof(unsigned char));
1349 list++;
1352 return 0;
1355 static int add_packed_commits(const struct object_id *oid,
1356 struct packed_git *pack,
1357 uint32_t pos,
1358 void *data)
1360 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1361 enum object_type type;
1362 off_t offset = nth_packed_object_offset(pack, pos);
1363 struct object_info oi = OBJECT_INFO_INIT;
1365 if (ctx->progress)
1366 display_progress(ctx->progress, ++ctx->progress_done);
1368 oi.typep = &type;
1369 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1370 die(_("unable to get type of object %s"), oid_to_hex(oid));
1372 if (type != OBJ_COMMIT)
1373 return 0;
1375 oid_array_append(&ctx->oids, oid);
1376 set_commit_pos(ctx->r, oid);
1378 return 0;
1381 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1383 struct commit_list *parent;
1384 for (parent = commit->parents; parent; parent = parent->next) {
1385 if (!(parent->item->object.flags & REACHABLE)) {
1386 oid_array_append(&ctx->oids, &parent->item->object.oid);
1387 parent->item->object.flags |= REACHABLE;
1392 static void close_reachable(struct write_commit_graph_context *ctx)
1394 int i;
1395 struct commit *commit;
1396 enum commit_graph_split_flags flags = ctx->opts ?
1397 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1399 if (ctx->report_progress)
1400 ctx->progress = start_delayed_progress(
1401 _("Loading known commits in commit graph"),
1402 ctx->oids.nr);
1403 for (i = 0; i < ctx->oids.nr; i++) {
1404 display_progress(ctx->progress, i + 1);
1405 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1406 if (commit)
1407 commit->object.flags |= REACHABLE;
1409 stop_progress(&ctx->progress);
1412 * As this loop runs, ctx->oids.nr may grow, but not more
1413 * than the number of missing commits in the reachable
1414 * closure.
1416 if (ctx->report_progress)
1417 ctx->progress = start_delayed_progress(
1418 _("Expanding reachable commits in commit graph"),
1420 for (i = 0; i < ctx->oids.nr; i++) {
1421 display_progress(ctx->progress, i + 1);
1422 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1424 if (!commit)
1425 continue;
1426 if (ctx->split) {
1427 if ((!repo_parse_commit(ctx->r, commit) &&
1428 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1429 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1430 add_missing_parents(ctx, commit);
1431 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1432 add_missing_parents(ctx, commit);
1434 stop_progress(&ctx->progress);
1436 if (ctx->report_progress)
1437 ctx->progress = start_delayed_progress(
1438 _("Clearing commit marks in commit graph"),
1439 ctx->oids.nr);
1440 for (i = 0; i < ctx->oids.nr; i++) {
1441 display_progress(ctx->progress, i + 1);
1442 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1444 if (commit)
1445 commit->object.flags &= ~REACHABLE;
1447 stop_progress(&ctx->progress);
1450 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1452 int i;
1453 struct commit_list *list = NULL;
1455 if (ctx->report_progress)
1456 ctx->progress = start_delayed_progress(
1457 _("Computing commit graph topological levels"),
1458 ctx->commits.nr);
1459 for (i = 0; i < ctx->commits.nr; i++) {
1460 struct commit *c = ctx->commits.list[i];
1461 uint32_t level;
1463 repo_parse_commit(ctx->r, c);
1464 level = *topo_level_slab_at(ctx->topo_levels, c);
1466 display_progress(ctx->progress, i + 1);
1467 if (level != GENERATION_NUMBER_ZERO)
1468 continue;
1470 commit_list_insert(c, &list);
1471 while (list) {
1472 struct commit *current = list->item;
1473 struct commit_list *parent;
1474 int all_parents_computed = 1;
1475 uint32_t max_level = 0;
1477 for (parent = current->parents; parent; parent = parent->next) {
1478 repo_parse_commit(ctx->r, parent->item);
1479 level = *topo_level_slab_at(ctx->topo_levels, parent->item);
1481 if (level == GENERATION_NUMBER_ZERO) {
1482 all_parents_computed = 0;
1483 commit_list_insert(parent->item, &list);
1484 break;
1487 if (level > max_level)
1488 max_level = level;
1491 if (all_parents_computed) {
1492 pop_commit(&list);
1494 if (max_level > GENERATION_NUMBER_V1_MAX - 1)
1495 max_level = GENERATION_NUMBER_V1_MAX - 1;
1496 *topo_level_slab_at(ctx->topo_levels, current) = max_level + 1;
1500 stop_progress(&ctx->progress);
1503 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1505 int i;
1506 struct commit_list *list = NULL;
1508 if (ctx->report_progress)
1509 ctx->progress = start_delayed_progress(
1510 _("Computing commit graph generation numbers"),
1511 ctx->commits.nr);
1513 if (!ctx->trust_generation_numbers) {
1514 for (i = 0; i < ctx->commits.nr; i++) {
1515 struct commit *c = ctx->commits.list[i];
1516 repo_parse_commit(ctx->r, c);
1517 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1521 for (i = 0; i < ctx->commits.nr; i++) {
1522 struct commit *c = ctx->commits.list[i];
1523 timestamp_t corrected_commit_date;
1525 repo_parse_commit(ctx->r, c);
1526 corrected_commit_date = commit_graph_data_at(c)->generation;
1528 display_progress(ctx->progress, i + 1);
1529 if (corrected_commit_date != GENERATION_NUMBER_ZERO)
1530 continue;
1532 commit_list_insert(c, &list);
1533 while (list) {
1534 struct commit *current = list->item;
1535 struct commit_list *parent;
1536 int all_parents_computed = 1;
1537 timestamp_t max_corrected_commit_date = 0;
1539 for (parent = current->parents; parent; parent = parent->next) {
1540 repo_parse_commit(ctx->r, parent->item);
1541 corrected_commit_date = commit_graph_data_at(parent->item)->generation;
1543 if (corrected_commit_date == GENERATION_NUMBER_ZERO) {
1544 all_parents_computed = 0;
1545 commit_list_insert(parent->item, &list);
1546 break;
1549 if (corrected_commit_date > max_corrected_commit_date)
1550 max_corrected_commit_date = corrected_commit_date;
1553 if (all_parents_computed) {
1554 pop_commit(&list);
1556 if (current->date && current->date > max_corrected_commit_date)
1557 max_corrected_commit_date = current->date - 1;
1558 commit_graph_data_at(current)->generation = max_corrected_commit_date + 1;
1563 for (i = 0; i < ctx->commits.nr; i++) {
1564 struct commit *c = ctx->commits.list[i];
1565 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1566 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1567 ctx->num_generation_data_overflows++;
1569 stop_progress(&ctx->progress);
1572 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1574 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1575 ctx->count_bloom_filter_computed);
1576 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1577 ctx->count_bloom_filter_not_computed);
1578 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1579 ctx->count_bloom_filter_trunc_empty);
1580 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1581 ctx->count_bloom_filter_trunc_large);
1584 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1586 int i;
1587 struct progress *progress = NULL;
1588 struct commit **sorted_commits;
1589 int max_new_filters;
1591 init_bloom_filters();
1593 if (ctx->report_progress)
1594 progress = start_delayed_progress(
1595 _("Computing commit changed paths Bloom filters"),
1596 ctx->commits.nr);
1598 ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1599 COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1601 if (ctx->order_by_pack)
1602 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1603 else
1604 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1606 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1607 ctx->opts->max_new_filters : ctx->commits.nr;
1609 for (i = 0; i < ctx->commits.nr; i++) {
1610 enum bloom_filter_computed computed = 0;
1611 struct commit *c = sorted_commits[i];
1612 struct bloom_filter *filter = get_or_compute_bloom_filter(
1613 ctx->r,
1615 ctx->count_bloom_filter_computed < max_new_filters,
1616 ctx->bloom_settings,
1617 &computed);
1618 if (computed & BLOOM_COMPUTED) {
1619 ctx->count_bloom_filter_computed++;
1620 if (computed & BLOOM_TRUNC_EMPTY)
1621 ctx->count_bloom_filter_trunc_empty++;
1622 if (computed & BLOOM_TRUNC_LARGE)
1623 ctx->count_bloom_filter_trunc_large++;
1624 } else if (computed & BLOOM_NOT_COMPUTED)
1625 ctx->count_bloom_filter_not_computed++;
1626 ctx->total_bloom_filter_data_size += filter
1627 ? sizeof(unsigned char) * filter->len : 0;
1628 display_progress(progress, i + 1);
1631 if (trace2_is_enabled())
1632 trace2_bloom_filter_write_statistics(ctx);
1634 free(sorted_commits);
1635 stop_progress(&progress);
1638 struct refs_cb_data {
1639 struct oidset *commits;
1640 struct progress *progress;
1643 static int add_ref_to_set(const char *refname,
1644 const struct object_id *oid,
1645 int flags, void *cb_data)
1647 struct object_id peeled;
1648 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1650 if (!peel_iterated_oid(oid, &peeled))
1651 oid = &peeled;
1652 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1653 oidset_insert(data->commits, oid);
1655 display_progress(data->progress, oidset_size(data->commits));
1657 return 0;
1660 int write_commit_graph_reachable(struct object_directory *odb,
1661 enum commit_graph_write_flags flags,
1662 const struct commit_graph_opts *opts)
1664 struct oidset commits = OIDSET_INIT;
1665 struct refs_cb_data data;
1666 int result;
1668 memset(&data, 0, sizeof(data));
1669 data.commits = &commits;
1670 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1671 data.progress = start_delayed_progress(
1672 _("Collecting referenced commits"), 0);
1674 for_each_ref(add_ref_to_set, &data);
1676 stop_progress(&data.progress);
1678 result = write_commit_graph(odb, NULL, &commits,
1679 flags, opts);
1681 oidset_clear(&commits);
1682 return result;
1685 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1686 const struct string_list *pack_indexes)
1688 uint32_t i;
1689 struct strbuf progress_title = STRBUF_INIT;
1690 struct strbuf packname = STRBUF_INIT;
1691 int dirlen;
1692 int ret = 0;
1694 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1695 dirlen = packname.len;
1696 if (ctx->report_progress) {
1697 strbuf_addf(&progress_title,
1698 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1699 "Finding commits for commit graph in %"PRIuMAX" packs",
1700 pack_indexes->nr),
1701 (uintmax_t)pack_indexes->nr);
1702 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1703 ctx->progress_done = 0;
1705 for (i = 0; i < pack_indexes->nr; i++) {
1706 struct packed_git *p;
1707 strbuf_setlen(&packname, dirlen);
1708 strbuf_addstr(&packname, pack_indexes->items[i].string);
1709 p = add_packed_git(packname.buf, packname.len, 1);
1710 if (!p) {
1711 ret = error(_("error adding pack %s"), packname.buf);
1712 goto cleanup;
1714 if (open_pack_index(p)) {
1715 ret = error(_("error opening index for %s"), packname.buf);
1716 goto cleanup;
1718 for_each_object_in_pack(p, add_packed_commits, ctx,
1719 FOR_EACH_OBJECT_PACK_ORDER);
1720 close_pack(p);
1721 free(p);
1724 cleanup:
1725 stop_progress(&ctx->progress);
1726 strbuf_release(&progress_title);
1727 strbuf_release(&packname);
1729 return ret;
1732 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1733 struct oidset *commits)
1735 struct oidset_iter iter;
1736 struct object_id *oid;
1738 if (!oidset_size(commits))
1739 return 0;
1741 oidset_iter_init(commits, &iter);
1742 while ((oid = oidset_iter_next(&iter))) {
1743 oid_array_append(&ctx->oids, oid);
1746 return 0;
1749 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1751 if (ctx->report_progress)
1752 ctx->progress = start_delayed_progress(
1753 _("Finding commits for commit graph among packed objects"),
1754 ctx->approx_nr_objects);
1755 for_each_packed_object(add_packed_commits, ctx,
1756 FOR_EACH_OBJECT_PACK_ORDER);
1757 if (ctx->progress_done < ctx->approx_nr_objects)
1758 display_progress(ctx->progress, ctx->approx_nr_objects);
1759 stop_progress(&ctx->progress);
1762 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1764 uint32_t i;
1765 enum commit_graph_split_flags flags = ctx->opts ?
1766 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1768 ctx->num_extra_edges = 0;
1769 if (ctx->report_progress)
1770 ctx->progress = start_delayed_progress(
1771 _("Finding extra edges in commit graph"),
1772 ctx->oids.nr);
1773 oid_array_sort(&ctx->oids);
1774 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1775 unsigned int num_parents;
1777 display_progress(ctx->progress, i + 1);
1779 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1780 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1782 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1783 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1784 continue;
1786 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1787 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1788 else
1789 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1791 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1792 if (num_parents > 2)
1793 ctx->num_extra_edges += num_parents - 1;
1795 ctx->commits.nr++;
1797 stop_progress(&ctx->progress);
1800 static int write_graph_chunk_base_1(struct hashfile *f,
1801 struct commit_graph *g)
1803 int num = 0;
1805 if (!g)
1806 return 0;
1808 num = write_graph_chunk_base_1(f, g->base_graph);
1809 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1810 return num + 1;
1813 static int write_graph_chunk_base(struct hashfile *f,
1814 void *data)
1816 struct write_commit_graph_context *ctx = data;
1817 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1819 if (num != ctx->num_commit_graphs_after - 1) {
1820 error(_("failed to write correct number of base graph ids"));
1821 return -1;
1824 return 0;
1827 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1829 uint32_t i;
1830 int fd;
1831 struct hashfile *f;
1832 struct lock_file lk = LOCK_INIT;
1833 const unsigned hashsz = the_hash_algo->rawsz;
1834 struct strbuf progress_title = STRBUF_INIT;
1835 struct chunkfile *cf;
1836 unsigned char file_hash[GIT_MAX_RAWSZ];
1838 if (ctx->split) {
1839 struct strbuf tmp_file = STRBUF_INIT;
1841 strbuf_addf(&tmp_file,
1842 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1843 ctx->odb->path);
1844 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1845 } else {
1846 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1849 if (safe_create_leading_directories(ctx->graph_name)) {
1850 UNLEAK(ctx->graph_name);
1851 error(_("unable to create leading directories of %s"),
1852 ctx->graph_name);
1853 return -1;
1856 if (ctx->split) {
1857 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
1859 hold_lock_file_for_update_mode(&lk, lock_name,
1860 LOCK_DIE_ON_ERROR, 0444);
1861 free(lock_name);
1863 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1864 if (fd < 0) {
1865 error(_("unable to create temporary graph layer"));
1866 return -1;
1869 if (adjust_shared_perm(ctx->graph_name)) {
1870 error(_("unable to adjust shared permissions for '%s'"),
1871 ctx->graph_name);
1872 return -1;
1875 f = hashfd(fd, ctx->graph_name);
1876 } else {
1877 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1878 LOCK_DIE_ON_ERROR, 0444);
1879 fd = get_lock_file_fd(&lk);
1880 f = hashfd(fd, get_lock_file_path(&lk));
1883 cf = init_chunkfile(f);
1885 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
1886 write_graph_chunk_fanout);
1887 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, hashsz * ctx->commits.nr,
1888 write_graph_chunk_oids);
1889 add_chunk(cf, GRAPH_CHUNKID_DATA, (hashsz + 16) * ctx->commits.nr,
1890 write_graph_chunk_data);
1892 if (ctx->write_generation_data)
1893 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
1894 sizeof(uint32_t) * ctx->commits.nr,
1895 write_graph_chunk_generation_data);
1896 if (ctx->num_generation_data_overflows)
1897 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
1898 sizeof(timestamp_t) * ctx->num_generation_data_overflows,
1899 write_graph_chunk_generation_data_overflow);
1900 if (ctx->num_extra_edges)
1901 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
1902 4 * ctx->num_extra_edges,
1903 write_graph_chunk_extra_edges);
1904 if (ctx->changed_paths) {
1905 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
1906 sizeof(uint32_t) * ctx->commits.nr,
1907 write_graph_chunk_bloom_indexes);
1908 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
1909 sizeof(uint32_t) * 3
1910 + ctx->total_bloom_filter_data_size,
1911 write_graph_chunk_bloom_data);
1913 if (ctx->num_commit_graphs_after > 1)
1914 add_chunk(cf, GRAPH_CHUNKID_BASE,
1915 hashsz * (ctx->num_commit_graphs_after - 1),
1916 write_graph_chunk_base);
1918 hashwrite_be32(f, GRAPH_SIGNATURE);
1920 hashwrite_u8(f, GRAPH_VERSION);
1921 hashwrite_u8(f, oid_version(the_hash_algo));
1922 hashwrite_u8(f, get_num_chunks(cf));
1923 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1925 if (ctx->report_progress) {
1926 strbuf_addf(&progress_title,
1927 Q_("Writing out commit graph in %d pass",
1928 "Writing out commit graph in %d passes",
1929 get_num_chunks(cf)),
1930 get_num_chunks(cf));
1931 ctx->progress = start_delayed_progress(
1932 progress_title.buf,
1933 get_num_chunks(cf) * ctx->commits.nr);
1936 write_chunkfile(cf, ctx);
1938 stop_progress(&ctx->progress);
1939 strbuf_release(&progress_title);
1941 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1942 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1943 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
1945 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1946 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1947 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1948 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1951 close_commit_graph(ctx->r->objects);
1952 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
1953 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1954 free_chunkfile(cf);
1956 if (ctx->split) {
1957 FILE *chainf = fdopen_lock_file(&lk, "w");
1958 char *final_graph_name;
1959 int result;
1961 close(fd);
1963 if (!chainf) {
1964 error(_("unable to open commit-graph chain file"));
1965 return -1;
1968 if (ctx->base_graph_name) {
1969 const char *dest;
1970 int idx = ctx->num_commit_graphs_after - 1;
1971 if (ctx->num_commit_graphs_after > 1)
1972 idx--;
1974 dest = ctx->commit_graph_filenames_after[idx];
1976 if (strcmp(ctx->base_graph_name, dest)) {
1977 result = rename(ctx->base_graph_name, dest);
1979 if (result) {
1980 error(_("failed to rename base commit-graph file"));
1981 return -1;
1984 } else {
1985 char *graph_name = get_commit_graph_filename(ctx->odb);
1986 unlink(graph_name);
1987 free(graph_name);
1990 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
1991 final_graph_name = get_split_graph_filename(ctx->odb,
1992 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1993 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1995 result = rename(ctx->graph_name, final_graph_name);
1997 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1998 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2000 if (result) {
2001 error(_("failed to rename temporary commit-graph file"));
2002 return -1;
2006 commit_lock_file(&lk);
2008 return 0;
2011 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2013 struct commit_graph *g;
2014 uint32_t num_commits;
2015 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2016 uint32_t i;
2018 int max_commits = 0;
2019 int size_mult = 2;
2021 if (ctx->opts) {
2022 max_commits = ctx->opts->max_commits;
2024 if (ctx->opts->size_multiple)
2025 size_mult = ctx->opts->size_multiple;
2027 flags = ctx->opts->split_flags;
2030 g = ctx->r->objects->commit_graph;
2031 num_commits = ctx->commits.nr;
2032 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2033 ctx->num_commit_graphs_after = 1;
2034 else
2035 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2037 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2038 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2039 while (g && (g->num_commits <= size_mult * num_commits ||
2040 (max_commits && num_commits > max_commits))) {
2041 if (g->odb != ctx->odb)
2042 break;
2044 num_commits += g->num_commits;
2045 g = g->base_graph;
2047 ctx->num_commit_graphs_after--;
2051 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2052 ctx->new_base_graph = g;
2053 else if (ctx->num_commit_graphs_after != 1)
2054 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2055 "should be 1 with --split=replace");
2057 if (ctx->num_commit_graphs_after == 2) {
2058 char *old_graph_name = get_commit_graph_filename(g->odb);
2060 if (!strcmp(g->filename, old_graph_name) &&
2061 g->odb != ctx->odb) {
2062 ctx->num_commit_graphs_after = 1;
2063 ctx->new_base_graph = NULL;
2066 free(old_graph_name);
2069 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2070 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2072 for (i = 0; i < ctx->num_commit_graphs_after &&
2073 i < ctx->num_commit_graphs_before; i++)
2074 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2076 i = ctx->num_commit_graphs_before - 1;
2077 g = ctx->r->objects->commit_graph;
2079 while (g) {
2080 if (i < ctx->num_commit_graphs_after)
2081 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2084 * If the topmost remaining layer has generation data chunk, the
2085 * resultant layer also has generation data chunk.
2087 if (i == ctx->num_commit_graphs_after - 2)
2088 ctx->write_generation_data = !!g->chunk_generation_data;
2090 i--;
2091 g = g->base_graph;
2095 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2096 struct commit_graph *g)
2098 uint32_t i;
2099 uint32_t offset = g->num_commits_in_base;
2101 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2103 for (i = 0; i < g->num_commits; i++) {
2104 struct object_id oid;
2105 struct commit *result;
2107 display_progress(ctx->progress, i + 1);
2109 load_oid_from_graph(g, i + offset, &oid);
2111 /* only add commits if they still exist in the repo */
2112 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2114 if (result) {
2115 ctx->commits.list[ctx->commits.nr] = result;
2116 ctx->commits.nr++;
2121 static int commit_compare(const void *_a, const void *_b)
2123 const struct commit *a = *(const struct commit **)_a;
2124 const struct commit *b = *(const struct commit **)_b;
2125 return oidcmp(&a->object.oid, &b->object.oid);
2128 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2130 uint32_t i, dedup_i = 0;
2132 if (ctx->report_progress)
2133 ctx->progress = start_delayed_progress(
2134 _("Scanning merged commits"),
2135 ctx->commits.nr);
2137 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2139 ctx->num_extra_edges = 0;
2140 for (i = 0; i < ctx->commits.nr; i++) {
2141 display_progress(ctx->progress, i + 1);
2143 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2144 &ctx->commits.list[i]->object.oid)) {
2146 * Silently ignore duplicates. These were likely
2147 * created due to a commit appearing in multiple
2148 * layers of the chain, which is unexpected but
2149 * not invalid. We should make sure there is a
2150 * unique copy in the new layer.
2152 } else {
2153 unsigned int num_parents;
2155 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2156 dedup_i++;
2158 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2159 if (num_parents > 2)
2160 ctx->num_extra_edges += num_parents - 1;
2164 ctx->commits.nr = dedup_i;
2166 stop_progress(&ctx->progress);
2169 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2171 struct commit_graph *g = ctx->r->objects->commit_graph;
2172 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2174 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2175 current_graph_number--;
2177 if (ctx->report_progress)
2178 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2180 merge_commit_graph(ctx, g);
2181 stop_progress(&ctx->progress);
2183 g = g->base_graph;
2186 if (g) {
2187 ctx->new_base_graph = g;
2188 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2191 if (ctx->new_base_graph)
2192 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2194 sort_and_scan_merged_commits(ctx);
2197 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2199 uint32_t i;
2200 time_t now = time(NULL);
2202 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2203 struct stat st;
2204 struct utimbuf updated_time;
2206 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2207 continue;
2209 updated_time.actime = st.st_atime;
2210 updated_time.modtime = now;
2211 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2215 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2217 struct strbuf path = STRBUF_INIT;
2218 DIR *dir;
2219 struct dirent *de;
2220 size_t dirnamelen;
2221 timestamp_t expire_time = time(NULL);
2223 if (ctx->opts && ctx->opts->expire_time)
2224 expire_time = ctx->opts->expire_time;
2225 if (!ctx->split) {
2226 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2227 unlink(chain_file_name);
2228 free(chain_file_name);
2229 ctx->num_commit_graphs_after = 0;
2232 strbuf_addstr(&path, ctx->odb->path);
2233 strbuf_addstr(&path, "/info/commit-graphs");
2234 dir = opendir(path.buf);
2236 if (!dir)
2237 goto out;
2239 strbuf_addch(&path, '/');
2240 dirnamelen = path.len;
2241 while ((de = readdir(dir)) != NULL) {
2242 struct stat st;
2243 uint32_t i, found = 0;
2245 strbuf_setlen(&path, dirnamelen);
2246 strbuf_addstr(&path, de->d_name);
2248 if (stat(path.buf, &st) < 0)
2249 continue;
2251 if (st.st_mtime > expire_time)
2252 continue;
2253 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2254 continue;
2256 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2257 if (!strcmp(ctx->commit_graph_filenames_after[i],
2258 path.buf)) {
2259 found = 1;
2260 break;
2264 if (!found)
2265 unlink(path.buf);
2268 out:
2269 strbuf_release(&path);
2272 int write_commit_graph(struct object_directory *odb,
2273 const struct string_list *const pack_indexes,
2274 struct oidset *commits,
2275 enum commit_graph_write_flags flags,
2276 const struct commit_graph_opts *opts)
2278 struct repository *r = the_repository;
2279 struct write_commit_graph_context *ctx;
2280 uint32_t i;
2281 int res = 0;
2282 int replace = 0;
2283 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2284 struct topo_level_slab topo_levels;
2286 prepare_repo_settings(r);
2287 if (!r->settings.core_commit_graph) {
2288 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2289 return 0;
2291 if (!commit_graph_compatible(r))
2292 return 0;
2294 CALLOC_ARRAY(ctx, 1);
2295 ctx->r = r;
2296 ctx->odb = odb;
2297 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2298 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2299 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2300 ctx->opts = opts;
2301 ctx->total_bloom_filter_data_size = 0;
2302 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2303 ctx->num_generation_data_overflows = 0;
2305 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2306 bloom_settings.bits_per_entry);
2307 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2308 bloom_settings.num_hashes);
2309 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2310 bloom_settings.max_changed_paths);
2311 ctx->bloom_settings = &bloom_settings;
2313 init_topo_level_slab(&topo_levels);
2314 ctx->topo_levels = &topo_levels;
2316 prepare_commit_graph(ctx->r);
2317 if (ctx->r->objects->commit_graph) {
2318 struct commit_graph *g = ctx->r->objects->commit_graph;
2320 while (g) {
2321 g->topo_levels = &topo_levels;
2322 g = g->base_graph;
2326 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2327 ctx->changed_paths = 1;
2328 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2329 struct commit_graph *g;
2331 g = ctx->r->objects->commit_graph;
2333 /* We have changed-paths already. Keep them in the next graph */
2334 if (g && g->chunk_bloom_data) {
2335 ctx->changed_paths = 1;
2336 ctx->bloom_settings = g->bloom_filter_settings;
2340 if (ctx->split) {
2341 struct commit_graph *g = ctx->r->objects->commit_graph;
2343 while (g) {
2344 ctx->num_commit_graphs_before++;
2345 g = g->base_graph;
2348 if (ctx->num_commit_graphs_before) {
2349 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2350 i = ctx->num_commit_graphs_before;
2351 g = ctx->r->objects->commit_graph;
2353 while (g) {
2354 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2355 g = g->base_graph;
2359 if (ctx->opts)
2360 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2363 ctx->approx_nr_objects = approximate_object_count();
2365 if (ctx->append && ctx->r->objects->commit_graph) {
2366 struct commit_graph *g = ctx->r->objects->commit_graph;
2367 for (i = 0; i < g->num_commits; i++) {
2368 struct object_id oid;
2369 oidread(&oid, g->chunk_oid_lookup + g->hash_len * i);
2370 oid_array_append(&ctx->oids, &oid);
2374 if (pack_indexes) {
2375 ctx->order_by_pack = 1;
2376 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2377 goto cleanup;
2380 if (commits) {
2381 if ((res = fill_oids_from_commits(ctx, commits)))
2382 goto cleanup;
2385 if (!pack_indexes && !commits) {
2386 ctx->order_by_pack = 1;
2387 fill_oids_from_all_packs(ctx);
2390 close_reachable(ctx);
2392 copy_oids_to_commits(ctx);
2394 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2395 error(_("too many commits to write graph"));
2396 res = -1;
2397 goto cleanup;
2400 if (!ctx->commits.nr && !replace)
2401 goto cleanup;
2403 if (ctx->split) {
2404 split_graph_merge_strategy(ctx);
2406 if (!replace)
2407 merge_commit_graphs(ctx);
2408 } else
2409 ctx->num_commit_graphs_after = 1;
2411 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2413 compute_topological_levels(ctx);
2414 if (ctx->write_generation_data)
2415 compute_generation_numbers(ctx);
2417 if (ctx->changed_paths)
2418 compute_bloom_filters(ctx);
2420 res = write_commit_graph_file(ctx);
2422 if (ctx->split)
2423 mark_commit_graphs(ctx);
2425 expire_commit_graphs(ctx);
2427 cleanup:
2428 free(ctx->graph_name);
2429 free(ctx->commits.list);
2430 oid_array_clear(&ctx->oids);
2431 clear_topo_level_slab(&topo_levels);
2433 if (ctx->commit_graph_filenames_after) {
2434 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2435 free(ctx->commit_graph_filenames_after[i]);
2436 free(ctx->commit_graph_hash_after[i]);
2439 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2440 free(ctx->commit_graph_filenames_before[i]);
2442 free(ctx->commit_graph_filenames_after);
2443 free(ctx->commit_graph_filenames_before);
2444 free(ctx->commit_graph_hash_after);
2447 free(ctx);
2449 return res;
2452 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2453 static int verify_commit_graph_error;
2455 __attribute__((format (printf, 1, 2)))
2456 static void graph_report(const char *fmt, ...)
2458 va_list ap;
2460 verify_commit_graph_error = 1;
2461 va_start(ap, fmt);
2462 vfprintf(stderr, fmt, ap);
2463 fprintf(stderr, "\n");
2464 va_end(ap);
2467 #define GENERATION_ZERO_EXISTS 1
2468 #define GENERATION_NUMBER_EXISTS 2
2470 static int commit_graph_checksum_valid(struct commit_graph *g)
2472 return hashfile_checksum_valid(g->data, g->data_len);
2475 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2477 uint32_t i, cur_fanout_pos = 0;
2478 struct object_id prev_oid, cur_oid;
2479 int generation_zero = 0;
2480 struct progress *progress = NULL;
2481 int local_error = 0;
2483 if (!g) {
2484 graph_report("no commit-graph file loaded");
2485 return 1;
2488 verify_commit_graph_error = verify_commit_graph_lite(g);
2489 if (verify_commit_graph_error)
2490 return verify_commit_graph_error;
2492 if (!commit_graph_checksum_valid(g)) {
2493 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2494 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2497 for (i = 0; i < g->num_commits; i++) {
2498 struct commit *graph_commit;
2500 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2502 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2503 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2504 oid_to_hex(&prev_oid),
2505 oid_to_hex(&cur_oid));
2507 oidcpy(&prev_oid, &cur_oid);
2509 while (cur_oid.hash[0] > cur_fanout_pos) {
2510 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2512 if (i != fanout_value)
2513 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2514 cur_fanout_pos, fanout_value, i);
2515 cur_fanout_pos++;
2518 graph_commit = lookup_commit(r, &cur_oid);
2519 if (!parse_commit_in_graph_one(r, g, graph_commit))
2520 graph_report(_("failed to parse commit %s from commit-graph"),
2521 oid_to_hex(&cur_oid));
2524 while (cur_fanout_pos < 256) {
2525 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2527 if (g->num_commits != fanout_value)
2528 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2529 cur_fanout_pos, fanout_value, i);
2531 cur_fanout_pos++;
2534 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2535 return verify_commit_graph_error;
2537 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2538 progress = start_progress(_("Verifying commits in commit graph"),
2539 g->num_commits);
2541 for (i = 0; i < g->num_commits; i++) {
2542 struct commit *graph_commit, *odb_commit;
2543 struct commit_list *graph_parents, *odb_parents;
2544 timestamp_t max_generation = 0;
2545 timestamp_t generation;
2547 display_progress(progress, i + 1);
2548 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2550 graph_commit = lookup_commit(r, &cur_oid);
2551 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2552 if (parse_commit_internal(odb_commit, 0, 0)) {
2553 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2554 oid_to_hex(&cur_oid));
2555 continue;
2558 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2559 get_commit_tree_oid(odb_commit)))
2560 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2561 oid_to_hex(&cur_oid),
2562 oid_to_hex(get_commit_tree_oid(graph_commit)),
2563 oid_to_hex(get_commit_tree_oid(odb_commit)));
2565 graph_parents = graph_commit->parents;
2566 odb_parents = odb_commit->parents;
2568 while (graph_parents) {
2569 if (!odb_parents) {
2570 graph_report(_("commit-graph parent list for commit %s is too long"),
2571 oid_to_hex(&cur_oid));
2572 break;
2575 /* parse parent in case it is in a base graph */
2576 parse_commit_in_graph_one(r, g, graph_parents->item);
2578 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2579 graph_report(_("commit-graph parent for %s is %s != %s"),
2580 oid_to_hex(&cur_oid),
2581 oid_to_hex(&graph_parents->item->object.oid),
2582 oid_to_hex(&odb_parents->item->object.oid));
2584 generation = commit_graph_generation(graph_parents->item);
2585 if (generation > max_generation)
2586 max_generation = generation;
2588 graph_parents = graph_parents->next;
2589 odb_parents = odb_parents->next;
2592 if (odb_parents)
2593 graph_report(_("commit-graph parent list for commit %s terminates early"),
2594 oid_to_hex(&cur_oid));
2596 if (!commit_graph_generation(graph_commit)) {
2597 if (generation_zero == GENERATION_NUMBER_EXISTS)
2598 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2599 oid_to_hex(&cur_oid));
2600 generation_zero = GENERATION_ZERO_EXISTS;
2601 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2602 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2603 oid_to_hex(&cur_oid));
2605 if (generation_zero == GENERATION_ZERO_EXISTS)
2606 continue;
2609 * If we are using topological level and one of our parents has
2610 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2611 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2612 * in the following condition.
2614 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2615 max_generation--;
2617 generation = commit_graph_generation(graph_commit);
2618 if (generation < max_generation + 1)
2619 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2620 oid_to_hex(&cur_oid),
2621 generation,
2622 max_generation + 1);
2624 if (graph_commit->date != odb_commit->date)
2625 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2626 oid_to_hex(&cur_oid),
2627 graph_commit->date,
2628 odb_commit->date);
2630 stop_progress(&progress);
2632 local_error = verify_commit_graph_error;
2634 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2635 local_error |= verify_commit_graph(r, g->base_graph, flags);
2637 return local_error;
2640 void free_commit_graph(struct commit_graph *g)
2642 if (!g)
2643 return;
2644 if (g->data) {
2645 munmap((void *)g->data, g->data_len);
2646 g->data = NULL;
2648 free(g->filename);
2649 free(g->bloom_filter_settings);
2650 free(g);
2653 void disable_commit_graph(struct repository *r)
2655 r->commit_graph_disabled = 1;