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