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