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