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