]> git.ipfire.org Git - thirdparty/git.git/blame - commit-graph.c
commit-graph: don't call write_graph_chunk_extra_edges() unnecessarily
[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" */
08fd81c9
DS
25
26#define GRAPH_DATA_WIDTH 36
27
28#define GRAPH_VERSION_1 0x1
29#define GRAPH_VERSION GRAPH_VERSION_1
30
31#define GRAPH_OID_VERSION_SHA1 1
32#define GRAPH_OID_LEN_SHA1 GIT_SHA1_RAWSZ
33#define GRAPH_OID_VERSION GRAPH_OID_VERSION_SHA1
34#define GRAPH_OID_LEN GRAPH_OID_LEN_SHA1
35
5af7417b 36#define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
08fd81c9
DS
37#define GRAPH_PARENT_MISSING 0x7fffffff
38#define GRAPH_EDGE_LAST_MASK 0x7fffffff
39#define GRAPH_PARENT_NONE 0x70000000
40
41#define GRAPH_LAST_EDGE 0x80000000
42
0e3b97cc 43#define GRAPH_HEADER_SIZE 8
08fd81c9
DS
44#define GRAPH_FANOUT_SIZE (4 * 256)
45#define GRAPH_CHUNKLOOKUP_WIDTH 12
0e3b97cc
DS
46#define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
47 + GRAPH_FANOUT_SIZE + GRAPH_OID_LEN)
08fd81c9 48
2a2e32bd 49char *get_commit_graph_filename(const char *obj_dir)
08fd81c9
DS
50{
51 return xstrfmt("%s/info/commit-graph", obj_dir);
52}
53
2a2e32bd
DS
54static struct commit_graph *alloc_commit_graph(void)
55{
56 struct commit_graph *g = xcalloc(1, sizeof(*g));
57 g->graph_fd = -1;
58
59 return g;
60}
61
d6538246
DS
62extern int read_replace_refs;
63
64static int commit_graph_compatible(struct repository *r)
65{
5cef295f
DS
66 if (!r->gitdir)
67 return 0;
68
d6538246
DS
69 if (read_replace_refs) {
70 prepare_replace_object(r);
71 if (hashmap_get_size(&r->objects->replace_map->map))
72 return 0;
73 }
74
20fd6d57
DS
75 prepare_commit_graft(r);
76 if (r->parsed_objects && r->parsed_objects->grafts_nr)
77 return 0;
78 if (is_repository_shallow(r))
79 return 0;
80
d6538246
DS
81 return 1;
82}
83
2a2e32bd
DS
84struct commit_graph *load_commit_graph_one(const char *graph_file)
85{
86 void *graph_map;
87 const unsigned char *data, *chunk_lookup;
88 size_t graph_size;
89 struct stat st;
90 uint32_t i;
91 struct commit_graph *graph;
92 int fd = git_open(graph_file);
93 uint64_t last_chunk_offset;
94 uint32_t last_chunk_id;
95 uint32_t graph_signature;
96 unsigned char graph_version, hash_version;
97
98 if (fd < 0)
99 return NULL;
100 if (fstat(fd, &st)) {
101 close(fd);
102 return NULL;
103 }
104 graph_size = xsize_t(st.st_size);
105
106 if (graph_size < GRAPH_MIN_SIZE) {
107 close(fd);
4f5b532d 108 die(_("graph file %s is too small"), graph_file);
2a2e32bd
DS
109 }
110 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
111 data = (const unsigned char *)graph_map;
112
113 graph_signature = get_be32(data);
114 if (graph_signature != GRAPH_SIGNATURE) {
4f5b532d 115 error(_("graph signature %X does not match signature %X"),
2a2e32bd
DS
116 graph_signature, GRAPH_SIGNATURE);
117 goto cleanup_fail;
118 }
119
120 graph_version = *(unsigned char*)(data + 4);
121 if (graph_version != GRAPH_VERSION) {
4f5b532d 122 error(_("graph version %X does not match version %X"),
2a2e32bd
DS
123 graph_version, GRAPH_VERSION);
124 goto cleanup_fail;
125 }
126
127 hash_version = *(unsigned char*)(data + 5);
128 if (hash_version != GRAPH_OID_VERSION) {
4f5b532d 129 error(_("hash version %X does not match version %X"),
2a2e32bd
DS
130 hash_version, GRAPH_OID_VERSION);
131 goto cleanup_fail;
132 }
133
134 graph = alloc_commit_graph();
135
136 graph->hash_len = GRAPH_OID_LEN;
137 graph->num_chunks = *(unsigned char*)(data + 6);
138 graph->graph_fd = fd;
139 graph->data = graph_map;
140 graph->data_len = graph_size;
141
142 last_chunk_id = 0;
143 last_chunk_offset = 8;
144 chunk_lookup = data + 8;
145 for (i = 0; i < graph->num_chunks; i++) {
146 uint32_t chunk_id = get_be32(chunk_lookup + 0);
147 uint64_t chunk_offset = get_be64(chunk_lookup + 4);
148 int chunk_repeated = 0;
149
150 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
151
152 if (chunk_offset > graph_size - GIT_MAX_RAWSZ) {
4f5b532d 153 error(_("improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
2a2e32bd
DS
154 (uint32_t)chunk_offset);
155 goto cleanup_fail;
156 }
157
158 switch (chunk_id) {
159 case GRAPH_CHUNKID_OIDFANOUT:
160 if (graph->chunk_oid_fanout)
161 chunk_repeated = 1;
162 else
163 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
164 break;
165
166 case GRAPH_CHUNKID_OIDLOOKUP:
167 if (graph->chunk_oid_lookup)
168 chunk_repeated = 1;
169 else
170 graph->chunk_oid_lookup = data + chunk_offset;
171 break;
172
173 case GRAPH_CHUNKID_DATA:
174 if (graph->chunk_commit_data)
175 chunk_repeated = 1;
176 else
177 graph->chunk_commit_data = data + chunk_offset;
178 break;
179
5af7417b
SG
180 case GRAPH_CHUNKID_EXTRAEDGES:
181 if (graph->chunk_extra_edges)
2a2e32bd
DS
182 chunk_repeated = 1;
183 else
5af7417b 184 graph->chunk_extra_edges = data + chunk_offset;
2a2e32bd
DS
185 break;
186 }
187
188 if (chunk_repeated) {
4f5b532d 189 error(_("chunk id %08x appears multiple times"), chunk_id);
2a2e32bd
DS
190 goto cleanup_fail;
191 }
192
193 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
194 {
195 graph->num_commits = (chunk_offset - last_chunk_offset)
196 / graph->hash_len;
197 }
198
199 last_chunk_id = chunk_id;
200 last_chunk_offset = chunk_offset;
201 }
202
203 return graph;
204
205cleanup_fail:
206 munmap(graph_map, graph_size);
207 close(fd);
208 exit(1);
209}
210
dade47c0 211static void prepare_commit_graph_one(struct repository *r, const char *obj_dir)
177722b3
DS
212{
213 char *graph_name;
214
dade47c0 215 if (r->objects->commit_graph)
177722b3
DS
216 return;
217
218 graph_name = get_commit_graph_filename(obj_dir);
dade47c0 219 r->objects->commit_graph =
85277506 220 load_commit_graph_one(graph_name);
177722b3
DS
221
222 FREE_AND_NULL(graph_name);
223}
224
5faf357b
JT
225/*
226 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
227 *
228 * On the first invocation, this function attemps to load the commit
229 * graph if the_repository is configured to have one.
230 */
dade47c0 231static int prepare_commit_graph(struct repository *r)
177722b3 232{
263db403 233 struct object_directory *odb;
dade47c0
JT
234 int config_value;
235
236 if (r->objects->commit_graph_attempted)
237 return !!r->objects->commit_graph;
238 r->objects->commit_graph_attempted = 1;
239
859fdc0c
DS
240 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
241 (repo_config_get_bool(r, "core.commitgraph", &config_value) ||
242 !config_value))
dade47c0
JT
243 /*
244 * This repository is not configured to use commit graphs, so
245 * do not load one. (But report commit_graph_attempted anyway
246 * so that commit graph loading is not attempted again for this
247 * repository.)
248 */
5faf357b
JT
249 return 0;
250
d6538246
DS
251 if (!commit_graph_compatible(r))
252 return 0;
253
dade47c0 254 prepare_alt_odb(r);
f0eaf638 255 for (odb = r->objects->odb;
263db403
JK
256 !r->objects->commit_graph && odb;
257 odb = odb->next)
258 prepare_commit_graph_one(r, odb->path);
dade47c0 259 return !!r->objects->commit_graph;
177722b3
DS
260}
261
6cc01743
DS
262int generation_numbers_enabled(struct repository *r)
263{
264 uint32_t first_generation;
265 struct commit_graph *g;
266 if (!prepare_commit_graph(r))
267 return 0;
268
269 g = r->objects->commit_graph;
270
271 if (!g->num_commits)
272 return 0;
273
274 first_generation = get_be32(g->chunk_commit_data +
275 g->hash_len + 8) >> 2;
276
277 return !!first_generation;
278}
279
829a3215 280void close_commit_graph(struct repository *r)
177722b3 281{
829a3215
DS
282 free_commit_graph(r->objects->commit_graph);
283 r->objects->commit_graph = NULL;
177722b3
DS
284}
285
286static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
287{
288 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
289 g->chunk_oid_lookup, g->hash_len, pos);
290}
291
292static struct commit_list **insert_parent_or_die(struct commit_graph *g,
293 uint64_t pos,
294 struct commit_list **pptr)
295{
296 struct commit *c;
297 struct object_id oid;
96af91d4 298
53614b13
DS
299 if (pos >= g->num_commits)
300 die("invalid parent position %"PRIu64, pos);
301
177722b3 302 hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * pos);
c1f5eb49 303 c = lookup_commit(the_repository, &oid);
177722b3 304 if (!c)
4f5b532d 305 die(_("could not find commit %s"), oid_to_hex(&oid));
177722b3
DS
306 c->graph_pos = pos;
307 return &commit_list_insert(c, pptr)->next;
308}
309
e2838d85
DS
310static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
311{
312 const unsigned char *commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * pos;
313 item->graph_pos = pos;
314 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
315}
316
177722b3
DS
317static int fill_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t pos)
318{
177722b3
DS
319 uint32_t edge_value;
320 uint32_t *parent_data_ptr;
321 uint64_t date_low, date_high;
322 struct commit_list **pptr;
323 const unsigned char *commit_data = g->chunk_commit_data + (g->hash_len + 16) * pos;
324
325 item->object.parsed = 1;
326 item->graph_pos = pos;
327
7b8a21db 328 item->maybe_tree = NULL;
177722b3
DS
329
330 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
331 date_low = get_be32(commit_data + g->hash_len + 12);
332 item->date = (timestamp_t)((date_high << 32) | date_low);
333
83073cc9
DS
334 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
335
177722b3
DS
336 pptr = &item->parents;
337
338 edge_value = get_be32(commit_data + g->hash_len);
339 if (edge_value == GRAPH_PARENT_NONE)
340 return 1;
341 pptr = insert_parent_or_die(g, edge_value, pptr);
342
343 edge_value = get_be32(commit_data + g->hash_len + 4);
344 if (edge_value == GRAPH_PARENT_NONE)
345 return 1;
5af7417b 346 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
177722b3
DS
347 pptr = insert_parent_or_die(g, edge_value, pptr);
348 return 1;
349 }
350
5af7417b 351 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
177722b3
DS
352 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
353 do {
354 edge_value = get_be32(parent_data_ptr);
355 pptr = insert_parent_or_die(g,
356 edge_value & GRAPH_EDGE_LAST_MASK,
357 pptr);
358 parent_data_ptr++;
359 } while (!(edge_value & GRAPH_LAST_EDGE));
360
361 return 1;
362}
363
e2838d85
DS
364static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
365{
366 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
367 *pos = item->graph_pos;
368 return 1;
369 } else {
370 return bsearch_graph(g, &(item->object.oid), pos);
371 }
372}
373
ee797053 374static int parse_commit_in_graph_one(struct commit_graph *g, struct commit *item)
177722b3 375{
e2838d85
DS
376 uint32_t pos;
377
177722b3
DS
378 if (item->object.parsed)
379 return 1;
ee797053
DS
380
381 if (find_commit_in_graph(item, g, &pos))
382 return fill_commit_in_graph(item, g, pos);
383
384 return 0;
385}
386
dade47c0 387int parse_commit_in_graph(struct repository *r, struct commit *item)
ee797053 388{
dade47c0 389 if (!prepare_commit_graph(r))
ee797053 390 return 0;
dade47c0 391 return parse_commit_in_graph_one(r->objects->commit_graph, item);
177722b3
DS
392}
393
dade47c0 394void load_commit_graph_info(struct repository *r, struct commit *item)
e2838d85
DS
395{
396 uint32_t pos;
dade47c0 397 if (!prepare_commit_graph(r))
e2838d85 398 return;
dade47c0
JT
399 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
400 fill_commit_graph_info(item, r->objects->commit_graph, pos);
e2838d85
DS
401}
402
7b8a21db
DS
403static struct tree *load_tree_for_commit(struct commit_graph *g, struct commit *c)
404{
405 struct object_id oid;
406 const unsigned char *commit_data = g->chunk_commit_data +
407 GRAPH_DATA_WIDTH * (c->graph_pos);
408
409 hashcpy(oid.hash, commit_data);
f86bcc7b 410 c->maybe_tree = lookup_tree(the_repository, &oid);
7b8a21db
DS
411
412 return c->maybe_tree;
413}
414
0cbef8f8
DS
415static struct tree *get_commit_tree_in_graph_one(struct commit_graph *g,
416 const struct commit *c)
7b8a21db
DS
417{
418 if (c->maybe_tree)
419 return c->maybe_tree;
420 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
0cbef8f8
DS
421 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
422
423 return load_tree_for_commit(g, (struct commit *)c);
424}
7b8a21db 425
dade47c0 426struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
0cbef8f8 427{
dade47c0 428 return get_commit_tree_in_graph_one(r->objects->commit_graph, c);
7b8a21db
DS
429}
430
08fd81c9
DS
431static void write_graph_chunk_fanout(struct hashfile *f,
432 struct commit **commits,
433 int nr_commits)
434{
435 int i, count = 0;
436 struct commit **list = commits;
437
438 /*
439 * Write the first-level table (the list is sorted,
440 * but we use a 256-entry lookup to be able to avoid
441 * having to do eight extra binary search iterations).
442 */
443 for (i = 0; i < 256; i++) {
444 while (count < nr_commits) {
445 if ((*list)->object.oid.hash[0] != i)
446 break;
447 count++;
448 list++;
449 }
450
451 hashwrite_be32(f, count);
452 }
453}
454
455static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
456 struct commit **commits, int nr_commits)
457{
458 struct commit **list = commits;
459 int count;
460 for (count = 0; count < nr_commits; count++, list++)
461 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
462}
463
464static const unsigned char *commit_to_sha1(size_t index, void *table)
465{
466 struct commit **commits = table;
467 return commits[index]->object.oid.hash;
468}
469
470static void write_graph_chunk_data(struct hashfile *f, int hash_len,
471 struct commit **commits, int nr_commits)
472{
473 struct commit **list = commits;
474 struct commit **last = commits + nr_commits;
475 uint32_t num_extra_edges = 0;
476
477 while (list < last) {
478 struct commit_list *parent;
479 int edge_value;
480 uint32_t packedDate[2];
481
482 parse_commit(*list);
2e27bd77 483 hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
08fd81c9
DS
484
485 parent = (*list)->parents;
486
487 if (!parent)
488 edge_value = GRAPH_PARENT_NONE;
489 else {
490 edge_value = sha1_pos(parent->item->object.oid.hash,
491 commits,
492 nr_commits,
493 commit_to_sha1);
494
495 if (edge_value < 0)
496 edge_value = GRAPH_PARENT_MISSING;
497 }
498
499 hashwrite_be32(f, edge_value);
500
501 if (parent)
502 parent = parent->next;
503
504 if (!parent)
505 edge_value = GRAPH_PARENT_NONE;
506 else if (parent->next)
5af7417b 507 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
08fd81c9
DS
508 else {
509 edge_value = sha1_pos(parent->item->object.oid.hash,
510 commits,
511 nr_commits,
512 commit_to_sha1);
513 if (edge_value < 0)
514 edge_value = GRAPH_PARENT_MISSING;
515 }
516
517 hashwrite_be32(f, edge_value);
518
5af7417b 519 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
08fd81c9
DS
520 do {
521 num_extra_edges++;
522 parent = parent->next;
523 } while (parent);
524 }
525
526 if (sizeof((*list)->date) > 4)
527 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
528 else
529 packedDate[0] = 0;
530
3258c663
DS
531 packedDate[0] |= htonl((*list)->generation << 2);
532
08fd81c9
DS
533 packedDate[1] = htonl((*list)->date);
534 hashwrite(f, packedDate, 8);
535
536 list++;
537 }
538}
539
5af7417b 540static void write_graph_chunk_extra_edges(struct hashfile *f,
08fd81c9
DS
541 struct commit **commits,
542 int nr_commits)
543{
544 struct commit **list = commits;
545 struct commit **last = commits + nr_commits;
546 struct commit_list *parent;
547
548 while (list < last) {
549 int num_parents = 0;
550 for (parent = (*list)->parents; num_parents < 3 && parent;
551 parent = parent->next)
552 num_parents++;
553
554 if (num_parents <= 2) {
555 list++;
556 continue;
557 }
558
559 /* Since num_parents > 2, this initializer is safe. */
560 for (parent = (*list)->parents->next; parent; parent = parent->next) {
561 int edge_value = sha1_pos(parent->item->object.oid.hash,
562 commits,
563 nr_commits,
564 commit_to_sha1);
565
566 if (edge_value < 0)
567 edge_value = GRAPH_PARENT_MISSING;
568 else if (!parent->next)
569 edge_value |= GRAPH_LAST_EDGE;
570
571 hashwrite_be32(f, edge_value);
572 }
573
574 list++;
575 }
576}
577
578static int commit_compare(const void *_a, const void *_b)
579{
580 const struct object_id *a = (const struct object_id *)_a;
581 const struct object_id *b = (const struct object_id *)_b;
582 return oidcmp(a, b);
583}
584
585struct packed_commit_list {
586 struct commit **list;
587 int nr;
588 int alloc;
589};
590
591struct packed_oid_list {
592 struct object_id *list;
593 int nr;
594 int alloc;
7b0f2292
ÆAB
595 struct progress *progress;
596 int progress_done;
08fd81c9
DS
597};
598
599static int add_packed_commits(const struct object_id *oid,
600 struct packed_git *pack,
601 uint32_t pos,
602 void *data)
603{
604 struct packed_oid_list *list = (struct packed_oid_list*)data;
605 enum object_type type;
606 off_t offset = nth_packed_object_offset(pack, pos);
607 struct object_info oi = OBJECT_INFO_INIT;
608
7b0f2292
ÆAB
609 if (list->progress)
610 display_progress(list->progress, ++list->progress_done);
611
08fd81c9 612 oi.typep = &type;
fcb6df32 613 if (packed_object_info(the_repository, pack, offset, &oi) < 0)
4f5b532d 614 die(_("unable to get type of object %s"), oid_to_hex(oid));
08fd81c9
DS
615
616 if (type != OBJ_COMMIT)
617 return 0;
618
619 ALLOC_GROW(list->list, list->nr + 1, list->alloc);
620 oidcpy(&(list->list[list->nr]), oid);
621 list->nr++;
622
623 return 0;
624}
625
4f2542b4
DS
626static void add_missing_parents(struct packed_oid_list *oids, struct commit *commit)
627{
628 struct commit_list *parent;
629 for (parent = commit->parents; parent; parent = parent->next) {
630 if (!(parent->item->object.flags & UNINTERESTING)) {
631 ALLOC_GROW(oids->list, oids->nr + 1, oids->alloc);
632 oidcpy(&oids->list[oids->nr], &(parent->item->object.oid));
633 oids->nr++;
634 parent->item->object.flags |= UNINTERESTING;
635 }
636 }
637}
638
7b0f2292 639static void close_reachable(struct packed_oid_list *oids, int report_progress)
4f2542b4 640{
01ca3877 641 int i, j;
4f2542b4 642 struct commit *commit;
7b0f2292 643 struct progress *progress = NULL;
4f2542b4 644
7b0f2292
ÆAB
645 if (report_progress)
646 progress = start_delayed_progress(
01ca3877 647 _("Loading known commits in commit graph"), j = 0);
4f2542b4 648 for (i = 0; i < oids->nr; i++) {
7b0f2292 649 display_progress(progress, ++j);
c1f5eb49 650 commit = lookup_commit(the_repository, &oids->list[i]);
4f2542b4
DS
651 if (commit)
652 commit->object.flags |= UNINTERESTING;
653 }
01ca3877 654 stop_progress(&progress);
4f2542b4
DS
655
656 /*
657 * As this loop runs, oids->nr may grow, but not more
658 * than the number of missing commits in the reachable
659 * closure.
660 */
01ca3877
ÆAB
661 if (report_progress)
662 progress = start_delayed_progress(
663 _("Expanding reachable commits in commit graph"), j = 0);
4f2542b4 664 for (i = 0; i < oids->nr; i++) {
7b0f2292 665 display_progress(progress, ++j);
c1f5eb49 666 commit = lookup_commit(the_repository, &oids->list[i]);
4f2542b4
DS
667
668 if (commit && !parse_commit(commit))
669 add_missing_parents(oids, commit);
670 }
01ca3877 671 stop_progress(&progress);
4f2542b4 672
01ca3877
ÆAB
673 if (report_progress)
674 progress = start_delayed_progress(
675 _("Clearing commit marks in commit graph"), j = 0);
4f2542b4 676 for (i = 0; i < oids->nr; i++) {
7b0f2292 677 display_progress(progress, ++j);
c1f5eb49 678 commit = lookup_commit(the_repository, &oids->list[i]);
4f2542b4
DS
679
680 if (commit)
681 commit->object.flags &= ~UNINTERESTING;
682 }
7b0f2292 683 stop_progress(&progress);
4f2542b4
DS
684}
685
7b0f2292
ÆAB
686static void compute_generation_numbers(struct packed_commit_list* commits,
687 int report_progress)
3258c663
DS
688{
689 int i;
690 struct commit_list *list = NULL;
7b0f2292 691 struct progress *progress = NULL;
3258c663 692
7b0f2292
ÆAB
693 if (report_progress)
694 progress = start_progress(
695 _("Computing commit graph generation numbers"),
696 commits->nr);
3258c663 697 for (i = 0; i < commits->nr; i++) {
7b0f2292 698 display_progress(progress, i + 1);
3258c663
DS
699 if (commits->list[i]->generation != GENERATION_NUMBER_INFINITY &&
700 commits->list[i]->generation != GENERATION_NUMBER_ZERO)
701 continue;
702
703 commit_list_insert(commits->list[i], &list);
704 while (list) {
705 struct commit *current = list->item;
706 struct commit_list *parent;
707 int all_parents_computed = 1;
708 uint32_t max_generation = 0;
709
710 for (parent = current->parents; parent; parent = parent->next) {
711 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
712 parent->item->generation == GENERATION_NUMBER_ZERO) {
713 all_parents_computed = 0;
714 commit_list_insert(parent->item, &list);
715 break;
716 } else if (parent->item->generation > max_generation) {
717 max_generation = parent->item->generation;
718 }
719 }
720
721 if (all_parents_computed) {
722 current->generation = max_generation + 1;
723 pop_commit(&list);
724
725 if (current->generation > GENERATION_NUMBER_MAX)
726 current->generation = GENERATION_NUMBER_MAX;
727 }
728 }
729 }
7b0f2292 730 stop_progress(&progress);
3258c663
DS
731}
732
59fb8770
DS
733static int add_ref_to_list(const char *refname,
734 const struct object_id *oid,
735 int flags, void *cb_data)
736{
737 struct string_list *list = (struct string_list *)cb_data;
738
739 string_list_append(list, oid_to_hex(oid));
740 return 0;
741}
742
7b0f2292
ÆAB
743void write_commit_graph_reachable(const char *obj_dir, int append,
744 int report_progress)
59fb8770 745{
f4dbdfc4 746 struct string_list list = STRING_LIST_INIT_DUP;
59fb8770 747
59fb8770 748 for_each_ref(add_ref_to_list, &list);
7b0f2292 749 write_commit_graph(obj_dir, NULL, &list, append, report_progress);
f4dbdfc4
DS
750
751 string_list_clear(&list, 0);
59fb8770
DS
752}
753
049d51a2 754void write_commit_graph(const char *obj_dir,
d88b14b3
DS
755 struct string_list *pack_indexes,
756 struct string_list *commit_hex,
7b0f2292 757 int append, int report_progress)
08fd81c9
DS
758{
759 struct packed_oid_list oids;
760 struct packed_commit_list commits;
761 struct hashfile *f;
762 uint32_t i, count_distinct = 0;
763 char *graph_name;
08fd81c9
DS
764 struct lock_file lk = LOCK_INIT;
765 uint32_t chunk_ids[5];
766 uint64_t chunk_offsets[5];
767 int num_chunks;
768 int num_extra_edges;
769 struct commit_list *parent;
7b0f2292 770 struct progress *progress = NULL;
08fd81c9 771
d6538246
DS
772 if (!commit_graph_compatible(the_repository))
773 return;
774
08fd81c9 775 oids.nr = 0;
53c36670 776 oids.alloc = approximate_object_count() / 32;
7b0f2292
ÆAB
777 oids.progress = NULL;
778 oids.progress_done = 0;
08fd81c9 779
7547b95b 780 if (append) {
dade47c0 781 prepare_commit_graph_one(the_repository, obj_dir);
85277506
JT
782 if (the_repository->objects->commit_graph)
783 oids.alloc += the_repository->objects->commit_graph->num_commits;
7547b95b
DS
784 }
785
08fd81c9
DS
786 if (oids.alloc < 1024)
787 oids.alloc = 1024;
788 ALLOC_ARRAY(oids.list, oids.alloc);
789
85277506
JT
790 if (append && the_repository->objects->commit_graph) {
791 struct commit_graph *commit_graph =
792 the_repository->objects->commit_graph;
7547b95b
DS
793 for (i = 0; i < commit_graph->num_commits; i++) {
794 const unsigned char *hash = commit_graph->chunk_oid_lookup +
795 commit_graph->hash_len * i;
796 hashcpy(oids.list[oids.nr++].hash, hash);
797 }
798 }
799
049d51a2
DS
800 if (pack_indexes) {
801 struct strbuf packname = STRBUF_INIT;
802 int dirlen;
803 strbuf_addf(&packname, "%s/pack/", obj_dir);
804 dirlen = packname.len;
7b0f2292
ÆAB
805 if (report_progress) {
806 oids.progress = start_delayed_progress(
807 _("Finding commits for commit graph"), 0);
808 oids.progress_done = 0;
809 }
d88b14b3 810 for (i = 0; i < pack_indexes->nr; i++) {
049d51a2
DS
811 struct packed_git *p;
812 strbuf_setlen(&packname, dirlen);
d88b14b3 813 strbuf_addstr(&packname, pack_indexes->items[i].string);
049d51a2
DS
814 p = add_packed_git(packname.buf, packname.len, 1);
815 if (!p)
4f5b532d 816 die(_("error adding pack %s"), packname.buf);
049d51a2 817 if (open_pack_index(p))
4f5b532d 818 die(_("error opening index for %s"), packname.buf);
d7574c95
ÆAB
819 for_each_object_in_pack(p, add_packed_commits, &oids,
820 FOR_EACH_OBJECT_PACK_ORDER);
049d51a2 821 close_pack(p);
f4dbdfc4 822 free(p);
049d51a2 823 }
7b0f2292 824 stop_progress(&oids.progress);
049d51a2 825 strbuf_release(&packname);
3d5df01b
DS
826 }
827
828 if (commit_hex) {
7b0f2292
ÆAB
829 if (report_progress)
830 progress = start_delayed_progress(
831 _("Finding commits for commit graph"),
832 commit_hex->nr);
d88b14b3 833 for (i = 0; i < commit_hex->nr; i++) {
3d5df01b
DS
834 const char *end;
835 struct object_id oid;
836 struct commit *result;
837
7b0f2292 838 display_progress(progress, i + 1);
d88b14b3
DS
839 if (commit_hex->items[i].string &&
840 parse_oid_hex(commit_hex->items[i].string, &oid, &end))
3d5df01b
DS
841 continue;
842
21e1ee8f 843 result = lookup_commit_reference_gently(the_repository, &oid, 1);
3d5df01b
DS
844
845 if (result) {
846 ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc);
847 oidcpy(&oids.list[oids.nr], &(result->object.oid));
848 oids.nr++;
849 }
850 }
7b0f2292 851 stop_progress(&progress);
3d5df01b
DS
852 }
853
7b0f2292
ÆAB
854 if (!pack_indexes && !commit_hex) {
855 if (report_progress)
856 oids.progress = start_delayed_progress(
857 _("Finding commits for commit graph"), 0);
d7574c95
ÆAB
858 for_each_packed_object(add_packed_commits, &oids,
859 FOR_EACH_OBJECT_PACK_ORDER);
7b0f2292
ÆAB
860 stop_progress(&oids.progress);
861 }
049d51a2 862
7b0f2292 863 close_reachable(&oids, report_progress);
08fd81c9
DS
864
865 QSORT(oids.list, oids.nr, commit_compare);
866
867 count_distinct = 1;
868 for (i = 1; i < oids.nr; i++) {
9001dc2a 869 if (!oideq(&oids.list[i - 1], &oids.list[i]))
08fd81c9
DS
870 count_distinct++;
871 }
872
873 if (count_distinct >= GRAPH_PARENT_MISSING)
874 die(_("the commit graph format cannot write %d commits"), count_distinct);
875
876 commits.nr = 0;
877 commits.alloc = count_distinct;
878 ALLOC_ARRAY(commits.list, commits.alloc);
879
880 num_extra_edges = 0;
881 for (i = 0; i < oids.nr; i++) {
882 int num_parents = 0;
4a7e27e9 883 if (i > 0 && oideq(&oids.list[i - 1], &oids.list[i]))
08fd81c9
DS
884 continue;
885
c1f5eb49 886 commits.list[commits.nr] = lookup_commit(the_repository, &oids.list[i]);
08fd81c9
DS
887 parse_commit(commits.list[commits.nr]);
888
889 for (parent = commits.list[commits.nr]->parents;
890 parent; parent = parent->next)
891 num_parents++;
892
893 if (num_parents > 2)
894 num_extra_edges += num_parents - 1;
895
896 commits.nr++;
897 }
898 num_chunks = num_extra_edges ? 4 : 3;
899
900 if (commits.nr >= GRAPH_PARENT_MISSING)
901 die(_("too many commits to write graph"));
902
7b0f2292 903 compute_generation_numbers(&commits, report_progress);
08fd81c9 904
08fd81c9 905 graph_name = get_commit_graph_filename(obj_dir);
f4dbdfc4
DS
906 if (safe_create_leading_directories(graph_name)) {
907 UNLEAK(graph_name);
33286dcd
DS
908 die_errno(_("unable to create leading directories of %s"),
909 graph_name);
f4dbdfc4 910 }
08fd81c9 911
33286dcd 912 hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR);
08fd81c9
DS
913 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
914
915 hashwrite_be32(f, GRAPH_SIGNATURE);
916
917 hashwrite_u8(f, GRAPH_VERSION);
918 hashwrite_u8(f, GRAPH_OID_VERSION);
919 hashwrite_u8(f, num_chunks);
920 hashwrite_u8(f, 0); /* unused padding byte */
921
922 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
923 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
924 chunk_ids[2] = GRAPH_CHUNKID_DATA;
925 if (num_extra_edges)
5af7417b 926 chunk_ids[3] = GRAPH_CHUNKID_EXTRAEDGES;
08fd81c9
DS
927 else
928 chunk_ids[3] = 0;
929 chunk_ids[4] = 0;
930
931 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
932 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
933 chunk_offsets[2] = chunk_offsets[1] + GRAPH_OID_LEN * commits.nr;
934 chunk_offsets[3] = chunk_offsets[2] + (GRAPH_OID_LEN + 16) * commits.nr;
935 chunk_offsets[4] = chunk_offsets[3] + 4 * num_extra_edges;
936
937 for (i = 0; i <= num_chunks; i++) {
938 uint32_t chunk_write[3];
939
940 chunk_write[0] = htonl(chunk_ids[i]);
941 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
942 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
943 hashwrite(f, chunk_write, 12);
944 }
945
946 write_graph_chunk_fanout(f, commits.list, commits.nr);
947 write_graph_chunk_oids(f, GRAPH_OID_LEN, commits.list, commits.nr);
948 write_graph_chunk_data(f, GRAPH_OID_LEN, commits.list, commits.nr);
857ba928
SG
949 if (num_extra_edges)
950 write_graph_chunk_extra_edges(f, commits.list, commits.nr);
08fd81c9 951
829a3215 952 close_commit_graph(the_repository);
08fd81c9
DS
953 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
954 commit_lock_file(&lk);
955
f4dbdfc4
DS
956 free(graph_name);
957 free(commits.list);
08fd81c9 958 free(oids.list);
08fd81c9 959}
283e68c7 960
41df0e30 961#define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
283e68c7
DS
962static int verify_commit_graph_error;
963
964static void graph_report(const char *fmt, ...)
965{
966 va_list ap;
967
968 verify_commit_graph_error = 1;
969 va_start(ap, fmt);
970 vfprintf(stderr, fmt, ap);
971 fprintf(stderr, "\n");
972 va_end(ap);
973}
974
1373e547
DS
975#define GENERATION_ZERO_EXISTS 1
976#define GENERATION_NUMBER_EXISTS 2
977
283e68c7
DS
978int verify_commit_graph(struct repository *r, struct commit_graph *g)
979{
9bda8467 980 uint32_t i, cur_fanout_pos = 0;
41df0e30 981 struct object_id prev_oid, cur_oid, checksum;
1373e547 982 int generation_zero = 0;
41df0e30
DS
983 struct hashfile *f;
984 int devnull;
1f7f557f 985 struct progress *progress = NULL;
9bda8467 986
283e68c7
DS
987 if (!g) {
988 graph_report("no commit-graph file loaded");
989 return 1;
990 }
991
2bd0365f
DS
992 verify_commit_graph_error = 0;
993
994 if (!g->chunk_oid_fanout)
995 graph_report("commit-graph is missing the OID Fanout chunk");
996 if (!g->chunk_oid_lookup)
997 graph_report("commit-graph is missing the OID Lookup chunk");
998 if (!g->chunk_commit_data)
999 graph_report("commit-graph is missing the Commit Data chunk");
1000
9bda8467
DS
1001 if (verify_commit_graph_error)
1002 return verify_commit_graph_error;
1003
41df0e30
DS
1004 devnull = open("/dev/null", O_WRONLY);
1005 f = hashfd(devnull, NULL);
1006 hashwrite(f, g->data, g->data_len - g->hash_len);
1007 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
67947c34 1008 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
41df0e30
DS
1009 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
1010 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
1011 }
1012
9bda8467 1013 for (i = 0; i < g->num_commits; i++) {
2e3c0737
DS
1014 struct commit *graph_commit;
1015
9bda8467
DS
1016 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1017
1018 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
1019 graph_report("commit-graph has incorrect OID order: %s then %s",
1020 oid_to_hex(&prev_oid),
1021 oid_to_hex(&cur_oid));
1022
1023 oidcpy(&prev_oid, &cur_oid);
1024
1025 while (cur_oid.hash[0] > cur_fanout_pos) {
1026 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1027
1028 if (i != fanout_value)
1029 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
1030 cur_fanout_pos, fanout_value, i);
1031 cur_fanout_pos++;
1032 }
2e3c0737 1033
82952964 1034 graph_commit = lookup_commit(r, &cur_oid);
2e3c0737
DS
1035 if (!parse_commit_in_graph_one(g, graph_commit))
1036 graph_report("failed to parse %s from commit-graph",
1037 oid_to_hex(&cur_oid));
9bda8467
DS
1038 }
1039
1040 while (cur_fanout_pos < 256) {
1041 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1042
1043 if (g->num_commits != fanout_value)
1044 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
1045 cur_fanout_pos, fanout_value, i);
1046
1047 cur_fanout_pos++;
1048 }
1049
41df0e30 1050 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
96af91d4
DS
1051 return verify_commit_graph_error;
1052
1f7f557f
ÆAB
1053 progress = start_progress(_("Verifying commits in commit graph"),
1054 g->num_commits);
96af91d4 1055 for (i = 0; i < g->num_commits; i++) {
2e3c0737 1056 struct commit *graph_commit, *odb_commit;
53614b13 1057 struct commit_list *graph_parents, *odb_parents;
1373e547 1058 uint32_t max_generation = 0;
96af91d4 1059
1f7f557f 1060 display_progress(progress, i + 1);
96af91d4
DS
1061 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1062
82952964 1063 graph_commit = lookup_commit(r, &cur_oid);
96af91d4
DS
1064 odb_commit = (struct commit *)create_object(r, cur_oid.hash, alloc_commit_node(r));
1065 if (parse_commit_internal(odb_commit, 0, 0)) {
1066 graph_report("failed to parse %s from object database",
1067 oid_to_hex(&cur_oid));
1068 continue;
1069 }
2e3c0737 1070
9001dc2a 1071 if (!oideq(&get_commit_tree_in_graph_one(g, graph_commit)->object.oid,
2e3c0737
DS
1072 get_commit_tree_oid(odb_commit)))
1073 graph_report("root tree OID for commit %s in commit-graph is %s != %s",
1074 oid_to_hex(&cur_oid),
1075 oid_to_hex(get_commit_tree_oid(graph_commit)),
1076 oid_to_hex(get_commit_tree_oid(odb_commit)));
53614b13
DS
1077
1078 graph_parents = graph_commit->parents;
1079 odb_parents = odb_commit->parents;
1080
1081 while (graph_parents) {
1082 if (odb_parents == NULL) {
1083 graph_report("commit-graph parent list for commit %s is too long",
1084 oid_to_hex(&cur_oid));
1085 break;
1086 }
1087
9001dc2a 1088 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
53614b13
DS
1089 graph_report("commit-graph parent for %s is %s != %s",
1090 oid_to_hex(&cur_oid),
1091 oid_to_hex(&graph_parents->item->object.oid),
1092 oid_to_hex(&odb_parents->item->object.oid));
1093
1373e547
DS
1094 if (graph_parents->item->generation > max_generation)
1095 max_generation = graph_parents->item->generation;
1096
53614b13
DS
1097 graph_parents = graph_parents->next;
1098 odb_parents = odb_parents->next;
1099 }
1100
1101 if (odb_parents != NULL)
1102 graph_report("commit-graph parent list for commit %s terminates early",
1103 oid_to_hex(&cur_oid));
1373e547
DS
1104
1105 if (!graph_commit->generation) {
1106 if (generation_zero == GENERATION_NUMBER_EXISTS)
1107 graph_report("commit-graph has generation number zero for commit %s, but non-zero elsewhere",
1108 oid_to_hex(&cur_oid));
1109 generation_zero = GENERATION_ZERO_EXISTS;
1110 } else if (generation_zero == GENERATION_ZERO_EXISTS)
1111 graph_report("commit-graph has non-zero generation number for commit %s, but zero elsewhere",
1112 oid_to_hex(&cur_oid));
1113
1114 if (generation_zero == GENERATION_ZERO_EXISTS)
1115 continue;
1116
1117 /*
1118 * If one of our parents has generation GENERATION_NUMBER_MAX, then
1119 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
1120 * extra logic in the following condition.
1121 */
1122 if (max_generation == GENERATION_NUMBER_MAX)
1123 max_generation--;
1124
1125 if (graph_commit->generation != max_generation + 1)
1126 graph_report("commit-graph generation for commit %s is %u != %u",
1127 oid_to_hex(&cur_oid),
1128 graph_commit->generation,
1129 max_generation + 1);
88968ebf
DS
1130
1131 if (graph_commit->date != odb_commit->date)
1132 graph_report("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime,
1133 oid_to_hex(&cur_oid),
1134 graph_commit->date,
1135 odb_commit->date);
96af91d4 1136 }
1f7f557f 1137 stop_progress(&progress);
96af91d4 1138
283e68c7
DS
1139 return verify_commit_graph_error;
1140}
c3756d5b
JT
1141
1142void free_commit_graph(struct commit_graph *g)
1143{
1144 if (!g)
1145 return;
1146 if (g->graph_fd >= 0) {
1147 munmap((void *)g->data, g->data_len);
1148 g->data = NULL;
1149 close(g->graph_fd);
1150 }
1151 free(g);
1152}