blob: f18380b922c12208cbceae92fce98e2e6b42e6ed [file] [log] [blame]
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001#include "git-compat-util.h"
SZEDER Gáborfa796532020-06-05 13:00:28 +00002#include "config.h"
Derrick Stolee08fd81c2018-04-02 16:34:19 -04003#include "lockfile.h"
4#include "pack.h"
5#include "packfile.h"
6#include "commit.h"
7#include "object.h"
Derrick Stolee59fb8772018-06-27 09:24:45 -04008#include "refs.h"
Derrick Stolee08fd81c2018-04-02 16:34:19 -04009#include "revision.h"
Martin Ågrenbc626922020-12-31 12:56:23 +010010#include "hash-lookup.h"
Derrick Stolee08fd81c2018-04-02 16:34:19 -040011#include "commit-graph.h"
Junio C Hamanob10edb22018-05-08 15:59:20 +090012#include "object-store.h"
Derrick Stolee96af91d2018-06-27 09:24:36 -040013#include "alloc.h"
Derrick Stoleed6538242018-08-20 18:24:27 +000014#include "hashmap.h"
15#include "replace-object.h"
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +000016#include "progress.h"
Garima Singhf97b9322020-03-30 00:31:28 +000017#include "bloom.h"
Jeff Kingd21ee7d2020-03-30 00:31:29 +000018#include "commit-slab.h"
Taylor Blau120ad2b2020-04-30 13:48:50 -060019#include "shallow.h"
Derrick Stolee0087a872020-07-01 13:27:24 +000020#include "json-writer.h"
21#include "trace2.h"
Derrick Stolee47410aa2021-02-18 14:07:25 +000022#include "chunk-format.h"
Derrick Stolee08fd81c2018-04-02 16:34:19 -040023
Derrick Stoleeb23ea972020-04-16 20:14:03 +000024void git_test_write_commit_graph_or_die(void)
25{
26 int flags = 0;
27 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
28 return;
29
30 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
31 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
32
33 if (write_commit_graph_reachable(the_repository->objects->odb,
34 flags, NULL))
35 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
36}
37
Derrick Stolee08fd81c2018-04-02 16:34:19 -040038#define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
39#define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
40#define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
41#define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
Abhishek Kumare8b63002021-01-16 18:11:15 +000042#define GRAPH_CHUNKID_GENERATION_DATA 0x47444154 /* "GDAT" */
43#define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f56 /* "GDOV" */
SZEDER Gábor5af74172019-01-19 21:21:13 +010044#define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
Garima Singh76ffbca2020-04-06 16:59:49 +000045#define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
46#define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
Derrick Stolee118bd572019-06-18 11:14:26 -070047#define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
Derrick Stolee08fd81c2018-04-02 16:34:19 -040048
brian m. carlsonc1665992018-11-14 04:09:35 +000049#define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
Derrick Stolee08fd81c2018-04-02 16:34:19 -040050
51#define GRAPH_VERSION_1 0x1
52#define GRAPH_VERSION GRAPH_VERSION_1
53
SZEDER Gábor5af74172019-01-19 21:21:13 +010054#define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
Derrick Stolee08fd81c2018-04-02 16:34:19 -040055#define GRAPH_EDGE_LAST_MASK 0x7fffffff
56#define GRAPH_PARENT_NONE 0x70000000
57
58#define GRAPH_LAST_EDGE 0x80000000
59
Derrick Stolee0e3b97c2018-06-27 09:24:28 -040060#define GRAPH_HEADER_SIZE 8
Derrick Stolee08fd81c2018-04-02 16:34:19 -040061#define GRAPH_FANOUT_SIZE (4 * 256)
Derrick Stolee2692c2f2021-02-18 14:07:35 +000062#define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
brian m. carlsonc1665992018-11-14 04:09:35 +000063 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
Derrick Stolee08fd81c2018-04-02 16:34:19 -040064
Abhishek Kumare8b63002021-01-16 18:11:15 +000065#define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
66
Derrick Stoleecb99a342019-10-24 13:40:42 +000067/* Remember to update object flag allocation in object.h */
68#define REACHABLE (1u<<15)
69
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +000070define_commit_slab(topo_level_slab, uint32_t);
71
Jeff Kingd21ee7d2020-03-30 00:31:29 +000072/* Keep track of the order in which commits are added to our list. */
73define_commit_slab(commit_pos, int);
74static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
75
76static void set_commit_pos(struct repository *r, const struct object_id *oid)
Derrick Stolee08fd81c2018-04-02 16:34:19 -040077{
Jeff Kingd21ee7d2020-03-30 00:31:29 +000078 static int32_t max_pos;
79 struct commit *commit = lookup_commit(r, oid);
80
81 if (!commit)
82 return; /* should never happen, but be lenient */
83
84 *commit_pos_at(&commit_pos, commit) = max_pos++;
85}
86
87static int commit_pos_cmp(const void *va, const void *vb)
88{
89 const struct commit *a = *(const struct commit **)va;
90 const struct commit *b = *(const struct commit **)vb;
91 return commit_pos_at(&commit_pos, a) -
92 commit_pos_at(&commit_pos, b);
93}
94
Abhishek Kumar48448122020-06-17 14:44:09 +053095define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
96static struct commit_graph_data_slab commit_graph_data_slab =
97 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
98
Derrick Stolee702110a2021-02-25 18:19:43 +000099static int get_configured_generation_version(struct repository *r)
100{
101 int version = 2;
102 repo_config_get_int(r, "commitgraph.generationversion", &version);
103 return version;
104}
105
Abhishek Kumar48448122020-06-17 14:44:09 +0530106uint32_t commit_graph_position(const struct commit *c)
107{
108 struct commit_graph_data *data =
109 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
110
111 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
112}
113
Abhishek Kumard7f92782021-01-16 18:11:13 +0000114timestamp_t commit_graph_generation(const struct commit *c)
Abhishek Kumar48448122020-06-17 14:44:09 +0530115{
116 struct commit_graph_data *data =
117 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
118
119 if (!data)
120 return GENERATION_NUMBER_INFINITY;
121 else if (data->graph_pos == COMMIT_NOT_FROM_GRAPH)
122 return GENERATION_NUMBER_INFINITY;
123
124 return data->generation;
125}
126
127static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
128{
129 unsigned int i, nth_slab;
130 struct commit_graph_data *data =
131 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
132
133 if (data)
134 return data;
135
136 nth_slab = c->index / commit_graph_data_slab.slab_size;
137 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
138
139 /*
140 * commit-slab initializes elements with zero, overwrite this with
141 * COMMIT_NOT_FROM_GRAPH for graph_pos.
142 *
143 * We avoid initializing generation with checking if graph position
144 * is not COMMIT_NOT_FROM_GRAPH.
145 */
146 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
147 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
148 COMMIT_NOT_FROM_GRAPH;
149 }
150
151 return data;
152}
153
Abhishek Kumare30c5ee2021-01-16 18:11:08 +0000154/*
155 * Should be used only while writing commit-graph as it compares
156 * generation value of commits by directly accessing commit-slab.
157 */
Garima Singh3d112752020-03-30 00:31:30 +0000158static int commit_gen_cmp(const void *va, const void *vb)
159{
160 const struct commit *a = *(const struct commit **)va;
161 const struct commit *b = *(const struct commit **)vb;
162
Abhishek Kumard7f92782021-01-16 18:11:13 +0000163 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
164 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
Garima Singh3d112752020-03-30 00:31:30 +0000165 /* lower generation commits first */
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530166 if (generation_a < generation_b)
Garima Singh3d112752020-03-30 00:31:30 +0000167 return -1;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530168 else if (generation_a > generation_b)
Garima Singh3d112752020-03-30 00:31:30 +0000169 return 1;
170
171 /* use date as a heuristic when generations are equal */
172 if (a->date < b->date)
173 return -1;
174 else if (a->date > b->date)
175 return 1;
176 return 0;
177}
178
Jeff Kingd21ee7d2020-03-30 00:31:29 +0000179char *get_commit_graph_filename(struct object_directory *obj_dir)
180{
181 return xstrfmt("%s/info/commit-graph", obj_dir->path);
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400182}
183
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800184static char *get_split_graph_filename(struct object_directory *odb,
Derrick Stolee5c84b332019-06-18 11:14:25 -0700185 const char *oid_hex)
186{
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800187 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
188 oid_hex);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700189}
190
Derrick Stolee663b2b12020-09-17 18:11:46 +0000191char *get_commit_graph_chain_filename(struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700192{
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800193 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400194}
195
brian m. carlsonc1665992018-11-14 04:09:35 +0000196static uint8_t oid_version(void)
197{
Derrick Stolee665d70a2020-08-17 14:04:47 +0000198 switch (hash_algo_by_ptr(the_hash_algo)) {
199 case GIT_HASH_SHA1:
200 return 1;
201 case GIT_HASH_SHA256:
202 return 2;
203 default:
204 die(_("invalid hash version"));
205 }
brian m. carlsonc1665992018-11-14 04:09:35 +0000206}
207
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400208static struct commit_graph *alloc_commit_graph(void)
209{
210 struct commit_graph *g = xcalloc(1, sizeof(*g));
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400211
212 return g;
213}
214
Derrick Stoleed6538242018-08-20 18:24:27 +0000215extern int read_replace_refs;
216
217static int commit_graph_compatible(struct repository *r)
218{
Derrick Stolee5cef2952018-08-20 18:24:32 +0000219 if (!r->gitdir)
220 return 0;
221
Derrick Stoleed6538242018-08-20 18:24:27 +0000222 if (read_replace_refs) {
223 prepare_replace_object(r);
Junio C Hamanocdc986a2021-03-01 09:19:37 -0800224 if (hashmap_get_size(&r->objects->replace_map->map))
Derrick Stoleed6538242018-08-20 18:24:27 +0000225 return 0;
226 }
227
Derrick Stolee20fd6d52018-08-20 18:24:30 +0000228 prepare_commit_graft(r);
Taylor Blauce163642020-07-08 17:10:53 -0400229 if (r->parsed_objects &&
Junio C Hamanocdc986a2021-03-01 09:19:37 -0800230 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
Derrick Stolee20fd6d52018-08-20 18:24:30 +0000231 return 0;
Junio C Hamanocdc986a2021-03-01 09:19:37 -0800232 if (is_repository_shallow(r))
Derrick Stolee20fd6d52018-08-20 18:24:30 +0000233 return 0;
234
Derrick Stoleed6538242018-08-20 18:24:27 +0000235 return 1;
236}
237
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100238int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
239{
240 *fd = git_open(graph_file);
241 if (*fd < 0)
242 return 0;
243 if (fstat(*fd, st)) {
244 close(*fd);
245 return 0;
246 }
247 return 1;
248}
249
Taylor Blauab14d062020-09-09 11:22:56 -0400250struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
251 int fd, struct stat *st,
Taylor Blaua7df60c2020-02-03 13:18:04 -0800252 struct object_directory *odb)
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400253{
254 void *graph_map;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400255 size_t graph_size;
Josh Steadmonaa658572019-01-15 14:25:50 -0800256 struct commit_graph *ret;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400257
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100258 graph_size = xsize_t(st->st_size);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400259
260 if (graph_size < GRAPH_MIN_SIZE) {
261 close(fd);
Ævar Arnfjörð Bjarmason67a530f2019-03-25 13:08:31 +0100262 error(_("commit-graph file is too small"));
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100263 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400264 }
265 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
Jeff Kingc8828532020-04-23 15:41:13 -0600266 close(fd);
Taylor Blauab14d062020-09-09 11:22:56 -0400267 ret = parse_commit_graph(r, graph_map, graph_size);
Josh Steadmonaa658572019-01-15 14:25:50 -0800268
Taylor Blaua7df60c2020-02-03 13:18:04 -0800269 if (ret)
270 ret->odb = odb;
Jeff Kingc8828532020-04-23 15:41:13 -0600271 else
Josh Steadmonaa658572019-01-15 14:25:50 -0800272 munmap(graph_map, graph_size);
Josh Steadmonaa658572019-01-15 14:25:50 -0800273
274 return ret;
275}
276
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +0100277static int verify_commit_graph_lite(struct commit_graph *g)
278{
279 /*
280 * Basic validation shared between parse_commit_graph()
281 * which'll be called every time the graph is used, and the
282 * much more expensive verify_commit_graph() used by
283 * "commit-graph verify".
284 *
285 * There should only be very basic checks here to ensure that
286 * we don't e.g. segfault in fill_commit_in_graph(), but
287 * because this is a very hot codepath nothing that e.g. loops
288 * over g->num_commits, or runs a checksum on the commit-graph
289 * itself.
290 */
291 if (!g->chunk_oid_fanout) {
292 error("commit-graph is missing the OID Fanout chunk");
293 return 1;
294 }
295 if (!g->chunk_oid_lookup) {
296 error("commit-graph is missing the OID Lookup chunk");
297 return 1;
298 }
299 if (!g->chunk_commit_data) {
300 error("commit-graph is missing the Commit Data chunk");
301 return 1;
302 }
303
304 return 0;
305}
306
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000307static int graph_read_oid_lookup(const unsigned char *chunk_start,
308 size_t chunk_size, void *data)
309{
310 struct commit_graph *g = data;
311 g->chunk_oid_lookup = chunk_start;
312 g->num_commits = chunk_size / g->hash_len;
313 return 0;
314}
315
316static int graph_read_bloom_data(const unsigned char *chunk_start,
317 size_t chunk_size, void *data)
318{
319 struct commit_graph *g = data;
320 uint32_t hash_version;
321 g->chunk_bloom_data = chunk_start;
322 hash_version = get_be32(chunk_start);
323
324 if (hash_version != 1)
325 return 0;
326
327 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
328 g->bloom_filter_settings->hash_version = hash_version;
329 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
330 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
331 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
332
333 return 0;
334}
335
Taylor Blauab14d062020-09-09 11:22:56 -0400336struct commit_graph *parse_commit_graph(struct repository *r,
337 void *graph_map, size_t graph_size)
Josh Steadmonaa658572019-01-15 14:25:50 -0800338{
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000339 const unsigned char *data;
Josh Steadmonaa658572019-01-15 14:25:50 -0800340 struct commit_graph *graph;
Josh Steadmonaa658572019-01-15 14:25:50 -0800341 uint32_t graph_signature;
342 unsigned char graph_version, hash_version;
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000343 struct chunkfile *cf = NULL;
Josh Steadmonaa658572019-01-15 14:25:50 -0800344
345 if (!graph_map)
346 return NULL;
347
348 if (graph_size < GRAPH_MIN_SIZE)
349 return NULL;
350
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400351 data = (const unsigned char *)graph_map;
352
353 graph_signature = get_be32(data);
354 if (graph_signature != GRAPH_SIGNATURE) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100355 error(_("commit-graph signature %X does not match signature %X"),
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400356 graph_signature, GRAPH_SIGNATURE);
Josh Steadmonaa658572019-01-15 14:25:50 -0800357 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400358 }
359
360 graph_version = *(unsigned char*)(data + 4);
361 if (graph_version != GRAPH_VERSION) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100362 error(_("commit-graph version %X does not match version %X"),
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400363 graph_version, GRAPH_VERSION);
Josh Steadmonaa658572019-01-15 14:25:50 -0800364 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400365 }
366
367 hash_version = *(unsigned char*)(data + 5);
brian m. carlsonc1665992018-11-14 04:09:35 +0000368 if (hash_version != oid_version()) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100369 error(_("commit-graph hash version %X does not match version %X"),
brian m. carlsonc1665992018-11-14 04:09:35 +0000370 hash_version, oid_version());
Josh Steadmonaa658572019-01-15 14:25:50 -0800371 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400372 }
373
Taylor Blaub66d8472020-09-09 11:23:10 -0400374 prepare_repo_settings(r);
375
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400376 graph = alloc_commit_graph();
377
brian m. carlsonc1665992018-11-14 04:09:35 +0000378 graph->hash_len = the_hash_algo->rawsz;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400379 graph->num_chunks = *(unsigned char*)(data + 6);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400380 graph->data = graph_map;
381 graph->data_len = graph_size;
382
SZEDER Gábor2ad4f1a2020-06-05 13:00:29 +0000383 if (graph_size < GRAPH_HEADER_SIZE +
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000384 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
SZEDER Gábor2ad4f1a2020-06-05 13:00:29 +0000385 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
386 error(_("commit-graph file is too small to hold %u chunks"),
387 graph->num_chunks);
388 free(graph);
389 return NULL;
390 }
391
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000392 cf = init_chunkfile(NULL);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400393
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000394 if (read_table_of_contents(cf, graph->data, graph_size,
395 GRAPH_HEADER_SIZE, graph->num_chunks))
396 goto free_and_return;
Josh Steadmond2b86fb2019-01-15 14:25:51 -0800397
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000398 pair_chunk(cf, GRAPH_CHUNKID_OIDFANOUT,
399 (const unsigned char **)&graph->chunk_oid_fanout);
400 read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph);
401 pair_chunk(cf, GRAPH_CHUNKID_DATA, &graph->chunk_commit_data);
402 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges);
403 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs);
Derrick Stolee702110a2021-02-25 18:19:43 +0000404
405 if (get_configured_generation_version(r) >= 2) {
406 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
407 &graph->chunk_generation_data);
408 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
409 &graph->chunk_generation_data_overflow);
410 }
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400411
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000412 if (r->settings.commit_graph_read_changed_paths) {
413 pair_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
414 &graph->chunk_bloom_indexes);
415 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
416 graph_read_bloom_data, graph);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400417 }
418
Garima Singh76ffbca2020-04-06 16:59:49 +0000419 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
420 init_bloom_filters();
421 } else {
422 /* We need both the bloom chunks to exist together. Else ignore the data */
423 graph->chunk_bloom_indexes = NULL;
424 graph->chunk_bloom_data = NULL;
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700425 FREE_AND_NULL(graph->bloom_filter_settings);
Garima Singh76ffbca2020-04-06 16:59:49 +0000426 }
427
Derrick Stolee118bd572019-06-18 11:14:26 -0700428 hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
429
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700430 if (verify_commit_graph_lite(graph))
431 goto free_and_return;
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +0100432
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000433 free_chunkfile(cf);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400434 return graph;
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700435
436free_and_return:
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000437 free_chunkfile(cf);
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700438 free(graph->bloom_filter_settings);
439 free(graph);
440 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400441}
442
Taylor Blauab14d062020-09-09 11:22:56 -0400443static struct commit_graph *load_commit_graph_one(struct repository *r,
444 const char *graph_file,
Taylor Blaua7df60c2020-02-03 13:18:04 -0800445 struct object_directory *odb)
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100446{
447
448 struct stat st;
449 int fd;
Derrick Stolee6c622f92019-06-18 11:14:27 -0700450 struct commit_graph *g;
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100451 int open_ok = open_commit_graph(graph_file, &fd, &st);
452
453 if (!open_ok)
454 return NULL;
455
Taylor Blauab14d062020-09-09 11:22:56 -0400456 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -0700457
458 if (g)
459 g->filename = xstrdup(graph_file);
460
461 return g;
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100462}
463
Taylor Blau13c24992020-02-03 13:18:00 -0800464static struct commit_graph *load_commit_graph_v1(struct repository *r,
465 struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700466{
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800467 char *graph_name = get_commit_graph_filename(odb);
Taylor Blauab14d062020-09-09 11:22:56 -0400468 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700469 free(graph_name);
470
471 return g;
472}
473
474static int add_graph_to_chain(struct commit_graph *g,
475 struct commit_graph *chain,
476 struct object_id *oids,
477 int n)
478{
479 struct commit_graph *cur_g = chain;
480
Derrick Stolee118bd572019-06-18 11:14:26 -0700481 if (n && !g->chunk_base_graphs) {
482 warning(_("commit-graph has no base graphs chunk"));
483 return 0;
484 }
485
Derrick Stolee5c84b332019-06-18 11:14:25 -0700486 while (n) {
487 n--;
Derrick Stolee118bd572019-06-18 11:14:26 -0700488
489 if (!cur_g ||
490 !oideq(&oids[n], &cur_g->oid) ||
491 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
492 warning(_("commit-graph chain does not match"));
493 return 0;
494 }
495
Derrick Stolee5c84b332019-06-18 11:14:25 -0700496 cur_g = cur_g->base_graph;
497 }
498
499 g->base_graph = chain;
500
501 if (chain)
502 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
503
504 return 1;
505}
506
Taylor Blau13c24992020-02-03 13:18:00 -0800507static struct commit_graph *load_commit_graph_chain(struct repository *r,
508 struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700509{
510 struct commit_graph *graph_chain = NULL;
511 struct strbuf line = STRBUF_INIT;
512 struct stat st;
513 struct object_id *oids;
514 int i = 0, valid = 1, count;
Derrick Stolee663b2b12020-09-17 18:11:46 +0000515 char *chain_name = get_commit_graph_chain_filename(odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700516 FILE *fp;
517 int stat_res;
518
519 fp = fopen(chain_name, "r");
520 stat_res = stat(chain_name, &st);
521 free(chain_name);
522
523 if (!fp ||
524 stat_res ||
525 st.st_size <= the_hash_algo->hexsz)
526 return NULL;
527
528 count = st.st_size / (the_hash_algo->hexsz + 1);
René Scharfeca56dad2021-03-13 17:17:22 +0100529 CALLOC_ARRAY(oids, count);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700530
Derrick Stoleec5230352019-06-18 11:14:30 -0700531 prepare_alt_odb(r);
532
533 for (i = 0; i < count; i++) {
534 struct object_directory *odb;
Derrick Stolee5c84b332019-06-18 11:14:25 -0700535
536 if (strbuf_getline_lf(&line, fp) == EOF)
537 break;
538
539 if (get_oid_hex(line.buf, &oids[i])) {
540 warning(_("invalid commit-graph chain: line '%s' not a hash"),
541 line.buf);
542 valid = 0;
543 break;
544 }
545
Derrick Stoleec5230352019-06-18 11:14:30 -0700546 valid = 0;
547 for (odb = r->objects->odb; odb; odb = odb->next) {
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800548 char *graph_name = get_split_graph_filename(odb, line.buf);
Taylor Blauab14d062020-09-09 11:22:56 -0400549 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700550
Derrick Stoleec5230352019-06-18 11:14:30 -0700551 free(graph_name);
552
553 if (g) {
Derrick Stoleec5230352019-06-18 11:14:30 -0700554 if (add_graph_to_chain(g, graph_chain, oids, i)) {
555 graph_chain = g;
556 valid = 1;
557 }
558
559 break;
560 }
561 }
562
563 if (!valid) {
564 warning(_("unable to find all commit-graph files"));
565 break;
566 }
Derrick Stolee5c84b332019-06-18 11:14:25 -0700567 }
568
569 free(oids);
570 fclose(fp);
René Scharfe0aa6bce2019-08-07 13:15:02 +0200571 strbuf_release(&line);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700572
573 return graph_chain;
574}
575
Derrick Stolee448a39e2021-02-02 03:01:20 +0000576/*
577 * returns 1 if and only if all graphs in the chain have
578 * corrected commit dates stored in the generation_data chunk.
579 */
580static int validate_mixed_generation_chain(struct commit_graph *g)
Abhishek Kumar1fdc3832021-01-16 18:11:16 +0000581{
Derrick Stolee448a39e2021-02-02 03:01:20 +0000582 int read_generation_data = 1;
583 struct commit_graph *p = g;
Abhishek Kumar1fdc3832021-01-16 18:11:16 +0000584
Derrick Stolee448a39e2021-02-02 03:01:20 +0000585 while (read_generation_data && p) {
586 read_generation_data = p->read_generation_data;
587 p = p->base_graph;
588 }
Abhishek Kumar1fdc3832021-01-16 18:11:16 +0000589
Derrick Stolee448a39e2021-02-02 03:01:20 +0000590 if (read_generation_data)
591 return 1;
Abhishek Kumar1fdc3832021-01-16 18:11:16 +0000592
593 while (g) {
Derrick Stolee448a39e2021-02-02 03:01:20 +0000594 g->read_generation_data = 0;
Abhishek Kumar1fdc3832021-01-16 18:11:16 +0000595 g = g->base_graph;
596 }
Derrick Stolee448a39e2021-02-02 03:01:20 +0000597
598 return 0;
Abhishek Kumar1fdc3832021-01-16 18:11:16 +0000599}
600
Taylor Blau13c24992020-02-03 13:18:00 -0800601struct commit_graph *read_commit_graph_one(struct repository *r,
602 struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700603{
Taylor Blau13c24992020-02-03 13:18:00 -0800604 struct commit_graph *g = load_commit_graph_v1(r, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700605
606 if (!g)
Taylor Blau13c24992020-02-03 13:18:00 -0800607 g = load_commit_graph_chain(r, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700608
Abhishek Kumar1fdc3832021-01-16 18:11:16 +0000609 validate_mixed_generation_chain(g);
610
Derrick Stolee5c84b332019-06-18 11:14:25 -0700611 return g;
Jonathan Tan5faf3572018-07-11 15:42:37 -0700612}
613
Taylor Blau13c24992020-02-03 13:18:00 -0800614static void prepare_commit_graph_one(struct repository *r,
615 struct object_directory *odb)
Derrick Stolee177722b2018-04-10 08:56:05 -0400616{
Derrick Stolee177722b2018-04-10 08:56:05 -0400617
618 if (r->objects->commit_graph)
619 return;
620
Taylor Blau13c24992020-02-03 13:18:00 -0800621 r->objects->commit_graph = read_commit_graph_one(r, odb);
Derrick Stolee177722b2018-04-10 08:56:05 -0400622}
Jonathan Tan5faf3572018-07-11 15:42:37 -0700623
624/*
625 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
626 *
Elijah Newren15beaaa2019-11-05 17:07:23 +0000627 * On the first invocation, this function attempts to load the commit
Jonathan Tan5faf3572018-07-11 15:42:37 -0700628 * graph if the_repository is configured to have one.
629 */
Jonathan Tandade47c2018-07-11 15:42:42 -0700630static int prepare_commit_graph(struct repository *r)
Derrick Stolee177722b2018-04-10 08:56:05 -0400631{
Jeff King263db402018-11-12 09:48:47 -0500632 struct object_directory *odb;
Derrick Stolee177722b2018-04-10 08:56:05 -0400633
Jeff King6abada12019-09-12 10:44:45 -0400634 /*
635 * This must come before the "already attempted?" check below, because
636 * we want to disable even an already-loaded graph file.
637 */
638 if (r->commit_graph_disabled)
639 return 0;
Ævar Arnfjörð Bjarmason43d35612019-03-25 13:08:33 +0100640
Jonathan Tandade47c2018-07-11 15:42:42 -0700641 if (r->objects->commit_graph_attempted)
642 return !!r->objects->commit_graph;
643 r->objects->commit_graph_attempted = 1;
Derrick Stolee177722b2018-04-10 08:56:05 -0400644
Derrick Stolee7211b9e2019-08-13 11:37:43 -0700645 prepare_repo_settings(r);
646
Derrick Stolee859fdc02018-08-29 05:49:04 -0700647 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
Derrick Stolee7211b9e2019-08-13 11:37:43 -0700648 r->settings.core_commit_graph != 1)
Jonathan Tandade47c2018-07-11 15:42:42 -0700649 /*
650 * This repository is not configured to use commit graphs, so
651 * do not load one. (But report commit_graph_attempted anyway
652 * so that commit graph loading is not attempted again for this
653 * repository.)
654 */
Jonathan Tan5faf3572018-07-11 15:42:37 -0700655 return 0;
656
Derrick Stoleed6538242018-08-20 18:24:27 +0000657 if (!commit_graph_compatible(r))
658 return 0;
659
Jonathan Tandade47c2018-07-11 15:42:42 -0700660 prepare_alt_odb(r);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500661 for (odb = r->objects->odb;
Jeff King263db402018-11-12 09:48:47 -0500662 !r->objects->commit_graph && odb;
663 odb = odb->next)
Taylor Blau13c24992020-02-03 13:18:00 -0800664 prepare_commit_graph_one(r, odb);
Jonathan Tandade47c2018-07-11 15:42:42 -0700665 return !!r->objects->commit_graph;
Derrick Stolee177722b2018-04-10 08:56:05 -0400666}
667
Derrick Stolee6cc01742018-07-20 16:33:30 +0000668int generation_numbers_enabled(struct repository *r)
669{
670 uint32_t first_generation;
671 struct commit_graph *g;
672 if (!prepare_commit_graph(r))
673 return 0;
674
675 g = r->objects->commit_graph;
676
677 if (!g->num_commits)
678 return 0;
679
680 first_generation = get_be32(g->chunk_commit_data +
681 g->hash_len + 8) >> 2;
682
683 return !!first_generation;
684}
685
Abhishek Kumar8d00d7c2021-01-16 18:11:17 +0000686int corrected_commit_dates_enabled(struct repository *r)
687{
688 struct commit_graph *g;
689 if (!prepare_commit_graph(r))
690 return 0;
691
692 g = r->objects->commit_graph;
693
694 if (!g->num_commits)
695 return 0;
696
697 return g->read_generation_data;
698}
699
Taylor Blau4f364402020-09-09 11:22:44 -0400700struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
701{
702 struct commit_graph *g = r->objects->commit_graph;
703 while (g) {
704 if (g->bloom_filter_settings)
705 return g->bloom_filter_settings;
706 g = g->base_graph;
707 }
708 return NULL;
709}
710
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700711static void close_commit_graph_one(struct commit_graph *g)
712{
713 if (!g)
714 return;
715
716 close_commit_graph_one(g->base_graph);
717 free_commit_graph(g);
718}
719
Derrick Stoleec3a3a962019-05-17 11:41:47 -0700720void close_commit_graph(struct raw_object_store *o)
Derrick Stolee177722b2018-04-10 08:56:05 -0400721{
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700722 close_commit_graph_one(o->commit_graph);
Derrick Stoleec3a3a962019-05-17 11:41:47 -0700723 o->commit_graph = NULL;
Derrick Stolee177722b2018-04-10 08:56:05 -0400724}
725
726static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
727{
728 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
729 g->chunk_oid_lookup, g->hash_len, pos);
730}
731
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700732static void load_oid_from_graph(struct commit_graph *g,
733 uint32_t pos,
734 struct object_id *oid)
735{
736 uint32_t lex_index;
737
738 while (g && pos < g->num_commits_in_base)
739 g = g->base_graph;
740
741 if (!g)
742 BUG("NULL commit-graph");
743
744 if (pos >= g->num_commits + g->num_commits_in_base)
745 die(_("invalid commit position. commit-graph is likely corrupt"));
746
747 lex_index = pos - g->num_commits_in_base;
748
749 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
750}
751
Stefan Beller4f542b72018-12-14 16:09:39 -0800752static struct commit_list **insert_parent_or_die(struct repository *r,
753 struct commit_graph *g,
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700754 uint32_t pos,
Derrick Stolee177722b2018-04-10 08:56:05 -0400755 struct commit_list **pptr)
756{
757 struct commit *c;
758 struct object_id oid;
Derrick Stolee96af91d2018-06-27 09:24:36 -0400759
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700760 if (pos >= g->num_commits + g->num_commits_in_base)
761 die("invalid parent position %"PRIu32, pos);
Derrick Stolee53614b12018-06-27 09:24:38 -0400762
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700763 load_oid_from_graph(g, pos, &oid);
Stefan Beller4f542b72018-12-14 16:09:39 -0800764 c = lookup_commit(r, &oid);
Derrick Stolee177722b2018-04-10 08:56:05 -0400765 if (!c)
Nguyễn Thái Ngọc Duy4f5b5322018-07-21 09:49:26 +0200766 die(_("could not find commit %s"), oid_to_hex(&oid));
Abhishek Kumarc49c82a2020-06-17 14:44:10 +0530767 commit_graph_data_at(c)->graph_pos = pos;
Derrick Stolee177722b2018-04-10 08:56:05 -0400768 return &commit_list_insert(c, pptr)->next;
769}
770
Derrick Stoleee2838d82018-05-01 12:47:13 +0000771static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
772{
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700773 const unsigned char *commit_data;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530774 struct commit_graph_data *graph_data;
Abhishek Kumare8b63002021-01-16 18:11:15 +0000775 uint32_t lex_index, offset_pos;
776 uint64_t date_high, date_low, offset;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700777
778 while (pos < g->num_commits_in_base)
779 g = g->base_graph;
780
Abhishek Kumarf90fca62021-01-16 18:11:10 +0000781 if (pos >= g->num_commits + g->num_commits_in_base)
782 die(_("invalid commit position. commit-graph is likely corrupt"));
783
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700784 lex_index = pos - g->num_commits_in_base;
785 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530786
787 graph_data = commit_graph_data_at(item);
788 graph_data->graph_pos = pos;
Abhishek Kumarf90fca62021-01-16 18:11:10 +0000789
790 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
791 date_low = get_be32(commit_data + g->hash_len + 12);
792 item->date = (timestamp_t)((date_high << 32) | date_low);
793
Abhishek Kumar1fdc3832021-01-16 18:11:16 +0000794 if (g->read_generation_data) {
Abhishek Kumare8b63002021-01-16 18:11:15 +0000795 offset = (timestamp_t)get_be32(g->chunk_generation_data + sizeof(uint32_t) * lex_index);
796
797 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
798 if (!g->chunk_generation_data_overflow)
799 die(_("commit-graph requires overflow generation data but has none"));
800
801 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
802 graph_data->generation = get_be64(g->chunk_generation_data_overflow + 8 * offset_pos);
803 } else
804 graph_data->generation = item->date + offset;
805 } else
806 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +0000807
808 if (g->topo_levels)
809 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
Derrick Stoleee2838d82018-05-01 12:47:13 +0000810}
811
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700812static inline void set_commit_tree(struct commit *c, struct tree *t)
813{
814 c->maybe_tree = t;
815}
816
Stefan Beller4f542b72018-12-14 16:09:39 -0800817static int fill_commit_in_graph(struct repository *r,
818 struct commit *item,
819 struct commit_graph *g, uint32_t pos)
Derrick Stolee177722b2018-04-10 08:56:05 -0400820{
Derrick Stolee177722b2018-04-10 08:56:05 -0400821 uint32_t edge_value;
822 uint32_t *parent_data_ptr;
Derrick Stolee177722b2018-04-10 08:56:05 -0400823 struct commit_list **pptr;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700824 const unsigned char *commit_data;
825 uint32_t lex_index;
826
827 while (pos < g->num_commits_in_base)
828 g = g->base_graph;
829
Abhishek Kumarf90fca62021-01-16 18:11:10 +0000830 fill_commit_graph_info(item, g, pos);
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700831
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700832 lex_index = pos - g->num_commits_in_base;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700833 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
Derrick Stolee177722b2018-04-10 08:56:05 -0400834
835 item->object.parsed = 1;
Derrick Stolee177722b2018-04-10 08:56:05 -0400836
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700837 set_commit_tree(item, NULL);
Derrick Stolee177722b2018-04-10 08:56:05 -0400838
Derrick Stolee177722b2018-04-10 08:56:05 -0400839 pptr = &item->parents;
840
841 edge_value = get_be32(commit_data + g->hash_len);
842 if (edge_value == GRAPH_PARENT_NONE)
843 return 1;
Stefan Beller4f542b72018-12-14 16:09:39 -0800844 pptr = insert_parent_or_die(r, g, edge_value, pptr);
Derrick Stolee177722b2018-04-10 08:56:05 -0400845
846 edge_value = get_be32(commit_data + g->hash_len + 4);
847 if (edge_value == GRAPH_PARENT_NONE)
848 return 1;
SZEDER Gábor5af74172019-01-19 21:21:13 +0100849 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
Stefan Beller4f542b72018-12-14 16:09:39 -0800850 pptr = insert_parent_or_die(r, g, edge_value, pptr);
Derrick Stolee177722b2018-04-10 08:56:05 -0400851 return 1;
852 }
853
SZEDER Gábor5af74172019-01-19 21:21:13 +0100854 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
Derrick Stolee177722b2018-04-10 08:56:05 -0400855 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
856 do {
857 edge_value = get_be32(parent_data_ptr);
Stefan Beller4f542b72018-12-14 16:09:39 -0800858 pptr = insert_parent_or_die(r, g,
Derrick Stolee177722b2018-04-10 08:56:05 -0400859 edge_value & GRAPH_EDGE_LAST_MASK,
860 pptr);
861 parent_data_ptr++;
862 } while (!(edge_value & GRAPH_LAST_EDGE));
863
864 return 1;
865}
866
Derrick Stoleee2838d82018-05-01 12:47:13 +0000867static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
868{
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530869 uint32_t graph_pos = commit_graph_position(item);
870 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
871 *pos = graph_pos;
Derrick Stoleee2838d82018-05-01 12:47:13 +0000872 return 1;
873 } else {
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700874 struct commit_graph *cur_g = g;
875 uint32_t lex_index;
876
877 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
878 cur_g = cur_g->base_graph;
879
880 if (cur_g) {
881 *pos = lex_index + cur_g->num_commits_in_base;
882 return 1;
883 }
884
885 return 0;
Derrick Stoleee2838d82018-05-01 12:47:13 +0000886 }
887}
888
Stefan Beller4f542b72018-12-14 16:09:39 -0800889static int parse_commit_in_graph_one(struct repository *r,
890 struct commit_graph *g,
891 struct commit *item)
Derrick Stolee177722b2018-04-10 08:56:05 -0400892{
Derrick Stoleee2838d82018-05-01 12:47:13 +0000893 uint32_t pos;
894
Derrick Stolee177722b2018-04-10 08:56:05 -0400895 if (item->object.parsed)
896 return 1;
Derrick Stoleeee797052018-06-27 09:24:29 -0400897
898 if (find_commit_in_graph(item, g, &pos))
Stefan Beller4f542b72018-12-14 16:09:39 -0800899 return fill_commit_in_graph(r, item, g, pos);
Derrick Stoleeee797052018-06-27 09:24:29 -0400900
901 return 0;
902}
903
Jonathan Tandade47c2018-07-11 15:42:42 -0700904int parse_commit_in_graph(struct repository *r, struct commit *item)
Derrick Stoleeee797052018-06-27 09:24:29 -0400905{
Derrick Stolee7b671f82020-06-23 17:47:01 +0000906 static int checked_env = 0;
907
908 if (!checked_env &&
909 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
910 die("dying as requested by the '%s' variable on commit-graph parse!",
911 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
912 checked_env = 1;
913
Jonathan Tandade47c2018-07-11 15:42:42 -0700914 if (!prepare_commit_graph(r))
Derrick Stoleeee797052018-06-27 09:24:29 -0400915 return 0;
Stefan Beller4f542b72018-12-14 16:09:39 -0800916 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
Derrick Stolee177722b2018-04-10 08:56:05 -0400917}
918
Jonathan Tandade47c2018-07-11 15:42:42 -0700919void load_commit_graph_info(struct repository *r, struct commit *item)
Derrick Stoleee2838d82018-05-01 12:47:13 +0000920{
921 uint32_t pos;
Jonathan Tandade47c2018-07-11 15:42:42 -0700922 if (!prepare_commit_graph(r))
Derrick Stoleee2838d82018-05-01 12:47:13 +0000923 return;
Jonathan Tandade47c2018-07-11 15:42:42 -0700924 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
925 fill_commit_graph_info(item, r->objects->commit_graph, pos);
Derrick Stoleee2838d82018-05-01 12:47:13 +0000926}
927
Stefan Beller4f542b72018-12-14 16:09:39 -0800928static struct tree *load_tree_for_commit(struct repository *r,
929 struct commit_graph *g,
930 struct commit *c)
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000931{
932 struct object_id oid;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700933 const unsigned char *commit_data;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530934 uint32_t graph_pos = commit_graph_position(c);
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700935
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530936 while (graph_pos < g->num_commits_in_base)
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700937 g = g->base_graph;
938
939 commit_data = g->chunk_commit_data +
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530940 GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base);
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000941
942 hashcpy(oid.hash, commit_data);
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700943 set_commit_tree(c, lookup_tree(r, &oid));
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000944
945 return c->maybe_tree;
946}
947
Stefan Beller4f542b72018-12-14 16:09:39 -0800948static struct tree *get_commit_tree_in_graph_one(struct repository *r,
949 struct commit_graph *g,
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400950 const struct commit *c)
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000951{
952 if (c->maybe_tree)
953 return c->maybe_tree;
Abhishek Kumarc49c82a2020-06-17 14:44:10 +0530954 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400955 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000956
Stefan Beller4f542b72018-12-14 16:09:39 -0800957 return load_tree_for_commit(r, g, (struct commit *)c);
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400958}
959
Jonathan Tandade47c2018-07-11 15:42:42 -0700960struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400961{
Stefan Beller4f542b72018-12-14 16:09:39 -0800962 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000963}
964
Derrick Stoleec9905be2019-06-12 06:29:40 -0700965struct packed_commit_list {
966 struct commit **list;
Jeff King33613902020-12-07 14:11:08 -0500967 size_t nr;
968 size_t alloc;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700969};
970
Derrick Stoleec9905be2019-06-12 06:29:40 -0700971struct write_commit_graph_context {
972 struct repository *r;
Taylor Blau0bd52e22020-02-03 21:51:50 -0800973 struct object_directory *odb;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700974 char *graph_name;
Jeff Kinga5f1c442020-12-07 14:11:05 -0500975 struct oid_array oids;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700976 struct packed_commit_list commits;
977 int num_extra_edges;
Abhishek Kumare8b63002021-01-16 18:11:15 +0000978 int num_generation_data_overflows;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700979 unsigned long approx_nr_objects;
980 struct progress *progress;
981 int progress_done;
982 uint64_t progress_cnt;
Derrick Stolee6c622f92019-06-18 11:14:27 -0700983
984 char *base_graph_name;
985 int num_commit_graphs_before;
986 int num_commit_graphs_after;
987 char **commit_graph_filenames_before;
988 char **commit_graph_filenames_after;
989 char **commit_graph_hash_after;
990 uint32_t new_num_commits_in_base;
991 struct commit_graph *new_base_graph;
992
Derrick Stoleec9905be2019-06-12 06:29:40 -0700993 unsigned append:1,
Derrick Stolee6c622f92019-06-18 11:14:27 -0700994 report_progress:1,
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +0200995 split:1,
Garima Singh3d112752020-03-30 00:31:30 +0000996 changed_paths:1,
Abhishek Kumare8b63002021-01-16 18:11:15 +0000997 order_by_pack:1,
Derrick Stoleefde55b02021-02-02 03:01:22 +0000998 write_generation_data:1,
999 trust_generation_numbers:1;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001000
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +00001001 struct topo_level_slab *topo_levels;
Taylor Blau98bb7962020-09-17 22:59:49 -04001002 const struct commit_graph_opts *opts;
Garima Singhf97b9322020-03-30 00:31:28 +00001003 size_t total_bloom_filter_data_size;
Derrick Stolee98037f22020-06-23 17:47:00 +00001004 const struct bloom_filter_settings *bloom_settings;
Taylor Blau312cff52020-09-16 14:07:32 -04001005
1006 int count_bloom_filter_computed;
1007 int count_bloom_filter_not_computed;
Taylor Blau59f0d502020-09-17 22:59:44 -04001008 int count_bloom_filter_trunc_empty;
Taylor Blau312cff52020-09-16 14:07:32 -04001009 int count_bloom_filter_trunc_large;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001010};
1011
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001012static int write_graph_chunk_fanout(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001013 void *data)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001014{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001015 struct write_commit_graph_context *ctx = data;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001016 int i, count = 0;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001017 struct commit **list = ctx->commits.list;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001018
1019 /*
1020 * Write the first-level table (the list is sorted,
1021 * but we use a 256-entry lookup to be able to avoid
1022 * having to do eight extra binary search iterations).
1023 */
1024 for (i = 0; i < 256; i++) {
Derrick Stoleec9905be2019-06-12 06:29:40 -07001025 while (count < ctx->commits.nr) {
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001026 if ((*list)->object.oid.hash[0] != i)
1027 break;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001028 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001029 count++;
1030 list++;
1031 }
1032
1033 hashwrite_be32(f, count);
1034 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001035
1036 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001037}
1038
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001039static int write_graph_chunk_oids(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001040 void *data)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001041{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001042 struct write_commit_graph_context *ctx = data;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001043 struct commit **list = ctx->commits.list;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001044 int count;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001045 for (count = 0; count < ctx->commits.nr; count++, list++) {
1046 display_progress(ctx->progress, ++ctx->progress_cnt);
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001047 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +01001048 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001049
1050 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001051}
1052
Jeff King8380dcd2021-01-28 01:20:23 -05001053static const struct object_id *commit_to_oid(size_t index, const void *table)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001054{
Jeff King8380dcd2021-01-28 01:20:23 -05001055 const struct commit * const *commits = table;
Jeff King45ee13b2021-01-28 01:19:42 -05001056 return &commits[index]->object.oid;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001057}
1058
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001059static int write_graph_chunk_data(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001060 void *data)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001061{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001062 struct write_commit_graph_context *ctx = data;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001063 struct commit **list = ctx->commits.list;
1064 struct commit **last = ctx->commits.list + ctx->commits.nr;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001065 uint32_t num_extra_edges = 0;
1066
1067 while (list < last) {
1068 struct commit_list *parent;
Taylor Blau806278d2019-09-05 18:04:57 -04001069 struct object_id *tree;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001070 int edge_value;
1071 uint32_t packedDate[2];
Derrick Stoleec9905be2019-06-12 06:29:40 -07001072 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001073
Derrick Stoleec4cc0832021-02-01 17:15:03 +00001074 if (repo_parse_commit_no_graph(ctx->r, *list))
Taylor Blau16749b82019-09-05 18:04:55 -04001075 die(_("unable to parse commit %s"),
1076 oid_to_hex(&(*list)->object.oid));
Taylor Blau806278d2019-09-05 18:04:57 -04001077 tree = get_commit_tree_oid(*list);
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001078 hashwrite(f, tree->hash, the_hash_algo->rawsz);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001079
1080 parent = (*list)->parents;
1081
1082 if (!parent)
1083 edge_value = GRAPH_PARENT_NONE;
1084 else {
Jeff King45ee13b2021-01-28 01:19:42 -05001085 edge_value = oid_pos(&parent->item->object.oid,
1086 ctx->commits.list,
1087 ctx->commits.nr,
1088 commit_to_oid);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001089
Derrick Stolee6c622f92019-06-18 11:14:27 -07001090 if (edge_value >= 0)
1091 edge_value += ctx->new_num_commits_in_base;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001092 else if (ctx->new_base_graph) {
Derrick Stolee6c622f92019-06-18 11:14:27 -07001093 uint32_t pos;
1094 if (find_commit_in_graph(parent->item,
1095 ctx->new_base_graph,
1096 &pos))
1097 edge_value = pos;
1098 }
1099
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001100 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -08001101 BUG("missing parent %s for commit %s",
1102 oid_to_hex(&parent->item->object.oid),
1103 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001104 }
1105
1106 hashwrite_be32(f, edge_value);
1107
1108 if (parent)
1109 parent = parent->next;
1110
1111 if (!parent)
1112 edge_value = GRAPH_PARENT_NONE;
1113 else if (parent->next)
SZEDER Gábor5af74172019-01-19 21:21:13 +01001114 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001115 else {
Jeff King45ee13b2021-01-28 01:19:42 -05001116 edge_value = oid_pos(&parent->item->object.oid,
1117 ctx->commits.list,
1118 ctx->commits.nr,
1119 commit_to_oid);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001120
1121 if (edge_value >= 0)
1122 edge_value += ctx->new_num_commits_in_base;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001123 else if (ctx->new_base_graph) {
Derrick Stolee6c622f92019-06-18 11:14:27 -07001124 uint32_t pos;
1125 if (find_commit_in_graph(parent->item,
1126 ctx->new_base_graph,
1127 &pos))
1128 edge_value = pos;
1129 }
1130
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001131 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -08001132 BUG("missing parent %s for commit %s",
1133 oid_to_hex(&parent->item->object.oid),
1134 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001135 }
1136
1137 hashwrite_be32(f, edge_value);
1138
SZEDER Gábor5af74172019-01-19 21:21:13 +01001139 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001140 do {
1141 num_extra_edges++;
1142 parent = parent->next;
1143 } while (parent);
1144 }
1145
1146 if (sizeof((*list)->date) > 4)
1147 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1148 else
1149 packedDate[0] = 0;
1150
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +00001151 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
Derrick Stolee3258c662018-05-01 12:47:09 +00001152
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001153 packedDate[1] = htonl((*list)->date);
1154 hashwrite(f, packedDate, 8);
1155
1156 list++;
1157 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001158
1159 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001160}
1161
Abhishek Kumare8b63002021-01-16 18:11:15 +00001162static int write_graph_chunk_generation_data(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001163 void *data)
Abhishek Kumare8b63002021-01-16 18:11:15 +00001164{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001165 struct write_commit_graph_context *ctx = data;
Abhishek Kumare8b63002021-01-16 18:11:15 +00001166 int i, num_generation_data_overflows = 0;
1167
1168 for (i = 0; i < ctx->commits.nr; i++) {
1169 struct commit *c = ctx->commits.list[i];
Derrick Stolee90cb1c42021-02-01 17:15:04 +00001170 timestamp_t offset;
1171 repo_parse_commit(ctx->r, c);
1172 offset = commit_graph_data_at(c)->generation - c->date;
Abhishek Kumare8b63002021-01-16 18:11:15 +00001173 display_progress(ctx->progress, ++ctx->progress_cnt);
1174
1175 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1176 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1177 num_generation_data_overflows++;
1178 }
1179
1180 hashwrite_be32(f, offset);
1181 }
1182
1183 return 0;
1184}
1185
1186static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001187 void *data)
Abhishek Kumare8b63002021-01-16 18:11:15 +00001188{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001189 struct write_commit_graph_context *ctx = data;
Abhishek Kumare8b63002021-01-16 18:11:15 +00001190 int i;
1191 for (i = 0; i < ctx->commits.nr; i++) {
1192 struct commit *c = ctx->commits.list[i];
1193 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1194 display_progress(ctx->progress, ++ctx->progress_cnt);
1195
1196 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1197 hashwrite_be32(f, offset >> 32);
1198 hashwrite_be32(f, (uint32_t) offset);
1199 }
1200 }
1201
1202 return 0;
1203}
1204
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001205static int write_graph_chunk_extra_edges(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001206 void *data)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001207{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001208 struct write_commit_graph_context *ctx = data;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001209 struct commit **list = ctx->commits.list;
1210 struct commit **last = ctx->commits.list + ctx->commits.nr;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001211 struct commit_list *parent;
1212
1213 while (list < last) {
1214 int num_parents = 0;
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +01001215
Derrick Stoleec9905be2019-06-12 06:29:40 -07001216 display_progress(ctx->progress, ++ctx->progress_cnt);
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +01001217
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001218 for (parent = (*list)->parents; num_parents < 3 && parent;
1219 parent = parent->next)
1220 num_parents++;
1221
1222 if (num_parents <= 2) {
1223 list++;
1224 continue;
1225 }
1226
1227 /* Since num_parents > 2, this initializer is safe. */
1228 for (parent = (*list)->parents->next; parent; parent = parent->next) {
Jeff King45ee13b2021-01-28 01:19:42 -05001229 int edge_value = oid_pos(&parent->item->object.oid,
1230 ctx->commits.list,
1231 ctx->commits.nr,
1232 commit_to_oid);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001233
Derrick Stolee6c622f92019-06-18 11:14:27 -07001234 if (edge_value >= 0)
1235 edge_value += ctx->new_num_commits_in_base;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001236 else if (ctx->new_base_graph) {
Derrick Stolee6c622f92019-06-18 11:14:27 -07001237 uint32_t pos;
1238 if (find_commit_in_graph(parent->item,
1239 ctx->new_base_graph,
1240 &pos))
1241 edge_value = pos;
1242 }
1243
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001244 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -08001245 BUG("missing parent %s for commit %s",
1246 oid_to_hex(&parent->item->object.oid),
1247 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001248 else if (!parent->next)
1249 edge_value |= GRAPH_LAST_EDGE;
1250
1251 hashwrite_be32(f, edge_value);
1252 }
1253
1254 list++;
1255 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001256
1257 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001258}
1259
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001260static int write_graph_chunk_bloom_indexes(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001261 void *data)
Garima Singh76ffbca2020-04-06 16:59:49 +00001262{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001263 struct write_commit_graph_context *ctx = data;
Garima Singh76ffbca2020-04-06 16:59:49 +00001264 struct commit **list = ctx->commits.list;
1265 struct commit **last = ctx->commits.list + ctx->commits.nr;
1266 uint32_t cur_pos = 0;
Garima Singh76ffbca2020-04-06 16:59:49 +00001267
1268 while (list < last) {
Taylor Blau312cff52020-09-16 14:07:32 -04001269 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
Derrick Stolee94919742020-07-01 13:27:23 +00001270 size_t len = filter ? filter->len : 0;
1271 cur_pos += len;
SZEDER Gábor150cd3b2020-07-09 19:00:03 +02001272 display_progress(ctx->progress, ++ctx->progress_cnt);
Garima Singh76ffbca2020-04-06 16:59:49 +00001273 hashwrite_be32(f, cur_pos);
1274 list++;
1275 }
1276
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001277 return 0;
Garima Singh76ffbca2020-04-06 16:59:49 +00001278}
1279
Derrick Stolee0087a872020-07-01 13:27:24 +00001280static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1281{
1282 struct json_writer jw = JSON_WRITER_INIT;
1283
1284 jw_object_begin(&jw, 0);
1285 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1286 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1287 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
Taylor Blau97ffa4f2020-09-17 09:34:42 -04001288 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
Derrick Stolee0087a872020-07-01 13:27:24 +00001289 jw_end(&jw);
1290
1291 trace2_data_json("bloom", ctx->r, "settings", &jw);
1292
1293 jw_release(&jw);
1294}
1295
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001296static int write_graph_chunk_bloom_data(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001297 void *data)
Garima Singh76ffbca2020-04-06 16:59:49 +00001298{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001299 struct write_commit_graph_context *ctx = data;
Garima Singh76ffbca2020-04-06 16:59:49 +00001300 struct commit **list = ctx->commits.list;
1301 struct commit **last = ctx->commits.list + ctx->commits.nr;
Garima Singh76ffbca2020-04-06 16:59:49 +00001302
Derrick Stolee0087a872020-07-01 13:27:24 +00001303 trace2_bloom_filter_settings(ctx);
1304
Derrick Stolee98037f22020-06-23 17:47:00 +00001305 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1306 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1307 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
Garima Singh76ffbca2020-04-06 16:59:49 +00001308
1309 while (list < last) {
Taylor Blau312cff52020-09-16 14:07:32 -04001310 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
Derrick Stolee94919742020-07-01 13:27:23 +00001311 size_t len = filter ? filter->len : 0;
Derrick Stolee94919742020-07-01 13:27:23 +00001312
SZEDER Gábor150cd3b2020-07-09 19:00:03 +02001313 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee94919742020-07-01 13:27:23 +00001314 if (len)
1315 hashwrite(f, filter->data, len * sizeof(unsigned char));
Garima Singh76ffbca2020-04-06 16:59:49 +00001316 list++;
1317 }
1318
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001319 return 0;
Garima Singh76ffbca2020-04-06 16:59:49 +00001320}
1321
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001322static int add_packed_commits(const struct object_id *oid,
1323 struct packed_git *pack,
1324 uint32_t pos,
1325 void *data)
1326{
Derrick Stoleec9905be2019-06-12 06:29:40 -07001327 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001328 enum object_type type;
1329 off_t offset = nth_packed_object_offset(pack, pos);
1330 struct object_info oi = OBJECT_INFO_INIT;
1331
Derrick Stoleec9905be2019-06-12 06:29:40 -07001332 if (ctx->progress)
1333 display_progress(ctx->progress, ++ctx->progress_done);
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +00001334
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001335 oi.typep = &type;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001336 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
Nguyễn Thái Ngọc Duy4f5b5322018-07-21 09:49:26 +02001337 die(_("unable to get type of object %s"), oid_to_hex(oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001338
1339 if (type != OBJ_COMMIT)
1340 return 0;
1341
Jeff Kinga5f1c442020-12-07 14:11:05 -05001342 oid_array_append(&ctx->oids, oid);
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001343 set_commit_pos(ctx->r, oid);
1344
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001345 return 0;
1346}
1347
Derrick Stoleec9905be2019-06-12 06:29:40 -07001348static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001349{
1350 struct commit_list *parent;
1351 for (parent = commit->parents; parent; parent = parent->next) {
Derrick Stoleecb99a342019-10-24 13:40:42 +00001352 if (!(parent->item->object.flags & REACHABLE)) {
Jeff Kinga5f1c442020-12-07 14:11:05 -05001353 oid_array_append(&ctx->oids, &parent->item->object.oid);
Derrick Stoleecb99a342019-10-24 13:40:42 +00001354 parent->item->object.flags |= REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001355 }
1356 }
1357}
1358
Derrick Stoleec9905be2019-06-12 06:29:40 -07001359static void close_reachable(struct write_commit_graph_context *ctx)
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001360{
Ævar Arnfjörð Bjarmason49bbc572019-01-19 21:21:21 +01001361 int i;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001362 struct commit *commit;
Taylor Blau98bb7962020-09-17 22:59:49 -04001363 enum commit_graph_split_flags flags = ctx->opts ?
1364 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001365
Derrick Stoleec9905be2019-06-12 06:29:40 -07001366 if (ctx->report_progress)
1367 ctx->progress = start_delayed_progress(
1368 _("Loading known commits in commit graph"),
1369 ctx->oids.nr);
1370 for (i = 0; i < ctx->oids.nr; i++) {
1371 display_progress(ctx->progress, i + 1);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001372 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001373 if (commit)
Derrick Stoleecb99a342019-10-24 13:40:42 +00001374 commit->object.flags |= REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001375 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001376 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001377
1378 /*
Derrick Stoleec9905be2019-06-12 06:29:40 -07001379 * As this loop runs, ctx->oids.nr may grow, but not more
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001380 * than the number of missing commits in the reachable
1381 * closure.
1382 */
Derrick Stoleec9905be2019-06-12 06:29:40 -07001383 if (ctx->report_progress)
1384 ctx->progress = start_delayed_progress(
1385 _("Expanding reachable commits in commit graph"),
SZEDER Gábor67fa6aa2019-09-07 01:01:33 -04001386 0);
Derrick Stoleec9905be2019-06-12 06:29:40 -07001387 for (i = 0; i < ctx->oids.nr; i++) {
1388 display_progress(ctx->progress, i + 1);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001389 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001390
Derrick Stolee6c622f92019-06-18 11:14:27 -07001391 if (!commit)
1392 continue;
1393 if (ctx->split) {
Derrick Stoleec4cc0832021-02-01 17:15:03 +00001394 if ((!repo_parse_commit(ctx->r, commit) &&
Abhishek Kumarc49c82a2020-06-17 14:44:10 +05301395 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
Taylor Blau8a6ac282020-04-13 22:04:17 -06001396 flags == COMMIT_GRAPH_SPLIT_REPLACE)
Derrick Stolee6c622f92019-06-18 11:14:27 -07001397 add_missing_parents(ctx, commit);
Derrick Stoleec4cc0832021-02-01 17:15:03 +00001398 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
Derrick Stoleec9905be2019-06-12 06:29:40 -07001399 add_missing_parents(ctx, commit);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001400 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001401 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001402
Derrick Stoleec9905be2019-06-12 06:29:40 -07001403 if (ctx->report_progress)
1404 ctx->progress = start_delayed_progress(
1405 _("Clearing commit marks in commit graph"),
1406 ctx->oids.nr);
1407 for (i = 0; i < ctx->oids.nr; i++) {
1408 display_progress(ctx->progress, i + 1);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001409 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001410
1411 if (commit)
Derrick Stoleecb99a342019-10-24 13:40:42 +00001412 commit->object.flags &= ~REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001413 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001414 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001415}
1416
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00001417static void compute_topological_levels(struct write_commit_graph_context *ctx)
1418{
1419 int i;
1420 struct commit_list *list = NULL;
1421
1422 if (ctx->report_progress)
1423 ctx->progress = start_delayed_progress(
1424 _("Computing commit graph topological levels"),
1425 ctx->commits.nr);
1426 for (i = 0; i < ctx->commits.nr; i++) {
1427 struct commit *c = ctx->commits.list[i];
1428 uint32_t level;
1429
1430 repo_parse_commit(ctx->r, c);
1431 level = *topo_level_slab_at(ctx->topo_levels, c);
1432
1433 display_progress(ctx->progress, i + 1);
1434 if (level != GENERATION_NUMBER_ZERO)
1435 continue;
1436
1437 commit_list_insert(c, &list);
1438 while (list) {
1439 struct commit *current = list->item;
1440 struct commit_list *parent;
1441 int all_parents_computed = 1;
1442 uint32_t max_level = 0;
1443
1444 for (parent = current->parents; parent; parent = parent->next) {
1445 repo_parse_commit(ctx->r, parent->item);
1446 level = *topo_level_slab_at(ctx->topo_levels, parent->item);
1447
1448 if (level == GENERATION_NUMBER_ZERO) {
1449 all_parents_computed = 0;
1450 commit_list_insert(parent->item, &list);
1451 break;
1452 }
1453
1454 if (level > max_level)
1455 max_level = level;
1456 }
1457
1458 if (all_parents_computed) {
1459 pop_commit(&list);
1460
1461 if (max_level > GENERATION_NUMBER_V1_MAX - 1)
1462 max_level = GENERATION_NUMBER_V1_MAX - 1;
1463 *topo_level_slab_at(ctx->topo_levels, current) = max_level + 1;
1464 }
1465 }
1466 }
1467 stop_progress(&ctx->progress);
1468}
1469
Derrick Stoleec9905be2019-06-12 06:29:40 -07001470static void compute_generation_numbers(struct write_commit_graph_context *ctx)
Derrick Stolee3258c662018-05-01 12:47:09 +00001471{
1472 int i;
1473 struct commit_list *list = NULL;
1474
Derrick Stoleec9905be2019-06-12 06:29:40 -07001475 if (ctx->report_progress)
Derrick Stoleeecc08692019-11-25 21:28:23 +00001476 ctx->progress = start_delayed_progress(
Derrick Stoleec9905be2019-06-12 06:29:40 -07001477 _("Computing commit graph generation numbers"),
1478 ctx->commits.nr);
Derrick Stoleefde55b02021-02-02 03:01:22 +00001479
1480 if (!ctx->trust_generation_numbers) {
1481 for (i = 0; i < ctx->commits.nr; i++) {
1482 struct commit *c = ctx->commits.list[i];
1483 repo_parse_commit(ctx->r, c);
1484 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1485 }
1486 }
1487
Derrick Stoleec9905be2019-06-12 06:29:40 -07001488 for (i = 0; i < ctx->commits.nr; i++) {
Derrick Stolee90cb1c42021-02-01 17:15:04 +00001489 struct commit *c = ctx->commits.list[i];
Derrick Stolee90cb1c42021-02-01 17:15:04 +00001490 timestamp_t corrected_commit_date;
1491
1492 repo_parse_commit(ctx->r, c);
Derrick Stolee90cb1c42021-02-01 17:15:04 +00001493 corrected_commit_date = commit_graph_data_at(c)->generation;
Abhishek Kumar48448122020-06-17 14:44:09 +05301494
Derrick Stoleec9905be2019-06-12 06:29:40 -07001495 display_progress(ctx->progress, i + 1);
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00001496 if (corrected_commit_date != GENERATION_NUMBER_ZERO)
Derrick Stolee3258c662018-05-01 12:47:09 +00001497 continue;
1498
Derrick Stolee90cb1c42021-02-01 17:15:04 +00001499 commit_list_insert(c, &list);
Derrick Stolee3258c662018-05-01 12:47:09 +00001500 while (list) {
1501 struct commit *current = list->item;
1502 struct commit_list *parent;
1503 int all_parents_computed = 1;
Abhishek Kumarc1a09112021-01-16 18:11:14 +00001504 timestamp_t max_corrected_commit_date = 0;
Derrick Stolee3258c662018-05-01 12:47:09 +00001505
1506 for (parent = current->parents; parent; parent = parent->next) {
Derrick Stolee90cb1c42021-02-01 17:15:04 +00001507 repo_parse_commit(ctx->r, parent->item);
Abhishek Kumarc1a09112021-01-16 18:11:14 +00001508 corrected_commit_date = commit_graph_data_at(parent->item)->generation;
Abhishek Kumar48448122020-06-17 14:44:09 +05301509
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00001510 if (corrected_commit_date == GENERATION_NUMBER_ZERO) {
Derrick Stolee3258c662018-05-01 12:47:09 +00001511 all_parents_computed = 0;
1512 commit_list_insert(parent->item, &list);
1513 break;
Derrick Stolee3258c662018-05-01 12:47:09 +00001514 }
Abhishek Kumarc1a09112021-01-16 18:11:14 +00001515
Abhishek Kumarc1a09112021-01-16 18:11:14 +00001516 if (corrected_commit_date > max_corrected_commit_date)
1517 max_corrected_commit_date = corrected_commit_date;
Derrick Stolee3258c662018-05-01 12:47:09 +00001518 }
1519
1520 if (all_parents_computed) {
Derrick Stolee3258c662018-05-01 12:47:09 +00001521 pop_commit(&list);
1522
Abhishek Kumarc1a09112021-01-16 18:11:14 +00001523 if (current->date && current->date > max_corrected_commit_date)
1524 max_corrected_commit_date = current->date - 1;
1525 commit_graph_data_at(current)->generation = max_corrected_commit_date + 1;
Abhishek Kumare8b63002021-01-16 18:11:15 +00001526
1527 if (commit_graph_data_at(current)->generation - current->date > GENERATION_NUMBER_V2_OFFSET_MAX)
1528 ctx->num_generation_data_overflows++;
Derrick Stolee3258c662018-05-01 12:47:09 +00001529 }
1530 }
1531 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001532 stop_progress(&ctx->progress);
Derrick Stolee3258c662018-05-01 12:47:09 +00001533}
1534
Taylor Blau312cff52020-09-16 14:07:32 -04001535static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1536{
1537 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1538 ctx->count_bloom_filter_computed);
1539 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1540 ctx->count_bloom_filter_not_computed);
Taylor Blau59f0d502020-09-17 22:59:44 -04001541 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1542 ctx->count_bloom_filter_trunc_empty);
Taylor Blau312cff52020-09-16 14:07:32 -04001543 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1544 ctx->count_bloom_filter_trunc_large);
1545}
1546
Garima Singhf97b9322020-03-30 00:31:28 +00001547static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1548{
1549 int i;
1550 struct progress *progress = NULL;
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001551 struct commit **sorted_commits;
Taylor Blau809e0322020-09-18 09:27:27 -04001552 int max_new_filters;
Garima Singhf97b9322020-03-30 00:31:28 +00001553
1554 init_bloom_filters();
1555
1556 if (ctx->report_progress)
1557 progress = start_delayed_progress(
1558 _("Computing commit changed paths Bloom filters"),
1559 ctx->commits.nr);
1560
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001561 ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1562 COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
Garima Singh3d112752020-03-30 00:31:30 +00001563
1564 if (ctx->order_by_pack)
1565 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1566 else
1567 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001568
Taylor Blau809e0322020-09-18 09:27:27 -04001569 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1570 ctx->opts->max_new_filters : ctx->commits.nr;
1571
Garima Singhf97b9322020-03-30 00:31:28 +00001572 for (i = 0; i < ctx->commits.nr; i++) {
Taylor Blau312cff52020-09-16 14:07:32 -04001573 enum bloom_filter_computed computed = 0;
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001574 struct commit *c = sorted_commits[i];
Taylor Blau312cff52020-09-16 14:07:32 -04001575 struct bloom_filter *filter = get_or_compute_bloom_filter(
1576 ctx->r,
1577 c,
Taylor Blau809e0322020-09-18 09:27:27 -04001578 ctx->count_bloom_filter_computed < max_new_filters,
Taylor Blau9a7a9ed2020-09-16 14:07:46 -04001579 ctx->bloom_settings,
Taylor Blau312cff52020-09-16 14:07:32 -04001580 &computed);
1581 if (computed & BLOOM_COMPUTED) {
1582 ctx->count_bloom_filter_computed++;
Taylor Blau59f0d502020-09-17 22:59:44 -04001583 if (computed & BLOOM_TRUNC_EMPTY)
1584 ctx->count_bloom_filter_trunc_empty++;
Taylor Blau312cff52020-09-16 14:07:32 -04001585 if (computed & BLOOM_TRUNC_LARGE)
1586 ctx->count_bloom_filter_trunc_large++;
1587 } else if (computed & BLOOM_NOT_COMPUTED)
1588 ctx->count_bloom_filter_not_computed++;
Taylor Blau809e0322020-09-18 09:27:27 -04001589 ctx->total_bloom_filter_data_size += filter
1590 ? sizeof(unsigned char) * filter->len : 0;
Garima Singhf97b9322020-03-30 00:31:28 +00001591 display_progress(progress, i + 1);
1592 }
1593
Taylor Blau312cff52020-09-16 14:07:32 -04001594 if (trace2_is_enabled())
1595 trace2_bloom_filter_write_statistics(ctx);
1596
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001597 free(sorted_commits);
Garima Singhf97b9322020-03-30 00:31:28 +00001598 stop_progress(&progress);
1599}
1600
Taylor Blau1fe10842020-05-04 19:13:35 -06001601struct refs_cb_data {
1602 struct oidset *commits;
Taylor Blaud335ce82020-05-13 15:59:33 -06001603 struct progress *progress;
Taylor Blau1fe10842020-05-04 19:13:35 -06001604};
1605
Taylor Blau6830c362020-04-13 22:04:25 -06001606static int add_ref_to_set(const char *refname,
1607 const struct object_id *oid,
1608 int flags, void *cb_data)
Derrick Stolee59fb8772018-06-27 09:24:45 -04001609{
Taylor Blau630cd512020-05-13 15:59:37 -06001610 struct object_id peeled;
Taylor Blau1fe10842020-05-04 19:13:35 -06001611 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001612
Jeff King36a31792021-01-20 14:44:43 -05001613 if (!peel_iterated_oid(oid, &peeled))
Taylor Blau630cd512020-05-13 15:59:37 -06001614 oid = &peeled;
1615 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1616 oidset_insert(data->commits, oid);
Taylor Blaud335ce82020-05-13 15:59:33 -06001617
1618 display_progress(data->progress, oidset_size(data->commits));
1619
Derrick Stolee59fb8772018-06-27 09:24:45 -04001620 return 0;
1621}
1622
Taylor Blau0bd52e22020-02-03 21:51:50 -08001623int write_commit_graph_reachable(struct object_directory *odb,
SZEDER Gábor39d88312019-08-05 10:02:39 +02001624 enum commit_graph_write_flags flags,
Taylor Blau98bb7962020-09-17 22:59:49 -04001625 const struct commit_graph_opts *opts)
Derrick Stolee59fb8772018-06-27 09:24:45 -04001626{
Taylor Blau6830c362020-04-13 22:04:25 -06001627 struct oidset commits = OIDSET_INIT;
Taylor Blau1fe10842020-05-04 19:13:35 -06001628 struct refs_cb_data data;
Derrick Stoleee103f722019-06-12 06:29:37 -07001629 int result;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001630
Taylor Blau1fe10842020-05-04 19:13:35 -06001631 memset(&data, 0, sizeof(data));
1632 data.commits = &commits;
Taylor Blaud335ce82020-05-13 15:59:33 -06001633 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1634 data.progress = start_delayed_progress(
1635 _("Collecting referenced commits"), 0);
Taylor Blau1fe10842020-05-04 19:13:35 -06001636
1637 for_each_ref(add_ref_to_set, &data);
SZEDER Gábor6f9d5f22020-07-09 18:54:32 +02001638
1639 stop_progress(&data.progress);
1640
Taylor Blau6830c362020-04-13 22:04:25 -06001641 result = write_commit_graph(odb, NULL, &commits,
Taylor Blau98bb7962020-09-17 22:59:49 -04001642 flags, opts);
Derrick Stoleef4dbdfc2018-10-03 10:12:15 -07001643
Taylor Blau6830c362020-04-13 22:04:25 -06001644 oidset_clear(&commits);
Derrick Stoleee103f722019-06-12 06:29:37 -07001645 return result;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001646}
1647
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001648static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1649 struct string_list *pack_indexes)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001650{
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001651 uint32_t i;
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001652 struct strbuf progress_title = STRBUF_INIT;
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001653 struct strbuf packname = STRBUF_INIT;
1654 int dirlen;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001655
Taylor Blau0bd52e22020-02-03 21:51:50 -08001656 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001657 dirlen = packname.len;
1658 if (ctx->report_progress) {
1659 strbuf_addf(&progress_title,
1660 Q_("Finding commits for commit graph in %d pack",
1661 "Finding commits for commit graph in %d packs",
1662 pack_indexes->nr),
1663 pack_indexes->nr);
1664 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1665 ctx->progress_done = 0;
Derrick Stolee7547b952018-04-10 08:56:08 -04001666 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001667 for (i = 0; i < pack_indexes->nr; i++) {
1668 struct packed_git *p;
1669 strbuf_setlen(&packname, dirlen);
1670 strbuf_addstr(&packname, pack_indexes->items[i].string);
1671 p = add_packed_git(packname.buf, packname.len, 1);
1672 if (!p) {
1673 error(_("error adding pack %s"), packname.buf);
1674 return -1;
Derrick Stolee7547b952018-04-10 08:56:08 -04001675 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001676 if (open_pack_index(p)) {
1677 error(_("error opening index for %s"), packname.buf);
1678 return -1;
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +00001679 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001680 for_each_object_in_pack(p, add_packed_commits, ctx,
1681 FOR_EACH_OBJECT_PACK_ORDER);
1682 close_pack(p);
1683 free(p);
Derrick Stolee3d5df012018-04-10 08:56:07 -04001684 }
1685
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001686 stop_progress(&ctx->progress);
René Scharfe0aa6bce2019-08-07 13:15:02 +02001687 strbuf_release(&progress_title);
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001688 strbuf_release(&packname);
Derrick Stolee3d5df012018-04-10 08:56:07 -04001689
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001690 return 0;
1691}
Derrick Stolee3d5df012018-04-10 08:56:07 -04001692
Taylor Blau6830c362020-04-13 22:04:25 -06001693static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1694 struct oidset *commits)
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001695{
Taylor Blau6830c362020-04-13 22:04:25 -06001696 struct oidset_iter iter;
1697 struct object_id *oid;
1698
1699 if (!oidset_size(commits))
1700 return 0;
Derrick Stolee3d5df012018-04-10 08:56:07 -04001701
Taylor Blau6830c362020-04-13 22:04:25 -06001702 oidset_iter_init(commits, &iter);
1703 while ((oid = oidset_iter_next(&iter))) {
Jeff Kinga5f1c442020-12-07 14:11:05 -05001704 oid_array_append(&ctx->oids, oid);
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001705 }
Taylor Blau6830c362020-04-13 22:04:25 -06001706
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001707 return 0;
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001708}
1709
Derrick Stoleeb2c83062019-06-12 06:29:42 -07001710static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1711{
1712 if (ctx->report_progress)
1713 ctx->progress = start_delayed_progress(
1714 _("Finding commits for commit graph among packed objects"),
1715 ctx->approx_nr_objects);
1716 for_each_packed_object(add_packed_commits, ctx,
1717 FOR_EACH_OBJECT_PACK_ORDER);
1718 if (ctx->progress_done < ctx->approx_nr_objects)
1719 display_progress(ctx->progress, ctx->approx_nr_objects);
1720 stop_progress(&ctx->progress);
1721}
1722
Derrick Stoleef998d542019-06-12 06:29:44 -07001723static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1724{
1725 uint32_t i;
Taylor Blau98bb7962020-09-17 22:59:49 -04001726 enum commit_graph_split_flags flags = ctx->opts ?
1727 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
Derrick Stoleef998d542019-06-12 06:29:44 -07001728
1729 ctx->num_extra_edges = 0;
1730 if (ctx->report_progress)
1731 ctx->progress = start_delayed_progress(
1732 _("Finding extra edges in commit graph"),
1733 ctx->oids.nr);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001734 oid_array_sort(&ctx->oids);
1735 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
René Scharfe689a1462019-09-15 19:07:44 +02001736 unsigned int num_parents;
1737
Derrick Stoleef998d542019-06-12 06:29:44 -07001738 display_progress(ctx->progress, i + 1);
Derrick Stoleef998d542019-06-12 06:29:44 -07001739
Derrick Stolee6c622f92019-06-18 11:14:27 -07001740 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001741 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001742
Taylor Blau8a6ac282020-04-13 22:04:17 -06001743 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
Abhishek Kumarc49c82a2020-06-17 14:44:10 +05301744 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
Derrick Stolee6c622f92019-06-18 11:14:27 -07001745 continue;
1746
Taylor Blau8a6ac282020-04-13 22:04:17 -06001747 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
Derrick Stoleec4cc0832021-02-01 17:15:03 +00001748 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
Taylor Blau8a6ac282020-04-13 22:04:17 -06001749 else
Derrick Stoleec4cc0832021-02-01 17:15:03 +00001750 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
Derrick Stoleef998d542019-06-12 06:29:44 -07001751
René Scharfe689a1462019-09-15 19:07:44 +02001752 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001753 if (num_parents > 2)
Derrick Stoleef998d542019-06-12 06:29:44 -07001754 ctx->num_extra_edges += num_parents - 1;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001755
Derrick Stoleef998d542019-06-12 06:29:44 -07001756 ctx->commits.nr++;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001757 }
Derrick Stoleef998d542019-06-12 06:29:44 -07001758 stop_progress(&ctx->progress);
1759}
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001760
Derrick Stolee6c622f92019-06-18 11:14:27 -07001761static int write_graph_chunk_base_1(struct hashfile *f,
1762 struct commit_graph *g)
1763{
1764 int num = 0;
1765
1766 if (!g)
1767 return 0;
1768
1769 num = write_graph_chunk_base_1(f, g->base_graph);
1770 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1771 return num + 1;
1772}
1773
1774static int write_graph_chunk_base(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001775 void *data)
Derrick Stolee6c622f92019-06-18 11:14:27 -07001776{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001777 struct write_commit_graph_context *ctx = data;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001778 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1779
1780 if (num != ctx->num_commit_graphs_after - 1) {
1781 error(_("failed to write correct number of base graph ids"));
1782 return -1;
1783 }
1784
1785 return 0;
1786}
1787
Derrick Stolee238def52019-06-12 06:29:45 -07001788static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1789{
1790 uint32_t i;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001791 int fd;
Derrick Stolee238def52019-06-12 06:29:45 -07001792 struct hashfile *f;
1793 struct lock_file lk = LOCK_INIT;
Derrick Stolee238def52019-06-12 06:29:45 -07001794 const unsigned hashsz = the_hash_algo->rawsz;
1795 struct strbuf progress_title = STRBUF_INIT;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001796 struct object_id file_hash;
Derrick Stolee47410aa2021-02-18 14:07:25 +00001797 struct chunkfile *cf;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001798
Derrick Stolee6c622f92019-06-18 11:14:27 -07001799 if (ctx->split) {
1800 struct strbuf tmp_file = STRBUF_INIT;
1801
1802 strbuf_addf(&tmp_file,
1803 "%s/info/commit-graphs/tmp_graph_XXXXXX",
Taylor Blau0bd52e22020-02-03 21:51:50 -08001804 ctx->odb->path);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001805 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1806 } else {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001807 ctx->graph_name = get_commit_graph_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001808 }
1809
Derrick Stolee238def52019-06-12 06:29:45 -07001810 if (safe_create_leading_directories(ctx->graph_name)) {
1811 UNLEAK(ctx->graph_name);
1812 error(_("unable to create leading directories of %s"),
1813 ctx->graph_name);
1814 return -1;
Derrick Stoleef4dbdfc2018-10-03 10:12:15 -07001815 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001816
Derrick Stolee6c622f92019-06-18 11:14:27 -07001817 if (ctx->split) {
Derrick Stolee663b2b12020-09-17 18:11:46 +00001818 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001819
Taylor Blau45a43652020-04-29 11:36:46 -06001820 hold_lock_file_for_update_mode(&lk, lock_name,
1821 LOCK_DIE_ON_ERROR, 0444);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001822
1823 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1824 if (fd < 0) {
Taylor Blaua2d57e22020-04-23 15:41:02 -06001825 error(_("unable to create temporary graph layer"));
Derrick Stolee6c622f92019-06-18 11:14:27 -07001826 return -1;
1827 }
1828
Taylor Blauf4d62842020-04-29 11:36:42 -06001829 if (adjust_shared_perm(ctx->graph_name)) {
1830 error(_("unable to adjust shared permissions for '%s'"),
1831 ctx->graph_name);
1832 return -1;
1833 }
1834
Derrick Stolee6c622f92019-06-18 11:14:27 -07001835 f = hashfd(fd, ctx->graph_name);
1836 } else {
Taylor Blau1f9beca2020-04-29 11:36:38 -06001837 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1838 LOCK_DIE_ON_ERROR, 0444);
Martin Ågrena52cdce2021-01-05 20:23:47 +01001839 fd = get_lock_file_fd(&lk);
1840 f = hashfd(fd, get_lock_file_path(&lk));
Derrick Stolee6c622f92019-06-18 11:14:27 -07001841 }
Derrick Stolee238def52019-06-12 06:29:45 -07001842
Derrick Stolee47410aa2021-02-18 14:07:25 +00001843 cf = init_chunkfile(f);
1844
1845 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
1846 write_graph_chunk_fanout);
1847 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, hashsz * ctx->commits.nr,
1848 write_graph_chunk_oids);
1849 add_chunk(cf, GRAPH_CHUNKID_DATA, (hashsz + 16) * ctx->commits.nr,
1850 write_graph_chunk_data);
Abhishek Kumare8b63002021-01-16 18:11:15 +00001851
Derrick Stolee47410aa2021-02-18 14:07:25 +00001852 if (ctx->write_generation_data)
1853 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
1854 sizeof(uint32_t) * ctx->commits.nr,
1855 write_graph_chunk_generation_data);
1856 if (ctx->num_generation_data_overflows)
1857 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
1858 sizeof(timestamp_t) * ctx->num_generation_data_overflows,
1859 write_graph_chunk_generation_data_overflow);
1860 if (ctx->num_extra_edges)
1861 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
1862 4 * ctx->num_extra_edges,
1863 write_graph_chunk_extra_edges);
Garima Singh76ffbca2020-04-06 16:59:49 +00001864 if (ctx->changed_paths) {
Derrick Stolee47410aa2021-02-18 14:07:25 +00001865 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
1866 sizeof(uint32_t) * ctx->commits.nr,
1867 write_graph_chunk_bloom_indexes);
1868 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
1869 sizeof(uint32_t) * 3
1870 + ctx->total_bloom_filter_data_size,
1871 write_graph_chunk_bloom_data);
Garima Singh76ffbca2020-04-06 16:59:49 +00001872 }
Derrick Stolee47410aa2021-02-18 14:07:25 +00001873 if (ctx->num_commit_graphs_after > 1)
1874 add_chunk(cf, GRAPH_CHUNKID_BASE,
1875 hashsz * (ctx->num_commit_graphs_after - 1),
1876 write_graph_chunk_base);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001877
1878 hashwrite_be32(f, GRAPH_SIGNATURE);
1879
1880 hashwrite_u8(f, GRAPH_VERSION);
brian m. carlsonc1665992018-11-14 04:09:35 +00001881 hashwrite_u8(f, oid_version());
Derrick Stolee47410aa2021-02-18 14:07:25 +00001882 hashwrite_u8(f, get_num_chunks(cf));
Derrick Stolee6c622f92019-06-18 11:14:27 -07001883 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001884
Derrick Stolee238def52019-06-12 06:29:45 -07001885 if (ctx->report_progress) {
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001886 strbuf_addf(&progress_title,
1887 Q_("Writing out commit graph in %d pass",
1888 "Writing out commit graph in %d passes",
Taylor Blauc4ff24b2021-02-24 12:12:22 -05001889 get_num_chunks(cf)),
1890 get_num_chunks(cf));
Derrick Stolee238def52019-06-12 06:29:45 -07001891 ctx->progress = start_delayed_progress(
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001892 progress_title.buf,
Taylor Blauc4ff24b2021-02-24 12:12:22 -05001893 get_num_chunks(cf) * ctx->commits.nr);
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001894 }
SZEDER Gábor17e62752020-07-01 13:27:26 +00001895
Derrick Stolee47410aa2021-02-18 14:07:25 +00001896 write_chunkfile(cf, ctx);
SZEDER Gábor17e62752020-07-01 13:27:26 +00001897
Derrick Stolee238def52019-06-12 06:29:45 -07001898 stop_progress(&ctx->progress);
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001899 strbuf_release(&progress_title);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001900
Derrick Stolee6c622f92019-06-18 11:14:27 -07001901 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1902 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001903 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001904
1905 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1906 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1907 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1908 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1909 }
1910
Derrick Stoleec3a3a962019-05-17 11:41:47 -07001911 close_commit_graph(ctx->r->objects);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001912 finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
Derrick Stolee47410aa2021-02-18 14:07:25 +00001913 free_chunkfile(cf);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001914
1915 if (ctx->split) {
1916 FILE *chainf = fdopen_lock_file(&lk, "w");
1917 char *final_graph_name;
1918 int result;
1919
1920 close(fd);
1921
1922 if (!chainf) {
1923 error(_("unable to open commit-graph chain file"));
1924 return -1;
1925 }
1926
1927 if (ctx->base_graph_name) {
Taylor Blau8a6ac282020-04-13 22:04:17 -06001928 const char *dest;
1929 int idx = ctx->num_commit_graphs_after - 1;
1930 if (ctx->num_commit_graphs_after > 1)
1931 idx--;
1932
1933 dest = ctx->commit_graph_filenames_after[idx];
Derrick Stolee6c622f92019-06-18 11:14:27 -07001934
Derrick Stolee135a7122019-06-18 11:14:28 -07001935 if (strcmp(ctx->base_graph_name, dest)) {
1936 result = rename(ctx->base_graph_name, dest);
1937
1938 if (result) {
1939 error(_("failed to rename base commit-graph file"));
1940 return -1;
1941 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07001942 }
1943 } else {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001944 char *graph_name = get_commit_graph_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001945 unlink(graph_name);
1946 }
1947
1948 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001949 final_graph_name = get_split_graph_filename(ctx->odb,
Derrick Stolee6c622f92019-06-18 11:14:27 -07001950 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1951 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1952
1953 result = rename(ctx->graph_name, final_graph_name);
1954
1955 for (i = 0; i < ctx->num_commit_graphs_after; i++)
Martin Ågrena52cdce2021-01-05 20:23:47 +01001956 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001957
1958 if (result) {
1959 error(_("failed to rename temporary commit-graph file"));
1960 return -1;
1961 }
1962 }
1963
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001964 commit_lock_file(&lk);
1965
Derrick Stolee238def52019-06-12 06:29:45 -07001966 return 0;
1967}
1968
Derrick Stolee1771be92019-06-18 11:14:29 -07001969static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
1970{
Alex Henrie8da02ce2019-09-30 20:29:34 -06001971 struct commit_graph *g;
1972 uint32_t num_commits;
Taylor Blaufdbde822020-04-13 22:04:12 -06001973 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
Derrick Stolee1771be92019-06-18 11:14:29 -07001974 uint32_t i;
1975
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001976 int max_commits = 0;
1977 int size_mult = 2;
1978
Taylor Blau98bb7962020-09-17 22:59:49 -04001979 if (ctx->opts) {
1980 max_commits = ctx->opts->max_commits;
Derrick Stolee63020f12020-01-02 16:14:14 +00001981
Taylor Blau98bb7962020-09-17 22:59:49 -04001982 if (ctx->opts->size_multiple)
1983 size_mult = ctx->opts->size_multiple;
Taylor Blaufdbde822020-04-13 22:04:12 -06001984
Taylor Blau98bb7962020-09-17 22:59:49 -04001985 flags = ctx->opts->split_flags;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001986 }
1987
Derrick Stolee1771be92019-06-18 11:14:29 -07001988 g = ctx->r->objects->commit_graph;
Alex Henrie8da02ce2019-09-30 20:29:34 -06001989 num_commits = ctx->commits.nr;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001990 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
1991 ctx->num_commit_graphs_after = 1;
1992 else
1993 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
Derrick Stolee1771be92019-06-18 11:14:29 -07001994
Taylor Blau8a6ac282020-04-13 22:04:17 -06001995 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
1996 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
Taylor Blaufdbde822020-04-13 22:04:12 -06001997 while (g && (g->num_commits <= size_mult * num_commits ||
1998 (max_commits && num_commits > max_commits))) {
1999 if (g->odb != ctx->odb)
2000 break;
Derrick Stoleec5230352019-06-18 11:14:30 -07002001
Taylor Blaufdbde822020-04-13 22:04:12 -06002002 num_commits += g->num_commits;
2003 g = g->base_graph;
Derrick Stolee1771be92019-06-18 11:14:29 -07002004
Taylor Blaufdbde822020-04-13 22:04:12 -06002005 ctx->num_commit_graphs_after--;
2006 }
Derrick Stolee1771be92019-06-18 11:14:29 -07002007 }
2008
Taylor Blau8a6ac282020-04-13 22:04:17 -06002009 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2010 ctx->new_base_graph = g;
2011 else if (ctx->num_commit_graphs_after != 1)
2012 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2013 "should be 1 with --split=replace");
Derrick Stolee1771be92019-06-18 11:14:29 -07002014
Derrick Stoleec5230352019-06-18 11:14:30 -07002015 if (ctx->num_commit_graphs_after == 2) {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08002016 char *old_graph_name = get_commit_graph_filename(g->odb);
Derrick Stoleec5230352019-06-18 11:14:30 -07002017
2018 if (!strcmp(g->filename, old_graph_name) &&
Taylor Blauad2dd5b2020-02-03 13:18:02 -08002019 g->odb != ctx->odb) {
Derrick Stoleec5230352019-06-18 11:14:30 -07002020 ctx->num_commit_graphs_after = 1;
2021 ctx->new_base_graph = NULL;
2022 }
2023
2024 free(old_graph_name);
2025 }
2026
Taylor Blaub78a5562020-04-23 15:41:09 -06002027 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2028 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
Derrick Stolee1771be92019-06-18 11:14:29 -07002029
2030 for (i = 0; i < ctx->num_commit_graphs_after &&
2031 i < ctx->num_commit_graphs_before; i++)
2032 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2033
2034 i = ctx->num_commit_graphs_before - 1;
2035 g = ctx->r->objects->commit_graph;
2036
2037 while (g) {
2038 if (i < ctx->num_commit_graphs_after)
2039 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2040
Abhishek Kumar1fdc3832021-01-16 18:11:16 +00002041 /*
2042 * If the topmost remaining layer has generation data chunk, the
2043 * resultant layer also has generation data chunk.
2044 */
2045 if (i == ctx->num_commit_graphs_after - 2)
2046 ctx->write_generation_data = !!g->chunk_generation_data;
2047
Derrick Stolee1771be92019-06-18 11:14:29 -07002048 i--;
2049 g = g->base_graph;
2050 }
2051}
2052
2053static void merge_commit_graph(struct write_commit_graph_context *ctx,
2054 struct commit_graph *g)
2055{
2056 uint32_t i;
2057 uint32_t offset = g->num_commits_in_base;
2058
2059 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2060
2061 for (i = 0; i < g->num_commits; i++) {
2062 struct object_id oid;
2063 struct commit *result;
2064
2065 display_progress(ctx->progress, i + 1);
2066
2067 load_oid_from_graph(g, i + offset, &oid);
2068
2069 /* only add commits if they still exist in the repo */
2070 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2071
2072 if (result) {
2073 ctx->commits.list[ctx->commits.nr] = result;
2074 ctx->commits.nr++;
2075 }
2076 }
2077}
2078
2079static int commit_compare(const void *_a, const void *_b)
2080{
2081 const struct commit *a = *(const struct commit **)_a;
2082 const struct commit *b = *(const struct commit **)_b;
2083 return oidcmp(&a->object.oid, &b->object.oid);
2084}
2085
2086static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2087{
Derrick Stolee150f1152020-10-09 20:53:51 +00002088 uint32_t i, dedup_i = 0;
Derrick Stolee1771be92019-06-18 11:14:29 -07002089
2090 if (ctx->report_progress)
2091 ctx->progress = start_delayed_progress(
2092 _("Scanning merged commits"),
2093 ctx->commits.nr);
2094
2095 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2096
2097 ctx->num_extra_edges = 0;
2098 for (i = 0; i < ctx->commits.nr; i++) {
2099 display_progress(ctx->progress, i);
2100
2101 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2102 &ctx->commits.list[i]->object.oid)) {
Derrick Stolee150f1152020-10-09 20:53:51 +00002103 /*
2104 * Silently ignore duplicates. These were likely
2105 * created due to a commit appearing in multiple
2106 * layers of the chain, which is unexpected but
2107 * not invalid. We should make sure there is a
2108 * unique copy in the new layer.
2109 */
Derrick Stolee1771be92019-06-18 11:14:29 -07002110 } else {
René Scharfe689a1462019-09-15 19:07:44 +02002111 unsigned int num_parents;
Derrick Stolee1771be92019-06-18 11:14:29 -07002112
Derrick Stolee150f1152020-10-09 20:53:51 +00002113 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2114 dedup_i++;
2115
René Scharfe689a1462019-09-15 19:07:44 +02002116 num_parents = commit_list_count(ctx->commits.list[i]->parents);
Derrick Stolee1771be92019-06-18 11:14:29 -07002117 if (num_parents > 2)
Derrick Stoleea35bea42019-08-05 09:43:41 -07002118 ctx->num_extra_edges += num_parents - 1;
Derrick Stolee1771be92019-06-18 11:14:29 -07002119 }
2120 }
2121
Derrick Stolee150f1152020-10-09 20:53:51 +00002122 ctx->commits.nr = dedup_i;
2123
Derrick Stolee1771be92019-06-18 11:14:29 -07002124 stop_progress(&ctx->progress);
2125}
2126
2127static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2128{
2129 struct commit_graph *g = ctx->r->objects->commit_graph;
2130 uint32_t current_graph_number = ctx->num_commit_graphs_before;
Derrick Stolee1771be92019-06-18 11:14:29 -07002131
2132 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2133 current_graph_number--;
2134
René Scharfed68ce902020-02-20 19:49:18 +01002135 if (ctx->report_progress)
2136 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
Derrick Stolee1771be92019-06-18 11:14:29 -07002137
2138 merge_commit_graph(ctx, g);
2139 stop_progress(&ctx->progress);
Derrick Stolee1771be92019-06-18 11:14:29 -07002140
2141 g = g->base_graph;
2142 }
2143
2144 if (g) {
2145 ctx->new_base_graph = g;
2146 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2147 }
2148
2149 if (ctx->new_base_graph)
2150 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2151
2152 sort_and_scan_merged_commits(ctx);
2153}
2154
Derrick Stolee8d840972019-06-18 11:14:31 -07002155static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2156{
2157 uint32_t i;
2158 time_t now = time(NULL);
2159
2160 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2161 struct stat st;
2162 struct utimbuf updated_time;
2163
2164 stat(ctx->commit_graph_filenames_before[i], &st);
2165
2166 updated_time.actime = st.st_atime;
2167 updated_time.modtime = now;
2168 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2169 }
2170}
2171
2172static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2173{
2174 struct strbuf path = STRBUF_INIT;
2175 DIR *dir;
2176 struct dirent *de;
2177 size_t dirnamelen;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07002178 timestamp_t expire_time = time(NULL);
2179
Taylor Blau98bb7962020-09-17 22:59:49 -04002180 if (ctx->opts && ctx->opts->expire_time)
2181 expire_time = ctx->opts->expire_time;
Derrick Stoleeba411122019-06-18 11:14:33 -07002182 if (!ctx->split) {
Derrick Stolee663b2b12020-09-17 18:11:46 +00002183 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
Derrick Stoleeba411122019-06-18 11:14:33 -07002184 unlink(chain_file_name);
2185 free(chain_file_name);
2186 ctx->num_commit_graphs_after = 0;
2187 }
Derrick Stolee8d840972019-06-18 11:14:31 -07002188
Taylor Blau0bd52e22020-02-03 21:51:50 -08002189 strbuf_addstr(&path, ctx->odb->path);
Derrick Stolee8d840972019-06-18 11:14:31 -07002190 strbuf_addstr(&path, "/info/commit-graphs");
2191 dir = opendir(path.buf);
2192
René Scharfe0aa6bce2019-08-07 13:15:02 +02002193 if (!dir)
2194 goto out;
Derrick Stolee8d840972019-06-18 11:14:31 -07002195
2196 strbuf_addch(&path, '/');
2197 dirnamelen = path.len;
2198 while ((de = readdir(dir)) != NULL) {
2199 struct stat st;
2200 uint32_t i, found = 0;
2201
2202 strbuf_setlen(&path, dirnamelen);
2203 strbuf_addstr(&path, de->d_name);
2204
2205 stat(path.buf, &st);
2206
2207 if (st.st_mtime > expire_time)
2208 continue;
2209 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2210 continue;
2211
2212 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2213 if (!strcmp(ctx->commit_graph_filenames_after[i],
2214 path.buf)) {
2215 found = 1;
2216 break;
2217 }
2218 }
2219
2220 if (!found)
2221 unlink(path.buf);
Derrick Stolee8d840972019-06-18 11:14:31 -07002222 }
René Scharfe0aa6bce2019-08-07 13:15:02 +02002223
2224out:
2225 strbuf_release(&path);
Derrick Stolee8d840972019-06-18 11:14:31 -07002226}
2227
Taylor Blau0bd52e22020-02-03 21:51:50 -08002228int write_commit_graph(struct object_directory *odb,
Derrick Stoleee103f722019-06-12 06:29:37 -07002229 struct string_list *pack_indexes,
Taylor Blau6830c362020-04-13 22:04:25 -06002230 struct oidset *commits,
SZEDER Gábor39d88312019-08-05 10:02:39 +02002231 enum commit_graph_write_flags flags,
Taylor Blau98bb7962020-09-17 22:59:49 -04002232 const struct commit_graph_opts *opts)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002233{
Derrick Stoleec7ef8fe2021-02-25 18:19:42 +00002234 struct repository *r = the_repository;
Derrick Stoleec9905be2019-06-12 06:29:40 -07002235 struct write_commit_graph_context *ctx;
Jeff King1cbdbf32020-12-07 14:11:02 -05002236 uint32_t i;
Derrick Stoleee103f722019-06-12 06:29:37 -07002237 int res = 0;
Taylor Blau8a6ac282020-04-13 22:04:17 -06002238 int replace = 0;
Taylor Blau9a7a9ed2020-09-16 14:07:46 -04002239 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +00002240 struct topo_level_slab topo_levels;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002241
Derrick Stoleec7ef8fe2021-02-25 18:19:42 +00002242 prepare_repo_settings(r);
2243 if (!r->settings.core_commit_graph) {
Derrick Stolee85102ac2020-10-09 20:53:52 +00002244 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2245 return 0;
2246 }
Derrick Stoleec7ef8fe2021-02-25 18:19:42 +00002247 if (!commit_graph_compatible(r))
Derrick Stoleee103f722019-06-12 06:29:37 -07002248 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002249
René Scharfeca56dad2021-03-13 17:17:22 +01002250 CALLOC_ARRAY(ctx, 1);
Derrick Stoleec7ef8fe2021-02-25 18:19:42 +00002251 ctx->r = r;
Taylor Blau0bd52e22020-02-03 21:51:50 -08002252 ctx->odb = odb;
SZEDER Gábor39d88312019-08-05 10:02:39 +02002253 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2254 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2255 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
Taylor Blau98bb7962020-09-17 22:59:49 -04002256 ctx->opts = opts;
Garima Singhf97b9322020-03-30 00:31:28 +00002257 ctx->total_bloom_filter_data_size = 0;
Derrick Stolee702110a2021-02-25 18:19:43 +00002258 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
Abhishek Kumare8b63002021-01-16 18:11:15 +00002259 ctx->num_generation_data_overflows = 0;
Derrick Stolee6c622f92019-06-18 11:14:27 -07002260
Taylor Blau9a7a9ed2020-09-16 14:07:46 -04002261 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2262 bloom_settings.bits_per_entry);
2263 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2264 bloom_settings.num_hashes);
2265 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2266 bloom_settings.max_changed_paths);
2267 ctx->bloom_settings = &bloom_settings;
2268
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +00002269 init_topo_level_slab(&topo_levels);
2270 ctx->topo_levels = &topo_levels;
2271
Derrick Stoleebc50d6c2021-02-02 03:01:23 +00002272 prepare_commit_graph(ctx->r);
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +00002273 if (ctx->r->objects->commit_graph) {
2274 struct commit_graph *g = ctx->r->objects->commit_graph;
2275
2276 while (g) {
2277 g->topo_levels = &topo_levels;
2278 g = g->base_graph;
2279 }
2280 }
2281
Derrick Stolee0087a872020-07-01 13:27:24 +00002282 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2283 ctx->changed_paths = 1;
2284 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2285 struct commit_graph *g;
Derrick Stolee0087a872020-07-01 13:27:24 +00002286
2287 g = ctx->r->objects->commit_graph;
2288
2289 /* We have changed-paths already. Keep them in the next graph */
2290 if (g && g->chunk_bloom_data) {
2291 ctx->changed_paths = 1;
2292 ctx->bloom_settings = g->bloom_filter_settings;
2293 }
2294 }
2295
Derrick Stolee6c622f92019-06-18 11:14:27 -07002296 if (ctx->split) {
Derrick Stoleebc50d6c2021-02-02 03:01:23 +00002297 struct commit_graph *g = ctx->r->objects->commit_graph;
Derrick Stolee6c622f92019-06-18 11:14:27 -07002298
2299 while (g) {
2300 ctx->num_commit_graphs_before++;
2301 g = g->base_graph;
2302 }
2303
2304 if (ctx->num_commit_graphs_before) {
2305 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2306 i = ctx->num_commit_graphs_before;
2307 g = ctx->r->objects->commit_graph;
2308
2309 while (g) {
2310 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2311 g = g->base_graph;
2312 }
2313 }
Taylor Blau8a6ac282020-04-13 22:04:17 -06002314
Taylor Blau98bb7962020-09-17 22:59:49 -04002315 if (ctx->opts)
2316 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
Derrick Stolee6c622f92019-06-18 11:14:27 -07002317 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002318
Derrick Stoleec9905be2019-06-12 06:29:40 -07002319 ctx->approx_nr_objects = approximate_object_count();
Derrick Stoleec9905be2019-06-12 06:29:40 -07002320
Derrick Stoleec9905be2019-06-12 06:29:40 -07002321 if (ctx->append && ctx->r->objects->commit_graph) {
2322 struct commit_graph *g = ctx->r->objects->commit_graph;
2323 for (i = 0; i < g->num_commits; i++) {
Jeff Kinga5f1c442020-12-07 14:11:05 -05002324 struct object_id oid;
2325 hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2326 oid_array_append(&ctx->oids, &oid);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002327 }
2328 }
2329
2330 if (pack_indexes) {
Garima Singh3d112752020-03-30 00:31:30 +00002331 ctx->order_by_pack = 1;
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07002332 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2333 goto cleanup;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002334 }
2335
Taylor Blau6830c362020-04-13 22:04:25 -06002336 if (commits) {
2337 if ((res = fill_oids_from_commits(ctx, commits)))
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02002338 goto cleanup;
2339 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002340
Junio C Hamano9b6606f2020-05-01 13:39:53 -07002341 if (!pack_indexes && !commits) {
Garima Singh3d112752020-03-30 00:31:30 +00002342 ctx->order_by_pack = 1;
Derrick Stoleeb2c83062019-06-12 06:29:42 -07002343 fill_oids_from_all_packs(ctx);
Garima Singh3d112752020-03-30 00:31:30 +00002344 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002345
Derrick Stoleec9905be2019-06-12 06:29:40 -07002346 close_reachable(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002347
Derrick Stoleef998d542019-06-12 06:29:44 -07002348 copy_oids_to_commits(ctx);
Derrick Stolee1373e542018-06-27 09:24:39 -04002349
Derrick Stoleec9905be2019-06-12 06:29:40 -07002350 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
Derrick Stoleee103f722019-06-12 06:29:37 -07002351 error(_("too many commits to write graph"));
2352 res = -1;
2353 goto cleanup;
2354 }
Derrick Stolee283e68c2018-06-27 09:24:32 -04002355
Taylor Blau8a6ac282020-04-13 22:04:17 -06002356 if (!ctx->commits.nr && !replace)
Derrick Stolee6c622f92019-06-18 11:14:27 -07002357 goto cleanup;
2358
Derrick Stolee1771be92019-06-18 11:14:29 -07002359 if (ctx->split) {
2360 split_graph_merge_strategy(ctx);
2361
Taylor Blau8a6ac282020-04-13 22:04:17 -06002362 if (!replace)
2363 merge_commit_graphs(ctx);
Derrick Stolee1771be92019-06-18 11:14:29 -07002364 } else
Derrick Stolee6c622f92019-06-18 11:14:27 -07002365 ctx->num_commit_graphs_after = 1;
2366
Derrick Stoleefde55b02021-02-02 03:01:22 +00002367 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
Abhishek Kumar1fdc3832021-01-16 18:11:16 +00002368
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00002369 compute_topological_levels(ctx);
2370 if (ctx->write_generation_data)
2371 compute_generation_numbers(ctx);
Derrick Stolee9bda8462018-06-27 09:24:35 -04002372
Garima Singhf97b9322020-03-30 00:31:28 +00002373 if (ctx->changed_paths)
2374 compute_bloom_filters(ctx);
2375
Derrick Stolee238def52019-06-12 06:29:45 -07002376 res = write_commit_graph_file(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002377
Derrick Stoleeba411122019-06-18 11:14:33 -07002378 if (ctx->split)
Derrick Stolee8d840972019-06-18 11:14:31 -07002379 mark_commit_graphs(ctx);
Derrick Stoleeba411122019-06-18 11:14:33 -07002380
2381 expire_commit_graphs(ctx);
Derrick Stolee8d840972019-06-18 11:14:31 -07002382
Derrick Stoleee103f722019-06-12 06:29:37 -07002383cleanup:
Derrick Stolee238def52019-06-12 06:29:45 -07002384 free(ctx->graph_name);
Derrick Stoleec9905be2019-06-12 06:29:40 -07002385 free(ctx->commits.list);
Jeff Kinga5f1c442020-12-07 14:11:05 -05002386 oid_array_clear(&ctx->oids);
Andrzej Huntbf4bb9f2021-02-19 20:13:10 +00002387 clear_topo_level_slab(&topo_levels);
Derrick Stolee6c622f92019-06-18 11:14:27 -07002388
2389 if (ctx->commit_graph_filenames_after) {
2390 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2391 free(ctx->commit_graph_filenames_after[i]);
2392 free(ctx->commit_graph_hash_after[i]);
2393 }
2394
2395 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2396 free(ctx->commit_graph_filenames_before[i]);
2397
2398 free(ctx->commit_graph_filenames_after);
2399 free(ctx->commit_graph_filenames_before);
2400 free(ctx->commit_graph_hash_after);
2401 }
2402
Derrick Stoleec9905be2019-06-12 06:29:40 -07002403 free(ctx);
Derrick Stoleee103f722019-06-12 06:29:37 -07002404
2405 return res;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002406}
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002407
2408#define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2409static int verify_commit_graph_error;
2410
Derrick Stolee283e68c2018-06-27 09:24:32 -04002411static void graph_report(const char *fmt, ...)
2412{
2413 va_list ap;
2414
2415 verify_commit_graph_error = 1;
2416 va_start(ap, fmt);
2417 vfprintf(stderr, fmt, ap);
2418 fprintf(stderr, "\n");
2419 va_end(ap);
2420}
2421
2422#define GENERATION_ZERO_EXISTS 1
2423#define GENERATION_NUMBER_EXISTS 2
2424
Derrick Stolee3da4b602019-06-18 11:14:32 -07002425int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
Derrick Stolee283e68c2018-06-27 09:24:32 -04002426{
Derrick Stolee9bda8462018-06-27 09:24:35 -04002427 uint32_t i, cur_fanout_pos = 0;
Derrick Stolee41df0e32018-06-27 09:24:42 -04002428 struct object_id prev_oid, cur_oid, checksum;
Derrick Stolee1373e542018-06-27 09:24:39 -04002429 int generation_zero = 0;
Derrick Stolee41df0e32018-06-27 09:24:42 -04002430 struct hashfile *f;
2431 int devnull;
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00002432 struct progress *progress = NULL;
Derrick Stolee3da4b602019-06-18 11:14:32 -07002433 int local_error = 0;
Derrick Stolee9bda8462018-06-27 09:24:35 -04002434
Derrick Stolee283e68c2018-06-27 09:24:32 -04002435 if (!g) {
2436 graph_report("no commit-graph file loaded");
2437 return 1;
2438 }
2439
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +01002440 verify_commit_graph_error = verify_commit_graph_lite(g);
Derrick Stolee9bda8462018-06-27 09:24:35 -04002441 if (verify_commit_graph_error)
2442 return verify_commit_graph_error;
2443
Derrick Stolee41df0e32018-06-27 09:24:42 -04002444 devnull = open("/dev/null", O_WRONLY);
2445 f = hashfd(devnull, NULL);
2446 hashwrite(f, g->data, g->data_len - g->hash_len);
2447 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
Jeff King67947c32018-08-28 17:22:52 -04002448 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
Derrick Stolee41df0e32018-06-27 09:24:42 -04002449 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2450 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2451 }
2452
Derrick Stolee9bda8462018-06-27 09:24:35 -04002453 for (i = 0; i < g->num_commits; i++) {
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002454 struct commit *graph_commit;
2455
Derrick Stolee9bda8462018-06-27 09:24:35 -04002456 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2457
2458 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002459 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04002460 oid_to_hex(&prev_oid),
2461 oid_to_hex(&cur_oid));
2462
2463 oidcpy(&prev_oid, &cur_oid);
2464
2465 while (cur_oid.hash[0] > cur_fanout_pos) {
2466 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2467
2468 if (i != fanout_value)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002469 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04002470 cur_fanout_pos, fanout_value, i);
2471 cur_fanout_pos++;
2472 }
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002473
Junio C Hamano82952962018-07-17 15:46:19 -07002474 graph_commit = lookup_commit(r, &cur_oid);
Stefan Beller4f542b72018-12-14 16:09:39 -08002475 if (!parse_commit_in_graph_one(r, g, graph_commit))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002476 graph_report(_("failed to parse commit %s from commit-graph"),
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002477 oid_to_hex(&cur_oid));
Derrick Stolee9bda8462018-06-27 09:24:35 -04002478 }
2479
2480 while (cur_fanout_pos < 256) {
2481 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2482
2483 if (g->num_commits != fanout_value)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002484 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04002485 cur_fanout_pos, fanout_value, i);
2486
2487 cur_fanout_pos++;
2488 }
2489
Derrick Stolee41df0e32018-06-27 09:24:42 -04002490 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
Derrick Stolee96af91d2018-06-27 09:24:36 -04002491 return verify_commit_graph_error;
2492
Garima Singh73716122019-08-26 09:29:58 -07002493 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2494 progress = start_progress(_("Verifying commits in commit graph"),
2495 g->num_commits);
2496
Derrick Stolee96af91d2018-06-27 09:24:36 -04002497 for (i = 0; i < g->num_commits; i++) {
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002498 struct commit *graph_commit, *odb_commit;
Derrick Stolee53614b12018-06-27 09:24:38 -04002499 struct commit_list *graph_parents, *odb_parents;
Abhishek Kumard7f92782021-01-16 18:11:13 +00002500 timestamp_t max_generation = 0;
2501 timestamp_t generation;
Derrick Stolee96af91d2018-06-27 09:24:36 -04002502
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00002503 display_progress(progress, i + 1);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002504 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2505
Junio C Hamano82952962018-07-17 15:46:19 -07002506 graph_commit = lookup_commit(r, &cur_oid);
Jeff Kinga3785092019-06-20 03:41:21 -04002507 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
Derrick Stolee96af91d2018-06-27 09:24:36 -04002508 if (parse_commit_internal(odb_commit, 0, 0)) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002509 graph_report(_("failed to parse commit %s from object database for commit-graph"),
Derrick Stolee96af91d2018-06-27 09:24:36 -04002510 oid_to_hex(&cur_oid));
2511 continue;
2512 }
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002513
Stefan Beller4f542b72018-12-14 16:09:39 -08002514 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002515 get_commit_tree_oid(odb_commit)))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002516 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002517 oid_to_hex(&cur_oid),
2518 oid_to_hex(get_commit_tree_oid(graph_commit)),
2519 oid_to_hex(get_commit_tree_oid(odb_commit)));
Derrick Stolee53614b12018-06-27 09:24:38 -04002520
2521 graph_parents = graph_commit->parents;
2522 odb_parents = odb_commit->parents;
2523
2524 while (graph_parents) {
2525 if (odb_parents == NULL) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002526 graph_report(_("commit-graph parent list for commit %s is too long"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002527 oid_to_hex(&cur_oid));
2528 break;
2529 }
2530
Derrick Stolee3da4b602019-06-18 11:14:32 -07002531 /* parse parent in case it is in a base graph */
2532 parse_commit_in_graph_one(r, g, graph_parents->item);
2533
Jeff King9001dc22018-08-28 17:22:48 -04002534 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002535 graph_report(_("commit-graph parent for %s is %s != %s"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002536 oid_to_hex(&cur_oid),
2537 oid_to_hex(&graph_parents->item->object.oid),
2538 oid_to_hex(&odb_parents->item->object.oid));
2539
Abhishek Kumarc752ad02020-06-17 14:44:11 +05302540 generation = commit_graph_generation(graph_parents->item);
2541 if (generation > max_generation)
2542 max_generation = generation;
Derrick Stolee1373e542018-06-27 09:24:39 -04002543
Derrick Stolee53614b12018-06-27 09:24:38 -04002544 graph_parents = graph_parents->next;
2545 odb_parents = odb_parents->next;
2546 }
2547
2548 if (odb_parents != NULL)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002549 graph_report(_("commit-graph parent list for commit %s terminates early"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002550 oid_to_hex(&cur_oid));
Derrick Stolee1373e542018-06-27 09:24:39 -04002551
Abhishek Kumarc49c82a2020-06-17 14:44:10 +05302552 if (!commit_graph_generation(graph_commit)) {
Derrick Stolee1373e542018-06-27 09:24:39 -04002553 if (generation_zero == GENERATION_NUMBER_EXISTS)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002554 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
Derrick Stolee1373e542018-06-27 09:24:39 -04002555 oid_to_hex(&cur_oid));
2556 generation_zero = GENERATION_ZERO_EXISTS;
2557 } else if (generation_zero == GENERATION_ZERO_EXISTS)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002558 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
Derrick Stolee1373e542018-06-27 09:24:39 -04002559 oid_to_hex(&cur_oid));
2560
2561 if (generation_zero == GENERATION_ZERO_EXISTS)
2562 continue;
2563
2564 /*
Abhishek Kumare8b63002021-01-16 18:11:15 +00002565 * If we are using topological level and one of our parents has
2566 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2567 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2568 * in the following condition.
Derrick Stolee1373e542018-06-27 09:24:39 -04002569 */
Abhishek Kumar1fdc3832021-01-16 18:11:16 +00002570 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
Derrick Stolee1373e542018-06-27 09:24:39 -04002571 max_generation--;
2572
Abhishek Kumarc752ad02020-06-17 14:44:11 +05302573 generation = commit_graph_generation(graph_commit);
Abhishek Kumare8b63002021-01-16 18:11:15 +00002574 if (generation < max_generation + 1)
2575 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
Derrick Stolee1373e542018-06-27 09:24:39 -04002576 oid_to_hex(&cur_oid),
Abhishek Kumarc752ad02020-06-17 14:44:11 +05302577 generation,
Derrick Stolee1373e542018-06-27 09:24:39 -04002578 max_generation + 1);
Derrick Stolee88968eb2018-06-27 09:24:40 -04002579
2580 if (graph_commit->date != odb_commit->date)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002581 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
Derrick Stolee88968eb2018-06-27 09:24:40 -04002582 oid_to_hex(&cur_oid),
2583 graph_commit->date,
2584 odb_commit->date);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002585 }
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00002586 stop_progress(&progress);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002587
Derrick Stolee3da4b602019-06-18 11:14:32 -07002588 local_error = verify_commit_graph_error;
2589
2590 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2591 local_error |= verify_commit_graph(r, g->base_graph, flags);
2592
2593 return local_error;
Derrick Stolee283e68c2018-06-27 09:24:32 -04002594}
Jonathan Tanc3756d52018-07-11 15:42:40 -07002595
2596void free_commit_graph(struct commit_graph *g)
2597{
2598 if (!g)
2599 return;
Jeff Kingc8828532020-04-23 15:41:13 -06002600 if (g->data) {
Jonathan Tanc3756d52018-07-11 15:42:40 -07002601 munmap((void *)g->data, g->data_len);
2602 g->data = NULL;
Jonathan Tanc3756d52018-07-11 15:42:40 -07002603 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07002604 free(g->filename);
Garima Singh76ffbca2020-04-06 16:59:49 +00002605 free(g->bloom_filter_settings);
Jonathan Tanc3756d52018-07-11 15:42:40 -07002606 free(g);
2607}
Jeff King6abada12019-09-12 10:44:45 -04002608
2609void disable_commit_graph(struct repository *r)
2610{
2611 r->commit_graph_disabled = 1;
2612}