commit-graph: close file before returning NULL
[alt-git.git] / commit-graph.c
blob06107beedcb6faf43f980f8ae3eea515a896dab4
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 uint8_t oid_version(void)
198 switch (hash_algo_by_ptr(the_hash_algo)) {
199 case GIT_HASH_SHA1:
200 return 1;
201 case GIT_HASH_SHA256:
202 return 2;
203 default:
204 die(_("invalid hash version"));
208 static struct commit_graph *alloc_commit_graph(void)
210 struct commit_graph *g = xcalloc(1, sizeof(*g));
212 return g;
215 extern int read_replace_refs;
217 static int commit_graph_compatible(struct repository *r)
219 if (!r->gitdir)
220 return 0;
222 if (read_replace_refs) {
223 prepare_replace_object(r);
224 if (hashmap_get_size(&r->objects->replace_map->map))
225 return 0;
228 prepare_commit_graft(r);
229 if (r->parsed_objects &&
230 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
231 return 0;
232 if (is_repository_shallow(r))
233 return 0;
235 return 1;
238 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
240 *fd = git_open(graph_file);
241 if (*fd < 0)
242 return 0;
243 if (fstat(*fd, st)) {
244 close(*fd);
245 return 0;
247 return 1;
250 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
251 int fd, struct stat *st,
252 struct object_directory *odb)
254 void *graph_map;
255 size_t graph_size;
256 struct commit_graph *ret;
258 graph_size = xsize_t(st->st_size);
260 if (graph_size < GRAPH_MIN_SIZE) {
261 close(fd);
262 error(_("commit-graph file is too small"));
263 return NULL;
265 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
266 close(fd);
267 ret = parse_commit_graph(r, graph_map, graph_size);
269 if (ret)
270 ret->odb = odb;
271 else
272 munmap(graph_map, graph_size);
274 return ret;
277 static int verify_commit_graph_lite(struct commit_graph *g)
280 * Basic validation shared between parse_commit_graph()
281 * which'll be called every time the graph is used, and the
282 * much more expensive verify_commit_graph() used by
283 * "commit-graph verify".
285 * There should only be very basic checks here to ensure that
286 * we don't e.g. segfault in fill_commit_in_graph(), but
287 * because this is a very hot codepath nothing that e.g. loops
288 * over g->num_commits, or runs a checksum on the commit-graph
289 * itself.
291 if (!g->chunk_oid_fanout) {
292 error("commit-graph is missing the OID Fanout chunk");
293 return 1;
295 if (!g->chunk_oid_lookup) {
296 error("commit-graph is missing the OID Lookup chunk");
297 return 1;
299 if (!g->chunk_commit_data) {
300 error("commit-graph is missing the Commit Data chunk");
301 return 1;
304 return 0;
307 static int graph_read_oid_lookup(const unsigned char *chunk_start,
308 size_t chunk_size, void *data)
310 struct commit_graph *g = data;
311 g->chunk_oid_lookup = chunk_start;
312 g->num_commits = chunk_size / g->hash_len;
313 return 0;
316 static int graph_read_bloom_data(const unsigned char *chunk_start,
317 size_t chunk_size, void *data)
319 struct commit_graph *g = data;
320 uint32_t hash_version;
321 g->chunk_bloom_data = chunk_start;
322 hash_version = get_be32(chunk_start);
324 if (hash_version != 1)
325 return 0;
327 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
328 g->bloom_filter_settings->hash_version = hash_version;
329 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
330 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
331 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
333 return 0;
336 struct commit_graph *parse_commit_graph(struct repository *r,
337 void *graph_map, size_t graph_size)
339 const unsigned char *data;
340 struct commit_graph *graph;
341 uint32_t graph_signature;
342 unsigned char graph_version, hash_version;
343 struct chunkfile *cf = NULL;
345 if (!graph_map)
346 return NULL;
348 if (graph_size < GRAPH_MIN_SIZE)
349 return NULL;
351 data = (const unsigned char *)graph_map;
353 graph_signature = get_be32(data);
354 if (graph_signature != GRAPH_SIGNATURE) {
355 error(_("commit-graph signature %X does not match signature %X"),
356 graph_signature, GRAPH_SIGNATURE);
357 return NULL;
360 graph_version = *(unsigned char*)(data + 4);
361 if (graph_version != GRAPH_VERSION) {
362 error(_("commit-graph version %X does not match version %X"),
363 graph_version, GRAPH_VERSION);
364 return NULL;
367 hash_version = *(unsigned char*)(data + 5);
368 if (hash_version != oid_version()) {
369 error(_("commit-graph hash version %X does not match version %X"),
370 hash_version, oid_version());
371 return NULL;
374 prepare_repo_settings(r);
376 graph = alloc_commit_graph();
378 graph->hash_len = the_hash_algo->rawsz;
379 graph->num_chunks = *(unsigned char*)(data + 6);
380 graph->data = graph_map;
381 graph->data_len = graph_size;
383 if (graph_size < GRAPH_HEADER_SIZE +
384 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
385 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
386 error(_("commit-graph file is too small to hold %u chunks"),
387 graph->num_chunks);
388 free(graph);
389 return NULL;
392 cf = init_chunkfile(NULL);
394 if (read_table_of_contents(cf, graph->data, graph_size,
395 GRAPH_HEADER_SIZE, graph->num_chunks))
396 goto free_and_return;
398 pair_chunk(cf, GRAPH_CHUNKID_OIDFANOUT,
399 (const unsigned char **)&graph->chunk_oid_fanout);
400 read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph);
401 pair_chunk(cf, GRAPH_CHUNKID_DATA, &graph->chunk_commit_data);
402 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges);
403 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs);
405 if (get_configured_generation_version(r) >= 2) {
406 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
407 &graph->chunk_generation_data);
408 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
409 &graph->chunk_generation_data_overflow);
411 if (graph->chunk_generation_data)
412 graph->read_generation_data = 1;
415 if (r->settings.commit_graph_read_changed_paths) {
416 pair_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
417 &graph->chunk_bloom_indexes);
418 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
419 graph_read_bloom_data, graph);
422 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
423 init_bloom_filters();
424 } else {
425 /* We need both the bloom chunks to exist together. Else ignore the data */
426 graph->chunk_bloom_indexes = NULL;
427 graph->chunk_bloom_data = NULL;
428 FREE_AND_NULL(graph->bloom_filter_settings);
431 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
433 if (verify_commit_graph_lite(graph))
434 goto free_and_return;
436 free_chunkfile(cf);
437 return graph;
439 free_and_return:
440 free_chunkfile(cf);
441 free(graph->bloom_filter_settings);
442 free(graph);
443 return NULL;
446 static struct commit_graph *load_commit_graph_one(struct repository *r,
447 const char *graph_file,
448 struct object_directory *odb)
451 struct stat st;
452 int fd;
453 struct commit_graph *g;
454 int open_ok = open_commit_graph(graph_file, &fd, &st);
456 if (!open_ok)
457 return NULL;
459 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
461 if (g)
462 g->filename = xstrdup(graph_file);
464 return g;
467 static struct commit_graph *load_commit_graph_v1(struct repository *r,
468 struct object_directory *odb)
470 char *graph_name = get_commit_graph_filename(odb);
471 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
472 free(graph_name);
474 return g;
477 static int add_graph_to_chain(struct commit_graph *g,
478 struct commit_graph *chain,
479 struct object_id *oids,
480 int n)
482 struct commit_graph *cur_g = chain;
484 if (n && !g->chunk_base_graphs) {
485 warning(_("commit-graph has no base graphs chunk"));
486 return 0;
489 while (n) {
490 n--;
492 if (!cur_g ||
493 !oideq(&oids[n], &cur_g->oid) ||
494 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
495 warning(_("commit-graph chain does not match"));
496 return 0;
499 cur_g = cur_g->base_graph;
502 g->base_graph = chain;
504 if (chain)
505 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
507 return 1;
510 static struct commit_graph *load_commit_graph_chain(struct repository *r,
511 struct object_directory *odb)
513 struct commit_graph *graph_chain = NULL;
514 struct strbuf line = STRBUF_INIT;
515 struct stat st;
516 struct object_id *oids;
517 int i = 0, valid = 1, count;
518 char *chain_name = get_commit_graph_chain_filename(odb);
519 FILE *fp;
520 int stat_res;
522 fp = fopen(chain_name, "r");
523 stat_res = stat(chain_name, &st);
524 free(chain_name);
526 if (!fp)
527 return NULL;
528 if (stat_res ||
529 st.st_size <= the_hash_algo->hexsz) {
530 fclose(fp);
531 return NULL;
534 count = st.st_size / (the_hash_algo->hexsz + 1);
535 CALLOC_ARRAY(oids, count);
537 prepare_alt_odb(r);
539 for (i = 0; i < count; i++) {
540 struct object_directory *odb;
542 if (strbuf_getline_lf(&line, fp) == EOF)
543 break;
545 if (get_oid_hex(line.buf, &oids[i])) {
546 warning(_("invalid commit-graph chain: line '%s' not a hash"),
547 line.buf);
548 valid = 0;
549 break;
552 valid = 0;
553 for (odb = r->objects->odb; odb; odb = odb->next) {
554 char *graph_name = get_split_graph_filename(odb, line.buf);
555 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
557 free(graph_name);
559 if (g) {
560 if (add_graph_to_chain(g, graph_chain, oids, i)) {
561 graph_chain = g;
562 valid = 1;
565 break;
569 if (!valid) {
570 warning(_("unable to find all commit-graph files"));
571 break;
575 free(oids);
576 fclose(fp);
577 strbuf_release(&line);
579 return graph_chain;
583 * returns 1 if and only if all graphs in the chain have
584 * corrected commit dates stored in the generation_data chunk.
586 static int validate_mixed_generation_chain(struct commit_graph *g)
588 int read_generation_data = 1;
589 struct commit_graph *p = g;
591 while (read_generation_data && p) {
592 read_generation_data = p->read_generation_data;
593 p = p->base_graph;
596 if (read_generation_data)
597 return 1;
599 while (g) {
600 g->read_generation_data = 0;
601 g = g->base_graph;
604 return 0;
607 struct commit_graph *read_commit_graph_one(struct repository *r,
608 struct object_directory *odb)
610 struct commit_graph *g = load_commit_graph_v1(r, odb);
612 if (!g)
613 g = load_commit_graph_chain(r, odb);
615 validate_mixed_generation_chain(g);
617 return g;
620 static void prepare_commit_graph_one(struct repository *r,
621 struct object_directory *odb)
624 if (r->objects->commit_graph)
625 return;
627 r->objects->commit_graph = read_commit_graph_one(r, odb);
631 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
633 * On the first invocation, this function attempts to load the commit
634 * graph if the_repository is configured to have one.
636 static int prepare_commit_graph(struct repository *r)
638 struct object_directory *odb;
641 * Early return if there is no git dir or if the commit graph is
642 * disabled.
644 * This must come before the "already attempted?" check below, because
645 * we want to disable even an already-loaded graph file.
647 if (!r->gitdir || r->commit_graph_disabled)
648 return 0;
650 if (r->objects->commit_graph_attempted)
651 return !!r->objects->commit_graph;
652 r->objects->commit_graph_attempted = 1;
654 prepare_repo_settings(r);
656 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
657 r->settings.core_commit_graph != 1)
659 * This repository is not configured to use commit graphs, so
660 * do not load one. (But report commit_graph_attempted anyway
661 * so that commit graph loading is not attempted again for this
662 * repository.)
664 return 0;
666 if (!commit_graph_compatible(r))
667 return 0;
669 prepare_alt_odb(r);
670 for (odb = r->objects->odb;
671 !r->objects->commit_graph && odb;
672 odb = odb->next)
673 prepare_commit_graph_one(r, odb);
674 return !!r->objects->commit_graph;
677 int generation_numbers_enabled(struct repository *r)
679 uint32_t first_generation;
680 struct commit_graph *g;
681 if (!prepare_commit_graph(r))
682 return 0;
684 g = r->objects->commit_graph;
686 if (!g->num_commits)
687 return 0;
689 first_generation = get_be32(g->chunk_commit_data +
690 g->hash_len + 8) >> 2;
692 return !!first_generation;
695 int corrected_commit_dates_enabled(struct repository *r)
697 struct commit_graph *g;
698 if (!prepare_commit_graph(r))
699 return 0;
701 g = r->objects->commit_graph;
703 if (!g->num_commits)
704 return 0;
706 return g->read_generation_data;
709 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
711 struct commit_graph *g = r->objects->commit_graph;
712 while (g) {
713 if (g->bloom_filter_settings)
714 return g->bloom_filter_settings;
715 g = g->base_graph;
717 return NULL;
720 static void close_commit_graph_one(struct commit_graph *g)
722 if (!g)
723 return;
725 clear_commit_graph_data_slab(&commit_graph_data_slab);
726 close_commit_graph_one(g->base_graph);
727 free_commit_graph(g);
730 void close_commit_graph(struct raw_object_store *o)
732 close_commit_graph_one(o->commit_graph);
733 o->commit_graph = NULL;
736 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
738 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
739 g->chunk_oid_lookup, g->hash_len, pos);
742 static void load_oid_from_graph(struct commit_graph *g,
743 uint32_t pos,
744 struct object_id *oid)
746 uint32_t lex_index;
748 while (g && pos < g->num_commits_in_base)
749 g = g->base_graph;
751 if (!g)
752 BUG("NULL commit-graph");
754 if (pos >= g->num_commits + g->num_commits_in_base)
755 die(_("invalid commit position. commit-graph is likely corrupt"));
757 lex_index = pos - g->num_commits_in_base;
759 oidread(oid, g->chunk_oid_lookup + g->hash_len * lex_index);
762 static struct commit_list **insert_parent_or_die(struct repository *r,
763 struct commit_graph *g,
764 uint32_t pos,
765 struct commit_list **pptr)
767 struct commit *c;
768 struct object_id oid;
770 if (pos >= g->num_commits + g->num_commits_in_base)
771 die("invalid parent position %"PRIu32, pos);
773 load_oid_from_graph(g, pos, &oid);
774 c = lookup_commit(r, &oid);
775 if (!c)
776 die(_("could not find commit %s"), oid_to_hex(&oid));
777 commit_graph_data_at(c)->graph_pos = pos;
778 return &commit_list_insert(c, pptr)->next;
781 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
783 const unsigned char *commit_data;
784 struct commit_graph_data *graph_data;
785 uint32_t lex_index, offset_pos;
786 uint64_t date_high, date_low, offset;
788 while (pos < g->num_commits_in_base)
789 g = g->base_graph;
791 if (pos >= g->num_commits + g->num_commits_in_base)
792 die(_("invalid commit position. commit-graph is likely corrupt"));
794 lex_index = pos - g->num_commits_in_base;
795 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
797 graph_data = commit_graph_data_at(item);
798 graph_data->graph_pos = pos;
800 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
801 date_low = get_be32(commit_data + g->hash_len + 12);
802 item->date = (timestamp_t)((date_high << 32) | date_low);
804 if (g->read_generation_data) {
805 offset = (timestamp_t)get_be32(g->chunk_generation_data + sizeof(uint32_t) * lex_index);
807 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
808 if (!g->chunk_generation_data_overflow)
809 die(_("commit-graph requires overflow generation data but has none"));
811 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
812 graph_data->generation = item->date + get_be64(g->chunk_generation_data_overflow + 8 * offset_pos);
813 } else
814 graph_data->generation = item->date + offset;
815 } else
816 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
818 if (g->topo_levels)
819 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
822 static inline void set_commit_tree(struct commit *c, struct tree *t)
824 c->maybe_tree = t;
827 static int fill_commit_in_graph(struct repository *r,
828 struct commit *item,
829 struct commit_graph *g, uint32_t pos)
831 uint32_t edge_value;
832 uint32_t *parent_data_ptr;
833 struct commit_list **pptr;
834 const unsigned char *commit_data;
835 uint32_t lex_index;
837 while (pos < g->num_commits_in_base)
838 g = g->base_graph;
840 fill_commit_graph_info(item, g, pos);
842 lex_index = pos - g->num_commits_in_base;
843 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
845 item->object.parsed = 1;
847 set_commit_tree(item, NULL);
849 pptr = &item->parents;
851 edge_value = get_be32(commit_data + g->hash_len);
852 if (edge_value == GRAPH_PARENT_NONE)
853 return 1;
854 pptr = insert_parent_or_die(r, g, edge_value, pptr);
856 edge_value = get_be32(commit_data + g->hash_len + 4);
857 if (edge_value == GRAPH_PARENT_NONE)
858 return 1;
859 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
860 pptr = insert_parent_or_die(r, g, edge_value, pptr);
861 return 1;
864 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
865 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
866 do {
867 edge_value = get_be32(parent_data_ptr);
868 pptr = insert_parent_or_die(r, g,
869 edge_value & GRAPH_EDGE_LAST_MASK,
870 pptr);
871 parent_data_ptr++;
872 } while (!(edge_value & GRAPH_LAST_EDGE));
874 return 1;
877 static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
879 struct commit_graph *cur_g = g;
880 uint32_t lex_index;
882 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
883 cur_g = cur_g->base_graph;
885 if (cur_g) {
886 *pos = lex_index + cur_g->num_commits_in_base;
887 return 1;
890 return 0;
893 static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
895 uint32_t graph_pos = commit_graph_position(item);
896 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
897 *pos = graph_pos;
898 return 1;
899 } else {
900 return search_commit_pos_in_graph(&item->object.oid, g, 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 (!repo->objects->commit_graph)
910 return NULL;
911 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
912 return NULL;
913 if (!repo_has_object_file(repo, id))
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 (!prepare_commit_graph(r))
962 return;
963 if (find_commit_pos_in_graph(item, r->objects->commit_graph, &pos))
964 fill_commit_graph_info(item, r->objects->commit_graph, pos);
967 static struct tree *load_tree_for_commit(struct repository *r,
968 struct commit_graph *g,
969 struct commit *c)
971 struct object_id oid;
972 const unsigned char *commit_data;
973 uint32_t graph_pos = commit_graph_position(c);
975 while (graph_pos < g->num_commits_in_base)
976 g = g->base_graph;
978 commit_data = g->chunk_commit_data +
979 GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base);
981 oidread(&oid, commit_data);
982 set_commit_tree(c, lookup_tree(r, &oid));
984 return c->maybe_tree;
987 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
988 struct commit_graph *g,
989 const struct commit *c)
991 if (c->maybe_tree)
992 return c->maybe_tree;
993 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
994 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
996 return load_tree_for_commit(r, g, (struct commit *)c);
999 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
1001 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
1004 struct packed_commit_list {
1005 struct commit **list;
1006 size_t nr;
1007 size_t alloc;
1010 struct write_commit_graph_context {
1011 struct repository *r;
1012 struct object_directory *odb;
1013 char *graph_name;
1014 struct oid_array oids;
1015 struct packed_commit_list commits;
1016 int num_extra_edges;
1017 int num_generation_data_overflows;
1018 unsigned long approx_nr_objects;
1019 struct progress *progress;
1020 int progress_done;
1021 uint64_t progress_cnt;
1023 char *base_graph_name;
1024 int num_commit_graphs_before;
1025 int num_commit_graphs_after;
1026 char **commit_graph_filenames_before;
1027 char **commit_graph_filenames_after;
1028 char **commit_graph_hash_after;
1029 uint32_t new_num_commits_in_base;
1030 struct commit_graph *new_base_graph;
1032 unsigned append:1,
1033 report_progress:1,
1034 split:1,
1035 changed_paths:1,
1036 order_by_pack:1,
1037 write_generation_data:1,
1038 trust_generation_numbers:1;
1040 struct topo_level_slab *topo_levels;
1041 const struct commit_graph_opts *opts;
1042 size_t total_bloom_filter_data_size;
1043 const struct bloom_filter_settings *bloom_settings;
1045 int count_bloom_filter_computed;
1046 int count_bloom_filter_not_computed;
1047 int count_bloom_filter_trunc_empty;
1048 int count_bloom_filter_trunc_large;
1051 static int write_graph_chunk_fanout(struct hashfile *f,
1052 void *data)
1054 struct write_commit_graph_context *ctx = data;
1055 int i, count = 0;
1056 struct commit **list = ctx->commits.list;
1059 * Write the first-level table (the list is sorted,
1060 * but we use a 256-entry lookup to be able to avoid
1061 * having to do eight extra binary search iterations).
1063 for (i = 0; i < 256; i++) {
1064 while (count < ctx->commits.nr) {
1065 if ((*list)->object.oid.hash[0] != i)
1066 break;
1067 display_progress(ctx->progress, ++ctx->progress_cnt);
1068 count++;
1069 list++;
1072 hashwrite_be32(f, count);
1075 return 0;
1078 static int write_graph_chunk_oids(struct hashfile *f,
1079 void *data)
1081 struct write_commit_graph_context *ctx = data;
1082 struct commit **list = ctx->commits.list;
1083 int count;
1084 for (count = 0; count < ctx->commits.nr; count++, list++) {
1085 display_progress(ctx->progress, ++ctx->progress_cnt);
1086 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1089 return 0;
1092 static const struct object_id *commit_to_oid(size_t index, const void *table)
1094 const struct commit * const *commits = table;
1095 return &commits[index]->object.oid;
1098 static int write_graph_chunk_data(struct hashfile *f,
1099 void *data)
1101 struct write_commit_graph_context *ctx = data;
1102 struct commit **list = ctx->commits.list;
1103 struct commit **last = ctx->commits.list + ctx->commits.nr;
1104 uint32_t num_extra_edges = 0;
1106 while (list < last) {
1107 struct commit_list *parent;
1108 struct object_id *tree;
1109 int edge_value;
1110 uint32_t packedDate[2];
1111 display_progress(ctx->progress, ++ctx->progress_cnt);
1113 if (repo_parse_commit_no_graph(ctx->r, *list))
1114 die(_("unable to parse commit %s"),
1115 oid_to_hex(&(*list)->object.oid));
1116 tree = get_commit_tree_oid(*list);
1117 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1119 parent = (*list)->parents;
1121 if (!parent)
1122 edge_value = GRAPH_PARENT_NONE;
1123 else {
1124 edge_value = oid_pos(&parent->item->object.oid,
1125 ctx->commits.list,
1126 ctx->commits.nr,
1127 commit_to_oid);
1129 if (edge_value >= 0)
1130 edge_value += ctx->new_num_commits_in_base;
1131 else if (ctx->new_base_graph) {
1132 uint32_t pos;
1133 if (find_commit_pos_in_graph(parent->item,
1134 ctx->new_base_graph,
1135 &pos))
1136 edge_value = pos;
1139 if (edge_value < 0)
1140 BUG("missing parent %s for commit %s",
1141 oid_to_hex(&parent->item->object.oid),
1142 oid_to_hex(&(*list)->object.oid));
1145 hashwrite_be32(f, edge_value);
1147 if (parent)
1148 parent = parent->next;
1150 if (!parent)
1151 edge_value = GRAPH_PARENT_NONE;
1152 else if (parent->next)
1153 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1154 else {
1155 edge_value = oid_pos(&parent->item->object.oid,
1156 ctx->commits.list,
1157 ctx->commits.nr,
1158 commit_to_oid);
1160 if (edge_value >= 0)
1161 edge_value += ctx->new_num_commits_in_base;
1162 else if (ctx->new_base_graph) {
1163 uint32_t pos;
1164 if (find_commit_pos_in_graph(parent->item,
1165 ctx->new_base_graph,
1166 &pos))
1167 edge_value = pos;
1170 if (edge_value < 0)
1171 BUG("missing parent %s for commit %s",
1172 oid_to_hex(&parent->item->object.oid),
1173 oid_to_hex(&(*list)->object.oid));
1176 hashwrite_be32(f, edge_value);
1178 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1179 do {
1180 num_extra_edges++;
1181 parent = parent->next;
1182 } while (parent);
1185 if (sizeof((*list)->date) > 4)
1186 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1187 else
1188 packedDate[0] = 0;
1190 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1192 packedDate[1] = htonl((*list)->date);
1193 hashwrite(f, packedDate, 8);
1195 list++;
1198 return 0;
1201 static int write_graph_chunk_generation_data(struct hashfile *f,
1202 void *data)
1204 struct write_commit_graph_context *ctx = data;
1205 int i, num_generation_data_overflows = 0;
1207 for (i = 0; i < ctx->commits.nr; i++) {
1208 struct commit *c = ctx->commits.list[i];
1209 timestamp_t offset;
1210 repo_parse_commit(ctx->r, c);
1211 offset = commit_graph_data_at(c)->generation - c->date;
1212 display_progress(ctx->progress, ++ctx->progress_cnt);
1214 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1215 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1216 num_generation_data_overflows++;
1219 hashwrite_be32(f, offset);
1222 return 0;
1225 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1226 void *data)
1228 struct write_commit_graph_context *ctx = data;
1229 int i;
1230 for (i = 0; i < ctx->commits.nr; i++) {
1231 struct commit *c = ctx->commits.list[i];
1232 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1233 display_progress(ctx->progress, ++ctx->progress_cnt);
1235 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1236 hashwrite_be32(f, offset >> 32);
1237 hashwrite_be32(f, (uint32_t) offset);
1241 return 0;
1244 static int write_graph_chunk_extra_edges(struct hashfile *f,
1245 void *data)
1247 struct write_commit_graph_context *ctx = data;
1248 struct commit **list = ctx->commits.list;
1249 struct commit **last = ctx->commits.list + ctx->commits.nr;
1250 struct commit_list *parent;
1252 while (list < last) {
1253 int num_parents = 0;
1255 display_progress(ctx->progress, ++ctx->progress_cnt);
1257 for (parent = (*list)->parents; num_parents < 3 && parent;
1258 parent = parent->next)
1259 num_parents++;
1261 if (num_parents <= 2) {
1262 list++;
1263 continue;
1266 /* Since num_parents > 2, this initializer is safe. */
1267 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1268 int edge_value = oid_pos(&parent->item->object.oid,
1269 ctx->commits.list,
1270 ctx->commits.nr,
1271 commit_to_oid);
1273 if (edge_value >= 0)
1274 edge_value += ctx->new_num_commits_in_base;
1275 else if (ctx->new_base_graph) {
1276 uint32_t pos;
1277 if (find_commit_pos_in_graph(parent->item,
1278 ctx->new_base_graph,
1279 &pos))
1280 edge_value = pos;
1283 if (edge_value < 0)
1284 BUG("missing parent %s for commit %s",
1285 oid_to_hex(&parent->item->object.oid),
1286 oid_to_hex(&(*list)->object.oid));
1287 else if (!parent->next)
1288 edge_value |= GRAPH_LAST_EDGE;
1290 hashwrite_be32(f, edge_value);
1293 list++;
1296 return 0;
1299 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1300 void *data)
1302 struct write_commit_graph_context *ctx = data;
1303 struct commit **list = ctx->commits.list;
1304 struct commit **last = ctx->commits.list + ctx->commits.nr;
1305 uint32_t cur_pos = 0;
1307 while (list < last) {
1308 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1309 size_t len = filter ? filter->len : 0;
1310 cur_pos += len;
1311 display_progress(ctx->progress, ++ctx->progress_cnt);
1312 hashwrite_be32(f, cur_pos);
1313 list++;
1316 return 0;
1319 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1321 struct json_writer jw = JSON_WRITER_INIT;
1323 jw_object_begin(&jw, 0);
1324 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1325 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1326 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1327 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1328 jw_end(&jw);
1330 trace2_data_json("bloom", ctx->r, "settings", &jw);
1332 jw_release(&jw);
1335 static int write_graph_chunk_bloom_data(struct hashfile *f,
1336 void *data)
1338 struct write_commit_graph_context *ctx = data;
1339 struct commit **list = ctx->commits.list;
1340 struct commit **last = ctx->commits.list + ctx->commits.nr;
1342 trace2_bloom_filter_settings(ctx);
1344 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1345 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1346 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1348 while (list < last) {
1349 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1350 size_t len = filter ? filter->len : 0;
1352 display_progress(ctx->progress, ++ctx->progress_cnt);
1353 if (len)
1354 hashwrite(f, filter->data, len * sizeof(unsigned char));
1355 list++;
1358 return 0;
1361 static int add_packed_commits(const struct object_id *oid,
1362 struct packed_git *pack,
1363 uint32_t pos,
1364 void *data)
1366 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1367 enum object_type type;
1368 off_t offset = nth_packed_object_offset(pack, pos);
1369 struct object_info oi = OBJECT_INFO_INIT;
1371 if (ctx->progress)
1372 display_progress(ctx->progress, ++ctx->progress_done);
1374 oi.typep = &type;
1375 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1376 die(_("unable to get type of object %s"), oid_to_hex(oid));
1378 if (type != OBJ_COMMIT)
1379 return 0;
1381 oid_array_append(&ctx->oids, oid);
1382 set_commit_pos(ctx->r, oid);
1384 return 0;
1387 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1389 struct commit_list *parent;
1390 for (parent = commit->parents; parent; parent = parent->next) {
1391 if (!(parent->item->object.flags & REACHABLE)) {
1392 oid_array_append(&ctx->oids, &parent->item->object.oid);
1393 parent->item->object.flags |= REACHABLE;
1398 static void close_reachable(struct write_commit_graph_context *ctx)
1400 int i;
1401 struct commit *commit;
1402 enum commit_graph_split_flags flags = ctx->opts ?
1403 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1405 if (ctx->report_progress)
1406 ctx->progress = start_delayed_progress(
1407 _("Loading known commits in commit graph"),
1408 ctx->oids.nr);
1409 for (i = 0; i < ctx->oids.nr; i++) {
1410 display_progress(ctx->progress, i + 1);
1411 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1412 if (commit)
1413 commit->object.flags |= REACHABLE;
1415 stop_progress(&ctx->progress);
1418 * As this loop runs, ctx->oids.nr may grow, but not more
1419 * than the number of missing commits in the reachable
1420 * closure.
1422 if (ctx->report_progress)
1423 ctx->progress = start_delayed_progress(
1424 _("Expanding reachable commits in commit graph"),
1426 for (i = 0; i < ctx->oids.nr; i++) {
1427 display_progress(ctx->progress, i + 1);
1428 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1430 if (!commit)
1431 continue;
1432 if (ctx->split) {
1433 if ((!repo_parse_commit(ctx->r, commit) &&
1434 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1435 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1436 add_missing_parents(ctx, commit);
1437 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1438 add_missing_parents(ctx, commit);
1440 stop_progress(&ctx->progress);
1442 if (ctx->report_progress)
1443 ctx->progress = start_delayed_progress(
1444 _("Clearing commit marks in commit graph"),
1445 ctx->oids.nr);
1446 for (i = 0; i < ctx->oids.nr; i++) {
1447 display_progress(ctx->progress, i + 1);
1448 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1450 if (commit)
1451 commit->object.flags &= ~REACHABLE;
1453 stop_progress(&ctx->progress);
1456 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1458 int i;
1459 struct commit_list *list = NULL;
1461 if (ctx->report_progress)
1462 ctx->progress = start_delayed_progress(
1463 _("Computing commit graph topological levels"),
1464 ctx->commits.nr);
1465 for (i = 0; i < ctx->commits.nr; i++) {
1466 struct commit *c = ctx->commits.list[i];
1467 uint32_t level;
1469 repo_parse_commit(ctx->r, c);
1470 level = *topo_level_slab_at(ctx->topo_levels, c);
1472 display_progress(ctx->progress, i + 1);
1473 if (level != GENERATION_NUMBER_ZERO)
1474 continue;
1476 commit_list_insert(c, &list);
1477 while (list) {
1478 struct commit *current = list->item;
1479 struct commit_list *parent;
1480 int all_parents_computed = 1;
1481 uint32_t max_level = 0;
1483 for (parent = current->parents; parent; parent = parent->next) {
1484 repo_parse_commit(ctx->r, parent->item);
1485 level = *topo_level_slab_at(ctx->topo_levels, parent->item);
1487 if (level == GENERATION_NUMBER_ZERO) {
1488 all_parents_computed = 0;
1489 commit_list_insert(parent->item, &list);
1490 break;
1493 if (level > max_level)
1494 max_level = level;
1497 if (all_parents_computed) {
1498 pop_commit(&list);
1500 if (max_level > GENERATION_NUMBER_V1_MAX - 1)
1501 max_level = GENERATION_NUMBER_V1_MAX - 1;
1502 *topo_level_slab_at(ctx->topo_levels, current) = max_level + 1;
1506 stop_progress(&ctx->progress);
1509 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1511 int i;
1512 struct commit_list *list = NULL;
1514 if (ctx->report_progress)
1515 ctx->progress = start_delayed_progress(
1516 _("Computing commit graph generation numbers"),
1517 ctx->commits.nr);
1519 if (!ctx->trust_generation_numbers) {
1520 for (i = 0; i < ctx->commits.nr; i++) {
1521 struct commit *c = ctx->commits.list[i];
1522 repo_parse_commit(ctx->r, c);
1523 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1527 for (i = 0; i < ctx->commits.nr; i++) {
1528 struct commit *c = ctx->commits.list[i];
1529 timestamp_t corrected_commit_date;
1531 repo_parse_commit(ctx->r, c);
1532 corrected_commit_date = commit_graph_data_at(c)->generation;
1534 display_progress(ctx->progress, i + 1);
1535 if (corrected_commit_date != GENERATION_NUMBER_ZERO)
1536 continue;
1538 commit_list_insert(c, &list);
1539 while (list) {
1540 struct commit *current = list->item;
1541 struct commit_list *parent;
1542 int all_parents_computed = 1;
1543 timestamp_t max_corrected_commit_date = 0;
1545 for (parent = current->parents; parent; parent = parent->next) {
1546 repo_parse_commit(ctx->r, parent->item);
1547 corrected_commit_date = commit_graph_data_at(parent->item)->generation;
1549 if (corrected_commit_date == GENERATION_NUMBER_ZERO) {
1550 all_parents_computed = 0;
1551 commit_list_insert(parent->item, &list);
1552 break;
1555 if (corrected_commit_date > max_corrected_commit_date)
1556 max_corrected_commit_date = corrected_commit_date;
1559 if (all_parents_computed) {
1560 pop_commit(&list);
1562 if (current->date && current->date > max_corrected_commit_date)
1563 max_corrected_commit_date = current->date - 1;
1564 commit_graph_data_at(current)->generation = max_corrected_commit_date + 1;
1569 for (i = 0; i < ctx->commits.nr; i++) {
1570 struct commit *c = ctx->commits.list[i];
1571 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1572 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1573 ctx->num_generation_data_overflows++;
1575 stop_progress(&ctx->progress);
1578 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1580 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1581 ctx->count_bloom_filter_computed);
1582 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1583 ctx->count_bloom_filter_not_computed);
1584 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1585 ctx->count_bloom_filter_trunc_empty);
1586 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1587 ctx->count_bloom_filter_trunc_large);
1590 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1592 int i;
1593 struct progress *progress = NULL;
1594 struct commit **sorted_commits;
1595 int max_new_filters;
1597 init_bloom_filters();
1599 if (ctx->report_progress)
1600 progress = start_delayed_progress(
1601 _("Computing commit changed paths Bloom filters"),
1602 ctx->commits.nr);
1604 ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1605 COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1607 if (ctx->order_by_pack)
1608 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1609 else
1610 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1612 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1613 ctx->opts->max_new_filters : ctx->commits.nr;
1615 for (i = 0; i < ctx->commits.nr; i++) {
1616 enum bloom_filter_computed computed = 0;
1617 struct commit *c = sorted_commits[i];
1618 struct bloom_filter *filter = get_or_compute_bloom_filter(
1619 ctx->r,
1621 ctx->count_bloom_filter_computed < max_new_filters,
1622 ctx->bloom_settings,
1623 &computed);
1624 if (computed & BLOOM_COMPUTED) {
1625 ctx->count_bloom_filter_computed++;
1626 if (computed & BLOOM_TRUNC_EMPTY)
1627 ctx->count_bloom_filter_trunc_empty++;
1628 if (computed & BLOOM_TRUNC_LARGE)
1629 ctx->count_bloom_filter_trunc_large++;
1630 } else if (computed & BLOOM_NOT_COMPUTED)
1631 ctx->count_bloom_filter_not_computed++;
1632 ctx->total_bloom_filter_data_size += filter
1633 ? sizeof(unsigned char) * filter->len : 0;
1634 display_progress(progress, i + 1);
1637 if (trace2_is_enabled())
1638 trace2_bloom_filter_write_statistics(ctx);
1640 free(sorted_commits);
1641 stop_progress(&progress);
1644 struct refs_cb_data {
1645 struct oidset *commits;
1646 struct progress *progress;
1649 static int add_ref_to_set(const char *refname,
1650 const struct object_id *oid,
1651 int flags, void *cb_data)
1653 struct object_id peeled;
1654 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1656 if (!peel_iterated_oid(oid, &peeled))
1657 oid = &peeled;
1658 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1659 oidset_insert(data->commits, oid);
1661 display_progress(data->progress, oidset_size(data->commits));
1663 return 0;
1666 int write_commit_graph_reachable(struct object_directory *odb,
1667 enum commit_graph_write_flags flags,
1668 const struct commit_graph_opts *opts)
1670 struct oidset commits = OIDSET_INIT;
1671 struct refs_cb_data data;
1672 int result;
1674 memset(&data, 0, sizeof(data));
1675 data.commits = &commits;
1676 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1677 data.progress = start_delayed_progress(
1678 _("Collecting referenced commits"), 0);
1680 for_each_ref(add_ref_to_set, &data);
1682 stop_progress(&data.progress);
1684 result = write_commit_graph(odb, NULL, &commits,
1685 flags, opts);
1687 oidset_clear(&commits);
1688 return result;
1691 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1692 const struct string_list *pack_indexes)
1694 uint32_t i;
1695 struct strbuf progress_title = STRBUF_INIT;
1696 struct strbuf packname = STRBUF_INIT;
1697 int dirlen;
1698 int ret = 0;
1700 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1701 dirlen = packname.len;
1702 if (ctx->report_progress) {
1703 strbuf_addf(&progress_title,
1704 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1705 "Finding commits for commit graph in %"PRIuMAX" packs",
1706 pack_indexes->nr),
1707 (uintmax_t)pack_indexes->nr);
1708 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1709 ctx->progress_done = 0;
1711 for (i = 0; i < pack_indexes->nr; i++) {
1712 struct packed_git *p;
1713 strbuf_setlen(&packname, dirlen);
1714 strbuf_addstr(&packname, pack_indexes->items[i].string);
1715 p = add_packed_git(packname.buf, packname.len, 1);
1716 if (!p) {
1717 ret = error(_("error adding pack %s"), packname.buf);
1718 goto cleanup;
1720 if (open_pack_index(p)) {
1721 ret = error(_("error opening index for %s"), packname.buf);
1722 goto cleanup;
1724 for_each_object_in_pack(p, add_packed_commits, ctx,
1725 FOR_EACH_OBJECT_PACK_ORDER);
1726 close_pack(p);
1727 free(p);
1730 cleanup:
1731 stop_progress(&ctx->progress);
1732 strbuf_release(&progress_title);
1733 strbuf_release(&packname);
1735 return ret;
1738 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1739 struct oidset *commits)
1741 struct oidset_iter iter;
1742 struct object_id *oid;
1744 if (!oidset_size(commits))
1745 return 0;
1747 oidset_iter_init(commits, &iter);
1748 while ((oid = oidset_iter_next(&iter))) {
1749 oid_array_append(&ctx->oids, oid);
1752 return 0;
1755 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1757 if (ctx->report_progress)
1758 ctx->progress = start_delayed_progress(
1759 _("Finding commits for commit graph among packed objects"),
1760 ctx->approx_nr_objects);
1761 for_each_packed_object(add_packed_commits, ctx,
1762 FOR_EACH_OBJECT_PACK_ORDER);
1763 if (ctx->progress_done < ctx->approx_nr_objects)
1764 display_progress(ctx->progress, ctx->approx_nr_objects);
1765 stop_progress(&ctx->progress);
1768 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1770 uint32_t i;
1771 enum commit_graph_split_flags flags = ctx->opts ?
1772 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1774 ctx->num_extra_edges = 0;
1775 if (ctx->report_progress)
1776 ctx->progress = start_delayed_progress(
1777 _("Finding extra edges in commit graph"),
1778 ctx->oids.nr);
1779 oid_array_sort(&ctx->oids);
1780 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1781 unsigned int num_parents;
1783 display_progress(ctx->progress, i + 1);
1785 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1786 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1788 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1789 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1790 continue;
1792 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1793 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1794 else
1795 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1797 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1798 if (num_parents > 2)
1799 ctx->num_extra_edges += num_parents - 1;
1801 ctx->commits.nr++;
1803 stop_progress(&ctx->progress);
1806 static int write_graph_chunk_base_1(struct hashfile *f,
1807 struct commit_graph *g)
1809 int num = 0;
1811 if (!g)
1812 return 0;
1814 num = write_graph_chunk_base_1(f, g->base_graph);
1815 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1816 return num + 1;
1819 static int write_graph_chunk_base(struct hashfile *f,
1820 void *data)
1822 struct write_commit_graph_context *ctx = data;
1823 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1825 if (num != ctx->num_commit_graphs_after - 1) {
1826 error(_("failed to write correct number of base graph ids"));
1827 return -1;
1830 return 0;
1833 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1835 uint32_t i;
1836 int fd;
1837 struct hashfile *f;
1838 struct lock_file lk = LOCK_INIT;
1839 const unsigned hashsz = the_hash_algo->rawsz;
1840 struct strbuf progress_title = STRBUF_INIT;
1841 struct chunkfile *cf;
1842 unsigned char file_hash[GIT_MAX_RAWSZ];
1844 if (ctx->split) {
1845 struct strbuf tmp_file = STRBUF_INIT;
1847 strbuf_addf(&tmp_file,
1848 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1849 ctx->odb->path);
1850 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1851 } else {
1852 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1855 if (safe_create_leading_directories(ctx->graph_name)) {
1856 UNLEAK(ctx->graph_name);
1857 error(_("unable to create leading directories of %s"),
1858 ctx->graph_name);
1859 return -1;
1862 if (ctx->split) {
1863 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
1865 hold_lock_file_for_update_mode(&lk, lock_name,
1866 LOCK_DIE_ON_ERROR, 0444);
1867 free(lock_name);
1869 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1870 if (fd < 0) {
1871 error(_("unable to create temporary graph layer"));
1872 return -1;
1875 if (adjust_shared_perm(ctx->graph_name)) {
1876 error(_("unable to adjust shared permissions for '%s'"),
1877 ctx->graph_name);
1878 return -1;
1881 f = hashfd(fd, ctx->graph_name);
1882 } else {
1883 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1884 LOCK_DIE_ON_ERROR, 0444);
1885 fd = get_lock_file_fd(&lk);
1886 f = hashfd(fd, get_lock_file_path(&lk));
1889 cf = init_chunkfile(f);
1891 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
1892 write_graph_chunk_fanout);
1893 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, hashsz * ctx->commits.nr,
1894 write_graph_chunk_oids);
1895 add_chunk(cf, GRAPH_CHUNKID_DATA, (hashsz + 16) * ctx->commits.nr,
1896 write_graph_chunk_data);
1898 if (ctx->write_generation_data)
1899 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
1900 sizeof(uint32_t) * ctx->commits.nr,
1901 write_graph_chunk_generation_data);
1902 if (ctx->num_generation_data_overflows)
1903 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
1904 sizeof(timestamp_t) * ctx->num_generation_data_overflows,
1905 write_graph_chunk_generation_data_overflow);
1906 if (ctx->num_extra_edges)
1907 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
1908 4 * ctx->num_extra_edges,
1909 write_graph_chunk_extra_edges);
1910 if (ctx->changed_paths) {
1911 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
1912 sizeof(uint32_t) * ctx->commits.nr,
1913 write_graph_chunk_bloom_indexes);
1914 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
1915 sizeof(uint32_t) * 3
1916 + ctx->total_bloom_filter_data_size,
1917 write_graph_chunk_bloom_data);
1919 if (ctx->num_commit_graphs_after > 1)
1920 add_chunk(cf, GRAPH_CHUNKID_BASE,
1921 hashsz * (ctx->num_commit_graphs_after - 1),
1922 write_graph_chunk_base);
1924 hashwrite_be32(f, GRAPH_SIGNATURE);
1926 hashwrite_u8(f, GRAPH_VERSION);
1927 hashwrite_u8(f, oid_version());
1928 hashwrite_u8(f, get_num_chunks(cf));
1929 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1931 if (ctx->report_progress) {
1932 strbuf_addf(&progress_title,
1933 Q_("Writing out commit graph in %d pass",
1934 "Writing out commit graph in %d passes",
1935 get_num_chunks(cf)),
1936 get_num_chunks(cf));
1937 ctx->progress = start_delayed_progress(
1938 progress_title.buf,
1939 get_num_chunks(cf) * ctx->commits.nr);
1942 write_chunkfile(cf, ctx);
1944 stop_progress(&ctx->progress);
1945 strbuf_release(&progress_title);
1947 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1948 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1949 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
1951 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1952 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1953 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1954 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1957 close_commit_graph(ctx->r->objects);
1958 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
1959 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1960 free_chunkfile(cf);
1962 if (ctx->split) {
1963 FILE *chainf = fdopen_lock_file(&lk, "w");
1964 char *final_graph_name;
1965 int result;
1967 close(fd);
1969 if (!chainf) {
1970 error(_("unable to open commit-graph chain file"));
1971 return -1;
1974 if (ctx->base_graph_name) {
1975 const char *dest;
1976 int idx = ctx->num_commit_graphs_after - 1;
1977 if (ctx->num_commit_graphs_after > 1)
1978 idx--;
1980 dest = ctx->commit_graph_filenames_after[idx];
1982 if (strcmp(ctx->base_graph_name, dest)) {
1983 result = rename(ctx->base_graph_name, dest);
1985 if (result) {
1986 error(_("failed to rename base commit-graph file"));
1987 return -1;
1990 } else {
1991 char *graph_name = get_commit_graph_filename(ctx->odb);
1992 unlink(graph_name);
1993 free(graph_name);
1996 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
1997 final_graph_name = get_split_graph_filename(ctx->odb,
1998 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1999 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
2001 result = rename(ctx->graph_name, final_graph_name);
2003 for (i = 0; i < ctx->num_commit_graphs_after; i++)
2004 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2006 if (result) {
2007 error(_("failed to rename temporary commit-graph file"));
2008 return -1;
2012 commit_lock_file(&lk);
2014 return 0;
2017 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2019 struct commit_graph *g;
2020 uint32_t num_commits;
2021 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2022 uint32_t i;
2024 int max_commits = 0;
2025 int size_mult = 2;
2027 if (ctx->opts) {
2028 max_commits = ctx->opts->max_commits;
2030 if (ctx->opts->size_multiple)
2031 size_mult = ctx->opts->size_multiple;
2033 flags = ctx->opts->split_flags;
2036 g = ctx->r->objects->commit_graph;
2037 num_commits = ctx->commits.nr;
2038 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2039 ctx->num_commit_graphs_after = 1;
2040 else
2041 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2043 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2044 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2045 while (g && (g->num_commits <= size_mult * num_commits ||
2046 (max_commits && num_commits > max_commits))) {
2047 if (g->odb != ctx->odb)
2048 break;
2050 num_commits += g->num_commits;
2051 g = g->base_graph;
2053 ctx->num_commit_graphs_after--;
2057 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2058 ctx->new_base_graph = g;
2059 else if (ctx->num_commit_graphs_after != 1)
2060 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2061 "should be 1 with --split=replace");
2063 if (ctx->num_commit_graphs_after == 2) {
2064 char *old_graph_name = get_commit_graph_filename(g->odb);
2066 if (!strcmp(g->filename, old_graph_name) &&
2067 g->odb != ctx->odb) {
2068 ctx->num_commit_graphs_after = 1;
2069 ctx->new_base_graph = NULL;
2072 free(old_graph_name);
2075 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2076 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2078 for (i = 0; i < ctx->num_commit_graphs_after &&
2079 i < ctx->num_commit_graphs_before; i++)
2080 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2082 i = ctx->num_commit_graphs_before - 1;
2083 g = ctx->r->objects->commit_graph;
2085 while (g) {
2086 if (i < ctx->num_commit_graphs_after)
2087 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2090 * If the topmost remaining layer has generation data chunk, the
2091 * resultant layer also has generation data chunk.
2093 if (i == ctx->num_commit_graphs_after - 2)
2094 ctx->write_generation_data = !!g->chunk_generation_data;
2096 i--;
2097 g = g->base_graph;
2101 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2102 struct commit_graph *g)
2104 uint32_t i;
2105 uint32_t offset = g->num_commits_in_base;
2107 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2109 for (i = 0; i < g->num_commits; i++) {
2110 struct object_id oid;
2111 struct commit *result;
2113 display_progress(ctx->progress, i + 1);
2115 load_oid_from_graph(g, i + offset, &oid);
2117 /* only add commits if they still exist in the repo */
2118 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2120 if (result) {
2121 ctx->commits.list[ctx->commits.nr] = result;
2122 ctx->commits.nr++;
2127 static int commit_compare(const void *_a, const void *_b)
2129 const struct commit *a = *(const struct commit **)_a;
2130 const struct commit *b = *(const struct commit **)_b;
2131 return oidcmp(&a->object.oid, &b->object.oid);
2134 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2136 uint32_t i, dedup_i = 0;
2138 if (ctx->report_progress)
2139 ctx->progress = start_delayed_progress(
2140 _("Scanning merged commits"),
2141 ctx->commits.nr);
2143 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2145 ctx->num_extra_edges = 0;
2146 for (i = 0; i < ctx->commits.nr; i++) {
2147 display_progress(ctx->progress, i + 1);
2149 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2150 &ctx->commits.list[i]->object.oid)) {
2152 * Silently ignore duplicates. These were likely
2153 * created due to a commit appearing in multiple
2154 * layers of the chain, which is unexpected but
2155 * not invalid. We should make sure there is a
2156 * unique copy in the new layer.
2158 } else {
2159 unsigned int num_parents;
2161 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2162 dedup_i++;
2164 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2165 if (num_parents > 2)
2166 ctx->num_extra_edges += num_parents - 1;
2170 ctx->commits.nr = dedup_i;
2172 stop_progress(&ctx->progress);
2175 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2177 struct commit_graph *g = ctx->r->objects->commit_graph;
2178 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2180 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2181 current_graph_number--;
2183 if (ctx->report_progress)
2184 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2186 merge_commit_graph(ctx, g);
2187 stop_progress(&ctx->progress);
2189 g = g->base_graph;
2192 if (g) {
2193 ctx->new_base_graph = g;
2194 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2197 if (ctx->new_base_graph)
2198 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2200 sort_and_scan_merged_commits(ctx);
2203 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2205 uint32_t i;
2206 time_t now = time(NULL);
2208 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2209 struct stat st;
2210 struct utimbuf updated_time;
2212 stat(ctx->commit_graph_filenames_before[i], &st);
2214 updated_time.actime = st.st_atime;
2215 updated_time.modtime = now;
2216 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2220 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2222 struct strbuf path = STRBUF_INIT;
2223 DIR *dir;
2224 struct dirent *de;
2225 size_t dirnamelen;
2226 timestamp_t expire_time = time(NULL);
2228 if (ctx->opts && ctx->opts->expire_time)
2229 expire_time = ctx->opts->expire_time;
2230 if (!ctx->split) {
2231 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2232 unlink(chain_file_name);
2233 free(chain_file_name);
2234 ctx->num_commit_graphs_after = 0;
2237 strbuf_addstr(&path, ctx->odb->path);
2238 strbuf_addstr(&path, "/info/commit-graphs");
2239 dir = opendir(path.buf);
2241 if (!dir)
2242 goto out;
2244 strbuf_addch(&path, '/');
2245 dirnamelen = path.len;
2246 while ((de = readdir(dir)) != NULL) {
2247 struct stat st;
2248 uint32_t i, found = 0;
2250 strbuf_setlen(&path, dirnamelen);
2251 strbuf_addstr(&path, de->d_name);
2253 stat(path.buf, &st);
2255 if (st.st_mtime > expire_time)
2256 continue;
2257 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2258 continue;
2260 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2261 if (!strcmp(ctx->commit_graph_filenames_after[i],
2262 path.buf)) {
2263 found = 1;
2264 break;
2268 if (!found)
2269 unlink(path.buf);
2272 out:
2273 strbuf_release(&path);
2276 int write_commit_graph(struct object_directory *odb,
2277 const struct string_list *const pack_indexes,
2278 struct oidset *commits,
2279 enum commit_graph_write_flags flags,
2280 const struct commit_graph_opts *opts)
2282 struct repository *r = the_repository;
2283 struct write_commit_graph_context *ctx;
2284 uint32_t i;
2285 int res = 0;
2286 int replace = 0;
2287 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2288 struct topo_level_slab topo_levels;
2290 prepare_repo_settings(r);
2291 if (!r->settings.core_commit_graph) {
2292 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2293 return 0;
2295 if (!commit_graph_compatible(r))
2296 return 0;
2298 CALLOC_ARRAY(ctx, 1);
2299 ctx->r = r;
2300 ctx->odb = odb;
2301 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2302 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2303 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2304 ctx->opts = opts;
2305 ctx->total_bloom_filter_data_size = 0;
2306 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2307 ctx->num_generation_data_overflows = 0;
2309 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2310 bloom_settings.bits_per_entry);
2311 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2312 bloom_settings.num_hashes);
2313 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2314 bloom_settings.max_changed_paths);
2315 ctx->bloom_settings = &bloom_settings;
2317 init_topo_level_slab(&topo_levels);
2318 ctx->topo_levels = &topo_levels;
2320 prepare_commit_graph(ctx->r);
2321 if (ctx->r->objects->commit_graph) {
2322 struct commit_graph *g = ctx->r->objects->commit_graph;
2324 while (g) {
2325 g->topo_levels = &topo_levels;
2326 g = g->base_graph;
2330 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2331 ctx->changed_paths = 1;
2332 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2333 struct commit_graph *g;
2335 g = ctx->r->objects->commit_graph;
2337 /* We have changed-paths already. Keep them in the next graph */
2338 if (g && g->chunk_bloom_data) {
2339 ctx->changed_paths = 1;
2340 ctx->bloom_settings = g->bloom_filter_settings;
2344 if (ctx->split) {
2345 struct commit_graph *g = ctx->r->objects->commit_graph;
2347 while (g) {
2348 ctx->num_commit_graphs_before++;
2349 g = g->base_graph;
2352 if (ctx->num_commit_graphs_before) {
2353 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2354 i = ctx->num_commit_graphs_before;
2355 g = ctx->r->objects->commit_graph;
2357 while (g) {
2358 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2359 g = g->base_graph;
2363 if (ctx->opts)
2364 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2367 ctx->approx_nr_objects = approximate_object_count();
2369 if (ctx->append && ctx->r->objects->commit_graph) {
2370 struct commit_graph *g = ctx->r->objects->commit_graph;
2371 for (i = 0; i < g->num_commits; i++) {
2372 struct object_id oid;
2373 oidread(&oid, g->chunk_oid_lookup + g->hash_len * i);
2374 oid_array_append(&ctx->oids, &oid);
2378 if (pack_indexes) {
2379 ctx->order_by_pack = 1;
2380 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2381 goto cleanup;
2384 if (commits) {
2385 if ((res = fill_oids_from_commits(ctx, commits)))
2386 goto cleanup;
2389 if (!pack_indexes && !commits) {
2390 ctx->order_by_pack = 1;
2391 fill_oids_from_all_packs(ctx);
2394 close_reachable(ctx);
2396 copy_oids_to_commits(ctx);
2398 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2399 error(_("too many commits to write graph"));
2400 res = -1;
2401 goto cleanup;
2404 if (!ctx->commits.nr && !replace)
2405 goto cleanup;
2407 if (ctx->split) {
2408 split_graph_merge_strategy(ctx);
2410 if (!replace)
2411 merge_commit_graphs(ctx);
2412 } else
2413 ctx->num_commit_graphs_after = 1;
2415 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2417 compute_topological_levels(ctx);
2418 if (ctx->write_generation_data)
2419 compute_generation_numbers(ctx);
2421 if (ctx->changed_paths)
2422 compute_bloom_filters(ctx);
2424 res = write_commit_graph_file(ctx);
2426 if (ctx->split)
2427 mark_commit_graphs(ctx);
2429 expire_commit_graphs(ctx);
2431 cleanup:
2432 free(ctx->graph_name);
2433 free(ctx->commits.list);
2434 oid_array_clear(&ctx->oids);
2435 clear_topo_level_slab(&topo_levels);
2437 if (ctx->commit_graph_filenames_after) {
2438 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2439 free(ctx->commit_graph_filenames_after[i]);
2440 free(ctx->commit_graph_hash_after[i]);
2443 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2444 free(ctx->commit_graph_filenames_before[i]);
2446 free(ctx->commit_graph_filenames_after);
2447 free(ctx->commit_graph_filenames_before);
2448 free(ctx->commit_graph_hash_after);
2451 free(ctx);
2453 return res;
2456 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2457 static int verify_commit_graph_error;
2459 __attribute__((format (printf, 1, 2)))
2460 static void graph_report(const char *fmt, ...)
2462 va_list ap;
2464 verify_commit_graph_error = 1;
2465 va_start(ap, fmt);
2466 vfprintf(stderr, fmt, ap);
2467 fprintf(stderr, "\n");
2468 va_end(ap);
2471 #define GENERATION_ZERO_EXISTS 1
2472 #define GENERATION_NUMBER_EXISTS 2
2474 static int commit_graph_checksum_valid(struct commit_graph *g)
2476 return hashfile_checksum_valid(g->data, g->data_len);
2479 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2481 uint32_t i, cur_fanout_pos = 0;
2482 struct object_id prev_oid, cur_oid;
2483 int generation_zero = 0;
2484 struct progress *progress = NULL;
2485 int local_error = 0;
2487 if (!g) {
2488 graph_report("no commit-graph file loaded");
2489 return 1;
2492 verify_commit_graph_error = verify_commit_graph_lite(g);
2493 if (verify_commit_graph_error)
2494 return verify_commit_graph_error;
2496 if (!commit_graph_checksum_valid(g)) {
2497 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2498 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2501 for (i = 0; i < g->num_commits; i++) {
2502 struct commit *graph_commit;
2504 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2506 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2507 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2508 oid_to_hex(&prev_oid),
2509 oid_to_hex(&cur_oid));
2511 oidcpy(&prev_oid, &cur_oid);
2513 while (cur_oid.hash[0] > cur_fanout_pos) {
2514 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2516 if (i != fanout_value)
2517 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2518 cur_fanout_pos, fanout_value, i);
2519 cur_fanout_pos++;
2522 graph_commit = lookup_commit(r, &cur_oid);
2523 if (!parse_commit_in_graph_one(r, g, graph_commit))
2524 graph_report(_("failed to parse commit %s from commit-graph"),
2525 oid_to_hex(&cur_oid));
2528 while (cur_fanout_pos < 256) {
2529 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2531 if (g->num_commits != fanout_value)
2532 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2533 cur_fanout_pos, fanout_value, i);
2535 cur_fanout_pos++;
2538 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2539 return verify_commit_graph_error;
2541 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2542 progress = start_progress(_("Verifying commits in commit graph"),
2543 g->num_commits);
2545 for (i = 0; i < g->num_commits; i++) {
2546 struct commit *graph_commit, *odb_commit;
2547 struct commit_list *graph_parents, *odb_parents;
2548 timestamp_t max_generation = 0;
2549 timestamp_t generation;
2551 display_progress(progress, i + 1);
2552 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2554 graph_commit = lookup_commit(r, &cur_oid);
2555 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2556 if (parse_commit_internal(odb_commit, 0, 0)) {
2557 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2558 oid_to_hex(&cur_oid));
2559 continue;
2562 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2563 get_commit_tree_oid(odb_commit)))
2564 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2565 oid_to_hex(&cur_oid),
2566 oid_to_hex(get_commit_tree_oid(graph_commit)),
2567 oid_to_hex(get_commit_tree_oid(odb_commit)));
2569 graph_parents = graph_commit->parents;
2570 odb_parents = odb_commit->parents;
2572 while (graph_parents) {
2573 if (odb_parents == NULL) {
2574 graph_report(_("commit-graph parent list for commit %s is too long"),
2575 oid_to_hex(&cur_oid));
2576 break;
2579 /* parse parent in case it is in a base graph */
2580 parse_commit_in_graph_one(r, g, graph_parents->item);
2582 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2583 graph_report(_("commit-graph parent for %s is %s != %s"),
2584 oid_to_hex(&cur_oid),
2585 oid_to_hex(&graph_parents->item->object.oid),
2586 oid_to_hex(&odb_parents->item->object.oid));
2588 generation = commit_graph_generation(graph_parents->item);
2589 if (generation > max_generation)
2590 max_generation = generation;
2592 graph_parents = graph_parents->next;
2593 odb_parents = odb_parents->next;
2596 if (odb_parents != NULL)
2597 graph_report(_("commit-graph parent list for commit %s terminates early"),
2598 oid_to_hex(&cur_oid));
2600 if (!commit_graph_generation(graph_commit)) {
2601 if (generation_zero == GENERATION_NUMBER_EXISTS)
2602 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2603 oid_to_hex(&cur_oid));
2604 generation_zero = GENERATION_ZERO_EXISTS;
2605 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2606 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2607 oid_to_hex(&cur_oid));
2609 if (generation_zero == GENERATION_ZERO_EXISTS)
2610 continue;
2613 * If we are using topological level and one of our parents has
2614 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2615 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2616 * in the following condition.
2618 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2619 max_generation--;
2621 generation = commit_graph_generation(graph_commit);
2622 if (generation < max_generation + 1)
2623 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2624 oid_to_hex(&cur_oid),
2625 generation,
2626 max_generation + 1);
2628 if (graph_commit->date != odb_commit->date)
2629 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2630 oid_to_hex(&cur_oid),
2631 graph_commit->date,
2632 odb_commit->date);
2634 stop_progress(&progress);
2636 local_error = verify_commit_graph_error;
2638 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2639 local_error |= verify_commit_graph(r, g->base_graph, flags);
2641 return local_error;
2644 void free_commit_graph(struct commit_graph *g)
2646 if (!g)
2647 return;
2648 if (g->data) {
2649 munmap((void *)g->data, g->data_len);
2650 g->data = NULL;
2652 free(g->filename);
2653 free(g->bloom_filter_settings);
2654 free(g);
2657 void disable_commit_graph(struct repository *r)
2659 r->commit_graph_disabled = 1;