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