]>
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" | |
08fd81c9 | 22 | |
b23ea979 DS |
23 | void git_test_write_commit_graph_or_die(void) |
24 | { | |
25 | int flags = 0; | |
26 | if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0)) | |
27 | return; | |
28 | ||
29 | if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0)) | |
30 | flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS; | |
31 | ||
32 | if (write_commit_graph_reachable(the_repository->objects->odb, | |
33 | flags, NULL)) | |
34 | die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH"); | |
35 | } | |
36 | ||
08fd81c9 DS |
37 | #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */ |
38 | #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */ | |
39 | #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */ | |
40 | #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */ | |
e8b63005 AK |
41 | #define GRAPH_CHUNKID_GENERATION_DATA 0x47444154 /* "GDAT" */ |
42 | #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f56 /* "GDOV" */ | |
5af7417b | 43 | #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */ |
76ffbca7 GS |
44 | #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */ |
45 | #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */ | |
118bd570 | 46 | #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */ |
e8b63005 | 47 | #define MAX_NUM_CHUNKS 9 |
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 | ||
448a39e6 DS |
617 | /* |
618 | * returns 1 if and only if all graphs in the chain have | |
619 | * corrected commit dates stored in the generation_data chunk. | |
620 | */ | |
621 | static int validate_mixed_generation_chain(struct commit_graph *g) | |
1fdc383c | 622 | { |
448a39e6 DS |
623 | int read_generation_data = 1; |
624 | struct commit_graph *p = g; | |
1fdc383c | 625 | |
448a39e6 DS |
626 | while (read_generation_data && p) { |
627 | read_generation_data = p->read_generation_data; | |
628 | p = p->base_graph; | |
629 | } | |
1fdc383c | 630 | |
448a39e6 DS |
631 | if (read_generation_data) |
632 | return 1; | |
1fdc383c AK |
633 | |
634 | while (g) { | |
448a39e6 | 635 | g->read_generation_data = 0; |
1fdc383c AK |
636 | g = g->base_graph; |
637 | } | |
448a39e6 DS |
638 | |
639 | return 0; | |
1fdc383c AK |
640 | } |
641 | ||
13c24992 TB |
642 | struct commit_graph *read_commit_graph_one(struct repository *r, |
643 | struct object_directory *odb) | |
5c84b339 | 644 | { |
13c24992 | 645 | struct commit_graph *g = load_commit_graph_v1(r, odb); |
5c84b339 DS |
646 | |
647 | if (!g) | |
13c24992 | 648 | g = load_commit_graph_chain(r, odb); |
5c84b339 | 649 | |
1fdc383c AK |
650 | validate_mixed_generation_chain(g); |
651 | ||
5c84b339 | 652 | return g; |
61df89c8 ÆAB |
653 | } |
654 | ||
13c24992 TB |
655 | static void prepare_commit_graph_one(struct repository *r, |
656 | struct object_directory *odb) | |
177722b3 | 657 | { |
177722b3 | 658 | |
dade47c0 | 659 | if (r->objects->commit_graph) |
177722b3 DS |
660 | return; |
661 | ||
13c24992 | 662 | r->objects->commit_graph = read_commit_graph_one(r, odb); |
177722b3 DS |
663 | } |
664 | ||
5faf357b JT |
665 | /* |
666 | * Return 1 if commit_graph is non-NULL, and 0 otherwise. | |
667 | * | |
15beaaa3 | 668 | * On the first invocation, this function attempts to load the commit |
5faf357b JT |
669 | * graph if the_repository is configured to have one. |
670 | */ | |
dade47c0 | 671 | static int prepare_commit_graph(struct repository *r) |
177722b3 | 672 | { |
263db403 | 673 | struct object_directory *odb; |
dade47c0 | 674 | |
6abada18 JK |
675 | /* |
676 | * This must come before the "already attempted?" check below, because | |
677 | * we want to disable even an already-loaded graph file. | |
678 | */ | |
679 | if (r->commit_graph_disabled) | |
680 | return 0; | |
43d35618 | 681 | |
dade47c0 JT |
682 | if (r->objects->commit_graph_attempted) |
683 | return !!r->objects->commit_graph; | |
684 | r->objects->commit_graph_attempted = 1; | |
685 | ||
7211b9e7 DS |
686 | prepare_repo_settings(r); |
687 | ||
859fdc0c | 688 | if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) && |
7211b9e7 | 689 | r->settings.core_commit_graph != 1) |
dade47c0 JT |
690 | /* |
691 | * This repository is not configured to use commit graphs, so | |
692 | * do not load one. (But report commit_graph_attempted anyway | |
693 | * so that commit graph loading is not attempted again for this | |
694 | * repository.) | |
695 | */ | |
5faf357b JT |
696 | return 0; |
697 | ||
d6538246 DS |
698 | if (!commit_graph_compatible(r)) |
699 | return 0; | |
700 | ||
dade47c0 | 701 | prepare_alt_odb(r); |
f0eaf638 | 702 | for (odb = r->objects->odb; |
263db403 JK |
703 | !r->objects->commit_graph && odb; |
704 | odb = odb->next) | |
13c24992 | 705 | prepare_commit_graph_one(r, odb); |
dade47c0 | 706 | return !!r->objects->commit_graph; |
177722b3 DS |
707 | } |
708 | ||
6cc01743 DS |
709 | int generation_numbers_enabled(struct repository *r) |
710 | { | |
711 | uint32_t first_generation; | |
712 | struct commit_graph *g; | |
713 | if (!prepare_commit_graph(r)) | |
714 | return 0; | |
715 | ||
716 | g = r->objects->commit_graph; | |
717 | ||
718 | if (!g->num_commits) | |
719 | return 0; | |
720 | ||
721 | first_generation = get_be32(g->chunk_commit_data + | |
722 | g->hash_len + 8) >> 2; | |
723 | ||
724 | return !!first_generation; | |
725 | } | |
726 | ||
8d00d7c3 AK |
727 | int corrected_commit_dates_enabled(struct repository *r) |
728 | { | |
729 | struct commit_graph *g; | |
730 | if (!prepare_commit_graph(r)) | |
731 | return 0; | |
732 | ||
733 | g = r->objects->commit_graph; | |
734 | ||
735 | if (!g->num_commits) | |
736 | return 0; | |
737 | ||
738 | return g->read_generation_data; | |
739 | } | |
740 | ||
4f364405 TB |
741 | struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r) |
742 | { | |
743 | struct commit_graph *g = r->objects->commit_graph; | |
744 | while (g) { | |
745 | if (g->bloom_filter_settings) | |
746 | return g->bloom_filter_settings; | |
747 | g = g->base_graph; | |
748 | } | |
749 | return NULL; | |
750 | } | |
751 | ||
d4f4d60f DS |
752 | static void close_commit_graph_one(struct commit_graph *g) |
753 | { | |
754 | if (!g) | |
755 | return; | |
756 | ||
757 | close_commit_graph_one(g->base_graph); | |
758 | free_commit_graph(g); | |
759 | } | |
760 | ||
c3a3a964 | 761 | void close_commit_graph(struct raw_object_store *o) |
177722b3 | 762 | { |
d4f4d60f | 763 | close_commit_graph_one(o->commit_graph); |
c3a3a964 | 764 | o->commit_graph = NULL; |
177722b3 DS |
765 | } |
766 | ||
767 | static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos) | |
768 | { | |
769 | return bsearch_hash(oid->hash, g->chunk_oid_fanout, | |
770 | g->chunk_oid_lookup, g->hash_len, pos); | |
771 | } | |
772 | ||
d4f4d60f DS |
773 | static void load_oid_from_graph(struct commit_graph *g, |
774 | uint32_t pos, | |
775 | struct object_id *oid) | |
776 | { | |
777 | uint32_t lex_index; | |
778 | ||
779 | while (g && pos < g->num_commits_in_base) | |
780 | g = g->base_graph; | |
781 | ||
782 | if (!g) | |
783 | BUG("NULL commit-graph"); | |
784 | ||
785 | if (pos >= g->num_commits + g->num_commits_in_base) | |
786 | die(_("invalid commit position. commit-graph is likely corrupt")); | |
787 | ||
788 | lex_index = pos - g->num_commits_in_base; | |
789 | ||
790 | hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index); | |
791 | } | |
792 | ||
4f542b7a SB |
793 | static struct commit_list **insert_parent_or_die(struct repository *r, |
794 | struct commit_graph *g, | |
d4f4d60f | 795 | uint32_t pos, |
177722b3 DS |
796 | struct commit_list **pptr) |
797 | { | |
798 | struct commit *c; | |
799 | struct object_id oid; | |
96af91d4 | 800 | |
d4f4d60f DS |
801 | if (pos >= g->num_commits + g->num_commits_in_base) |
802 | die("invalid parent position %"PRIu32, pos); | |
53614b13 | 803 | |
d4f4d60f | 804 | load_oid_from_graph(g, pos, &oid); |
4f542b7a | 805 | c = lookup_commit(r, &oid); |
177722b3 | 806 | if (!c) |
4f5b532d | 807 | die(_("could not find commit %s"), oid_to_hex(&oid)); |
c49c82aa | 808 | commit_graph_data_at(c)->graph_pos = pos; |
177722b3 DS |
809 | return &commit_list_insert(c, pptr)->next; |
810 | } | |
811 | ||
e2838d85 DS |
812 | static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos) |
813 | { | |
d4f4d60f | 814 | const unsigned char *commit_data; |
c752ad09 | 815 | struct commit_graph_data *graph_data; |
e8b63005 AK |
816 | uint32_t lex_index, offset_pos; |
817 | uint64_t date_high, date_low, offset; | |
d4f4d60f DS |
818 | |
819 | while (pos < g->num_commits_in_base) | |
820 | g = g->base_graph; | |
821 | ||
f90fca63 AK |
822 | if (pos >= g->num_commits + g->num_commits_in_base) |
823 | die(_("invalid commit position. commit-graph is likely corrupt")); | |
824 | ||
d4f4d60f DS |
825 | lex_index = pos - g->num_commits_in_base; |
826 | commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index; | |
c752ad09 AK |
827 | |
828 | graph_data = commit_graph_data_at(item); | |
829 | graph_data->graph_pos = pos; | |
f90fca63 AK |
830 | |
831 | date_high = get_be32(commit_data + g->hash_len + 8) & 0x3; | |
832 | date_low = get_be32(commit_data + g->hash_len + 12); | |
833 | item->date = (timestamp_t)((date_high << 32) | date_low); | |
834 | ||
1fdc383c | 835 | if (g->read_generation_data) { |
e8b63005 AK |
836 | offset = (timestamp_t)get_be32(g->chunk_generation_data + sizeof(uint32_t) * lex_index); |
837 | ||
838 | if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) { | |
839 | if (!g->chunk_generation_data_overflow) | |
840 | die(_("commit-graph requires overflow generation data but has none")); | |
841 | ||
842 | offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW; | |
843 | graph_data->generation = get_be64(g->chunk_generation_data_overflow + 8 * offset_pos); | |
844 | } else | |
845 | graph_data->generation = item->date + offset; | |
846 | } else | |
847 | graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2; | |
72a2bfca AK |
848 | |
849 | if (g->topo_levels) | |
850 | *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2; | |
e2838d85 DS |
851 | } |
852 | ||
a133c40b NTND |
853 | static inline void set_commit_tree(struct commit *c, struct tree *t) |
854 | { | |
855 | c->maybe_tree = t; | |
856 | } | |
857 | ||
4f542b7a SB |
858 | static int fill_commit_in_graph(struct repository *r, |
859 | struct commit *item, | |
860 | struct commit_graph *g, uint32_t pos) | |
177722b3 | 861 | { |
177722b3 DS |
862 | uint32_t edge_value; |
863 | uint32_t *parent_data_ptr; | |
177722b3 | 864 | struct commit_list **pptr; |
d4f4d60f DS |
865 | const unsigned char *commit_data; |
866 | uint32_t lex_index; | |
177722b3 | 867 | |
d4f4d60f DS |
868 | while (pos < g->num_commits_in_base) |
869 | g = g->base_graph; | |
870 | ||
f90fca63 | 871 | fill_commit_graph_info(item, g, pos); |
d4f4d60f | 872 | |
d4f4d60f | 873 | lex_index = pos - g->num_commits_in_base; |
d4f4d60f DS |
874 | commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index; |
875 | ||
876 | item->object.parsed = 1; | |
177722b3 | 877 | |
a133c40b | 878 | set_commit_tree(item, NULL); |
177722b3 | 879 | |
177722b3 DS |
880 | pptr = &item->parents; |
881 | ||
882 | edge_value = get_be32(commit_data + g->hash_len); | |
883 | if (edge_value == GRAPH_PARENT_NONE) | |
884 | return 1; | |
4f542b7a | 885 | pptr = insert_parent_or_die(r, g, edge_value, pptr); |
177722b3 DS |
886 | |
887 | edge_value = get_be32(commit_data + g->hash_len + 4); | |
888 | if (edge_value == GRAPH_PARENT_NONE) | |
889 | return 1; | |
5af7417b | 890 | if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) { |
4f542b7a | 891 | pptr = insert_parent_or_die(r, g, edge_value, pptr); |
177722b3 DS |
892 | return 1; |
893 | } | |
894 | ||
5af7417b | 895 | parent_data_ptr = (uint32_t*)(g->chunk_extra_edges + |
177722b3 DS |
896 | 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK)); |
897 | do { | |
898 | edge_value = get_be32(parent_data_ptr); | |
4f542b7a | 899 | pptr = insert_parent_or_die(r, g, |
177722b3 DS |
900 | edge_value & GRAPH_EDGE_LAST_MASK, |
901 | pptr); | |
902 | parent_data_ptr++; | |
903 | } while (!(edge_value & GRAPH_LAST_EDGE)); | |
904 | ||
905 | return 1; | |
906 | } | |
907 | ||
e2838d85 DS |
908 | static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos) |
909 | { | |
c752ad09 AK |
910 | uint32_t graph_pos = commit_graph_position(item); |
911 | if (graph_pos != COMMIT_NOT_FROM_GRAPH) { | |
912 | *pos = graph_pos; | |
e2838d85 DS |
913 | return 1; |
914 | } else { | |
d4f4d60f DS |
915 | struct commit_graph *cur_g = g; |
916 | uint32_t lex_index; | |
917 | ||
918 | while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index)) | |
919 | cur_g = cur_g->base_graph; | |
920 | ||
921 | if (cur_g) { | |
922 | *pos = lex_index + cur_g->num_commits_in_base; | |
923 | return 1; | |
924 | } | |
925 | ||
926 | return 0; | |
e2838d85 DS |
927 | } |
928 | } | |
929 | ||
4f542b7a SB |
930 | static int parse_commit_in_graph_one(struct repository *r, |
931 | struct commit_graph *g, | |
932 | struct commit *item) | |
177722b3 | 933 | { |
e2838d85 DS |
934 | uint32_t pos; |
935 | ||
177722b3 DS |
936 | if (item->object.parsed) |
937 | return 1; | |
ee797053 DS |
938 | |
939 | if (find_commit_in_graph(item, g, &pos)) | |
4f542b7a | 940 | return fill_commit_in_graph(r, item, g, pos); |
ee797053 DS |
941 | |
942 | return 0; | |
943 | } | |
944 | ||
dade47c0 | 945 | int parse_commit_in_graph(struct repository *r, struct commit *item) |
ee797053 | 946 | { |
7b671f8c DS |
947 | static int checked_env = 0; |
948 | ||
949 | if (!checked_env && | |
950 | git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0)) | |
951 | die("dying as requested by the '%s' variable on commit-graph parse!", | |
952 | GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE); | |
953 | checked_env = 1; | |
954 | ||
dade47c0 | 955 | if (!prepare_commit_graph(r)) |
ee797053 | 956 | return 0; |
4f542b7a | 957 | return parse_commit_in_graph_one(r, r->objects->commit_graph, item); |
177722b3 DS |
958 | } |
959 | ||
dade47c0 | 960 | void load_commit_graph_info(struct repository *r, struct commit *item) |
e2838d85 DS |
961 | { |
962 | uint32_t pos; | |
dade47c0 | 963 | if (!prepare_commit_graph(r)) |
e2838d85 | 964 | return; |
dade47c0 JT |
965 | if (find_commit_in_graph(item, r->objects->commit_graph, &pos)) |
966 | fill_commit_graph_info(item, r->objects->commit_graph, pos); | |
e2838d85 DS |
967 | } |
968 | ||
4f542b7a SB |
969 | static struct tree *load_tree_for_commit(struct repository *r, |
970 | struct commit_graph *g, | |
971 | struct commit *c) | |
7b8a21db DS |
972 | { |
973 | struct object_id oid; | |
d4f4d60f | 974 | const unsigned char *commit_data; |
c752ad09 | 975 | uint32_t graph_pos = commit_graph_position(c); |
d4f4d60f | 976 | |
c752ad09 | 977 | while (graph_pos < g->num_commits_in_base) |
d4f4d60f DS |
978 | g = g->base_graph; |
979 | ||
980 | commit_data = g->chunk_commit_data + | |
c752ad09 | 981 | GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base); |
7b8a21db DS |
982 | |
983 | hashcpy(oid.hash, commit_data); | |
a133c40b | 984 | set_commit_tree(c, lookup_tree(r, &oid)); |
7b8a21db DS |
985 | |
986 | return c->maybe_tree; | |
987 | } | |
988 | ||
4f542b7a SB |
989 | static struct tree *get_commit_tree_in_graph_one(struct repository *r, |
990 | struct commit_graph *g, | |
0cbef8f8 | 991 | const struct commit *c) |
7b8a21db DS |
992 | { |
993 | if (c->maybe_tree) | |
994 | return c->maybe_tree; | |
c49c82aa | 995 | if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH) |
0cbef8f8 DS |
996 | BUG("get_commit_tree_in_graph_one called from non-commit-graph commit"); |
997 | ||
4f542b7a | 998 | return load_tree_for_commit(r, g, (struct commit *)c); |
0cbef8f8 | 999 | } |
7b8a21db | 1000 | |
dade47c0 | 1001 | struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c) |
0cbef8f8 | 1002 | { |
4f542b7a | 1003 | return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c); |
7b8a21db DS |
1004 | } |
1005 | ||
c9905bea DS |
1006 | struct packed_commit_list { |
1007 | struct commit **list; | |
3361390c JK |
1008 | size_t nr; |
1009 | size_t alloc; | |
c9905bea DS |
1010 | }; |
1011 | ||
c9905bea DS |
1012 | struct write_commit_graph_context { |
1013 | struct repository *r; | |
0bd52e27 | 1014 | struct object_directory *odb; |
c9905bea | 1015 | char *graph_name; |
a5f1c448 | 1016 | struct oid_array oids; |
c9905bea DS |
1017 | struct packed_commit_list commits; |
1018 | int num_extra_edges; | |
e8b63005 | 1019 | int num_generation_data_overflows; |
c9905bea DS |
1020 | unsigned long approx_nr_objects; |
1021 | struct progress *progress; | |
1022 | int progress_done; | |
1023 | uint64_t progress_cnt; | |
6c622f9f DS |
1024 | |
1025 | char *base_graph_name; | |
1026 | int num_commit_graphs_before; | |
1027 | int num_commit_graphs_after; | |
1028 | char **commit_graph_filenames_before; | |
1029 | char **commit_graph_filenames_after; | |
1030 | char **commit_graph_hash_after; | |
1031 | uint32_t new_num_commits_in_base; | |
1032 | struct commit_graph *new_base_graph; | |
1033 | ||
c9905bea | 1034 | unsigned append:1, |
6c622f9f | 1035 | report_progress:1, |
7c5c9b9c | 1036 | split:1, |
3d112755 | 1037 | changed_paths:1, |
e8b63005 AK |
1038 | order_by_pack:1, |
1039 | write_generation_data:1; | |
c2bc6e6a | 1040 | |
72a2bfca | 1041 | struct topo_level_slab *topo_levels; |
98bb7961 | 1042 | const struct commit_graph_opts *opts; |
f97b9325 | 1043 | size_t total_bloom_filter_data_size; |
98037f2b | 1044 | const struct bloom_filter_settings *bloom_settings; |
312cff52 TB |
1045 | |
1046 | int count_bloom_filter_computed; | |
1047 | int count_bloom_filter_not_computed; | |
59f0d507 | 1048 | int count_bloom_filter_trunc_empty; |
312cff52 | 1049 | int count_bloom_filter_trunc_large; |
c9905bea DS |
1050 | }; |
1051 | ||
9bab081d SG |
1052 | static int write_graph_chunk_fanout(struct hashfile *f, |
1053 | struct write_commit_graph_context *ctx) | |
08fd81c9 DS |
1054 | { |
1055 | int i, count = 0; | |
c9905bea | 1056 | struct commit **list = ctx->commits.list; |
08fd81c9 DS |
1057 | |
1058 | /* | |
1059 | * Write the first-level table (the list is sorted, | |
1060 | * but we use a 256-entry lookup to be able to avoid | |
1061 | * having to do eight extra binary search iterations). | |
1062 | */ | |
1063 | for (i = 0; i < 256; i++) { | |
c9905bea | 1064 | while (count < ctx->commits.nr) { |
08fd81c9 DS |
1065 | if ((*list)->object.oid.hash[0] != i) |
1066 | break; | |
c9905bea | 1067 | display_progress(ctx->progress, ++ctx->progress_cnt); |
08fd81c9 DS |
1068 | count++; |
1069 | list++; | |
1070 | } | |
1071 | ||
1072 | hashwrite_be32(f, count); | |
1073 | } | |
9bab081d SG |
1074 | |
1075 | return 0; | |
08fd81c9 DS |
1076 | } |
1077 | ||
9bab081d SG |
1078 | static int write_graph_chunk_oids(struct hashfile *f, |
1079 | struct write_commit_graph_context *ctx) | |
08fd81c9 | 1080 | { |
c9905bea | 1081 | struct commit **list = ctx->commits.list; |
08fd81c9 | 1082 | int count; |
c9905bea DS |
1083 | for (count = 0; count < ctx->commits.nr; count++, list++) { |
1084 | display_progress(ctx->progress, ++ctx->progress_cnt); | |
9bab081d | 1085 | hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz); |
53035c4f | 1086 | } |
9bab081d SG |
1087 | |
1088 | return 0; | |
08fd81c9 DS |
1089 | } |
1090 | ||
1091 | static const unsigned char *commit_to_sha1(size_t index, void *table) | |
1092 | { | |
1093 | struct commit **commits = table; | |
1094 | return commits[index]->object.oid.hash; | |
1095 | } | |
1096 | ||
9bab081d SG |
1097 | static int write_graph_chunk_data(struct hashfile *f, |
1098 | struct write_commit_graph_context *ctx) | |
08fd81c9 | 1099 | { |
c9905bea DS |
1100 | struct commit **list = ctx->commits.list; |
1101 | struct commit **last = ctx->commits.list + ctx->commits.nr; | |
08fd81c9 DS |
1102 | uint32_t num_extra_edges = 0; |
1103 | ||
1104 | while (list < last) { | |
1105 | struct commit_list *parent; | |
806278de | 1106 | struct object_id *tree; |
08fd81c9 DS |
1107 | int edge_value; |
1108 | uint32_t packedDate[2]; | |
c9905bea | 1109 | display_progress(ctx->progress, ++ctx->progress_cnt); |
08fd81c9 | 1110 | |
c4cc0831 | 1111 | if (repo_parse_commit_no_graph(ctx->r, *list)) |
16749b8d TB |
1112 | die(_("unable to parse commit %s"), |
1113 | oid_to_hex(&(*list)->object.oid)); | |
806278de | 1114 | tree = get_commit_tree_oid(*list); |
9bab081d | 1115 | hashwrite(f, tree->hash, the_hash_algo->rawsz); |
08fd81c9 DS |
1116 | |
1117 | parent = (*list)->parents; | |
1118 | ||
1119 | if (!parent) | |
1120 | edge_value = GRAPH_PARENT_NONE; | |
1121 | else { | |
1122 | edge_value = sha1_pos(parent->item->object.oid.hash, | |
c9905bea DS |
1123 | ctx->commits.list, |
1124 | ctx->commits.nr, | |
08fd81c9 DS |
1125 | commit_to_sha1); |
1126 | ||
6c622f9f DS |
1127 | if (edge_value >= 0) |
1128 | edge_value += ctx->new_num_commits_in_base; | |
8a6ac287 | 1129 | else if (ctx->new_base_graph) { |
6c622f9f DS |
1130 | uint32_t pos; |
1131 | if (find_commit_in_graph(parent->item, | |
1132 | ctx->new_base_graph, | |
1133 | &pos)) | |
1134 | edge_value = pos; | |
1135 | } | |
1136 | ||
08fd81c9 | 1137 | if (edge_value < 0) |
cce99cd8 DS |
1138 | BUG("missing parent %s for commit %s", |
1139 | oid_to_hex(&parent->item->object.oid), | |
1140 | oid_to_hex(&(*list)->object.oid)); | |
08fd81c9 DS |
1141 | } |
1142 | ||
1143 | hashwrite_be32(f, edge_value); | |
1144 | ||
1145 | if (parent) | |
1146 | parent = parent->next; | |
1147 | ||
1148 | if (!parent) | |
1149 | edge_value = GRAPH_PARENT_NONE; | |
1150 | else if (parent->next) | |
5af7417b | 1151 | edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges; |
08fd81c9 DS |
1152 | else { |
1153 | edge_value = sha1_pos(parent->item->object.oid.hash, | |
c9905bea DS |
1154 | ctx->commits.list, |
1155 | ctx->commits.nr, | |
08fd81c9 | 1156 | commit_to_sha1); |
6c622f9f DS |
1157 | |
1158 | if (edge_value >= 0) | |
1159 | edge_value += ctx->new_num_commits_in_base; | |
8a6ac287 | 1160 | else if (ctx->new_base_graph) { |
6c622f9f DS |
1161 | uint32_t pos; |
1162 | if (find_commit_in_graph(parent->item, | |
1163 | ctx->new_base_graph, | |
1164 | &pos)) | |
1165 | edge_value = pos; | |
1166 | } | |
1167 | ||
08fd81c9 | 1168 | if (edge_value < 0) |
cce99cd8 DS |
1169 | BUG("missing parent %s for commit %s", |
1170 | oid_to_hex(&parent->item->object.oid), | |
1171 | oid_to_hex(&(*list)->object.oid)); | |
08fd81c9 DS |
1172 | } |
1173 | ||
1174 | hashwrite_be32(f, edge_value); | |
1175 | ||
5af7417b | 1176 | if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) { |
08fd81c9 DS |
1177 | do { |
1178 | num_extra_edges++; | |
1179 | parent = parent->next; | |
1180 | } while (parent); | |
1181 | } | |
1182 | ||
1183 | if (sizeof((*list)->date) > 4) | |
1184 | packedDate[0] = htonl(((*list)->date >> 32) & 0x3); | |
1185 | else | |
1186 | packedDate[0] = 0; | |
1187 | ||
72a2bfca | 1188 | packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2); |
3258c663 | 1189 | |
08fd81c9 DS |
1190 | packedDate[1] = htonl((*list)->date); |
1191 | hashwrite(f, packedDate, 8); | |
1192 | ||
1193 | list++; | |
1194 | } | |
9bab081d SG |
1195 | |
1196 | return 0; | |
08fd81c9 DS |
1197 | } |
1198 | ||
e8b63005 AK |
1199 | static int write_graph_chunk_generation_data(struct hashfile *f, |
1200 | struct write_commit_graph_context *ctx) | |
1201 | { | |
1202 | int i, num_generation_data_overflows = 0; | |
1203 | ||
1204 | for (i = 0; i < ctx->commits.nr; i++) { | |
1205 | struct commit *c = ctx->commits.list[i]; | |
90cb1c47 DS |
1206 | timestamp_t offset; |
1207 | repo_parse_commit(ctx->r, c); | |
1208 | offset = commit_graph_data_at(c)->generation - c->date; | |
e8b63005 AK |
1209 | display_progress(ctx->progress, ++ctx->progress_cnt); |
1210 | ||
1211 | if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) { | |
1212 | offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows; | |
1213 | num_generation_data_overflows++; | |
1214 | } | |
1215 | ||
1216 | hashwrite_be32(f, offset); | |
1217 | } | |
1218 | ||
1219 | return 0; | |
1220 | } | |
1221 | ||
1222 | static int write_graph_chunk_generation_data_overflow(struct hashfile *f, | |
1223 | struct write_commit_graph_context *ctx) | |
1224 | { | |
1225 | int i; | |
1226 | for (i = 0; i < ctx->commits.nr; i++) { | |
1227 | struct commit *c = ctx->commits.list[i]; | |
1228 | timestamp_t offset = commit_graph_data_at(c)->generation - c->date; | |
1229 | display_progress(ctx->progress, ++ctx->progress_cnt); | |
1230 | ||
1231 | if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) { | |
1232 | hashwrite_be32(f, offset >> 32); | |
1233 | hashwrite_be32(f, (uint32_t) offset); | |
1234 | } | |
1235 | } | |
1236 | ||
1237 | return 0; | |
1238 | } | |
1239 | ||
9bab081d SG |
1240 | static int write_graph_chunk_extra_edges(struct hashfile *f, |
1241 | struct write_commit_graph_context *ctx) | |
08fd81c9 | 1242 | { |
c9905bea DS |
1243 | struct commit **list = ctx->commits.list; |
1244 | struct commit **last = ctx->commits.list + ctx->commits.nr; | |
08fd81c9 DS |
1245 | struct commit_list *parent; |
1246 | ||
1247 | while (list < last) { | |
1248 | int num_parents = 0; | |
53035c4f | 1249 | |
c9905bea | 1250 | display_progress(ctx->progress, ++ctx->progress_cnt); |
53035c4f | 1251 | |
08fd81c9 DS |
1252 | for (parent = (*list)->parents; num_parents < 3 && parent; |
1253 | parent = parent->next) | |
1254 | num_parents++; | |
1255 | ||
1256 | if (num_parents <= 2) { | |
1257 | list++; | |
1258 | continue; | |
1259 | } | |
1260 | ||
1261 | /* Since num_parents > 2, this initializer is safe. */ | |
1262 | for (parent = (*list)->parents->next; parent; parent = parent->next) { | |
1263 | int edge_value = sha1_pos(parent->item->object.oid.hash, | |
c9905bea DS |
1264 | ctx->commits.list, |
1265 | ctx->commits.nr, | |
08fd81c9 DS |
1266 | commit_to_sha1); |
1267 | ||
6c622f9f DS |
1268 | if (edge_value >= 0) |
1269 | edge_value += ctx->new_num_commits_in_base; | |
8a6ac287 | 1270 | else if (ctx->new_base_graph) { |
6c622f9f DS |
1271 | uint32_t pos; |
1272 | if (find_commit_in_graph(parent->item, | |
1273 | ctx->new_base_graph, | |
1274 | &pos)) | |
1275 | edge_value = pos; | |
1276 | } | |
1277 | ||
08fd81c9 | 1278 | if (edge_value < 0) |
cce99cd8 DS |
1279 | BUG("missing parent %s for commit %s", |
1280 | oid_to_hex(&parent->item->object.oid), | |
1281 | oid_to_hex(&(*list)->object.oid)); | |
08fd81c9 DS |
1282 | else if (!parent->next) |
1283 | edge_value |= GRAPH_LAST_EDGE; | |
1284 | ||
1285 | hashwrite_be32(f, edge_value); | |
1286 | } | |
1287 | ||
1288 | list++; | |
1289 | } | |
9bab081d SG |
1290 | |
1291 | return 0; | |
08fd81c9 DS |
1292 | } |
1293 | ||
9bab081d SG |
1294 | static int write_graph_chunk_bloom_indexes(struct hashfile *f, |
1295 | struct write_commit_graph_context *ctx) | |
76ffbca7 GS |
1296 | { |
1297 | struct commit **list = ctx->commits.list; | |
1298 | struct commit **last = ctx->commits.list + ctx->commits.nr; | |
1299 | uint32_t cur_pos = 0; | |
76ffbca7 GS |
1300 | |
1301 | while (list < last) { | |
312cff52 | 1302 | struct bloom_filter *filter = get_bloom_filter(ctx->r, *list); |
94919742 DS |
1303 | size_t len = filter ? filter->len : 0; |
1304 | cur_pos += len; | |
150cd3b6 | 1305 | display_progress(ctx->progress, ++ctx->progress_cnt); |
76ffbca7 GS |
1306 | hashwrite_be32(f, cur_pos); |
1307 | list++; | |
1308 | } | |
1309 | ||
9bab081d | 1310 | return 0; |
76ffbca7 GS |
1311 | } |
1312 | ||
0087a87b DS |
1313 | static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx) |
1314 | { | |
1315 | struct json_writer jw = JSON_WRITER_INIT; | |
1316 | ||
1317 | jw_object_begin(&jw, 0); | |
1318 | jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version); | |
1319 | jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes); | |
1320 | jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry); | |
97ffa4fa | 1321 | jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths); |
0087a87b DS |
1322 | jw_end(&jw); |
1323 | ||
1324 | trace2_data_json("bloom", ctx->r, "settings", &jw); | |
1325 | ||
1326 | jw_release(&jw); | |
76ffbca7 GS |
1327 | } |
1328 | ||
9bab081d SG |
1329 | static int write_graph_chunk_bloom_data(struct hashfile *f, |
1330 | struct write_commit_graph_context *ctx) | |
76ffbca7 GS |
1331 | { |
1332 | struct commit **list = ctx->commits.list; | |
1333 | struct commit **last = ctx->commits.list + ctx->commits.nr; | |
76ffbca7 | 1334 | |
0087a87b DS |
1335 | trace2_bloom_filter_settings(ctx); |
1336 | ||
98037f2b DS |
1337 | hashwrite_be32(f, ctx->bloom_settings->hash_version); |
1338 | hashwrite_be32(f, ctx->bloom_settings->num_hashes); | |
1339 | hashwrite_be32(f, ctx->bloom_settings->bits_per_entry); | |
76ffbca7 GS |
1340 | |
1341 | while (list < last) { | |
312cff52 | 1342 | struct bloom_filter *filter = get_bloom_filter(ctx->r, *list); |
94919742 | 1343 | size_t len = filter ? filter->len : 0; |
94919742 | 1344 | |
150cd3b6 | 1345 | display_progress(ctx->progress, ++ctx->progress_cnt); |
94919742 DS |
1346 | if (len) |
1347 | hashwrite(f, filter->data, len * sizeof(unsigned char)); | |
76ffbca7 GS |
1348 | list++; |
1349 | } | |
1350 | ||
9bab081d | 1351 | return 0; |
76ffbca7 GS |
1352 | } |
1353 | ||
08fd81c9 DS |
1354 | static int add_packed_commits(const struct object_id *oid, |
1355 | struct packed_git *pack, | |
1356 | uint32_t pos, | |
1357 | void *data) | |
1358 | { | |
c9905bea | 1359 | struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data; |
08fd81c9 DS |
1360 | enum object_type type; |
1361 | off_t offset = nth_packed_object_offset(pack, pos); | |
1362 | struct object_info oi = OBJECT_INFO_INIT; | |
1363 | ||
c9905bea DS |
1364 | if (ctx->progress) |
1365 | display_progress(ctx->progress, ++ctx->progress_done); | |
7b0f2292 | 1366 | |
08fd81c9 | 1367 | oi.typep = &type; |
c9905bea | 1368 | if (packed_object_info(ctx->r, pack, offset, &oi) < 0) |
4f5b532d | 1369 | die(_("unable to get type of object %s"), oid_to_hex(oid)); |
08fd81c9 DS |
1370 | |
1371 | if (type != OBJ_COMMIT) | |
1372 | return 0; | |
1373 | ||
a5f1c448 | 1374 | oid_array_append(&ctx->oids, oid); |
d21ee7d1 JK |
1375 | set_commit_pos(ctx->r, oid); |
1376 | ||
08fd81c9 DS |
1377 | return 0; |
1378 | } | |
1379 | ||
c9905bea | 1380 | static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit) |
4f2542b4 DS |
1381 | { |
1382 | struct commit_list *parent; | |
1383 | for (parent = commit->parents; parent; parent = parent->next) { | |
cb99a34e | 1384 | if (!(parent->item->object.flags & REACHABLE)) { |
a5f1c448 | 1385 | oid_array_append(&ctx->oids, &parent->item->object.oid); |
cb99a34e | 1386 | parent->item->object.flags |= REACHABLE; |
4f2542b4 DS |
1387 | } |
1388 | } | |
1389 | } | |
1390 | ||
c9905bea | 1391 | static void close_reachable(struct write_commit_graph_context *ctx) |
4f2542b4 | 1392 | { |
49bbc57a | 1393 | int i; |
4f2542b4 | 1394 | struct commit *commit; |
98bb7961 TB |
1395 | enum commit_graph_split_flags flags = ctx->opts ? |
1396 | ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED; | |
4f2542b4 | 1397 | |
c9905bea DS |
1398 | if (ctx->report_progress) |
1399 | ctx->progress = start_delayed_progress( | |
1400 | _("Loading known commits in commit graph"), | |
1401 | ctx->oids.nr); | |
1402 | for (i = 0; i < ctx->oids.nr; i++) { | |
1403 | display_progress(ctx->progress, i + 1); | |
a5f1c448 | 1404 | commit = lookup_commit(ctx->r, &ctx->oids.oid[i]); |
4f2542b4 | 1405 | if (commit) |
cb99a34e | 1406 | commit->object.flags |= REACHABLE; |
4f2542b4 | 1407 | } |
c9905bea | 1408 | stop_progress(&ctx->progress); |
4f2542b4 DS |
1409 | |
1410 | /* | |
c9905bea | 1411 | * As this loop runs, ctx->oids.nr may grow, but not more |
4f2542b4 DS |
1412 | * than the number of missing commits in the reachable |
1413 | * closure. | |
1414 | */ | |
c9905bea DS |
1415 | if (ctx->report_progress) |
1416 | ctx->progress = start_delayed_progress( | |
1417 | _("Expanding reachable commits in commit graph"), | |
67fa6aac | 1418 | 0); |
c9905bea DS |
1419 | for (i = 0; i < ctx->oids.nr; i++) { |
1420 | display_progress(ctx->progress, i + 1); | |
a5f1c448 | 1421 | commit = lookup_commit(ctx->r, &ctx->oids.oid[i]); |
4f2542b4 | 1422 | |
6c622f9f DS |
1423 | if (!commit) |
1424 | continue; | |
1425 | if (ctx->split) { | |
c4cc0831 | 1426 | if ((!repo_parse_commit(ctx->r, commit) && |
c49c82aa | 1427 | commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) || |
8a6ac287 | 1428 | flags == COMMIT_GRAPH_SPLIT_REPLACE) |
6c622f9f | 1429 | add_missing_parents(ctx, commit); |
c4cc0831 | 1430 | } else if (!repo_parse_commit_no_graph(ctx->r, commit)) |
c9905bea | 1431 | add_missing_parents(ctx, commit); |
4f2542b4 | 1432 | } |
c9905bea | 1433 | stop_progress(&ctx->progress); |
4f2542b4 | 1434 | |
c9905bea DS |
1435 | if (ctx->report_progress) |
1436 | ctx->progress = start_delayed_progress( | |
1437 | _("Clearing commit marks in commit graph"), | |
1438 | ctx->oids.nr); | |
1439 | for (i = 0; i < ctx->oids.nr; i++) { | |
1440 | display_progress(ctx->progress, i + 1); | |
a5f1c448 | 1441 | commit = lookup_commit(ctx->r, &ctx->oids.oid[i]); |
4f2542b4 DS |
1442 | |
1443 | if (commit) | |
cb99a34e | 1444 | commit->object.flags &= ~REACHABLE; |
4f2542b4 | 1445 | } |
c9905bea | 1446 | stop_progress(&ctx->progress); |
4f2542b4 DS |
1447 | } |
1448 | ||
9c2c0a82 | 1449 | static void compute_topological_levels(struct write_commit_graph_context *ctx) |
3258c663 DS |
1450 | { |
1451 | int i; | |
1452 | struct commit_list *list = NULL; | |
1453 | ||
c9905bea | 1454 | if (ctx->report_progress) |
ecc08690 | 1455 | ctx->progress = start_delayed_progress( |
9c2c0a82 | 1456 | _("Computing commit graph topological levels"), |
c9905bea DS |
1457 | ctx->commits.nr); |
1458 | for (i = 0; i < ctx->commits.nr; i++) { | |
90cb1c47 DS |
1459 | struct commit *c = ctx->commits.list[i]; |
1460 | uint32_t level; | |
90cb1c47 DS |
1461 | |
1462 | repo_parse_commit(ctx->r, c); | |
1463 | level = *topo_level_slab_at(ctx->topo_levels, c); | |
4844812b | 1464 | |
c9905bea | 1465 | display_progress(ctx->progress, i + 1); |
9c2c0a82 | 1466 | if (level != GENERATION_NUMBER_ZERO) |
3258c663 DS |
1467 | continue; |
1468 | ||
90cb1c47 | 1469 | commit_list_insert(c, &list); |
3258c663 DS |
1470 | while (list) { |
1471 | struct commit *current = list->item; | |
1472 | struct commit_list *parent; | |
1473 | int all_parents_computed = 1; | |
72a2bfca | 1474 | uint32_t max_level = 0; |
3258c663 DS |
1475 | |
1476 | for (parent = current->parents; parent; parent = parent->next) { | |
90cb1c47 | 1477 | repo_parse_commit(ctx->r, parent->item); |
72a2bfca | 1478 | level = *topo_level_slab_at(ctx->topo_levels, parent->item); |
4844812b | 1479 | |
9c2c0a82 | 1480 | if (level == GENERATION_NUMBER_ZERO) { |
3258c663 DS |
1481 | all_parents_computed = 0; |
1482 | commit_list_insert(parent->item, &list); | |
1483 | break; | |
3258c663 | 1484 | } |
c1a09119 AK |
1485 | |
1486 | if (level > max_level) | |
1487 | max_level = level; | |
3258c663 DS |
1488 | } |
1489 | ||
1490 | if (all_parents_computed) { | |
3258c663 DS |
1491 | pop_commit(&list); |
1492 | ||
d7f92784 AK |
1493 | if (max_level > GENERATION_NUMBER_V1_MAX - 1) |
1494 | max_level = GENERATION_NUMBER_V1_MAX - 1; | |
72a2bfca | 1495 | *topo_level_slab_at(ctx->topo_levels, current) = max_level + 1; |
9c2c0a82 DS |
1496 | } |
1497 | } | |
1498 | } | |
1499 | stop_progress(&ctx->progress); | |
1500 | } | |
1501 | ||
1502 | static void compute_generation_numbers(struct write_commit_graph_context *ctx) | |
1503 | { | |
1504 | int i; | |
1505 | struct commit_list *list = NULL; | |
1506 | ||
1507 | if (ctx->report_progress) | |
1508 | ctx->progress = start_delayed_progress( | |
1509 | _("Computing commit graph generation numbers"), | |
1510 | ctx->commits.nr); | |
1511 | for (i = 0; i < ctx->commits.nr; i++) { | |
1512 | struct commit *c = ctx->commits.list[i]; | |
1513 | timestamp_t corrected_commit_date; | |
1514 | ||
1515 | repo_parse_commit(ctx->r, c); | |
1516 | corrected_commit_date = commit_graph_data_at(c)->generation; | |
1517 | ||
1518 | display_progress(ctx->progress, i + 1); | |
1519 | if (corrected_commit_date != GENERATION_NUMBER_ZERO) | |
1520 | continue; | |
1521 | ||
1522 | commit_list_insert(c, &list); | |
1523 | while (list) { | |
1524 | struct commit *current = list->item; | |
1525 | struct commit_list *parent; | |
1526 | int all_parents_computed = 1; | |
1527 | timestamp_t max_corrected_commit_date = 0; | |
1528 | ||
1529 | for (parent = current->parents; parent; parent = parent->next) { | |
1530 | repo_parse_commit(ctx->r, parent->item); | |
1531 | corrected_commit_date = commit_graph_data_at(parent->item)->generation; | |
1532 | ||
1533 | if (corrected_commit_date == GENERATION_NUMBER_ZERO) { | |
1534 | all_parents_computed = 0; | |
1535 | commit_list_insert(parent->item, &list); | |
1536 | break; | |
1537 | } | |
1538 | ||
1539 | if (corrected_commit_date > max_corrected_commit_date) | |
1540 | max_corrected_commit_date = corrected_commit_date; | |
1541 | } | |
1542 | ||
1543 | if (all_parents_computed) { | |
1544 | pop_commit(&list); | |
c1a09119 AK |
1545 | |
1546 | if (current->date && current->date > max_corrected_commit_date) | |
1547 | max_corrected_commit_date = current->date - 1; | |
1548 | commit_graph_data_at(current)->generation = max_corrected_commit_date + 1; | |
e8b63005 AK |
1549 | |
1550 | if (commit_graph_data_at(current)->generation - current->date > GENERATION_NUMBER_V2_OFFSET_MAX) | |
1551 | ctx->num_generation_data_overflows++; | |
3258c663 DS |
1552 | } |
1553 | } | |
1554 | } | |
c9905bea | 1555 | stop_progress(&ctx->progress); |
3258c663 DS |
1556 | } |
1557 | ||
312cff52 TB |
1558 | static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx) |
1559 | { | |
1560 | trace2_data_intmax("commit-graph", ctx->r, "filter-computed", | |
1561 | ctx->count_bloom_filter_computed); | |
1562 | trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed", | |
1563 | ctx->count_bloom_filter_not_computed); | |
59f0d507 TB |
1564 | trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty", |
1565 | ctx->count_bloom_filter_trunc_empty); | |
312cff52 TB |
1566 | trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large", |
1567 | ctx->count_bloom_filter_trunc_large); | |
1568 | } | |
1569 | ||
f97b9325 GS |
1570 | static void compute_bloom_filters(struct write_commit_graph_context *ctx) |
1571 | { | |
1572 | int i; | |
1573 | struct progress *progress = NULL; | |
d21ee7d1 | 1574 | struct commit **sorted_commits; |
809e0327 | 1575 | int max_new_filters; |
f97b9325 GS |
1576 | |
1577 | init_bloom_filters(); | |
1578 | ||
1579 | if (ctx->report_progress) | |
1580 | progress = start_delayed_progress( | |
1581 | _("Computing commit changed paths Bloom filters"), | |
1582 | ctx->commits.nr); | |
1583 | ||
d21ee7d1 JK |
1584 | ALLOC_ARRAY(sorted_commits, ctx->commits.nr); |
1585 | COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr); | |
3d112755 GS |
1586 | |
1587 | if (ctx->order_by_pack) | |
1588 | QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp); | |
1589 | else | |
1590 | QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp); | |
d21ee7d1 | 1591 | |
809e0327 TB |
1592 | max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ? |
1593 | ctx->opts->max_new_filters : ctx->commits.nr; | |
1594 | ||
f97b9325 | 1595 | for (i = 0; i < ctx->commits.nr; i++) { |
312cff52 | 1596 | enum bloom_filter_computed computed = 0; |
d21ee7d1 | 1597 | struct commit *c = sorted_commits[i]; |
312cff52 TB |
1598 | struct bloom_filter *filter = get_or_compute_bloom_filter( |
1599 | ctx->r, | |
1600 | c, | |
809e0327 | 1601 | ctx->count_bloom_filter_computed < max_new_filters, |
9a7a9ed1 | 1602 | ctx->bloom_settings, |
312cff52 TB |
1603 | &computed); |
1604 | if (computed & BLOOM_COMPUTED) { | |
1605 | ctx->count_bloom_filter_computed++; | |
59f0d507 TB |
1606 | if (computed & BLOOM_TRUNC_EMPTY) |
1607 | ctx->count_bloom_filter_trunc_empty++; | |
312cff52 TB |
1608 | if (computed & BLOOM_TRUNC_LARGE) |
1609 | ctx->count_bloom_filter_trunc_large++; | |
1610 | } else if (computed & BLOOM_NOT_COMPUTED) | |
1611 | ctx->count_bloom_filter_not_computed++; | |
809e0327 TB |
1612 | ctx->total_bloom_filter_data_size += filter |
1613 | ? sizeof(unsigned char) * filter->len : 0; | |
f97b9325 GS |
1614 | display_progress(progress, i + 1); |
1615 | } | |
1616 | ||
312cff52 TB |
1617 | if (trace2_is_enabled()) |
1618 | trace2_bloom_filter_write_statistics(ctx); | |
1619 | ||
d21ee7d1 | 1620 | free(sorted_commits); |
f97b9325 GS |
1621 | stop_progress(&progress); |
1622 | } | |
1623 | ||
1fe10844 TB |
1624 | struct refs_cb_data { |
1625 | struct oidset *commits; | |
d335ce8f | 1626 | struct progress *progress; |
1fe10844 TB |
1627 | }; |
1628 | ||
6830c360 TB |
1629 | static int add_ref_to_set(const char *refname, |
1630 | const struct object_id *oid, | |
1631 | int flags, void *cb_data) | |
59fb8770 | 1632 | { |
630cd519 | 1633 | struct object_id peeled; |
1fe10844 | 1634 | struct refs_cb_data *data = (struct refs_cb_data *)cb_data; |
59fb8770 | 1635 | |
630cd519 TB |
1636 | if (!peel_ref(refname, &peeled)) |
1637 | oid = &peeled; | |
1638 | if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT) | |
1639 | oidset_insert(data->commits, oid); | |
d335ce8f TB |
1640 | |
1641 | display_progress(data->progress, oidset_size(data->commits)); | |
59fb8770 | 1642 | |
59fb8770 DS |
1643 | return 0; |
1644 | } | |
1645 | ||
0bd52e27 | 1646 | int write_commit_graph_reachable(struct object_directory *odb, |
39d88318 | 1647 | enum commit_graph_write_flags flags, |
98bb7961 | 1648 | const struct commit_graph_opts *opts) |
59fb8770 | 1649 | { |
6830c360 | 1650 | struct oidset commits = OIDSET_INIT; |
1fe10844 | 1651 | struct refs_cb_data data; |
e103f727 | 1652 | int result; |
59fb8770 | 1653 | |
1fe10844 TB |
1654 | memset(&data, 0, sizeof(data)); |
1655 | data.commits = &commits; | |
d335ce8f TB |
1656 | if (flags & COMMIT_GRAPH_WRITE_PROGRESS) |
1657 | data.progress = start_delayed_progress( | |
1658 | _("Collecting referenced commits"), 0); | |
1fe10844 TB |
1659 | |
1660 | for_each_ref(add_ref_to_set, &data); | |
6f9d5f2f SG |
1661 | |
1662 | stop_progress(&data.progress); | |
1663 | ||
6830c360 | 1664 | result = write_commit_graph(odb, NULL, &commits, |
98bb7961 | 1665 | flags, opts); |
f4dbdfc4 | 1666 | |
6830c360 | 1667 | oidset_clear(&commits); |
e103f727 | 1668 | return result; |
59fb8770 DS |
1669 | } |
1670 | ||
ef5b83f2 DS |
1671 | static int fill_oids_from_packs(struct write_commit_graph_context *ctx, |
1672 | struct string_list *pack_indexes) | |
08fd81c9 | 1673 | { |
ef5b83f2 | 1674 | uint32_t i; |
28944739 | 1675 | struct strbuf progress_title = STRBUF_INIT; |
ef5b83f2 DS |
1676 | struct strbuf packname = STRBUF_INIT; |
1677 | int dirlen; | |
08fd81c9 | 1678 | |
0bd52e27 | 1679 | strbuf_addf(&packname, "%s/pack/", ctx->odb->path); |
ef5b83f2 DS |
1680 | dirlen = packname.len; |
1681 | if (ctx->report_progress) { | |
1682 | strbuf_addf(&progress_title, | |
1683 | Q_("Finding commits for commit graph in %d pack", | |
1684 | "Finding commits for commit graph in %d packs", | |
1685 | pack_indexes->nr), | |
1686 | pack_indexes->nr); | |
1687 | ctx->progress = start_delayed_progress(progress_title.buf, 0); | |
1688 | ctx->progress_done = 0; | |
7547b95b | 1689 | } |
ef5b83f2 DS |
1690 | for (i = 0; i < pack_indexes->nr; i++) { |
1691 | struct packed_git *p; | |
1692 | strbuf_setlen(&packname, dirlen); | |
1693 | strbuf_addstr(&packname, pack_indexes->items[i].string); | |
1694 | p = add_packed_git(packname.buf, packname.len, 1); | |
1695 | if (!p) { | |
1696 | error(_("error adding pack %s"), packname.buf); | |
1697 | return -1; | |
7547b95b | 1698 | } |
ef5b83f2 DS |
1699 | if (open_pack_index(p)) { |
1700 | error(_("error opening index for %s"), packname.buf); | |
1701 | return -1; | |
1702 | } | |
1703 | for_each_object_in_pack(p, add_packed_commits, ctx, | |
1704 | FOR_EACH_OBJECT_PACK_ORDER); | |
1705 | close_pack(p); | |
1706 | free(p); | |
7547b95b DS |
1707 | } |
1708 | ||
ef5b83f2 | 1709 | stop_progress(&ctx->progress); |
0aa6bce7 | 1710 | strbuf_release(&progress_title); |
ef5b83f2 DS |
1711 | strbuf_release(&packname); |
1712 | ||
1713 | return 0; | |
1714 | } | |
1715 | ||
6830c360 TB |
1716 | static int fill_oids_from_commits(struct write_commit_graph_context *ctx, |
1717 | struct oidset *commits) | |
4c9efe85 | 1718 | { |
6830c360 TB |
1719 | struct oidset_iter iter; |
1720 | struct object_id *oid; | |
1721 | ||
1722 | if (!oidset_size(commits)) | |
1723 | return 0; | |
4c9efe85 | 1724 | |
6830c360 TB |
1725 | oidset_iter_init(commits, &iter); |
1726 | while ((oid = oidset_iter_next(&iter))) { | |
a5f1c448 | 1727 | oid_array_append(&ctx->oids, oid); |
3d5df01b | 1728 | } |
6830c360 | 1729 | |
7c5c9b9c | 1730 | return 0; |
4c9efe85 | 1731 | } |
3d5df01b | 1732 | |
b2c83060 DS |
1733 | static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx) |
1734 | { | |
1735 | if (ctx->report_progress) | |
1736 | ctx->progress = start_delayed_progress( | |
1737 | _("Finding commits for commit graph among packed objects"), | |
1738 | ctx->approx_nr_objects); | |
1739 | for_each_packed_object(add_packed_commits, ctx, | |
1740 | FOR_EACH_OBJECT_PACK_ORDER); | |
1741 | if (ctx->progress_done < ctx->approx_nr_objects) | |
1742 | display_progress(ctx->progress, ctx->approx_nr_objects); | |
1743 | stop_progress(&ctx->progress); | |
1744 | } | |
049d51a2 | 1745 | |
f998d542 DS |
1746 | static void copy_oids_to_commits(struct write_commit_graph_context *ctx) |
1747 | { | |
1748 | uint32_t i; | |
98bb7961 TB |
1749 | enum commit_graph_split_flags flags = ctx->opts ? |
1750 | ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED; | |
08fd81c9 | 1751 | |
f998d542 DS |
1752 | ctx->num_extra_edges = 0; |
1753 | if (ctx->report_progress) | |
1754 | ctx->progress = start_delayed_progress( | |
890226cc | 1755 | _("Finding extra edges in commit graph"), |
f998d542 | 1756 | ctx->oids.nr); |
a5f1c448 JK |
1757 | oid_array_sort(&ctx->oids); |
1758 | for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) { | |
689a146c RS |
1759 | unsigned int num_parents; |
1760 | ||
f998d542 | 1761 | display_progress(ctx->progress, i + 1); |
08fd81c9 | 1762 | |
6c622f9f | 1763 | ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc); |
a5f1c448 | 1764 | ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]); |
6c622f9f | 1765 | |
8a6ac287 | 1766 | if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE && |
c49c82aa | 1767 | commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH) |
6c622f9f DS |
1768 | continue; |
1769 | ||
8a6ac287 | 1770 | if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE) |
c4cc0831 | 1771 | repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]); |
8a6ac287 | 1772 | else |
c4cc0831 | 1773 | repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]); |
08fd81c9 | 1774 | |
689a146c | 1775 | num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents); |
08fd81c9 | 1776 | if (num_parents > 2) |
f998d542 | 1777 | ctx->num_extra_edges += num_parents - 1; |
08fd81c9 | 1778 | |
f998d542 | 1779 | ctx->commits.nr++; |
08fd81c9 | 1780 | } |
f998d542 DS |
1781 | stop_progress(&ctx->progress); |
1782 | } | |
08fd81c9 | 1783 | |
6c622f9f DS |
1784 | static int write_graph_chunk_base_1(struct hashfile *f, |
1785 | struct commit_graph *g) | |
1786 | { | |
1787 | int num = 0; | |
1788 | ||
1789 | if (!g) | |
1790 | return 0; | |
1791 | ||
1792 | num = write_graph_chunk_base_1(f, g->base_graph); | |
1793 | hashwrite(f, g->oid.hash, the_hash_algo->rawsz); | |
1794 | return num + 1; | |
1795 | } | |
1796 | ||
1797 | static int write_graph_chunk_base(struct hashfile *f, | |
1798 | struct write_commit_graph_context *ctx) | |
1799 | { | |
1800 | int num = write_graph_chunk_base_1(f, ctx->new_base_graph); | |
1801 | ||
1802 | if (num != ctx->num_commit_graphs_after - 1) { | |
1803 | error(_("failed to write correct number of base graph ids")); | |
1804 | return -1; | |
1805 | } | |
1806 | ||
1807 | return 0; | |
1808 | } | |
1809 | ||
17e6275f SG |
1810 | typedef int (*chunk_write_fn)(struct hashfile *f, |
1811 | struct write_commit_graph_context *ctx); | |
1812 | ||
7fbfe07a SG |
1813 | struct chunk_info { |
1814 | uint32_t id; | |
1815 | uint64_t size; | |
17e6275f | 1816 | chunk_write_fn write_fn; |
7fbfe07a SG |
1817 | }; |
1818 | ||
238def57 | 1819 | static int write_commit_graph_file(struct write_commit_graph_context *ctx) |
08fd81c9 | 1820 | { |
238def57 | 1821 | uint32_t i; |
6c622f9f | 1822 | int fd; |
08fd81c9 | 1823 | struct hashfile *f; |
08fd81c9 | 1824 | struct lock_file lk = LOCK_INIT; |
7fbfe07a | 1825 | struct chunk_info chunks[MAX_NUM_CHUNKS + 1]; |
c1665998 | 1826 | const unsigned hashsz = the_hash_algo->rawsz; |
28944739 | 1827 | struct strbuf progress_title = STRBUF_INIT; |
144354b0 | 1828 | int num_chunks = 3; |
bb4d60e5 | 1829 | uint64_t chunk_offset; |
6c622f9f DS |
1830 | struct object_id file_hash; |
1831 | ||
1832 | if (ctx->split) { | |
1833 | struct strbuf tmp_file = STRBUF_INIT; | |
1834 | ||
1835 | strbuf_addf(&tmp_file, | |
1836 | "%s/info/commit-graphs/tmp_graph_XXXXXX", | |
0bd52e27 | 1837 | ctx->odb->path); |
6c622f9f DS |
1838 | ctx->graph_name = strbuf_detach(&tmp_file, NULL); |
1839 | } else { | |
ad2dd5bb | 1840 | ctx->graph_name = get_commit_graph_filename(ctx->odb); |
6c622f9f | 1841 | } |
238def57 | 1842 | |
238def57 DS |
1843 | if (safe_create_leading_directories(ctx->graph_name)) { |
1844 | UNLEAK(ctx->graph_name); | |
1845 | error(_("unable to create leading directories of %s"), | |
1846 | ctx->graph_name); | |
1847 | return -1; | |
f4dbdfc4 | 1848 | } |
08fd81c9 | 1849 | |
6c622f9f | 1850 | if (ctx->split) { |
663b2b1b | 1851 | char *lock_name = get_commit_graph_chain_filename(ctx->odb); |
08fd81c9 | 1852 | |
45a4365c TB |
1853 | hold_lock_file_for_update_mode(&lk, lock_name, |
1854 | LOCK_DIE_ON_ERROR, 0444); | |
08fd81c9 | 1855 | |
6c622f9f DS |
1856 | fd = git_mkstemp_mode(ctx->graph_name, 0444); |
1857 | if (fd < 0) { | |
a2d57e22 | 1858 | error(_("unable to create temporary graph layer")); |
6c622f9f DS |
1859 | return -1; |
1860 | } | |
1861 | ||
f4d62847 TB |
1862 | if (adjust_shared_perm(ctx->graph_name)) { |
1863 | error(_("unable to adjust shared permissions for '%s'"), | |
1864 | ctx->graph_name); | |
1865 | return -1; | |
1866 | } | |
1867 | ||
6c622f9f DS |
1868 | f = hashfd(fd, ctx->graph_name); |
1869 | } else { | |
1f9becae TB |
1870 | hold_lock_file_for_update_mode(&lk, ctx->graph_name, |
1871 | LOCK_DIE_ON_ERROR, 0444); | |
6c622f9f DS |
1872 | fd = lk.tempfile->fd; |
1873 | f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf); | |
1874 | } | |
08fd81c9 | 1875 | |
7fbfe07a SG |
1876 | chunks[0].id = GRAPH_CHUNKID_OIDFANOUT; |
1877 | chunks[0].size = GRAPH_FANOUT_SIZE; | |
17e6275f | 1878 | chunks[0].write_fn = write_graph_chunk_fanout; |
7fbfe07a SG |
1879 | chunks[1].id = GRAPH_CHUNKID_OIDLOOKUP; |
1880 | chunks[1].size = hashsz * ctx->commits.nr; | |
17e6275f | 1881 | chunks[1].write_fn = write_graph_chunk_oids; |
7fbfe07a SG |
1882 | chunks[2].id = GRAPH_CHUNKID_DATA; |
1883 | chunks[2].size = (hashsz + 16) * ctx->commits.nr; | |
17e6275f | 1884 | chunks[2].write_fn = write_graph_chunk_data; |
e8b63005 AK |
1885 | |
1886 | if (git_env_bool(GIT_TEST_COMMIT_GRAPH_NO_GDAT, 0)) | |
1887 | ctx->write_generation_data = 0; | |
1888 | if (ctx->write_generation_data) { | |
1889 | chunks[num_chunks].id = GRAPH_CHUNKID_GENERATION_DATA; | |
1890 | chunks[num_chunks].size = sizeof(uint32_t) * ctx->commits.nr; | |
1891 | chunks[num_chunks].write_fn = write_graph_chunk_generation_data; | |
1892 | num_chunks++; | |
1893 | } | |
1894 | if (ctx->num_generation_data_overflows) { | |
1895 | chunks[num_chunks].id = GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW; | |
1896 | chunks[num_chunks].size = sizeof(timestamp_t) * ctx->num_generation_data_overflows; | |
1897 | chunks[num_chunks].write_fn = write_graph_chunk_generation_data_overflow; | |
1898 | num_chunks++; | |
1899 | } | |
144354b0 | 1900 | if (ctx->num_extra_edges) { |
7fbfe07a SG |
1901 | chunks[num_chunks].id = GRAPH_CHUNKID_EXTRAEDGES; |
1902 | chunks[num_chunks].size = 4 * ctx->num_extra_edges; | |
17e6275f | 1903 | chunks[num_chunks].write_fn = write_graph_chunk_extra_edges; |
144354b0 DS |
1904 | num_chunks++; |
1905 | } | |
76ffbca7 | 1906 | if (ctx->changed_paths) { |
7fbfe07a SG |
1907 | chunks[num_chunks].id = GRAPH_CHUNKID_BLOOMINDEXES; |
1908 | chunks[num_chunks].size = sizeof(uint32_t) * ctx->commits.nr; | |
17e6275f | 1909 | chunks[num_chunks].write_fn = write_graph_chunk_bloom_indexes; |
76ffbca7 | 1910 | num_chunks++; |
7fbfe07a SG |
1911 | chunks[num_chunks].id = GRAPH_CHUNKID_BLOOMDATA; |
1912 | chunks[num_chunks].size = sizeof(uint32_t) * 3 | |
bb4d60e5 | 1913 | + ctx->total_bloom_filter_data_size; |
17e6275f | 1914 | chunks[num_chunks].write_fn = write_graph_chunk_bloom_data; |
76ffbca7 GS |
1915 | num_chunks++; |
1916 | } | |
6c622f9f | 1917 | if (ctx->num_commit_graphs_after > 1) { |
7fbfe07a SG |
1918 | chunks[num_chunks].id = GRAPH_CHUNKID_BASE; |
1919 | chunks[num_chunks].size = hashsz * (ctx->num_commit_graphs_after - 1); | |
17e6275f | 1920 | chunks[num_chunks].write_fn = write_graph_chunk_base; |
6c622f9f DS |
1921 | num_chunks++; |
1922 | } | |
144354b0 | 1923 | |
7fbfe07a SG |
1924 | chunks[num_chunks].id = 0; |
1925 | chunks[num_chunks].size = 0; | |
144354b0 DS |
1926 | |
1927 | hashwrite_be32(f, GRAPH_SIGNATURE); | |
1928 | ||
1929 | hashwrite_u8(f, GRAPH_VERSION); | |
1930 | hashwrite_u8(f, oid_version()); | |
1931 | hashwrite_u8(f, num_chunks); | |
6c622f9f | 1932 | hashwrite_u8(f, ctx->num_commit_graphs_after - 1); |
08fd81c9 | 1933 | |
bb4d60e5 | 1934 | chunk_offset = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH; |
08fd81c9 DS |
1935 | for (i = 0; i <= num_chunks; i++) { |
1936 | uint32_t chunk_write[3]; | |
1937 | ||
7fbfe07a | 1938 | chunk_write[0] = htonl(chunks[i].id); |
bb4d60e5 SG |
1939 | chunk_write[1] = htonl(chunk_offset >> 32); |
1940 | chunk_write[2] = htonl(chunk_offset & 0xffffffff); | |
08fd81c9 | 1941 | hashwrite(f, chunk_write, 12); |
bb4d60e5 | 1942 | |
7fbfe07a | 1943 | chunk_offset += chunks[i].size; |
08fd81c9 DS |
1944 | } |
1945 | ||
238def57 | 1946 | if (ctx->report_progress) { |
28944739 ÆAB |
1947 | strbuf_addf(&progress_title, |
1948 | Q_("Writing out commit graph in %d pass", | |
1949 | "Writing out commit graph in %d passes", | |
1950 | num_chunks), | |
1951 | num_chunks); | |
238def57 | 1952 | ctx->progress = start_delayed_progress( |
28944739 | 1953 | progress_title.buf, |
238def57 | 1954 | num_chunks * ctx->commits.nr); |
28944739 | 1955 | } |
17e6275f SG |
1956 | |
1957 | for (i = 0; i < num_chunks; i++) { | |
2dd4fed9 SG |
1958 | uint64_t start_offset = f->total + f->offset; |
1959 | ||
17e6275f SG |
1960 | if (chunks[i].write_fn(f, ctx)) |
1961 | return -1; | |
2dd4fed9 SG |
1962 | |
1963 | if (f->total + f->offset != start_offset + chunks[i].size) | |
1964 | BUG("expected to write %"PRId64" bytes to chunk %"PRIx32", but wrote %"PRId64" instead", | |
1965 | chunks[i].size, chunks[i].id, | |
1966 | f->total + f->offset - start_offset); | |
6c622f9f | 1967 | } |
17e6275f | 1968 | |
238def57 | 1969 | stop_progress(&ctx->progress); |
28944739 | 1970 | strbuf_release(&progress_title); |
08fd81c9 | 1971 | |
6c622f9f DS |
1972 | if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) { |
1973 | char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid)); | |
ad2dd5bb | 1974 | char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash); |
6c622f9f DS |
1975 | |
1976 | free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]); | |
1977 | free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]); | |
1978 | ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name; | |
1979 | ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash; | |
1980 | } | |
1981 | ||
c3a3a964 | 1982 | close_commit_graph(ctx->r->objects); |
6c622f9f DS |
1983 | finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC); |
1984 | ||
1985 | if (ctx->split) { | |
1986 | FILE *chainf = fdopen_lock_file(&lk, "w"); | |
1987 | char *final_graph_name; | |
1988 | int result; | |
1989 | ||
1990 | close(fd); | |
1991 | ||
1992 | if (!chainf) { | |
1993 | error(_("unable to open commit-graph chain file")); | |
1994 | return -1; | |
1995 | } | |
1996 | ||
1997 | if (ctx->base_graph_name) { | |
8a6ac287 TB |
1998 | const char *dest; |
1999 | int idx = ctx->num_commit_graphs_after - 1; | |
2000 | if (ctx->num_commit_graphs_after > 1) | |
2001 | idx--; | |
2002 | ||
2003 | dest = ctx->commit_graph_filenames_after[idx]; | |
6c622f9f | 2004 | |
135a7123 DS |
2005 | if (strcmp(ctx->base_graph_name, dest)) { |
2006 | result = rename(ctx->base_graph_name, dest); | |
2007 | ||
2008 | if (result) { | |
2009 | error(_("failed to rename base commit-graph file")); | |
2010 | return -1; | |
2011 | } | |
6c622f9f DS |
2012 | } |
2013 | } else { | |
ad2dd5bb | 2014 | char *graph_name = get_commit_graph_filename(ctx->odb); |
6c622f9f DS |
2015 | unlink(graph_name); |
2016 | } | |
2017 | ||
2018 | ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash)); | |
ad2dd5bb | 2019 | final_graph_name = get_split_graph_filename(ctx->odb, |
6c622f9f DS |
2020 | ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]); |
2021 | ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name; | |
2022 | ||
2023 | result = rename(ctx->graph_name, final_graph_name); | |
2024 | ||
2025 | for (i = 0; i < ctx->num_commit_graphs_after; i++) | |
2026 | fprintf(lk.tempfile->fp, "%s\n", ctx->commit_graph_hash_after[i]); | |
2027 | ||
2028 | if (result) { | |
2029 | error(_("failed to rename temporary commit-graph file")); | |
2030 | return -1; | |
2031 | } | |
2032 | } | |
2033 | ||
08fd81c9 DS |
2034 | commit_lock_file(&lk); |
2035 | ||
238def57 DS |
2036 | return 0; |
2037 | } | |
2038 | ||
1771be90 DS |
2039 | static void split_graph_merge_strategy(struct write_commit_graph_context *ctx) |
2040 | { | |
8da02ce6 AH |
2041 | struct commit_graph *g; |
2042 | uint32_t num_commits; | |
fdbde82f | 2043 | enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED; |
1771be90 DS |
2044 | uint32_t i; |
2045 | ||
c2bc6e6a DS |
2046 | int max_commits = 0; |
2047 | int size_mult = 2; | |
2048 | ||
98bb7961 TB |
2049 | if (ctx->opts) { |
2050 | max_commits = ctx->opts->max_commits; | |
63020f17 | 2051 | |
98bb7961 TB |
2052 | if (ctx->opts->size_multiple) |
2053 | size_mult = ctx->opts->size_multiple; | |
fdbde82f | 2054 | |
98bb7961 | 2055 | flags = ctx->opts->split_flags; |
c2bc6e6a DS |
2056 | } |
2057 | ||
1771be90 | 2058 | g = ctx->r->objects->commit_graph; |
8da02ce6 | 2059 | num_commits = ctx->commits.nr; |
8a6ac287 TB |
2060 | if (flags == COMMIT_GRAPH_SPLIT_REPLACE) |
2061 | ctx->num_commit_graphs_after = 1; | |
2062 | else | |
2063 | ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1; | |
1771be90 | 2064 | |
8a6ac287 TB |
2065 | if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED && |
2066 | flags != COMMIT_GRAPH_SPLIT_REPLACE) { | |
fdbde82f TB |
2067 | while (g && (g->num_commits <= size_mult * num_commits || |
2068 | (max_commits && num_commits > max_commits))) { | |
2069 | if (g->odb != ctx->odb) | |
2070 | break; | |
c523035c | 2071 | |
fdbde82f TB |
2072 | num_commits += g->num_commits; |
2073 | g = g->base_graph; | |
1771be90 | 2074 | |
fdbde82f TB |
2075 | ctx->num_commit_graphs_after--; |
2076 | } | |
1771be90 DS |
2077 | } |
2078 | ||
8a6ac287 TB |
2079 | if (flags != COMMIT_GRAPH_SPLIT_REPLACE) |
2080 | ctx->new_base_graph = g; | |
2081 | else if (ctx->num_commit_graphs_after != 1) | |
2082 | BUG("split_graph_merge_strategy: num_commit_graphs_after " | |
2083 | "should be 1 with --split=replace"); | |
1771be90 | 2084 | |
c523035c | 2085 | if (ctx->num_commit_graphs_after == 2) { |
ad2dd5bb | 2086 | char *old_graph_name = get_commit_graph_filename(g->odb); |
c523035c DS |
2087 | |
2088 | if (!strcmp(g->filename, old_graph_name) && | |
ad2dd5bb | 2089 | g->odb != ctx->odb) { |
c523035c DS |
2090 | ctx->num_commit_graphs_after = 1; |
2091 | ctx->new_base_graph = NULL; | |
2092 | } | |
2093 | ||
2094 | free(old_graph_name); | |
2095 | } | |
2096 | ||
b78a556a TB |
2097 | CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after); |
2098 | CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after); | |
1771be90 DS |
2099 | |
2100 | for (i = 0; i < ctx->num_commit_graphs_after && | |
2101 | i < ctx->num_commit_graphs_before; i++) | |
2102 | ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]); | |
2103 | ||
2104 | i = ctx->num_commit_graphs_before - 1; | |
2105 | g = ctx->r->objects->commit_graph; | |
2106 | ||
2107 | while (g) { | |
2108 | if (i < ctx->num_commit_graphs_after) | |
2109 | ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid)); | |
2110 | ||
1fdc383c AK |
2111 | /* |
2112 | * If the topmost remaining layer has generation data chunk, the | |
2113 | * resultant layer also has generation data chunk. | |
2114 | */ | |
2115 | if (i == ctx->num_commit_graphs_after - 2) | |
2116 | ctx->write_generation_data = !!g->chunk_generation_data; | |
2117 | ||
1771be90 DS |
2118 | i--; |
2119 | g = g->base_graph; | |
2120 | } | |
2121 | } | |
2122 | ||
2123 | static void merge_commit_graph(struct write_commit_graph_context *ctx, | |
2124 | struct commit_graph *g) | |
2125 | { | |
2126 | uint32_t i; | |
2127 | uint32_t offset = g->num_commits_in_base; | |
2128 | ||
2129 | ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc); | |
2130 | ||
2131 | for (i = 0; i < g->num_commits; i++) { | |
2132 | struct object_id oid; | |
2133 | struct commit *result; | |
2134 | ||
2135 | display_progress(ctx->progress, i + 1); | |
2136 | ||
2137 | load_oid_from_graph(g, i + offset, &oid); | |
2138 | ||
2139 | /* only add commits if they still exist in the repo */ | |
2140 | result = lookup_commit_reference_gently(ctx->r, &oid, 1); | |
2141 | ||
2142 | if (result) { | |
2143 | ctx->commits.list[ctx->commits.nr] = result; | |
2144 | ctx->commits.nr++; | |
2145 | } | |
2146 | } | |
2147 | } | |
2148 | ||
2149 | static int commit_compare(const void *_a, const void *_b) | |
2150 | { | |
2151 | const struct commit *a = *(const struct commit **)_a; | |
2152 | const struct commit *b = *(const struct commit **)_b; | |
2153 | return oidcmp(&a->object.oid, &b->object.oid); | |
2154 | } | |
2155 | ||
2156 | static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx) | |
2157 | { | |
150f1157 | 2158 | uint32_t i, dedup_i = 0; |
1771be90 DS |
2159 | |
2160 | if (ctx->report_progress) | |
2161 | ctx->progress = start_delayed_progress( | |
2162 | _("Scanning merged commits"), | |
2163 | ctx->commits.nr); | |
2164 | ||
2165 | QSORT(ctx->commits.list, ctx->commits.nr, commit_compare); | |
2166 | ||
2167 | ctx->num_extra_edges = 0; | |
2168 | for (i = 0; i < ctx->commits.nr; i++) { | |
2169 | display_progress(ctx->progress, i); | |
2170 | ||
2171 | if (i && oideq(&ctx->commits.list[i - 1]->object.oid, | |
2172 | &ctx->commits.list[i]->object.oid)) { | |
150f1157 DS |
2173 | /* |
2174 | * Silently ignore duplicates. These were likely | |
2175 | * created due to a commit appearing in multiple | |
2176 | * layers of the chain, which is unexpected but | |
2177 | * not invalid. We should make sure there is a | |
2178 | * unique copy in the new layer. | |
2179 | */ | |
1771be90 | 2180 | } else { |
689a146c | 2181 | unsigned int num_parents; |
1771be90 | 2182 | |
150f1157 DS |
2183 | ctx->commits.list[dedup_i] = ctx->commits.list[i]; |
2184 | dedup_i++; | |
2185 | ||
689a146c | 2186 | num_parents = commit_list_count(ctx->commits.list[i]->parents); |
1771be90 | 2187 | if (num_parents > 2) |
a35bea40 | 2188 | ctx->num_extra_edges += num_parents - 1; |
1771be90 DS |
2189 | } |
2190 | } | |
2191 | ||
150f1157 DS |
2192 | ctx->commits.nr = dedup_i; |
2193 | ||
1771be90 DS |
2194 | stop_progress(&ctx->progress); |
2195 | } | |
2196 | ||
2197 | static void merge_commit_graphs(struct write_commit_graph_context *ctx) | |
2198 | { | |
2199 | struct commit_graph *g = ctx->r->objects->commit_graph; | |
2200 | uint32_t current_graph_number = ctx->num_commit_graphs_before; | |
1771be90 DS |
2201 | |
2202 | while (g && current_graph_number >= ctx->num_commit_graphs_after) { | |
2203 | current_graph_number--; | |
2204 | ||
d68ce906 RS |
2205 | if (ctx->report_progress) |
2206 | ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0); | |
1771be90 DS |
2207 | |
2208 | merge_commit_graph(ctx, g); | |
2209 | stop_progress(&ctx->progress); | |
1771be90 DS |
2210 | |
2211 | g = g->base_graph; | |
2212 | } | |
2213 | ||
2214 | if (g) { | |
2215 | ctx->new_base_graph = g; | |
2216 | ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base; | |
2217 | } | |
2218 | ||
2219 | if (ctx->new_base_graph) | |
2220 | ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename); | |
2221 | ||
2222 | sort_and_scan_merged_commits(ctx); | |
2223 | } | |
2224 | ||
8d84097f DS |
2225 | static void mark_commit_graphs(struct write_commit_graph_context *ctx) |
2226 | { | |
2227 | uint32_t i; | |
2228 | time_t now = time(NULL); | |
2229 | ||
2230 | for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) { | |
2231 | struct stat st; | |
2232 | struct utimbuf updated_time; | |
2233 | ||
2234 | stat(ctx->commit_graph_filenames_before[i], &st); | |
2235 | ||
2236 | updated_time.actime = st.st_atime; | |
2237 | updated_time.modtime = now; | |
2238 | utime(ctx->commit_graph_filenames_before[i], &updated_time); | |
2239 | } | |
2240 | } | |
2241 | ||
2242 | static void expire_commit_graphs(struct write_commit_graph_context *ctx) | |
2243 | { | |
2244 | struct strbuf path = STRBUF_INIT; | |
2245 | DIR *dir; | |
2246 | struct dirent *de; | |
2247 | size_t dirnamelen; | |
c2bc6e6a DS |
2248 | timestamp_t expire_time = time(NULL); |
2249 | ||
98bb7961 TB |
2250 | if (ctx->opts && ctx->opts->expire_time) |
2251 | expire_time = ctx->opts->expire_time; | |
ba41112a | 2252 | if (!ctx->split) { |
663b2b1b | 2253 | char *chain_file_name = get_commit_graph_chain_filename(ctx->odb); |
ba41112a DS |
2254 | unlink(chain_file_name); |
2255 | free(chain_file_name); | |
2256 | ctx->num_commit_graphs_after = 0; | |
2257 | } | |
8d84097f | 2258 | |
0bd52e27 | 2259 | strbuf_addstr(&path, ctx->odb->path); |
8d84097f DS |
2260 | strbuf_addstr(&path, "/info/commit-graphs"); |
2261 | dir = opendir(path.buf); | |
2262 | ||
0aa6bce7 RS |
2263 | if (!dir) |
2264 | goto out; | |
8d84097f DS |
2265 | |
2266 | strbuf_addch(&path, '/'); | |
2267 | dirnamelen = path.len; | |
2268 | while ((de = readdir(dir)) != NULL) { | |
2269 | struct stat st; | |
2270 | uint32_t i, found = 0; | |
2271 | ||
2272 | strbuf_setlen(&path, dirnamelen); | |
2273 | strbuf_addstr(&path, de->d_name); | |
2274 | ||
2275 | stat(path.buf, &st); | |
2276 | ||
2277 | if (st.st_mtime > expire_time) | |
2278 | continue; | |
2279 | if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph")) | |
2280 | continue; | |
2281 | ||
2282 | for (i = 0; i < ctx->num_commit_graphs_after; i++) { | |
2283 | if (!strcmp(ctx->commit_graph_filenames_after[i], | |
2284 | path.buf)) { | |
2285 | found = 1; | |
2286 | break; | |
2287 | } | |
2288 | } | |
2289 | ||
2290 | if (!found) | |
2291 | unlink(path.buf); | |
8d84097f | 2292 | } |
0aa6bce7 RS |
2293 | |
2294 | out: | |
2295 | strbuf_release(&path); | |
8d84097f DS |
2296 | } |
2297 | ||
0bd52e27 | 2298 | int write_commit_graph(struct object_directory *odb, |
238def57 | 2299 | struct string_list *pack_indexes, |
6830c360 | 2300 | struct oidset *commits, |
39d88318 | 2301 | enum commit_graph_write_flags flags, |
98bb7961 | 2302 | const struct commit_graph_opts *opts) |
238def57 DS |
2303 | { |
2304 | struct write_commit_graph_context *ctx; | |
1cbdbf3b | 2305 | uint32_t i; |
e103f727 | 2306 | int res = 0; |
8a6ac287 | 2307 | int replace = 0; |
9a7a9ed1 | 2308 | struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS; |
72a2bfca | 2309 | struct topo_level_slab topo_levels; |
08fd81c9 | 2310 | |
85102ac7 DS |
2311 | prepare_repo_settings(the_repository); |
2312 | if (!the_repository->settings.core_commit_graph) { | |
2313 | warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled")); | |
2314 | return 0; | |
2315 | } | |
d6538246 | 2316 | if (!commit_graph_compatible(the_repository)) |
e103f727 | 2317 | return 0; |
d6538246 | 2318 | |
c9905bea DS |
2319 | ctx = xcalloc(1, sizeof(struct write_commit_graph_context)); |
2320 | ctx->r = the_repository; | |
0bd52e27 | 2321 | ctx->odb = odb; |
39d88318 SG |
2322 | ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0; |
2323 | ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0; | |
2324 | ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0; | |
98bb7961 | 2325 | ctx->opts = opts; |
f97b9325 | 2326 | ctx->total_bloom_filter_data_size = 0; |
e8b63005 AK |
2327 | ctx->write_generation_data = 1; |
2328 | ctx->num_generation_data_overflows = 0; | |
6c622f9f | 2329 | |
9a7a9ed1 TB |
2330 | bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY", |
2331 | bloom_settings.bits_per_entry); | |
2332 | bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES", | |
2333 | bloom_settings.num_hashes); | |
2334 | bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS", | |
2335 | bloom_settings.max_changed_paths); | |
2336 | ctx->bloom_settings = &bloom_settings; | |
2337 | ||
72a2bfca AK |
2338 | init_topo_level_slab(&topo_levels); |
2339 | ctx->topo_levels = &topo_levels; | |
2340 | ||
2341 | if (ctx->r->objects->commit_graph) { | |
2342 | struct commit_graph *g = ctx->r->objects->commit_graph; | |
2343 | ||
2344 | while (g) { | |
2345 | g->topo_levels = &topo_levels; | |
2346 | g = g->base_graph; | |
2347 | } | |
2348 | } | |
2349 | ||
0087a87b DS |
2350 | if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS) |
2351 | ctx->changed_paths = 1; | |
2352 | if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) { | |
2353 | struct commit_graph *g; | |
2354 | prepare_commit_graph_one(ctx->r, ctx->odb); | |
2355 | ||
2356 | g = ctx->r->objects->commit_graph; | |
2357 | ||
2358 | /* We have changed-paths already. Keep them in the next graph */ | |
2359 | if (g && g->chunk_bloom_data) { | |
2360 | ctx->changed_paths = 1; | |
2361 | ctx->bloom_settings = g->bloom_filter_settings; | |
2362 | } | |
2363 | } | |
2364 | ||
6c622f9f DS |
2365 | if (ctx->split) { |
2366 | struct commit_graph *g; | |
2367 | prepare_commit_graph(ctx->r); | |
2368 | ||
2369 | g = ctx->r->objects->commit_graph; | |
2370 | ||
2371 | while (g) { | |
2372 | ctx->num_commit_graphs_before++; | |
2373 | g = g->base_graph; | |
2374 | } | |
2375 | ||
2376 | if (ctx->num_commit_graphs_before) { | |
2377 | ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before); | |
2378 | i = ctx->num_commit_graphs_before; | |
2379 | g = ctx->r->objects->commit_graph; | |
2380 | ||
2381 | while (g) { | |
2382 | ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename); | |
2383 | g = g->base_graph; | |
2384 | } | |
2385 | } | |
8a6ac287 | 2386 | |
98bb7961 TB |
2387 | if (ctx->opts) |
2388 | replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE; | |
6c622f9f | 2389 | } |
c9905bea DS |
2390 | |
2391 | ctx->approx_nr_objects = approximate_object_count(); | |
c9905bea | 2392 | |
a5f1c448 | 2393 | if (ctx->append) |
13c24992 | 2394 | prepare_commit_graph_one(ctx->r, ctx->odb); |
c9905bea DS |
2395 | |
2396 | if (ctx->append && ctx->r->objects->commit_graph) { | |
2397 | struct commit_graph *g = ctx->r->objects->commit_graph; | |
2398 | for (i = 0; i < g->num_commits; i++) { | |
a5f1c448 JK |
2399 | struct object_id oid; |
2400 | hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * i); | |
2401 | oid_array_append(&ctx->oids, &oid); | |
7547b95b DS |
2402 | } |
2403 | } | |
2404 | ||
049d51a2 | 2405 | if (pack_indexes) { |
3d112755 | 2406 | ctx->order_by_pack = 1; |
ef5b83f2 DS |
2407 | if ((res = fill_oids_from_packs(ctx, pack_indexes))) |
2408 | goto cleanup; | |
3d5df01b DS |
2409 | } |
2410 | ||
6830c360 TB |
2411 | if (commits) { |
2412 | if ((res = fill_oids_from_commits(ctx, commits))) | |
7c5c9b9c SG |
2413 | goto cleanup; |
2414 | } | |
3d5df01b | 2415 | |
9b6606f4 | 2416 | if (!pack_indexes && !commits) { |
3d112755 | 2417 | ctx->order_by_pack = 1; |
b2c83060 | 2418 | fill_oids_from_all_packs(ctx); |
3d112755 | 2419 | } |
049d51a2 | 2420 | |
c9905bea | 2421 | close_reachable(ctx); |
08fd81c9 | 2422 | |
f998d542 | 2423 | copy_oids_to_commits(ctx); |
08fd81c9 | 2424 | |
c9905bea | 2425 | if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) { |
e103f727 DS |
2426 | error(_("too many commits to write graph")); |
2427 | res = -1; | |
2428 | goto cleanup; | |
2429 | } | |
08fd81c9 | 2430 | |
8a6ac287 | 2431 | if (!ctx->commits.nr && !replace) |
6c622f9f DS |
2432 | goto cleanup; |
2433 | ||
1771be90 DS |
2434 | if (ctx->split) { |
2435 | split_graph_merge_strategy(ctx); | |
2436 | ||
8a6ac287 TB |
2437 | if (!replace) |
2438 | merge_commit_graphs(ctx); | |
1771be90 | 2439 | } else |
6c622f9f DS |
2440 | ctx->num_commit_graphs_after = 1; |
2441 | ||
1fdc383c AK |
2442 | validate_mixed_generation_chain(ctx->r->objects->commit_graph); |
2443 | ||
9c2c0a82 DS |
2444 | compute_topological_levels(ctx); |
2445 | if (ctx->write_generation_data) | |
2446 | compute_generation_numbers(ctx); | |
08fd81c9 | 2447 | |
f97b9325 GS |
2448 | if (ctx->changed_paths) |
2449 | compute_bloom_filters(ctx); | |
2450 | ||
238def57 | 2451 | res = write_commit_graph_file(ctx); |
08fd81c9 | 2452 | |
ba41112a | 2453 | if (ctx->split) |
8d84097f | 2454 | mark_commit_graphs(ctx); |
ba41112a DS |
2455 | |
2456 | expire_commit_graphs(ctx); | |
8d84097f | 2457 | |
e103f727 | 2458 | cleanup: |
238def57 | 2459 | free(ctx->graph_name); |
c9905bea | 2460 | free(ctx->commits.list); |
a5f1c448 | 2461 | oid_array_clear(&ctx->oids); |
6c622f9f DS |
2462 | |
2463 | if (ctx->commit_graph_filenames_after) { | |
2464 | for (i = 0; i < ctx->num_commit_graphs_after; i++) { | |
2465 | free(ctx->commit_graph_filenames_after[i]); | |
2466 | free(ctx->commit_graph_hash_after[i]); | |
2467 | } | |
2468 | ||
2469 | for (i = 0; i < ctx->num_commit_graphs_before; i++) | |
2470 | free(ctx->commit_graph_filenames_before[i]); | |
2471 | ||
2472 | free(ctx->commit_graph_filenames_after); | |
2473 | free(ctx->commit_graph_filenames_before); | |
2474 | free(ctx->commit_graph_hash_after); | |
2475 | } | |
2476 | ||
c9905bea | 2477 | free(ctx); |
e103f727 DS |
2478 | |
2479 | return res; | |
08fd81c9 | 2480 | } |
283e68c7 | 2481 | |
41df0e30 | 2482 | #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2 |
283e68c7 DS |
2483 | static int verify_commit_graph_error; |
2484 | ||
2485 | static void graph_report(const char *fmt, ...) | |
2486 | { | |
2487 | va_list ap; | |
2488 | ||
2489 | verify_commit_graph_error = 1; | |
2490 | va_start(ap, fmt); | |
2491 | vfprintf(stderr, fmt, ap); | |
2492 | fprintf(stderr, "\n"); | |
2493 | va_end(ap); | |
2494 | } | |
2495 | ||
1373e547 DS |
2496 | #define GENERATION_ZERO_EXISTS 1 |
2497 | #define GENERATION_NUMBER_EXISTS 2 | |
2498 | ||
3da4b609 | 2499 | int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags) |
283e68c7 | 2500 | { |
9bda8467 | 2501 | uint32_t i, cur_fanout_pos = 0; |
41df0e30 | 2502 | struct object_id prev_oid, cur_oid, checksum; |
1373e547 | 2503 | int generation_zero = 0; |
41df0e30 DS |
2504 | struct hashfile *f; |
2505 | int devnull; | |
1f7f557f | 2506 | struct progress *progress = NULL; |
3da4b609 | 2507 | int local_error = 0; |
9bda8467 | 2508 | |
283e68c7 DS |
2509 | if (!g) { |
2510 | graph_report("no commit-graph file loaded"); | |
2511 | return 1; | |
2512 | } | |
2513 | ||
2ac138d5 | 2514 | verify_commit_graph_error = verify_commit_graph_lite(g); |
9bda8467 DS |
2515 | if (verify_commit_graph_error) |
2516 | return verify_commit_graph_error; | |
2517 | ||
41df0e30 DS |
2518 | devnull = open("/dev/null", O_WRONLY); |
2519 | f = hashfd(devnull, NULL); | |
2520 | hashwrite(f, g->data, g->data_len - g->hash_len); | |
2521 | finalize_hashfile(f, checksum.hash, CSUM_CLOSE); | |
67947c34 | 2522 | if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) { |
41df0e30 DS |
2523 | graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt")); |
2524 | verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH; | |
2525 | } | |
2526 | ||
9bda8467 | 2527 | for (i = 0; i < g->num_commits; i++) { |
2e3c0737 DS |
2528 | struct commit *graph_commit; |
2529 | ||
9bda8467 DS |
2530 | hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i); |
2531 | ||
2532 | if (i && oidcmp(&prev_oid, &cur_oid) >= 0) | |
93b4405f | 2533 | graph_report(_("commit-graph has incorrect OID order: %s then %s"), |
9bda8467 DS |
2534 | oid_to_hex(&prev_oid), |
2535 | oid_to_hex(&cur_oid)); | |
2536 | ||
2537 | oidcpy(&prev_oid, &cur_oid); | |
2538 | ||
2539 | while (cur_oid.hash[0] > cur_fanout_pos) { | |
2540 | uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos); | |
2541 | ||
2542 | if (i != fanout_value) | |
93b4405f | 2543 | graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"), |
9bda8467 DS |
2544 | cur_fanout_pos, fanout_value, i); |
2545 | cur_fanout_pos++; | |
2546 | } | |
2e3c0737 | 2547 | |
82952964 | 2548 | graph_commit = lookup_commit(r, &cur_oid); |
4f542b7a | 2549 | if (!parse_commit_in_graph_one(r, g, graph_commit)) |
93b4405f | 2550 | graph_report(_("failed to parse commit %s from commit-graph"), |
2e3c0737 | 2551 | oid_to_hex(&cur_oid)); |
9bda8467 DS |
2552 | } |
2553 | ||
2554 | while (cur_fanout_pos < 256) { | |
2555 | uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos); | |
2556 | ||
2557 | if (g->num_commits != fanout_value) | |
93b4405f | 2558 | graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"), |
9bda8467 DS |
2559 | cur_fanout_pos, fanout_value, i); |
2560 | ||
2561 | cur_fanout_pos++; | |
2562 | } | |
2563 | ||
41df0e30 | 2564 | if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH) |
96af91d4 DS |
2565 | return verify_commit_graph_error; |
2566 | ||
73716122 GS |
2567 | if (flags & COMMIT_GRAPH_WRITE_PROGRESS) |
2568 | progress = start_progress(_("Verifying commits in commit graph"), | |
2569 | g->num_commits); | |
2570 | ||
96af91d4 | 2571 | for (i = 0; i < g->num_commits; i++) { |
2e3c0737 | 2572 | struct commit *graph_commit, *odb_commit; |
53614b13 | 2573 | struct commit_list *graph_parents, *odb_parents; |
d7f92784 AK |
2574 | timestamp_t max_generation = 0; |
2575 | timestamp_t generation; | |
96af91d4 | 2576 | |
1f7f557f | 2577 | display_progress(progress, i + 1); |
96af91d4 DS |
2578 | hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i); |
2579 | ||
82952964 | 2580 | graph_commit = lookup_commit(r, &cur_oid); |
a378509e | 2581 | odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r)); |
96af91d4 | 2582 | if (parse_commit_internal(odb_commit, 0, 0)) { |
93b4405f | 2583 | graph_report(_("failed to parse commit %s from object database for commit-graph"), |
96af91d4 DS |
2584 | oid_to_hex(&cur_oid)); |
2585 | continue; | |
2586 | } | |
2e3c0737 | 2587 | |
4f542b7a | 2588 | if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid, |
2e3c0737 | 2589 | get_commit_tree_oid(odb_commit))) |
93b4405f | 2590 | graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"), |
2e3c0737 DS |
2591 | oid_to_hex(&cur_oid), |
2592 | oid_to_hex(get_commit_tree_oid(graph_commit)), | |
2593 | oid_to_hex(get_commit_tree_oid(odb_commit))); | |
53614b13 DS |
2594 | |
2595 | graph_parents = graph_commit->parents; | |
2596 | odb_parents = odb_commit->parents; | |
2597 | ||
2598 | while (graph_parents) { | |
2599 | if (odb_parents == NULL) { | |
93b4405f | 2600 | graph_report(_("commit-graph parent list for commit %s is too long"), |
53614b13 DS |
2601 | oid_to_hex(&cur_oid)); |
2602 | break; | |
2603 | } | |
2604 | ||
3da4b609 DS |
2605 | /* parse parent in case it is in a base graph */ |
2606 | parse_commit_in_graph_one(r, g, graph_parents->item); | |
2607 | ||
9001dc2a | 2608 | if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid)) |
93b4405f | 2609 | graph_report(_("commit-graph parent for %s is %s != %s"), |
53614b13 DS |
2610 | oid_to_hex(&cur_oid), |
2611 | oid_to_hex(&graph_parents->item->object.oid), | |
2612 | oid_to_hex(&odb_parents->item->object.oid)); | |
2613 | ||
c752ad09 AK |
2614 | generation = commit_graph_generation(graph_parents->item); |
2615 | if (generation > max_generation) | |
2616 | max_generation = generation; | |
1373e547 | 2617 | |
53614b13 DS |
2618 | graph_parents = graph_parents->next; |
2619 | odb_parents = odb_parents->next; | |
2620 | } | |
2621 | ||
2622 | if (odb_parents != NULL) | |
93b4405f | 2623 | graph_report(_("commit-graph parent list for commit %s terminates early"), |
53614b13 | 2624 | oid_to_hex(&cur_oid)); |
1373e547 | 2625 | |
c49c82aa | 2626 | if (!commit_graph_generation(graph_commit)) { |
1373e547 | 2627 | if (generation_zero == GENERATION_NUMBER_EXISTS) |
93b4405f | 2628 | graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"), |
1373e547 DS |
2629 | oid_to_hex(&cur_oid)); |
2630 | generation_zero = GENERATION_ZERO_EXISTS; | |
2631 | } else if (generation_zero == GENERATION_ZERO_EXISTS) | |
93b4405f | 2632 | graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"), |
1373e547 DS |
2633 | oid_to_hex(&cur_oid)); |
2634 | ||
2635 | if (generation_zero == GENERATION_ZERO_EXISTS) | |
2636 | continue; | |
2637 | ||
2638 | /* | |
e8b63005 AK |
2639 | * If we are using topological level and one of our parents has |
2640 | * generation GENERATION_NUMBER_V1_MAX, then our generation is | |
2641 | * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic | |
2642 | * in the following condition. | |
1373e547 | 2643 | */ |
1fdc383c | 2644 | if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX) |
1373e547 DS |
2645 | max_generation--; |
2646 | ||
c752ad09 | 2647 | generation = commit_graph_generation(graph_commit); |
e8b63005 AK |
2648 | if (generation < max_generation + 1) |
2649 | graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime), | |
1373e547 | 2650 | oid_to_hex(&cur_oid), |
c752ad09 | 2651 | generation, |
1373e547 | 2652 | max_generation + 1); |
88968ebf DS |
2653 | |
2654 | if (graph_commit->date != odb_commit->date) | |
93b4405f | 2655 | graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime), |
88968ebf DS |
2656 | oid_to_hex(&cur_oid), |
2657 | graph_commit->date, | |
2658 | odb_commit->date); | |
96af91d4 | 2659 | } |
1f7f557f | 2660 | stop_progress(&progress); |
96af91d4 | 2661 | |
3da4b609 DS |
2662 | local_error = verify_commit_graph_error; |
2663 | ||
2664 | if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph) | |
2665 | local_error |= verify_commit_graph(r, g->base_graph, flags); | |
2666 | ||
2667 | return local_error; | |
283e68c7 | 2668 | } |
c3756d5b JT |
2669 | |
2670 | void free_commit_graph(struct commit_graph *g) | |
2671 | { | |
2672 | if (!g) | |
2673 | return; | |
c8828530 | 2674 | if (g->data) { |
c3756d5b JT |
2675 | munmap((void *)g->data, g->data_len); |
2676 | g->data = NULL; | |
c3756d5b | 2677 | } |
6c622f9f | 2678 | free(g->filename); |
76ffbca7 | 2679 | free(g->bloom_filter_settings); |
c3756d5b JT |
2680 | free(g); |
2681 | } | |
6abada18 JK |
2682 | |
2683 | void disable_commit_graph(struct repository *r) | |
2684 | { | |
2685 | r->commit_graph_disabled = 1; | |
2686 | } |