]>
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" | |
10 | #include "revision.h" | |
11 | #include "sha1-lookup.h" | |
12 | #include "commit-graph.h" | |
b10edb2d | 13 | #include "object-store.h" |
08fd81c9 DS |
14 | |
15 | #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */ | |
16 | #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */ | |
17 | #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */ | |
18 | #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */ | |
19 | #define GRAPH_CHUNKID_LARGEEDGES 0x45444745 /* "EDGE" */ | |
20 | ||
21 | #define GRAPH_DATA_WIDTH 36 | |
22 | ||
23 | #define GRAPH_VERSION_1 0x1 | |
24 | #define GRAPH_VERSION GRAPH_VERSION_1 | |
25 | ||
26 | #define GRAPH_OID_VERSION_SHA1 1 | |
27 | #define GRAPH_OID_LEN_SHA1 GIT_SHA1_RAWSZ | |
28 | #define GRAPH_OID_VERSION GRAPH_OID_VERSION_SHA1 | |
29 | #define GRAPH_OID_LEN GRAPH_OID_LEN_SHA1 | |
30 | ||
31 | #define GRAPH_OCTOPUS_EDGES_NEEDED 0x80000000 | |
32 | #define GRAPH_PARENT_MISSING 0x7fffffff | |
33 | #define GRAPH_EDGE_LAST_MASK 0x7fffffff | |
34 | #define GRAPH_PARENT_NONE 0x70000000 | |
35 | ||
36 | #define GRAPH_LAST_EDGE 0x80000000 | |
37 | ||
38 | #define GRAPH_FANOUT_SIZE (4 * 256) | |
39 | #define GRAPH_CHUNKLOOKUP_WIDTH 12 | |
40 | #define GRAPH_MIN_SIZE (5 * GRAPH_CHUNKLOOKUP_WIDTH + GRAPH_FANOUT_SIZE + \ | |
41 | GRAPH_OID_LEN + 8) | |
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 | ||
2a2e32bd DS |
48 | static struct commit_graph *alloc_commit_graph(void) |
49 | { | |
50 | struct commit_graph *g = xcalloc(1, sizeof(*g)); | |
51 | g->graph_fd = -1; | |
52 | ||
53 | return g; | |
54 | } | |
55 | ||
56 | struct commit_graph *load_commit_graph_one(const char *graph_file) | |
57 | { | |
58 | void *graph_map; | |
59 | const unsigned char *data, *chunk_lookup; | |
60 | size_t graph_size; | |
61 | struct stat st; | |
62 | uint32_t i; | |
63 | struct commit_graph *graph; | |
64 | int fd = git_open(graph_file); | |
65 | uint64_t last_chunk_offset; | |
66 | uint32_t last_chunk_id; | |
67 | uint32_t graph_signature; | |
68 | unsigned char graph_version, hash_version; | |
69 | ||
70 | if (fd < 0) | |
71 | return NULL; | |
72 | if (fstat(fd, &st)) { | |
73 | close(fd); | |
74 | return NULL; | |
75 | } | |
76 | graph_size = xsize_t(st.st_size); | |
77 | ||
78 | if (graph_size < GRAPH_MIN_SIZE) { | |
79 | close(fd); | |
80 | die("graph file %s is too small", graph_file); | |
81 | } | |
82 | graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0); | |
83 | data = (const unsigned char *)graph_map; | |
84 | ||
85 | graph_signature = get_be32(data); | |
86 | if (graph_signature != GRAPH_SIGNATURE) { | |
87 | error("graph signature %X does not match signature %X", | |
88 | graph_signature, GRAPH_SIGNATURE); | |
89 | goto cleanup_fail; | |
90 | } | |
91 | ||
92 | graph_version = *(unsigned char*)(data + 4); | |
93 | if (graph_version != GRAPH_VERSION) { | |
94 | error("graph version %X does not match version %X", | |
95 | graph_version, GRAPH_VERSION); | |
96 | goto cleanup_fail; | |
97 | } | |
98 | ||
99 | hash_version = *(unsigned char*)(data + 5); | |
100 | if (hash_version != GRAPH_OID_VERSION) { | |
101 | error("hash version %X does not match version %X", | |
102 | hash_version, GRAPH_OID_VERSION); | |
103 | goto cleanup_fail; | |
104 | } | |
105 | ||
106 | graph = alloc_commit_graph(); | |
107 | ||
108 | graph->hash_len = GRAPH_OID_LEN; | |
109 | graph->num_chunks = *(unsigned char*)(data + 6); | |
110 | graph->graph_fd = fd; | |
111 | graph->data = graph_map; | |
112 | graph->data_len = graph_size; | |
113 | ||
114 | last_chunk_id = 0; | |
115 | last_chunk_offset = 8; | |
116 | chunk_lookup = data + 8; | |
117 | for (i = 0; i < graph->num_chunks; i++) { | |
118 | uint32_t chunk_id = get_be32(chunk_lookup + 0); | |
119 | uint64_t chunk_offset = get_be64(chunk_lookup + 4); | |
120 | int chunk_repeated = 0; | |
121 | ||
122 | chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH; | |
123 | ||
124 | if (chunk_offset > graph_size - GIT_MAX_RAWSZ) { | |
125 | error("improper chunk offset %08x%08x", (uint32_t)(chunk_offset >> 32), | |
126 | (uint32_t)chunk_offset); | |
127 | goto cleanup_fail; | |
128 | } | |
129 | ||
130 | switch (chunk_id) { | |
131 | case GRAPH_CHUNKID_OIDFANOUT: | |
132 | if (graph->chunk_oid_fanout) | |
133 | chunk_repeated = 1; | |
134 | else | |
135 | graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset); | |
136 | break; | |
137 | ||
138 | case GRAPH_CHUNKID_OIDLOOKUP: | |
139 | if (graph->chunk_oid_lookup) | |
140 | chunk_repeated = 1; | |
141 | else | |
142 | graph->chunk_oid_lookup = data + chunk_offset; | |
143 | break; | |
144 | ||
145 | case GRAPH_CHUNKID_DATA: | |
146 | if (graph->chunk_commit_data) | |
147 | chunk_repeated = 1; | |
148 | else | |
149 | graph->chunk_commit_data = data + chunk_offset; | |
150 | break; | |
151 | ||
152 | case GRAPH_CHUNKID_LARGEEDGES: | |
153 | if (graph->chunk_large_edges) | |
154 | chunk_repeated = 1; | |
155 | else | |
156 | graph->chunk_large_edges = data + chunk_offset; | |
157 | break; | |
158 | } | |
159 | ||
160 | if (chunk_repeated) { | |
161 | error("chunk id %08x appears multiple times", chunk_id); | |
162 | goto cleanup_fail; | |
163 | } | |
164 | ||
165 | if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP) | |
166 | { | |
167 | graph->num_commits = (chunk_offset - last_chunk_offset) | |
168 | / graph->hash_len; | |
169 | } | |
170 | ||
171 | last_chunk_id = chunk_id; | |
172 | last_chunk_offset = chunk_offset; | |
173 | } | |
174 | ||
175 | return graph; | |
176 | ||
177 | cleanup_fail: | |
178 | munmap(graph_map, graph_size); | |
179 | close(fd); | |
180 | exit(1); | |
181 | } | |
182 | ||
177722b3 DS |
183 | /* global storage */ |
184 | static struct commit_graph *commit_graph = NULL; | |
185 | ||
186 | static void prepare_commit_graph_one(const char *obj_dir) | |
187 | { | |
188 | char *graph_name; | |
189 | ||
190 | if (commit_graph) | |
191 | return; | |
192 | ||
193 | graph_name = get_commit_graph_filename(obj_dir); | |
194 | commit_graph = load_commit_graph_one(graph_name); | |
195 | ||
196 | FREE_AND_NULL(graph_name); | |
197 | } | |
198 | ||
199 | static int prepare_commit_graph_run_once = 0; | |
200 | static void prepare_commit_graph(void) | |
201 | { | |
202 | struct alternate_object_database *alt; | |
203 | char *obj_dir; | |
204 | ||
205 | if (prepare_commit_graph_run_once) | |
206 | return; | |
207 | prepare_commit_graph_run_once = 1; | |
208 | ||
209 | obj_dir = get_object_directory(); | |
210 | prepare_commit_graph_one(obj_dir); | |
b10edb2d JH |
211 | prepare_alt_odb(the_repository); |
212 | for (alt = the_repository->objects->alt_odb_list; | |
213 | !commit_graph && alt; | |
214 | alt = alt->next) | |
177722b3 DS |
215 | prepare_commit_graph_one(alt->path); |
216 | } | |
217 | ||
218 | static void close_commit_graph(void) | |
219 | { | |
220 | if (!commit_graph) | |
221 | return; | |
222 | ||
223 | if (commit_graph->graph_fd >= 0) { | |
224 | munmap((void *)commit_graph->data, commit_graph->data_len); | |
225 | commit_graph->data = NULL; | |
226 | close(commit_graph->graph_fd); | |
227 | } | |
228 | ||
229 | FREE_AND_NULL(commit_graph); | |
230 | } | |
231 | ||
232 | static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos) | |
233 | { | |
234 | return bsearch_hash(oid->hash, g->chunk_oid_fanout, | |
235 | g->chunk_oid_lookup, g->hash_len, pos); | |
236 | } | |
237 | ||
238 | static struct commit_list **insert_parent_or_die(struct commit_graph *g, | |
239 | uint64_t pos, | |
240 | struct commit_list **pptr) | |
241 | { | |
242 | struct commit *c; | |
243 | struct object_id oid; | |
244 | hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * pos); | |
245 | c = lookup_commit(&oid); | |
246 | if (!c) | |
247 | die("could not find commit %s", oid_to_hex(&oid)); | |
248 | c->graph_pos = pos; | |
249 | return &commit_list_insert(c, pptr)->next; | |
250 | } | |
251 | ||
e2838d85 DS |
252 | static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos) |
253 | { | |
254 | const unsigned char *commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * pos; | |
255 | item->graph_pos = pos; | |
256 | item->generation = get_be32(commit_data + g->hash_len + 8) >> 2; | |
257 | } | |
258 | ||
177722b3 DS |
259 | static int fill_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t pos) |
260 | { | |
177722b3 DS |
261 | uint32_t edge_value; |
262 | uint32_t *parent_data_ptr; | |
263 | uint64_t date_low, date_high; | |
264 | struct commit_list **pptr; | |
265 | const unsigned char *commit_data = g->chunk_commit_data + (g->hash_len + 16) * pos; | |
266 | ||
267 | item->object.parsed = 1; | |
268 | item->graph_pos = pos; | |
269 | ||
7b8a21db | 270 | item->maybe_tree = NULL; |
177722b3 DS |
271 | |
272 | date_high = get_be32(commit_data + g->hash_len + 8) & 0x3; | |
273 | date_low = get_be32(commit_data + g->hash_len + 12); | |
274 | item->date = (timestamp_t)((date_high << 32) | date_low); | |
275 | ||
83073cc9 DS |
276 | item->generation = get_be32(commit_data + g->hash_len + 8) >> 2; |
277 | ||
177722b3 DS |
278 | pptr = &item->parents; |
279 | ||
280 | edge_value = get_be32(commit_data + g->hash_len); | |
281 | if (edge_value == GRAPH_PARENT_NONE) | |
282 | return 1; | |
283 | pptr = insert_parent_or_die(g, edge_value, pptr); | |
284 | ||
285 | edge_value = get_be32(commit_data + g->hash_len + 4); | |
286 | if (edge_value == GRAPH_PARENT_NONE) | |
287 | return 1; | |
288 | if (!(edge_value & GRAPH_OCTOPUS_EDGES_NEEDED)) { | |
289 | pptr = insert_parent_or_die(g, edge_value, pptr); | |
290 | return 1; | |
291 | } | |
292 | ||
293 | parent_data_ptr = (uint32_t*)(g->chunk_large_edges + | |
294 | 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK)); | |
295 | do { | |
296 | edge_value = get_be32(parent_data_ptr); | |
297 | pptr = insert_parent_or_die(g, | |
298 | edge_value & GRAPH_EDGE_LAST_MASK, | |
299 | pptr); | |
300 | parent_data_ptr++; | |
301 | } while (!(edge_value & GRAPH_LAST_EDGE)); | |
302 | ||
303 | return 1; | |
304 | } | |
305 | ||
e2838d85 DS |
306 | static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos) |
307 | { | |
308 | if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) { | |
309 | *pos = item->graph_pos; | |
310 | return 1; | |
311 | } else { | |
312 | return bsearch_graph(g, &(item->object.oid), pos); | |
313 | } | |
314 | } | |
315 | ||
177722b3 DS |
316 | int parse_commit_in_graph(struct commit *item) |
317 | { | |
e2838d85 DS |
318 | uint32_t pos; |
319 | ||
177722b3 DS |
320 | if (!core_commit_graph) |
321 | return 0; | |
322 | if (item->object.parsed) | |
323 | return 1; | |
177722b3 | 324 | prepare_commit_graph(); |
e2838d85 DS |
325 | if (commit_graph && find_commit_in_graph(item, commit_graph, &pos)) |
326 | return fill_commit_in_graph(item, commit_graph, pos); | |
177722b3 DS |
327 | return 0; |
328 | } | |
329 | ||
e2838d85 DS |
330 | void load_commit_graph_info(struct commit *item) |
331 | { | |
332 | uint32_t pos; | |
333 | if (!core_commit_graph) | |
334 | return; | |
335 | prepare_commit_graph(); | |
336 | if (commit_graph && find_commit_in_graph(item, commit_graph, &pos)) | |
337 | fill_commit_graph_info(item, commit_graph, pos); | |
338 | } | |
339 | ||
7b8a21db DS |
340 | static struct tree *load_tree_for_commit(struct commit_graph *g, struct commit *c) |
341 | { | |
342 | struct object_id oid; | |
343 | const unsigned char *commit_data = g->chunk_commit_data + | |
344 | GRAPH_DATA_WIDTH * (c->graph_pos); | |
345 | ||
346 | hashcpy(oid.hash, commit_data); | |
347 | c->maybe_tree = lookup_tree(&oid); | |
348 | ||
349 | return c->maybe_tree; | |
350 | } | |
351 | ||
352 | struct tree *get_commit_tree_in_graph(const struct commit *c) | |
353 | { | |
354 | if (c->maybe_tree) | |
355 | return c->maybe_tree; | |
356 | if (c->graph_pos == COMMIT_NOT_FROM_GRAPH) | |
357 | BUG("get_commit_tree_in_graph called from non-commit-graph commit"); | |
358 | ||
359 | return load_tree_for_commit(commit_graph, (struct commit *)c); | |
360 | } | |
361 | ||
08fd81c9 DS |
362 | static void write_graph_chunk_fanout(struct hashfile *f, |
363 | struct commit **commits, | |
364 | int nr_commits) | |
365 | { | |
366 | int i, count = 0; | |
367 | struct commit **list = commits; | |
368 | ||
369 | /* | |
370 | * Write the first-level table (the list is sorted, | |
371 | * but we use a 256-entry lookup to be able to avoid | |
372 | * having to do eight extra binary search iterations). | |
373 | */ | |
374 | for (i = 0; i < 256; i++) { | |
375 | while (count < nr_commits) { | |
376 | if ((*list)->object.oid.hash[0] != i) | |
377 | break; | |
378 | count++; | |
379 | list++; | |
380 | } | |
381 | ||
382 | hashwrite_be32(f, count); | |
383 | } | |
384 | } | |
385 | ||
386 | static void write_graph_chunk_oids(struct hashfile *f, int hash_len, | |
387 | struct commit **commits, int nr_commits) | |
388 | { | |
389 | struct commit **list = commits; | |
390 | int count; | |
391 | for (count = 0; count < nr_commits; count++, list++) | |
392 | hashwrite(f, (*list)->object.oid.hash, (int)hash_len); | |
393 | } | |
394 | ||
395 | static const unsigned char *commit_to_sha1(size_t index, void *table) | |
396 | { | |
397 | struct commit **commits = table; | |
398 | return commits[index]->object.oid.hash; | |
399 | } | |
400 | ||
401 | static void write_graph_chunk_data(struct hashfile *f, int hash_len, | |
402 | struct commit **commits, int nr_commits) | |
403 | { | |
404 | struct commit **list = commits; | |
405 | struct commit **last = commits + nr_commits; | |
406 | uint32_t num_extra_edges = 0; | |
407 | ||
408 | while (list < last) { | |
409 | struct commit_list *parent; | |
410 | int edge_value; | |
411 | uint32_t packedDate[2]; | |
412 | ||
413 | parse_commit(*list); | |
2e27bd77 | 414 | hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len); |
08fd81c9 DS |
415 | |
416 | parent = (*list)->parents; | |
417 | ||
418 | if (!parent) | |
419 | edge_value = GRAPH_PARENT_NONE; | |
420 | else { | |
421 | edge_value = sha1_pos(parent->item->object.oid.hash, | |
422 | commits, | |
423 | nr_commits, | |
424 | commit_to_sha1); | |
425 | ||
426 | if (edge_value < 0) | |
427 | edge_value = GRAPH_PARENT_MISSING; | |
428 | } | |
429 | ||
430 | hashwrite_be32(f, edge_value); | |
431 | ||
432 | if (parent) | |
433 | parent = parent->next; | |
434 | ||
435 | if (!parent) | |
436 | edge_value = GRAPH_PARENT_NONE; | |
437 | else if (parent->next) | |
438 | edge_value = GRAPH_OCTOPUS_EDGES_NEEDED | num_extra_edges; | |
439 | else { | |
440 | edge_value = sha1_pos(parent->item->object.oid.hash, | |
441 | commits, | |
442 | nr_commits, | |
443 | commit_to_sha1); | |
444 | if (edge_value < 0) | |
445 | edge_value = GRAPH_PARENT_MISSING; | |
446 | } | |
447 | ||
448 | hashwrite_be32(f, edge_value); | |
449 | ||
450 | if (edge_value & GRAPH_OCTOPUS_EDGES_NEEDED) { | |
451 | do { | |
452 | num_extra_edges++; | |
453 | parent = parent->next; | |
454 | } while (parent); | |
455 | } | |
456 | ||
457 | if (sizeof((*list)->date) > 4) | |
458 | packedDate[0] = htonl(((*list)->date >> 32) & 0x3); | |
459 | else | |
460 | packedDate[0] = 0; | |
461 | ||
3258c663 DS |
462 | packedDate[0] |= htonl((*list)->generation << 2); |
463 | ||
08fd81c9 DS |
464 | packedDate[1] = htonl((*list)->date); |
465 | hashwrite(f, packedDate, 8); | |
466 | ||
467 | list++; | |
468 | } | |
469 | } | |
470 | ||
471 | static void write_graph_chunk_large_edges(struct hashfile *f, | |
472 | struct commit **commits, | |
473 | int nr_commits) | |
474 | { | |
475 | struct commit **list = commits; | |
476 | struct commit **last = commits + nr_commits; | |
477 | struct commit_list *parent; | |
478 | ||
479 | while (list < last) { | |
480 | int num_parents = 0; | |
481 | for (parent = (*list)->parents; num_parents < 3 && parent; | |
482 | parent = parent->next) | |
483 | num_parents++; | |
484 | ||
485 | if (num_parents <= 2) { | |
486 | list++; | |
487 | continue; | |
488 | } | |
489 | ||
490 | /* Since num_parents > 2, this initializer is safe. */ | |
491 | for (parent = (*list)->parents->next; parent; parent = parent->next) { | |
492 | int edge_value = sha1_pos(parent->item->object.oid.hash, | |
493 | commits, | |
494 | nr_commits, | |
495 | commit_to_sha1); | |
496 | ||
497 | if (edge_value < 0) | |
498 | edge_value = GRAPH_PARENT_MISSING; | |
499 | else if (!parent->next) | |
500 | edge_value |= GRAPH_LAST_EDGE; | |
501 | ||
502 | hashwrite_be32(f, edge_value); | |
503 | } | |
504 | ||
505 | list++; | |
506 | } | |
507 | } | |
508 | ||
509 | static int commit_compare(const void *_a, const void *_b) | |
510 | { | |
511 | const struct object_id *a = (const struct object_id *)_a; | |
512 | const struct object_id *b = (const struct object_id *)_b; | |
513 | return oidcmp(a, b); | |
514 | } | |
515 | ||
516 | struct packed_commit_list { | |
517 | struct commit **list; | |
518 | int nr; | |
519 | int alloc; | |
520 | }; | |
521 | ||
522 | struct packed_oid_list { | |
523 | struct object_id *list; | |
524 | int nr; | |
525 | int alloc; | |
526 | }; | |
527 | ||
528 | static int add_packed_commits(const struct object_id *oid, | |
529 | struct packed_git *pack, | |
530 | uint32_t pos, | |
531 | void *data) | |
532 | { | |
533 | struct packed_oid_list *list = (struct packed_oid_list*)data; | |
534 | enum object_type type; | |
535 | off_t offset = nth_packed_object_offset(pack, pos); | |
536 | struct object_info oi = OBJECT_INFO_INIT; | |
537 | ||
538 | oi.typep = &type; | |
fcb6df32 | 539 | if (packed_object_info(the_repository, pack, offset, &oi) < 0) |
08fd81c9 DS |
540 | die("unable to get type of object %s", oid_to_hex(oid)); |
541 | ||
542 | if (type != OBJ_COMMIT) | |
543 | return 0; | |
544 | ||
545 | ALLOC_GROW(list->list, list->nr + 1, list->alloc); | |
546 | oidcpy(&(list->list[list->nr]), oid); | |
547 | list->nr++; | |
548 | ||
549 | return 0; | |
550 | } | |
551 | ||
4f2542b4 DS |
552 | static void add_missing_parents(struct packed_oid_list *oids, struct commit *commit) |
553 | { | |
554 | struct commit_list *parent; | |
555 | for (parent = commit->parents; parent; parent = parent->next) { | |
556 | if (!(parent->item->object.flags & UNINTERESTING)) { | |
557 | ALLOC_GROW(oids->list, oids->nr + 1, oids->alloc); | |
558 | oidcpy(&oids->list[oids->nr], &(parent->item->object.oid)); | |
559 | oids->nr++; | |
560 | parent->item->object.flags |= UNINTERESTING; | |
561 | } | |
562 | } | |
563 | } | |
564 | ||
565 | static void close_reachable(struct packed_oid_list *oids) | |
566 | { | |
567 | int i; | |
568 | struct commit *commit; | |
569 | ||
570 | for (i = 0; i < oids->nr; i++) { | |
571 | commit = lookup_commit(&oids->list[i]); | |
572 | if (commit) | |
573 | commit->object.flags |= UNINTERESTING; | |
574 | } | |
575 | ||
576 | /* | |
577 | * As this loop runs, oids->nr may grow, but not more | |
578 | * than the number of missing commits in the reachable | |
579 | * closure. | |
580 | */ | |
581 | for (i = 0; i < oids->nr; i++) { | |
582 | commit = lookup_commit(&oids->list[i]); | |
583 | ||
584 | if (commit && !parse_commit(commit)) | |
585 | add_missing_parents(oids, commit); | |
586 | } | |
587 | ||
588 | for (i = 0; i < oids->nr; i++) { | |
589 | commit = lookup_commit(&oids->list[i]); | |
590 | ||
591 | if (commit) | |
592 | commit->object.flags &= ~UNINTERESTING; | |
593 | } | |
594 | } | |
595 | ||
3258c663 DS |
596 | static void compute_generation_numbers(struct packed_commit_list* commits) |
597 | { | |
598 | int i; | |
599 | struct commit_list *list = NULL; | |
600 | ||
601 | for (i = 0; i < commits->nr; i++) { | |
602 | if (commits->list[i]->generation != GENERATION_NUMBER_INFINITY && | |
603 | commits->list[i]->generation != GENERATION_NUMBER_ZERO) | |
604 | continue; | |
605 | ||
606 | commit_list_insert(commits->list[i], &list); | |
607 | while (list) { | |
608 | struct commit *current = list->item; | |
609 | struct commit_list *parent; | |
610 | int all_parents_computed = 1; | |
611 | uint32_t max_generation = 0; | |
612 | ||
613 | for (parent = current->parents; parent; parent = parent->next) { | |
614 | if (parent->item->generation == GENERATION_NUMBER_INFINITY || | |
615 | parent->item->generation == GENERATION_NUMBER_ZERO) { | |
616 | all_parents_computed = 0; | |
617 | commit_list_insert(parent->item, &list); | |
618 | break; | |
619 | } else if (parent->item->generation > max_generation) { | |
620 | max_generation = parent->item->generation; | |
621 | } | |
622 | } | |
623 | ||
624 | if (all_parents_computed) { | |
625 | current->generation = max_generation + 1; | |
626 | pop_commit(&list); | |
627 | ||
628 | if (current->generation > GENERATION_NUMBER_MAX) | |
629 | current->generation = GENERATION_NUMBER_MAX; | |
630 | } | |
631 | } | |
632 | } | |
633 | } | |
634 | ||
049d51a2 DS |
635 | void write_commit_graph(const char *obj_dir, |
636 | const char **pack_indexes, | |
3d5df01b DS |
637 | int nr_packs, |
638 | const char **commit_hex, | |
7547b95b DS |
639 | int nr_commits, |
640 | int append) | |
08fd81c9 DS |
641 | { |
642 | struct packed_oid_list oids; | |
643 | struct packed_commit_list commits; | |
644 | struct hashfile *f; | |
645 | uint32_t i, count_distinct = 0; | |
646 | char *graph_name; | |
08fd81c9 DS |
647 | struct lock_file lk = LOCK_INIT; |
648 | uint32_t chunk_ids[5]; | |
649 | uint64_t chunk_offsets[5]; | |
650 | int num_chunks; | |
651 | int num_extra_edges; | |
652 | struct commit_list *parent; | |
653 | ||
654 | oids.nr = 0; | |
655 | oids.alloc = approximate_object_count() / 4; | |
656 | ||
7547b95b DS |
657 | if (append) { |
658 | prepare_commit_graph_one(obj_dir); | |
659 | if (commit_graph) | |
660 | oids.alloc += commit_graph->num_commits; | |
661 | } | |
662 | ||
08fd81c9 DS |
663 | if (oids.alloc < 1024) |
664 | oids.alloc = 1024; | |
665 | ALLOC_ARRAY(oids.list, oids.alloc); | |
666 | ||
7547b95b DS |
667 | if (append && commit_graph) { |
668 | for (i = 0; i < commit_graph->num_commits; i++) { | |
669 | const unsigned char *hash = commit_graph->chunk_oid_lookup + | |
670 | commit_graph->hash_len * i; | |
671 | hashcpy(oids.list[oids.nr++].hash, hash); | |
672 | } | |
673 | } | |
674 | ||
049d51a2 DS |
675 | if (pack_indexes) { |
676 | struct strbuf packname = STRBUF_INIT; | |
677 | int dirlen; | |
678 | strbuf_addf(&packname, "%s/pack/", obj_dir); | |
679 | dirlen = packname.len; | |
680 | for (i = 0; i < nr_packs; i++) { | |
681 | struct packed_git *p; | |
682 | strbuf_setlen(&packname, dirlen); | |
683 | strbuf_addstr(&packname, pack_indexes[i]); | |
684 | p = add_packed_git(packname.buf, packname.len, 1); | |
685 | if (!p) | |
686 | die("error adding pack %s", packname.buf); | |
687 | if (open_pack_index(p)) | |
688 | die("error opening index for %s", packname.buf); | |
689 | for_each_object_in_pack(p, add_packed_commits, &oids); | |
690 | close_pack(p); | |
691 | } | |
692 | strbuf_release(&packname); | |
3d5df01b DS |
693 | } |
694 | ||
695 | if (commit_hex) { | |
696 | for (i = 0; i < nr_commits; i++) { | |
697 | const char *end; | |
698 | struct object_id oid; | |
699 | struct commit *result; | |
700 | ||
701 | if (commit_hex[i] && parse_oid_hex(commit_hex[i], &oid, &end)) | |
702 | continue; | |
703 | ||
704 | result = lookup_commit_reference_gently(&oid, 1); | |
705 | ||
706 | if (result) { | |
707 | ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc); | |
708 | oidcpy(&oids.list[oids.nr], &(result->object.oid)); | |
709 | oids.nr++; | |
710 | } | |
711 | } | |
712 | } | |
713 | ||
714 | if (!pack_indexes && !commit_hex) | |
049d51a2 DS |
715 | for_each_packed_object(add_packed_commits, &oids, 0); |
716 | ||
4f2542b4 | 717 | close_reachable(&oids); |
08fd81c9 DS |
718 | |
719 | QSORT(oids.list, oids.nr, commit_compare); | |
720 | ||
721 | count_distinct = 1; | |
722 | for (i = 1; i < oids.nr; i++) { | |
723 | if (oidcmp(&oids.list[i-1], &oids.list[i])) | |
724 | count_distinct++; | |
725 | } | |
726 | ||
727 | if (count_distinct >= GRAPH_PARENT_MISSING) | |
728 | die(_("the commit graph format cannot write %d commits"), count_distinct); | |
729 | ||
730 | commits.nr = 0; | |
731 | commits.alloc = count_distinct; | |
732 | ALLOC_ARRAY(commits.list, commits.alloc); | |
733 | ||
734 | num_extra_edges = 0; | |
735 | for (i = 0; i < oids.nr; i++) { | |
736 | int num_parents = 0; | |
737 | if (i > 0 && !oidcmp(&oids.list[i-1], &oids.list[i])) | |
738 | continue; | |
739 | ||
740 | commits.list[commits.nr] = lookup_commit(&oids.list[i]); | |
741 | parse_commit(commits.list[commits.nr]); | |
742 | ||
743 | for (parent = commits.list[commits.nr]->parents; | |
744 | parent; parent = parent->next) | |
745 | num_parents++; | |
746 | ||
747 | if (num_parents > 2) | |
748 | num_extra_edges += num_parents - 1; | |
749 | ||
750 | commits.nr++; | |
751 | } | |
752 | num_chunks = num_extra_edges ? 4 : 3; | |
753 | ||
754 | if (commits.nr >= GRAPH_PARENT_MISSING) | |
755 | die(_("too many commits to write graph")); | |
756 | ||
3258c663 | 757 | compute_generation_numbers(&commits); |
08fd81c9 | 758 | |
08fd81c9 | 759 | graph_name = get_commit_graph_filename(obj_dir); |
33286dcd DS |
760 | if (safe_create_leading_directories(graph_name)) |
761 | die_errno(_("unable to create leading directories of %s"), | |
762 | graph_name); | |
08fd81c9 | 763 | |
33286dcd | 764 | hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR); |
08fd81c9 DS |
765 | f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf); |
766 | ||
767 | hashwrite_be32(f, GRAPH_SIGNATURE); | |
768 | ||
769 | hashwrite_u8(f, GRAPH_VERSION); | |
770 | hashwrite_u8(f, GRAPH_OID_VERSION); | |
771 | hashwrite_u8(f, num_chunks); | |
772 | hashwrite_u8(f, 0); /* unused padding byte */ | |
773 | ||
774 | chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT; | |
775 | chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP; | |
776 | chunk_ids[2] = GRAPH_CHUNKID_DATA; | |
777 | if (num_extra_edges) | |
778 | chunk_ids[3] = GRAPH_CHUNKID_LARGEEDGES; | |
779 | else | |
780 | chunk_ids[3] = 0; | |
781 | chunk_ids[4] = 0; | |
782 | ||
783 | chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH; | |
784 | chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE; | |
785 | chunk_offsets[2] = chunk_offsets[1] + GRAPH_OID_LEN * commits.nr; | |
786 | chunk_offsets[3] = chunk_offsets[2] + (GRAPH_OID_LEN + 16) * commits.nr; | |
787 | chunk_offsets[4] = chunk_offsets[3] + 4 * num_extra_edges; | |
788 | ||
789 | for (i = 0; i <= num_chunks; i++) { | |
790 | uint32_t chunk_write[3]; | |
791 | ||
792 | chunk_write[0] = htonl(chunk_ids[i]); | |
793 | chunk_write[1] = htonl(chunk_offsets[i] >> 32); | |
794 | chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff); | |
795 | hashwrite(f, chunk_write, 12); | |
796 | } | |
797 | ||
798 | write_graph_chunk_fanout(f, commits.list, commits.nr); | |
799 | write_graph_chunk_oids(f, GRAPH_OID_LEN, commits.list, commits.nr); | |
800 | write_graph_chunk_data(f, GRAPH_OID_LEN, commits.list, commits.nr); | |
801 | write_graph_chunk_large_edges(f, commits.list, commits.nr); | |
802 | ||
177722b3 | 803 | close_commit_graph(); |
08fd81c9 DS |
804 | finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC); |
805 | commit_lock_file(&lk); | |
806 | ||
807 | free(oids.list); | |
808 | oids.alloc = 0; | |
809 | oids.nr = 0; | |
810 | } |