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