blob: 2706683acfe1c1fc982b1c3119b3e895cac57006 [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
brian m. carlson92e2cab2021-04-26 01:02:50 +0000428 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
Derrick Stolee118bd572019-06-18 11:14:26 -0700429
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
Johannes Schindelin957ba812021-09-08 08:29:30 +0000716 clear_commit_graph_data_slab(&commit_graph_data_slab);
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700717 close_commit_graph_one(g->base_graph);
718 free_commit_graph(g);
719}
720
Derrick Stoleec3a3a962019-05-17 11:41:47 -0700721void close_commit_graph(struct raw_object_store *o)
Derrick Stolee177722b2018-04-10 08:56:05 -0400722{
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700723 close_commit_graph_one(o->commit_graph);
Derrick Stoleec3a3a962019-05-17 11:41:47 -0700724 o->commit_graph = NULL;
Derrick Stolee177722b2018-04-10 08:56:05 -0400725}
726
Patrick Steinhardt809ea282021-08-09 10:11:59 +0200727static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
Derrick Stolee177722b2018-04-10 08:56:05 -0400728{
729 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
730 g->chunk_oid_lookup, g->hash_len, pos);
731}
732
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700733static void load_oid_from_graph(struct commit_graph *g,
734 uint32_t pos,
735 struct object_id *oid)
736{
737 uint32_t lex_index;
738
739 while (g && pos < g->num_commits_in_base)
740 g = g->base_graph;
741
742 if (!g)
743 BUG("NULL commit-graph");
744
745 if (pos >= g->num_commits + g->num_commits_in_base)
746 die(_("invalid commit position. commit-graph is likely corrupt"));
747
748 lex_index = pos - g->num_commits_in_base;
749
brian m. carlson92e2cab2021-04-26 01:02:50 +0000750 oidread(oid, g->chunk_oid_lookup + g->hash_len * lex_index);
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700751}
752
Stefan Beller4f542b72018-12-14 16:09:39 -0800753static struct commit_list **insert_parent_or_die(struct repository *r,
754 struct commit_graph *g,
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700755 uint32_t pos,
Derrick Stolee177722b2018-04-10 08:56:05 -0400756 struct commit_list **pptr)
757{
758 struct commit *c;
759 struct object_id oid;
Derrick Stolee96af91d2018-06-27 09:24:36 -0400760
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700761 if (pos >= g->num_commits + g->num_commits_in_base)
762 die("invalid parent position %"PRIu32, pos);
Derrick Stolee53614b12018-06-27 09:24:38 -0400763
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700764 load_oid_from_graph(g, pos, &oid);
Stefan Beller4f542b72018-12-14 16:09:39 -0800765 c = lookup_commit(r, &oid);
Derrick Stolee177722b2018-04-10 08:56:05 -0400766 if (!c)
Nguyễn Thái Ngọc Duy4f5b5322018-07-21 09:49:26 +0200767 die(_("could not find commit %s"), oid_to_hex(&oid));
Abhishek Kumarc49c82a2020-06-17 14:44:10 +0530768 commit_graph_data_at(c)->graph_pos = pos;
Derrick Stolee177722b2018-04-10 08:56:05 -0400769 return &commit_list_insert(c, pptr)->next;
770}
771
Derrick Stoleee2838d82018-05-01 12:47:13 +0000772static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
773{
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700774 const unsigned char *commit_data;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530775 struct commit_graph_data *graph_data;
Abhishek Kumare8b63002021-01-16 18:11:15 +0000776 uint32_t lex_index, offset_pos;
777 uint64_t date_high, date_low, offset;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700778
779 while (pos < g->num_commits_in_base)
780 g = g->base_graph;
781
Abhishek Kumarf90fca62021-01-16 18:11:10 +0000782 if (pos >= g->num_commits + g->num_commits_in_base)
783 die(_("invalid commit position. commit-graph is likely corrupt"));
784
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700785 lex_index = pos - g->num_commits_in_base;
786 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530787
788 graph_data = commit_graph_data_at(item);
789 graph_data->graph_pos = pos;
Abhishek Kumarf90fca62021-01-16 18:11:10 +0000790
791 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
792 date_low = get_be32(commit_data + g->hash_len + 12);
793 item->date = (timestamp_t)((date_high << 32) | date_low);
794
Abhishek Kumar1fdc3832021-01-16 18:11:16 +0000795 if (g->read_generation_data) {
Abhishek Kumare8b63002021-01-16 18:11:15 +0000796 offset = (timestamp_t)get_be32(g->chunk_generation_data + sizeof(uint32_t) * lex_index);
797
798 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
799 if (!g->chunk_generation_data_overflow)
800 die(_("commit-graph requires overflow generation data but has none"));
801
802 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
803 graph_data->generation = get_be64(g->chunk_generation_data_overflow + 8 * offset_pos);
804 } else
805 graph_data->generation = item->date + offset;
806 } else
807 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +0000808
809 if (g->topo_levels)
810 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
Derrick Stoleee2838d82018-05-01 12:47:13 +0000811}
812
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700813static inline void set_commit_tree(struct commit *c, struct tree *t)
814{
815 c->maybe_tree = t;
816}
817
Stefan Beller4f542b72018-12-14 16:09:39 -0800818static int fill_commit_in_graph(struct repository *r,
819 struct commit *item,
820 struct commit_graph *g, uint32_t pos)
Derrick Stolee177722b2018-04-10 08:56:05 -0400821{
Derrick Stolee177722b2018-04-10 08:56:05 -0400822 uint32_t edge_value;
823 uint32_t *parent_data_ptr;
Derrick Stolee177722b2018-04-10 08:56:05 -0400824 struct commit_list **pptr;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700825 const unsigned char *commit_data;
826 uint32_t lex_index;
827
828 while (pos < g->num_commits_in_base)
829 g = g->base_graph;
830
Abhishek Kumarf90fca62021-01-16 18:11:10 +0000831 fill_commit_graph_info(item, g, pos);
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700832
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700833 lex_index = pos - g->num_commits_in_base;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700834 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
Derrick Stolee177722b2018-04-10 08:56:05 -0400835
836 item->object.parsed = 1;
Derrick Stolee177722b2018-04-10 08:56:05 -0400837
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700838 set_commit_tree(item, NULL);
Derrick Stolee177722b2018-04-10 08:56:05 -0400839
Derrick Stolee177722b2018-04-10 08:56:05 -0400840 pptr = &item->parents;
841
842 edge_value = get_be32(commit_data + g->hash_len);
843 if (edge_value == GRAPH_PARENT_NONE)
844 return 1;
Stefan Beller4f542b72018-12-14 16:09:39 -0800845 pptr = insert_parent_or_die(r, g, edge_value, pptr);
Derrick Stolee177722b2018-04-10 08:56:05 -0400846
847 edge_value = get_be32(commit_data + g->hash_len + 4);
848 if (edge_value == GRAPH_PARENT_NONE)
849 return 1;
SZEDER Gábor5af74172019-01-19 21:21:13 +0100850 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
Stefan Beller4f542b72018-12-14 16:09:39 -0800851 pptr = insert_parent_or_die(r, g, edge_value, pptr);
Derrick Stolee177722b2018-04-10 08:56:05 -0400852 return 1;
853 }
854
SZEDER Gábor5af74172019-01-19 21:21:13 +0100855 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
Derrick Stolee177722b2018-04-10 08:56:05 -0400856 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
857 do {
858 edge_value = get_be32(parent_data_ptr);
Stefan Beller4f542b72018-12-14 16:09:39 -0800859 pptr = insert_parent_or_die(r, g,
Derrick Stolee177722b2018-04-10 08:56:05 -0400860 edge_value & GRAPH_EDGE_LAST_MASK,
861 pptr);
862 parent_data_ptr++;
863 } while (!(edge_value & GRAPH_LAST_EDGE));
864
865 return 1;
866}
867
Patrick Steinhardt809ea282021-08-09 10:11:59 +0200868static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
869{
870 struct commit_graph *cur_g = g;
871 uint32_t lex_index;
872
873 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
874 cur_g = cur_g->base_graph;
875
876 if (cur_g) {
877 *pos = lex_index + cur_g->num_commits_in_base;
878 return 1;
879 }
880
881 return 0;
882}
883
884static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
Derrick Stoleee2838d82018-05-01 12:47:13 +0000885{
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530886 uint32_t graph_pos = commit_graph_position(item);
887 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
888 *pos = graph_pos;
Derrick Stoleee2838d82018-05-01 12:47:13 +0000889 return 1;
890 } else {
Patrick Steinhardt809ea282021-08-09 10:11:59 +0200891 return search_commit_pos_in_graph(&item->object.oid, g, pos);
Derrick Stoleee2838d82018-05-01 12:47:13 +0000892 }
893}
894
Patrick Steinhardtf559d6d2021-08-09 10:12:03 +0200895struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
896{
897 struct commit *commit;
898 uint32_t pos;
899
900 if (!repo->objects->commit_graph)
901 return NULL;
902 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
903 return NULL;
904 if (!repo_has_object_file(repo, id))
905 return NULL;
906
907 commit = lookup_commit(repo, id);
908 if (!commit)
909 return NULL;
910 if (commit->object.parsed)
911 return commit;
912
913 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
914 return NULL;
915
916 return commit;
917}
918
Stefan Beller4f542b72018-12-14 16:09:39 -0800919static int parse_commit_in_graph_one(struct repository *r,
920 struct commit_graph *g,
921 struct commit *item)
Derrick Stolee177722b2018-04-10 08:56:05 -0400922{
Derrick Stoleee2838d82018-05-01 12:47:13 +0000923 uint32_t pos;
924
Derrick Stolee177722b2018-04-10 08:56:05 -0400925 if (item->object.parsed)
926 return 1;
Derrick Stoleeee797052018-06-27 09:24:29 -0400927
Patrick Steinhardt809ea282021-08-09 10:11:59 +0200928 if (find_commit_pos_in_graph(item, g, &pos))
Stefan Beller4f542b72018-12-14 16:09:39 -0800929 return fill_commit_in_graph(r, item, g, pos);
Derrick Stoleeee797052018-06-27 09:24:29 -0400930
931 return 0;
932}
933
Jonathan Tandade47c2018-07-11 15:42:42 -0700934int parse_commit_in_graph(struct repository *r, struct commit *item)
Derrick Stoleeee797052018-06-27 09:24:29 -0400935{
Derrick Stolee7b671f82020-06-23 17:47:01 +0000936 static int checked_env = 0;
937
938 if (!checked_env &&
939 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
940 die("dying as requested by the '%s' variable on commit-graph parse!",
941 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
942 checked_env = 1;
943
Jonathan Tandade47c2018-07-11 15:42:42 -0700944 if (!prepare_commit_graph(r))
Derrick Stoleeee797052018-06-27 09:24:29 -0400945 return 0;
Stefan Beller4f542b72018-12-14 16:09:39 -0800946 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
Derrick Stolee177722b2018-04-10 08:56:05 -0400947}
948
Jonathan Tandade47c2018-07-11 15:42:42 -0700949void load_commit_graph_info(struct repository *r, struct commit *item)
Derrick Stoleee2838d82018-05-01 12:47:13 +0000950{
951 uint32_t pos;
Jonathan Tandade47c2018-07-11 15:42:42 -0700952 if (!prepare_commit_graph(r))
Derrick Stoleee2838d82018-05-01 12:47:13 +0000953 return;
Patrick Steinhardt809ea282021-08-09 10:11:59 +0200954 if (find_commit_pos_in_graph(item, r->objects->commit_graph, &pos))
Jonathan Tandade47c2018-07-11 15:42:42 -0700955 fill_commit_graph_info(item, r->objects->commit_graph, pos);
Derrick Stoleee2838d82018-05-01 12:47:13 +0000956}
957
Stefan Beller4f542b72018-12-14 16:09:39 -0800958static struct tree *load_tree_for_commit(struct repository *r,
959 struct commit_graph *g,
960 struct commit *c)
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000961{
962 struct object_id oid;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700963 const unsigned char *commit_data;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530964 uint32_t graph_pos = commit_graph_position(c);
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700965
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530966 while (graph_pos < g->num_commits_in_base)
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700967 g = g->base_graph;
968
969 commit_data = g->chunk_commit_data +
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530970 GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base);
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000971
brian m. carlson92e2cab2021-04-26 01:02:50 +0000972 oidread(&oid, commit_data);
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700973 set_commit_tree(c, lookup_tree(r, &oid));
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000974
975 return c->maybe_tree;
976}
977
Stefan Beller4f542b72018-12-14 16:09:39 -0800978static struct tree *get_commit_tree_in_graph_one(struct repository *r,
979 struct commit_graph *g,
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400980 const struct commit *c)
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000981{
982 if (c->maybe_tree)
983 return c->maybe_tree;
Abhishek Kumarc49c82a2020-06-17 14:44:10 +0530984 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400985 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000986
Stefan Beller4f542b72018-12-14 16:09:39 -0800987 return load_tree_for_commit(r, g, (struct commit *)c);
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400988}
989
Jonathan Tandade47c2018-07-11 15:42:42 -0700990struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400991{
Stefan Beller4f542b72018-12-14 16:09:39 -0800992 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000993}
994
Derrick Stoleec9905be2019-06-12 06:29:40 -0700995struct packed_commit_list {
996 struct commit **list;
Jeff King33613902020-12-07 14:11:08 -0500997 size_t nr;
998 size_t alloc;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700999};
1000
Derrick Stoleec9905be2019-06-12 06:29:40 -07001001struct write_commit_graph_context {
1002 struct repository *r;
Taylor Blau0bd52e22020-02-03 21:51:50 -08001003 struct object_directory *odb;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001004 char *graph_name;
Jeff Kinga5f1c442020-12-07 14:11:05 -05001005 struct oid_array oids;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001006 struct packed_commit_list commits;
1007 int num_extra_edges;
Abhishek Kumare8b63002021-01-16 18:11:15 +00001008 int num_generation_data_overflows;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001009 unsigned long approx_nr_objects;
1010 struct progress *progress;
1011 int progress_done;
1012 uint64_t progress_cnt;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001013
1014 char *base_graph_name;
1015 int num_commit_graphs_before;
1016 int num_commit_graphs_after;
1017 char **commit_graph_filenames_before;
1018 char **commit_graph_filenames_after;
1019 char **commit_graph_hash_after;
1020 uint32_t new_num_commits_in_base;
1021 struct commit_graph *new_base_graph;
1022
Derrick Stoleec9905be2019-06-12 06:29:40 -07001023 unsigned append:1,
Derrick Stolee6c622f92019-06-18 11:14:27 -07001024 report_progress:1,
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001025 split:1,
Garima Singh3d112752020-03-30 00:31:30 +00001026 changed_paths:1,
Abhishek Kumare8b63002021-01-16 18:11:15 +00001027 order_by_pack:1,
Derrick Stoleefde55b02021-02-02 03:01:22 +00001028 write_generation_data:1,
1029 trust_generation_numbers:1;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001030
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +00001031 struct topo_level_slab *topo_levels;
Taylor Blau98bb7962020-09-17 22:59:49 -04001032 const struct commit_graph_opts *opts;
Garima Singhf97b9322020-03-30 00:31:28 +00001033 size_t total_bloom_filter_data_size;
Derrick Stolee98037f22020-06-23 17:47:00 +00001034 const struct bloom_filter_settings *bloom_settings;
Taylor Blau312cff52020-09-16 14:07:32 -04001035
1036 int count_bloom_filter_computed;
1037 int count_bloom_filter_not_computed;
Taylor Blau59f0d502020-09-17 22:59:44 -04001038 int count_bloom_filter_trunc_empty;
Taylor Blau312cff52020-09-16 14:07:32 -04001039 int count_bloom_filter_trunc_large;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001040};
1041
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001042static int write_graph_chunk_fanout(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001043 void *data)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001044{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001045 struct write_commit_graph_context *ctx = data;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001046 int i, count = 0;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001047 struct commit **list = ctx->commits.list;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001048
1049 /*
1050 * Write the first-level table (the list is sorted,
1051 * but we use a 256-entry lookup to be able to avoid
1052 * having to do eight extra binary search iterations).
1053 */
1054 for (i = 0; i < 256; i++) {
Derrick Stoleec9905be2019-06-12 06:29:40 -07001055 while (count < ctx->commits.nr) {
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001056 if ((*list)->object.oid.hash[0] != i)
1057 break;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001058 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001059 count++;
1060 list++;
1061 }
1062
1063 hashwrite_be32(f, count);
1064 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001065
1066 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001067}
1068
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001069static int write_graph_chunk_oids(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001070 void *data)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001071{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001072 struct write_commit_graph_context *ctx = data;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001073 struct commit **list = ctx->commits.list;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001074 int count;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001075 for (count = 0; count < ctx->commits.nr; count++, list++) {
1076 display_progress(ctx->progress, ++ctx->progress_cnt);
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001077 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +01001078 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001079
1080 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001081}
1082
Jeff King8380dcd2021-01-28 01:20:23 -05001083static const struct object_id *commit_to_oid(size_t index, const void *table)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001084{
Jeff King8380dcd2021-01-28 01:20:23 -05001085 const struct commit * const *commits = table;
Jeff King45ee13b2021-01-28 01:19:42 -05001086 return &commits[index]->object.oid;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001087}
1088
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001089static int write_graph_chunk_data(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001090 void *data)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001091{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001092 struct write_commit_graph_context *ctx = data;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001093 struct commit **list = ctx->commits.list;
1094 struct commit **last = ctx->commits.list + ctx->commits.nr;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001095 uint32_t num_extra_edges = 0;
1096
1097 while (list < last) {
1098 struct commit_list *parent;
Taylor Blau806278d2019-09-05 18:04:57 -04001099 struct object_id *tree;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001100 int edge_value;
1101 uint32_t packedDate[2];
Derrick Stoleec9905be2019-06-12 06:29:40 -07001102 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001103
Derrick Stoleec4cc0832021-02-01 17:15:03 +00001104 if (repo_parse_commit_no_graph(ctx->r, *list))
Taylor Blau16749b82019-09-05 18:04:55 -04001105 die(_("unable to parse commit %s"),
1106 oid_to_hex(&(*list)->object.oid));
Taylor Blau806278d2019-09-05 18:04:57 -04001107 tree = get_commit_tree_oid(*list);
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001108 hashwrite(f, tree->hash, the_hash_algo->rawsz);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001109
1110 parent = (*list)->parents;
1111
1112 if (!parent)
1113 edge_value = GRAPH_PARENT_NONE;
1114 else {
Jeff King45ee13b2021-01-28 01:19:42 -05001115 edge_value = oid_pos(&parent->item->object.oid,
1116 ctx->commits.list,
1117 ctx->commits.nr,
1118 commit_to_oid);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001119
Derrick Stolee6c622f92019-06-18 11:14:27 -07001120 if (edge_value >= 0)
1121 edge_value += ctx->new_num_commits_in_base;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001122 else if (ctx->new_base_graph) {
Derrick Stolee6c622f92019-06-18 11:14:27 -07001123 uint32_t pos;
Patrick Steinhardt809ea282021-08-09 10:11:59 +02001124 if (find_commit_pos_in_graph(parent->item,
1125 ctx->new_base_graph,
1126 &pos))
Derrick Stolee6c622f92019-06-18 11:14:27 -07001127 edge_value = pos;
1128 }
1129
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001130 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -08001131 BUG("missing parent %s for commit %s",
1132 oid_to_hex(&parent->item->object.oid),
1133 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001134 }
1135
1136 hashwrite_be32(f, edge_value);
1137
1138 if (parent)
1139 parent = parent->next;
1140
1141 if (!parent)
1142 edge_value = GRAPH_PARENT_NONE;
1143 else if (parent->next)
SZEDER Gábor5af74172019-01-19 21:21:13 +01001144 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001145 else {
Jeff King45ee13b2021-01-28 01:19:42 -05001146 edge_value = oid_pos(&parent->item->object.oid,
1147 ctx->commits.list,
1148 ctx->commits.nr,
1149 commit_to_oid);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001150
1151 if (edge_value >= 0)
1152 edge_value += ctx->new_num_commits_in_base;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001153 else if (ctx->new_base_graph) {
Derrick Stolee6c622f92019-06-18 11:14:27 -07001154 uint32_t pos;
Patrick Steinhardt809ea282021-08-09 10:11:59 +02001155 if (find_commit_pos_in_graph(parent->item,
1156 ctx->new_base_graph,
1157 &pos))
Derrick Stolee6c622f92019-06-18 11:14:27 -07001158 edge_value = pos;
1159 }
1160
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001161 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -08001162 BUG("missing parent %s for commit %s",
1163 oid_to_hex(&parent->item->object.oid),
1164 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001165 }
1166
1167 hashwrite_be32(f, edge_value);
1168
SZEDER Gábor5af74172019-01-19 21:21:13 +01001169 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001170 do {
1171 num_extra_edges++;
1172 parent = parent->next;
1173 } while (parent);
1174 }
1175
1176 if (sizeof((*list)->date) > 4)
1177 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1178 else
1179 packedDate[0] = 0;
1180
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +00001181 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
Derrick Stolee3258c662018-05-01 12:47:09 +00001182
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001183 packedDate[1] = htonl((*list)->date);
1184 hashwrite(f, packedDate, 8);
1185
1186 list++;
1187 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001188
1189 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001190}
1191
Abhishek Kumare8b63002021-01-16 18:11:15 +00001192static int write_graph_chunk_generation_data(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001193 void *data)
Abhishek Kumare8b63002021-01-16 18:11:15 +00001194{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001195 struct write_commit_graph_context *ctx = data;
Abhishek Kumare8b63002021-01-16 18:11:15 +00001196 int i, num_generation_data_overflows = 0;
1197
1198 for (i = 0; i < ctx->commits.nr; i++) {
1199 struct commit *c = ctx->commits.list[i];
Derrick Stolee90cb1c42021-02-01 17:15:04 +00001200 timestamp_t offset;
1201 repo_parse_commit(ctx->r, c);
1202 offset = commit_graph_data_at(c)->generation - c->date;
Abhishek Kumare8b63002021-01-16 18:11:15 +00001203 display_progress(ctx->progress, ++ctx->progress_cnt);
1204
1205 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1206 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1207 num_generation_data_overflows++;
1208 }
1209
1210 hashwrite_be32(f, offset);
1211 }
1212
1213 return 0;
1214}
1215
1216static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001217 void *data)
Abhishek Kumare8b63002021-01-16 18:11:15 +00001218{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001219 struct write_commit_graph_context *ctx = data;
Abhishek Kumare8b63002021-01-16 18:11:15 +00001220 int i;
1221 for (i = 0; i < ctx->commits.nr; i++) {
1222 struct commit *c = ctx->commits.list[i];
1223 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1224 display_progress(ctx->progress, ++ctx->progress_cnt);
1225
1226 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1227 hashwrite_be32(f, offset >> 32);
1228 hashwrite_be32(f, (uint32_t) offset);
1229 }
1230 }
1231
1232 return 0;
1233}
1234
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001235static int write_graph_chunk_extra_edges(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001236 void *data)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001237{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001238 struct write_commit_graph_context *ctx = data;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001239 struct commit **list = ctx->commits.list;
1240 struct commit **last = ctx->commits.list + ctx->commits.nr;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001241 struct commit_list *parent;
1242
1243 while (list < last) {
1244 int num_parents = 0;
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +01001245
Derrick Stoleec9905be2019-06-12 06:29:40 -07001246 display_progress(ctx->progress, ++ctx->progress_cnt);
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +01001247
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001248 for (parent = (*list)->parents; num_parents < 3 && parent;
1249 parent = parent->next)
1250 num_parents++;
1251
1252 if (num_parents <= 2) {
1253 list++;
1254 continue;
1255 }
1256
1257 /* Since num_parents > 2, this initializer is safe. */
1258 for (parent = (*list)->parents->next; parent; parent = parent->next) {
Jeff King45ee13b2021-01-28 01:19:42 -05001259 int edge_value = oid_pos(&parent->item->object.oid,
1260 ctx->commits.list,
1261 ctx->commits.nr,
1262 commit_to_oid);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001263
Derrick Stolee6c622f92019-06-18 11:14:27 -07001264 if (edge_value >= 0)
1265 edge_value += ctx->new_num_commits_in_base;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001266 else if (ctx->new_base_graph) {
Derrick Stolee6c622f92019-06-18 11:14:27 -07001267 uint32_t pos;
Patrick Steinhardt809ea282021-08-09 10:11:59 +02001268 if (find_commit_pos_in_graph(parent->item,
1269 ctx->new_base_graph,
1270 &pos))
Derrick Stolee6c622f92019-06-18 11:14:27 -07001271 edge_value = pos;
1272 }
1273
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001274 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -08001275 BUG("missing parent %s for commit %s",
1276 oid_to_hex(&parent->item->object.oid),
1277 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001278 else if (!parent->next)
1279 edge_value |= GRAPH_LAST_EDGE;
1280
1281 hashwrite_be32(f, edge_value);
1282 }
1283
1284 list++;
1285 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001286
1287 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001288}
1289
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001290static int write_graph_chunk_bloom_indexes(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001291 void *data)
Garima Singh76ffbca2020-04-06 16:59:49 +00001292{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001293 struct write_commit_graph_context *ctx = data;
Garima Singh76ffbca2020-04-06 16:59:49 +00001294 struct commit **list = ctx->commits.list;
1295 struct commit **last = ctx->commits.list + ctx->commits.nr;
1296 uint32_t cur_pos = 0;
Garima Singh76ffbca2020-04-06 16:59:49 +00001297
1298 while (list < last) {
Taylor Blau312cff52020-09-16 14:07:32 -04001299 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
Derrick Stolee94919742020-07-01 13:27:23 +00001300 size_t len = filter ? filter->len : 0;
1301 cur_pos += len;
SZEDER Gábor150cd3b2020-07-09 19:00:03 +02001302 display_progress(ctx->progress, ++ctx->progress_cnt);
Garima Singh76ffbca2020-04-06 16:59:49 +00001303 hashwrite_be32(f, cur_pos);
1304 list++;
1305 }
1306
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001307 return 0;
Garima Singh76ffbca2020-04-06 16:59:49 +00001308}
1309
Derrick Stolee0087a872020-07-01 13:27:24 +00001310static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1311{
1312 struct json_writer jw = JSON_WRITER_INIT;
1313
1314 jw_object_begin(&jw, 0);
1315 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1316 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1317 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
Taylor Blau97ffa4f2020-09-17 09:34:42 -04001318 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
Derrick Stolee0087a872020-07-01 13:27:24 +00001319 jw_end(&jw);
1320
1321 trace2_data_json("bloom", ctx->r, "settings", &jw);
1322
1323 jw_release(&jw);
1324}
1325
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001326static int write_graph_chunk_bloom_data(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001327 void *data)
Garima Singh76ffbca2020-04-06 16:59:49 +00001328{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001329 struct write_commit_graph_context *ctx = data;
Garima Singh76ffbca2020-04-06 16:59:49 +00001330 struct commit **list = ctx->commits.list;
1331 struct commit **last = ctx->commits.list + ctx->commits.nr;
Garima Singh76ffbca2020-04-06 16:59:49 +00001332
Derrick Stolee0087a872020-07-01 13:27:24 +00001333 trace2_bloom_filter_settings(ctx);
1334
Derrick Stolee98037f22020-06-23 17:47:00 +00001335 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1336 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1337 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
Garima Singh76ffbca2020-04-06 16:59:49 +00001338
1339 while (list < last) {
Taylor Blau312cff52020-09-16 14:07:32 -04001340 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
Derrick Stolee94919742020-07-01 13:27:23 +00001341 size_t len = filter ? filter->len : 0;
Derrick Stolee94919742020-07-01 13:27:23 +00001342
SZEDER Gábor150cd3b2020-07-09 19:00:03 +02001343 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee94919742020-07-01 13:27:23 +00001344 if (len)
1345 hashwrite(f, filter->data, len * sizeof(unsigned char));
Garima Singh76ffbca2020-04-06 16:59:49 +00001346 list++;
1347 }
1348
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001349 return 0;
Garima Singh76ffbca2020-04-06 16:59:49 +00001350}
1351
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001352static int add_packed_commits(const struct object_id *oid,
1353 struct packed_git *pack,
1354 uint32_t pos,
1355 void *data)
1356{
Derrick Stoleec9905be2019-06-12 06:29:40 -07001357 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001358 enum object_type type;
1359 off_t offset = nth_packed_object_offset(pack, pos);
1360 struct object_info oi = OBJECT_INFO_INIT;
1361
Derrick Stoleec9905be2019-06-12 06:29:40 -07001362 if (ctx->progress)
1363 display_progress(ctx->progress, ++ctx->progress_done);
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +00001364
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001365 oi.typep = &type;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001366 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
Nguyễn Thái Ngọc Duy4f5b5322018-07-21 09:49:26 +02001367 die(_("unable to get type of object %s"), oid_to_hex(oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001368
1369 if (type != OBJ_COMMIT)
1370 return 0;
1371
Jeff Kinga5f1c442020-12-07 14:11:05 -05001372 oid_array_append(&ctx->oids, oid);
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001373 set_commit_pos(ctx->r, oid);
1374
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001375 return 0;
1376}
1377
Derrick Stoleec9905be2019-06-12 06:29:40 -07001378static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001379{
1380 struct commit_list *parent;
1381 for (parent = commit->parents; parent; parent = parent->next) {
Derrick Stoleecb99a342019-10-24 13:40:42 +00001382 if (!(parent->item->object.flags & REACHABLE)) {
Jeff Kinga5f1c442020-12-07 14:11:05 -05001383 oid_array_append(&ctx->oids, &parent->item->object.oid);
Derrick Stoleecb99a342019-10-24 13:40:42 +00001384 parent->item->object.flags |= REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001385 }
1386 }
1387}
1388
Derrick Stoleec9905be2019-06-12 06:29:40 -07001389static void close_reachable(struct write_commit_graph_context *ctx)
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001390{
Ævar Arnfjörð Bjarmason49bbc572019-01-19 21:21:21 +01001391 int i;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001392 struct commit *commit;
Taylor Blau98bb7962020-09-17 22:59:49 -04001393 enum commit_graph_split_flags flags = ctx->opts ?
1394 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001395
Derrick Stoleec9905be2019-06-12 06:29:40 -07001396 if (ctx->report_progress)
1397 ctx->progress = start_delayed_progress(
1398 _("Loading known commits in commit graph"),
1399 ctx->oids.nr);
1400 for (i = 0; i < ctx->oids.nr; i++) {
1401 display_progress(ctx->progress, i + 1);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001402 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001403 if (commit)
Derrick Stoleecb99a342019-10-24 13:40:42 +00001404 commit->object.flags |= REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001405 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001406 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001407
1408 /*
Derrick Stoleec9905be2019-06-12 06:29:40 -07001409 * As this loop runs, ctx->oids.nr may grow, but not more
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001410 * than the number of missing commits in the reachable
1411 * closure.
1412 */
Derrick Stoleec9905be2019-06-12 06:29:40 -07001413 if (ctx->report_progress)
1414 ctx->progress = start_delayed_progress(
1415 _("Expanding reachable commits in commit graph"),
SZEDER Gábor67fa6aa2019-09-07 01:01:33 -04001416 0);
Derrick Stoleec9905be2019-06-12 06:29:40 -07001417 for (i = 0; i < ctx->oids.nr; i++) {
1418 display_progress(ctx->progress, i + 1);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001419 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001420
Derrick Stolee6c622f92019-06-18 11:14:27 -07001421 if (!commit)
1422 continue;
1423 if (ctx->split) {
Derrick Stoleec4cc0832021-02-01 17:15:03 +00001424 if ((!repo_parse_commit(ctx->r, commit) &&
Abhishek Kumarc49c82a2020-06-17 14:44:10 +05301425 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
Taylor Blau8a6ac282020-04-13 22:04:17 -06001426 flags == COMMIT_GRAPH_SPLIT_REPLACE)
Derrick Stolee6c622f92019-06-18 11:14:27 -07001427 add_missing_parents(ctx, commit);
Derrick Stoleec4cc0832021-02-01 17:15:03 +00001428 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
Derrick Stoleec9905be2019-06-12 06:29:40 -07001429 add_missing_parents(ctx, commit);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001430 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001431 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001432
Derrick Stoleec9905be2019-06-12 06:29:40 -07001433 if (ctx->report_progress)
1434 ctx->progress = start_delayed_progress(
1435 _("Clearing commit marks in commit graph"),
1436 ctx->oids.nr);
1437 for (i = 0; i < ctx->oids.nr; i++) {
1438 display_progress(ctx->progress, i + 1);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001439 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001440
1441 if (commit)
Derrick Stoleecb99a342019-10-24 13:40:42 +00001442 commit->object.flags &= ~REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001443 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001444 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001445}
1446
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00001447static void compute_topological_levels(struct write_commit_graph_context *ctx)
1448{
1449 int i;
1450 struct commit_list *list = NULL;
1451
1452 if (ctx->report_progress)
1453 ctx->progress = start_delayed_progress(
1454 _("Computing commit graph topological levels"),
1455 ctx->commits.nr);
1456 for (i = 0; i < ctx->commits.nr; i++) {
1457 struct commit *c = ctx->commits.list[i];
1458 uint32_t level;
1459
1460 repo_parse_commit(ctx->r, c);
1461 level = *topo_level_slab_at(ctx->topo_levels, c);
1462
1463 display_progress(ctx->progress, i + 1);
1464 if (level != GENERATION_NUMBER_ZERO)
1465 continue;
1466
1467 commit_list_insert(c, &list);
1468 while (list) {
1469 struct commit *current = list->item;
1470 struct commit_list *parent;
1471 int all_parents_computed = 1;
1472 uint32_t max_level = 0;
1473
1474 for (parent = current->parents; parent; parent = parent->next) {
1475 repo_parse_commit(ctx->r, parent->item);
1476 level = *topo_level_slab_at(ctx->topo_levels, parent->item);
1477
1478 if (level == GENERATION_NUMBER_ZERO) {
1479 all_parents_computed = 0;
1480 commit_list_insert(parent->item, &list);
1481 break;
1482 }
1483
1484 if (level > max_level)
1485 max_level = level;
1486 }
1487
1488 if (all_parents_computed) {
1489 pop_commit(&list);
1490
1491 if (max_level > GENERATION_NUMBER_V1_MAX - 1)
1492 max_level = GENERATION_NUMBER_V1_MAX - 1;
1493 *topo_level_slab_at(ctx->topo_levels, current) = max_level + 1;
1494 }
1495 }
1496 }
1497 stop_progress(&ctx->progress);
1498}
1499
Derrick Stoleec9905be2019-06-12 06:29:40 -07001500static void compute_generation_numbers(struct write_commit_graph_context *ctx)
Derrick Stolee3258c662018-05-01 12:47:09 +00001501{
1502 int i;
1503 struct commit_list *list = NULL;
1504
Derrick Stoleec9905be2019-06-12 06:29:40 -07001505 if (ctx->report_progress)
Derrick Stoleeecc08692019-11-25 21:28:23 +00001506 ctx->progress = start_delayed_progress(
Derrick Stoleec9905be2019-06-12 06:29:40 -07001507 _("Computing commit graph generation numbers"),
1508 ctx->commits.nr);
Derrick Stoleefde55b02021-02-02 03:01:22 +00001509
1510 if (!ctx->trust_generation_numbers) {
1511 for (i = 0; i < ctx->commits.nr; i++) {
1512 struct commit *c = ctx->commits.list[i];
1513 repo_parse_commit(ctx->r, c);
1514 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1515 }
1516 }
1517
Derrick Stoleec9905be2019-06-12 06:29:40 -07001518 for (i = 0; i < ctx->commits.nr; i++) {
Derrick Stolee90cb1c42021-02-01 17:15:04 +00001519 struct commit *c = ctx->commits.list[i];
Derrick Stolee90cb1c42021-02-01 17:15:04 +00001520 timestamp_t corrected_commit_date;
1521
1522 repo_parse_commit(ctx->r, c);
Derrick Stolee90cb1c42021-02-01 17:15:04 +00001523 corrected_commit_date = commit_graph_data_at(c)->generation;
Abhishek Kumar48448122020-06-17 14:44:09 +05301524
Derrick Stoleec9905be2019-06-12 06:29:40 -07001525 display_progress(ctx->progress, i + 1);
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00001526 if (corrected_commit_date != GENERATION_NUMBER_ZERO)
Derrick Stolee3258c662018-05-01 12:47:09 +00001527 continue;
1528
Derrick Stolee90cb1c42021-02-01 17:15:04 +00001529 commit_list_insert(c, &list);
Derrick Stolee3258c662018-05-01 12:47:09 +00001530 while (list) {
1531 struct commit *current = list->item;
1532 struct commit_list *parent;
1533 int all_parents_computed = 1;
Abhishek Kumarc1a09112021-01-16 18:11:14 +00001534 timestamp_t max_corrected_commit_date = 0;
Derrick Stolee3258c662018-05-01 12:47:09 +00001535
1536 for (parent = current->parents; parent; parent = parent->next) {
Derrick Stolee90cb1c42021-02-01 17:15:04 +00001537 repo_parse_commit(ctx->r, parent->item);
Abhishek Kumarc1a09112021-01-16 18:11:14 +00001538 corrected_commit_date = commit_graph_data_at(parent->item)->generation;
Abhishek Kumar48448122020-06-17 14:44:09 +05301539
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00001540 if (corrected_commit_date == GENERATION_NUMBER_ZERO) {
Derrick Stolee3258c662018-05-01 12:47:09 +00001541 all_parents_computed = 0;
1542 commit_list_insert(parent->item, &list);
1543 break;
Derrick Stolee3258c662018-05-01 12:47:09 +00001544 }
Abhishek Kumarc1a09112021-01-16 18:11:14 +00001545
Abhishek Kumarc1a09112021-01-16 18:11:14 +00001546 if (corrected_commit_date > max_corrected_commit_date)
1547 max_corrected_commit_date = corrected_commit_date;
Derrick Stolee3258c662018-05-01 12:47:09 +00001548 }
1549
1550 if (all_parents_computed) {
Derrick Stolee3258c662018-05-01 12:47:09 +00001551 pop_commit(&list);
1552
Abhishek Kumarc1a09112021-01-16 18:11:14 +00001553 if (current->date && current->date > max_corrected_commit_date)
1554 max_corrected_commit_date = current->date - 1;
1555 commit_graph_data_at(current)->generation = max_corrected_commit_date + 1;
Abhishek Kumare8b63002021-01-16 18:11:15 +00001556
1557 if (commit_graph_data_at(current)->generation - current->date > GENERATION_NUMBER_V2_OFFSET_MAX)
1558 ctx->num_generation_data_overflows++;
Derrick Stolee3258c662018-05-01 12:47:09 +00001559 }
1560 }
1561 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001562 stop_progress(&ctx->progress);
Derrick Stolee3258c662018-05-01 12:47:09 +00001563}
1564
Taylor Blau312cff52020-09-16 14:07:32 -04001565static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1566{
1567 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1568 ctx->count_bloom_filter_computed);
1569 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1570 ctx->count_bloom_filter_not_computed);
Taylor Blau59f0d502020-09-17 22:59:44 -04001571 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1572 ctx->count_bloom_filter_trunc_empty);
Taylor Blau312cff52020-09-16 14:07:32 -04001573 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1574 ctx->count_bloom_filter_trunc_large);
1575}
1576
Garima Singhf97b9322020-03-30 00:31:28 +00001577static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1578{
1579 int i;
1580 struct progress *progress = NULL;
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001581 struct commit **sorted_commits;
Taylor Blau809e0322020-09-18 09:27:27 -04001582 int max_new_filters;
Garima Singhf97b9322020-03-30 00:31:28 +00001583
1584 init_bloom_filters();
1585
1586 if (ctx->report_progress)
1587 progress = start_delayed_progress(
1588 _("Computing commit changed paths Bloom filters"),
1589 ctx->commits.nr);
1590
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001591 ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1592 COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
Garima Singh3d112752020-03-30 00:31:30 +00001593
1594 if (ctx->order_by_pack)
1595 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1596 else
1597 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001598
Taylor Blau809e0322020-09-18 09:27:27 -04001599 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1600 ctx->opts->max_new_filters : ctx->commits.nr;
1601
Garima Singhf97b9322020-03-30 00:31:28 +00001602 for (i = 0; i < ctx->commits.nr; i++) {
Taylor Blau312cff52020-09-16 14:07:32 -04001603 enum bloom_filter_computed computed = 0;
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001604 struct commit *c = sorted_commits[i];
Taylor Blau312cff52020-09-16 14:07:32 -04001605 struct bloom_filter *filter = get_or_compute_bloom_filter(
1606 ctx->r,
1607 c,
Taylor Blau809e0322020-09-18 09:27:27 -04001608 ctx->count_bloom_filter_computed < max_new_filters,
Taylor Blau9a7a9ed2020-09-16 14:07:46 -04001609 ctx->bloom_settings,
Taylor Blau312cff52020-09-16 14:07:32 -04001610 &computed);
1611 if (computed & BLOOM_COMPUTED) {
1612 ctx->count_bloom_filter_computed++;
Taylor Blau59f0d502020-09-17 22:59:44 -04001613 if (computed & BLOOM_TRUNC_EMPTY)
1614 ctx->count_bloom_filter_trunc_empty++;
Taylor Blau312cff52020-09-16 14:07:32 -04001615 if (computed & BLOOM_TRUNC_LARGE)
1616 ctx->count_bloom_filter_trunc_large++;
1617 } else if (computed & BLOOM_NOT_COMPUTED)
1618 ctx->count_bloom_filter_not_computed++;
Taylor Blau809e0322020-09-18 09:27:27 -04001619 ctx->total_bloom_filter_data_size += filter
1620 ? sizeof(unsigned char) * filter->len : 0;
Garima Singhf97b9322020-03-30 00:31:28 +00001621 display_progress(progress, i + 1);
1622 }
1623
Taylor Blau312cff52020-09-16 14:07:32 -04001624 if (trace2_is_enabled())
1625 trace2_bloom_filter_write_statistics(ctx);
1626
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001627 free(sorted_commits);
Garima Singhf97b9322020-03-30 00:31:28 +00001628 stop_progress(&progress);
1629}
1630
Taylor Blau1fe10842020-05-04 19:13:35 -06001631struct refs_cb_data {
1632 struct oidset *commits;
Taylor Blaud335ce82020-05-13 15:59:33 -06001633 struct progress *progress;
Taylor Blau1fe10842020-05-04 19:13:35 -06001634};
1635
Taylor Blau6830c362020-04-13 22:04:25 -06001636static int add_ref_to_set(const char *refname,
1637 const struct object_id *oid,
1638 int flags, void *cb_data)
Derrick Stolee59fb8772018-06-27 09:24:45 -04001639{
Taylor Blau630cd512020-05-13 15:59:37 -06001640 struct object_id peeled;
Taylor Blau1fe10842020-05-04 19:13:35 -06001641 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001642
Jeff King36a31792021-01-20 14:44:43 -05001643 if (!peel_iterated_oid(oid, &peeled))
Taylor Blau630cd512020-05-13 15:59:37 -06001644 oid = &peeled;
1645 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1646 oidset_insert(data->commits, oid);
Taylor Blaud335ce82020-05-13 15:59:33 -06001647
1648 display_progress(data->progress, oidset_size(data->commits));
1649
Derrick Stolee59fb8772018-06-27 09:24:45 -04001650 return 0;
1651}
1652
Taylor Blau0bd52e22020-02-03 21:51:50 -08001653int write_commit_graph_reachable(struct object_directory *odb,
SZEDER Gábor39d88312019-08-05 10:02:39 +02001654 enum commit_graph_write_flags flags,
Taylor Blau98bb7962020-09-17 22:59:49 -04001655 const struct commit_graph_opts *opts)
Derrick Stolee59fb8772018-06-27 09:24:45 -04001656{
Taylor Blau6830c362020-04-13 22:04:25 -06001657 struct oidset commits = OIDSET_INIT;
Taylor Blau1fe10842020-05-04 19:13:35 -06001658 struct refs_cb_data data;
Derrick Stoleee103f722019-06-12 06:29:37 -07001659 int result;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001660
Taylor Blau1fe10842020-05-04 19:13:35 -06001661 memset(&data, 0, sizeof(data));
1662 data.commits = &commits;
Taylor Blaud335ce82020-05-13 15:59:33 -06001663 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1664 data.progress = start_delayed_progress(
1665 _("Collecting referenced commits"), 0);
Taylor Blau1fe10842020-05-04 19:13:35 -06001666
1667 for_each_ref(add_ref_to_set, &data);
SZEDER Gábor6f9d5f22020-07-09 18:54:32 +02001668
1669 stop_progress(&data.progress);
1670
Taylor Blau6830c362020-04-13 22:04:25 -06001671 result = write_commit_graph(odb, NULL, &commits,
Taylor Blau98bb7962020-09-17 22:59:49 -04001672 flags, opts);
Derrick Stoleef4dbdfc2018-10-03 10:12:15 -07001673
Taylor Blau6830c362020-04-13 22:04:25 -06001674 oidset_clear(&commits);
Derrick Stoleee103f722019-06-12 06:29:37 -07001675 return result;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001676}
1677
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001678static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1679 struct string_list *pack_indexes)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001680{
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001681 uint32_t i;
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001682 struct strbuf progress_title = STRBUF_INIT;
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001683 struct strbuf packname = STRBUF_INIT;
1684 int dirlen;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001685
Taylor Blau0bd52e22020-02-03 21:51:50 -08001686 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001687 dirlen = packname.len;
1688 if (ctx->report_progress) {
1689 strbuf_addf(&progress_title,
1690 Q_("Finding commits for commit graph in %d pack",
1691 "Finding commits for commit graph in %d packs",
1692 pack_indexes->nr),
1693 pack_indexes->nr);
1694 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1695 ctx->progress_done = 0;
Derrick Stolee7547b952018-04-10 08:56:08 -04001696 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001697 for (i = 0; i < pack_indexes->nr; i++) {
1698 struct packed_git *p;
1699 strbuf_setlen(&packname, dirlen);
1700 strbuf_addstr(&packname, pack_indexes->items[i].string);
1701 p = add_packed_git(packname.buf, packname.len, 1);
1702 if (!p) {
1703 error(_("error adding pack %s"), packname.buf);
1704 return -1;
Derrick Stolee7547b952018-04-10 08:56:08 -04001705 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001706 if (open_pack_index(p)) {
1707 error(_("error opening index for %s"), packname.buf);
1708 return -1;
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +00001709 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001710 for_each_object_in_pack(p, add_packed_commits, ctx,
1711 FOR_EACH_OBJECT_PACK_ORDER);
1712 close_pack(p);
1713 free(p);
Derrick Stolee3d5df012018-04-10 08:56:07 -04001714 }
1715
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001716 stop_progress(&ctx->progress);
René Scharfe0aa6bce2019-08-07 13:15:02 +02001717 strbuf_release(&progress_title);
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001718 strbuf_release(&packname);
Derrick Stolee3d5df012018-04-10 08:56:07 -04001719
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001720 return 0;
1721}
Derrick Stolee3d5df012018-04-10 08:56:07 -04001722
Taylor Blau6830c362020-04-13 22:04:25 -06001723static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1724 struct oidset *commits)
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001725{
Taylor Blau6830c362020-04-13 22:04:25 -06001726 struct oidset_iter iter;
1727 struct object_id *oid;
1728
1729 if (!oidset_size(commits))
1730 return 0;
Derrick Stolee3d5df012018-04-10 08:56:07 -04001731
Taylor Blau6830c362020-04-13 22:04:25 -06001732 oidset_iter_init(commits, &iter);
1733 while ((oid = oidset_iter_next(&iter))) {
Jeff Kinga5f1c442020-12-07 14:11:05 -05001734 oid_array_append(&ctx->oids, oid);
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001735 }
Taylor Blau6830c362020-04-13 22:04:25 -06001736
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001737 return 0;
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001738}
1739
Derrick Stoleeb2c83062019-06-12 06:29:42 -07001740static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1741{
1742 if (ctx->report_progress)
1743 ctx->progress = start_delayed_progress(
1744 _("Finding commits for commit graph among packed objects"),
1745 ctx->approx_nr_objects);
1746 for_each_packed_object(add_packed_commits, ctx,
1747 FOR_EACH_OBJECT_PACK_ORDER);
1748 if (ctx->progress_done < ctx->approx_nr_objects)
1749 display_progress(ctx->progress, ctx->approx_nr_objects);
1750 stop_progress(&ctx->progress);
1751}
1752
Derrick Stoleef998d542019-06-12 06:29:44 -07001753static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1754{
1755 uint32_t i;
Taylor Blau98bb7962020-09-17 22:59:49 -04001756 enum commit_graph_split_flags flags = ctx->opts ?
1757 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
Derrick Stoleef998d542019-06-12 06:29:44 -07001758
1759 ctx->num_extra_edges = 0;
1760 if (ctx->report_progress)
1761 ctx->progress = start_delayed_progress(
1762 _("Finding extra edges in commit graph"),
1763 ctx->oids.nr);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001764 oid_array_sort(&ctx->oids);
1765 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
René Scharfe689a1462019-09-15 19:07:44 +02001766 unsigned int num_parents;
1767
Derrick Stoleef998d542019-06-12 06:29:44 -07001768 display_progress(ctx->progress, i + 1);
Derrick Stoleef998d542019-06-12 06:29:44 -07001769
Derrick Stolee6c622f92019-06-18 11:14:27 -07001770 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001771 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001772
Taylor Blau8a6ac282020-04-13 22:04:17 -06001773 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
Abhishek Kumarc49c82a2020-06-17 14:44:10 +05301774 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
Derrick Stolee6c622f92019-06-18 11:14:27 -07001775 continue;
1776
Taylor Blau8a6ac282020-04-13 22:04:17 -06001777 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
Derrick Stoleec4cc0832021-02-01 17:15:03 +00001778 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
Taylor Blau8a6ac282020-04-13 22:04:17 -06001779 else
Derrick Stoleec4cc0832021-02-01 17:15:03 +00001780 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
Derrick Stoleef998d542019-06-12 06:29:44 -07001781
René Scharfe689a1462019-09-15 19:07:44 +02001782 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001783 if (num_parents > 2)
Derrick Stoleef998d542019-06-12 06:29:44 -07001784 ctx->num_extra_edges += num_parents - 1;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001785
Derrick Stoleef998d542019-06-12 06:29:44 -07001786 ctx->commits.nr++;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001787 }
Derrick Stoleef998d542019-06-12 06:29:44 -07001788 stop_progress(&ctx->progress);
1789}
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001790
Derrick Stolee6c622f92019-06-18 11:14:27 -07001791static int write_graph_chunk_base_1(struct hashfile *f,
1792 struct commit_graph *g)
1793{
1794 int num = 0;
1795
1796 if (!g)
1797 return 0;
1798
1799 num = write_graph_chunk_base_1(f, g->base_graph);
1800 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1801 return num + 1;
1802}
1803
1804static int write_graph_chunk_base(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001805 void *data)
Derrick Stolee6c622f92019-06-18 11:14:27 -07001806{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001807 struct write_commit_graph_context *ctx = data;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001808 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1809
1810 if (num != ctx->num_commit_graphs_after - 1) {
1811 error(_("failed to write correct number of base graph ids"));
1812 return -1;
1813 }
1814
1815 return 0;
1816}
1817
Derrick Stolee238def52019-06-12 06:29:45 -07001818static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1819{
1820 uint32_t i;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001821 int fd;
Derrick Stolee238def52019-06-12 06:29:45 -07001822 struct hashfile *f;
1823 struct lock_file lk = LOCK_INIT;
Derrick Stolee238def52019-06-12 06:29:45 -07001824 const unsigned hashsz = the_hash_algo->rawsz;
1825 struct strbuf progress_title = STRBUF_INIT;
Derrick Stolee47410aa2021-02-18 14:07:25 +00001826 struct chunkfile *cf;
brian m. carlson72871b132021-04-26 01:02:58 +00001827 unsigned char file_hash[GIT_MAX_RAWSZ];
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001828
Derrick Stolee6c622f92019-06-18 11:14:27 -07001829 if (ctx->split) {
1830 struct strbuf tmp_file = STRBUF_INIT;
1831
1832 strbuf_addf(&tmp_file,
1833 "%s/info/commit-graphs/tmp_graph_XXXXXX",
Taylor Blau0bd52e22020-02-03 21:51:50 -08001834 ctx->odb->path);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001835 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1836 } else {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001837 ctx->graph_name = get_commit_graph_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001838 }
1839
Derrick Stolee238def52019-06-12 06:29:45 -07001840 if (safe_create_leading_directories(ctx->graph_name)) {
1841 UNLEAK(ctx->graph_name);
1842 error(_("unable to create leading directories of %s"),
1843 ctx->graph_name);
1844 return -1;
Derrick Stoleef4dbdfc2018-10-03 10:12:15 -07001845 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001846
Derrick Stolee6c622f92019-06-18 11:14:27 -07001847 if (ctx->split) {
Derrick Stolee663b2b12020-09-17 18:11:46 +00001848 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001849
Taylor Blau45a43652020-04-29 11:36:46 -06001850 hold_lock_file_for_update_mode(&lk, lock_name,
1851 LOCK_DIE_ON_ERROR, 0444);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001852
1853 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1854 if (fd < 0) {
Taylor Blaua2d57e22020-04-23 15:41:02 -06001855 error(_("unable to create temporary graph layer"));
Derrick Stolee6c622f92019-06-18 11:14:27 -07001856 return -1;
1857 }
1858
Taylor Blauf4d62842020-04-29 11:36:42 -06001859 if (adjust_shared_perm(ctx->graph_name)) {
1860 error(_("unable to adjust shared permissions for '%s'"),
1861 ctx->graph_name);
1862 return -1;
1863 }
1864
Derrick Stolee6c622f92019-06-18 11:14:27 -07001865 f = hashfd(fd, ctx->graph_name);
1866 } else {
Taylor Blau1f9beca2020-04-29 11:36:38 -06001867 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1868 LOCK_DIE_ON_ERROR, 0444);
Martin Ågrena52cdce2021-01-05 20:23:47 +01001869 fd = get_lock_file_fd(&lk);
1870 f = hashfd(fd, get_lock_file_path(&lk));
Derrick Stolee6c622f92019-06-18 11:14:27 -07001871 }
Derrick Stolee238def52019-06-12 06:29:45 -07001872
Derrick Stolee47410aa2021-02-18 14:07:25 +00001873 cf = init_chunkfile(f);
1874
1875 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
1876 write_graph_chunk_fanout);
1877 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, hashsz * ctx->commits.nr,
1878 write_graph_chunk_oids);
1879 add_chunk(cf, GRAPH_CHUNKID_DATA, (hashsz + 16) * ctx->commits.nr,
1880 write_graph_chunk_data);
Abhishek Kumare8b63002021-01-16 18:11:15 +00001881
Derrick Stolee47410aa2021-02-18 14:07:25 +00001882 if (ctx->write_generation_data)
1883 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
1884 sizeof(uint32_t) * ctx->commits.nr,
1885 write_graph_chunk_generation_data);
1886 if (ctx->num_generation_data_overflows)
1887 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
1888 sizeof(timestamp_t) * ctx->num_generation_data_overflows,
1889 write_graph_chunk_generation_data_overflow);
1890 if (ctx->num_extra_edges)
1891 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
1892 4 * ctx->num_extra_edges,
1893 write_graph_chunk_extra_edges);
Garima Singh76ffbca2020-04-06 16:59:49 +00001894 if (ctx->changed_paths) {
Derrick Stolee47410aa2021-02-18 14:07:25 +00001895 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
1896 sizeof(uint32_t) * ctx->commits.nr,
1897 write_graph_chunk_bloom_indexes);
1898 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
1899 sizeof(uint32_t) * 3
1900 + ctx->total_bloom_filter_data_size,
1901 write_graph_chunk_bloom_data);
Garima Singh76ffbca2020-04-06 16:59:49 +00001902 }
Derrick Stolee47410aa2021-02-18 14:07:25 +00001903 if (ctx->num_commit_graphs_after > 1)
1904 add_chunk(cf, GRAPH_CHUNKID_BASE,
1905 hashsz * (ctx->num_commit_graphs_after - 1),
1906 write_graph_chunk_base);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001907
1908 hashwrite_be32(f, GRAPH_SIGNATURE);
1909
1910 hashwrite_u8(f, GRAPH_VERSION);
brian m. carlsonc1665992018-11-14 04:09:35 +00001911 hashwrite_u8(f, oid_version());
Derrick Stolee47410aa2021-02-18 14:07:25 +00001912 hashwrite_u8(f, get_num_chunks(cf));
Derrick Stolee6c622f92019-06-18 11:14:27 -07001913 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001914
Derrick Stolee238def52019-06-12 06:29:45 -07001915 if (ctx->report_progress) {
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001916 strbuf_addf(&progress_title,
1917 Q_("Writing out commit graph in %d pass",
1918 "Writing out commit graph in %d passes",
Taylor Blauc4ff24b2021-02-24 12:12:22 -05001919 get_num_chunks(cf)),
1920 get_num_chunks(cf));
Derrick Stolee238def52019-06-12 06:29:45 -07001921 ctx->progress = start_delayed_progress(
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001922 progress_title.buf,
Taylor Blauc4ff24b2021-02-24 12:12:22 -05001923 get_num_chunks(cf) * ctx->commits.nr);
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001924 }
SZEDER Gábor17e62752020-07-01 13:27:26 +00001925
Derrick Stolee47410aa2021-02-18 14:07:25 +00001926 write_chunkfile(cf, ctx);
SZEDER Gábor17e62752020-07-01 13:27:26 +00001927
Derrick Stolee238def52019-06-12 06:29:45 -07001928 stop_progress(&ctx->progress);
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001929 strbuf_release(&progress_title);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001930
Derrick Stolee6c622f92019-06-18 11:14:27 -07001931 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1932 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001933 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001934
1935 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1936 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1937 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1938 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1939 }
1940
Derrick Stoleec3a3a962019-05-17 11:41:47 -07001941 close_commit_graph(ctx->r->objects);
brian m. carlson72871b132021-04-26 01:02:58 +00001942 finalize_hashfile(f, file_hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
Derrick Stolee47410aa2021-02-18 14:07:25 +00001943 free_chunkfile(cf);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001944
1945 if (ctx->split) {
1946 FILE *chainf = fdopen_lock_file(&lk, "w");
1947 char *final_graph_name;
1948 int result;
1949
1950 close(fd);
1951
1952 if (!chainf) {
1953 error(_("unable to open commit-graph chain file"));
1954 return -1;
1955 }
1956
1957 if (ctx->base_graph_name) {
Taylor Blau8a6ac282020-04-13 22:04:17 -06001958 const char *dest;
1959 int idx = ctx->num_commit_graphs_after - 1;
1960 if (ctx->num_commit_graphs_after > 1)
1961 idx--;
1962
1963 dest = ctx->commit_graph_filenames_after[idx];
Derrick Stolee6c622f92019-06-18 11:14:27 -07001964
Derrick Stolee135a7122019-06-18 11:14:28 -07001965 if (strcmp(ctx->base_graph_name, dest)) {
1966 result = rename(ctx->base_graph_name, dest);
1967
1968 if (result) {
1969 error(_("failed to rename base commit-graph file"));
1970 return -1;
1971 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07001972 }
1973 } else {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001974 char *graph_name = get_commit_graph_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001975 unlink(graph_name);
1976 }
1977
brian m. carlson72871b132021-04-26 01:02:58 +00001978 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001979 final_graph_name = get_split_graph_filename(ctx->odb,
Derrick Stolee6c622f92019-06-18 11:14:27 -07001980 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1981 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1982
1983 result = rename(ctx->graph_name, final_graph_name);
1984
1985 for (i = 0; i < ctx->num_commit_graphs_after; i++)
Martin Ågrena52cdce2021-01-05 20:23:47 +01001986 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001987
1988 if (result) {
1989 error(_("failed to rename temporary commit-graph file"));
1990 return -1;
1991 }
1992 }
1993
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001994 commit_lock_file(&lk);
1995
Derrick Stolee238def52019-06-12 06:29:45 -07001996 return 0;
1997}
1998
Derrick Stolee1771be92019-06-18 11:14:29 -07001999static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2000{
Alex Henrie8da02ce2019-09-30 20:29:34 -06002001 struct commit_graph *g;
2002 uint32_t num_commits;
Taylor Blaufdbde822020-04-13 22:04:12 -06002003 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
Derrick Stolee1771be92019-06-18 11:14:29 -07002004 uint32_t i;
2005
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07002006 int max_commits = 0;
2007 int size_mult = 2;
2008
Taylor Blau98bb7962020-09-17 22:59:49 -04002009 if (ctx->opts) {
2010 max_commits = ctx->opts->max_commits;
Derrick Stolee63020f12020-01-02 16:14:14 +00002011
Taylor Blau98bb7962020-09-17 22:59:49 -04002012 if (ctx->opts->size_multiple)
2013 size_mult = ctx->opts->size_multiple;
Taylor Blaufdbde822020-04-13 22:04:12 -06002014
Taylor Blau98bb7962020-09-17 22:59:49 -04002015 flags = ctx->opts->split_flags;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07002016 }
2017
Derrick Stolee1771be92019-06-18 11:14:29 -07002018 g = ctx->r->objects->commit_graph;
Alex Henrie8da02ce2019-09-30 20:29:34 -06002019 num_commits = ctx->commits.nr;
Taylor Blau8a6ac282020-04-13 22:04:17 -06002020 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2021 ctx->num_commit_graphs_after = 1;
2022 else
2023 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
Derrick Stolee1771be92019-06-18 11:14:29 -07002024
Taylor Blau8a6ac282020-04-13 22:04:17 -06002025 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2026 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
Taylor Blaufdbde822020-04-13 22:04:12 -06002027 while (g && (g->num_commits <= size_mult * num_commits ||
2028 (max_commits && num_commits > max_commits))) {
2029 if (g->odb != ctx->odb)
2030 break;
Derrick Stoleec5230352019-06-18 11:14:30 -07002031
Taylor Blaufdbde822020-04-13 22:04:12 -06002032 num_commits += g->num_commits;
2033 g = g->base_graph;
Derrick Stolee1771be92019-06-18 11:14:29 -07002034
Taylor Blaufdbde822020-04-13 22:04:12 -06002035 ctx->num_commit_graphs_after--;
2036 }
Derrick Stolee1771be92019-06-18 11:14:29 -07002037 }
2038
Taylor Blau8a6ac282020-04-13 22:04:17 -06002039 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2040 ctx->new_base_graph = g;
2041 else if (ctx->num_commit_graphs_after != 1)
2042 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2043 "should be 1 with --split=replace");
Derrick Stolee1771be92019-06-18 11:14:29 -07002044
Derrick Stoleec5230352019-06-18 11:14:30 -07002045 if (ctx->num_commit_graphs_after == 2) {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08002046 char *old_graph_name = get_commit_graph_filename(g->odb);
Derrick Stoleec5230352019-06-18 11:14:30 -07002047
2048 if (!strcmp(g->filename, old_graph_name) &&
Taylor Blauad2dd5b2020-02-03 13:18:02 -08002049 g->odb != ctx->odb) {
Derrick Stoleec5230352019-06-18 11:14:30 -07002050 ctx->num_commit_graphs_after = 1;
2051 ctx->new_base_graph = NULL;
2052 }
2053
2054 free(old_graph_name);
2055 }
2056
Taylor Blaub78a5562020-04-23 15:41:09 -06002057 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2058 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
Derrick Stolee1771be92019-06-18 11:14:29 -07002059
2060 for (i = 0; i < ctx->num_commit_graphs_after &&
2061 i < ctx->num_commit_graphs_before; i++)
2062 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2063
2064 i = ctx->num_commit_graphs_before - 1;
2065 g = ctx->r->objects->commit_graph;
2066
2067 while (g) {
2068 if (i < ctx->num_commit_graphs_after)
2069 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2070
Abhishek Kumar1fdc3832021-01-16 18:11:16 +00002071 /*
2072 * If the topmost remaining layer has generation data chunk, the
2073 * resultant layer also has generation data chunk.
2074 */
2075 if (i == ctx->num_commit_graphs_after - 2)
2076 ctx->write_generation_data = !!g->chunk_generation_data;
2077
Derrick Stolee1771be92019-06-18 11:14:29 -07002078 i--;
2079 g = g->base_graph;
2080 }
2081}
2082
2083static void merge_commit_graph(struct write_commit_graph_context *ctx,
2084 struct commit_graph *g)
2085{
2086 uint32_t i;
2087 uint32_t offset = g->num_commits_in_base;
2088
2089 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2090
2091 for (i = 0; i < g->num_commits; i++) {
2092 struct object_id oid;
2093 struct commit *result;
2094
2095 display_progress(ctx->progress, i + 1);
2096
2097 load_oid_from_graph(g, i + offset, &oid);
2098
2099 /* only add commits if they still exist in the repo */
2100 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2101
2102 if (result) {
2103 ctx->commits.list[ctx->commits.nr] = result;
2104 ctx->commits.nr++;
2105 }
2106 }
2107}
2108
2109static int commit_compare(const void *_a, const void *_b)
2110{
2111 const struct commit *a = *(const struct commit **)_a;
2112 const struct commit *b = *(const struct commit **)_b;
2113 return oidcmp(&a->object.oid, &b->object.oid);
2114}
2115
2116static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2117{
Derrick Stolee150f1152020-10-09 20:53:51 +00002118 uint32_t i, dedup_i = 0;
Derrick Stolee1771be92019-06-18 11:14:29 -07002119
2120 if (ctx->report_progress)
2121 ctx->progress = start_delayed_progress(
2122 _("Scanning merged commits"),
2123 ctx->commits.nr);
2124
2125 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2126
2127 ctx->num_extra_edges = 0;
2128 for (i = 0; i < ctx->commits.nr; i++) {
SZEDER Gábor40112242021-09-09 03:10:11 +02002129 display_progress(ctx->progress, i + 1);
Derrick Stolee1771be92019-06-18 11:14:29 -07002130
2131 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2132 &ctx->commits.list[i]->object.oid)) {
Derrick Stolee150f1152020-10-09 20:53:51 +00002133 /*
2134 * Silently ignore duplicates. These were likely
2135 * created due to a commit appearing in multiple
2136 * layers of the chain, which is unexpected but
2137 * not invalid. We should make sure there is a
2138 * unique copy in the new layer.
2139 */
Derrick Stolee1771be92019-06-18 11:14:29 -07002140 } else {
René Scharfe689a1462019-09-15 19:07:44 +02002141 unsigned int num_parents;
Derrick Stolee1771be92019-06-18 11:14:29 -07002142
Derrick Stolee150f1152020-10-09 20:53:51 +00002143 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2144 dedup_i++;
2145
René Scharfe689a1462019-09-15 19:07:44 +02002146 num_parents = commit_list_count(ctx->commits.list[i]->parents);
Derrick Stolee1771be92019-06-18 11:14:29 -07002147 if (num_parents > 2)
Derrick Stoleea35bea42019-08-05 09:43:41 -07002148 ctx->num_extra_edges += num_parents - 1;
Derrick Stolee1771be92019-06-18 11:14:29 -07002149 }
2150 }
2151
Derrick Stolee150f1152020-10-09 20:53:51 +00002152 ctx->commits.nr = dedup_i;
2153
Derrick Stolee1771be92019-06-18 11:14:29 -07002154 stop_progress(&ctx->progress);
2155}
2156
2157static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2158{
2159 struct commit_graph *g = ctx->r->objects->commit_graph;
2160 uint32_t current_graph_number = ctx->num_commit_graphs_before;
Derrick Stolee1771be92019-06-18 11:14:29 -07002161
2162 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2163 current_graph_number--;
2164
René Scharfed68ce902020-02-20 19:49:18 +01002165 if (ctx->report_progress)
2166 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
Derrick Stolee1771be92019-06-18 11:14:29 -07002167
2168 merge_commit_graph(ctx, g);
2169 stop_progress(&ctx->progress);
Derrick Stolee1771be92019-06-18 11:14:29 -07002170
2171 g = g->base_graph;
2172 }
2173
2174 if (g) {
2175 ctx->new_base_graph = g;
2176 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2177 }
2178
2179 if (ctx->new_base_graph)
2180 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2181
2182 sort_and_scan_merged_commits(ctx);
2183}
2184
Derrick Stolee8d840972019-06-18 11:14:31 -07002185static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2186{
2187 uint32_t i;
2188 time_t now = time(NULL);
2189
2190 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2191 struct stat st;
2192 struct utimbuf updated_time;
2193
2194 stat(ctx->commit_graph_filenames_before[i], &st);
2195
2196 updated_time.actime = st.st_atime;
2197 updated_time.modtime = now;
2198 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2199 }
2200}
2201
2202static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2203{
2204 struct strbuf path = STRBUF_INIT;
2205 DIR *dir;
2206 struct dirent *de;
2207 size_t dirnamelen;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07002208 timestamp_t expire_time = time(NULL);
2209
Taylor Blau98bb7962020-09-17 22:59:49 -04002210 if (ctx->opts && ctx->opts->expire_time)
2211 expire_time = ctx->opts->expire_time;
Derrick Stoleeba411122019-06-18 11:14:33 -07002212 if (!ctx->split) {
Derrick Stolee663b2b12020-09-17 18:11:46 +00002213 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
Derrick Stoleeba411122019-06-18 11:14:33 -07002214 unlink(chain_file_name);
2215 free(chain_file_name);
2216 ctx->num_commit_graphs_after = 0;
2217 }
Derrick Stolee8d840972019-06-18 11:14:31 -07002218
Taylor Blau0bd52e22020-02-03 21:51:50 -08002219 strbuf_addstr(&path, ctx->odb->path);
Derrick Stolee8d840972019-06-18 11:14:31 -07002220 strbuf_addstr(&path, "/info/commit-graphs");
2221 dir = opendir(path.buf);
2222
René Scharfe0aa6bce2019-08-07 13:15:02 +02002223 if (!dir)
2224 goto out;
Derrick Stolee8d840972019-06-18 11:14:31 -07002225
2226 strbuf_addch(&path, '/');
2227 dirnamelen = path.len;
2228 while ((de = readdir(dir)) != NULL) {
2229 struct stat st;
2230 uint32_t i, found = 0;
2231
2232 strbuf_setlen(&path, dirnamelen);
2233 strbuf_addstr(&path, de->d_name);
2234
2235 stat(path.buf, &st);
2236
2237 if (st.st_mtime > expire_time)
2238 continue;
2239 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2240 continue;
2241
2242 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2243 if (!strcmp(ctx->commit_graph_filenames_after[i],
2244 path.buf)) {
2245 found = 1;
2246 break;
2247 }
2248 }
2249
2250 if (!found)
2251 unlink(path.buf);
Derrick Stolee8d840972019-06-18 11:14:31 -07002252 }
René Scharfe0aa6bce2019-08-07 13:15:02 +02002253
2254out:
2255 strbuf_release(&path);
Derrick Stolee8d840972019-06-18 11:14:31 -07002256}
2257
Taylor Blau0bd52e22020-02-03 21:51:50 -08002258int write_commit_graph(struct object_directory *odb,
Derrick Stoleee103f722019-06-12 06:29:37 -07002259 struct string_list *pack_indexes,
Taylor Blau6830c362020-04-13 22:04:25 -06002260 struct oidset *commits,
SZEDER Gábor39d88312019-08-05 10:02:39 +02002261 enum commit_graph_write_flags flags,
Taylor Blau98bb7962020-09-17 22:59:49 -04002262 const struct commit_graph_opts *opts)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002263{
Derrick Stoleec7ef8fe2021-02-25 18:19:42 +00002264 struct repository *r = the_repository;
Derrick Stoleec9905be2019-06-12 06:29:40 -07002265 struct write_commit_graph_context *ctx;
Jeff King1cbdbf32020-12-07 14:11:02 -05002266 uint32_t i;
Derrick Stoleee103f722019-06-12 06:29:37 -07002267 int res = 0;
Taylor Blau8a6ac282020-04-13 22:04:17 -06002268 int replace = 0;
Taylor Blau9a7a9ed2020-09-16 14:07:46 -04002269 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +00002270 struct topo_level_slab topo_levels;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002271
Derrick Stoleec7ef8fe2021-02-25 18:19:42 +00002272 prepare_repo_settings(r);
2273 if (!r->settings.core_commit_graph) {
Derrick Stolee85102ac2020-10-09 20:53:52 +00002274 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2275 return 0;
2276 }
Derrick Stoleec7ef8fe2021-02-25 18:19:42 +00002277 if (!commit_graph_compatible(r))
Derrick Stoleee103f722019-06-12 06:29:37 -07002278 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002279
René Scharfeca56dad2021-03-13 17:17:22 +01002280 CALLOC_ARRAY(ctx, 1);
Derrick Stoleec7ef8fe2021-02-25 18:19:42 +00002281 ctx->r = r;
Taylor Blau0bd52e22020-02-03 21:51:50 -08002282 ctx->odb = odb;
SZEDER Gábor39d88312019-08-05 10:02:39 +02002283 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2284 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2285 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
Taylor Blau98bb7962020-09-17 22:59:49 -04002286 ctx->opts = opts;
Garima Singhf97b9322020-03-30 00:31:28 +00002287 ctx->total_bloom_filter_data_size = 0;
Derrick Stolee702110a2021-02-25 18:19:43 +00002288 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
Abhishek Kumare8b63002021-01-16 18:11:15 +00002289 ctx->num_generation_data_overflows = 0;
Derrick Stolee6c622f92019-06-18 11:14:27 -07002290
Taylor Blau9a7a9ed2020-09-16 14:07:46 -04002291 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2292 bloom_settings.bits_per_entry);
2293 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2294 bloom_settings.num_hashes);
2295 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2296 bloom_settings.max_changed_paths);
2297 ctx->bloom_settings = &bloom_settings;
2298
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +00002299 init_topo_level_slab(&topo_levels);
2300 ctx->topo_levels = &topo_levels;
2301
Derrick Stoleebc50d6c2021-02-02 03:01:23 +00002302 prepare_commit_graph(ctx->r);
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +00002303 if (ctx->r->objects->commit_graph) {
2304 struct commit_graph *g = ctx->r->objects->commit_graph;
2305
2306 while (g) {
2307 g->topo_levels = &topo_levels;
2308 g = g->base_graph;
2309 }
2310 }
2311
Derrick Stolee0087a872020-07-01 13:27:24 +00002312 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2313 ctx->changed_paths = 1;
2314 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2315 struct commit_graph *g;
Derrick Stolee0087a872020-07-01 13:27:24 +00002316
2317 g = ctx->r->objects->commit_graph;
2318
2319 /* We have changed-paths already. Keep them in the next graph */
2320 if (g && g->chunk_bloom_data) {
2321 ctx->changed_paths = 1;
2322 ctx->bloom_settings = g->bloom_filter_settings;
2323 }
2324 }
2325
Derrick Stolee6c622f92019-06-18 11:14:27 -07002326 if (ctx->split) {
Derrick Stoleebc50d6c2021-02-02 03:01:23 +00002327 struct commit_graph *g = ctx->r->objects->commit_graph;
Derrick Stolee6c622f92019-06-18 11:14:27 -07002328
2329 while (g) {
2330 ctx->num_commit_graphs_before++;
2331 g = g->base_graph;
2332 }
2333
2334 if (ctx->num_commit_graphs_before) {
2335 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2336 i = ctx->num_commit_graphs_before;
2337 g = ctx->r->objects->commit_graph;
2338
2339 while (g) {
2340 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2341 g = g->base_graph;
2342 }
2343 }
Taylor Blau8a6ac282020-04-13 22:04:17 -06002344
Taylor Blau98bb7962020-09-17 22:59:49 -04002345 if (ctx->opts)
2346 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
Derrick Stolee6c622f92019-06-18 11:14:27 -07002347 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002348
Derrick Stoleec9905be2019-06-12 06:29:40 -07002349 ctx->approx_nr_objects = approximate_object_count();
Derrick Stoleec9905be2019-06-12 06:29:40 -07002350
Derrick Stoleec9905be2019-06-12 06:29:40 -07002351 if (ctx->append && ctx->r->objects->commit_graph) {
2352 struct commit_graph *g = ctx->r->objects->commit_graph;
2353 for (i = 0; i < g->num_commits; i++) {
Jeff Kinga5f1c442020-12-07 14:11:05 -05002354 struct object_id oid;
brian m. carlson92e2cab2021-04-26 01:02:50 +00002355 oidread(&oid, g->chunk_oid_lookup + g->hash_len * i);
Jeff Kinga5f1c442020-12-07 14:11:05 -05002356 oid_array_append(&ctx->oids, &oid);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002357 }
2358 }
2359
2360 if (pack_indexes) {
Garima Singh3d112752020-03-30 00:31:30 +00002361 ctx->order_by_pack = 1;
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07002362 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2363 goto cleanup;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002364 }
2365
Taylor Blau6830c362020-04-13 22:04:25 -06002366 if (commits) {
2367 if ((res = fill_oids_from_commits(ctx, commits)))
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02002368 goto cleanup;
2369 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002370
Junio C Hamano9b6606f2020-05-01 13:39:53 -07002371 if (!pack_indexes && !commits) {
Garima Singh3d112752020-03-30 00:31:30 +00002372 ctx->order_by_pack = 1;
Derrick Stoleeb2c83062019-06-12 06:29:42 -07002373 fill_oids_from_all_packs(ctx);
Garima Singh3d112752020-03-30 00:31:30 +00002374 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002375
Derrick Stoleec9905be2019-06-12 06:29:40 -07002376 close_reachable(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002377
Derrick Stoleef998d542019-06-12 06:29:44 -07002378 copy_oids_to_commits(ctx);
Derrick Stolee1373e542018-06-27 09:24:39 -04002379
Derrick Stoleec9905be2019-06-12 06:29:40 -07002380 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
Derrick Stoleee103f722019-06-12 06:29:37 -07002381 error(_("too many commits to write graph"));
2382 res = -1;
2383 goto cleanup;
2384 }
Derrick Stolee283e68c2018-06-27 09:24:32 -04002385
Taylor Blau8a6ac282020-04-13 22:04:17 -06002386 if (!ctx->commits.nr && !replace)
Derrick Stolee6c622f92019-06-18 11:14:27 -07002387 goto cleanup;
2388
Derrick Stolee1771be92019-06-18 11:14:29 -07002389 if (ctx->split) {
2390 split_graph_merge_strategy(ctx);
2391
Taylor Blau8a6ac282020-04-13 22:04:17 -06002392 if (!replace)
2393 merge_commit_graphs(ctx);
Derrick Stolee1771be92019-06-18 11:14:29 -07002394 } else
Derrick Stolee6c622f92019-06-18 11:14:27 -07002395 ctx->num_commit_graphs_after = 1;
2396
Derrick Stoleefde55b02021-02-02 03:01:22 +00002397 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
Abhishek Kumar1fdc3832021-01-16 18:11:16 +00002398
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00002399 compute_topological_levels(ctx);
2400 if (ctx->write_generation_data)
2401 compute_generation_numbers(ctx);
Derrick Stolee9bda8462018-06-27 09:24:35 -04002402
Garima Singhf97b9322020-03-30 00:31:28 +00002403 if (ctx->changed_paths)
2404 compute_bloom_filters(ctx);
2405
Derrick Stolee238def52019-06-12 06:29:45 -07002406 res = write_commit_graph_file(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002407
Derrick Stoleeba411122019-06-18 11:14:33 -07002408 if (ctx->split)
Derrick Stolee8d840972019-06-18 11:14:31 -07002409 mark_commit_graphs(ctx);
Derrick Stoleeba411122019-06-18 11:14:33 -07002410
2411 expire_commit_graphs(ctx);
Derrick Stolee8d840972019-06-18 11:14:31 -07002412
Derrick Stoleee103f722019-06-12 06:29:37 -07002413cleanup:
Derrick Stolee238def52019-06-12 06:29:45 -07002414 free(ctx->graph_name);
Derrick Stoleec9905be2019-06-12 06:29:40 -07002415 free(ctx->commits.list);
Jeff Kinga5f1c442020-12-07 14:11:05 -05002416 oid_array_clear(&ctx->oids);
Andrzej Huntbf4bb9f2021-02-19 20:13:10 +00002417 clear_topo_level_slab(&topo_levels);
Derrick Stolee6c622f92019-06-18 11:14:27 -07002418
2419 if (ctx->commit_graph_filenames_after) {
2420 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2421 free(ctx->commit_graph_filenames_after[i]);
2422 free(ctx->commit_graph_hash_after[i]);
2423 }
2424
2425 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2426 free(ctx->commit_graph_filenames_before[i]);
2427
2428 free(ctx->commit_graph_filenames_after);
2429 free(ctx->commit_graph_filenames_before);
2430 free(ctx->commit_graph_hash_after);
2431 }
2432
Derrick Stoleec9905be2019-06-12 06:29:40 -07002433 free(ctx);
Derrick Stoleee103f722019-06-12 06:29:37 -07002434
2435 return res;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002436}
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002437
2438#define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2439static int verify_commit_graph_error;
2440
Ævar Arnfjörð Bjarmason48ca53c2021-07-13 10:05:18 +02002441__attribute__((format (printf, 1, 2)))
Derrick Stolee283e68c2018-06-27 09:24:32 -04002442static void graph_report(const char *fmt, ...)
2443{
2444 va_list ap;
2445
2446 verify_commit_graph_error = 1;
2447 va_start(ap, fmt);
2448 vfprintf(stderr, fmt, ap);
2449 fprintf(stderr, "\n");
2450 va_end(ap);
2451}
2452
2453#define GENERATION_ZERO_EXISTS 1
2454#define GENERATION_NUMBER_EXISTS 2
2455
Taylor Blau15316a42021-06-23 14:39:09 -04002456static int commit_graph_checksum_valid(struct commit_graph *g)
2457{
2458 return hashfile_checksum_valid(g->data, g->data_len);
2459}
2460
Derrick Stolee3da4b602019-06-18 11:14:32 -07002461int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
Derrick Stolee283e68c2018-06-27 09:24:32 -04002462{
Derrick Stolee9bda8462018-06-27 09:24:35 -04002463 uint32_t i, cur_fanout_pos = 0;
brian m. carlson72871b132021-04-26 01:02:58 +00002464 struct object_id prev_oid, cur_oid;
Derrick Stolee1373e542018-06-27 09:24:39 -04002465 int generation_zero = 0;
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00002466 struct progress *progress = NULL;
Derrick Stolee3da4b602019-06-18 11:14:32 -07002467 int local_error = 0;
Derrick Stolee9bda8462018-06-27 09:24:35 -04002468
Derrick Stolee283e68c2018-06-27 09:24:32 -04002469 if (!g) {
2470 graph_report("no commit-graph file loaded");
2471 return 1;
2472 }
2473
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +01002474 verify_commit_graph_error = verify_commit_graph_lite(g);
Derrick Stolee9bda8462018-06-27 09:24:35 -04002475 if (verify_commit_graph_error)
2476 return verify_commit_graph_error;
2477
Taylor Blau15316a42021-06-23 14:39:09 -04002478 if (!commit_graph_checksum_valid(g)) {
Derrick Stolee41df0e32018-06-27 09:24:42 -04002479 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2480 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2481 }
2482
Derrick Stolee9bda8462018-06-27 09:24:35 -04002483 for (i = 0; i < g->num_commits; i++) {
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002484 struct commit *graph_commit;
2485
brian m. carlson92e2cab2021-04-26 01:02:50 +00002486 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
Derrick Stolee9bda8462018-06-27 09:24:35 -04002487
2488 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002489 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04002490 oid_to_hex(&prev_oid),
2491 oid_to_hex(&cur_oid));
2492
2493 oidcpy(&prev_oid, &cur_oid);
2494
2495 while (cur_oid.hash[0] > cur_fanout_pos) {
2496 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2497
2498 if (i != fanout_value)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002499 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04002500 cur_fanout_pos, fanout_value, i);
2501 cur_fanout_pos++;
2502 }
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002503
Junio C Hamano82952962018-07-17 15:46:19 -07002504 graph_commit = lookup_commit(r, &cur_oid);
Stefan Beller4f542b72018-12-14 16:09:39 -08002505 if (!parse_commit_in_graph_one(r, g, graph_commit))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002506 graph_report(_("failed to parse commit %s from commit-graph"),
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002507 oid_to_hex(&cur_oid));
Derrick Stolee9bda8462018-06-27 09:24:35 -04002508 }
2509
2510 while (cur_fanout_pos < 256) {
2511 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2512
2513 if (g->num_commits != fanout_value)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002514 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04002515 cur_fanout_pos, fanout_value, i);
2516
2517 cur_fanout_pos++;
2518 }
2519
Derrick Stolee41df0e32018-06-27 09:24:42 -04002520 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
Derrick Stolee96af91d2018-06-27 09:24:36 -04002521 return verify_commit_graph_error;
2522
Garima Singh73716122019-08-26 09:29:58 -07002523 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2524 progress = start_progress(_("Verifying commits in commit graph"),
2525 g->num_commits);
2526
Derrick Stolee96af91d2018-06-27 09:24:36 -04002527 for (i = 0; i < g->num_commits; i++) {
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002528 struct commit *graph_commit, *odb_commit;
Derrick Stolee53614b12018-06-27 09:24:38 -04002529 struct commit_list *graph_parents, *odb_parents;
Abhishek Kumard7f92782021-01-16 18:11:13 +00002530 timestamp_t max_generation = 0;
2531 timestamp_t generation;
Derrick Stolee96af91d2018-06-27 09:24:36 -04002532
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00002533 display_progress(progress, i + 1);
brian m. carlson92e2cab2021-04-26 01:02:50 +00002534 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002535
Junio C Hamano82952962018-07-17 15:46:19 -07002536 graph_commit = lookup_commit(r, &cur_oid);
Jeff Kinga3785092019-06-20 03:41:21 -04002537 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
Derrick Stolee96af91d2018-06-27 09:24:36 -04002538 if (parse_commit_internal(odb_commit, 0, 0)) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002539 graph_report(_("failed to parse commit %s from object database for commit-graph"),
Derrick Stolee96af91d2018-06-27 09:24:36 -04002540 oid_to_hex(&cur_oid));
2541 continue;
2542 }
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002543
Stefan Beller4f542b72018-12-14 16:09:39 -08002544 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002545 get_commit_tree_oid(odb_commit)))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002546 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002547 oid_to_hex(&cur_oid),
2548 oid_to_hex(get_commit_tree_oid(graph_commit)),
2549 oid_to_hex(get_commit_tree_oid(odb_commit)));
Derrick Stolee53614b12018-06-27 09:24:38 -04002550
2551 graph_parents = graph_commit->parents;
2552 odb_parents = odb_commit->parents;
2553
2554 while (graph_parents) {
2555 if (odb_parents == NULL) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002556 graph_report(_("commit-graph parent list for commit %s is too long"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002557 oid_to_hex(&cur_oid));
2558 break;
2559 }
2560
Derrick Stolee3da4b602019-06-18 11:14:32 -07002561 /* parse parent in case it is in a base graph */
2562 parse_commit_in_graph_one(r, g, graph_parents->item);
2563
Jeff King9001dc22018-08-28 17:22:48 -04002564 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002565 graph_report(_("commit-graph parent for %s is %s != %s"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002566 oid_to_hex(&cur_oid),
2567 oid_to_hex(&graph_parents->item->object.oid),
2568 oid_to_hex(&odb_parents->item->object.oid));
2569
Abhishek Kumarc752ad02020-06-17 14:44:11 +05302570 generation = commit_graph_generation(graph_parents->item);
2571 if (generation > max_generation)
2572 max_generation = generation;
Derrick Stolee1373e542018-06-27 09:24:39 -04002573
Derrick Stolee53614b12018-06-27 09:24:38 -04002574 graph_parents = graph_parents->next;
2575 odb_parents = odb_parents->next;
2576 }
2577
2578 if (odb_parents != NULL)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002579 graph_report(_("commit-graph parent list for commit %s terminates early"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002580 oid_to_hex(&cur_oid));
Derrick Stolee1373e542018-06-27 09:24:39 -04002581
Abhishek Kumarc49c82a2020-06-17 14:44:10 +05302582 if (!commit_graph_generation(graph_commit)) {
Derrick Stolee1373e542018-06-27 09:24:39 -04002583 if (generation_zero == GENERATION_NUMBER_EXISTS)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002584 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
Derrick Stolee1373e542018-06-27 09:24:39 -04002585 oid_to_hex(&cur_oid));
2586 generation_zero = GENERATION_ZERO_EXISTS;
2587 } else if (generation_zero == GENERATION_ZERO_EXISTS)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002588 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
Derrick Stolee1373e542018-06-27 09:24:39 -04002589 oid_to_hex(&cur_oid));
2590
2591 if (generation_zero == GENERATION_ZERO_EXISTS)
2592 continue;
2593
2594 /*
Abhishek Kumare8b63002021-01-16 18:11:15 +00002595 * If we are using topological level and one of our parents has
2596 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2597 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2598 * in the following condition.
Derrick Stolee1373e542018-06-27 09:24:39 -04002599 */
Abhishek Kumar1fdc3832021-01-16 18:11:16 +00002600 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
Derrick Stolee1373e542018-06-27 09:24:39 -04002601 max_generation--;
2602
Abhishek Kumarc752ad02020-06-17 14:44:11 +05302603 generation = commit_graph_generation(graph_commit);
Abhishek Kumare8b63002021-01-16 18:11:15 +00002604 if (generation < max_generation + 1)
2605 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
Derrick Stolee1373e542018-06-27 09:24:39 -04002606 oid_to_hex(&cur_oid),
Abhishek Kumarc752ad02020-06-17 14:44:11 +05302607 generation,
Derrick Stolee1373e542018-06-27 09:24:39 -04002608 max_generation + 1);
Derrick Stolee88968eb2018-06-27 09:24:40 -04002609
2610 if (graph_commit->date != odb_commit->date)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002611 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
Derrick Stolee88968eb2018-06-27 09:24:40 -04002612 oid_to_hex(&cur_oid),
2613 graph_commit->date,
2614 odb_commit->date);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002615 }
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00002616 stop_progress(&progress);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002617
Derrick Stolee3da4b602019-06-18 11:14:32 -07002618 local_error = verify_commit_graph_error;
2619
2620 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2621 local_error |= verify_commit_graph(r, g->base_graph, flags);
2622
2623 return local_error;
Derrick Stolee283e68c2018-06-27 09:24:32 -04002624}
Jonathan Tanc3756d52018-07-11 15:42:40 -07002625
2626void free_commit_graph(struct commit_graph *g)
2627{
2628 if (!g)
2629 return;
Jeff Kingc8828532020-04-23 15:41:13 -06002630 if (g->data) {
Jonathan Tanc3756d52018-07-11 15:42:40 -07002631 munmap((void *)g->data, g->data_len);
2632 g->data = NULL;
Jonathan Tanc3756d52018-07-11 15:42:40 -07002633 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07002634 free(g->filename);
Garima Singh76ffbca2020-04-06 16:59:49 +00002635 free(g->bloom_filter_settings);
Jonathan Tanc3756d52018-07-11 15:42:40 -07002636 free(g);
2637}
Jeff King6abada12019-09-12 10:44:45 -04002638
2639void disable_commit_graph(struct repository *r)
2640{
2641 r->commit_graph_disabled = 1;
2642}