]>
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" |
08fd81c9 DS |
19 | |
20 | #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */ | |
21 | #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */ | |
22 | #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */ | |
23 | #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */ | |
5af7417b | 24 | #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */ |
08fd81c9 | 25 | |
c1665998 | 26 | #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16) |
08fd81c9 DS |
27 | |
28 | #define GRAPH_VERSION_1 0x1 | |
29 | #define GRAPH_VERSION GRAPH_VERSION_1 | |
30 | ||
5af7417b | 31 | #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000 |
08fd81c9 DS |
32 | #define GRAPH_EDGE_LAST_MASK 0x7fffffff |
33 | #define GRAPH_PARENT_NONE 0x70000000 | |
34 | ||
35 | #define GRAPH_LAST_EDGE 0x80000000 | |
36 | ||
0e3b97cc | 37 | #define GRAPH_HEADER_SIZE 8 |
08fd81c9 DS |
38 | #define GRAPH_FANOUT_SIZE (4 * 256) |
39 | #define GRAPH_CHUNKLOOKUP_WIDTH 12 | |
0e3b97cc | 40 | #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \ |
c1665998 | 41 | + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) |
08fd81c9 | 42 | |
2a2e32bd | 43 | char *get_commit_graph_filename(const char *obj_dir) |
08fd81c9 DS |
44 | { |
45 | return xstrfmt("%s/info/commit-graph", obj_dir); | |
46 | } | |
47 | ||
c1665998 | 48 | static uint8_t oid_version(void) |
49 | { | |
50 | return 1; | |
51 | } | |
52 | ||
2a2e32bd DS |
53 | static struct commit_graph *alloc_commit_graph(void) |
54 | { | |
55 | struct commit_graph *g = xcalloc(1, sizeof(*g)); | |
56 | g->graph_fd = -1; | |
57 | ||
58 | return g; | |
59 | } | |
60 | ||
d6538246 DS |
61 | extern int read_replace_refs; |
62 | ||
63 | static int commit_graph_compatible(struct repository *r) | |
64 | { | |
5cef295f DS |
65 | if (!r->gitdir) |
66 | return 0; | |
67 | ||
d6538246 DS |
68 | if (read_replace_refs) { |
69 | prepare_replace_object(r); | |
70 | if (hashmap_get_size(&r->objects->replace_map->map)) | |
71 | return 0; | |
72 | } | |
73 | ||
20fd6d57 DS |
74 | prepare_commit_graft(r); |
75 | if (r->parsed_objects && r->parsed_objects->grafts_nr) | |
76 | return 0; | |
77 | if (is_repository_shallow(r)) | |
78 | return 0; | |
79 | ||
d6538246 DS |
80 | return 1; |
81 | } | |
82 | ||
61df89c8 ÆAB |
83 | int open_commit_graph(const char *graph_file, int *fd, struct stat *st) |
84 | { | |
85 | *fd = git_open(graph_file); | |
86 | if (*fd < 0) | |
87 | return 0; | |
88 | if (fstat(*fd, st)) { | |
89 | close(*fd); | |
90 | return 0; | |
91 | } | |
92 | return 1; | |
93 | } | |
94 | ||
67a530fa | 95 | struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st) |
2a2e32bd DS |
96 | { |
97 | void *graph_map; | |
2a2e32bd | 98 | size_t graph_size; |
aa658574 | 99 | struct commit_graph *ret; |
2a2e32bd | 100 | |
61df89c8 | 101 | graph_size = xsize_t(st->st_size); |
2a2e32bd DS |
102 | |
103 | if (graph_size < GRAPH_MIN_SIZE) { | |
104 | close(fd); | |
67a530fa | 105 | error(_("commit-graph file is too small")); |
61df89c8 | 106 | return NULL; |
2a2e32bd DS |
107 | } |
108 | graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0); | |
aa658574 JS |
109 | ret = parse_commit_graph(graph_map, fd, graph_size); |
110 | ||
111 | if (!ret) { | |
112 | munmap(graph_map, graph_size); | |
113 | close(fd); | |
aa658574 JS |
114 | } |
115 | ||
116 | return ret; | |
117 | } | |
118 | ||
2ac138d5 ÆAB |
119 | static int verify_commit_graph_lite(struct commit_graph *g) |
120 | { | |
121 | /* | |
122 | * Basic validation shared between parse_commit_graph() | |
123 | * which'll be called every time the graph is used, and the | |
124 | * much more expensive verify_commit_graph() used by | |
125 | * "commit-graph verify". | |
126 | * | |
127 | * There should only be very basic checks here to ensure that | |
128 | * we don't e.g. segfault in fill_commit_in_graph(), but | |
129 | * because this is a very hot codepath nothing that e.g. loops | |
130 | * over g->num_commits, or runs a checksum on the commit-graph | |
131 | * itself. | |
132 | */ | |
133 | if (!g->chunk_oid_fanout) { | |
134 | error("commit-graph is missing the OID Fanout chunk"); | |
135 | return 1; | |
136 | } | |
137 | if (!g->chunk_oid_lookup) { | |
138 | error("commit-graph is missing the OID Lookup chunk"); | |
139 | return 1; | |
140 | } | |
141 | if (!g->chunk_commit_data) { | |
142 | error("commit-graph is missing the Commit Data chunk"); | |
143 | return 1; | |
144 | } | |
145 | ||
146 | return 0; | |
147 | } | |
148 | ||
aa658574 JS |
149 | struct commit_graph *parse_commit_graph(void *graph_map, int fd, |
150 | size_t graph_size) | |
151 | { | |
152 | const unsigned char *data, *chunk_lookup; | |
153 | uint32_t i; | |
154 | struct commit_graph *graph; | |
155 | uint64_t last_chunk_offset; | |
156 | uint32_t last_chunk_id; | |
157 | uint32_t graph_signature; | |
158 | unsigned char graph_version, hash_version; | |
159 | ||
160 | if (!graph_map) | |
161 | return NULL; | |
162 | ||
163 | if (graph_size < GRAPH_MIN_SIZE) | |
164 | return NULL; | |
165 | ||
2a2e32bd DS |
166 | data = (const unsigned char *)graph_map; |
167 | ||
168 | graph_signature = get_be32(data); | |
169 | if (graph_signature != GRAPH_SIGNATURE) { | |
4f5b532d | 170 | error(_("graph signature %X does not match signature %X"), |
2a2e32bd | 171 | graph_signature, GRAPH_SIGNATURE); |
aa658574 | 172 | return NULL; |
2a2e32bd DS |
173 | } |
174 | ||
175 | graph_version = *(unsigned char*)(data + 4); | |
176 | if (graph_version != GRAPH_VERSION) { | |
4f5b532d | 177 | error(_("graph version %X does not match version %X"), |
2a2e32bd | 178 | graph_version, GRAPH_VERSION); |
aa658574 | 179 | return NULL; |
2a2e32bd DS |
180 | } |
181 | ||
182 | hash_version = *(unsigned char*)(data + 5); | |
c1665998 | 183 | if (hash_version != oid_version()) { |
4f5b532d | 184 | error(_("hash version %X does not match version %X"), |
c1665998 | 185 | hash_version, oid_version()); |
aa658574 | 186 | return NULL; |
2a2e32bd DS |
187 | } |
188 | ||
189 | graph = alloc_commit_graph(); | |
190 | ||
c1665998 | 191 | graph->hash_len = the_hash_algo->rawsz; |
2a2e32bd DS |
192 | graph->num_chunks = *(unsigned char*)(data + 6); |
193 | graph->graph_fd = fd; | |
194 | graph->data = graph_map; | |
195 | graph->data_len = graph_size; | |
196 | ||
197 | last_chunk_id = 0; | |
198 | last_chunk_offset = 8; | |
199 | chunk_lookup = data + 8; | |
200 | for (i = 0; i < graph->num_chunks; i++) { | |
d2b86fba JS |
201 | uint32_t chunk_id; |
202 | uint64_t chunk_offset; | |
2a2e32bd DS |
203 | int chunk_repeated = 0; |
204 | ||
d2b86fba JS |
205 | if (data + graph_size - chunk_lookup < |
206 | GRAPH_CHUNKLOOKUP_WIDTH) { | |
207 | error(_("chunk lookup table entry missing; graph file may be incomplete")); | |
208 | free(graph); | |
209 | return NULL; | |
210 | } | |
211 | ||
212 | chunk_id = get_be32(chunk_lookup + 0); | |
213 | chunk_offset = get_be64(chunk_lookup + 4); | |
214 | ||
2a2e32bd DS |
215 | chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH; |
216 | ||
c1665998 | 217 | if (chunk_offset > graph_size - the_hash_algo->rawsz) { |
4f5b532d | 218 | error(_("improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32), |
2a2e32bd | 219 | (uint32_t)chunk_offset); |
aa658574 JS |
220 | free(graph); |
221 | return NULL; | |
2a2e32bd DS |
222 | } |
223 | ||
224 | switch (chunk_id) { | |
225 | case GRAPH_CHUNKID_OIDFANOUT: | |
226 | if (graph->chunk_oid_fanout) | |
227 | chunk_repeated = 1; | |
228 | else | |
229 | graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset); | |
230 | break; | |
231 | ||
232 | case GRAPH_CHUNKID_OIDLOOKUP: | |
233 | if (graph->chunk_oid_lookup) | |
234 | chunk_repeated = 1; | |
235 | else | |
236 | graph->chunk_oid_lookup = data + chunk_offset; | |
237 | break; | |
238 | ||
239 | case GRAPH_CHUNKID_DATA: | |
240 | if (graph->chunk_commit_data) | |
241 | chunk_repeated = 1; | |
242 | else | |
243 | graph->chunk_commit_data = data + chunk_offset; | |
244 | break; | |
245 | ||
5af7417b SG |
246 | case GRAPH_CHUNKID_EXTRAEDGES: |
247 | if (graph->chunk_extra_edges) | |
2a2e32bd DS |
248 | chunk_repeated = 1; |
249 | else | |
5af7417b | 250 | graph->chunk_extra_edges = data + chunk_offset; |
2a2e32bd DS |
251 | break; |
252 | } | |
253 | ||
254 | if (chunk_repeated) { | |
4f5b532d | 255 | error(_("chunk id %08x appears multiple times"), chunk_id); |
aa658574 JS |
256 | free(graph); |
257 | return NULL; | |
2a2e32bd DS |
258 | } |
259 | ||
260 | if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP) | |
261 | { | |
262 | graph->num_commits = (chunk_offset - last_chunk_offset) | |
263 | / graph->hash_len; | |
264 | } | |
265 | ||
266 | last_chunk_id = chunk_id; | |
267 | last_chunk_offset = chunk_offset; | |
268 | } | |
269 | ||
2ac138d5 ÆAB |
270 | if (verify_commit_graph_lite(graph)) |
271 | return NULL; | |
272 | ||
2a2e32bd | 273 | return graph; |
2a2e32bd DS |
274 | } |
275 | ||
61df89c8 ÆAB |
276 | static struct commit_graph *load_commit_graph_one(const char *graph_file) |
277 | { | |
278 | ||
279 | struct stat st; | |
280 | int fd; | |
281 | int open_ok = open_commit_graph(graph_file, &fd, &st); | |
282 | ||
283 | if (!open_ok) | |
284 | return NULL; | |
285 | ||
67a530fa | 286 | return load_commit_graph_one_fd_st(fd, &st); |
61df89c8 ÆAB |
287 | } |
288 | ||
dade47c0 | 289 | static void prepare_commit_graph_one(struct repository *r, const char *obj_dir) |
177722b3 DS |
290 | { |
291 | char *graph_name; | |
292 | ||
dade47c0 | 293 | if (r->objects->commit_graph) |
177722b3 DS |
294 | return; |
295 | ||
296 | graph_name = get_commit_graph_filename(obj_dir); | |
dade47c0 | 297 | r->objects->commit_graph = |
85277506 | 298 | load_commit_graph_one(graph_name); |
177722b3 DS |
299 | |
300 | FREE_AND_NULL(graph_name); | |
301 | } | |
302 | ||
5faf357b JT |
303 | /* |
304 | * Return 1 if commit_graph is non-NULL, and 0 otherwise. | |
305 | * | |
306 | * On the first invocation, this function attemps to load the commit | |
307 | * graph if the_repository is configured to have one. | |
308 | */ | |
dade47c0 | 309 | static int prepare_commit_graph(struct repository *r) |
177722b3 | 310 | { |
263db403 | 311 | struct object_directory *odb; |
dade47c0 JT |
312 | int config_value; |
313 | ||
314 | if (r->objects->commit_graph_attempted) | |
315 | return !!r->objects->commit_graph; | |
316 | r->objects->commit_graph_attempted = 1; | |
317 | ||
859fdc0c DS |
318 | if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) && |
319 | (repo_config_get_bool(r, "core.commitgraph", &config_value) || | |
320 | !config_value)) | |
dade47c0 JT |
321 | /* |
322 | * This repository is not configured to use commit graphs, so | |
323 | * do not load one. (But report commit_graph_attempted anyway | |
324 | * so that commit graph loading is not attempted again for this | |
325 | * repository.) | |
326 | */ | |
5faf357b JT |
327 | return 0; |
328 | ||
d6538246 DS |
329 | if (!commit_graph_compatible(r)) |
330 | return 0; | |
331 | ||
dade47c0 | 332 | prepare_alt_odb(r); |
f0eaf638 | 333 | for (odb = r->objects->odb; |
263db403 JK |
334 | !r->objects->commit_graph && odb; |
335 | odb = odb->next) | |
336 | prepare_commit_graph_one(r, odb->path); | |
dade47c0 | 337 | return !!r->objects->commit_graph; |
177722b3 DS |
338 | } |
339 | ||
6cc01743 DS |
340 | int generation_numbers_enabled(struct repository *r) |
341 | { | |
342 | uint32_t first_generation; | |
343 | struct commit_graph *g; | |
344 | if (!prepare_commit_graph(r)) | |
345 | return 0; | |
346 | ||
347 | g = r->objects->commit_graph; | |
348 | ||
349 | if (!g->num_commits) | |
350 | return 0; | |
351 | ||
352 | first_generation = get_be32(g->chunk_commit_data + | |
353 | g->hash_len + 8) >> 2; | |
354 | ||
355 | return !!first_generation; | |
356 | } | |
357 | ||
829a3215 | 358 | void close_commit_graph(struct repository *r) |
177722b3 | 359 | { |
829a3215 DS |
360 | free_commit_graph(r->objects->commit_graph); |
361 | r->objects->commit_graph = NULL; | |
177722b3 DS |
362 | } |
363 | ||
364 | static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos) | |
365 | { | |
366 | return bsearch_hash(oid->hash, g->chunk_oid_fanout, | |
367 | g->chunk_oid_lookup, g->hash_len, pos); | |
368 | } | |
369 | ||
4f542b7a SB |
370 | static struct commit_list **insert_parent_or_die(struct repository *r, |
371 | struct commit_graph *g, | |
177722b3 DS |
372 | uint64_t pos, |
373 | struct commit_list **pptr) | |
374 | { | |
375 | struct commit *c; | |
376 | struct object_id oid; | |
96af91d4 | 377 | |
53614b13 DS |
378 | if (pos >= g->num_commits) |
379 | die("invalid parent position %"PRIu64, pos); | |
380 | ||
177722b3 | 381 | hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * pos); |
4f542b7a | 382 | c = lookup_commit(r, &oid); |
177722b3 | 383 | if (!c) |
4f5b532d | 384 | die(_("could not find commit %s"), oid_to_hex(&oid)); |
177722b3 DS |
385 | c->graph_pos = pos; |
386 | return &commit_list_insert(c, pptr)->next; | |
387 | } | |
388 | ||
e2838d85 DS |
389 | static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos) |
390 | { | |
391 | const unsigned char *commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * pos; | |
392 | item->graph_pos = pos; | |
393 | item->generation = get_be32(commit_data + g->hash_len + 8) >> 2; | |
394 | } | |
395 | ||
4f542b7a SB |
396 | static int fill_commit_in_graph(struct repository *r, |
397 | struct commit *item, | |
398 | struct commit_graph *g, uint32_t pos) | |
177722b3 | 399 | { |
177722b3 DS |
400 | uint32_t edge_value; |
401 | uint32_t *parent_data_ptr; | |
402 | uint64_t date_low, date_high; | |
403 | struct commit_list **pptr; | |
404 | const unsigned char *commit_data = g->chunk_commit_data + (g->hash_len + 16) * pos; | |
405 | ||
406 | item->object.parsed = 1; | |
407 | item->graph_pos = pos; | |
408 | ||
7b8a21db | 409 | item->maybe_tree = NULL; |
177722b3 DS |
410 | |
411 | date_high = get_be32(commit_data + g->hash_len + 8) & 0x3; | |
412 | date_low = get_be32(commit_data + g->hash_len + 12); | |
413 | item->date = (timestamp_t)((date_high << 32) | date_low); | |
414 | ||
83073cc9 DS |
415 | item->generation = get_be32(commit_data + g->hash_len + 8) >> 2; |
416 | ||
177722b3 DS |
417 | pptr = &item->parents; |
418 | ||
419 | edge_value = get_be32(commit_data + g->hash_len); | |
420 | if (edge_value == GRAPH_PARENT_NONE) | |
421 | return 1; | |
4f542b7a | 422 | pptr = insert_parent_or_die(r, g, edge_value, pptr); |
177722b3 DS |
423 | |
424 | edge_value = get_be32(commit_data + g->hash_len + 4); | |
425 | if (edge_value == GRAPH_PARENT_NONE) | |
426 | return 1; | |
5af7417b | 427 | if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) { |
4f542b7a | 428 | pptr = insert_parent_or_die(r, g, edge_value, pptr); |
177722b3 DS |
429 | return 1; |
430 | } | |
431 | ||
5af7417b | 432 | parent_data_ptr = (uint32_t*)(g->chunk_extra_edges + |
177722b3 DS |
433 | 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK)); |
434 | do { | |
435 | edge_value = get_be32(parent_data_ptr); | |
4f542b7a | 436 | pptr = insert_parent_or_die(r, g, |
177722b3 DS |
437 | edge_value & GRAPH_EDGE_LAST_MASK, |
438 | pptr); | |
439 | parent_data_ptr++; | |
440 | } while (!(edge_value & GRAPH_LAST_EDGE)); | |
441 | ||
442 | return 1; | |
443 | } | |
444 | ||
e2838d85 DS |
445 | static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos) |
446 | { | |
447 | if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) { | |
448 | *pos = item->graph_pos; | |
449 | return 1; | |
450 | } else { | |
451 | return bsearch_graph(g, &(item->object.oid), pos); | |
452 | } | |
453 | } | |
454 | ||
4f542b7a SB |
455 | static int parse_commit_in_graph_one(struct repository *r, |
456 | struct commit_graph *g, | |
457 | struct commit *item) | |
177722b3 | 458 | { |
e2838d85 DS |
459 | uint32_t pos; |
460 | ||
177722b3 DS |
461 | if (item->object.parsed) |
462 | return 1; | |
ee797053 DS |
463 | |
464 | if (find_commit_in_graph(item, g, &pos)) | |
4f542b7a | 465 | return fill_commit_in_graph(r, item, g, pos); |
ee797053 DS |
466 | |
467 | return 0; | |
468 | } | |
469 | ||
dade47c0 | 470 | int parse_commit_in_graph(struct repository *r, struct commit *item) |
ee797053 | 471 | { |
dade47c0 | 472 | if (!prepare_commit_graph(r)) |
ee797053 | 473 | return 0; |
4f542b7a | 474 | return parse_commit_in_graph_one(r, r->objects->commit_graph, item); |
177722b3 DS |
475 | } |
476 | ||
dade47c0 | 477 | void load_commit_graph_info(struct repository *r, struct commit *item) |
e2838d85 DS |
478 | { |
479 | uint32_t pos; | |
dade47c0 | 480 | if (!prepare_commit_graph(r)) |
e2838d85 | 481 | return; |
dade47c0 JT |
482 | if (find_commit_in_graph(item, r->objects->commit_graph, &pos)) |
483 | fill_commit_graph_info(item, r->objects->commit_graph, pos); | |
e2838d85 DS |
484 | } |
485 | ||
4f542b7a SB |
486 | static struct tree *load_tree_for_commit(struct repository *r, |
487 | struct commit_graph *g, | |
488 | struct commit *c) | |
7b8a21db DS |
489 | { |
490 | struct object_id oid; | |
491 | const unsigned char *commit_data = g->chunk_commit_data + | |
492 | GRAPH_DATA_WIDTH * (c->graph_pos); | |
493 | ||
494 | hashcpy(oid.hash, commit_data); | |
4f542b7a | 495 | c->maybe_tree = lookup_tree(r, &oid); |
7b8a21db DS |
496 | |
497 | return c->maybe_tree; | |
498 | } | |
499 | ||
4f542b7a SB |
500 | static struct tree *get_commit_tree_in_graph_one(struct repository *r, |
501 | struct commit_graph *g, | |
0cbef8f8 | 502 | const struct commit *c) |
7b8a21db DS |
503 | { |
504 | if (c->maybe_tree) | |
505 | return c->maybe_tree; | |
506 | if (c->graph_pos == COMMIT_NOT_FROM_GRAPH) | |
0cbef8f8 DS |
507 | BUG("get_commit_tree_in_graph_one called from non-commit-graph commit"); |
508 | ||
4f542b7a | 509 | return load_tree_for_commit(r, g, (struct commit *)c); |
0cbef8f8 | 510 | } |
7b8a21db | 511 | |
dade47c0 | 512 | struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c) |
0cbef8f8 | 513 | { |
4f542b7a | 514 | return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c); |
7b8a21db DS |
515 | } |
516 | ||
08fd81c9 DS |
517 | static void write_graph_chunk_fanout(struct hashfile *f, |
518 | struct commit **commits, | |
53035c4f ÆAB |
519 | int nr_commits, |
520 | struct progress *progress, | |
521 | uint64_t *progress_cnt) | |
08fd81c9 DS |
522 | { |
523 | int i, count = 0; | |
524 | struct commit **list = commits; | |
525 | ||
526 | /* | |
527 | * Write the first-level table (the list is sorted, | |
528 | * but we use a 256-entry lookup to be able to avoid | |
529 | * having to do eight extra binary search iterations). | |
530 | */ | |
531 | for (i = 0; i < 256; i++) { | |
532 | while (count < nr_commits) { | |
533 | if ((*list)->object.oid.hash[0] != i) | |
534 | break; | |
53035c4f | 535 | display_progress(progress, ++*progress_cnt); |
08fd81c9 DS |
536 | count++; |
537 | list++; | |
538 | } | |
539 | ||
540 | hashwrite_be32(f, count); | |
541 | } | |
542 | } | |
543 | ||
544 | static void write_graph_chunk_oids(struct hashfile *f, int hash_len, | |
53035c4f ÆAB |
545 | struct commit **commits, int nr_commits, |
546 | struct progress *progress, | |
547 | uint64_t *progress_cnt) | |
08fd81c9 DS |
548 | { |
549 | struct commit **list = commits; | |
550 | int count; | |
53035c4f ÆAB |
551 | for (count = 0; count < nr_commits; count++, list++) { |
552 | display_progress(progress, ++*progress_cnt); | |
08fd81c9 | 553 | hashwrite(f, (*list)->object.oid.hash, (int)hash_len); |
53035c4f | 554 | } |
08fd81c9 DS |
555 | } |
556 | ||
557 | static const unsigned char *commit_to_sha1(size_t index, void *table) | |
558 | { | |
559 | struct commit **commits = table; | |
560 | return commits[index]->object.oid.hash; | |
561 | } | |
562 | ||
563 | static void write_graph_chunk_data(struct hashfile *f, int hash_len, | |
53035c4f ÆAB |
564 | struct commit **commits, int nr_commits, |
565 | struct progress *progress, | |
566 | uint64_t *progress_cnt) | |
08fd81c9 DS |
567 | { |
568 | struct commit **list = commits; | |
569 | struct commit **last = commits + nr_commits; | |
570 | uint32_t num_extra_edges = 0; | |
571 | ||
572 | while (list < last) { | |
573 | struct commit_list *parent; | |
574 | int edge_value; | |
575 | uint32_t packedDate[2]; | |
53035c4f | 576 | display_progress(progress, ++*progress_cnt); |
08fd81c9 DS |
577 | |
578 | parse_commit(*list); | |
2e27bd77 | 579 | hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len); |
08fd81c9 DS |
580 | |
581 | parent = (*list)->parents; | |
582 | ||
583 | if (!parent) | |
584 | edge_value = GRAPH_PARENT_NONE; | |
585 | else { | |
586 | edge_value = sha1_pos(parent->item->object.oid.hash, | |
587 | commits, | |
588 | nr_commits, | |
589 | commit_to_sha1); | |
590 | ||
591 | if (edge_value < 0) | |
cce99cd8 DS |
592 | BUG("missing parent %s for commit %s", |
593 | oid_to_hex(&parent->item->object.oid), | |
594 | oid_to_hex(&(*list)->object.oid)); | |
08fd81c9 DS |
595 | } |
596 | ||
597 | hashwrite_be32(f, edge_value); | |
598 | ||
599 | if (parent) | |
600 | parent = parent->next; | |
601 | ||
602 | if (!parent) | |
603 | edge_value = GRAPH_PARENT_NONE; | |
604 | else if (parent->next) | |
5af7417b | 605 | edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges; |
08fd81c9 DS |
606 | else { |
607 | edge_value = sha1_pos(parent->item->object.oid.hash, | |
608 | commits, | |
609 | nr_commits, | |
610 | commit_to_sha1); | |
611 | if (edge_value < 0) | |
cce99cd8 DS |
612 | BUG("missing parent %s for commit %s", |
613 | oid_to_hex(&parent->item->object.oid), | |
614 | oid_to_hex(&(*list)->object.oid)); | |
08fd81c9 DS |
615 | } |
616 | ||
617 | hashwrite_be32(f, edge_value); | |
618 | ||
5af7417b | 619 | if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) { |
08fd81c9 DS |
620 | do { |
621 | num_extra_edges++; | |
622 | parent = parent->next; | |
623 | } while (parent); | |
624 | } | |
625 | ||
626 | if (sizeof((*list)->date) > 4) | |
627 | packedDate[0] = htonl(((*list)->date >> 32) & 0x3); | |
628 | else | |
629 | packedDate[0] = 0; | |
630 | ||
3258c663 DS |
631 | packedDate[0] |= htonl((*list)->generation << 2); |
632 | ||
08fd81c9 DS |
633 | packedDate[1] = htonl((*list)->date); |
634 | hashwrite(f, packedDate, 8); | |
635 | ||
636 | list++; | |
637 | } | |
638 | } | |
639 | ||
5af7417b | 640 | static void write_graph_chunk_extra_edges(struct hashfile *f, |
08fd81c9 | 641 | struct commit **commits, |
53035c4f ÆAB |
642 | int nr_commits, |
643 | struct progress *progress, | |
644 | uint64_t *progress_cnt) | |
08fd81c9 DS |
645 | { |
646 | struct commit **list = commits; | |
647 | struct commit **last = commits + nr_commits; | |
648 | struct commit_list *parent; | |
649 | ||
650 | while (list < last) { | |
651 | int num_parents = 0; | |
53035c4f ÆAB |
652 | |
653 | display_progress(progress, ++*progress_cnt); | |
654 | ||
08fd81c9 DS |
655 | for (parent = (*list)->parents; num_parents < 3 && parent; |
656 | parent = parent->next) | |
657 | num_parents++; | |
658 | ||
659 | if (num_parents <= 2) { | |
660 | list++; | |
661 | continue; | |
662 | } | |
663 | ||
664 | /* Since num_parents > 2, this initializer is safe. */ | |
665 | for (parent = (*list)->parents->next; parent; parent = parent->next) { | |
666 | int edge_value = sha1_pos(parent->item->object.oid.hash, | |
667 | commits, | |
668 | nr_commits, | |
669 | commit_to_sha1); | |
670 | ||
671 | if (edge_value < 0) | |
cce99cd8 DS |
672 | BUG("missing parent %s for commit %s", |
673 | oid_to_hex(&parent->item->object.oid), | |
674 | oid_to_hex(&(*list)->object.oid)); | |
08fd81c9 DS |
675 | else if (!parent->next) |
676 | edge_value |= GRAPH_LAST_EDGE; | |
677 | ||
678 | hashwrite_be32(f, edge_value); | |
679 | } | |
680 | ||
681 | list++; | |
682 | } | |
683 | } | |
684 | ||
685 | static int commit_compare(const void *_a, const void *_b) | |
686 | { | |
687 | const struct object_id *a = (const struct object_id *)_a; | |
688 | const struct object_id *b = (const struct object_id *)_b; | |
689 | return oidcmp(a, b); | |
690 | } | |
691 | ||
692 | struct packed_commit_list { | |
693 | struct commit **list; | |
694 | int nr; | |
695 | int alloc; | |
696 | }; | |
697 | ||
698 | struct packed_oid_list { | |
699 | struct object_id *list; | |
700 | int nr; | |
701 | int alloc; | |
7b0f2292 ÆAB |
702 | struct progress *progress; |
703 | int progress_done; | |
08fd81c9 DS |
704 | }; |
705 | ||
706 | static int add_packed_commits(const struct object_id *oid, | |
707 | struct packed_git *pack, | |
708 | uint32_t pos, | |
709 | void *data) | |
710 | { | |
711 | struct packed_oid_list *list = (struct packed_oid_list*)data; | |
712 | enum object_type type; | |
713 | off_t offset = nth_packed_object_offset(pack, pos); | |
714 | struct object_info oi = OBJECT_INFO_INIT; | |
715 | ||
7b0f2292 ÆAB |
716 | if (list->progress) |
717 | display_progress(list->progress, ++list->progress_done); | |
718 | ||
08fd81c9 | 719 | oi.typep = &type; |
fcb6df32 | 720 | if (packed_object_info(the_repository, pack, offset, &oi) < 0) |
4f5b532d | 721 | die(_("unable to get type of object %s"), oid_to_hex(oid)); |
08fd81c9 DS |
722 | |
723 | if (type != OBJ_COMMIT) | |
724 | return 0; | |
725 | ||
726 | ALLOC_GROW(list->list, list->nr + 1, list->alloc); | |
727 | oidcpy(&(list->list[list->nr]), oid); | |
728 | list->nr++; | |
729 | ||
730 | return 0; | |
731 | } | |
732 | ||
4f2542b4 DS |
733 | static void add_missing_parents(struct packed_oid_list *oids, struct commit *commit) |
734 | { | |
735 | struct commit_list *parent; | |
736 | for (parent = commit->parents; parent; parent = parent->next) { | |
737 | if (!(parent->item->object.flags & UNINTERESTING)) { | |
738 | ALLOC_GROW(oids->list, oids->nr + 1, oids->alloc); | |
739 | oidcpy(&oids->list[oids->nr], &(parent->item->object.oid)); | |
740 | oids->nr++; | |
741 | parent->item->object.flags |= UNINTERESTING; | |
742 | } | |
743 | } | |
744 | } | |
745 | ||
7b0f2292 | 746 | static void close_reachable(struct packed_oid_list *oids, int report_progress) |
4f2542b4 | 747 | { |
49bbc57a | 748 | int i; |
4f2542b4 | 749 | struct commit *commit; |
7b0f2292 | 750 | struct progress *progress = NULL; |
4f2542b4 | 751 | |
7b0f2292 ÆAB |
752 | if (report_progress) |
753 | progress = start_delayed_progress( | |
49bbc57a | 754 | _("Loading known commits in commit graph"), oids->nr); |
4f2542b4 | 755 | for (i = 0; i < oids->nr; i++) { |
49bbc57a | 756 | display_progress(progress, i + 1); |
c1f5eb49 | 757 | commit = lookup_commit(the_repository, &oids->list[i]); |
4f2542b4 DS |
758 | if (commit) |
759 | commit->object.flags |= UNINTERESTING; | |
760 | } | |
01ca3877 | 761 | stop_progress(&progress); |
4f2542b4 DS |
762 | |
763 | /* | |
764 | * As this loop runs, oids->nr may grow, but not more | |
765 | * than the number of missing commits in the reachable | |
766 | * closure. | |
767 | */ | |
01ca3877 ÆAB |
768 | if (report_progress) |
769 | progress = start_delayed_progress( | |
49bbc57a | 770 | _("Expanding reachable commits in commit graph"), oids->nr); |
4f2542b4 | 771 | for (i = 0; i < oids->nr; i++) { |
49bbc57a | 772 | display_progress(progress, i + 1); |
c1f5eb49 | 773 | commit = lookup_commit(the_repository, &oids->list[i]); |
4f2542b4 DS |
774 | |
775 | if (commit && !parse_commit(commit)) | |
776 | add_missing_parents(oids, commit); | |
777 | } | |
01ca3877 | 778 | stop_progress(&progress); |
4f2542b4 | 779 | |
01ca3877 ÆAB |
780 | if (report_progress) |
781 | progress = start_delayed_progress( | |
49bbc57a | 782 | _("Clearing commit marks in commit graph"), oids->nr); |
4f2542b4 | 783 | for (i = 0; i < oids->nr; i++) { |
49bbc57a | 784 | display_progress(progress, i + 1); |
c1f5eb49 | 785 | commit = lookup_commit(the_repository, &oids->list[i]); |
4f2542b4 DS |
786 | |
787 | if (commit) | |
788 | commit->object.flags &= ~UNINTERESTING; | |
789 | } | |
7b0f2292 | 790 | stop_progress(&progress); |
4f2542b4 DS |
791 | } |
792 | ||
7b0f2292 ÆAB |
793 | static void compute_generation_numbers(struct packed_commit_list* commits, |
794 | int report_progress) | |
3258c663 DS |
795 | { |
796 | int i; | |
797 | struct commit_list *list = NULL; | |
7b0f2292 | 798 | struct progress *progress = NULL; |
3258c663 | 799 | |
7b0f2292 ÆAB |
800 | if (report_progress) |
801 | progress = start_progress( | |
802 | _("Computing commit graph generation numbers"), | |
803 | commits->nr); | |
3258c663 | 804 | for (i = 0; i < commits->nr; i++) { |
7b0f2292 | 805 | display_progress(progress, i + 1); |
3258c663 DS |
806 | if (commits->list[i]->generation != GENERATION_NUMBER_INFINITY && |
807 | commits->list[i]->generation != GENERATION_NUMBER_ZERO) | |
808 | continue; | |
809 | ||
810 | commit_list_insert(commits->list[i], &list); | |
811 | while (list) { | |
812 | struct commit *current = list->item; | |
813 | struct commit_list *parent; | |
814 | int all_parents_computed = 1; | |
815 | uint32_t max_generation = 0; | |
816 | ||
817 | for (parent = current->parents; parent; parent = parent->next) { | |
818 | if (parent->item->generation == GENERATION_NUMBER_INFINITY || | |
819 | parent->item->generation == GENERATION_NUMBER_ZERO) { | |
820 | all_parents_computed = 0; | |
821 | commit_list_insert(parent->item, &list); | |
822 | break; | |
823 | } else if (parent->item->generation > max_generation) { | |
824 | max_generation = parent->item->generation; | |
825 | } | |
826 | } | |
827 | ||
828 | if (all_parents_computed) { | |
829 | current->generation = max_generation + 1; | |
830 | pop_commit(&list); | |
831 | ||
832 | if (current->generation > GENERATION_NUMBER_MAX) | |
833 | current->generation = GENERATION_NUMBER_MAX; | |
834 | } | |
835 | } | |
836 | } | |
7b0f2292 | 837 | stop_progress(&progress); |
3258c663 DS |
838 | } |
839 | ||
59fb8770 DS |
840 | static int add_ref_to_list(const char *refname, |
841 | const struct object_id *oid, | |
842 | int flags, void *cb_data) | |
843 | { | |
844 | struct string_list *list = (struct string_list *)cb_data; | |
845 | ||
846 | string_list_append(list, oid_to_hex(oid)); | |
847 | return 0; | |
848 | } | |
849 | ||
7b0f2292 ÆAB |
850 | void write_commit_graph_reachable(const char *obj_dir, int append, |
851 | int report_progress) | |
59fb8770 | 852 | { |
f4dbdfc4 | 853 | struct string_list list = STRING_LIST_INIT_DUP; |
59fb8770 | 854 | |
59fb8770 | 855 | for_each_ref(add_ref_to_list, &list); |
7b0f2292 | 856 | write_commit_graph(obj_dir, NULL, &list, append, report_progress); |
f4dbdfc4 DS |
857 | |
858 | string_list_clear(&list, 0); | |
59fb8770 DS |
859 | } |
860 | ||
049d51a2 | 861 | void write_commit_graph(const char *obj_dir, |
d88b14b3 DS |
862 | struct string_list *pack_indexes, |
863 | struct string_list *commit_hex, | |
7b0f2292 | 864 | int append, int report_progress) |
08fd81c9 DS |
865 | { |
866 | struct packed_oid_list oids; | |
867 | struct packed_commit_list commits; | |
868 | struct hashfile *f; | |
869 | uint32_t i, count_distinct = 0; | |
870 | char *graph_name; | |
08fd81c9 DS |
871 | struct lock_file lk = LOCK_INIT; |
872 | uint32_t chunk_ids[5]; | |
873 | uint64_t chunk_offsets[5]; | |
874 | int num_chunks; | |
875 | int num_extra_edges; | |
876 | struct commit_list *parent; | |
7b0f2292 | 877 | struct progress *progress = NULL; |
c1665998 | 878 | const unsigned hashsz = the_hash_algo->rawsz; |
53035c4f | 879 | uint64_t progress_cnt = 0; |
28944739 | 880 | struct strbuf progress_title = STRBUF_INIT; |
d9b1b309 | 881 | unsigned long approx_nr_objects; |
08fd81c9 | 882 | |
d6538246 DS |
883 | if (!commit_graph_compatible(the_repository)) |
884 | return; | |
885 | ||
08fd81c9 | 886 | oids.nr = 0; |
d9b1b309 ÆAB |
887 | approx_nr_objects = approximate_object_count(); |
888 | oids.alloc = approx_nr_objects / 32; | |
7b0f2292 ÆAB |
889 | oids.progress = NULL; |
890 | oids.progress_done = 0; | |
08fd81c9 | 891 | |
7547b95b | 892 | if (append) { |
dade47c0 | 893 | prepare_commit_graph_one(the_repository, obj_dir); |
85277506 JT |
894 | if (the_repository->objects->commit_graph) |
895 | oids.alloc += the_repository->objects->commit_graph->num_commits; | |
7547b95b DS |
896 | } |
897 | ||
08fd81c9 DS |
898 | if (oids.alloc < 1024) |
899 | oids.alloc = 1024; | |
900 | ALLOC_ARRAY(oids.list, oids.alloc); | |
901 | ||
85277506 JT |
902 | if (append && the_repository->objects->commit_graph) { |
903 | struct commit_graph *commit_graph = | |
904 | the_repository->objects->commit_graph; | |
7547b95b DS |
905 | for (i = 0; i < commit_graph->num_commits; i++) { |
906 | const unsigned char *hash = commit_graph->chunk_oid_lookup + | |
907 | commit_graph->hash_len * i; | |
908 | hashcpy(oids.list[oids.nr++].hash, hash); | |
909 | } | |
910 | } | |
911 | ||
049d51a2 DS |
912 | if (pack_indexes) { |
913 | struct strbuf packname = STRBUF_INIT; | |
914 | int dirlen; | |
915 | strbuf_addf(&packname, "%s/pack/", obj_dir); | |
916 | dirlen = packname.len; | |
7b0f2292 | 917 | if (report_progress) { |
7c7b8a7f ÆAB |
918 | strbuf_addf(&progress_title, |
919 | Q_("Finding commits for commit graph in %d pack", | |
920 | "Finding commits for commit graph in %d packs", | |
921 | pack_indexes->nr), | |
922 | pack_indexes->nr); | |
923 | oids.progress = start_delayed_progress(progress_title.buf, 0); | |
7b0f2292 ÆAB |
924 | oids.progress_done = 0; |
925 | } | |
d88b14b3 | 926 | for (i = 0; i < pack_indexes->nr; i++) { |
049d51a2 DS |
927 | struct packed_git *p; |
928 | strbuf_setlen(&packname, dirlen); | |
d88b14b3 | 929 | strbuf_addstr(&packname, pack_indexes->items[i].string); |
049d51a2 DS |
930 | p = add_packed_git(packname.buf, packname.len, 1); |
931 | if (!p) | |
4f5b532d | 932 | die(_("error adding pack %s"), packname.buf); |
049d51a2 | 933 | if (open_pack_index(p)) |
4f5b532d | 934 | die(_("error opening index for %s"), packname.buf); |
d7574c95 ÆAB |
935 | for_each_object_in_pack(p, add_packed_commits, &oids, |
936 | FOR_EACH_OBJECT_PACK_ORDER); | |
049d51a2 | 937 | close_pack(p); |
f4dbdfc4 | 938 | free(p); |
049d51a2 | 939 | } |
7b0f2292 | 940 | stop_progress(&oids.progress); |
7c7b8a7f | 941 | strbuf_reset(&progress_title); |
049d51a2 | 942 | strbuf_release(&packname); |
3d5df01b DS |
943 | } |
944 | ||
945 | if (commit_hex) { | |
7c7b8a7f ÆAB |
946 | if (report_progress) { |
947 | strbuf_addf(&progress_title, | |
948 | Q_("Finding commits for commit graph from %d ref", | |
949 | "Finding commits for commit graph from %d refs", | |
950 | commit_hex->nr), | |
951 | commit_hex->nr); | |
952 | progress = start_delayed_progress(progress_title.buf, | |
953 | commit_hex->nr); | |
954 | } | |
d88b14b3 | 955 | for (i = 0; i < commit_hex->nr; i++) { |
3d5df01b DS |
956 | const char *end; |
957 | struct object_id oid; | |
958 | struct commit *result; | |
959 | ||
7b0f2292 | 960 | display_progress(progress, i + 1); |
d88b14b3 DS |
961 | if (commit_hex->items[i].string && |
962 | parse_oid_hex(commit_hex->items[i].string, &oid, &end)) | |
3d5df01b DS |
963 | continue; |
964 | ||
21e1ee8f | 965 | result = lookup_commit_reference_gently(the_repository, &oid, 1); |
3d5df01b DS |
966 | |
967 | if (result) { | |
968 | ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc); | |
969 | oidcpy(&oids.list[oids.nr], &(result->object.oid)); | |
970 | oids.nr++; | |
971 | } | |
972 | } | |
7b0f2292 | 973 | stop_progress(&progress); |
7c7b8a7f | 974 | strbuf_reset(&progress_title); |
3d5df01b DS |
975 | } |
976 | ||
7b0f2292 ÆAB |
977 | if (!pack_indexes && !commit_hex) { |
978 | if (report_progress) | |
979 | oids.progress = start_delayed_progress( | |
7c7b8a7f | 980 | _("Finding commits for commit graph among packed objects"), |
d9b1b309 | 981 | approx_nr_objects); |
d7574c95 ÆAB |
982 | for_each_packed_object(add_packed_commits, &oids, |
983 | FOR_EACH_OBJECT_PACK_ORDER); | |
d9b1b309 ÆAB |
984 | if (oids.progress_done < approx_nr_objects) |
985 | display_progress(oids.progress, approx_nr_objects); | |
7b0f2292 ÆAB |
986 | stop_progress(&oids.progress); |
987 | } | |
049d51a2 | 988 | |
7b0f2292 | 989 | close_reachable(&oids, report_progress); |
08fd81c9 | 990 | |
890226cc ÆAB |
991 | if (report_progress) |
992 | progress = start_delayed_progress( | |
993 | _("Counting distinct commits in commit graph"), | |
994 | oids.nr); | |
995 | display_progress(progress, 0); /* TODO: Measure QSORT() progress */ | |
08fd81c9 | 996 | QSORT(oids.list, oids.nr, commit_compare); |
08fd81c9 DS |
997 | count_distinct = 1; |
998 | for (i = 1; i < oids.nr; i++) { | |
890226cc | 999 | display_progress(progress, i + 1); |
9001dc2a | 1000 | if (!oideq(&oids.list[i - 1], &oids.list[i])) |
08fd81c9 DS |
1001 | count_distinct++; |
1002 | } | |
890226cc | 1003 | stop_progress(&progress); |
08fd81c9 | 1004 | |
cce99cd8 | 1005 | if (count_distinct >= GRAPH_EDGE_LAST_MASK) |
08fd81c9 DS |
1006 | die(_("the commit graph format cannot write %d commits"), count_distinct); |
1007 | ||
1008 | commits.nr = 0; | |
1009 | commits.alloc = count_distinct; | |
1010 | ALLOC_ARRAY(commits.list, commits.alloc); | |
1011 | ||
1012 | num_extra_edges = 0; | |
890226cc ÆAB |
1013 | if (report_progress) |
1014 | progress = start_delayed_progress( | |
1015 | _("Finding extra edges in commit graph"), | |
1016 | oids.nr); | |
08fd81c9 DS |
1017 | for (i = 0; i < oids.nr; i++) { |
1018 | int num_parents = 0; | |
890226cc | 1019 | display_progress(progress, i + 1); |
4a7e27e9 | 1020 | if (i > 0 && oideq(&oids.list[i - 1], &oids.list[i])) |
08fd81c9 DS |
1021 | continue; |
1022 | ||
c1f5eb49 | 1023 | commits.list[commits.nr] = lookup_commit(the_repository, &oids.list[i]); |
08fd81c9 DS |
1024 | parse_commit(commits.list[commits.nr]); |
1025 | ||
1026 | for (parent = commits.list[commits.nr]->parents; | |
1027 | parent; parent = parent->next) | |
1028 | num_parents++; | |
1029 | ||
1030 | if (num_parents > 2) | |
1031 | num_extra_edges += num_parents - 1; | |
1032 | ||
1033 | commits.nr++; | |
1034 | } | |
1035 | num_chunks = num_extra_edges ? 4 : 3; | |
890226cc | 1036 | stop_progress(&progress); |
08fd81c9 | 1037 | |
cce99cd8 | 1038 | if (commits.nr >= GRAPH_EDGE_LAST_MASK) |
08fd81c9 DS |
1039 | die(_("too many commits to write graph")); |
1040 | ||
7b0f2292 | 1041 | compute_generation_numbers(&commits, report_progress); |
08fd81c9 | 1042 | |
08fd81c9 | 1043 | graph_name = get_commit_graph_filename(obj_dir); |
f4dbdfc4 DS |
1044 | if (safe_create_leading_directories(graph_name)) { |
1045 | UNLEAK(graph_name); | |
33286dcd DS |
1046 | die_errno(_("unable to create leading directories of %s"), |
1047 | graph_name); | |
f4dbdfc4 | 1048 | } |
08fd81c9 | 1049 | |
33286dcd | 1050 | hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR); |
08fd81c9 DS |
1051 | f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf); |
1052 | ||
1053 | hashwrite_be32(f, GRAPH_SIGNATURE); | |
1054 | ||
1055 | hashwrite_u8(f, GRAPH_VERSION); | |
c1665998 | 1056 | hashwrite_u8(f, oid_version()); |
08fd81c9 DS |
1057 | hashwrite_u8(f, num_chunks); |
1058 | hashwrite_u8(f, 0); /* unused padding byte */ | |
1059 | ||
1060 | chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT; | |
1061 | chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP; | |
1062 | chunk_ids[2] = GRAPH_CHUNKID_DATA; | |
1063 | if (num_extra_edges) | |
5af7417b | 1064 | chunk_ids[3] = GRAPH_CHUNKID_EXTRAEDGES; |
08fd81c9 DS |
1065 | else |
1066 | chunk_ids[3] = 0; | |
1067 | chunk_ids[4] = 0; | |
1068 | ||
1069 | chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH; | |
1070 | chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE; | |
c1665998 | 1071 | chunk_offsets[2] = chunk_offsets[1] + hashsz * commits.nr; |
1072 | chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * commits.nr; | |
08fd81c9 DS |
1073 | chunk_offsets[4] = chunk_offsets[3] + 4 * num_extra_edges; |
1074 | ||
1075 | for (i = 0; i <= num_chunks; i++) { | |
1076 | uint32_t chunk_write[3]; | |
1077 | ||
1078 | chunk_write[0] = htonl(chunk_ids[i]); | |
1079 | chunk_write[1] = htonl(chunk_offsets[i] >> 32); | |
1080 | chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff); | |
1081 | hashwrite(f, chunk_write, 12); | |
1082 | } | |
1083 | ||
28944739 ÆAB |
1084 | if (report_progress) { |
1085 | strbuf_addf(&progress_title, | |
1086 | Q_("Writing out commit graph in %d pass", | |
1087 | "Writing out commit graph in %d passes", | |
1088 | num_chunks), | |
1089 | num_chunks); | |
53035c4f | 1090 | progress = start_delayed_progress( |
28944739 | 1091 | progress_title.buf, |
53035c4f | 1092 | num_chunks * commits.nr); |
28944739 | 1093 | } |
53035c4f | 1094 | write_graph_chunk_fanout(f, commits.list, commits.nr, progress, &progress_cnt); |
e5eac573 JH |
1095 | write_graph_chunk_oids(f, hashsz, commits.list, commits.nr, progress, &progress_cnt); |
1096 | write_graph_chunk_data(f, hashsz, commits.list, commits.nr, progress, &progress_cnt); | |
857ba928 | 1097 | if (num_extra_edges) |
53035c4f ÆAB |
1098 | write_graph_chunk_extra_edges(f, commits.list, commits.nr, progress, &progress_cnt); |
1099 | stop_progress(&progress); | |
28944739 | 1100 | strbuf_release(&progress_title); |
08fd81c9 | 1101 | |
829a3215 | 1102 | close_commit_graph(the_repository); |
08fd81c9 DS |
1103 | finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC); |
1104 | commit_lock_file(&lk); | |
1105 | ||
f4dbdfc4 DS |
1106 | free(graph_name); |
1107 | free(commits.list); | |
08fd81c9 | 1108 | free(oids.list); |
08fd81c9 | 1109 | } |
283e68c7 | 1110 | |
41df0e30 | 1111 | #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2 |
283e68c7 DS |
1112 | static int verify_commit_graph_error; |
1113 | ||
1114 | static void graph_report(const char *fmt, ...) | |
1115 | { | |
1116 | va_list ap; | |
1117 | ||
1118 | verify_commit_graph_error = 1; | |
1119 | va_start(ap, fmt); | |
1120 | vfprintf(stderr, fmt, ap); | |
1121 | fprintf(stderr, "\n"); | |
1122 | va_end(ap); | |
1123 | } | |
1124 | ||
1373e547 DS |
1125 | #define GENERATION_ZERO_EXISTS 1 |
1126 | #define GENERATION_NUMBER_EXISTS 2 | |
1127 | ||
283e68c7 DS |
1128 | int verify_commit_graph(struct repository *r, struct commit_graph *g) |
1129 | { | |
9bda8467 | 1130 | uint32_t i, cur_fanout_pos = 0; |
41df0e30 | 1131 | struct object_id prev_oid, cur_oid, checksum; |
1373e547 | 1132 | int generation_zero = 0; |
41df0e30 DS |
1133 | struct hashfile *f; |
1134 | int devnull; | |
1f7f557f | 1135 | struct progress *progress = NULL; |
9bda8467 | 1136 | |
283e68c7 DS |
1137 | if (!g) { |
1138 | graph_report("no commit-graph file loaded"); | |
1139 | return 1; | |
1140 | } | |
1141 | ||
2ac138d5 | 1142 | verify_commit_graph_error = verify_commit_graph_lite(g); |
9bda8467 DS |
1143 | if (verify_commit_graph_error) |
1144 | return verify_commit_graph_error; | |
1145 | ||
41df0e30 DS |
1146 | devnull = open("/dev/null", O_WRONLY); |
1147 | f = hashfd(devnull, NULL); | |
1148 | hashwrite(f, g->data, g->data_len - g->hash_len); | |
1149 | finalize_hashfile(f, checksum.hash, CSUM_CLOSE); | |
67947c34 | 1150 | if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) { |
41df0e30 DS |
1151 | graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt")); |
1152 | verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH; | |
1153 | } | |
1154 | ||
9bda8467 | 1155 | for (i = 0; i < g->num_commits; i++) { |
2e3c0737 DS |
1156 | struct commit *graph_commit; |
1157 | ||
9bda8467 DS |
1158 | hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i); |
1159 | ||
1160 | if (i && oidcmp(&prev_oid, &cur_oid) >= 0) | |
1161 | graph_report("commit-graph has incorrect OID order: %s then %s", | |
1162 | oid_to_hex(&prev_oid), | |
1163 | oid_to_hex(&cur_oid)); | |
1164 | ||
1165 | oidcpy(&prev_oid, &cur_oid); | |
1166 | ||
1167 | while (cur_oid.hash[0] > cur_fanout_pos) { | |
1168 | uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos); | |
1169 | ||
1170 | if (i != fanout_value) | |
1171 | graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u", | |
1172 | cur_fanout_pos, fanout_value, i); | |
1173 | cur_fanout_pos++; | |
1174 | } | |
2e3c0737 | 1175 | |
82952964 | 1176 | graph_commit = lookup_commit(r, &cur_oid); |
4f542b7a | 1177 | if (!parse_commit_in_graph_one(r, g, graph_commit)) |
2e3c0737 DS |
1178 | graph_report("failed to parse %s from commit-graph", |
1179 | oid_to_hex(&cur_oid)); | |
9bda8467 DS |
1180 | } |
1181 | ||
1182 | while (cur_fanout_pos < 256) { | |
1183 | uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos); | |
1184 | ||
1185 | if (g->num_commits != fanout_value) | |
1186 | graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u", | |
1187 | cur_fanout_pos, fanout_value, i); | |
1188 | ||
1189 | cur_fanout_pos++; | |
1190 | } | |
1191 | ||
41df0e30 | 1192 | if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH) |
96af91d4 DS |
1193 | return verify_commit_graph_error; |
1194 | ||
1f7f557f ÆAB |
1195 | progress = start_progress(_("Verifying commits in commit graph"), |
1196 | g->num_commits); | |
96af91d4 | 1197 | for (i = 0; i < g->num_commits; i++) { |
2e3c0737 | 1198 | struct commit *graph_commit, *odb_commit; |
53614b13 | 1199 | struct commit_list *graph_parents, *odb_parents; |
1373e547 | 1200 | uint32_t max_generation = 0; |
96af91d4 | 1201 | |
1f7f557f | 1202 | display_progress(progress, i + 1); |
96af91d4 DS |
1203 | hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i); |
1204 | ||
82952964 | 1205 | graph_commit = lookup_commit(r, &cur_oid); |
96af91d4 DS |
1206 | odb_commit = (struct commit *)create_object(r, cur_oid.hash, alloc_commit_node(r)); |
1207 | if (parse_commit_internal(odb_commit, 0, 0)) { | |
1208 | graph_report("failed to parse %s from object database", | |
1209 | oid_to_hex(&cur_oid)); | |
1210 | continue; | |
1211 | } | |
2e3c0737 | 1212 | |
4f542b7a | 1213 | if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid, |
2e3c0737 DS |
1214 | get_commit_tree_oid(odb_commit))) |
1215 | graph_report("root tree OID for commit %s in commit-graph is %s != %s", | |
1216 | oid_to_hex(&cur_oid), | |
1217 | oid_to_hex(get_commit_tree_oid(graph_commit)), | |
1218 | oid_to_hex(get_commit_tree_oid(odb_commit))); | |
53614b13 DS |
1219 | |
1220 | graph_parents = graph_commit->parents; | |
1221 | odb_parents = odb_commit->parents; | |
1222 | ||
1223 | while (graph_parents) { | |
1224 | if (odb_parents == NULL) { | |
1225 | graph_report("commit-graph parent list for commit %s is too long", | |
1226 | oid_to_hex(&cur_oid)); | |
1227 | break; | |
1228 | } | |
1229 | ||
9001dc2a | 1230 | if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid)) |
53614b13 DS |
1231 | graph_report("commit-graph parent for %s is %s != %s", |
1232 | oid_to_hex(&cur_oid), | |
1233 | oid_to_hex(&graph_parents->item->object.oid), | |
1234 | oid_to_hex(&odb_parents->item->object.oid)); | |
1235 | ||
1373e547 DS |
1236 | if (graph_parents->item->generation > max_generation) |
1237 | max_generation = graph_parents->item->generation; | |
1238 | ||
53614b13 DS |
1239 | graph_parents = graph_parents->next; |
1240 | odb_parents = odb_parents->next; | |
1241 | } | |
1242 | ||
1243 | if (odb_parents != NULL) | |
1244 | graph_report("commit-graph parent list for commit %s terminates early", | |
1245 | oid_to_hex(&cur_oid)); | |
1373e547 DS |
1246 | |
1247 | if (!graph_commit->generation) { | |
1248 | if (generation_zero == GENERATION_NUMBER_EXISTS) | |
1249 | graph_report("commit-graph has generation number zero for commit %s, but non-zero elsewhere", | |
1250 | oid_to_hex(&cur_oid)); | |
1251 | generation_zero = GENERATION_ZERO_EXISTS; | |
1252 | } else if (generation_zero == GENERATION_ZERO_EXISTS) | |
1253 | graph_report("commit-graph has non-zero generation number for commit %s, but zero elsewhere", | |
1254 | oid_to_hex(&cur_oid)); | |
1255 | ||
1256 | if (generation_zero == GENERATION_ZERO_EXISTS) | |
1257 | continue; | |
1258 | ||
1259 | /* | |
1260 | * If one of our parents has generation GENERATION_NUMBER_MAX, then | |
1261 | * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid | |
1262 | * extra logic in the following condition. | |
1263 | */ | |
1264 | if (max_generation == GENERATION_NUMBER_MAX) | |
1265 | max_generation--; | |
1266 | ||
1267 | if (graph_commit->generation != max_generation + 1) | |
1268 | graph_report("commit-graph generation for commit %s is %u != %u", | |
1269 | oid_to_hex(&cur_oid), | |
1270 | graph_commit->generation, | |
1271 | max_generation + 1); | |
88968ebf DS |
1272 | |
1273 | if (graph_commit->date != odb_commit->date) | |
1274 | graph_report("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime, | |
1275 | oid_to_hex(&cur_oid), | |
1276 | graph_commit->date, | |
1277 | odb_commit->date); | |
96af91d4 | 1278 | } |
1f7f557f | 1279 | stop_progress(&progress); |
96af91d4 | 1280 | |
283e68c7 DS |
1281 | return verify_commit_graph_error; |
1282 | } | |
c3756d5b JT |
1283 | |
1284 | void free_commit_graph(struct commit_graph *g) | |
1285 | { | |
1286 | if (!g) | |
1287 | return; | |
1288 | if (g->graph_fd >= 0) { | |
1289 | munmap((void *)g->data, g->data_len); | |
1290 | g->data = NULL; | |
1291 | close(g->graph_fd); | |
1292 | } | |
1293 | free(g); | |
1294 | } |