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