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