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