]> git.ipfire.org Git - thirdparty/git.git/blame - commit-graph.c
l10n: fr.po v2.22.0.rnd1
[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
a133c40b
NTND
400static inline void set_commit_tree(struct commit *c, struct tree *t)
401{
402 c->maybe_tree = t;
403}
404
4f542b7a
SB
405static int fill_commit_in_graph(struct repository *r,
406 struct commit *item,
407 struct commit_graph *g, uint32_t pos)
177722b3 408{
177722b3
DS
409 uint32_t edge_value;
410 uint32_t *parent_data_ptr;
411 uint64_t date_low, date_high;
412 struct commit_list **pptr;
413 const unsigned char *commit_data = g->chunk_commit_data + (g->hash_len + 16) * pos;
414
415 item->object.parsed = 1;
416 item->graph_pos = pos;
417
a133c40b 418 set_commit_tree(item, NULL);
177722b3
DS
419
420 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
421 date_low = get_be32(commit_data + g->hash_len + 12);
422 item->date = (timestamp_t)((date_high << 32) | date_low);
423
83073cc9
DS
424 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
425
177722b3
DS
426 pptr = &item->parents;
427
428 edge_value = get_be32(commit_data + g->hash_len);
429 if (edge_value == GRAPH_PARENT_NONE)
430 return 1;
4f542b7a 431 pptr = insert_parent_or_die(r, g, edge_value, pptr);
177722b3
DS
432
433 edge_value = get_be32(commit_data + g->hash_len + 4);
434 if (edge_value == GRAPH_PARENT_NONE)
435 return 1;
5af7417b 436 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
4f542b7a 437 pptr = insert_parent_or_die(r, g, edge_value, pptr);
177722b3
DS
438 return 1;
439 }
440
5af7417b 441 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
177722b3
DS
442 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
443 do {
444 edge_value = get_be32(parent_data_ptr);
4f542b7a 445 pptr = insert_parent_or_die(r, g,
177722b3
DS
446 edge_value & GRAPH_EDGE_LAST_MASK,
447 pptr);
448 parent_data_ptr++;
449 } while (!(edge_value & GRAPH_LAST_EDGE));
450
451 return 1;
452}
453
e2838d85
DS
454static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
455{
456 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
457 *pos = item->graph_pos;
458 return 1;
459 } else {
460 return bsearch_graph(g, &(item->object.oid), pos);
461 }
462}
463
4f542b7a
SB
464static int parse_commit_in_graph_one(struct repository *r,
465 struct commit_graph *g,
466 struct commit *item)
177722b3 467{
e2838d85
DS
468 uint32_t pos;
469
177722b3
DS
470 if (item->object.parsed)
471 return 1;
ee797053
DS
472
473 if (find_commit_in_graph(item, g, &pos))
4f542b7a 474 return fill_commit_in_graph(r, item, g, pos);
ee797053
DS
475
476 return 0;
477}
478
dade47c0 479int parse_commit_in_graph(struct repository *r, struct commit *item)
ee797053 480{
dade47c0 481 if (!prepare_commit_graph(r))
ee797053 482 return 0;
4f542b7a 483 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
177722b3
DS
484}
485
dade47c0 486void load_commit_graph_info(struct repository *r, struct commit *item)
e2838d85
DS
487{
488 uint32_t pos;
dade47c0 489 if (!prepare_commit_graph(r))
e2838d85 490 return;
dade47c0
JT
491 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
492 fill_commit_graph_info(item, r->objects->commit_graph, pos);
e2838d85
DS
493}
494
4f542b7a
SB
495static struct tree *load_tree_for_commit(struct repository *r,
496 struct commit_graph *g,
497 struct commit *c)
7b8a21db
DS
498{
499 struct object_id oid;
500 const unsigned char *commit_data = g->chunk_commit_data +
501 GRAPH_DATA_WIDTH * (c->graph_pos);
502
503 hashcpy(oid.hash, commit_data);
a133c40b 504 set_commit_tree(c, lookup_tree(r, &oid));
7b8a21db
DS
505
506 return c->maybe_tree;
507}
508
4f542b7a
SB
509static struct tree *get_commit_tree_in_graph_one(struct repository *r,
510 struct commit_graph *g,
0cbef8f8 511 const struct commit *c)
7b8a21db
DS
512{
513 if (c->maybe_tree)
514 return c->maybe_tree;
515 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
0cbef8f8
DS
516 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
517
4f542b7a 518 return load_tree_for_commit(r, g, (struct commit *)c);
0cbef8f8 519}
7b8a21db 520
dade47c0 521struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
0cbef8f8 522{
4f542b7a 523 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
7b8a21db
DS
524}
525
08fd81c9
DS
526static void write_graph_chunk_fanout(struct hashfile *f,
527 struct commit **commits,
53035c4f
ÆAB
528 int nr_commits,
529 struct progress *progress,
530 uint64_t *progress_cnt)
08fd81c9
DS
531{
532 int i, count = 0;
533 struct commit **list = commits;
534
535 /*
536 * Write the first-level table (the list is sorted,
537 * but we use a 256-entry lookup to be able to avoid
538 * having to do eight extra binary search iterations).
539 */
540 for (i = 0; i < 256; i++) {
541 while (count < nr_commits) {
542 if ((*list)->object.oid.hash[0] != i)
543 break;
53035c4f 544 display_progress(progress, ++*progress_cnt);
08fd81c9
DS
545 count++;
546 list++;
547 }
548
549 hashwrite_be32(f, count);
550 }
551}
552
553static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
53035c4f
ÆAB
554 struct commit **commits, int nr_commits,
555 struct progress *progress,
556 uint64_t *progress_cnt)
08fd81c9
DS
557{
558 struct commit **list = commits;
559 int count;
53035c4f
ÆAB
560 for (count = 0; count < nr_commits; count++, list++) {
561 display_progress(progress, ++*progress_cnt);
08fd81c9 562 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
53035c4f 563 }
08fd81c9
DS
564}
565
566static const unsigned char *commit_to_sha1(size_t index, void *table)
567{
568 struct commit **commits = table;
569 return commits[index]->object.oid.hash;
570}
571
572static void write_graph_chunk_data(struct hashfile *f, int hash_len,
53035c4f
ÆAB
573 struct commit **commits, int nr_commits,
574 struct progress *progress,
575 uint64_t *progress_cnt)
08fd81c9
DS
576{
577 struct commit **list = commits;
578 struct commit **last = commits + nr_commits;
579 uint32_t num_extra_edges = 0;
580
581 while (list < last) {
582 struct commit_list *parent;
583 int edge_value;
584 uint32_t packedDate[2];
53035c4f 585 display_progress(progress, ++*progress_cnt);
08fd81c9 586
43d35618 587 parse_commit_no_graph(*list);
2e27bd77 588 hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
08fd81c9
DS
589
590 parent = (*list)->parents;
591
592 if (!parent)
593 edge_value = GRAPH_PARENT_NONE;
594 else {
595 edge_value = sha1_pos(parent->item->object.oid.hash,
596 commits,
597 nr_commits,
598 commit_to_sha1);
599
600 if (edge_value < 0)
cce99cd8
DS
601 BUG("missing parent %s for commit %s",
602 oid_to_hex(&parent->item->object.oid),
603 oid_to_hex(&(*list)->object.oid));
08fd81c9
DS
604 }
605
606 hashwrite_be32(f, edge_value);
607
608 if (parent)
609 parent = parent->next;
610
611 if (!parent)
612 edge_value = GRAPH_PARENT_NONE;
613 else if (parent->next)
5af7417b 614 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
08fd81c9
DS
615 else {
616 edge_value = sha1_pos(parent->item->object.oid.hash,
617 commits,
618 nr_commits,
619 commit_to_sha1);
620 if (edge_value < 0)
cce99cd8
DS
621 BUG("missing parent %s for commit %s",
622 oid_to_hex(&parent->item->object.oid),
623 oid_to_hex(&(*list)->object.oid));
08fd81c9
DS
624 }
625
626 hashwrite_be32(f, edge_value);
627
5af7417b 628 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
08fd81c9
DS
629 do {
630 num_extra_edges++;
631 parent = parent->next;
632 } while (parent);
633 }
634
635 if (sizeof((*list)->date) > 4)
636 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
637 else
638 packedDate[0] = 0;
639
3258c663
DS
640 packedDate[0] |= htonl((*list)->generation << 2);
641
08fd81c9
DS
642 packedDate[1] = htonl((*list)->date);
643 hashwrite(f, packedDate, 8);
644
645 list++;
646 }
647}
648
5af7417b 649static void write_graph_chunk_extra_edges(struct hashfile *f,
08fd81c9 650 struct commit **commits,
53035c4f
ÆAB
651 int nr_commits,
652 struct progress *progress,
653 uint64_t *progress_cnt)
08fd81c9
DS
654{
655 struct commit **list = commits;
656 struct commit **last = commits + nr_commits;
657 struct commit_list *parent;
658
659 while (list < last) {
660 int num_parents = 0;
53035c4f
ÆAB
661
662 display_progress(progress, ++*progress_cnt);
663
08fd81c9
DS
664 for (parent = (*list)->parents; num_parents < 3 && parent;
665 parent = parent->next)
666 num_parents++;
667
668 if (num_parents <= 2) {
669 list++;
670 continue;
671 }
672
673 /* Since num_parents > 2, this initializer is safe. */
674 for (parent = (*list)->parents->next; parent; parent = parent->next) {
675 int edge_value = sha1_pos(parent->item->object.oid.hash,
676 commits,
677 nr_commits,
678 commit_to_sha1);
679
680 if (edge_value < 0)
cce99cd8
DS
681 BUG("missing parent %s for commit %s",
682 oid_to_hex(&parent->item->object.oid),
683 oid_to_hex(&(*list)->object.oid));
08fd81c9
DS
684 else if (!parent->next)
685 edge_value |= GRAPH_LAST_EDGE;
686
687 hashwrite_be32(f, edge_value);
688 }
689
690 list++;
691 }
692}
693
694static int commit_compare(const void *_a, const void *_b)
695{
696 const struct object_id *a = (const struct object_id *)_a;
697 const struct object_id *b = (const struct object_id *)_b;
698 return oidcmp(a, b);
699}
700
701struct packed_commit_list {
702 struct commit **list;
703 int nr;
704 int alloc;
705};
706
707struct packed_oid_list {
708 struct object_id *list;
709 int nr;
710 int alloc;
7b0f2292
ÆAB
711 struct progress *progress;
712 int progress_done;
08fd81c9
DS
713};
714
715static int add_packed_commits(const struct object_id *oid,
716 struct packed_git *pack,
717 uint32_t pos,
718 void *data)
719{
720 struct packed_oid_list *list = (struct packed_oid_list*)data;
721 enum object_type type;
722 off_t offset = nth_packed_object_offset(pack, pos);
723 struct object_info oi = OBJECT_INFO_INIT;
724
7b0f2292
ÆAB
725 if (list->progress)
726 display_progress(list->progress, ++list->progress_done);
727
08fd81c9 728 oi.typep = &type;
fcb6df32 729 if (packed_object_info(the_repository, pack, offset, &oi) < 0)
4f5b532d 730 die(_("unable to get type of object %s"), oid_to_hex(oid));
08fd81c9
DS
731
732 if (type != OBJ_COMMIT)
733 return 0;
734
735 ALLOC_GROW(list->list, list->nr + 1, list->alloc);
736 oidcpy(&(list->list[list->nr]), oid);
737 list->nr++;
738
739 return 0;
740}
741
4f2542b4
DS
742static void add_missing_parents(struct packed_oid_list *oids, struct commit *commit)
743{
744 struct commit_list *parent;
745 for (parent = commit->parents; parent; parent = parent->next) {
746 if (!(parent->item->object.flags & UNINTERESTING)) {
747 ALLOC_GROW(oids->list, oids->nr + 1, oids->alloc);
748 oidcpy(&oids->list[oids->nr], &(parent->item->object.oid));
749 oids->nr++;
750 parent->item->object.flags |= UNINTERESTING;
751 }
752 }
753}
754
7b0f2292 755static void close_reachable(struct packed_oid_list *oids, int report_progress)
4f2542b4 756{
49bbc57a 757 int i;
4f2542b4 758 struct commit *commit;
7b0f2292 759 struct progress *progress = NULL;
4f2542b4 760
7b0f2292
ÆAB
761 if (report_progress)
762 progress = start_delayed_progress(
49bbc57a 763 _("Loading known commits in commit graph"), oids->nr);
4f2542b4 764 for (i = 0; i < oids->nr; i++) {
49bbc57a 765 display_progress(progress, i + 1);
c1f5eb49 766 commit = lookup_commit(the_repository, &oids->list[i]);
4f2542b4
DS
767 if (commit)
768 commit->object.flags |= UNINTERESTING;
769 }
01ca3877 770 stop_progress(&progress);
4f2542b4
DS
771
772 /*
773 * As this loop runs, oids->nr may grow, but not more
774 * than the number of missing commits in the reachable
775 * closure.
776 */
01ca3877
ÆAB
777 if (report_progress)
778 progress = start_delayed_progress(
49bbc57a 779 _("Expanding reachable commits in commit graph"), oids->nr);
4f2542b4 780 for (i = 0; i < oids->nr; i++) {
49bbc57a 781 display_progress(progress, i + 1);
c1f5eb49 782 commit = lookup_commit(the_repository, &oids->list[i]);
4f2542b4 783
43d35618 784 if (commit && !parse_commit_no_graph(commit))
4f2542b4
DS
785 add_missing_parents(oids, commit);
786 }
01ca3877 787 stop_progress(&progress);
4f2542b4 788
01ca3877
ÆAB
789 if (report_progress)
790 progress = start_delayed_progress(
49bbc57a 791 _("Clearing commit marks in commit graph"), oids->nr);
4f2542b4 792 for (i = 0; i < oids->nr; i++) {
49bbc57a 793 display_progress(progress, i + 1);
c1f5eb49 794 commit = lookup_commit(the_repository, &oids->list[i]);
4f2542b4
DS
795
796 if (commit)
797 commit->object.flags &= ~UNINTERESTING;
798 }
7b0f2292 799 stop_progress(&progress);
4f2542b4
DS
800}
801
7b0f2292
ÆAB
802static void compute_generation_numbers(struct packed_commit_list* commits,
803 int report_progress)
3258c663
DS
804{
805 int i;
806 struct commit_list *list = NULL;
7b0f2292 807 struct progress *progress = NULL;
3258c663 808
7b0f2292
ÆAB
809 if (report_progress)
810 progress = start_progress(
811 _("Computing commit graph generation numbers"),
812 commits->nr);
3258c663 813 for (i = 0; i < commits->nr; i++) {
7b0f2292 814 display_progress(progress, i + 1);
3258c663
DS
815 if (commits->list[i]->generation != GENERATION_NUMBER_INFINITY &&
816 commits->list[i]->generation != GENERATION_NUMBER_ZERO)
817 continue;
818
819 commit_list_insert(commits->list[i], &list);
820 while (list) {
821 struct commit *current = list->item;
822 struct commit_list *parent;
823 int all_parents_computed = 1;
824 uint32_t max_generation = 0;
825
826 for (parent = current->parents; parent; parent = parent->next) {
827 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
828 parent->item->generation == GENERATION_NUMBER_ZERO) {
829 all_parents_computed = 0;
830 commit_list_insert(parent->item, &list);
831 break;
832 } else if (parent->item->generation > max_generation) {
833 max_generation = parent->item->generation;
834 }
835 }
836
837 if (all_parents_computed) {
838 current->generation = max_generation + 1;
839 pop_commit(&list);
840
841 if (current->generation > GENERATION_NUMBER_MAX)
842 current->generation = GENERATION_NUMBER_MAX;
843 }
844 }
845 }
7b0f2292 846 stop_progress(&progress);
3258c663
DS
847}
848
59fb8770
DS
849static int add_ref_to_list(const char *refname,
850 const struct object_id *oid,
851 int flags, void *cb_data)
852{
853 struct string_list *list = (struct string_list *)cb_data;
854
855 string_list_append(list, oid_to_hex(oid));
856 return 0;
857}
858
7b0f2292
ÆAB
859void write_commit_graph_reachable(const char *obj_dir, int append,
860 int report_progress)
59fb8770 861{
f4dbdfc4 862 struct string_list list = STRING_LIST_INIT_DUP;
59fb8770 863
59fb8770 864 for_each_ref(add_ref_to_list, &list);
7b0f2292 865 write_commit_graph(obj_dir, NULL, &list, append, report_progress);
f4dbdfc4
DS
866
867 string_list_clear(&list, 0);
59fb8770
DS
868}
869
049d51a2 870void write_commit_graph(const char *obj_dir,
d88b14b3
DS
871 struct string_list *pack_indexes,
872 struct string_list *commit_hex,
7b0f2292 873 int append, int report_progress)
08fd81c9
DS
874{
875 struct packed_oid_list oids;
876 struct packed_commit_list commits;
877 struct hashfile *f;
878 uint32_t i, count_distinct = 0;
879 char *graph_name;
08fd81c9
DS
880 struct lock_file lk = LOCK_INIT;
881 uint32_t chunk_ids[5];
882 uint64_t chunk_offsets[5];
883 int num_chunks;
884 int num_extra_edges;
885 struct commit_list *parent;
7b0f2292 886 struct progress *progress = NULL;
c1665998 887 const unsigned hashsz = the_hash_algo->rawsz;
53035c4f 888 uint64_t progress_cnt = 0;
28944739 889 struct strbuf progress_title = STRBUF_INIT;
d9b1b309 890 unsigned long approx_nr_objects;
08fd81c9 891
d6538246
DS
892 if (!commit_graph_compatible(the_repository))
893 return;
894
08fd81c9 895 oids.nr = 0;
d9b1b309
ÆAB
896 approx_nr_objects = approximate_object_count();
897 oids.alloc = approx_nr_objects / 32;
7b0f2292
ÆAB
898 oids.progress = NULL;
899 oids.progress_done = 0;
08fd81c9 900
7547b95b 901 if (append) {
dade47c0 902 prepare_commit_graph_one(the_repository, obj_dir);
85277506
JT
903 if (the_repository->objects->commit_graph)
904 oids.alloc += the_repository->objects->commit_graph->num_commits;
7547b95b
DS
905 }
906
08fd81c9
DS
907 if (oids.alloc < 1024)
908 oids.alloc = 1024;
909 ALLOC_ARRAY(oids.list, oids.alloc);
910
85277506
JT
911 if (append && the_repository->objects->commit_graph) {
912 struct commit_graph *commit_graph =
913 the_repository->objects->commit_graph;
7547b95b
DS
914 for (i = 0; i < commit_graph->num_commits; i++) {
915 const unsigned char *hash = commit_graph->chunk_oid_lookup +
916 commit_graph->hash_len * i;
917 hashcpy(oids.list[oids.nr++].hash, hash);
918 }
919 }
920
049d51a2
DS
921 if (pack_indexes) {
922 struct strbuf packname = STRBUF_INIT;
923 int dirlen;
924 strbuf_addf(&packname, "%s/pack/", obj_dir);
925 dirlen = packname.len;
7b0f2292 926 if (report_progress) {
7c7b8a7f
ÆAB
927 strbuf_addf(&progress_title,
928 Q_("Finding commits for commit graph in %d pack",
929 "Finding commits for commit graph in %d packs",
930 pack_indexes->nr),
931 pack_indexes->nr);
932 oids.progress = start_delayed_progress(progress_title.buf, 0);
7b0f2292
ÆAB
933 oids.progress_done = 0;
934 }
d88b14b3 935 for (i = 0; i < pack_indexes->nr; i++) {
049d51a2
DS
936 struct packed_git *p;
937 strbuf_setlen(&packname, dirlen);
d88b14b3 938 strbuf_addstr(&packname, pack_indexes->items[i].string);
049d51a2
DS
939 p = add_packed_git(packname.buf, packname.len, 1);
940 if (!p)
4f5b532d 941 die(_("error adding pack %s"), packname.buf);
049d51a2 942 if (open_pack_index(p))
4f5b532d 943 die(_("error opening index for %s"), packname.buf);
d7574c95
ÆAB
944 for_each_object_in_pack(p, add_packed_commits, &oids,
945 FOR_EACH_OBJECT_PACK_ORDER);
049d51a2 946 close_pack(p);
f4dbdfc4 947 free(p);
049d51a2 948 }
7b0f2292 949 stop_progress(&oids.progress);
7c7b8a7f 950 strbuf_reset(&progress_title);
049d51a2 951 strbuf_release(&packname);
3d5df01b
DS
952 }
953
954 if (commit_hex) {
7c7b8a7f
ÆAB
955 if (report_progress) {
956 strbuf_addf(&progress_title,
957 Q_("Finding commits for commit graph from %d ref",
958 "Finding commits for commit graph from %d refs",
959 commit_hex->nr),
960 commit_hex->nr);
961 progress = start_delayed_progress(progress_title.buf,
962 commit_hex->nr);
963 }
d88b14b3 964 for (i = 0; i < commit_hex->nr; i++) {
3d5df01b
DS
965 const char *end;
966 struct object_id oid;
967 struct commit *result;
968
7b0f2292 969 display_progress(progress, i + 1);
d88b14b3
DS
970 if (commit_hex->items[i].string &&
971 parse_oid_hex(commit_hex->items[i].string, &oid, &end))
3d5df01b
DS
972 continue;
973
21e1ee8f 974 result = lookup_commit_reference_gently(the_repository, &oid, 1);
3d5df01b
DS
975
976 if (result) {
977 ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc);
978 oidcpy(&oids.list[oids.nr], &(result->object.oid));
979 oids.nr++;
980 }
981 }
7b0f2292 982 stop_progress(&progress);
7c7b8a7f 983 strbuf_reset(&progress_title);
3d5df01b
DS
984 }
985
7b0f2292
ÆAB
986 if (!pack_indexes && !commit_hex) {
987 if (report_progress)
988 oids.progress = start_delayed_progress(
7c7b8a7f 989 _("Finding commits for commit graph among packed objects"),
d9b1b309 990 approx_nr_objects);
d7574c95
ÆAB
991 for_each_packed_object(add_packed_commits, &oids,
992 FOR_EACH_OBJECT_PACK_ORDER);
d9b1b309
ÆAB
993 if (oids.progress_done < approx_nr_objects)
994 display_progress(oids.progress, approx_nr_objects);
7b0f2292
ÆAB
995 stop_progress(&oids.progress);
996 }
049d51a2 997
7b0f2292 998 close_reachable(&oids, report_progress);
08fd81c9 999
890226cc
ÆAB
1000 if (report_progress)
1001 progress = start_delayed_progress(
1002 _("Counting distinct commits in commit graph"),
1003 oids.nr);
1004 display_progress(progress, 0); /* TODO: Measure QSORT() progress */
08fd81c9 1005 QSORT(oids.list, oids.nr, commit_compare);
08fd81c9
DS
1006 count_distinct = 1;
1007 for (i = 1; i < oids.nr; i++) {
890226cc 1008 display_progress(progress, i + 1);
9001dc2a 1009 if (!oideq(&oids.list[i - 1], &oids.list[i]))
08fd81c9
DS
1010 count_distinct++;
1011 }
890226cc 1012 stop_progress(&progress);
08fd81c9 1013
cce99cd8 1014 if (count_distinct >= GRAPH_EDGE_LAST_MASK)
08fd81c9
DS
1015 die(_("the commit graph format cannot write %d commits"), count_distinct);
1016
1017 commits.nr = 0;
1018 commits.alloc = count_distinct;
1019 ALLOC_ARRAY(commits.list, commits.alloc);
1020
1021 num_extra_edges = 0;
890226cc
ÆAB
1022 if (report_progress)
1023 progress = start_delayed_progress(
1024 _("Finding extra edges in commit graph"),
1025 oids.nr);
08fd81c9
DS
1026 for (i = 0; i < oids.nr; i++) {
1027 int num_parents = 0;
890226cc 1028 display_progress(progress, i + 1);
4a7e27e9 1029 if (i > 0 && oideq(&oids.list[i - 1], &oids.list[i]))
08fd81c9
DS
1030 continue;
1031
c1f5eb49 1032 commits.list[commits.nr] = lookup_commit(the_repository, &oids.list[i]);
43d35618 1033 parse_commit_no_graph(commits.list[commits.nr]);
08fd81c9
DS
1034
1035 for (parent = commits.list[commits.nr]->parents;
1036 parent; parent = parent->next)
1037 num_parents++;
1038
1039 if (num_parents > 2)
1040 num_extra_edges += num_parents - 1;
1041
1042 commits.nr++;
1043 }
1044 num_chunks = num_extra_edges ? 4 : 3;
890226cc 1045 stop_progress(&progress);
08fd81c9 1046
cce99cd8 1047 if (commits.nr >= GRAPH_EDGE_LAST_MASK)
08fd81c9
DS
1048 die(_("too many commits to write graph"));
1049
7b0f2292 1050 compute_generation_numbers(&commits, report_progress);
08fd81c9 1051
08fd81c9 1052 graph_name = get_commit_graph_filename(obj_dir);
f4dbdfc4
DS
1053 if (safe_create_leading_directories(graph_name)) {
1054 UNLEAK(graph_name);
33286dcd
DS
1055 die_errno(_("unable to create leading directories of %s"),
1056 graph_name);
f4dbdfc4 1057 }
08fd81c9 1058
33286dcd 1059 hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR);
08fd81c9
DS
1060 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1061
1062 hashwrite_be32(f, GRAPH_SIGNATURE);
1063
1064 hashwrite_u8(f, GRAPH_VERSION);
c1665998 1065 hashwrite_u8(f, oid_version());
08fd81c9
DS
1066 hashwrite_u8(f, num_chunks);
1067 hashwrite_u8(f, 0); /* unused padding byte */
1068
1069 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
1070 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
1071 chunk_ids[2] = GRAPH_CHUNKID_DATA;
1072 if (num_extra_edges)
5af7417b 1073 chunk_ids[3] = GRAPH_CHUNKID_EXTRAEDGES;
08fd81c9
DS
1074 else
1075 chunk_ids[3] = 0;
1076 chunk_ids[4] = 0;
1077
1078 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1079 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
c1665998 1080 chunk_offsets[2] = chunk_offsets[1] + hashsz * commits.nr;
1081 chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * commits.nr;
08fd81c9
DS
1082 chunk_offsets[4] = chunk_offsets[3] + 4 * num_extra_edges;
1083
1084 for (i = 0; i <= num_chunks; i++) {
1085 uint32_t chunk_write[3];
1086
1087 chunk_write[0] = htonl(chunk_ids[i]);
1088 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
1089 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
1090 hashwrite(f, chunk_write, 12);
1091 }
1092
28944739
ÆAB
1093 if (report_progress) {
1094 strbuf_addf(&progress_title,
1095 Q_("Writing out commit graph in %d pass",
1096 "Writing out commit graph in %d passes",
1097 num_chunks),
1098 num_chunks);
53035c4f 1099 progress = start_delayed_progress(
28944739 1100 progress_title.buf,
53035c4f 1101 num_chunks * commits.nr);
28944739 1102 }
53035c4f 1103 write_graph_chunk_fanout(f, commits.list, commits.nr, progress, &progress_cnt);
e5eac573
JH
1104 write_graph_chunk_oids(f, hashsz, commits.list, commits.nr, progress, &progress_cnt);
1105 write_graph_chunk_data(f, hashsz, commits.list, commits.nr, progress, &progress_cnt);
857ba928 1106 if (num_extra_edges)
53035c4f
ÆAB
1107 write_graph_chunk_extra_edges(f, commits.list, commits.nr, progress, &progress_cnt);
1108 stop_progress(&progress);
28944739 1109 strbuf_release(&progress_title);
08fd81c9 1110
829a3215 1111 close_commit_graph(the_repository);
08fd81c9
DS
1112 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1113 commit_lock_file(&lk);
1114
f4dbdfc4
DS
1115 free(graph_name);
1116 free(commits.list);
08fd81c9 1117 free(oids.list);
08fd81c9 1118}
283e68c7 1119
41df0e30 1120#define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
283e68c7
DS
1121static int verify_commit_graph_error;
1122
1123static void graph_report(const char *fmt, ...)
1124{
1125 va_list ap;
1126
1127 verify_commit_graph_error = 1;
1128 va_start(ap, fmt);
1129 vfprintf(stderr, fmt, ap);
1130 fprintf(stderr, "\n");
1131 va_end(ap);
1132}
1133
1373e547
DS
1134#define GENERATION_ZERO_EXISTS 1
1135#define GENERATION_NUMBER_EXISTS 2
1136
283e68c7
DS
1137int verify_commit_graph(struct repository *r, struct commit_graph *g)
1138{
9bda8467 1139 uint32_t i, cur_fanout_pos = 0;
41df0e30 1140 struct object_id prev_oid, cur_oid, checksum;
1373e547 1141 int generation_zero = 0;
41df0e30
DS
1142 struct hashfile *f;
1143 int devnull;
1f7f557f 1144 struct progress *progress = NULL;
9bda8467 1145
283e68c7
DS
1146 if (!g) {
1147 graph_report("no commit-graph file loaded");
1148 return 1;
1149 }
1150
2ac138d5 1151 verify_commit_graph_error = verify_commit_graph_lite(g);
9bda8467
DS
1152 if (verify_commit_graph_error)
1153 return verify_commit_graph_error;
1154
41df0e30
DS
1155 devnull = open("/dev/null", O_WRONLY);
1156 f = hashfd(devnull, NULL);
1157 hashwrite(f, g->data, g->data_len - g->hash_len);
1158 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
67947c34 1159 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
41df0e30
DS
1160 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
1161 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
1162 }
1163
9bda8467 1164 for (i = 0; i < g->num_commits; i++) {
2e3c0737
DS
1165 struct commit *graph_commit;
1166
9bda8467
DS
1167 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1168
1169 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
93b4405f 1170 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
9bda8467
DS
1171 oid_to_hex(&prev_oid),
1172 oid_to_hex(&cur_oid));
1173
1174 oidcpy(&prev_oid, &cur_oid);
1175
1176 while (cur_oid.hash[0] > cur_fanout_pos) {
1177 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1178
1179 if (i != fanout_value)
93b4405f 1180 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
9bda8467
DS
1181 cur_fanout_pos, fanout_value, i);
1182 cur_fanout_pos++;
1183 }
2e3c0737 1184
82952964 1185 graph_commit = lookup_commit(r, &cur_oid);
4f542b7a 1186 if (!parse_commit_in_graph_one(r, g, graph_commit))
93b4405f 1187 graph_report(_("failed to parse commit %s from commit-graph"),
2e3c0737 1188 oid_to_hex(&cur_oid));
9bda8467
DS
1189 }
1190
1191 while (cur_fanout_pos < 256) {
1192 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1193
1194 if (g->num_commits != fanout_value)
93b4405f 1195 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
9bda8467
DS
1196 cur_fanout_pos, fanout_value, i);
1197
1198 cur_fanout_pos++;
1199 }
1200
41df0e30 1201 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
96af91d4
DS
1202 return verify_commit_graph_error;
1203
1f7f557f
ÆAB
1204 progress = start_progress(_("Verifying commits in commit graph"),
1205 g->num_commits);
96af91d4 1206 for (i = 0; i < g->num_commits; i++) {
2e3c0737 1207 struct commit *graph_commit, *odb_commit;
53614b13 1208 struct commit_list *graph_parents, *odb_parents;
1373e547 1209 uint32_t max_generation = 0;
96af91d4 1210
1f7f557f 1211 display_progress(progress, i + 1);
96af91d4
DS
1212 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1213
82952964 1214 graph_commit = lookup_commit(r, &cur_oid);
96af91d4
DS
1215 odb_commit = (struct commit *)create_object(r, cur_oid.hash, alloc_commit_node(r));
1216 if (parse_commit_internal(odb_commit, 0, 0)) {
93b4405f 1217 graph_report(_("failed to parse commit %s from object database for commit-graph"),
96af91d4
DS
1218 oid_to_hex(&cur_oid));
1219 continue;
1220 }
2e3c0737 1221
4f542b7a 1222 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2e3c0737 1223 get_commit_tree_oid(odb_commit)))
93b4405f 1224 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2e3c0737
DS
1225 oid_to_hex(&cur_oid),
1226 oid_to_hex(get_commit_tree_oid(graph_commit)),
1227 oid_to_hex(get_commit_tree_oid(odb_commit)));
53614b13
DS
1228
1229 graph_parents = graph_commit->parents;
1230 odb_parents = odb_commit->parents;
1231
1232 while (graph_parents) {
1233 if (odb_parents == NULL) {
93b4405f 1234 graph_report(_("commit-graph parent list for commit %s is too long"),
53614b13
DS
1235 oid_to_hex(&cur_oid));
1236 break;
1237 }
1238
9001dc2a 1239 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
93b4405f 1240 graph_report(_("commit-graph parent for %s is %s != %s"),
53614b13
DS
1241 oid_to_hex(&cur_oid),
1242 oid_to_hex(&graph_parents->item->object.oid),
1243 oid_to_hex(&odb_parents->item->object.oid));
1244
1373e547
DS
1245 if (graph_parents->item->generation > max_generation)
1246 max_generation = graph_parents->item->generation;
1247
53614b13
DS
1248 graph_parents = graph_parents->next;
1249 odb_parents = odb_parents->next;
1250 }
1251
1252 if (odb_parents != NULL)
93b4405f 1253 graph_report(_("commit-graph parent list for commit %s terminates early"),
53614b13 1254 oid_to_hex(&cur_oid));
1373e547
DS
1255
1256 if (!graph_commit->generation) {
1257 if (generation_zero == GENERATION_NUMBER_EXISTS)
93b4405f 1258 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
1373e547
DS
1259 oid_to_hex(&cur_oid));
1260 generation_zero = GENERATION_ZERO_EXISTS;
1261 } else if (generation_zero == GENERATION_ZERO_EXISTS)
93b4405f 1262 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
1373e547
DS
1263 oid_to_hex(&cur_oid));
1264
1265 if (generation_zero == GENERATION_ZERO_EXISTS)
1266 continue;
1267
1268 /*
1269 * If one of our parents has generation GENERATION_NUMBER_MAX, then
1270 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
1271 * extra logic in the following condition.
1272 */
1273 if (max_generation == GENERATION_NUMBER_MAX)
1274 max_generation--;
1275
1276 if (graph_commit->generation != max_generation + 1)
93b4405f 1277 graph_report(_("commit-graph generation for commit %s is %u != %u"),
1373e547
DS
1278 oid_to_hex(&cur_oid),
1279 graph_commit->generation,
1280 max_generation + 1);
88968ebf
DS
1281
1282 if (graph_commit->date != odb_commit->date)
93b4405f 1283 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
88968ebf
DS
1284 oid_to_hex(&cur_oid),
1285 graph_commit->date,
1286 odb_commit->date);
96af91d4 1287 }
1f7f557f 1288 stop_progress(&progress);
96af91d4 1289
283e68c7
DS
1290 return verify_commit_graph_error;
1291}
c3756d5b
JT
1292
1293void free_commit_graph(struct commit_graph *g)
1294{
1295 if (!g)
1296 return;
1297 if (g->graph_fd >= 0) {
1298 munmap((void *)g->data, g->data_len);
1299 g->data = NULL;
1300 close(g->graph_fd);
1301 }
1302 free(g);
1303}