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