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