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