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