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