]> git.ipfire.org Git - thirdparty/git.git/blame - commit-graph.c
commit-graph.c: handle commit parsing errors
[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
JT
470 int config_value;
471
43d35618
ÆAB
472 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD, 0))
473 die("dying as requested by the '%s' variable on commit-graph load!",
474 GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD);
475
dade47c0
JT
476 if (r->objects->commit_graph_attempted)
477 return !!r->objects->commit_graph;
478 r->objects->commit_graph_attempted = 1;
479
859fdc0c
DS
480 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
481 (repo_config_get_bool(r, "core.commitgraph", &config_value) ||
482 !config_value))
dade47c0
JT
483 /*
484 * This repository is not configured to use commit graphs, so
485 * do not load one. (But report commit_graph_attempted anyway
486 * so that commit graph loading is not attempted again for this
487 * repository.)
488 */
5faf357b
JT
489 return 0;
490
d6538246
DS
491 if (!commit_graph_compatible(r))
492 return 0;
493
dade47c0 494 prepare_alt_odb(r);
f0eaf638 495 for (odb = r->objects->odb;
263db403
JK
496 !r->objects->commit_graph && odb;
497 odb = odb->next)
498 prepare_commit_graph_one(r, odb->path);
dade47c0 499 return !!r->objects->commit_graph;
177722b3
DS
500}
501
6cc01743
DS
502int generation_numbers_enabled(struct repository *r)
503{
504 uint32_t first_generation;
505 struct commit_graph *g;
506 if (!prepare_commit_graph(r))
507 return 0;
508
509 g = r->objects->commit_graph;
510
511 if (!g->num_commits)
512 return 0;
513
514 first_generation = get_be32(g->chunk_commit_data +
515 g->hash_len + 8) >> 2;
516
517 return !!first_generation;
518}
519
d4f4d60f
DS
520static void close_commit_graph_one(struct commit_graph *g)
521{
522 if (!g)
523 return;
524
525 close_commit_graph_one(g->base_graph);
526 free_commit_graph(g);
527}
528
c3a3a964 529void close_commit_graph(struct raw_object_store *o)
177722b3 530{
d4f4d60f 531 close_commit_graph_one(o->commit_graph);
c3a3a964 532 o->commit_graph = NULL;
177722b3
DS
533}
534
535static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
536{
537 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
538 g->chunk_oid_lookup, g->hash_len, pos);
539}
540
d4f4d60f
DS
541static void load_oid_from_graph(struct commit_graph *g,
542 uint32_t pos,
543 struct object_id *oid)
544{
545 uint32_t lex_index;
546
547 while (g && pos < g->num_commits_in_base)
548 g = g->base_graph;
549
550 if (!g)
551 BUG("NULL commit-graph");
552
553 if (pos >= g->num_commits + g->num_commits_in_base)
554 die(_("invalid commit position. commit-graph is likely corrupt"));
555
556 lex_index = pos - g->num_commits_in_base;
557
558 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
559}
560
4f542b7a
SB
561static struct commit_list **insert_parent_or_die(struct repository *r,
562 struct commit_graph *g,
d4f4d60f 563 uint32_t pos,
177722b3
DS
564 struct commit_list **pptr)
565{
566 struct commit *c;
567 struct object_id oid;
96af91d4 568
d4f4d60f
DS
569 if (pos >= g->num_commits + g->num_commits_in_base)
570 die("invalid parent position %"PRIu32, pos);
53614b13 571
d4f4d60f 572 load_oid_from_graph(g, pos, &oid);
4f542b7a 573 c = lookup_commit(r, &oid);
177722b3 574 if (!c)
4f5b532d 575 die(_("could not find commit %s"), oid_to_hex(&oid));
177722b3
DS
576 c->graph_pos = pos;
577 return &commit_list_insert(c, pptr)->next;
578}
579
e2838d85
DS
580static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
581{
d4f4d60f
DS
582 const unsigned char *commit_data;
583 uint32_t lex_index;
584
585 while (pos < g->num_commits_in_base)
586 g = g->base_graph;
587
588 lex_index = pos - g->num_commits_in_base;
589 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
e2838d85
DS
590 item->graph_pos = pos;
591 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
592}
593
a133c40b
NTND
594static inline void set_commit_tree(struct commit *c, struct tree *t)
595{
596 c->maybe_tree = t;
597}
598
4f542b7a
SB
599static int fill_commit_in_graph(struct repository *r,
600 struct commit *item,
601 struct commit_graph *g, uint32_t pos)
177722b3 602{
177722b3
DS
603 uint32_t edge_value;
604 uint32_t *parent_data_ptr;
605 uint64_t date_low, date_high;
606 struct commit_list **pptr;
d4f4d60f
DS
607 const unsigned char *commit_data;
608 uint32_t lex_index;
177722b3 609
d4f4d60f
DS
610 while (pos < g->num_commits_in_base)
611 g = g->base_graph;
612
613 if (pos >= g->num_commits + g->num_commits_in_base)
614 die(_("invalid commit position. commit-graph is likely corrupt"));
615
616 /*
617 * Store the "full" position, but then use the
618 * "local" position for the rest of the calculation.
619 */
177722b3 620 item->graph_pos = pos;
d4f4d60f
DS
621 lex_index = pos - g->num_commits_in_base;
622
623 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
624
625 item->object.parsed = 1;
177722b3 626
a133c40b 627 set_commit_tree(item, NULL);
177722b3
DS
628
629 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
630 date_low = get_be32(commit_data + g->hash_len + 12);
631 item->date = (timestamp_t)((date_high << 32) | date_low);
632
83073cc9
DS
633 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
634
177722b3
DS
635 pptr = &item->parents;
636
637 edge_value = get_be32(commit_data + g->hash_len);
638 if (edge_value == GRAPH_PARENT_NONE)
639 return 1;
4f542b7a 640 pptr = insert_parent_or_die(r, g, edge_value, pptr);
177722b3
DS
641
642 edge_value = get_be32(commit_data + g->hash_len + 4);
643 if (edge_value == GRAPH_PARENT_NONE)
644 return 1;
5af7417b 645 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
4f542b7a 646 pptr = insert_parent_or_die(r, g, edge_value, pptr);
177722b3
DS
647 return 1;
648 }
649
5af7417b 650 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
177722b3
DS
651 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
652 do {
653 edge_value = get_be32(parent_data_ptr);
4f542b7a 654 pptr = insert_parent_or_die(r, g,
177722b3
DS
655 edge_value & GRAPH_EDGE_LAST_MASK,
656 pptr);
657 parent_data_ptr++;
658 } while (!(edge_value & GRAPH_LAST_EDGE));
659
660 return 1;
661}
662
e2838d85
DS
663static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
664{
665 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
666 *pos = item->graph_pos;
667 return 1;
668 } else {
d4f4d60f
DS
669 struct commit_graph *cur_g = g;
670 uint32_t lex_index;
671
672 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
673 cur_g = cur_g->base_graph;
674
675 if (cur_g) {
676 *pos = lex_index + cur_g->num_commits_in_base;
677 return 1;
678 }
679
680 return 0;
e2838d85
DS
681 }
682}
683
4f542b7a
SB
684static int parse_commit_in_graph_one(struct repository *r,
685 struct commit_graph *g,
686 struct commit *item)
177722b3 687{
e2838d85
DS
688 uint32_t pos;
689
177722b3
DS
690 if (item->object.parsed)
691 return 1;
ee797053
DS
692
693 if (find_commit_in_graph(item, g, &pos))
4f542b7a 694 return fill_commit_in_graph(r, item, g, pos);
ee797053
DS
695
696 return 0;
697}
698
dade47c0 699int parse_commit_in_graph(struct repository *r, struct commit *item)
ee797053 700{
dade47c0 701 if (!prepare_commit_graph(r))
ee797053 702 return 0;
4f542b7a 703 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
177722b3
DS
704}
705
dade47c0 706void load_commit_graph_info(struct repository *r, struct commit *item)
e2838d85
DS
707{
708 uint32_t pos;
dade47c0 709 if (!prepare_commit_graph(r))
e2838d85 710 return;
dade47c0
JT
711 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
712 fill_commit_graph_info(item, r->objects->commit_graph, pos);
e2838d85
DS
713}
714
4f542b7a
SB
715static struct tree *load_tree_for_commit(struct repository *r,
716 struct commit_graph *g,
717 struct commit *c)
7b8a21db
DS
718{
719 struct object_id oid;
d4f4d60f
DS
720 const unsigned char *commit_data;
721
722 while (c->graph_pos < g->num_commits_in_base)
723 g = g->base_graph;
724
725 commit_data = g->chunk_commit_data +
726 GRAPH_DATA_WIDTH * (c->graph_pos - g->num_commits_in_base);
7b8a21db
DS
727
728 hashcpy(oid.hash, commit_data);
a133c40b 729 set_commit_tree(c, lookup_tree(r, &oid));
7b8a21db
DS
730
731 return c->maybe_tree;
732}
733
4f542b7a
SB
734static struct tree *get_commit_tree_in_graph_one(struct repository *r,
735 struct commit_graph *g,
0cbef8f8 736 const struct commit *c)
7b8a21db
DS
737{
738 if (c->maybe_tree)
739 return c->maybe_tree;
740 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
0cbef8f8
DS
741 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
742
4f542b7a 743 return load_tree_for_commit(r, g, (struct commit *)c);
0cbef8f8 744}
7b8a21db 745
dade47c0 746struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
0cbef8f8 747{
4f542b7a 748 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
7b8a21db
DS
749}
750
c9905bea
DS
751struct packed_commit_list {
752 struct commit **list;
753 int nr;
754 int alloc;
755};
756
757struct packed_oid_list {
758 struct object_id *list;
759 int nr;
760 int alloc;
761};
762
763struct write_commit_graph_context {
764 struct repository *r;
16110c93 765 char *obj_dir;
c9905bea
DS
766 char *graph_name;
767 struct packed_oid_list oids;
768 struct packed_commit_list commits;
769 int num_extra_edges;
770 unsigned long approx_nr_objects;
771 struct progress *progress;
772 int progress_done;
773 uint64_t progress_cnt;
6c622f9f
DS
774
775 char *base_graph_name;
776 int num_commit_graphs_before;
777 int num_commit_graphs_after;
778 char **commit_graph_filenames_before;
779 char **commit_graph_filenames_after;
780 char **commit_graph_hash_after;
781 uint32_t new_num_commits_in_base;
782 struct commit_graph *new_base_graph;
783
c9905bea 784 unsigned append:1,
6c622f9f 785 report_progress:1,
7c5c9b9c
SG
786 split:1,
787 check_oids:1;
c2bc6e6a
DS
788
789 const struct split_commit_graph_opts *split_opts;
c9905bea
DS
790};
791
08fd81c9 792static void write_graph_chunk_fanout(struct hashfile *f,
c9905bea 793 struct write_commit_graph_context *ctx)
08fd81c9
DS
794{
795 int i, count = 0;
c9905bea 796 struct commit **list = ctx->commits.list;
08fd81c9
DS
797
798 /*
799 * Write the first-level table (the list is sorted,
800 * but we use a 256-entry lookup to be able to avoid
801 * having to do eight extra binary search iterations).
802 */
803 for (i = 0; i < 256; i++) {
c9905bea 804 while (count < ctx->commits.nr) {
08fd81c9
DS
805 if ((*list)->object.oid.hash[0] != i)
806 break;
c9905bea 807 display_progress(ctx->progress, ++ctx->progress_cnt);
08fd81c9
DS
808 count++;
809 list++;
810 }
811
812 hashwrite_be32(f, count);
813 }
814}
815
816static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
c9905bea 817 struct write_commit_graph_context *ctx)
08fd81c9 818{
c9905bea 819 struct commit **list = ctx->commits.list;
08fd81c9 820 int count;
c9905bea
DS
821 for (count = 0; count < ctx->commits.nr; count++, list++) {
822 display_progress(ctx->progress, ++ctx->progress_cnt);
08fd81c9 823 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
53035c4f 824 }
08fd81c9
DS
825}
826
827static const unsigned char *commit_to_sha1(size_t index, void *table)
828{
829 struct commit **commits = table;
830 return commits[index]->object.oid.hash;
831}
832
833static void write_graph_chunk_data(struct hashfile *f, int hash_len,
c9905bea 834 struct write_commit_graph_context *ctx)
08fd81c9 835{
c9905bea
DS
836 struct commit **list = ctx->commits.list;
837 struct commit **last = ctx->commits.list + ctx->commits.nr;
08fd81c9
DS
838 uint32_t num_extra_edges = 0;
839
840 while (list < last) {
841 struct commit_list *parent;
842 int edge_value;
843 uint32_t packedDate[2];
c9905bea 844 display_progress(ctx->progress, ++ctx->progress_cnt);
08fd81c9 845
16749b8d
TB
846 if (parse_commit_no_graph(*list))
847 die(_("unable to parse commit %s"),
848 oid_to_hex(&(*list)->object.oid));
2e27bd77 849 hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
08fd81c9
DS
850
851 parent = (*list)->parents;
852
853 if (!parent)
854 edge_value = GRAPH_PARENT_NONE;
855 else {
856 edge_value = sha1_pos(parent->item->object.oid.hash,
c9905bea
DS
857 ctx->commits.list,
858 ctx->commits.nr,
08fd81c9
DS
859 commit_to_sha1);
860
6c622f9f
DS
861 if (edge_value >= 0)
862 edge_value += ctx->new_num_commits_in_base;
863 else {
864 uint32_t pos;
865 if (find_commit_in_graph(parent->item,
866 ctx->new_base_graph,
867 &pos))
868 edge_value = pos;
869 }
870
08fd81c9 871 if (edge_value < 0)
cce99cd8
DS
872 BUG("missing parent %s for commit %s",
873 oid_to_hex(&parent->item->object.oid),
874 oid_to_hex(&(*list)->object.oid));
08fd81c9
DS
875 }
876
877 hashwrite_be32(f, edge_value);
878
879 if (parent)
880 parent = parent->next;
881
882 if (!parent)
883 edge_value = GRAPH_PARENT_NONE;
884 else if (parent->next)
5af7417b 885 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
08fd81c9
DS
886 else {
887 edge_value = sha1_pos(parent->item->object.oid.hash,
c9905bea
DS
888 ctx->commits.list,
889 ctx->commits.nr,
08fd81c9 890 commit_to_sha1);
6c622f9f
DS
891
892 if (edge_value >= 0)
893 edge_value += ctx->new_num_commits_in_base;
894 else {
895 uint32_t pos;
896 if (find_commit_in_graph(parent->item,
897 ctx->new_base_graph,
898 &pos))
899 edge_value = pos;
900 }
901
08fd81c9 902 if (edge_value < 0)
cce99cd8
DS
903 BUG("missing parent %s for commit %s",
904 oid_to_hex(&parent->item->object.oid),
905 oid_to_hex(&(*list)->object.oid));
08fd81c9
DS
906 }
907
908 hashwrite_be32(f, edge_value);
909
5af7417b 910 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
08fd81c9
DS
911 do {
912 num_extra_edges++;
913 parent = parent->next;
914 } while (parent);
915 }
916
917 if (sizeof((*list)->date) > 4)
918 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
919 else
920 packedDate[0] = 0;
921
3258c663
DS
922 packedDate[0] |= htonl((*list)->generation << 2);
923
08fd81c9
DS
924 packedDate[1] = htonl((*list)->date);
925 hashwrite(f, packedDate, 8);
926
927 list++;
928 }
929}
930
5af7417b 931static void write_graph_chunk_extra_edges(struct hashfile *f,
c9905bea 932 struct write_commit_graph_context *ctx)
08fd81c9 933{
c9905bea
DS
934 struct commit **list = ctx->commits.list;
935 struct commit **last = ctx->commits.list + ctx->commits.nr;
08fd81c9
DS
936 struct commit_list *parent;
937
938 while (list < last) {
939 int num_parents = 0;
53035c4f 940
c9905bea 941 display_progress(ctx->progress, ++ctx->progress_cnt);
53035c4f 942
08fd81c9
DS
943 for (parent = (*list)->parents; num_parents < 3 && parent;
944 parent = parent->next)
945 num_parents++;
946
947 if (num_parents <= 2) {
948 list++;
949 continue;
950 }
951
952 /* Since num_parents > 2, this initializer is safe. */
953 for (parent = (*list)->parents->next; parent; parent = parent->next) {
954 int edge_value = sha1_pos(parent->item->object.oid.hash,
c9905bea
DS
955 ctx->commits.list,
956 ctx->commits.nr,
08fd81c9
DS
957 commit_to_sha1);
958
6c622f9f
DS
959 if (edge_value >= 0)
960 edge_value += ctx->new_num_commits_in_base;
961 else {
962 uint32_t pos;
963 if (find_commit_in_graph(parent->item,
964 ctx->new_base_graph,
965 &pos))
966 edge_value = pos;
967 }
968
08fd81c9 969 if (edge_value < 0)
cce99cd8
DS
970 BUG("missing parent %s for commit %s",
971 oid_to_hex(&parent->item->object.oid),
972 oid_to_hex(&(*list)->object.oid));
08fd81c9
DS
973 else if (!parent->next)
974 edge_value |= GRAPH_LAST_EDGE;
975
976 hashwrite_be32(f, edge_value);
977 }
978
979 list++;
980 }
981}
982
3cbc6ed3 983static int oid_compare(const void *_a, const void *_b)
08fd81c9
DS
984{
985 const struct object_id *a = (const struct object_id *)_a;
986 const struct object_id *b = (const struct object_id *)_b;
987 return oidcmp(a, b);
988}
989
08fd81c9
DS
990static int add_packed_commits(const struct object_id *oid,
991 struct packed_git *pack,
992 uint32_t pos,
993 void *data)
994{
c9905bea 995 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
08fd81c9
DS
996 enum object_type type;
997 off_t offset = nth_packed_object_offset(pack, pos);
998 struct object_info oi = OBJECT_INFO_INIT;
999
c9905bea
DS
1000 if (ctx->progress)
1001 display_progress(ctx->progress, ++ctx->progress_done);
7b0f2292 1002
08fd81c9 1003 oi.typep = &type;
c9905bea 1004 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
4f5b532d 1005 die(_("unable to get type of object %s"), oid_to_hex(oid));
08fd81c9
DS
1006
1007 if (type != OBJ_COMMIT)
1008 return 0;
1009
c9905bea
DS
1010 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1011 oidcpy(&(ctx->oids.list[ctx->oids.nr]), oid);
1012 ctx->oids.nr++;
08fd81c9
DS
1013
1014 return 0;
1015}
1016
c9905bea 1017static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
4f2542b4
DS
1018{
1019 struct commit_list *parent;
1020 for (parent = commit->parents; parent; parent = parent->next) {
1021 if (!(parent->item->object.flags & UNINTERESTING)) {
c9905bea
DS
1022 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1023 oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid));
1024 ctx->oids.nr++;
4f2542b4
DS
1025 parent->item->object.flags |= UNINTERESTING;
1026 }
1027 }
1028}
1029
c9905bea 1030static void close_reachable(struct write_commit_graph_context *ctx)
4f2542b4 1031{
49bbc57a 1032 int i;
4f2542b4
DS
1033 struct commit *commit;
1034
c9905bea
DS
1035 if (ctx->report_progress)
1036 ctx->progress = start_delayed_progress(
1037 _("Loading known commits in commit graph"),
1038 ctx->oids.nr);
1039 for (i = 0; i < ctx->oids.nr; i++) {
1040 display_progress(ctx->progress, i + 1);
1041 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
4f2542b4
DS
1042 if (commit)
1043 commit->object.flags |= UNINTERESTING;
1044 }
c9905bea 1045 stop_progress(&ctx->progress);
4f2542b4
DS
1046
1047 /*
c9905bea 1048 * As this loop runs, ctx->oids.nr may grow, but not more
4f2542b4
DS
1049 * than the number of missing commits in the reachable
1050 * closure.
1051 */
c9905bea
DS
1052 if (ctx->report_progress)
1053 ctx->progress = start_delayed_progress(
1054 _("Expanding reachable commits in commit graph"),
1055 ctx->oids.nr);
1056 for (i = 0; i < ctx->oids.nr; i++) {
1057 display_progress(ctx->progress, i + 1);
1058 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
4f2542b4 1059
6c622f9f
DS
1060 if (!commit)
1061 continue;
1062 if (ctx->split) {
1063 if (!parse_commit(commit) &&
1064 commit->graph_pos == COMMIT_NOT_FROM_GRAPH)
1065 add_missing_parents(ctx, commit);
1066 } else if (!parse_commit_no_graph(commit))
c9905bea 1067 add_missing_parents(ctx, commit);
4f2542b4 1068 }
c9905bea 1069 stop_progress(&ctx->progress);
4f2542b4 1070
c9905bea
DS
1071 if (ctx->report_progress)
1072 ctx->progress = start_delayed_progress(
1073 _("Clearing commit marks in commit graph"),
1074 ctx->oids.nr);
1075 for (i = 0; i < ctx->oids.nr; i++) {
1076 display_progress(ctx->progress, i + 1);
1077 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
4f2542b4
DS
1078
1079 if (commit)
1080 commit->object.flags &= ~UNINTERESTING;
1081 }
c9905bea 1082 stop_progress(&ctx->progress);
4f2542b4
DS
1083}
1084
c9905bea 1085static void compute_generation_numbers(struct write_commit_graph_context *ctx)
3258c663
DS
1086{
1087 int i;
1088 struct commit_list *list = NULL;
1089
c9905bea
DS
1090 if (ctx->report_progress)
1091 ctx->progress = start_progress(
1092 _("Computing commit graph generation numbers"),
1093 ctx->commits.nr);
1094 for (i = 0; i < ctx->commits.nr; i++) {
1095 display_progress(ctx->progress, i + 1);
1096 if (ctx->commits.list[i]->generation != GENERATION_NUMBER_INFINITY &&
1097 ctx->commits.list[i]->generation != GENERATION_NUMBER_ZERO)
3258c663
DS
1098 continue;
1099
c9905bea 1100 commit_list_insert(ctx->commits.list[i], &list);
3258c663
DS
1101 while (list) {
1102 struct commit *current = list->item;
1103 struct commit_list *parent;
1104 int all_parents_computed = 1;
1105 uint32_t max_generation = 0;
1106
1107 for (parent = current->parents; parent; parent = parent->next) {
1108 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
1109 parent->item->generation == GENERATION_NUMBER_ZERO) {
1110 all_parents_computed = 0;
1111 commit_list_insert(parent->item, &list);
1112 break;
1113 } else if (parent->item->generation > max_generation) {
1114 max_generation = parent->item->generation;
1115 }
1116 }
1117
1118 if (all_parents_computed) {
1119 current->generation = max_generation + 1;
1120 pop_commit(&list);
1121
1122 if (current->generation > GENERATION_NUMBER_MAX)
1123 current->generation = GENERATION_NUMBER_MAX;
1124 }
1125 }
1126 }
c9905bea 1127 stop_progress(&ctx->progress);
3258c663
DS
1128}
1129
59fb8770
DS
1130static int add_ref_to_list(const char *refname,
1131 const struct object_id *oid,
1132 int flags, void *cb_data)
1133{
1134 struct string_list *list = (struct string_list *)cb_data;
1135
1136 string_list_append(list, oid_to_hex(oid));
1137 return 0;
1138}
1139
39d88318
SG
1140int write_commit_graph_reachable(const char *obj_dir,
1141 enum commit_graph_write_flags flags,
c2bc6e6a 1142 const struct split_commit_graph_opts *split_opts)
59fb8770 1143{
f4dbdfc4 1144 struct string_list list = STRING_LIST_INIT_DUP;
e103f727 1145 int result;
59fb8770 1146
59fb8770 1147 for_each_ref(add_ref_to_list, &list);
e103f727 1148 result = write_commit_graph(obj_dir, NULL, &list,
c2bc6e6a 1149 flags, split_opts);
f4dbdfc4
DS
1150
1151 string_list_clear(&list, 0);
e103f727 1152 return result;
59fb8770
DS
1153}
1154
ef5b83f2
DS
1155static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1156 struct string_list *pack_indexes)
08fd81c9 1157{
ef5b83f2 1158 uint32_t i;
28944739 1159 struct strbuf progress_title = STRBUF_INIT;
ef5b83f2
DS
1160 struct strbuf packname = STRBUF_INIT;
1161 int dirlen;
08fd81c9 1162
ef5b83f2
DS
1163 strbuf_addf(&packname, "%s/pack/", ctx->obj_dir);
1164 dirlen = packname.len;
1165 if (ctx->report_progress) {
1166 strbuf_addf(&progress_title,
1167 Q_("Finding commits for commit graph in %d pack",
1168 "Finding commits for commit graph in %d packs",
1169 pack_indexes->nr),
1170 pack_indexes->nr);
1171 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1172 ctx->progress_done = 0;
7547b95b 1173 }
ef5b83f2
DS
1174 for (i = 0; i < pack_indexes->nr; i++) {
1175 struct packed_git *p;
1176 strbuf_setlen(&packname, dirlen);
1177 strbuf_addstr(&packname, pack_indexes->items[i].string);
1178 p = add_packed_git(packname.buf, packname.len, 1);
1179 if (!p) {
1180 error(_("error adding pack %s"), packname.buf);
1181 return -1;
7547b95b 1182 }
ef5b83f2
DS
1183 if (open_pack_index(p)) {
1184 error(_("error opening index for %s"), packname.buf);
1185 return -1;
1186 }
1187 for_each_object_in_pack(p, add_packed_commits, ctx,
1188 FOR_EACH_OBJECT_PACK_ORDER);
1189 close_pack(p);
1190 free(p);
7547b95b
DS
1191 }
1192
ef5b83f2 1193 stop_progress(&ctx->progress);
0aa6bce7 1194 strbuf_release(&progress_title);
ef5b83f2
DS
1195 strbuf_release(&packname);
1196
1197 return 0;
1198}
1199
7c5c9b9c
SG
1200static int fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
1201 struct string_list *commit_hex)
4c9efe85
DS
1202{
1203 uint32_t i;
1204 struct strbuf progress_title = STRBUF_INIT;
1205
1206 if (ctx->report_progress) {
1207 strbuf_addf(&progress_title,
1208 Q_("Finding commits for commit graph from %d ref",
1209 "Finding commits for commit graph from %d refs",
1210 commit_hex->nr),
1211 commit_hex->nr);
1212 ctx->progress = start_delayed_progress(
1213 progress_title.buf,
1214 commit_hex->nr);
3d5df01b 1215 }
4c9efe85
DS
1216 for (i = 0; i < commit_hex->nr; i++) {
1217 const char *end;
1218 struct object_id oid;
1219 struct commit *result;
1220
1221 display_progress(ctx->progress, i + 1);
7c5c9b9c
SG
1222 if (!parse_oid_hex(commit_hex->items[i].string, &oid, &end) &&
1223 (result = lookup_commit_reference_gently(ctx->r, &oid, 1))) {
4c9efe85
DS
1224 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1225 oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid));
1226 ctx->oids.nr++;
7c5c9b9c
SG
1227 } else if (ctx->check_oids) {
1228 error(_("invalid commit object id: %s"),
1229 commit_hex->items[i].string);
1230 return -1;
3d5df01b
DS
1231 }
1232 }
4c9efe85
DS
1233 stop_progress(&ctx->progress);
1234 strbuf_release(&progress_title);
7c5c9b9c
SG
1235
1236 return 0;
4c9efe85 1237}
3d5df01b 1238
b2c83060
DS
1239static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1240{
1241 if (ctx->report_progress)
1242 ctx->progress = start_delayed_progress(
1243 _("Finding commits for commit graph among packed objects"),
1244 ctx->approx_nr_objects);
1245 for_each_packed_object(add_packed_commits, ctx,
1246 FOR_EACH_OBJECT_PACK_ORDER);
1247 if (ctx->progress_done < ctx->approx_nr_objects)
1248 display_progress(ctx->progress, ctx->approx_nr_objects);
1249 stop_progress(&ctx->progress);
1250}
049d51a2 1251
014e3440
DS
1252static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
1253{
1254 uint32_t i, count_distinct = 1;
08fd81c9 1255
014e3440
DS
1256 if (ctx->report_progress)
1257 ctx->progress = start_delayed_progress(
890226cc 1258 _("Counting distinct commits in commit graph"),
014e3440
DS
1259 ctx->oids.nr);
1260 display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
3cbc6ed3 1261 QSORT(ctx->oids.list, ctx->oids.nr, oid_compare);
014e3440
DS
1262
1263 for (i = 1; i < ctx->oids.nr; i++) {
1264 display_progress(ctx->progress, i + 1);
6c622f9f
DS
1265 if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i])) {
1266 if (ctx->split) {
1267 struct commit *c = lookup_commit(ctx->r, &ctx->oids.list[i]);
1268
1269 if (!c || c->graph_pos != COMMIT_NOT_FROM_GRAPH)
1270 continue;
1271 }
1272
08fd81c9 1273 count_distinct++;
6c622f9f 1274 }
08fd81c9 1275 }
014e3440 1276 stop_progress(&ctx->progress);
08fd81c9 1277
014e3440
DS
1278 return count_distinct;
1279}
08fd81c9 1280
f998d542
DS
1281static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1282{
1283 uint32_t i;
1284 struct commit_list *parent;
08fd81c9 1285
f998d542
DS
1286 ctx->num_extra_edges = 0;
1287 if (ctx->report_progress)
1288 ctx->progress = start_delayed_progress(
890226cc 1289 _("Finding extra edges in commit graph"),
f998d542
DS
1290 ctx->oids.nr);
1291 for (i = 0; i < ctx->oids.nr; i++) {
08fd81c9 1292 int num_parents = 0;
f998d542
DS
1293 display_progress(ctx->progress, i + 1);
1294 if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
08fd81c9
DS
1295 continue;
1296
6c622f9f 1297 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
f998d542 1298 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.list[i]);
6c622f9f
DS
1299
1300 if (ctx->split &&
1301 ctx->commits.list[ctx->commits.nr]->graph_pos != COMMIT_NOT_FROM_GRAPH)
1302 continue;
1303
f998d542 1304 parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
08fd81c9 1305
f998d542 1306 for (parent = ctx->commits.list[ctx->commits.nr]->parents;
08fd81c9
DS
1307 parent; parent = parent->next)
1308 num_parents++;
1309
1310 if (num_parents > 2)
f998d542 1311 ctx->num_extra_edges += num_parents - 1;
08fd81c9 1312
f998d542 1313 ctx->commits.nr++;
08fd81c9 1314 }
f998d542
DS
1315 stop_progress(&ctx->progress);
1316}
08fd81c9 1317
6c622f9f
DS
1318static int write_graph_chunk_base_1(struct hashfile *f,
1319 struct commit_graph *g)
1320{
1321 int num = 0;
1322
1323 if (!g)
1324 return 0;
1325
1326 num = write_graph_chunk_base_1(f, g->base_graph);
1327 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1328 return num + 1;
1329}
1330
1331static int write_graph_chunk_base(struct hashfile *f,
1332 struct write_commit_graph_context *ctx)
1333{
1334 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1335
1336 if (num != ctx->num_commit_graphs_after - 1) {
1337 error(_("failed to write correct number of base graph ids"));
1338 return -1;
1339 }
1340
1341 return 0;
1342}
1343
238def57 1344static int write_commit_graph_file(struct write_commit_graph_context *ctx)
08fd81c9 1345{
238def57 1346 uint32_t i;
6c622f9f 1347 int fd;
08fd81c9 1348 struct hashfile *f;
08fd81c9 1349 struct lock_file lk = LOCK_INIT;
6c622f9f
DS
1350 uint32_t chunk_ids[6];
1351 uint64_t chunk_offsets[6];
c1665998 1352 const unsigned hashsz = the_hash_algo->rawsz;
28944739 1353 struct strbuf progress_title = STRBUF_INIT;
144354b0 1354 int num_chunks = 3;
6c622f9f
DS
1355 struct object_id file_hash;
1356
1357 if (ctx->split) {
1358 struct strbuf tmp_file = STRBUF_INIT;
1359
1360 strbuf_addf(&tmp_file,
1361 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1362 ctx->obj_dir);
1363 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1364 } else {
1365 ctx->graph_name = get_commit_graph_filename(ctx->obj_dir);
1366 }
238def57 1367
238def57
DS
1368 if (safe_create_leading_directories(ctx->graph_name)) {
1369 UNLEAK(ctx->graph_name);
1370 error(_("unable to create leading directories of %s"),
1371 ctx->graph_name);
1372 return -1;
f4dbdfc4 1373 }
08fd81c9 1374
6c622f9f
DS
1375 if (ctx->split) {
1376 char *lock_name = get_chain_filename(ctx->obj_dir);
08fd81c9 1377
6c622f9f 1378 hold_lock_file_for_update(&lk, lock_name, LOCK_DIE_ON_ERROR);
08fd81c9 1379
6c622f9f
DS
1380 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1381 if (fd < 0) {
1382 error(_("unable to create '%s'"), ctx->graph_name);
1383 return -1;
1384 }
1385
1386 f = hashfd(fd, ctx->graph_name);
1387 } else {
1388 hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR);
1389 fd = lk.tempfile->fd;
1390 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1391 }
08fd81c9
DS
1392
1393 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
1394 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
1395 chunk_ids[2] = GRAPH_CHUNKID_DATA;
144354b0
DS
1396 if (ctx->num_extra_edges) {
1397 chunk_ids[num_chunks] = GRAPH_CHUNKID_EXTRAEDGES;
1398 num_chunks++;
1399 }
6c622f9f
DS
1400 if (ctx->num_commit_graphs_after > 1) {
1401 chunk_ids[num_chunks] = GRAPH_CHUNKID_BASE;
1402 num_chunks++;
1403 }
144354b0
DS
1404
1405 chunk_ids[num_chunks] = 0;
08fd81c9
DS
1406
1407 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1408 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
238def57
DS
1409 chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
1410 chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
144354b0
DS
1411
1412 num_chunks = 3;
1413 if (ctx->num_extra_edges) {
1414 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1415 4 * ctx->num_extra_edges;
1416 num_chunks++;
1417 }
6c622f9f
DS
1418 if (ctx->num_commit_graphs_after > 1) {
1419 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1420 hashsz * (ctx->num_commit_graphs_after - 1);
1421 num_chunks++;
1422 }
144354b0
DS
1423
1424 hashwrite_be32(f, GRAPH_SIGNATURE);
1425
1426 hashwrite_u8(f, GRAPH_VERSION);
1427 hashwrite_u8(f, oid_version());
1428 hashwrite_u8(f, num_chunks);
6c622f9f 1429 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
08fd81c9
DS
1430
1431 for (i = 0; i <= num_chunks; i++) {
1432 uint32_t chunk_write[3];
1433
1434 chunk_write[0] = htonl(chunk_ids[i]);
1435 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
1436 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
1437 hashwrite(f, chunk_write, 12);
1438 }
1439
238def57 1440 if (ctx->report_progress) {
28944739
ÆAB
1441 strbuf_addf(&progress_title,
1442 Q_("Writing out commit graph in %d pass",
1443 "Writing out commit graph in %d passes",
1444 num_chunks),
1445 num_chunks);
238def57 1446 ctx->progress = start_delayed_progress(
28944739 1447 progress_title.buf,
238def57 1448 num_chunks * ctx->commits.nr);
28944739 1449 }
238def57
DS
1450 write_graph_chunk_fanout(f, ctx);
1451 write_graph_chunk_oids(f, hashsz, ctx);
1452 write_graph_chunk_data(f, hashsz, ctx);
1453 if (ctx->num_extra_edges)
1454 write_graph_chunk_extra_edges(f, ctx);
6c622f9f
DS
1455 if (ctx->num_commit_graphs_after > 1 &&
1456 write_graph_chunk_base(f, ctx)) {
1457 return -1;
1458 }
238def57 1459 stop_progress(&ctx->progress);
28944739 1460 strbuf_release(&progress_title);
08fd81c9 1461
6c622f9f
DS
1462 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1463 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
c523035c 1464 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->obj_dir, new_base_hash);
6c622f9f
DS
1465
1466 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1467 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1468 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1469 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1470 }
1471
c3a3a964 1472 close_commit_graph(ctx->r->objects);
6c622f9f
DS
1473 finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1474
1475 if (ctx->split) {
1476 FILE *chainf = fdopen_lock_file(&lk, "w");
1477 char *final_graph_name;
1478 int result;
1479
1480 close(fd);
1481
1482 if (!chainf) {
1483 error(_("unable to open commit-graph chain file"));
1484 return -1;
1485 }
1486
1487 if (ctx->base_graph_name) {
135a7123
DS
1488 const char *dest = ctx->commit_graph_filenames_after[
1489 ctx->num_commit_graphs_after - 2];
6c622f9f 1490
135a7123
DS
1491 if (strcmp(ctx->base_graph_name, dest)) {
1492 result = rename(ctx->base_graph_name, dest);
1493
1494 if (result) {
1495 error(_("failed to rename base commit-graph file"));
1496 return -1;
1497 }
6c622f9f
DS
1498 }
1499 } else {
1500 char *graph_name = get_commit_graph_filename(ctx->obj_dir);
1501 unlink(graph_name);
1502 }
1503
1504 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
1505 final_graph_name = get_split_graph_filename(ctx->obj_dir,
1506 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1507 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1508
1509 result = rename(ctx->graph_name, final_graph_name);
1510
1511 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1512 fprintf(lk.tempfile->fp, "%s\n", ctx->commit_graph_hash_after[i]);
1513
1514 if (result) {
1515 error(_("failed to rename temporary commit-graph file"));
1516 return -1;
1517 }
1518 }
1519
08fd81c9
DS
1520 commit_lock_file(&lk);
1521
238def57
DS
1522 return 0;
1523}
1524
1771be90
DS
1525static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
1526{
1527 struct commit_graph *g = ctx->r->objects->commit_graph;
1528 uint32_t num_commits = ctx->commits.nr;
1529 uint32_t i;
1530
c2bc6e6a
DS
1531 int max_commits = 0;
1532 int size_mult = 2;
1533
1534 if (ctx->split_opts) {
1535 max_commits = ctx->split_opts->max_commits;
1536 size_mult = ctx->split_opts->size_multiple;
1537 }
1538
1771be90
DS
1539 g = ctx->r->objects->commit_graph;
1540 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
1541
c2bc6e6a
DS
1542 while (g && (g->num_commits <= size_mult * num_commits ||
1543 (max_commits && num_commits > max_commits))) {
c523035c
DS
1544 if (strcmp(g->obj_dir, ctx->obj_dir))
1545 break;
1546
1771be90
DS
1547 num_commits += g->num_commits;
1548 g = g->base_graph;
1549
1550 ctx->num_commit_graphs_after--;
1551 }
1552
1553 ctx->new_base_graph = g;
1554
c523035c
DS
1555 if (ctx->num_commit_graphs_after == 2) {
1556 char *old_graph_name = get_commit_graph_filename(g->obj_dir);
1557
1558 if (!strcmp(g->filename, old_graph_name) &&
1559 strcmp(g->obj_dir, ctx->obj_dir)) {
1560 ctx->num_commit_graphs_after = 1;
1561 ctx->new_base_graph = NULL;
1562 }
1563
1564 free(old_graph_name);
1565 }
1566
1771be90
DS
1567 ALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
1568 ALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
1569
1570 for (i = 0; i < ctx->num_commit_graphs_after &&
1571 i < ctx->num_commit_graphs_before; i++)
1572 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
1573
1574 i = ctx->num_commit_graphs_before - 1;
1575 g = ctx->r->objects->commit_graph;
1576
1577 while (g) {
1578 if (i < ctx->num_commit_graphs_after)
1579 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
1580
1581 i--;
1582 g = g->base_graph;
1583 }
1584}
1585
1586static void merge_commit_graph(struct write_commit_graph_context *ctx,
1587 struct commit_graph *g)
1588{
1589 uint32_t i;
1590 uint32_t offset = g->num_commits_in_base;
1591
1592 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
1593
1594 for (i = 0; i < g->num_commits; i++) {
1595 struct object_id oid;
1596 struct commit *result;
1597
1598 display_progress(ctx->progress, i + 1);
1599
1600 load_oid_from_graph(g, i + offset, &oid);
1601
1602 /* only add commits if they still exist in the repo */
1603 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
1604
1605 if (result) {
1606 ctx->commits.list[ctx->commits.nr] = result;
1607 ctx->commits.nr++;
1608 }
1609 }
1610}
1611
1612static int commit_compare(const void *_a, const void *_b)
1613{
1614 const struct commit *a = *(const struct commit **)_a;
1615 const struct commit *b = *(const struct commit **)_b;
1616 return oidcmp(&a->object.oid, &b->object.oid);
1617}
1618
1619static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
1620{
1621 uint32_t i, num_parents;
1622 struct commit_list *parent;
1623
1624 if (ctx->report_progress)
1625 ctx->progress = start_delayed_progress(
1626 _("Scanning merged commits"),
1627 ctx->commits.nr);
1628
1629 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
1630
1631 ctx->num_extra_edges = 0;
1632 for (i = 0; i < ctx->commits.nr; i++) {
1633 display_progress(ctx->progress, i);
1634
1635 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
1636 &ctx->commits.list[i]->object.oid)) {
1637 die(_("unexpected duplicate commit id %s"),
1638 oid_to_hex(&ctx->commits.list[i]->object.oid));
1639 } else {
1640 num_parents = 0;
1641 for (parent = ctx->commits.list[i]->parents; parent; parent = parent->next)
1642 num_parents++;
1643
1644 if (num_parents > 2)
a35bea40 1645 ctx->num_extra_edges += num_parents - 1;
1771be90
DS
1646 }
1647 }
1648
1649 stop_progress(&ctx->progress);
1650}
1651
1652static void merge_commit_graphs(struct write_commit_graph_context *ctx)
1653{
1654 struct commit_graph *g = ctx->r->objects->commit_graph;
1655 uint32_t current_graph_number = ctx->num_commit_graphs_before;
1656 struct strbuf progress_title = STRBUF_INIT;
1657
1658 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
1659 current_graph_number--;
1660
1661 if (ctx->report_progress) {
1662 strbuf_addstr(&progress_title, _("Merging commit-graph"));
1663 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1664 }
1665
1666 merge_commit_graph(ctx, g);
1667 stop_progress(&ctx->progress);
1668 strbuf_release(&progress_title);
1669
1670 g = g->base_graph;
1671 }
1672
1673 if (g) {
1674 ctx->new_base_graph = g;
1675 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
1676 }
1677
1678 if (ctx->new_base_graph)
1679 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
1680
1681 sort_and_scan_merged_commits(ctx);
1682}
1683
8d84097f
DS
1684static void mark_commit_graphs(struct write_commit_graph_context *ctx)
1685{
1686 uint32_t i;
1687 time_t now = time(NULL);
1688
1689 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
1690 struct stat st;
1691 struct utimbuf updated_time;
1692
1693 stat(ctx->commit_graph_filenames_before[i], &st);
1694
1695 updated_time.actime = st.st_atime;
1696 updated_time.modtime = now;
1697 utime(ctx->commit_graph_filenames_before[i], &updated_time);
1698 }
1699}
1700
1701static void expire_commit_graphs(struct write_commit_graph_context *ctx)
1702{
1703 struct strbuf path = STRBUF_INIT;
1704 DIR *dir;
1705 struct dirent *de;
1706 size_t dirnamelen;
c2bc6e6a
DS
1707 timestamp_t expire_time = time(NULL);
1708
1709 if (ctx->split_opts && ctx->split_opts->expire_time)
1710 expire_time -= ctx->split_opts->expire_time;
ba41112a
DS
1711 if (!ctx->split) {
1712 char *chain_file_name = get_chain_filename(ctx->obj_dir);
1713 unlink(chain_file_name);
1714 free(chain_file_name);
1715 ctx->num_commit_graphs_after = 0;
1716 }
8d84097f
DS
1717
1718 strbuf_addstr(&path, ctx->obj_dir);
1719 strbuf_addstr(&path, "/info/commit-graphs");
1720 dir = opendir(path.buf);
1721
0aa6bce7
RS
1722 if (!dir)
1723 goto out;
8d84097f
DS
1724
1725 strbuf_addch(&path, '/');
1726 dirnamelen = path.len;
1727 while ((de = readdir(dir)) != NULL) {
1728 struct stat st;
1729 uint32_t i, found = 0;
1730
1731 strbuf_setlen(&path, dirnamelen);
1732 strbuf_addstr(&path, de->d_name);
1733
1734 stat(path.buf, &st);
1735
1736 if (st.st_mtime > expire_time)
1737 continue;
1738 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
1739 continue;
1740
1741 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
1742 if (!strcmp(ctx->commit_graph_filenames_after[i],
1743 path.buf)) {
1744 found = 1;
1745 break;
1746 }
1747 }
1748
1749 if (!found)
1750 unlink(path.buf);
8d84097f 1751 }
0aa6bce7
RS
1752
1753out:
1754 strbuf_release(&path);
8d84097f
DS
1755}
1756
238def57
DS
1757int write_commit_graph(const char *obj_dir,
1758 struct string_list *pack_indexes,
1759 struct string_list *commit_hex,
39d88318 1760 enum commit_graph_write_flags flags,
c2bc6e6a 1761 const struct split_commit_graph_opts *split_opts)
238def57
DS
1762{
1763 struct write_commit_graph_context *ctx;
1764 uint32_t i, count_distinct = 0;
16110c93 1765 size_t len;
e103f727 1766 int res = 0;
08fd81c9 1767
d6538246 1768 if (!commit_graph_compatible(the_repository))
e103f727 1769 return 0;
d6538246 1770
c9905bea
DS
1771 ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
1772 ctx->r = the_repository;
16110c93
DS
1773
1774 /* normalize object dir with no trailing slash */
1775 ctx->obj_dir = xmallocz(strlen(obj_dir) + 1);
1776 normalize_path_copy(ctx->obj_dir, obj_dir);
1777 len = strlen(ctx->obj_dir);
1778 if (len && ctx->obj_dir[len - 1] == '/')
1779 ctx->obj_dir[len - 1] = 0;
1780
39d88318
SG
1781 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
1782 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
1783 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
7c5c9b9c 1784 ctx->check_oids = flags & COMMIT_GRAPH_WRITE_CHECK_OIDS ? 1 : 0;
c2bc6e6a 1785 ctx->split_opts = split_opts;
6c622f9f
DS
1786
1787 if (ctx->split) {
1788 struct commit_graph *g;
1789 prepare_commit_graph(ctx->r);
1790
1791 g = ctx->r->objects->commit_graph;
1792
1793 while (g) {
1794 ctx->num_commit_graphs_before++;
1795 g = g->base_graph;
1796 }
1797
1798 if (ctx->num_commit_graphs_before) {
1799 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
1800 i = ctx->num_commit_graphs_before;
1801 g = ctx->r->objects->commit_graph;
1802
1803 while (g) {
1804 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
1805 g = g->base_graph;
1806 }
1807 }
1808 }
c9905bea
DS
1809
1810 ctx->approx_nr_objects = approximate_object_count();
1811 ctx->oids.alloc = ctx->approx_nr_objects / 32;
1812
c2bc6e6a
DS
1813 if (ctx->split && split_opts && ctx->oids.alloc > split_opts->max_commits)
1814 ctx->oids.alloc = split_opts->max_commits;
1771be90 1815
c9905bea
DS
1816 if (ctx->append) {
1817 prepare_commit_graph_one(ctx->r, ctx->obj_dir);
1818 if (ctx->r->objects->commit_graph)
1819 ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
7547b95b
DS
1820 }
1821
c9905bea
DS
1822 if (ctx->oids.alloc < 1024)
1823 ctx->oids.alloc = 1024;
1824 ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc);
1825
1826 if (ctx->append && ctx->r->objects->commit_graph) {
1827 struct commit_graph *g = ctx->r->objects->commit_graph;
1828 for (i = 0; i < g->num_commits; i++) {
1829 const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i;
1830 hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash);
7547b95b
DS
1831 }
1832 }
1833
049d51a2 1834 if (pack_indexes) {
ef5b83f2
DS
1835 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
1836 goto cleanup;
3d5df01b
DS
1837 }
1838
7c5c9b9c
SG
1839 if (commit_hex) {
1840 if ((res = fill_oids_from_commit_hex(ctx, commit_hex)))
1841 goto cleanup;
1842 }
3d5df01b 1843
b2c83060
DS
1844 if (!pack_indexes && !commit_hex)
1845 fill_oids_from_all_packs(ctx);
049d51a2 1846
c9905bea 1847 close_reachable(ctx);
08fd81c9 1848
014e3440 1849 count_distinct = count_distinct_commits(ctx);
08fd81c9 1850
e103f727
DS
1851 if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
1852 error(_("the commit graph format cannot write %d commits"), count_distinct);
1853 res = -1;
1854 goto cleanup;
1855 }
08fd81c9 1856
c9905bea
DS
1857 ctx->commits.alloc = count_distinct;
1858 ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc);
08fd81c9 1859
f998d542 1860 copy_oids_to_commits(ctx);
08fd81c9 1861
c9905bea 1862 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
e103f727
DS
1863 error(_("too many commits to write graph"));
1864 res = -1;
1865 goto cleanup;
1866 }
08fd81c9 1867
6c622f9f
DS
1868 if (!ctx->commits.nr)
1869 goto cleanup;
1870
1771be90
DS
1871 if (ctx->split) {
1872 split_graph_merge_strategy(ctx);
1873
1874 merge_commit_graphs(ctx);
1875 } else
6c622f9f
DS
1876 ctx->num_commit_graphs_after = 1;
1877
c9905bea 1878 compute_generation_numbers(ctx);
08fd81c9 1879
238def57 1880 res = write_commit_graph_file(ctx);
08fd81c9 1881
ba41112a 1882 if (ctx->split)
8d84097f 1883 mark_commit_graphs(ctx);
ba41112a
DS
1884
1885 expire_commit_graphs(ctx);
8d84097f 1886
e103f727 1887cleanup:
238def57 1888 free(ctx->graph_name);
c9905bea
DS
1889 free(ctx->commits.list);
1890 free(ctx->oids.list);
16110c93 1891 free(ctx->obj_dir);
6c622f9f
DS
1892
1893 if (ctx->commit_graph_filenames_after) {
1894 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
1895 free(ctx->commit_graph_filenames_after[i]);
1896 free(ctx->commit_graph_hash_after[i]);
1897 }
1898
1899 for (i = 0; i < ctx->num_commit_graphs_before; i++)
1900 free(ctx->commit_graph_filenames_before[i]);
1901
1902 free(ctx->commit_graph_filenames_after);
1903 free(ctx->commit_graph_filenames_before);
1904 free(ctx->commit_graph_hash_after);
1905 }
1906
c9905bea 1907 free(ctx);
e103f727
DS
1908
1909 return res;
08fd81c9 1910}
283e68c7 1911
41df0e30 1912#define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
283e68c7
DS
1913static int verify_commit_graph_error;
1914
1915static void graph_report(const char *fmt, ...)
1916{
1917 va_list ap;
1918
1919 verify_commit_graph_error = 1;
1920 va_start(ap, fmt);
1921 vfprintf(stderr, fmt, ap);
1922 fprintf(stderr, "\n");
1923 va_end(ap);
1924}
1925
1373e547
DS
1926#define GENERATION_ZERO_EXISTS 1
1927#define GENERATION_NUMBER_EXISTS 2
1928
3da4b609 1929int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
283e68c7 1930{
9bda8467 1931 uint32_t i, cur_fanout_pos = 0;
41df0e30 1932 struct object_id prev_oid, cur_oid, checksum;
1373e547 1933 int generation_zero = 0;
41df0e30
DS
1934 struct hashfile *f;
1935 int devnull;
1f7f557f 1936 struct progress *progress = NULL;
3da4b609 1937 int local_error = 0;
9bda8467 1938
283e68c7
DS
1939 if (!g) {
1940 graph_report("no commit-graph file loaded");
1941 return 1;
1942 }
1943
2ac138d5 1944 verify_commit_graph_error = verify_commit_graph_lite(g);
9bda8467
DS
1945 if (verify_commit_graph_error)
1946 return verify_commit_graph_error;
1947
41df0e30
DS
1948 devnull = open("/dev/null", O_WRONLY);
1949 f = hashfd(devnull, NULL);
1950 hashwrite(f, g->data, g->data_len - g->hash_len);
1951 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
67947c34 1952 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
41df0e30
DS
1953 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
1954 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
1955 }
1956
9bda8467 1957 for (i = 0; i < g->num_commits; i++) {
2e3c0737
DS
1958 struct commit *graph_commit;
1959
9bda8467
DS
1960 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1961
1962 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
93b4405f 1963 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
9bda8467
DS
1964 oid_to_hex(&prev_oid),
1965 oid_to_hex(&cur_oid));
1966
1967 oidcpy(&prev_oid, &cur_oid);
1968
1969 while (cur_oid.hash[0] > cur_fanout_pos) {
1970 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1971
1972 if (i != fanout_value)
93b4405f 1973 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
9bda8467
DS
1974 cur_fanout_pos, fanout_value, i);
1975 cur_fanout_pos++;
1976 }
2e3c0737 1977
82952964 1978 graph_commit = lookup_commit(r, &cur_oid);
4f542b7a 1979 if (!parse_commit_in_graph_one(r, g, graph_commit))
93b4405f 1980 graph_report(_("failed to parse commit %s from commit-graph"),
2e3c0737 1981 oid_to_hex(&cur_oid));
9bda8467
DS
1982 }
1983
1984 while (cur_fanout_pos < 256) {
1985 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1986
1987 if (g->num_commits != fanout_value)
93b4405f 1988 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
9bda8467
DS
1989 cur_fanout_pos, fanout_value, i);
1990
1991 cur_fanout_pos++;
1992 }
1993
41df0e30 1994 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
96af91d4
DS
1995 return verify_commit_graph_error;
1996
1f7f557f
ÆAB
1997 progress = start_progress(_("Verifying commits in commit graph"),
1998 g->num_commits);
96af91d4 1999 for (i = 0; i < g->num_commits; i++) {
2e3c0737 2000 struct commit *graph_commit, *odb_commit;
53614b13 2001 struct commit_list *graph_parents, *odb_parents;
1373e547 2002 uint32_t max_generation = 0;
96af91d4 2003
1f7f557f 2004 display_progress(progress, i + 1);
96af91d4
DS
2005 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2006
82952964 2007 graph_commit = lookup_commit(r, &cur_oid);
a378509e 2008 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
96af91d4 2009 if (parse_commit_internal(odb_commit, 0, 0)) {
93b4405f 2010 graph_report(_("failed to parse commit %s from object database for commit-graph"),
96af91d4
DS
2011 oid_to_hex(&cur_oid));
2012 continue;
2013 }
2e3c0737 2014
4f542b7a 2015 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2e3c0737 2016 get_commit_tree_oid(odb_commit)))
93b4405f 2017 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2e3c0737
DS
2018 oid_to_hex(&cur_oid),
2019 oid_to_hex(get_commit_tree_oid(graph_commit)),
2020 oid_to_hex(get_commit_tree_oid(odb_commit)));
53614b13
DS
2021
2022 graph_parents = graph_commit->parents;
2023 odb_parents = odb_commit->parents;
2024
2025 while (graph_parents) {
2026 if (odb_parents == NULL) {
93b4405f 2027 graph_report(_("commit-graph parent list for commit %s is too long"),
53614b13
DS
2028 oid_to_hex(&cur_oid));
2029 break;
2030 }
2031
3da4b609
DS
2032 /* parse parent in case it is in a base graph */
2033 parse_commit_in_graph_one(r, g, graph_parents->item);
2034
9001dc2a 2035 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
93b4405f 2036 graph_report(_("commit-graph parent for %s is %s != %s"),
53614b13
DS
2037 oid_to_hex(&cur_oid),
2038 oid_to_hex(&graph_parents->item->object.oid),
2039 oid_to_hex(&odb_parents->item->object.oid));
2040
1373e547
DS
2041 if (graph_parents->item->generation > max_generation)
2042 max_generation = graph_parents->item->generation;
2043
53614b13
DS
2044 graph_parents = graph_parents->next;
2045 odb_parents = odb_parents->next;
2046 }
2047
2048 if (odb_parents != NULL)
93b4405f 2049 graph_report(_("commit-graph parent list for commit %s terminates early"),
53614b13 2050 oid_to_hex(&cur_oid));
1373e547
DS
2051
2052 if (!graph_commit->generation) {
2053 if (generation_zero == GENERATION_NUMBER_EXISTS)
93b4405f 2054 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
1373e547
DS
2055 oid_to_hex(&cur_oid));
2056 generation_zero = GENERATION_ZERO_EXISTS;
2057 } else if (generation_zero == GENERATION_ZERO_EXISTS)
93b4405f 2058 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
1373e547
DS
2059 oid_to_hex(&cur_oid));
2060
2061 if (generation_zero == GENERATION_ZERO_EXISTS)
2062 continue;
2063
2064 /*
2065 * If one of our parents has generation GENERATION_NUMBER_MAX, then
2066 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
2067 * extra logic in the following condition.
2068 */
2069 if (max_generation == GENERATION_NUMBER_MAX)
2070 max_generation--;
2071
2072 if (graph_commit->generation != max_generation + 1)
93b4405f 2073 graph_report(_("commit-graph generation for commit %s is %u != %u"),
1373e547
DS
2074 oid_to_hex(&cur_oid),
2075 graph_commit->generation,
2076 max_generation + 1);
88968ebf
DS
2077
2078 if (graph_commit->date != odb_commit->date)
93b4405f 2079 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
88968ebf
DS
2080 oid_to_hex(&cur_oid),
2081 graph_commit->date,
2082 odb_commit->date);
96af91d4 2083 }
1f7f557f 2084 stop_progress(&progress);
96af91d4 2085
3da4b609
DS
2086 local_error = verify_commit_graph_error;
2087
2088 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2089 local_error |= verify_commit_graph(r, g->base_graph, flags);
2090
2091 return local_error;
283e68c7 2092}
c3756d5b
JT
2093
2094void free_commit_graph(struct commit_graph *g)
2095{
2096 if (!g)
2097 return;
2098 if (g->graph_fd >= 0) {
2099 munmap((void *)g->data, g->data_len);
2100 g->data = NULL;
2101 close(g->graph_fd);
2102 }
6c622f9f 2103 free(g->filename);
c3756d5b
JT
2104 free(g);
2105}