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