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