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