Git 2.32
[git] / commit-graph.c
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"
23
24 void git_test_write_commit_graph_or_die(void)
25 {
26         int flags = 0;
27         if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
28                 return;
29
30         if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
31                 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
32
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");
36 }
37
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 0x47444154 /* "GDAT" */
43 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f56 /* "GDOV" */
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" */
48
49 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
50
51 #define GRAPH_VERSION_1 0x1
52 #define GRAPH_VERSION GRAPH_VERSION_1
53
54 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
55 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
56 #define GRAPH_PARENT_NONE 0x70000000
57
58 #define GRAPH_LAST_EDGE 0x80000000
59
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)
64
65 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
66
67 /* Remember to update object flag allocation in object.h */
68 #define REACHABLE       (1u<<15)
69
70 define_commit_slab(topo_level_slab, uint32_t);
71
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);
75
76 static void set_commit_pos(struct repository *r, const struct object_id *oid)
77 {
78         static int32_t max_pos;
79         struct commit *commit = lookup_commit(r, oid);
80
81         if (!commit)
82                 return; /* should never happen, but be lenient */
83
84         *commit_pos_at(&commit_pos, commit) = max_pos++;
85 }
86
87 static int commit_pos_cmp(const void *va, const void *vb)
88 {
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);
93 }
94
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);
98
99 static int get_configured_generation_version(struct repository *r)
100 {
101         int version = 2;
102         repo_config_get_int(r, "commitgraph.generationversion", &version);
103         return version;
104 }
105
106 uint32_t commit_graph_position(const struct commit *c)
107 {
108         struct commit_graph_data *data =
109                 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
110
111         return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
112 }
113
114 timestamp_t commit_graph_generation(const struct commit *c)
115 {
116         struct commit_graph_data *data =
117                 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
118
119         if (!data)
120                 return GENERATION_NUMBER_INFINITY;
121         else if (data->graph_pos == COMMIT_NOT_FROM_GRAPH)
122                 return GENERATION_NUMBER_INFINITY;
123
124         return data->generation;
125 }
126
127 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
128 {
129         unsigned int i, nth_slab;
130         struct commit_graph_data *data =
131                 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
132
133         if (data)
134                 return data;
135
136         nth_slab = c->index / commit_graph_data_slab.slab_size;
137         data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
138
139         /*
140          * commit-slab initializes elements with zero, overwrite this with
141          * COMMIT_NOT_FROM_GRAPH for graph_pos.
142          *
143          * We avoid initializing generation with checking if graph position
144          * is not COMMIT_NOT_FROM_GRAPH.
145          */
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;
149         }
150
151         return data;
152 }
153
154 /*
155  * Should be used only while writing commit-graph as it compares
156  * generation value of commits by directly accessing commit-slab.
157  */
158 static int commit_gen_cmp(const void *va, const void *vb)
159 {
160         const struct commit *a = *(const struct commit **)va;
161         const struct commit *b = *(const struct commit **)vb;
162
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;
170
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;
177 }
178
179 char *get_commit_graph_filename(struct object_directory *obj_dir)
180 {
181         return xstrfmt("%s/info/commit-graph", obj_dir->path);
182 }
183
184 static char *get_split_graph_filename(struct object_directory *odb,
185                                       const char *oid_hex)
186 {
187         return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
188                        oid_hex);
189 }
190
191 char *get_commit_graph_chain_filename(struct object_directory *odb)
192 {
193         return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
194 }
195
196 static uint8_t oid_version(void)
197 {
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"));
205         }
206 }
207
208 static struct commit_graph *alloc_commit_graph(void)
209 {
210         struct commit_graph *g = xcalloc(1, sizeof(*g));
211
212         return g;
213 }
214
215 extern int read_replace_refs;
216
217 static int commit_graph_compatible(struct repository *r)
218 {
219         if (!r->gitdir)
220                 return 0;
221
222         if (read_replace_refs) {
223                 prepare_replace_object(r);
224                 if (hashmap_get_size(&r->objects->replace_map->map))
225                         return 0;
226         }
227
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;
234
235         return 1;
236 }
237
238 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
239 {
240         *fd = git_open(graph_file);
241         if (*fd < 0)
242                 return 0;
243         if (fstat(*fd, st)) {
244                 close(*fd);
245                 return 0;
246         }
247         return 1;
248 }
249
250 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
251                                                  int fd, struct stat *st,
252                                                  struct object_directory *odb)
253 {
254         void *graph_map;
255         size_t graph_size;
256         struct commit_graph *ret;
257
258         graph_size = xsize_t(st->st_size);
259
260         if (graph_size < GRAPH_MIN_SIZE) {
261                 close(fd);
262                 error(_("commit-graph file is too small"));
263                 return NULL;
264         }
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);
268
269         if (ret)
270                 ret->odb = odb;
271         else
272                 munmap(graph_map, graph_size);
273
274         return ret;
275 }
276
277 static int verify_commit_graph_lite(struct commit_graph *g)
278 {
279         /*
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".
284          *
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.
290          */
291         if (!g->chunk_oid_fanout) {
292                 error("commit-graph is missing the OID Fanout chunk");
293                 return 1;
294         }
295         if (!g->chunk_oid_lookup) {
296                 error("commit-graph is missing the OID Lookup chunk");
297                 return 1;
298         }
299         if (!g->chunk_commit_data) {
300                 error("commit-graph is missing the Commit Data chunk");
301                 return 1;
302         }
303
304         return 0;
305 }
306
307 static int graph_read_oid_lookup(const unsigned char *chunk_start,
308                                  size_t chunk_size, void *data)
309 {
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;
314 }
315
316 static int graph_read_bloom_data(const unsigned char *chunk_start,
317                                   size_t chunk_size, void *data)
318 {
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);
323
324         if (hash_version != 1)
325                 return 0;
326
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;
332
333         return 0;
334 }
335
336 struct commit_graph *parse_commit_graph(struct repository *r,
337                                         void *graph_map, size_t graph_size)
338 {
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;
344
345         if (!graph_map)
346                 return NULL;
347
348         if (graph_size < GRAPH_MIN_SIZE)
349                 return NULL;
350
351         data = (const unsigned char *)graph_map;
352
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;
358         }
359
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;
365         }
366
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;
372         }
373
374         prepare_repo_settings(r);
375
376         graph = alloc_commit_graph();
377
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;
382
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;
390         }
391
392         cf = init_chunkfile(NULL);
393
394         if (read_table_of_contents(cf, graph->data, graph_size,
395                                    GRAPH_HEADER_SIZE, graph->num_chunks))
396                 goto free_and_return;
397
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);
404
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);
410         }
411
412         if (r->settings.commit_graph_read_changed_paths) {
413                 pair_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
414                            &graph->chunk_bloom_indexes);
415                 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
416                            graph_read_bloom_data, graph);
417         }
418
419         if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
420                 init_bloom_filters();
421         } else {
422                 /* We need both the bloom chunks to exist together. Else ignore the data */
423                 graph->chunk_bloom_indexes = NULL;
424                 graph->chunk_bloom_data = NULL;
425                 FREE_AND_NULL(graph->bloom_filter_settings);
426         }
427
428         oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
429
430         if (verify_commit_graph_lite(graph))
431                 goto free_and_return;
432
433         free_chunkfile(cf);
434         return graph;
435
436 free_and_return:
437         free_chunkfile(cf);
438         free(graph->bloom_filter_settings);
439         free(graph);
440         return NULL;
441 }
442
443 static struct commit_graph *load_commit_graph_one(struct repository *r,
444                                                   const char *graph_file,
445                                                   struct object_directory *odb)
446 {
447
448         struct stat st;
449         int fd;
450         struct commit_graph *g;
451         int open_ok = open_commit_graph(graph_file, &fd, &st);
452
453         if (!open_ok)
454                 return NULL;
455
456         g = load_commit_graph_one_fd_st(r, fd, &st, odb);
457
458         if (g)
459                 g->filename = xstrdup(graph_file);
460
461         return g;
462 }
463
464 static struct commit_graph *load_commit_graph_v1(struct repository *r,
465                                                  struct object_directory *odb)
466 {
467         char *graph_name = get_commit_graph_filename(odb);
468         struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
469         free(graph_name);
470
471         return g;
472 }
473
474 static int add_graph_to_chain(struct commit_graph *g,
475                               struct commit_graph *chain,
476                               struct object_id *oids,
477                               int n)
478 {
479         struct commit_graph *cur_g = chain;
480
481         if (n && !g->chunk_base_graphs) {
482                 warning(_("commit-graph has no base graphs chunk"));
483                 return 0;
484         }
485
486         while (n) {
487                 n--;
488
489                 if (!cur_g ||
490                     !oideq(&oids[n], &cur_g->oid) ||
491                     !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
492                         warning(_("commit-graph chain does not match"));
493                         return 0;
494                 }
495
496                 cur_g = cur_g->base_graph;
497         }
498
499         g->base_graph = chain;
500
501         if (chain)
502                 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
503
504         return 1;
505 }
506
507 static struct commit_graph *load_commit_graph_chain(struct repository *r,
508                                                     struct object_directory *odb)
509 {
510         struct commit_graph *graph_chain = NULL;
511         struct strbuf line = STRBUF_INIT;
512         struct stat st;
513         struct object_id *oids;
514         int i = 0, valid = 1, count;
515         char *chain_name = get_commit_graph_chain_filename(odb);
516         FILE *fp;
517         int stat_res;
518
519         fp = fopen(chain_name, "r");
520         stat_res = stat(chain_name, &st);
521         free(chain_name);
522
523         if (!fp ||
524             stat_res ||
525             st.st_size <= the_hash_algo->hexsz)
526                 return NULL;
527
528         count = st.st_size / (the_hash_algo->hexsz + 1);
529         CALLOC_ARRAY(oids, count);
530
531         prepare_alt_odb(r);
532
533         for (i = 0; i < count; i++) {
534                 struct object_directory *odb;
535
536                 if (strbuf_getline_lf(&line, fp) == EOF)
537                         break;
538
539                 if (get_oid_hex(line.buf, &oids[i])) {
540                         warning(_("invalid commit-graph chain: line '%s' not a hash"),
541                                 line.buf);
542                         valid = 0;
543                         break;
544                 }
545
546                 valid = 0;
547                 for (odb = r->objects->odb; odb; odb = odb->next) {
548                         char *graph_name = get_split_graph_filename(odb, line.buf);
549                         struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
550
551                         free(graph_name);
552
553                         if (g) {
554                                 if (add_graph_to_chain(g, graph_chain, oids, i)) {
555                                         graph_chain = g;
556                                         valid = 1;
557                                 }
558
559                                 break;
560                         }
561                 }
562
563                 if (!valid) {
564                         warning(_("unable to find all commit-graph files"));
565                         break;
566                 }
567         }
568
569         free(oids);
570         fclose(fp);
571         strbuf_release(&line);
572
573         return graph_chain;
574 }
575
576 /*
577  * returns 1 if and only if all graphs in the chain have
578  * corrected commit dates stored in the generation_data chunk.
579  */
580 static int validate_mixed_generation_chain(struct commit_graph *g)
581 {
582         int read_generation_data = 1;
583         struct commit_graph *p = g;
584
585         while (read_generation_data && p) {
586                 read_generation_data = p->read_generation_data;
587                 p = p->base_graph;
588         }
589
590         if (read_generation_data)
591                 return 1;
592
593         while (g) {
594                 g->read_generation_data = 0;
595                 g = g->base_graph;
596         }
597
598         return 0;
599 }
600
601 struct commit_graph *read_commit_graph_one(struct repository *r,
602                                            struct object_directory *odb)
603 {
604         struct commit_graph *g = load_commit_graph_v1(r, odb);
605
606         if (!g)
607                 g = load_commit_graph_chain(r, odb);
608
609         validate_mixed_generation_chain(g);
610
611         return g;
612 }
613
614 static void prepare_commit_graph_one(struct repository *r,
615                                      struct object_directory *odb)
616 {
617
618         if (r->objects->commit_graph)
619                 return;
620
621         r->objects->commit_graph = read_commit_graph_one(r, odb);
622 }
623
624 /*
625  * Return 1 if commit_graph is non-NULL, and 0 otherwise.
626  *
627  * On the first invocation, this function attempts to load the commit
628  * graph if the_repository is configured to have one.
629  */
630 static int prepare_commit_graph(struct repository *r)
631 {
632         struct object_directory *odb;
633
634         /*
635          * This must come before the "already attempted?" check below, because
636          * we want to disable even an already-loaded graph file.
637          */
638         if (r->commit_graph_disabled)
639                 return 0;
640
641         if (r->objects->commit_graph_attempted)
642                 return !!r->objects->commit_graph;
643         r->objects->commit_graph_attempted = 1;
644
645         prepare_repo_settings(r);
646
647         if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
648             r->settings.core_commit_graph != 1)
649                 /*
650                  * This repository is not configured to use commit graphs, so
651                  * do not load one. (But report commit_graph_attempted anyway
652                  * so that commit graph loading is not attempted again for this
653                  * repository.)
654                  */
655                 return 0;
656
657         if (!commit_graph_compatible(r))
658                 return 0;
659
660         prepare_alt_odb(r);
661         for (odb = r->objects->odb;
662              !r->objects->commit_graph && odb;
663              odb = odb->next)
664                 prepare_commit_graph_one(r, odb);
665         return !!r->objects->commit_graph;
666 }
667
668 int generation_numbers_enabled(struct repository *r)
669 {
670         uint32_t first_generation;
671         struct commit_graph *g;
672         if (!prepare_commit_graph(r))
673                return 0;
674
675         g = r->objects->commit_graph;
676
677         if (!g->num_commits)
678                 return 0;
679
680         first_generation = get_be32(g->chunk_commit_data +
681                                     g->hash_len + 8) >> 2;
682
683         return !!first_generation;
684 }
685
686 int corrected_commit_dates_enabled(struct repository *r)
687 {
688         struct commit_graph *g;
689         if (!prepare_commit_graph(r))
690                 return 0;
691
692         g = r->objects->commit_graph;
693
694         if (!g->num_commits)
695                 return 0;
696
697         return g->read_generation_data;
698 }
699
700 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
701 {
702         struct commit_graph *g = r->objects->commit_graph;
703         while (g) {
704                 if (g->bloom_filter_settings)
705                         return g->bloom_filter_settings;
706                 g = g->base_graph;
707         }
708         return NULL;
709 }
710
711 static void close_commit_graph_one(struct commit_graph *g)
712 {
713         if (!g)
714                 return;
715
716         close_commit_graph_one(g->base_graph);
717         free_commit_graph(g);
718 }
719
720 void close_commit_graph(struct raw_object_store *o)
721 {
722         close_commit_graph_one(o->commit_graph);
723         o->commit_graph = NULL;
724 }
725
726 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
727 {
728         return bsearch_hash(oid->hash, g->chunk_oid_fanout,
729                             g->chunk_oid_lookup, g->hash_len, pos);
730 }
731
732 static void load_oid_from_graph(struct commit_graph *g,
733                                 uint32_t pos,
734                                 struct object_id *oid)
735 {
736         uint32_t lex_index;
737
738         while (g && pos < g->num_commits_in_base)
739                 g = g->base_graph;
740
741         if (!g)
742                 BUG("NULL commit-graph");
743
744         if (pos >= g->num_commits + g->num_commits_in_base)
745                 die(_("invalid commit position. commit-graph is likely corrupt"));
746
747         lex_index = pos - g->num_commits_in_base;
748
749         oidread(oid, g->chunk_oid_lookup + g->hash_len * lex_index);
750 }
751
752 static struct commit_list **insert_parent_or_die(struct repository *r,
753                                                  struct commit_graph *g,
754                                                  uint32_t pos,
755                                                  struct commit_list **pptr)
756 {
757         struct commit *c;
758         struct object_id oid;
759
760         if (pos >= g->num_commits + g->num_commits_in_base)
761                 die("invalid parent position %"PRIu32, pos);
762
763         load_oid_from_graph(g, pos, &oid);
764         c = lookup_commit(r, &oid);
765         if (!c)
766                 die(_("could not find commit %s"), oid_to_hex(&oid));
767         commit_graph_data_at(c)->graph_pos = pos;
768         return &commit_list_insert(c, pptr)->next;
769 }
770
771 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
772 {
773         const unsigned char *commit_data;
774         struct commit_graph_data *graph_data;
775         uint32_t lex_index, offset_pos;
776         uint64_t date_high, date_low, offset;
777
778         while (pos < g->num_commits_in_base)
779                 g = g->base_graph;
780
781         if (pos >= g->num_commits + g->num_commits_in_base)
782                 die(_("invalid commit position. commit-graph is likely corrupt"));
783
784         lex_index = pos - g->num_commits_in_base;
785         commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
786
787         graph_data = commit_graph_data_at(item);
788         graph_data->graph_pos = pos;
789
790         date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
791         date_low = get_be32(commit_data + g->hash_len + 12);
792         item->date = (timestamp_t)((date_high << 32) | date_low);
793
794         if (g->read_generation_data) {
795                 offset = (timestamp_t)get_be32(g->chunk_generation_data + sizeof(uint32_t) * lex_index);
796
797                 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
798                         if (!g->chunk_generation_data_overflow)
799                                 die(_("commit-graph requires overflow generation data but has none"));
800
801                         offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
802                         graph_data->generation = get_be64(g->chunk_generation_data_overflow + 8 * offset_pos);
803                 } else
804                         graph_data->generation = item->date + offset;
805         } else
806                 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
807
808         if (g->topo_levels)
809                 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
810 }
811
812 static inline void set_commit_tree(struct commit *c, struct tree *t)
813 {
814         c->maybe_tree = t;
815 }
816
817 static int fill_commit_in_graph(struct repository *r,
818                                 struct commit *item,
819                                 struct commit_graph *g, uint32_t pos)
820 {
821         uint32_t edge_value;
822         uint32_t *parent_data_ptr;
823         struct commit_list **pptr;
824         const unsigned char *commit_data;
825         uint32_t lex_index;
826
827         while (pos < g->num_commits_in_base)
828                 g = g->base_graph;
829
830         fill_commit_graph_info(item, g, pos);
831
832         lex_index = pos - g->num_commits_in_base;
833         commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
834
835         item->object.parsed = 1;
836
837         set_commit_tree(item, NULL);
838
839         pptr = &item->parents;
840
841         edge_value = get_be32(commit_data + g->hash_len);
842         if (edge_value == GRAPH_PARENT_NONE)
843                 return 1;
844         pptr = insert_parent_or_die(r, g, edge_value, pptr);
845
846         edge_value = get_be32(commit_data + g->hash_len + 4);
847         if (edge_value == GRAPH_PARENT_NONE)
848                 return 1;
849         if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
850                 pptr = insert_parent_or_die(r, g, edge_value, pptr);
851                 return 1;
852         }
853
854         parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
855                           4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
856         do {
857                 edge_value = get_be32(parent_data_ptr);
858                 pptr = insert_parent_or_die(r, g,
859                                             edge_value & GRAPH_EDGE_LAST_MASK,
860                                             pptr);
861                 parent_data_ptr++;
862         } while (!(edge_value & GRAPH_LAST_EDGE));
863
864         return 1;
865 }
866
867 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
868 {
869         uint32_t graph_pos = commit_graph_position(item);
870         if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
871                 *pos = graph_pos;
872                 return 1;
873         } else {
874                 struct commit_graph *cur_g = g;
875                 uint32_t lex_index;
876
877                 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
878                         cur_g = cur_g->base_graph;
879
880                 if (cur_g) {
881                         *pos = lex_index + cur_g->num_commits_in_base;
882                         return 1;
883                 }
884
885                 return 0;
886         }
887 }
888
889 static int parse_commit_in_graph_one(struct repository *r,
890                                      struct commit_graph *g,
891                                      struct commit *item)
892 {
893         uint32_t pos;
894
895         if (item->object.parsed)
896                 return 1;
897
898         if (find_commit_in_graph(item, g, &pos))
899                 return fill_commit_in_graph(r, item, g, pos);
900
901         return 0;
902 }
903
904 int parse_commit_in_graph(struct repository *r, struct commit *item)
905 {
906         static int checked_env = 0;
907
908         if (!checked_env &&
909             git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
910                 die("dying as requested by the '%s' variable on commit-graph parse!",
911                     GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
912         checked_env = 1;
913
914         if (!prepare_commit_graph(r))
915                 return 0;
916         return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
917 }
918
919 void load_commit_graph_info(struct repository *r, struct commit *item)
920 {
921         uint32_t pos;
922         if (!prepare_commit_graph(r))
923                 return;
924         if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
925                 fill_commit_graph_info(item, r->objects->commit_graph, pos);
926 }
927
928 static struct tree *load_tree_for_commit(struct repository *r,
929                                          struct commit_graph *g,
930                                          struct commit *c)
931 {
932         struct object_id oid;
933         const unsigned char *commit_data;
934         uint32_t graph_pos = commit_graph_position(c);
935
936         while (graph_pos < g->num_commits_in_base)
937                 g = g->base_graph;
938
939         commit_data = g->chunk_commit_data +
940                         GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base);
941
942         oidread(&oid, commit_data);
943         set_commit_tree(c, lookup_tree(r, &oid));
944
945         return c->maybe_tree;
946 }
947
948 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
949                                                  struct commit_graph *g,
950                                                  const struct commit *c)
951 {
952         if (c->maybe_tree)
953                 return c->maybe_tree;
954         if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
955                 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
956
957         return load_tree_for_commit(r, g, (struct commit *)c);
958 }
959
960 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
961 {
962         return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
963 }
964
965 struct packed_commit_list {
966         struct commit **list;
967         size_t nr;
968         size_t alloc;
969 };
970
971 struct write_commit_graph_context {
972         struct repository *r;
973         struct object_directory *odb;
974         char *graph_name;
975         struct oid_array oids;
976         struct packed_commit_list commits;
977         int num_extra_edges;
978         int num_generation_data_overflows;
979         unsigned long approx_nr_objects;
980         struct progress *progress;
981         int progress_done;
982         uint64_t progress_cnt;
983
984         char *base_graph_name;
985         int num_commit_graphs_before;
986         int num_commit_graphs_after;
987         char **commit_graph_filenames_before;
988         char **commit_graph_filenames_after;
989         char **commit_graph_hash_after;
990         uint32_t new_num_commits_in_base;
991         struct commit_graph *new_base_graph;
992
993         unsigned append:1,
994                  report_progress:1,
995                  split:1,
996                  changed_paths:1,
997                  order_by_pack:1,
998                  write_generation_data:1,
999                  trust_generation_numbers:1;
1000
1001         struct topo_level_slab *topo_levels;
1002         const struct commit_graph_opts *opts;
1003         size_t total_bloom_filter_data_size;
1004         const struct bloom_filter_settings *bloom_settings;
1005
1006         int count_bloom_filter_computed;
1007         int count_bloom_filter_not_computed;
1008         int count_bloom_filter_trunc_empty;
1009         int count_bloom_filter_trunc_large;
1010 };
1011
1012 static int write_graph_chunk_fanout(struct hashfile *f,
1013                                     void *data)
1014 {
1015         struct write_commit_graph_context *ctx = data;
1016         int i, count = 0;
1017         struct commit **list = ctx->commits.list;
1018
1019         /*
1020          * Write the first-level table (the list is sorted,
1021          * but we use a 256-entry lookup to be able to avoid
1022          * having to do eight extra binary search iterations).
1023          */
1024         for (i = 0; i < 256; i++) {
1025                 while (count < ctx->commits.nr) {
1026                         if ((*list)->object.oid.hash[0] != i)
1027                                 break;
1028                         display_progress(ctx->progress, ++ctx->progress_cnt);
1029                         count++;
1030                         list++;
1031                 }
1032
1033                 hashwrite_be32(f, count);
1034         }
1035
1036         return 0;
1037 }
1038
1039 static int write_graph_chunk_oids(struct hashfile *f,
1040                                   void *data)
1041 {
1042         struct write_commit_graph_context *ctx = data;
1043         struct commit **list = ctx->commits.list;
1044         int count;
1045         for (count = 0; count < ctx->commits.nr; count++, list++) {
1046                 display_progress(ctx->progress, ++ctx->progress_cnt);
1047                 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1048         }
1049
1050         return 0;
1051 }
1052
1053 static const struct object_id *commit_to_oid(size_t index, const void *table)
1054 {
1055         const struct commit * const *commits = table;
1056         return &commits[index]->object.oid;
1057 }
1058
1059 static int write_graph_chunk_data(struct hashfile *f,
1060                                   void *data)
1061 {
1062         struct write_commit_graph_context *ctx = data;
1063         struct commit **list = ctx->commits.list;
1064         struct commit **last = ctx->commits.list + ctx->commits.nr;
1065         uint32_t num_extra_edges = 0;
1066
1067         while (list < last) {
1068                 struct commit_list *parent;
1069                 struct object_id *tree;
1070                 int edge_value;
1071                 uint32_t packedDate[2];
1072                 display_progress(ctx->progress, ++ctx->progress_cnt);
1073
1074                 if (repo_parse_commit_no_graph(ctx->r, *list))
1075                         die(_("unable to parse commit %s"),
1076                                 oid_to_hex(&(*list)->object.oid));
1077                 tree = get_commit_tree_oid(*list);
1078                 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1079
1080                 parent = (*list)->parents;
1081
1082                 if (!parent)
1083                         edge_value = GRAPH_PARENT_NONE;
1084                 else {
1085                         edge_value = oid_pos(&parent->item->object.oid,
1086                                              ctx->commits.list,
1087                                              ctx->commits.nr,
1088                                              commit_to_oid);
1089
1090                         if (edge_value >= 0)
1091                                 edge_value += ctx->new_num_commits_in_base;
1092                         else if (ctx->new_base_graph) {
1093                                 uint32_t pos;
1094                                 if (find_commit_in_graph(parent->item,
1095                                                          ctx->new_base_graph,
1096                                                          &pos))
1097                                         edge_value = pos;
1098                         }
1099
1100                         if (edge_value < 0)
1101                                 BUG("missing parent %s for commit %s",
1102                                     oid_to_hex(&parent->item->object.oid),
1103                                     oid_to_hex(&(*list)->object.oid));
1104                 }
1105
1106                 hashwrite_be32(f, edge_value);
1107
1108                 if (parent)
1109                         parent = parent->next;
1110
1111                 if (!parent)
1112                         edge_value = GRAPH_PARENT_NONE;
1113                 else if (parent->next)
1114                         edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1115                 else {
1116                         edge_value = oid_pos(&parent->item->object.oid,
1117                                              ctx->commits.list,
1118                                              ctx->commits.nr,
1119                                              commit_to_oid);
1120
1121                         if (edge_value >= 0)
1122                                 edge_value += ctx->new_num_commits_in_base;
1123                         else if (ctx->new_base_graph) {
1124                                 uint32_t pos;
1125                                 if (find_commit_in_graph(parent->item,
1126                                                          ctx->new_base_graph,
1127                                                          &pos))
1128                                         edge_value = pos;
1129                         }
1130
1131                         if (edge_value < 0)
1132                                 BUG("missing parent %s for commit %s",
1133                                     oid_to_hex(&parent->item->object.oid),
1134                                     oid_to_hex(&(*list)->object.oid));
1135                 }
1136
1137                 hashwrite_be32(f, edge_value);
1138
1139                 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1140                         do {
1141                                 num_extra_edges++;
1142                                 parent = parent->next;
1143                         } while (parent);
1144                 }
1145
1146                 if (sizeof((*list)->date) > 4)
1147                         packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1148                 else
1149                         packedDate[0] = 0;
1150
1151                 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1152
1153                 packedDate[1] = htonl((*list)->date);
1154                 hashwrite(f, packedDate, 8);
1155
1156                 list++;
1157         }
1158
1159         return 0;
1160 }
1161
1162 static int write_graph_chunk_generation_data(struct hashfile *f,
1163                                              void *data)
1164 {
1165         struct write_commit_graph_context *ctx = data;
1166         int i, num_generation_data_overflows = 0;
1167
1168         for (i = 0; i < ctx->commits.nr; i++) {
1169                 struct commit *c = ctx->commits.list[i];
1170                 timestamp_t offset;
1171                 repo_parse_commit(ctx->r, c);
1172                 offset = commit_graph_data_at(c)->generation - c->date;
1173                 display_progress(ctx->progress, ++ctx->progress_cnt);
1174
1175                 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1176                         offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1177                         num_generation_data_overflows++;
1178                 }
1179
1180                 hashwrite_be32(f, offset);
1181         }
1182
1183         return 0;
1184 }
1185
1186 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1187                                                       void *data)
1188 {
1189         struct write_commit_graph_context *ctx = data;
1190         int i;
1191         for (i = 0; i < ctx->commits.nr; i++) {
1192                 struct commit *c = ctx->commits.list[i];
1193                 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1194                 display_progress(ctx->progress, ++ctx->progress_cnt);
1195
1196                 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1197                         hashwrite_be32(f, offset >> 32);
1198                         hashwrite_be32(f, (uint32_t) offset);
1199                 }
1200         }
1201
1202         return 0;
1203 }
1204
1205 static int write_graph_chunk_extra_edges(struct hashfile *f,
1206                                          void *data)
1207 {
1208         struct write_commit_graph_context *ctx = data;
1209         struct commit **list = ctx->commits.list;
1210         struct commit **last = ctx->commits.list + ctx->commits.nr;
1211         struct commit_list *parent;
1212
1213         while (list < last) {
1214                 int num_parents = 0;
1215
1216                 display_progress(ctx->progress, ++ctx->progress_cnt);
1217
1218                 for (parent = (*list)->parents; num_parents < 3 && parent;
1219                      parent = parent->next)
1220                         num_parents++;
1221
1222                 if (num_parents <= 2) {
1223                         list++;
1224                         continue;
1225                 }
1226
1227                 /* Since num_parents > 2, this initializer is safe. */
1228                 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1229                         int edge_value = oid_pos(&parent->item->object.oid,
1230                                                  ctx->commits.list,
1231                                                  ctx->commits.nr,
1232                                                  commit_to_oid);
1233
1234                         if (edge_value >= 0)
1235                                 edge_value += ctx->new_num_commits_in_base;
1236                         else if (ctx->new_base_graph) {
1237                                 uint32_t pos;
1238                                 if (find_commit_in_graph(parent->item,
1239                                                          ctx->new_base_graph,
1240                                                          &pos))
1241                                         edge_value = pos;
1242                         }
1243
1244                         if (edge_value < 0)
1245                                 BUG("missing parent %s for commit %s",
1246                                     oid_to_hex(&parent->item->object.oid),
1247                                     oid_to_hex(&(*list)->object.oid));
1248                         else if (!parent->next)
1249                                 edge_value |= GRAPH_LAST_EDGE;
1250
1251                         hashwrite_be32(f, edge_value);
1252                 }
1253
1254                 list++;
1255         }
1256
1257         return 0;
1258 }
1259
1260 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1261                                            void *data)
1262 {
1263         struct write_commit_graph_context *ctx = data;
1264         struct commit **list = ctx->commits.list;
1265         struct commit **last = ctx->commits.list + ctx->commits.nr;
1266         uint32_t cur_pos = 0;
1267
1268         while (list < last) {
1269                 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1270                 size_t len = filter ? filter->len : 0;
1271                 cur_pos += len;
1272                 display_progress(ctx->progress, ++ctx->progress_cnt);
1273                 hashwrite_be32(f, cur_pos);
1274                 list++;
1275         }
1276
1277         return 0;
1278 }
1279
1280 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1281 {
1282         struct json_writer jw = JSON_WRITER_INIT;
1283
1284         jw_object_begin(&jw, 0);
1285         jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1286         jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1287         jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1288         jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1289         jw_end(&jw);
1290
1291         trace2_data_json("bloom", ctx->r, "settings", &jw);
1292
1293         jw_release(&jw);
1294 }
1295
1296 static int write_graph_chunk_bloom_data(struct hashfile *f,
1297                                         void *data)
1298 {
1299         struct write_commit_graph_context *ctx = data;
1300         struct commit **list = ctx->commits.list;
1301         struct commit **last = ctx->commits.list + ctx->commits.nr;
1302
1303         trace2_bloom_filter_settings(ctx);
1304
1305         hashwrite_be32(f, ctx->bloom_settings->hash_version);
1306         hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1307         hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1308
1309         while (list < last) {
1310                 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1311                 size_t len = filter ? filter->len : 0;
1312
1313                 display_progress(ctx->progress, ++ctx->progress_cnt);
1314                 if (len)
1315                         hashwrite(f, filter->data, len * sizeof(unsigned char));
1316                 list++;
1317         }
1318
1319         return 0;
1320 }
1321
1322 static int add_packed_commits(const struct object_id *oid,
1323                               struct packed_git *pack,
1324                               uint32_t pos,
1325                               void *data)
1326 {
1327         struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1328         enum object_type type;
1329         off_t offset = nth_packed_object_offset(pack, pos);
1330         struct object_info oi = OBJECT_INFO_INIT;
1331
1332         if (ctx->progress)
1333                 display_progress(ctx->progress, ++ctx->progress_done);
1334
1335         oi.typep = &type;
1336         if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1337                 die(_("unable to get type of object %s"), oid_to_hex(oid));
1338
1339         if (type != OBJ_COMMIT)
1340                 return 0;
1341
1342         oid_array_append(&ctx->oids, oid);
1343         set_commit_pos(ctx->r, oid);
1344
1345         return 0;
1346 }
1347
1348 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1349 {
1350         struct commit_list *parent;
1351         for (parent = commit->parents; parent; parent = parent->next) {
1352                 if (!(parent->item->object.flags & REACHABLE)) {
1353                         oid_array_append(&ctx->oids, &parent->item->object.oid);
1354                         parent->item->object.flags |= REACHABLE;
1355                 }
1356         }
1357 }
1358
1359 static void close_reachable(struct write_commit_graph_context *ctx)
1360 {
1361         int i;
1362         struct commit *commit;
1363         enum commit_graph_split_flags flags = ctx->opts ?
1364                 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1365
1366         if (ctx->report_progress)
1367                 ctx->progress = start_delayed_progress(
1368                                         _("Loading known commits in commit graph"),
1369                                         ctx->oids.nr);
1370         for (i = 0; i < ctx->oids.nr; i++) {
1371                 display_progress(ctx->progress, i + 1);
1372                 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1373                 if (commit)
1374                         commit->object.flags |= REACHABLE;
1375         }
1376         stop_progress(&ctx->progress);
1377
1378         /*
1379          * As this loop runs, ctx->oids.nr may grow, but not more
1380          * than the number of missing commits in the reachable
1381          * closure.
1382          */
1383         if (ctx->report_progress)
1384                 ctx->progress = start_delayed_progress(
1385                                         _("Expanding reachable commits in commit graph"),
1386                                         0);
1387         for (i = 0; i < ctx->oids.nr; i++) {
1388                 display_progress(ctx->progress, i + 1);
1389                 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1390
1391                 if (!commit)
1392                         continue;
1393                 if (ctx->split) {
1394                         if ((!repo_parse_commit(ctx->r, commit) &&
1395                              commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1396                             flags == COMMIT_GRAPH_SPLIT_REPLACE)
1397                                 add_missing_parents(ctx, commit);
1398                 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1399                         add_missing_parents(ctx, commit);
1400         }
1401         stop_progress(&ctx->progress);
1402
1403         if (ctx->report_progress)
1404                 ctx->progress = start_delayed_progress(
1405                                         _("Clearing commit marks in commit graph"),
1406                                         ctx->oids.nr);
1407         for (i = 0; i < ctx->oids.nr; i++) {
1408                 display_progress(ctx->progress, i + 1);
1409                 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1410
1411                 if (commit)
1412                         commit->object.flags &= ~REACHABLE;
1413         }
1414         stop_progress(&ctx->progress);
1415 }
1416
1417 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1418 {
1419         int i;
1420         struct commit_list *list = NULL;
1421
1422         if (ctx->report_progress)
1423                 ctx->progress = start_delayed_progress(
1424                                         _("Computing commit graph topological levels"),
1425                                         ctx->commits.nr);
1426         for (i = 0; i < ctx->commits.nr; i++) {
1427                 struct commit *c = ctx->commits.list[i];
1428                 uint32_t level;
1429
1430                 repo_parse_commit(ctx->r, c);
1431                 level = *topo_level_slab_at(ctx->topo_levels, c);
1432
1433                 display_progress(ctx->progress, i + 1);
1434                 if (level != GENERATION_NUMBER_ZERO)
1435                         continue;
1436
1437                 commit_list_insert(c, &list);
1438                 while (list) {
1439                         struct commit *current = list->item;
1440                         struct commit_list *parent;
1441                         int all_parents_computed = 1;
1442                         uint32_t max_level = 0;
1443
1444                         for (parent = current->parents; parent; parent = parent->next) {
1445                                 repo_parse_commit(ctx->r, parent->item);
1446                                 level = *topo_level_slab_at(ctx->topo_levels, parent->item);
1447
1448                                 if (level == GENERATION_NUMBER_ZERO) {
1449                                         all_parents_computed = 0;
1450                                         commit_list_insert(parent->item, &list);
1451                                         break;
1452                                 }
1453
1454                                 if (level > max_level)
1455                                         max_level = level;
1456                         }
1457
1458                         if (all_parents_computed) {
1459                                 pop_commit(&list);
1460
1461                                 if (max_level > GENERATION_NUMBER_V1_MAX - 1)
1462                                         max_level = GENERATION_NUMBER_V1_MAX - 1;
1463                                 *topo_level_slab_at(ctx->topo_levels, current) = max_level + 1;
1464                         }
1465                 }
1466         }
1467         stop_progress(&ctx->progress);
1468 }
1469
1470 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1471 {
1472         int i;
1473         struct commit_list *list = NULL;
1474
1475         if (ctx->report_progress)
1476                 ctx->progress = start_delayed_progress(
1477                                         _("Computing commit graph generation numbers"),
1478                                         ctx->commits.nr);
1479
1480         if (!ctx->trust_generation_numbers) {
1481                 for (i = 0; i < ctx->commits.nr; i++) {
1482                         struct commit *c = ctx->commits.list[i];
1483                         repo_parse_commit(ctx->r, c);
1484                         commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1485                 }
1486         }
1487
1488         for (i = 0; i < ctx->commits.nr; i++) {
1489                 struct commit *c = ctx->commits.list[i];
1490                 timestamp_t corrected_commit_date;
1491
1492                 repo_parse_commit(ctx->r, c);
1493                 corrected_commit_date = commit_graph_data_at(c)->generation;
1494
1495                 display_progress(ctx->progress, i + 1);
1496                 if (corrected_commit_date != GENERATION_NUMBER_ZERO)
1497                         continue;
1498
1499                 commit_list_insert(c, &list);
1500                 while (list) {
1501                         struct commit *current = list->item;
1502                         struct commit_list *parent;
1503                         int all_parents_computed = 1;
1504                         timestamp_t max_corrected_commit_date = 0;
1505
1506                         for (parent = current->parents; parent; parent = parent->next) {
1507                                 repo_parse_commit(ctx->r, parent->item);
1508                                 corrected_commit_date = commit_graph_data_at(parent->item)->generation;
1509
1510                                 if (corrected_commit_date == GENERATION_NUMBER_ZERO) {
1511                                         all_parents_computed = 0;
1512                                         commit_list_insert(parent->item, &list);
1513                                         break;
1514                                 }
1515
1516                                 if (corrected_commit_date > max_corrected_commit_date)
1517                                         max_corrected_commit_date = corrected_commit_date;
1518                         }
1519
1520                         if (all_parents_computed) {
1521                                 pop_commit(&list);
1522
1523                                 if (current->date && current->date > max_corrected_commit_date)
1524                                         max_corrected_commit_date = current->date - 1;
1525                                 commit_graph_data_at(current)->generation = max_corrected_commit_date + 1;
1526
1527                                 if (commit_graph_data_at(current)->generation - current->date > GENERATION_NUMBER_V2_OFFSET_MAX)
1528                                         ctx->num_generation_data_overflows++;
1529                         }
1530                 }
1531         }
1532         stop_progress(&ctx->progress);
1533 }
1534
1535 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1536 {
1537         trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1538                            ctx->count_bloom_filter_computed);
1539         trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1540                            ctx->count_bloom_filter_not_computed);
1541         trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1542                            ctx->count_bloom_filter_trunc_empty);
1543         trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1544                            ctx->count_bloom_filter_trunc_large);
1545 }
1546
1547 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1548 {
1549         int i;
1550         struct progress *progress = NULL;
1551         struct commit **sorted_commits;
1552         int max_new_filters;
1553
1554         init_bloom_filters();
1555
1556         if (ctx->report_progress)
1557                 progress = start_delayed_progress(
1558                         _("Computing commit changed paths Bloom filters"),
1559                         ctx->commits.nr);
1560
1561         ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1562         COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1563
1564         if (ctx->order_by_pack)
1565                 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1566         else
1567                 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1568
1569         max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1570                 ctx->opts->max_new_filters : ctx->commits.nr;
1571
1572         for (i = 0; i < ctx->commits.nr; i++) {
1573                 enum bloom_filter_computed computed = 0;
1574                 struct commit *c = sorted_commits[i];
1575                 struct bloom_filter *filter = get_or_compute_bloom_filter(
1576                         ctx->r,
1577                         c,
1578                         ctx->count_bloom_filter_computed < max_new_filters,
1579                         ctx->bloom_settings,
1580                         &computed);
1581                 if (computed & BLOOM_COMPUTED) {
1582                         ctx->count_bloom_filter_computed++;
1583                         if (computed & BLOOM_TRUNC_EMPTY)
1584                                 ctx->count_bloom_filter_trunc_empty++;
1585                         if (computed & BLOOM_TRUNC_LARGE)
1586                                 ctx->count_bloom_filter_trunc_large++;
1587                 } else if (computed & BLOOM_NOT_COMPUTED)
1588                         ctx->count_bloom_filter_not_computed++;
1589                 ctx->total_bloom_filter_data_size += filter
1590                         ? sizeof(unsigned char) * filter->len : 0;
1591                 display_progress(progress, i + 1);
1592         }
1593
1594         if (trace2_is_enabled())
1595                 trace2_bloom_filter_write_statistics(ctx);
1596
1597         free(sorted_commits);
1598         stop_progress(&progress);
1599 }
1600
1601 struct refs_cb_data {
1602         struct oidset *commits;
1603         struct progress *progress;
1604 };
1605
1606 static int add_ref_to_set(const char *refname,
1607                           const struct object_id *oid,
1608                           int flags, void *cb_data)
1609 {
1610         struct object_id peeled;
1611         struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1612
1613         if (!peel_iterated_oid(oid, &peeled))
1614                 oid = &peeled;
1615         if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1616                 oidset_insert(data->commits, oid);
1617
1618         display_progress(data->progress, oidset_size(data->commits));
1619
1620         return 0;
1621 }
1622
1623 int write_commit_graph_reachable(struct object_directory *odb,
1624                                  enum commit_graph_write_flags flags,
1625                                  const struct commit_graph_opts *opts)
1626 {
1627         struct oidset commits = OIDSET_INIT;
1628         struct refs_cb_data data;
1629         int result;
1630
1631         memset(&data, 0, sizeof(data));
1632         data.commits = &commits;
1633         if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1634                 data.progress = start_delayed_progress(
1635                         _("Collecting referenced commits"), 0);
1636
1637         for_each_ref(add_ref_to_set, &data);
1638
1639         stop_progress(&data.progress);
1640
1641         result = write_commit_graph(odb, NULL, &commits,
1642                                     flags, opts);
1643
1644         oidset_clear(&commits);
1645         return result;
1646 }
1647
1648 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1649                                 struct string_list *pack_indexes)
1650 {
1651         uint32_t i;
1652         struct strbuf progress_title = STRBUF_INIT;
1653         struct strbuf packname = STRBUF_INIT;
1654         int dirlen;
1655
1656         strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1657         dirlen = packname.len;
1658         if (ctx->report_progress) {
1659                 strbuf_addf(&progress_title,
1660                             Q_("Finding commits for commit graph in %d pack",
1661                                "Finding commits for commit graph in %d packs",
1662                                pack_indexes->nr),
1663                             pack_indexes->nr);
1664                 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1665                 ctx->progress_done = 0;
1666         }
1667         for (i = 0; i < pack_indexes->nr; i++) {
1668                 struct packed_git *p;
1669                 strbuf_setlen(&packname, dirlen);
1670                 strbuf_addstr(&packname, pack_indexes->items[i].string);
1671                 p = add_packed_git(packname.buf, packname.len, 1);
1672                 if (!p) {
1673                         error(_("error adding pack %s"), packname.buf);
1674                         return -1;
1675                 }
1676                 if (open_pack_index(p)) {
1677                         error(_("error opening index for %s"), packname.buf);
1678                         return -1;
1679                 }
1680                 for_each_object_in_pack(p, add_packed_commits, ctx,
1681                                         FOR_EACH_OBJECT_PACK_ORDER);
1682                 close_pack(p);
1683                 free(p);
1684         }
1685
1686         stop_progress(&ctx->progress);
1687         strbuf_release(&progress_title);
1688         strbuf_release(&packname);
1689
1690         return 0;
1691 }
1692
1693 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1694                                   struct oidset *commits)
1695 {
1696         struct oidset_iter iter;
1697         struct object_id *oid;
1698
1699         if (!oidset_size(commits))
1700                 return 0;
1701
1702         oidset_iter_init(commits, &iter);
1703         while ((oid = oidset_iter_next(&iter))) {
1704                 oid_array_append(&ctx->oids, oid);
1705         }
1706
1707         return 0;
1708 }
1709
1710 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1711 {
1712         if (ctx->report_progress)
1713                 ctx->progress = start_delayed_progress(
1714                         _("Finding commits for commit graph among packed objects"),
1715                         ctx->approx_nr_objects);
1716         for_each_packed_object(add_packed_commits, ctx,
1717                                FOR_EACH_OBJECT_PACK_ORDER);
1718         if (ctx->progress_done < ctx->approx_nr_objects)
1719                 display_progress(ctx->progress, ctx->approx_nr_objects);
1720         stop_progress(&ctx->progress);
1721 }
1722
1723 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1724 {
1725         uint32_t i;
1726         enum commit_graph_split_flags flags = ctx->opts ?
1727                 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1728
1729         ctx->num_extra_edges = 0;
1730         if (ctx->report_progress)
1731                 ctx->progress = start_delayed_progress(
1732                         _("Finding extra edges in commit graph"),
1733                         ctx->oids.nr);
1734         oid_array_sort(&ctx->oids);
1735         for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1736                 unsigned int num_parents;
1737
1738                 display_progress(ctx->progress, i + 1);
1739
1740                 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1741                 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1742
1743                 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1744                     commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1745                         continue;
1746
1747                 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1748                         repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1749                 else
1750                         repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1751
1752                 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1753                 if (num_parents > 2)
1754                         ctx->num_extra_edges += num_parents - 1;
1755
1756                 ctx->commits.nr++;
1757         }
1758         stop_progress(&ctx->progress);
1759 }
1760
1761 static int write_graph_chunk_base_1(struct hashfile *f,
1762                                     struct commit_graph *g)
1763 {
1764         int num = 0;
1765
1766         if (!g)
1767                 return 0;
1768
1769         num = write_graph_chunk_base_1(f, g->base_graph);
1770         hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1771         return num + 1;
1772 }
1773
1774 static int write_graph_chunk_base(struct hashfile *f,
1775                                     void *data)
1776 {
1777         struct write_commit_graph_context *ctx = data;
1778         int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1779
1780         if (num != ctx->num_commit_graphs_after - 1) {
1781                 error(_("failed to write correct number of base graph ids"));
1782                 return -1;
1783         }
1784
1785         return 0;
1786 }
1787
1788 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1789 {
1790         uint32_t i;
1791         int fd;
1792         struct hashfile *f;
1793         struct lock_file lk = LOCK_INIT;
1794         const unsigned hashsz = the_hash_algo->rawsz;
1795         struct strbuf progress_title = STRBUF_INIT;
1796         struct chunkfile *cf;
1797         unsigned char file_hash[GIT_MAX_RAWSZ];
1798
1799         if (ctx->split) {
1800                 struct strbuf tmp_file = STRBUF_INIT;
1801
1802                 strbuf_addf(&tmp_file,
1803                             "%s/info/commit-graphs/tmp_graph_XXXXXX",
1804                             ctx->odb->path);
1805                 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1806         } else {
1807                 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1808         }
1809
1810         if (safe_create_leading_directories(ctx->graph_name)) {
1811                 UNLEAK(ctx->graph_name);
1812                 error(_("unable to create leading directories of %s"),
1813                         ctx->graph_name);
1814                 return -1;
1815         }
1816
1817         if (ctx->split) {
1818                 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
1819
1820                 hold_lock_file_for_update_mode(&lk, lock_name,
1821                                                LOCK_DIE_ON_ERROR, 0444);
1822
1823                 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1824                 if (fd < 0) {
1825                         error(_("unable to create temporary graph layer"));
1826                         return -1;
1827                 }
1828
1829                 if (adjust_shared_perm(ctx->graph_name)) {
1830                         error(_("unable to adjust shared permissions for '%s'"),
1831                               ctx->graph_name);
1832                         return -1;
1833                 }
1834
1835                 f = hashfd(fd, ctx->graph_name);
1836         } else {
1837                 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1838                                                LOCK_DIE_ON_ERROR, 0444);
1839                 fd = get_lock_file_fd(&lk);
1840                 f = hashfd(fd, get_lock_file_path(&lk));
1841         }
1842
1843         cf = init_chunkfile(f);
1844
1845         add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
1846                   write_graph_chunk_fanout);
1847         add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, hashsz * ctx->commits.nr,
1848                   write_graph_chunk_oids);
1849         add_chunk(cf, GRAPH_CHUNKID_DATA, (hashsz + 16) * ctx->commits.nr,
1850                   write_graph_chunk_data);
1851
1852         if (ctx->write_generation_data)
1853                 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
1854                           sizeof(uint32_t) * ctx->commits.nr,
1855                           write_graph_chunk_generation_data);
1856         if (ctx->num_generation_data_overflows)
1857                 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
1858                           sizeof(timestamp_t) * ctx->num_generation_data_overflows,
1859                           write_graph_chunk_generation_data_overflow);
1860         if (ctx->num_extra_edges)
1861                 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
1862                           4 * ctx->num_extra_edges,
1863                           write_graph_chunk_extra_edges);
1864         if (ctx->changed_paths) {
1865                 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
1866                           sizeof(uint32_t) * ctx->commits.nr,
1867                           write_graph_chunk_bloom_indexes);
1868                 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
1869                           sizeof(uint32_t) * 3
1870                                 + ctx->total_bloom_filter_data_size,
1871                           write_graph_chunk_bloom_data);
1872         }
1873         if (ctx->num_commit_graphs_after > 1)
1874                 add_chunk(cf, GRAPH_CHUNKID_BASE,
1875                           hashsz * (ctx->num_commit_graphs_after - 1),
1876                           write_graph_chunk_base);
1877
1878         hashwrite_be32(f, GRAPH_SIGNATURE);
1879
1880         hashwrite_u8(f, GRAPH_VERSION);
1881         hashwrite_u8(f, oid_version());
1882         hashwrite_u8(f, get_num_chunks(cf));
1883         hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1884
1885         if (ctx->report_progress) {
1886                 strbuf_addf(&progress_title,
1887                             Q_("Writing out commit graph in %d pass",
1888                                "Writing out commit graph in %d passes",
1889                                get_num_chunks(cf)),
1890                             get_num_chunks(cf));
1891                 ctx->progress = start_delayed_progress(
1892                         progress_title.buf,
1893                         get_num_chunks(cf) * ctx->commits.nr);
1894         }
1895
1896         write_chunkfile(cf, ctx);
1897
1898         stop_progress(&ctx->progress);
1899         strbuf_release(&progress_title);
1900
1901         if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1902                 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1903                 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
1904
1905                 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1906                 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1907                 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1908                 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1909         }
1910
1911         close_commit_graph(ctx->r->objects);
1912         finalize_hashfile(f, file_hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1913         free_chunkfile(cf);
1914
1915         if (ctx->split) {
1916                 FILE *chainf = fdopen_lock_file(&lk, "w");
1917                 char *final_graph_name;
1918                 int result;
1919
1920                 close(fd);
1921
1922                 if (!chainf) {
1923                         error(_("unable to open commit-graph chain file"));
1924                         return -1;
1925                 }
1926
1927                 if (ctx->base_graph_name) {
1928                         const char *dest;
1929                         int idx = ctx->num_commit_graphs_after - 1;
1930                         if (ctx->num_commit_graphs_after > 1)
1931                                 idx--;
1932
1933                         dest = ctx->commit_graph_filenames_after[idx];
1934
1935                         if (strcmp(ctx->base_graph_name, dest)) {
1936                                 result = rename(ctx->base_graph_name, dest);
1937
1938                                 if (result) {
1939                                         error(_("failed to rename base commit-graph file"));
1940                                         return -1;
1941                                 }
1942                         }
1943                 } else {
1944                         char *graph_name = get_commit_graph_filename(ctx->odb);
1945                         unlink(graph_name);
1946                 }
1947
1948                 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
1949                 final_graph_name = get_split_graph_filename(ctx->odb,
1950                                         ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1951                 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1952
1953                 result = rename(ctx->graph_name, final_graph_name);
1954
1955                 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1956                         fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
1957
1958                 if (result) {
1959                         error(_("failed to rename temporary commit-graph file"));
1960                         return -1;
1961                 }
1962         }
1963
1964         commit_lock_file(&lk);
1965
1966         return 0;
1967 }
1968
1969 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
1970 {
1971         struct commit_graph *g;
1972         uint32_t num_commits;
1973         enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1974         uint32_t i;
1975
1976         int max_commits = 0;
1977         int size_mult = 2;
1978
1979         if (ctx->opts) {
1980                 max_commits = ctx->opts->max_commits;
1981
1982                 if (ctx->opts->size_multiple)
1983                         size_mult = ctx->opts->size_multiple;
1984
1985                 flags = ctx->opts->split_flags;
1986         }
1987
1988         g = ctx->r->objects->commit_graph;
1989         num_commits = ctx->commits.nr;
1990         if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
1991                 ctx->num_commit_graphs_after = 1;
1992         else
1993                 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
1994
1995         if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
1996             flags != COMMIT_GRAPH_SPLIT_REPLACE) {
1997                 while (g && (g->num_commits <= size_mult * num_commits ||
1998                             (max_commits && num_commits > max_commits))) {
1999                         if (g->odb != ctx->odb)
2000                                 break;
2001
2002                         num_commits += g->num_commits;
2003                         g = g->base_graph;
2004
2005                         ctx->num_commit_graphs_after--;
2006                 }
2007         }
2008
2009         if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2010                 ctx->new_base_graph = g;
2011         else if (ctx->num_commit_graphs_after != 1)
2012                 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2013                     "should be 1 with --split=replace");
2014
2015         if (ctx->num_commit_graphs_after == 2) {
2016                 char *old_graph_name = get_commit_graph_filename(g->odb);
2017
2018                 if (!strcmp(g->filename, old_graph_name) &&
2019                     g->odb != ctx->odb) {
2020                         ctx->num_commit_graphs_after = 1;
2021                         ctx->new_base_graph = NULL;
2022                 }
2023
2024                 free(old_graph_name);
2025         }
2026
2027         CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2028         CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2029
2030         for (i = 0; i < ctx->num_commit_graphs_after &&
2031                     i < ctx->num_commit_graphs_before; i++)
2032                 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2033
2034         i = ctx->num_commit_graphs_before - 1;
2035         g = ctx->r->objects->commit_graph;
2036
2037         while (g) {
2038                 if (i < ctx->num_commit_graphs_after)
2039                         ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2040
2041                 /*
2042                  * If the topmost remaining layer has generation data chunk, the
2043                  * resultant layer also has generation data chunk.
2044                  */
2045                 if (i == ctx->num_commit_graphs_after - 2)
2046                         ctx->write_generation_data = !!g->chunk_generation_data;
2047
2048                 i--;
2049                 g = g->base_graph;
2050         }
2051 }
2052
2053 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2054                                struct commit_graph *g)
2055 {
2056         uint32_t i;
2057         uint32_t offset = g->num_commits_in_base;
2058
2059         ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2060
2061         for (i = 0; i < g->num_commits; i++) {
2062                 struct object_id oid;
2063                 struct commit *result;
2064
2065                 display_progress(ctx->progress, i + 1);
2066
2067                 load_oid_from_graph(g, i + offset, &oid);
2068
2069                 /* only add commits if they still exist in the repo */
2070                 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2071
2072                 if (result) {
2073                         ctx->commits.list[ctx->commits.nr] = result;
2074                         ctx->commits.nr++;
2075                 }
2076         }
2077 }
2078
2079 static int commit_compare(const void *_a, const void *_b)
2080 {
2081         const struct commit *a = *(const struct commit **)_a;
2082         const struct commit *b = *(const struct commit **)_b;
2083         return oidcmp(&a->object.oid, &b->object.oid);
2084 }
2085
2086 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2087 {
2088         uint32_t i, dedup_i = 0;
2089
2090         if (ctx->report_progress)
2091                 ctx->progress = start_delayed_progress(
2092                                         _("Scanning merged commits"),
2093                                         ctx->commits.nr);
2094
2095         QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2096
2097         ctx->num_extra_edges = 0;
2098         for (i = 0; i < ctx->commits.nr; i++) {
2099                 display_progress(ctx->progress, i);
2100
2101                 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2102                           &ctx->commits.list[i]->object.oid)) {
2103                         /*
2104                          * Silently ignore duplicates. These were likely
2105                          * created due to a commit appearing in multiple
2106                          * layers of the chain, which is unexpected but
2107                          * not invalid. We should make sure there is a
2108                          * unique copy in the new layer.
2109                          */
2110                 } else {
2111                         unsigned int num_parents;
2112
2113                         ctx->commits.list[dedup_i] = ctx->commits.list[i];
2114                         dedup_i++;
2115
2116                         num_parents = commit_list_count(ctx->commits.list[i]->parents);
2117                         if (num_parents > 2)
2118                                 ctx->num_extra_edges += num_parents - 1;
2119                 }
2120         }
2121
2122         ctx->commits.nr = dedup_i;
2123
2124         stop_progress(&ctx->progress);
2125 }
2126
2127 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2128 {
2129         struct commit_graph *g = ctx->r->objects->commit_graph;
2130         uint32_t current_graph_number = ctx->num_commit_graphs_before;
2131
2132         while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2133                 current_graph_number--;
2134
2135                 if (ctx->report_progress)
2136                         ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2137
2138                 merge_commit_graph(ctx, g);
2139                 stop_progress(&ctx->progress);
2140
2141                 g = g->base_graph;
2142         }
2143
2144         if (g) {
2145                 ctx->new_base_graph = g;
2146                 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2147         }
2148
2149         if (ctx->new_base_graph)
2150                 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2151
2152         sort_and_scan_merged_commits(ctx);
2153 }
2154
2155 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2156 {
2157         uint32_t i;
2158         time_t now = time(NULL);
2159
2160         for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2161                 struct stat st;
2162                 struct utimbuf updated_time;
2163
2164                 stat(ctx->commit_graph_filenames_before[i], &st);
2165
2166                 updated_time.actime = st.st_atime;
2167                 updated_time.modtime = now;
2168                 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2169         }
2170 }
2171
2172 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2173 {
2174         struct strbuf path = STRBUF_INIT;
2175         DIR *dir;
2176         struct dirent *de;
2177         size_t dirnamelen;
2178         timestamp_t expire_time = time(NULL);
2179
2180         if (ctx->opts && ctx->opts->expire_time)
2181                 expire_time = ctx->opts->expire_time;
2182         if (!ctx->split) {
2183                 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2184                 unlink(chain_file_name);
2185                 free(chain_file_name);
2186                 ctx->num_commit_graphs_after = 0;
2187         }
2188
2189         strbuf_addstr(&path, ctx->odb->path);
2190         strbuf_addstr(&path, "/info/commit-graphs");
2191         dir = opendir(path.buf);
2192
2193         if (!dir)
2194                 goto out;
2195
2196         strbuf_addch(&path, '/');
2197         dirnamelen = path.len;
2198         while ((de = readdir(dir)) != NULL) {
2199                 struct stat st;
2200                 uint32_t i, found = 0;
2201
2202                 strbuf_setlen(&path, dirnamelen);
2203                 strbuf_addstr(&path, de->d_name);
2204
2205                 stat(path.buf, &st);
2206
2207                 if (st.st_mtime > expire_time)
2208                         continue;
2209                 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2210                         continue;
2211
2212                 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2213                         if (!strcmp(ctx->commit_graph_filenames_after[i],
2214                                     path.buf)) {
2215                                 found = 1;
2216                                 break;
2217                         }
2218                 }
2219
2220                 if (!found)
2221                         unlink(path.buf);
2222         }
2223
2224 out:
2225         strbuf_release(&path);
2226 }
2227
2228 int write_commit_graph(struct object_directory *odb,
2229                        struct string_list *pack_indexes,
2230                        struct oidset *commits,
2231                        enum commit_graph_write_flags flags,
2232                        const struct commit_graph_opts *opts)
2233 {
2234         struct repository *r = the_repository;
2235         struct write_commit_graph_context *ctx;
2236         uint32_t i;
2237         int res = 0;
2238         int replace = 0;
2239         struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2240         struct topo_level_slab topo_levels;
2241
2242         prepare_repo_settings(r);
2243         if (!r->settings.core_commit_graph) {
2244                 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2245                 return 0;
2246         }
2247         if (!commit_graph_compatible(r))
2248                 return 0;
2249
2250         CALLOC_ARRAY(ctx, 1);
2251         ctx->r = r;
2252         ctx->odb = odb;
2253         ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2254         ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2255         ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2256         ctx->opts = opts;
2257         ctx->total_bloom_filter_data_size = 0;
2258         ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2259         ctx->num_generation_data_overflows = 0;
2260
2261         bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2262                                                       bloom_settings.bits_per_entry);
2263         bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2264                                                   bloom_settings.num_hashes);
2265         bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2266                                                          bloom_settings.max_changed_paths);
2267         ctx->bloom_settings = &bloom_settings;
2268
2269         init_topo_level_slab(&topo_levels);
2270         ctx->topo_levels = &topo_levels;
2271
2272         prepare_commit_graph(ctx->r);
2273         if (ctx->r->objects->commit_graph) {
2274                 struct commit_graph *g = ctx->r->objects->commit_graph;
2275
2276                 while (g) {
2277                         g->topo_levels = &topo_levels;
2278                         g = g->base_graph;
2279                 }
2280         }
2281
2282         if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2283                 ctx->changed_paths = 1;
2284         if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2285                 struct commit_graph *g;
2286
2287                 g = ctx->r->objects->commit_graph;
2288
2289                 /* We have changed-paths already. Keep them in the next graph */
2290                 if (g && g->chunk_bloom_data) {
2291                         ctx->changed_paths = 1;
2292                         ctx->bloom_settings = g->bloom_filter_settings;
2293                 }
2294         }
2295
2296         if (ctx->split) {
2297                 struct commit_graph *g = ctx->r->objects->commit_graph;
2298
2299                 while (g) {
2300                         ctx->num_commit_graphs_before++;
2301                         g = g->base_graph;
2302                 }
2303
2304                 if (ctx->num_commit_graphs_before) {
2305                         ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2306                         i = ctx->num_commit_graphs_before;
2307                         g = ctx->r->objects->commit_graph;
2308
2309                         while (g) {
2310                                 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2311                                 g = g->base_graph;
2312                         }
2313                 }
2314
2315                 if (ctx->opts)
2316                         replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2317         }
2318
2319         ctx->approx_nr_objects = approximate_object_count();
2320
2321         if (ctx->append && ctx->r->objects->commit_graph) {
2322                 struct commit_graph *g = ctx->r->objects->commit_graph;
2323                 for (i = 0; i < g->num_commits; i++) {
2324                         struct object_id oid;
2325                         oidread(&oid, g->chunk_oid_lookup + g->hash_len * i);
2326                         oid_array_append(&ctx->oids, &oid);
2327                 }
2328         }
2329
2330         if (pack_indexes) {
2331                 ctx->order_by_pack = 1;
2332                 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2333                         goto cleanup;
2334         }
2335
2336         if (commits) {
2337                 if ((res = fill_oids_from_commits(ctx, commits)))
2338                         goto cleanup;
2339         }
2340
2341         if (!pack_indexes && !commits) {
2342                 ctx->order_by_pack = 1;
2343                 fill_oids_from_all_packs(ctx);
2344         }
2345
2346         close_reachable(ctx);
2347
2348         copy_oids_to_commits(ctx);
2349
2350         if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2351                 error(_("too many commits to write graph"));
2352                 res = -1;
2353                 goto cleanup;
2354         }
2355
2356         if (!ctx->commits.nr && !replace)
2357                 goto cleanup;
2358
2359         if (ctx->split) {
2360                 split_graph_merge_strategy(ctx);
2361
2362                 if (!replace)
2363                         merge_commit_graphs(ctx);
2364         } else
2365                 ctx->num_commit_graphs_after = 1;
2366
2367         ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2368
2369         compute_topological_levels(ctx);
2370         if (ctx->write_generation_data)
2371                 compute_generation_numbers(ctx);
2372
2373         if (ctx->changed_paths)
2374                 compute_bloom_filters(ctx);
2375
2376         res = write_commit_graph_file(ctx);
2377
2378         if (ctx->split)
2379                 mark_commit_graphs(ctx);
2380
2381         expire_commit_graphs(ctx);
2382
2383 cleanup:
2384         free(ctx->graph_name);
2385         free(ctx->commits.list);
2386         oid_array_clear(&ctx->oids);
2387         clear_topo_level_slab(&topo_levels);
2388
2389         if (ctx->commit_graph_filenames_after) {
2390                 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2391                         free(ctx->commit_graph_filenames_after[i]);
2392                         free(ctx->commit_graph_hash_after[i]);
2393                 }
2394
2395                 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2396                         free(ctx->commit_graph_filenames_before[i]);
2397
2398                 free(ctx->commit_graph_filenames_after);
2399                 free(ctx->commit_graph_filenames_before);
2400                 free(ctx->commit_graph_hash_after);
2401         }
2402
2403         free(ctx);
2404
2405         return res;
2406 }
2407
2408 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2409 static int verify_commit_graph_error;
2410
2411 static void graph_report(const char *fmt, ...)
2412 {
2413         va_list ap;
2414
2415         verify_commit_graph_error = 1;
2416         va_start(ap, fmt);
2417         vfprintf(stderr, fmt, ap);
2418         fprintf(stderr, "\n");
2419         va_end(ap);
2420 }
2421
2422 #define GENERATION_ZERO_EXISTS 1
2423 #define GENERATION_NUMBER_EXISTS 2
2424
2425 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2426 {
2427         uint32_t i, cur_fanout_pos = 0;
2428         struct object_id prev_oid, cur_oid;
2429         unsigned char checksum[GIT_MAX_HEXSZ];
2430         int generation_zero = 0;
2431         struct hashfile *f;
2432         int devnull;
2433         struct progress *progress = NULL;
2434         int local_error = 0;
2435
2436         if (!g) {
2437                 graph_report("no commit-graph file loaded");
2438                 return 1;
2439         }
2440
2441         verify_commit_graph_error = verify_commit_graph_lite(g);
2442         if (verify_commit_graph_error)
2443                 return verify_commit_graph_error;
2444
2445         devnull = open("/dev/null", O_WRONLY);
2446         f = hashfd(devnull, NULL);
2447         hashwrite(f, g->data, g->data_len - g->hash_len);
2448         finalize_hashfile(f, checksum, CSUM_CLOSE);
2449         if (!hasheq(checksum, g->data + g->data_len - g->hash_len)) {
2450                 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2451                 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2452         }
2453
2454         for (i = 0; i < g->num_commits; i++) {
2455                 struct commit *graph_commit;
2456
2457                 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2458
2459                 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2460                         graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2461                                      oid_to_hex(&prev_oid),
2462                                      oid_to_hex(&cur_oid));
2463
2464                 oidcpy(&prev_oid, &cur_oid);
2465
2466                 while (cur_oid.hash[0] > cur_fanout_pos) {
2467                         uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2468
2469                         if (i != fanout_value)
2470                                 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2471                                              cur_fanout_pos, fanout_value, i);
2472                         cur_fanout_pos++;
2473                 }
2474
2475                 graph_commit = lookup_commit(r, &cur_oid);
2476                 if (!parse_commit_in_graph_one(r, g, graph_commit))
2477                         graph_report(_("failed to parse commit %s from commit-graph"),
2478                                      oid_to_hex(&cur_oid));
2479         }
2480
2481         while (cur_fanout_pos < 256) {
2482                 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2483
2484                 if (g->num_commits != fanout_value)
2485                         graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2486                                      cur_fanout_pos, fanout_value, i);
2487
2488                 cur_fanout_pos++;
2489         }
2490
2491         if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2492                 return verify_commit_graph_error;
2493
2494         if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2495                 progress = start_progress(_("Verifying commits in commit graph"),
2496                                         g->num_commits);
2497
2498         for (i = 0; i < g->num_commits; i++) {
2499                 struct commit *graph_commit, *odb_commit;
2500                 struct commit_list *graph_parents, *odb_parents;
2501                 timestamp_t max_generation = 0;
2502                 timestamp_t generation;
2503
2504                 display_progress(progress, i + 1);
2505                 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2506
2507                 graph_commit = lookup_commit(r, &cur_oid);
2508                 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2509                 if (parse_commit_internal(odb_commit, 0, 0)) {
2510                         graph_report(_("failed to parse commit %s from object database for commit-graph"),
2511                                      oid_to_hex(&cur_oid));
2512                         continue;
2513                 }
2514
2515                 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2516                            get_commit_tree_oid(odb_commit)))
2517                         graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2518                                      oid_to_hex(&cur_oid),
2519                                      oid_to_hex(get_commit_tree_oid(graph_commit)),
2520                                      oid_to_hex(get_commit_tree_oid(odb_commit)));
2521
2522                 graph_parents = graph_commit->parents;
2523                 odb_parents = odb_commit->parents;
2524
2525                 while (graph_parents) {
2526                         if (odb_parents == NULL) {
2527                                 graph_report(_("commit-graph parent list for commit %s is too long"),
2528                                              oid_to_hex(&cur_oid));
2529                                 break;
2530                         }
2531
2532                         /* parse parent in case it is in a base graph */
2533                         parse_commit_in_graph_one(r, g, graph_parents->item);
2534
2535                         if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2536                                 graph_report(_("commit-graph parent for %s is %s != %s"),
2537                                              oid_to_hex(&cur_oid),
2538                                              oid_to_hex(&graph_parents->item->object.oid),
2539                                              oid_to_hex(&odb_parents->item->object.oid));
2540
2541                         generation = commit_graph_generation(graph_parents->item);
2542                         if (generation > max_generation)
2543                                 max_generation = generation;
2544
2545                         graph_parents = graph_parents->next;
2546                         odb_parents = odb_parents->next;
2547                 }
2548
2549                 if (odb_parents != NULL)
2550                         graph_report(_("commit-graph parent list for commit %s terminates early"),
2551                                      oid_to_hex(&cur_oid));
2552
2553                 if (!commit_graph_generation(graph_commit)) {
2554                         if (generation_zero == GENERATION_NUMBER_EXISTS)
2555                                 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2556                                              oid_to_hex(&cur_oid));
2557                         generation_zero = GENERATION_ZERO_EXISTS;
2558                 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2559                         graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2560                                      oid_to_hex(&cur_oid));
2561
2562                 if (generation_zero == GENERATION_ZERO_EXISTS)
2563                         continue;
2564
2565                 /*
2566                  * If we are using topological level and one of our parents has
2567                  * generation GENERATION_NUMBER_V1_MAX, then our generation is
2568                  * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2569                  * in the following condition.
2570                  */
2571                 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2572                         max_generation--;
2573
2574                 generation = commit_graph_generation(graph_commit);
2575                 if (generation < max_generation + 1)
2576                         graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2577                                      oid_to_hex(&cur_oid),
2578                                      generation,
2579                                      max_generation + 1);
2580
2581                 if (graph_commit->date != odb_commit->date)
2582                         graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2583                                      oid_to_hex(&cur_oid),
2584                                      graph_commit->date,
2585                                      odb_commit->date);
2586         }
2587         stop_progress(&progress);
2588
2589         local_error = verify_commit_graph_error;
2590
2591         if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2592                 local_error |= verify_commit_graph(r, g->base_graph, flags);
2593
2594         return local_error;
2595 }
2596
2597 void free_commit_graph(struct commit_graph *g)
2598 {
2599         if (!g)
2600                 return;
2601         if (g->data) {
2602                 munmap((void *)g->data, g->data_len);
2603                 g->data = NULL;
2604         }
2605         free(g->filename);
2606         free(g->bloom_filter_settings);
2607         free(g);
2608 }
2609
2610 void disable_commit_graph(struct repository *r)
2611 {
2612         r->commit_graph_disabled = 1;
2613 }