]>
| Commit | Line | Data |
|---|---|---|
| 03eae9af | 1 | #define USE_THE_REPOSITORY_VARIABLE |
| baffc0e7 | 2 | #include "builtin.h" |
| f394e093 | 3 | #include "gettext.h" |
| 41771fa4 | 4 | #include "hex.h" |
| b2141fc1 | 5 | #include "config.h" |
| ff5ebe39 DB |
6 | #include "commit.h" |
| 7 | #include "tree.h" | |
| 8 | #include "blob.h" | |
| c418eda4 | 9 | #include "tag.h" |
| 944d8589 | 10 | #include "refs.h" |
| f9253394 | 11 | #include "pack.h" |
| 53dc3f3e | 12 | #include "cache-tree.h" |
| 271b8d25 | 13 | #include "fsck.h" |
| 5ac0a206 | 14 | #include "parse-options.h" |
| 1e49f22f | 15 | #include "progress.h" |
| 6f7f3beb | 16 | #include "streaming.h" |
| 0317f455 | 17 | #include "packfile.h" |
| 87bed179 | 18 | #include "object-file.h" |
| dabab1d6 | 19 | #include "object-name.h" |
| 8f491517 | 20 | #include "odb.h" |
| c339932b | 21 | #include "path.h" |
| 08c46a49 | 22 | #include "read-cache-ll.h" |
| cbeab747 | 23 | #include "replace-object.h" |
| 5a5ea141 | 24 | #include "resolve-undo.h" |
| e0fd51e1 | 25 | #include "run-command.h" |
| baf889c2 | 26 | #include "sparse-index.h" |
| b29759d8 | 27 | #include "worktree.h" |
| 0d30feef | 28 | #include "pack-revindex.h" |
| 756f1bcd | 29 | #include "pack-bitmap.h" |
| ff5ebe39 DB |
30 | |
| 31 | #define REACHABLE 0x0001 | |
| 2d9c58c6 | 32 | #define SEEN 0x0002 |
| 6e454b9a | 33 | #define HAS_OBJ 0x0004 |
| 092c55d0 JT |
34 | /* This flag is set if something points to this object. */ |
| 35 | #define USED 0x0008 | |
| d9839e03 | 36 | |
| 96f1e58f DR |
37 | static int show_root; |
| 38 | static int show_tags; | |
| 39 | static int show_unreachable; | |
| 566842f6 | 40 | static int include_reflogs = 1; |
| f29cd393 | 41 | static int check_full = 1; |
| 02976bf8 | 42 | static int connectivity_only; |
| 96f1e58f DR |
43 | static int check_strict; |
| 44 | static int keep_cache_objects; | |
| 22410549 JS |
45 | static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT; |
| 46 | static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT; | |
| e2b4f635 | 47 | static int errors_found; |
| 68f6c019 | 48 | static int write_lost_and_found; |
| 20f1eb6b | 49 | static int verbose; |
| 1e49f22f | 50 | static int show_progress = -1; |
| c6a13b2c | 51 | static int show_dangling = 1; |
| 90cf590f | 52 | static int name_objects; |
| c1cf918d | 53 | static int check_references = 1; |
| e2b4f635 JH |
54 | #define ERROR_OBJECT 01 |
| 55 | #define ERROR_REACHABLE 02 | |
| a3ed7552 | 56 | #define ERROR_PACK 04 |
| 122f76f5 | 57 | #define ERROR_REFS 010 |
| e0fd51e1 | 58 | #define ERROR_COMMIT_GRAPH 020 |
| e68a5272 | 59 | #define ERROR_MULTI_PACK_INDEX 040 |
| 0d30feef | 60 | #define ERROR_PACK_REV_INDEX 0100 |
| 756f1bcd | 61 | #define ERROR_BITMAP 0200 |
| d9839e03 | 62 | |
| 82ef89b3 | 63 | static const char *describe_object(const struct object_id *oid) |
| 993a21b0 | 64 | { |
| 82ef89b3 | 65 | return fsck_describe_object(&fsck_walk_options, oid); |
| 993a21b0 JS |
66 | } |
| 67 | ||
| 82ef89b3 JK |
68 | static const char *printable_type(const struct object_id *oid, |
| 69 | enum object_type type) | |
| 97ca7ca8 JK |
70 | { |
| 71 | const char *ret; | |
| 72 | ||
| 82ef89b3 | 73 | if (type == OBJ_NONE) |
| e989dd96 PS |
74 | type = odb_read_object_info(the_repository->objects, |
| 75 | oid, NULL); | |
| a2b22854 | 76 | |
| 82ef89b3 | 77 | ret = type_name(type); |
| 97ca7ca8 | 78 | if (!ret) |
| 674ba340 | 79 | ret = _("unknown"); |
| 97ca7ca8 JK |
80 | |
| 81 | return ret; | |
| 82 | } | |
| 83 | ||
| c99ba492 | 84 | static int objerror(struct object *obj, const char *err) |
| f1f0d088 | 85 | { |
| e2b4f635 | 86 | errors_found |= ERROR_OBJECT; |
| 674ba340 NTND |
87 | /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */ |
| 88 | fprintf_ln(stderr, _("error in %s %s: %s"), | |
| 82ef89b3 JK |
89 | printable_type(&obj->oid, obj->type), |
| 90 | describe_object(&obj->oid), err); | |
| f1f0d088 PB |
91 | return -1; |
| 92 | } | |
| 93 | ||
| 8cd4a447 | 94 | static int fsck_objects_error_func(struct fsck_options *o UNUSED, |
| 0ec5dfe8 | 95 | void *fsck_report, |
| 8cd4a447 | 96 | enum fsck_msg_type msg_type, |
| 97 | enum fsck_msg_id msg_id UNUSED, | |
| 98 | const char *message) | |
| f1f0d088 | 99 | { |
| 0ec5dfe8 | 100 | struct fsck_object_report *report = fsck_report; |
| 101 | const struct object_id *oid = report->oid; | |
| 102 | enum object_type object_type = report->object_type; | |
| 103 | ||
| 5afc4b1d | 104 | switch (msg_type) { |
| bbb15c51 | 105 | case FSCK_WARN: |
| 674ba340 NTND |
106 | /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */ |
| 107 | fprintf_ln(stderr, _("warning in %s %s: %s"), | |
| 5afc4b1d JK |
108 | printable_type(oid, object_type), |
| 109 | describe_object(oid), message); | |
| bbb15c51 NTND |
110 | return 0; |
| 111 | case FSCK_ERROR: | |
| 674ba340 NTND |
112 | /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */ |
| 113 | fprintf_ln(stderr, _("error in %s %s: %s"), | |
| 5afc4b1d JK |
114 | printable_type(oid, object_type), |
| 115 | describe_object(oid), message); | |
| bbb15c51 NTND |
116 | return 1; |
| 117 | default: | |
| 5afc4b1d JK |
118 | BUG("%d (FSCK_IGNORE?) should never trigger this callback", |
| 119 | msg_type); | |
| bbb15c51 | 120 | } |
| f1f0d088 PB |
121 | } |
| 122 | ||
| 04d39759 LT |
123 | static struct object_array pending; |
| 124 | ||
| 28abf260 | 125 | static int mark_object(struct object *obj, enum object_type type, |
| 0b4e9013 | 126 | void *data, struct fsck_options *options UNUSED) |
| 271b8d25 | 127 | { |
| 271b8d25 | 128 | struct object *parent = data; |
| 271b8d25 | 129 | |
| a1cdc251 JH |
130 | /* |
| 131 | * The only case data is NULL or type is OBJ_ANY is when | |
| 132 | * mark_object_reachable() calls us. All the callers of | |
| 133 | * that function has non-NULL obj hence ... | |
| 134 | */ | |
| 271b8d25 | 135 | if (!obj) { |
| a1cdc251 | 136 | /* ... these references to parent->fld are safe here */ |
| 674ba340 | 137 | printf_ln(_("broken link from %7s %s"), |
| 82ef89b3 JK |
138 | printable_type(&parent->oid, parent->type), |
| 139 | describe_object(&parent->oid)); | |
| 674ba340 NTND |
140 | printf_ln(_("broken link from %7s %s"), |
| 141 | (type == OBJ_ANY ? _("unknown") : type_name(type)), | |
| 142 | _("unknown")); | |
| 271b8d25 MK |
143 | errors_found |= ERROR_REACHABLE; |
| 144 | return 1; | |
| 145 | } | |
| 146 | ||
| 147 | if (type != OBJ_ANY && obj->type != type) | |
| a1cdc251 | 148 | /* ... and the reference to parent is safe here */ |
| 674ba340 | 149 | objerror(parent, _("wrong object type in link")); |
| 271b8d25 MK |
150 | |
| 151 | if (obj->flags & REACHABLE) | |
| 152 | return 0; | |
| 153 | obj->flags |= REACHABLE; | |
| caba7fc3 | 154 | |
| c87910b9 | 155 | if (is_promisor_object(the_repository, &obj->oid)) |
| caba7fc3 JT |
156 | /* |
| 157 | * Further recursion does not need to be performed on this | |
| 158 | * object since it is a promisor object (so it does not need to | |
| 159 | * be added to "pending"). | |
| 160 | */ | |
| 161 | return 0; | |
| 162 | ||
| 6e454b9a | 163 | if (!(obj->flags & HAS_OBJ)) { |
| fcf8e3e1 | 164 | if (parent && !odb_has_object(the_repository->objects, &obj->oid, 1)) { |
| 674ba340 NTND |
165 | printf_ln(_("broken link from %7s %s\n" |
| 166 | " to %7s %s"), | |
| 82ef89b3 JK |
167 | printable_type(&parent->oid, parent->type), |
| 168 | describe_object(&parent->oid), | |
| 169 | printable_type(&obj->oid, obj->type), | |
| 170 | describe_object(&obj->oid)); | |
| 271b8d25 MK |
171 | errors_found |= ERROR_REACHABLE; |
| 172 | } | |
| 173 | return 1; | |
| 174 | } | |
| 175 | ||
| 16aa3bfc | 176 | add_object_array(obj, NULL, &pending); |
| 04d39759 LT |
177 | return 0; |
| 178 | } | |
| 179 | ||
| 180 | static void mark_object_reachable(struct object *obj) | |
| 181 | { | |
| 22410549 | 182 | mark_object(obj, OBJ_ANY, NULL, NULL); |
| 04d39759 LT |
183 | } |
| 184 | ||
| a1cdc251 | 185 | static int traverse_one_object(struct object *obj) |
| 04d39759 | 186 | { |
| ba3a08ca EW |
187 | int result = fsck_walk(obj, obj, &fsck_walk_options); |
| 188 | ||
| 189 | if (obj->type == OBJ_TREE) { | |
| 190 | struct tree *tree = (struct tree *)obj; | |
| 191 | free_tree_buffer(tree); | |
| 192 | } | |
| 193 | return result; | |
| 271b8d25 MK |
194 | } |
| 195 | ||
| 04d39759 | 196 | static int traverse_reachable(void) |
| 271b8d25 | 197 | { |
| 1e49f22f NTND |
198 | struct progress *progress = NULL; |
| 199 | unsigned int nr = 0; | |
| 04d39759 | 200 | int result = 0; |
| 1e49f22f | 201 | if (show_progress) |
| 1f7e6478 PS |
202 | progress = start_delayed_progress(the_repository, |
| 203 | _("Checking connectivity"), 0); | |
| 04d39759 | 204 | while (pending.nr) { |
| 71992039 | 205 | result |= traverse_one_object(object_array_pop(&pending)); |
| 1e49f22f | 206 | display_progress(progress, ++nr); |
| 04d39759 | 207 | } |
| 1e49f22f | 208 | stop_progress(&progress); |
| 04d39759 | 209 | return !!result; |
| 271b8d25 MK |
210 | } |
| 211 | ||
| 2bbeddee | 212 | static int mark_used(struct object *obj, enum object_type type UNUSED, |
| 0b4e9013 | 213 | void *data UNUSED, struct fsck_options *options UNUSED) |
| 271b8d25 MK |
214 | { |
| 215 | if (!obj) | |
| 216 | return 1; | |
| 092c55d0 | 217 | obj->flags |= USED; |
| 271b8d25 | 218 | return 0; |
| f1f0d088 PB |
219 | } |
| 220 | ||
| 8d8c2a5a JK |
221 | static void mark_unreachable_referents(const struct object_id *oid) |
| 222 | { | |
| 223 | struct fsck_options options = FSCK_OPTIONS_DEFAULT; | |
| d0229abd | 224 | struct object *obj = lookup_object(the_repository, oid); |
| 8d8c2a5a JK |
225 | |
| 226 | if (!obj || !(obj->flags & HAS_OBJ)) | |
| 227 | return; /* not part of our original set */ | |
| 228 | if (obj->flags & REACHABLE) | |
| 229 | return; /* reachable objects already traversed */ | |
| 230 | ||
| 231 | /* | |
| 232 | * Avoid passing OBJ_NONE to fsck_walk, which will parse the object | |
| 233 | * (and we want to avoid parsing blobs). | |
| 234 | */ | |
| 235 | if (obj->type == OBJ_NONE) { | |
| e989dd96 PS |
236 | enum object_type type = odb_read_object_info(the_repository->objects, |
| 237 | &obj->oid, NULL); | |
| 8d8c2a5a | 238 | if (type > 0) |
| 6da43d93 | 239 | object_as_type(obj, type, 0); |
| 8d8c2a5a JK |
240 | } |
| 241 | ||
| 242 | options.walk = mark_used; | |
| 243 | fsck_walk(obj, NULL, &options); | |
| fbce4fa9 JK |
244 | if (obj->type == OBJ_TREE) |
| 245 | free_tree_buffer((struct tree *)obj); | |
| 8d8c2a5a JK |
246 | } |
| 247 | ||
| 248 | static int mark_loose_unreachable_referents(const struct object_id *oid, | |
| be252d33 JK |
249 | const char *path UNUSED, |
| 250 | void *data UNUSED) | |
| 8d8c2a5a JK |
251 | { |
| 252 | mark_unreachable_referents(oid); | |
| 253 | return 0; | |
| 254 | } | |
| 255 | ||
| 256 | static int mark_packed_unreachable_referents(const struct object_id *oid, | |
| be252d33 JK |
257 | struct packed_git *pack UNUSED, |
| 258 | uint32_t pos UNUSED, | |
| 259 | void *data UNUSED) | |
| 8d8c2a5a JK |
260 | { |
| 261 | mark_unreachable_referents(oid); | |
| 262 | return 0; | |
| 263 | } | |
| 264 | ||
| 18af29f2 LT |
265 | /* |
| 266 | * Check a single reachable object | |
| 267 | */ | |
| 268 | static void check_reachable_object(struct object *obj) | |
| 269 | { | |
| 18af29f2 LT |
270 | /* |
| 271 | * We obviously want the object to be parsed, | |
| 272 | * except if it was in a pack-file and we didn't | |
| 273 | * do a full fsck | |
| 274 | */ | |
| 6e454b9a | 275 | if (!(obj->flags & HAS_OBJ)) { |
| c87910b9 | 276 | if (is_promisor_object(the_repository, &obj->oid)) |
| caba7fc3 | 277 | return; |
| cc656f4e | 278 | if (has_object_pack(the_repository, &obj->oid)) |
| 18af29f2 | 279 | return; /* it is in pack - forget about it */ |
| 82ef89b3 JK |
280 | printf_ln(_("missing %s %s"), |
| 281 | printable_type(&obj->oid, obj->type), | |
| 282 | describe_object(&obj->oid)); | |
| e2b4f635 | 283 | errors_found |= ERROR_REACHABLE; |
| 18af29f2 LT |
284 | return; |
| 285 | } | |
| 18af29f2 LT |
286 | } |
| 287 | ||
| 288 | /* | |
| 289 | * Check a single unreachable object | |
| 290 | */ | |
| 291 | static void check_unreachable_object(struct object *obj) | |
| 292 | { | |
| 293 | /* | |
| 294 | * Missing unreachable object? Ignore it. It's not like | |
| 295 | * we miss it (since it can't be reached), nor do we want | |
| 296 | * to complain about it being unreachable (since it does | |
| 297 | * not exist). | |
| 298 | */ | |
| b4584e4f | 299 | if (!(obj->flags & HAS_OBJ)) |
| 18af29f2 LT |
300 | return; |
| 301 | ||
| 302 | /* | |
| 303 | * Unreachable object that exists? Show it if asked to, | |
| 304 | * since this is something that is prunable. | |
| 305 | */ | |
| 306 | if (show_unreachable) { | |
| 82ef89b3 JK |
307 | printf_ln(_("unreachable %s %s"), |
| 308 | printable_type(&obj->oid, obj->type), | |
| 309 | describe_object(&obj->oid)); | |
| 18af29f2 LT |
310 | return; |
| 311 | } | |
| 312 | ||
| 313 | /* | |
| 092c55d0 | 314 | * "!USED" means that nothing at all points to it, including |
| 3dff5379 | 315 | * other unreachable objects. In other words, it's the "tip" |
| 18af29f2 LT |
316 | * of some set of unreachable objects, usually a commit that |
| 317 | * got dropped. | |
| 318 | * | |
| 319 | * Such starting points are more interesting than some random | |
| 320 | * set of unreachable objects, so we show them even if the user | |
| 321 | * hasn't asked for _all_ unreachable objects. If you have | |
| 322 | * deleted a branch by mistake, this is a prime candidate to | |
| 323 | * start looking at, for example. | |
| 324 | */ | |
| 092c55d0 | 325 | if (!(obj->flags & USED)) { |
| c6a13b2c | 326 | if (show_dangling) |
| 82ef89b3 JK |
327 | printf_ln(_("dangling %s %s"), |
| 328 | printable_type(&obj->oid, obj->type), | |
| 329 | describe_object(&obj->oid)); | |
| 68f6c019 | 330 | if (write_lost_and_found) { |
| bba59f58 | 331 | char *filename = repo_git_path(the_repository, "lost-found/%s/%s", |
| 68f6c019 | 332 | obj->type == OBJ_COMMIT ? "commit" : "other", |
| 82ef89b3 | 333 | describe_object(&obj->oid)); |
| 68f6c019 JS |
334 | FILE *f; |
| 335 | ||
| 1a99fe80 | 336 | if (safe_create_leading_directories_const(the_repository, filename)) { |
| 674ba340 | 337 | error(_("could not create lost-found")); |
| fcd12db6 | 338 | free(filename); |
| 68f6c019 JS |
339 | return; |
| 340 | } | |
| 23a9e071 | 341 | f = xfopen(filename, "w"); |
| 16a7fcfe | 342 | if (obj->type == OBJ_BLOB) { |
| 7eda0e4f | 343 | if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1)) |
| 674ba340 | 344 | die_errno(_("could not write '%s'"), filename); |
| 16a7fcfe | 345 | } else |
| 82ef89b3 | 346 | fprintf(f, "%s\n", describe_object(&obj->oid)); |
| 47d32af2 | 347 | if (fclose(f)) |
| 674ba340 | 348 | die_errno(_("could not finish '%s'"), |
| d824cbba | 349 | filename); |
| fcd12db6 | 350 | free(filename); |
| 68f6c019 | 351 | } |
| 18af29f2 LT |
352 | return; |
| 353 | } | |
| 354 | ||
| 355 | /* | |
| 356 | * Otherwise? It's there, it's unreachable, and some other unreachable | |
| 357 | * object points to it. Ignore it - it's not interesting, and we showed | |
| 358 | * all the interesting cases above. | |
| 359 | */ | |
| 360 | } | |
| 361 | ||
| 362 | static void check_object(struct object *obj) | |
| 363 | { | |
| 20f1eb6b | 364 | if (verbose) |
| 82ef89b3 | 365 | fprintf_ln(stderr, _("Checking %s"), describe_object(&obj->oid)); |
| 20f1eb6b | 366 | |
| 18af29f2 LT |
367 | if (obj->flags & REACHABLE) |
| 368 | check_reachable_object(obj); | |
| 369 | else | |
| 370 | check_unreachable_object(obj); | |
| 371 | } | |
| f1f0d088 | 372 | |
| 8ba0bbb2 LT |
373 | static void check_connectivity(void) |
| 374 | { | |
| fc046a75 | 375 | int i, max; |
| 8ba0bbb2 | 376 | |
| 04d39759 LT |
377 | /* Traverse the pending reachable objects */ |
| 378 | traverse_reachable(); | |
| 379 | ||
| 8d8c2a5a JK |
380 | /* |
| 381 | * With --connectivity-only, we won't have actually opened and marked | |
| 382 | * unreachable objects with USED. Do that now to make --dangling, etc | |
| 383 | * accurate. | |
| 384 | */ | |
| 385 | if (connectivity_only && (show_dangling || write_lost_and_found)) { | |
| 386 | /* | |
| 387 | * Even though we already have a "struct object" for each of | |
| 388 | * these in memory, we must not iterate over the internal | |
| 389 | * object hash as we do below. Our loop would potentially | |
| 390 | * resize the hash, making our iteration invalid. | |
| 391 | * | |
| 392 | * Instead, we'll just go back to the source list of objects, | |
| 393 | * and ignore any that weren't present in our earlier | |
| 394 | * traversal. | |
| 395 | */ | |
| d81712ce PS |
396 | for_each_loose_object(the_repository->objects, |
| 397 | mark_loose_unreachable_referents, NULL, 0); | |
| c87910b9 KN |
398 | for_each_packed_object(the_repository, |
| 399 | mark_packed_unreachable_referents, | |
| 400 | NULL, | |
| 401 | 0); | |
| 8d8c2a5a JK |
402 | } |
| 403 | ||
| 8ba0bbb2 | 404 | /* Look up all the requirements, warn about missing objects.. */ |
| 74d414c9 | 405 | max = get_max_object_index(the_repository); |
| 20f1eb6b | 406 | if (verbose) |
| 674ba340 | 407 | fprintf_ln(stderr, _("Checking connectivity (%d objects)"), max); |
| 20f1eb6b | 408 | |
| fc046a75 | 409 | for (i = 0; i < max; i++) { |
| 74d414c9 | 410 | struct object *obj = get_indexed_object(the_repository, i); |
| 8ba0bbb2 | 411 | |
| 18af29f2 LT |
412 | if (obj) |
| 413 | check_object(obj); | |
| 8ba0bbb2 LT |
414 | } |
| 415 | } | |
| 416 | ||
| 7ac4f3a0 | 417 | static int fsck_obj(struct object *obj, void *buffer, unsigned long size) |
| 85003492 | 418 | { |
| 83cd6f90 RS |
419 | int err; |
| 420 | ||
| ba002f3b | 421 | if (obj->flags & SEEN) |
| 85003492 | 422 | return 0; |
| ba002f3b | 423 | obj->flags |= SEEN; |
| 85003492 | 424 | |
| 20f1eb6b | 425 | if (verbose) |
| 674ba340 | 426 | fprintf_ln(stderr, _("Checking %s %s"), |
| 82ef89b3 JK |
427 | printable_type(&obj->oid, obj->type), |
| 428 | describe_object(&obj->oid)); | |
| 42ea9cb2 | 429 | |
| 22410549 | 430 | if (fsck_walk(obj, NULL, &fsck_obj_options)) |
| 674ba340 | 431 | objerror(obj, _("broken links")); |
| 7ac4f3a0 | 432 | err = fsck_object(obj, buffer, size, &fsck_obj_options); |
| 83cd6f90 RS |
433 | if (err) |
| 434 | goto out; | |
| 1ea34e36 | 435 | |
| ba002f3b MK |
436 | if (obj->type == OBJ_COMMIT) { |
| 437 | struct commit *commit = (struct commit *) obj; | |
| de2eb7f6 | 438 | |
| ba002f3b | 439 | if (!commit->parents && show_root) |
| 674ba340 | 440 | printf_ln(_("root %s"), |
| 82ef89b3 | 441 | describe_object(&commit->object.oid)); |
| ba002f3b | 442 | } |
| 92d4c85d | 443 | |
| ba002f3b MK |
444 | if (obj->type == OBJ_TAG) { |
| 445 | struct tag *tag = (struct tag *) obj; | |
| 20f1eb6b | 446 | |
| ba002f3b | 447 | if (show_tags && tag->tagged) { |
| 674ba340 | 448 | printf_ln(_("tagged %s %s (%s) in %s"), |
| 82ef89b3 JK |
449 | printable_type(&tag->tagged->oid, tag->tagged->type), |
| 450 | describe_object(&tag->tagged->oid), | |
| bbb15c51 | 451 | tag->tag, |
| 82ef89b3 | 452 | describe_object(&tag->object.oid)); |
| ba002f3b | 453 | } |
| 92d4c85d | 454 | } |
| 889262ea | 455 | |
| 83cd6f90 RS |
456 | out: |
| 457 | if (obj->type == OBJ_TREE) | |
| 458 | free_tree_buffer((struct tree *)obj); | |
| 83cd6f90 | 459 | return err; |
| 20222118 LT |
460 | } |
| 461 | ||
| 9fd75046 | 462 | static int fsck_obj_buffer(const struct object_id *oid, enum object_type type, |
| c9486eb0 NTND |
463 | unsigned long size, void *buffer, int *eaten) |
| 464 | { | |
| ec9d2249 NTND |
465 | /* |
| 466 | * Note, buffer may be NULL if type is OBJ_BLOB. See | |
| 467 | * verify_packfile(), data_valid variable for details. | |
| 468 | */ | |
| c9486eb0 | 469 | struct object *obj; |
| 1ec5bfd2 SB |
470 | obj = parse_object_buffer(the_repository, oid, type, size, buffer, |
| 471 | eaten); | |
| c9486eb0 NTND |
472 | if (!obj) { |
| 473 | errors_found |= ERROR_OBJECT; | |
| 674ba340 NTND |
474 | return error(_("%s: object corrupt or missing"), |
| 475 | oid_to_hex(oid)); | |
| c9486eb0 | 476 | } |
| 092c55d0 JT |
477 | obj->flags &= ~(REACHABLE | SEEN); |
| 478 | obj->flags |= HAS_OBJ; | |
| 7ac4f3a0 | 479 | return fsck_obj(obj, buffer, size); |
| c9486eb0 NTND |
480 | } |
| 481 | ||
| 96f1e58f | 482 | static int default_refs; |
| 944d8589 | 483 | |
| 9461d272 | 484 | static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid, |
| dddbad72 | 485 | timestamp_t timestamp) |
| 55dd5526 JH |
486 | { |
| 487 | struct object *obj; | |
| 488 | ||
| 9461d272 | 489 | if (!is_null_oid(oid)) { |
| d0229abd | 490 | obj = lookup_object(the_repository, oid); |
| c2d17b3b | 491 | if (obj && (obj->flags & HAS_OBJ)) { |
| a59cfb32 | 492 | if (timestamp) |
| 73390290 | 493 | fsck_put_object_name(&fsck_walk_options, oid, |
| a59cfb32 JK |
494 | "%s@{%"PRItime"}", |
| 495 | refname, timestamp); | |
| 092c55d0 | 496 | obj->flags |= USED; |
| 271b8d25 | 497 | mark_object_reachable(obj); |
| c87910b9 | 498 | } else if (!is_promisor_object(the_repository, oid)) { |
| 674ba340 NTND |
499 | error(_("%s: invalid reflog entry %s"), |
| 500 | refname, oid_to_hex(oid)); | |
| 19bf6c9b | 501 | errors_found |= ERROR_REACHABLE; |
| 55dd5526 JH |
502 | } |
| 503 | } | |
| d66ae59b MH |
504 | } |
| 505 | ||
| b9fd73a2 PS |
506 | static int fsck_handle_reflog_ent(const char *refname, |
| 507 | struct object_id *ooid, struct object_id *noid, | |
| 5cf88fd8 ÆAB |
508 | const char *email UNUSED, |
| 509 | timestamp_t timestamp, int tz UNUSED, | |
| b9fd73a2 | 510 | const char *message UNUSED, void *cb_data UNUSED) |
| 55dd5526 | 511 | { |
| 20f1eb6b | 512 | if (verbose) |
| 674ba340 NTND |
513 | fprintf_ln(stderr, _("Checking reflog %s->%s"), |
| 514 | oid_to_hex(ooid), oid_to_hex(noid)); | |
| 20f1eb6b | 515 | |
| 9461d272 | 516 | fsck_handle_reflog_oid(refname, ooid, 0); |
| 517 | fsck_handle_reflog_oid(refname, noid, timestamp); | |
| 55dd5526 JH |
518 | return 0; |
| 519 | } | |
| 520 | ||
| 31f89839 | 521 | static int fsck_handle_reflog(const char *logname, void *cb_data) |
| eb8381c8 | 522 | { |
| b29759d8 NTND |
523 | struct strbuf refname = STRBUF_INIT; |
| 524 | ||
| 525 | strbuf_worktree_ref(cb_data, &refname, logname); | |
| 2e5c4758 PS |
526 | refs_for_each_reflog_ent(get_main_ref_store(the_repository), |
| 527 | refname.buf, fsck_handle_reflog_ent, | |
| b9fd73a2 | 528 | NULL); |
| b29759d8 | 529 | strbuf_release(&refname); |
| eb8381c8 NP |
530 | return 0; |
| 531 | } | |
| 532 | ||
| bdbebe57 | 533 | static int fsck_handle_ref(const struct reference *ref, void *cb_data UNUSED) |
| 1024932f | 534 | { |
| 1024932f LT |
535 | struct object *obj; |
| 536 | ||
| bdbebe57 | 537 | obj = parse_object(the_repository, ref->oid); |
| 8a498a05 | 538 | if (!obj) { |
| bdbebe57 | 539 | if (is_promisor_object(the_repository, ref->oid)) { |
| 43f25158 JT |
540 | /* |
| 541 | * Increment default_refs anyway, because this is a | |
| 542 | * valid ref. | |
| 543 | */ | |
| 544 | default_refs++; | |
| 545 | return 0; | |
| 546 | } | |
| 674ba340 | 547 | error(_("%s: invalid sha1 pointer %s"), |
| bdbebe57 | 548 | ref->name, oid_to_hex(ref->oid)); |
| 30d1038d | 549 | errors_found |= ERROR_REACHABLE; |
| 944d8589 LT |
550 | /* We'll continue with the rest despite the error.. */ |
| 551 | return 0; | |
| 8a498a05 | 552 | } |
| bdbebe57 PS |
553 | if (obj->type != OBJ_COMMIT && is_branch(ref->name)) { |
| 554 | error(_("%s: not a commit"), ref->name); | |
| 122f76f5 JH |
555 | errors_found |= ERROR_REFS; |
| 556 | } | |
| 944d8589 | 557 | default_refs++; |
| 092c55d0 | 558 | obj->flags |= USED; |
| a59cfb32 | 559 | fsck_put_object_name(&fsck_walk_options, |
| bdbebe57 | 560 | ref->oid, "%s", ref->name); |
| 271b8d25 | 561 | mark_object_reachable(obj); |
| 55dd5526 | 562 | |
| 7c4d07c7 | 563 | return 0; |
| 1024932f LT |
564 | } |
| 565 | ||
| b29759d8 NTND |
566 | static int fsck_head_link(const char *head_ref_name, |
| 567 | const char **head_points_at, | |
| a8c754d4 EN |
568 | struct object_id *head_oid); |
| 569 | ||
| 1024932f LT |
570 | static void get_default_heads(void) |
| 571 | { | |
| b29759d8 | 572 | struct worktree **worktrees, **p; |
| a8c754d4 EN |
573 | const char *head_points_at; |
| 574 | struct object_id head_oid; | |
| 575 | ||
| 2e5c4758 PS |
576 | refs_for_each_rawref(get_main_ref_store(the_repository), |
| 577 | fsck_handle_ref, NULL); | |
| b29759d8 | 578 | |
| 03f2465b | 579 | worktrees = get_worktrees(); |
| b29759d8 NTND |
580 | for (p = worktrees; *p; p++) { |
| 581 | struct worktree *wt = *p; | |
| bdbebe57 | 582 | struct strbuf refname = STRBUF_INIT; |
| b29759d8 | 583 | |
| bdbebe57 PS |
584 | strbuf_worktree_ref(wt, &refname, "HEAD"); |
| 585 | fsck_head_link(refname.buf, &head_points_at, &head_oid); | |
| 586 | if (head_points_at && !is_null_oid(&head_oid)) { | |
| 587 | struct reference ref = { | |
| 588 | .name = refname.buf, | |
| 589 | .oid = &head_oid, | |
| 590 | }; | |
| 591 | ||
| 592 | fsck_handle_ref(&ref, NULL); | |
| 593 | } | |
| 594 | strbuf_release(&refname); | |
| b29759d8 NTND |
595 | |
| 596 | if (include_reflogs) | |
| 597 | refs_for_each_reflog(get_worktree_ref_store(wt), | |
| 598 | fsck_handle_reflog, wt); | |
| 599 | } | |
| 600 | free_worktrees(worktrees); | |
| 071fa89e LT |
601 | |
| 602 | /* | |
| 603 | * Not having any default heads isn't really fatal, but | |
| 604 | * it does mean that "--unreachable" no longer makes any | |
| 605 | * sense (since in this case everything will obviously | |
| 606 | * be unreachable by definition. | |
| 607 | * | |
| 608 | * Showing dangling objects is valid, though (as those | |
| 609 | * dangling objects are likely lost heads). | |
| 610 | * | |
| 611 | * So we just print a warning about it, and clear the | |
| 612 | * "show_unreachable" flag. | |
| 613 | */ | |
| 614 | if (!default_refs) { | |
| 674ba340 | 615 | fprintf_ln(stderr, _("notice: No default references")); |
| 071fa89e LT |
616 | show_unreachable = 0; |
| 617 | } | |
| 1024932f LT |
618 | } |
| 619 | ||
| 31deb28f ÆAB |
620 | struct for_each_loose_cb |
| 621 | { | |
| 622 | struct progress *progress; | |
| 31deb28f ÆAB |
623 | }; |
| 624 | ||
| 4ae0e942 JK |
625 | static int fsck_loose(const struct object_id *oid, const char *path, |
| 626 | void *data UNUSED) | |
| c68b489e JK |
627 | { |
| 628 | struct object *obj; | |
| 31deb28f | 629 | enum object_type type = OBJ_NONE; |
| c68b489e | 630 | unsigned long size; |
| 16235e3b | 631 | void *contents = NULL; |
| c68b489e | 632 | int eaten; |
| 31deb28f | 633 | struct object_info oi = OBJECT_INFO_INIT; |
| 7d70b29c | 634 | struct object_id real_oid = *null_oid(the_hash_algo); |
| 31deb28f ÆAB |
635 | int err = 0; |
| 636 | ||
| 31deb28f ÆAB |
637 | oi.sizep = &size; |
| 638 | oi.typep = &type; | |
| 639 | ||
| 0df00535 | 640 | if (read_loose_object(the_repository, path, oid, &real_oid, &contents, &oi) < 0) { |
| 96e41f58 ÆAB |
641 | if (contents && !oideq(&real_oid, oid)) |
| 642 | err = error(_("%s: hash-path mismatch, found at: %s"), | |
| 643 | oid_to_hex(&real_oid), path); | |
| 644 | else | |
| 645 | err = error(_("%s: object corrupt or missing: %s"), | |
| 646 | oid_to_hex(oid), path); | |
| 647 | } | |
| 31deb28f | 648 | if (err < 0) { |
| 7ac4f3a0 | 649 | errors_found |= ERROR_OBJECT; |
| 16235e3b | 650 | free(contents); |
| 7ac4f3a0 JK |
651 | return 0; /* keep checking other objects */ |
| 652 | } | |
| c68b489e JK |
653 | |
| 654 | if (!contents && type != OBJ_BLOB) | |
| 7ac4f3a0 | 655 | BUG("read_loose_object streamed a non-blob"); |
| c68b489e | 656 | |
| 1ec5bfd2 SB |
657 | obj = parse_object_buffer(the_repository, oid, type, size, |
| 658 | contents, &eaten); | |
| 659 | ||
| c68b489e JK |
660 | if (!obj) { |
| 661 | errors_found |= ERROR_OBJECT; | |
| 674ba340 | 662 | error(_("%s: object could not be parsed: %s"), |
| 76c1d9a0 | 663 | oid_to_hex(oid), path); |
| 7ac4f3a0 JK |
664 | if (!eaten) |
| 665 | free(contents); | |
| c68b489e JK |
666 | return 0; /* keep checking other objects */ |
| 667 | } | |
| 668 | ||
| 092c55d0 JT |
669 | obj->flags &= ~(REACHABLE | SEEN); |
| 670 | obj->flags |= HAS_OBJ; | |
| 7ac4f3a0 | 671 | if (fsck_obj(obj, contents, size)) |
| f0766bf9 | 672 | errors_found |= ERROR_OBJECT; |
| 7ac4f3a0 JK |
673 | |
| 674 | if (!eaten) | |
| 675 | free(contents); | |
| 676 | return 0; /* keep checking other objects, even if we saw an error */ | |
| f0766bf9 JK |
677 | } |
| 678 | ||
| be252d33 JK |
679 | static int fsck_cruft(const char *basename, const char *path, |
| 680 | void *data UNUSED) | |
| f0766bf9 JK |
681 | { |
| 682 | if (!starts_with(basename, "tmp_obj_")) | |
| 674ba340 | 683 | fprintf_ln(stderr, _("bad sha1 file: %s"), path); |
| f0766bf9 JK |
684 | return 0; |
| 685 | } | |
| 686 | ||
| be252d33 | 687 | static int fsck_subdir(unsigned int nr, const char *path UNUSED, void *data) |
| f0766bf9 | 688 | { |
| 31deb28f ÆAB |
689 | struct for_each_loose_cb *cb_data = data; |
| 690 | struct progress *progress = cb_data->progress; | |
| f0766bf9 JK |
691 | display_progress(progress, nr + 1); |
| 692 | return 0; | |
| 693 | } | |
| 694 | ||
| d81712ce | 695 | static void fsck_source(struct odb_source *source) |
| 8a498a05 | 696 | { |
| 1e49f22f | 697 | struct progress *progress = NULL; |
| 31deb28f | 698 | struct for_each_loose_cb cb_data = { |
| 31deb28f ÆAB |
699 | .progress = progress, |
| 700 | }; | |
| 20f1eb6b JS |
701 | |
| 702 | if (verbose) | |
| 674ba340 | 703 | fprintf_ln(stderr, _("Checking object directory")); |
| 20f1eb6b | 704 | |
| 1e49f22f | 705 | if (show_progress) |
| 1f7e6478 PS |
706 | progress = start_progress(the_repository, |
| 707 | _("Checking object directories"), 256); | |
| f0766bf9 | 708 | |
| d81712ce PS |
709 | for_each_loose_file_in_source(source, fsck_loose, |
| 710 | fsck_cruft, fsck_subdir, &cb_data); | |
| f0766bf9 | 711 | display_progress(progress, 256); |
| 1e49f22f | 712 | stop_progress(&progress); |
| 8a498a05 JH |
713 | } |
| 714 | ||
| b29759d8 NTND |
715 | static int fsck_head_link(const char *head_ref_name, |
| 716 | const char **head_points_at, | |
| a8c754d4 | 717 | struct object_id *head_oid) |
| c3330383 | 718 | { |
| 8eb2d0be | 719 | int null_is_error = 0; |
| 8eb2d0be | 720 | |
| 20f1eb6b | 721 | if (verbose) |
| 3813a89f | 722 | fprintf_ln(stderr, _("Checking %s link"), head_ref_name); |
| 20f1eb6b | 723 | |
| 2e5c4758 PS |
724 | *head_points_at = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), |
| 725 | head_ref_name, 0, head_oid, | |
| 726 | NULL); | |
| a8c754d4 | 727 | if (!*head_points_at) { |
| 122f76f5 | 728 | errors_found |= ERROR_REFS; |
| 3813a89f | 729 | return error(_("invalid %s"), head_ref_name); |
| 122f76f5 | 730 | } |
| b29759d8 | 731 | if (!strcmp(*head_points_at, head_ref_name)) |
| 8eb2d0be JH |
732 | /* detached HEAD */ |
| 733 | null_is_error = 1; | |
| a8c754d4 | 734 | else if (!starts_with(*head_points_at, "refs/heads/")) { |
| 122f76f5 | 735 | errors_found |= ERROR_REFS; |
| 3813a89f | 736 | return error(_("%s points to something strange (%s)"), |
| b29759d8 | 737 | head_ref_name, *head_points_at); |
| 122f76f5 | 738 | } |
| a8c754d4 | 739 | if (is_null_oid(head_oid)) { |
| 122f76f5 JH |
740 | if (null_is_error) { |
| 741 | errors_found |= ERROR_REFS; | |
| 3813a89f | 742 | return error(_("%s: detached HEAD points at nothing"), |
| b29759d8 | 743 | head_ref_name); |
| 122f76f5 | 744 | } |
| 3813a89f JH |
745 | fprintf_ln(stderr, |
| 746 | _("notice: %s points to an unborn branch (%s)"), | |
| 747 | head_ref_name, *head_points_at + 11); | |
| 8eb2d0be | 748 | } |
| c3330383 LT |
749 | return 0; |
| 750 | } | |
| 751 | ||
| 592ec63b | 752 | static int fsck_cache_tree(struct cache_tree *it, const char *index_path) |
| 53dc3f3e JH |
753 | { |
| 754 | int i; | |
| 755 | int err = 0; | |
| 756 | ||
| 20f1eb6b | 757 | if (verbose) |
| 592ec63b | 758 | fprintf_ln(stderr, _("Checking cache tree of %s"), index_path); |
| 20f1eb6b | 759 | |
| 53dc3f3e | 760 | if (0 <= it->entry_count) { |
| 109cd76d | 761 | struct object *obj = parse_object(the_repository, &it->oid); |
| 6d60bbef | 762 | if (!obj) { |
| 592ec63b JK |
763 | error(_("%s: invalid sha1 pointer in cache-tree of %s"), |
| 764 | oid_to_hex(&it->oid), index_path); | |
| 122f76f5 | 765 | errors_found |= ERROR_REFS; |
| 6d60bbef JH |
766 | return 1; |
| 767 | } | |
| 092c55d0 | 768 | obj->flags |= USED; |
| 73390290 | 769 | fsck_put_object_name(&fsck_walk_options, &it->oid, ":"); |
| a1cdc251 | 770 | mark_object_reachable(obj); |
| 1974632c | 771 | if (obj->type != OBJ_TREE) |
| 674ba340 | 772 | err |= objerror(obj, _("non-tree in cache-tree")); |
| 53dc3f3e JH |
773 | } |
| 774 | for (i = 0; i < it->subtree_nr; i++) | |
| 592ec63b | 775 | err |= fsck_cache_tree(it->down[i]->cache_tree, index_path); |
| 53dc3f3e JH |
776 | return err; |
| 777 | } | |
| 778 | ||
| 592ec63b JK |
779 | static int fsck_resolve_undo(struct index_state *istate, |
| 780 | const char *index_path) | |
| 5a5ea141 JH |
781 | { |
| 782 | struct string_list_item *item; | |
| 783 | struct string_list *resolve_undo = istate->resolve_undo; | |
| 784 | ||
| 785 | if (!resolve_undo) | |
| 786 | return 0; | |
| 787 | ||
| 788 | for_each_string_list_item(item, resolve_undo) { | |
| 789 | const char *path = item->string; | |
| 790 | struct resolve_undo_info *ru = item->util; | |
| 791 | int i; | |
| 792 | ||
| 793 | if (!ru) | |
| 794 | continue; | |
| 795 | for (i = 0; i < 3; i++) { | |
| 796 | struct object *obj; | |
| 797 | ||
| 798 | if (!ru->mode[i] || !S_ISREG(ru->mode[i])) | |
| 799 | continue; | |
| 800 | ||
| 801 | obj = parse_object(the_repository, &ru->oid[i]); | |
| 802 | if (!obj) { | |
| 592ec63b JK |
803 | error(_("%s: invalid sha1 pointer in resolve-undo of %s"), |
| 804 | oid_to_hex(&ru->oid[i]), | |
| 805 | index_path); | |
| 5a5ea141 | 806 | errors_found |= ERROR_REFS; |
| e0ad1397 | 807 | continue; |
| 5a5ea141 JH |
808 | } |
| 809 | obj->flags |= USED; | |
| 810 | fsck_put_object_name(&fsck_walk_options, &ru->oid[i], | |
| 811 | ":(%d):%s", i, path); | |
| 812 | mark_object_reachable(obj); | |
| 813 | } | |
| 814 | } | |
| 815 | return 0; | |
| 816 | } | |
| 817 | ||
| 592ec63b | 818 | static void fsck_index(struct index_state *istate, const char *index_path, |
| 6e6a529b | 819 | int is_current_worktree) |
| 8840069a JK |
820 | { |
| 821 | unsigned int i; | |
| 822 | ||
| 823 | /* TODO: audit for interaction with sparse-index. */ | |
| 824 | ensure_full_index(istate); | |
| 825 | for (i = 0; i < istate->cache_nr; i++) { | |
| 826 | unsigned int mode; | |
| 827 | struct blob *blob; | |
| 828 | struct object *obj; | |
| 829 | ||
| 830 | mode = istate->cache[i]->ce_mode; | |
| 831 | if (S_ISGITLINK(mode)) | |
| 832 | continue; | |
| 833 | blob = lookup_blob(the_repository, | |
| 834 | &istate->cache[i]->oid); | |
| 835 | if (!blob) | |
| 836 | continue; | |
| 837 | obj = &blob->object; | |
| 838 | obj->flags |= USED; | |
| 839 | fsck_put_object_name(&fsck_walk_options, &obj->oid, | |
| 592ec63b | 840 | "%s:%s", |
| 6e6a529b | 841 | is_current_worktree ? "" : index_path, |
| 592ec63b | 842 | istate->cache[i]->name); |
| 8840069a JK |
843 | mark_object_reachable(obj); |
| 844 | } | |
| 845 | if (istate->cache_tree) | |
| 592ec63b JK |
846 | fsck_cache_tree(istate->cache_tree, index_path); |
| 847 | fsck_resolve_undo(istate, index_path); | |
| 8840069a JK |
848 | } |
| 849 | ||
| 76c1d9a0 | 850 | static void mark_object_for_connectivity(const struct object_id *oid) |
| 3e3f8bd6 | 851 | { |
| 45a187cc | 852 | struct object *obj = lookup_unknown_object(the_repository, oid); |
| 3e3f8bd6 JK |
853 | obj->flags |= HAS_OBJ; |
| 854 | } | |
| 855 | ||
| 76c1d9a0 | 856 | static int mark_loose_for_connectivity(const struct object_id *oid, |
| be252d33 JK |
857 | const char *path UNUSED, |
| 858 | void *data UNUSED) | |
| 3e3f8bd6 | 859 | { |
| 76c1d9a0 | 860 | mark_object_for_connectivity(oid); |
| 3e3f8bd6 JK |
861 | return 0; |
| 862 | } | |
| 863 | ||
| 76c1d9a0 | 864 | static int mark_packed_for_connectivity(const struct object_id *oid, |
| be252d33 JK |
865 | struct packed_git *pack UNUSED, |
| 866 | uint32_t pos UNUSED, | |
| 867 | void *data UNUSED) | |
| 3e3f8bd6 | 868 | { |
| 76c1d9a0 | 869 | mark_object_for_connectivity(oid); |
| 3e3f8bd6 JK |
870 | return 0; |
| 871 | } | |
| 872 | ||
| 0d30feef DS |
873 | static int check_pack_rev_indexes(struct repository *r, int show_progress) |
| 874 | { | |
| 875 | struct progress *progress = NULL; | |
| 86d8c62f | 876 | struct packed_git *p; |
| 0d30feef DS |
877 | uint32_t pack_count = 0; |
| 878 | int res = 0; | |
| 879 | ||
| 880 | if (show_progress) { | |
| 86d8c62f | 881 | repo_for_each_pack(r, p) |
| 0d30feef | 882 | pack_count++; |
| 1f7e6478 PS |
883 | progress = start_delayed_progress(the_repository, |
| 884 | "Verifying reverse pack-indexes", pack_count); | |
| 0d30feef DS |
885 | pack_count = 0; |
| 886 | } | |
| 887 | ||
| 86d8c62f | 888 | repo_for_each_pack(r, p) { |
| 5a6072f6 DS |
889 | int load_error = load_pack_revindex_from_disk(p); |
| 890 | ||
| 891 | if (load_error < 0) { | |
| 892 | error(_("unable to load rev-index for pack '%s'"), p->pack_name); | |
| 893 | res = ERROR_PACK_REV_INDEX; | |
| 894 | } else if (!load_error && | |
| cf9cd8b5 | 895 | !load_pack_revindex(r, p) && |
| 5a6072f6 | 896 | verify_pack_revindex(p)) { |
| 0d30feef DS |
897 | error(_("invalid rev-index for pack '%s'"), p->pack_name); |
| 898 | res = ERROR_PACK_REV_INDEX; | |
| 899 | } | |
| 900 | display_progress(progress, ++pack_count); | |
| 901 | } | |
| 902 | stop_progress(&progress); | |
| 903 | ||
| 904 | return res; | |
| 905 | } | |
| 906 | ||
| c1cf918d | 907 | static void fsck_refs(struct repository *r) |
| 908 | { | |
| 909 | struct child_process refs_verify = CHILD_PROCESS_INIT; | |
| 910 | struct progress *progress = NULL; | |
| 911 | ||
| 912 | if (show_progress) | |
| 913 | progress = start_progress(r, _("Checking ref database"), 1); | |
| 914 | ||
| 915 | if (verbose) | |
| 916 | fprintf_ln(stderr, _("Checking ref database")); | |
| 917 | ||
| 918 | child_process_init(&refs_verify); | |
| 919 | refs_verify.git_cmd = 1; | |
| 920 | strvec_pushl(&refs_verify.args, "refs", "verify", NULL); | |
| 921 | if (verbose) | |
| 922 | strvec_push(&refs_verify.args, "--verbose"); | |
| 923 | if (check_strict) | |
| 924 | strvec_push(&refs_verify.args, "--strict"); | |
| 925 | ||
| 926 | if (run_command(&refs_verify)) | |
| 927 | errors_found |= ERROR_REFS; | |
| 928 | ||
| 929 | display_progress(progress, 1); | |
| 930 | stop_progress(&progress); | |
| 931 | } | |
| 932 | ||
| 5ac0a206 | 933 | static char const * const fsck_usage[] = { |
| d9054a19 ÆAB |
934 | N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n" |
| 935 | " [--[no-]full] [--strict] [--verbose] [--lost-found]\n" | |
| 936 | " [--[no-]dangling] [--[no-]progress] [--connectivity-only]\n" | |
| c1cf918d | 937 | " [--[no-]name-objects] [--[no-]references] [<object>...]"), |
| 5ac0a206 PH |
938 | NULL |
| 939 | }; | |
| 940 | ||
| 941 | static struct option fsck_opts[] = { | |
| cf8fe315 | 942 | OPT__VERBOSE(&verbose, N_("be verbose")), |
| d5d09d47 | 943 | OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")), |
| cf8fe315 | 944 | OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")), |
| d5d09d47 SB |
945 | OPT_BOOL(0, "tags", &show_tags, N_("report tags")), |
| 946 | OPT_BOOL(0, "root", &show_root, N_("report root nodes")), | |
| 947 | OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")), | |
| 948 | OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")), | |
| 949 | OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")), | |
| 02976bf8 | 950 | OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")), |
| d5d09d47 SB |
951 | OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")), |
| 952 | OPT_BOOL(0, "lost-found", &write_lost_and_found, | |
| cf8fe315 NTND |
953 | N_("write dangling objects in .git/lost-found")), |
| 954 | OPT_BOOL(0, "progress", &show_progress, N_("show progress")), | |
| 90cf590f | 955 | OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")), |
| c1cf918d | 956 | OPT_BOOL(0, "references", &check_references, N_("check reference database consistency")), |
| 5ac0a206 PH |
957 | OPT_END(), |
| 958 | }; | |
| e2b4f635 | 959 | |
| 9b1cb507 JC |
960 | int cmd_fsck(int argc, |
| 961 | const char **argv, | |
| 962 | const char *prefix, | |
| 963 | struct repository *repo UNUSED) | |
| 20222118 | 964 | { |
| 78e7b98f | 965 | int i; |
| a1e2581a | 966 | struct odb_source *source; |
| 20222118 | 967 | |
| 8b4c0103 JT |
968 | /* fsck knows how to handle missing promisor objects */ |
| 969 | fetch_if_missing = 0; | |
| 970 | ||
| e2b4f635 | 971 | errors_found = 0; |
| d24eda4e | 972 | disable_replace_refs(); |
| 069e4452 | 973 | save_commit_buffer = 0; |
| 61e2b015 | 974 | |
| 37782920 | 975 | argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0); |
| 1e49f22f | 976 | |
| 22410549 JS |
977 | fsck_walk_options.walk = mark_object; |
| 978 | fsck_obj_options.walk = mark_used; | |
| 8cd4a447 | 979 | fsck_obj_options.error_func = fsck_objects_error_func; |
| 22410549 JS |
980 | if (check_strict) |
| 981 | fsck_obj_options.strict = 1; | |
| 982 | ||
| 1e49f22f NTND |
983 | if (show_progress == -1) |
| 984 | show_progress = isatty(2); | |
| 985 | if (verbose) | |
| 986 | show_progress = 0; | |
| 987 | ||
| 5ac0a206 PH |
988 | if (write_lost_and_found) { |
| 989 | check_full = 1; | |
| 990 | include_reflogs = 0; | |
| 889262ea LT |
991 | } |
| 992 | ||
| 90cf590f | 993 | if (name_objects) |
| a59cfb32 | 994 | fsck_enable_object_names(&fsck_walk_options); |
| 90cf590f | 995 | |
| 9ce196e8 | 996 | repo_config(the_repository, git_fsck_config, &fsck_obj_options); |
| f30e4d85 | 997 | prepare_repo_settings(the_repository); |
| 2becf00f | 998 | |
| c1cf918d | 999 | if (check_references) |
| 1000 | fsck_refs(the_repository); | |
| 1001 | ||
| 3e3f8bd6 | 1002 | if (connectivity_only) { |
| d81712ce PS |
1003 | for_each_loose_object(the_repository->objects, |
| 1004 | mark_loose_for_connectivity, NULL, 0); | |
| c87910b9 KN |
1005 | for_each_packed_object(the_repository, |
| 1006 | mark_packed_for_connectivity, NULL, 0); | |
| 3e3f8bd6 | 1007 | } else { |
| c44185f6 | 1008 | odb_prepare_alternates(the_repository->objects); |
| a1e2581a | 1009 | for (source = the_repository->objects->sources; source; source = source->next) |
| d81712ce | 1010 | fsck_source(source); |
| e15ef669 | 1011 | |
| 3e3f8bd6 JK |
1012 | if (check_full) { |
| 1013 | struct packed_git *p; | |
| 1014 | uint32_t total = 0, count = 0; | |
| 1015 | struct progress *progress = NULL; | |
| e15ef669 | 1016 | |
| 3e3f8bd6 | 1017 | if (show_progress) { |
| 86d8c62f | 1018 | repo_for_each_pack(the_repository, p) { |
| 3e3f8bd6 JK |
1019 | if (open_pack_index(p)) |
| 1020 | continue; | |
| 1021 | total += p->num_objects; | |
| 1022 | } | |
| 1e49f22f | 1023 | |
| 1f7e6478 PS |
1024 | progress = start_progress(the_repository, |
| 1025 | _("Checking objects"), total); | |
| 3e3f8bd6 | 1026 | } |
| 86d8c62f PS |
1027 | |
| 1028 | repo_for_each_pack(the_repository, p) { | |
| 3e3f8bd6 | 1029 | /* verify gives error messages itself */ |
| 94e10825 NTND |
1030 | if (verify_pack(the_repository, |
| 1031 | p, fsck_obj_buffer, | |
| 3e3f8bd6 JK |
1032 | progress, count)) |
| 1033 | errors_found |= ERROR_PACK; | |
| 1034 | count += p->num_objects; | |
| 1e49f22f | 1035 | } |
| 3e3f8bd6 | 1036 | stop_progress(&progress); |
| 1e49f22f | 1037 | } |
| 1995b5e0 JK |
1038 | |
| 1039 | if (fsck_finish(&fsck_obj_options)) | |
| 1040 | errors_found |= ERROR_OBJECT; | |
| bcee6fd8 LT |
1041 | } |
| 1042 | ||
| 3aed2fda | 1043 | for (i = 0; i < argc; i++) { |
| a6080a0a | 1044 | const char *arg = argv[i]; |
| aca6065c | 1045 | struct object_id oid; |
| d850b7a5 | 1046 | if (!repo_get_oid(the_repository, arg, &oid)) { |
| 5abddd1e | 1047 | struct object *obj = lookup_object(the_repository, |
| d0229abd | 1048 | &oid); |
| e1a1388d | 1049 | |
| c2d17b3b | 1050 | if (!obj || !(obj->flags & HAS_OBJ)) { |
| c87910b9 | 1051 | if (is_promisor_object(the_repository, &oid)) |
| 096c9b8b | 1052 | continue; |
| 674ba340 | 1053 | error(_("%s: object missing"), oid_to_hex(&oid)); |
| c6c7b16d | 1054 | errors_found |= ERROR_OBJECT; |
| e1a1388d | 1055 | continue; |
| c6c7b16d | 1056 | } |
| e1a1388d | 1057 | |
| 092c55d0 | 1058 | obj->flags |= USED; |
| 73390290 | 1059 | fsck_put_object_name(&fsck_walk_options, &oid, |
| a59cfb32 | 1060 | "%s", arg); |
| 271b8d25 | 1061 | mark_object_reachable(obj); |
| d9839e03 LT |
1062 | continue; |
| 1063 | } | |
| 674ba340 | 1064 | error(_("invalid parameter: expected sha1, got '%s'"), arg); |
| c6c7b16d | 1065 | errors_found |= ERROR_OBJECT; |
| d9839e03 | 1066 | } |
| d9839e03 | 1067 | |
| 1024932f | 1068 | /* |
| d1af002d | 1069 | * If we've not been given any explicit head information, do the |
| e7bd907d LT |
1070 | * default ones from .git/refs. We also consider the index file |
| 1071 | * in this case (ie this implies --cache). | |
| 1024932f | 1072 | */ |
| c3271a0e | 1073 | if (!argc) { |
| 1024932f LT |
1074 | get_default_heads(); |
| 1075 | keep_cache_objects = 1; | |
| 1076 | } | |
| 1077 | ||
| ae7c0c92 | 1078 | if (keep_cache_objects) { |
| fb64ca52 JK |
1079 | struct worktree **worktrees, **p; |
| 1080 | ||
| a33fc72f | 1081 | verify_index_checksum = 1; |
| 00ec50e5 | 1082 | verify_ce_order = 1; |
| fb64ca52 JK |
1083 | |
| 1084 | worktrees = get_worktrees(); | |
| 1085 | for (p = worktrees; *p; p++) { | |
| 1086 | struct worktree *wt = *p; | |
| 1087 | struct index_state istate = | |
| 1088 | INDEX_STATE_INIT(the_repository); | |
| 8e4710f0 | 1089 | char *path, *wt_gitdir; |
| fb64ca52 | 1090 | |
| 592ec63b JK |
1091 | /* |
| 1092 | * Make a copy since the buffer is reusable | |
| 1093 | * and may get overwritten by other calls | |
| 1094 | * while we're examining the index. | |
| 1095 | */ | |
| a973f60d | 1096 | path = xstrdup(worktree_git_path(the_repository, wt, "index")); |
| 8e4710f0 PS |
1097 | wt_gitdir = get_worktree_git_dir(wt); |
| 1098 | ||
| 1099 | read_index_from(&istate, path, wt_gitdir); | |
| 8d3e7eac | 1100 | fsck_index(&istate, path, wt->is_current); |
| 8e4710f0 | 1101 | |
| fb64ca52 | 1102 | discard_index(&istate); |
| 8e4710f0 | 1103 | free(wt_gitdir); |
| 592ec63b | 1104 | free(path); |
| fb64ca52 JK |
1105 | } |
| 1106 | free_worktrees(worktrees); | |
| ae7c0c92 JH |
1107 | } |
| 1108 | ||
| 0d30feef | 1109 | errors_found |= check_pack_rev_indexes(the_repository, show_progress); |
| 756f1bcd DS |
1110 | if (verify_bitmap_files(the_repository)) |
| 1111 | errors_found |= ERROR_BITMAP; | |
| 0d30feef | 1112 | |
| 8ba0bbb2 | 1113 | check_connectivity(); |
| e0fd51e1 | 1114 | |
| f30e4d85 | 1115 | if (the_repository->settings.core_commit_graph) { |
| e0fd51e1 | 1116 | struct child_process commit_graph_verify = CHILD_PROCESS_INIT; |
| e0fd51e1 | 1117 | |
| c44185f6 | 1118 | odb_prepare_alternates(the_repository->objects); |
| a1e2581a | 1119 | for (source = the_repository->objects->sources; source; source = source->next) { |
| 4d0984be | 1120 | child_process_init(&commit_graph_verify); |
| 4d0984be | 1121 | commit_graph_verify.git_cmd = 1; |
| 2b709893 | 1122 | strvec_pushl(&commit_graph_verify.args, "commit-graph", |
| a1e2581a | 1123 | "verify", "--object-dir", source->path, NULL); |
| eda206f6 TB |
1124 | if (show_progress) |
| 1125 | strvec_push(&commit_graph_verify.args, "--progress"); | |
| 1126 | else | |
| 1127 | strvec_push(&commit_graph_verify.args, "--no-progress"); | |
| e0fd51e1 DS |
1128 | if (run_command(&commit_graph_verify)) |
| 1129 | errors_found |= ERROR_COMMIT_GRAPH; | |
| 1130 | } | |
| 1131 | } | |
| 1132 | ||
| dc557087 | 1133 | if (the_repository->settings.core_multi_pack_index) { |
| 66ec0390 | 1134 | struct child_process midx_verify = CHILD_PROCESS_INIT; |
| 66ec0390 | 1135 | |
| c44185f6 | 1136 | odb_prepare_alternates(the_repository->objects); |
| a1e2581a | 1137 | for (source = the_repository->objects->sources; source; source = source->next) { |
| 4d0984be | 1138 | child_process_init(&midx_verify); |
| 4d0984be | 1139 | midx_verify.git_cmd = 1; |
| 2b709893 | 1140 | strvec_pushl(&midx_verify.args, "multi-pack-index", |
| a1e2581a | 1141 | "verify", "--object-dir", source->path, NULL); |
| 39bdd303 TB |
1142 | if (show_progress) |
| 1143 | strvec_push(&midx_verify.args, "--progress"); | |
| 1144 | else | |
| 1145 | strvec_push(&midx_verify.args, "--no-progress"); | |
| 66ec0390 | 1146 | if (run_command(&midx_verify)) |
| e68a5272 | 1147 | errors_found |= ERROR_MULTI_PACK_INDEX; |
| 66ec0390 DS |
1148 | } |
| 1149 | } | |
| 1150 | ||
| e2b4f635 | 1151 | return errors_found; |
| 20222118 | 1152 | } |