commit-graph: close descriptors after mmap
[git/raj.git] / commit-graph.c
blobac6ba5af4578dbd693abcb7d17b455cdc7402807
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_EXTRAEDGES 0x45444745 /* "EDGE" */
25 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
27 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
29 #define GRAPH_VERSION_1 0x1
30 #define GRAPH_VERSION GRAPH_VERSION_1
32 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
33 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
34 #define GRAPH_PARENT_NONE 0x70000000
36 #define GRAPH_LAST_EDGE 0x80000000
38 #define GRAPH_HEADER_SIZE 8
39 #define GRAPH_FANOUT_SIZE (4 * 256)
40 #define GRAPH_CHUNKLOOKUP_WIDTH 12
41 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
42 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
44 /* Remember to update object flag allocation in object.h */
45 #define REACHABLE (1u<<15)
47 char *get_commit_graph_filename(struct object_directory *odb)
49 return xstrfmt("%s/info/commit-graph", odb->path);
52 static char *get_split_graph_filename(struct object_directory *odb,
53 const char *oid_hex)
55 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
56 oid_hex);
59 static char *get_chain_filename(struct object_directory *odb)
61 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
64 static uint8_t oid_version(void)
66 return 1;
69 static struct commit_graph *alloc_commit_graph(void)
71 struct commit_graph *g = xcalloc(1, sizeof(*g));
73 return g;
76 extern int read_replace_refs;
78 static int commit_graph_compatible(struct repository *r)
80 if (!r->gitdir)
81 return 0;
83 if (read_replace_refs) {
84 prepare_replace_object(r);
85 if (hashmap_get_size(&r->objects->replace_map->map))
86 return 0;
89 prepare_commit_graft(r);
90 if (r->parsed_objects && r->parsed_objects->grafts_nr)
91 return 0;
92 if (is_repository_shallow(r))
93 return 0;
95 return 1;
98 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
100 *fd = git_open(graph_file);
101 if (*fd < 0)
102 return 0;
103 if (fstat(*fd, st)) {
104 close(*fd);
105 return 0;
107 return 1;
110 struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st,
111 struct object_directory *odb)
113 void *graph_map;
114 size_t graph_size;
115 struct commit_graph *ret;
117 graph_size = xsize_t(st->st_size);
119 if (graph_size < GRAPH_MIN_SIZE) {
120 close(fd);
121 error(_("commit-graph file is too small"));
122 return NULL;
124 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
125 close(fd);
126 ret = parse_commit_graph(graph_map, graph_size);
128 if (ret)
129 ret->odb = odb;
130 else
131 munmap(graph_map, graph_size);
133 return ret;
136 static int verify_commit_graph_lite(struct commit_graph *g)
139 * Basic validation shared between parse_commit_graph()
140 * which'll be called every time the graph is used, and the
141 * much more expensive verify_commit_graph() used by
142 * "commit-graph verify".
144 * There should only be very basic checks here to ensure that
145 * we don't e.g. segfault in fill_commit_in_graph(), but
146 e because this is a very hot codepath nothing that e.g. loops
147 * over g->num_commits, or runs a checksum on the commit-graph
148 * itself.
150 if (!g->chunk_oid_fanout) {
151 error("commit-graph is missing the OID Fanout chunk");
152 return 1;
154 if (!g->chunk_oid_lookup) {
155 error("commit-graph is missing the OID Lookup chunk");
156 return 1;
158 if (!g->chunk_commit_data) {
159 error("commit-graph is missing the Commit Data chunk");
160 return 1;
163 return 0;
166 struct commit_graph *parse_commit_graph(void *graph_map, size_t graph_size)
168 const unsigned char *data, *chunk_lookup;
169 uint32_t i;
170 struct commit_graph *graph;
171 uint64_t last_chunk_offset;
172 uint32_t last_chunk_id;
173 uint32_t graph_signature;
174 unsigned char graph_version, hash_version;
176 if (!graph_map)
177 return NULL;
179 if (graph_size < GRAPH_MIN_SIZE)
180 return NULL;
182 data = (const unsigned char *)graph_map;
184 graph_signature = get_be32(data);
185 if (graph_signature != GRAPH_SIGNATURE) {
186 error(_("commit-graph signature %X does not match signature %X"),
187 graph_signature, GRAPH_SIGNATURE);
188 return NULL;
191 graph_version = *(unsigned char*)(data + 4);
192 if (graph_version != GRAPH_VERSION) {
193 error(_("commit-graph version %X does not match version %X"),
194 graph_version, GRAPH_VERSION);
195 return NULL;
198 hash_version = *(unsigned char*)(data + 5);
199 if (hash_version != oid_version()) {
200 error(_("commit-graph hash version %X does not match version %X"),
201 hash_version, oid_version());
202 return NULL;
205 graph = alloc_commit_graph();
207 graph->hash_len = the_hash_algo->rawsz;
208 graph->num_chunks = *(unsigned char*)(data + 6);
209 graph->data = graph_map;
210 graph->data_len = graph_size;
212 last_chunk_id = 0;
213 last_chunk_offset = 8;
214 chunk_lookup = data + 8;
215 for (i = 0; i < graph->num_chunks; i++) {
216 uint32_t chunk_id;
217 uint64_t chunk_offset;
218 int chunk_repeated = 0;
220 if (data + graph_size - chunk_lookup <
221 GRAPH_CHUNKLOOKUP_WIDTH) {
222 error(_("commit-graph chunk lookup table entry missing; file may be incomplete"));
223 free(graph);
224 return NULL;
227 chunk_id = get_be32(chunk_lookup + 0);
228 chunk_offset = get_be64(chunk_lookup + 4);
230 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
232 if (chunk_offset > graph_size - the_hash_algo->rawsz) {
233 error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
234 (uint32_t)chunk_offset);
235 free(graph);
236 return NULL;
239 switch (chunk_id) {
240 case GRAPH_CHUNKID_OIDFANOUT:
241 if (graph->chunk_oid_fanout)
242 chunk_repeated = 1;
243 else
244 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
245 break;
247 case GRAPH_CHUNKID_OIDLOOKUP:
248 if (graph->chunk_oid_lookup)
249 chunk_repeated = 1;
250 else
251 graph->chunk_oid_lookup = data + chunk_offset;
252 break;
254 case GRAPH_CHUNKID_DATA:
255 if (graph->chunk_commit_data)
256 chunk_repeated = 1;
257 else
258 graph->chunk_commit_data = data + chunk_offset;
259 break;
261 case GRAPH_CHUNKID_EXTRAEDGES:
262 if (graph->chunk_extra_edges)
263 chunk_repeated = 1;
264 else
265 graph->chunk_extra_edges = data + chunk_offset;
266 break;
268 case GRAPH_CHUNKID_BASE:
269 if (graph->chunk_base_graphs)
270 chunk_repeated = 1;
271 else
272 graph->chunk_base_graphs = data + chunk_offset;
275 if (chunk_repeated) {
276 error(_("commit-graph chunk id %08x appears multiple times"), chunk_id);
277 free(graph);
278 return NULL;
281 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
283 graph->num_commits = (chunk_offset - last_chunk_offset)
284 / graph->hash_len;
287 last_chunk_id = chunk_id;
288 last_chunk_offset = chunk_offset;
291 hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
293 if (verify_commit_graph_lite(graph)) {
294 free(graph);
295 return NULL;
298 return graph;
301 static struct commit_graph *load_commit_graph_one(const char *graph_file,
302 struct object_directory *odb)
305 struct stat st;
306 int fd;
307 struct commit_graph *g;
308 int open_ok = open_commit_graph(graph_file, &fd, &st);
310 if (!open_ok)
311 return NULL;
313 g = load_commit_graph_one_fd_st(fd, &st, odb);
315 if (g)
316 g->filename = xstrdup(graph_file);
318 return g;
321 static struct commit_graph *load_commit_graph_v1(struct repository *r,
322 struct object_directory *odb)
324 char *graph_name = get_commit_graph_filename(odb);
325 struct commit_graph *g = load_commit_graph_one(graph_name, odb);
326 free(graph_name);
328 return g;
331 static int add_graph_to_chain(struct commit_graph *g,
332 struct commit_graph *chain,
333 struct object_id *oids,
334 int n)
336 struct commit_graph *cur_g = chain;
338 if (n && !g->chunk_base_graphs) {
339 warning(_("commit-graph has no base graphs chunk"));
340 return 0;
343 while (n) {
344 n--;
346 if (!cur_g ||
347 !oideq(&oids[n], &cur_g->oid) ||
348 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
349 warning(_("commit-graph chain does not match"));
350 return 0;
353 cur_g = cur_g->base_graph;
356 g->base_graph = chain;
358 if (chain)
359 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
361 return 1;
364 static struct commit_graph *load_commit_graph_chain(struct repository *r,
365 struct object_directory *odb)
367 struct commit_graph *graph_chain = NULL;
368 struct strbuf line = STRBUF_INIT;
369 struct stat st;
370 struct object_id *oids;
371 int i = 0, valid = 1, count;
372 char *chain_name = get_chain_filename(odb);
373 FILE *fp;
374 int stat_res;
376 fp = fopen(chain_name, "r");
377 stat_res = stat(chain_name, &st);
378 free(chain_name);
380 if (!fp ||
381 stat_res ||
382 st.st_size <= the_hash_algo->hexsz)
383 return NULL;
385 count = st.st_size / (the_hash_algo->hexsz + 1);
386 oids = xcalloc(count, sizeof(struct object_id));
388 prepare_alt_odb(r);
390 for (i = 0; i < count; i++) {
391 struct object_directory *odb;
393 if (strbuf_getline_lf(&line, fp) == EOF)
394 break;
396 if (get_oid_hex(line.buf, &oids[i])) {
397 warning(_("invalid commit-graph chain: line '%s' not a hash"),
398 line.buf);
399 valid = 0;
400 break;
403 valid = 0;
404 for (odb = r->objects->odb; odb; odb = odb->next) {
405 char *graph_name = get_split_graph_filename(odb, line.buf);
406 struct commit_graph *g = load_commit_graph_one(graph_name, odb);
408 free(graph_name);
410 if (g) {
411 if (add_graph_to_chain(g, graph_chain, oids, i)) {
412 graph_chain = g;
413 valid = 1;
416 break;
420 if (!valid) {
421 warning(_("unable to find all commit-graph files"));
422 break;
426 free(oids);
427 fclose(fp);
428 strbuf_release(&line);
430 return graph_chain;
433 struct commit_graph *read_commit_graph_one(struct repository *r,
434 struct object_directory *odb)
436 struct commit_graph *g = load_commit_graph_v1(r, odb);
438 if (!g)
439 g = load_commit_graph_chain(r, odb);
441 return g;
444 static void prepare_commit_graph_one(struct repository *r,
445 struct object_directory *odb)
448 if (r->objects->commit_graph)
449 return;
451 r->objects->commit_graph = read_commit_graph_one(r, odb);
455 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
457 * On the first invocation, this function attempts to load the commit
458 * graph if the_repository is configured to have one.
460 static int prepare_commit_graph(struct repository *r)
462 struct object_directory *odb;
465 * This must come before the "already attempted?" check below, because
466 * we want to disable even an already-loaded graph file.
468 if (r->commit_graph_disabled)
469 return 0;
471 if (r->objects->commit_graph_attempted)
472 return !!r->objects->commit_graph;
473 r->objects->commit_graph_attempted = 1;
475 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD, 0))
476 die("dying as requested by the '%s' variable on commit-graph load!",
477 GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD);
479 prepare_repo_settings(r);
481 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
482 r->settings.core_commit_graph != 1)
484 * This repository is not configured to use commit graphs, so
485 * do not load one. (But report commit_graph_attempted anyway
486 * so that commit graph loading is not attempted again for this
487 * repository.)
489 return 0;
491 if (!commit_graph_compatible(r))
492 return 0;
494 prepare_alt_odb(r);
495 for (odb = r->objects->odb;
496 !r->objects->commit_graph && odb;
497 odb = odb->next)
498 prepare_commit_graph_one(r, odb);
499 return !!r->objects->commit_graph;
502 int generation_numbers_enabled(struct repository *r)
504 uint32_t first_generation;
505 struct commit_graph *g;
506 if (!prepare_commit_graph(r))
507 return 0;
509 g = r->objects->commit_graph;
511 if (!g->num_commits)
512 return 0;
514 first_generation = get_be32(g->chunk_commit_data +
515 g->hash_len + 8) >> 2;
517 return !!first_generation;
520 static void close_commit_graph_one(struct commit_graph *g)
522 if (!g)
523 return;
525 close_commit_graph_one(g->base_graph);
526 free_commit_graph(g);
529 void close_commit_graph(struct raw_object_store *o)
531 close_commit_graph_one(o->commit_graph);
532 o->commit_graph = NULL;
535 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
537 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
538 g->chunk_oid_lookup, g->hash_len, pos);
541 static void load_oid_from_graph(struct commit_graph *g,
542 uint32_t pos,
543 struct object_id *oid)
545 uint32_t lex_index;
547 while (g && pos < g->num_commits_in_base)
548 g = g->base_graph;
550 if (!g)
551 BUG("NULL commit-graph");
553 if (pos >= g->num_commits + g->num_commits_in_base)
554 die(_("invalid commit position. commit-graph is likely corrupt"));
556 lex_index = pos - g->num_commits_in_base;
558 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
561 static struct commit_list **insert_parent_or_die(struct repository *r,
562 struct commit_graph *g,
563 uint32_t pos,
564 struct commit_list **pptr)
566 struct commit *c;
567 struct object_id oid;
569 if (pos >= g->num_commits + g->num_commits_in_base)
570 die("invalid parent position %"PRIu32, pos);
572 load_oid_from_graph(g, pos, &oid);
573 c = lookup_commit(r, &oid);
574 if (!c)
575 die(_("could not find commit %s"), oid_to_hex(&oid));
576 c->graph_pos = pos;
577 return &commit_list_insert(c, pptr)->next;
580 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
582 const unsigned char *commit_data;
583 uint32_t lex_index;
585 while (pos < g->num_commits_in_base)
586 g = g->base_graph;
588 lex_index = pos - g->num_commits_in_base;
589 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
590 item->graph_pos = pos;
591 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
594 static inline void set_commit_tree(struct commit *c, struct tree *t)
596 c->maybe_tree = t;
599 static int fill_commit_in_graph(struct repository *r,
600 struct commit *item,
601 struct commit_graph *g, uint32_t pos)
603 uint32_t edge_value;
604 uint32_t *parent_data_ptr;
605 uint64_t date_low, date_high;
606 struct commit_list **pptr;
607 const unsigned char *commit_data;
608 uint32_t lex_index;
610 while (pos < g->num_commits_in_base)
611 g = g->base_graph;
613 if (pos >= g->num_commits + g->num_commits_in_base)
614 die(_("invalid commit position. commit-graph is likely corrupt"));
617 * Store the "full" position, but then use the
618 * "local" position for the rest of the calculation.
620 item->graph_pos = pos;
621 lex_index = pos - g->num_commits_in_base;
623 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
625 item->object.parsed = 1;
627 set_commit_tree(item, NULL);
629 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
630 date_low = get_be32(commit_data + g->hash_len + 12);
631 item->date = (timestamp_t)((date_high << 32) | date_low);
633 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
635 pptr = &item->parents;
637 edge_value = get_be32(commit_data + g->hash_len);
638 if (edge_value == GRAPH_PARENT_NONE)
639 return 1;
640 pptr = insert_parent_or_die(r, g, edge_value, pptr);
642 edge_value = get_be32(commit_data + g->hash_len + 4);
643 if (edge_value == GRAPH_PARENT_NONE)
644 return 1;
645 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
646 pptr = insert_parent_or_die(r, g, edge_value, pptr);
647 return 1;
650 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
651 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
652 do {
653 edge_value = get_be32(parent_data_ptr);
654 pptr = insert_parent_or_die(r, g,
655 edge_value & GRAPH_EDGE_LAST_MASK,
656 pptr);
657 parent_data_ptr++;
658 } while (!(edge_value & GRAPH_LAST_EDGE));
660 return 1;
663 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
665 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
666 *pos = item->graph_pos;
667 return 1;
668 } else {
669 struct commit_graph *cur_g = g;
670 uint32_t lex_index;
672 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
673 cur_g = cur_g->base_graph;
675 if (cur_g) {
676 *pos = lex_index + cur_g->num_commits_in_base;
677 return 1;
680 return 0;
684 static int parse_commit_in_graph_one(struct repository *r,
685 struct commit_graph *g,
686 struct commit *item)
688 uint32_t pos;
690 if (item->object.parsed)
691 return 1;
693 if (find_commit_in_graph(item, g, &pos))
694 return fill_commit_in_graph(r, item, g, pos);
696 return 0;
699 int parse_commit_in_graph(struct repository *r, struct commit *item)
701 if (!prepare_commit_graph(r))
702 return 0;
703 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
706 void load_commit_graph_info(struct repository *r, struct commit *item)
708 uint32_t pos;
709 if (!prepare_commit_graph(r))
710 return;
711 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
712 fill_commit_graph_info(item, r->objects->commit_graph, pos);
715 static struct tree *load_tree_for_commit(struct repository *r,
716 struct commit_graph *g,
717 struct commit *c)
719 struct object_id oid;
720 const unsigned char *commit_data;
722 while (c->graph_pos < g->num_commits_in_base)
723 g = g->base_graph;
725 commit_data = g->chunk_commit_data +
726 GRAPH_DATA_WIDTH * (c->graph_pos - g->num_commits_in_base);
728 hashcpy(oid.hash, commit_data);
729 set_commit_tree(c, lookup_tree(r, &oid));
731 return c->maybe_tree;
734 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
735 struct commit_graph *g,
736 const struct commit *c)
738 if (c->maybe_tree)
739 return c->maybe_tree;
740 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
741 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
743 return load_tree_for_commit(r, g, (struct commit *)c);
746 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
748 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
751 struct packed_commit_list {
752 struct commit **list;
753 int nr;
754 int alloc;
757 struct packed_oid_list {
758 struct object_id *list;
759 int nr;
760 int alloc;
763 struct write_commit_graph_context {
764 struct repository *r;
765 struct object_directory *odb;
766 char *graph_name;
767 struct packed_oid_list oids;
768 struct packed_commit_list commits;
769 int num_extra_edges;
770 unsigned long approx_nr_objects;
771 struct progress *progress;
772 int progress_done;
773 uint64_t progress_cnt;
775 char *base_graph_name;
776 int num_commit_graphs_before;
777 int num_commit_graphs_after;
778 char **commit_graph_filenames_before;
779 char **commit_graph_filenames_after;
780 char **commit_graph_hash_after;
781 uint32_t new_num_commits_in_base;
782 struct commit_graph *new_base_graph;
784 unsigned append:1,
785 report_progress:1,
786 split:1,
787 check_oids:1;
789 const struct split_commit_graph_opts *split_opts;
792 static void write_graph_chunk_fanout(struct hashfile *f,
793 struct write_commit_graph_context *ctx)
795 int i, count = 0;
796 struct commit **list = ctx->commits.list;
799 * Write the first-level table (the list is sorted,
800 * but we use a 256-entry lookup to be able to avoid
801 * having to do eight extra binary search iterations).
803 for (i = 0; i < 256; i++) {
804 while (count < ctx->commits.nr) {
805 if ((*list)->object.oid.hash[0] != i)
806 break;
807 display_progress(ctx->progress, ++ctx->progress_cnt);
808 count++;
809 list++;
812 hashwrite_be32(f, count);
816 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
817 struct write_commit_graph_context *ctx)
819 struct commit **list = ctx->commits.list;
820 int count;
821 for (count = 0; count < ctx->commits.nr; count++, list++) {
822 display_progress(ctx->progress, ++ctx->progress_cnt);
823 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
827 static const unsigned char *commit_to_sha1(size_t index, void *table)
829 struct commit **commits = table;
830 return commits[index]->object.oid.hash;
833 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
834 struct write_commit_graph_context *ctx)
836 struct commit **list = ctx->commits.list;
837 struct commit **last = ctx->commits.list + ctx->commits.nr;
838 uint32_t num_extra_edges = 0;
840 while (list < last) {
841 struct commit_list *parent;
842 struct object_id *tree;
843 int edge_value;
844 uint32_t packedDate[2];
845 display_progress(ctx->progress, ++ctx->progress_cnt);
847 if (parse_commit_no_graph(*list))
848 die(_("unable to parse commit %s"),
849 oid_to_hex(&(*list)->object.oid));
850 tree = get_commit_tree_oid(*list);
851 hashwrite(f, tree->hash, hash_len);
853 parent = (*list)->parents;
855 if (!parent)
856 edge_value = GRAPH_PARENT_NONE;
857 else {
858 edge_value = sha1_pos(parent->item->object.oid.hash,
859 ctx->commits.list,
860 ctx->commits.nr,
861 commit_to_sha1);
863 if (edge_value >= 0)
864 edge_value += ctx->new_num_commits_in_base;
865 else if (ctx->new_base_graph) {
866 uint32_t pos;
867 if (find_commit_in_graph(parent->item,
868 ctx->new_base_graph,
869 &pos))
870 edge_value = pos;
873 if (edge_value < 0)
874 BUG("missing parent %s for commit %s",
875 oid_to_hex(&parent->item->object.oid),
876 oid_to_hex(&(*list)->object.oid));
879 hashwrite_be32(f, edge_value);
881 if (parent)
882 parent = parent->next;
884 if (!parent)
885 edge_value = GRAPH_PARENT_NONE;
886 else if (parent->next)
887 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
888 else {
889 edge_value = sha1_pos(parent->item->object.oid.hash,
890 ctx->commits.list,
891 ctx->commits.nr,
892 commit_to_sha1);
894 if (edge_value >= 0)
895 edge_value += ctx->new_num_commits_in_base;
896 else if (ctx->new_base_graph) {
897 uint32_t pos;
898 if (find_commit_in_graph(parent->item,
899 ctx->new_base_graph,
900 &pos))
901 edge_value = pos;
904 if (edge_value < 0)
905 BUG("missing parent %s for commit %s",
906 oid_to_hex(&parent->item->object.oid),
907 oid_to_hex(&(*list)->object.oid));
910 hashwrite_be32(f, edge_value);
912 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
913 do {
914 num_extra_edges++;
915 parent = parent->next;
916 } while (parent);
919 if (sizeof((*list)->date) > 4)
920 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
921 else
922 packedDate[0] = 0;
924 packedDate[0] |= htonl((*list)->generation << 2);
926 packedDate[1] = htonl((*list)->date);
927 hashwrite(f, packedDate, 8);
929 list++;
933 static void write_graph_chunk_extra_edges(struct hashfile *f,
934 struct write_commit_graph_context *ctx)
936 struct commit **list = ctx->commits.list;
937 struct commit **last = ctx->commits.list + ctx->commits.nr;
938 struct commit_list *parent;
940 while (list < last) {
941 int num_parents = 0;
943 display_progress(ctx->progress, ++ctx->progress_cnt);
945 for (parent = (*list)->parents; num_parents < 3 && parent;
946 parent = parent->next)
947 num_parents++;
949 if (num_parents <= 2) {
950 list++;
951 continue;
954 /* Since num_parents > 2, this initializer is safe. */
955 for (parent = (*list)->parents->next; parent; parent = parent->next) {
956 int edge_value = sha1_pos(parent->item->object.oid.hash,
957 ctx->commits.list,
958 ctx->commits.nr,
959 commit_to_sha1);
961 if (edge_value >= 0)
962 edge_value += ctx->new_num_commits_in_base;
963 else if (ctx->new_base_graph) {
964 uint32_t pos;
965 if (find_commit_in_graph(parent->item,
966 ctx->new_base_graph,
967 &pos))
968 edge_value = pos;
971 if (edge_value < 0)
972 BUG("missing parent %s for commit %s",
973 oid_to_hex(&parent->item->object.oid),
974 oid_to_hex(&(*list)->object.oid));
975 else if (!parent->next)
976 edge_value |= GRAPH_LAST_EDGE;
978 hashwrite_be32(f, edge_value);
981 list++;
985 static int oid_compare(const void *_a, const void *_b)
987 const struct object_id *a = (const struct object_id *)_a;
988 const struct object_id *b = (const struct object_id *)_b;
989 return oidcmp(a, b);
992 static int add_packed_commits(const struct object_id *oid,
993 struct packed_git *pack,
994 uint32_t pos,
995 void *data)
997 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
998 enum object_type type;
999 off_t offset = nth_packed_object_offset(pack, pos);
1000 struct object_info oi = OBJECT_INFO_INIT;
1002 if (ctx->progress)
1003 display_progress(ctx->progress, ++ctx->progress_done);
1005 oi.typep = &type;
1006 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1007 die(_("unable to get type of object %s"), oid_to_hex(oid));
1009 if (type != OBJ_COMMIT)
1010 return 0;
1012 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1013 oidcpy(&(ctx->oids.list[ctx->oids.nr]), oid);
1014 ctx->oids.nr++;
1016 return 0;
1019 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1021 struct commit_list *parent;
1022 for (parent = commit->parents; parent; parent = parent->next) {
1023 if (!(parent->item->object.flags & REACHABLE)) {
1024 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1025 oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid));
1026 ctx->oids.nr++;
1027 parent->item->object.flags |= REACHABLE;
1032 static void close_reachable(struct write_commit_graph_context *ctx)
1034 int i;
1035 struct commit *commit;
1036 enum commit_graph_split_flags flags = ctx->split_opts ?
1037 ctx->split_opts->flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1039 if (ctx->report_progress)
1040 ctx->progress = start_delayed_progress(
1041 _("Loading known commits in commit graph"),
1042 ctx->oids.nr);
1043 for (i = 0; i < ctx->oids.nr; i++) {
1044 display_progress(ctx->progress, i + 1);
1045 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1046 if (commit)
1047 commit->object.flags |= REACHABLE;
1049 stop_progress(&ctx->progress);
1052 * As this loop runs, ctx->oids.nr may grow, but not more
1053 * than the number of missing commits in the reachable
1054 * closure.
1056 if (ctx->report_progress)
1057 ctx->progress = start_delayed_progress(
1058 _("Expanding reachable commits in commit graph"),
1060 for (i = 0; i < ctx->oids.nr; i++) {
1061 display_progress(ctx->progress, i + 1);
1062 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1064 if (!commit)
1065 continue;
1066 if (ctx->split) {
1067 if ((!parse_commit(commit) &&
1068 commit->graph_pos == COMMIT_NOT_FROM_GRAPH) ||
1069 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1070 add_missing_parents(ctx, commit);
1071 } else if (!parse_commit_no_graph(commit))
1072 add_missing_parents(ctx, commit);
1074 stop_progress(&ctx->progress);
1076 if (ctx->report_progress)
1077 ctx->progress = start_delayed_progress(
1078 _("Clearing commit marks in commit graph"),
1079 ctx->oids.nr);
1080 for (i = 0; i < ctx->oids.nr; i++) {
1081 display_progress(ctx->progress, i + 1);
1082 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1084 if (commit)
1085 commit->object.flags &= ~REACHABLE;
1087 stop_progress(&ctx->progress);
1090 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1092 int i;
1093 struct commit_list *list = NULL;
1095 if (ctx->report_progress)
1096 ctx->progress = start_delayed_progress(
1097 _("Computing commit graph generation numbers"),
1098 ctx->commits.nr);
1099 for (i = 0; i < ctx->commits.nr; i++) {
1100 display_progress(ctx->progress, i + 1);
1101 if (ctx->commits.list[i]->generation != GENERATION_NUMBER_INFINITY &&
1102 ctx->commits.list[i]->generation != GENERATION_NUMBER_ZERO)
1103 continue;
1105 commit_list_insert(ctx->commits.list[i], &list);
1106 while (list) {
1107 struct commit *current = list->item;
1108 struct commit_list *parent;
1109 int all_parents_computed = 1;
1110 uint32_t max_generation = 0;
1112 for (parent = current->parents; parent; parent = parent->next) {
1113 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
1114 parent->item->generation == GENERATION_NUMBER_ZERO) {
1115 all_parents_computed = 0;
1116 commit_list_insert(parent->item, &list);
1117 break;
1118 } else if (parent->item->generation > max_generation) {
1119 max_generation = parent->item->generation;
1123 if (all_parents_computed) {
1124 current->generation = max_generation + 1;
1125 pop_commit(&list);
1127 if (current->generation > GENERATION_NUMBER_MAX)
1128 current->generation = GENERATION_NUMBER_MAX;
1132 stop_progress(&ctx->progress);
1135 static int add_ref_to_set(const char *refname,
1136 const struct object_id *oid,
1137 int flags, void *cb_data)
1139 struct oidset *commits = (struct oidset *)cb_data;
1141 oidset_insert(commits, oid);
1142 return 0;
1145 int write_commit_graph_reachable(struct object_directory *odb,
1146 enum commit_graph_write_flags flags,
1147 const struct split_commit_graph_opts *split_opts)
1149 struct oidset commits = OIDSET_INIT;
1150 int result;
1152 for_each_ref(add_ref_to_set, &commits);
1153 result = write_commit_graph(odb, NULL, &commits,
1154 flags, split_opts);
1156 oidset_clear(&commits);
1157 return result;
1160 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1161 struct string_list *pack_indexes)
1163 uint32_t i;
1164 struct strbuf progress_title = STRBUF_INIT;
1165 struct strbuf packname = STRBUF_INIT;
1166 int dirlen;
1168 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1169 dirlen = packname.len;
1170 if (ctx->report_progress) {
1171 strbuf_addf(&progress_title,
1172 Q_("Finding commits for commit graph in %d pack",
1173 "Finding commits for commit graph in %d packs",
1174 pack_indexes->nr),
1175 pack_indexes->nr);
1176 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1177 ctx->progress_done = 0;
1179 for (i = 0; i < pack_indexes->nr; i++) {
1180 struct packed_git *p;
1181 strbuf_setlen(&packname, dirlen);
1182 strbuf_addstr(&packname, pack_indexes->items[i].string);
1183 p = add_packed_git(packname.buf, packname.len, 1);
1184 if (!p) {
1185 error(_("error adding pack %s"), packname.buf);
1186 return -1;
1188 if (open_pack_index(p)) {
1189 error(_("error opening index for %s"), packname.buf);
1190 return -1;
1192 for_each_object_in_pack(p, add_packed_commits, ctx,
1193 FOR_EACH_OBJECT_PACK_ORDER);
1194 close_pack(p);
1195 free(p);
1198 stop_progress(&ctx->progress);
1199 strbuf_release(&progress_title);
1200 strbuf_release(&packname);
1202 return 0;
1205 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1206 struct oidset *commits)
1208 uint32_t i = 0;
1209 struct strbuf progress_title = STRBUF_INIT;
1210 struct oidset_iter iter;
1211 struct object_id *oid;
1213 if (!oidset_size(commits))
1214 return 0;
1216 if (ctx->report_progress) {
1217 strbuf_addf(&progress_title,
1218 Q_("Finding commits for commit graph from %d ref",
1219 "Finding commits for commit graph from %d refs",
1220 oidset_size(commits)),
1221 oidset_size(commits));
1222 ctx->progress = start_delayed_progress(
1223 progress_title.buf,
1224 oidset_size(commits));
1227 oidset_iter_init(commits, &iter);
1228 while ((oid = oidset_iter_next(&iter))) {
1229 struct commit *result;
1231 display_progress(ctx->progress, ++i);
1233 result = lookup_commit_reference_gently(ctx->r, oid, 1);
1234 if (result) {
1235 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1236 oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid));
1237 ctx->oids.nr++;
1238 } else if (ctx->check_oids) {
1239 error(_("invalid commit object id: %s"),
1240 oid_to_hex(oid));
1241 return -1;
1245 stop_progress(&ctx->progress);
1246 strbuf_release(&progress_title);
1248 return 0;
1251 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1253 if (ctx->report_progress)
1254 ctx->progress = start_delayed_progress(
1255 _("Finding commits for commit graph among packed objects"),
1256 ctx->approx_nr_objects);
1257 for_each_packed_object(add_packed_commits, ctx,
1258 FOR_EACH_OBJECT_PACK_ORDER);
1259 if (ctx->progress_done < ctx->approx_nr_objects)
1260 display_progress(ctx->progress, ctx->approx_nr_objects);
1261 stop_progress(&ctx->progress);
1264 static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
1266 uint32_t i, count_distinct = 1;
1268 if (ctx->report_progress)
1269 ctx->progress = start_delayed_progress(
1270 _("Counting distinct commits in commit graph"),
1271 ctx->oids.nr);
1272 display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
1273 QSORT(ctx->oids.list, ctx->oids.nr, oid_compare);
1275 for (i = 1; i < ctx->oids.nr; i++) {
1276 display_progress(ctx->progress, i + 1);
1277 if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i])) {
1278 if (ctx->split) {
1279 struct commit *c = lookup_commit(ctx->r, &ctx->oids.list[i]);
1281 if (!c || c->graph_pos != COMMIT_NOT_FROM_GRAPH)
1282 continue;
1285 count_distinct++;
1288 stop_progress(&ctx->progress);
1290 return count_distinct;
1293 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1295 uint32_t i;
1296 enum commit_graph_split_flags flags = ctx->split_opts ?
1297 ctx->split_opts->flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1299 ctx->num_extra_edges = 0;
1300 if (ctx->report_progress)
1301 ctx->progress = start_delayed_progress(
1302 _("Finding extra edges in commit graph"),
1303 ctx->oids.nr);
1304 for (i = 0; i < ctx->oids.nr; i++) {
1305 unsigned int num_parents;
1307 display_progress(ctx->progress, i + 1);
1308 if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
1309 continue;
1311 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1312 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.list[i]);
1314 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1315 ctx->commits.list[ctx->commits.nr]->graph_pos != COMMIT_NOT_FROM_GRAPH)
1316 continue;
1318 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1319 parse_commit(ctx->commits.list[ctx->commits.nr]);
1320 else
1321 parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
1323 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1324 if (num_parents > 2)
1325 ctx->num_extra_edges += num_parents - 1;
1327 ctx->commits.nr++;
1329 stop_progress(&ctx->progress);
1332 static int write_graph_chunk_base_1(struct hashfile *f,
1333 struct commit_graph *g)
1335 int num = 0;
1337 if (!g)
1338 return 0;
1340 num = write_graph_chunk_base_1(f, g->base_graph);
1341 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1342 return num + 1;
1345 static int write_graph_chunk_base(struct hashfile *f,
1346 struct write_commit_graph_context *ctx)
1348 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1350 if (num != ctx->num_commit_graphs_after - 1) {
1351 error(_("failed to write correct number of base graph ids"));
1352 return -1;
1355 return 0;
1358 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1360 uint32_t i;
1361 int fd;
1362 struct hashfile *f;
1363 struct lock_file lk = LOCK_INIT;
1364 uint32_t chunk_ids[6];
1365 uint64_t chunk_offsets[6];
1366 const unsigned hashsz = the_hash_algo->rawsz;
1367 struct strbuf progress_title = STRBUF_INIT;
1368 int num_chunks = 3;
1369 struct object_id file_hash;
1371 if (ctx->split) {
1372 struct strbuf tmp_file = STRBUF_INIT;
1374 strbuf_addf(&tmp_file,
1375 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1376 ctx->odb->path);
1377 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1378 } else {
1379 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1382 if (safe_create_leading_directories(ctx->graph_name)) {
1383 UNLEAK(ctx->graph_name);
1384 error(_("unable to create leading directories of %s"),
1385 ctx->graph_name);
1386 return -1;
1389 if (ctx->split) {
1390 char *lock_name = get_chain_filename(ctx->odb);
1392 hold_lock_file_for_update(&lk, lock_name, LOCK_DIE_ON_ERROR);
1394 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1395 if (fd < 0) {
1396 error(_("unable to create temporary graph layer"));
1397 return -1;
1400 f = hashfd(fd, ctx->graph_name);
1401 } else {
1402 hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR);
1403 fd = lk.tempfile->fd;
1404 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1407 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
1408 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
1409 chunk_ids[2] = GRAPH_CHUNKID_DATA;
1410 if (ctx->num_extra_edges) {
1411 chunk_ids[num_chunks] = GRAPH_CHUNKID_EXTRAEDGES;
1412 num_chunks++;
1414 if (ctx->num_commit_graphs_after > 1) {
1415 chunk_ids[num_chunks] = GRAPH_CHUNKID_BASE;
1416 num_chunks++;
1419 chunk_ids[num_chunks] = 0;
1421 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1422 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
1423 chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
1424 chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
1426 num_chunks = 3;
1427 if (ctx->num_extra_edges) {
1428 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1429 4 * ctx->num_extra_edges;
1430 num_chunks++;
1432 if (ctx->num_commit_graphs_after > 1) {
1433 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1434 hashsz * (ctx->num_commit_graphs_after - 1);
1435 num_chunks++;
1438 hashwrite_be32(f, GRAPH_SIGNATURE);
1440 hashwrite_u8(f, GRAPH_VERSION);
1441 hashwrite_u8(f, oid_version());
1442 hashwrite_u8(f, num_chunks);
1443 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1445 for (i = 0; i <= num_chunks; i++) {
1446 uint32_t chunk_write[3];
1448 chunk_write[0] = htonl(chunk_ids[i]);
1449 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
1450 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
1451 hashwrite(f, chunk_write, 12);
1454 if (ctx->report_progress) {
1455 strbuf_addf(&progress_title,
1456 Q_("Writing out commit graph in %d pass",
1457 "Writing out commit graph in %d passes",
1458 num_chunks),
1459 num_chunks);
1460 ctx->progress = start_delayed_progress(
1461 progress_title.buf,
1462 num_chunks * ctx->commits.nr);
1464 write_graph_chunk_fanout(f, ctx);
1465 write_graph_chunk_oids(f, hashsz, ctx);
1466 write_graph_chunk_data(f, hashsz, ctx);
1467 if (ctx->num_extra_edges)
1468 write_graph_chunk_extra_edges(f, ctx);
1469 if (ctx->num_commit_graphs_after > 1 &&
1470 write_graph_chunk_base(f, ctx)) {
1471 return -1;
1473 stop_progress(&ctx->progress);
1474 strbuf_release(&progress_title);
1476 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1477 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1478 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
1480 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1481 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1482 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1483 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1486 close_commit_graph(ctx->r->objects);
1487 finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1489 if (ctx->split) {
1490 FILE *chainf = fdopen_lock_file(&lk, "w");
1491 char *final_graph_name;
1492 int result;
1494 close(fd);
1496 if (!chainf) {
1497 error(_("unable to open commit-graph chain file"));
1498 return -1;
1501 if (ctx->base_graph_name) {
1502 const char *dest;
1503 int idx = ctx->num_commit_graphs_after - 1;
1504 if (ctx->num_commit_graphs_after > 1)
1505 idx--;
1507 dest = ctx->commit_graph_filenames_after[idx];
1509 if (strcmp(ctx->base_graph_name, dest)) {
1510 result = rename(ctx->base_graph_name, dest);
1512 if (result) {
1513 error(_("failed to rename base commit-graph file"));
1514 return -1;
1517 } else {
1518 char *graph_name = get_commit_graph_filename(ctx->odb);
1519 unlink(graph_name);
1522 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
1523 final_graph_name = get_split_graph_filename(ctx->odb,
1524 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1525 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1527 result = rename(ctx->graph_name, final_graph_name);
1529 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1530 fprintf(lk.tempfile->fp, "%s\n", ctx->commit_graph_hash_after[i]);
1532 if (result) {
1533 error(_("failed to rename temporary commit-graph file"));
1534 return -1;
1538 commit_lock_file(&lk);
1540 return 0;
1543 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
1545 struct commit_graph *g;
1546 uint32_t num_commits;
1547 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1548 uint32_t i;
1550 int max_commits = 0;
1551 int size_mult = 2;
1553 if (ctx->split_opts) {
1554 max_commits = ctx->split_opts->max_commits;
1556 if (ctx->split_opts->size_multiple)
1557 size_mult = ctx->split_opts->size_multiple;
1559 flags = ctx->split_opts->flags;
1562 g = ctx->r->objects->commit_graph;
1563 num_commits = ctx->commits.nr;
1564 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
1565 ctx->num_commit_graphs_after = 1;
1566 else
1567 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
1569 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
1570 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
1571 while (g && (g->num_commits <= size_mult * num_commits ||
1572 (max_commits && num_commits > max_commits))) {
1573 if (g->odb != ctx->odb)
1574 break;
1576 num_commits += g->num_commits;
1577 g = g->base_graph;
1579 ctx->num_commit_graphs_after--;
1583 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
1584 ctx->new_base_graph = g;
1585 else if (ctx->num_commit_graphs_after != 1)
1586 BUG("split_graph_merge_strategy: num_commit_graphs_after "
1587 "should be 1 with --split=replace");
1589 if (ctx->num_commit_graphs_after == 2) {
1590 char *old_graph_name = get_commit_graph_filename(g->odb);
1592 if (!strcmp(g->filename, old_graph_name) &&
1593 g->odb != ctx->odb) {
1594 ctx->num_commit_graphs_after = 1;
1595 ctx->new_base_graph = NULL;
1598 free(old_graph_name);
1601 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
1602 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
1604 for (i = 0; i < ctx->num_commit_graphs_after &&
1605 i < ctx->num_commit_graphs_before; i++)
1606 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
1608 i = ctx->num_commit_graphs_before - 1;
1609 g = ctx->r->objects->commit_graph;
1611 while (g) {
1612 if (i < ctx->num_commit_graphs_after)
1613 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
1615 i--;
1616 g = g->base_graph;
1620 static void merge_commit_graph(struct write_commit_graph_context *ctx,
1621 struct commit_graph *g)
1623 uint32_t i;
1624 uint32_t offset = g->num_commits_in_base;
1626 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
1628 for (i = 0; i < g->num_commits; i++) {
1629 struct object_id oid;
1630 struct commit *result;
1632 display_progress(ctx->progress, i + 1);
1634 load_oid_from_graph(g, i + offset, &oid);
1636 /* only add commits if they still exist in the repo */
1637 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
1639 if (result) {
1640 ctx->commits.list[ctx->commits.nr] = result;
1641 ctx->commits.nr++;
1646 static int commit_compare(const void *_a, const void *_b)
1648 const struct commit *a = *(const struct commit **)_a;
1649 const struct commit *b = *(const struct commit **)_b;
1650 return oidcmp(&a->object.oid, &b->object.oid);
1653 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
1655 uint32_t i;
1657 if (ctx->report_progress)
1658 ctx->progress = start_delayed_progress(
1659 _("Scanning merged commits"),
1660 ctx->commits.nr);
1662 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
1664 ctx->num_extra_edges = 0;
1665 for (i = 0; i < ctx->commits.nr; i++) {
1666 display_progress(ctx->progress, i);
1668 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
1669 &ctx->commits.list[i]->object.oid)) {
1670 die(_("unexpected duplicate commit id %s"),
1671 oid_to_hex(&ctx->commits.list[i]->object.oid));
1672 } else {
1673 unsigned int num_parents;
1675 num_parents = commit_list_count(ctx->commits.list[i]->parents);
1676 if (num_parents > 2)
1677 ctx->num_extra_edges += num_parents - 1;
1681 stop_progress(&ctx->progress);
1684 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
1686 struct commit_graph *g = ctx->r->objects->commit_graph;
1687 uint32_t current_graph_number = ctx->num_commit_graphs_before;
1688 struct strbuf progress_title = STRBUF_INIT;
1690 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
1691 current_graph_number--;
1693 if (ctx->report_progress) {
1694 strbuf_addstr(&progress_title, _("Merging commit-graph"));
1695 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1698 merge_commit_graph(ctx, g);
1699 stop_progress(&ctx->progress);
1700 strbuf_release(&progress_title);
1702 g = g->base_graph;
1705 if (g) {
1706 ctx->new_base_graph = g;
1707 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
1710 if (ctx->new_base_graph)
1711 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
1713 sort_and_scan_merged_commits(ctx);
1716 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
1718 uint32_t i;
1719 time_t now = time(NULL);
1721 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
1722 struct stat st;
1723 struct utimbuf updated_time;
1725 stat(ctx->commit_graph_filenames_before[i], &st);
1727 updated_time.actime = st.st_atime;
1728 updated_time.modtime = now;
1729 utime(ctx->commit_graph_filenames_before[i], &updated_time);
1733 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
1735 struct strbuf path = STRBUF_INIT;
1736 DIR *dir;
1737 struct dirent *de;
1738 size_t dirnamelen;
1739 timestamp_t expire_time = time(NULL);
1741 if (ctx->split_opts && ctx->split_opts->expire_time)
1742 expire_time -= ctx->split_opts->expire_time;
1743 if (!ctx->split) {
1744 char *chain_file_name = get_chain_filename(ctx->odb);
1745 unlink(chain_file_name);
1746 free(chain_file_name);
1747 ctx->num_commit_graphs_after = 0;
1750 strbuf_addstr(&path, ctx->odb->path);
1751 strbuf_addstr(&path, "/info/commit-graphs");
1752 dir = opendir(path.buf);
1754 if (!dir)
1755 goto out;
1757 strbuf_addch(&path, '/');
1758 dirnamelen = path.len;
1759 while ((de = readdir(dir)) != NULL) {
1760 struct stat st;
1761 uint32_t i, found = 0;
1763 strbuf_setlen(&path, dirnamelen);
1764 strbuf_addstr(&path, de->d_name);
1766 stat(path.buf, &st);
1768 if (st.st_mtime > expire_time)
1769 continue;
1770 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
1771 continue;
1773 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
1774 if (!strcmp(ctx->commit_graph_filenames_after[i],
1775 path.buf)) {
1776 found = 1;
1777 break;
1781 if (!found)
1782 unlink(path.buf);
1785 out:
1786 strbuf_release(&path);
1789 int write_commit_graph(struct object_directory *odb,
1790 struct string_list *pack_indexes,
1791 struct oidset *commits,
1792 enum commit_graph_write_flags flags,
1793 const struct split_commit_graph_opts *split_opts)
1795 struct write_commit_graph_context *ctx;
1796 uint32_t i, count_distinct = 0;
1797 int res = 0;
1798 int replace = 0;
1800 if (!commit_graph_compatible(the_repository))
1801 return 0;
1803 ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
1804 ctx->r = the_repository;
1805 ctx->odb = odb;
1806 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
1807 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
1808 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
1809 ctx->check_oids = flags & COMMIT_GRAPH_WRITE_CHECK_OIDS ? 1 : 0;
1810 ctx->split_opts = split_opts;
1812 if (ctx->split) {
1813 struct commit_graph *g;
1814 prepare_commit_graph(ctx->r);
1816 g = ctx->r->objects->commit_graph;
1818 while (g) {
1819 ctx->num_commit_graphs_before++;
1820 g = g->base_graph;
1823 if (ctx->num_commit_graphs_before) {
1824 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
1825 i = ctx->num_commit_graphs_before;
1826 g = ctx->r->objects->commit_graph;
1828 while (g) {
1829 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
1830 g = g->base_graph;
1834 if (ctx->split_opts)
1835 replace = ctx->split_opts->flags & COMMIT_GRAPH_SPLIT_REPLACE;
1838 ctx->approx_nr_objects = approximate_object_count();
1839 ctx->oids.alloc = ctx->approx_nr_objects / 32;
1841 if (ctx->split && split_opts && ctx->oids.alloc > split_opts->max_commits)
1842 ctx->oids.alloc = split_opts->max_commits;
1844 if (ctx->append) {
1845 prepare_commit_graph_one(ctx->r, ctx->odb);
1846 if (ctx->r->objects->commit_graph)
1847 ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
1850 if (ctx->oids.alloc < 1024)
1851 ctx->oids.alloc = 1024;
1852 ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc);
1854 if (ctx->append && ctx->r->objects->commit_graph) {
1855 struct commit_graph *g = ctx->r->objects->commit_graph;
1856 for (i = 0; i < g->num_commits; i++) {
1857 const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i;
1858 hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash);
1862 if (pack_indexes) {
1863 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
1864 goto cleanup;
1867 if (commits) {
1868 if ((res = fill_oids_from_commits(ctx, commits)))
1869 goto cleanup;
1872 if (!pack_indexes && !commits)
1873 fill_oids_from_all_packs(ctx);
1875 close_reachable(ctx);
1877 count_distinct = count_distinct_commits(ctx);
1879 if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
1880 error(_("the commit graph format cannot write %d commits"), count_distinct);
1881 res = -1;
1882 goto cleanup;
1885 ctx->commits.alloc = count_distinct;
1886 ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc);
1888 copy_oids_to_commits(ctx);
1890 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
1891 error(_("too many commits to write graph"));
1892 res = -1;
1893 goto cleanup;
1896 if (!ctx->commits.nr && !replace)
1897 goto cleanup;
1899 if (ctx->split) {
1900 split_graph_merge_strategy(ctx);
1902 if (!replace)
1903 merge_commit_graphs(ctx);
1904 } else
1905 ctx->num_commit_graphs_after = 1;
1907 compute_generation_numbers(ctx);
1909 res = write_commit_graph_file(ctx);
1911 if (ctx->split)
1912 mark_commit_graphs(ctx);
1914 expire_commit_graphs(ctx);
1916 cleanup:
1917 free(ctx->graph_name);
1918 free(ctx->commits.list);
1919 free(ctx->oids.list);
1921 if (ctx->commit_graph_filenames_after) {
1922 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
1923 free(ctx->commit_graph_filenames_after[i]);
1924 free(ctx->commit_graph_hash_after[i]);
1927 for (i = 0; i < ctx->num_commit_graphs_before; i++)
1928 free(ctx->commit_graph_filenames_before[i]);
1930 free(ctx->commit_graph_filenames_after);
1931 free(ctx->commit_graph_filenames_before);
1932 free(ctx->commit_graph_hash_after);
1935 free(ctx);
1937 return res;
1940 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
1941 static int verify_commit_graph_error;
1943 static void graph_report(const char *fmt, ...)
1945 va_list ap;
1947 verify_commit_graph_error = 1;
1948 va_start(ap, fmt);
1949 vfprintf(stderr, fmt, ap);
1950 fprintf(stderr, "\n");
1951 va_end(ap);
1954 #define GENERATION_ZERO_EXISTS 1
1955 #define GENERATION_NUMBER_EXISTS 2
1957 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
1959 uint32_t i, cur_fanout_pos = 0;
1960 struct object_id prev_oid, cur_oid, checksum;
1961 int generation_zero = 0;
1962 struct hashfile *f;
1963 int devnull;
1964 struct progress *progress = NULL;
1965 int local_error = 0;
1967 if (!g) {
1968 graph_report("no commit-graph file loaded");
1969 return 1;
1972 verify_commit_graph_error = verify_commit_graph_lite(g);
1973 if (verify_commit_graph_error)
1974 return verify_commit_graph_error;
1976 devnull = open("/dev/null", O_WRONLY);
1977 f = hashfd(devnull, NULL);
1978 hashwrite(f, g->data, g->data_len - g->hash_len);
1979 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
1980 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
1981 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
1982 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
1985 for (i = 0; i < g->num_commits; i++) {
1986 struct commit *graph_commit;
1988 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1990 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
1991 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
1992 oid_to_hex(&prev_oid),
1993 oid_to_hex(&cur_oid));
1995 oidcpy(&prev_oid, &cur_oid);
1997 while (cur_oid.hash[0] > cur_fanout_pos) {
1998 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2000 if (i != fanout_value)
2001 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2002 cur_fanout_pos, fanout_value, i);
2003 cur_fanout_pos++;
2006 graph_commit = lookup_commit(r, &cur_oid);
2007 if (!parse_commit_in_graph_one(r, g, graph_commit))
2008 graph_report(_("failed to parse commit %s from commit-graph"),
2009 oid_to_hex(&cur_oid));
2012 while (cur_fanout_pos < 256) {
2013 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2015 if (g->num_commits != fanout_value)
2016 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2017 cur_fanout_pos, fanout_value, i);
2019 cur_fanout_pos++;
2022 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2023 return verify_commit_graph_error;
2025 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2026 progress = start_progress(_("Verifying commits in commit graph"),
2027 g->num_commits);
2029 for (i = 0; i < g->num_commits; i++) {
2030 struct commit *graph_commit, *odb_commit;
2031 struct commit_list *graph_parents, *odb_parents;
2032 uint32_t max_generation = 0;
2034 display_progress(progress, i + 1);
2035 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2037 graph_commit = lookup_commit(r, &cur_oid);
2038 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2039 if (parse_commit_internal(odb_commit, 0, 0)) {
2040 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2041 oid_to_hex(&cur_oid));
2042 continue;
2045 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2046 get_commit_tree_oid(odb_commit)))
2047 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2048 oid_to_hex(&cur_oid),
2049 oid_to_hex(get_commit_tree_oid(graph_commit)),
2050 oid_to_hex(get_commit_tree_oid(odb_commit)));
2052 graph_parents = graph_commit->parents;
2053 odb_parents = odb_commit->parents;
2055 while (graph_parents) {
2056 if (odb_parents == NULL) {
2057 graph_report(_("commit-graph parent list for commit %s is too long"),
2058 oid_to_hex(&cur_oid));
2059 break;
2062 /* parse parent in case it is in a base graph */
2063 parse_commit_in_graph_one(r, g, graph_parents->item);
2065 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2066 graph_report(_("commit-graph parent for %s is %s != %s"),
2067 oid_to_hex(&cur_oid),
2068 oid_to_hex(&graph_parents->item->object.oid),
2069 oid_to_hex(&odb_parents->item->object.oid));
2071 if (graph_parents->item->generation > max_generation)
2072 max_generation = graph_parents->item->generation;
2074 graph_parents = graph_parents->next;
2075 odb_parents = odb_parents->next;
2078 if (odb_parents != NULL)
2079 graph_report(_("commit-graph parent list for commit %s terminates early"),
2080 oid_to_hex(&cur_oid));
2082 if (!graph_commit->generation) {
2083 if (generation_zero == GENERATION_NUMBER_EXISTS)
2084 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2085 oid_to_hex(&cur_oid));
2086 generation_zero = GENERATION_ZERO_EXISTS;
2087 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2088 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2089 oid_to_hex(&cur_oid));
2091 if (generation_zero == GENERATION_ZERO_EXISTS)
2092 continue;
2095 * If one of our parents has generation GENERATION_NUMBER_MAX, then
2096 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
2097 * extra logic in the following condition.
2099 if (max_generation == GENERATION_NUMBER_MAX)
2100 max_generation--;
2102 if (graph_commit->generation != max_generation + 1)
2103 graph_report(_("commit-graph generation for commit %s is %u != %u"),
2104 oid_to_hex(&cur_oid),
2105 graph_commit->generation,
2106 max_generation + 1);
2108 if (graph_commit->date != odb_commit->date)
2109 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2110 oid_to_hex(&cur_oid),
2111 graph_commit->date,
2112 odb_commit->date);
2114 stop_progress(&progress);
2116 local_error = verify_commit_graph_error;
2118 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2119 local_error |= verify_commit_graph(r, g->base_graph, flags);
2121 return local_error;
2124 void free_commit_graph(struct commit_graph *g)
2126 if (!g)
2127 return;
2128 if (g->data) {
2129 munmap((void *)g->data, g->data_len);
2130 g->data = NULL;
2132 free(g->filename);
2133 free(g);
2136 void disable_commit_graph(struct repository *r)
2138 r->commit_graph_disabled = 1;