blob: a7d8755932884cfcb05fad4c539c3b8be8322897 [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" */
Derrick Stolee6dbf4b82022-03-02 09:45:13 -050042#define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
43#define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
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
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400196static struct commit_graph *alloc_commit_graph(void)
197{
198 struct commit_graph *g = xcalloc(1, sizeof(*g));
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400199
200 return g;
201}
202
Derrick Stoleed6538242018-08-20 18:24:27 +0000203extern int read_replace_refs;
204
205static int commit_graph_compatible(struct repository *r)
206{
Derrick Stolee5cef2952018-08-20 18:24:32 +0000207 if (!r->gitdir)
208 return 0;
209
Derrick Stoleed6538242018-08-20 18:24:27 +0000210 if (read_replace_refs) {
211 prepare_replace_object(r);
Junio C Hamanocdc986a2021-03-01 09:19:37 -0800212 if (hashmap_get_size(&r->objects->replace_map->map))
Derrick Stoleed6538242018-08-20 18:24:27 +0000213 return 0;
214 }
215
Derrick Stolee20fd6d52018-08-20 18:24:30 +0000216 prepare_commit_graft(r);
Taylor Blauce163642020-07-08 17:10:53 -0400217 if (r->parsed_objects &&
Junio C Hamanocdc986a2021-03-01 09:19:37 -0800218 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
Derrick Stolee20fd6d52018-08-20 18:24:30 +0000219 return 0;
Junio C Hamanocdc986a2021-03-01 09:19:37 -0800220 if (is_repository_shallow(r))
Derrick Stolee20fd6d52018-08-20 18:24:30 +0000221 return 0;
222
Derrick Stoleed6538242018-08-20 18:24:27 +0000223 return 1;
224}
225
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100226int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
227{
228 *fd = git_open(graph_file);
229 if (*fd < 0)
230 return 0;
231 if (fstat(*fd, st)) {
232 close(*fd);
233 return 0;
234 }
235 return 1;
236}
237
Taylor Blauab14d062020-09-09 11:22:56 -0400238struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
239 int fd, struct stat *st,
Taylor Blaua7df60c2020-02-03 13:18:04 -0800240 struct object_directory *odb)
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400241{
242 void *graph_map;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400243 size_t graph_size;
Josh Steadmonaa658572019-01-15 14:25:50 -0800244 struct commit_graph *ret;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400245
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100246 graph_size = xsize_t(st->st_size);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400247
248 if (graph_size < GRAPH_MIN_SIZE) {
249 close(fd);
Ævar Arnfjörð Bjarmason67a530f2019-03-25 13:08:31 +0100250 error(_("commit-graph file is too small"));
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100251 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400252 }
253 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
Jeff Kingc8828532020-04-23 15:41:13 -0600254 close(fd);
Taylor Blaua92d8522022-07-14 14:43:06 -0700255 prepare_repo_settings(r);
256 ret = parse_commit_graph(&r->settings, graph_map, graph_size);
Josh Steadmonaa658572019-01-15 14:25:50 -0800257
Taylor Blaua7df60c2020-02-03 13:18:04 -0800258 if (ret)
259 ret->odb = odb;
Jeff Kingc8828532020-04-23 15:41:13 -0600260 else
Josh Steadmonaa658572019-01-15 14:25:50 -0800261 munmap(graph_map, graph_size);
Josh Steadmonaa658572019-01-15 14:25:50 -0800262
263 return ret;
264}
265
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +0100266static int verify_commit_graph_lite(struct commit_graph *g)
267{
268 /*
269 * Basic validation shared between parse_commit_graph()
270 * which'll be called every time the graph is used, and the
271 * much more expensive verify_commit_graph() used by
272 * "commit-graph verify".
273 *
274 * There should only be very basic checks here to ensure that
275 * we don't e.g. segfault in fill_commit_in_graph(), but
276 * because this is a very hot codepath nothing that e.g. loops
277 * over g->num_commits, or runs a checksum on the commit-graph
278 * itself.
279 */
280 if (!g->chunk_oid_fanout) {
281 error("commit-graph is missing the OID Fanout chunk");
282 return 1;
283 }
284 if (!g->chunk_oid_lookup) {
285 error("commit-graph is missing the OID Lookup chunk");
286 return 1;
287 }
288 if (!g->chunk_commit_data) {
289 error("commit-graph is missing the Commit Data chunk");
290 return 1;
291 }
292
293 return 0;
294}
295
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000296static int graph_read_oid_lookup(const unsigned char *chunk_start,
297 size_t chunk_size, void *data)
298{
299 struct commit_graph *g = data;
300 g->chunk_oid_lookup = chunk_start;
301 g->num_commits = chunk_size / g->hash_len;
302 return 0;
303}
304
305static int graph_read_bloom_data(const unsigned char *chunk_start,
306 size_t chunk_size, void *data)
307{
308 struct commit_graph *g = data;
309 uint32_t hash_version;
310 g->chunk_bloom_data = chunk_start;
311 hash_version = get_be32(chunk_start);
312
313 if (hash_version != 1)
314 return 0;
315
316 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
317 g->bloom_filter_settings->hash_version = hash_version;
318 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
319 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
320 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
321
322 return 0;
323}
324
Taylor Blaua92d8522022-07-14 14:43:06 -0700325struct commit_graph *parse_commit_graph(struct repo_settings *s,
Taylor Blauab14d062020-09-09 11:22:56 -0400326 void *graph_map, size_t graph_size)
Josh Steadmonaa658572019-01-15 14:25:50 -0800327{
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000328 const unsigned char *data;
Josh Steadmonaa658572019-01-15 14:25:50 -0800329 struct commit_graph *graph;
Josh Steadmonaa658572019-01-15 14:25:50 -0800330 uint32_t graph_signature;
331 unsigned char graph_version, hash_version;
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000332 struct chunkfile *cf = NULL;
Josh Steadmonaa658572019-01-15 14:25:50 -0800333
334 if (!graph_map)
335 return NULL;
336
337 if (graph_size < GRAPH_MIN_SIZE)
338 return NULL;
339
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400340 data = (const unsigned char *)graph_map;
341
342 graph_signature = get_be32(data);
343 if (graph_signature != GRAPH_SIGNATURE) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100344 error(_("commit-graph signature %X does not match signature %X"),
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400345 graph_signature, GRAPH_SIGNATURE);
Josh Steadmonaa658572019-01-15 14:25:50 -0800346 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400347 }
348
349 graph_version = *(unsigned char*)(data + 4);
350 if (graph_version != GRAPH_VERSION) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100351 error(_("commit-graph version %X does not match version %X"),
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400352 graph_version, GRAPH_VERSION);
Josh Steadmonaa658572019-01-15 14:25:50 -0800353 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400354 }
355
356 hash_version = *(unsigned char*)(data + 5);
Taylor Blaud9fef9d2022-05-20 19:17:41 -0400357 if (hash_version != oid_version(the_hash_algo)) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100358 error(_("commit-graph hash version %X does not match version %X"),
Taylor Blaud9fef9d2022-05-20 19:17:41 -0400359 hash_version, oid_version(the_hash_algo));
Josh Steadmonaa658572019-01-15 14:25:50 -0800360 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400361 }
362
363 graph = alloc_commit_graph();
364
brian m. carlsonc1665992018-11-14 04:09:35 +0000365 graph->hash_len = the_hash_algo->rawsz;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400366 graph->num_chunks = *(unsigned char*)(data + 6);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400367 graph->data = graph_map;
368 graph->data_len = graph_size;
369
SZEDER Gábor2ad4f1a2020-06-05 13:00:29 +0000370 if (graph_size < GRAPH_HEADER_SIZE +
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000371 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
SZEDER Gábor2ad4f1a2020-06-05 13:00:29 +0000372 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
373 error(_("commit-graph file is too small to hold %u chunks"),
374 graph->num_chunks);
375 free(graph);
376 return NULL;
377 }
378
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000379 cf = init_chunkfile(NULL);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400380
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000381 if (read_table_of_contents(cf, graph->data, graph_size,
382 GRAPH_HEADER_SIZE, graph->num_chunks))
383 goto free_and_return;
Josh Steadmond2b86fb2019-01-15 14:25:51 -0800384
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000385 pair_chunk(cf, GRAPH_CHUNKID_OIDFANOUT,
386 (const unsigned char **)&graph->chunk_oid_fanout);
387 read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph);
388 pair_chunk(cf, GRAPH_CHUNKID_DATA, &graph->chunk_commit_data);
389 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges);
390 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs);
Derrick Stolee702110a2021-02-25 18:19:43 +0000391
Taylor Blaua92d8522022-07-14 14:43:06 -0700392 if (s->commit_graph_generation_version >= 2) {
Derrick Stolee702110a2021-02-25 18:19:43 +0000393 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
394 &graph->chunk_generation_data);
395 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
396 &graph->chunk_generation_data_overflow);
Derrick Stolee3b0199d2022-03-01 19:48:31 +0000397
398 if (graph->chunk_generation_data)
399 graph->read_generation_data = 1;
Derrick Stolee702110a2021-02-25 18:19:43 +0000400 }
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400401
Taylor Blaua92d8522022-07-14 14:43:06 -0700402 if (s->commit_graph_read_changed_paths) {
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000403 pair_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
404 &graph->chunk_bloom_indexes);
405 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
406 graph_read_bloom_data, graph);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400407 }
408
Garima Singh76ffbca2020-04-06 16:59:49 +0000409 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
410 init_bloom_filters();
411 } else {
412 /* We need both the bloom chunks to exist together. Else ignore the data */
413 graph->chunk_bloom_indexes = NULL;
414 graph->chunk_bloom_data = NULL;
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700415 FREE_AND_NULL(graph->bloom_filter_settings);
Garima Singh76ffbca2020-04-06 16:59:49 +0000416 }
417
brian m. carlson92e2cab2021-04-26 01:02:50 +0000418 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
Derrick Stolee118bd572019-06-18 11:14:26 -0700419
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700420 if (verify_commit_graph_lite(graph))
421 goto free_and_return;
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +0100422
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000423 free_chunkfile(cf);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400424 return graph;
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700425
426free_and_return:
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000427 free_chunkfile(cf);
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700428 free(graph->bloom_filter_settings);
429 free(graph);
430 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400431}
432
Taylor Blauab14d062020-09-09 11:22:56 -0400433static struct commit_graph *load_commit_graph_one(struct repository *r,
434 const char *graph_file,
Taylor Blaua7df60c2020-02-03 13:18:04 -0800435 struct object_directory *odb)
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100436{
437
438 struct stat st;
439 int fd;
Derrick Stolee6c622f92019-06-18 11:14:27 -0700440 struct commit_graph *g;
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100441 int open_ok = open_commit_graph(graph_file, &fd, &st);
442
443 if (!open_ok)
444 return NULL;
445
Taylor Blauab14d062020-09-09 11:22:56 -0400446 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -0700447
448 if (g)
449 g->filename = xstrdup(graph_file);
450
451 return g;
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100452}
453
Taylor Blau13c24992020-02-03 13:18:00 -0800454static struct commit_graph *load_commit_graph_v1(struct repository *r,
455 struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700456{
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800457 char *graph_name = get_commit_graph_filename(odb);
Taylor Blauab14d062020-09-09 11:22:56 -0400458 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700459 free(graph_name);
460
461 return g;
462}
463
464static int add_graph_to_chain(struct commit_graph *g,
465 struct commit_graph *chain,
466 struct object_id *oids,
467 int n)
468{
469 struct commit_graph *cur_g = chain;
470
Derrick Stolee118bd572019-06-18 11:14:26 -0700471 if (n && !g->chunk_base_graphs) {
472 warning(_("commit-graph has no base graphs chunk"));
473 return 0;
474 }
475
Derrick Stolee5c84b332019-06-18 11:14:25 -0700476 while (n) {
477 n--;
Derrick Stolee118bd572019-06-18 11:14:26 -0700478
479 if (!cur_g ||
480 !oideq(&oids[n], &cur_g->oid) ||
481 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
482 warning(_("commit-graph chain does not match"));
483 return 0;
484 }
485
Derrick Stolee5c84b332019-06-18 11:14:25 -0700486 cur_g = cur_g->base_graph;
487 }
488
489 g->base_graph = chain;
490
491 if (chain)
492 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
493
494 return 1;
495}
496
Taylor Blau13c24992020-02-03 13:18:00 -0800497static struct commit_graph *load_commit_graph_chain(struct repository *r,
498 struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700499{
500 struct commit_graph *graph_chain = NULL;
501 struct strbuf line = STRBUF_INIT;
502 struct stat st;
503 struct object_id *oids;
504 int i = 0, valid = 1, count;
Derrick Stolee663b2b12020-09-17 18:11:46 +0000505 char *chain_name = get_commit_graph_chain_filename(odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700506 FILE *fp;
507 int stat_res;
508
509 fp = fopen(chain_name, "r");
510 stat_res = stat(chain_name, &st);
511 free(chain_name);
512
Kleber Tarcísioc0befa02022-04-18 17:13:27 +0000513 if (!fp)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700514 return NULL;
Kleber Tarcísioc0befa02022-04-18 17:13:27 +0000515 if (stat_res ||
516 st.st_size <= the_hash_algo->hexsz) {
517 fclose(fp);
518 return NULL;
519 }
Derrick Stolee5c84b332019-06-18 11:14:25 -0700520
521 count = st.st_size / (the_hash_algo->hexsz + 1);
René Scharfeca56dad2021-03-13 17:17:22 +0100522 CALLOC_ARRAY(oids, count);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700523
Derrick Stoleec5230352019-06-18 11:14:30 -0700524 prepare_alt_odb(r);
525
526 for (i = 0; i < count; i++) {
527 struct object_directory *odb;
Derrick Stolee5c84b332019-06-18 11:14:25 -0700528
529 if (strbuf_getline_lf(&line, fp) == EOF)
530 break;
531
532 if (get_oid_hex(line.buf, &oids[i])) {
533 warning(_("invalid commit-graph chain: line '%s' not a hash"),
534 line.buf);
535 valid = 0;
536 break;
537 }
538
Derrick Stoleec5230352019-06-18 11:14:30 -0700539 valid = 0;
540 for (odb = r->objects->odb; odb; odb = odb->next) {
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800541 char *graph_name = get_split_graph_filename(odb, line.buf);
Taylor Blauab14d062020-09-09 11:22:56 -0400542 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700543
Derrick Stoleec5230352019-06-18 11:14:30 -0700544 free(graph_name);
545
546 if (g) {
Derrick Stoleec5230352019-06-18 11:14:30 -0700547 if (add_graph_to_chain(g, graph_chain, oids, i)) {
548 graph_chain = g;
549 valid = 1;
550 }
551
552 break;
553 }
554 }
555
556 if (!valid) {
557 warning(_("unable to find all commit-graph files"));
558 break;
559 }
Derrick Stolee5c84b332019-06-18 11:14:25 -0700560 }
561
562 free(oids);
563 fclose(fp);
René Scharfe0aa6bce2019-08-07 13:15:02 +0200564 strbuf_release(&line);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700565
566 return graph_chain;
567}
568
Derrick Stolee448a39e2021-02-02 03:01:20 +0000569/*
570 * returns 1 if and only if all graphs in the chain have
571 * corrected commit dates stored in the generation_data chunk.
572 */
573static int validate_mixed_generation_chain(struct commit_graph *g)
Abhishek Kumar1fdc3832021-01-16 18:11:16 +0000574{
Derrick Stolee448a39e2021-02-02 03:01:20 +0000575 int read_generation_data = 1;
576 struct commit_graph *p = g;
Abhishek Kumar1fdc3832021-01-16 18:11:16 +0000577
Derrick Stolee448a39e2021-02-02 03:01:20 +0000578 while (read_generation_data && p) {
579 read_generation_data = p->read_generation_data;
580 p = p->base_graph;
581 }
Abhishek Kumar1fdc3832021-01-16 18:11:16 +0000582
Derrick Stolee448a39e2021-02-02 03:01:20 +0000583 if (read_generation_data)
584 return 1;
Abhishek Kumar1fdc3832021-01-16 18:11:16 +0000585
586 while (g) {
Derrick Stolee448a39e2021-02-02 03:01:20 +0000587 g->read_generation_data = 0;
Abhishek Kumar1fdc3832021-01-16 18:11:16 +0000588 g = g->base_graph;
589 }
Derrick Stolee448a39e2021-02-02 03:01:20 +0000590
591 return 0;
Abhishek Kumar1fdc3832021-01-16 18:11:16 +0000592}
593
Taylor Blau13c24992020-02-03 13:18:00 -0800594struct commit_graph *read_commit_graph_one(struct repository *r,
595 struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700596{
Taylor Blau13c24992020-02-03 13:18:00 -0800597 struct commit_graph *g = load_commit_graph_v1(r, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700598
599 if (!g)
Taylor Blau13c24992020-02-03 13:18:00 -0800600 g = load_commit_graph_chain(r, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700601
Abhishek Kumar1fdc3832021-01-16 18:11:16 +0000602 validate_mixed_generation_chain(g);
603
Derrick Stolee5c84b332019-06-18 11:14:25 -0700604 return g;
Jonathan Tan5faf3572018-07-11 15:42:37 -0700605}
606
Taylor Blau13c24992020-02-03 13:18:00 -0800607static void prepare_commit_graph_one(struct repository *r,
608 struct object_directory *odb)
Derrick Stolee177722b2018-04-10 08:56:05 -0400609{
Derrick Stolee177722b2018-04-10 08:56:05 -0400610
611 if (r->objects->commit_graph)
612 return;
613
Taylor Blau13c24992020-02-03 13:18:00 -0800614 r->objects->commit_graph = read_commit_graph_one(r, odb);
Derrick Stolee177722b2018-04-10 08:56:05 -0400615}
Jonathan Tan5faf3572018-07-11 15:42:37 -0700616
617/*
618 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
619 *
Elijah Newren15beaaa2019-11-05 17:07:23 +0000620 * On the first invocation, this function attempts to load the commit
Jonathan Tan5faf3572018-07-11 15:42:37 -0700621 * graph if the_repository is configured to have one.
622 */
Jonathan Tandade47c2018-07-11 15:42:42 -0700623static int prepare_commit_graph(struct repository *r)
Derrick Stolee177722b2018-04-10 08:56:05 -0400624{
Jeff King263db402018-11-12 09:48:47 -0500625 struct object_directory *odb;
Derrick Stolee177722b2018-04-10 08:56:05 -0400626
Jeff King6abada12019-09-12 10:44:45 -0400627 /*
Lessley Dennington0803f9c2021-12-06 15:55:56 +0000628 * Early return if there is no git dir or if the commit graph is
629 * disabled.
630 *
Jeff King6abada12019-09-12 10:44:45 -0400631 * This must come before the "already attempted?" check below, because
632 * we want to disable even an already-loaded graph file.
633 */
Lessley Dennington0803f9c2021-12-06 15:55:56 +0000634 if (!r->gitdir || r->commit_graph_disabled)
Jeff King6abada12019-09-12 10:44:45 -0400635 return 0;
Ævar Arnfjörð Bjarmason43d35612019-03-25 13:08:33 +0100636
Jonathan Tandade47c2018-07-11 15:42:42 -0700637 if (r->objects->commit_graph_attempted)
638 return !!r->objects->commit_graph;
639 r->objects->commit_graph_attempted = 1;
Derrick Stolee177722b2018-04-10 08:56:05 -0400640
Derrick Stolee7211b9e2019-08-13 11:37:43 -0700641 prepare_repo_settings(r);
642
Derrick Stolee859fdc02018-08-29 05:49:04 -0700643 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
Derrick Stolee7211b9e2019-08-13 11:37:43 -0700644 r->settings.core_commit_graph != 1)
Jonathan Tandade47c2018-07-11 15:42:42 -0700645 /*
646 * This repository is not configured to use commit graphs, so
647 * do not load one. (But report commit_graph_attempted anyway
648 * so that commit graph loading is not attempted again for this
649 * repository.)
650 */
Jonathan Tan5faf3572018-07-11 15:42:37 -0700651 return 0;
652
Derrick Stoleed6538242018-08-20 18:24:27 +0000653 if (!commit_graph_compatible(r))
654 return 0;
655
Jonathan Tandade47c2018-07-11 15:42:42 -0700656 prepare_alt_odb(r);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500657 for (odb = r->objects->odb;
Jeff King263db402018-11-12 09:48:47 -0500658 !r->objects->commit_graph && odb;
659 odb = odb->next)
Taylor Blau13c24992020-02-03 13:18:00 -0800660 prepare_commit_graph_one(r, odb);
Jonathan Tandade47c2018-07-11 15:42:42 -0700661 return !!r->objects->commit_graph;
Derrick Stolee177722b2018-04-10 08:56:05 -0400662}
663
Derrick Stolee6cc01742018-07-20 16:33:30 +0000664int generation_numbers_enabled(struct repository *r)
665{
666 uint32_t first_generation;
667 struct commit_graph *g;
668 if (!prepare_commit_graph(r))
669 return 0;
670
671 g = r->objects->commit_graph;
672
673 if (!g->num_commits)
674 return 0;
675
676 first_generation = get_be32(g->chunk_commit_data +
677 g->hash_len + 8) >> 2;
678
679 return !!first_generation;
680}
681
Abhishek Kumar8d00d7c2021-01-16 18:11:17 +0000682int corrected_commit_dates_enabled(struct repository *r)
683{
684 struct commit_graph *g;
685 if (!prepare_commit_graph(r))
686 return 0;
687
688 g = r->objects->commit_graph;
689
690 if (!g->num_commits)
691 return 0;
692
693 return g->read_generation_data;
694}
695
Taylor Blau4f364402020-09-09 11:22:44 -0400696struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
697{
698 struct commit_graph *g = r->objects->commit_graph;
699 while (g) {
700 if (g->bloom_filter_settings)
701 return g->bloom_filter_settings;
702 g = g->base_graph;
703 }
704 return NULL;
705}
706
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700707static void close_commit_graph_one(struct commit_graph *g)
708{
709 if (!g)
710 return;
711
Johannes Schindelin957ba812021-09-08 08:29:30 +0000712 clear_commit_graph_data_slab(&commit_graph_data_slab);
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700713 close_commit_graph_one(g->base_graph);
714 free_commit_graph(g);
715}
716
Derrick Stoleec3a3a962019-05-17 11:41:47 -0700717void close_commit_graph(struct raw_object_store *o)
Derrick Stolee177722b2018-04-10 08:56:05 -0400718{
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700719 close_commit_graph_one(o->commit_graph);
Derrick Stoleec3a3a962019-05-17 11:41:47 -0700720 o->commit_graph = NULL;
Derrick Stolee177722b2018-04-10 08:56:05 -0400721}
722
Patrick Steinhardt809ea282021-08-09 10:11:59 +0200723static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
Derrick Stolee177722b2018-04-10 08:56:05 -0400724{
725 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
726 g->chunk_oid_lookup, g->hash_len, pos);
727}
728
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700729static void load_oid_from_graph(struct commit_graph *g,
730 uint32_t pos,
731 struct object_id *oid)
732{
733 uint32_t lex_index;
734
735 while (g && pos < g->num_commits_in_base)
736 g = g->base_graph;
737
738 if (!g)
739 BUG("NULL commit-graph");
740
741 if (pos >= g->num_commits + g->num_commits_in_base)
742 die(_("invalid commit position. commit-graph is likely corrupt"));
743
744 lex_index = pos - g->num_commits_in_base;
745
brian m. carlson92e2cab2021-04-26 01:02:50 +0000746 oidread(oid, g->chunk_oid_lookup + g->hash_len * lex_index);
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700747}
748
Stefan Beller4f542b72018-12-14 16:09:39 -0800749static struct commit_list **insert_parent_or_die(struct repository *r,
750 struct commit_graph *g,
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700751 uint32_t pos,
Derrick Stolee177722b2018-04-10 08:56:05 -0400752 struct commit_list **pptr)
753{
754 struct commit *c;
755 struct object_id oid;
Derrick Stolee96af91d2018-06-27 09:24:36 -0400756
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700757 if (pos >= g->num_commits + g->num_commits_in_base)
758 die("invalid parent position %"PRIu32, pos);
Derrick Stolee53614b12018-06-27 09:24:38 -0400759
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700760 load_oid_from_graph(g, pos, &oid);
Stefan Beller4f542b72018-12-14 16:09:39 -0800761 c = lookup_commit(r, &oid);
Derrick Stolee177722b2018-04-10 08:56:05 -0400762 if (!c)
Nguyễn Thái Ngọc Duy4f5b5322018-07-21 09:49:26 +0200763 die(_("could not find commit %s"), oid_to_hex(&oid));
Abhishek Kumarc49c82a2020-06-17 14:44:10 +0530764 commit_graph_data_at(c)->graph_pos = pos;
Derrick Stolee177722b2018-04-10 08:56:05 -0400765 return &commit_list_insert(c, pptr)->next;
766}
767
Derrick Stoleee2838d82018-05-01 12:47:13 +0000768static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
769{
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700770 const unsigned char *commit_data;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530771 struct commit_graph_data *graph_data;
Abhishek Kumare8b63002021-01-16 18:11:15 +0000772 uint32_t lex_index, offset_pos;
773 uint64_t date_high, date_low, offset;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700774
775 while (pos < g->num_commits_in_base)
776 g = g->base_graph;
777
Abhishek Kumarf90fca62021-01-16 18:11:10 +0000778 if (pos >= g->num_commits + g->num_commits_in_base)
779 die(_("invalid commit position. commit-graph is likely corrupt"));
780
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700781 lex_index = pos - g->num_commits_in_base;
782 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530783
784 graph_data = commit_graph_data_at(item);
785 graph_data->graph_pos = pos;
Abhishek Kumarf90fca62021-01-16 18:11:10 +0000786
787 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
788 date_low = get_be32(commit_data + g->hash_len + 12);
789 item->date = (timestamp_t)((date_high << 32) | date_low);
790
Abhishek Kumar1fdc3832021-01-16 18:11:16 +0000791 if (g->read_generation_data) {
Abhishek Kumare8b63002021-01-16 18:11:15 +0000792 offset = (timestamp_t)get_be32(g->chunk_generation_data + sizeof(uint32_t) * lex_index);
793
794 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
795 if (!g->chunk_generation_data_overflow)
796 die(_("commit-graph requires overflow generation data but has none"));
797
798 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
Derrick Stoleec8d67b92022-03-01 19:48:32 +0000799 graph_data->generation = item->date + get_be64(g->chunk_generation_data_overflow + 8 * offset_pos);
Abhishek Kumare8b63002021-01-16 18:11:15 +0000800 } else
801 graph_data->generation = item->date + offset;
802 } else
803 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +0000804
805 if (g->topo_levels)
806 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
Derrick Stoleee2838d82018-05-01 12:47:13 +0000807}
808
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700809static inline void set_commit_tree(struct commit *c, struct tree *t)
810{
811 c->maybe_tree = t;
812}
813
Stefan Beller4f542b72018-12-14 16:09:39 -0800814static int fill_commit_in_graph(struct repository *r,
815 struct commit *item,
816 struct commit_graph *g, uint32_t pos)
Derrick Stolee177722b2018-04-10 08:56:05 -0400817{
Derrick Stolee177722b2018-04-10 08:56:05 -0400818 uint32_t edge_value;
819 uint32_t *parent_data_ptr;
Derrick Stolee177722b2018-04-10 08:56:05 -0400820 struct commit_list **pptr;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700821 const unsigned char *commit_data;
822 uint32_t lex_index;
823
824 while (pos < g->num_commits_in_base)
825 g = g->base_graph;
826
Abhishek Kumarf90fca62021-01-16 18:11:10 +0000827 fill_commit_graph_info(item, g, pos);
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700828
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700829 lex_index = pos - g->num_commits_in_base;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700830 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
Derrick Stolee177722b2018-04-10 08:56:05 -0400831
832 item->object.parsed = 1;
Derrick Stolee177722b2018-04-10 08:56:05 -0400833
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700834 set_commit_tree(item, NULL);
Derrick Stolee177722b2018-04-10 08:56:05 -0400835
Derrick Stolee177722b2018-04-10 08:56:05 -0400836 pptr = &item->parents;
837
838 edge_value = get_be32(commit_data + g->hash_len);
839 if (edge_value == GRAPH_PARENT_NONE)
840 return 1;
Stefan Beller4f542b72018-12-14 16:09:39 -0800841 pptr = insert_parent_or_die(r, g, edge_value, pptr);
Derrick Stolee177722b2018-04-10 08:56:05 -0400842
843 edge_value = get_be32(commit_data + g->hash_len + 4);
844 if (edge_value == GRAPH_PARENT_NONE)
845 return 1;
SZEDER Gábor5af74172019-01-19 21:21:13 +0100846 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
Stefan Beller4f542b72018-12-14 16:09:39 -0800847 pptr = insert_parent_or_die(r, g, edge_value, pptr);
Derrick Stolee177722b2018-04-10 08:56:05 -0400848 return 1;
849 }
850
SZEDER Gábor5af74172019-01-19 21:21:13 +0100851 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
Derrick Stolee177722b2018-04-10 08:56:05 -0400852 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
853 do {
854 edge_value = get_be32(parent_data_ptr);
Stefan Beller4f542b72018-12-14 16:09:39 -0800855 pptr = insert_parent_or_die(r, g,
Derrick Stolee177722b2018-04-10 08:56:05 -0400856 edge_value & GRAPH_EDGE_LAST_MASK,
857 pptr);
858 parent_data_ptr++;
859 } while (!(edge_value & GRAPH_LAST_EDGE));
860
861 return 1;
862}
863
Patrick Steinhardt809ea282021-08-09 10:11:59 +0200864static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
865{
866 struct commit_graph *cur_g = g;
867 uint32_t lex_index;
868
869 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
870 cur_g = cur_g->base_graph;
871
872 if (cur_g) {
873 *pos = lex_index + cur_g->num_commits_in_base;
874 return 1;
875 }
876
877 return 0;
878}
879
880static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
Derrick Stoleee2838d82018-05-01 12:47:13 +0000881{
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530882 uint32_t graph_pos = commit_graph_position(item);
883 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
884 *pos = graph_pos;
Derrick Stoleee2838d82018-05-01 12:47:13 +0000885 return 1;
886 } else {
Patrick Steinhardt809ea282021-08-09 10:11:59 +0200887 return search_commit_pos_in_graph(&item->object.oid, g, pos);
Derrick Stoleee2838d82018-05-01 12:47:13 +0000888 }
889}
890
Taylor Blau78053602022-07-12 19:10:31 -0400891int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
892 uint32_t *pos)
893{
894 if (!prepare_commit_graph(r))
895 return 0;
896 return find_commit_pos_in_graph(c, r->objects->commit_graph, pos);
897}
898
Patrick Steinhardtf559d6d2021-08-09 10:12:03 +0200899struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
900{
901 struct commit *commit;
902 uint32_t pos;
903
Jeff Kingd6045292022-09-06 17:02:13 -0400904 if (!prepare_commit_graph(repo))
Patrick Steinhardtf559d6d2021-08-09 10:12:03 +0200905 return NULL;
906 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
907 return NULL;
Han Xin3a1ea942022-07-01 09:34:30 +0800908 if (!has_object(repo, id, 0))
Patrick Steinhardtf559d6d2021-08-09 10:12:03 +0200909 return NULL;
910
911 commit = lookup_commit(repo, id);
912 if (!commit)
913 return NULL;
914 if (commit->object.parsed)
915 return commit;
916
917 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
918 return NULL;
919
920 return commit;
921}
922
Stefan Beller4f542b72018-12-14 16:09:39 -0800923static int parse_commit_in_graph_one(struct repository *r,
924 struct commit_graph *g,
925 struct commit *item)
Derrick Stolee177722b2018-04-10 08:56:05 -0400926{
Derrick Stoleee2838d82018-05-01 12:47:13 +0000927 uint32_t pos;
928
Derrick Stolee177722b2018-04-10 08:56:05 -0400929 if (item->object.parsed)
930 return 1;
Derrick Stoleeee797052018-06-27 09:24:29 -0400931
Patrick Steinhardt809ea282021-08-09 10:11:59 +0200932 if (find_commit_pos_in_graph(item, g, &pos))
Stefan Beller4f542b72018-12-14 16:09:39 -0800933 return fill_commit_in_graph(r, item, g, pos);
Derrick Stoleeee797052018-06-27 09:24:29 -0400934
935 return 0;
936}
937
Jonathan Tandade47c2018-07-11 15:42:42 -0700938int parse_commit_in_graph(struct repository *r, struct commit *item)
Derrick Stoleeee797052018-06-27 09:24:29 -0400939{
Derrick Stolee7b671f82020-06-23 17:47:01 +0000940 static int checked_env = 0;
941
942 if (!checked_env &&
943 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
944 die("dying as requested by the '%s' variable on commit-graph parse!",
945 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
946 checked_env = 1;
947
Jonathan Tandade47c2018-07-11 15:42:42 -0700948 if (!prepare_commit_graph(r))
Derrick Stoleeee797052018-06-27 09:24:29 -0400949 return 0;
Stefan Beller4f542b72018-12-14 16:09:39 -0800950 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
Derrick Stolee177722b2018-04-10 08:56:05 -0400951}
952
Jonathan Tandade47c2018-07-11 15:42:42 -0700953void load_commit_graph_info(struct repository *r, struct commit *item)
Derrick Stoleee2838d82018-05-01 12:47:13 +0000954{
955 uint32_t pos;
Taylor Blau78053602022-07-12 19:10:31 -0400956 if (repo_find_commit_pos_in_graph(r, item, &pos))
Jonathan Tandade47c2018-07-11 15:42:42 -0700957 fill_commit_graph_info(item, r->objects->commit_graph, pos);
Derrick Stoleee2838d82018-05-01 12:47:13 +0000958}
959
Stefan Beller4f542b72018-12-14 16:09:39 -0800960static struct tree *load_tree_for_commit(struct repository *r,
961 struct commit_graph *g,
962 struct commit *c)
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000963{
964 struct object_id oid;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700965 const unsigned char *commit_data;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530966 uint32_t graph_pos = commit_graph_position(c);
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700967
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530968 while (graph_pos < g->num_commits_in_base)
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700969 g = g->base_graph;
970
971 commit_data = g->chunk_commit_data +
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530972 GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base);
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000973
brian m. carlson92e2cab2021-04-26 01:02:50 +0000974 oidread(&oid, commit_data);
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700975 set_commit_tree(c, lookup_tree(r, &oid));
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000976
977 return c->maybe_tree;
978}
979
Stefan Beller4f542b72018-12-14 16:09:39 -0800980static struct tree *get_commit_tree_in_graph_one(struct repository *r,
981 struct commit_graph *g,
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400982 const struct commit *c)
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000983{
984 if (c->maybe_tree)
985 return c->maybe_tree;
Abhishek Kumarc49c82a2020-06-17 14:44:10 +0530986 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400987 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000988
Stefan Beller4f542b72018-12-14 16:09:39 -0800989 return load_tree_for_commit(r, g, (struct commit *)c);
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400990}
991
Jonathan Tandade47c2018-07-11 15:42:42 -0700992struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400993{
Stefan Beller4f542b72018-12-14 16:09:39 -0800994 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000995}
996
Derrick Stoleec9905be2019-06-12 06:29:40 -0700997struct packed_commit_list {
998 struct commit **list;
Jeff King33613902020-12-07 14:11:08 -0500999 size_t nr;
1000 size_t alloc;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001001};
1002
Derrick Stoleec9905be2019-06-12 06:29:40 -07001003struct write_commit_graph_context {
1004 struct repository *r;
Taylor Blau0bd52e22020-02-03 21:51:50 -08001005 struct object_directory *odb;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001006 char *graph_name;
Jeff Kinga5f1c442020-12-07 14:11:05 -05001007 struct oid_array oids;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001008 struct packed_commit_list commits;
1009 int num_extra_edges;
Abhishek Kumare8b63002021-01-16 18:11:15 +00001010 int num_generation_data_overflows;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001011 unsigned long approx_nr_objects;
1012 struct progress *progress;
1013 int progress_done;
1014 uint64_t progress_cnt;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001015
1016 char *base_graph_name;
1017 int num_commit_graphs_before;
1018 int num_commit_graphs_after;
1019 char **commit_graph_filenames_before;
1020 char **commit_graph_filenames_after;
1021 char **commit_graph_hash_after;
1022 uint32_t new_num_commits_in_base;
1023 struct commit_graph *new_base_graph;
1024
Derrick Stoleec9905be2019-06-12 06:29:40 -07001025 unsigned append:1,
Derrick Stolee6c622f92019-06-18 11:14:27 -07001026 report_progress:1,
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001027 split:1,
Garima Singh3d112752020-03-30 00:31:30 +00001028 changed_paths:1,
Abhishek Kumare8b63002021-01-16 18:11:15 +00001029 order_by_pack:1,
Derrick Stoleefde55b02021-02-02 03:01:22 +00001030 write_generation_data:1,
1031 trust_generation_numbers:1;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001032
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +00001033 struct topo_level_slab *topo_levels;
Taylor Blau98bb7962020-09-17 22:59:49 -04001034 const struct commit_graph_opts *opts;
Garima Singhf97b9322020-03-30 00:31:28 +00001035 size_t total_bloom_filter_data_size;
Derrick Stolee98037f22020-06-23 17:47:00 +00001036 const struct bloom_filter_settings *bloom_settings;
Taylor Blau312cff52020-09-16 14:07:32 -04001037
1038 int count_bloom_filter_computed;
1039 int count_bloom_filter_not_computed;
Taylor Blau59f0d502020-09-17 22:59:44 -04001040 int count_bloom_filter_trunc_empty;
Taylor Blau312cff52020-09-16 14:07:32 -04001041 int count_bloom_filter_trunc_large;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001042};
1043
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001044static int write_graph_chunk_fanout(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001045 void *data)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001046{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001047 struct write_commit_graph_context *ctx = data;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001048 int i, count = 0;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001049 struct commit **list = ctx->commits.list;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001050
1051 /*
1052 * Write the first-level table (the list is sorted,
1053 * but we use a 256-entry lookup to be able to avoid
1054 * having to do eight extra binary search iterations).
1055 */
1056 for (i = 0; i < 256; i++) {
Derrick Stoleec9905be2019-06-12 06:29:40 -07001057 while (count < ctx->commits.nr) {
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001058 if ((*list)->object.oid.hash[0] != i)
1059 break;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001060 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001061 count++;
1062 list++;
1063 }
1064
1065 hashwrite_be32(f, count);
1066 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001067
1068 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001069}
1070
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001071static int write_graph_chunk_oids(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001072 void *data)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001073{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001074 struct write_commit_graph_context *ctx = data;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001075 struct commit **list = ctx->commits.list;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001076 int count;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001077 for (count = 0; count < ctx->commits.nr; count++, list++) {
1078 display_progress(ctx->progress, ++ctx->progress_cnt);
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001079 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +01001080 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001081
1082 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001083}
1084
Jeff King8380dcd2021-01-28 01:20:23 -05001085static const struct object_id *commit_to_oid(size_t index, const void *table)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001086{
Jeff King8380dcd2021-01-28 01:20:23 -05001087 const struct commit * const *commits = table;
Jeff King45ee13b2021-01-28 01:19:42 -05001088 return &commits[index]->object.oid;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001089}
1090
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001091static int write_graph_chunk_data(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001092 void *data)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001093{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001094 struct write_commit_graph_context *ctx = data;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001095 struct commit **list = ctx->commits.list;
1096 struct commit **last = ctx->commits.list + ctx->commits.nr;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001097 uint32_t num_extra_edges = 0;
1098
1099 while (list < last) {
1100 struct commit_list *parent;
Taylor Blau806278d2019-09-05 18:04:57 -04001101 struct object_id *tree;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001102 int edge_value;
1103 uint32_t packedDate[2];
Derrick Stoleec9905be2019-06-12 06:29:40 -07001104 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001105
Derrick Stoleec4cc0832021-02-01 17:15:03 +00001106 if (repo_parse_commit_no_graph(ctx->r, *list))
Taylor Blau16749b82019-09-05 18:04:55 -04001107 die(_("unable to parse commit %s"),
1108 oid_to_hex(&(*list)->object.oid));
Taylor Blau806278d2019-09-05 18:04:57 -04001109 tree = get_commit_tree_oid(*list);
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001110 hashwrite(f, tree->hash, the_hash_algo->rawsz);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001111
1112 parent = (*list)->parents;
1113
1114 if (!parent)
1115 edge_value = GRAPH_PARENT_NONE;
1116 else {
Jeff King45ee13b2021-01-28 01:19:42 -05001117 edge_value = oid_pos(&parent->item->object.oid,
1118 ctx->commits.list,
1119 ctx->commits.nr,
1120 commit_to_oid);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001121
Derrick Stolee6c622f92019-06-18 11:14:27 -07001122 if (edge_value >= 0)
1123 edge_value += ctx->new_num_commits_in_base;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001124 else if (ctx->new_base_graph) {
Derrick Stolee6c622f92019-06-18 11:14:27 -07001125 uint32_t pos;
Patrick Steinhardt809ea282021-08-09 10:11:59 +02001126 if (find_commit_pos_in_graph(parent->item,
1127 ctx->new_base_graph,
1128 &pos))
Derrick Stolee6c622f92019-06-18 11:14:27 -07001129 edge_value = pos;
1130 }
1131
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001132 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -08001133 BUG("missing parent %s for commit %s",
1134 oid_to_hex(&parent->item->object.oid),
1135 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001136 }
1137
1138 hashwrite_be32(f, edge_value);
1139
1140 if (parent)
1141 parent = parent->next;
1142
1143 if (!parent)
1144 edge_value = GRAPH_PARENT_NONE;
1145 else if (parent->next)
SZEDER Gábor5af74172019-01-19 21:21:13 +01001146 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001147 else {
Jeff King45ee13b2021-01-28 01:19:42 -05001148 edge_value = oid_pos(&parent->item->object.oid,
1149 ctx->commits.list,
1150 ctx->commits.nr,
1151 commit_to_oid);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001152
1153 if (edge_value >= 0)
1154 edge_value += ctx->new_num_commits_in_base;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001155 else if (ctx->new_base_graph) {
Derrick Stolee6c622f92019-06-18 11:14:27 -07001156 uint32_t pos;
Patrick Steinhardt809ea282021-08-09 10:11:59 +02001157 if (find_commit_pos_in_graph(parent->item,
1158 ctx->new_base_graph,
1159 &pos))
Derrick Stolee6c622f92019-06-18 11:14:27 -07001160 edge_value = pos;
1161 }
1162
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001163 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -08001164 BUG("missing parent %s for commit %s",
1165 oid_to_hex(&parent->item->object.oid),
1166 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001167 }
1168
1169 hashwrite_be32(f, edge_value);
1170
SZEDER Gábor5af74172019-01-19 21:21:13 +01001171 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001172 do {
1173 num_extra_edges++;
1174 parent = parent->next;
1175 } while (parent);
1176 }
1177
1178 if (sizeof((*list)->date) > 4)
1179 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1180 else
1181 packedDate[0] = 0;
1182
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +00001183 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
Derrick Stolee3258c662018-05-01 12:47:09 +00001184
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001185 packedDate[1] = htonl((*list)->date);
1186 hashwrite(f, packedDate, 8);
1187
1188 list++;
1189 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001190
1191 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001192}
1193
Abhishek Kumare8b63002021-01-16 18:11:15 +00001194static int write_graph_chunk_generation_data(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001195 void *data)
Abhishek Kumare8b63002021-01-16 18:11:15 +00001196{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001197 struct write_commit_graph_context *ctx = data;
Abhishek Kumare8b63002021-01-16 18:11:15 +00001198 int i, num_generation_data_overflows = 0;
1199
1200 for (i = 0; i < ctx->commits.nr; i++) {
1201 struct commit *c = ctx->commits.list[i];
Derrick Stolee90cb1c42021-02-01 17:15:04 +00001202 timestamp_t offset;
1203 repo_parse_commit(ctx->r, c);
1204 offset = commit_graph_data_at(c)->generation - c->date;
Abhishek Kumare8b63002021-01-16 18:11:15 +00001205 display_progress(ctx->progress, ++ctx->progress_cnt);
1206
1207 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1208 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1209 num_generation_data_overflows++;
1210 }
1211
1212 hashwrite_be32(f, offset);
1213 }
1214
1215 return 0;
1216}
1217
1218static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001219 void *data)
Abhishek Kumare8b63002021-01-16 18:11:15 +00001220{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001221 struct write_commit_graph_context *ctx = data;
Abhishek Kumare8b63002021-01-16 18:11:15 +00001222 int i;
1223 for (i = 0; i < ctx->commits.nr; i++) {
1224 struct commit *c = ctx->commits.list[i];
1225 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1226 display_progress(ctx->progress, ++ctx->progress_cnt);
1227
1228 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1229 hashwrite_be32(f, offset >> 32);
1230 hashwrite_be32(f, (uint32_t) offset);
1231 }
1232 }
1233
1234 return 0;
1235}
1236
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001237static int write_graph_chunk_extra_edges(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001238 void *data)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001239{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001240 struct write_commit_graph_context *ctx = data;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001241 struct commit **list = ctx->commits.list;
1242 struct commit **last = ctx->commits.list + ctx->commits.nr;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001243 struct commit_list *parent;
1244
1245 while (list < last) {
1246 int num_parents = 0;
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +01001247
Derrick Stoleec9905be2019-06-12 06:29:40 -07001248 display_progress(ctx->progress, ++ctx->progress_cnt);
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +01001249
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001250 for (parent = (*list)->parents; num_parents < 3 && parent;
1251 parent = parent->next)
1252 num_parents++;
1253
1254 if (num_parents <= 2) {
1255 list++;
1256 continue;
1257 }
1258
1259 /* Since num_parents > 2, this initializer is safe. */
1260 for (parent = (*list)->parents->next; parent; parent = parent->next) {
Jeff King45ee13b2021-01-28 01:19:42 -05001261 int edge_value = oid_pos(&parent->item->object.oid,
1262 ctx->commits.list,
1263 ctx->commits.nr,
1264 commit_to_oid);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001265
Derrick Stolee6c622f92019-06-18 11:14:27 -07001266 if (edge_value >= 0)
1267 edge_value += ctx->new_num_commits_in_base;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001268 else if (ctx->new_base_graph) {
Derrick Stolee6c622f92019-06-18 11:14:27 -07001269 uint32_t pos;
Patrick Steinhardt809ea282021-08-09 10:11:59 +02001270 if (find_commit_pos_in_graph(parent->item,
1271 ctx->new_base_graph,
1272 &pos))
Derrick Stolee6c622f92019-06-18 11:14:27 -07001273 edge_value = pos;
1274 }
1275
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001276 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -08001277 BUG("missing parent %s for commit %s",
1278 oid_to_hex(&parent->item->object.oid),
1279 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001280 else if (!parent->next)
1281 edge_value |= GRAPH_LAST_EDGE;
1282
1283 hashwrite_be32(f, edge_value);
1284 }
1285
1286 list++;
1287 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001288
1289 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001290}
1291
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001292static int write_graph_chunk_bloom_indexes(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001293 void *data)
Garima Singh76ffbca2020-04-06 16:59:49 +00001294{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001295 struct write_commit_graph_context *ctx = data;
Garima Singh76ffbca2020-04-06 16:59:49 +00001296 struct commit **list = ctx->commits.list;
1297 struct commit **last = ctx->commits.list + ctx->commits.nr;
1298 uint32_t cur_pos = 0;
Garima Singh76ffbca2020-04-06 16:59:49 +00001299
1300 while (list < last) {
Taylor Blau312cff52020-09-16 14:07:32 -04001301 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
Derrick Stolee94919742020-07-01 13:27:23 +00001302 size_t len = filter ? filter->len : 0;
1303 cur_pos += len;
SZEDER Gábor150cd3b2020-07-09 19:00:03 +02001304 display_progress(ctx->progress, ++ctx->progress_cnt);
Garima Singh76ffbca2020-04-06 16:59:49 +00001305 hashwrite_be32(f, cur_pos);
1306 list++;
1307 }
1308
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001309 return 0;
Garima Singh76ffbca2020-04-06 16:59:49 +00001310}
1311
Derrick Stolee0087a872020-07-01 13:27:24 +00001312static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1313{
1314 struct json_writer jw = JSON_WRITER_INIT;
1315
1316 jw_object_begin(&jw, 0);
1317 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1318 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1319 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
Taylor Blau97ffa4f2020-09-17 09:34:42 -04001320 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
Derrick Stolee0087a872020-07-01 13:27:24 +00001321 jw_end(&jw);
1322
1323 trace2_data_json("bloom", ctx->r, "settings", &jw);
1324
1325 jw_release(&jw);
1326}
1327
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001328static int write_graph_chunk_bloom_data(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001329 void *data)
Garima Singh76ffbca2020-04-06 16:59:49 +00001330{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001331 struct write_commit_graph_context *ctx = data;
Garima Singh76ffbca2020-04-06 16:59:49 +00001332 struct commit **list = ctx->commits.list;
1333 struct commit **last = ctx->commits.list + ctx->commits.nr;
Garima Singh76ffbca2020-04-06 16:59:49 +00001334
Derrick Stolee0087a872020-07-01 13:27:24 +00001335 trace2_bloom_filter_settings(ctx);
1336
Derrick Stolee98037f22020-06-23 17:47:00 +00001337 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1338 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1339 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
Garima Singh76ffbca2020-04-06 16:59:49 +00001340
1341 while (list < last) {
Taylor Blau312cff52020-09-16 14:07:32 -04001342 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
Derrick Stolee94919742020-07-01 13:27:23 +00001343 size_t len = filter ? filter->len : 0;
Derrick Stolee94919742020-07-01 13:27:23 +00001344
SZEDER Gábor150cd3b2020-07-09 19:00:03 +02001345 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee94919742020-07-01 13:27:23 +00001346 if (len)
1347 hashwrite(f, filter->data, len * sizeof(unsigned char));
Garima Singh76ffbca2020-04-06 16:59:49 +00001348 list++;
1349 }
1350
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001351 return 0;
Garima Singh76ffbca2020-04-06 16:59:49 +00001352}
1353
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001354static int add_packed_commits(const struct object_id *oid,
1355 struct packed_git *pack,
1356 uint32_t pos,
1357 void *data)
1358{
Derrick Stoleec9905be2019-06-12 06:29:40 -07001359 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001360 enum object_type type;
1361 off_t offset = nth_packed_object_offset(pack, pos);
1362 struct object_info oi = OBJECT_INFO_INIT;
1363
Derrick Stoleec9905be2019-06-12 06:29:40 -07001364 if (ctx->progress)
1365 display_progress(ctx->progress, ++ctx->progress_done);
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +00001366
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001367 oi.typep = &type;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001368 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
Nguyễn Thái Ngọc Duy4f5b5322018-07-21 09:49:26 +02001369 die(_("unable to get type of object %s"), oid_to_hex(oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001370
1371 if (type != OBJ_COMMIT)
1372 return 0;
1373
Jeff Kinga5f1c442020-12-07 14:11:05 -05001374 oid_array_append(&ctx->oids, oid);
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001375 set_commit_pos(ctx->r, oid);
1376
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001377 return 0;
1378}
1379
Derrick Stoleec9905be2019-06-12 06:29:40 -07001380static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001381{
1382 struct commit_list *parent;
1383 for (parent = commit->parents; parent; parent = parent->next) {
Derrick Stoleecb99a342019-10-24 13:40:42 +00001384 if (!(parent->item->object.flags & REACHABLE)) {
Jeff Kinga5f1c442020-12-07 14:11:05 -05001385 oid_array_append(&ctx->oids, &parent->item->object.oid);
Derrick Stoleecb99a342019-10-24 13:40:42 +00001386 parent->item->object.flags |= REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001387 }
1388 }
1389}
1390
Derrick Stoleec9905be2019-06-12 06:29:40 -07001391static void close_reachable(struct write_commit_graph_context *ctx)
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001392{
Ævar Arnfjörð Bjarmason49bbc572019-01-19 21:21:21 +01001393 int i;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001394 struct commit *commit;
Taylor Blau98bb7962020-09-17 22:59:49 -04001395 enum commit_graph_split_flags flags = ctx->opts ?
1396 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001397
Derrick Stoleec9905be2019-06-12 06:29:40 -07001398 if (ctx->report_progress)
1399 ctx->progress = start_delayed_progress(
1400 _("Loading known commits in commit graph"),
1401 ctx->oids.nr);
1402 for (i = 0; i < ctx->oids.nr; i++) {
1403 display_progress(ctx->progress, i + 1);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001404 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001405 if (commit)
Derrick Stoleecb99a342019-10-24 13:40:42 +00001406 commit->object.flags |= REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001407 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001408 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001409
1410 /*
Derrick Stoleec9905be2019-06-12 06:29:40 -07001411 * As this loop runs, ctx->oids.nr may grow, but not more
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001412 * than the number of missing commits in the reachable
1413 * closure.
1414 */
Derrick Stoleec9905be2019-06-12 06:29:40 -07001415 if (ctx->report_progress)
1416 ctx->progress = start_delayed_progress(
1417 _("Expanding reachable commits in commit graph"),
SZEDER Gábor67fa6aa2019-09-07 01:01:33 -04001418 0);
Derrick Stoleec9905be2019-06-12 06:29:40 -07001419 for (i = 0; i < ctx->oids.nr; i++) {
1420 display_progress(ctx->progress, i + 1);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001421 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001422
Derrick Stolee6c622f92019-06-18 11:14:27 -07001423 if (!commit)
1424 continue;
1425 if (ctx->split) {
Derrick Stoleec4cc0832021-02-01 17:15:03 +00001426 if ((!repo_parse_commit(ctx->r, commit) &&
Abhishek Kumarc49c82a2020-06-17 14:44:10 +05301427 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
Taylor Blau8a6ac282020-04-13 22:04:17 -06001428 flags == COMMIT_GRAPH_SPLIT_REPLACE)
Derrick Stolee6c622f92019-06-18 11:14:27 -07001429 add_missing_parents(ctx, commit);
Derrick Stoleec4cc0832021-02-01 17:15:03 +00001430 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
Derrick Stoleec9905be2019-06-12 06:29:40 -07001431 add_missing_parents(ctx, commit);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001432 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001433 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001434
Derrick Stoleec9905be2019-06-12 06:29:40 -07001435 if (ctx->report_progress)
1436 ctx->progress = start_delayed_progress(
1437 _("Clearing commit marks in commit graph"),
1438 ctx->oids.nr);
1439 for (i = 0; i < ctx->oids.nr; i++) {
1440 display_progress(ctx->progress, i + 1);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001441 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001442
1443 if (commit)
Derrick Stoleecb99a342019-10-24 13:40:42 +00001444 commit->object.flags &= ~REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001445 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001446 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001447}
1448
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00001449static void compute_topological_levels(struct write_commit_graph_context *ctx)
1450{
1451 int i;
1452 struct commit_list *list = NULL;
1453
1454 if (ctx->report_progress)
1455 ctx->progress = start_delayed_progress(
1456 _("Computing commit graph topological levels"),
1457 ctx->commits.nr);
1458 for (i = 0; i < ctx->commits.nr; i++) {
1459 struct commit *c = ctx->commits.list[i];
1460 uint32_t level;
1461
1462 repo_parse_commit(ctx->r, c);
1463 level = *topo_level_slab_at(ctx->topo_levels, c);
1464
1465 display_progress(ctx->progress, i + 1);
1466 if (level != GENERATION_NUMBER_ZERO)
1467 continue;
1468
1469 commit_list_insert(c, &list);
1470 while (list) {
1471 struct commit *current = list->item;
1472 struct commit_list *parent;
1473 int all_parents_computed = 1;
1474 uint32_t max_level = 0;
1475
1476 for (parent = current->parents; parent; parent = parent->next) {
1477 repo_parse_commit(ctx->r, parent->item);
1478 level = *topo_level_slab_at(ctx->topo_levels, parent->item);
1479
1480 if (level == GENERATION_NUMBER_ZERO) {
1481 all_parents_computed = 0;
1482 commit_list_insert(parent->item, &list);
1483 break;
1484 }
1485
1486 if (level > max_level)
1487 max_level = level;
1488 }
1489
1490 if (all_parents_computed) {
1491 pop_commit(&list);
1492
1493 if (max_level > GENERATION_NUMBER_V1_MAX - 1)
1494 max_level = GENERATION_NUMBER_V1_MAX - 1;
1495 *topo_level_slab_at(ctx->topo_levels, current) = max_level + 1;
1496 }
1497 }
1498 }
1499 stop_progress(&ctx->progress);
1500}
1501
Derrick Stoleec9905be2019-06-12 06:29:40 -07001502static void compute_generation_numbers(struct write_commit_graph_context *ctx)
Derrick Stolee3258c662018-05-01 12:47:09 +00001503{
1504 int i;
1505 struct commit_list *list = NULL;
1506
Derrick Stoleec9905be2019-06-12 06:29:40 -07001507 if (ctx->report_progress)
Derrick Stoleeecc08692019-11-25 21:28:23 +00001508 ctx->progress = start_delayed_progress(
Derrick Stoleec9905be2019-06-12 06:29:40 -07001509 _("Computing commit graph generation numbers"),
1510 ctx->commits.nr);
Derrick Stoleefde55b02021-02-02 03:01:22 +00001511
1512 if (!ctx->trust_generation_numbers) {
1513 for (i = 0; i < ctx->commits.nr; i++) {
1514 struct commit *c = ctx->commits.list[i];
1515 repo_parse_commit(ctx->r, c);
1516 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1517 }
1518 }
1519
Derrick Stoleec9905be2019-06-12 06:29:40 -07001520 for (i = 0; i < ctx->commits.nr; i++) {
Derrick Stolee90cb1c42021-02-01 17:15:04 +00001521 struct commit *c = ctx->commits.list[i];
Derrick Stolee90cb1c42021-02-01 17:15:04 +00001522 timestamp_t corrected_commit_date;
1523
1524 repo_parse_commit(ctx->r, c);
Derrick Stolee90cb1c42021-02-01 17:15:04 +00001525 corrected_commit_date = commit_graph_data_at(c)->generation;
Abhishek Kumar48448122020-06-17 14:44:09 +05301526
Derrick Stoleec9905be2019-06-12 06:29:40 -07001527 display_progress(ctx->progress, i + 1);
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00001528 if (corrected_commit_date != GENERATION_NUMBER_ZERO)
Derrick Stolee3258c662018-05-01 12:47:09 +00001529 continue;
1530
Derrick Stolee90cb1c42021-02-01 17:15:04 +00001531 commit_list_insert(c, &list);
Derrick Stolee3258c662018-05-01 12:47:09 +00001532 while (list) {
1533 struct commit *current = list->item;
1534 struct commit_list *parent;
1535 int all_parents_computed = 1;
Abhishek Kumarc1a09112021-01-16 18:11:14 +00001536 timestamp_t max_corrected_commit_date = 0;
Derrick Stolee3258c662018-05-01 12:47:09 +00001537
1538 for (parent = current->parents; parent; parent = parent->next) {
Derrick Stolee90cb1c42021-02-01 17:15:04 +00001539 repo_parse_commit(ctx->r, parent->item);
Abhishek Kumarc1a09112021-01-16 18:11:14 +00001540 corrected_commit_date = commit_graph_data_at(parent->item)->generation;
Abhishek Kumar48448122020-06-17 14:44:09 +05301541
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00001542 if (corrected_commit_date == GENERATION_NUMBER_ZERO) {
Derrick Stolee3258c662018-05-01 12:47:09 +00001543 all_parents_computed = 0;
1544 commit_list_insert(parent->item, &list);
1545 break;
Derrick Stolee3258c662018-05-01 12:47:09 +00001546 }
Abhishek Kumarc1a09112021-01-16 18:11:14 +00001547
Abhishek Kumarc1a09112021-01-16 18:11:14 +00001548 if (corrected_commit_date > max_corrected_commit_date)
1549 max_corrected_commit_date = corrected_commit_date;
Derrick Stolee3258c662018-05-01 12:47:09 +00001550 }
1551
1552 if (all_parents_computed) {
Derrick Stolee3258c662018-05-01 12:47:09 +00001553 pop_commit(&list);
1554
Abhishek Kumarc1a09112021-01-16 18:11:14 +00001555 if (current->date && current->date > max_corrected_commit_date)
1556 max_corrected_commit_date = current->date - 1;
1557 commit_graph_data_at(current)->generation = max_corrected_commit_date + 1;
Derrick Stolee3258c662018-05-01 12:47:09 +00001558 }
1559 }
1560 }
Derrick Stolee75979d92022-03-01 19:48:30 +00001561
1562 for (i = 0; i < ctx->commits.nr; i++) {
1563 struct commit *c = ctx->commits.list[i];
1564 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1565 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1566 ctx->num_generation_data_overflows++;
1567 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001568 stop_progress(&ctx->progress);
Derrick Stolee3258c662018-05-01 12:47:09 +00001569}
1570
Taylor Blau312cff52020-09-16 14:07:32 -04001571static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1572{
1573 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1574 ctx->count_bloom_filter_computed);
1575 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1576 ctx->count_bloom_filter_not_computed);
Taylor Blau59f0d502020-09-17 22:59:44 -04001577 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1578 ctx->count_bloom_filter_trunc_empty);
Taylor Blau312cff52020-09-16 14:07:32 -04001579 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1580 ctx->count_bloom_filter_trunc_large);
1581}
1582
Garima Singhf97b9322020-03-30 00:31:28 +00001583static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1584{
1585 int i;
1586 struct progress *progress = NULL;
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001587 struct commit **sorted_commits;
Taylor Blau809e0322020-09-18 09:27:27 -04001588 int max_new_filters;
Garima Singhf97b9322020-03-30 00:31:28 +00001589
1590 init_bloom_filters();
1591
1592 if (ctx->report_progress)
1593 progress = start_delayed_progress(
1594 _("Computing commit changed paths Bloom filters"),
1595 ctx->commits.nr);
1596
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001597 ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1598 COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
Garima Singh3d112752020-03-30 00:31:30 +00001599
1600 if (ctx->order_by_pack)
1601 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1602 else
1603 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001604
Taylor Blau809e0322020-09-18 09:27:27 -04001605 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1606 ctx->opts->max_new_filters : ctx->commits.nr;
1607
Garima Singhf97b9322020-03-30 00:31:28 +00001608 for (i = 0; i < ctx->commits.nr; i++) {
Taylor Blau312cff52020-09-16 14:07:32 -04001609 enum bloom_filter_computed computed = 0;
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001610 struct commit *c = sorted_commits[i];
Taylor Blau312cff52020-09-16 14:07:32 -04001611 struct bloom_filter *filter = get_or_compute_bloom_filter(
1612 ctx->r,
1613 c,
Taylor Blau809e0322020-09-18 09:27:27 -04001614 ctx->count_bloom_filter_computed < max_new_filters,
Taylor Blau9a7a9ed2020-09-16 14:07:46 -04001615 ctx->bloom_settings,
Taylor Blau312cff52020-09-16 14:07:32 -04001616 &computed);
1617 if (computed & BLOOM_COMPUTED) {
1618 ctx->count_bloom_filter_computed++;
Taylor Blau59f0d502020-09-17 22:59:44 -04001619 if (computed & BLOOM_TRUNC_EMPTY)
1620 ctx->count_bloom_filter_trunc_empty++;
Taylor Blau312cff52020-09-16 14:07:32 -04001621 if (computed & BLOOM_TRUNC_LARGE)
1622 ctx->count_bloom_filter_trunc_large++;
1623 } else if (computed & BLOOM_NOT_COMPUTED)
1624 ctx->count_bloom_filter_not_computed++;
Taylor Blau809e0322020-09-18 09:27:27 -04001625 ctx->total_bloom_filter_data_size += filter
1626 ? sizeof(unsigned char) * filter->len : 0;
Garima Singhf97b9322020-03-30 00:31:28 +00001627 display_progress(progress, i + 1);
1628 }
1629
Taylor Blau312cff52020-09-16 14:07:32 -04001630 if (trace2_is_enabled())
1631 trace2_bloom_filter_write_statistics(ctx);
1632
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001633 free(sorted_commits);
Garima Singhf97b9322020-03-30 00:31:28 +00001634 stop_progress(&progress);
1635}
1636
Taylor Blau1fe10842020-05-04 19:13:35 -06001637struct refs_cb_data {
1638 struct oidset *commits;
Taylor Blaud335ce82020-05-13 15:59:33 -06001639 struct progress *progress;
Taylor Blau1fe10842020-05-04 19:13:35 -06001640};
1641
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +02001642static int add_ref_to_set(const char *refname UNUSED,
Taylor Blau6830c362020-04-13 22:04:25 -06001643 const struct object_id *oid,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +02001644 int flags UNUSED, void *cb_data)
Derrick Stolee59fb8772018-06-27 09:24:45 -04001645{
Taylor Blau630cd512020-05-13 15:59:37 -06001646 struct object_id peeled;
Taylor Blau1fe10842020-05-04 19:13:35 -06001647 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001648
Jeff King36a31792021-01-20 14:44:43 -05001649 if (!peel_iterated_oid(oid, &peeled))
Taylor Blau630cd512020-05-13 15:59:37 -06001650 oid = &peeled;
1651 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1652 oidset_insert(data->commits, oid);
Taylor Blaud335ce82020-05-13 15:59:33 -06001653
1654 display_progress(data->progress, oidset_size(data->commits));
1655
Derrick Stolee59fb8772018-06-27 09:24:45 -04001656 return 0;
1657}
1658
Taylor Blau0bd52e22020-02-03 21:51:50 -08001659int write_commit_graph_reachable(struct object_directory *odb,
SZEDER Gábor39d88312019-08-05 10:02:39 +02001660 enum commit_graph_write_flags flags,
Taylor Blau98bb7962020-09-17 22:59:49 -04001661 const struct commit_graph_opts *opts)
Derrick Stolee59fb8772018-06-27 09:24:45 -04001662{
Taylor Blau6830c362020-04-13 22:04:25 -06001663 struct oidset commits = OIDSET_INIT;
Taylor Blau1fe10842020-05-04 19:13:35 -06001664 struct refs_cb_data data;
Derrick Stoleee103f722019-06-12 06:29:37 -07001665 int result;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001666
Taylor Blau1fe10842020-05-04 19:13:35 -06001667 memset(&data, 0, sizeof(data));
1668 data.commits = &commits;
Taylor Blaud335ce82020-05-13 15:59:33 -06001669 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1670 data.progress = start_delayed_progress(
1671 _("Collecting referenced commits"), 0);
Taylor Blau1fe10842020-05-04 19:13:35 -06001672
1673 for_each_ref(add_ref_to_set, &data);
SZEDER Gábor6f9d5f22020-07-09 18:54:32 +02001674
1675 stop_progress(&data.progress);
1676
Taylor Blau6830c362020-04-13 22:04:25 -06001677 result = write_commit_graph(odb, NULL, &commits,
Taylor Blau98bb7962020-09-17 22:59:49 -04001678 flags, opts);
Derrick Stoleef4dbdfc2018-10-03 10:12:15 -07001679
Taylor Blau6830c362020-04-13 22:04:25 -06001680 oidset_clear(&commits);
Derrick Stoleee103f722019-06-12 06:29:37 -07001681 return result;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001682}
1683
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001684static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
Ævar Arnfjörð Bjarmason4a047902022-03-04 19:32:12 +01001685 const struct string_list *pack_indexes)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001686{
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001687 uint32_t i;
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001688 struct strbuf progress_title = STRBUF_INIT;
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001689 struct strbuf packname = STRBUF_INIT;
1690 int dirlen;
Ævar Arnfjörð Bjarmason51a94d82022-03-04 19:32:13 +01001691 int ret = 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001692
Taylor Blau0bd52e22020-02-03 21:51:50 -08001693 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001694 dirlen = packname.len;
1695 if (ctx->report_progress) {
1696 strbuf_addf(&progress_title,
Ævar Arnfjörð Bjarmason99d60542022-03-07 16:27:08 +01001697 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1698 "Finding commits for commit graph in %"PRIuMAX" packs",
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001699 pack_indexes->nr),
Ævar Arnfjörð Bjarmason99d60542022-03-07 16:27:08 +01001700 (uintmax_t)pack_indexes->nr);
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001701 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1702 ctx->progress_done = 0;
Derrick Stolee7547b952018-04-10 08:56:08 -04001703 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001704 for (i = 0; i < pack_indexes->nr; i++) {
1705 struct packed_git *p;
1706 strbuf_setlen(&packname, dirlen);
1707 strbuf_addstr(&packname, pack_indexes->items[i].string);
1708 p = add_packed_git(packname.buf, packname.len, 1);
1709 if (!p) {
Ævar Arnfjörð Bjarmason51a94d82022-03-04 19:32:13 +01001710 ret = error(_("error adding pack %s"), packname.buf);
1711 goto cleanup;
Derrick Stolee7547b952018-04-10 08:56:08 -04001712 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001713 if (open_pack_index(p)) {
Ævar Arnfjörð Bjarmason51a94d82022-03-04 19:32:13 +01001714 ret = error(_("error opening index for %s"), packname.buf);
1715 goto cleanup;
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +00001716 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001717 for_each_object_in_pack(p, add_packed_commits, ctx,
1718 FOR_EACH_OBJECT_PACK_ORDER);
1719 close_pack(p);
1720 free(p);
Derrick Stolee3d5df012018-04-10 08:56:07 -04001721 }
1722
Ævar Arnfjörð Bjarmason51a94d82022-03-04 19:32:13 +01001723cleanup:
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001724 stop_progress(&ctx->progress);
René Scharfe0aa6bce2019-08-07 13:15:02 +02001725 strbuf_release(&progress_title);
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001726 strbuf_release(&packname);
Derrick Stolee3d5df012018-04-10 08:56:07 -04001727
Ævar Arnfjörð Bjarmason51a94d82022-03-04 19:32:13 +01001728 return ret;
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001729}
Derrick Stolee3d5df012018-04-10 08:56:07 -04001730
Taylor Blau6830c362020-04-13 22:04:25 -06001731static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1732 struct oidset *commits)
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001733{
Taylor Blau6830c362020-04-13 22:04:25 -06001734 struct oidset_iter iter;
1735 struct object_id *oid;
1736
1737 if (!oidset_size(commits))
1738 return 0;
Derrick Stolee3d5df012018-04-10 08:56:07 -04001739
Taylor Blau6830c362020-04-13 22:04:25 -06001740 oidset_iter_init(commits, &iter);
1741 while ((oid = oidset_iter_next(&iter))) {
Jeff Kinga5f1c442020-12-07 14:11:05 -05001742 oid_array_append(&ctx->oids, oid);
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001743 }
Taylor Blau6830c362020-04-13 22:04:25 -06001744
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001745 return 0;
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001746}
1747
Derrick Stoleeb2c83062019-06-12 06:29:42 -07001748static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1749{
1750 if (ctx->report_progress)
1751 ctx->progress = start_delayed_progress(
1752 _("Finding commits for commit graph among packed objects"),
1753 ctx->approx_nr_objects);
1754 for_each_packed_object(add_packed_commits, ctx,
1755 FOR_EACH_OBJECT_PACK_ORDER);
1756 if (ctx->progress_done < ctx->approx_nr_objects)
1757 display_progress(ctx->progress, ctx->approx_nr_objects);
1758 stop_progress(&ctx->progress);
1759}
1760
Derrick Stoleef998d542019-06-12 06:29:44 -07001761static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1762{
1763 uint32_t i;
Taylor Blau98bb7962020-09-17 22:59:49 -04001764 enum commit_graph_split_flags flags = ctx->opts ?
1765 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
Derrick Stoleef998d542019-06-12 06:29:44 -07001766
1767 ctx->num_extra_edges = 0;
1768 if (ctx->report_progress)
1769 ctx->progress = start_delayed_progress(
1770 _("Finding extra edges in commit graph"),
1771 ctx->oids.nr);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001772 oid_array_sort(&ctx->oids);
1773 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
René Scharfe689a1462019-09-15 19:07:44 +02001774 unsigned int num_parents;
1775
Derrick Stoleef998d542019-06-12 06:29:44 -07001776 display_progress(ctx->progress, i + 1);
Derrick Stoleef998d542019-06-12 06:29:44 -07001777
Derrick Stolee6c622f92019-06-18 11:14:27 -07001778 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001779 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001780
Taylor Blau8a6ac282020-04-13 22:04:17 -06001781 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
Abhishek Kumarc49c82a2020-06-17 14:44:10 +05301782 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
Derrick Stolee6c622f92019-06-18 11:14:27 -07001783 continue;
1784
Taylor Blau8a6ac282020-04-13 22:04:17 -06001785 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
Derrick Stoleec4cc0832021-02-01 17:15:03 +00001786 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
Taylor Blau8a6ac282020-04-13 22:04:17 -06001787 else
Derrick Stoleec4cc0832021-02-01 17:15:03 +00001788 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
Derrick Stoleef998d542019-06-12 06:29:44 -07001789
René Scharfe689a1462019-09-15 19:07:44 +02001790 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001791 if (num_parents > 2)
Derrick Stoleef998d542019-06-12 06:29:44 -07001792 ctx->num_extra_edges += num_parents - 1;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001793
Derrick Stoleef998d542019-06-12 06:29:44 -07001794 ctx->commits.nr++;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001795 }
Derrick Stoleef998d542019-06-12 06:29:44 -07001796 stop_progress(&ctx->progress);
1797}
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001798
Derrick Stolee6c622f92019-06-18 11:14:27 -07001799static int write_graph_chunk_base_1(struct hashfile *f,
1800 struct commit_graph *g)
1801{
1802 int num = 0;
1803
1804 if (!g)
1805 return 0;
1806
1807 num = write_graph_chunk_base_1(f, g->base_graph);
1808 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1809 return num + 1;
1810}
1811
1812static int write_graph_chunk_base(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001813 void *data)
Derrick Stolee6c622f92019-06-18 11:14:27 -07001814{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001815 struct write_commit_graph_context *ctx = data;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001816 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1817
1818 if (num != ctx->num_commit_graphs_after - 1) {
1819 error(_("failed to write correct number of base graph ids"));
1820 return -1;
1821 }
1822
1823 return 0;
1824}
1825
Derrick Stolee238def52019-06-12 06:29:45 -07001826static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1827{
1828 uint32_t i;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001829 int fd;
Derrick Stolee238def52019-06-12 06:29:45 -07001830 struct hashfile *f;
1831 struct lock_file lk = LOCK_INIT;
Derrick Stolee238def52019-06-12 06:29:45 -07001832 const unsigned hashsz = the_hash_algo->rawsz;
1833 struct strbuf progress_title = STRBUF_INIT;
Derrick Stolee47410aa2021-02-18 14:07:25 +00001834 struct chunkfile *cf;
brian m. carlson72871b132021-04-26 01:02:58 +00001835 unsigned char file_hash[GIT_MAX_RAWSZ];
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001836
Derrick Stolee6c622f92019-06-18 11:14:27 -07001837 if (ctx->split) {
1838 struct strbuf tmp_file = STRBUF_INIT;
1839
1840 strbuf_addf(&tmp_file,
1841 "%s/info/commit-graphs/tmp_graph_XXXXXX",
Taylor Blau0bd52e22020-02-03 21:51:50 -08001842 ctx->odb->path);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001843 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1844 } else {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001845 ctx->graph_name = get_commit_graph_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001846 }
1847
Derrick Stolee238def52019-06-12 06:29:45 -07001848 if (safe_create_leading_directories(ctx->graph_name)) {
1849 UNLEAK(ctx->graph_name);
1850 error(_("unable to create leading directories of %s"),
1851 ctx->graph_name);
1852 return -1;
Derrick Stoleef4dbdfc2018-10-03 10:12:15 -07001853 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001854
Derrick Stolee6c622f92019-06-18 11:14:27 -07001855 if (ctx->split) {
Derrick Stolee663b2b12020-09-17 18:11:46 +00001856 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001857
Taylor Blau45a43652020-04-29 11:36:46 -06001858 hold_lock_file_for_update_mode(&lk, lock_name,
1859 LOCK_DIE_ON_ERROR, 0444);
Ævar Arnfjörð Bjarmasonef3fe212022-03-04 19:32:14 +01001860 free(lock_name);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001861
1862 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1863 if (fd < 0) {
Taylor Blaua2d57e22020-04-23 15:41:02 -06001864 error(_("unable to create temporary graph layer"));
Derrick Stolee6c622f92019-06-18 11:14:27 -07001865 return -1;
1866 }
1867
Taylor Blauf4d62842020-04-29 11:36:42 -06001868 if (adjust_shared_perm(ctx->graph_name)) {
1869 error(_("unable to adjust shared permissions for '%s'"),
1870 ctx->graph_name);
1871 return -1;
1872 }
1873
Derrick Stolee6c622f92019-06-18 11:14:27 -07001874 f = hashfd(fd, ctx->graph_name);
1875 } else {
Taylor Blau1f9beca2020-04-29 11:36:38 -06001876 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1877 LOCK_DIE_ON_ERROR, 0444);
Martin Ågrena52cdce2021-01-05 20:23:47 +01001878 fd = get_lock_file_fd(&lk);
1879 f = hashfd(fd, get_lock_file_path(&lk));
Derrick Stolee6c622f92019-06-18 11:14:27 -07001880 }
Derrick Stolee238def52019-06-12 06:29:45 -07001881
Derrick Stolee47410aa2021-02-18 14:07:25 +00001882 cf = init_chunkfile(f);
1883
1884 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
1885 write_graph_chunk_fanout);
1886 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, hashsz * ctx->commits.nr,
1887 write_graph_chunk_oids);
1888 add_chunk(cf, GRAPH_CHUNKID_DATA, (hashsz + 16) * ctx->commits.nr,
1889 write_graph_chunk_data);
Abhishek Kumare8b63002021-01-16 18:11:15 +00001890
Derrick Stolee47410aa2021-02-18 14:07:25 +00001891 if (ctx->write_generation_data)
1892 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
1893 sizeof(uint32_t) * ctx->commits.nr,
1894 write_graph_chunk_generation_data);
1895 if (ctx->num_generation_data_overflows)
1896 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
1897 sizeof(timestamp_t) * ctx->num_generation_data_overflows,
1898 write_graph_chunk_generation_data_overflow);
1899 if (ctx->num_extra_edges)
1900 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
1901 4 * ctx->num_extra_edges,
1902 write_graph_chunk_extra_edges);
Garima Singh76ffbca2020-04-06 16:59:49 +00001903 if (ctx->changed_paths) {
Derrick Stolee47410aa2021-02-18 14:07:25 +00001904 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
1905 sizeof(uint32_t) * ctx->commits.nr,
1906 write_graph_chunk_bloom_indexes);
1907 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
1908 sizeof(uint32_t) * 3
1909 + ctx->total_bloom_filter_data_size,
1910 write_graph_chunk_bloom_data);
Garima Singh76ffbca2020-04-06 16:59:49 +00001911 }
Derrick Stolee47410aa2021-02-18 14:07:25 +00001912 if (ctx->num_commit_graphs_after > 1)
1913 add_chunk(cf, GRAPH_CHUNKID_BASE,
1914 hashsz * (ctx->num_commit_graphs_after - 1),
1915 write_graph_chunk_base);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001916
1917 hashwrite_be32(f, GRAPH_SIGNATURE);
1918
1919 hashwrite_u8(f, GRAPH_VERSION);
Taylor Blaud9fef9d2022-05-20 19:17:41 -04001920 hashwrite_u8(f, oid_version(the_hash_algo));
Derrick Stolee47410aa2021-02-18 14:07:25 +00001921 hashwrite_u8(f, get_num_chunks(cf));
Derrick Stolee6c622f92019-06-18 11:14:27 -07001922 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001923
Derrick Stolee238def52019-06-12 06:29:45 -07001924 if (ctx->report_progress) {
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001925 strbuf_addf(&progress_title,
1926 Q_("Writing out commit graph in %d pass",
1927 "Writing out commit graph in %d passes",
Taylor Blauc4ff24b2021-02-24 12:12:22 -05001928 get_num_chunks(cf)),
1929 get_num_chunks(cf));
Derrick Stolee238def52019-06-12 06:29:45 -07001930 ctx->progress = start_delayed_progress(
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001931 progress_title.buf,
Taylor Blauc4ff24b2021-02-24 12:12:22 -05001932 get_num_chunks(cf) * ctx->commits.nr);
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001933 }
SZEDER Gábor17e62752020-07-01 13:27:26 +00001934
Derrick Stolee47410aa2021-02-18 14:07:25 +00001935 write_chunkfile(cf, ctx);
SZEDER Gábor17e62752020-07-01 13:27:26 +00001936
Derrick Stolee238def52019-06-12 06:29:45 -07001937 stop_progress(&ctx->progress);
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001938 strbuf_release(&progress_title);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001939
Derrick Stolee6c622f92019-06-18 11:14:27 -07001940 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1941 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001942 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001943
1944 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1945 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1946 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1947 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1948 }
1949
Derrick Stoleec3a3a962019-05-17 11:41:47 -07001950 close_commit_graph(ctx->r->objects);
Neeraj Singh020406e2022-03-10 22:43:21 +00001951 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
1952 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
Derrick Stolee47410aa2021-02-18 14:07:25 +00001953 free_chunkfile(cf);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001954
1955 if (ctx->split) {
1956 FILE *chainf = fdopen_lock_file(&lk, "w");
1957 char *final_graph_name;
1958 int result;
1959
1960 close(fd);
1961
1962 if (!chainf) {
1963 error(_("unable to open commit-graph chain file"));
1964 return -1;
1965 }
1966
1967 if (ctx->base_graph_name) {
Taylor Blau8a6ac282020-04-13 22:04:17 -06001968 const char *dest;
1969 int idx = ctx->num_commit_graphs_after - 1;
1970 if (ctx->num_commit_graphs_after > 1)
1971 idx--;
1972
1973 dest = ctx->commit_graph_filenames_after[idx];
Derrick Stolee6c622f92019-06-18 11:14:27 -07001974
Derrick Stolee135a7122019-06-18 11:14:28 -07001975 if (strcmp(ctx->base_graph_name, dest)) {
1976 result = rename(ctx->base_graph_name, dest);
1977
1978 if (result) {
1979 error(_("failed to rename base commit-graph file"));
1980 return -1;
1981 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07001982 }
1983 } else {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001984 char *graph_name = get_commit_graph_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001985 unlink(graph_name);
Ævar Arnfjörð Bjarmasonef3fe212022-03-04 19:32:14 +01001986 free(graph_name);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001987 }
1988
brian m. carlson72871b132021-04-26 01:02:58 +00001989 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001990 final_graph_name = get_split_graph_filename(ctx->odb,
Derrick Stolee6c622f92019-06-18 11:14:27 -07001991 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1992 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1993
1994 result = rename(ctx->graph_name, final_graph_name);
1995
1996 for (i = 0; i < ctx->num_commit_graphs_after; i++)
Martin Ågrena52cdce2021-01-05 20:23:47 +01001997 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001998
1999 if (result) {
2000 error(_("failed to rename temporary commit-graph file"));
2001 return -1;
2002 }
2003 }
2004
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002005 commit_lock_file(&lk);
2006
Derrick Stolee238def52019-06-12 06:29:45 -07002007 return 0;
2008}
2009
Derrick Stolee1771be92019-06-18 11:14:29 -07002010static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2011{
Alex Henrie8da02ce2019-09-30 20:29:34 -06002012 struct commit_graph *g;
2013 uint32_t num_commits;
Taylor Blaufdbde822020-04-13 22:04:12 -06002014 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
Derrick Stolee1771be92019-06-18 11:14:29 -07002015 uint32_t i;
2016
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07002017 int max_commits = 0;
2018 int size_mult = 2;
2019
Taylor Blau98bb7962020-09-17 22:59:49 -04002020 if (ctx->opts) {
2021 max_commits = ctx->opts->max_commits;
Derrick Stolee63020f12020-01-02 16:14:14 +00002022
Taylor Blau98bb7962020-09-17 22:59:49 -04002023 if (ctx->opts->size_multiple)
2024 size_mult = ctx->opts->size_multiple;
Taylor Blaufdbde822020-04-13 22:04:12 -06002025
Taylor Blau98bb7962020-09-17 22:59:49 -04002026 flags = ctx->opts->split_flags;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07002027 }
2028
Derrick Stolee1771be92019-06-18 11:14:29 -07002029 g = ctx->r->objects->commit_graph;
Alex Henrie8da02ce2019-09-30 20:29:34 -06002030 num_commits = ctx->commits.nr;
Taylor Blau8a6ac282020-04-13 22:04:17 -06002031 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2032 ctx->num_commit_graphs_after = 1;
2033 else
2034 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
Derrick Stolee1771be92019-06-18 11:14:29 -07002035
Taylor Blau8a6ac282020-04-13 22:04:17 -06002036 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2037 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
Taylor Blaufdbde822020-04-13 22:04:12 -06002038 while (g && (g->num_commits <= size_mult * num_commits ||
2039 (max_commits && num_commits > max_commits))) {
2040 if (g->odb != ctx->odb)
2041 break;
Derrick Stoleec5230352019-06-18 11:14:30 -07002042
Taylor Blaufdbde822020-04-13 22:04:12 -06002043 num_commits += g->num_commits;
2044 g = g->base_graph;
Derrick Stolee1771be92019-06-18 11:14:29 -07002045
Taylor Blaufdbde822020-04-13 22:04:12 -06002046 ctx->num_commit_graphs_after--;
2047 }
Derrick Stolee1771be92019-06-18 11:14:29 -07002048 }
2049
Taylor Blau8a6ac282020-04-13 22:04:17 -06002050 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2051 ctx->new_base_graph = g;
2052 else if (ctx->num_commit_graphs_after != 1)
2053 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2054 "should be 1 with --split=replace");
Derrick Stolee1771be92019-06-18 11:14:29 -07002055
Derrick Stoleec5230352019-06-18 11:14:30 -07002056 if (ctx->num_commit_graphs_after == 2) {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08002057 char *old_graph_name = get_commit_graph_filename(g->odb);
Derrick Stoleec5230352019-06-18 11:14:30 -07002058
2059 if (!strcmp(g->filename, old_graph_name) &&
Taylor Blauad2dd5b2020-02-03 13:18:02 -08002060 g->odb != ctx->odb) {
Derrick Stoleec5230352019-06-18 11:14:30 -07002061 ctx->num_commit_graphs_after = 1;
2062 ctx->new_base_graph = NULL;
2063 }
2064
2065 free(old_graph_name);
2066 }
2067
Taylor Blaub78a5562020-04-23 15:41:09 -06002068 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2069 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
Derrick Stolee1771be92019-06-18 11:14:29 -07002070
2071 for (i = 0; i < ctx->num_commit_graphs_after &&
2072 i < ctx->num_commit_graphs_before; i++)
2073 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2074
2075 i = ctx->num_commit_graphs_before - 1;
2076 g = ctx->r->objects->commit_graph;
2077
2078 while (g) {
2079 if (i < ctx->num_commit_graphs_after)
2080 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2081
Abhishek Kumar1fdc3832021-01-16 18:11:16 +00002082 /*
2083 * If the topmost remaining layer has generation data chunk, the
2084 * resultant layer also has generation data chunk.
2085 */
2086 if (i == ctx->num_commit_graphs_after - 2)
2087 ctx->write_generation_data = !!g->chunk_generation_data;
2088
Derrick Stolee1771be92019-06-18 11:14:29 -07002089 i--;
2090 g = g->base_graph;
2091 }
2092}
2093
2094static void merge_commit_graph(struct write_commit_graph_context *ctx,
2095 struct commit_graph *g)
2096{
2097 uint32_t i;
2098 uint32_t offset = g->num_commits_in_base;
2099
2100 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2101
2102 for (i = 0; i < g->num_commits; i++) {
2103 struct object_id oid;
2104 struct commit *result;
2105
2106 display_progress(ctx->progress, i + 1);
2107
2108 load_oid_from_graph(g, i + offset, &oid);
2109
2110 /* only add commits if they still exist in the repo */
2111 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2112
2113 if (result) {
2114 ctx->commits.list[ctx->commits.nr] = result;
2115 ctx->commits.nr++;
2116 }
2117 }
2118}
2119
2120static int commit_compare(const void *_a, const void *_b)
2121{
2122 const struct commit *a = *(const struct commit **)_a;
2123 const struct commit *b = *(const struct commit **)_b;
2124 return oidcmp(&a->object.oid, &b->object.oid);
2125}
2126
2127static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2128{
Derrick Stolee150f1152020-10-09 20:53:51 +00002129 uint32_t i, dedup_i = 0;
Derrick Stolee1771be92019-06-18 11:14:29 -07002130
2131 if (ctx->report_progress)
2132 ctx->progress = start_delayed_progress(
2133 _("Scanning merged commits"),
2134 ctx->commits.nr);
2135
2136 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2137
2138 ctx->num_extra_edges = 0;
2139 for (i = 0; i < ctx->commits.nr; i++) {
SZEDER Gábor40112242021-09-09 03:10:11 +02002140 display_progress(ctx->progress, i + 1);
Derrick Stolee1771be92019-06-18 11:14:29 -07002141
2142 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2143 &ctx->commits.list[i]->object.oid)) {
Derrick Stolee150f1152020-10-09 20:53:51 +00002144 /*
2145 * Silently ignore duplicates. These were likely
2146 * created due to a commit appearing in multiple
2147 * layers of the chain, which is unexpected but
2148 * not invalid. We should make sure there is a
2149 * unique copy in the new layer.
2150 */
Derrick Stolee1771be92019-06-18 11:14:29 -07002151 } else {
René Scharfe689a1462019-09-15 19:07:44 +02002152 unsigned int num_parents;
Derrick Stolee1771be92019-06-18 11:14:29 -07002153
Derrick Stolee150f1152020-10-09 20:53:51 +00002154 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2155 dedup_i++;
2156
René Scharfe689a1462019-09-15 19:07:44 +02002157 num_parents = commit_list_count(ctx->commits.list[i]->parents);
Derrick Stolee1771be92019-06-18 11:14:29 -07002158 if (num_parents > 2)
Derrick Stoleea35bea42019-08-05 09:43:41 -07002159 ctx->num_extra_edges += num_parents - 1;
Derrick Stolee1771be92019-06-18 11:14:29 -07002160 }
2161 }
2162
Derrick Stolee150f1152020-10-09 20:53:51 +00002163 ctx->commits.nr = dedup_i;
2164
Derrick Stolee1771be92019-06-18 11:14:29 -07002165 stop_progress(&ctx->progress);
2166}
2167
2168static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2169{
2170 struct commit_graph *g = ctx->r->objects->commit_graph;
2171 uint32_t current_graph_number = ctx->num_commit_graphs_before;
Derrick Stolee1771be92019-06-18 11:14:29 -07002172
2173 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2174 current_graph_number--;
2175
René Scharfed68ce902020-02-20 19:49:18 +01002176 if (ctx->report_progress)
2177 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
Derrick Stolee1771be92019-06-18 11:14:29 -07002178
2179 merge_commit_graph(ctx, g);
2180 stop_progress(&ctx->progress);
Derrick Stolee1771be92019-06-18 11:14:29 -07002181
2182 g = g->base_graph;
2183 }
2184
2185 if (g) {
2186 ctx->new_base_graph = g;
2187 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2188 }
2189
2190 if (ctx->new_base_graph)
2191 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2192
2193 sort_and_scan_merged_commits(ctx);
2194}
2195
Derrick Stolee8d840972019-06-18 11:14:31 -07002196static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2197{
2198 uint32_t i;
2199 time_t now = time(NULL);
2200
2201 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2202 struct stat st;
2203 struct utimbuf updated_time;
2204
Ævar Arnfjörð Bjarmason7c898552022-05-12 15:32:17 -07002205 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2206 continue;
Derrick Stolee8d840972019-06-18 11:14:31 -07002207
2208 updated_time.actime = st.st_atime;
2209 updated_time.modtime = now;
2210 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2211 }
2212}
2213
2214static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2215{
2216 struct strbuf path = STRBUF_INIT;
2217 DIR *dir;
2218 struct dirent *de;
2219 size_t dirnamelen;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07002220 timestamp_t expire_time = time(NULL);
2221
Taylor Blau98bb7962020-09-17 22:59:49 -04002222 if (ctx->opts && ctx->opts->expire_time)
2223 expire_time = ctx->opts->expire_time;
Derrick Stoleeba411122019-06-18 11:14:33 -07002224 if (!ctx->split) {
Derrick Stolee663b2b12020-09-17 18:11:46 +00002225 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
Derrick Stoleeba411122019-06-18 11:14:33 -07002226 unlink(chain_file_name);
2227 free(chain_file_name);
2228 ctx->num_commit_graphs_after = 0;
2229 }
Derrick Stolee8d840972019-06-18 11:14:31 -07002230
Taylor Blau0bd52e22020-02-03 21:51:50 -08002231 strbuf_addstr(&path, ctx->odb->path);
Derrick Stolee8d840972019-06-18 11:14:31 -07002232 strbuf_addstr(&path, "/info/commit-graphs");
2233 dir = opendir(path.buf);
2234
René Scharfe0aa6bce2019-08-07 13:15:02 +02002235 if (!dir)
2236 goto out;
Derrick Stolee8d840972019-06-18 11:14:31 -07002237
2238 strbuf_addch(&path, '/');
2239 dirnamelen = path.len;
2240 while ((de = readdir(dir)) != NULL) {
2241 struct stat st;
2242 uint32_t i, found = 0;
2243
2244 strbuf_setlen(&path, dirnamelen);
2245 strbuf_addstr(&path, de->d_name);
2246
Ævar Arnfjörð Bjarmason7c898552022-05-12 15:32:17 -07002247 if (stat(path.buf, &st) < 0)
2248 continue;
Derrick Stolee8d840972019-06-18 11:14:31 -07002249
2250 if (st.st_mtime > expire_time)
2251 continue;
2252 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2253 continue;
2254
2255 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2256 if (!strcmp(ctx->commit_graph_filenames_after[i],
2257 path.buf)) {
2258 found = 1;
2259 break;
2260 }
2261 }
2262
2263 if (!found)
2264 unlink(path.buf);
Derrick Stolee8d840972019-06-18 11:14:31 -07002265 }
René Scharfe0aa6bce2019-08-07 13:15:02 +02002266
2267out:
Miaoqian Lin12f1ae52022-09-19 18:14:40 +04002268 if(dir)
2269 closedir(dir);
René Scharfe0aa6bce2019-08-07 13:15:02 +02002270 strbuf_release(&path);
Derrick Stolee8d840972019-06-18 11:14:31 -07002271}
2272
Taylor Blau0bd52e22020-02-03 21:51:50 -08002273int write_commit_graph(struct object_directory *odb,
Ævar Arnfjörð Bjarmason4a047902022-03-04 19:32:12 +01002274 const struct string_list *const pack_indexes,
Taylor Blau6830c362020-04-13 22:04:25 -06002275 struct oidset *commits,
SZEDER Gábor39d88312019-08-05 10:02:39 +02002276 enum commit_graph_write_flags flags,
Taylor Blau98bb7962020-09-17 22:59:49 -04002277 const struct commit_graph_opts *opts)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002278{
Derrick Stoleec7ef8fe2021-02-25 18:19:42 +00002279 struct repository *r = the_repository;
Derrick Stoleec9905be2019-06-12 06:29:40 -07002280 struct write_commit_graph_context *ctx;
Jeff King1cbdbf32020-12-07 14:11:02 -05002281 uint32_t i;
Derrick Stoleee103f722019-06-12 06:29:37 -07002282 int res = 0;
Taylor Blau8a6ac282020-04-13 22:04:17 -06002283 int replace = 0;
Taylor Blau9a7a9ed2020-09-16 14:07:46 -04002284 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +00002285 struct topo_level_slab topo_levels;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002286
Derrick Stoleec7ef8fe2021-02-25 18:19:42 +00002287 prepare_repo_settings(r);
2288 if (!r->settings.core_commit_graph) {
Derrick Stolee85102ac2020-10-09 20:53:52 +00002289 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2290 return 0;
2291 }
Derrick Stoleec7ef8fe2021-02-25 18:19:42 +00002292 if (!commit_graph_compatible(r))
Derrick Stoleee103f722019-06-12 06:29:37 -07002293 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002294
René Scharfeca56dad2021-03-13 17:17:22 +01002295 CALLOC_ARRAY(ctx, 1);
Derrick Stoleec7ef8fe2021-02-25 18:19:42 +00002296 ctx->r = r;
Taylor Blau0bd52e22020-02-03 21:51:50 -08002297 ctx->odb = odb;
SZEDER Gábor39d88312019-08-05 10:02:39 +02002298 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2299 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2300 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
Taylor Blau98bb7962020-09-17 22:59:49 -04002301 ctx->opts = opts;
Garima Singhf97b9322020-03-30 00:31:28 +00002302 ctx->total_bloom_filter_data_size = 0;
Derrick Stolee702110a2021-02-25 18:19:43 +00002303 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
Abhishek Kumare8b63002021-01-16 18:11:15 +00002304 ctx->num_generation_data_overflows = 0;
Derrick Stolee6c622f92019-06-18 11:14:27 -07002305
Taylor Blau9a7a9ed2020-09-16 14:07:46 -04002306 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2307 bloom_settings.bits_per_entry);
2308 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2309 bloom_settings.num_hashes);
2310 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2311 bloom_settings.max_changed_paths);
2312 ctx->bloom_settings = &bloom_settings;
2313
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +00002314 init_topo_level_slab(&topo_levels);
2315 ctx->topo_levels = &topo_levels;
2316
Derrick Stoleebc50d6c2021-02-02 03:01:23 +00002317 prepare_commit_graph(ctx->r);
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +00002318 if (ctx->r->objects->commit_graph) {
2319 struct commit_graph *g = ctx->r->objects->commit_graph;
2320
2321 while (g) {
2322 g->topo_levels = &topo_levels;
2323 g = g->base_graph;
2324 }
2325 }
2326
Derrick Stolee0087a872020-07-01 13:27:24 +00002327 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2328 ctx->changed_paths = 1;
2329 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2330 struct commit_graph *g;
Derrick Stolee0087a872020-07-01 13:27:24 +00002331
2332 g = ctx->r->objects->commit_graph;
2333
2334 /* We have changed-paths already. Keep them in the next graph */
2335 if (g && g->chunk_bloom_data) {
2336 ctx->changed_paths = 1;
2337 ctx->bloom_settings = g->bloom_filter_settings;
2338 }
2339 }
2340
Derrick Stolee6c622f92019-06-18 11:14:27 -07002341 if (ctx->split) {
Derrick Stoleebc50d6c2021-02-02 03:01:23 +00002342 struct commit_graph *g = ctx->r->objects->commit_graph;
Derrick Stolee6c622f92019-06-18 11:14:27 -07002343
2344 while (g) {
2345 ctx->num_commit_graphs_before++;
2346 g = g->base_graph;
2347 }
2348
2349 if (ctx->num_commit_graphs_before) {
2350 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2351 i = ctx->num_commit_graphs_before;
2352 g = ctx->r->objects->commit_graph;
2353
2354 while (g) {
2355 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2356 g = g->base_graph;
2357 }
2358 }
Taylor Blau8a6ac282020-04-13 22:04:17 -06002359
Taylor Blau98bb7962020-09-17 22:59:49 -04002360 if (ctx->opts)
2361 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
Derrick Stolee6c622f92019-06-18 11:14:27 -07002362 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002363
Derrick Stoleec9905be2019-06-12 06:29:40 -07002364 ctx->approx_nr_objects = approximate_object_count();
Derrick Stoleec9905be2019-06-12 06:29:40 -07002365
Derrick Stoleec9905be2019-06-12 06:29:40 -07002366 if (ctx->append && ctx->r->objects->commit_graph) {
2367 struct commit_graph *g = ctx->r->objects->commit_graph;
2368 for (i = 0; i < g->num_commits; i++) {
Jeff Kinga5f1c442020-12-07 14:11:05 -05002369 struct object_id oid;
brian m. carlson92e2cab2021-04-26 01:02:50 +00002370 oidread(&oid, g->chunk_oid_lookup + g->hash_len * i);
Jeff Kinga5f1c442020-12-07 14:11:05 -05002371 oid_array_append(&ctx->oids, &oid);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002372 }
2373 }
2374
2375 if (pack_indexes) {
Garima Singh3d112752020-03-30 00:31:30 +00002376 ctx->order_by_pack = 1;
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07002377 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2378 goto cleanup;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002379 }
2380
Taylor Blau6830c362020-04-13 22:04:25 -06002381 if (commits) {
2382 if ((res = fill_oids_from_commits(ctx, commits)))
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02002383 goto cleanup;
2384 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002385
Junio C Hamano9b6606f2020-05-01 13:39:53 -07002386 if (!pack_indexes && !commits) {
Garima Singh3d112752020-03-30 00:31:30 +00002387 ctx->order_by_pack = 1;
Derrick Stoleeb2c83062019-06-12 06:29:42 -07002388 fill_oids_from_all_packs(ctx);
Garima Singh3d112752020-03-30 00:31:30 +00002389 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002390
Derrick Stoleec9905be2019-06-12 06:29:40 -07002391 close_reachable(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002392
Derrick Stoleef998d542019-06-12 06:29:44 -07002393 copy_oids_to_commits(ctx);
Derrick Stolee1373e542018-06-27 09:24:39 -04002394
Derrick Stoleec9905be2019-06-12 06:29:40 -07002395 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
Derrick Stoleee103f722019-06-12 06:29:37 -07002396 error(_("too many commits to write graph"));
2397 res = -1;
2398 goto cleanup;
2399 }
Derrick Stolee283e68c2018-06-27 09:24:32 -04002400
Taylor Blau8a6ac282020-04-13 22:04:17 -06002401 if (!ctx->commits.nr && !replace)
Derrick Stolee6c622f92019-06-18 11:14:27 -07002402 goto cleanup;
2403
Derrick Stolee1771be92019-06-18 11:14:29 -07002404 if (ctx->split) {
2405 split_graph_merge_strategy(ctx);
2406
Taylor Blau8a6ac282020-04-13 22:04:17 -06002407 if (!replace)
2408 merge_commit_graphs(ctx);
Derrick Stolee1771be92019-06-18 11:14:29 -07002409 } else
Derrick Stolee6c622f92019-06-18 11:14:27 -07002410 ctx->num_commit_graphs_after = 1;
2411
Derrick Stoleefde55b02021-02-02 03:01:22 +00002412 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
Abhishek Kumar1fdc3832021-01-16 18:11:16 +00002413
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00002414 compute_topological_levels(ctx);
2415 if (ctx->write_generation_data)
2416 compute_generation_numbers(ctx);
Derrick Stolee9bda8462018-06-27 09:24:35 -04002417
Garima Singhf97b9322020-03-30 00:31:28 +00002418 if (ctx->changed_paths)
2419 compute_bloom_filters(ctx);
2420
Derrick Stolee238def52019-06-12 06:29:45 -07002421 res = write_commit_graph_file(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002422
Derrick Stoleeba411122019-06-18 11:14:33 -07002423 if (ctx->split)
Derrick Stolee8d840972019-06-18 11:14:31 -07002424 mark_commit_graphs(ctx);
Derrick Stoleeba411122019-06-18 11:14:33 -07002425
2426 expire_commit_graphs(ctx);
Derrick Stolee8d840972019-06-18 11:14:31 -07002427
Derrick Stoleee103f722019-06-12 06:29:37 -07002428cleanup:
Derrick Stolee238def52019-06-12 06:29:45 -07002429 free(ctx->graph_name);
Derrick Stoleec9905be2019-06-12 06:29:40 -07002430 free(ctx->commits.list);
Jeff Kinga5f1c442020-12-07 14:11:05 -05002431 oid_array_clear(&ctx->oids);
Andrzej Huntbf4bb9f2021-02-19 20:13:10 +00002432 clear_topo_level_slab(&topo_levels);
Derrick Stolee6c622f92019-06-18 11:14:27 -07002433
2434 if (ctx->commit_graph_filenames_after) {
2435 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2436 free(ctx->commit_graph_filenames_after[i]);
2437 free(ctx->commit_graph_hash_after[i]);
2438 }
2439
2440 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2441 free(ctx->commit_graph_filenames_before[i]);
2442
2443 free(ctx->commit_graph_filenames_after);
2444 free(ctx->commit_graph_filenames_before);
2445 free(ctx->commit_graph_hash_after);
2446 }
2447
Derrick Stoleec9905be2019-06-12 06:29:40 -07002448 free(ctx);
Derrick Stoleee103f722019-06-12 06:29:37 -07002449
2450 return res;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002451}
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002452
2453#define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2454static int verify_commit_graph_error;
2455
Ævar Arnfjörð Bjarmason48ca53c2021-07-13 10:05:18 +02002456__attribute__((format (printf, 1, 2)))
Derrick Stolee283e68c2018-06-27 09:24:32 -04002457static void graph_report(const char *fmt, ...)
2458{
2459 va_list ap;
2460
2461 verify_commit_graph_error = 1;
2462 va_start(ap, fmt);
2463 vfprintf(stderr, fmt, ap);
2464 fprintf(stderr, "\n");
2465 va_end(ap);
2466}
2467
2468#define GENERATION_ZERO_EXISTS 1
2469#define GENERATION_NUMBER_EXISTS 2
2470
Taylor Blau15316a42021-06-23 14:39:09 -04002471static int commit_graph_checksum_valid(struct commit_graph *g)
2472{
2473 return hashfile_checksum_valid(g->data, g->data_len);
2474}
2475
Derrick Stolee3da4b602019-06-18 11:14:32 -07002476int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
Derrick Stolee283e68c2018-06-27 09:24:32 -04002477{
Derrick Stolee9bda8462018-06-27 09:24:35 -04002478 uint32_t i, cur_fanout_pos = 0;
brian m. carlson72871b132021-04-26 01:02:58 +00002479 struct object_id prev_oid, cur_oid;
Derrick Stolee1373e542018-06-27 09:24:39 -04002480 int generation_zero = 0;
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00002481 struct progress *progress = NULL;
Derrick Stolee3da4b602019-06-18 11:14:32 -07002482 int local_error = 0;
Derrick Stolee9bda8462018-06-27 09:24:35 -04002483
Derrick Stolee283e68c2018-06-27 09:24:32 -04002484 if (!g) {
2485 graph_report("no commit-graph file loaded");
2486 return 1;
2487 }
2488
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +01002489 verify_commit_graph_error = verify_commit_graph_lite(g);
Derrick Stolee9bda8462018-06-27 09:24:35 -04002490 if (verify_commit_graph_error)
2491 return verify_commit_graph_error;
2492
Taylor Blau15316a42021-06-23 14:39:09 -04002493 if (!commit_graph_checksum_valid(g)) {
Derrick Stolee41df0e32018-06-27 09:24:42 -04002494 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2495 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2496 }
2497
Derrick Stolee9bda8462018-06-27 09:24:35 -04002498 for (i = 0; i < g->num_commits; i++) {
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002499 struct commit *graph_commit;
2500
brian m. carlson92e2cab2021-04-26 01:02:50 +00002501 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
Derrick Stolee9bda8462018-06-27 09:24:35 -04002502
2503 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002504 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04002505 oid_to_hex(&prev_oid),
2506 oid_to_hex(&cur_oid));
2507
2508 oidcpy(&prev_oid, &cur_oid);
2509
2510 while (cur_oid.hash[0] > cur_fanout_pos) {
2511 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2512
2513 if (i != 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 cur_fanout_pos++;
2517 }
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002518
Junio C Hamano82952962018-07-17 15:46:19 -07002519 graph_commit = lookup_commit(r, &cur_oid);
Stefan Beller4f542b72018-12-14 16:09:39 -08002520 if (!parse_commit_in_graph_one(r, g, graph_commit))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002521 graph_report(_("failed to parse commit %s from commit-graph"),
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002522 oid_to_hex(&cur_oid));
Derrick Stolee9bda8462018-06-27 09:24:35 -04002523 }
2524
2525 while (cur_fanout_pos < 256) {
2526 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2527
2528 if (g->num_commits != fanout_value)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002529 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04002530 cur_fanout_pos, fanout_value, i);
2531
2532 cur_fanout_pos++;
2533 }
2534
Derrick Stolee41df0e32018-06-27 09:24:42 -04002535 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
Derrick Stolee96af91d2018-06-27 09:24:36 -04002536 return verify_commit_graph_error;
2537
Garima Singh73716122019-08-26 09:29:58 -07002538 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2539 progress = start_progress(_("Verifying commits in commit graph"),
2540 g->num_commits);
2541
Derrick Stolee96af91d2018-06-27 09:24:36 -04002542 for (i = 0; i < g->num_commits; i++) {
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002543 struct commit *graph_commit, *odb_commit;
Derrick Stolee53614b12018-06-27 09:24:38 -04002544 struct commit_list *graph_parents, *odb_parents;
Abhishek Kumard7f92782021-01-16 18:11:13 +00002545 timestamp_t max_generation = 0;
2546 timestamp_t generation;
Derrick Stolee96af91d2018-06-27 09:24:36 -04002547
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00002548 display_progress(progress, i + 1);
brian m. carlson92e2cab2021-04-26 01:02:50 +00002549 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002550
Junio C Hamano82952962018-07-17 15:46:19 -07002551 graph_commit = lookup_commit(r, &cur_oid);
Jeff Kinga3785092019-06-20 03:41:21 -04002552 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
Derrick Stolee96af91d2018-06-27 09:24:36 -04002553 if (parse_commit_internal(odb_commit, 0, 0)) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002554 graph_report(_("failed to parse commit %s from object database for commit-graph"),
Derrick Stolee96af91d2018-06-27 09:24:36 -04002555 oid_to_hex(&cur_oid));
2556 continue;
2557 }
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002558
Stefan Beller4f542b72018-12-14 16:09:39 -08002559 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002560 get_commit_tree_oid(odb_commit)))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002561 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002562 oid_to_hex(&cur_oid),
2563 oid_to_hex(get_commit_tree_oid(graph_commit)),
2564 oid_to_hex(get_commit_tree_oid(odb_commit)));
Derrick Stolee53614b12018-06-27 09:24:38 -04002565
2566 graph_parents = graph_commit->parents;
2567 odb_parents = odb_commit->parents;
2568
2569 while (graph_parents) {
Junio C Hamanoafe8a902022-05-02 09:50:37 -07002570 if (!odb_parents) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002571 graph_report(_("commit-graph parent list for commit %s is too long"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002572 oid_to_hex(&cur_oid));
2573 break;
2574 }
2575
Derrick Stolee3da4b602019-06-18 11:14:32 -07002576 /* parse parent in case it is in a base graph */
2577 parse_commit_in_graph_one(r, g, graph_parents->item);
2578
Jeff King9001dc22018-08-28 17:22:48 -04002579 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002580 graph_report(_("commit-graph parent for %s is %s != %s"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002581 oid_to_hex(&cur_oid),
2582 oid_to_hex(&graph_parents->item->object.oid),
2583 oid_to_hex(&odb_parents->item->object.oid));
2584
Abhishek Kumarc752ad02020-06-17 14:44:11 +05302585 generation = commit_graph_generation(graph_parents->item);
2586 if (generation > max_generation)
2587 max_generation = generation;
Derrick Stolee1373e542018-06-27 09:24:39 -04002588
Derrick Stolee53614b12018-06-27 09:24:38 -04002589 graph_parents = graph_parents->next;
2590 odb_parents = odb_parents->next;
2591 }
2592
Junio C Hamanoafe8a902022-05-02 09:50:37 -07002593 if (odb_parents)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002594 graph_report(_("commit-graph parent list for commit %s terminates early"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002595 oid_to_hex(&cur_oid));
Derrick Stolee1373e542018-06-27 09:24:39 -04002596
Abhishek Kumarc49c82a2020-06-17 14:44:10 +05302597 if (!commit_graph_generation(graph_commit)) {
Derrick Stolee1373e542018-06-27 09:24:39 -04002598 if (generation_zero == GENERATION_NUMBER_EXISTS)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002599 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
Derrick Stolee1373e542018-06-27 09:24:39 -04002600 oid_to_hex(&cur_oid));
2601 generation_zero = GENERATION_ZERO_EXISTS;
2602 } else if (generation_zero == GENERATION_ZERO_EXISTS)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002603 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
Derrick Stolee1373e542018-06-27 09:24:39 -04002604 oid_to_hex(&cur_oid));
2605
2606 if (generation_zero == GENERATION_ZERO_EXISTS)
2607 continue;
2608
2609 /*
Abhishek Kumare8b63002021-01-16 18:11:15 +00002610 * If we are using topological level and one of our parents has
2611 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2612 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2613 * in the following condition.
Derrick Stolee1373e542018-06-27 09:24:39 -04002614 */
Abhishek Kumar1fdc3832021-01-16 18:11:16 +00002615 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
Derrick Stolee1373e542018-06-27 09:24:39 -04002616 max_generation--;
2617
Abhishek Kumarc752ad02020-06-17 14:44:11 +05302618 generation = commit_graph_generation(graph_commit);
Abhishek Kumare8b63002021-01-16 18:11:15 +00002619 if (generation < max_generation + 1)
2620 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
Derrick Stolee1373e542018-06-27 09:24:39 -04002621 oid_to_hex(&cur_oid),
Abhishek Kumarc752ad02020-06-17 14:44:11 +05302622 generation,
Derrick Stolee1373e542018-06-27 09:24:39 -04002623 max_generation + 1);
Derrick Stolee88968eb2018-06-27 09:24:40 -04002624
2625 if (graph_commit->date != odb_commit->date)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002626 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
Derrick Stolee88968eb2018-06-27 09:24:40 -04002627 oid_to_hex(&cur_oid),
2628 graph_commit->date,
2629 odb_commit->date);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002630 }
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00002631 stop_progress(&progress);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002632
Derrick Stolee3da4b602019-06-18 11:14:32 -07002633 local_error = verify_commit_graph_error;
2634
2635 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2636 local_error |= verify_commit_graph(r, g->base_graph, flags);
2637
2638 return local_error;
Derrick Stolee283e68c2018-06-27 09:24:32 -04002639}
Jonathan Tanc3756d52018-07-11 15:42:40 -07002640
2641void free_commit_graph(struct commit_graph *g)
2642{
2643 if (!g)
2644 return;
Jeff Kingc8828532020-04-23 15:41:13 -06002645 if (g->data) {
Jonathan Tanc3756d52018-07-11 15:42:40 -07002646 munmap((void *)g->data, g->data_len);
2647 g->data = NULL;
Jonathan Tanc3756d52018-07-11 15:42:40 -07002648 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07002649 free(g->filename);
Garima Singh76ffbca2020-04-06 16:59:49 +00002650 free(g->bloom_filter_settings);
Jonathan Tanc3756d52018-07-11 15:42:40 -07002651 free(g);
2652}
Jeff King6abada12019-09-12 10:44:45 -04002653
2654void disable_commit_graph(struct repository *r)
2655{
2656 r->commit_graph_disabled = 1;
2657}