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