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