Merge branch 'md/exclude-promisor-objects-fix-cleanup'
[git/raj.git] / commit-graph.c
blob5c8fb4b134e96354cb25ea2d5bb060ce77ae3e2b
1 #include "cache.h"
2 #include "config.h"
3 #include "dir.h"
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"
10 #include "refs.h"
11 #include "revision.h"
12 #include "sha1-lookup.h"
13 #include "commit-graph.h"
14 #include "object-store.h"
15 #include "alloc.h"
16 #include "hashmap.h"
17 #include "replace-object.h"
18 #include "progress.h"
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" */
24 #define GRAPH_CHUNKID_LARGEEDGES 0x45444745 /* "EDGE" */
26 #define GRAPH_DATA_WIDTH 36
28 #define GRAPH_VERSION_1 0x1
29 #define GRAPH_VERSION GRAPH_VERSION_1
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
36 #define GRAPH_OCTOPUS_EDGES_NEEDED 0x80000000
37 #define GRAPH_PARENT_MISSING 0x7fffffff
38 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
39 #define GRAPH_PARENT_NONE 0x70000000
41 #define GRAPH_LAST_EDGE 0x80000000
43 #define GRAPH_HEADER_SIZE 8
44 #define GRAPH_FANOUT_SIZE (4 * 256)
45 #define GRAPH_CHUNKLOOKUP_WIDTH 12
46 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
47 + GRAPH_FANOUT_SIZE + GRAPH_OID_LEN)
49 char *get_commit_graph_filename(const char *obj_dir)
51 return xstrfmt("%s/info/commit-graph", obj_dir);
54 static struct commit_graph *alloc_commit_graph(void)
56 struct commit_graph *g = xcalloc(1, sizeof(*g));
57 g->graph_fd = -1;
59 return g;
62 extern int read_replace_refs;
64 static int commit_graph_compatible(struct repository *r)
66 if (!r->gitdir)
67 return 0;
69 if (read_replace_refs) {
70 prepare_replace_object(r);
71 if (hashmap_get_size(&r->objects->replace_map->map))
72 return 0;
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;
81 return 1;
84 struct commit_graph *load_commit_graph_one(const char *graph_file)
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;
98 if (fd < 0)
99 return NULL;
100 if (fstat(fd, &st)) {
101 close(fd);
102 return NULL;
104 graph_size = xsize_t(st.st_size);
106 if (graph_size < GRAPH_MIN_SIZE) {
107 close(fd);
108 die(_("graph file %s is too small"), graph_file);
110 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
111 data = (const unsigned char *)graph_map;
113 graph_signature = get_be32(data);
114 if (graph_signature != GRAPH_SIGNATURE) {
115 error(_("graph signature %X does not match signature %X"),
116 graph_signature, GRAPH_SIGNATURE);
117 goto cleanup_fail;
120 graph_version = *(unsigned char*)(data + 4);
121 if (graph_version != GRAPH_VERSION) {
122 error(_("graph version %X does not match version %X"),
123 graph_version, GRAPH_VERSION);
124 goto cleanup_fail;
127 hash_version = *(unsigned char*)(data + 5);
128 if (hash_version != GRAPH_OID_VERSION) {
129 error(_("hash version %X does not match version %X"),
130 hash_version, GRAPH_OID_VERSION);
131 goto cleanup_fail;
134 graph = alloc_commit_graph();
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;
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;
150 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
152 if (chunk_offset > graph_size - GIT_MAX_RAWSZ) {
153 error(_("improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
154 (uint32_t)chunk_offset);
155 goto cleanup_fail;
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;
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;
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;
180 case GRAPH_CHUNKID_LARGEEDGES:
181 if (graph->chunk_large_edges)
182 chunk_repeated = 1;
183 else
184 graph->chunk_large_edges = data + chunk_offset;
185 break;
188 if (chunk_repeated) {
189 error(_("chunk id %08x appears multiple times"), chunk_id);
190 goto cleanup_fail;
193 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
195 graph->num_commits = (chunk_offset - last_chunk_offset)
196 / graph->hash_len;
199 last_chunk_id = chunk_id;
200 last_chunk_offset = chunk_offset;
203 return graph;
205 cleanup_fail:
206 munmap(graph_map, graph_size);
207 close(fd);
208 exit(1);
211 static void prepare_commit_graph_one(struct repository *r, const char *obj_dir)
213 char *graph_name;
215 if (r->objects->commit_graph)
216 return;
218 graph_name = get_commit_graph_filename(obj_dir);
219 r->objects->commit_graph =
220 load_commit_graph_one(graph_name);
222 FREE_AND_NULL(graph_name);
226 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
228 * On the first invocation, this function attemps to load the commit
229 * graph if the_repository is configured to have one.
231 static int prepare_commit_graph(struct repository *r)
233 struct object_directory *odb;
234 int config_value;
236 if (r->objects->commit_graph_attempted)
237 return !!r->objects->commit_graph;
238 r->objects->commit_graph_attempted = 1;
240 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
241 (repo_config_get_bool(r, "core.commitgraph", &config_value) ||
242 !config_value))
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.)
249 return 0;
251 if (!commit_graph_compatible(r))
252 return 0;
254 prepare_alt_odb(r);
255 for (odb = r->objects->odb;
256 !r->objects->commit_graph && odb;
257 odb = odb->next)
258 prepare_commit_graph_one(r, odb->path);
259 return !!r->objects->commit_graph;
262 int generation_numbers_enabled(struct repository *r)
264 uint32_t first_generation;
265 struct commit_graph *g;
266 if (!prepare_commit_graph(r))
267 return 0;
269 g = r->objects->commit_graph;
271 if (!g->num_commits)
272 return 0;
274 first_generation = get_be32(g->chunk_commit_data +
275 g->hash_len + 8) >> 2;
277 return !!first_generation;
280 void close_commit_graph(struct repository *r)
282 free_commit_graph(r->objects->commit_graph);
283 r->objects->commit_graph = NULL;
286 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
288 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
289 g->chunk_oid_lookup, g->hash_len, pos);
292 static struct commit_list **insert_parent_or_die(struct commit_graph *g,
293 uint64_t pos,
294 struct commit_list **pptr)
296 struct commit *c;
297 struct object_id oid;
299 if (pos >= g->num_commits)
300 die("invalid parent position %"PRIu64, pos);
302 hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * pos);
303 c = lookup_commit(the_repository, &oid);
304 if (!c)
305 die(_("could not find commit %s"), oid_to_hex(&oid));
306 c->graph_pos = pos;
307 return &commit_list_insert(c, pptr)->next;
310 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
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;
317 static int fill_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t pos)
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;
325 item->object.parsed = 1;
326 item->graph_pos = pos;
328 item->maybe_tree = NULL;
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);
334 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
336 pptr = &item->parents;
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);
343 edge_value = get_be32(commit_data + g->hash_len + 4);
344 if (edge_value == GRAPH_PARENT_NONE)
345 return 1;
346 if (!(edge_value & GRAPH_OCTOPUS_EDGES_NEEDED)) {
347 pptr = insert_parent_or_die(g, edge_value, pptr);
348 return 1;
351 parent_data_ptr = (uint32_t*)(g->chunk_large_edges +
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));
361 return 1;
364 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
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);
374 static int parse_commit_in_graph_one(struct commit_graph *g, struct commit *item)
376 uint32_t pos;
378 if (item->object.parsed)
379 return 1;
381 if (find_commit_in_graph(item, g, &pos))
382 return fill_commit_in_graph(item, g, pos);
384 return 0;
387 int parse_commit_in_graph(struct repository *r, struct commit *item)
389 if (!prepare_commit_graph(r))
390 return 0;
391 return parse_commit_in_graph_one(r->objects->commit_graph, item);
394 void load_commit_graph_info(struct repository *r, struct commit *item)
396 uint32_t pos;
397 if (!prepare_commit_graph(r))
398 return;
399 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
400 fill_commit_graph_info(item, r->objects->commit_graph, pos);
403 static struct tree *load_tree_for_commit(struct commit_graph *g, struct commit *c)
405 struct object_id oid;
406 const unsigned char *commit_data = g->chunk_commit_data +
407 GRAPH_DATA_WIDTH * (c->graph_pos);
409 hashcpy(oid.hash, commit_data);
410 c->maybe_tree = lookup_tree(the_repository, &oid);
412 return c->maybe_tree;
415 static struct tree *get_commit_tree_in_graph_one(struct commit_graph *g,
416 const struct commit *c)
418 if (c->maybe_tree)
419 return c->maybe_tree;
420 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
421 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
423 return load_tree_for_commit(g, (struct commit *)c);
426 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
428 return get_commit_tree_in_graph_one(r->objects->commit_graph, c);
431 static void write_graph_chunk_fanout(struct hashfile *f,
432 struct commit **commits,
433 int nr_commits)
435 int i, count = 0;
436 struct commit **list = commits;
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).
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++;
451 hashwrite_be32(f, count);
455 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
456 struct commit **commits, int nr_commits)
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);
464 static const unsigned char *commit_to_sha1(size_t index, void *table)
466 struct commit **commits = table;
467 return commits[index]->object.oid.hash;
470 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
471 struct commit **commits, int nr_commits)
473 struct commit **list = commits;
474 struct commit **last = commits + nr_commits;
475 uint32_t num_extra_edges = 0;
477 while (list < last) {
478 struct commit_list *parent;
479 int edge_value;
480 uint32_t packedDate[2];
482 parse_commit(*list);
483 hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
485 parent = (*list)->parents;
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);
495 if (edge_value < 0)
496 edge_value = GRAPH_PARENT_MISSING;
499 hashwrite_be32(f, edge_value);
501 if (parent)
502 parent = parent->next;
504 if (!parent)
505 edge_value = GRAPH_PARENT_NONE;
506 else if (parent->next)
507 edge_value = GRAPH_OCTOPUS_EDGES_NEEDED | num_extra_edges;
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;
517 hashwrite_be32(f, edge_value);
519 if (edge_value & GRAPH_OCTOPUS_EDGES_NEEDED) {
520 do {
521 num_extra_edges++;
522 parent = parent->next;
523 } while (parent);
526 if (sizeof((*list)->date) > 4)
527 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
528 else
529 packedDate[0] = 0;
531 packedDate[0] |= htonl((*list)->generation << 2);
533 packedDate[1] = htonl((*list)->date);
534 hashwrite(f, packedDate, 8);
536 list++;
540 static void write_graph_chunk_large_edges(struct hashfile *f,
541 struct commit **commits,
542 int nr_commits)
544 struct commit **list = commits;
545 struct commit **last = commits + nr_commits;
546 struct commit_list *parent;
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++;
554 if (num_parents <= 2) {
555 list++;
556 continue;
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);
566 if (edge_value < 0)
567 edge_value = GRAPH_PARENT_MISSING;
568 else if (!parent->next)
569 edge_value |= GRAPH_LAST_EDGE;
571 hashwrite_be32(f, edge_value);
574 list++;
578 static int commit_compare(const void *_a, const void *_b)
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);
585 struct packed_commit_list {
586 struct commit **list;
587 int nr;
588 int alloc;
591 struct packed_oid_list {
592 struct object_id *list;
593 int nr;
594 int alloc;
595 struct progress *progress;
596 int progress_done;
599 static int add_packed_commits(const struct object_id *oid,
600 struct packed_git *pack,
601 uint32_t pos,
602 void *data)
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;
609 if (list->progress)
610 display_progress(list->progress, ++list->progress_done);
612 oi.typep = &type;
613 if (packed_object_info(the_repository, pack, offset, &oi) < 0)
614 die(_("unable to get type of object %s"), oid_to_hex(oid));
616 if (type != OBJ_COMMIT)
617 return 0;
619 ALLOC_GROW(list->list, list->nr + 1, list->alloc);
620 oidcpy(&(list->list[list->nr]), oid);
621 list->nr++;
623 return 0;
626 static void add_missing_parents(struct packed_oid_list *oids, struct commit *commit)
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;
639 static void close_reachable(struct packed_oid_list *oids, int report_progress)
641 int i, j;
642 struct commit *commit;
643 struct progress *progress = NULL;
645 if (report_progress)
646 progress = start_delayed_progress(
647 _("Loading known commits in commit graph"), j = 0);
648 for (i = 0; i < oids->nr; i++) {
649 display_progress(progress, ++j);
650 commit = lookup_commit(the_repository, &oids->list[i]);
651 if (commit)
652 commit->object.flags |= UNINTERESTING;
654 stop_progress(&progress);
657 * As this loop runs, oids->nr may grow, but not more
658 * than the number of missing commits in the reachable
659 * closure.
661 if (report_progress)
662 progress = start_delayed_progress(
663 _("Expanding reachable commits in commit graph"), j = 0);
664 for (i = 0; i < oids->nr; i++) {
665 display_progress(progress, ++j);
666 commit = lookup_commit(the_repository, &oids->list[i]);
668 if (commit && !parse_commit(commit))
669 add_missing_parents(oids, commit);
671 stop_progress(&progress);
673 if (report_progress)
674 progress = start_delayed_progress(
675 _("Clearing commit marks in commit graph"), j = 0);
676 for (i = 0; i < oids->nr; i++) {
677 display_progress(progress, ++j);
678 commit = lookup_commit(the_repository, &oids->list[i]);
680 if (commit)
681 commit->object.flags &= ~UNINTERESTING;
683 stop_progress(&progress);
686 static void compute_generation_numbers(struct packed_commit_list* commits,
687 int report_progress)
689 int i;
690 struct commit_list *list = NULL;
691 struct progress *progress = NULL;
693 if (report_progress)
694 progress = start_progress(
695 _("Computing commit graph generation numbers"),
696 commits->nr);
697 for (i = 0; i < commits->nr; i++) {
698 display_progress(progress, i + 1);
699 if (commits->list[i]->generation != GENERATION_NUMBER_INFINITY &&
700 commits->list[i]->generation != GENERATION_NUMBER_ZERO)
701 continue;
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;
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;
721 if (all_parents_computed) {
722 current->generation = max_generation + 1;
723 pop_commit(&list);
725 if (current->generation > GENERATION_NUMBER_MAX)
726 current->generation = GENERATION_NUMBER_MAX;
730 stop_progress(&progress);
733 static int add_ref_to_list(const char *refname,
734 const struct object_id *oid,
735 int flags, void *cb_data)
737 struct string_list *list = (struct string_list *)cb_data;
739 string_list_append(list, oid_to_hex(oid));
740 return 0;
743 void write_commit_graph_reachable(const char *obj_dir, int append,
744 int report_progress)
746 struct string_list list = STRING_LIST_INIT_DUP;
748 for_each_ref(add_ref_to_list, &list);
749 write_commit_graph(obj_dir, NULL, &list, append, report_progress);
751 string_list_clear(&list, 0);
754 void write_commit_graph(const char *obj_dir,
755 struct string_list *pack_indexes,
756 struct string_list *commit_hex,
757 int append, int report_progress)
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;
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;
770 struct progress *progress = NULL;
772 if (!commit_graph_compatible(the_repository))
773 return;
775 oids.nr = 0;
776 oids.alloc = approximate_object_count() / 32;
777 oids.progress = NULL;
778 oids.progress_done = 0;
780 if (append) {
781 prepare_commit_graph_one(the_repository, obj_dir);
782 if (the_repository->objects->commit_graph)
783 oids.alloc += the_repository->objects->commit_graph->num_commits;
786 if (oids.alloc < 1024)
787 oids.alloc = 1024;
788 ALLOC_ARRAY(oids.list, oids.alloc);
790 if (append && the_repository->objects->commit_graph) {
791 struct commit_graph *commit_graph =
792 the_repository->objects->commit_graph;
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);
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;
805 if (report_progress) {
806 oids.progress = start_delayed_progress(
807 _("Finding commits for commit graph"), 0);
808 oids.progress_done = 0;
810 for (i = 0; i < pack_indexes->nr; i++) {
811 struct packed_git *p;
812 strbuf_setlen(&packname, dirlen);
813 strbuf_addstr(&packname, pack_indexes->items[i].string);
814 p = add_packed_git(packname.buf, packname.len, 1);
815 if (!p)
816 die(_("error adding pack %s"), packname.buf);
817 if (open_pack_index(p))
818 die(_("error opening index for %s"), packname.buf);
819 for_each_object_in_pack(p, add_packed_commits, &oids, 0);
820 close_pack(p);
821 free(p);
823 stop_progress(&oids.progress);
824 strbuf_release(&packname);
827 if (commit_hex) {
828 if (report_progress)
829 progress = start_delayed_progress(
830 _("Finding commits for commit graph"),
831 commit_hex->nr);
832 for (i = 0; i < commit_hex->nr; i++) {
833 const char *end;
834 struct object_id oid;
835 struct commit *result;
837 display_progress(progress, i + 1);
838 if (commit_hex->items[i].string &&
839 parse_oid_hex(commit_hex->items[i].string, &oid, &end))
840 continue;
842 result = lookup_commit_reference_gently(the_repository, &oid, 1);
844 if (result) {
845 ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc);
846 oidcpy(&oids.list[oids.nr], &(result->object.oid));
847 oids.nr++;
850 stop_progress(&progress);
853 if (!pack_indexes && !commit_hex) {
854 if (report_progress)
855 oids.progress = start_delayed_progress(
856 _("Finding commits for commit graph"), 0);
857 for_each_packed_object(add_packed_commits, &oids, 0);
858 stop_progress(&oids.progress);
861 close_reachable(&oids, report_progress);
863 QSORT(oids.list, oids.nr, commit_compare);
865 count_distinct = 1;
866 for (i = 1; i < oids.nr; i++) {
867 if (!oideq(&oids.list[i - 1], &oids.list[i]))
868 count_distinct++;
871 if (count_distinct >= GRAPH_PARENT_MISSING)
872 die(_("the commit graph format cannot write %d commits"), count_distinct);
874 commits.nr = 0;
875 commits.alloc = count_distinct;
876 ALLOC_ARRAY(commits.list, commits.alloc);
878 num_extra_edges = 0;
879 for (i = 0; i < oids.nr; i++) {
880 int num_parents = 0;
881 if (i > 0 && oideq(&oids.list[i - 1], &oids.list[i]))
882 continue;
884 commits.list[commits.nr] = lookup_commit(the_repository, &oids.list[i]);
885 parse_commit(commits.list[commits.nr]);
887 for (parent = commits.list[commits.nr]->parents;
888 parent; parent = parent->next)
889 num_parents++;
891 if (num_parents > 2)
892 num_extra_edges += num_parents - 1;
894 commits.nr++;
896 num_chunks = num_extra_edges ? 4 : 3;
898 if (commits.nr >= GRAPH_PARENT_MISSING)
899 die(_("too many commits to write graph"));
901 compute_generation_numbers(&commits, report_progress);
903 graph_name = get_commit_graph_filename(obj_dir);
904 if (safe_create_leading_directories(graph_name)) {
905 UNLEAK(graph_name);
906 die_errno(_("unable to create leading directories of %s"),
907 graph_name);
910 hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR);
911 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
913 hashwrite_be32(f, GRAPH_SIGNATURE);
915 hashwrite_u8(f, GRAPH_VERSION);
916 hashwrite_u8(f, GRAPH_OID_VERSION);
917 hashwrite_u8(f, num_chunks);
918 hashwrite_u8(f, 0); /* unused padding byte */
920 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
921 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
922 chunk_ids[2] = GRAPH_CHUNKID_DATA;
923 if (num_extra_edges)
924 chunk_ids[3] = GRAPH_CHUNKID_LARGEEDGES;
925 else
926 chunk_ids[3] = 0;
927 chunk_ids[4] = 0;
929 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
930 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
931 chunk_offsets[2] = chunk_offsets[1] + GRAPH_OID_LEN * commits.nr;
932 chunk_offsets[3] = chunk_offsets[2] + (GRAPH_OID_LEN + 16) * commits.nr;
933 chunk_offsets[4] = chunk_offsets[3] + 4 * num_extra_edges;
935 for (i = 0; i <= num_chunks; i++) {
936 uint32_t chunk_write[3];
938 chunk_write[0] = htonl(chunk_ids[i]);
939 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
940 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
941 hashwrite(f, chunk_write, 12);
944 write_graph_chunk_fanout(f, commits.list, commits.nr);
945 write_graph_chunk_oids(f, GRAPH_OID_LEN, commits.list, commits.nr);
946 write_graph_chunk_data(f, GRAPH_OID_LEN, commits.list, commits.nr);
947 write_graph_chunk_large_edges(f, commits.list, commits.nr);
949 close_commit_graph(the_repository);
950 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
951 commit_lock_file(&lk);
953 free(graph_name);
954 free(commits.list);
955 free(oids.list);
958 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
959 static int verify_commit_graph_error;
961 static void graph_report(const char *fmt, ...)
963 va_list ap;
965 verify_commit_graph_error = 1;
966 va_start(ap, fmt);
967 vfprintf(stderr, fmt, ap);
968 fprintf(stderr, "\n");
969 va_end(ap);
972 #define GENERATION_ZERO_EXISTS 1
973 #define GENERATION_NUMBER_EXISTS 2
975 int verify_commit_graph(struct repository *r, struct commit_graph *g)
977 uint32_t i, cur_fanout_pos = 0;
978 struct object_id prev_oid, cur_oid, checksum;
979 int generation_zero = 0;
980 struct hashfile *f;
981 int devnull;
982 struct progress *progress = NULL;
984 if (!g) {
985 graph_report("no commit-graph file loaded");
986 return 1;
989 verify_commit_graph_error = 0;
991 if (!g->chunk_oid_fanout)
992 graph_report("commit-graph is missing the OID Fanout chunk");
993 if (!g->chunk_oid_lookup)
994 graph_report("commit-graph is missing the OID Lookup chunk");
995 if (!g->chunk_commit_data)
996 graph_report("commit-graph is missing the Commit Data chunk");
998 if (verify_commit_graph_error)
999 return verify_commit_graph_error;
1001 devnull = open("/dev/null", O_WRONLY);
1002 f = hashfd(devnull, NULL);
1003 hashwrite(f, g->data, g->data_len - g->hash_len);
1004 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
1005 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
1006 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
1007 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
1010 for (i = 0; i < g->num_commits; i++) {
1011 struct commit *graph_commit;
1013 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1015 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
1016 graph_report("commit-graph has incorrect OID order: %s then %s",
1017 oid_to_hex(&prev_oid),
1018 oid_to_hex(&cur_oid));
1020 oidcpy(&prev_oid, &cur_oid);
1022 while (cur_oid.hash[0] > cur_fanout_pos) {
1023 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1025 if (i != fanout_value)
1026 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
1027 cur_fanout_pos, fanout_value, i);
1028 cur_fanout_pos++;
1031 graph_commit = lookup_commit(r, &cur_oid);
1032 if (!parse_commit_in_graph_one(g, graph_commit))
1033 graph_report("failed to parse %s from commit-graph",
1034 oid_to_hex(&cur_oid));
1037 while (cur_fanout_pos < 256) {
1038 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1040 if (g->num_commits != fanout_value)
1041 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
1042 cur_fanout_pos, fanout_value, i);
1044 cur_fanout_pos++;
1047 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
1048 return verify_commit_graph_error;
1050 progress = start_progress(_("Verifying commits in commit graph"),
1051 g->num_commits);
1052 for (i = 0; i < g->num_commits; i++) {
1053 struct commit *graph_commit, *odb_commit;
1054 struct commit_list *graph_parents, *odb_parents;
1055 uint32_t max_generation = 0;
1057 display_progress(progress, i + 1);
1058 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1060 graph_commit = lookup_commit(r, &cur_oid);
1061 odb_commit = (struct commit *)create_object(r, cur_oid.hash, alloc_commit_node(r));
1062 if (parse_commit_internal(odb_commit, 0, 0)) {
1063 graph_report("failed to parse %s from object database",
1064 oid_to_hex(&cur_oid));
1065 continue;
1068 if (!oideq(&get_commit_tree_in_graph_one(g, graph_commit)->object.oid,
1069 get_commit_tree_oid(odb_commit)))
1070 graph_report("root tree OID for commit %s in commit-graph is %s != %s",
1071 oid_to_hex(&cur_oid),
1072 oid_to_hex(get_commit_tree_oid(graph_commit)),
1073 oid_to_hex(get_commit_tree_oid(odb_commit)));
1075 graph_parents = graph_commit->parents;
1076 odb_parents = odb_commit->parents;
1078 while (graph_parents) {
1079 if (odb_parents == NULL) {
1080 graph_report("commit-graph parent list for commit %s is too long",
1081 oid_to_hex(&cur_oid));
1082 break;
1085 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
1086 graph_report("commit-graph parent for %s is %s != %s",
1087 oid_to_hex(&cur_oid),
1088 oid_to_hex(&graph_parents->item->object.oid),
1089 oid_to_hex(&odb_parents->item->object.oid));
1091 if (graph_parents->item->generation > max_generation)
1092 max_generation = graph_parents->item->generation;
1094 graph_parents = graph_parents->next;
1095 odb_parents = odb_parents->next;
1098 if (odb_parents != NULL)
1099 graph_report("commit-graph parent list for commit %s terminates early",
1100 oid_to_hex(&cur_oid));
1102 if (!graph_commit->generation) {
1103 if (generation_zero == GENERATION_NUMBER_EXISTS)
1104 graph_report("commit-graph has generation number zero for commit %s, but non-zero elsewhere",
1105 oid_to_hex(&cur_oid));
1106 generation_zero = GENERATION_ZERO_EXISTS;
1107 } else if (generation_zero == GENERATION_ZERO_EXISTS)
1108 graph_report("commit-graph has non-zero generation number for commit %s, but zero elsewhere",
1109 oid_to_hex(&cur_oid));
1111 if (generation_zero == GENERATION_ZERO_EXISTS)
1112 continue;
1115 * If one of our parents has generation GENERATION_NUMBER_MAX, then
1116 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
1117 * extra logic in the following condition.
1119 if (max_generation == GENERATION_NUMBER_MAX)
1120 max_generation--;
1122 if (graph_commit->generation != max_generation + 1)
1123 graph_report("commit-graph generation for commit %s is %u != %u",
1124 oid_to_hex(&cur_oid),
1125 graph_commit->generation,
1126 max_generation + 1);
1128 if (graph_commit->date != odb_commit->date)
1129 graph_report("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime,
1130 oid_to_hex(&cur_oid),
1131 graph_commit->date,
1132 odb_commit->date);
1134 stop_progress(&progress);
1136 return verify_commit_graph_error;
1139 void free_commit_graph(struct commit_graph *g)
1141 if (!g)
1142 return;
1143 if (g->graph_fd >= 0) {
1144 munmap((void *)g->data, g->data_len);
1145 g->data = NULL;
1146 close(g->graph_fd);
1148 free(g);