dir.h: move DTYPE defines from cache.h
[git.git] / commit-graph.c
blob0c4f2266445c94f1cc3598e0ebf29d1068cade9a
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "gettext.h"
4 #include "hex.h"
5 #include "lockfile.h"
6 #include "pack.h"
7 #include "packfile.h"
8 #include "commit.h"
9 #include "object.h"
10 #include "refs.h"
11 #include "revision.h"
12 #include "hash-lookup.h"
13 #include "commit-graph.h"
14 #include "object-file.h"
15 #include "object-store.h"
16 #include "oid-array.h"
17 #include "alloc.h"
18 #include "hashmap.h"
19 #include "replace-object.h"
20 #include "progress.h"
21 #include "bloom.h"
22 #include "commit-slab.h"
23 #include "shallow.h"
24 #include "json-writer.h"
25 #include "trace2.h"
26 #include "chunk-format.h"
27 #include "wrapper.h"
29 void git_test_write_commit_graph_or_die(void)
31 int flags = 0;
32 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
33 return;
35 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
36 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
38 if (write_commit_graph_reachable(the_repository->objects->odb,
39 flags, NULL))
40 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
43 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
44 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
45 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
46 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
47 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
48 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
49 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
50 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
51 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
52 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
54 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
56 #define GRAPH_VERSION_1 0x1
57 #define GRAPH_VERSION GRAPH_VERSION_1
59 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
60 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
61 #define GRAPH_PARENT_NONE 0x70000000
63 #define GRAPH_LAST_EDGE 0x80000000
65 #define GRAPH_HEADER_SIZE 8
66 #define GRAPH_FANOUT_SIZE (4 * 256)
67 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
68 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
70 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
72 /* Remember to update object flag allocation in object.h */
73 #define REACHABLE (1u<<15)
75 define_commit_slab(topo_level_slab, uint32_t);
77 /* Keep track of the order in which commits are added to our list. */
78 define_commit_slab(commit_pos, int);
79 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
81 static void set_commit_pos(struct repository *r, const struct object_id *oid)
83 static int32_t max_pos;
84 struct commit *commit = lookup_commit(r, oid);
86 if (!commit)
87 return; /* should never happen, but be lenient */
89 *commit_pos_at(&commit_pos, commit) = max_pos++;
92 static int commit_pos_cmp(const void *va, const void *vb)
94 const struct commit *a = *(const struct commit **)va;
95 const struct commit *b = *(const struct commit **)vb;
96 return commit_pos_at(&commit_pos, a) -
97 commit_pos_at(&commit_pos, b);
100 define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
101 static struct commit_graph_data_slab commit_graph_data_slab =
102 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
104 static int get_configured_generation_version(struct repository *r)
106 int version = 2;
107 repo_config_get_int(r, "commitgraph.generationversion", &version);
108 return version;
111 uint32_t commit_graph_position(const struct commit *c)
113 struct commit_graph_data *data =
114 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
116 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
119 timestamp_t commit_graph_generation(const struct commit *c)
121 struct commit_graph_data *data =
122 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
124 if (!data)
125 return GENERATION_NUMBER_INFINITY;
126 else if (data->graph_pos == COMMIT_NOT_FROM_GRAPH)
127 return GENERATION_NUMBER_INFINITY;
129 return data->generation;
132 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
134 unsigned int i, nth_slab;
135 struct commit_graph_data *data =
136 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
138 if (data)
139 return data;
141 nth_slab = c->index / commit_graph_data_slab.slab_size;
142 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
145 * commit-slab initializes elements with zero, overwrite this with
146 * COMMIT_NOT_FROM_GRAPH for graph_pos.
148 * We avoid initializing generation with checking if graph position
149 * is not COMMIT_NOT_FROM_GRAPH.
151 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
152 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
153 COMMIT_NOT_FROM_GRAPH;
156 return data;
160 * Should be used only while writing commit-graph as it compares
161 * generation value of commits by directly accessing commit-slab.
163 static int commit_gen_cmp(const void *va, const void *vb)
165 const struct commit *a = *(const struct commit **)va;
166 const struct commit *b = *(const struct commit **)vb;
168 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
169 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
170 /* lower generation commits first */
171 if (generation_a < generation_b)
172 return -1;
173 else if (generation_a > generation_b)
174 return 1;
176 /* use date as a heuristic when generations are equal */
177 if (a->date < b->date)
178 return -1;
179 else if (a->date > b->date)
180 return 1;
181 return 0;
184 char *get_commit_graph_filename(struct object_directory *obj_dir)
186 return xstrfmt("%s/info/commit-graph", obj_dir->path);
189 static char *get_split_graph_filename(struct object_directory *odb,
190 const char *oid_hex)
192 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
193 oid_hex);
196 char *get_commit_graph_chain_filename(struct object_directory *odb)
198 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
201 static struct commit_graph *alloc_commit_graph(void)
203 struct commit_graph *g = xcalloc(1, sizeof(*g));
205 return g;
208 extern int read_replace_refs;
210 static int commit_graph_compatible(struct repository *r)
212 if (!r->gitdir)
213 return 0;
215 if (read_replace_refs) {
216 prepare_replace_object(r);
217 if (hashmap_get_size(&r->objects->replace_map->map))
218 return 0;
221 prepare_commit_graft(r);
222 if (r->parsed_objects &&
223 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
224 return 0;
225 if (is_repository_shallow(r))
226 return 0;
228 return 1;
231 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
233 *fd = git_open(graph_file);
234 if (*fd < 0)
235 return 0;
236 if (fstat(*fd, st)) {
237 close(*fd);
238 return 0;
240 return 1;
243 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
244 int fd, struct stat *st,
245 struct object_directory *odb)
247 void *graph_map;
248 size_t graph_size;
249 struct commit_graph *ret;
251 graph_size = xsize_t(st->st_size);
253 if (graph_size < GRAPH_MIN_SIZE) {
254 close(fd);
255 error(_("commit-graph file is too small"));
256 return NULL;
258 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
259 close(fd);
260 prepare_repo_settings(r);
261 ret = parse_commit_graph(&r->settings, graph_map, graph_size);
263 if (ret)
264 ret->odb = odb;
265 else
266 munmap(graph_map, graph_size);
268 return ret;
271 static int verify_commit_graph_lite(struct commit_graph *g)
274 * Basic validation shared between parse_commit_graph()
275 * which'll be called every time the graph is used, and the
276 * much more expensive verify_commit_graph() used by
277 * "commit-graph verify".
279 * There should only be very basic checks here to ensure that
280 * we don't e.g. segfault in fill_commit_in_graph(), but
281 * because this is a very hot codepath nothing that e.g. loops
282 * over g->num_commits, or runs a checksum on the commit-graph
283 * itself.
285 if (!g->chunk_oid_fanout) {
286 error("commit-graph is missing the OID Fanout chunk");
287 return 1;
289 if (!g->chunk_oid_lookup) {
290 error("commit-graph is missing the OID Lookup chunk");
291 return 1;
293 if (!g->chunk_commit_data) {
294 error("commit-graph is missing the Commit Data chunk");
295 return 1;
298 return 0;
301 static int graph_read_oid_lookup(const unsigned char *chunk_start,
302 size_t chunk_size, void *data)
304 struct commit_graph *g = data;
305 g->chunk_oid_lookup = chunk_start;
306 g->num_commits = chunk_size / g->hash_len;
307 return 0;
310 static int graph_read_bloom_data(const unsigned char *chunk_start,
311 size_t chunk_size, void *data)
313 struct commit_graph *g = data;
314 uint32_t hash_version;
315 g->chunk_bloom_data = chunk_start;
316 hash_version = get_be32(chunk_start);
318 if (hash_version != 1)
319 return 0;
321 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
322 g->bloom_filter_settings->hash_version = hash_version;
323 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
324 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
325 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
327 return 0;
330 struct commit_graph *parse_commit_graph(struct repo_settings *s,
331 void *graph_map, size_t graph_size)
333 const unsigned char *data;
334 struct commit_graph *graph;
335 uint32_t graph_signature;
336 unsigned char graph_version, hash_version;
337 struct chunkfile *cf = NULL;
339 if (!graph_map)
340 return NULL;
342 if (graph_size < GRAPH_MIN_SIZE)
343 return NULL;
345 data = (const unsigned char *)graph_map;
347 graph_signature = get_be32(data);
348 if (graph_signature != GRAPH_SIGNATURE) {
349 error(_("commit-graph signature %X does not match signature %X"),
350 graph_signature, GRAPH_SIGNATURE);
351 return NULL;
354 graph_version = *(unsigned char*)(data + 4);
355 if (graph_version != GRAPH_VERSION) {
356 error(_("commit-graph version %X does not match version %X"),
357 graph_version, GRAPH_VERSION);
358 return NULL;
361 hash_version = *(unsigned char*)(data + 5);
362 if (hash_version != oid_version(the_hash_algo)) {
363 error(_("commit-graph hash version %X does not match version %X"),
364 hash_version, oid_version(the_hash_algo));
365 return NULL;
368 graph = alloc_commit_graph();
370 graph->hash_len = the_hash_algo->rawsz;
371 graph->num_chunks = *(unsigned char*)(data + 6);
372 graph->data = graph_map;
373 graph->data_len = graph_size;
375 if (graph_size < GRAPH_HEADER_SIZE +
376 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
377 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
378 error(_("commit-graph file is too small to hold %u chunks"),
379 graph->num_chunks);
380 free(graph);
381 return NULL;
384 cf = init_chunkfile(NULL);
386 if (read_table_of_contents(cf, graph->data, graph_size,
387 GRAPH_HEADER_SIZE, graph->num_chunks))
388 goto free_and_return;
390 pair_chunk(cf, GRAPH_CHUNKID_OIDFANOUT,
391 (const unsigned char **)&graph->chunk_oid_fanout);
392 read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph);
393 pair_chunk(cf, GRAPH_CHUNKID_DATA, &graph->chunk_commit_data);
394 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges);
395 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs);
397 if (s->commit_graph_generation_version >= 2) {
398 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
399 &graph->chunk_generation_data);
400 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
401 &graph->chunk_generation_data_overflow);
403 if (graph->chunk_generation_data)
404 graph->read_generation_data = 1;
407 if (s->commit_graph_read_changed_paths) {
408 pair_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
409 &graph->chunk_bloom_indexes);
410 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
411 graph_read_bloom_data, graph);
414 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
415 init_bloom_filters();
416 } else {
417 /* We need both the bloom chunks to exist together. Else ignore the data */
418 graph->chunk_bloom_indexes = NULL;
419 graph->chunk_bloom_data = NULL;
420 FREE_AND_NULL(graph->bloom_filter_settings);
423 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
425 if (verify_commit_graph_lite(graph))
426 goto free_and_return;
428 free_chunkfile(cf);
429 return graph;
431 free_and_return:
432 free_chunkfile(cf);
433 free(graph->bloom_filter_settings);
434 free(graph);
435 return NULL;
438 static struct commit_graph *load_commit_graph_one(struct repository *r,
439 const char *graph_file,
440 struct object_directory *odb)
443 struct stat st;
444 int fd;
445 struct commit_graph *g;
446 int open_ok = open_commit_graph(graph_file, &fd, &st);
448 if (!open_ok)
449 return NULL;
451 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
453 if (g)
454 g->filename = xstrdup(graph_file);
456 return g;
459 static struct commit_graph *load_commit_graph_v1(struct repository *r,
460 struct object_directory *odb)
462 char *graph_name = get_commit_graph_filename(odb);
463 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
464 free(graph_name);
466 return g;
469 static int add_graph_to_chain(struct commit_graph *g,
470 struct commit_graph *chain,
471 struct object_id *oids,
472 int n)
474 struct commit_graph *cur_g = chain;
476 if (n && !g->chunk_base_graphs) {
477 warning(_("commit-graph has no base graphs chunk"));
478 return 0;
481 while (n) {
482 n--;
484 if (!cur_g ||
485 !oideq(&oids[n], &cur_g->oid) ||
486 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
487 warning(_("commit-graph chain does not match"));
488 return 0;
491 cur_g = cur_g->base_graph;
494 g->base_graph = chain;
496 if (chain)
497 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
499 return 1;
502 static struct commit_graph *load_commit_graph_chain(struct repository *r,
503 struct object_directory *odb)
505 struct commit_graph *graph_chain = NULL;
506 struct strbuf line = STRBUF_INIT;
507 struct stat st;
508 struct object_id *oids;
509 int i = 0, valid = 1, count;
510 char *chain_name = get_commit_graph_chain_filename(odb);
511 FILE *fp;
512 int stat_res;
514 fp = fopen(chain_name, "r");
515 stat_res = stat(chain_name, &st);
516 free(chain_name);
518 if (!fp)
519 return NULL;
520 if (stat_res ||
521 st.st_size <= the_hash_algo->hexsz) {
522 fclose(fp);
523 return NULL;
526 count = st.st_size / (the_hash_algo->hexsz + 1);
527 CALLOC_ARRAY(oids, count);
529 prepare_alt_odb(r);
531 for (i = 0; i < count; i++) {
532 struct object_directory *odb;
534 if (strbuf_getline_lf(&line, fp) == EOF)
535 break;
537 if (get_oid_hex(line.buf, &oids[i])) {
538 warning(_("invalid commit-graph chain: line '%s' not a hash"),
539 line.buf);
540 valid = 0;
541 break;
544 valid = 0;
545 for (odb = r->objects->odb; odb; odb = odb->next) {
546 char *graph_name = get_split_graph_filename(odb, line.buf);
547 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
549 free(graph_name);
551 if (g) {
552 if (add_graph_to_chain(g, graph_chain, oids, i)) {
553 graph_chain = g;
554 valid = 1;
557 break;
561 if (!valid) {
562 warning(_("unable to find all commit-graph files"));
563 break;
567 free(oids);
568 fclose(fp);
569 strbuf_release(&line);
571 return graph_chain;
575 * returns 1 if and only if all graphs in the chain have
576 * corrected commit dates stored in the generation_data chunk.
578 static int validate_mixed_generation_chain(struct commit_graph *g)
580 int read_generation_data = 1;
581 struct commit_graph *p = g;
583 while (read_generation_data && p) {
584 read_generation_data = p->read_generation_data;
585 p = p->base_graph;
588 if (read_generation_data)
589 return 1;
591 while (g) {
592 g->read_generation_data = 0;
593 g = g->base_graph;
596 return 0;
599 struct commit_graph *read_commit_graph_one(struct repository *r,
600 struct object_directory *odb)
602 struct commit_graph *g = load_commit_graph_v1(r, odb);
604 if (!g)
605 g = load_commit_graph_chain(r, odb);
607 validate_mixed_generation_chain(g);
609 return g;
612 static void prepare_commit_graph_one(struct repository *r,
613 struct object_directory *odb)
616 if (r->objects->commit_graph)
617 return;
619 r->objects->commit_graph = read_commit_graph_one(r, odb);
623 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
625 * On the first invocation, this function attempts to load the commit
626 * graph if the_repository is configured to have one.
628 static int prepare_commit_graph(struct repository *r)
630 struct object_directory *odb;
633 * Early return if there is no git dir or if the commit graph is
634 * disabled.
636 * This must come before the "already attempted?" check below, because
637 * we want to disable even an already-loaded graph file.
639 if (!r->gitdir || r->commit_graph_disabled)
640 return 0;
642 if (r->objects->commit_graph_attempted)
643 return !!r->objects->commit_graph;
644 r->objects->commit_graph_attempted = 1;
646 prepare_repo_settings(r);
648 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
649 r->settings.core_commit_graph != 1)
651 * This repository is not configured to use commit graphs, so
652 * do not load one. (But report commit_graph_attempted anyway
653 * so that commit graph loading is not attempted again for this
654 * repository.)
656 return 0;
658 if (!commit_graph_compatible(r))
659 return 0;
661 prepare_alt_odb(r);
662 for (odb = r->objects->odb;
663 !r->objects->commit_graph && odb;
664 odb = odb->next)
665 prepare_commit_graph_one(r, odb);
666 return !!r->objects->commit_graph;
669 int generation_numbers_enabled(struct repository *r)
671 uint32_t first_generation;
672 struct commit_graph *g;
673 if (!prepare_commit_graph(r))
674 return 0;
676 g = r->objects->commit_graph;
678 if (!g->num_commits)
679 return 0;
681 first_generation = get_be32(g->chunk_commit_data +
682 g->hash_len + 8) >> 2;
684 return !!first_generation;
687 int corrected_commit_dates_enabled(struct repository *r)
689 struct commit_graph *g;
690 if (!prepare_commit_graph(r))
691 return 0;
693 g = r->objects->commit_graph;
695 if (!g->num_commits)
696 return 0;
698 return g->read_generation_data;
701 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
703 struct commit_graph *g = r->objects->commit_graph;
704 while (g) {
705 if (g->bloom_filter_settings)
706 return g->bloom_filter_settings;
707 g = g->base_graph;
709 return NULL;
712 static void close_commit_graph_one(struct commit_graph *g)
714 if (!g)
715 return;
717 clear_commit_graph_data_slab(&commit_graph_data_slab);
718 close_commit_graph_one(g->base_graph);
719 free_commit_graph(g);
722 void close_commit_graph(struct raw_object_store *o)
724 close_commit_graph_one(o->commit_graph);
725 o->commit_graph = NULL;
728 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
730 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
731 g->chunk_oid_lookup, g->hash_len, pos);
734 static void load_oid_from_graph(struct commit_graph *g,
735 uint32_t pos,
736 struct object_id *oid)
738 uint32_t lex_index;
740 while (g && pos < g->num_commits_in_base)
741 g = g->base_graph;
743 if (!g)
744 BUG("NULL commit-graph");
746 if (pos >= g->num_commits + g->num_commits_in_base)
747 die(_("invalid commit position. commit-graph is likely corrupt"));
749 lex_index = pos - g->num_commits_in_base;
751 oidread(oid, g->chunk_oid_lookup + g->hash_len * lex_index);
754 static struct commit_list **insert_parent_or_die(struct repository *r,
755 struct commit_graph *g,
756 uint32_t pos,
757 struct commit_list **pptr)
759 struct commit *c;
760 struct object_id oid;
762 if (pos >= g->num_commits + g->num_commits_in_base)
763 die("invalid parent position %"PRIu32, pos);
765 load_oid_from_graph(g, pos, &oid);
766 c = lookup_commit(r, &oid);
767 if (!c)
768 die(_("could not find commit %s"), oid_to_hex(&oid));
769 commit_graph_data_at(c)->graph_pos = pos;
770 return &commit_list_insert(c, pptr)->next;
773 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
775 const unsigned char *commit_data;
776 struct commit_graph_data *graph_data;
777 uint32_t lex_index, offset_pos;
778 uint64_t date_high, date_low, offset;
780 while (pos < g->num_commits_in_base)
781 g = g->base_graph;
783 if (pos >= g->num_commits + g->num_commits_in_base)
784 die(_("invalid commit position. commit-graph is likely corrupt"));
786 lex_index = pos - g->num_commits_in_base;
787 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
789 graph_data = commit_graph_data_at(item);
790 graph_data->graph_pos = pos;
792 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
793 date_low = get_be32(commit_data + g->hash_len + 12);
794 item->date = (timestamp_t)((date_high << 32) | date_low);
796 if (g->read_generation_data) {
797 offset = (timestamp_t)get_be32(g->chunk_generation_data + sizeof(uint32_t) * lex_index);
799 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
800 if (!g->chunk_generation_data_overflow)
801 die(_("commit-graph requires overflow generation data but has none"));
803 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
804 graph_data->generation = item->date + get_be64(g->chunk_generation_data_overflow + 8 * offset_pos);
805 } else
806 graph_data->generation = item->date + offset;
807 } else
808 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
810 if (g->topo_levels)
811 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
814 static inline void set_commit_tree(struct commit *c, struct tree *t)
816 c->maybe_tree = t;
819 static int fill_commit_in_graph(struct repository *r,
820 struct commit *item,
821 struct commit_graph *g, uint32_t pos)
823 uint32_t edge_value;
824 uint32_t *parent_data_ptr;
825 struct commit_list **pptr;
826 const unsigned char *commit_data;
827 uint32_t lex_index;
829 while (pos < g->num_commits_in_base)
830 g = g->base_graph;
832 fill_commit_graph_info(item, g, pos);
834 lex_index = pos - g->num_commits_in_base;
835 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
837 item->object.parsed = 1;
839 set_commit_tree(item, NULL);
841 pptr = &item->parents;
843 edge_value = get_be32(commit_data + g->hash_len);
844 if (edge_value == GRAPH_PARENT_NONE)
845 return 1;
846 pptr = insert_parent_or_die(r, g, edge_value, pptr);
848 edge_value = get_be32(commit_data + g->hash_len + 4);
849 if (edge_value == GRAPH_PARENT_NONE)
850 return 1;
851 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
852 pptr = insert_parent_or_die(r, g, edge_value, pptr);
853 return 1;
856 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
857 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
858 do {
859 edge_value = get_be32(parent_data_ptr);
860 pptr = insert_parent_or_die(r, g,
861 edge_value & GRAPH_EDGE_LAST_MASK,
862 pptr);
863 parent_data_ptr++;
864 } while (!(edge_value & GRAPH_LAST_EDGE));
866 return 1;
869 static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
871 struct commit_graph *cur_g = g;
872 uint32_t lex_index;
874 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
875 cur_g = cur_g->base_graph;
877 if (cur_g) {
878 *pos = lex_index + cur_g->num_commits_in_base;
879 return 1;
882 return 0;
885 static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
887 uint32_t graph_pos = commit_graph_position(item);
888 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
889 *pos = graph_pos;
890 return 1;
891 } else {
892 return search_commit_pos_in_graph(&item->object.oid, g, pos);
896 int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
897 uint32_t *pos)
899 if (!prepare_commit_graph(r))
900 return 0;
901 return find_commit_pos_in_graph(c, r->objects->commit_graph, pos);
904 struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
906 struct commit *commit;
907 uint32_t pos;
909 if (!prepare_commit_graph(repo))
910 return NULL;
911 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
912 return NULL;
913 if (!has_object(repo, id, 0))
914 return NULL;
916 commit = lookup_commit(repo, id);
917 if (!commit)
918 return NULL;
919 if (commit->object.parsed)
920 return commit;
922 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
923 return NULL;
925 return commit;
928 static int parse_commit_in_graph_one(struct repository *r,
929 struct commit_graph *g,
930 struct commit *item)
932 uint32_t pos;
934 if (item->object.parsed)
935 return 1;
937 if (find_commit_pos_in_graph(item, g, &pos))
938 return fill_commit_in_graph(r, item, g, pos);
940 return 0;
943 int parse_commit_in_graph(struct repository *r, struct commit *item)
945 static int checked_env = 0;
947 if (!checked_env &&
948 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
949 die("dying as requested by the '%s' variable on commit-graph parse!",
950 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
951 checked_env = 1;
953 if (!prepare_commit_graph(r))
954 return 0;
955 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
958 void load_commit_graph_info(struct repository *r, struct commit *item)
960 uint32_t pos;
961 if (repo_find_commit_pos_in_graph(r, item, &pos))
962 fill_commit_graph_info(item, r->objects->commit_graph, pos);
965 static struct tree *load_tree_for_commit(struct repository *r,
966 struct commit_graph *g,
967 struct commit *c)
969 struct object_id oid;
970 const unsigned char *commit_data;
971 uint32_t graph_pos = commit_graph_position(c);
973 while (graph_pos < g->num_commits_in_base)
974 g = g->base_graph;
976 commit_data = g->chunk_commit_data +
977 GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base);
979 oidread(&oid, commit_data);
980 set_commit_tree(c, lookup_tree(r, &oid));
982 return c->maybe_tree;
985 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
986 struct commit_graph *g,
987 const struct commit *c)
989 if (c->maybe_tree)
990 return c->maybe_tree;
991 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
992 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
994 return load_tree_for_commit(r, g, (struct commit *)c);
997 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
999 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
1002 struct packed_commit_list {
1003 struct commit **list;
1004 size_t nr;
1005 size_t alloc;
1008 struct write_commit_graph_context {
1009 struct repository *r;
1010 struct object_directory *odb;
1011 char *graph_name;
1012 struct oid_array oids;
1013 struct packed_commit_list commits;
1014 int num_extra_edges;
1015 int num_generation_data_overflows;
1016 unsigned long approx_nr_objects;
1017 struct progress *progress;
1018 int progress_done;
1019 uint64_t progress_cnt;
1021 char *base_graph_name;
1022 int num_commit_graphs_before;
1023 int num_commit_graphs_after;
1024 char **commit_graph_filenames_before;
1025 char **commit_graph_filenames_after;
1026 char **commit_graph_hash_after;
1027 uint32_t new_num_commits_in_base;
1028 struct commit_graph *new_base_graph;
1030 unsigned append:1,
1031 report_progress:1,
1032 split:1,
1033 changed_paths:1,
1034 order_by_pack:1,
1035 write_generation_data:1,
1036 trust_generation_numbers:1;
1038 struct topo_level_slab *topo_levels;
1039 const struct commit_graph_opts *opts;
1040 size_t total_bloom_filter_data_size;
1041 const struct bloom_filter_settings *bloom_settings;
1043 int count_bloom_filter_computed;
1044 int count_bloom_filter_not_computed;
1045 int count_bloom_filter_trunc_empty;
1046 int count_bloom_filter_trunc_large;
1049 static int write_graph_chunk_fanout(struct hashfile *f,
1050 void *data)
1052 struct write_commit_graph_context *ctx = data;
1053 int i, count = 0;
1054 struct commit **list = ctx->commits.list;
1057 * Write the first-level table (the list is sorted,
1058 * but we use a 256-entry lookup to be able to avoid
1059 * having to do eight extra binary search iterations).
1061 for (i = 0; i < 256; i++) {
1062 while (count < ctx->commits.nr) {
1063 if ((*list)->object.oid.hash[0] != i)
1064 break;
1065 display_progress(ctx->progress, ++ctx->progress_cnt);
1066 count++;
1067 list++;
1070 hashwrite_be32(f, count);
1073 return 0;
1076 static int write_graph_chunk_oids(struct hashfile *f,
1077 void *data)
1079 struct write_commit_graph_context *ctx = data;
1080 struct commit **list = ctx->commits.list;
1081 int count;
1082 for (count = 0; count < ctx->commits.nr; count++, list++) {
1083 display_progress(ctx->progress, ++ctx->progress_cnt);
1084 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1087 return 0;
1090 static const struct object_id *commit_to_oid(size_t index, const void *table)
1092 const struct commit * const *commits = table;
1093 return &commits[index]->object.oid;
1096 static int write_graph_chunk_data(struct hashfile *f,
1097 void *data)
1099 struct write_commit_graph_context *ctx = data;
1100 struct commit **list = ctx->commits.list;
1101 struct commit **last = ctx->commits.list + ctx->commits.nr;
1102 uint32_t num_extra_edges = 0;
1104 while (list < last) {
1105 struct commit_list *parent;
1106 struct object_id *tree;
1107 int edge_value;
1108 uint32_t packedDate[2];
1109 display_progress(ctx->progress, ++ctx->progress_cnt);
1111 if (repo_parse_commit_no_graph(ctx->r, *list))
1112 die(_("unable to parse commit %s"),
1113 oid_to_hex(&(*list)->object.oid));
1114 tree = get_commit_tree_oid(*list);
1115 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1117 parent = (*list)->parents;
1119 if (!parent)
1120 edge_value = GRAPH_PARENT_NONE;
1121 else {
1122 edge_value = oid_pos(&parent->item->object.oid,
1123 ctx->commits.list,
1124 ctx->commits.nr,
1125 commit_to_oid);
1127 if (edge_value >= 0)
1128 edge_value += ctx->new_num_commits_in_base;
1129 else if (ctx->new_base_graph) {
1130 uint32_t pos;
1131 if (find_commit_pos_in_graph(parent->item,
1132 ctx->new_base_graph,
1133 &pos))
1134 edge_value = pos;
1137 if (edge_value < 0)
1138 BUG("missing parent %s for commit %s",
1139 oid_to_hex(&parent->item->object.oid),
1140 oid_to_hex(&(*list)->object.oid));
1143 hashwrite_be32(f, edge_value);
1145 if (parent)
1146 parent = parent->next;
1148 if (!parent)
1149 edge_value = GRAPH_PARENT_NONE;
1150 else if (parent->next)
1151 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1152 else {
1153 edge_value = oid_pos(&parent->item->object.oid,
1154 ctx->commits.list,
1155 ctx->commits.nr,
1156 commit_to_oid);
1158 if (edge_value >= 0)
1159 edge_value += ctx->new_num_commits_in_base;
1160 else if (ctx->new_base_graph) {
1161 uint32_t pos;
1162 if (find_commit_pos_in_graph(parent->item,
1163 ctx->new_base_graph,
1164 &pos))
1165 edge_value = pos;
1168 if (edge_value < 0)
1169 BUG("missing parent %s for commit %s",
1170 oid_to_hex(&parent->item->object.oid),
1171 oid_to_hex(&(*list)->object.oid));
1174 hashwrite_be32(f, edge_value);
1176 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1177 do {
1178 num_extra_edges++;
1179 parent = parent->next;
1180 } while (parent);
1183 if (sizeof((*list)->date) > 4)
1184 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1185 else
1186 packedDate[0] = 0;
1188 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1190 packedDate[1] = htonl((*list)->date);
1191 hashwrite(f, packedDate, 8);
1193 list++;
1196 return 0;
1199 static int write_graph_chunk_generation_data(struct hashfile *f,
1200 void *data)
1202 struct write_commit_graph_context *ctx = data;
1203 int i, num_generation_data_overflows = 0;
1205 for (i = 0; i < ctx->commits.nr; i++) {
1206 struct commit *c = ctx->commits.list[i];
1207 timestamp_t offset;
1208 repo_parse_commit(ctx->r, c);
1209 offset = commit_graph_data_at(c)->generation - c->date;
1210 display_progress(ctx->progress, ++ctx->progress_cnt);
1212 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1213 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1214 num_generation_data_overflows++;
1217 hashwrite_be32(f, offset);
1220 return 0;
1223 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1224 void *data)
1226 struct write_commit_graph_context *ctx = data;
1227 int i;
1228 for (i = 0; i < ctx->commits.nr; i++) {
1229 struct commit *c = ctx->commits.list[i];
1230 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1231 display_progress(ctx->progress, ++ctx->progress_cnt);
1233 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1234 hashwrite_be32(f, offset >> 32);
1235 hashwrite_be32(f, (uint32_t) offset);
1239 return 0;
1242 static int write_graph_chunk_extra_edges(struct hashfile *f,
1243 void *data)
1245 struct write_commit_graph_context *ctx = data;
1246 struct commit **list = ctx->commits.list;
1247 struct commit **last = ctx->commits.list + ctx->commits.nr;
1248 struct commit_list *parent;
1250 while (list < last) {
1251 int num_parents = 0;
1253 display_progress(ctx->progress, ++ctx->progress_cnt);
1255 for (parent = (*list)->parents; num_parents < 3 && parent;
1256 parent = parent->next)
1257 num_parents++;
1259 if (num_parents <= 2) {
1260 list++;
1261 continue;
1264 /* Since num_parents > 2, this initializer is safe. */
1265 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1266 int edge_value = oid_pos(&parent->item->object.oid,
1267 ctx->commits.list,
1268 ctx->commits.nr,
1269 commit_to_oid);
1271 if (edge_value >= 0)
1272 edge_value += ctx->new_num_commits_in_base;
1273 else if (ctx->new_base_graph) {
1274 uint32_t pos;
1275 if (find_commit_pos_in_graph(parent->item,
1276 ctx->new_base_graph,
1277 &pos))
1278 edge_value = pos;
1281 if (edge_value < 0)
1282 BUG("missing parent %s for commit %s",
1283 oid_to_hex(&parent->item->object.oid),
1284 oid_to_hex(&(*list)->object.oid));
1285 else if (!parent->next)
1286 edge_value |= GRAPH_LAST_EDGE;
1288 hashwrite_be32(f, edge_value);
1291 list++;
1294 return 0;
1297 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1298 void *data)
1300 struct write_commit_graph_context *ctx = data;
1301 struct commit **list = ctx->commits.list;
1302 struct commit **last = ctx->commits.list + ctx->commits.nr;
1303 uint32_t cur_pos = 0;
1305 while (list < last) {
1306 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1307 size_t len = filter ? filter->len : 0;
1308 cur_pos += len;
1309 display_progress(ctx->progress, ++ctx->progress_cnt);
1310 hashwrite_be32(f, cur_pos);
1311 list++;
1314 return 0;
1317 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1319 struct json_writer jw = JSON_WRITER_INIT;
1321 jw_object_begin(&jw, 0);
1322 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1323 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1324 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1325 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1326 jw_end(&jw);
1328 trace2_data_json("bloom", ctx->r, "settings", &jw);
1330 jw_release(&jw);
1333 static int write_graph_chunk_bloom_data(struct hashfile *f,
1334 void *data)
1336 struct write_commit_graph_context *ctx = data;
1337 struct commit **list = ctx->commits.list;
1338 struct commit **last = ctx->commits.list + ctx->commits.nr;
1340 trace2_bloom_filter_settings(ctx);
1342 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1343 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1344 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1346 while (list < last) {
1347 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1348 size_t len = filter ? filter->len : 0;
1350 display_progress(ctx->progress, ++ctx->progress_cnt);
1351 if (len)
1352 hashwrite(f, filter->data, len * sizeof(unsigned char));
1353 list++;
1356 return 0;
1359 static int add_packed_commits(const struct object_id *oid,
1360 struct packed_git *pack,
1361 uint32_t pos,
1362 void *data)
1364 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1365 enum object_type type;
1366 off_t offset = nth_packed_object_offset(pack, pos);
1367 struct object_info oi = OBJECT_INFO_INIT;
1369 if (ctx->progress)
1370 display_progress(ctx->progress, ++ctx->progress_done);
1372 oi.typep = &type;
1373 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1374 die(_("unable to get type of object %s"), oid_to_hex(oid));
1376 if (type != OBJ_COMMIT)
1377 return 0;
1379 oid_array_append(&ctx->oids, oid);
1380 set_commit_pos(ctx->r, oid);
1382 return 0;
1385 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1387 struct commit_list *parent;
1388 for (parent = commit->parents; parent; parent = parent->next) {
1389 if (!(parent->item->object.flags & REACHABLE)) {
1390 oid_array_append(&ctx->oids, &parent->item->object.oid);
1391 parent->item->object.flags |= REACHABLE;
1396 static void close_reachable(struct write_commit_graph_context *ctx)
1398 int i;
1399 struct commit *commit;
1400 enum commit_graph_split_flags flags = ctx->opts ?
1401 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1403 if (ctx->report_progress)
1404 ctx->progress = start_delayed_progress(
1405 _("Loading known commits in commit graph"),
1406 ctx->oids.nr);
1407 for (i = 0; i < ctx->oids.nr; i++) {
1408 display_progress(ctx->progress, i + 1);
1409 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1410 if (commit)
1411 commit->object.flags |= REACHABLE;
1413 stop_progress(&ctx->progress);
1416 * As this loop runs, ctx->oids.nr may grow, but not more
1417 * than the number of missing commits in the reachable
1418 * closure.
1420 if (ctx->report_progress)
1421 ctx->progress = start_delayed_progress(
1422 _("Expanding reachable commits in commit graph"),
1424 for (i = 0; i < ctx->oids.nr; i++) {
1425 display_progress(ctx->progress, i + 1);
1426 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1428 if (!commit)
1429 continue;
1430 if (ctx->split) {
1431 if ((!repo_parse_commit(ctx->r, commit) &&
1432 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1433 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1434 add_missing_parents(ctx, commit);
1435 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1436 add_missing_parents(ctx, commit);
1438 stop_progress(&ctx->progress);
1440 if (ctx->report_progress)
1441 ctx->progress = start_delayed_progress(
1442 _("Clearing commit marks in commit graph"),
1443 ctx->oids.nr);
1444 for (i = 0; i < ctx->oids.nr; i++) {
1445 display_progress(ctx->progress, i + 1);
1446 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1448 if (commit)
1449 commit->object.flags &= ~REACHABLE;
1451 stop_progress(&ctx->progress);
1454 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1456 int i;
1457 struct commit_list *list = NULL;
1459 if (ctx->report_progress)
1460 ctx->progress = start_delayed_progress(
1461 _("Computing commit graph topological levels"),
1462 ctx->commits.nr);
1463 for (i = 0; i < ctx->commits.nr; i++) {
1464 struct commit *c = ctx->commits.list[i];
1465 uint32_t level;
1467 repo_parse_commit(ctx->r, c);
1468 level = *topo_level_slab_at(ctx->topo_levels, c);
1470 display_progress(ctx->progress, i + 1);
1471 if (level != GENERATION_NUMBER_ZERO)
1472 continue;
1474 commit_list_insert(c, &list);
1475 while (list) {
1476 struct commit *current = list->item;
1477 struct commit_list *parent;
1478 int all_parents_computed = 1;
1479 uint32_t max_level = 0;
1481 for (parent = current->parents; parent; parent = parent->next) {
1482 repo_parse_commit(ctx->r, parent->item);
1483 level = *topo_level_slab_at(ctx->topo_levels, parent->item);
1485 if (level == GENERATION_NUMBER_ZERO) {
1486 all_parents_computed = 0;
1487 commit_list_insert(parent->item, &list);
1488 break;
1491 if (level > max_level)
1492 max_level = level;
1495 if (all_parents_computed) {
1496 pop_commit(&list);
1498 if (max_level > GENERATION_NUMBER_V1_MAX - 1)
1499 max_level = GENERATION_NUMBER_V1_MAX - 1;
1500 *topo_level_slab_at(ctx->topo_levels, current) = max_level + 1;
1504 stop_progress(&ctx->progress);
1507 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1509 int i;
1510 struct commit_list *list = NULL;
1512 if (ctx->report_progress)
1513 ctx->progress = start_delayed_progress(
1514 _("Computing commit graph generation numbers"),
1515 ctx->commits.nr);
1517 if (!ctx->trust_generation_numbers) {
1518 for (i = 0; i < ctx->commits.nr; i++) {
1519 struct commit *c = ctx->commits.list[i];
1520 repo_parse_commit(ctx->r, c);
1521 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1525 for (i = 0; i < ctx->commits.nr; i++) {
1526 struct commit *c = ctx->commits.list[i];
1527 timestamp_t corrected_commit_date;
1529 repo_parse_commit(ctx->r, c);
1530 corrected_commit_date = commit_graph_data_at(c)->generation;
1532 display_progress(ctx->progress, i + 1);
1533 if (corrected_commit_date != GENERATION_NUMBER_ZERO)
1534 continue;
1536 commit_list_insert(c, &list);
1537 while (list) {
1538 struct commit *current = list->item;
1539 struct commit_list *parent;
1540 int all_parents_computed = 1;
1541 timestamp_t max_corrected_commit_date = 0;
1543 for (parent = current->parents; parent; parent = parent->next) {
1544 repo_parse_commit(ctx->r, parent->item);
1545 corrected_commit_date = commit_graph_data_at(parent->item)->generation;
1547 if (corrected_commit_date == GENERATION_NUMBER_ZERO) {
1548 all_parents_computed = 0;
1549 commit_list_insert(parent->item, &list);
1550 break;
1553 if (corrected_commit_date > max_corrected_commit_date)
1554 max_corrected_commit_date = corrected_commit_date;
1557 if (all_parents_computed) {
1558 pop_commit(&list);
1560 if (current->date && current->date > max_corrected_commit_date)
1561 max_corrected_commit_date = current->date - 1;
1562 commit_graph_data_at(current)->generation = max_corrected_commit_date + 1;
1567 for (i = 0; i < ctx->commits.nr; i++) {
1568 struct commit *c = ctx->commits.list[i];
1569 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1570 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1571 ctx->num_generation_data_overflows++;
1573 stop_progress(&ctx->progress);
1576 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1578 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1579 ctx->count_bloom_filter_computed);
1580 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1581 ctx->count_bloom_filter_not_computed);
1582 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1583 ctx->count_bloom_filter_trunc_empty);
1584 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1585 ctx->count_bloom_filter_trunc_large);
1588 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1590 int i;
1591 struct progress *progress = NULL;
1592 struct commit **sorted_commits;
1593 int max_new_filters;
1595 init_bloom_filters();
1597 if (ctx->report_progress)
1598 progress = start_delayed_progress(
1599 _("Computing commit changed paths Bloom filters"),
1600 ctx->commits.nr);
1602 DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1604 if (ctx->order_by_pack)
1605 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1606 else
1607 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1609 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1610 ctx->opts->max_new_filters : ctx->commits.nr;
1612 for (i = 0; i < ctx->commits.nr; i++) {
1613 enum bloom_filter_computed computed = 0;
1614 struct commit *c = sorted_commits[i];
1615 struct bloom_filter *filter = get_or_compute_bloom_filter(
1616 ctx->r,
1618 ctx->count_bloom_filter_computed < max_new_filters,
1619 ctx->bloom_settings,
1620 &computed);
1621 if (computed & BLOOM_COMPUTED) {
1622 ctx->count_bloom_filter_computed++;
1623 if (computed & BLOOM_TRUNC_EMPTY)
1624 ctx->count_bloom_filter_trunc_empty++;
1625 if (computed & BLOOM_TRUNC_LARGE)
1626 ctx->count_bloom_filter_trunc_large++;
1627 } else if (computed & BLOOM_NOT_COMPUTED)
1628 ctx->count_bloom_filter_not_computed++;
1629 ctx->total_bloom_filter_data_size += filter
1630 ? sizeof(unsigned char) * filter->len : 0;
1631 display_progress(progress, i + 1);
1634 if (trace2_is_enabled())
1635 trace2_bloom_filter_write_statistics(ctx);
1637 free(sorted_commits);
1638 stop_progress(&progress);
1641 struct refs_cb_data {
1642 struct oidset *commits;
1643 struct progress *progress;
1646 static int add_ref_to_set(const char *refname UNUSED,
1647 const struct object_id *oid,
1648 int flags UNUSED, void *cb_data)
1650 struct object_id peeled;
1651 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1653 if (!peel_iterated_oid(oid, &peeled))
1654 oid = &peeled;
1655 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1656 oidset_insert(data->commits, oid);
1658 display_progress(data->progress, oidset_size(data->commits));
1660 return 0;
1663 int write_commit_graph_reachable(struct object_directory *odb,
1664 enum commit_graph_write_flags flags,
1665 const struct commit_graph_opts *opts)
1667 struct oidset commits = OIDSET_INIT;
1668 struct refs_cb_data data;
1669 int result;
1671 memset(&data, 0, sizeof(data));
1672 data.commits = &commits;
1673 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1674 data.progress = start_delayed_progress(
1675 _("Collecting referenced commits"), 0);
1677 for_each_ref(add_ref_to_set, &data);
1679 stop_progress(&data.progress);
1681 result = write_commit_graph(odb, NULL, &commits,
1682 flags, opts);
1684 oidset_clear(&commits);
1685 return result;
1688 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1689 const struct string_list *pack_indexes)
1691 uint32_t i;
1692 struct strbuf progress_title = STRBUF_INIT;
1693 struct strbuf packname = STRBUF_INIT;
1694 int dirlen;
1695 int ret = 0;
1697 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1698 dirlen = packname.len;
1699 if (ctx->report_progress) {
1700 strbuf_addf(&progress_title,
1701 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1702 "Finding commits for commit graph in %"PRIuMAX" packs",
1703 pack_indexes->nr),
1704 (uintmax_t)pack_indexes->nr);
1705 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1706 ctx->progress_done = 0;
1708 for (i = 0; i < pack_indexes->nr; i++) {
1709 struct packed_git *p;
1710 strbuf_setlen(&packname, dirlen);
1711 strbuf_addstr(&packname, pack_indexes->items[i].string);
1712 p = add_packed_git(packname.buf, packname.len, 1);
1713 if (!p) {
1714 ret = error(_("error adding pack %s"), packname.buf);
1715 goto cleanup;
1717 if (open_pack_index(p)) {
1718 ret = error(_("error opening index for %s"), packname.buf);
1719 goto cleanup;
1721 for_each_object_in_pack(p, add_packed_commits, ctx,
1722 FOR_EACH_OBJECT_PACK_ORDER);
1723 close_pack(p);
1724 free(p);
1727 cleanup:
1728 stop_progress(&ctx->progress);
1729 strbuf_release(&progress_title);
1730 strbuf_release(&packname);
1732 return ret;
1735 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1736 struct oidset *commits)
1738 struct oidset_iter iter;
1739 struct object_id *oid;
1741 if (!oidset_size(commits))
1742 return 0;
1744 oidset_iter_init(commits, &iter);
1745 while ((oid = oidset_iter_next(&iter))) {
1746 oid_array_append(&ctx->oids, oid);
1749 return 0;
1752 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1754 if (ctx->report_progress)
1755 ctx->progress = start_delayed_progress(
1756 _("Finding commits for commit graph among packed objects"),
1757 ctx->approx_nr_objects);
1758 for_each_packed_object(add_packed_commits, ctx,
1759 FOR_EACH_OBJECT_PACK_ORDER);
1760 if (ctx->progress_done < ctx->approx_nr_objects)
1761 display_progress(ctx->progress, ctx->approx_nr_objects);
1762 stop_progress(&ctx->progress);
1765 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1767 uint32_t i;
1768 enum commit_graph_split_flags flags = ctx->opts ?
1769 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1771 ctx->num_extra_edges = 0;
1772 if (ctx->report_progress)
1773 ctx->progress = start_delayed_progress(
1774 _("Finding extra edges in commit graph"),
1775 ctx->oids.nr);
1776 oid_array_sort(&ctx->oids);
1777 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1778 unsigned int num_parents;
1780 display_progress(ctx->progress, i + 1);
1782 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1783 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1785 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1786 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1787 continue;
1789 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1790 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1791 else
1792 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1794 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1795 if (num_parents > 2)
1796 ctx->num_extra_edges += num_parents - 1;
1798 ctx->commits.nr++;
1800 stop_progress(&ctx->progress);
1803 static int write_graph_chunk_base_1(struct hashfile *f,
1804 struct commit_graph *g)
1806 int num = 0;
1808 if (!g)
1809 return 0;
1811 num = write_graph_chunk_base_1(f, g->base_graph);
1812 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1813 return num + 1;
1816 static int write_graph_chunk_base(struct hashfile *f,
1817 void *data)
1819 struct write_commit_graph_context *ctx = data;
1820 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1822 if (num != ctx->num_commit_graphs_after - 1) {
1823 error(_("failed to write correct number of base graph ids"));
1824 return -1;
1827 return 0;
1830 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1832 uint32_t i;
1833 int fd;
1834 struct hashfile *f;
1835 struct lock_file lk = LOCK_INIT;
1836 const unsigned hashsz = the_hash_algo->rawsz;
1837 struct strbuf progress_title = STRBUF_INIT;
1838 struct chunkfile *cf;
1839 unsigned char file_hash[GIT_MAX_RAWSZ];
1841 if (ctx->split) {
1842 struct strbuf tmp_file = STRBUF_INIT;
1844 strbuf_addf(&tmp_file,
1845 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1846 ctx->odb->path);
1847 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1848 } else {
1849 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1852 if (safe_create_leading_directories(ctx->graph_name)) {
1853 UNLEAK(ctx->graph_name);
1854 error(_("unable to create leading directories of %s"),
1855 ctx->graph_name);
1856 return -1;
1859 if (ctx->split) {
1860 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
1862 hold_lock_file_for_update_mode(&lk, lock_name,
1863 LOCK_DIE_ON_ERROR, 0444);
1864 free(lock_name);
1866 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1867 if (fd < 0) {
1868 error(_("unable to create temporary graph layer"));
1869 return -1;
1872 if (adjust_shared_perm(ctx->graph_name)) {
1873 error(_("unable to adjust shared permissions for '%s'"),
1874 ctx->graph_name);
1875 return -1;
1878 f = hashfd(fd, ctx->graph_name);
1879 } else {
1880 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1881 LOCK_DIE_ON_ERROR, 0444);
1882 fd = get_lock_file_fd(&lk);
1883 f = hashfd(fd, get_lock_file_path(&lk));
1886 cf = init_chunkfile(f);
1888 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
1889 write_graph_chunk_fanout);
1890 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, hashsz * ctx->commits.nr,
1891 write_graph_chunk_oids);
1892 add_chunk(cf, GRAPH_CHUNKID_DATA, (hashsz + 16) * ctx->commits.nr,
1893 write_graph_chunk_data);
1895 if (ctx->write_generation_data)
1896 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
1897 sizeof(uint32_t) * ctx->commits.nr,
1898 write_graph_chunk_generation_data);
1899 if (ctx->num_generation_data_overflows)
1900 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
1901 sizeof(timestamp_t) * ctx->num_generation_data_overflows,
1902 write_graph_chunk_generation_data_overflow);
1903 if (ctx->num_extra_edges)
1904 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
1905 4 * ctx->num_extra_edges,
1906 write_graph_chunk_extra_edges);
1907 if (ctx->changed_paths) {
1908 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
1909 sizeof(uint32_t) * ctx->commits.nr,
1910 write_graph_chunk_bloom_indexes);
1911 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
1912 sizeof(uint32_t) * 3
1913 + ctx->total_bloom_filter_data_size,
1914 write_graph_chunk_bloom_data);
1916 if (ctx->num_commit_graphs_after > 1)
1917 add_chunk(cf, GRAPH_CHUNKID_BASE,
1918 hashsz * (ctx->num_commit_graphs_after - 1),
1919 write_graph_chunk_base);
1921 hashwrite_be32(f, GRAPH_SIGNATURE);
1923 hashwrite_u8(f, GRAPH_VERSION);
1924 hashwrite_u8(f, oid_version(the_hash_algo));
1925 hashwrite_u8(f, get_num_chunks(cf));
1926 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1928 if (ctx->report_progress) {
1929 strbuf_addf(&progress_title,
1930 Q_("Writing out commit graph in %d pass",
1931 "Writing out commit graph in %d passes",
1932 get_num_chunks(cf)),
1933 get_num_chunks(cf));
1934 ctx->progress = start_delayed_progress(
1935 progress_title.buf,
1936 get_num_chunks(cf) * ctx->commits.nr);
1939 write_chunkfile(cf, ctx);
1941 stop_progress(&ctx->progress);
1942 strbuf_release(&progress_title);
1944 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1945 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1946 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
1948 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1949 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1950 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1951 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1954 close_commit_graph(ctx->r->objects);
1955 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
1956 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1957 free_chunkfile(cf);
1959 if (ctx->split) {
1960 FILE *chainf = fdopen_lock_file(&lk, "w");
1961 char *final_graph_name;
1962 int result;
1964 close(fd);
1966 if (!chainf) {
1967 error(_("unable to open commit-graph chain file"));
1968 return -1;
1971 if (ctx->base_graph_name) {
1972 const char *dest;
1973 int idx = ctx->num_commit_graphs_after - 1;
1974 if (ctx->num_commit_graphs_after > 1)
1975 idx--;
1977 dest = ctx->commit_graph_filenames_after[idx];
1979 if (strcmp(ctx->base_graph_name, dest)) {
1980 result = rename(ctx->base_graph_name, dest);
1982 if (result) {
1983 error(_("failed to rename base commit-graph file"));
1984 return -1;
1987 } else {
1988 char *graph_name = get_commit_graph_filename(ctx->odb);
1989 unlink(graph_name);
1990 free(graph_name);
1993 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
1994 final_graph_name = get_split_graph_filename(ctx->odb,
1995 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1996 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1998 result = rename(ctx->graph_name, final_graph_name);
2000 for (i = 0; i < ctx->num_commit_graphs_after; i++)
2001 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2003 if (result) {
2004 error(_("failed to rename temporary commit-graph file"));
2005 return -1;
2009 commit_lock_file(&lk);
2011 return 0;
2014 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2016 struct commit_graph *g;
2017 uint32_t num_commits;
2018 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2019 uint32_t i;
2021 int max_commits = 0;
2022 int size_mult = 2;
2024 if (ctx->opts) {
2025 max_commits = ctx->opts->max_commits;
2027 if (ctx->opts->size_multiple)
2028 size_mult = ctx->opts->size_multiple;
2030 flags = ctx->opts->split_flags;
2033 g = ctx->r->objects->commit_graph;
2034 num_commits = ctx->commits.nr;
2035 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2036 ctx->num_commit_graphs_after = 1;
2037 else
2038 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2040 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2041 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2042 while (g && (g->num_commits <= size_mult * num_commits ||
2043 (max_commits && num_commits > max_commits))) {
2044 if (g->odb != ctx->odb)
2045 break;
2047 num_commits += g->num_commits;
2048 g = g->base_graph;
2050 ctx->num_commit_graphs_after--;
2054 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2055 ctx->new_base_graph = g;
2056 else if (ctx->num_commit_graphs_after != 1)
2057 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2058 "should be 1 with --split=replace");
2060 if (ctx->num_commit_graphs_after == 2) {
2061 char *old_graph_name = get_commit_graph_filename(g->odb);
2063 if (!strcmp(g->filename, old_graph_name) &&
2064 g->odb != ctx->odb) {
2065 ctx->num_commit_graphs_after = 1;
2066 ctx->new_base_graph = NULL;
2069 free(old_graph_name);
2072 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2073 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2075 for (i = 0; i < ctx->num_commit_graphs_after &&
2076 i < ctx->num_commit_graphs_before; i++)
2077 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2079 i = ctx->num_commit_graphs_before - 1;
2080 g = ctx->r->objects->commit_graph;
2082 while (g) {
2083 if (i < ctx->num_commit_graphs_after)
2084 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2087 * If the topmost remaining layer has generation data chunk, the
2088 * resultant layer also has generation data chunk.
2090 if (i == ctx->num_commit_graphs_after - 2)
2091 ctx->write_generation_data = !!g->chunk_generation_data;
2093 i--;
2094 g = g->base_graph;
2098 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2099 struct commit_graph *g)
2101 uint32_t i;
2102 uint32_t offset = g->num_commits_in_base;
2104 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2106 for (i = 0; i < g->num_commits; i++) {
2107 struct object_id oid;
2108 struct commit *result;
2110 display_progress(ctx->progress, i + 1);
2112 load_oid_from_graph(g, i + offset, &oid);
2114 /* only add commits if they still exist in the repo */
2115 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2117 if (result) {
2118 ctx->commits.list[ctx->commits.nr] = result;
2119 ctx->commits.nr++;
2124 static int commit_compare(const void *_a, const void *_b)
2126 const struct commit *a = *(const struct commit **)_a;
2127 const struct commit *b = *(const struct commit **)_b;
2128 return oidcmp(&a->object.oid, &b->object.oid);
2131 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2133 uint32_t i, dedup_i = 0;
2135 if (ctx->report_progress)
2136 ctx->progress = start_delayed_progress(
2137 _("Scanning merged commits"),
2138 ctx->commits.nr);
2140 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2142 ctx->num_extra_edges = 0;
2143 for (i = 0; i < ctx->commits.nr; i++) {
2144 display_progress(ctx->progress, i + 1);
2146 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2147 &ctx->commits.list[i]->object.oid)) {
2149 * Silently ignore duplicates. These were likely
2150 * created due to a commit appearing in multiple
2151 * layers of the chain, which is unexpected but
2152 * not invalid. We should make sure there is a
2153 * unique copy in the new layer.
2155 } else {
2156 unsigned int num_parents;
2158 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2159 dedup_i++;
2161 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2162 if (num_parents > 2)
2163 ctx->num_extra_edges += num_parents - 1;
2167 ctx->commits.nr = dedup_i;
2169 stop_progress(&ctx->progress);
2172 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2174 struct commit_graph *g = ctx->r->objects->commit_graph;
2175 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2177 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2178 current_graph_number--;
2180 if (ctx->report_progress)
2181 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2183 merge_commit_graph(ctx, g);
2184 stop_progress(&ctx->progress);
2186 g = g->base_graph;
2189 if (g) {
2190 ctx->new_base_graph = g;
2191 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2194 if (ctx->new_base_graph)
2195 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2197 sort_and_scan_merged_commits(ctx);
2200 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2202 uint32_t i;
2203 time_t now = time(NULL);
2205 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2206 struct stat st;
2207 struct utimbuf updated_time;
2209 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2210 continue;
2212 updated_time.actime = st.st_atime;
2213 updated_time.modtime = now;
2214 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2218 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2220 struct strbuf path = STRBUF_INIT;
2221 DIR *dir;
2222 struct dirent *de;
2223 size_t dirnamelen;
2224 timestamp_t expire_time = time(NULL);
2226 if (ctx->opts && ctx->opts->expire_time)
2227 expire_time = ctx->opts->expire_time;
2228 if (!ctx->split) {
2229 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2230 unlink(chain_file_name);
2231 free(chain_file_name);
2232 ctx->num_commit_graphs_after = 0;
2235 strbuf_addstr(&path, ctx->odb->path);
2236 strbuf_addstr(&path, "/info/commit-graphs");
2237 dir = opendir(path.buf);
2239 if (!dir)
2240 goto out;
2242 strbuf_addch(&path, '/');
2243 dirnamelen = path.len;
2244 while ((de = readdir(dir)) != NULL) {
2245 struct stat st;
2246 uint32_t i, found = 0;
2248 strbuf_setlen(&path, dirnamelen);
2249 strbuf_addstr(&path, de->d_name);
2251 if (stat(path.buf, &st) < 0)
2252 continue;
2254 if (st.st_mtime > expire_time)
2255 continue;
2256 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2257 continue;
2259 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2260 if (!strcmp(ctx->commit_graph_filenames_after[i],
2261 path.buf)) {
2262 found = 1;
2263 break;
2267 if (!found)
2268 unlink(path.buf);
2271 out:
2272 if(dir)
2273 closedir(dir);
2274 strbuf_release(&path);
2277 int write_commit_graph(struct object_directory *odb,
2278 const struct string_list *const pack_indexes,
2279 struct oidset *commits,
2280 enum commit_graph_write_flags flags,
2281 const struct commit_graph_opts *opts)
2283 struct repository *r = the_repository;
2284 struct write_commit_graph_context *ctx;
2285 uint32_t i;
2286 int res = 0;
2287 int replace = 0;
2288 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2289 struct topo_level_slab topo_levels;
2291 prepare_repo_settings(r);
2292 if (!r->settings.core_commit_graph) {
2293 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2294 return 0;
2296 if (!commit_graph_compatible(r))
2297 return 0;
2299 CALLOC_ARRAY(ctx, 1);
2300 ctx->r = r;
2301 ctx->odb = odb;
2302 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2303 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2304 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2305 ctx->opts = opts;
2306 ctx->total_bloom_filter_data_size = 0;
2307 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2308 ctx->num_generation_data_overflows = 0;
2310 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2311 bloom_settings.bits_per_entry);
2312 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2313 bloom_settings.num_hashes);
2314 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2315 bloom_settings.max_changed_paths);
2316 ctx->bloom_settings = &bloom_settings;
2318 init_topo_level_slab(&topo_levels);
2319 ctx->topo_levels = &topo_levels;
2321 prepare_commit_graph(ctx->r);
2322 if (ctx->r->objects->commit_graph) {
2323 struct commit_graph *g = ctx->r->objects->commit_graph;
2325 while (g) {
2326 g->topo_levels = &topo_levels;
2327 g = g->base_graph;
2331 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2332 ctx->changed_paths = 1;
2333 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2334 struct commit_graph *g;
2336 g = ctx->r->objects->commit_graph;
2338 /* We have changed-paths already. Keep them in the next graph */
2339 if (g && g->chunk_bloom_data) {
2340 ctx->changed_paths = 1;
2341 ctx->bloom_settings = g->bloom_filter_settings;
2345 if (ctx->split) {
2346 struct commit_graph *g = ctx->r->objects->commit_graph;
2348 while (g) {
2349 ctx->num_commit_graphs_before++;
2350 g = g->base_graph;
2353 if (ctx->num_commit_graphs_before) {
2354 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2355 i = ctx->num_commit_graphs_before;
2356 g = ctx->r->objects->commit_graph;
2358 while (g) {
2359 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2360 g = g->base_graph;
2364 if (ctx->opts)
2365 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2368 ctx->approx_nr_objects = repo_approximate_object_count(the_repository);
2370 if (ctx->append && ctx->r->objects->commit_graph) {
2371 struct commit_graph *g = ctx->r->objects->commit_graph;
2372 for (i = 0; i < g->num_commits; i++) {
2373 struct object_id oid;
2374 oidread(&oid, g->chunk_oid_lookup + g->hash_len * i);
2375 oid_array_append(&ctx->oids, &oid);
2379 if (pack_indexes) {
2380 ctx->order_by_pack = 1;
2381 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2382 goto cleanup;
2385 if (commits) {
2386 if ((res = fill_oids_from_commits(ctx, commits)))
2387 goto cleanup;
2390 if (!pack_indexes && !commits) {
2391 ctx->order_by_pack = 1;
2392 fill_oids_from_all_packs(ctx);
2395 close_reachable(ctx);
2397 copy_oids_to_commits(ctx);
2399 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2400 error(_("too many commits to write graph"));
2401 res = -1;
2402 goto cleanup;
2405 if (!ctx->commits.nr && !replace)
2406 goto cleanup;
2408 if (ctx->split) {
2409 split_graph_merge_strategy(ctx);
2411 if (!replace)
2412 merge_commit_graphs(ctx);
2413 } else
2414 ctx->num_commit_graphs_after = 1;
2416 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2418 compute_topological_levels(ctx);
2419 if (ctx->write_generation_data)
2420 compute_generation_numbers(ctx);
2422 if (ctx->changed_paths)
2423 compute_bloom_filters(ctx);
2425 res = write_commit_graph_file(ctx);
2427 if (ctx->split)
2428 mark_commit_graphs(ctx);
2430 expire_commit_graphs(ctx);
2432 cleanup:
2433 free(ctx->graph_name);
2434 free(ctx->commits.list);
2435 oid_array_clear(&ctx->oids);
2436 clear_topo_level_slab(&topo_levels);
2438 if (ctx->commit_graph_filenames_after) {
2439 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2440 free(ctx->commit_graph_filenames_after[i]);
2441 free(ctx->commit_graph_hash_after[i]);
2444 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2445 free(ctx->commit_graph_filenames_before[i]);
2447 free(ctx->commit_graph_filenames_after);
2448 free(ctx->commit_graph_filenames_before);
2449 free(ctx->commit_graph_hash_after);
2452 free(ctx);
2454 return res;
2457 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2458 static int verify_commit_graph_error;
2460 __attribute__((format (printf, 1, 2)))
2461 static void graph_report(const char *fmt, ...)
2463 va_list ap;
2465 verify_commit_graph_error = 1;
2466 va_start(ap, fmt);
2467 vfprintf(stderr, fmt, ap);
2468 fprintf(stderr, "\n");
2469 va_end(ap);
2472 #define GENERATION_ZERO_EXISTS 1
2473 #define GENERATION_NUMBER_EXISTS 2
2475 static int commit_graph_checksum_valid(struct commit_graph *g)
2477 return hashfile_checksum_valid(g->data, g->data_len);
2480 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2482 uint32_t i, cur_fanout_pos = 0;
2483 struct object_id prev_oid, cur_oid;
2484 int generation_zero = 0;
2485 struct progress *progress = NULL;
2486 int local_error = 0;
2488 if (!g) {
2489 graph_report("no commit-graph file loaded");
2490 return 1;
2493 verify_commit_graph_error = verify_commit_graph_lite(g);
2494 if (verify_commit_graph_error)
2495 return verify_commit_graph_error;
2497 if (!commit_graph_checksum_valid(g)) {
2498 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2499 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2502 for (i = 0; i < g->num_commits; i++) {
2503 struct commit *graph_commit;
2505 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2507 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2508 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2509 oid_to_hex(&prev_oid),
2510 oid_to_hex(&cur_oid));
2512 oidcpy(&prev_oid, &cur_oid);
2514 while (cur_oid.hash[0] > cur_fanout_pos) {
2515 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2517 if (i != fanout_value)
2518 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2519 cur_fanout_pos, fanout_value, i);
2520 cur_fanout_pos++;
2523 graph_commit = lookup_commit(r, &cur_oid);
2524 if (!parse_commit_in_graph_one(r, g, graph_commit))
2525 graph_report(_("failed to parse commit %s from commit-graph"),
2526 oid_to_hex(&cur_oid));
2529 while (cur_fanout_pos < 256) {
2530 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2532 if (g->num_commits != fanout_value)
2533 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2534 cur_fanout_pos, fanout_value, i);
2536 cur_fanout_pos++;
2539 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2540 return verify_commit_graph_error;
2542 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2543 progress = start_progress(_("Verifying commits in commit graph"),
2544 g->num_commits);
2546 for (i = 0; i < g->num_commits; i++) {
2547 struct commit *graph_commit, *odb_commit;
2548 struct commit_list *graph_parents, *odb_parents;
2549 timestamp_t max_generation = 0;
2550 timestamp_t generation;
2552 display_progress(progress, i + 1);
2553 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2555 graph_commit = lookup_commit(r, &cur_oid);
2556 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2557 if (repo_parse_commit_internal(r, odb_commit, 0, 0)) {
2558 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2559 oid_to_hex(&cur_oid));
2560 continue;
2563 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2564 get_commit_tree_oid(odb_commit)))
2565 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2566 oid_to_hex(&cur_oid),
2567 oid_to_hex(get_commit_tree_oid(graph_commit)),
2568 oid_to_hex(get_commit_tree_oid(odb_commit)));
2570 graph_parents = graph_commit->parents;
2571 odb_parents = odb_commit->parents;
2573 while (graph_parents) {
2574 if (!odb_parents) {
2575 graph_report(_("commit-graph parent list for commit %s is too long"),
2576 oid_to_hex(&cur_oid));
2577 break;
2580 /* parse parent in case it is in a base graph */
2581 parse_commit_in_graph_one(r, g, graph_parents->item);
2583 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2584 graph_report(_("commit-graph parent for %s is %s != %s"),
2585 oid_to_hex(&cur_oid),
2586 oid_to_hex(&graph_parents->item->object.oid),
2587 oid_to_hex(&odb_parents->item->object.oid));
2589 generation = commit_graph_generation(graph_parents->item);
2590 if (generation > max_generation)
2591 max_generation = generation;
2593 graph_parents = graph_parents->next;
2594 odb_parents = odb_parents->next;
2597 if (odb_parents)
2598 graph_report(_("commit-graph parent list for commit %s terminates early"),
2599 oid_to_hex(&cur_oid));
2601 if (!commit_graph_generation(graph_commit)) {
2602 if (generation_zero == GENERATION_NUMBER_EXISTS)
2603 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2604 oid_to_hex(&cur_oid));
2605 generation_zero = GENERATION_ZERO_EXISTS;
2606 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2607 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2608 oid_to_hex(&cur_oid));
2610 if (generation_zero == GENERATION_ZERO_EXISTS)
2611 continue;
2614 * If we are using topological level and one of our parents has
2615 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2616 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2617 * in the following condition.
2619 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2620 max_generation--;
2622 generation = commit_graph_generation(graph_commit);
2623 if (generation < max_generation + 1)
2624 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2625 oid_to_hex(&cur_oid),
2626 generation,
2627 max_generation + 1);
2629 if (graph_commit->date != odb_commit->date)
2630 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2631 oid_to_hex(&cur_oid),
2632 graph_commit->date,
2633 odb_commit->date);
2635 stop_progress(&progress);
2637 local_error = verify_commit_graph_error;
2639 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2640 local_error |= verify_commit_graph(r, g->base_graph, flags);
2642 return local_error;
2645 void free_commit_graph(struct commit_graph *g)
2647 if (!g)
2648 return;
2649 if (g->data) {
2650 munmap((void *)g->data, g->data_len);
2651 g->data = NULL;
2653 free(g->filename);
2654 free(g->bloom_filter_settings);
2655 free(g);
2658 void disable_commit_graph(struct repository *r)
2660 r->commit_graph_disabled = 1;