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