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