blob: 45417d7412202e54d21de0ae49941536eb2c82b4 [file] [log] [blame]
Elijah Newrenb6fdc442023-04-11 00:41:54 -07001#include "git-compat-util.h"
SZEDER Gáborfa796532020-06-05 13:00:28 +00002#include "config.h"
Elijah Newrenec2101a2023-12-23 17:14:59 +00003#include "csum-file.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00004#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00005#include "hex.h"
Derrick Stolee08fd81c2018-04-02 16:34:19 -04006#include "lockfile.h"
Derrick Stolee08fd81c2018-04-02 16:34:19 -04007#include "packfile.h"
8#include "commit.h"
9#include "object.h"
Derrick Stolee59fb8772018-06-27 09:24:45 -040010#include "refs.h"
Martin Ågrenbc626922020-12-31 12:56:23 +010011#include "hash-lookup.h"
Derrick Stolee08fd81c2018-04-02 16:34:19 -040012#include "commit-graph.h"
Elijah Newren87bed172023-04-11 00:41:53 -070013#include "object-file.h"
Elijah Newrena034e912023-05-16 06:34:06 +000014#include "object-store-ll.h"
Elijah Newren6f2d7432023-04-11 03:00:42 +000015#include "oid-array.h"
Elijah Newrenc3399322023-05-16 06:33:59 +000016#include "path.h"
Derrick Stolee96af91d2018-06-27 09:24:36 -040017#include "alloc.h"
Derrick Stoleed6538242018-08-20 18:24:27 +000018#include "hashmap.h"
19#include "replace-object.h"
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +000020#include "progress.h"
Garima Singhf97b9322020-03-30 00:31:28 +000021#include "bloom.h"
Jeff Kingd21ee7d2020-03-30 00:31:29 +000022#include "commit-slab.h"
Taylor Blau120ad2b2020-04-30 13:48:50 -060023#include "shallow.h"
Derrick Stolee0087a872020-07-01 13:27:24 +000024#include "json-writer.h"
25#include "trace2.h"
Elijah Newrend4a4f922023-04-22 20:17:26 +000026#include "tree.h"
Derrick Stolee47410aa2021-02-18 14:07:25 +000027#include "chunk-format.h"
Derrick Stolee08fd81c2018-04-02 16:34:19 -040028
Derrick Stoleeb23ea972020-04-16 20:14:03 +000029void git_test_write_commit_graph_or_die(void)
30{
31 int flags = 0;
32 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
33 return;
34
35 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
36 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
37
38 if (write_commit_graph_reachable(the_repository->objects->odb,
39 flags, NULL))
40 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
41}
42
Derrick Stolee08fd81c2018-04-02 16:34:19 -040043#define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
44#define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
45#define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
46#define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
Derrick Stolee6dbf4b82022-03-02 09:45:13 -050047#define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
48#define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
SZEDER Gábor5af74172019-01-19 21:21:13 +010049#define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
Garima Singh76ffbca2020-04-06 16:59:49 +000050#define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
51#define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
Derrick Stolee118bd572019-06-18 11:14:26 -070052#define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
Derrick Stolee08fd81c2018-04-02 16:34:19 -040053
brian m. carlsonc1665992018-11-14 04:09:35 +000054#define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
Derrick Stolee08fd81c2018-04-02 16:34:19 -040055
56#define GRAPH_VERSION_1 0x1
57#define GRAPH_VERSION GRAPH_VERSION_1
58
SZEDER Gábor5af74172019-01-19 21:21:13 +010059#define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
Derrick Stolee08fd81c2018-04-02 16:34:19 -040060#define GRAPH_EDGE_LAST_MASK 0x7fffffff
61#define GRAPH_PARENT_NONE 0x70000000
62
63#define GRAPH_LAST_EDGE 0x80000000
64
Derrick Stolee0e3b97c2018-06-27 09:24:28 -040065#define GRAPH_HEADER_SIZE 8
Derrick Stolee08fd81c2018-04-02 16:34:19 -040066#define GRAPH_FANOUT_SIZE (4 * 256)
Derrick Stolee2692c2f2021-02-18 14:07:35 +000067#define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
brian m. carlsonc1665992018-11-14 04:09:35 +000068 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
Derrick Stolee08fd81c2018-04-02 16:34:19 -040069
Abhishek Kumare8b63002021-01-16 18:11:15 +000070#define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
71
Derrick Stoleecb99a342019-10-24 13:40:42 +000072/* Remember to update object flag allocation in object.h */
73#define REACHABLE (1u<<15)
74
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +000075define_commit_slab(topo_level_slab, uint32_t);
76
Jeff Kingd21ee7d2020-03-30 00:31:29 +000077/* Keep track of the order in which commits are added to our list. */
78define_commit_slab(commit_pos, int);
79static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
80
81static void set_commit_pos(struct repository *r, const struct object_id *oid)
Derrick Stolee08fd81c2018-04-02 16:34:19 -040082{
Jeff Kingd21ee7d2020-03-30 00:31:29 +000083 static int32_t max_pos;
84 struct commit *commit = lookup_commit(r, oid);
85
86 if (!commit)
87 return; /* should never happen, but be lenient */
88
89 *commit_pos_at(&commit_pos, commit) = max_pos++;
90}
91
92static int commit_pos_cmp(const void *va, const void *vb)
93{
94 const struct commit *a = *(const struct commit **)va;
95 const struct commit *b = *(const struct commit **)vb;
96 return commit_pos_at(&commit_pos, a) -
97 commit_pos_at(&commit_pos, b);
98}
99
Abhishek Kumar48448122020-06-17 14:44:09 +0530100define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
101static struct commit_graph_data_slab commit_graph_data_slab =
102 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
103
Derrick Stolee702110a2021-02-25 18:19:43 +0000104static int get_configured_generation_version(struct repository *r)
105{
106 int version = 2;
107 repo_config_get_int(r, "commitgraph.generationversion", &version);
108 return version;
109}
110
Abhishek Kumar48448122020-06-17 14:44:09 +0530111uint32_t commit_graph_position(const struct commit *c)
112{
113 struct commit_graph_data *data =
114 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
115
116 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
117}
118
Abhishek Kumard7f92782021-01-16 18:11:13 +0000119timestamp_t commit_graph_generation(const struct commit *c)
Abhishek Kumar48448122020-06-17 14:44:09 +0530120{
121 struct commit_graph_data *data =
122 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
123
Derrick Stolee2ee11f72023-03-20 11:26:51 +0000124 if (data && data->generation)
125 return data->generation;
Abhishek Kumar48448122020-06-17 14:44:09 +0530126
Derrick Stolee2ee11f72023-03-20 11:26:51 +0000127 return GENERATION_NUMBER_INFINITY;
Abhishek Kumar48448122020-06-17 14:44:09 +0530128}
129
Taylor Blau868c9912023-08-21 17:34:34 -0400130static timestamp_t commit_graph_generation_from_graph(const struct commit *c)
131{
132 struct commit_graph_data *data =
133 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
134
135 if (!data || data->graph_pos == COMMIT_NOT_FROM_GRAPH)
136 return GENERATION_NUMBER_INFINITY;
137 return data->generation;
138}
139
Abhishek Kumar48448122020-06-17 14:44:09 +0530140static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
141{
142 unsigned int i, nth_slab;
143 struct commit_graph_data *data =
144 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
145
146 if (data)
147 return data;
148
149 nth_slab = c->index / commit_graph_data_slab.slab_size;
150 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
151
152 /*
153 * commit-slab initializes elements with zero, overwrite this with
154 * COMMIT_NOT_FROM_GRAPH for graph_pos.
155 *
156 * We avoid initializing generation with checking if graph position
157 * is not COMMIT_NOT_FROM_GRAPH.
158 */
159 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
160 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
161 COMMIT_NOT_FROM_GRAPH;
162 }
163
164 return data;
165}
166
Abhishek Kumare30c5ee2021-01-16 18:11:08 +0000167/*
168 * Should be used only while writing commit-graph as it compares
169 * generation value of commits by directly accessing commit-slab.
170 */
Garima Singh3d112752020-03-30 00:31:30 +0000171static int commit_gen_cmp(const void *va, const void *vb)
172{
173 const struct commit *a = *(const struct commit **)va;
174 const struct commit *b = *(const struct commit **)vb;
175
Abhishek Kumard7f92782021-01-16 18:11:13 +0000176 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
177 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
Garima Singh3d112752020-03-30 00:31:30 +0000178 /* lower generation commits first */
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530179 if (generation_a < generation_b)
Garima Singh3d112752020-03-30 00:31:30 +0000180 return -1;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530181 else if (generation_a > generation_b)
Garima Singh3d112752020-03-30 00:31:30 +0000182 return 1;
183
184 /* use date as a heuristic when generations are equal */
185 if (a->date < b->date)
186 return -1;
187 else if (a->date > b->date)
188 return 1;
189 return 0;
190}
191
Jeff Kingd21ee7d2020-03-30 00:31:29 +0000192char *get_commit_graph_filename(struct object_directory *obj_dir)
193{
194 return xstrfmt("%s/info/commit-graph", obj_dir->path);
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400195}
196
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800197static char *get_split_graph_filename(struct object_directory *odb,
Derrick Stolee5c84b332019-06-18 11:14:25 -0700198 const char *oid_hex)
199{
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800200 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
201 oid_hex);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700202}
203
Derrick Stolee663b2b12020-09-17 18:11:46 +0000204char *get_commit_graph_chain_filename(struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700205{
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800206 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400207}
208
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400209static struct commit_graph *alloc_commit_graph(void)
210{
211 struct commit_graph *g = xcalloc(1, sizeof(*g));
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400212
213 return g;
214}
215
Derrick Stoleed6538242018-08-20 18:24:27 +0000216static int commit_graph_compatible(struct repository *r)
217{
Derrick Stolee5cef2952018-08-20 18:24:32 +0000218 if (!r->gitdir)
219 return 0;
220
Derrick Stoleef1178382023-06-06 13:24:36 +0000221 if (replace_refs_enabled(r)) {
Derrick Stoleed6538242018-08-20 18:24:27 +0000222 prepare_replace_object(r);
Junio C Hamanocdc986a2021-03-01 09:19:37 -0800223 if (hashmap_get_size(&r->objects->replace_map->map))
Derrick Stoleed6538242018-08-20 18:24:27 +0000224 return 0;
225 }
226
Derrick Stolee20fd6d52018-08-20 18:24:30 +0000227 prepare_commit_graft(r);
Taylor Blauce163642020-07-08 17:10:53 -0400228 if (r->parsed_objects &&
Junio C Hamanocdc986a2021-03-01 09:19:37 -0800229 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
Derrick Stolee20fd6d52018-08-20 18:24:30 +0000230 return 0;
Junio C Hamanocdc986a2021-03-01 09:19:37 -0800231 if (is_repository_shallow(r))
Derrick Stolee20fd6d52018-08-20 18:24:30 +0000232 return 0;
233
Derrick Stoleed6538242018-08-20 18:24:27 +0000234 return 1;
235}
236
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100237int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
238{
239 *fd = git_open(graph_file);
240 if (*fd < 0)
241 return 0;
242 if (fstat(*fd, st)) {
243 close(*fd);
244 return 0;
245 }
246 return 1;
247}
248
Taylor Blauab14d062020-09-09 11:22:56 -0400249struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
250 int fd, struct stat *st,
Taylor Blaua7df60c2020-02-03 13:18:04 -0800251 struct object_directory *odb)
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400252{
253 void *graph_map;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400254 size_t graph_size;
Josh Steadmonaa658572019-01-15 14:25:50 -0800255 struct commit_graph *ret;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400256
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100257 graph_size = xsize_t(st->st_size);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400258
259 if (graph_size < GRAPH_MIN_SIZE) {
260 close(fd);
Ævar Arnfjörð Bjarmason67a530f2019-03-25 13:08:31 +0100261 error(_("commit-graph file is too small"));
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100262 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400263 }
264 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
Jeff Kingc8828532020-04-23 15:41:13 -0600265 close(fd);
Taylor Blaua92d8522022-07-14 14:43:06 -0700266 prepare_repo_settings(r);
267 ret = parse_commit_graph(&r->settings, graph_map, graph_size);
Josh Steadmonaa658572019-01-15 14:25:50 -0800268
Taylor Blaua7df60c2020-02-03 13:18:04 -0800269 if (ret)
270 ret->odb = odb;
Jeff Kingc8828532020-04-23 15:41:13 -0600271 else
Josh Steadmonaa658572019-01-15 14:25:50 -0800272 munmap(graph_map, graph_size);
Josh Steadmonaa658572019-01-15 14:25:50 -0800273
274 return ret;
275}
276
Jeff King06fb1352023-11-09 02:25:07 -0500277static int graph_read_oid_fanout(const unsigned char *chunk_start,
278 size_t chunk_size, void *data)
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +0100279{
Jeff King06fb1352023-11-09 02:25:07 -0500280 struct commit_graph *g = data;
Jeff King4169d892023-10-09 17:04:58 -0400281 int i;
282
Jeff King06fb1352023-11-09 02:25:07 -0500283 if (chunk_size != 256 * sizeof(uint32_t))
Jeff Kinge0203912023-11-09 02:26:28 -0500284 return error(_("commit-graph oid fanout chunk is wrong size"));
Jeff King06fb1352023-11-09 02:25:07 -0500285 g->chunk_oid_fanout = (const uint32_t *)chunk_start;
286 g->num_commits = ntohl(g->chunk_oid_fanout[255]);
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +0100287
Jeff King4169d892023-10-09 17:04:58 -0400288 for (i = 0; i < 255; i++) {
289 uint32_t oid_fanout1 = ntohl(g->chunk_oid_fanout[i]);
290 uint32_t oid_fanout2 = ntohl(g->chunk_oid_fanout[i + 1]);
291
292 if (oid_fanout1 > oid_fanout2) {
Jeff Kinge0203912023-11-09 02:26:28 -0500293 error(_("commit-graph fanout values out of order"));
Jeff King4169d892023-10-09 17:04:58 -0400294 return 1;
295 }
296 }
Jeff King4169d892023-10-09 17:04:58 -0400297
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +0100298 return 0;
299}
300
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000301static int graph_read_oid_lookup(const unsigned char *chunk_start,
302 size_t chunk_size, void *data)
303{
304 struct commit_graph *g = data;
305 g->chunk_oid_lookup = chunk_start;
Jeff Kingd3b6f6c2023-11-09 02:24:35 -0500306 if (chunk_size / g->hash_len != g->num_commits)
307 return error(_("commit-graph OID lookup chunk is the wrong size"));
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000308 return 0;
309}
310
Jeff Kingb72df612023-10-09 17:05:36 -0400311static int graph_read_commit_data(const unsigned char *chunk_start,
312 size_t chunk_size, void *data)
313{
314 struct commit_graph *g = data;
Jeff King4bc6d432023-11-09 02:09:48 -0500315 if (chunk_size / GRAPH_DATA_WIDTH != g->num_commits)
Jeff Kinge0203912023-11-09 02:26:28 -0500316 return error(_("commit-graph commit data chunk is wrong size"));
Jeff Kingb72df612023-10-09 17:05:36 -0400317 g->chunk_commit_data = chunk_start;
318 return 0;
319}
320
Jeff King4a3c3462023-10-09 17:05:44 -0400321static int graph_read_generation_data(const unsigned char *chunk_start,
322 size_t chunk_size, void *data)
323{
324 struct commit_graph *g = data;
Jeff King4bc6d432023-11-09 02:09:48 -0500325 if (chunk_size / sizeof(uint32_t) != g->num_commits)
Jeff Kinge0203912023-11-09 02:26:28 -0500326 return error(_("commit-graph generations chunk is wrong size"));
Jeff King4a3c3462023-10-09 17:05:44 -0400327 g->chunk_generation_data = chunk_start;
328 return 0;
329}
330
Jeff King581e0f82023-10-09 17:05:53 -0400331static int graph_read_bloom_index(const unsigned char *chunk_start,
332 size_t chunk_size, void *data)
333{
334 struct commit_graph *g = data;
Jeff King4bc6d432023-11-09 02:09:48 -0500335 if (chunk_size / 4 != g->num_commits) {
Jeff Kinge0203912023-11-09 02:26:28 -0500336 warning(_("commit-graph changed-path index chunk is too small"));
Jeff King581e0f82023-10-09 17:05:53 -0400337 return -1;
338 }
339 g->chunk_bloom_indexes = chunk_start;
340 return 0;
341}
342
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000343static int graph_read_bloom_data(const unsigned char *chunk_start,
344 size_t chunk_size, void *data)
345{
346 struct commit_graph *g = data;
347 uint32_t hash_version;
Jeff King920f4002023-10-09 17:05:50 -0400348
349 if (chunk_size < BLOOMDATA_CHUNK_HEADER_SIZE) {
Jeff Kinge0203912023-11-09 02:26:28 -0500350 warning(_("ignoring too-small changed-path chunk"
351 " (%"PRIuMAX" < %"PRIuMAX") in commit-graph file"),
Jeff King920f4002023-10-09 17:05:50 -0400352 (uintmax_t)chunk_size,
353 (uintmax_t)BLOOMDATA_CHUNK_HEADER_SIZE);
354 return -1;
355 }
356
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000357 g->chunk_bloom_data = chunk_start;
Jeff King920f4002023-10-09 17:05:50 -0400358 g->chunk_bloom_data_size = chunk_size;
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000359 hash_version = get_be32(chunk_start);
360
361 if (hash_version != 1)
362 return 0;
363
364 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
365 g->bloom_filter_settings->hash_version = hash_version;
366 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
367 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
368 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
369
370 return 0;
371}
372
Taylor Blaua92d8522022-07-14 14:43:06 -0700373struct commit_graph *parse_commit_graph(struct repo_settings *s,
Taylor Blauab14d062020-09-09 11:22:56 -0400374 void *graph_map, size_t graph_size)
Josh Steadmonaa658572019-01-15 14:25:50 -0800375{
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000376 const unsigned char *data;
Josh Steadmonaa658572019-01-15 14:25:50 -0800377 struct commit_graph *graph;
Josh Steadmonaa658572019-01-15 14:25:50 -0800378 uint32_t graph_signature;
379 unsigned char graph_version, hash_version;
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000380 struct chunkfile *cf = NULL;
Josh Steadmonaa658572019-01-15 14:25:50 -0800381
382 if (!graph_map)
383 return NULL;
384
385 if (graph_size < GRAPH_MIN_SIZE)
386 return NULL;
387
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400388 data = (const unsigned char *)graph_map;
389
390 graph_signature = get_be32(data);
391 if (graph_signature != GRAPH_SIGNATURE) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100392 error(_("commit-graph signature %X does not match signature %X"),
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400393 graph_signature, GRAPH_SIGNATURE);
Josh Steadmonaa658572019-01-15 14:25:50 -0800394 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400395 }
396
397 graph_version = *(unsigned char*)(data + 4);
398 if (graph_version != GRAPH_VERSION) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100399 error(_("commit-graph version %X does not match version %X"),
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400400 graph_version, GRAPH_VERSION);
Josh Steadmonaa658572019-01-15 14:25:50 -0800401 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400402 }
403
404 hash_version = *(unsigned char*)(data + 5);
Taylor Blaud9fef9d2022-05-20 19:17:41 -0400405 if (hash_version != oid_version(the_hash_algo)) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100406 error(_("commit-graph hash version %X does not match version %X"),
Taylor Blaud9fef9d2022-05-20 19:17:41 -0400407 hash_version, oid_version(the_hash_algo));
Josh Steadmonaa658572019-01-15 14:25:50 -0800408 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400409 }
410
411 graph = alloc_commit_graph();
412
brian m. carlsonc1665992018-11-14 04:09:35 +0000413 graph->hash_len = the_hash_algo->rawsz;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400414 graph->num_chunks = *(unsigned char*)(data + 6);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400415 graph->data = graph_map;
416 graph->data_len = graph_size;
417
SZEDER Gábor2ad4f1a2020-06-05 13:00:29 +0000418 if (graph_size < GRAPH_HEADER_SIZE +
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000419 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
SZEDER Gábor2ad4f1a2020-06-05 13:00:29 +0000420 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
421 error(_("commit-graph file is too small to hold %u chunks"),
422 graph->num_chunks);
423 free(graph);
424 return NULL;
425 }
426
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000427 cf = init_chunkfile(NULL);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400428
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000429 if (read_table_of_contents(cf, graph->data, graph_size,
Jeff Kingc9b9fef2023-10-09 17:05:23 -0400430 GRAPH_HEADER_SIZE, graph->num_chunks, 1))
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000431 goto free_and_return;
Josh Steadmond2b86fb2019-01-15 14:25:51 -0800432
Jeff King8bd40ed2023-11-09 02:17:11 -0500433 if (read_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, graph_read_oid_fanout, graph)) {
434 error(_("commit-graph required OID fanout chunk missing or corrupted"));
435 goto free_and_return;
436 }
437 if (read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph)) {
438 error(_("commit-graph required OID lookup chunk missing or corrupted"));
439 goto free_and_return;
440 }
441 if (read_chunk(cf, GRAPH_CHUNKID_DATA, graph_read_commit_data, graph)) {
442 error(_("commit-graph required commit data chunk missing or corrupted"));
443 goto free_and_return;
444 }
445
Jeff King96226102023-10-09 17:05:38 -0400446 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges,
447 &graph->chunk_extra_edges_size);
Jeff King6cf61d02023-10-09 17:05:41 -0400448 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs,
449 &graph->chunk_base_graphs_size);
Derrick Stolee702110a2021-02-25 18:19:43 +0000450
Taylor Blaua92d8522022-07-14 14:43:06 -0700451 if (s->commit_graph_generation_version >= 2) {
Jeff King4a3c3462023-10-09 17:05:44 -0400452 read_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
453 graph_read_generation_data, graph);
Derrick Stolee702110a2021-02-25 18:19:43 +0000454 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
Jeff Kingee6a7922023-10-09 17:05:47 -0400455 &graph->chunk_generation_data_overflow,
456 &graph->chunk_generation_data_overflow_size);
Derrick Stolee3b0199d2022-03-01 19:48:31 +0000457
458 if (graph->chunk_generation_data)
459 graph->read_generation_data = 1;
Derrick Stolee702110a2021-02-25 18:19:43 +0000460 }
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400461
Taylor Blaua92d8522022-07-14 14:43:06 -0700462 if (s->commit_graph_read_changed_paths) {
Jeff King581e0f82023-10-09 17:05:53 -0400463 read_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
464 graph_read_bloom_index, graph);
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000465 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
466 graph_read_bloom_data, graph);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400467 }
468
Garima Singh76ffbca2020-04-06 16:59:49 +0000469 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
470 init_bloom_filters();
471 } else {
472 /* We need both the bloom chunks to exist together. Else ignore the data */
473 graph->chunk_bloom_indexes = NULL;
474 graph->chunk_bloom_data = NULL;
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700475 FREE_AND_NULL(graph->bloom_filter_settings);
Garima Singh76ffbca2020-04-06 16:59:49 +0000476 }
477
brian m. carlson92e2cab2021-04-26 01:02:50 +0000478 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
Derrick Stolee118bd572019-06-18 11:14:26 -0700479
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000480 free_chunkfile(cf);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400481 return graph;
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700482
483free_and_return:
Derrick Stolee2692c2f2021-02-18 14:07:35 +0000484 free_chunkfile(cf);
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700485 free(graph->bloom_filter_settings);
486 free(graph);
487 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400488}
489
Taylor Blauab14d062020-09-09 11:22:56 -0400490static struct commit_graph *load_commit_graph_one(struct repository *r,
491 const char *graph_file,
Taylor Blaua7df60c2020-02-03 13:18:04 -0800492 struct object_directory *odb)
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100493{
494
495 struct stat st;
496 int fd;
Derrick Stolee6c622f92019-06-18 11:14:27 -0700497 struct commit_graph *g;
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100498 int open_ok = open_commit_graph(graph_file, &fd, &st);
499
500 if (!open_ok)
501 return NULL;
502
Taylor Blauab14d062020-09-09 11:22:56 -0400503 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -0700504
505 if (g)
506 g->filename = xstrdup(graph_file);
507
508 return g;
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100509}
510
Taylor Blau13c24992020-02-03 13:18:00 -0800511static struct commit_graph *load_commit_graph_v1(struct repository *r,
512 struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700513{
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800514 char *graph_name = get_commit_graph_filename(odb);
Taylor Blauab14d062020-09-09 11:22:56 -0400515 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700516 free(graph_name);
517
518 return g;
519}
520
Jeff King8298b542023-09-28 00:38:17 -0400521/*
522 * returns 1 if and only if all graphs in the chain have
523 * corrected commit dates stored in the generation_data chunk.
524 */
525static int validate_mixed_generation_chain(struct commit_graph *g)
526{
527 int read_generation_data = 1;
528 struct commit_graph *p = g;
529
530 while (read_generation_data && p) {
531 read_generation_data = p->read_generation_data;
532 p = p->base_graph;
533 }
534
535 if (read_generation_data)
536 return 1;
537
538 while (g) {
539 g->read_generation_data = 0;
540 g = g->base_graph;
541 }
542
543 return 0;
544}
545
Derrick Stolee5c84b332019-06-18 11:14:25 -0700546static int add_graph_to_chain(struct commit_graph *g,
547 struct commit_graph *chain,
548 struct object_id *oids,
549 int n)
550{
551 struct commit_graph *cur_g = chain;
552
Derrick Stolee118bd572019-06-18 11:14:26 -0700553 if (n && !g->chunk_base_graphs) {
554 warning(_("commit-graph has no base graphs chunk"));
555 return 0;
556 }
557
Jeff King6cf61d02023-10-09 17:05:41 -0400558 if (g->chunk_base_graphs_size / g->hash_len < n) {
559 warning(_("commit-graph base graphs chunk is too small"));
560 return 0;
561 }
562
Derrick Stolee5c84b332019-06-18 11:14:25 -0700563 while (n) {
564 n--;
Derrick Stolee118bd572019-06-18 11:14:26 -0700565
566 if (!cur_g ||
567 !oideq(&oids[n], &cur_g->oid) ||
Taylor Blau209250e2023-07-12 19:37:57 -0400568 !hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_len, n))) {
Derrick Stolee118bd572019-06-18 11:14:26 -0700569 warning(_("commit-graph chain does not match"));
570 return 0;
571 }
572
Derrick Stolee5c84b332019-06-18 11:14:25 -0700573 cur_g = cur_g->base_graph;
574 }
575
Taylor Blau209250e2023-07-12 19:37:57 -0400576 if (chain) {
577 if (unsigned_add_overflows(chain->num_commits,
578 chain->num_commits_in_base)) {
579 warning(_("commit count in base graph too high: %"PRIuMAX),
580 (uintmax_t)chain->num_commits_in_base);
581 return 0;
582 }
Derrick Stolee5c84b332019-06-18 11:14:25 -0700583 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
Taylor Blau209250e2023-07-12 19:37:57 -0400584 }
Derrick Stolee5c84b332019-06-18 11:14:25 -0700585
Jeff King991d5492023-10-03 16:30:04 -0400586 g->base_graph = chain;
587
Derrick Stolee5c84b332019-06-18 11:14:25 -0700588 return 1;
589}
590
Jeff King7ed76b42023-09-28 00:38:07 -0400591int open_commit_graph_chain(const char *chain_file,
592 int *fd, struct stat *st)
593{
594 *fd = git_open(chain_file);
595 if (*fd < 0)
596 return 0;
597 if (fstat(*fd, st)) {
598 close(*fd);
599 return 0;
600 }
Jeff King7754a562023-09-28 00:39:10 -0400601 if (st->st_size < the_hash_algo->hexsz) {
Jeff King7ed76b42023-09-28 00:38:07 -0400602 close(*fd);
Jeff King7754a562023-09-28 00:39:10 -0400603 if (!st->st_size) {
604 /* treat empty files the same as missing */
605 errno = ENOENT;
606 } else {
Jeff Kinge0203912023-11-09 02:26:28 -0500607 warning(_("commit-graph chain file too small"));
Jeff King7754a562023-09-28 00:39:10 -0400608 errno = EINVAL;
609 }
Jeff King7ed76b42023-09-28 00:38:07 -0400610 return 0;
611 }
612 return 1;
613}
614
615struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
Jeff King5f259192023-09-28 00:39:51 -0400616 int fd, struct stat *st,
617 int *incomplete_chain)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700618{
619 struct commit_graph *graph_chain = NULL;
620 struct strbuf line = STRBUF_INIT;
Derrick Stolee5c84b332019-06-18 11:14:25 -0700621 struct object_id *oids;
622 int i = 0, valid = 1, count;
Jeff King7ed76b42023-09-28 00:38:07 -0400623 FILE *fp = xfdopen(fd, "r");
Derrick Stolee5c84b332019-06-18 11:14:25 -0700624
Jeff King7ed76b42023-09-28 00:38:07 -0400625 count = st->st_size / (the_hash_algo->hexsz + 1);
René Scharfeca56dad2021-03-13 17:17:22 +0100626 CALLOC_ARRAY(oids, count);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700627
Derrick Stoleec5230352019-06-18 11:14:30 -0700628 prepare_alt_odb(r);
629
630 for (i = 0; i < count; i++) {
631 struct object_directory *odb;
Derrick Stolee5c84b332019-06-18 11:14:25 -0700632
633 if (strbuf_getline_lf(&line, fp) == EOF)
634 break;
635
636 if (get_oid_hex(line.buf, &oids[i])) {
637 warning(_("invalid commit-graph chain: line '%s' not a hash"),
638 line.buf);
639 valid = 0;
640 break;
641 }
642
Derrick Stoleec5230352019-06-18 11:14:30 -0700643 valid = 0;
644 for (odb = r->objects->odb; odb; odb = odb->next) {
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800645 char *graph_name = get_split_graph_filename(odb, line.buf);
Taylor Blauab14d062020-09-09 11:22:56 -0400646 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700647
Derrick Stoleec5230352019-06-18 11:14:30 -0700648 free(graph_name);
649
650 if (g) {
Derrick Stoleec5230352019-06-18 11:14:30 -0700651 if (add_graph_to_chain(g, graph_chain, oids, i)) {
652 graph_chain = g;
653 valid = 1;
Jeff King1d94abf2023-10-03 16:30:44 -0400654 } else {
655 free_commit_graph(g);
Derrick Stoleec5230352019-06-18 11:14:30 -0700656 }
657
658 break;
659 }
660 }
661
662 if (!valid) {
663 warning(_("unable to find all commit-graph files"));
664 break;
665 }
Derrick Stolee5c84b332019-06-18 11:14:25 -0700666 }
667
Jeff King8298b542023-09-28 00:38:17 -0400668 validate_mixed_generation_chain(graph_chain);
669
Derrick Stolee5c84b332019-06-18 11:14:25 -0700670 free(oids);
671 fclose(fp);
René Scharfe0aa6bce2019-08-07 13:15:02 +0200672 strbuf_release(&line);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700673
Jeff King5f259192023-09-28 00:39:51 -0400674 *incomplete_chain = !valid;
Derrick Stolee5c84b332019-06-18 11:14:25 -0700675 return graph_chain;
676}
677
Jeff King7ed76b42023-09-28 00:38:07 -0400678static struct commit_graph *load_commit_graph_chain(struct repository *r,
679 struct object_directory *odb)
680{
681 char *chain_file = get_commit_graph_chain_filename(odb);
682 struct stat st;
683 int fd;
684 struct commit_graph *g = NULL;
685
686 if (open_commit_graph_chain(chain_file, &fd, &st)) {
Jeff King5f259192023-09-28 00:39:51 -0400687 int incomplete;
Jeff King7ed76b42023-09-28 00:38:07 -0400688 /* ownership of fd is taken over by load function */
Jeff King5f259192023-09-28 00:39:51 -0400689 g = load_commit_graph_chain_fd_st(r, fd, &st, &incomplete);
Jeff King7ed76b42023-09-28 00:38:07 -0400690 }
691
692 free(chain_file);
693 return g;
694}
695
Taylor Blau13c24992020-02-03 13:18:00 -0800696struct commit_graph *read_commit_graph_one(struct repository *r,
697 struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700698{
Taylor Blau13c24992020-02-03 13:18:00 -0800699 struct commit_graph *g = load_commit_graph_v1(r, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700700
701 if (!g)
Taylor Blau13c24992020-02-03 13:18:00 -0800702 g = load_commit_graph_chain(r, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700703
704 return g;
Jonathan Tan5faf3572018-07-11 15:42:37 -0700705}
706
Taylor Blau13c24992020-02-03 13:18:00 -0800707static void prepare_commit_graph_one(struct repository *r,
708 struct object_directory *odb)
Derrick Stolee177722b2018-04-10 08:56:05 -0400709{
Derrick Stolee177722b2018-04-10 08:56:05 -0400710
711 if (r->objects->commit_graph)
712 return;
713
Taylor Blau13c24992020-02-03 13:18:00 -0800714 r->objects->commit_graph = read_commit_graph_one(r, odb);
Derrick Stolee177722b2018-04-10 08:56:05 -0400715}
Jonathan Tan5faf3572018-07-11 15:42:37 -0700716
717/*
718 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
719 *
Elijah Newren15beaaa2019-11-05 17:07:23 +0000720 * On the first invocation, this function attempts to load the commit
Jonathan Tan5faf3572018-07-11 15:42:37 -0700721 * graph if the_repository is configured to have one.
722 */
Jonathan Tandade47c2018-07-11 15:42:42 -0700723static int prepare_commit_graph(struct repository *r)
Derrick Stolee177722b2018-04-10 08:56:05 -0400724{
Jeff King263db402018-11-12 09:48:47 -0500725 struct object_directory *odb;
Derrick Stolee177722b2018-04-10 08:56:05 -0400726
Jeff King6abada12019-09-12 10:44:45 -0400727 /*
Lessley Dennington0803f9c2021-12-06 15:55:56 +0000728 * Early return if there is no git dir or if the commit graph is
729 * disabled.
730 *
Jeff King6abada12019-09-12 10:44:45 -0400731 * This must come before the "already attempted?" check below, because
732 * we want to disable even an already-loaded graph file.
733 */
Lessley Dennington0803f9c2021-12-06 15:55:56 +0000734 if (!r->gitdir || r->commit_graph_disabled)
Jeff King6abada12019-09-12 10:44:45 -0400735 return 0;
Ævar Arnfjörð Bjarmason43d35612019-03-25 13:08:33 +0100736
Jonathan Tandade47c2018-07-11 15:42:42 -0700737 if (r->objects->commit_graph_attempted)
738 return !!r->objects->commit_graph;
739 r->objects->commit_graph_attempted = 1;
Derrick Stolee177722b2018-04-10 08:56:05 -0400740
Derrick Stolee7211b9e2019-08-13 11:37:43 -0700741 prepare_repo_settings(r);
742
Derrick Stolee859fdc02018-08-29 05:49:04 -0700743 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
Derrick Stolee7211b9e2019-08-13 11:37:43 -0700744 r->settings.core_commit_graph != 1)
Jonathan Tandade47c2018-07-11 15:42:42 -0700745 /*
746 * This repository is not configured to use commit graphs, so
747 * do not load one. (But report commit_graph_attempted anyway
748 * so that commit graph loading is not attempted again for this
749 * repository.)
750 */
Jonathan Tan5faf3572018-07-11 15:42:37 -0700751 return 0;
752
Derrick Stoleed6538242018-08-20 18:24:27 +0000753 if (!commit_graph_compatible(r))
754 return 0;
755
Jonathan Tandade47c2018-07-11 15:42:42 -0700756 prepare_alt_odb(r);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500757 for (odb = r->objects->odb;
Jeff King263db402018-11-12 09:48:47 -0500758 !r->objects->commit_graph && odb;
759 odb = odb->next)
Taylor Blau13c24992020-02-03 13:18:00 -0800760 prepare_commit_graph_one(r, odb);
Jonathan Tandade47c2018-07-11 15:42:42 -0700761 return !!r->objects->commit_graph;
Derrick Stolee177722b2018-04-10 08:56:05 -0400762}
763
Derrick Stolee6cc01742018-07-20 16:33:30 +0000764int generation_numbers_enabled(struct repository *r)
765{
766 uint32_t first_generation;
767 struct commit_graph *g;
768 if (!prepare_commit_graph(r))
769 return 0;
770
771 g = r->objects->commit_graph;
772
773 if (!g->num_commits)
774 return 0;
775
776 first_generation = get_be32(g->chunk_commit_data +
777 g->hash_len + 8) >> 2;
778
779 return !!first_generation;
780}
781
Abhishek Kumar8d00d7c2021-01-16 18:11:17 +0000782int corrected_commit_dates_enabled(struct repository *r)
783{
784 struct commit_graph *g;
785 if (!prepare_commit_graph(r))
786 return 0;
787
788 g = r->objects->commit_graph;
789
790 if (!g->num_commits)
791 return 0;
792
793 return g->read_generation_data;
794}
795
Taylor Blau4f364402020-09-09 11:22:44 -0400796struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
797{
798 struct commit_graph *g = r->objects->commit_graph;
799 while (g) {
800 if (g->bloom_filter_settings)
801 return g->bloom_filter_settings;
802 g = g->base_graph;
803 }
804 return NULL;
805}
806
Derrick Stoleec3a3a962019-05-17 11:41:47 -0700807void close_commit_graph(struct raw_object_store *o)
Derrick Stolee177722b2018-04-10 08:56:05 -0400808{
Jeff Kingd70f5542024-01-05 00:41:42 -0500809 if (!o->commit_graph)
810 return;
811
Jeff Kingac6d45d2023-10-03 16:27:52 -0400812 clear_commit_graph_data_slab(&commit_graph_data_slab);
Jeff King09a75ab2023-10-03 16:29:30 -0400813 free_commit_graph(o->commit_graph);
Derrick Stoleec3a3a962019-05-17 11:41:47 -0700814 o->commit_graph = NULL;
Derrick Stolee177722b2018-04-10 08:56:05 -0400815}
816
Patrick Steinhardt809ea282021-08-09 10:11:59 +0200817static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
Derrick Stolee177722b2018-04-10 08:56:05 -0400818{
819 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
820 g->chunk_oid_lookup, g->hash_len, pos);
821}
822
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700823static void load_oid_from_graph(struct commit_graph *g,
824 uint32_t pos,
825 struct object_id *oid)
826{
827 uint32_t lex_index;
828
829 while (g && pos < g->num_commits_in_base)
830 g = g->base_graph;
831
832 if (!g)
833 BUG("NULL commit-graph");
834
835 if (pos >= g->num_commits + g->num_commits_in_base)
836 die(_("invalid commit position. commit-graph is likely corrupt"));
837
838 lex_index = pos - g->num_commits_in_base;
839
Taylor Blau0bd8f302023-07-12 19:38:00 -0400840 oidread(oid, g->chunk_oid_lookup + st_mult(g->hash_len, lex_index));
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700841}
842
Stefan Beller4f542b72018-12-14 16:09:39 -0800843static struct commit_list **insert_parent_or_die(struct repository *r,
844 struct commit_graph *g,
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700845 uint32_t pos,
Derrick Stolee177722b2018-04-10 08:56:05 -0400846 struct commit_list **pptr)
847{
848 struct commit *c;
849 struct object_id oid;
Derrick Stolee96af91d2018-06-27 09:24:36 -0400850
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700851 if (pos >= g->num_commits + g->num_commits_in_base)
852 die("invalid parent position %"PRIu32, pos);
Derrick Stolee53614b12018-06-27 09:24:38 -0400853
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700854 load_oid_from_graph(g, pos, &oid);
Stefan Beller4f542b72018-12-14 16:09:39 -0800855 c = lookup_commit(r, &oid);
Derrick Stolee177722b2018-04-10 08:56:05 -0400856 if (!c)
Nguyễn Thái Ngọc Duy4f5b5322018-07-21 09:49:26 +0200857 die(_("could not find commit %s"), oid_to_hex(&oid));
Abhishek Kumarc49c82a2020-06-17 14:44:10 +0530858 commit_graph_data_at(c)->graph_pos = pos;
Derrick Stolee177722b2018-04-10 08:56:05 -0400859 return &commit_list_insert(c, pptr)->next;
860}
861
Derrick Stoleee2838d82018-05-01 12:47:13 +0000862static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
863{
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700864 const unsigned char *commit_data;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530865 struct commit_graph_data *graph_data;
Abhishek Kumare8b63002021-01-16 18:11:15 +0000866 uint32_t lex_index, offset_pos;
867 uint64_t date_high, date_low, offset;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700868
869 while (pos < g->num_commits_in_base)
870 g = g->base_graph;
871
Abhishek Kumarf90fca62021-01-16 18:11:10 +0000872 if (pos >= g->num_commits + g->num_commits_in_base)
873 die(_("invalid commit position. commit-graph is likely corrupt"));
874
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700875 lex_index = pos - g->num_commits_in_base;
Taylor Blau2740ed12023-07-12 19:38:03 -0400876 commit_data = g->chunk_commit_data + st_mult(GRAPH_DATA_WIDTH, lex_index);
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530877
878 graph_data = commit_graph_data_at(item);
879 graph_data->graph_pos = pos;
Abhishek Kumarf90fca62021-01-16 18:11:10 +0000880
881 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
882 date_low = get_be32(commit_data + g->hash_len + 12);
883 item->date = (timestamp_t)((date_high << 32) | date_low);
884
Abhishek Kumar1fdc3832021-01-16 18:11:16 +0000885 if (g->read_generation_data) {
Taylor Blau2740ed12023-07-12 19:38:03 -0400886 offset = (timestamp_t)get_be32(g->chunk_generation_data + st_mult(sizeof(uint32_t), lex_index));
Abhishek Kumare8b63002021-01-16 18:11:15 +0000887
888 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
889 if (!g->chunk_generation_data_overflow)
890 die(_("commit-graph requires overflow generation data but has none"));
891
892 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
Jeff Kingee6a7922023-10-09 17:05:47 -0400893 if (g->chunk_generation_data_overflow_size / sizeof(uint64_t) <= offset_pos)
894 die(_("commit-graph overflow generation data is too small"));
895 graph_data->generation = item->date +
896 get_be64(g->chunk_generation_data_overflow + sizeof(uint64_t) * offset_pos);
Abhishek Kumare8b63002021-01-16 18:11:15 +0000897 } else
898 graph_data->generation = item->date + offset;
899 } else
900 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +0000901
902 if (g->topo_levels)
903 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
Derrick Stoleee2838d82018-05-01 12:47:13 +0000904}
905
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700906static inline void set_commit_tree(struct commit *c, struct tree *t)
907{
908 c->maybe_tree = t;
909}
910
Stefan Beller4f542b72018-12-14 16:09:39 -0800911static int fill_commit_in_graph(struct repository *r,
912 struct commit *item,
913 struct commit_graph *g, uint32_t pos)
Derrick Stolee177722b2018-04-10 08:56:05 -0400914{
Derrick Stolee177722b2018-04-10 08:56:05 -0400915 uint32_t edge_value;
Jeff King96226102023-10-09 17:05:38 -0400916 uint32_t parent_data_pos;
Derrick Stolee177722b2018-04-10 08:56:05 -0400917 struct commit_list **pptr;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700918 const unsigned char *commit_data;
919 uint32_t lex_index;
920
921 while (pos < g->num_commits_in_base)
922 g = g->base_graph;
923
Abhishek Kumarf90fca62021-01-16 18:11:10 +0000924 fill_commit_graph_info(item, g, pos);
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700925
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700926 lex_index = pos - g->num_commits_in_base;
Taylor Blau50a71c22023-07-12 19:38:05 -0400927 commit_data = g->chunk_commit_data + st_mult(g->hash_len + 16, lex_index);
Derrick Stolee177722b2018-04-10 08:56:05 -0400928
929 item->object.parsed = 1;
Derrick Stolee177722b2018-04-10 08:56:05 -0400930
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700931 set_commit_tree(item, NULL);
Derrick Stolee177722b2018-04-10 08:56:05 -0400932
Derrick Stolee177722b2018-04-10 08:56:05 -0400933 pptr = &item->parents;
934
935 edge_value = get_be32(commit_data + g->hash_len);
936 if (edge_value == GRAPH_PARENT_NONE)
937 return 1;
Stefan Beller4f542b72018-12-14 16:09:39 -0800938 pptr = insert_parent_or_die(r, g, edge_value, pptr);
Derrick Stolee177722b2018-04-10 08:56:05 -0400939
940 edge_value = get_be32(commit_data + g->hash_len + 4);
941 if (edge_value == GRAPH_PARENT_NONE)
942 return 1;
SZEDER Gábor5af74172019-01-19 21:21:13 +0100943 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
Stefan Beller4f542b72018-12-14 16:09:39 -0800944 pptr = insert_parent_or_die(r, g, edge_value, pptr);
Derrick Stolee177722b2018-04-10 08:56:05 -0400945 return 1;
946 }
947
Jeff King96226102023-10-09 17:05:38 -0400948 parent_data_pos = edge_value & GRAPH_EDGE_LAST_MASK;
Derrick Stolee177722b2018-04-10 08:56:05 -0400949 do {
Jeff King96226102023-10-09 17:05:38 -0400950 if (g->chunk_extra_edges_size / sizeof(uint32_t) <= parent_data_pos) {
Jeff Kinge0203912023-11-09 02:26:28 -0500951 error(_("commit-graph extra-edges pointer out of bounds"));
Jeff King96226102023-10-09 17:05:38 -0400952 free_commit_list(item->parents);
953 item->parents = NULL;
954 item->object.parsed = 0;
955 return 0;
956 }
957 edge_value = get_be32(g->chunk_extra_edges +
958 sizeof(uint32_t) * parent_data_pos);
Stefan Beller4f542b72018-12-14 16:09:39 -0800959 pptr = insert_parent_or_die(r, g,
Derrick Stolee177722b2018-04-10 08:56:05 -0400960 edge_value & GRAPH_EDGE_LAST_MASK,
961 pptr);
Jeff King96226102023-10-09 17:05:38 -0400962 parent_data_pos++;
Derrick Stolee177722b2018-04-10 08:56:05 -0400963 } while (!(edge_value & GRAPH_LAST_EDGE));
964
965 return 1;
966}
967
Patrick Steinhardt809ea282021-08-09 10:11:59 +0200968static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
969{
970 struct commit_graph *cur_g = g;
971 uint32_t lex_index;
972
973 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
974 cur_g = cur_g->base_graph;
975
976 if (cur_g) {
977 *pos = lex_index + cur_g->num_commits_in_base;
978 return 1;
979 }
980
981 return 0;
982}
983
984static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
Derrick Stoleee2838d82018-05-01 12:47:13 +0000985{
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530986 uint32_t graph_pos = commit_graph_position(item);
987 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
988 *pos = graph_pos;
Derrick Stoleee2838d82018-05-01 12:47:13 +0000989 return 1;
990 } else {
Patrick Steinhardt809ea282021-08-09 10:11:59 +0200991 return search_commit_pos_in_graph(&item->object.oid, g, pos);
Derrick Stoleee2838d82018-05-01 12:47:13 +0000992 }
993}
994
Taylor Blau78053602022-07-12 19:10:31 -0400995int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
996 uint32_t *pos)
997{
998 if (!prepare_commit_graph(r))
999 return 0;
1000 return find_commit_pos_in_graph(c, r->objects->commit_graph, pos);
1001}
1002
Patrick Steinhardtf559d6d2021-08-09 10:12:03 +02001003struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
1004{
Patrick Steinhardte04838e2023-10-31 08:16:13 +01001005 static int commit_graph_paranoia = -1;
Patrick Steinhardtf559d6d2021-08-09 10:12:03 +02001006 struct commit *commit;
1007 uint32_t pos;
1008
Patrick Steinhardte04838e2023-10-31 08:16:13 +01001009 if (commit_graph_paranoia == -1)
Patrick Steinhardtb1df3b32023-11-24 12:08:21 +01001010 commit_graph_paranoia = git_env_bool(GIT_COMMIT_GRAPH_PARANOIA, 0);
Patrick Steinhardte04838e2023-10-31 08:16:13 +01001011
Jeff Kingd6045292022-09-06 17:02:13 -04001012 if (!prepare_commit_graph(repo))
Patrick Steinhardtf559d6d2021-08-09 10:12:03 +02001013 return NULL;
1014 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
1015 return NULL;
Patrick Steinhardte04838e2023-10-31 08:16:13 +01001016 if (commit_graph_paranoia && !has_object(repo, id, 0))
Patrick Steinhardtf559d6d2021-08-09 10:12:03 +02001017 return NULL;
1018
1019 commit = lookup_commit(repo, id);
1020 if (!commit)
1021 return NULL;
1022 if (commit->object.parsed)
1023 return commit;
1024
1025 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
1026 return NULL;
1027
1028 return commit;
1029}
1030
Stefan Beller4f542b72018-12-14 16:09:39 -08001031static int parse_commit_in_graph_one(struct repository *r,
1032 struct commit_graph *g,
1033 struct commit *item)
Derrick Stolee177722b2018-04-10 08:56:05 -04001034{
Derrick Stoleee2838d82018-05-01 12:47:13 +00001035 uint32_t pos;
1036
Derrick Stolee177722b2018-04-10 08:56:05 -04001037 if (item->object.parsed)
1038 return 1;
Derrick Stoleeee797052018-06-27 09:24:29 -04001039
Patrick Steinhardt809ea282021-08-09 10:11:59 +02001040 if (find_commit_pos_in_graph(item, g, &pos))
Stefan Beller4f542b72018-12-14 16:09:39 -08001041 return fill_commit_in_graph(r, item, g, pos);
Derrick Stoleeee797052018-06-27 09:24:29 -04001042
1043 return 0;
1044}
1045
Jonathan Tandade47c2018-07-11 15:42:42 -07001046int parse_commit_in_graph(struct repository *r, struct commit *item)
Derrick Stoleeee797052018-06-27 09:24:29 -04001047{
Derrick Stolee7b671f82020-06-23 17:47:01 +00001048 static int checked_env = 0;
1049
1050 if (!checked_env &&
1051 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
1052 die("dying as requested by the '%s' variable on commit-graph parse!",
1053 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
1054 checked_env = 1;
1055
Jonathan Tandade47c2018-07-11 15:42:42 -07001056 if (!prepare_commit_graph(r))
Derrick Stoleeee797052018-06-27 09:24:29 -04001057 return 0;
Stefan Beller4f542b72018-12-14 16:09:39 -08001058 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
Derrick Stolee177722b2018-04-10 08:56:05 -04001059}
1060
Jonathan Tandade47c2018-07-11 15:42:42 -07001061void load_commit_graph_info(struct repository *r, struct commit *item)
Derrick Stoleee2838d82018-05-01 12:47:13 +00001062{
1063 uint32_t pos;
Taylor Blau78053602022-07-12 19:10:31 -04001064 if (repo_find_commit_pos_in_graph(r, item, &pos))
Jonathan Tandade47c2018-07-11 15:42:42 -07001065 fill_commit_graph_info(item, r->objects->commit_graph, pos);
Derrick Stoleee2838d82018-05-01 12:47:13 +00001066}
1067
Stefan Beller4f542b72018-12-14 16:09:39 -08001068static struct tree *load_tree_for_commit(struct repository *r,
1069 struct commit_graph *g,
1070 struct commit *c)
Derrick Stolee7b8a21d2018-04-06 19:09:46 +00001071{
1072 struct object_id oid;
Derrick Stoleed4f4d602019-06-18 11:14:24 -07001073 const unsigned char *commit_data;
Abhishek Kumarc752ad02020-06-17 14:44:11 +05301074 uint32_t graph_pos = commit_graph_position(c);
Derrick Stoleed4f4d602019-06-18 11:14:24 -07001075
Abhishek Kumarc752ad02020-06-17 14:44:11 +05301076 while (graph_pos < g->num_commits_in_base)
Derrick Stoleed4f4d602019-06-18 11:14:24 -07001077 g = g->base_graph;
1078
1079 commit_data = g->chunk_commit_data +
Taylor Blau51c31a62023-07-12 19:38:08 -04001080 st_mult(GRAPH_DATA_WIDTH, graph_pos - g->num_commits_in_base);
Derrick Stolee7b8a21d2018-04-06 19:09:46 +00001081
brian m. carlson92e2cab2021-04-26 01:02:50 +00001082 oidread(&oid, commit_data);
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +07001083 set_commit_tree(c, lookup_tree(r, &oid));
Derrick Stolee7b8a21d2018-04-06 19:09:46 +00001084
1085 return c->maybe_tree;
1086}
1087
Stefan Beller4f542b72018-12-14 16:09:39 -08001088static struct tree *get_commit_tree_in_graph_one(struct repository *r,
1089 struct commit_graph *g,
Derrick Stolee0cbef8f2018-06-27 09:24:31 -04001090 const struct commit *c)
Derrick Stolee7b8a21d2018-04-06 19:09:46 +00001091{
1092 if (c->maybe_tree)
1093 return c->maybe_tree;
Abhishek Kumarc49c82a2020-06-17 14:44:10 +05301094 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
Derrick Stolee0cbef8f2018-06-27 09:24:31 -04001095 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
Derrick Stolee7b8a21d2018-04-06 19:09:46 +00001096
Stefan Beller4f542b72018-12-14 16:09:39 -08001097 return load_tree_for_commit(r, g, (struct commit *)c);
Derrick Stolee0cbef8f2018-06-27 09:24:31 -04001098}
1099
Jonathan Tandade47c2018-07-11 15:42:42 -07001100struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
Derrick Stolee0cbef8f2018-06-27 09:24:31 -04001101{
Stefan Beller4f542b72018-12-14 16:09:39 -08001102 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
Derrick Stolee7b8a21d2018-04-06 19:09:46 +00001103}
1104
Derrick Stoleec9905be2019-06-12 06:29:40 -07001105struct packed_commit_list {
1106 struct commit **list;
Jeff King33613902020-12-07 14:11:08 -05001107 size_t nr;
1108 size_t alloc;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001109};
1110
Derrick Stoleec9905be2019-06-12 06:29:40 -07001111struct write_commit_graph_context {
1112 struct repository *r;
Taylor Blau0bd52e22020-02-03 21:51:50 -08001113 struct object_directory *odb;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001114 char *graph_name;
Jeff Kinga5f1c442020-12-07 14:11:05 -05001115 struct oid_array oids;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001116 struct packed_commit_list commits;
1117 int num_extra_edges;
Abhishek Kumare8b63002021-01-16 18:11:15 +00001118 int num_generation_data_overflows;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001119 unsigned long approx_nr_objects;
1120 struct progress *progress;
1121 int progress_done;
1122 uint64_t progress_cnt;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001123
1124 char *base_graph_name;
1125 int num_commit_graphs_before;
1126 int num_commit_graphs_after;
1127 char **commit_graph_filenames_before;
1128 char **commit_graph_filenames_after;
1129 char **commit_graph_hash_after;
1130 uint32_t new_num_commits_in_base;
1131 struct commit_graph *new_base_graph;
1132
Derrick Stoleec9905be2019-06-12 06:29:40 -07001133 unsigned append:1,
Derrick Stolee6c622f92019-06-18 11:14:27 -07001134 report_progress:1,
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001135 split:1,
Garima Singh3d112752020-03-30 00:31:30 +00001136 changed_paths:1,
Abhishek Kumare8b63002021-01-16 18:11:15 +00001137 order_by_pack:1,
Derrick Stoleefde55b02021-02-02 03:01:22 +00001138 write_generation_data:1,
1139 trust_generation_numbers:1;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001140
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +00001141 struct topo_level_slab *topo_levels;
Taylor Blau98bb7962020-09-17 22:59:49 -04001142 const struct commit_graph_opts *opts;
Garima Singhf97b9322020-03-30 00:31:28 +00001143 size_t total_bloom_filter_data_size;
Derrick Stolee98037f22020-06-23 17:47:00 +00001144 const struct bloom_filter_settings *bloom_settings;
Taylor Blau312cff52020-09-16 14:07:32 -04001145
1146 int count_bloom_filter_computed;
1147 int count_bloom_filter_not_computed;
Taylor Blau59f0d502020-09-17 22:59:44 -04001148 int count_bloom_filter_trunc_empty;
Taylor Blau312cff52020-09-16 14:07:32 -04001149 int count_bloom_filter_trunc_large;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001150};
1151
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001152static int write_graph_chunk_fanout(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001153 void *data)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001154{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001155 struct write_commit_graph_context *ctx = data;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001156 int i, count = 0;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001157 struct commit **list = ctx->commits.list;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001158
1159 /*
1160 * Write the first-level table (the list is sorted,
1161 * but we use a 256-entry lookup to be able to avoid
1162 * having to do eight extra binary search iterations).
1163 */
1164 for (i = 0; i < 256; i++) {
Derrick Stoleec9905be2019-06-12 06:29:40 -07001165 while (count < ctx->commits.nr) {
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001166 if ((*list)->object.oid.hash[0] != i)
1167 break;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001168 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001169 count++;
1170 list++;
1171 }
1172
1173 hashwrite_be32(f, count);
1174 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001175
1176 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001177}
1178
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001179static int write_graph_chunk_oids(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001180 void *data)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001181{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001182 struct write_commit_graph_context *ctx = data;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001183 struct commit **list = ctx->commits.list;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001184 int count;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001185 for (count = 0; count < ctx->commits.nr; count++, list++) {
1186 display_progress(ctx->progress, ++ctx->progress_cnt);
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001187 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +01001188 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001189
1190 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001191}
1192
Jeff King8380dcd2021-01-28 01:20:23 -05001193static const struct object_id *commit_to_oid(size_t index, const void *table)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001194{
Jeff King8380dcd2021-01-28 01:20:23 -05001195 const struct commit * const *commits = table;
Jeff King45ee13b2021-01-28 01:19:42 -05001196 return &commits[index]->object.oid;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001197}
1198
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001199static int write_graph_chunk_data(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001200 void *data)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001201{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001202 struct write_commit_graph_context *ctx = data;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001203 struct commit **list = ctx->commits.list;
1204 struct commit **last = ctx->commits.list + ctx->commits.nr;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001205 uint32_t num_extra_edges = 0;
1206
1207 while (list < last) {
1208 struct commit_list *parent;
Taylor Blau806278d2019-09-05 18:04:57 -04001209 struct object_id *tree;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001210 int edge_value;
1211 uint32_t packedDate[2];
Derrick Stoleec9905be2019-06-12 06:29:40 -07001212 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001213
Derrick Stoleec4cc0832021-02-01 17:15:03 +00001214 if (repo_parse_commit_no_graph(ctx->r, *list))
Taylor Blau16749b82019-09-05 18:04:55 -04001215 die(_("unable to parse commit %s"),
1216 oid_to_hex(&(*list)->object.oid));
Taylor Blau806278d2019-09-05 18:04:57 -04001217 tree = get_commit_tree_oid(*list);
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001218 hashwrite(f, tree->hash, the_hash_algo->rawsz);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001219
1220 parent = (*list)->parents;
1221
1222 if (!parent)
1223 edge_value = GRAPH_PARENT_NONE;
1224 else {
Jeff King45ee13b2021-01-28 01:19:42 -05001225 edge_value = oid_pos(&parent->item->object.oid,
1226 ctx->commits.list,
1227 ctx->commits.nr,
1228 commit_to_oid);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001229
Derrick Stolee6c622f92019-06-18 11:14:27 -07001230 if (edge_value >= 0)
1231 edge_value += ctx->new_num_commits_in_base;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001232 else if (ctx->new_base_graph) {
Derrick Stolee6c622f92019-06-18 11:14:27 -07001233 uint32_t pos;
Patrick Steinhardt809ea282021-08-09 10:11:59 +02001234 if (find_commit_pos_in_graph(parent->item,
1235 ctx->new_base_graph,
1236 &pos))
Derrick Stolee6c622f92019-06-18 11:14:27 -07001237 edge_value = pos;
1238 }
1239
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001240 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -08001241 BUG("missing parent %s for commit %s",
1242 oid_to_hex(&parent->item->object.oid),
1243 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001244 }
1245
1246 hashwrite_be32(f, edge_value);
1247
1248 if (parent)
1249 parent = parent->next;
1250
1251 if (!parent)
1252 edge_value = GRAPH_PARENT_NONE;
1253 else if (parent->next)
SZEDER Gábor5af74172019-01-19 21:21:13 +01001254 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001255 else {
Jeff King45ee13b2021-01-28 01:19:42 -05001256 edge_value = oid_pos(&parent->item->object.oid,
1257 ctx->commits.list,
1258 ctx->commits.nr,
1259 commit_to_oid);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001260
1261 if (edge_value >= 0)
1262 edge_value += ctx->new_num_commits_in_base;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001263 else if (ctx->new_base_graph) {
Derrick Stolee6c622f92019-06-18 11:14:27 -07001264 uint32_t pos;
Patrick Steinhardt809ea282021-08-09 10:11:59 +02001265 if (find_commit_pos_in_graph(parent->item,
1266 ctx->new_base_graph,
1267 &pos))
Derrick Stolee6c622f92019-06-18 11:14:27 -07001268 edge_value = pos;
1269 }
1270
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001271 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -08001272 BUG("missing parent %s for commit %s",
1273 oid_to_hex(&parent->item->object.oid),
1274 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001275 }
1276
1277 hashwrite_be32(f, edge_value);
1278
SZEDER Gábor5af74172019-01-19 21:21:13 +01001279 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001280 do {
1281 num_extra_edges++;
1282 parent = parent->next;
1283 } while (parent);
1284 }
1285
1286 if (sizeof((*list)->date) > 4)
1287 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1288 else
1289 packedDate[0] = 0;
1290
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +00001291 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
Derrick Stolee3258c662018-05-01 12:47:09 +00001292
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001293 packedDate[1] = htonl((*list)->date);
1294 hashwrite(f, packedDate, 8);
1295
1296 list++;
1297 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001298
1299 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001300}
1301
Abhishek Kumare8b63002021-01-16 18:11:15 +00001302static int write_graph_chunk_generation_data(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001303 void *data)
Abhishek Kumare8b63002021-01-16 18:11:15 +00001304{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001305 struct write_commit_graph_context *ctx = data;
Abhishek Kumare8b63002021-01-16 18:11:15 +00001306 int i, num_generation_data_overflows = 0;
1307
1308 for (i = 0; i < ctx->commits.nr; i++) {
1309 struct commit *c = ctx->commits.list[i];
Derrick Stolee90cb1c42021-02-01 17:15:04 +00001310 timestamp_t offset;
1311 repo_parse_commit(ctx->r, c);
1312 offset = commit_graph_data_at(c)->generation - c->date;
Abhishek Kumare8b63002021-01-16 18:11:15 +00001313 display_progress(ctx->progress, ++ctx->progress_cnt);
1314
1315 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1316 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1317 num_generation_data_overflows++;
1318 }
1319
1320 hashwrite_be32(f, offset);
1321 }
1322
1323 return 0;
1324}
1325
1326static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001327 void *data)
Abhishek Kumare8b63002021-01-16 18:11:15 +00001328{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001329 struct write_commit_graph_context *ctx = data;
Abhishek Kumare8b63002021-01-16 18:11:15 +00001330 int i;
1331 for (i = 0; i < ctx->commits.nr; i++) {
1332 struct commit *c = ctx->commits.list[i];
1333 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1334 display_progress(ctx->progress, ++ctx->progress_cnt);
1335
1336 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1337 hashwrite_be32(f, offset >> 32);
1338 hashwrite_be32(f, (uint32_t) offset);
1339 }
1340 }
1341
1342 return 0;
1343}
1344
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001345static int write_graph_chunk_extra_edges(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001346 void *data)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001347{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001348 struct write_commit_graph_context *ctx = data;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001349 struct commit **list = ctx->commits.list;
1350 struct commit **last = ctx->commits.list + ctx->commits.nr;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001351 struct commit_list *parent;
1352
1353 while (list < last) {
1354 int num_parents = 0;
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +01001355
Derrick Stoleec9905be2019-06-12 06:29:40 -07001356 display_progress(ctx->progress, ++ctx->progress_cnt);
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +01001357
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001358 for (parent = (*list)->parents; num_parents < 3 && parent;
1359 parent = parent->next)
1360 num_parents++;
1361
1362 if (num_parents <= 2) {
1363 list++;
1364 continue;
1365 }
1366
1367 /* Since num_parents > 2, this initializer is safe. */
1368 for (parent = (*list)->parents->next; parent; parent = parent->next) {
Jeff King45ee13b2021-01-28 01:19:42 -05001369 int edge_value = oid_pos(&parent->item->object.oid,
1370 ctx->commits.list,
1371 ctx->commits.nr,
1372 commit_to_oid);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001373
Derrick Stolee6c622f92019-06-18 11:14:27 -07001374 if (edge_value >= 0)
1375 edge_value += ctx->new_num_commits_in_base;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001376 else if (ctx->new_base_graph) {
Derrick Stolee6c622f92019-06-18 11:14:27 -07001377 uint32_t pos;
Patrick Steinhardt809ea282021-08-09 10:11:59 +02001378 if (find_commit_pos_in_graph(parent->item,
1379 ctx->new_base_graph,
1380 &pos))
Derrick Stolee6c622f92019-06-18 11:14:27 -07001381 edge_value = pos;
1382 }
1383
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001384 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -08001385 BUG("missing parent %s for commit %s",
1386 oid_to_hex(&parent->item->object.oid),
1387 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001388 else if (!parent->next)
1389 edge_value |= GRAPH_LAST_EDGE;
1390
1391 hashwrite_be32(f, edge_value);
1392 }
1393
1394 list++;
1395 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001396
1397 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001398}
1399
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001400static int write_graph_chunk_bloom_indexes(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001401 void *data)
Garima Singh76ffbca2020-04-06 16:59:49 +00001402{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001403 struct write_commit_graph_context *ctx = data;
Garima Singh76ffbca2020-04-06 16:59:49 +00001404 struct commit **list = ctx->commits.list;
1405 struct commit **last = ctx->commits.list + ctx->commits.nr;
1406 uint32_t cur_pos = 0;
Garima Singh76ffbca2020-04-06 16:59:49 +00001407
1408 while (list < last) {
Taylor Blau312cff52020-09-16 14:07:32 -04001409 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
Derrick Stolee94919742020-07-01 13:27:23 +00001410 size_t len = filter ? filter->len : 0;
1411 cur_pos += len;
SZEDER Gábor150cd3b2020-07-09 19:00:03 +02001412 display_progress(ctx->progress, ++ctx->progress_cnt);
Garima Singh76ffbca2020-04-06 16:59:49 +00001413 hashwrite_be32(f, cur_pos);
1414 list++;
1415 }
1416
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001417 return 0;
Garima Singh76ffbca2020-04-06 16:59:49 +00001418}
1419
Derrick Stolee0087a872020-07-01 13:27:24 +00001420static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1421{
1422 struct json_writer jw = JSON_WRITER_INIT;
1423
1424 jw_object_begin(&jw, 0);
1425 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1426 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1427 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
Taylor Blau97ffa4f2020-09-17 09:34:42 -04001428 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
Derrick Stolee0087a872020-07-01 13:27:24 +00001429 jw_end(&jw);
1430
1431 trace2_data_json("bloom", ctx->r, "settings", &jw);
1432
1433 jw_release(&jw);
1434}
1435
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001436static int write_graph_chunk_bloom_data(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001437 void *data)
Garima Singh76ffbca2020-04-06 16:59:49 +00001438{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001439 struct write_commit_graph_context *ctx = data;
Garima Singh76ffbca2020-04-06 16:59:49 +00001440 struct commit **list = ctx->commits.list;
1441 struct commit **last = ctx->commits.list + ctx->commits.nr;
Garima Singh76ffbca2020-04-06 16:59:49 +00001442
Derrick Stolee0087a872020-07-01 13:27:24 +00001443 trace2_bloom_filter_settings(ctx);
1444
Derrick Stolee98037f22020-06-23 17:47:00 +00001445 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1446 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1447 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
Garima Singh76ffbca2020-04-06 16:59:49 +00001448
1449 while (list < last) {
Taylor Blau312cff52020-09-16 14:07:32 -04001450 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
Derrick Stolee94919742020-07-01 13:27:23 +00001451 size_t len = filter ? filter->len : 0;
Derrick Stolee94919742020-07-01 13:27:23 +00001452
SZEDER Gábor150cd3b2020-07-09 19:00:03 +02001453 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee94919742020-07-01 13:27:23 +00001454 if (len)
1455 hashwrite(f, filter->data, len * sizeof(unsigned char));
Garima Singh76ffbca2020-04-06 16:59:49 +00001456 list++;
1457 }
1458
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001459 return 0;
Garima Singh76ffbca2020-04-06 16:59:49 +00001460}
1461
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001462static int add_packed_commits(const struct object_id *oid,
1463 struct packed_git *pack,
1464 uint32_t pos,
1465 void *data)
1466{
Derrick Stoleec9905be2019-06-12 06:29:40 -07001467 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001468 enum object_type type;
1469 off_t offset = nth_packed_object_offset(pack, pos);
1470 struct object_info oi = OBJECT_INFO_INIT;
1471
Derrick Stoleec9905be2019-06-12 06:29:40 -07001472 if (ctx->progress)
1473 display_progress(ctx->progress, ++ctx->progress_done);
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +00001474
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001475 oi.typep = &type;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001476 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
Nguyễn Thái Ngọc Duy4f5b5322018-07-21 09:49:26 +02001477 die(_("unable to get type of object %s"), oid_to_hex(oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001478
1479 if (type != OBJ_COMMIT)
1480 return 0;
1481
Jeff Kinga5f1c442020-12-07 14:11:05 -05001482 oid_array_append(&ctx->oids, oid);
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001483 set_commit_pos(ctx->r, oid);
1484
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001485 return 0;
1486}
1487
Derrick Stoleec9905be2019-06-12 06:29:40 -07001488static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001489{
1490 struct commit_list *parent;
1491 for (parent = commit->parents; parent; parent = parent->next) {
Derrick Stoleecb99a342019-10-24 13:40:42 +00001492 if (!(parent->item->object.flags & REACHABLE)) {
Jeff Kinga5f1c442020-12-07 14:11:05 -05001493 oid_array_append(&ctx->oids, &parent->item->object.oid);
Derrick Stoleecb99a342019-10-24 13:40:42 +00001494 parent->item->object.flags |= REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001495 }
1496 }
1497}
1498
Derrick Stoleec9905be2019-06-12 06:29:40 -07001499static void close_reachable(struct write_commit_graph_context *ctx)
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001500{
Ævar Arnfjörð Bjarmason49bbc572019-01-19 21:21:21 +01001501 int i;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001502 struct commit *commit;
Taylor Blau98bb7962020-09-17 22:59:49 -04001503 enum commit_graph_split_flags flags = ctx->opts ?
1504 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001505
Derrick Stoleec9905be2019-06-12 06:29:40 -07001506 if (ctx->report_progress)
1507 ctx->progress = start_delayed_progress(
1508 _("Loading known commits in commit graph"),
1509 ctx->oids.nr);
1510 for (i = 0; i < ctx->oids.nr; i++) {
1511 display_progress(ctx->progress, i + 1);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001512 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001513 if (commit)
Derrick Stoleecb99a342019-10-24 13:40:42 +00001514 commit->object.flags |= REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001515 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001516 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001517
1518 /*
Derrick Stoleec9905be2019-06-12 06:29:40 -07001519 * As this loop runs, ctx->oids.nr may grow, but not more
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001520 * than the number of missing commits in the reachable
1521 * closure.
1522 */
Derrick Stoleec9905be2019-06-12 06:29:40 -07001523 if (ctx->report_progress)
1524 ctx->progress = start_delayed_progress(
1525 _("Expanding reachable commits in commit graph"),
SZEDER Gábor67fa6aa2019-09-07 01:01:33 -04001526 0);
Derrick Stoleec9905be2019-06-12 06:29:40 -07001527 for (i = 0; i < ctx->oids.nr; i++) {
1528 display_progress(ctx->progress, i + 1);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001529 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001530
Derrick Stolee6c622f92019-06-18 11:14:27 -07001531 if (!commit)
1532 continue;
1533 if (ctx->split) {
Derrick Stoleec4cc0832021-02-01 17:15:03 +00001534 if ((!repo_parse_commit(ctx->r, commit) &&
Abhishek Kumarc49c82a2020-06-17 14:44:10 +05301535 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
Taylor Blau8a6ac282020-04-13 22:04:17 -06001536 flags == COMMIT_GRAPH_SPLIT_REPLACE)
Derrick Stolee6c622f92019-06-18 11:14:27 -07001537 add_missing_parents(ctx, commit);
Derrick Stoleec4cc0832021-02-01 17:15:03 +00001538 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
Derrick Stoleec9905be2019-06-12 06:29:40 -07001539 add_missing_parents(ctx, commit);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001540 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001541 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001542
Derrick Stoleec9905be2019-06-12 06:29:40 -07001543 if (ctx->report_progress)
1544 ctx->progress = start_delayed_progress(
1545 _("Clearing commit marks in commit graph"),
1546 ctx->oids.nr);
1547 for (i = 0; i < ctx->oids.nr; i++) {
1548 display_progress(ctx->progress, i + 1);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001549 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001550
1551 if (commit)
Derrick Stoleecb99a342019-10-24 13:40:42 +00001552 commit->object.flags &= ~REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001553 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001554 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001555}
1556
Derrick Stolee368d19b2023-03-20 11:26:49 +00001557struct compute_generation_info {
1558 struct repository *r;
1559 struct packed_commit_list *commits;
1560 struct progress *progress;
1561 int progress_cnt;
1562
1563 timestamp_t (*get_generation)(struct commit *c, void *data);
1564 void (*set_generation)(struct commit *c, timestamp_t gen, void *data);
1565 void *data;
1566};
1567
1568static timestamp_t compute_generation_from_max(struct commit *c,
1569 timestamp_t max_gen,
1570 int generation_version)
1571{
1572 switch (generation_version) {
1573 case 1: /* topological levels */
1574 if (max_gen > GENERATION_NUMBER_V1_MAX - 1)
1575 max_gen = GENERATION_NUMBER_V1_MAX - 1;
1576 return max_gen + 1;
1577
1578 case 2: /* corrected commit date */
1579 if (c->date && c->date > max_gen)
1580 max_gen = c->date - 1;
1581 return max_gen + 1;
1582
1583 default:
1584 BUG("attempting unimplemented version");
1585 }
1586}
1587
1588static void compute_reachable_generation_numbers(
1589 struct compute_generation_info *info,
1590 int generation_version)
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00001591{
1592 int i;
1593 struct commit_list *list = NULL;
1594
Derrick Stolee368d19b2023-03-20 11:26:49 +00001595 for (i = 0; i < info->commits->nr; i++) {
1596 struct commit *c = info->commits->list[i];
1597 timestamp_t gen;
1598 repo_parse_commit(info->r, c);
1599 gen = info->get_generation(c, info->data);
1600 display_progress(info->progress, info->progress_cnt + 1);
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00001601
Derrick Stolee368d19b2023-03-20 11:26:49 +00001602 if (gen != GENERATION_NUMBER_ZERO && gen != GENERATION_NUMBER_INFINITY)
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00001603 continue;
1604
1605 commit_list_insert(c, &list);
1606 while (list) {
1607 struct commit *current = list->item;
1608 struct commit_list *parent;
1609 int all_parents_computed = 1;
Derrick Stolee368d19b2023-03-20 11:26:49 +00001610 uint32_t max_gen = 0;
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00001611
1612 for (parent = current->parents; parent; parent = parent->next) {
Derrick Stolee368d19b2023-03-20 11:26:49 +00001613 repo_parse_commit(info->r, parent->item);
1614 gen = info->get_generation(parent->item, info->data);
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00001615
Derrick Stolee368d19b2023-03-20 11:26:49 +00001616 if (gen == GENERATION_NUMBER_ZERO) {
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00001617 all_parents_computed = 0;
1618 commit_list_insert(parent->item, &list);
1619 break;
1620 }
1621
Derrick Stolee368d19b2023-03-20 11:26:49 +00001622 if (gen > max_gen)
1623 max_gen = gen;
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00001624 }
1625
1626 if (all_parents_computed) {
1627 pop_commit(&list);
Derrick Stolee368d19b2023-03-20 11:26:49 +00001628 gen = compute_generation_from_max(
1629 current, max_gen,
1630 generation_version);
1631 info->set_generation(current, gen, info->data);
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00001632 }
1633 }
1634 }
Derrick Stolee368d19b2023-03-20 11:26:49 +00001635}
1636
1637static timestamp_t get_topo_level(struct commit *c, void *data)
1638{
1639 struct write_commit_graph_context *ctx = data;
1640 return *topo_level_slab_at(ctx->topo_levels, c);
1641}
1642
1643static void set_topo_level(struct commit *c, timestamp_t t, void *data)
1644{
1645 struct write_commit_graph_context *ctx = data;
1646 *topo_level_slab_at(ctx->topo_levels, c) = (uint32_t)t;
1647}
1648
1649static void compute_topological_levels(struct write_commit_graph_context *ctx)
1650{
1651 struct compute_generation_info info = {
1652 .r = ctx->r,
1653 .commits = &ctx->commits,
1654 .get_generation = get_topo_level,
1655 .set_generation = set_topo_level,
1656 .data = ctx,
1657 };
1658
1659 if (ctx->report_progress)
1660 info.progress = ctx->progress
1661 = start_delayed_progress(
1662 _("Computing commit graph topological levels"),
1663 ctx->commits.nr);
1664
1665 compute_reachable_generation_numbers(&info, 1);
1666
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00001667 stop_progress(&ctx->progress);
1668}
1669
Jeff Kinge1cba402023-08-29 19:45:17 -04001670static timestamp_t get_generation_from_graph_data(struct commit *c,
1671 void *data UNUSED)
Derrick Stolee80c928d2023-03-20 11:26:50 +00001672{
1673 return commit_graph_data_at(c)->generation;
1674}
1675
Jeff Kinge1cba402023-08-29 19:45:17 -04001676static void set_generation_v2(struct commit *c, timestamp_t t,
1677 void *data UNUSED)
Derrick Stolee80c928d2023-03-20 11:26:50 +00001678{
1679 struct commit_graph_data *g = commit_graph_data_at(c);
Patrick Steinhardtd3af1c12023-03-27 10:08:25 +02001680 g->generation = t;
Derrick Stolee80c928d2023-03-20 11:26:50 +00001681}
1682
Derrick Stoleec9905be2019-06-12 06:29:40 -07001683static void compute_generation_numbers(struct write_commit_graph_context *ctx)
Derrick Stolee3258c662018-05-01 12:47:09 +00001684{
1685 int i;
Derrick Stolee80c928d2023-03-20 11:26:50 +00001686 struct compute_generation_info info = {
1687 .r = ctx->r,
1688 .commits = &ctx->commits,
1689 .get_generation = get_generation_from_graph_data,
1690 .set_generation = set_generation_v2,
Derrick Stolee80c928d2023-03-20 11:26:50 +00001691 };
Derrick Stolee3258c662018-05-01 12:47:09 +00001692
Derrick Stoleec9905be2019-06-12 06:29:40 -07001693 if (ctx->report_progress)
Derrick Stolee80c928d2023-03-20 11:26:50 +00001694 info.progress = ctx->progress
1695 = start_delayed_progress(
Derrick Stoleec9905be2019-06-12 06:29:40 -07001696 _("Computing commit graph generation numbers"),
1697 ctx->commits.nr);
Derrick Stoleefde55b02021-02-02 03:01:22 +00001698
1699 if (!ctx->trust_generation_numbers) {
1700 for (i = 0; i < ctx->commits.nr; i++) {
1701 struct commit *c = ctx->commits.list[i];
1702 repo_parse_commit(ctx->r, c);
1703 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1704 }
1705 }
1706
Derrick Stolee80c928d2023-03-20 11:26:50 +00001707 compute_reachable_generation_numbers(&info, 2);
Derrick Stolee75979d92022-03-01 19:48:30 +00001708
1709 for (i = 0; i < ctx->commits.nr; i++) {
1710 struct commit *c = ctx->commits.list[i];
1711 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1712 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1713 ctx->num_generation_data_overflows++;
1714 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001715 stop_progress(&ctx->progress);
Derrick Stolee3258c662018-05-01 12:47:09 +00001716}
1717
Taylor Blauc08645b2023-03-20 11:26:52 +00001718static void set_generation_in_graph_data(struct commit *c, timestamp_t t,
Jeff Kinge1cba402023-08-29 19:45:17 -04001719 void *data UNUSED)
Taylor Blauc08645b2023-03-20 11:26:52 +00001720{
1721 commit_graph_data_at(c)->generation = t;
1722}
1723
1724/*
1725 * After this method, all commits reachable from those in the given
1726 * list will have non-zero, non-infinite generation numbers.
1727 */
1728void ensure_generations_valid(struct repository *r,
1729 struct commit **commits, size_t nr)
1730{
1731 int generation_version = get_configured_generation_version(r);
1732 struct packed_commit_list list = {
1733 .list = commits,
1734 .alloc = nr,
1735 .nr = nr,
1736 };
1737 struct compute_generation_info info = {
1738 .r = r,
1739 .commits = &list,
1740 .get_generation = get_generation_from_graph_data,
1741 .set_generation = set_generation_in_graph_data,
1742 };
1743
1744 compute_reachable_generation_numbers(&info, generation_version);
1745}
1746
Taylor Blau312cff52020-09-16 14:07:32 -04001747static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1748{
1749 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1750 ctx->count_bloom_filter_computed);
1751 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1752 ctx->count_bloom_filter_not_computed);
Taylor Blau59f0d502020-09-17 22:59:44 -04001753 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1754 ctx->count_bloom_filter_trunc_empty);
Taylor Blau312cff52020-09-16 14:07:32 -04001755 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1756 ctx->count_bloom_filter_trunc_large);
1757}
1758
Garima Singhf97b9322020-03-30 00:31:28 +00001759static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1760{
1761 int i;
1762 struct progress *progress = NULL;
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001763 struct commit **sorted_commits;
Taylor Blau809e0322020-09-18 09:27:27 -04001764 int max_new_filters;
Garima Singhf97b9322020-03-30 00:31:28 +00001765
1766 init_bloom_filters();
1767
1768 if (ctx->report_progress)
1769 progress = start_delayed_progress(
1770 _("Computing commit changed paths Bloom filters"),
1771 ctx->commits.nr);
1772
René Scharfe6e578412023-01-01 22:16:48 +01001773 DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
Garima Singh3d112752020-03-30 00:31:30 +00001774
1775 if (ctx->order_by_pack)
1776 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1777 else
1778 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001779
Taylor Blau809e0322020-09-18 09:27:27 -04001780 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1781 ctx->opts->max_new_filters : ctx->commits.nr;
1782
Garima Singhf97b9322020-03-30 00:31:28 +00001783 for (i = 0; i < ctx->commits.nr; i++) {
Taylor Blau312cff52020-09-16 14:07:32 -04001784 enum bloom_filter_computed computed = 0;
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001785 struct commit *c = sorted_commits[i];
Taylor Blau312cff52020-09-16 14:07:32 -04001786 struct bloom_filter *filter = get_or_compute_bloom_filter(
1787 ctx->r,
1788 c,
Taylor Blau809e0322020-09-18 09:27:27 -04001789 ctx->count_bloom_filter_computed < max_new_filters,
Taylor Blau9a7a9ed2020-09-16 14:07:46 -04001790 ctx->bloom_settings,
Taylor Blau312cff52020-09-16 14:07:32 -04001791 &computed);
1792 if (computed & BLOOM_COMPUTED) {
1793 ctx->count_bloom_filter_computed++;
Taylor Blau59f0d502020-09-17 22:59:44 -04001794 if (computed & BLOOM_TRUNC_EMPTY)
1795 ctx->count_bloom_filter_trunc_empty++;
Taylor Blau312cff52020-09-16 14:07:32 -04001796 if (computed & BLOOM_TRUNC_LARGE)
1797 ctx->count_bloom_filter_trunc_large++;
1798 } else if (computed & BLOOM_NOT_COMPUTED)
1799 ctx->count_bloom_filter_not_computed++;
Taylor Blau809e0322020-09-18 09:27:27 -04001800 ctx->total_bloom_filter_data_size += filter
1801 ? sizeof(unsigned char) * filter->len : 0;
Garima Singhf97b9322020-03-30 00:31:28 +00001802 display_progress(progress, i + 1);
1803 }
1804
Taylor Blau312cff52020-09-16 14:07:32 -04001805 if (trace2_is_enabled())
1806 trace2_bloom_filter_write_statistics(ctx);
1807
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001808 free(sorted_commits);
Garima Singhf97b9322020-03-30 00:31:28 +00001809 stop_progress(&progress);
1810}
1811
Taylor Blau1fe10842020-05-04 19:13:35 -06001812struct refs_cb_data {
1813 struct oidset *commits;
Taylor Blaud335ce82020-05-13 15:59:33 -06001814 struct progress *progress;
Taylor Blau1fe10842020-05-04 19:13:35 -06001815};
1816
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +02001817static int add_ref_to_set(const char *refname UNUSED,
Taylor Blau6830c362020-04-13 22:04:25 -06001818 const struct object_id *oid,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +02001819 int flags UNUSED, void *cb_data)
Derrick Stolee59fb8772018-06-27 09:24:45 -04001820{
Taylor Blau630cd512020-05-13 15:59:37 -06001821 struct object_id peeled;
Taylor Blau1fe10842020-05-04 19:13:35 -06001822 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001823
Jeff King36a31792021-01-20 14:44:43 -05001824 if (!peel_iterated_oid(oid, &peeled))
Taylor Blau630cd512020-05-13 15:59:37 -06001825 oid = &peeled;
1826 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1827 oidset_insert(data->commits, oid);
Taylor Blaud335ce82020-05-13 15:59:33 -06001828
1829 display_progress(data->progress, oidset_size(data->commits));
1830
Derrick Stolee59fb8772018-06-27 09:24:45 -04001831 return 0;
1832}
1833
Taylor Blau0bd52e22020-02-03 21:51:50 -08001834int write_commit_graph_reachable(struct object_directory *odb,
SZEDER Gábor39d88312019-08-05 10:02:39 +02001835 enum commit_graph_write_flags flags,
Taylor Blau98bb7962020-09-17 22:59:49 -04001836 const struct commit_graph_opts *opts)
Derrick Stolee59fb8772018-06-27 09:24:45 -04001837{
Taylor Blau6830c362020-04-13 22:04:25 -06001838 struct oidset commits = OIDSET_INIT;
Taylor Blau1fe10842020-05-04 19:13:35 -06001839 struct refs_cb_data data;
Derrick Stoleee103f722019-06-12 06:29:37 -07001840 int result;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001841
Taylor Blau1fe10842020-05-04 19:13:35 -06001842 memset(&data, 0, sizeof(data));
1843 data.commits = &commits;
Taylor Blaud335ce82020-05-13 15:59:33 -06001844 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1845 data.progress = start_delayed_progress(
1846 _("Collecting referenced commits"), 0);
Taylor Blau1fe10842020-05-04 19:13:35 -06001847
1848 for_each_ref(add_ref_to_set, &data);
SZEDER Gábor6f9d5f22020-07-09 18:54:32 +02001849
1850 stop_progress(&data.progress);
1851
Taylor Blau6830c362020-04-13 22:04:25 -06001852 result = write_commit_graph(odb, NULL, &commits,
Taylor Blau98bb7962020-09-17 22:59:49 -04001853 flags, opts);
Derrick Stoleef4dbdfc2018-10-03 10:12:15 -07001854
Taylor Blau6830c362020-04-13 22:04:25 -06001855 oidset_clear(&commits);
Derrick Stoleee103f722019-06-12 06:29:37 -07001856 return result;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001857}
1858
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001859static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
Ævar Arnfjörð Bjarmason4a047902022-03-04 19:32:12 +01001860 const struct string_list *pack_indexes)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001861{
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001862 uint32_t i;
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001863 struct strbuf progress_title = STRBUF_INIT;
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001864 struct strbuf packname = STRBUF_INIT;
1865 int dirlen;
Ævar Arnfjörð Bjarmason51a94d82022-03-04 19:32:13 +01001866 int ret = 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001867
Taylor Blau0bd52e22020-02-03 21:51:50 -08001868 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001869 dirlen = packname.len;
1870 if (ctx->report_progress) {
1871 strbuf_addf(&progress_title,
Ævar Arnfjörð Bjarmason99d60542022-03-07 16:27:08 +01001872 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1873 "Finding commits for commit graph in %"PRIuMAX" packs",
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001874 pack_indexes->nr),
Ævar Arnfjörð Bjarmason99d60542022-03-07 16:27:08 +01001875 (uintmax_t)pack_indexes->nr);
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001876 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1877 ctx->progress_done = 0;
Derrick Stolee7547b952018-04-10 08:56:08 -04001878 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001879 for (i = 0; i < pack_indexes->nr; i++) {
1880 struct packed_git *p;
1881 strbuf_setlen(&packname, dirlen);
1882 strbuf_addstr(&packname, pack_indexes->items[i].string);
1883 p = add_packed_git(packname.buf, packname.len, 1);
1884 if (!p) {
Ævar Arnfjörð Bjarmason51a94d82022-03-04 19:32:13 +01001885 ret = error(_("error adding pack %s"), packname.buf);
1886 goto cleanup;
Derrick Stolee7547b952018-04-10 08:56:08 -04001887 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001888 if (open_pack_index(p)) {
Ævar Arnfjörð Bjarmason51a94d82022-03-04 19:32:13 +01001889 ret = error(_("error opening index for %s"), packname.buf);
1890 goto cleanup;
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +00001891 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001892 for_each_object_in_pack(p, add_packed_commits, ctx,
1893 FOR_EACH_OBJECT_PACK_ORDER);
1894 close_pack(p);
1895 free(p);
Derrick Stolee3d5df012018-04-10 08:56:07 -04001896 }
1897
Ævar Arnfjörð Bjarmason51a94d82022-03-04 19:32:13 +01001898cleanup:
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001899 stop_progress(&ctx->progress);
René Scharfe0aa6bce2019-08-07 13:15:02 +02001900 strbuf_release(&progress_title);
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001901 strbuf_release(&packname);
Derrick Stolee3d5df012018-04-10 08:56:07 -04001902
Ævar Arnfjörð Bjarmason51a94d82022-03-04 19:32:13 +01001903 return ret;
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001904}
Derrick Stolee3d5df012018-04-10 08:56:07 -04001905
Taylor Blau6830c362020-04-13 22:04:25 -06001906static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1907 struct oidset *commits)
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001908{
Taylor Blau6830c362020-04-13 22:04:25 -06001909 struct oidset_iter iter;
1910 struct object_id *oid;
1911
1912 if (!oidset_size(commits))
1913 return 0;
Derrick Stolee3d5df012018-04-10 08:56:07 -04001914
Taylor Blau6830c362020-04-13 22:04:25 -06001915 oidset_iter_init(commits, &iter);
1916 while ((oid = oidset_iter_next(&iter))) {
Jeff Kinga5f1c442020-12-07 14:11:05 -05001917 oid_array_append(&ctx->oids, oid);
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001918 }
Taylor Blau6830c362020-04-13 22:04:25 -06001919
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001920 return 0;
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001921}
1922
Derrick Stoleeb2c83062019-06-12 06:29:42 -07001923static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1924{
1925 if (ctx->report_progress)
1926 ctx->progress = start_delayed_progress(
1927 _("Finding commits for commit graph among packed objects"),
1928 ctx->approx_nr_objects);
1929 for_each_packed_object(add_packed_commits, ctx,
1930 FOR_EACH_OBJECT_PACK_ORDER);
1931 if (ctx->progress_done < ctx->approx_nr_objects)
1932 display_progress(ctx->progress, ctx->approx_nr_objects);
1933 stop_progress(&ctx->progress);
1934}
1935
Derrick Stoleef998d542019-06-12 06:29:44 -07001936static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1937{
1938 uint32_t i;
Taylor Blau98bb7962020-09-17 22:59:49 -04001939 enum commit_graph_split_flags flags = ctx->opts ?
1940 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
Derrick Stoleef998d542019-06-12 06:29:44 -07001941
1942 ctx->num_extra_edges = 0;
1943 if (ctx->report_progress)
1944 ctx->progress = start_delayed_progress(
1945 _("Finding extra edges in commit graph"),
1946 ctx->oids.nr);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001947 oid_array_sort(&ctx->oids);
1948 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
René Scharfe689a1462019-09-15 19:07:44 +02001949 unsigned int num_parents;
1950
Derrick Stoleef998d542019-06-12 06:29:44 -07001951 display_progress(ctx->progress, i + 1);
Derrick Stoleef998d542019-06-12 06:29:44 -07001952
Derrick Stolee6c622f92019-06-18 11:14:27 -07001953 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001954 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001955
Taylor Blau8a6ac282020-04-13 22:04:17 -06001956 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
Abhishek Kumarc49c82a2020-06-17 14:44:10 +05301957 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
Derrick Stolee6c622f92019-06-18 11:14:27 -07001958 continue;
1959
Taylor Blau8a6ac282020-04-13 22:04:17 -06001960 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
Derrick Stoleec4cc0832021-02-01 17:15:03 +00001961 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
Taylor Blau8a6ac282020-04-13 22:04:17 -06001962 else
Derrick Stoleec4cc0832021-02-01 17:15:03 +00001963 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
Derrick Stoleef998d542019-06-12 06:29:44 -07001964
René Scharfe689a1462019-09-15 19:07:44 +02001965 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001966 if (num_parents > 2)
Derrick Stoleef998d542019-06-12 06:29:44 -07001967 ctx->num_extra_edges += num_parents - 1;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001968
Derrick Stoleef998d542019-06-12 06:29:44 -07001969 ctx->commits.nr++;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001970 }
Derrick Stoleef998d542019-06-12 06:29:44 -07001971 stop_progress(&ctx->progress);
1972}
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001973
Derrick Stolee6c622f92019-06-18 11:14:27 -07001974static int write_graph_chunk_base_1(struct hashfile *f,
1975 struct commit_graph *g)
1976{
1977 int num = 0;
1978
1979 if (!g)
1980 return 0;
1981
1982 num = write_graph_chunk_base_1(f, g->base_graph);
1983 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1984 return num + 1;
1985}
1986
1987static int write_graph_chunk_base(struct hashfile *f,
Derrick Stoleeeb907192021-02-05 14:30:36 +00001988 void *data)
Derrick Stolee6c622f92019-06-18 11:14:27 -07001989{
Derrick Stoleeeb907192021-02-05 14:30:36 +00001990 struct write_commit_graph_context *ctx = data;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001991 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1992
1993 if (num != ctx->num_commit_graphs_after - 1) {
1994 error(_("failed to write correct number of base graph ids"));
1995 return -1;
1996 }
1997
1998 return 0;
1999}
2000
Derrick Stolee238def52019-06-12 06:29:45 -07002001static int write_commit_graph_file(struct write_commit_graph_context *ctx)
2002{
2003 uint32_t i;
Derrick Stolee6c622f92019-06-18 11:14:27 -07002004 int fd;
Derrick Stolee238def52019-06-12 06:29:45 -07002005 struct hashfile *f;
2006 struct lock_file lk = LOCK_INIT;
Derrick Stolee238def52019-06-12 06:29:45 -07002007 const unsigned hashsz = the_hash_algo->rawsz;
2008 struct strbuf progress_title = STRBUF_INIT;
Derrick Stolee47410aa2021-02-18 14:07:25 +00002009 struct chunkfile *cf;
brian m. carlson72871b132021-04-26 01:02:58 +00002010 unsigned char file_hash[GIT_MAX_RAWSZ];
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002011
Derrick Stolee6c622f92019-06-18 11:14:27 -07002012 if (ctx->split) {
2013 struct strbuf tmp_file = STRBUF_INIT;
2014
2015 strbuf_addf(&tmp_file,
2016 "%s/info/commit-graphs/tmp_graph_XXXXXX",
Taylor Blau0bd52e22020-02-03 21:51:50 -08002017 ctx->odb->path);
Derrick Stolee6c622f92019-06-18 11:14:27 -07002018 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
2019 } else {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08002020 ctx->graph_name = get_commit_graph_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07002021 }
2022
Derrick Stolee238def52019-06-12 06:29:45 -07002023 if (safe_create_leading_directories(ctx->graph_name)) {
2024 UNLEAK(ctx->graph_name);
2025 error(_("unable to create leading directories of %s"),
2026 ctx->graph_name);
2027 return -1;
Derrick Stoleef4dbdfc2018-10-03 10:12:15 -07002028 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002029
Derrick Stolee6c622f92019-06-18 11:14:27 -07002030 if (ctx->split) {
Derrick Stolee663b2b12020-09-17 18:11:46 +00002031 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07002032
Taylor Blau45a43652020-04-29 11:36:46 -06002033 hold_lock_file_for_update_mode(&lk, lock_name,
2034 LOCK_DIE_ON_ERROR, 0444);
Ævar Arnfjörð Bjarmasonef3fe212022-03-04 19:32:14 +01002035 free(lock_name);
Derrick Stolee6c622f92019-06-18 11:14:27 -07002036
2037 fd = git_mkstemp_mode(ctx->graph_name, 0444);
2038 if (fd < 0) {
Taylor Blaua2d57e22020-04-23 15:41:02 -06002039 error(_("unable to create temporary graph layer"));
Derrick Stolee6c622f92019-06-18 11:14:27 -07002040 return -1;
2041 }
2042
Taylor Blauf4d62842020-04-29 11:36:42 -06002043 if (adjust_shared_perm(ctx->graph_name)) {
2044 error(_("unable to adjust shared permissions for '%s'"),
2045 ctx->graph_name);
2046 return -1;
2047 }
2048
Derrick Stolee6c622f92019-06-18 11:14:27 -07002049 f = hashfd(fd, ctx->graph_name);
2050 } else {
Taylor Blau1f9beca2020-04-29 11:36:38 -06002051 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
2052 LOCK_DIE_ON_ERROR, 0444);
Martin Ågrena52cdce2021-01-05 20:23:47 +01002053 fd = get_lock_file_fd(&lk);
2054 f = hashfd(fd, get_lock_file_path(&lk));
Derrick Stolee6c622f92019-06-18 11:14:27 -07002055 }
Derrick Stolee238def52019-06-12 06:29:45 -07002056
Derrick Stolee47410aa2021-02-18 14:07:25 +00002057 cf = init_chunkfile(f);
2058
2059 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
2060 write_graph_chunk_fanout);
Taylor Blau48f3f8c2023-07-12 19:37:54 -04002061 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, st_mult(hashsz, ctx->commits.nr),
Derrick Stolee47410aa2021-02-18 14:07:25 +00002062 write_graph_chunk_oids);
Taylor Blau48f3f8c2023-07-12 19:37:54 -04002063 add_chunk(cf, GRAPH_CHUNKID_DATA, st_mult(hashsz + 16, ctx->commits.nr),
Derrick Stolee47410aa2021-02-18 14:07:25 +00002064 write_graph_chunk_data);
Abhishek Kumare8b63002021-01-16 18:11:15 +00002065
Derrick Stolee47410aa2021-02-18 14:07:25 +00002066 if (ctx->write_generation_data)
2067 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
Taylor Blau48f3f8c2023-07-12 19:37:54 -04002068 st_mult(sizeof(uint32_t), ctx->commits.nr),
Derrick Stolee47410aa2021-02-18 14:07:25 +00002069 write_graph_chunk_generation_data);
2070 if (ctx->num_generation_data_overflows)
2071 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
Taylor Blau48f3f8c2023-07-12 19:37:54 -04002072 st_mult(sizeof(timestamp_t), ctx->num_generation_data_overflows),
Derrick Stolee47410aa2021-02-18 14:07:25 +00002073 write_graph_chunk_generation_data_overflow);
2074 if (ctx->num_extra_edges)
2075 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
Taylor Blau48f3f8c2023-07-12 19:37:54 -04002076 st_mult(4, ctx->num_extra_edges),
Derrick Stolee47410aa2021-02-18 14:07:25 +00002077 write_graph_chunk_extra_edges);
Garima Singh76ffbca2020-04-06 16:59:49 +00002078 if (ctx->changed_paths) {
Derrick Stolee47410aa2021-02-18 14:07:25 +00002079 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
Taylor Blau48f3f8c2023-07-12 19:37:54 -04002080 st_mult(sizeof(uint32_t), ctx->commits.nr),
Derrick Stolee47410aa2021-02-18 14:07:25 +00002081 write_graph_chunk_bloom_indexes);
2082 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
Taylor Blau48f3f8c2023-07-12 19:37:54 -04002083 st_add(sizeof(uint32_t) * 3,
2084 ctx->total_bloom_filter_data_size),
Derrick Stolee47410aa2021-02-18 14:07:25 +00002085 write_graph_chunk_bloom_data);
Garima Singh76ffbca2020-04-06 16:59:49 +00002086 }
Derrick Stolee47410aa2021-02-18 14:07:25 +00002087 if (ctx->num_commit_graphs_after > 1)
2088 add_chunk(cf, GRAPH_CHUNKID_BASE,
Taylor Blau48f3f8c2023-07-12 19:37:54 -04002089 st_mult(hashsz, ctx->num_commit_graphs_after - 1),
Derrick Stolee47410aa2021-02-18 14:07:25 +00002090 write_graph_chunk_base);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002091
2092 hashwrite_be32(f, GRAPH_SIGNATURE);
2093
2094 hashwrite_u8(f, GRAPH_VERSION);
Taylor Blaud9fef9d2022-05-20 19:17:41 -04002095 hashwrite_u8(f, oid_version(the_hash_algo));
Derrick Stolee47410aa2021-02-18 14:07:25 +00002096 hashwrite_u8(f, get_num_chunks(cf));
Derrick Stolee6c622f92019-06-18 11:14:27 -07002097 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002098
Derrick Stolee238def52019-06-12 06:29:45 -07002099 if (ctx->report_progress) {
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01002100 strbuf_addf(&progress_title,
2101 Q_("Writing out commit graph in %d pass",
2102 "Writing out commit graph in %d passes",
Taylor Blauc4ff24b2021-02-24 12:12:22 -05002103 get_num_chunks(cf)),
2104 get_num_chunks(cf));
Derrick Stolee238def52019-06-12 06:29:45 -07002105 ctx->progress = start_delayed_progress(
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01002106 progress_title.buf,
Taylor Blau48f3f8c2023-07-12 19:37:54 -04002107 st_mult(get_num_chunks(cf), ctx->commits.nr));
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01002108 }
SZEDER Gábor17e62752020-07-01 13:27:26 +00002109
Derrick Stolee47410aa2021-02-18 14:07:25 +00002110 write_chunkfile(cf, ctx);
SZEDER Gábor17e62752020-07-01 13:27:26 +00002111
Derrick Stolee238def52019-06-12 06:29:45 -07002112 stop_progress(&ctx->progress);
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01002113 strbuf_release(&progress_title);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002114
Derrick Stolee6c622f92019-06-18 11:14:27 -07002115 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
2116 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
Taylor Blauad2dd5b2020-02-03 13:18:02 -08002117 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
Derrick Stolee6c622f92019-06-18 11:14:27 -07002118
2119 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
2120 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
2121 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
2122 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
2123 }
2124
Derrick Stoleec3a3a962019-05-17 11:41:47 -07002125 close_commit_graph(ctx->r->objects);
Neeraj Singh020406e2022-03-10 22:43:21 +00002126 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
2127 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
Derrick Stolee47410aa2021-02-18 14:07:25 +00002128 free_chunkfile(cf);
Derrick Stolee6c622f92019-06-18 11:14:27 -07002129
2130 if (ctx->split) {
2131 FILE *chainf = fdopen_lock_file(&lk, "w");
2132 char *final_graph_name;
2133 int result;
2134
2135 close(fd);
2136
2137 if (!chainf) {
2138 error(_("unable to open commit-graph chain file"));
2139 return -1;
2140 }
2141
2142 if (ctx->base_graph_name) {
Taylor Blau8a6ac282020-04-13 22:04:17 -06002143 const char *dest;
2144 int idx = ctx->num_commit_graphs_after - 1;
2145 if (ctx->num_commit_graphs_after > 1)
2146 idx--;
2147
2148 dest = ctx->commit_graph_filenames_after[idx];
Derrick Stolee6c622f92019-06-18 11:14:27 -07002149
Derrick Stolee135a7122019-06-18 11:14:28 -07002150 if (strcmp(ctx->base_graph_name, dest)) {
2151 result = rename(ctx->base_graph_name, dest);
2152
2153 if (result) {
2154 error(_("failed to rename base commit-graph file"));
2155 return -1;
2156 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07002157 }
2158 } else {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08002159 char *graph_name = get_commit_graph_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07002160 unlink(graph_name);
Ævar Arnfjörð Bjarmasonef3fe212022-03-04 19:32:14 +01002161 free(graph_name);
Derrick Stolee6c622f92019-06-18 11:14:27 -07002162 }
2163
Jeff King274bfa72023-10-03 16:30:55 -04002164 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
brian m. carlson72871b132021-04-26 01:02:58 +00002165 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
Taylor Blauad2dd5b2020-02-03 13:18:02 -08002166 final_graph_name = get_split_graph_filename(ctx->odb,
Derrick Stolee6c622f92019-06-18 11:14:27 -07002167 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
Jeff King274bfa72023-10-03 16:30:55 -04002168 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1]);
Derrick Stolee6c622f92019-06-18 11:14:27 -07002169 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
2170
2171 result = rename(ctx->graph_name, final_graph_name);
2172
2173 for (i = 0; i < ctx->num_commit_graphs_after; i++)
Martin Ågrena52cdce2021-01-05 20:23:47 +01002174 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
Derrick Stolee6c622f92019-06-18 11:14:27 -07002175
2176 if (result) {
2177 error(_("failed to rename temporary commit-graph file"));
2178 return -1;
2179 }
2180 }
2181
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002182 commit_lock_file(&lk);
2183
Derrick Stolee238def52019-06-12 06:29:45 -07002184 return 0;
2185}
2186
Derrick Stolee1771be92019-06-18 11:14:29 -07002187static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2188{
Alex Henrie8da02ce2019-09-30 20:29:34 -06002189 struct commit_graph *g;
2190 uint32_t num_commits;
Taylor Blaufdbde822020-04-13 22:04:12 -06002191 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
Derrick Stolee1771be92019-06-18 11:14:29 -07002192 uint32_t i;
2193
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07002194 int max_commits = 0;
2195 int size_mult = 2;
2196
Taylor Blau98bb7962020-09-17 22:59:49 -04002197 if (ctx->opts) {
2198 max_commits = ctx->opts->max_commits;
Derrick Stolee63020f12020-01-02 16:14:14 +00002199
Taylor Blau98bb7962020-09-17 22:59:49 -04002200 if (ctx->opts->size_multiple)
2201 size_mult = ctx->opts->size_multiple;
Taylor Blaufdbde822020-04-13 22:04:12 -06002202
Taylor Blau98bb7962020-09-17 22:59:49 -04002203 flags = ctx->opts->split_flags;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07002204 }
2205
Derrick Stolee1771be92019-06-18 11:14:29 -07002206 g = ctx->r->objects->commit_graph;
Alex Henrie8da02ce2019-09-30 20:29:34 -06002207 num_commits = ctx->commits.nr;
Taylor Blau8a6ac282020-04-13 22:04:17 -06002208 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2209 ctx->num_commit_graphs_after = 1;
2210 else
2211 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
Derrick Stolee1771be92019-06-18 11:14:29 -07002212
Taylor Blau8a6ac282020-04-13 22:04:17 -06002213 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2214 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
Taylor Blau19565d02023-07-12 19:38:11 -04002215 while (g && (g->num_commits <= st_mult(size_mult, num_commits) ||
Taylor Blaufdbde822020-04-13 22:04:12 -06002216 (max_commits && num_commits > max_commits))) {
2217 if (g->odb != ctx->odb)
2218 break;
Derrick Stoleec5230352019-06-18 11:14:30 -07002219
Taylor Blau19565d02023-07-12 19:38:11 -04002220 if (unsigned_add_overflows(num_commits, g->num_commits))
2221 die(_("cannot merge graphs with %"PRIuMAX", "
2222 "%"PRIuMAX" commits"),
2223 (uintmax_t)num_commits,
2224 (uintmax_t)g->num_commits);
Taylor Blaufdbde822020-04-13 22:04:12 -06002225 num_commits += g->num_commits;
2226 g = g->base_graph;
Derrick Stolee1771be92019-06-18 11:14:29 -07002227
Taylor Blaufdbde822020-04-13 22:04:12 -06002228 ctx->num_commit_graphs_after--;
2229 }
Derrick Stolee1771be92019-06-18 11:14:29 -07002230 }
2231
Taylor Blau8a6ac282020-04-13 22:04:17 -06002232 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2233 ctx->new_base_graph = g;
2234 else if (ctx->num_commit_graphs_after != 1)
2235 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2236 "should be 1 with --split=replace");
Derrick Stolee1771be92019-06-18 11:14:29 -07002237
Derrick Stoleec5230352019-06-18 11:14:30 -07002238 if (ctx->num_commit_graphs_after == 2) {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08002239 char *old_graph_name = get_commit_graph_filename(g->odb);
Derrick Stoleec5230352019-06-18 11:14:30 -07002240
2241 if (!strcmp(g->filename, old_graph_name) &&
Taylor Blauad2dd5b2020-02-03 13:18:02 -08002242 g->odb != ctx->odb) {
Derrick Stoleec5230352019-06-18 11:14:30 -07002243 ctx->num_commit_graphs_after = 1;
2244 ctx->new_base_graph = NULL;
2245 }
2246
2247 free(old_graph_name);
2248 }
2249
Taylor Blaub78a5562020-04-23 15:41:09 -06002250 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2251 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
Derrick Stolee1771be92019-06-18 11:14:29 -07002252
2253 for (i = 0; i < ctx->num_commit_graphs_after &&
2254 i < ctx->num_commit_graphs_before; i++)
2255 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2256
2257 i = ctx->num_commit_graphs_before - 1;
2258 g = ctx->r->objects->commit_graph;
2259
2260 while (g) {
2261 if (i < ctx->num_commit_graphs_after)
2262 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2263
Abhishek Kumar1fdc3832021-01-16 18:11:16 +00002264 /*
2265 * If the topmost remaining layer has generation data chunk, the
2266 * resultant layer also has generation data chunk.
2267 */
2268 if (i == ctx->num_commit_graphs_after - 2)
2269 ctx->write_generation_data = !!g->chunk_generation_data;
2270
Derrick Stolee1771be92019-06-18 11:14:29 -07002271 i--;
2272 g = g->base_graph;
2273 }
2274}
2275
2276static void merge_commit_graph(struct write_commit_graph_context *ctx,
2277 struct commit_graph *g)
2278{
2279 uint32_t i;
2280 uint32_t offset = g->num_commits_in_base;
2281
Taylor Blaud76e0a72023-07-12 19:38:13 -04002282 if (unsigned_add_overflows(ctx->commits.nr, g->num_commits))
2283 die(_("cannot merge graph %s, too many commits: %"PRIuMAX),
2284 oid_to_hex(&g->oid),
2285 (uintmax_t)st_add(ctx->commits.nr, g->num_commits));
2286
Derrick Stolee1771be92019-06-18 11:14:29 -07002287 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2288
2289 for (i = 0; i < g->num_commits; i++) {
2290 struct object_id oid;
2291 struct commit *result;
2292
2293 display_progress(ctx->progress, i + 1);
2294
2295 load_oid_from_graph(g, i + offset, &oid);
2296
2297 /* only add commits if they still exist in the repo */
2298 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2299
2300 if (result) {
2301 ctx->commits.list[ctx->commits.nr] = result;
2302 ctx->commits.nr++;
2303 }
2304 }
2305}
2306
2307static int commit_compare(const void *_a, const void *_b)
2308{
2309 const struct commit *a = *(const struct commit **)_a;
2310 const struct commit *b = *(const struct commit **)_b;
2311 return oidcmp(&a->object.oid, &b->object.oid);
2312}
2313
2314static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2315{
Derrick Stolee150f1152020-10-09 20:53:51 +00002316 uint32_t i, dedup_i = 0;
Derrick Stolee1771be92019-06-18 11:14:29 -07002317
2318 if (ctx->report_progress)
2319 ctx->progress = start_delayed_progress(
2320 _("Scanning merged commits"),
2321 ctx->commits.nr);
2322
2323 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2324
2325 ctx->num_extra_edges = 0;
2326 for (i = 0; i < ctx->commits.nr; i++) {
SZEDER Gábor40112242021-09-09 03:10:11 +02002327 display_progress(ctx->progress, i + 1);
Derrick Stolee1771be92019-06-18 11:14:29 -07002328
2329 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2330 &ctx->commits.list[i]->object.oid)) {
Derrick Stolee150f1152020-10-09 20:53:51 +00002331 /*
2332 * Silently ignore duplicates. These were likely
2333 * created due to a commit appearing in multiple
2334 * layers of the chain, which is unexpected but
2335 * not invalid. We should make sure there is a
2336 * unique copy in the new layer.
2337 */
Derrick Stolee1771be92019-06-18 11:14:29 -07002338 } else {
René Scharfe689a1462019-09-15 19:07:44 +02002339 unsigned int num_parents;
Derrick Stolee1771be92019-06-18 11:14:29 -07002340
Derrick Stolee150f1152020-10-09 20:53:51 +00002341 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2342 dedup_i++;
2343
René Scharfe689a1462019-09-15 19:07:44 +02002344 num_parents = commit_list_count(ctx->commits.list[i]->parents);
Derrick Stolee1771be92019-06-18 11:14:29 -07002345 if (num_parents > 2)
Derrick Stoleea35bea42019-08-05 09:43:41 -07002346 ctx->num_extra_edges += num_parents - 1;
Derrick Stolee1771be92019-06-18 11:14:29 -07002347 }
2348 }
2349
Derrick Stolee150f1152020-10-09 20:53:51 +00002350 ctx->commits.nr = dedup_i;
2351
Derrick Stolee1771be92019-06-18 11:14:29 -07002352 stop_progress(&ctx->progress);
2353}
2354
2355static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2356{
2357 struct commit_graph *g = ctx->r->objects->commit_graph;
2358 uint32_t current_graph_number = ctx->num_commit_graphs_before;
Derrick Stolee1771be92019-06-18 11:14:29 -07002359
2360 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2361 current_graph_number--;
2362
René Scharfed68ce902020-02-20 19:49:18 +01002363 if (ctx->report_progress)
2364 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
Derrick Stolee1771be92019-06-18 11:14:29 -07002365
2366 merge_commit_graph(ctx, g);
2367 stop_progress(&ctx->progress);
Derrick Stolee1771be92019-06-18 11:14:29 -07002368
2369 g = g->base_graph;
2370 }
2371
2372 if (g) {
2373 ctx->new_base_graph = g;
2374 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2375 }
2376
2377 if (ctx->new_base_graph)
2378 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2379
2380 sort_and_scan_merged_commits(ctx);
2381}
2382
Derrick Stolee8d840972019-06-18 11:14:31 -07002383static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2384{
2385 uint32_t i;
2386 time_t now = time(NULL);
2387
2388 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2389 struct stat st;
2390 struct utimbuf updated_time;
2391
Ævar Arnfjörð Bjarmason7c898552022-05-12 15:32:17 -07002392 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2393 continue;
Derrick Stolee8d840972019-06-18 11:14:31 -07002394
2395 updated_time.actime = st.st_atime;
2396 updated_time.modtime = now;
2397 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2398 }
2399}
2400
2401static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2402{
2403 struct strbuf path = STRBUF_INIT;
2404 DIR *dir;
2405 struct dirent *de;
2406 size_t dirnamelen;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07002407 timestamp_t expire_time = time(NULL);
2408
Taylor Blau98bb7962020-09-17 22:59:49 -04002409 if (ctx->opts && ctx->opts->expire_time)
2410 expire_time = ctx->opts->expire_time;
Derrick Stoleeba411122019-06-18 11:14:33 -07002411 if (!ctx->split) {
Derrick Stolee663b2b12020-09-17 18:11:46 +00002412 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
Derrick Stoleeba411122019-06-18 11:14:33 -07002413 unlink(chain_file_name);
2414 free(chain_file_name);
2415 ctx->num_commit_graphs_after = 0;
2416 }
Derrick Stolee8d840972019-06-18 11:14:31 -07002417
Taylor Blau0bd52e22020-02-03 21:51:50 -08002418 strbuf_addstr(&path, ctx->odb->path);
Derrick Stolee8d840972019-06-18 11:14:31 -07002419 strbuf_addstr(&path, "/info/commit-graphs");
2420 dir = opendir(path.buf);
2421
René Scharfe0aa6bce2019-08-07 13:15:02 +02002422 if (!dir)
2423 goto out;
Derrick Stolee8d840972019-06-18 11:14:31 -07002424
2425 strbuf_addch(&path, '/');
2426 dirnamelen = path.len;
2427 while ((de = readdir(dir)) != NULL) {
2428 struct stat st;
2429 uint32_t i, found = 0;
2430
2431 strbuf_setlen(&path, dirnamelen);
2432 strbuf_addstr(&path, de->d_name);
2433
Ævar Arnfjörð Bjarmason7c898552022-05-12 15:32:17 -07002434 if (stat(path.buf, &st) < 0)
2435 continue;
Derrick Stolee8d840972019-06-18 11:14:31 -07002436
2437 if (st.st_mtime > expire_time)
2438 continue;
2439 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2440 continue;
2441
2442 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2443 if (!strcmp(ctx->commit_graph_filenames_after[i],
2444 path.buf)) {
2445 found = 1;
2446 break;
2447 }
2448 }
2449
2450 if (!found)
2451 unlink(path.buf);
Derrick Stolee8d840972019-06-18 11:14:31 -07002452 }
René Scharfe0aa6bce2019-08-07 13:15:02 +02002453
2454out:
Miaoqian Lin12f1ae52022-09-19 18:14:40 +04002455 if(dir)
2456 closedir(dir);
René Scharfe0aa6bce2019-08-07 13:15:02 +02002457 strbuf_release(&path);
Derrick Stolee8d840972019-06-18 11:14:31 -07002458}
2459
Taylor Blau0bd52e22020-02-03 21:51:50 -08002460int write_commit_graph(struct object_directory *odb,
Ævar Arnfjörð Bjarmason4a047902022-03-04 19:32:12 +01002461 const struct string_list *const pack_indexes,
Taylor Blau6830c362020-04-13 22:04:25 -06002462 struct oidset *commits,
SZEDER Gábor39d88312019-08-05 10:02:39 +02002463 enum commit_graph_write_flags flags,
Taylor Blau98bb7962020-09-17 22:59:49 -04002464 const struct commit_graph_opts *opts)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002465{
Derrick Stoleec7ef8fe2021-02-25 18:19:42 +00002466 struct repository *r = the_repository;
Derrick Stoleec9905be2019-06-12 06:29:40 -07002467 struct write_commit_graph_context *ctx;
Jeff King1cbdbf32020-12-07 14:11:02 -05002468 uint32_t i;
Derrick Stoleee103f722019-06-12 06:29:37 -07002469 int res = 0;
Taylor Blau8a6ac282020-04-13 22:04:17 -06002470 int replace = 0;
Taylor Blau9a7a9ed2020-09-16 14:07:46 -04002471 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +00002472 struct topo_level_slab topo_levels;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002473
Derrick Stoleec7ef8fe2021-02-25 18:19:42 +00002474 prepare_repo_settings(r);
2475 if (!r->settings.core_commit_graph) {
Derrick Stolee85102ac2020-10-09 20:53:52 +00002476 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2477 return 0;
2478 }
Derrick Stoleec7ef8fe2021-02-25 18:19:42 +00002479 if (!commit_graph_compatible(r))
Derrick Stoleee103f722019-06-12 06:29:37 -07002480 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002481
René Scharfeca56dad2021-03-13 17:17:22 +01002482 CALLOC_ARRAY(ctx, 1);
Derrick Stoleec7ef8fe2021-02-25 18:19:42 +00002483 ctx->r = r;
Taylor Blau0bd52e22020-02-03 21:51:50 -08002484 ctx->odb = odb;
SZEDER Gábor39d88312019-08-05 10:02:39 +02002485 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2486 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2487 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
Taylor Blau98bb7962020-09-17 22:59:49 -04002488 ctx->opts = opts;
Garima Singhf97b9322020-03-30 00:31:28 +00002489 ctx->total_bloom_filter_data_size = 0;
Derrick Stolee702110a2021-02-25 18:19:43 +00002490 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
Abhishek Kumare8b63002021-01-16 18:11:15 +00002491 ctx->num_generation_data_overflows = 0;
Derrick Stolee6c622f92019-06-18 11:14:27 -07002492
Taylor Blau9a7a9ed2020-09-16 14:07:46 -04002493 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2494 bloom_settings.bits_per_entry);
2495 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2496 bloom_settings.num_hashes);
2497 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2498 bloom_settings.max_changed_paths);
2499 ctx->bloom_settings = &bloom_settings;
2500
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +00002501 init_topo_level_slab(&topo_levels);
2502 ctx->topo_levels = &topo_levels;
2503
Derrick Stoleebc50d6c2021-02-02 03:01:23 +00002504 prepare_commit_graph(ctx->r);
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +00002505 if (ctx->r->objects->commit_graph) {
2506 struct commit_graph *g = ctx->r->objects->commit_graph;
2507
2508 while (g) {
2509 g->topo_levels = &topo_levels;
2510 g = g->base_graph;
2511 }
2512 }
2513
Derrick Stolee0087a872020-07-01 13:27:24 +00002514 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2515 ctx->changed_paths = 1;
2516 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2517 struct commit_graph *g;
Derrick Stolee0087a872020-07-01 13:27:24 +00002518
2519 g = ctx->r->objects->commit_graph;
2520
2521 /* We have changed-paths already. Keep them in the next graph */
2522 if (g && g->chunk_bloom_data) {
2523 ctx->changed_paths = 1;
2524 ctx->bloom_settings = g->bloom_filter_settings;
2525 }
2526 }
2527
Derrick Stolee6c622f92019-06-18 11:14:27 -07002528 if (ctx->split) {
Derrick Stoleebc50d6c2021-02-02 03:01:23 +00002529 struct commit_graph *g = ctx->r->objects->commit_graph;
Derrick Stolee6c622f92019-06-18 11:14:27 -07002530
2531 while (g) {
2532 ctx->num_commit_graphs_before++;
2533 g = g->base_graph;
2534 }
2535
2536 if (ctx->num_commit_graphs_before) {
2537 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2538 i = ctx->num_commit_graphs_before;
2539 g = ctx->r->objects->commit_graph;
2540
2541 while (g) {
2542 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2543 g = g->base_graph;
2544 }
2545 }
Taylor Blau8a6ac282020-04-13 22:04:17 -06002546
Taylor Blau98bb7962020-09-17 22:59:49 -04002547 if (ctx->opts)
2548 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
Derrick Stolee6c622f92019-06-18 11:14:27 -07002549 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002550
Ævar Arnfjörð Bjarmasonafe27c82023-03-28 15:58:52 +02002551 ctx->approx_nr_objects = repo_approximate_object_count(the_repository);
Derrick Stoleec9905be2019-06-12 06:29:40 -07002552
Derrick Stoleec9905be2019-06-12 06:29:40 -07002553 if (ctx->append && ctx->r->objects->commit_graph) {
2554 struct commit_graph *g = ctx->r->objects->commit_graph;
2555 for (i = 0; i < g->num_commits; i++) {
Jeff Kinga5f1c442020-12-07 14:11:05 -05002556 struct object_id oid;
Taylor Blau588af1b2023-07-12 19:38:16 -04002557 oidread(&oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
Jeff Kinga5f1c442020-12-07 14:11:05 -05002558 oid_array_append(&ctx->oids, &oid);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002559 }
2560 }
2561
2562 if (pack_indexes) {
Garima Singh3d112752020-03-30 00:31:30 +00002563 ctx->order_by_pack = 1;
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07002564 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2565 goto cleanup;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002566 }
2567
Taylor Blau6830c362020-04-13 22:04:25 -06002568 if (commits) {
2569 if ((res = fill_oids_from_commits(ctx, commits)))
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02002570 goto cleanup;
2571 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002572
Junio C Hamano9b6606f2020-05-01 13:39:53 -07002573 if (!pack_indexes && !commits) {
Garima Singh3d112752020-03-30 00:31:30 +00002574 ctx->order_by_pack = 1;
Derrick Stoleeb2c83062019-06-12 06:29:42 -07002575 fill_oids_from_all_packs(ctx);
Garima Singh3d112752020-03-30 00:31:30 +00002576 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002577
Derrick Stoleec9905be2019-06-12 06:29:40 -07002578 close_reachable(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002579
Derrick Stoleef998d542019-06-12 06:29:44 -07002580 copy_oids_to_commits(ctx);
Derrick Stolee1373e542018-06-27 09:24:39 -04002581
Derrick Stoleec9905be2019-06-12 06:29:40 -07002582 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
Derrick Stoleee103f722019-06-12 06:29:37 -07002583 error(_("too many commits to write graph"));
2584 res = -1;
2585 goto cleanup;
2586 }
Derrick Stolee283e68c2018-06-27 09:24:32 -04002587
Taylor Blau8a6ac282020-04-13 22:04:17 -06002588 if (!ctx->commits.nr && !replace)
Derrick Stolee6c622f92019-06-18 11:14:27 -07002589 goto cleanup;
2590
Derrick Stolee1771be92019-06-18 11:14:29 -07002591 if (ctx->split) {
2592 split_graph_merge_strategy(ctx);
2593
Taylor Blau8a6ac282020-04-13 22:04:17 -06002594 if (!replace)
2595 merge_commit_graphs(ctx);
Derrick Stolee1771be92019-06-18 11:14:29 -07002596 } else
Derrick Stolee6c622f92019-06-18 11:14:27 -07002597 ctx->num_commit_graphs_after = 1;
2598
Derrick Stoleefde55b02021-02-02 03:01:22 +00002599 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
Abhishek Kumar1fdc3832021-01-16 18:11:16 +00002600
Derrick Stolee9c2c0a82021-02-02 03:01:21 +00002601 compute_topological_levels(ctx);
2602 if (ctx->write_generation_data)
2603 compute_generation_numbers(ctx);
Derrick Stolee9bda8462018-06-27 09:24:35 -04002604
Garima Singhf97b9322020-03-30 00:31:28 +00002605 if (ctx->changed_paths)
2606 compute_bloom_filters(ctx);
2607
Derrick Stolee238def52019-06-12 06:29:45 -07002608 res = write_commit_graph_file(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002609
Derrick Stoleeba411122019-06-18 11:14:33 -07002610 if (ctx->split)
Derrick Stolee8d840972019-06-18 11:14:31 -07002611 mark_commit_graphs(ctx);
Derrick Stoleeba411122019-06-18 11:14:33 -07002612
2613 expire_commit_graphs(ctx);
Derrick Stolee8d840972019-06-18 11:14:31 -07002614
Derrick Stoleee103f722019-06-12 06:29:37 -07002615cleanup:
Derrick Stolee238def52019-06-12 06:29:45 -07002616 free(ctx->graph_name);
Jeff Kingd9c84c62023-10-03 16:31:11 -04002617 free(ctx->base_graph_name);
Derrick Stoleec9905be2019-06-12 06:29:40 -07002618 free(ctx->commits.list);
Jeff Kinga5f1c442020-12-07 14:11:05 -05002619 oid_array_clear(&ctx->oids);
Andrzej Huntbf4bb9f2021-02-19 20:13:10 +00002620 clear_topo_level_slab(&topo_levels);
Derrick Stolee6c622f92019-06-18 11:14:27 -07002621
Patrick Steinhardt4efa9302023-12-18 11:02:28 +01002622 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2623 free(ctx->commit_graph_filenames_before[i]);
2624 free(ctx->commit_graph_filenames_before);
Derrick Stolee6c622f92019-06-18 11:14:27 -07002625
Patrick Steinhardt4efa9302023-12-18 11:02:28 +01002626 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2627 free(ctx->commit_graph_filenames_after[i]);
2628 free(ctx->commit_graph_hash_after[i]);
Derrick Stolee6c622f92019-06-18 11:14:27 -07002629 }
Patrick Steinhardt4efa9302023-12-18 11:02:28 +01002630 free(ctx->commit_graph_filenames_after);
2631 free(ctx->commit_graph_hash_after);
Derrick Stolee6c622f92019-06-18 11:14:27 -07002632
Derrick Stoleec9905be2019-06-12 06:29:40 -07002633 free(ctx);
Derrick Stoleee103f722019-06-12 06:29:37 -07002634
2635 return res;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002636}
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002637
2638#define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2639static int verify_commit_graph_error;
2640
Ævar Arnfjörð Bjarmason48ca53c2021-07-13 10:05:18 +02002641__attribute__((format (printf, 1, 2)))
Derrick Stolee283e68c2018-06-27 09:24:32 -04002642static void graph_report(const char *fmt, ...)
2643{
2644 va_list ap;
2645
2646 verify_commit_graph_error = 1;
2647 va_start(ap, fmt);
2648 vfprintf(stderr, fmt, ap);
2649 fprintf(stderr, "\n");
2650 va_end(ap);
2651}
2652
Taylor Blau15316a42021-06-23 14:39:09 -04002653static int commit_graph_checksum_valid(struct commit_graph *g)
2654{
2655 return hashfile_checksum_valid(g->data, g->data_len);
2656}
2657
Taylor Blaueb319d62023-07-07 20:31:36 -04002658static int verify_one_commit_graph(struct repository *r,
2659 struct commit_graph *g,
Taylor Blau9281cd02023-07-07 20:31:45 -04002660 struct progress *progress,
2661 uint64_t *seen)
Derrick Stolee283e68c2018-06-27 09:24:32 -04002662{
Derrick Stolee9bda8462018-06-27 09:24:35 -04002663 uint32_t i, cur_fanout_pos = 0;
brian m. carlson72871b132021-04-26 01:02:58 +00002664 struct object_id prev_oid, cur_oid;
Taylor Blaudb6044d2023-08-21 17:34:42 -04002665 struct commit *seen_gen_zero = NULL;
2666 struct commit *seen_gen_non_zero = NULL;
Derrick Stolee283e68c2018-06-27 09:24:32 -04002667
Taylor Blau15316a42021-06-23 14:39:09 -04002668 if (!commit_graph_checksum_valid(g)) {
Derrick Stolee41df0e32018-06-27 09:24:42 -04002669 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2670 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2671 }
2672
Derrick Stolee9bda8462018-06-27 09:24:35 -04002673 for (i = 0; i < g->num_commits; i++) {
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002674 struct commit *graph_commit;
2675
Taylor Blau9a25cad2023-07-12 19:38:19 -04002676 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
Derrick Stolee9bda8462018-06-27 09:24:35 -04002677
2678 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002679 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04002680 oid_to_hex(&prev_oid),
2681 oid_to_hex(&cur_oid));
2682
2683 oidcpy(&prev_oid, &cur_oid);
2684
2685 while (cur_oid.hash[0] > cur_fanout_pos) {
2686 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2687
2688 if (i != fanout_value)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002689 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04002690 cur_fanout_pos, fanout_value, i);
2691 cur_fanout_pos++;
2692 }
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002693
Junio C Hamano82952962018-07-17 15:46:19 -07002694 graph_commit = lookup_commit(r, &cur_oid);
Stefan Beller4f542b72018-12-14 16:09:39 -08002695 if (!parse_commit_in_graph_one(r, g, graph_commit))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002696 graph_report(_("failed to parse commit %s from commit-graph"),
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002697 oid_to_hex(&cur_oid));
Derrick Stolee9bda8462018-06-27 09:24:35 -04002698 }
2699
2700 while (cur_fanout_pos < 256) {
2701 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2702
2703 if (g->num_commits != fanout_value)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002704 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04002705 cur_fanout_pos, fanout_value, i);
2706
2707 cur_fanout_pos++;
2708 }
2709
Derrick Stolee41df0e32018-06-27 09:24:42 -04002710 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
Derrick Stolee96af91d2018-06-27 09:24:36 -04002711 return verify_commit_graph_error;
2712
2713 for (i = 0; i < g->num_commits; i++) {
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002714 struct commit *graph_commit, *odb_commit;
Derrick Stolee53614b12018-06-27 09:24:38 -04002715 struct commit_list *graph_parents, *odb_parents;
Abhishek Kumard7f92782021-01-16 18:11:13 +00002716 timestamp_t max_generation = 0;
2717 timestamp_t generation;
Derrick Stolee96af91d2018-06-27 09:24:36 -04002718
Taylor Blau9281cd02023-07-07 20:31:45 -04002719 display_progress(progress, ++(*seen));
Taylor Blau9a25cad2023-07-12 19:38:19 -04002720 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
Derrick Stolee96af91d2018-06-27 09:24:36 -04002721
Junio C Hamano82952962018-07-17 15:46:19 -07002722 graph_commit = lookup_commit(r, &cur_oid);
Jeff Kinga3785092019-06-20 03:41:21 -04002723 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +02002724 if (repo_parse_commit_internal(r, odb_commit, 0, 0)) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002725 graph_report(_("failed to parse commit %s from object database for commit-graph"),
Derrick Stolee96af91d2018-06-27 09:24:36 -04002726 oid_to_hex(&cur_oid));
2727 continue;
2728 }
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002729
Stefan Beller4f542b72018-12-14 16:09:39 -08002730 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002731 get_commit_tree_oid(odb_commit)))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002732 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002733 oid_to_hex(&cur_oid),
2734 oid_to_hex(get_commit_tree_oid(graph_commit)),
2735 oid_to_hex(get_commit_tree_oid(odb_commit)));
Derrick Stolee53614b12018-06-27 09:24:38 -04002736
2737 graph_parents = graph_commit->parents;
2738 odb_parents = odb_commit->parents;
2739
2740 while (graph_parents) {
Junio C Hamanoafe8a902022-05-02 09:50:37 -07002741 if (!odb_parents) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002742 graph_report(_("commit-graph parent list for commit %s is too long"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002743 oid_to_hex(&cur_oid));
2744 break;
2745 }
2746
Derrick Stolee3da4b602019-06-18 11:14:32 -07002747 /* parse parent in case it is in a base graph */
2748 parse_commit_in_graph_one(r, g, graph_parents->item);
2749
Jeff King9001dc22018-08-28 17:22:48 -04002750 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002751 graph_report(_("commit-graph parent for %s is %s != %s"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002752 oid_to_hex(&cur_oid),
2753 oid_to_hex(&graph_parents->item->object.oid),
2754 oid_to_hex(&odb_parents->item->object.oid));
2755
Taylor Blau868c9912023-08-21 17:34:34 -04002756 generation = commit_graph_generation_from_graph(graph_parents->item);
Abhishek Kumarc752ad02020-06-17 14:44:11 +05302757 if (generation > max_generation)
2758 max_generation = generation;
Derrick Stolee1373e542018-06-27 09:24:39 -04002759
Derrick Stolee53614b12018-06-27 09:24:38 -04002760 graph_parents = graph_parents->next;
2761 odb_parents = odb_parents->next;
2762 }
2763
Junio C Hamanoafe8a902022-05-02 09:50:37 -07002764 if (odb_parents)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002765 graph_report(_("commit-graph parent list for commit %s terminates early"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002766 oid_to_hex(&cur_oid));
Derrick Stolee1373e542018-06-27 09:24:39 -04002767
Taylor Blaudb6044d2023-08-21 17:34:42 -04002768 if (commit_graph_generation_from_graph(graph_commit))
2769 seen_gen_non_zero = graph_commit;
2770 else
2771 seen_gen_zero = graph_commit;
Derrick Stolee1373e542018-06-27 09:24:39 -04002772
Taylor Blaudb6044d2023-08-21 17:34:42 -04002773 if (seen_gen_zero)
Derrick Stolee1373e542018-06-27 09:24:39 -04002774 continue;
2775
2776 /*
Abhishek Kumare8b63002021-01-16 18:11:15 +00002777 * If we are using topological level and one of our parents has
2778 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2779 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2780 * in the following condition.
Derrick Stolee1373e542018-06-27 09:24:39 -04002781 */
Abhishek Kumar1fdc3832021-01-16 18:11:16 +00002782 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
Derrick Stolee1373e542018-06-27 09:24:39 -04002783 max_generation--;
2784
Abhishek Kumarc752ad02020-06-17 14:44:11 +05302785 generation = commit_graph_generation(graph_commit);
Abhishek Kumare8b63002021-01-16 18:11:15 +00002786 if (generation < max_generation + 1)
2787 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
Derrick Stolee1373e542018-06-27 09:24:39 -04002788 oid_to_hex(&cur_oid),
Abhishek Kumarc752ad02020-06-17 14:44:11 +05302789 generation,
Derrick Stolee1373e542018-06-27 09:24:39 -04002790 max_generation + 1);
Derrick Stolee88968eb2018-06-27 09:24:40 -04002791
2792 if (graph_commit->date != odb_commit->date)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002793 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
Derrick Stolee88968eb2018-06-27 09:24:40 -04002794 oid_to_hex(&cur_oid),
2795 graph_commit->date,
2796 odb_commit->date);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002797 }
2798
Taylor Blaudb6044d2023-08-21 17:34:42 -04002799 if (seen_gen_zero && seen_gen_non_zero)
2800 graph_report(_("commit-graph has both zero and non-zero "
2801 "generations (e.g., commits '%s' and '%s')"),
2802 oid_to_hex(&seen_gen_zero->object.oid),
2803 oid_to_hex(&seen_gen_non_zero->object.oid));
2804
Taylor Blaueb319d62023-07-07 20:31:36 -04002805 return verify_commit_graph_error;
2806}
2807
2808int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2809{
Taylor Blau9281cd02023-07-07 20:31:45 -04002810 struct progress *progress = NULL;
Taylor Blaueb319d62023-07-07 20:31:36 -04002811 int local_error = 0;
Taylor Blau9281cd02023-07-07 20:31:45 -04002812 uint64_t seen = 0;
Taylor Blaueb319d62023-07-07 20:31:36 -04002813
2814 if (!g) {
2815 graph_report("no commit-graph file loaded");
2816 return 1;
2817 }
2818
Taylor Blau9281cd02023-07-07 20:31:45 -04002819 if (flags & COMMIT_GRAPH_WRITE_PROGRESS) {
2820 uint64_t total = g->num_commits;
2821 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW))
2822 total += g->num_commits_in_base;
Taylor Blau72488572023-07-07 20:31:42 -04002823
Taylor Blau9281cd02023-07-07 20:31:45 -04002824 progress = start_progress(_("Verifying commits in commit graph"),
2825 total);
2826 }
2827
2828 for (; g; g = g->base_graph) {
2829 local_error |= verify_one_commit_graph(r, g, progress, &seen);
Taylor Blauf5facaa2023-07-07 20:31:39 -04002830 if (flags & COMMIT_GRAPH_VERIFY_SHALLOW)
2831 break;
2832 }
Derrick Stolee3da4b602019-06-18 11:14:32 -07002833
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002834 stop_progress(&progress);
2835
Derrick Stolee3da4b602019-06-18 11:14:32 -07002836 return local_error;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002837}
2838
2839void free_commit_graph(struct commit_graph *g)
2840{
Jeff King09a75ab2023-10-03 16:29:30 -04002841 while (g) {
2842 struct commit_graph *next = g->base_graph;
2843
2844 if (g->data)
2845 munmap((void *)g->data, g->data_len);
2846 free(g->filename);
2847 free(g->bloom_filter_settings);
2848 free(g);
2849
2850 g = next;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002851 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002852}
Jeff King6abada12019-09-12 10:44:45 -04002853
2854void disable_commit_graph(struct repository *r)
2855{
2856 r->commit_graph_disabled = 1;
2857}