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