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