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