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