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