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