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