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