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