]> git.ipfire.org Git - thirdparty/git.git/blame - commit-graph.c
commit-graph: simplify parse_commit_graph() #2
[thirdparty/git.git] / commit-graph.c
CommitLineData
08fd81c9 1#include "git-compat-util.h"
fa796530 2#include "config.h"
08fd81c9
DS
3#include "lockfile.h"
4#include "pack.h"
5#include "packfile.h"
6#include "commit.h"
7#include "object.h"
59fb8770 8#include "refs.h"
08fd81c9
DS
9#include "revision.h"
10#include "sha1-lookup.h"
11#include "commit-graph.h"
b10edb2d 12#include "object-store.h"
96af91d4 13#include "alloc.h"
d6538246
DS
14#include "hashmap.h"
15#include "replace-object.h"
7b0f2292 16#include "progress.h"
f97b9325 17#include "bloom.h"
d21ee7d1 18#include "commit-slab.h"
08fd81c9 19
b23ea979
DS
20void git_test_write_commit_graph_or_die(void)
21{
22 int flags = 0;
23 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
24 return;
25
26 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
27 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
28
29 if (write_commit_graph_reachable(the_repository->objects->odb,
30 flags, NULL))
31 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
32}
33
08fd81c9
DS
34#define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
35#define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
36#define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
37#define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
5af7417b 38#define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
76ffbca7
GS
39#define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
40#define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
118bd570 41#define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
76ffbca7 42#define MAX_NUM_CHUNKS 7
08fd81c9 43
c1665998 44#define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
08fd81c9
DS
45
46#define GRAPH_VERSION_1 0x1
47#define GRAPH_VERSION GRAPH_VERSION_1
48
5af7417b 49#define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
08fd81c9
DS
50#define GRAPH_EDGE_LAST_MASK 0x7fffffff
51#define GRAPH_PARENT_NONE 0x70000000
52
53#define GRAPH_LAST_EDGE 0x80000000
54
0e3b97cc 55#define GRAPH_HEADER_SIZE 8
08fd81c9
DS
56#define GRAPH_FANOUT_SIZE (4 * 256)
57#define GRAPH_CHUNKLOOKUP_WIDTH 12
0e3b97cc 58#define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
c1665998 59 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
08fd81c9 60
cb99a34e
DS
61/* Remember to update object flag allocation in object.h */
62#define REACHABLE (1u<<15)
63
d21ee7d1
JK
64/* Keep track of the order in which commits are added to our list. */
65define_commit_slab(commit_pos, int);
66static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
67
68static void set_commit_pos(struct repository *r, const struct object_id *oid)
69{
70 static int32_t max_pos;
71 struct commit *commit = lookup_commit(r, oid);
72
73 if (!commit)
74 return; /* should never happen, but be lenient */
75
76 *commit_pos_at(&commit_pos, commit) = max_pos++;
77}
78
79static int commit_pos_cmp(const void *va, const void *vb)
08fd81c9 80{
d21ee7d1
JK
81 const struct commit *a = *(const struct commit **)va;
82 const struct commit *b = *(const struct commit **)vb;
83 return commit_pos_at(&commit_pos, a) -
84 commit_pos_at(&commit_pos, b);
85}
86
3d112755
GS
87static int commit_gen_cmp(const void *va, const void *vb)
88{
89 const struct commit *a = *(const struct commit **)va;
90 const struct commit *b = *(const struct commit **)vb;
91
92 /* lower generation commits first */
93 if (a->generation < b->generation)
94 return -1;
95 else if (a->generation > b->generation)
96 return 1;
97
98 /* use date as a heuristic when generations are equal */
99 if (a->date < b->date)
100 return -1;
101 else if (a->date > b->date)
102 return 1;
103 return 0;
104}
105
d21ee7d1
JK
106char *get_commit_graph_filename(struct object_directory *obj_dir)
107{
108 return xstrfmt("%s/info/commit-graph", obj_dir->path);
08fd81c9
DS
109}
110
ad2dd5bb 111static char *get_split_graph_filename(struct object_directory *odb,
5c84b339
DS
112 const char *oid_hex)
113{
ad2dd5bb
TB
114 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
115 oid_hex);
5c84b339
DS
116}
117
ad2dd5bb 118static char *get_chain_filename(struct object_directory *odb)
5c84b339 119{
ad2dd5bb 120 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
08fd81c9
DS
121}
122
c1665998 123static uint8_t oid_version(void)
124{
125 return 1;
126}
127
2a2e32bd
DS
128static struct commit_graph *alloc_commit_graph(void)
129{
130 struct commit_graph *g = xcalloc(1, sizeof(*g));
131 g->graph_fd = -1;
132
133 return g;
134}
135
d6538246
DS
136extern int read_replace_refs;
137
138static int commit_graph_compatible(struct repository *r)
139{
5cef295f
DS
140 if (!r->gitdir)
141 return 0;
142
d6538246
DS
143 if (read_replace_refs) {
144 prepare_replace_object(r);
145 if (hashmap_get_size(&r->objects->replace_map->map))
146 return 0;
147 }
148
20fd6d57
DS
149 prepare_commit_graft(r);
150 if (r->parsed_objects && r->parsed_objects->grafts_nr)
151 return 0;
152 if (is_repository_shallow(r))
153 return 0;
154
d6538246
DS
155 return 1;
156}
157
61df89c8
ÆAB
158int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
159{
160 *fd = git_open(graph_file);
161 if (*fd < 0)
162 return 0;
163 if (fstat(*fd, st)) {
164 close(*fd);
165 return 0;
166 }
167 return 1;
168}
169
a7df60ca
TB
170struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st,
171 struct object_directory *odb)
2a2e32bd
DS
172{
173 void *graph_map;
2a2e32bd 174 size_t graph_size;
aa658574 175 struct commit_graph *ret;
2a2e32bd 176
61df89c8 177 graph_size = xsize_t(st->st_size);
2a2e32bd
DS
178
179 if (graph_size < GRAPH_MIN_SIZE) {
180 close(fd);
67a530fa 181 error(_("commit-graph file is too small"));
61df89c8 182 return NULL;
2a2e32bd
DS
183 }
184 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
aa658574
JS
185 ret = parse_commit_graph(graph_map, fd, graph_size);
186
a7df60ca
TB
187 if (ret)
188 ret->odb = odb;
189 else {
aa658574
JS
190 munmap(graph_map, graph_size);
191 close(fd);
aa658574
JS
192 }
193
194 return ret;
195}
196
2ac138d5
ÆAB
197static int verify_commit_graph_lite(struct commit_graph *g)
198{
199 /*
200 * Basic validation shared between parse_commit_graph()
201 * which'll be called every time the graph is used, and the
202 * much more expensive verify_commit_graph() used by
203 * "commit-graph verify".
204 *
205 * There should only be very basic checks here to ensure that
206 * we don't e.g. segfault in fill_commit_in_graph(), but
207 * because this is a very hot codepath nothing that e.g. loops
208 * over g->num_commits, or runs a checksum on the commit-graph
209 * itself.
210 */
211 if (!g->chunk_oid_fanout) {
212 error("commit-graph is missing the OID Fanout chunk");
213 return 1;
214 }
215 if (!g->chunk_oid_lookup) {
216 error("commit-graph is missing the OID Lookup chunk");
217 return 1;
218 }
219 if (!g->chunk_commit_data) {
220 error("commit-graph is missing the Commit Data chunk");
221 return 1;
222 }
223
224 return 0;
225}
226
aa658574
JS
227struct commit_graph *parse_commit_graph(void *graph_map, int fd,
228 size_t graph_size)
229{
230 const unsigned char *data, *chunk_lookup;
231 uint32_t i;
232 struct commit_graph *graph;
5cfa438a 233 uint64_t next_chunk_offset;
aa658574
JS
234 uint32_t graph_signature;
235 unsigned char graph_version, hash_version;
236
237 if (!graph_map)
238 return NULL;
239
240 if (graph_size < GRAPH_MIN_SIZE)
241 return NULL;
242
2a2e32bd
DS
243 data = (const unsigned char *)graph_map;
244
245 graph_signature = get_be32(data);
246 if (graph_signature != GRAPH_SIGNATURE) {
93b4405f 247 error(_("commit-graph signature %X does not match signature %X"),
2a2e32bd 248 graph_signature, GRAPH_SIGNATURE);
aa658574 249 return NULL;
2a2e32bd
DS
250 }
251
252 graph_version = *(unsigned char*)(data + 4);
253 if (graph_version != GRAPH_VERSION) {
93b4405f 254 error(_("commit-graph version %X does not match version %X"),
2a2e32bd 255 graph_version, GRAPH_VERSION);
aa658574 256 return NULL;
2a2e32bd
DS
257 }
258
259 hash_version = *(unsigned char*)(data + 5);
c1665998 260 if (hash_version != oid_version()) {
93b4405f 261 error(_("commit-graph hash version %X does not match version %X"),
c1665998 262 hash_version, oid_version());
aa658574 263 return NULL;
2a2e32bd
DS
264 }
265
266 graph = alloc_commit_graph();
267
c1665998 268 graph->hash_len = the_hash_algo->rawsz;
2a2e32bd
DS
269 graph->num_chunks = *(unsigned char*)(data + 6);
270 graph->graph_fd = fd;
271 graph->data = graph_map;
272 graph->data_len = graph_size;
273
2ad4f1a7
SG
274 if (graph_size < GRAPH_HEADER_SIZE +
275 (graph->num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH +
276 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
277 error(_("commit-graph file is too small to hold %u chunks"),
278 graph->num_chunks);
279 free(graph);
280 return NULL;
281 }
282
2a2e32bd 283 chunk_lookup = data + 8;
5cfa438a
SG
284 next_chunk_offset = get_be64(chunk_lookup + 4);
285 for (i = 0; i < graph->num_chunks; i++) {
d2b86fba 286 uint32_t chunk_id;
5cfa438a 287 uint64_t chunk_offset = next_chunk_offset;
2a2e32bd
DS
288 int chunk_repeated = 0;
289
d2b86fba 290 chunk_id = get_be32(chunk_lookup + 0);
d2b86fba 291
2a2e32bd 292 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
5cfa438a 293 next_chunk_offset = get_be64(chunk_lookup + 4);
2a2e32bd 294
c1665998 295 if (chunk_offset > graph_size - the_hash_algo->rawsz) {
93b4405f 296 error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
2a2e32bd 297 (uint32_t)chunk_offset);
aa658574
JS
298 free(graph);
299 return NULL;
2a2e32bd
DS
300 }
301
302 switch (chunk_id) {
303 case GRAPH_CHUNKID_OIDFANOUT:
304 if (graph->chunk_oid_fanout)
305 chunk_repeated = 1;
306 else
307 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
308 break;
309
310 case GRAPH_CHUNKID_OIDLOOKUP:
311 if (graph->chunk_oid_lookup)
312 chunk_repeated = 1;
5cfa438a 313 else {
2a2e32bd 314 graph->chunk_oid_lookup = data + chunk_offset;
5cfa438a
SG
315 graph->num_commits = (next_chunk_offset - chunk_offset)
316 / graph->hash_len;
317 }
2a2e32bd
DS
318 break;
319
320 case GRAPH_CHUNKID_DATA:
321 if (graph->chunk_commit_data)
322 chunk_repeated = 1;
323 else
324 graph->chunk_commit_data = data + chunk_offset;
325 break;
326
5af7417b
SG
327 case GRAPH_CHUNKID_EXTRAEDGES:
328 if (graph->chunk_extra_edges)
2a2e32bd
DS
329 chunk_repeated = 1;
330 else
5af7417b 331 graph->chunk_extra_edges = data + chunk_offset;
2a2e32bd 332 break;
118bd570
DS
333
334 case GRAPH_CHUNKID_BASE:
335 if (graph->chunk_base_graphs)
336 chunk_repeated = 1;
337 else
338 graph->chunk_base_graphs = data + chunk_offset;
76ffbca7
GS
339 break;
340
341 case GRAPH_CHUNKID_BLOOMINDEXES:
342 if (graph->chunk_bloom_indexes)
343 chunk_repeated = 1;
344 else
345 graph->chunk_bloom_indexes = data + chunk_offset;
346 break;
347
348 case GRAPH_CHUNKID_BLOOMDATA:
349 if (graph->chunk_bloom_data)
350 chunk_repeated = 1;
351 else {
352 uint32_t hash_version;
353 graph->chunk_bloom_data = data + chunk_offset;
354 hash_version = get_be32(data + chunk_offset);
355
356 if (hash_version != 1)
357 break;
358
359 graph->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
360 graph->bloom_filter_settings->hash_version = hash_version;
361 graph->bloom_filter_settings->num_hashes = get_be32(data + chunk_offset + 4);
362 graph->bloom_filter_settings->bits_per_entry = get_be32(data + chunk_offset + 8);
363 }
364 break;
2a2e32bd
DS
365 }
366
367 if (chunk_repeated) {
93b4405f 368 error(_("commit-graph chunk id %08x appears multiple times"), chunk_id);
aa658574
JS
369 free(graph);
370 return NULL;
2a2e32bd 371 }
2a2e32bd
DS
372 }
373
76ffbca7
GS
374 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
375 init_bloom_filters();
376 } else {
377 /* We need both the bloom chunks to exist together. Else ignore the data */
378 graph->chunk_bloom_indexes = NULL;
379 graph->chunk_bloom_data = NULL;
380 graph->bloom_filter_settings = NULL;
381 }
382
118bd570
DS
383 hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
384
98552f25
JS
385 if (verify_commit_graph_lite(graph)) {
386 free(graph);
2ac138d5 387 return NULL;
98552f25 388 }
2ac138d5 389
2a2e32bd 390 return graph;
2a2e32bd
DS
391}
392
a7df60ca
TB
393static struct commit_graph *load_commit_graph_one(const char *graph_file,
394 struct object_directory *odb)
61df89c8
ÆAB
395{
396
397 struct stat st;
398 int fd;
6c622f9f 399 struct commit_graph *g;
61df89c8
ÆAB
400 int open_ok = open_commit_graph(graph_file, &fd, &st);
401
402 if (!open_ok)
403 return NULL;
404
a7df60ca 405 g = load_commit_graph_one_fd_st(fd, &st, odb);
6c622f9f
DS
406
407 if (g)
408 g->filename = xstrdup(graph_file);
409
410 return g;
61df89c8
ÆAB
411}
412
13c24992
TB
413static struct commit_graph *load_commit_graph_v1(struct repository *r,
414 struct object_directory *odb)
5c84b339 415{
ad2dd5bb 416 char *graph_name = get_commit_graph_filename(odb);
a7df60ca 417 struct commit_graph *g = load_commit_graph_one(graph_name, odb);
5c84b339
DS
418 free(graph_name);
419
420 return g;
421}
422
423static int add_graph_to_chain(struct commit_graph *g,
424 struct commit_graph *chain,
425 struct object_id *oids,
426 int n)
427{
428 struct commit_graph *cur_g = chain;
429
118bd570
DS
430 if (n && !g->chunk_base_graphs) {
431 warning(_("commit-graph has no base graphs chunk"));
432 return 0;
433 }
434
5c84b339
DS
435 while (n) {
436 n--;
118bd570
DS
437
438 if (!cur_g ||
439 !oideq(&oids[n], &cur_g->oid) ||
440 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
441 warning(_("commit-graph chain does not match"));
442 return 0;
443 }
444
5c84b339
DS
445 cur_g = cur_g->base_graph;
446 }
447
448 g->base_graph = chain;
449
450 if (chain)
451 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
452
453 return 1;
454}
455
13c24992
TB
456static struct commit_graph *load_commit_graph_chain(struct repository *r,
457 struct object_directory *odb)
5c84b339
DS
458{
459 struct commit_graph *graph_chain = NULL;
460 struct strbuf line = STRBUF_INIT;
461 struct stat st;
462 struct object_id *oids;
463 int i = 0, valid = 1, count;
ad2dd5bb 464 char *chain_name = get_chain_filename(odb);
5c84b339
DS
465 FILE *fp;
466 int stat_res;
467
468 fp = fopen(chain_name, "r");
469 stat_res = stat(chain_name, &st);
470 free(chain_name);
471
472 if (!fp ||
473 stat_res ||
474 st.st_size <= the_hash_algo->hexsz)
475 return NULL;
476
477 count = st.st_size / (the_hash_algo->hexsz + 1);
478 oids = xcalloc(count, sizeof(struct object_id));
479
c523035c
DS
480 prepare_alt_odb(r);
481
482 for (i = 0; i < count; i++) {
483 struct object_directory *odb;
5c84b339
DS
484
485 if (strbuf_getline_lf(&line, fp) == EOF)
486 break;
487
488 if (get_oid_hex(line.buf, &oids[i])) {
489 warning(_("invalid commit-graph chain: line '%s' not a hash"),
490 line.buf);
491 valid = 0;
492 break;
493 }
494
c523035c
DS
495 valid = 0;
496 for (odb = r->objects->odb; odb; odb = odb->next) {
ad2dd5bb 497 char *graph_name = get_split_graph_filename(odb, line.buf);
a7df60ca 498 struct commit_graph *g = load_commit_graph_one(graph_name, odb);
5c84b339 499
c523035c
DS
500 free(graph_name);
501
502 if (g) {
c523035c
DS
503 if (add_graph_to_chain(g, graph_chain, oids, i)) {
504 graph_chain = g;
505 valid = 1;
506 }
507
508 break;
509 }
510 }
511
512 if (!valid) {
513 warning(_("unable to find all commit-graph files"));
514 break;
515 }
5c84b339
DS
516 }
517
518 free(oids);
519 fclose(fp);
0aa6bce7 520 strbuf_release(&line);
5c84b339
DS
521
522 return graph_chain;
523}
524
13c24992
TB
525struct commit_graph *read_commit_graph_one(struct repository *r,
526 struct object_directory *odb)
5c84b339 527{
13c24992 528 struct commit_graph *g = load_commit_graph_v1(r, odb);
5c84b339
DS
529
530 if (!g)
13c24992 531 g = load_commit_graph_chain(r, odb);
5c84b339
DS
532
533 return g;
61df89c8
ÆAB
534}
535
13c24992
TB
536static void prepare_commit_graph_one(struct repository *r,
537 struct object_directory *odb)
177722b3 538{
177722b3 539
dade47c0 540 if (r->objects->commit_graph)
177722b3
DS
541 return;
542
13c24992 543 r->objects->commit_graph = read_commit_graph_one(r, odb);
177722b3
DS
544}
545
5faf357b
JT
546/*
547 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
548 *
15beaaa3 549 * On the first invocation, this function attempts to load the commit
5faf357b
JT
550 * graph if the_repository is configured to have one.
551 */
dade47c0 552static int prepare_commit_graph(struct repository *r)
177722b3 553{
263db403 554 struct object_directory *odb;
dade47c0 555
6abada18
JK
556 /*
557 * This must come before the "already attempted?" check below, because
558 * we want to disable even an already-loaded graph file.
559 */
560 if (r->commit_graph_disabled)
561 return 0;
43d35618 562
dade47c0
JT
563 if (r->objects->commit_graph_attempted)
564 return !!r->objects->commit_graph;
565 r->objects->commit_graph_attempted = 1;
566
fbab552a
JK
567 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD, 0))
568 die("dying as requested by the '%s' variable on commit-graph load!",
569 GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD);
570
7211b9e7
DS
571 prepare_repo_settings(r);
572
859fdc0c 573 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
7211b9e7 574 r->settings.core_commit_graph != 1)
dade47c0
JT
575 /*
576 * This repository is not configured to use commit graphs, so
577 * do not load one. (But report commit_graph_attempted anyway
578 * so that commit graph loading is not attempted again for this
579 * repository.)
580 */
5faf357b
JT
581 return 0;
582
d6538246
DS
583 if (!commit_graph_compatible(r))
584 return 0;
585
dade47c0 586 prepare_alt_odb(r);
f0eaf638 587 for (odb = r->objects->odb;
263db403
JK
588 !r->objects->commit_graph && odb;
589 odb = odb->next)
13c24992 590 prepare_commit_graph_one(r, odb);
dade47c0 591 return !!r->objects->commit_graph;
177722b3
DS
592}
593
6cc01743
DS
594int generation_numbers_enabled(struct repository *r)
595{
596 uint32_t first_generation;
597 struct commit_graph *g;
598 if (!prepare_commit_graph(r))
599 return 0;
600
601 g = r->objects->commit_graph;
602
603 if (!g->num_commits)
604 return 0;
605
606 first_generation = get_be32(g->chunk_commit_data +
607 g->hash_len + 8) >> 2;
608
609 return !!first_generation;
610}
611
d4f4d60f
DS
612static void close_commit_graph_one(struct commit_graph *g)
613{
614 if (!g)
615 return;
616
617 close_commit_graph_one(g->base_graph);
618 free_commit_graph(g);
619}
620
c3a3a964 621void close_commit_graph(struct raw_object_store *o)
177722b3 622{
d4f4d60f 623 close_commit_graph_one(o->commit_graph);
c3a3a964 624 o->commit_graph = NULL;
177722b3
DS
625}
626
627static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
628{
629 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
630 g->chunk_oid_lookup, g->hash_len, pos);
631}
632
d4f4d60f
DS
633static void load_oid_from_graph(struct commit_graph *g,
634 uint32_t pos,
635 struct object_id *oid)
636{
637 uint32_t lex_index;
638
639 while (g && pos < g->num_commits_in_base)
640 g = g->base_graph;
641
642 if (!g)
643 BUG("NULL commit-graph");
644
645 if (pos >= g->num_commits + g->num_commits_in_base)
646 die(_("invalid commit position. commit-graph is likely corrupt"));
647
648 lex_index = pos - g->num_commits_in_base;
649
650 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
651}
652
4f542b7a
SB
653static struct commit_list **insert_parent_or_die(struct repository *r,
654 struct commit_graph *g,
d4f4d60f 655 uint32_t pos,
177722b3
DS
656 struct commit_list **pptr)
657{
658 struct commit *c;
659 struct object_id oid;
96af91d4 660
d4f4d60f
DS
661 if (pos >= g->num_commits + g->num_commits_in_base)
662 die("invalid parent position %"PRIu32, pos);
53614b13 663
d4f4d60f 664 load_oid_from_graph(g, pos, &oid);
4f542b7a 665 c = lookup_commit(r, &oid);
177722b3 666 if (!c)
4f5b532d 667 die(_("could not find commit %s"), oid_to_hex(&oid));
177722b3
DS
668 c->graph_pos = pos;
669 return &commit_list_insert(c, pptr)->next;
670}
671
e2838d85
DS
672static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
673{
d4f4d60f
DS
674 const unsigned char *commit_data;
675 uint32_t lex_index;
676
677 while (pos < g->num_commits_in_base)
678 g = g->base_graph;
679
680 lex_index = pos - g->num_commits_in_base;
681 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
e2838d85
DS
682 item->graph_pos = pos;
683 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
684}
685
a133c40b
NTND
686static inline void set_commit_tree(struct commit *c, struct tree *t)
687{
688 c->maybe_tree = t;
689}
690
4f542b7a
SB
691static int fill_commit_in_graph(struct repository *r,
692 struct commit *item,
693 struct commit_graph *g, uint32_t pos)
177722b3 694{
177722b3
DS
695 uint32_t edge_value;
696 uint32_t *parent_data_ptr;
697 uint64_t date_low, date_high;
698 struct commit_list **pptr;
d4f4d60f
DS
699 const unsigned char *commit_data;
700 uint32_t lex_index;
177722b3 701
d4f4d60f
DS
702 while (pos < g->num_commits_in_base)
703 g = g->base_graph;
704
705 if (pos >= g->num_commits + g->num_commits_in_base)
706 die(_("invalid commit position. commit-graph is likely corrupt"));
707
708 /*
709 * Store the "full" position, but then use the
710 * "local" position for the rest of the calculation.
711 */
177722b3 712 item->graph_pos = pos;
d4f4d60f
DS
713 lex_index = pos - g->num_commits_in_base;
714
715 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
716
717 item->object.parsed = 1;
177722b3 718
a133c40b 719 set_commit_tree(item, NULL);
177722b3
DS
720
721 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
722 date_low = get_be32(commit_data + g->hash_len + 12);
723 item->date = (timestamp_t)((date_high << 32) | date_low);
724
83073cc9
DS
725 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
726
177722b3
DS
727 pptr = &item->parents;
728
729 edge_value = get_be32(commit_data + g->hash_len);
730 if (edge_value == GRAPH_PARENT_NONE)
731 return 1;
4f542b7a 732 pptr = insert_parent_or_die(r, g, edge_value, pptr);
177722b3
DS
733
734 edge_value = get_be32(commit_data + g->hash_len + 4);
735 if (edge_value == GRAPH_PARENT_NONE)
736 return 1;
5af7417b 737 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
4f542b7a 738 pptr = insert_parent_or_die(r, g, edge_value, pptr);
177722b3
DS
739 return 1;
740 }
741
5af7417b 742 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
177722b3
DS
743 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
744 do {
745 edge_value = get_be32(parent_data_ptr);
4f542b7a 746 pptr = insert_parent_or_die(r, g,
177722b3
DS
747 edge_value & GRAPH_EDGE_LAST_MASK,
748 pptr);
749 parent_data_ptr++;
750 } while (!(edge_value & GRAPH_LAST_EDGE));
751
752 return 1;
753}
754
e2838d85
DS
755static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
756{
757 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
758 *pos = item->graph_pos;
759 return 1;
760 } else {
d4f4d60f
DS
761 struct commit_graph *cur_g = g;
762 uint32_t lex_index;
763
764 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
765 cur_g = cur_g->base_graph;
766
767 if (cur_g) {
768 *pos = lex_index + cur_g->num_commits_in_base;
769 return 1;
770 }
771
772 return 0;
e2838d85
DS
773 }
774}
775
4f542b7a
SB
776static int parse_commit_in_graph_one(struct repository *r,
777 struct commit_graph *g,
778 struct commit *item)
177722b3 779{
e2838d85
DS
780 uint32_t pos;
781
177722b3
DS
782 if (item->object.parsed)
783 return 1;
ee797053
DS
784
785 if (find_commit_in_graph(item, g, &pos))
4f542b7a 786 return fill_commit_in_graph(r, item, g, pos);
ee797053
DS
787
788 return 0;
789}
790
dade47c0 791int parse_commit_in_graph(struct repository *r, struct commit *item)
ee797053 792{
dade47c0 793 if (!prepare_commit_graph(r))
ee797053 794 return 0;
4f542b7a 795 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
177722b3
DS
796}
797
dade47c0 798void load_commit_graph_info(struct repository *r, struct commit *item)
e2838d85
DS
799{
800 uint32_t pos;
dade47c0 801 if (!prepare_commit_graph(r))
e2838d85 802 return;
dade47c0
JT
803 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
804 fill_commit_graph_info(item, r->objects->commit_graph, pos);
e2838d85
DS
805}
806
4f542b7a
SB
807static struct tree *load_tree_for_commit(struct repository *r,
808 struct commit_graph *g,
809 struct commit *c)
7b8a21db
DS
810{
811 struct object_id oid;
d4f4d60f
DS
812 const unsigned char *commit_data;
813
814 while (c->graph_pos < g->num_commits_in_base)
815 g = g->base_graph;
816
817 commit_data = g->chunk_commit_data +
818 GRAPH_DATA_WIDTH * (c->graph_pos - g->num_commits_in_base);
7b8a21db
DS
819
820 hashcpy(oid.hash, commit_data);
a133c40b 821 set_commit_tree(c, lookup_tree(r, &oid));
7b8a21db
DS
822
823 return c->maybe_tree;
824}
825
4f542b7a
SB
826static struct tree *get_commit_tree_in_graph_one(struct repository *r,
827 struct commit_graph *g,
0cbef8f8 828 const struct commit *c)
7b8a21db
DS
829{
830 if (c->maybe_tree)
831 return c->maybe_tree;
832 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
0cbef8f8
DS
833 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
834
4f542b7a 835 return load_tree_for_commit(r, g, (struct commit *)c);
0cbef8f8 836}
7b8a21db 837
dade47c0 838struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
0cbef8f8 839{
4f542b7a 840 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
7b8a21db
DS
841}
842
c9905bea
DS
843struct packed_commit_list {
844 struct commit **list;
845 int nr;
846 int alloc;
847};
848
849struct packed_oid_list {
850 struct object_id *list;
851 int nr;
852 int alloc;
853};
854
855struct write_commit_graph_context {
856 struct repository *r;
0bd52e27 857 struct object_directory *odb;
c9905bea
DS
858 char *graph_name;
859 struct packed_oid_list oids;
860 struct packed_commit_list commits;
861 int num_extra_edges;
862 unsigned long approx_nr_objects;
863 struct progress *progress;
864 int progress_done;
865 uint64_t progress_cnt;
6c622f9f
DS
866
867 char *base_graph_name;
868 int num_commit_graphs_before;
869 int num_commit_graphs_after;
870 char **commit_graph_filenames_before;
871 char **commit_graph_filenames_after;
872 char **commit_graph_hash_after;
873 uint32_t new_num_commits_in_base;
874 struct commit_graph *new_base_graph;
875
c9905bea 876 unsigned append:1,
6c622f9f 877 report_progress:1,
7c5c9b9c 878 split:1,
f97b9325 879 check_oids:1,
3d112755
GS
880 changed_paths:1,
881 order_by_pack:1;
c2bc6e6a
DS
882
883 const struct split_commit_graph_opts *split_opts;
f97b9325 884 size_t total_bloom_filter_data_size;
c9905bea
DS
885};
886
08fd81c9 887static void write_graph_chunk_fanout(struct hashfile *f,
c9905bea 888 struct write_commit_graph_context *ctx)
08fd81c9
DS
889{
890 int i, count = 0;
c9905bea 891 struct commit **list = ctx->commits.list;
08fd81c9
DS
892
893 /*
894 * Write the first-level table (the list is sorted,
895 * but we use a 256-entry lookup to be able to avoid
896 * having to do eight extra binary search iterations).
897 */
898 for (i = 0; i < 256; i++) {
c9905bea 899 while (count < ctx->commits.nr) {
08fd81c9
DS
900 if ((*list)->object.oid.hash[0] != i)
901 break;
c9905bea 902 display_progress(ctx->progress, ++ctx->progress_cnt);
08fd81c9
DS
903 count++;
904 list++;
905 }
906
907 hashwrite_be32(f, count);
908 }
909}
910
911static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
c9905bea 912 struct write_commit_graph_context *ctx)
08fd81c9 913{
c9905bea 914 struct commit **list = ctx->commits.list;
08fd81c9 915 int count;
c9905bea
DS
916 for (count = 0; count < ctx->commits.nr; count++, list++) {
917 display_progress(ctx->progress, ++ctx->progress_cnt);
08fd81c9 918 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
53035c4f 919 }
08fd81c9
DS
920}
921
922static const unsigned char *commit_to_sha1(size_t index, void *table)
923{
924 struct commit **commits = table;
925 return commits[index]->object.oid.hash;
926}
927
928static void write_graph_chunk_data(struct hashfile *f, int hash_len,
c9905bea 929 struct write_commit_graph_context *ctx)
08fd81c9 930{
c9905bea
DS
931 struct commit **list = ctx->commits.list;
932 struct commit **last = ctx->commits.list + ctx->commits.nr;
08fd81c9
DS
933 uint32_t num_extra_edges = 0;
934
935 while (list < last) {
936 struct commit_list *parent;
806278de 937 struct object_id *tree;
08fd81c9
DS
938 int edge_value;
939 uint32_t packedDate[2];
c9905bea 940 display_progress(ctx->progress, ++ctx->progress_cnt);
08fd81c9 941
16749b8d
TB
942 if (parse_commit_no_graph(*list))
943 die(_("unable to parse commit %s"),
944 oid_to_hex(&(*list)->object.oid));
806278de 945 tree = get_commit_tree_oid(*list);
806278de 946 hashwrite(f, tree->hash, hash_len);
08fd81c9
DS
947
948 parent = (*list)->parents;
949
950 if (!parent)
951 edge_value = GRAPH_PARENT_NONE;
952 else {
953 edge_value = sha1_pos(parent->item->object.oid.hash,
c9905bea
DS
954 ctx->commits.list,
955 ctx->commits.nr,
08fd81c9
DS
956 commit_to_sha1);
957
6c622f9f
DS
958 if (edge_value >= 0)
959 edge_value += ctx->new_num_commits_in_base;
960 else {
961 uint32_t pos;
962 if (find_commit_in_graph(parent->item,
963 ctx->new_base_graph,
964 &pos))
965 edge_value = pos;
966 }
967
08fd81c9 968 if (edge_value < 0)
cce99cd8
DS
969 BUG("missing parent %s for commit %s",
970 oid_to_hex(&parent->item->object.oid),
971 oid_to_hex(&(*list)->object.oid));
08fd81c9
DS
972 }
973
974 hashwrite_be32(f, edge_value);
975
976 if (parent)
977 parent = parent->next;
978
979 if (!parent)
980 edge_value = GRAPH_PARENT_NONE;
981 else if (parent->next)
5af7417b 982 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
08fd81c9
DS
983 else {
984 edge_value = sha1_pos(parent->item->object.oid.hash,
c9905bea
DS
985 ctx->commits.list,
986 ctx->commits.nr,
08fd81c9 987 commit_to_sha1);
6c622f9f
DS
988
989 if (edge_value >= 0)
990 edge_value += ctx->new_num_commits_in_base;
991 else {
992 uint32_t pos;
993 if (find_commit_in_graph(parent->item,
994 ctx->new_base_graph,
995 &pos))
996 edge_value = pos;
997 }
998
08fd81c9 999 if (edge_value < 0)
cce99cd8
DS
1000 BUG("missing parent %s for commit %s",
1001 oid_to_hex(&parent->item->object.oid),
1002 oid_to_hex(&(*list)->object.oid));
08fd81c9
DS
1003 }
1004
1005 hashwrite_be32(f, edge_value);
1006
5af7417b 1007 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
08fd81c9
DS
1008 do {
1009 num_extra_edges++;
1010 parent = parent->next;
1011 } while (parent);
1012 }
1013
1014 if (sizeof((*list)->date) > 4)
1015 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1016 else
1017 packedDate[0] = 0;
1018
3258c663
DS
1019 packedDate[0] |= htonl((*list)->generation << 2);
1020
08fd81c9
DS
1021 packedDate[1] = htonl((*list)->date);
1022 hashwrite(f, packedDate, 8);
1023
1024 list++;
1025 }
1026}
1027
5af7417b 1028static void write_graph_chunk_extra_edges(struct hashfile *f,
c9905bea 1029 struct write_commit_graph_context *ctx)
08fd81c9 1030{
c9905bea
DS
1031 struct commit **list = ctx->commits.list;
1032 struct commit **last = ctx->commits.list + ctx->commits.nr;
08fd81c9
DS
1033 struct commit_list *parent;
1034
1035 while (list < last) {
1036 int num_parents = 0;
53035c4f 1037
c9905bea 1038 display_progress(ctx->progress, ++ctx->progress_cnt);
53035c4f 1039
08fd81c9
DS
1040 for (parent = (*list)->parents; num_parents < 3 && parent;
1041 parent = parent->next)
1042 num_parents++;
1043
1044 if (num_parents <= 2) {
1045 list++;
1046 continue;
1047 }
1048
1049 /* Since num_parents > 2, this initializer is safe. */
1050 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1051 int edge_value = sha1_pos(parent->item->object.oid.hash,
c9905bea
DS
1052 ctx->commits.list,
1053 ctx->commits.nr,
08fd81c9
DS
1054 commit_to_sha1);
1055
6c622f9f
DS
1056 if (edge_value >= 0)
1057 edge_value += ctx->new_num_commits_in_base;
1058 else {
1059 uint32_t pos;
1060 if (find_commit_in_graph(parent->item,
1061 ctx->new_base_graph,
1062 &pos))
1063 edge_value = pos;
1064 }
1065
08fd81c9 1066 if (edge_value < 0)
cce99cd8
DS
1067 BUG("missing parent %s for commit %s",
1068 oid_to_hex(&parent->item->object.oid),
1069 oid_to_hex(&(*list)->object.oid));
08fd81c9
DS
1070 else if (!parent->next)
1071 edge_value |= GRAPH_LAST_EDGE;
1072
1073 hashwrite_be32(f, edge_value);
1074 }
1075
1076 list++;
1077 }
1078}
1079
76ffbca7
GS
1080static void write_graph_chunk_bloom_indexes(struct hashfile *f,
1081 struct write_commit_graph_context *ctx)
1082{
1083 struct commit **list = ctx->commits.list;
1084 struct commit **last = ctx->commits.list + ctx->commits.nr;
1085 uint32_t cur_pos = 0;
1086 struct progress *progress = NULL;
1087 int i = 0;
1088
1089 if (ctx->report_progress)
1090 progress = start_delayed_progress(
1091 _("Writing changed paths Bloom filters index"),
1092 ctx->commits.nr);
1093
1094 while (list < last) {
1217c03e 1095 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list, 0);
76ffbca7
GS
1096 cur_pos += filter->len;
1097 display_progress(progress, ++i);
1098 hashwrite_be32(f, cur_pos);
1099 list++;
1100 }
1101
1102 stop_progress(&progress);
1103}
1104
1105static void write_graph_chunk_bloom_data(struct hashfile *f,
1106 struct write_commit_graph_context *ctx,
1107 const struct bloom_filter_settings *settings)
1108{
1109 struct commit **list = ctx->commits.list;
1110 struct commit **last = ctx->commits.list + ctx->commits.nr;
1111 struct progress *progress = NULL;
1112 int i = 0;
1113
1114 if (ctx->report_progress)
1115 progress = start_delayed_progress(
1116 _("Writing changed paths Bloom filters data"),
1117 ctx->commits.nr);
1118
1119 hashwrite_be32(f, settings->hash_version);
1120 hashwrite_be32(f, settings->num_hashes);
1121 hashwrite_be32(f, settings->bits_per_entry);
1122
1123 while (list < last) {
1217c03e 1124 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list, 0);
76ffbca7
GS
1125 display_progress(progress, ++i);
1126 hashwrite(f, filter->data, filter->len * sizeof(unsigned char));
1127 list++;
1128 }
1129
1130 stop_progress(&progress);
1131}
1132
3cbc6ed3 1133static int oid_compare(const void *_a, const void *_b)
08fd81c9
DS
1134{
1135 const struct object_id *a = (const struct object_id *)_a;
1136 const struct object_id *b = (const struct object_id *)_b;
1137 return oidcmp(a, b);
1138}
1139
08fd81c9
DS
1140static int add_packed_commits(const struct object_id *oid,
1141 struct packed_git *pack,
1142 uint32_t pos,
1143 void *data)
1144{
c9905bea 1145 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
08fd81c9
DS
1146 enum object_type type;
1147 off_t offset = nth_packed_object_offset(pack, pos);
1148 struct object_info oi = OBJECT_INFO_INIT;
1149
c9905bea
DS
1150 if (ctx->progress)
1151 display_progress(ctx->progress, ++ctx->progress_done);
7b0f2292 1152
08fd81c9 1153 oi.typep = &type;
c9905bea 1154 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
4f5b532d 1155 die(_("unable to get type of object %s"), oid_to_hex(oid));
08fd81c9
DS
1156
1157 if (type != OBJ_COMMIT)
1158 return 0;
1159
c9905bea
DS
1160 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1161 oidcpy(&(ctx->oids.list[ctx->oids.nr]), oid);
1162 ctx->oids.nr++;
08fd81c9 1163
d21ee7d1
JK
1164 set_commit_pos(ctx->r, oid);
1165
08fd81c9
DS
1166 return 0;
1167}
1168
c9905bea 1169static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
4f2542b4
DS
1170{
1171 struct commit_list *parent;
1172 for (parent = commit->parents; parent; parent = parent->next) {
cb99a34e 1173 if (!(parent->item->object.flags & REACHABLE)) {
c9905bea
DS
1174 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1175 oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid));
1176 ctx->oids.nr++;
cb99a34e 1177 parent->item->object.flags |= REACHABLE;
4f2542b4
DS
1178 }
1179 }
1180}
1181
c9905bea 1182static void close_reachable(struct write_commit_graph_context *ctx)
4f2542b4 1183{
49bbc57a 1184 int i;
4f2542b4
DS
1185 struct commit *commit;
1186
c9905bea
DS
1187 if (ctx->report_progress)
1188 ctx->progress = start_delayed_progress(
1189 _("Loading known commits in commit graph"),
1190 ctx->oids.nr);
1191 for (i = 0; i < ctx->oids.nr; i++) {
1192 display_progress(ctx->progress, i + 1);
1193 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
4f2542b4 1194 if (commit)
cb99a34e 1195 commit->object.flags |= REACHABLE;
4f2542b4 1196 }
c9905bea 1197 stop_progress(&ctx->progress);
4f2542b4
DS
1198
1199 /*
c9905bea 1200 * As this loop runs, ctx->oids.nr may grow, but not more
4f2542b4
DS
1201 * than the number of missing commits in the reachable
1202 * closure.
1203 */
c9905bea
DS
1204 if (ctx->report_progress)
1205 ctx->progress = start_delayed_progress(
1206 _("Expanding reachable commits in commit graph"),
67fa6aac 1207 0);
c9905bea
DS
1208 for (i = 0; i < ctx->oids.nr; i++) {
1209 display_progress(ctx->progress, i + 1);
1210 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
4f2542b4 1211
6c622f9f
DS
1212 if (!commit)
1213 continue;
1214 if (ctx->split) {
1215 if (!parse_commit(commit) &&
1216 commit->graph_pos == COMMIT_NOT_FROM_GRAPH)
1217 add_missing_parents(ctx, commit);
1218 } else if (!parse_commit_no_graph(commit))
c9905bea 1219 add_missing_parents(ctx, commit);
4f2542b4 1220 }
c9905bea 1221 stop_progress(&ctx->progress);
4f2542b4 1222
c9905bea
DS
1223 if (ctx->report_progress)
1224 ctx->progress = start_delayed_progress(
1225 _("Clearing commit marks in commit graph"),
1226 ctx->oids.nr);
1227 for (i = 0; i < ctx->oids.nr; i++) {
1228 display_progress(ctx->progress, i + 1);
1229 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
4f2542b4
DS
1230
1231 if (commit)
cb99a34e 1232 commit->object.flags &= ~REACHABLE;
4f2542b4 1233 }
c9905bea 1234 stop_progress(&ctx->progress);
4f2542b4
DS
1235}
1236
c9905bea 1237static void compute_generation_numbers(struct write_commit_graph_context *ctx)
3258c663
DS
1238{
1239 int i;
1240 struct commit_list *list = NULL;
1241
c9905bea 1242 if (ctx->report_progress)
ecc08690 1243 ctx->progress = start_delayed_progress(
c9905bea
DS
1244 _("Computing commit graph generation numbers"),
1245 ctx->commits.nr);
1246 for (i = 0; i < ctx->commits.nr; i++) {
1247 display_progress(ctx->progress, i + 1);
1248 if (ctx->commits.list[i]->generation != GENERATION_NUMBER_INFINITY &&
1249 ctx->commits.list[i]->generation != GENERATION_NUMBER_ZERO)
3258c663
DS
1250 continue;
1251
c9905bea 1252 commit_list_insert(ctx->commits.list[i], &list);
3258c663
DS
1253 while (list) {
1254 struct commit *current = list->item;
1255 struct commit_list *parent;
1256 int all_parents_computed = 1;
1257 uint32_t max_generation = 0;
1258
1259 for (parent = current->parents; parent; parent = parent->next) {
1260 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
1261 parent->item->generation == GENERATION_NUMBER_ZERO) {
1262 all_parents_computed = 0;
1263 commit_list_insert(parent->item, &list);
1264 break;
1265 } else if (parent->item->generation > max_generation) {
1266 max_generation = parent->item->generation;
1267 }
1268 }
1269
1270 if (all_parents_computed) {
1271 current->generation = max_generation + 1;
1272 pop_commit(&list);
1273
1274 if (current->generation > GENERATION_NUMBER_MAX)
1275 current->generation = GENERATION_NUMBER_MAX;
1276 }
1277 }
1278 }
c9905bea 1279 stop_progress(&ctx->progress);
3258c663
DS
1280}
1281
f97b9325
GS
1282static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1283{
1284 int i;
1285 struct progress *progress = NULL;
d21ee7d1 1286 struct commit **sorted_commits;
f97b9325
GS
1287
1288 init_bloom_filters();
1289
1290 if (ctx->report_progress)
1291 progress = start_delayed_progress(
1292 _("Computing commit changed paths Bloom filters"),
1293 ctx->commits.nr);
1294
d21ee7d1
JK
1295 ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1296 COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
3d112755
GS
1297
1298 if (ctx->order_by_pack)
1299 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1300 else
1301 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
d21ee7d1 1302
f97b9325 1303 for (i = 0; i < ctx->commits.nr; i++) {
d21ee7d1 1304 struct commit *c = sorted_commits[i];
1217c03e 1305 struct bloom_filter *filter = get_bloom_filter(ctx->r, c, 1);
f97b9325
GS
1306 ctx->total_bloom_filter_data_size += sizeof(unsigned char) * filter->len;
1307 display_progress(progress, i + 1);
1308 }
1309
d21ee7d1 1310 free(sorted_commits);
f97b9325
GS
1311 stop_progress(&progress);
1312}
1313
59fb8770
DS
1314static int add_ref_to_list(const char *refname,
1315 const struct object_id *oid,
1316 int flags, void *cb_data)
1317{
1318 struct string_list *list = (struct string_list *)cb_data;
1319
1320 string_list_append(list, oid_to_hex(oid));
1321 return 0;
1322}
1323
0bd52e27 1324int write_commit_graph_reachable(struct object_directory *odb,
39d88318 1325 enum commit_graph_write_flags flags,
c2bc6e6a 1326 const struct split_commit_graph_opts *split_opts)
59fb8770 1327{
f4dbdfc4 1328 struct string_list list = STRING_LIST_INIT_DUP;
e103f727 1329 int result;
59fb8770 1330
59fb8770 1331 for_each_ref(add_ref_to_list, &list);
0bd52e27 1332 result = write_commit_graph(odb, NULL, &list,
c2bc6e6a 1333 flags, split_opts);
f4dbdfc4
DS
1334
1335 string_list_clear(&list, 0);
e103f727 1336 return result;
59fb8770
DS
1337}
1338
ef5b83f2
DS
1339static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1340 struct string_list *pack_indexes)
08fd81c9 1341{
ef5b83f2 1342 uint32_t i;
28944739 1343 struct strbuf progress_title = STRBUF_INIT;
ef5b83f2
DS
1344 struct strbuf packname = STRBUF_INIT;
1345 int dirlen;
08fd81c9 1346
0bd52e27 1347 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
ef5b83f2
DS
1348 dirlen = packname.len;
1349 if (ctx->report_progress) {
1350 strbuf_addf(&progress_title,
1351 Q_("Finding commits for commit graph in %d pack",
1352 "Finding commits for commit graph in %d packs",
1353 pack_indexes->nr),
1354 pack_indexes->nr);
1355 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1356 ctx->progress_done = 0;
7547b95b 1357 }
ef5b83f2
DS
1358 for (i = 0; i < pack_indexes->nr; i++) {
1359 struct packed_git *p;
1360 strbuf_setlen(&packname, dirlen);
1361 strbuf_addstr(&packname, pack_indexes->items[i].string);
1362 p = add_packed_git(packname.buf, packname.len, 1);
1363 if (!p) {
1364 error(_("error adding pack %s"), packname.buf);
1365 return -1;
7547b95b 1366 }
ef5b83f2
DS
1367 if (open_pack_index(p)) {
1368 error(_("error opening index for %s"), packname.buf);
1369 return -1;
1370 }
1371 for_each_object_in_pack(p, add_packed_commits, ctx,
1372 FOR_EACH_OBJECT_PACK_ORDER);
1373 close_pack(p);
1374 free(p);
7547b95b
DS
1375 }
1376
ef5b83f2 1377 stop_progress(&ctx->progress);
0aa6bce7 1378 strbuf_release(&progress_title);
ef5b83f2
DS
1379 strbuf_release(&packname);
1380
1381 return 0;
1382}
1383
7c5c9b9c
SG
1384static int fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
1385 struct string_list *commit_hex)
4c9efe85
DS
1386{
1387 uint32_t i;
1388 struct strbuf progress_title = STRBUF_INIT;
1389
1390 if (ctx->report_progress) {
1391 strbuf_addf(&progress_title,
1392 Q_("Finding commits for commit graph from %d ref",
1393 "Finding commits for commit graph from %d refs",
1394 commit_hex->nr),
1395 commit_hex->nr);
1396 ctx->progress = start_delayed_progress(
1397 progress_title.buf,
1398 commit_hex->nr);
3d5df01b 1399 }
4c9efe85
DS
1400 for (i = 0; i < commit_hex->nr; i++) {
1401 const char *end;
1402 struct object_id oid;
1403 struct commit *result;
1404
1405 display_progress(ctx->progress, i + 1);
7c5c9b9c
SG
1406 if (!parse_oid_hex(commit_hex->items[i].string, &oid, &end) &&
1407 (result = lookup_commit_reference_gently(ctx->r, &oid, 1))) {
4c9efe85
DS
1408 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1409 oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid));
1410 ctx->oids.nr++;
7c5c9b9c
SG
1411 } else if (ctx->check_oids) {
1412 error(_("invalid commit object id: %s"),
1413 commit_hex->items[i].string);
1414 return -1;
3d5df01b
DS
1415 }
1416 }
4c9efe85
DS
1417 stop_progress(&ctx->progress);
1418 strbuf_release(&progress_title);
7c5c9b9c
SG
1419
1420 return 0;
4c9efe85 1421}
3d5df01b 1422
b2c83060
DS
1423static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1424{
1425 if (ctx->report_progress)
1426 ctx->progress = start_delayed_progress(
1427 _("Finding commits for commit graph among packed objects"),
1428 ctx->approx_nr_objects);
1429 for_each_packed_object(add_packed_commits, ctx,
1430 FOR_EACH_OBJECT_PACK_ORDER);
1431 if (ctx->progress_done < ctx->approx_nr_objects)
1432 display_progress(ctx->progress, ctx->approx_nr_objects);
1433 stop_progress(&ctx->progress);
1434}
049d51a2 1435
014e3440
DS
1436static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
1437{
1438 uint32_t i, count_distinct = 1;
08fd81c9 1439
014e3440
DS
1440 if (ctx->report_progress)
1441 ctx->progress = start_delayed_progress(
890226cc 1442 _("Counting distinct commits in commit graph"),
014e3440
DS
1443 ctx->oids.nr);
1444 display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
3cbc6ed3 1445 QSORT(ctx->oids.list, ctx->oids.nr, oid_compare);
014e3440
DS
1446
1447 for (i = 1; i < ctx->oids.nr; i++) {
1448 display_progress(ctx->progress, i + 1);
6c622f9f
DS
1449 if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i])) {
1450 if (ctx->split) {
1451 struct commit *c = lookup_commit(ctx->r, &ctx->oids.list[i]);
1452
1453 if (!c || c->graph_pos != COMMIT_NOT_FROM_GRAPH)
1454 continue;
1455 }
1456
08fd81c9 1457 count_distinct++;
6c622f9f 1458 }
08fd81c9 1459 }
014e3440 1460 stop_progress(&ctx->progress);
08fd81c9 1461
014e3440
DS
1462 return count_distinct;
1463}
08fd81c9 1464
f998d542
DS
1465static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1466{
1467 uint32_t i;
08fd81c9 1468
f998d542
DS
1469 ctx->num_extra_edges = 0;
1470 if (ctx->report_progress)
1471 ctx->progress = start_delayed_progress(
890226cc 1472 _("Finding extra edges in commit graph"),
f998d542
DS
1473 ctx->oids.nr);
1474 for (i = 0; i < ctx->oids.nr; i++) {
689a146c
RS
1475 unsigned int num_parents;
1476
f998d542
DS
1477 display_progress(ctx->progress, i + 1);
1478 if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
08fd81c9
DS
1479 continue;
1480
6c622f9f 1481 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
f998d542 1482 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.list[i]);
6c622f9f
DS
1483
1484 if (ctx->split &&
1485 ctx->commits.list[ctx->commits.nr]->graph_pos != COMMIT_NOT_FROM_GRAPH)
1486 continue;
1487
f998d542 1488 parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
08fd81c9 1489
689a146c 1490 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
08fd81c9 1491 if (num_parents > 2)
f998d542 1492 ctx->num_extra_edges += num_parents - 1;
08fd81c9 1493
f998d542 1494 ctx->commits.nr++;
08fd81c9 1495 }
f998d542
DS
1496 stop_progress(&ctx->progress);
1497}
08fd81c9 1498
6c622f9f
DS
1499static int write_graph_chunk_base_1(struct hashfile *f,
1500 struct commit_graph *g)
1501{
1502 int num = 0;
1503
1504 if (!g)
1505 return 0;
1506
1507 num = write_graph_chunk_base_1(f, g->base_graph);
1508 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1509 return num + 1;
1510}
1511
1512static int write_graph_chunk_base(struct hashfile *f,
1513 struct write_commit_graph_context *ctx)
1514{
1515 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1516
1517 if (num != ctx->num_commit_graphs_after - 1) {
1518 error(_("failed to write correct number of base graph ids"));
1519 return -1;
1520 }
1521
1522 return 0;
1523}
1524
238def57 1525static int write_commit_graph_file(struct write_commit_graph_context *ctx)
08fd81c9 1526{
238def57 1527 uint32_t i;
6c622f9f 1528 int fd;
08fd81c9 1529 struct hashfile *f;
08fd81c9 1530 struct lock_file lk = LOCK_INIT;
3be7efca
GS
1531 uint32_t chunk_ids[MAX_NUM_CHUNKS + 1];
1532 uint64_t chunk_offsets[MAX_NUM_CHUNKS + 1];
c1665998 1533 const unsigned hashsz = the_hash_algo->rawsz;
28944739 1534 struct strbuf progress_title = STRBUF_INIT;
144354b0 1535 int num_chunks = 3;
6c622f9f 1536 struct object_id file_hash;
76ffbca7 1537 const struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
6c622f9f
DS
1538
1539 if (ctx->split) {
1540 struct strbuf tmp_file = STRBUF_INIT;
1541
1542 strbuf_addf(&tmp_file,
1543 "%s/info/commit-graphs/tmp_graph_XXXXXX",
0bd52e27 1544 ctx->odb->path);
6c622f9f
DS
1545 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1546 } else {
ad2dd5bb 1547 ctx->graph_name = get_commit_graph_filename(ctx->odb);
6c622f9f 1548 }
238def57 1549
238def57
DS
1550 if (safe_create_leading_directories(ctx->graph_name)) {
1551 UNLEAK(ctx->graph_name);
1552 error(_("unable to create leading directories of %s"),
1553 ctx->graph_name);
1554 return -1;
f4dbdfc4 1555 }
08fd81c9 1556
6c622f9f 1557 if (ctx->split) {
ad2dd5bb 1558 char *lock_name = get_chain_filename(ctx->odb);
08fd81c9 1559
6c622f9f 1560 hold_lock_file_for_update(&lk, lock_name, LOCK_DIE_ON_ERROR);
08fd81c9 1561
6c622f9f
DS
1562 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1563 if (fd < 0) {
1564 error(_("unable to create '%s'"), ctx->graph_name);
1565 return -1;
1566 }
1567
1568 f = hashfd(fd, ctx->graph_name);
1569 } else {
1570 hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR);
1571 fd = lk.tempfile->fd;
1572 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1573 }
08fd81c9
DS
1574
1575 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
1576 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
1577 chunk_ids[2] = GRAPH_CHUNKID_DATA;
144354b0
DS
1578 if (ctx->num_extra_edges) {
1579 chunk_ids[num_chunks] = GRAPH_CHUNKID_EXTRAEDGES;
1580 num_chunks++;
1581 }
76ffbca7
GS
1582 if (ctx->changed_paths) {
1583 chunk_ids[num_chunks] = GRAPH_CHUNKID_BLOOMINDEXES;
1584 num_chunks++;
1585 chunk_ids[num_chunks] = GRAPH_CHUNKID_BLOOMDATA;
1586 num_chunks++;
1587 }
6c622f9f
DS
1588 if (ctx->num_commit_graphs_after > 1) {
1589 chunk_ids[num_chunks] = GRAPH_CHUNKID_BASE;
1590 num_chunks++;
1591 }
144354b0
DS
1592
1593 chunk_ids[num_chunks] = 0;
08fd81c9
DS
1594
1595 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1596 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
238def57
DS
1597 chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
1598 chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
144354b0
DS
1599
1600 num_chunks = 3;
1601 if (ctx->num_extra_edges) {
1602 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1603 4 * ctx->num_extra_edges;
1604 num_chunks++;
1605 }
76ffbca7
GS
1606 if (ctx->changed_paths) {
1607 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1608 sizeof(uint32_t) * ctx->commits.nr;
1609 num_chunks++;
1610
1611 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1612 sizeof(uint32_t) * 3 + ctx->total_bloom_filter_data_size;
1613 num_chunks++;
1614 }
6c622f9f
DS
1615 if (ctx->num_commit_graphs_after > 1) {
1616 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1617 hashsz * (ctx->num_commit_graphs_after - 1);
1618 num_chunks++;
1619 }
144354b0
DS
1620
1621 hashwrite_be32(f, GRAPH_SIGNATURE);
1622
1623 hashwrite_u8(f, GRAPH_VERSION);
1624 hashwrite_u8(f, oid_version());
1625 hashwrite_u8(f, num_chunks);
6c622f9f 1626 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
08fd81c9
DS
1627
1628 for (i = 0; i <= num_chunks; i++) {
1629 uint32_t chunk_write[3];
1630
1631 chunk_write[0] = htonl(chunk_ids[i]);
1632 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
1633 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
1634 hashwrite(f, chunk_write, 12);
1635 }
1636
238def57 1637 if (ctx->report_progress) {
28944739
ÆAB
1638 strbuf_addf(&progress_title,
1639 Q_("Writing out commit graph in %d pass",
1640 "Writing out commit graph in %d passes",
1641 num_chunks),
1642 num_chunks);
238def57 1643 ctx->progress = start_delayed_progress(
28944739 1644 progress_title.buf,
238def57 1645 num_chunks * ctx->commits.nr);
28944739 1646 }
238def57
DS
1647 write_graph_chunk_fanout(f, ctx);
1648 write_graph_chunk_oids(f, hashsz, ctx);
1649 write_graph_chunk_data(f, hashsz, ctx);
1650 if (ctx->num_extra_edges)
1651 write_graph_chunk_extra_edges(f, ctx);
76ffbca7
GS
1652 if (ctx->changed_paths) {
1653 write_graph_chunk_bloom_indexes(f, ctx);
1654 write_graph_chunk_bloom_data(f, ctx, &bloom_settings);
1655 }
6c622f9f
DS
1656 if (ctx->num_commit_graphs_after > 1 &&
1657 write_graph_chunk_base(f, ctx)) {
1658 return -1;
1659 }
238def57 1660 stop_progress(&ctx->progress);
28944739 1661 strbuf_release(&progress_title);
08fd81c9 1662
6c622f9f
DS
1663 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1664 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
ad2dd5bb 1665 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
6c622f9f
DS
1666
1667 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1668 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1669 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1670 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1671 }
1672
c3a3a964 1673 close_commit_graph(ctx->r->objects);
6c622f9f
DS
1674 finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1675
1676 if (ctx->split) {
1677 FILE *chainf = fdopen_lock_file(&lk, "w");
1678 char *final_graph_name;
1679 int result;
1680
1681 close(fd);
1682
1683 if (!chainf) {
1684 error(_("unable to open commit-graph chain file"));
1685 return -1;
1686 }
1687
1688 if (ctx->base_graph_name) {
135a7123
DS
1689 const char *dest = ctx->commit_graph_filenames_after[
1690 ctx->num_commit_graphs_after - 2];
6c622f9f 1691
135a7123
DS
1692 if (strcmp(ctx->base_graph_name, dest)) {
1693 result = rename(ctx->base_graph_name, dest);
1694
1695 if (result) {
1696 error(_("failed to rename base commit-graph file"));
1697 return -1;
1698 }
6c622f9f
DS
1699 }
1700 } else {
ad2dd5bb 1701 char *graph_name = get_commit_graph_filename(ctx->odb);
6c622f9f
DS
1702 unlink(graph_name);
1703 }
1704
1705 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
ad2dd5bb 1706 final_graph_name = get_split_graph_filename(ctx->odb,
6c622f9f
DS
1707 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1708 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1709
1710 result = rename(ctx->graph_name, final_graph_name);
1711
1712 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1713 fprintf(lk.tempfile->fp, "%s\n", ctx->commit_graph_hash_after[i]);
1714
1715 if (result) {
1716 error(_("failed to rename temporary commit-graph file"));
1717 return -1;
1718 }
1719 }
1720
08fd81c9
DS
1721 commit_lock_file(&lk);
1722
238def57
DS
1723 return 0;
1724}
1725
1771be90
DS
1726static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
1727{
8da02ce6
AH
1728 struct commit_graph *g;
1729 uint32_t num_commits;
1771be90
DS
1730 uint32_t i;
1731
c2bc6e6a
DS
1732 int max_commits = 0;
1733 int size_mult = 2;
1734
1735 if (ctx->split_opts) {
1736 max_commits = ctx->split_opts->max_commits;
63020f17
DS
1737
1738 if (ctx->split_opts->size_multiple)
1739 size_mult = ctx->split_opts->size_multiple;
c2bc6e6a
DS
1740 }
1741
1771be90 1742 g = ctx->r->objects->commit_graph;
8da02ce6 1743 num_commits = ctx->commits.nr;
1771be90
DS
1744 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
1745
c2bc6e6a
DS
1746 while (g && (g->num_commits <= size_mult * num_commits ||
1747 (max_commits && num_commits > max_commits))) {
ad2dd5bb 1748 if (g->odb != ctx->odb)
c523035c
DS
1749 break;
1750
1771be90
DS
1751 num_commits += g->num_commits;
1752 g = g->base_graph;
1753
1754 ctx->num_commit_graphs_after--;
1755 }
1756
1757 ctx->new_base_graph = g;
1758
c523035c 1759 if (ctx->num_commit_graphs_after == 2) {
ad2dd5bb 1760 char *old_graph_name = get_commit_graph_filename(g->odb);
c523035c
DS
1761
1762 if (!strcmp(g->filename, old_graph_name) &&
ad2dd5bb 1763 g->odb != ctx->odb) {
c523035c
DS
1764 ctx->num_commit_graphs_after = 1;
1765 ctx->new_base_graph = NULL;
1766 }
1767
1768 free(old_graph_name);
1769 }
1770
1771be90
DS
1771 ALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
1772 ALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
1773
1774 for (i = 0; i < ctx->num_commit_graphs_after &&
1775 i < ctx->num_commit_graphs_before; i++)
1776 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
1777
1778 i = ctx->num_commit_graphs_before - 1;
1779 g = ctx->r->objects->commit_graph;
1780
1781 while (g) {
1782 if (i < ctx->num_commit_graphs_after)
1783 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
1784
1785 i--;
1786 g = g->base_graph;
1787 }
1788}
1789
1790static void merge_commit_graph(struct write_commit_graph_context *ctx,
1791 struct commit_graph *g)
1792{
1793 uint32_t i;
1794 uint32_t offset = g->num_commits_in_base;
1795
1796 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
1797
1798 for (i = 0; i < g->num_commits; i++) {
1799 struct object_id oid;
1800 struct commit *result;
1801
1802 display_progress(ctx->progress, i + 1);
1803
1804 load_oid_from_graph(g, i + offset, &oid);
1805
1806 /* only add commits if they still exist in the repo */
1807 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
1808
1809 if (result) {
1810 ctx->commits.list[ctx->commits.nr] = result;
1811 ctx->commits.nr++;
1812 }
1813 }
1814}
1815
1816static int commit_compare(const void *_a, const void *_b)
1817{
1818 const struct commit *a = *(const struct commit **)_a;
1819 const struct commit *b = *(const struct commit **)_b;
1820 return oidcmp(&a->object.oid, &b->object.oid);
1821}
1822
1823static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
1824{
689a146c 1825 uint32_t i;
1771be90
DS
1826
1827 if (ctx->report_progress)
1828 ctx->progress = start_delayed_progress(
1829 _("Scanning merged commits"),
1830 ctx->commits.nr);
1831
1832 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
1833
1834 ctx->num_extra_edges = 0;
1835 for (i = 0; i < ctx->commits.nr; i++) {
1836 display_progress(ctx->progress, i);
1837
1838 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
1839 &ctx->commits.list[i]->object.oid)) {
1840 die(_("unexpected duplicate commit id %s"),
1841 oid_to_hex(&ctx->commits.list[i]->object.oid));
1842 } else {
689a146c 1843 unsigned int num_parents;
1771be90 1844
689a146c 1845 num_parents = commit_list_count(ctx->commits.list[i]->parents);
1771be90 1846 if (num_parents > 2)
a35bea40 1847 ctx->num_extra_edges += num_parents - 1;
1771be90
DS
1848 }
1849 }
1850
1851 stop_progress(&ctx->progress);
1852}
1853
1854static void merge_commit_graphs(struct write_commit_graph_context *ctx)
1855{
1856 struct commit_graph *g = ctx->r->objects->commit_graph;
1857 uint32_t current_graph_number = ctx->num_commit_graphs_before;
1771be90
DS
1858
1859 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
1860 current_graph_number--;
1861
d68ce906
RS
1862 if (ctx->report_progress)
1863 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
1771be90
DS
1864
1865 merge_commit_graph(ctx, g);
1866 stop_progress(&ctx->progress);
1771be90
DS
1867
1868 g = g->base_graph;
1869 }
1870
1871 if (g) {
1872 ctx->new_base_graph = g;
1873 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
1874 }
1875
1876 if (ctx->new_base_graph)
1877 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
1878
1879 sort_and_scan_merged_commits(ctx);
1880}
1881
8d84097f
DS
1882static void mark_commit_graphs(struct write_commit_graph_context *ctx)
1883{
1884 uint32_t i;
1885 time_t now = time(NULL);
1886
1887 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
1888 struct stat st;
1889 struct utimbuf updated_time;
1890
1891 stat(ctx->commit_graph_filenames_before[i], &st);
1892
1893 updated_time.actime = st.st_atime;
1894 updated_time.modtime = now;
1895 utime(ctx->commit_graph_filenames_before[i], &updated_time);
1896 }
1897}
1898
1899static void expire_commit_graphs(struct write_commit_graph_context *ctx)
1900{
1901 struct strbuf path = STRBUF_INIT;
1902 DIR *dir;
1903 struct dirent *de;
1904 size_t dirnamelen;
c2bc6e6a
DS
1905 timestamp_t expire_time = time(NULL);
1906
1907 if (ctx->split_opts && ctx->split_opts->expire_time)
1908 expire_time -= ctx->split_opts->expire_time;
ba41112a 1909 if (!ctx->split) {
ad2dd5bb 1910 char *chain_file_name = get_chain_filename(ctx->odb);
ba41112a
DS
1911 unlink(chain_file_name);
1912 free(chain_file_name);
1913 ctx->num_commit_graphs_after = 0;
1914 }
8d84097f 1915
0bd52e27 1916 strbuf_addstr(&path, ctx->odb->path);
8d84097f
DS
1917 strbuf_addstr(&path, "/info/commit-graphs");
1918 dir = opendir(path.buf);
1919
0aa6bce7
RS
1920 if (!dir)
1921 goto out;
8d84097f
DS
1922
1923 strbuf_addch(&path, '/');
1924 dirnamelen = path.len;
1925 while ((de = readdir(dir)) != NULL) {
1926 struct stat st;
1927 uint32_t i, found = 0;
1928
1929 strbuf_setlen(&path, dirnamelen);
1930 strbuf_addstr(&path, de->d_name);
1931
1932 stat(path.buf, &st);
1933
1934 if (st.st_mtime > expire_time)
1935 continue;
1936 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
1937 continue;
1938
1939 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
1940 if (!strcmp(ctx->commit_graph_filenames_after[i],
1941 path.buf)) {
1942 found = 1;
1943 break;
1944 }
1945 }
1946
1947 if (!found)
1948 unlink(path.buf);
8d84097f 1949 }
0aa6bce7
RS
1950
1951out:
1952 strbuf_release(&path);
8d84097f
DS
1953}
1954
0bd52e27 1955int write_commit_graph(struct object_directory *odb,
238def57
DS
1956 struct string_list *pack_indexes,
1957 struct string_list *commit_hex,
39d88318 1958 enum commit_graph_write_flags flags,
c2bc6e6a 1959 const struct split_commit_graph_opts *split_opts)
238def57
DS
1960{
1961 struct write_commit_graph_context *ctx;
1962 uint32_t i, count_distinct = 0;
e103f727 1963 int res = 0;
08fd81c9 1964
d6538246 1965 if (!commit_graph_compatible(the_repository))
e103f727 1966 return 0;
d6538246 1967
c9905bea
DS
1968 ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
1969 ctx->r = the_repository;
0bd52e27 1970 ctx->odb = odb;
39d88318
SG
1971 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
1972 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
1973 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
7c5c9b9c 1974 ctx->check_oids = flags & COMMIT_GRAPH_WRITE_CHECK_OIDS ? 1 : 0;
c2bc6e6a 1975 ctx->split_opts = split_opts;
f97b9325
GS
1976 ctx->changed_paths = flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS ? 1 : 0;
1977 ctx->total_bloom_filter_data_size = 0;
6c622f9f
DS
1978
1979 if (ctx->split) {
1980 struct commit_graph *g;
1981 prepare_commit_graph(ctx->r);
1982
1983 g = ctx->r->objects->commit_graph;
1984
1985 while (g) {
1986 ctx->num_commit_graphs_before++;
1987 g = g->base_graph;
1988 }
1989
1990 if (ctx->num_commit_graphs_before) {
1991 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
1992 i = ctx->num_commit_graphs_before;
1993 g = ctx->r->objects->commit_graph;
1994
1995 while (g) {
1996 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
1997 g = g->base_graph;
1998 }
1999 }
2000 }
c9905bea
DS
2001
2002 ctx->approx_nr_objects = approximate_object_count();
2003 ctx->oids.alloc = ctx->approx_nr_objects / 32;
2004
c2bc6e6a
DS
2005 if (ctx->split && split_opts && ctx->oids.alloc > split_opts->max_commits)
2006 ctx->oids.alloc = split_opts->max_commits;
1771be90 2007
c9905bea 2008 if (ctx->append) {
13c24992 2009 prepare_commit_graph_one(ctx->r, ctx->odb);
c9905bea
DS
2010 if (ctx->r->objects->commit_graph)
2011 ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
7547b95b
DS
2012 }
2013
c9905bea
DS
2014 if (ctx->oids.alloc < 1024)
2015 ctx->oids.alloc = 1024;
2016 ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc);
2017
2018 if (ctx->append && ctx->r->objects->commit_graph) {
2019 struct commit_graph *g = ctx->r->objects->commit_graph;
2020 for (i = 0; i < g->num_commits; i++) {
2021 const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i;
2022 hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash);
7547b95b
DS
2023 }
2024 }
2025
049d51a2 2026 if (pack_indexes) {
3d112755 2027 ctx->order_by_pack = 1;
ef5b83f2
DS
2028 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2029 goto cleanup;
3d5df01b
DS
2030 }
2031
7c5c9b9c
SG
2032 if (commit_hex) {
2033 if ((res = fill_oids_from_commit_hex(ctx, commit_hex)))
2034 goto cleanup;
2035 }
3d5df01b 2036
3d112755
GS
2037 if (!pack_indexes && !commit_hex) {
2038 ctx->order_by_pack = 1;
b2c83060 2039 fill_oids_from_all_packs(ctx);
3d112755 2040 }
049d51a2 2041
c9905bea 2042 close_reachable(ctx);
08fd81c9 2043
014e3440 2044 count_distinct = count_distinct_commits(ctx);
08fd81c9 2045
e103f727
DS
2046 if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
2047 error(_("the commit graph format cannot write %d commits"), count_distinct);
2048 res = -1;
2049 goto cleanup;
2050 }
08fd81c9 2051
c9905bea
DS
2052 ctx->commits.alloc = count_distinct;
2053 ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc);
08fd81c9 2054
f998d542 2055 copy_oids_to_commits(ctx);
08fd81c9 2056
c9905bea 2057 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
e103f727
DS
2058 error(_("too many commits to write graph"));
2059 res = -1;
2060 goto cleanup;
2061 }
08fd81c9 2062
6c622f9f
DS
2063 if (!ctx->commits.nr)
2064 goto cleanup;
2065
1771be90
DS
2066 if (ctx->split) {
2067 split_graph_merge_strategy(ctx);
2068
2069 merge_commit_graphs(ctx);
2070 } else
6c622f9f
DS
2071 ctx->num_commit_graphs_after = 1;
2072
c9905bea 2073 compute_generation_numbers(ctx);
08fd81c9 2074
f97b9325
GS
2075 if (ctx->changed_paths)
2076 compute_bloom_filters(ctx);
2077
238def57 2078 res = write_commit_graph_file(ctx);
08fd81c9 2079
ba41112a 2080 if (ctx->split)
8d84097f 2081 mark_commit_graphs(ctx);
ba41112a
DS
2082
2083 expire_commit_graphs(ctx);
8d84097f 2084
e103f727 2085cleanup:
238def57 2086 free(ctx->graph_name);
c9905bea
DS
2087 free(ctx->commits.list);
2088 free(ctx->oids.list);
6c622f9f
DS
2089
2090 if (ctx->commit_graph_filenames_after) {
2091 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2092 free(ctx->commit_graph_filenames_after[i]);
2093 free(ctx->commit_graph_hash_after[i]);
2094 }
2095
2096 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2097 free(ctx->commit_graph_filenames_before[i]);
2098
2099 free(ctx->commit_graph_filenames_after);
2100 free(ctx->commit_graph_filenames_before);
2101 free(ctx->commit_graph_hash_after);
2102 }
2103
c9905bea 2104 free(ctx);
e103f727
DS
2105
2106 return res;
08fd81c9 2107}
283e68c7 2108
41df0e30 2109#define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
283e68c7
DS
2110static int verify_commit_graph_error;
2111
2112static void graph_report(const char *fmt, ...)
2113{
2114 va_list ap;
2115
2116 verify_commit_graph_error = 1;
2117 va_start(ap, fmt);
2118 vfprintf(stderr, fmt, ap);
2119 fprintf(stderr, "\n");
2120 va_end(ap);
2121}
2122
1373e547
DS
2123#define GENERATION_ZERO_EXISTS 1
2124#define GENERATION_NUMBER_EXISTS 2
2125
3da4b609 2126int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
283e68c7 2127{
9bda8467 2128 uint32_t i, cur_fanout_pos = 0;
41df0e30 2129 struct object_id prev_oid, cur_oid, checksum;
1373e547 2130 int generation_zero = 0;
41df0e30
DS
2131 struct hashfile *f;
2132 int devnull;
1f7f557f 2133 struct progress *progress = NULL;
3da4b609 2134 int local_error = 0;
9bda8467 2135
283e68c7
DS
2136 if (!g) {
2137 graph_report("no commit-graph file loaded");
2138 return 1;
2139 }
2140
2ac138d5 2141 verify_commit_graph_error = verify_commit_graph_lite(g);
9bda8467
DS
2142 if (verify_commit_graph_error)
2143 return verify_commit_graph_error;
2144
41df0e30
DS
2145 devnull = open("/dev/null", O_WRONLY);
2146 f = hashfd(devnull, NULL);
2147 hashwrite(f, g->data, g->data_len - g->hash_len);
2148 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
67947c34 2149 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
41df0e30
DS
2150 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2151 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2152 }
2153
9bda8467 2154 for (i = 0; i < g->num_commits; i++) {
2e3c0737
DS
2155 struct commit *graph_commit;
2156
9bda8467
DS
2157 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2158
2159 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
93b4405f 2160 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
9bda8467
DS
2161 oid_to_hex(&prev_oid),
2162 oid_to_hex(&cur_oid));
2163
2164 oidcpy(&prev_oid, &cur_oid);
2165
2166 while (cur_oid.hash[0] > cur_fanout_pos) {
2167 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2168
2169 if (i != fanout_value)
93b4405f 2170 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
9bda8467
DS
2171 cur_fanout_pos, fanout_value, i);
2172 cur_fanout_pos++;
2173 }
2e3c0737 2174
82952964 2175 graph_commit = lookup_commit(r, &cur_oid);
4f542b7a 2176 if (!parse_commit_in_graph_one(r, g, graph_commit))
93b4405f 2177 graph_report(_("failed to parse commit %s from commit-graph"),
2e3c0737 2178 oid_to_hex(&cur_oid));
9bda8467
DS
2179 }
2180
2181 while (cur_fanout_pos < 256) {
2182 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2183
2184 if (g->num_commits != fanout_value)
93b4405f 2185 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
9bda8467
DS
2186 cur_fanout_pos, fanout_value, i);
2187
2188 cur_fanout_pos++;
2189 }
2190
41df0e30 2191 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
96af91d4
DS
2192 return verify_commit_graph_error;
2193
73716122
GS
2194 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2195 progress = start_progress(_("Verifying commits in commit graph"),
2196 g->num_commits);
2197
96af91d4 2198 for (i = 0; i < g->num_commits; i++) {
2e3c0737 2199 struct commit *graph_commit, *odb_commit;
53614b13 2200 struct commit_list *graph_parents, *odb_parents;
1373e547 2201 uint32_t max_generation = 0;
96af91d4 2202
1f7f557f 2203 display_progress(progress, i + 1);
96af91d4
DS
2204 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2205
82952964 2206 graph_commit = lookup_commit(r, &cur_oid);
a378509e 2207 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
96af91d4 2208 if (parse_commit_internal(odb_commit, 0, 0)) {
93b4405f 2209 graph_report(_("failed to parse commit %s from object database for commit-graph"),
96af91d4
DS
2210 oid_to_hex(&cur_oid));
2211 continue;
2212 }
2e3c0737 2213
4f542b7a 2214 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2e3c0737 2215 get_commit_tree_oid(odb_commit)))
93b4405f 2216 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2e3c0737
DS
2217 oid_to_hex(&cur_oid),
2218 oid_to_hex(get_commit_tree_oid(graph_commit)),
2219 oid_to_hex(get_commit_tree_oid(odb_commit)));
53614b13
DS
2220
2221 graph_parents = graph_commit->parents;
2222 odb_parents = odb_commit->parents;
2223
2224 while (graph_parents) {
2225 if (odb_parents == NULL) {
93b4405f 2226 graph_report(_("commit-graph parent list for commit %s is too long"),
53614b13
DS
2227 oid_to_hex(&cur_oid));
2228 break;
2229 }
2230
3da4b609
DS
2231 /* parse parent in case it is in a base graph */
2232 parse_commit_in_graph_one(r, g, graph_parents->item);
2233
9001dc2a 2234 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
93b4405f 2235 graph_report(_("commit-graph parent for %s is %s != %s"),
53614b13
DS
2236 oid_to_hex(&cur_oid),
2237 oid_to_hex(&graph_parents->item->object.oid),
2238 oid_to_hex(&odb_parents->item->object.oid));
2239
1373e547
DS
2240 if (graph_parents->item->generation > max_generation)
2241 max_generation = graph_parents->item->generation;
2242
53614b13
DS
2243 graph_parents = graph_parents->next;
2244 odb_parents = odb_parents->next;
2245 }
2246
2247 if (odb_parents != NULL)
93b4405f 2248 graph_report(_("commit-graph parent list for commit %s terminates early"),
53614b13 2249 oid_to_hex(&cur_oid));
1373e547
DS
2250
2251 if (!graph_commit->generation) {
2252 if (generation_zero == GENERATION_NUMBER_EXISTS)
93b4405f 2253 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
1373e547
DS
2254 oid_to_hex(&cur_oid));
2255 generation_zero = GENERATION_ZERO_EXISTS;
2256 } else if (generation_zero == GENERATION_ZERO_EXISTS)
93b4405f 2257 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
1373e547
DS
2258 oid_to_hex(&cur_oid));
2259
2260 if (generation_zero == GENERATION_ZERO_EXISTS)
2261 continue;
2262
2263 /*
2264 * If one of our parents has generation GENERATION_NUMBER_MAX, then
2265 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
2266 * extra logic in the following condition.
2267 */
2268 if (max_generation == GENERATION_NUMBER_MAX)
2269 max_generation--;
2270
2271 if (graph_commit->generation != max_generation + 1)
93b4405f 2272 graph_report(_("commit-graph generation for commit %s is %u != %u"),
1373e547
DS
2273 oid_to_hex(&cur_oid),
2274 graph_commit->generation,
2275 max_generation + 1);
88968ebf
DS
2276
2277 if (graph_commit->date != odb_commit->date)
93b4405f 2278 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
88968ebf
DS
2279 oid_to_hex(&cur_oid),
2280 graph_commit->date,
2281 odb_commit->date);
96af91d4 2282 }
1f7f557f 2283 stop_progress(&progress);
96af91d4 2284
3da4b609
DS
2285 local_error = verify_commit_graph_error;
2286
2287 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2288 local_error |= verify_commit_graph(r, g->base_graph, flags);
2289
2290 return local_error;
283e68c7 2291}
c3756d5b
JT
2292
2293void free_commit_graph(struct commit_graph *g)
2294{
2295 if (!g)
2296 return;
2297 if (g->graph_fd >= 0) {
2298 munmap((void *)g->data, g->data_len);
2299 g->data = NULL;
2300 close(g->graph_fd);
2301 }
6c622f9f 2302 free(g->filename);
76ffbca7 2303 free(g->bloom_filter_settings);
c3756d5b
JT
2304 free(g);
2305}
6abada18
JK
2306
2307void disable_commit_graph(struct repository *r)
2308{
2309 r->commit_graph_disabled = 1;
2310}