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