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