]> git.ipfire.org Git - thirdparty/git.git/blame - commit-graph.c
commit-graph: don't early exit(1) on e.g. "git status"
[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" */
08fd81c9 25
c1665998 26#define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
08fd81c9
DS
27
28#define GRAPH_VERSION_1 0x1
29#define GRAPH_VERSION GRAPH_VERSION_1
30
5af7417b 31#define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
08fd81c9
DS
32#define GRAPH_EDGE_LAST_MASK 0x7fffffff
33#define GRAPH_PARENT_NONE 0x70000000
34
35#define GRAPH_LAST_EDGE 0x80000000
36
0e3b97cc 37#define GRAPH_HEADER_SIZE 8
08fd81c9
DS
38#define GRAPH_FANOUT_SIZE (4 * 256)
39#define GRAPH_CHUNKLOOKUP_WIDTH 12
0e3b97cc 40#define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
c1665998 41 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
08fd81c9 42
2a2e32bd 43char *get_commit_graph_filename(const char *obj_dir)
08fd81c9
DS
44{
45 return xstrfmt("%s/info/commit-graph", obj_dir);
46}
47
c1665998 48static uint8_t oid_version(void)
49{
50 return 1;
51}
52
2a2e32bd
DS
53static struct commit_graph *alloc_commit_graph(void)
54{
55 struct commit_graph *g = xcalloc(1, sizeof(*g));
56 g->graph_fd = -1;
57
58 return g;
59}
60
d6538246
DS
61extern int read_replace_refs;
62
63static int commit_graph_compatible(struct repository *r)
64{
5cef295f
DS
65 if (!r->gitdir)
66 return 0;
67
d6538246
DS
68 if (read_replace_refs) {
69 prepare_replace_object(r);
70 if (hashmap_get_size(&r->objects->replace_map->map))
71 return 0;
72 }
73
20fd6d57
DS
74 prepare_commit_graft(r);
75 if (r->parsed_objects && r->parsed_objects->grafts_nr)
76 return 0;
77 if (is_repository_shallow(r))
78 return 0;
79
d6538246
DS
80 return 1;
81}
82
61df89c8
ÆAB
83int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
84{
85 *fd = git_open(graph_file);
86 if (*fd < 0)
87 return 0;
88 if (fstat(*fd, st)) {
89 close(*fd);
90 return 0;
91 }
92 return 1;
93}
94
95struct commit_graph *load_commit_graph_one_fd_st(const char *graph_file,
96 int fd, struct stat *st)
2a2e32bd
DS
97{
98 void *graph_map;
2a2e32bd 99 size_t graph_size;
aa658574 100 struct commit_graph *ret;
2a2e32bd 101
61df89c8 102 graph_size = xsize_t(st->st_size);
2a2e32bd
DS
103
104 if (graph_size < GRAPH_MIN_SIZE) {
105 close(fd);
61df89c8
ÆAB
106 error(_("graph file %s is too small"), graph_file);
107 return NULL;
2a2e32bd
DS
108 }
109 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
aa658574
JS
110 ret = parse_commit_graph(graph_map, fd, graph_size);
111
112 if (!ret) {
113 munmap(graph_map, graph_size);
114 close(fd);
aa658574
JS
115 }
116
117 return ret;
118}
119
2ac138d5
ÆAB
120static int verify_commit_graph_lite(struct commit_graph *g)
121{
122 /*
123 * Basic validation shared between parse_commit_graph()
124 * which'll be called every time the graph is used, and the
125 * much more expensive verify_commit_graph() used by
126 * "commit-graph verify".
127 *
128 * There should only be very basic checks here to ensure that
129 * we don't e.g. segfault in fill_commit_in_graph(), but
130 * because this is a very hot codepath nothing that e.g. loops
131 * over g->num_commits, or runs a checksum on the commit-graph
132 * itself.
133 */
134 if (!g->chunk_oid_fanout) {
135 error("commit-graph is missing the OID Fanout chunk");
136 return 1;
137 }
138 if (!g->chunk_oid_lookup) {
139 error("commit-graph is missing the OID Lookup chunk");
140 return 1;
141 }
142 if (!g->chunk_commit_data) {
143 error("commit-graph is missing the Commit Data chunk");
144 return 1;
145 }
146
147 return 0;
148}
149
aa658574
JS
150struct commit_graph *parse_commit_graph(void *graph_map, int fd,
151 size_t graph_size)
152{
153 const unsigned char *data, *chunk_lookup;
154 uint32_t i;
155 struct commit_graph *graph;
156 uint64_t last_chunk_offset;
157 uint32_t last_chunk_id;
158 uint32_t graph_signature;
159 unsigned char graph_version, hash_version;
160
161 if (!graph_map)
162 return NULL;
163
164 if (graph_size < GRAPH_MIN_SIZE)
165 return NULL;
166
2a2e32bd
DS
167 data = (const unsigned char *)graph_map;
168
169 graph_signature = get_be32(data);
170 if (graph_signature != GRAPH_SIGNATURE) {
4f5b532d 171 error(_("graph signature %X does not match signature %X"),
2a2e32bd 172 graph_signature, GRAPH_SIGNATURE);
aa658574 173 return NULL;
2a2e32bd
DS
174 }
175
176 graph_version = *(unsigned char*)(data + 4);
177 if (graph_version != GRAPH_VERSION) {
4f5b532d 178 error(_("graph version %X does not match version %X"),
2a2e32bd 179 graph_version, GRAPH_VERSION);
aa658574 180 return NULL;
2a2e32bd
DS
181 }
182
183 hash_version = *(unsigned char*)(data + 5);
c1665998 184 if (hash_version != oid_version()) {
4f5b532d 185 error(_("hash version %X does not match version %X"),
c1665998 186 hash_version, oid_version());
aa658574 187 return NULL;
2a2e32bd
DS
188 }
189
190 graph = alloc_commit_graph();
191
c1665998 192 graph->hash_len = the_hash_algo->rawsz;
2a2e32bd
DS
193 graph->num_chunks = *(unsigned char*)(data + 6);
194 graph->graph_fd = fd;
195 graph->data = graph_map;
196 graph->data_len = graph_size;
197
198 last_chunk_id = 0;
199 last_chunk_offset = 8;
200 chunk_lookup = data + 8;
201 for (i = 0; i < graph->num_chunks; i++) {
d2b86fba
JS
202 uint32_t chunk_id;
203 uint64_t chunk_offset;
2a2e32bd
DS
204 int chunk_repeated = 0;
205
d2b86fba
JS
206 if (data + graph_size - chunk_lookup <
207 GRAPH_CHUNKLOOKUP_WIDTH) {
208 error(_("chunk lookup table entry missing; graph file may be incomplete"));
209 free(graph);
210 return NULL;
211 }
212
213 chunk_id = get_be32(chunk_lookup + 0);
214 chunk_offset = get_be64(chunk_lookup + 4);
215
2a2e32bd
DS
216 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
217
c1665998 218 if (chunk_offset > graph_size - the_hash_algo->rawsz) {
4f5b532d 219 error(_("improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
2a2e32bd 220 (uint32_t)chunk_offset);
aa658574
JS
221 free(graph);
222 return NULL;
2a2e32bd
DS
223 }
224
225 switch (chunk_id) {
226 case GRAPH_CHUNKID_OIDFANOUT:
227 if (graph->chunk_oid_fanout)
228 chunk_repeated = 1;
229 else
230 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
231 break;
232
233 case GRAPH_CHUNKID_OIDLOOKUP:
234 if (graph->chunk_oid_lookup)
235 chunk_repeated = 1;
236 else
237 graph->chunk_oid_lookup = data + chunk_offset;
238 break;
239
240 case GRAPH_CHUNKID_DATA:
241 if (graph->chunk_commit_data)
242 chunk_repeated = 1;
243 else
244 graph->chunk_commit_data = data + chunk_offset;
245 break;
246
5af7417b
SG
247 case GRAPH_CHUNKID_EXTRAEDGES:
248 if (graph->chunk_extra_edges)
2a2e32bd
DS
249 chunk_repeated = 1;
250 else
5af7417b 251 graph->chunk_extra_edges = data + chunk_offset;
2a2e32bd
DS
252 break;
253 }
254
255 if (chunk_repeated) {
4f5b532d 256 error(_("chunk id %08x appears multiple times"), chunk_id);
aa658574
JS
257 free(graph);
258 return NULL;
2a2e32bd
DS
259 }
260
261 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
262 {
263 graph->num_commits = (chunk_offset - last_chunk_offset)
264 / graph->hash_len;
265 }
266
267 last_chunk_id = chunk_id;
268 last_chunk_offset = chunk_offset;
269 }
270
2ac138d5
ÆAB
271 if (verify_commit_graph_lite(graph))
272 return NULL;
273
2a2e32bd 274 return graph;
2a2e32bd
DS
275}
276
61df89c8
ÆAB
277static struct commit_graph *load_commit_graph_one(const char *graph_file)
278{
279
280 struct stat st;
281 int fd;
282 int open_ok = open_commit_graph(graph_file, &fd, &st);
283
284 if (!open_ok)
285 return NULL;
286
287 return load_commit_graph_one_fd_st(graph_file, fd, &st);
288}
289
dade47c0 290static void prepare_commit_graph_one(struct repository *r, const char *obj_dir)
177722b3
DS
291{
292 char *graph_name;
293
dade47c0 294 if (r->objects->commit_graph)
177722b3
DS
295 return;
296
297 graph_name = get_commit_graph_filename(obj_dir);
dade47c0 298 r->objects->commit_graph =
85277506 299 load_commit_graph_one(graph_name);
177722b3
DS
300
301 FREE_AND_NULL(graph_name);
302}
303
5faf357b
JT
304/*
305 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
306 *
307 * On the first invocation, this function attemps to load the commit
308 * graph if the_repository is configured to have one.
309 */
dade47c0 310static int prepare_commit_graph(struct repository *r)
177722b3 311{
263db403 312 struct object_directory *odb;
dade47c0
JT
313 int config_value;
314
315 if (r->objects->commit_graph_attempted)
316 return !!r->objects->commit_graph;
317 r->objects->commit_graph_attempted = 1;
318
859fdc0c
DS
319 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
320 (repo_config_get_bool(r, "core.commitgraph", &config_value) ||
321 !config_value))
dade47c0
JT
322 /*
323 * This repository is not configured to use commit graphs, so
324 * do not load one. (But report commit_graph_attempted anyway
325 * so that commit graph loading is not attempted again for this
326 * repository.)
327 */
5faf357b
JT
328 return 0;
329
d6538246
DS
330 if (!commit_graph_compatible(r))
331 return 0;
332
dade47c0 333 prepare_alt_odb(r);
f0eaf638 334 for (odb = r->objects->odb;
263db403
JK
335 !r->objects->commit_graph && odb;
336 odb = odb->next)
337 prepare_commit_graph_one(r, odb->path);
dade47c0 338 return !!r->objects->commit_graph;
177722b3
DS
339}
340
6cc01743
DS
341int generation_numbers_enabled(struct repository *r)
342{
343 uint32_t first_generation;
344 struct commit_graph *g;
345 if (!prepare_commit_graph(r))
346 return 0;
347
348 g = r->objects->commit_graph;
349
350 if (!g->num_commits)
351 return 0;
352
353 first_generation = get_be32(g->chunk_commit_data +
354 g->hash_len + 8) >> 2;
355
356 return !!first_generation;
357}
358
829a3215 359void close_commit_graph(struct repository *r)
177722b3 360{
829a3215
DS
361 free_commit_graph(r->objects->commit_graph);
362 r->objects->commit_graph = NULL;
177722b3
DS
363}
364
365static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
366{
367 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
368 g->chunk_oid_lookup, g->hash_len, pos);
369}
370
4f542b7a
SB
371static struct commit_list **insert_parent_or_die(struct repository *r,
372 struct commit_graph *g,
177722b3
DS
373 uint64_t pos,
374 struct commit_list **pptr)
375{
376 struct commit *c;
377 struct object_id oid;
96af91d4 378
53614b13
DS
379 if (pos >= g->num_commits)
380 die("invalid parent position %"PRIu64, pos);
381
177722b3 382 hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * pos);
4f542b7a 383 c = lookup_commit(r, &oid);
177722b3 384 if (!c)
4f5b532d 385 die(_("could not find commit %s"), oid_to_hex(&oid));
177722b3
DS
386 c->graph_pos = pos;
387 return &commit_list_insert(c, pptr)->next;
388}
389
e2838d85
DS
390static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
391{
392 const unsigned char *commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * pos;
393 item->graph_pos = pos;
394 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
395}
396
4f542b7a
SB
397static int fill_commit_in_graph(struct repository *r,
398 struct commit *item,
399 struct commit_graph *g, uint32_t pos)
177722b3 400{
177722b3
DS
401 uint32_t edge_value;
402 uint32_t *parent_data_ptr;
403 uint64_t date_low, date_high;
404 struct commit_list **pptr;
405 const unsigned char *commit_data = g->chunk_commit_data + (g->hash_len + 16) * pos;
406
407 item->object.parsed = 1;
408 item->graph_pos = pos;
409
7b8a21db 410 item->maybe_tree = NULL;
177722b3
DS
411
412 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
413 date_low = get_be32(commit_data + g->hash_len + 12);
414 item->date = (timestamp_t)((date_high << 32) | date_low);
415
83073cc9
DS
416 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
417
177722b3
DS
418 pptr = &item->parents;
419
420 edge_value = get_be32(commit_data + g->hash_len);
421 if (edge_value == GRAPH_PARENT_NONE)
422 return 1;
4f542b7a 423 pptr = insert_parent_or_die(r, g, edge_value, pptr);
177722b3
DS
424
425 edge_value = get_be32(commit_data + g->hash_len + 4);
426 if (edge_value == GRAPH_PARENT_NONE)
427 return 1;
5af7417b 428 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
4f542b7a 429 pptr = insert_parent_or_die(r, g, edge_value, pptr);
177722b3
DS
430 return 1;
431 }
432
5af7417b 433 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
177722b3
DS
434 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
435 do {
436 edge_value = get_be32(parent_data_ptr);
4f542b7a 437 pptr = insert_parent_or_die(r, g,
177722b3
DS
438 edge_value & GRAPH_EDGE_LAST_MASK,
439 pptr);
440 parent_data_ptr++;
441 } while (!(edge_value & GRAPH_LAST_EDGE));
442
443 return 1;
444}
445
e2838d85
DS
446static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
447{
448 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
449 *pos = item->graph_pos;
450 return 1;
451 } else {
452 return bsearch_graph(g, &(item->object.oid), pos);
453 }
454}
455
4f542b7a
SB
456static int parse_commit_in_graph_one(struct repository *r,
457 struct commit_graph *g,
458 struct commit *item)
177722b3 459{
e2838d85
DS
460 uint32_t pos;
461
177722b3
DS
462 if (item->object.parsed)
463 return 1;
ee797053
DS
464
465 if (find_commit_in_graph(item, g, &pos))
4f542b7a 466 return fill_commit_in_graph(r, item, g, pos);
ee797053
DS
467
468 return 0;
469}
470
dade47c0 471int parse_commit_in_graph(struct repository *r, struct commit *item)
ee797053 472{
dade47c0 473 if (!prepare_commit_graph(r))
ee797053 474 return 0;
4f542b7a 475 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
177722b3
DS
476}
477
dade47c0 478void load_commit_graph_info(struct repository *r, struct commit *item)
e2838d85
DS
479{
480 uint32_t pos;
dade47c0 481 if (!prepare_commit_graph(r))
e2838d85 482 return;
dade47c0
JT
483 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
484 fill_commit_graph_info(item, r->objects->commit_graph, pos);
e2838d85
DS
485}
486
4f542b7a
SB
487static struct tree *load_tree_for_commit(struct repository *r,
488 struct commit_graph *g,
489 struct commit *c)
7b8a21db
DS
490{
491 struct object_id oid;
492 const unsigned char *commit_data = g->chunk_commit_data +
493 GRAPH_DATA_WIDTH * (c->graph_pos);
494
495 hashcpy(oid.hash, commit_data);
4f542b7a 496 c->maybe_tree = lookup_tree(r, &oid);
7b8a21db
DS
497
498 return c->maybe_tree;
499}
500
4f542b7a
SB
501static struct tree *get_commit_tree_in_graph_one(struct repository *r,
502 struct commit_graph *g,
0cbef8f8 503 const struct commit *c)
7b8a21db
DS
504{
505 if (c->maybe_tree)
506 return c->maybe_tree;
507 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
0cbef8f8
DS
508 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
509
4f542b7a 510 return load_tree_for_commit(r, g, (struct commit *)c);
0cbef8f8 511}
7b8a21db 512
dade47c0 513struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
0cbef8f8 514{
4f542b7a 515 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
7b8a21db
DS
516}
517
08fd81c9
DS
518static void write_graph_chunk_fanout(struct hashfile *f,
519 struct commit **commits,
53035c4f
ÆAB
520 int nr_commits,
521 struct progress *progress,
522 uint64_t *progress_cnt)
08fd81c9
DS
523{
524 int i, count = 0;
525 struct commit **list = commits;
526
527 /*
528 * Write the first-level table (the list is sorted,
529 * but we use a 256-entry lookup to be able to avoid
530 * having to do eight extra binary search iterations).
531 */
532 for (i = 0; i < 256; i++) {
533 while (count < nr_commits) {
534 if ((*list)->object.oid.hash[0] != i)
535 break;
53035c4f 536 display_progress(progress, ++*progress_cnt);
08fd81c9
DS
537 count++;
538 list++;
539 }
540
541 hashwrite_be32(f, count);
542 }
543}
544
545static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
53035c4f
ÆAB
546 struct commit **commits, int nr_commits,
547 struct progress *progress,
548 uint64_t *progress_cnt)
08fd81c9
DS
549{
550 struct commit **list = commits;
551 int count;
53035c4f
ÆAB
552 for (count = 0; count < nr_commits; count++, list++) {
553 display_progress(progress, ++*progress_cnt);
08fd81c9 554 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
53035c4f 555 }
08fd81c9
DS
556}
557
558static const unsigned char *commit_to_sha1(size_t index, void *table)
559{
560 struct commit **commits = table;
561 return commits[index]->object.oid.hash;
562}
563
564static void write_graph_chunk_data(struct hashfile *f, int hash_len,
53035c4f
ÆAB
565 struct commit **commits, int nr_commits,
566 struct progress *progress,
567 uint64_t *progress_cnt)
08fd81c9
DS
568{
569 struct commit **list = commits;
570 struct commit **last = commits + nr_commits;
571 uint32_t num_extra_edges = 0;
572
573 while (list < last) {
574 struct commit_list *parent;
575 int edge_value;
576 uint32_t packedDate[2];
53035c4f 577 display_progress(progress, ++*progress_cnt);
08fd81c9
DS
578
579 parse_commit(*list);
2e27bd77 580 hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
08fd81c9
DS
581
582 parent = (*list)->parents;
583
584 if (!parent)
585 edge_value = GRAPH_PARENT_NONE;
586 else {
587 edge_value = sha1_pos(parent->item->object.oid.hash,
588 commits,
589 nr_commits,
590 commit_to_sha1);
591
592 if (edge_value < 0)
cce99cd8
DS
593 BUG("missing parent %s for commit %s",
594 oid_to_hex(&parent->item->object.oid),
595 oid_to_hex(&(*list)->object.oid));
08fd81c9
DS
596 }
597
598 hashwrite_be32(f, edge_value);
599
600 if (parent)
601 parent = parent->next;
602
603 if (!parent)
604 edge_value = GRAPH_PARENT_NONE;
605 else if (parent->next)
5af7417b 606 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
08fd81c9
DS
607 else {
608 edge_value = sha1_pos(parent->item->object.oid.hash,
609 commits,
610 nr_commits,
611 commit_to_sha1);
612 if (edge_value < 0)
cce99cd8
DS
613 BUG("missing parent %s for commit %s",
614 oid_to_hex(&parent->item->object.oid),
615 oid_to_hex(&(*list)->object.oid));
08fd81c9
DS
616 }
617
618 hashwrite_be32(f, edge_value);
619
5af7417b 620 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
08fd81c9
DS
621 do {
622 num_extra_edges++;
623 parent = parent->next;
624 } while (parent);
625 }
626
627 if (sizeof((*list)->date) > 4)
628 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
629 else
630 packedDate[0] = 0;
631
3258c663
DS
632 packedDate[0] |= htonl((*list)->generation << 2);
633
08fd81c9
DS
634 packedDate[1] = htonl((*list)->date);
635 hashwrite(f, packedDate, 8);
636
637 list++;
638 }
639}
640
5af7417b 641static void write_graph_chunk_extra_edges(struct hashfile *f,
08fd81c9 642 struct commit **commits,
53035c4f
ÆAB
643 int nr_commits,
644 struct progress *progress,
645 uint64_t *progress_cnt)
08fd81c9
DS
646{
647 struct commit **list = commits;
648 struct commit **last = commits + nr_commits;
649 struct commit_list *parent;
650
651 while (list < last) {
652 int num_parents = 0;
53035c4f
ÆAB
653
654 display_progress(progress, ++*progress_cnt);
655
08fd81c9
DS
656 for (parent = (*list)->parents; num_parents < 3 && parent;
657 parent = parent->next)
658 num_parents++;
659
660 if (num_parents <= 2) {
661 list++;
662 continue;
663 }
664
665 /* Since num_parents > 2, this initializer is safe. */
666 for (parent = (*list)->parents->next; parent; parent = parent->next) {
667 int edge_value = sha1_pos(parent->item->object.oid.hash,
668 commits,
669 nr_commits,
670 commit_to_sha1);
671
672 if (edge_value < 0)
cce99cd8
DS
673 BUG("missing parent %s for commit %s",
674 oid_to_hex(&parent->item->object.oid),
675 oid_to_hex(&(*list)->object.oid));
08fd81c9
DS
676 else if (!parent->next)
677 edge_value |= GRAPH_LAST_EDGE;
678
679 hashwrite_be32(f, edge_value);
680 }
681
682 list++;
683 }
684}
685
686static int commit_compare(const void *_a, const void *_b)
687{
688 const struct object_id *a = (const struct object_id *)_a;
689 const struct object_id *b = (const struct object_id *)_b;
690 return oidcmp(a, b);
691}
692
693struct packed_commit_list {
694 struct commit **list;
695 int nr;
696 int alloc;
697};
698
699struct packed_oid_list {
700 struct object_id *list;
701 int nr;
702 int alloc;
7b0f2292
ÆAB
703 struct progress *progress;
704 int progress_done;
08fd81c9
DS
705};
706
707static int add_packed_commits(const struct object_id *oid,
708 struct packed_git *pack,
709 uint32_t pos,
710 void *data)
711{
712 struct packed_oid_list *list = (struct packed_oid_list*)data;
713 enum object_type type;
714 off_t offset = nth_packed_object_offset(pack, pos);
715 struct object_info oi = OBJECT_INFO_INIT;
716
7b0f2292
ÆAB
717 if (list->progress)
718 display_progress(list->progress, ++list->progress_done);
719
08fd81c9 720 oi.typep = &type;
fcb6df32 721 if (packed_object_info(the_repository, pack, offset, &oi) < 0)
4f5b532d 722 die(_("unable to get type of object %s"), oid_to_hex(oid));
08fd81c9
DS
723
724 if (type != OBJ_COMMIT)
725 return 0;
726
727 ALLOC_GROW(list->list, list->nr + 1, list->alloc);
728 oidcpy(&(list->list[list->nr]), oid);
729 list->nr++;
730
731 return 0;
732}
733
4f2542b4
DS
734static void add_missing_parents(struct packed_oid_list *oids, struct commit *commit)
735{
736 struct commit_list *parent;
737 for (parent = commit->parents; parent; parent = parent->next) {
738 if (!(parent->item->object.flags & UNINTERESTING)) {
739 ALLOC_GROW(oids->list, oids->nr + 1, oids->alloc);
740 oidcpy(&oids->list[oids->nr], &(parent->item->object.oid));
741 oids->nr++;
742 parent->item->object.flags |= UNINTERESTING;
743 }
744 }
745}
746
7b0f2292 747static void close_reachable(struct packed_oid_list *oids, int report_progress)
4f2542b4 748{
49bbc57a 749 int i;
4f2542b4 750 struct commit *commit;
7b0f2292 751 struct progress *progress = NULL;
4f2542b4 752
7b0f2292
ÆAB
753 if (report_progress)
754 progress = start_delayed_progress(
49bbc57a 755 _("Loading known commits in commit graph"), oids->nr);
4f2542b4 756 for (i = 0; i < oids->nr; i++) {
49bbc57a 757 display_progress(progress, i + 1);
c1f5eb49 758 commit = lookup_commit(the_repository, &oids->list[i]);
4f2542b4
DS
759 if (commit)
760 commit->object.flags |= UNINTERESTING;
761 }
01ca3877 762 stop_progress(&progress);
4f2542b4
DS
763
764 /*
765 * As this loop runs, oids->nr may grow, but not more
766 * than the number of missing commits in the reachable
767 * closure.
768 */
01ca3877
ÆAB
769 if (report_progress)
770 progress = start_delayed_progress(
49bbc57a 771 _("Expanding reachable commits in commit graph"), oids->nr);
4f2542b4 772 for (i = 0; i < oids->nr; i++) {
49bbc57a 773 display_progress(progress, i + 1);
c1f5eb49 774 commit = lookup_commit(the_repository, &oids->list[i]);
4f2542b4
DS
775
776 if (commit && !parse_commit(commit))
777 add_missing_parents(oids, commit);
778 }
01ca3877 779 stop_progress(&progress);
4f2542b4 780
01ca3877
ÆAB
781 if (report_progress)
782 progress = start_delayed_progress(
49bbc57a 783 _("Clearing commit marks in commit graph"), oids->nr);
4f2542b4 784 for (i = 0; i < oids->nr; i++) {
49bbc57a 785 display_progress(progress, i + 1);
c1f5eb49 786 commit = lookup_commit(the_repository, &oids->list[i]);
4f2542b4
DS
787
788 if (commit)
789 commit->object.flags &= ~UNINTERESTING;
790 }
7b0f2292 791 stop_progress(&progress);
4f2542b4
DS
792}
793
7b0f2292
ÆAB
794static void compute_generation_numbers(struct packed_commit_list* commits,
795 int report_progress)
3258c663
DS
796{
797 int i;
798 struct commit_list *list = NULL;
7b0f2292 799 struct progress *progress = NULL;
3258c663 800
7b0f2292
ÆAB
801 if (report_progress)
802 progress = start_progress(
803 _("Computing commit graph generation numbers"),
804 commits->nr);
3258c663 805 for (i = 0; i < commits->nr; i++) {
7b0f2292 806 display_progress(progress, i + 1);
3258c663
DS
807 if (commits->list[i]->generation != GENERATION_NUMBER_INFINITY &&
808 commits->list[i]->generation != GENERATION_NUMBER_ZERO)
809 continue;
810
811 commit_list_insert(commits->list[i], &list);
812 while (list) {
813 struct commit *current = list->item;
814 struct commit_list *parent;
815 int all_parents_computed = 1;
816 uint32_t max_generation = 0;
817
818 for (parent = current->parents; parent; parent = parent->next) {
819 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
820 parent->item->generation == GENERATION_NUMBER_ZERO) {
821 all_parents_computed = 0;
822 commit_list_insert(parent->item, &list);
823 break;
824 } else if (parent->item->generation > max_generation) {
825 max_generation = parent->item->generation;
826 }
827 }
828
829 if (all_parents_computed) {
830 current->generation = max_generation + 1;
831 pop_commit(&list);
832
833 if (current->generation > GENERATION_NUMBER_MAX)
834 current->generation = GENERATION_NUMBER_MAX;
835 }
836 }
837 }
7b0f2292 838 stop_progress(&progress);
3258c663
DS
839}
840
59fb8770
DS
841static int add_ref_to_list(const char *refname,
842 const struct object_id *oid,
843 int flags, void *cb_data)
844{
845 struct string_list *list = (struct string_list *)cb_data;
846
847 string_list_append(list, oid_to_hex(oid));
848 return 0;
849}
850
7b0f2292
ÆAB
851void write_commit_graph_reachable(const char *obj_dir, int append,
852 int report_progress)
59fb8770 853{
f4dbdfc4 854 struct string_list list = STRING_LIST_INIT_DUP;
59fb8770 855
59fb8770 856 for_each_ref(add_ref_to_list, &list);
7b0f2292 857 write_commit_graph(obj_dir, NULL, &list, append, report_progress);
f4dbdfc4
DS
858
859 string_list_clear(&list, 0);
59fb8770
DS
860}
861
049d51a2 862void write_commit_graph(const char *obj_dir,
d88b14b3
DS
863 struct string_list *pack_indexes,
864 struct string_list *commit_hex,
7b0f2292 865 int append, int report_progress)
08fd81c9
DS
866{
867 struct packed_oid_list oids;
868 struct packed_commit_list commits;
869 struct hashfile *f;
870 uint32_t i, count_distinct = 0;
871 char *graph_name;
08fd81c9
DS
872 struct lock_file lk = LOCK_INIT;
873 uint32_t chunk_ids[5];
874 uint64_t chunk_offsets[5];
875 int num_chunks;
876 int num_extra_edges;
877 struct commit_list *parent;
7b0f2292 878 struct progress *progress = NULL;
c1665998 879 const unsigned hashsz = the_hash_algo->rawsz;
53035c4f 880 uint64_t progress_cnt = 0;
28944739 881 struct strbuf progress_title = STRBUF_INIT;
d9b1b309 882 unsigned long approx_nr_objects;
08fd81c9 883
d6538246
DS
884 if (!commit_graph_compatible(the_repository))
885 return;
886
08fd81c9 887 oids.nr = 0;
d9b1b309
ÆAB
888 approx_nr_objects = approximate_object_count();
889 oids.alloc = approx_nr_objects / 32;
7b0f2292
ÆAB
890 oids.progress = NULL;
891 oids.progress_done = 0;
08fd81c9 892
7547b95b 893 if (append) {
dade47c0 894 prepare_commit_graph_one(the_repository, obj_dir);
85277506
JT
895 if (the_repository->objects->commit_graph)
896 oids.alloc += the_repository->objects->commit_graph->num_commits;
7547b95b
DS
897 }
898
08fd81c9
DS
899 if (oids.alloc < 1024)
900 oids.alloc = 1024;
901 ALLOC_ARRAY(oids.list, oids.alloc);
902
85277506
JT
903 if (append && the_repository->objects->commit_graph) {
904 struct commit_graph *commit_graph =
905 the_repository->objects->commit_graph;
7547b95b
DS
906 for (i = 0; i < commit_graph->num_commits; i++) {
907 const unsigned char *hash = commit_graph->chunk_oid_lookup +
908 commit_graph->hash_len * i;
909 hashcpy(oids.list[oids.nr++].hash, hash);
910 }
911 }
912
049d51a2
DS
913 if (pack_indexes) {
914 struct strbuf packname = STRBUF_INIT;
915 int dirlen;
916 strbuf_addf(&packname, "%s/pack/", obj_dir);
917 dirlen = packname.len;
7b0f2292 918 if (report_progress) {
7c7b8a7f
ÆAB
919 strbuf_addf(&progress_title,
920 Q_("Finding commits for commit graph in %d pack",
921 "Finding commits for commit graph in %d packs",
922 pack_indexes->nr),
923 pack_indexes->nr);
924 oids.progress = start_delayed_progress(progress_title.buf, 0);
7b0f2292
ÆAB
925 oids.progress_done = 0;
926 }
d88b14b3 927 for (i = 0; i < pack_indexes->nr; i++) {
049d51a2
DS
928 struct packed_git *p;
929 strbuf_setlen(&packname, dirlen);
d88b14b3 930 strbuf_addstr(&packname, pack_indexes->items[i].string);
049d51a2
DS
931 p = add_packed_git(packname.buf, packname.len, 1);
932 if (!p)
4f5b532d 933 die(_("error adding pack %s"), packname.buf);
049d51a2 934 if (open_pack_index(p))
4f5b532d 935 die(_("error opening index for %s"), packname.buf);
d7574c95
ÆAB
936 for_each_object_in_pack(p, add_packed_commits, &oids,
937 FOR_EACH_OBJECT_PACK_ORDER);
049d51a2 938 close_pack(p);
f4dbdfc4 939 free(p);
049d51a2 940 }
7b0f2292 941 stop_progress(&oids.progress);
7c7b8a7f 942 strbuf_reset(&progress_title);
049d51a2 943 strbuf_release(&packname);
3d5df01b
DS
944 }
945
946 if (commit_hex) {
7c7b8a7f
ÆAB
947 if (report_progress) {
948 strbuf_addf(&progress_title,
949 Q_("Finding commits for commit graph from %d ref",
950 "Finding commits for commit graph from %d refs",
951 commit_hex->nr),
952 commit_hex->nr);
953 progress = start_delayed_progress(progress_title.buf,
954 commit_hex->nr);
955 }
d88b14b3 956 for (i = 0; i < commit_hex->nr; i++) {
3d5df01b
DS
957 const char *end;
958 struct object_id oid;
959 struct commit *result;
960
7b0f2292 961 display_progress(progress, i + 1);
d88b14b3
DS
962 if (commit_hex->items[i].string &&
963 parse_oid_hex(commit_hex->items[i].string, &oid, &end))
3d5df01b
DS
964 continue;
965
21e1ee8f 966 result = lookup_commit_reference_gently(the_repository, &oid, 1);
3d5df01b
DS
967
968 if (result) {
969 ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc);
970 oidcpy(&oids.list[oids.nr], &(result->object.oid));
971 oids.nr++;
972 }
973 }
7b0f2292 974 stop_progress(&progress);
7c7b8a7f 975 strbuf_reset(&progress_title);
3d5df01b
DS
976 }
977
7b0f2292
ÆAB
978 if (!pack_indexes && !commit_hex) {
979 if (report_progress)
980 oids.progress = start_delayed_progress(
7c7b8a7f 981 _("Finding commits for commit graph among packed objects"),
d9b1b309 982 approx_nr_objects);
d7574c95
ÆAB
983 for_each_packed_object(add_packed_commits, &oids,
984 FOR_EACH_OBJECT_PACK_ORDER);
d9b1b309
ÆAB
985 if (oids.progress_done < approx_nr_objects)
986 display_progress(oids.progress, approx_nr_objects);
7b0f2292
ÆAB
987 stop_progress(&oids.progress);
988 }
049d51a2 989
7b0f2292 990 close_reachable(&oids, report_progress);
08fd81c9 991
890226cc
ÆAB
992 if (report_progress)
993 progress = start_delayed_progress(
994 _("Counting distinct commits in commit graph"),
995 oids.nr);
996 display_progress(progress, 0); /* TODO: Measure QSORT() progress */
08fd81c9 997 QSORT(oids.list, oids.nr, commit_compare);
08fd81c9
DS
998 count_distinct = 1;
999 for (i = 1; i < oids.nr; i++) {
890226cc 1000 display_progress(progress, i + 1);
9001dc2a 1001 if (!oideq(&oids.list[i - 1], &oids.list[i]))
08fd81c9
DS
1002 count_distinct++;
1003 }
890226cc 1004 stop_progress(&progress);
08fd81c9 1005
cce99cd8 1006 if (count_distinct >= GRAPH_EDGE_LAST_MASK)
08fd81c9
DS
1007 die(_("the commit graph format cannot write %d commits"), count_distinct);
1008
1009 commits.nr = 0;
1010 commits.alloc = count_distinct;
1011 ALLOC_ARRAY(commits.list, commits.alloc);
1012
1013 num_extra_edges = 0;
890226cc
ÆAB
1014 if (report_progress)
1015 progress = start_delayed_progress(
1016 _("Finding extra edges in commit graph"),
1017 oids.nr);
08fd81c9
DS
1018 for (i = 0; i < oids.nr; i++) {
1019 int num_parents = 0;
890226cc 1020 display_progress(progress, i + 1);
4a7e27e9 1021 if (i > 0 && oideq(&oids.list[i - 1], &oids.list[i]))
08fd81c9
DS
1022 continue;
1023
c1f5eb49 1024 commits.list[commits.nr] = lookup_commit(the_repository, &oids.list[i]);
08fd81c9
DS
1025 parse_commit(commits.list[commits.nr]);
1026
1027 for (parent = commits.list[commits.nr]->parents;
1028 parent; parent = parent->next)
1029 num_parents++;
1030
1031 if (num_parents > 2)
1032 num_extra_edges += num_parents - 1;
1033
1034 commits.nr++;
1035 }
1036 num_chunks = num_extra_edges ? 4 : 3;
890226cc 1037 stop_progress(&progress);
08fd81c9 1038
cce99cd8 1039 if (commits.nr >= GRAPH_EDGE_LAST_MASK)
08fd81c9
DS
1040 die(_("too many commits to write graph"));
1041
7b0f2292 1042 compute_generation_numbers(&commits, report_progress);
08fd81c9 1043
08fd81c9 1044 graph_name = get_commit_graph_filename(obj_dir);
f4dbdfc4
DS
1045 if (safe_create_leading_directories(graph_name)) {
1046 UNLEAK(graph_name);
33286dcd
DS
1047 die_errno(_("unable to create leading directories of %s"),
1048 graph_name);
f4dbdfc4 1049 }
08fd81c9 1050
33286dcd 1051 hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR);
08fd81c9
DS
1052 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1053
1054 hashwrite_be32(f, GRAPH_SIGNATURE);
1055
1056 hashwrite_u8(f, GRAPH_VERSION);
c1665998 1057 hashwrite_u8(f, oid_version());
08fd81c9
DS
1058 hashwrite_u8(f, num_chunks);
1059 hashwrite_u8(f, 0); /* unused padding byte */
1060
1061 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
1062 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
1063 chunk_ids[2] = GRAPH_CHUNKID_DATA;
1064 if (num_extra_edges)
5af7417b 1065 chunk_ids[3] = GRAPH_CHUNKID_EXTRAEDGES;
08fd81c9
DS
1066 else
1067 chunk_ids[3] = 0;
1068 chunk_ids[4] = 0;
1069
1070 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1071 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
c1665998 1072 chunk_offsets[2] = chunk_offsets[1] + hashsz * commits.nr;
1073 chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * commits.nr;
08fd81c9
DS
1074 chunk_offsets[4] = chunk_offsets[3] + 4 * num_extra_edges;
1075
1076 for (i = 0; i <= num_chunks; i++) {
1077 uint32_t chunk_write[3];
1078
1079 chunk_write[0] = htonl(chunk_ids[i]);
1080 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
1081 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
1082 hashwrite(f, chunk_write, 12);
1083 }
1084
28944739
ÆAB
1085 if (report_progress) {
1086 strbuf_addf(&progress_title,
1087 Q_("Writing out commit graph in %d pass",
1088 "Writing out commit graph in %d passes",
1089 num_chunks),
1090 num_chunks);
53035c4f 1091 progress = start_delayed_progress(
28944739 1092 progress_title.buf,
53035c4f 1093 num_chunks * commits.nr);
28944739 1094 }
53035c4f 1095 write_graph_chunk_fanout(f, commits.list, commits.nr, progress, &progress_cnt);
e5eac573
JH
1096 write_graph_chunk_oids(f, hashsz, commits.list, commits.nr, progress, &progress_cnt);
1097 write_graph_chunk_data(f, hashsz, commits.list, commits.nr, progress, &progress_cnt);
857ba928 1098 if (num_extra_edges)
53035c4f
ÆAB
1099 write_graph_chunk_extra_edges(f, commits.list, commits.nr, progress, &progress_cnt);
1100 stop_progress(&progress);
28944739 1101 strbuf_release(&progress_title);
08fd81c9 1102
829a3215 1103 close_commit_graph(the_repository);
08fd81c9
DS
1104 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1105 commit_lock_file(&lk);
1106
f4dbdfc4
DS
1107 free(graph_name);
1108 free(commits.list);
08fd81c9 1109 free(oids.list);
08fd81c9 1110}
283e68c7 1111
41df0e30 1112#define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
283e68c7
DS
1113static int verify_commit_graph_error;
1114
1115static void graph_report(const char *fmt, ...)
1116{
1117 va_list ap;
1118
1119 verify_commit_graph_error = 1;
1120 va_start(ap, fmt);
1121 vfprintf(stderr, fmt, ap);
1122 fprintf(stderr, "\n");
1123 va_end(ap);
1124}
1125
1373e547
DS
1126#define GENERATION_ZERO_EXISTS 1
1127#define GENERATION_NUMBER_EXISTS 2
1128
283e68c7
DS
1129int verify_commit_graph(struct repository *r, struct commit_graph *g)
1130{
9bda8467 1131 uint32_t i, cur_fanout_pos = 0;
41df0e30 1132 struct object_id prev_oid, cur_oid, checksum;
1373e547 1133 int generation_zero = 0;
41df0e30
DS
1134 struct hashfile *f;
1135 int devnull;
1f7f557f 1136 struct progress *progress = NULL;
9bda8467 1137
283e68c7
DS
1138 if (!g) {
1139 graph_report("no commit-graph file loaded");
1140 return 1;
1141 }
1142
2ac138d5 1143 verify_commit_graph_error = verify_commit_graph_lite(g);
9bda8467
DS
1144 if (verify_commit_graph_error)
1145 return verify_commit_graph_error;
1146
41df0e30
DS
1147 devnull = open("/dev/null", O_WRONLY);
1148 f = hashfd(devnull, NULL);
1149 hashwrite(f, g->data, g->data_len - g->hash_len);
1150 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
67947c34 1151 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
41df0e30
DS
1152 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
1153 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
1154 }
1155
9bda8467 1156 for (i = 0; i < g->num_commits; i++) {
2e3c0737
DS
1157 struct commit *graph_commit;
1158
9bda8467
DS
1159 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1160
1161 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
1162 graph_report("commit-graph has incorrect OID order: %s then %s",
1163 oid_to_hex(&prev_oid),
1164 oid_to_hex(&cur_oid));
1165
1166 oidcpy(&prev_oid, &cur_oid);
1167
1168 while (cur_oid.hash[0] > cur_fanout_pos) {
1169 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1170
1171 if (i != fanout_value)
1172 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
1173 cur_fanout_pos, fanout_value, i);
1174 cur_fanout_pos++;
1175 }
2e3c0737 1176
82952964 1177 graph_commit = lookup_commit(r, &cur_oid);
4f542b7a 1178 if (!parse_commit_in_graph_one(r, g, graph_commit))
2e3c0737
DS
1179 graph_report("failed to parse %s from commit-graph",
1180 oid_to_hex(&cur_oid));
9bda8467
DS
1181 }
1182
1183 while (cur_fanout_pos < 256) {
1184 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1185
1186 if (g->num_commits != fanout_value)
1187 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
1188 cur_fanout_pos, fanout_value, i);
1189
1190 cur_fanout_pos++;
1191 }
1192
41df0e30 1193 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
96af91d4
DS
1194 return verify_commit_graph_error;
1195
1f7f557f
ÆAB
1196 progress = start_progress(_("Verifying commits in commit graph"),
1197 g->num_commits);
96af91d4 1198 for (i = 0; i < g->num_commits; i++) {
2e3c0737 1199 struct commit *graph_commit, *odb_commit;
53614b13 1200 struct commit_list *graph_parents, *odb_parents;
1373e547 1201 uint32_t max_generation = 0;
96af91d4 1202
1f7f557f 1203 display_progress(progress, i + 1);
96af91d4
DS
1204 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1205
82952964 1206 graph_commit = lookup_commit(r, &cur_oid);
96af91d4
DS
1207 odb_commit = (struct commit *)create_object(r, cur_oid.hash, alloc_commit_node(r));
1208 if (parse_commit_internal(odb_commit, 0, 0)) {
1209 graph_report("failed to parse %s from object database",
1210 oid_to_hex(&cur_oid));
1211 continue;
1212 }
2e3c0737 1213
4f542b7a 1214 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2e3c0737
DS
1215 get_commit_tree_oid(odb_commit)))
1216 graph_report("root tree OID for commit %s in commit-graph is %s != %s",
1217 oid_to_hex(&cur_oid),
1218 oid_to_hex(get_commit_tree_oid(graph_commit)),
1219 oid_to_hex(get_commit_tree_oid(odb_commit)));
53614b13
DS
1220
1221 graph_parents = graph_commit->parents;
1222 odb_parents = odb_commit->parents;
1223
1224 while (graph_parents) {
1225 if (odb_parents == NULL) {
1226 graph_report("commit-graph parent list for commit %s is too long",
1227 oid_to_hex(&cur_oid));
1228 break;
1229 }
1230
9001dc2a 1231 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
53614b13
DS
1232 graph_report("commit-graph parent for %s is %s != %s",
1233 oid_to_hex(&cur_oid),
1234 oid_to_hex(&graph_parents->item->object.oid),
1235 oid_to_hex(&odb_parents->item->object.oid));
1236
1373e547
DS
1237 if (graph_parents->item->generation > max_generation)
1238 max_generation = graph_parents->item->generation;
1239
53614b13
DS
1240 graph_parents = graph_parents->next;
1241 odb_parents = odb_parents->next;
1242 }
1243
1244 if (odb_parents != NULL)
1245 graph_report("commit-graph parent list for commit %s terminates early",
1246 oid_to_hex(&cur_oid));
1373e547
DS
1247
1248 if (!graph_commit->generation) {
1249 if (generation_zero == GENERATION_NUMBER_EXISTS)
1250 graph_report("commit-graph has generation number zero for commit %s, but non-zero elsewhere",
1251 oid_to_hex(&cur_oid));
1252 generation_zero = GENERATION_ZERO_EXISTS;
1253 } else if (generation_zero == GENERATION_ZERO_EXISTS)
1254 graph_report("commit-graph has non-zero generation number for commit %s, but zero elsewhere",
1255 oid_to_hex(&cur_oid));
1256
1257 if (generation_zero == GENERATION_ZERO_EXISTS)
1258 continue;
1259
1260 /*
1261 * If one of our parents has generation GENERATION_NUMBER_MAX, then
1262 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
1263 * extra logic in the following condition.
1264 */
1265 if (max_generation == GENERATION_NUMBER_MAX)
1266 max_generation--;
1267
1268 if (graph_commit->generation != max_generation + 1)
1269 graph_report("commit-graph generation for commit %s is %u != %u",
1270 oid_to_hex(&cur_oid),
1271 graph_commit->generation,
1272 max_generation + 1);
88968ebf
DS
1273
1274 if (graph_commit->date != odb_commit->date)
1275 graph_report("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime,
1276 oid_to_hex(&cur_oid),
1277 graph_commit->date,
1278 odb_commit->date);
96af91d4 1279 }
1f7f557f 1280 stop_progress(&progress);
96af91d4 1281
283e68c7
DS
1282 return verify_commit_graph_error;
1283}
c3756d5b
JT
1284
1285void free_commit_graph(struct commit_graph *g)
1286{
1287 if (!g)
1288 return;
1289 if (g->graph_fd >= 0) {
1290 munmap((void *)g->data, g->data_len);
1291 g->data = NULL;
1292 close(g->graph_fd);
1293 }
1294 free(g);
1295}