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