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