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