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