scalar-diagnose: use "$GIT_UNZIP" in test
[git/gitster.git] / commit-graph.c
blobcc3c96659688a671ec7955cbc458f1c15970bd8f
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "lockfile.h"
4 #include "pack.h"
5 #include "packfile.h"
6 #include "commit.h"
7 #include "object.h"
8 #include "refs.h"
9 #include "revision.h"
10 #include "hash-lookup.h"
11 #include "commit-graph.h"
12 #include "object-store.h"
13 #include "alloc.h"
14 #include "hashmap.h"
15 #include "replace-object.h"
16 #include "progress.h"
17 #include "bloom.h"
18 #include "commit-slab.h"
19 #include "shallow.h"
20 #include "json-writer.h"
21 #include "trace2.h"
22 #include "chunk-format.h"
24 void git_test_write_commit_graph_or_die(void)
26 int flags = 0;
27 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
28 return;
30 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
31 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
33 if (write_commit_graph_reachable(the_repository->objects->odb,
34 flags, NULL))
35 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
38 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
39 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
40 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
41 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
42 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
43 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
44 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
45 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
46 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
47 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
49 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
51 #define GRAPH_VERSION_1 0x1
52 #define GRAPH_VERSION GRAPH_VERSION_1
54 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
55 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
56 #define GRAPH_PARENT_NONE 0x70000000
58 #define GRAPH_LAST_EDGE 0x80000000
60 #define GRAPH_HEADER_SIZE 8
61 #define GRAPH_FANOUT_SIZE (4 * 256)
62 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
63 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
65 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
67 /* Remember to update object flag allocation in object.h */
68 #define REACHABLE (1u<<15)
70 define_commit_slab(topo_level_slab, uint32_t);
72 /* Keep track of the order in which commits are added to our list. */
73 define_commit_slab(commit_pos, int);
74 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
76 static void set_commit_pos(struct repository *r, const struct object_id *oid)
78 static int32_t max_pos;
79 struct commit *commit = lookup_commit(r, oid);
81 if (!commit)
82 return; /* should never happen, but be lenient */
84 *commit_pos_at(&commit_pos, commit) = max_pos++;
87 static int commit_pos_cmp(const void *va, const void *vb)
89 const struct commit *a = *(const struct commit **)va;
90 const struct commit *b = *(const struct commit **)vb;
91 return commit_pos_at(&commit_pos, a) -
92 commit_pos_at(&commit_pos, b);
95 define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
96 static struct commit_graph_data_slab commit_graph_data_slab =
97 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
99 static int get_configured_generation_version(struct repository *r)
101 int version = 2;
102 repo_config_get_int(r, "commitgraph.generationversion", &version);
103 return version;
106 uint32_t commit_graph_position(const struct commit *c)
108 struct commit_graph_data *data =
109 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
111 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
114 timestamp_t commit_graph_generation(const struct commit *c)
116 struct commit_graph_data *data =
117 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
119 if (!data)
120 return GENERATION_NUMBER_INFINITY;
121 else if (data->graph_pos == COMMIT_NOT_FROM_GRAPH)
122 return GENERATION_NUMBER_INFINITY;
124 return data->generation;
127 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
129 unsigned int i, nth_slab;
130 struct commit_graph_data *data =
131 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
133 if (data)
134 return data;
136 nth_slab = c->index / commit_graph_data_slab.slab_size;
137 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
140 * commit-slab initializes elements with zero, overwrite this with
141 * COMMIT_NOT_FROM_GRAPH for graph_pos.
143 * We avoid initializing generation with checking if graph position
144 * is not COMMIT_NOT_FROM_GRAPH.
146 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
147 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
148 COMMIT_NOT_FROM_GRAPH;
151 return data;
155 * Should be used only while writing commit-graph as it compares
156 * generation value of commits by directly accessing commit-slab.
158 static int commit_gen_cmp(const void *va, const void *vb)
160 const struct commit *a = *(const struct commit **)va;
161 const struct commit *b = *(const struct commit **)vb;
163 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
164 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
165 /* lower generation commits first */
166 if (generation_a < generation_b)
167 return -1;
168 else if (generation_a > generation_b)
169 return 1;
171 /* use date as a heuristic when generations are equal */
172 if (a->date < b->date)
173 return -1;
174 else if (a->date > b->date)
175 return 1;
176 return 0;
179 char *get_commit_graph_filename(struct object_directory *obj_dir)
181 return xstrfmt("%s/info/commit-graph", obj_dir->path);
184 static char *get_split_graph_filename(struct object_directory *odb,
185 const char *oid_hex)
187 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
188 oid_hex);
191 char *get_commit_graph_chain_filename(struct object_directory *odb)
193 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
196 static struct commit_graph *alloc_commit_graph(void)
198 struct commit_graph *g = xcalloc(1, sizeof(*g));
200 return g;
203 extern int read_replace_refs;
205 static int commit_graph_compatible(struct repository *r)
207 if (!r->gitdir)
208 return 0;
210 if (read_replace_refs) {
211 prepare_replace_object(r);
212 if (hashmap_get_size(&r->objects->replace_map->map))
213 return 0;
216 prepare_commit_graft(r);
217 if (r->parsed_objects &&
218 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
219 return 0;
220 if (is_repository_shallow(r))
221 return 0;
223 return 1;
226 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
228 *fd = git_open(graph_file);
229 if (*fd < 0)
230 return 0;
231 if (fstat(*fd, st)) {
232 close(*fd);
233 return 0;
235 return 1;
238 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
239 int fd, struct stat *st,
240 struct object_directory *odb)
242 void *graph_map;
243 size_t graph_size;
244 struct commit_graph *ret;
246 graph_size = xsize_t(st->st_size);
248 if (graph_size < GRAPH_MIN_SIZE) {
249 close(fd);
250 error(_("commit-graph file is too small"));
251 return NULL;
253 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
254 close(fd);
255 prepare_repo_settings(r);
256 ret = parse_commit_graph(&r->settings, graph_map, graph_size);
258 if (ret)
259 ret->odb = odb;
260 else
261 munmap(graph_map, graph_size);
263 return ret;
266 static int verify_commit_graph_lite(struct commit_graph *g)
269 * Basic validation shared between parse_commit_graph()
270 * which'll be called every time the graph is used, and the
271 * much more expensive verify_commit_graph() used by
272 * "commit-graph verify".
274 * There should only be very basic checks here to ensure that
275 * we don't e.g. segfault in fill_commit_in_graph(), but
276 * because this is a very hot codepath nothing that e.g. loops
277 * over g->num_commits, or runs a checksum on the commit-graph
278 * itself.
280 if (!g->chunk_oid_fanout) {
281 error("commit-graph is missing the OID Fanout chunk");
282 return 1;
284 if (!g->chunk_oid_lookup) {
285 error("commit-graph is missing the OID Lookup chunk");
286 return 1;
288 if (!g->chunk_commit_data) {
289 error("commit-graph is missing the Commit Data chunk");
290 return 1;
293 return 0;
296 static int graph_read_oid_lookup(const unsigned char *chunk_start,
297 size_t chunk_size, void *data)
299 struct commit_graph *g = data;
300 g->chunk_oid_lookup = chunk_start;
301 g->num_commits = chunk_size / g->hash_len;
302 return 0;
305 static int graph_read_bloom_data(const unsigned char *chunk_start,
306 size_t chunk_size, void *data)
308 struct commit_graph *g = data;
309 uint32_t hash_version;
310 g->chunk_bloom_data = chunk_start;
311 hash_version = get_be32(chunk_start);
313 if (hash_version != 1)
314 return 0;
316 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
317 g->bloom_filter_settings->hash_version = hash_version;
318 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
319 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
320 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
322 return 0;
325 struct commit_graph *parse_commit_graph(struct repo_settings *s,
326 void *graph_map, size_t graph_size)
328 const unsigned char *data;
329 struct commit_graph *graph;
330 uint32_t graph_signature;
331 unsigned char graph_version, hash_version;
332 struct chunkfile *cf = NULL;
334 if (!graph_map)
335 return NULL;
337 if (graph_size < GRAPH_MIN_SIZE)
338 return NULL;
340 data = (const unsigned char *)graph_map;
342 graph_signature = get_be32(data);
343 if (graph_signature != GRAPH_SIGNATURE) {
344 error(_("commit-graph signature %X does not match signature %X"),
345 graph_signature, GRAPH_SIGNATURE);
346 return NULL;
349 graph_version = *(unsigned char*)(data + 4);
350 if (graph_version != GRAPH_VERSION) {
351 error(_("commit-graph version %X does not match version %X"),
352 graph_version, GRAPH_VERSION);
353 return NULL;
356 hash_version = *(unsigned char*)(data + 5);
357 if (hash_version != oid_version(the_hash_algo)) {
358 error(_("commit-graph hash version %X does not match version %X"),
359 hash_version, oid_version(the_hash_algo));
360 return NULL;
363 graph = alloc_commit_graph();
365 graph->hash_len = the_hash_algo->rawsz;
366 graph->num_chunks = *(unsigned char*)(data + 6);
367 graph->data = graph_map;
368 graph->data_len = graph_size;
370 if (graph_size < GRAPH_HEADER_SIZE +
371 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
372 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
373 error(_("commit-graph file is too small to hold %u chunks"),
374 graph->num_chunks);
375 free(graph);
376 return NULL;
379 cf = init_chunkfile(NULL);
381 if (read_table_of_contents(cf, graph->data, graph_size,
382 GRAPH_HEADER_SIZE, graph->num_chunks))
383 goto free_and_return;
385 pair_chunk(cf, GRAPH_CHUNKID_OIDFANOUT,
386 (const unsigned char **)&graph->chunk_oid_fanout);
387 read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph);
388 pair_chunk(cf, GRAPH_CHUNKID_DATA, &graph->chunk_commit_data);
389 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges);
390 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs);
392 if (s->commit_graph_generation_version >= 2) {
393 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
394 &graph->chunk_generation_data);
395 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
396 &graph->chunk_generation_data_overflow);
398 if (graph->chunk_generation_data)
399 graph->read_generation_data = 1;
402 if (s->commit_graph_read_changed_paths) {
403 pair_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
404 &graph->chunk_bloom_indexes);
405 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
406 graph_read_bloom_data, graph);
409 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
410 init_bloom_filters();
411 } else {
412 /* We need both the bloom chunks to exist together. Else ignore the data */
413 graph->chunk_bloom_indexes = NULL;
414 graph->chunk_bloom_data = NULL;
415 FREE_AND_NULL(graph->bloom_filter_settings);
418 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
420 if (verify_commit_graph_lite(graph))
421 goto free_and_return;
423 free_chunkfile(cf);
424 return graph;
426 free_and_return:
427 free_chunkfile(cf);
428 free(graph->bloom_filter_settings);
429 free(graph);
430 return NULL;
433 static struct commit_graph *load_commit_graph_one(struct repository *r,
434 const char *graph_file,
435 struct object_directory *odb)
438 struct stat st;
439 int fd;
440 struct commit_graph *g;
441 int open_ok = open_commit_graph(graph_file, &fd, &st);
443 if (!open_ok)
444 return NULL;
446 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
448 if (g)
449 g->filename = xstrdup(graph_file);
451 return g;
454 static struct commit_graph *load_commit_graph_v1(struct repository *r,
455 struct object_directory *odb)
457 char *graph_name = get_commit_graph_filename(odb);
458 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
459 free(graph_name);
461 return g;
464 static int add_graph_to_chain(struct commit_graph *g,
465 struct commit_graph *chain,
466 struct object_id *oids,
467 int n)
469 struct commit_graph *cur_g = chain;
471 if (n && !g->chunk_base_graphs) {
472 warning(_("commit-graph has no base graphs chunk"));
473 return 0;
476 while (n) {
477 n--;
479 if (!cur_g ||
480 !oideq(&oids[n], &cur_g->oid) ||
481 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
482 warning(_("commit-graph chain does not match"));
483 return 0;
486 cur_g = cur_g->base_graph;
489 g->base_graph = chain;
491 if (chain)
492 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
494 return 1;
497 static struct commit_graph *load_commit_graph_chain(struct repository *r,
498 struct object_directory *odb)
500 struct commit_graph *graph_chain = NULL;
501 struct strbuf line = STRBUF_INIT;
502 struct stat st;
503 struct object_id *oids;
504 int i = 0, valid = 1, count;
505 char *chain_name = get_commit_graph_chain_filename(odb);
506 FILE *fp;
507 int stat_res;
509 fp = fopen(chain_name, "r");
510 stat_res = stat(chain_name, &st);
511 free(chain_name);
513 if (!fp)
514 return NULL;
515 if (stat_res ||
516 st.st_size <= the_hash_algo->hexsz) {
517 fclose(fp);
518 return NULL;
521 count = st.st_size / (the_hash_algo->hexsz + 1);
522 CALLOC_ARRAY(oids, count);
524 prepare_alt_odb(r);
526 for (i = 0; i < count; i++) {
527 struct object_directory *odb;
529 if (strbuf_getline_lf(&line, fp) == EOF)
530 break;
532 if (get_oid_hex(line.buf, &oids[i])) {
533 warning(_("invalid commit-graph chain: line '%s' not a hash"),
534 line.buf);
535 valid = 0;
536 break;
539 valid = 0;
540 for (odb = r->objects->odb; odb; odb = odb->next) {
541 char *graph_name = get_split_graph_filename(odb, line.buf);
542 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
544 free(graph_name);
546 if (g) {
547 if (add_graph_to_chain(g, graph_chain, oids, i)) {
548 graph_chain = g;
549 valid = 1;
552 break;
556 if (!valid) {
557 warning(_("unable to find all commit-graph files"));
558 break;
562 free(oids);
563 fclose(fp);
564 strbuf_release(&line);
566 return graph_chain;
570 * returns 1 if and only if all graphs in the chain have
571 * corrected commit dates stored in the generation_data chunk.
573 static int validate_mixed_generation_chain(struct commit_graph *g)
575 int read_generation_data = 1;
576 struct commit_graph *p = g;
578 while (read_generation_data && p) {
579 read_generation_data = p->read_generation_data;
580 p = p->base_graph;
583 if (read_generation_data)
584 return 1;
586 while (g) {
587 g->read_generation_data = 0;
588 g = g->base_graph;
591 return 0;
594 struct commit_graph *read_commit_graph_one(struct repository *r,
595 struct object_directory *odb)
597 struct commit_graph *g = load_commit_graph_v1(r, odb);
599 if (!g)
600 g = load_commit_graph_chain(r, odb);
602 validate_mixed_generation_chain(g);
604 return g;
607 static void prepare_commit_graph_one(struct repository *r,
608 struct object_directory *odb)
611 if (r->objects->commit_graph)
612 return;
614 r->objects->commit_graph = read_commit_graph_one(r, odb);
618 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
620 * On the first invocation, this function attempts to load the commit
621 * graph if the_repository is configured to have one.
623 static int prepare_commit_graph(struct repository *r)
625 struct object_directory *odb;
628 * Early return if there is no git dir or if the commit graph is
629 * disabled.
631 * This must come before the "already attempted?" check below, because
632 * we want to disable even an already-loaded graph file.
634 if (!r->gitdir || r->commit_graph_disabled)
635 return 0;
637 if (r->objects->commit_graph_attempted)
638 return !!r->objects->commit_graph;
639 r->objects->commit_graph_attempted = 1;
641 prepare_repo_settings(r);
643 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
644 r->settings.core_commit_graph != 1)
646 * This repository is not configured to use commit graphs, so
647 * do not load one. (But report commit_graph_attempted anyway
648 * so that commit graph loading is not attempted again for this
649 * repository.)
651 return 0;
653 if (!commit_graph_compatible(r))
654 return 0;
656 prepare_alt_odb(r);
657 for (odb = r->objects->odb;
658 !r->objects->commit_graph && odb;
659 odb = odb->next)
660 prepare_commit_graph_one(r, odb);
661 return !!r->objects->commit_graph;
664 int generation_numbers_enabled(struct repository *r)
666 uint32_t first_generation;
667 struct commit_graph *g;
668 if (!prepare_commit_graph(r))
669 return 0;
671 g = r->objects->commit_graph;
673 if (!g->num_commits)
674 return 0;
676 first_generation = get_be32(g->chunk_commit_data +
677 g->hash_len + 8) >> 2;
679 return !!first_generation;
682 int corrected_commit_dates_enabled(struct repository *r)
684 struct commit_graph *g;
685 if (!prepare_commit_graph(r))
686 return 0;
688 g = r->objects->commit_graph;
690 if (!g->num_commits)
691 return 0;
693 return g->read_generation_data;
696 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
698 struct commit_graph *g = r->objects->commit_graph;
699 while (g) {
700 if (g->bloom_filter_settings)
701 return g->bloom_filter_settings;
702 g = g->base_graph;
704 return NULL;
707 static void close_commit_graph_one(struct commit_graph *g)
709 if (!g)
710 return;
712 clear_commit_graph_data_slab(&commit_graph_data_slab);
713 close_commit_graph_one(g->base_graph);
714 free_commit_graph(g);
717 void close_commit_graph(struct raw_object_store *o)
719 close_commit_graph_one(o->commit_graph);
720 o->commit_graph = NULL;
723 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
725 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
726 g->chunk_oid_lookup, g->hash_len, pos);
729 static void load_oid_from_graph(struct commit_graph *g,
730 uint32_t pos,
731 struct object_id *oid)
733 uint32_t lex_index;
735 while (g && pos < g->num_commits_in_base)
736 g = g->base_graph;
738 if (!g)
739 BUG("NULL commit-graph");
741 if (pos >= g->num_commits + g->num_commits_in_base)
742 die(_("invalid commit position. commit-graph is likely corrupt"));
744 lex_index = pos - g->num_commits_in_base;
746 oidread(oid, g->chunk_oid_lookup + g->hash_len * lex_index);
749 static struct commit_list **insert_parent_or_die(struct repository *r,
750 struct commit_graph *g,
751 uint32_t pos,
752 struct commit_list **pptr)
754 struct commit *c;
755 struct object_id oid;
757 if (pos >= g->num_commits + g->num_commits_in_base)
758 die("invalid parent position %"PRIu32, pos);
760 load_oid_from_graph(g, pos, &oid);
761 c = lookup_commit(r, &oid);
762 if (!c)
763 die(_("could not find commit %s"), oid_to_hex(&oid));
764 commit_graph_data_at(c)->graph_pos = pos;
765 return &commit_list_insert(c, pptr)->next;
768 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
770 const unsigned char *commit_data;
771 struct commit_graph_data *graph_data;
772 uint32_t lex_index, offset_pos;
773 uint64_t date_high, date_low, offset;
775 while (pos < g->num_commits_in_base)
776 g = g->base_graph;
778 if (pos >= g->num_commits + g->num_commits_in_base)
779 die(_("invalid commit position. commit-graph is likely corrupt"));
781 lex_index = pos - g->num_commits_in_base;
782 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
784 graph_data = commit_graph_data_at(item);
785 graph_data->graph_pos = pos;
787 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
788 date_low = get_be32(commit_data + g->hash_len + 12);
789 item->date = (timestamp_t)((date_high << 32) | date_low);
791 if (g->read_generation_data) {
792 offset = (timestamp_t)get_be32(g->chunk_generation_data + sizeof(uint32_t) * lex_index);
794 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
795 if (!g->chunk_generation_data_overflow)
796 die(_("commit-graph requires overflow generation data but has none"));
798 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
799 graph_data->generation = item->date + get_be64(g->chunk_generation_data_overflow + 8 * offset_pos);
800 } else
801 graph_data->generation = item->date + offset;
802 } else
803 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
805 if (g->topo_levels)
806 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
809 static inline void set_commit_tree(struct commit *c, struct tree *t)
811 c->maybe_tree = t;
814 static int fill_commit_in_graph(struct repository *r,
815 struct commit *item,
816 struct commit_graph *g, uint32_t pos)
818 uint32_t edge_value;
819 uint32_t *parent_data_ptr;
820 struct commit_list **pptr;
821 const unsigned char *commit_data;
822 uint32_t lex_index;
824 while (pos < g->num_commits_in_base)
825 g = g->base_graph;
827 fill_commit_graph_info(item, g, pos);
829 lex_index = pos - g->num_commits_in_base;
830 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
832 item->object.parsed = 1;
834 set_commit_tree(item, NULL);
836 pptr = &item->parents;
838 edge_value = get_be32(commit_data + g->hash_len);
839 if (edge_value == GRAPH_PARENT_NONE)
840 return 1;
841 pptr = insert_parent_or_die(r, g, edge_value, pptr);
843 edge_value = get_be32(commit_data + g->hash_len + 4);
844 if (edge_value == GRAPH_PARENT_NONE)
845 return 1;
846 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
847 pptr = insert_parent_or_die(r, g, edge_value, pptr);
848 return 1;
851 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
852 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
853 do {
854 edge_value = get_be32(parent_data_ptr);
855 pptr = insert_parent_or_die(r, g,
856 edge_value & GRAPH_EDGE_LAST_MASK,
857 pptr);
858 parent_data_ptr++;
859 } while (!(edge_value & GRAPH_LAST_EDGE));
861 return 1;
864 static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
866 struct commit_graph *cur_g = g;
867 uint32_t lex_index;
869 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
870 cur_g = cur_g->base_graph;
872 if (cur_g) {
873 *pos = lex_index + cur_g->num_commits_in_base;
874 return 1;
877 return 0;
880 static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
882 uint32_t graph_pos = commit_graph_position(item);
883 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
884 *pos = graph_pos;
885 return 1;
886 } else {
887 return search_commit_pos_in_graph(&item->object.oid, g, pos);
891 struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
893 struct commit *commit;
894 uint32_t pos;
896 if (!repo->objects->commit_graph)
897 return NULL;
898 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
899 return NULL;
900 if (!has_object(repo, id, 0))
901 return NULL;
903 commit = lookup_commit(repo, id);
904 if (!commit)
905 return NULL;
906 if (commit->object.parsed)
907 return commit;
909 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
910 return NULL;
912 return commit;
915 static int parse_commit_in_graph_one(struct repository *r,
916 struct commit_graph *g,
917 struct commit *item)
919 uint32_t pos;
921 if (item->object.parsed)
922 return 1;
924 if (find_commit_pos_in_graph(item, g, &pos))
925 return fill_commit_in_graph(r, item, g, pos);
927 return 0;
930 int parse_commit_in_graph(struct repository *r, struct commit *item)
932 static int checked_env = 0;
934 if (!checked_env &&
935 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
936 die("dying as requested by the '%s' variable on commit-graph parse!",
937 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
938 checked_env = 1;
940 if (!prepare_commit_graph(r))
941 return 0;
942 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
945 void load_commit_graph_info(struct repository *r, struct commit *item)
947 uint32_t pos;
948 if (!prepare_commit_graph(r))
949 return;
950 if (find_commit_pos_in_graph(item, r->objects->commit_graph, &pos))
951 fill_commit_graph_info(item, r->objects->commit_graph, pos);
954 static struct tree *load_tree_for_commit(struct repository *r,
955 struct commit_graph *g,
956 struct commit *c)
958 struct object_id oid;
959 const unsigned char *commit_data;
960 uint32_t graph_pos = commit_graph_position(c);
962 while (graph_pos < g->num_commits_in_base)
963 g = g->base_graph;
965 commit_data = g->chunk_commit_data +
966 GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base);
968 oidread(&oid, commit_data);
969 set_commit_tree(c, lookup_tree(r, &oid));
971 return c->maybe_tree;
974 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
975 struct commit_graph *g,
976 const struct commit *c)
978 if (c->maybe_tree)
979 return c->maybe_tree;
980 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
981 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
983 return load_tree_for_commit(r, g, (struct commit *)c);
986 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
988 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
991 struct packed_commit_list {
992 struct commit **list;
993 size_t nr;
994 size_t alloc;
997 struct write_commit_graph_context {
998 struct repository *r;
999 struct object_directory *odb;
1000 char *graph_name;
1001 struct oid_array oids;
1002 struct packed_commit_list commits;
1003 int num_extra_edges;
1004 int num_generation_data_overflows;
1005 unsigned long approx_nr_objects;
1006 struct progress *progress;
1007 int progress_done;
1008 uint64_t progress_cnt;
1010 char *base_graph_name;
1011 int num_commit_graphs_before;
1012 int num_commit_graphs_after;
1013 char **commit_graph_filenames_before;
1014 char **commit_graph_filenames_after;
1015 char **commit_graph_hash_after;
1016 uint32_t new_num_commits_in_base;
1017 struct commit_graph *new_base_graph;
1019 unsigned append:1,
1020 report_progress:1,
1021 split:1,
1022 changed_paths:1,
1023 order_by_pack:1,
1024 write_generation_data:1,
1025 trust_generation_numbers:1;
1027 struct topo_level_slab *topo_levels;
1028 const struct commit_graph_opts *opts;
1029 size_t total_bloom_filter_data_size;
1030 const struct bloom_filter_settings *bloom_settings;
1032 int count_bloom_filter_computed;
1033 int count_bloom_filter_not_computed;
1034 int count_bloom_filter_trunc_empty;
1035 int count_bloom_filter_trunc_large;
1038 static int write_graph_chunk_fanout(struct hashfile *f,
1039 void *data)
1041 struct write_commit_graph_context *ctx = data;
1042 int i, count = 0;
1043 struct commit **list = ctx->commits.list;
1046 * Write the first-level table (the list is sorted,
1047 * but we use a 256-entry lookup to be able to avoid
1048 * having to do eight extra binary search iterations).
1050 for (i = 0; i < 256; i++) {
1051 while (count < ctx->commits.nr) {
1052 if ((*list)->object.oid.hash[0] != i)
1053 break;
1054 display_progress(ctx->progress, ++ctx->progress_cnt);
1055 count++;
1056 list++;
1059 hashwrite_be32(f, count);
1062 return 0;
1065 static int write_graph_chunk_oids(struct hashfile *f,
1066 void *data)
1068 struct write_commit_graph_context *ctx = data;
1069 struct commit **list = ctx->commits.list;
1070 int count;
1071 for (count = 0; count < ctx->commits.nr; count++, list++) {
1072 display_progress(ctx->progress, ++ctx->progress_cnt);
1073 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1076 return 0;
1079 static const struct object_id *commit_to_oid(size_t index, const void *table)
1081 const struct commit * const *commits = table;
1082 return &commits[index]->object.oid;
1085 static int write_graph_chunk_data(struct hashfile *f,
1086 void *data)
1088 struct write_commit_graph_context *ctx = data;
1089 struct commit **list = ctx->commits.list;
1090 struct commit **last = ctx->commits.list + ctx->commits.nr;
1091 uint32_t num_extra_edges = 0;
1093 while (list < last) {
1094 struct commit_list *parent;
1095 struct object_id *tree;
1096 int edge_value;
1097 uint32_t packedDate[2];
1098 display_progress(ctx->progress, ++ctx->progress_cnt);
1100 if (repo_parse_commit_no_graph(ctx->r, *list))
1101 die(_("unable to parse commit %s"),
1102 oid_to_hex(&(*list)->object.oid));
1103 tree = get_commit_tree_oid(*list);
1104 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1106 parent = (*list)->parents;
1108 if (!parent)
1109 edge_value = GRAPH_PARENT_NONE;
1110 else {
1111 edge_value = oid_pos(&parent->item->object.oid,
1112 ctx->commits.list,
1113 ctx->commits.nr,
1114 commit_to_oid);
1116 if (edge_value >= 0)
1117 edge_value += ctx->new_num_commits_in_base;
1118 else if (ctx->new_base_graph) {
1119 uint32_t pos;
1120 if (find_commit_pos_in_graph(parent->item,
1121 ctx->new_base_graph,
1122 &pos))
1123 edge_value = pos;
1126 if (edge_value < 0)
1127 BUG("missing parent %s for commit %s",
1128 oid_to_hex(&parent->item->object.oid),
1129 oid_to_hex(&(*list)->object.oid));
1132 hashwrite_be32(f, edge_value);
1134 if (parent)
1135 parent = parent->next;
1137 if (!parent)
1138 edge_value = GRAPH_PARENT_NONE;
1139 else if (parent->next)
1140 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1141 else {
1142 edge_value = oid_pos(&parent->item->object.oid,
1143 ctx->commits.list,
1144 ctx->commits.nr,
1145 commit_to_oid);
1147 if (edge_value >= 0)
1148 edge_value += ctx->new_num_commits_in_base;
1149 else if (ctx->new_base_graph) {
1150 uint32_t pos;
1151 if (find_commit_pos_in_graph(parent->item,
1152 ctx->new_base_graph,
1153 &pos))
1154 edge_value = pos;
1157 if (edge_value < 0)
1158 BUG("missing parent %s for commit %s",
1159 oid_to_hex(&parent->item->object.oid),
1160 oid_to_hex(&(*list)->object.oid));
1163 hashwrite_be32(f, edge_value);
1165 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1166 do {
1167 num_extra_edges++;
1168 parent = parent->next;
1169 } while (parent);
1172 if (sizeof((*list)->date) > 4)
1173 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1174 else
1175 packedDate[0] = 0;
1177 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1179 packedDate[1] = htonl((*list)->date);
1180 hashwrite(f, packedDate, 8);
1182 list++;
1185 return 0;
1188 static int write_graph_chunk_generation_data(struct hashfile *f,
1189 void *data)
1191 struct write_commit_graph_context *ctx = data;
1192 int i, num_generation_data_overflows = 0;
1194 for (i = 0; i < ctx->commits.nr; i++) {
1195 struct commit *c = ctx->commits.list[i];
1196 timestamp_t offset;
1197 repo_parse_commit(ctx->r, c);
1198 offset = commit_graph_data_at(c)->generation - c->date;
1199 display_progress(ctx->progress, ++ctx->progress_cnt);
1201 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1202 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1203 num_generation_data_overflows++;
1206 hashwrite_be32(f, offset);
1209 return 0;
1212 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1213 void *data)
1215 struct write_commit_graph_context *ctx = data;
1216 int i;
1217 for (i = 0; i < ctx->commits.nr; i++) {
1218 struct commit *c = ctx->commits.list[i];
1219 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1220 display_progress(ctx->progress, ++ctx->progress_cnt);
1222 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1223 hashwrite_be32(f, offset >> 32);
1224 hashwrite_be32(f, (uint32_t) offset);
1228 return 0;
1231 static int write_graph_chunk_extra_edges(struct hashfile *f,
1232 void *data)
1234 struct write_commit_graph_context *ctx = data;
1235 struct commit **list = ctx->commits.list;
1236 struct commit **last = ctx->commits.list + ctx->commits.nr;
1237 struct commit_list *parent;
1239 while (list < last) {
1240 int num_parents = 0;
1242 display_progress(ctx->progress, ++ctx->progress_cnt);
1244 for (parent = (*list)->parents; num_parents < 3 && parent;
1245 parent = parent->next)
1246 num_parents++;
1248 if (num_parents <= 2) {
1249 list++;
1250 continue;
1253 /* Since num_parents > 2, this initializer is safe. */
1254 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1255 int edge_value = oid_pos(&parent->item->object.oid,
1256 ctx->commits.list,
1257 ctx->commits.nr,
1258 commit_to_oid);
1260 if (edge_value >= 0)
1261 edge_value += ctx->new_num_commits_in_base;
1262 else if (ctx->new_base_graph) {
1263 uint32_t pos;
1264 if (find_commit_pos_in_graph(parent->item,
1265 ctx->new_base_graph,
1266 &pos))
1267 edge_value = pos;
1270 if (edge_value < 0)
1271 BUG("missing parent %s for commit %s",
1272 oid_to_hex(&parent->item->object.oid),
1273 oid_to_hex(&(*list)->object.oid));
1274 else if (!parent->next)
1275 edge_value |= GRAPH_LAST_EDGE;
1277 hashwrite_be32(f, edge_value);
1280 list++;
1283 return 0;
1286 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1287 void *data)
1289 struct write_commit_graph_context *ctx = data;
1290 struct commit **list = ctx->commits.list;
1291 struct commit **last = ctx->commits.list + ctx->commits.nr;
1292 uint32_t cur_pos = 0;
1294 while (list < last) {
1295 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1296 size_t len = filter ? filter->len : 0;
1297 cur_pos += len;
1298 display_progress(ctx->progress, ++ctx->progress_cnt);
1299 hashwrite_be32(f, cur_pos);
1300 list++;
1303 return 0;
1306 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1308 struct json_writer jw = JSON_WRITER_INIT;
1310 jw_object_begin(&jw, 0);
1311 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1312 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1313 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1314 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1315 jw_end(&jw);
1317 trace2_data_json("bloom", ctx->r, "settings", &jw);
1319 jw_release(&jw);
1322 static int write_graph_chunk_bloom_data(struct hashfile *f,
1323 void *data)
1325 struct write_commit_graph_context *ctx = data;
1326 struct commit **list = ctx->commits.list;
1327 struct commit **last = ctx->commits.list + ctx->commits.nr;
1329 trace2_bloom_filter_settings(ctx);
1331 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1332 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1333 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1335 while (list < last) {
1336 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1337 size_t len = filter ? filter->len : 0;
1339 display_progress(ctx->progress, ++ctx->progress_cnt);
1340 if (len)
1341 hashwrite(f, filter->data, len * sizeof(unsigned char));
1342 list++;
1345 return 0;
1348 static int add_packed_commits(const struct object_id *oid,
1349 struct packed_git *pack,
1350 uint32_t pos,
1351 void *data)
1353 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1354 enum object_type type;
1355 off_t offset = nth_packed_object_offset(pack, pos);
1356 struct object_info oi = OBJECT_INFO_INIT;
1358 if (ctx->progress)
1359 display_progress(ctx->progress, ++ctx->progress_done);
1361 oi.typep = &type;
1362 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1363 die(_("unable to get type of object %s"), oid_to_hex(oid));
1365 if (type != OBJ_COMMIT)
1366 return 0;
1368 oid_array_append(&ctx->oids, oid);
1369 set_commit_pos(ctx->r, oid);
1371 return 0;
1374 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1376 struct commit_list *parent;
1377 for (parent = commit->parents; parent; parent = parent->next) {
1378 if (!(parent->item->object.flags & REACHABLE)) {
1379 oid_array_append(&ctx->oids, &parent->item->object.oid);
1380 parent->item->object.flags |= REACHABLE;
1385 static void close_reachable(struct write_commit_graph_context *ctx)
1387 int i;
1388 struct commit *commit;
1389 enum commit_graph_split_flags flags = ctx->opts ?
1390 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1392 if (ctx->report_progress)
1393 ctx->progress = start_delayed_progress(
1394 _("Loading known commits in commit graph"),
1395 ctx->oids.nr);
1396 for (i = 0; i < ctx->oids.nr; i++) {
1397 display_progress(ctx->progress, i + 1);
1398 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1399 if (commit)
1400 commit->object.flags |= REACHABLE;
1402 stop_progress(&ctx->progress);
1405 * As this loop runs, ctx->oids.nr may grow, but not more
1406 * than the number of missing commits in the reachable
1407 * closure.
1409 if (ctx->report_progress)
1410 ctx->progress = start_delayed_progress(
1411 _("Expanding reachable commits in commit graph"),
1413 for (i = 0; i < ctx->oids.nr; i++) {
1414 display_progress(ctx->progress, i + 1);
1415 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1417 if (!commit)
1418 continue;
1419 if (ctx->split) {
1420 if ((!repo_parse_commit(ctx->r, commit) &&
1421 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1422 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1423 add_missing_parents(ctx, commit);
1424 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1425 add_missing_parents(ctx, commit);
1427 stop_progress(&ctx->progress);
1429 if (ctx->report_progress)
1430 ctx->progress = start_delayed_progress(
1431 _("Clearing commit marks in commit graph"),
1432 ctx->oids.nr);
1433 for (i = 0; i < ctx->oids.nr; i++) {
1434 display_progress(ctx->progress, i + 1);
1435 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1437 if (commit)
1438 commit->object.flags &= ~REACHABLE;
1440 stop_progress(&ctx->progress);
1443 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1445 int i;
1446 struct commit_list *list = NULL;
1448 if (ctx->report_progress)
1449 ctx->progress = start_delayed_progress(
1450 _("Computing commit graph topological levels"),
1451 ctx->commits.nr);
1452 for (i = 0; i < ctx->commits.nr; i++) {
1453 struct commit *c = ctx->commits.list[i];
1454 uint32_t level;
1456 repo_parse_commit(ctx->r, c);
1457 level = *topo_level_slab_at(ctx->topo_levels, c);
1459 display_progress(ctx->progress, i + 1);
1460 if (level != GENERATION_NUMBER_ZERO)
1461 continue;
1463 commit_list_insert(c, &list);
1464 while (list) {
1465 struct commit *current = list->item;
1466 struct commit_list *parent;
1467 int all_parents_computed = 1;
1468 uint32_t max_level = 0;
1470 for (parent = current->parents; parent; parent = parent->next) {
1471 repo_parse_commit(ctx->r, parent->item);
1472 level = *topo_level_slab_at(ctx->topo_levels, parent->item);
1474 if (level == GENERATION_NUMBER_ZERO) {
1475 all_parents_computed = 0;
1476 commit_list_insert(parent->item, &list);
1477 break;
1480 if (level > max_level)
1481 max_level = level;
1484 if (all_parents_computed) {
1485 pop_commit(&list);
1487 if (max_level > GENERATION_NUMBER_V1_MAX - 1)
1488 max_level = GENERATION_NUMBER_V1_MAX - 1;
1489 *topo_level_slab_at(ctx->topo_levels, current) = max_level + 1;
1493 stop_progress(&ctx->progress);
1496 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1498 int i;
1499 struct commit_list *list = NULL;
1501 if (ctx->report_progress)
1502 ctx->progress = start_delayed_progress(
1503 _("Computing commit graph generation numbers"),
1504 ctx->commits.nr);
1506 if (!ctx->trust_generation_numbers) {
1507 for (i = 0; i < ctx->commits.nr; i++) {
1508 struct commit *c = ctx->commits.list[i];
1509 repo_parse_commit(ctx->r, c);
1510 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1514 for (i = 0; i < ctx->commits.nr; i++) {
1515 struct commit *c = ctx->commits.list[i];
1516 timestamp_t corrected_commit_date;
1518 repo_parse_commit(ctx->r, c);
1519 corrected_commit_date = commit_graph_data_at(c)->generation;
1521 display_progress(ctx->progress, i + 1);
1522 if (corrected_commit_date != GENERATION_NUMBER_ZERO)
1523 continue;
1525 commit_list_insert(c, &list);
1526 while (list) {
1527 struct commit *current = list->item;
1528 struct commit_list *parent;
1529 int all_parents_computed = 1;
1530 timestamp_t max_corrected_commit_date = 0;
1532 for (parent = current->parents; parent; parent = parent->next) {
1533 repo_parse_commit(ctx->r, parent->item);
1534 corrected_commit_date = commit_graph_data_at(parent->item)->generation;
1536 if (corrected_commit_date == GENERATION_NUMBER_ZERO) {
1537 all_parents_computed = 0;
1538 commit_list_insert(parent->item, &list);
1539 break;
1542 if (corrected_commit_date > max_corrected_commit_date)
1543 max_corrected_commit_date = corrected_commit_date;
1546 if (all_parents_computed) {
1547 pop_commit(&list);
1549 if (current->date && current->date > max_corrected_commit_date)
1550 max_corrected_commit_date = current->date - 1;
1551 commit_graph_data_at(current)->generation = max_corrected_commit_date + 1;
1556 for (i = 0; i < ctx->commits.nr; i++) {
1557 struct commit *c = ctx->commits.list[i];
1558 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1559 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1560 ctx->num_generation_data_overflows++;
1562 stop_progress(&ctx->progress);
1565 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1567 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1568 ctx->count_bloom_filter_computed);
1569 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1570 ctx->count_bloom_filter_not_computed);
1571 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1572 ctx->count_bloom_filter_trunc_empty);
1573 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1574 ctx->count_bloom_filter_trunc_large);
1577 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1579 int i;
1580 struct progress *progress = NULL;
1581 struct commit **sorted_commits;
1582 int max_new_filters;
1584 init_bloom_filters();
1586 if (ctx->report_progress)
1587 progress = start_delayed_progress(
1588 _("Computing commit changed paths Bloom filters"),
1589 ctx->commits.nr);
1591 ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1592 COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1594 if (ctx->order_by_pack)
1595 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1596 else
1597 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1599 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1600 ctx->opts->max_new_filters : ctx->commits.nr;
1602 for (i = 0; i < ctx->commits.nr; i++) {
1603 enum bloom_filter_computed computed = 0;
1604 struct commit *c = sorted_commits[i];
1605 struct bloom_filter *filter = get_or_compute_bloom_filter(
1606 ctx->r,
1608 ctx->count_bloom_filter_computed < max_new_filters,
1609 ctx->bloom_settings,
1610 &computed);
1611 if (computed & BLOOM_COMPUTED) {
1612 ctx->count_bloom_filter_computed++;
1613 if (computed & BLOOM_TRUNC_EMPTY)
1614 ctx->count_bloom_filter_trunc_empty++;
1615 if (computed & BLOOM_TRUNC_LARGE)
1616 ctx->count_bloom_filter_trunc_large++;
1617 } else if (computed & BLOOM_NOT_COMPUTED)
1618 ctx->count_bloom_filter_not_computed++;
1619 ctx->total_bloom_filter_data_size += filter
1620 ? sizeof(unsigned char) * filter->len : 0;
1621 display_progress(progress, i + 1);
1624 if (trace2_is_enabled())
1625 trace2_bloom_filter_write_statistics(ctx);
1627 free(sorted_commits);
1628 stop_progress(&progress);
1631 struct refs_cb_data {
1632 struct oidset *commits;
1633 struct progress *progress;
1636 static int add_ref_to_set(const char *refname,
1637 const struct object_id *oid,
1638 int flags, void *cb_data)
1640 struct object_id peeled;
1641 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1643 if (!peel_iterated_oid(oid, &peeled))
1644 oid = &peeled;
1645 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1646 oidset_insert(data->commits, oid);
1648 display_progress(data->progress, oidset_size(data->commits));
1650 return 0;
1653 int write_commit_graph_reachable(struct object_directory *odb,
1654 enum commit_graph_write_flags flags,
1655 const struct commit_graph_opts *opts)
1657 struct oidset commits = OIDSET_INIT;
1658 struct refs_cb_data data;
1659 int result;
1661 memset(&data, 0, sizeof(data));
1662 data.commits = &commits;
1663 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1664 data.progress = start_delayed_progress(
1665 _("Collecting referenced commits"), 0);
1667 for_each_ref(add_ref_to_set, &data);
1669 stop_progress(&data.progress);
1671 result = write_commit_graph(odb, NULL, &commits,
1672 flags, opts);
1674 oidset_clear(&commits);
1675 return result;
1678 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1679 const struct string_list *pack_indexes)
1681 uint32_t i;
1682 struct strbuf progress_title = STRBUF_INIT;
1683 struct strbuf packname = STRBUF_INIT;
1684 int dirlen;
1685 int ret = 0;
1687 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1688 dirlen = packname.len;
1689 if (ctx->report_progress) {
1690 strbuf_addf(&progress_title,
1691 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1692 "Finding commits for commit graph in %"PRIuMAX" packs",
1693 pack_indexes->nr),
1694 (uintmax_t)pack_indexes->nr);
1695 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1696 ctx->progress_done = 0;
1698 for (i = 0; i < pack_indexes->nr; i++) {
1699 struct packed_git *p;
1700 strbuf_setlen(&packname, dirlen);
1701 strbuf_addstr(&packname, pack_indexes->items[i].string);
1702 p = add_packed_git(packname.buf, packname.len, 1);
1703 if (!p) {
1704 ret = error(_("error adding pack %s"), packname.buf);
1705 goto cleanup;
1707 if (open_pack_index(p)) {
1708 ret = error(_("error opening index for %s"), packname.buf);
1709 goto cleanup;
1711 for_each_object_in_pack(p, add_packed_commits, ctx,
1712 FOR_EACH_OBJECT_PACK_ORDER);
1713 close_pack(p);
1714 free(p);
1717 cleanup:
1718 stop_progress(&ctx->progress);
1719 strbuf_release(&progress_title);
1720 strbuf_release(&packname);
1722 return ret;
1725 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1726 struct oidset *commits)
1728 struct oidset_iter iter;
1729 struct object_id *oid;
1731 if (!oidset_size(commits))
1732 return 0;
1734 oidset_iter_init(commits, &iter);
1735 while ((oid = oidset_iter_next(&iter))) {
1736 oid_array_append(&ctx->oids, oid);
1739 return 0;
1742 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1744 if (ctx->report_progress)
1745 ctx->progress = start_delayed_progress(
1746 _("Finding commits for commit graph among packed objects"),
1747 ctx->approx_nr_objects);
1748 for_each_packed_object(add_packed_commits, ctx,
1749 FOR_EACH_OBJECT_PACK_ORDER);
1750 if (ctx->progress_done < ctx->approx_nr_objects)
1751 display_progress(ctx->progress, ctx->approx_nr_objects);
1752 stop_progress(&ctx->progress);
1755 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1757 uint32_t i;
1758 enum commit_graph_split_flags flags = ctx->opts ?
1759 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1761 ctx->num_extra_edges = 0;
1762 if (ctx->report_progress)
1763 ctx->progress = start_delayed_progress(
1764 _("Finding extra edges in commit graph"),
1765 ctx->oids.nr);
1766 oid_array_sort(&ctx->oids);
1767 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1768 unsigned int num_parents;
1770 display_progress(ctx->progress, i + 1);
1772 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1773 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1775 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1776 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1777 continue;
1779 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1780 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1781 else
1782 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1784 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1785 if (num_parents > 2)
1786 ctx->num_extra_edges += num_parents - 1;
1788 ctx->commits.nr++;
1790 stop_progress(&ctx->progress);
1793 static int write_graph_chunk_base_1(struct hashfile *f,
1794 struct commit_graph *g)
1796 int num = 0;
1798 if (!g)
1799 return 0;
1801 num = write_graph_chunk_base_1(f, g->base_graph);
1802 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1803 return num + 1;
1806 static int write_graph_chunk_base(struct hashfile *f,
1807 void *data)
1809 struct write_commit_graph_context *ctx = data;
1810 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1812 if (num != ctx->num_commit_graphs_after - 1) {
1813 error(_("failed to write correct number of base graph ids"));
1814 return -1;
1817 return 0;
1820 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1822 uint32_t i;
1823 int fd;
1824 struct hashfile *f;
1825 struct lock_file lk = LOCK_INIT;
1826 const unsigned hashsz = the_hash_algo->rawsz;
1827 struct strbuf progress_title = STRBUF_INIT;
1828 struct chunkfile *cf;
1829 unsigned char file_hash[GIT_MAX_RAWSZ];
1831 if (ctx->split) {
1832 struct strbuf tmp_file = STRBUF_INIT;
1834 strbuf_addf(&tmp_file,
1835 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1836 ctx->odb->path);
1837 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1838 } else {
1839 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1842 if (safe_create_leading_directories(ctx->graph_name)) {
1843 UNLEAK(ctx->graph_name);
1844 error(_("unable to create leading directories of %s"),
1845 ctx->graph_name);
1846 return -1;
1849 if (ctx->split) {
1850 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
1852 hold_lock_file_for_update_mode(&lk, lock_name,
1853 LOCK_DIE_ON_ERROR, 0444);
1854 free(lock_name);
1856 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1857 if (fd < 0) {
1858 error(_("unable to create temporary graph layer"));
1859 return -1;
1862 if (adjust_shared_perm(ctx->graph_name)) {
1863 error(_("unable to adjust shared permissions for '%s'"),
1864 ctx->graph_name);
1865 return -1;
1868 f = hashfd(fd, ctx->graph_name);
1869 } else {
1870 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1871 LOCK_DIE_ON_ERROR, 0444);
1872 fd = get_lock_file_fd(&lk);
1873 f = hashfd(fd, get_lock_file_path(&lk));
1876 cf = init_chunkfile(f);
1878 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
1879 write_graph_chunk_fanout);
1880 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, hashsz * ctx->commits.nr,
1881 write_graph_chunk_oids);
1882 add_chunk(cf, GRAPH_CHUNKID_DATA, (hashsz + 16) * ctx->commits.nr,
1883 write_graph_chunk_data);
1885 if (ctx->write_generation_data)
1886 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
1887 sizeof(uint32_t) * ctx->commits.nr,
1888 write_graph_chunk_generation_data);
1889 if (ctx->num_generation_data_overflows)
1890 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
1891 sizeof(timestamp_t) * ctx->num_generation_data_overflows,
1892 write_graph_chunk_generation_data_overflow);
1893 if (ctx->num_extra_edges)
1894 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
1895 4 * ctx->num_extra_edges,
1896 write_graph_chunk_extra_edges);
1897 if (ctx->changed_paths) {
1898 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
1899 sizeof(uint32_t) * ctx->commits.nr,
1900 write_graph_chunk_bloom_indexes);
1901 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
1902 sizeof(uint32_t) * 3
1903 + ctx->total_bloom_filter_data_size,
1904 write_graph_chunk_bloom_data);
1906 if (ctx->num_commit_graphs_after > 1)
1907 add_chunk(cf, GRAPH_CHUNKID_BASE,
1908 hashsz * (ctx->num_commit_graphs_after - 1),
1909 write_graph_chunk_base);
1911 hashwrite_be32(f, GRAPH_SIGNATURE);
1913 hashwrite_u8(f, GRAPH_VERSION);
1914 hashwrite_u8(f, oid_version(the_hash_algo));
1915 hashwrite_u8(f, get_num_chunks(cf));
1916 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1918 if (ctx->report_progress) {
1919 strbuf_addf(&progress_title,
1920 Q_("Writing out commit graph in %d pass",
1921 "Writing out commit graph in %d passes",
1922 get_num_chunks(cf)),
1923 get_num_chunks(cf));
1924 ctx->progress = start_delayed_progress(
1925 progress_title.buf,
1926 get_num_chunks(cf) * ctx->commits.nr);
1929 write_chunkfile(cf, ctx);
1931 stop_progress(&ctx->progress);
1932 strbuf_release(&progress_title);
1934 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1935 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1936 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
1938 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1939 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1940 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1941 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1944 close_commit_graph(ctx->r->objects);
1945 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
1946 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1947 free_chunkfile(cf);
1949 if (ctx->split) {
1950 FILE *chainf = fdopen_lock_file(&lk, "w");
1951 char *final_graph_name;
1952 int result;
1954 close(fd);
1956 if (!chainf) {
1957 error(_("unable to open commit-graph chain file"));
1958 return -1;
1961 if (ctx->base_graph_name) {
1962 const char *dest;
1963 int idx = ctx->num_commit_graphs_after - 1;
1964 if (ctx->num_commit_graphs_after > 1)
1965 idx--;
1967 dest = ctx->commit_graph_filenames_after[idx];
1969 if (strcmp(ctx->base_graph_name, dest)) {
1970 result = rename(ctx->base_graph_name, dest);
1972 if (result) {
1973 error(_("failed to rename base commit-graph file"));
1974 return -1;
1977 } else {
1978 char *graph_name = get_commit_graph_filename(ctx->odb);
1979 unlink(graph_name);
1980 free(graph_name);
1983 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
1984 final_graph_name = get_split_graph_filename(ctx->odb,
1985 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1986 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1988 result = rename(ctx->graph_name, final_graph_name);
1990 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1991 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
1993 if (result) {
1994 error(_("failed to rename temporary commit-graph file"));
1995 return -1;
1999 commit_lock_file(&lk);
2001 return 0;
2004 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2006 struct commit_graph *g;
2007 uint32_t num_commits;
2008 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2009 uint32_t i;
2011 int max_commits = 0;
2012 int size_mult = 2;
2014 if (ctx->opts) {
2015 max_commits = ctx->opts->max_commits;
2017 if (ctx->opts->size_multiple)
2018 size_mult = ctx->opts->size_multiple;
2020 flags = ctx->opts->split_flags;
2023 g = ctx->r->objects->commit_graph;
2024 num_commits = ctx->commits.nr;
2025 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2026 ctx->num_commit_graphs_after = 1;
2027 else
2028 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2030 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2031 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2032 while (g && (g->num_commits <= size_mult * num_commits ||
2033 (max_commits && num_commits > max_commits))) {
2034 if (g->odb != ctx->odb)
2035 break;
2037 num_commits += g->num_commits;
2038 g = g->base_graph;
2040 ctx->num_commit_graphs_after--;
2044 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2045 ctx->new_base_graph = g;
2046 else if (ctx->num_commit_graphs_after != 1)
2047 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2048 "should be 1 with --split=replace");
2050 if (ctx->num_commit_graphs_after == 2) {
2051 char *old_graph_name = get_commit_graph_filename(g->odb);
2053 if (!strcmp(g->filename, old_graph_name) &&
2054 g->odb != ctx->odb) {
2055 ctx->num_commit_graphs_after = 1;
2056 ctx->new_base_graph = NULL;
2059 free(old_graph_name);
2062 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2063 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2065 for (i = 0; i < ctx->num_commit_graphs_after &&
2066 i < ctx->num_commit_graphs_before; i++)
2067 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2069 i = ctx->num_commit_graphs_before - 1;
2070 g = ctx->r->objects->commit_graph;
2072 while (g) {
2073 if (i < ctx->num_commit_graphs_after)
2074 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2077 * If the topmost remaining layer has generation data chunk, the
2078 * resultant layer also has generation data chunk.
2080 if (i == ctx->num_commit_graphs_after - 2)
2081 ctx->write_generation_data = !!g->chunk_generation_data;
2083 i--;
2084 g = g->base_graph;
2088 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2089 struct commit_graph *g)
2091 uint32_t i;
2092 uint32_t offset = g->num_commits_in_base;
2094 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2096 for (i = 0; i < g->num_commits; i++) {
2097 struct object_id oid;
2098 struct commit *result;
2100 display_progress(ctx->progress, i + 1);
2102 load_oid_from_graph(g, i + offset, &oid);
2104 /* only add commits if they still exist in the repo */
2105 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2107 if (result) {
2108 ctx->commits.list[ctx->commits.nr] = result;
2109 ctx->commits.nr++;
2114 static int commit_compare(const void *_a, const void *_b)
2116 const struct commit *a = *(const struct commit **)_a;
2117 const struct commit *b = *(const struct commit **)_b;
2118 return oidcmp(&a->object.oid, &b->object.oid);
2121 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2123 uint32_t i, dedup_i = 0;
2125 if (ctx->report_progress)
2126 ctx->progress = start_delayed_progress(
2127 _("Scanning merged commits"),
2128 ctx->commits.nr);
2130 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2132 ctx->num_extra_edges = 0;
2133 for (i = 0; i < ctx->commits.nr; i++) {
2134 display_progress(ctx->progress, i + 1);
2136 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2137 &ctx->commits.list[i]->object.oid)) {
2139 * Silently ignore duplicates. These were likely
2140 * created due to a commit appearing in multiple
2141 * layers of the chain, which is unexpected but
2142 * not invalid. We should make sure there is a
2143 * unique copy in the new layer.
2145 } else {
2146 unsigned int num_parents;
2148 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2149 dedup_i++;
2151 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2152 if (num_parents > 2)
2153 ctx->num_extra_edges += num_parents - 1;
2157 ctx->commits.nr = dedup_i;
2159 stop_progress(&ctx->progress);
2162 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2164 struct commit_graph *g = ctx->r->objects->commit_graph;
2165 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2167 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2168 current_graph_number--;
2170 if (ctx->report_progress)
2171 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2173 merge_commit_graph(ctx, g);
2174 stop_progress(&ctx->progress);
2176 g = g->base_graph;
2179 if (g) {
2180 ctx->new_base_graph = g;
2181 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2184 if (ctx->new_base_graph)
2185 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2187 sort_and_scan_merged_commits(ctx);
2190 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2192 uint32_t i;
2193 time_t now = time(NULL);
2195 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2196 struct stat st;
2197 struct utimbuf updated_time;
2199 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2200 continue;
2202 updated_time.actime = st.st_atime;
2203 updated_time.modtime = now;
2204 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2208 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2210 struct strbuf path = STRBUF_INIT;
2211 DIR *dir;
2212 struct dirent *de;
2213 size_t dirnamelen;
2214 timestamp_t expire_time = time(NULL);
2216 if (ctx->opts && ctx->opts->expire_time)
2217 expire_time = ctx->opts->expire_time;
2218 if (!ctx->split) {
2219 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2220 unlink(chain_file_name);
2221 free(chain_file_name);
2222 ctx->num_commit_graphs_after = 0;
2225 strbuf_addstr(&path, ctx->odb->path);
2226 strbuf_addstr(&path, "/info/commit-graphs");
2227 dir = opendir(path.buf);
2229 if (!dir)
2230 goto out;
2232 strbuf_addch(&path, '/');
2233 dirnamelen = path.len;
2234 while ((de = readdir(dir)) != NULL) {
2235 struct stat st;
2236 uint32_t i, found = 0;
2238 strbuf_setlen(&path, dirnamelen);
2239 strbuf_addstr(&path, de->d_name);
2241 if (stat(path.buf, &st) < 0)
2242 continue;
2244 if (st.st_mtime > expire_time)
2245 continue;
2246 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2247 continue;
2249 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2250 if (!strcmp(ctx->commit_graph_filenames_after[i],
2251 path.buf)) {
2252 found = 1;
2253 break;
2257 if (!found)
2258 unlink(path.buf);
2261 out:
2262 strbuf_release(&path);
2265 int write_commit_graph(struct object_directory *odb,
2266 const struct string_list *const pack_indexes,
2267 struct oidset *commits,
2268 enum commit_graph_write_flags flags,
2269 const struct commit_graph_opts *opts)
2271 struct repository *r = the_repository;
2272 struct write_commit_graph_context *ctx;
2273 uint32_t i;
2274 int res = 0;
2275 int replace = 0;
2276 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2277 struct topo_level_slab topo_levels;
2279 prepare_repo_settings(r);
2280 if (!r->settings.core_commit_graph) {
2281 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2282 return 0;
2284 if (!commit_graph_compatible(r))
2285 return 0;
2287 CALLOC_ARRAY(ctx, 1);
2288 ctx->r = r;
2289 ctx->odb = odb;
2290 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2291 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2292 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2293 ctx->opts = opts;
2294 ctx->total_bloom_filter_data_size = 0;
2295 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2296 ctx->num_generation_data_overflows = 0;
2298 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2299 bloom_settings.bits_per_entry);
2300 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2301 bloom_settings.num_hashes);
2302 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2303 bloom_settings.max_changed_paths);
2304 ctx->bloom_settings = &bloom_settings;
2306 init_topo_level_slab(&topo_levels);
2307 ctx->topo_levels = &topo_levels;
2309 prepare_commit_graph(ctx->r);
2310 if (ctx->r->objects->commit_graph) {
2311 struct commit_graph *g = ctx->r->objects->commit_graph;
2313 while (g) {
2314 g->topo_levels = &topo_levels;
2315 g = g->base_graph;
2319 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2320 ctx->changed_paths = 1;
2321 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2322 struct commit_graph *g;
2324 g = ctx->r->objects->commit_graph;
2326 /* We have changed-paths already. Keep them in the next graph */
2327 if (g && g->chunk_bloom_data) {
2328 ctx->changed_paths = 1;
2329 ctx->bloom_settings = g->bloom_filter_settings;
2333 if (ctx->split) {
2334 struct commit_graph *g = ctx->r->objects->commit_graph;
2336 while (g) {
2337 ctx->num_commit_graphs_before++;
2338 g = g->base_graph;
2341 if (ctx->num_commit_graphs_before) {
2342 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2343 i = ctx->num_commit_graphs_before;
2344 g = ctx->r->objects->commit_graph;
2346 while (g) {
2347 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2348 g = g->base_graph;
2352 if (ctx->opts)
2353 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2356 ctx->approx_nr_objects = approximate_object_count();
2358 if (ctx->append && ctx->r->objects->commit_graph) {
2359 struct commit_graph *g = ctx->r->objects->commit_graph;
2360 for (i = 0; i < g->num_commits; i++) {
2361 struct object_id oid;
2362 oidread(&oid, g->chunk_oid_lookup + g->hash_len * i);
2363 oid_array_append(&ctx->oids, &oid);
2367 if (pack_indexes) {
2368 ctx->order_by_pack = 1;
2369 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2370 goto cleanup;
2373 if (commits) {
2374 if ((res = fill_oids_from_commits(ctx, commits)))
2375 goto cleanup;
2378 if (!pack_indexes && !commits) {
2379 ctx->order_by_pack = 1;
2380 fill_oids_from_all_packs(ctx);
2383 close_reachable(ctx);
2385 copy_oids_to_commits(ctx);
2387 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2388 error(_("too many commits to write graph"));
2389 res = -1;
2390 goto cleanup;
2393 if (!ctx->commits.nr && !replace)
2394 goto cleanup;
2396 if (ctx->split) {
2397 split_graph_merge_strategy(ctx);
2399 if (!replace)
2400 merge_commit_graphs(ctx);
2401 } else
2402 ctx->num_commit_graphs_after = 1;
2404 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2406 compute_topological_levels(ctx);
2407 if (ctx->write_generation_data)
2408 compute_generation_numbers(ctx);
2410 if (ctx->changed_paths)
2411 compute_bloom_filters(ctx);
2413 res = write_commit_graph_file(ctx);
2415 if (ctx->split)
2416 mark_commit_graphs(ctx);
2418 expire_commit_graphs(ctx);
2420 cleanup:
2421 free(ctx->graph_name);
2422 free(ctx->commits.list);
2423 oid_array_clear(&ctx->oids);
2424 clear_topo_level_slab(&topo_levels);
2426 if (ctx->commit_graph_filenames_after) {
2427 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2428 free(ctx->commit_graph_filenames_after[i]);
2429 free(ctx->commit_graph_hash_after[i]);
2432 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2433 free(ctx->commit_graph_filenames_before[i]);
2435 free(ctx->commit_graph_filenames_after);
2436 free(ctx->commit_graph_filenames_before);
2437 free(ctx->commit_graph_hash_after);
2440 free(ctx);
2442 return res;
2445 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2446 static int verify_commit_graph_error;
2448 __attribute__((format (printf, 1, 2)))
2449 static void graph_report(const char *fmt, ...)
2451 va_list ap;
2453 verify_commit_graph_error = 1;
2454 va_start(ap, fmt);
2455 vfprintf(stderr, fmt, ap);
2456 fprintf(stderr, "\n");
2457 va_end(ap);
2460 #define GENERATION_ZERO_EXISTS 1
2461 #define GENERATION_NUMBER_EXISTS 2
2463 static int commit_graph_checksum_valid(struct commit_graph *g)
2465 return hashfile_checksum_valid(g->data, g->data_len);
2468 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2470 uint32_t i, cur_fanout_pos = 0;
2471 struct object_id prev_oid, cur_oid;
2472 int generation_zero = 0;
2473 struct progress *progress = NULL;
2474 int local_error = 0;
2476 if (!g) {
2477 graph_report("no commit-graph file loaded");
2478 return 1;
2481 verify_commit_graph_error = verify_commit_graph_lite(g);
2482 if (verify_commit_graph_error)
2483 return verify_commit_graph_error;
2485 if (!commit_graph_checksum_valid(g)) {
2486 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2487 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2490 for (i = 0; i < g->num_commits; i++) {
2491 struct commit *graph_commit;
2493 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2495 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2496 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2497 oid_to_hex(&prev_oid),
2498 oid_to_hex(&cur_oid));
2500 oidcpy(&prev_oid, &cur_oid);
2502 while (cur_oid.hash[0] > cur_fanout_pos) {
2503 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2505 if (i != fanout_value)
2506 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2507 cur_fanout_pos, fanout_value, i);
2508 cur_fanout_pos++;
2511 graph_commit = lookup_commit(r, &cur_oid);
2512 if (!parse_commit_in_graph_one(r, g, graph_commit))
2513 graph_report(_("failed to parse commit %s from commit-graph"),
2514 oid_to_hex(&cur_oid));
2517 while (cur_fanout_pos < 256) {
2518 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2520 if (g->num_commits != fanout_value)
2521 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2522 cur_fanout_pos, fanout_value, i);
2524 cur_fanout_pos++;
2527 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2528 return verify_commit_graph_error;
2530 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2531 progress = start_progress(_("Verifying commits in commit graph"),
2532 g->num_commits);
2534 for (i = 0; i < g->num_commits; i++) {
2535 struct commit *graph_commit, *odb_commit;
2536 struct commit_list *graph_parents, *odb_parents;
2537 timestamp_t max_generation = 0;
2538 timestamp_t generation;
2540 display_progress(progress, i + 1);
2541 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2543 graph_commit = lookup_commit(r, &cur_oid);
2544 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2545 if (parse_commit_internal(odb_commit, 0, 0)) {
2546 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2547 oid_to_hex(&cur_oid));
2548 continue;
2551 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2552 get_commit_tree_oid(odb_commit)))
2553 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2554 oid_to_hex(&cur_oid),
2555 oid_to_hex(get_commit_tree_oid(graph_commit)),
2556 oid_to_hex(get_commit_tree_oid(odb_commit)));
2558 graph_parents = graph_commit->parents;
2559 odb_parents = odb_commit->parents;
2561 while (graph_parents) {
2562 if (!odb_parents) {
2563 graph_report(_("commit-graph parent list for commit %s is too long"),
2564 oid_to_hex(&cur_oid));
2565 break;
2568 /* parse parent in case it is in a base graph */
2569 parse_commit_in_graph_one(r, g, graph_parents->item);
2571 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2572 graph_report(_("commit-graph parent for %s is %s != %s"),
2573 oid_to_hex(&cur_oid),
2574 oid_to_hex(&graph_parents->item->object.oid),
2575 oid_to_hex(&odb_parents->item->object.oid));
2577 generation = commit_graph_generation(graph_parents->item);
2578 if (generation > max_generation)
2579 max_generation = generation;
2581 graph_parents = graph_parents->next;
2582 odb_parents = odb_parents->next;
2585 if (odb_parents)
2586 graph_report(_("commit-graph parent list for commit %s terminates early"),
2587 oid_to_hex(&cur_oid));
2589 if (!commit_graph_generation(graph_commit)) {
2590 if (generation_zero == GENERATION_NUMBER_EXISTS)
2591 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2592 oid_to_hex(&cur_oid));
2593 generation_zero = GENERATION_ZERO_EXISTS;
2594 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2595 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2596 oid_to_hex(&cur_oid));
2598 if (generation_zero == GENERATION_ZERO_EXISTS)
2599 continue;
2602 * If we are using topological level and one of our parents has
2603 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2604 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2605 * in the following condition.
2607 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2608 max_generation--;
2610 generation = commit_graph_generation(graph_commit);
2611 if (generation < max_generation + 1)
2612 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2613 oid_to_hex(&cur_oid),
2614 generation,
2615 max_generation + 1);
2617 if (graph_commit->date != odb_commit->date)
2618 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2619 oid_to_hex(&cur_oid),
2620 graph_commit->date,
2621 odb_commit->date);
2623 stop_progress(&progress);
2625 local_error = verify_commit_graph_error;
2627 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2628 local_error |= verify_commit_graph(r, g->base_graph, flags);
2630 return local_error;
2633 void free_commit_graph(struct commit_graph *g)
2635 if (!g)
2636 return;
2637 if (g->data) {
2638 munmap((void *)g->data, g->data_len);
2639 g->data = NULL;
2641 free(g->filename);
2642 free(g->bloom_filter_settings);
2643 free(g);
2646 void disable_commit_graph(struct repository *r)
2648 r->commit_graph_disabled = 1;