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