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