git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
| 1 | #include "builtin.h" |
| 2 | #include "gettext.h" |
| 3 | #include "hex.h" |
| 4 | #include "config.h" |
| 5 | #include "commit.h" |
| 6 | #include "tree.h" |
| 7 | #include "blob.h" |
| 8 | #include "tag.h" |
| 9 | #include "refs.h" |
| 10 | #include "cache-tree.h" |
| 11 | #include "fsck.h" |
| 12 | #include "parse-options.h" |
| 13 | #include "progress.h" |
| 14 | #include "packfile.h" |
| 15 | #include "object-name.h" |
| 16 | #include "odb.h" |
| 17 | #include "odb/streaming.h" |
| 18 | #include "path.h" |
| 19 | #include "read-cache-ll.h" |
| 20 | #include "replace-object.h" |
| 21 | #include "resolve-undo.h" |
| 22 | #include "run-command.h" |
| 23 | #include "sparse-index.h" |
| 24 | #include "worktree.h" |
| 25 | |
| 26 | #define REACHABLE 0x0001 |
| 27 | #define SEEN 0x0002 |
| 28 | #define HAS_OBJ 0x0004 |
| 29 | /* This flag is set if something points to this object. */ |
| 30 | #define USED 0x0008 |
| 31 | |
| 32 | static int show_root; |
| 33 | static int show_tags; |
| 34 | static int show_unreachable; |
| 35 | static int include_reflogs = 1; |
| 36 | static int connectivity_only; |
| 37 | static int check_strict; |
| 38 | static struct fsck_options fsck_walk_options; |
| 39 | static struct fsck_options fsck_obj_options; |
| 40 | static int errors_found; |
| 41 | static int write_lost_and_found; |
| 42 | static int verbose; |
| 43 | static int show_progress = -1; |
| 44 | static int show_dangling = 1; |
| 45 | static timestamp_t now; |
| 46 | #define ERROR_OBJECT 01 |
| 47 | #define ERROR_REACHABLE 02 |
| 48 | #define ERROR_REFS 010 |
| 49 | #define ERROR_COMMIT_GRAPH 020 |
| 50 | |
| 51 | static const char *describe_object(const struct object_id *oid) |
| 52 | { |
| 53 | return fsck_describe_object(&fsck_walk_options, oid); |
| 54 | } |
| 55 | |
| 56 | static const char *printable_type(struct repository *repo, |
| 57 | const struct object_id *oid, |
| 58 | enum object_type type) |
| 59 | { |
| 60 | const char *ret; |
| 61 | |
| 62 | if (type == OBJ_NONE) |
| 63 | type = odb_read_object_info(repo->objects, oid, NULL); |
| 64 | |
| 65 | ret = type_name(type); |
| 66 | if (!ret) |
| 67 | ret = _("unknown"); |
| 68 | |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | static int objerror(struct repository *repo, struct object *obj, const char *err) |
| 73 | { |
| 74 | errors_found |= ERROR_OBJECT; |
| 75 | /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */ |
| 76 | fprintf_ln(stderr, _("error in %s %s: %s"), |
| 77 | printable_type(repo, &obj->oid, obj->type), |
| 78 | describe_object(&obj->oid), err); |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | static int fsck_objects_error_func(struct fsck_options *o, |
| 83 | void *fsck_report, |
| 84 | enum fsck_msg_type msg_type, |
| 85 | enum fsck_msg_id msg_id UNUSED, |
| 86 | const char *message) |
| 87 | { |
| 88 | struct fsck_object_report *report = fsck_report; |
| 89 | const struct object_id *oid = report->oid; |
| 90 | enum object_type object_type = report->object_type; |
| 91 | |
| 92 | switch (msg_type) { |
| 93 | case FSCK_WARN: |
| 94 | /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */ |
| 95 | fprintf_ln(stderr, _("warning in %s %s: %s"), |
| 96 | printable_type(o->repo, oid, object_type), |
| 97 | describe_object(oid), message); |
| 98 | return 0; |
| 99 | case FSCK_ERROR: |
| 100 | /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */ |
| 101 | fprintf_ln(stderr, _("error in %s %s: %s"), |
| 102 | printable_type(o->repo, oid, object_type), |
| 103 | describe_object(oid), message); |
| 104 | return 1; |
| 105 | default: |
| 106 | BUG("%d (FSCK_IGNORE?) should never trigger this callback", |
| 107 | msg_type); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | static struct object_array pending; |
| 112 | |
| 113 | static int mark_object(struct object *obj, enum object_type type, |
| 114 | void *data, struct fsck_options *options) |
| 115 | { |
| 116 | struct object *parent = data; |
| 117 | |
| 118 | /* |
| 119 | * The only case data is NULL or type is OBJ_ANY is when |
| 120 | * mark_object_reachable() calls us. All the callers of |
| 121 | * that function has non-NULL obj hence ... |
| 122 | */ |
| 123 | if (!obj) { |
| 124 | /* ... these references to parent->fld are safe here */ |
| 125 | printf_ln(_("broken link from %7s %s"), |
| 126 | printable_type(options->repo, &parent->oid, parent->type), |
| 127 | describe_object(&parent->oid)); |
| 128 | printf_ln(_("broken link from %7s %s"), |
| 129 | (type == OBJ_ANY ? _("unknown") : type_name(type)), |
| 130 | _("unknown")); |
| 131 | errors_found |= ERROR_REACHABLE; |
| 132 | return 1; |
| 133 | } |
| 134 | |
| 135 | if (type != OBJ_ANY && obj->type != type) |
| 136 | /* ... and the reference to parent is safe here */ |
| 137 | objerror(options->repo, parent, _("wrong object type in link")); |
| 138 | |
| 139 | if (obj->flags & REACHABLE) |
| 140 | return 0; |
| 141 | obj->flags |= REACHABLE; |
| 142 | |
| 143 | if (is_promisor_object(options->repo, &obj->oid)) |
| 144 | /* |
| 145 | * Further recursion does not need to be performed on this |
| 146 | * object since it is a promisor object (so it does not need to |
| 147 | * be added to "pending"). |
| 148 | */ |
| 149 | return 0; |
| 150 | |
| 151 | if (!(obj->flags & HAS_OBJ)) { |
| 152 | if (parent && !odb_has_object(options->repo->objects, &obj->oid, |
| 153 | ODB_HAS_OBJECT_RECHECK_PACKED)) { |
| 154 | printf_ln(_("broken link from %7s %s\n" |
| 155 | " to %7s %s"), |
| 156 | printable_type(options->repo, &parent->oid, parent->type), |
| 157 | describe_object(&parent->oid), |
| 158 | printable_type(options->repo, &obj->oid, obj->type), |
| 159 | describe_object(&obj->oid)); |
| 160 | errors_found |= ERROR_REACHABLE; |
| 161 | } |
| 162 | return 1; |
| 163 | } |
| 164 | |
| 165 | add_object_array(obj, NULL, &pending); |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static void mark_object_reachable(struct object *obj) |
| 170 | { |
| 171 | mark_object(obj, OBJ_ANY, NULL, &fsck_walk_options); |
| 172 | } |
| 173 | |
| 174 | static int traverse_one_object(struct object *obj) |
| 175 | { |
| 176 | int result = fsck_walk(obj, obj, &fsck_walk_options); |
| 177 | |
| 178 | if (obj->type == OBJ_TREE) { |
| 179 | struct tree *tree = (struct tree *)obj; |
| 180 | free_tree_buffer(tree); |
| 181 | } |
| 182 | return result; |
| 183 | } |
| 184 | |
| 185 | static int traverse_reachable(struct repository *repo) |
| 186 | { |
| 187 | struct progress *progress = NULL; |
| 188 | unsigned int nr = 0; |
| 189 | int result = 0; |
| 190 | if (show_progress) |
| 191 | progress = start_delayed_progress(repo, |
| 192 | _("Checking connectivity"), 0); |
| 193 | while (pending.nr) { |
| 194 | result |= traverse_one_object(object_array_pop(&pending)); |
| 195 | display_progress(progress, ++nr); |
| 196 | } |
| 197 | stop_progress(&progress); |
| 198 | return !!result; |
| 199 | } |
| 200 | |
| 201 | static int mark_used(struct object *obj, enum object_type type UNUSED, |
| 202 | void *data UNUSED, struct fsck_options *options UNUSED) |
| 203 | { |
| 204 | if (!obj) |
| 205 | return 1; |
| 206 | obj->flags |= USED; |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static int mark_unreachable_referents(const struct object_id *oid, |
| 211 | struct object_info *oi UNUSED, |
| 212 | void *data) |
| 213 | { |
| 214 | struct repository *repo = data; |
| 215 | struct fsck_options options; |
| 216 | struct object *obj = lookup_object(data, oid); |
| 217 | |
| 218 | if (!obj || !(obj->flags & HAS_OBJ)) |
| 219 | return 0; /* not part of our original set */ |
| 220 | if (obj->flags & REACHABLE) |
| 221 | return 0; /* reachable objects already traversed */ |
| 222 | |
| 223 | /* |
| 224 | * Avoid passing OBJ_NONE to fsck_walk, which will parse the object |
| 225 | * (and we want to avoid parsing blobs). |
| 226 | */ |
| 227 | if (obj->type == OBJ_NONE) { |
| 228 | enum object_type type = odb_read_object_info(repo->objects, |
| 229 | &obj->oid, NULL); |
| 230 | if (type > 0) |
| 231 | object_as_type(obj, type, 0); |
| 232 | } |
| 233 | |
| 234 | fsck_options_init(&options, repo, FSCK_OPTIONS_DEFAULT); |
| 235 | options.walk = mark_used; |
| 236 | fsck_walk(obj, NULL, &options); |
| 237 | if (obj->type == OBJ_TREE) |
| 238 | free_tree_buffer((struct tree *)obj); |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Check a single reachable object |
| 245 | */ |
| 246 | static void check_reachable_object(struct repository *repo, struct object *obj) |
| 247 | { |
| 248 | /* |
| 249 | * We obviously want the object to be parsed, |
| 250 | * except if it was in a pack-file and we didn't |
| 251 | * do a full fsck |
| 252 | */ |
| 253 | if (!(obj->flags & HAS_OBJ)) { |
| 254 | if (is_promisor_object(repo, &obj->oid)) |
| 255 | return; |
| 256 | if (has_object_pack(repo, &obj->oid)) |
| 257 | return; /* it is in pack - forget about it */ |
| 258 | printf_ln(_("missing %s %s"), |
| 259 | printable_type(repo, &obj->oid, obj->type), |
| 260 | describe_object(&obj->oid)); |
| 261 | errors_found |= ERROR_REACHABLE; |
| 262 | return; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * Check a single unreachable object |
| 268 | */ |
| 269 | static void check_unreachable_object(struct repository *repo, struct object *obj) |
| 270 | { |
| 271 | /* |
| 272 | * Missing unreachable object? Ignore it. It's not like |
| 273 | * we miss it (since it can't be reached), nor do we want |
| 274 | * to complain about it being unreachable (since it does |
| 275 | * not exist). |
| 276 | */ |
| 277 | if (!(obj->flags & HAS_OBJ)) |
| 278 | return; |
| 279 | |
| 280 | /* |
| 281 | * Unreachable object that exists? Show it if asked to, |
| 282 | * since this is something that is prunable. |
| 283 | */ |
| 284 | if (show_unreachable) { |
| 285 | printf_ln(_("unreachable %s %s"), |
| 286 | printable_type(repo, &obj->oid, obj->type), |
| 287 | describe_object(&obj->oid)); |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * "!USED" means that nothing at all points to it, including |
| 293 | * other unreachable objects. In other words, it's the "tip" |
| 294 | * of some set of unreachable objects, usually a commit that |
| 295 | * got dropped. |
| 296 | * |
| 297 | * Such starting points are more interesting than some random |
| 298 | * set of unreachable objects, so we show them even if the user |
| 299 | * hasn't asked for _all_ unreachable objects. If you have |
| 300 | * deleted a branch by mistake, this is a prime candidate to |
| 301 | * start looking at, for example. |
| 302 | */ |
| 303 | if (!(obj->flags & USED)) { |
| 304 | if (show_dangling) |
| 305 | printf_ln(_("dangling %s %s"), |
| 306 | printable_type(repo, &obj->oid, obj->type), |
| 307 | describe_object(&obj->oid)); |
| 308 | if (write_lost_and_found) { |
| 309 | char *filename = repo_git_path(repo, "lost-found/%s/%s", |
| 310 | obj->type == OBJ_COMMIT ? "commit" : "other", |
| 311 | describe_object(&obj->oid)); |
| 312 | FILE *f; |
| 313 | |
| 314 | if (safe_create_leading_directories_const(repo, filename)) { |
| 315 | error(_("could not create lost-found")); |
| 316 | free(filename); |
| 317 | return; |
| 318 | } |
| 319 | f = xfopen(filename, "w"); |
| 320 | if (obj->type == OBJ_BLOB) { |
| 321 | if (odb_stream_blob_to_fd(repo->objects, fileno(f), |
| 322 | &obj->oid, NULL, 1)) |
| 323 | die_errno(_("could not write '%s'"), filename); |
| 324 | } else |
| 325 | fprintf(f, "%s\n", describe_object(&obj->oid)); |
| 326 | if (fclose(f)) |
| 327 | die_errno(_("could not finish '%s'"), |
| 328 | filename); |
| 329 | free(filename); |
| 330 | } |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * Otherwise? It's there, it's unreachable, and some other unreachable |
| 336 | * object points to it. Ignore it - it's not interesting, and we showed |
| 337 | * all the interesting cases above. |
| 338 | */ |
| 339 | } |
| 340 | |
| 341 | static void check_object(struct repository *repo, struct object *obj) |
| 342 | { |
| 343 | if (verbose) |
| 344 | fprintf_ln(stderr, _("Checking %s"), describe_object(&obj->oid)); |
| 345 | |
| 346 | if (obj->flags & REACHABLE) |
| 347 | check_reachable_object(repo, obj); |
| 348 | else |
| 349 | check_unreachable_object(repo, obj); |
| 350 | } |
| 351 | |
| 352 | static void check_connectivity(struct repository *repo) |
| 353 | { |
| 354 | int i, max; |
| 355 | |
| 356 | /* Traverse the pending reachable objects */ |
| 357 | traverse_reachable(repo); |
| 358 | |
| 359 | /* |
| 360 | * With --connectivity-only, we won't have actually opened and marked |
| 361 | * unreachable objects with USED. Do that now to make --dangling, etc |
| 362 | * accurate. |
| 363 | */ |
| 364 | if (connectivity_only && (show_dangling || write_lost_and_found)) { |
| 365 | /* |
| 366 | * Even though we already have a "struct object" for each of |
| 367 | * these in memory, we must not iterate over the internal |
| 368 | * object hash as we do below. Our loop would potentially |
| 369 | * resize the hash, making our iteration invalid. |
| 370 | * |
| 371 | * Instead, we'll just go back to the source list of objects, |
| 372 | * and ignore any that weren't present in our earlier |
| 373 | * traversal. |
| 374 | */ |
| 375 | odb_for_each_object(repo->objects, NULL, |
| 376 | mark_unreachable_referents, repo, 0); |
| 377 | } |
| 378 | |
| 379 | /* Look up all the requirements, warn about missing objects.. */ |
| 380 | max = get_max_object_index(repo); |
| 381 | if (verbose) |
| 382 | fprintf_ln(stderr, Q_("Checking connectivity (%d object)", |
| 383 | "Checking connectivity (%d objects)", |
| 384 | max), |
| 385 | max); |
| 386 | |
| 387 | for (i = 0; i < max; i++) { |
| 388 | struct object *obj = get_indexed_object(repo, i); |
| 389 | |
| 390 | if (obj) |
| 391 | check_object(repo, obj); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | static int fsck_obj_buffer(const struct object_id *oid, enum object_type type, |
| 396 | unsigned long size, void *buffer, int *eaten, void *cb_data) |
| 397 | { |
| 398 | struct repository *repo = cb_data; |
| 399 | struct object *obj; |
| 400 | int err; |
| 401 | |
| 402 | /* |
| 403 | * Note, buffer may be NULL if type is OBJ_BLOB. See |
| 404 | * verify_packfile(), data_valid variable for details. |
| 405 | */ |
| 406 | obj = parse_object_buffer(repo, oid, type, size, buffer, eaten); |
| 407 | if (!obj) { |
| 408 | errors_found |= ERROR_OBJECT; |
| 409 | err = error(_("%s: object corrupt or missing"), |
| 410 | oid_to_hex(oid)); |
| 411 | goto out; |
| 412 | } |
| 413 | |
| 414 | obj->flags &= ~REACHABLE; |
| 415 | obj->flags |= HAS_OBJ | SEEN; |
| 416 | |
| 417 | if (verbose) |
| 418 | fprintf_ln(stderr, _("Checking %s %s"), |
| 419 | printable_type(repo, &obj->oid, obj->type), |
| 420 | describe_object(&obj->oid)); |
| 421 | |
| 422 | if (fsck_walk(obj, NULL, &fsck_obj_options)) |
| 423 | objerror(repo, obj, _("broken links")); |
| 424 | |
| 425 | err = fsck_object(obj, buffer, size, &fsck_obj_options); |
| 426 | if (err) |
| 427 | goto out; |
| 428 | |
| 429 | if (obj->type == OBJ_COMMIT) { |
| 430 | struct commit *commit = (struct commit *) obj; |
| 431 | |
| 432 | if (!commit->parents && show_root) |
| 433 | printf_ln(_("root %s"), |
| 434 | describe_object(&commit->object.oid)); |
| 435 | } |
| 436 | |
| 437 | if (obj->type == OBJ_TAG) { |
| 438 | struct tag *tag = (struct tag *) obj; |
| 439 | |
| 440 | if (show_tags && tag->tagged) { |
| 441 | printf_ln(_("tagged %s %s (%s) in %s"), |
| 442 | printable_type(repo, &tag->tagged->oid, tag->tagged->type), |
| 443 | describe_object(&tag->tagged->oid), |
| 444 | tag->tag, |
| 445 | describe_object(&tag->object.oid)); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | out: |
| 450 | if (obj && obj->type == OBJ_TREE) |
| 451 | free_tree_buffer((struct tree *)obj); |
| 452 | return err; |
| 453 | } |
| 454 | |
| 455 | static int default_refs; |
| 456 | |
| 457 | static void fsck_handle_reflog_oid(struct repository *repo, |
| 458 | const char *refname, struct object_id *oid, |
| 459 | timestamp_t timestamp) |
| 460 | { |
| 461 | struct object *obj; |
| 462 | |
| 463 | if (!is_null_oid(oid)) { |
| 464 | obj = lookup_object(repo, oid); |
| 465 | if (obj && (obj->flags & HAS_OBJ)) { |
| 466 | if (timestamp) |
| 467 | fsck_put_object_name(&fsck_walk_options, oid, |
| 468 | "%s@{%"PRItime"}", |
| 469 | refname, timestamp); |
| 470 | obj->flags |= USED; |
| 471 | mark_object_reachable(obj); |
| 472 | } else if (!is_promisor_object(repo, oid)) { |
| 473 | error(_("%s: invalid reflog entry %s"), |
| 474 | refname, oid_to_hex(oid)); |
| 475 | errors_found |= ERROR_REACHABLE; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | static int fsck_handle_reflog_ent(const char *refname, |
| 481 | struct object_id *ooid, struct object_id *noid, |
| 482 | const char *email UNUSED, |
| 483 | timestamp_t timestamp, int tz UNUSED, |
| 484 | const char *message UNUSED, void *cb_data) |
| 485 | { |
| 486 | struct repository *repo = cb_data; |
| 487 | |
| 488 | if (now && timestamp > now) |
| 489 | return 0; |
| 490 | |
| 491 | if (verbose) |
| 492 | fprintf_ln(stderr, _("Checking reflog %s->%s"), |
| 493 | oid_to_hex(ooid), oid_to_hex(noid)); |
| 494 | |
| 495 | fsck_handle_reflog_oid(repo, refname, ooid, 0); |
| 496 | fsck_handle_reflog_oid(repo, refname, noid, timestamp); |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | static int fsck_handle_reflog(const char *logname, void *cb_data) |
| 501 | { |
| 502 | struct strbuf refname = STRBUF_INIT; |
| 503 | struct worktree *wt = cb_data; |
| 504 | |
| 505 | strbuf_worktree_ref(wt, &refname, logname); |
| 506 | refs_for_each_reflog_ent(get_main_ref_store(wt->repo), |
| 507 | refname.buf, fsck_handle_reflog_ent, |
| 508 | wt->repo); |
| 509 | strbuf_release(&refname); |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | struct ref_snapshot { |
| 514 | char *refname; |
| 515 | struct object_id oid; |
| 516 | /* TODO: Maybe supplement with latest reflog entry info too? */ |
| 517 | }; |
| 518 | |
| 519 | struct snapshot { |
| 520 | size_t nr; |
| 521 | size_t alloc; |
| 522 | struct ref_snapshot *ref; |
| 523 | /* TODO: Consider also snapshotting the index of each worktree. */ |
| 524 | }; |
| 525 | |
| 526 | struct snapshot_ref_data { |
| 527 | struct repository *repo; |
| 528 | struct snapshot *snap; |
| 529 | }; |
| 530 | |
| 531 | static int snapshot_ref(const struct reference *ref, void *cb_data) |
| 532 | { |
| 533 | struct snapshot_ref_data *data = cb_data; |
| 534 | struct snapshot *snap = data->snap; |
| 535 | struct object *obj; |
| 536 | |
| 537 | obj = parse_object(data->repo, ref->oid); |
| 538 | if (!obj) { |
| 539 | if (is_promisor_object(data->repo, ref->oid)) { |
| 540 | /* |
| 541 | * Increment default_refs anyway, because this is a |
| 542 | * valid ref. |
| 543 | */ |
| 544 | default_refs++; |
| 545 | return 0; |
| 546 | } |
| 547 | error(_("%s: invalid sha1 pointer %s"), |
| 548 | ref->name, oid_to_hex(ref->oid)); |
| 549 | errors_found |= ERROR_REACHABLE; |
| 550 | /* We'll continue with the rest despite the error.. */ |
| 551 | return 0; |
| 552 | } |
| 553 | if (obj->type != OBJ_COMMIT && is_branch(ref->name)) { |
| 554 | error(_("%s: not a commit"), ref->name); |
| 555 | errors_found |= ERROR_REFS; |
| 556 | } |
| 557 | default_refs++; |
| 558 | |
| 559 | ALLOC_GROW(snap->ref, snap->nr + 1, snap->alloc); |
| 560 | snap->ref[snap->nr].refname = xstrdup(ref->name); |
| 561 | oidcpy(&snap->ref[snap->nr].oid, ref->oid); |
| 562 | snap->nr++; |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | static int fsck_handle_ref(const struct reference *ref, void *cb_data) |
| 568 | { |
| 569 | struct repository *repo = cb_data; |
| 570 | struct object *obj; |
| 571 | |
| 572 | obj = parse_object(repo, ref->oid); |
| 573 | obj->flags |= USED; |
| 574 | fsck_put_object_name(&fsck_walk_options, |
| 575 | ref->oid, "%s", ref->name); |
| 576 | mark_object_reachable(obj); |
| 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | static void snapshot_refs(struct repository *repo, |
| 582 | struct snapshot *snap, int argc, const char **argv) |
| 583 | { |
| 584 | struct refs_for_each_ref_options opts = { |
| 585 | .flags = REFS_FOR_EACH_INCLUDE_BROKEN, |
| 586 | }; |
| 587 | struct snapshot_ref_data data = { |
| 588 | .repo = repo, |
| 589 | .snap = snap, |
| 590 | }; |
| 591 | struct worktree **worktrees, **p; |
| 592 | const char *head_points_at; |
| 593 | struct object_id head_oid; |
| 594 | |
| 595 | for (int i = 0; i < argc; i++) { |
| 596 | const char *arg = argv[i]; |
| 597 | struct object_id oid; |
| 598 | if (!repo_get_oid(repo, arg, &oid)) { |
| 599 | struct reference ref = { |
| 600 | .name = arg, |
| 601 | .oid = &oid, |
| 602 | }; |
| 603 | |
| 604 | snapshot_ref(&ref, &data); |
| 605 | continue; |
| 606 | } |
| 607 | error(_("invalid parameter: expected sha1, got '%s'"), arg); |
| 608 | errors_found |= ERROR_OBJECT; |
| 609 | } |
| 610 | |
| 611 | if (argc) { |
| 612 | include_reflogs = 0; |
| 613 | return; |
| 614 | } |
| 615 | |
| 616 | refs_for_each_ref_ext(get_main_ref_store(repo), |
| 617 | snapshot_ref, &data, &opts); |
| 618 | |
| 619 | worktrees = get_worktrees(repo); |
| 620 | for (p = worktrees; *p; p++) { |
| 621 | struct worktree *wt = *p; |
| 622 | struct strbuf refname = STRBUF_INIT; |
| 623 | |
| 624 | strbuf_worktree_ref(wt, &refname, "HEAD"); |
| 625 | |
| 626 | head_points_at = refs_resolve_ref_unsafe(get_main_ref_store(repo), |
| 627 | refname.buf, 0, &head_oid, NULL); |
| 628 | |
| 629 | if (head_points_at && !is_null_oid(&head_oid)) { |
| 630 | struct reference ref = { |
| 631 | .name = refname.buf, |
| 632 | .oid = &head_oid, |
| 633 | }; |
| 634 | |
| 635 | snapshot_ref(&ref, &data); |
| 636 | } |
| 637 | strbuf_release(&refname); |
| 638 | |
| 639 | /* |
| 640 | * TODO: Could use refs_for_each_reflog(...) to find |
| 641 | * latest entry instead of using a global 'now' for that |
| 642 | * purpose. |
| 643 | */ |
| 644 | } |
| 645 | free_worktrees(worktrees); |
| 646 | |
| 647 | /* Ignore reflogs newer than now */ |
| 648 | now = time(NULL); |
| 649 | } |
| 650 | |
| 651 | |
| 652 | static void free_snapshot_refs(struct snapshot *snap) |
| 653 | { |
| 654 | for (size_t i = 0; i < snap->nr; i++) |
| 655 | free(snap->ref[i].refname); |
| 656 | free(snap->ref); |
| 657 | } |
| 658 | |
| 659 | static void process_refs(struct repository *repo, struct snapshot *snap) |
| 660 | { |
| 661 | struct worktree **worktrees, **p; |
| 662 | |
| 663 | for (size_t i = 0; i < snap->nr; i++) { |
| 664 | struct reference ref = { |
| 665 | .name = snap->ref[i].refname, |
| 666 | .oid = &snap->ref[i].oid, |
| 667 | }; |
| 668 | fsck_handle_ref(&ref, repo); |
| 669 | } |
| 670 | |
| 671 | if (include_reflogs) { |
| 672 | worktrees = get_worktrees(repo); |
| 673 | for (p = worktrees; *p; p++) { |
| 674 | struct worktree *wt = *p; |
| 675 | |
| 676 | refs_for_each_reflog(get_worktree_ref_store(wt), |
| 677 | fsck_handle_reflog, wt); |
| 678 | } |
| 679 | free_worktrees(worktrees); |
| 680 | } |
| 681 | |
| 682 | /* |
| 683 | * Not having any default heads isn't really fatal, but |
| 684 | * it does mean that "--unreachable" no longer makes any |
| 685 | * sense (since in this case everything will obviously |
| 686 | * be unreachable by definition. |
| 687 | * |
| 688 | * Showing dangling objects is valid, though (as those |
| 689 | * dangling objects are likely lost heads). |
| 690 | * |
| 691 | * So we just print a warning about it, and clear the |
| 692 | * "show_unreachable" flag. |
| 693 | */ |
| 694 | if (!default_refs) { |
| 695 | fprintf_ln(stderr, _("notice: No default references")); |
| 696 | show_unreachable = 0; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | static int fsck_cache_tree(struct repository *repo, struct cache_tree *it, const char *index_path) |
| 701 | { |
| 702 | int i; |
| 703 | int err = 0; |
| 704 | |
| 705 | if (verbose) |
| 706 | fprintf_ln(stderr, _("Checking cache tree of %s"), index_path); |
| 707 | |
| 708 | if (0 <= it->entry_count) { |
| 709 | struct object *obj = parse_object(repo, &it->oid); |
| 710 | if (!obj) { |
| 711 | error(_("%s: invalid sha1 pointer in cache-tree of %s"), |
| 712 | oid_to_hex(&it->oid), index_path); |
| 713 | errors_found |= ERROR_REFS; |
| 714 | return 1; |
| 715 | } |
| 716 | obj->flags |= USED; |
| 717 | fsck_put_object_name(&fsck_walk_options, &it->oid, ":"); |
| 718 | mark_object_reachable(obj); |
| 719 | if (obj->type != OBJ_TREE) |
| 720 | err |= objerror(repo, obj, _("non-tree in cache-tree")); |
| 721 | } |
| 722 | for (i = 0; i < it->subtree_nr; i++) |
| 723 | err |= fsck_cache_tree(repo, it->down[i]->cache_tree, index_path); |
| 724 | return err; |
| 725 | } |
| 726 | |
| 727 | static int fsck_resolve_undo(struct index_state *istate, |
| 728 | const char *index_path) |
| 729 | { |
| 730 | struct string_list_item *item; |
| 731 | struct string_list *resolve_undo = istate->resolve_undo; |
| 732 | |
| 733 | if (!resolve_undo) |
| 734 | return 0; |
| 735 | |
| 736 | for_each_string_list_item(item, resolve_undo) { |
| 737 | const char *path = item->string; |
| 738 | struct resolve_undo_info *ru = item->util; |
| 739 | int i; |
| 740 | |
| 741 | if (!ru) |
| 742 | continue; |
| 743 | for (i = 0; i < 3; i++) { |
| 744 | struct object *obj; |
| 745 | |
| 746 | if (!ru->mode[i] || !S_ISREG(ru->mode[i])) |
| 747 | continue; |
| 748 | |
| 749 | obj = parse_object(istate->repo, &ru->oid[i]); |
| 750 | if (!obj) { |
| 751 | error(_("%s: invalid sha1 pointer in resolve-undo of %s"), |
| 752 | oid_to_hex(&ru->oid[i]), |
| 753 | index_path); |
| 754 | errors_found |= ERROR_REFS; |
| 755 | continue; |
| 756 | } |
| 757 | obj->flags |= USED; |
| 758 | fsck_put_object_name(&fsck_walk_options, &ru->oid[i], |
| 759 | ":(%d):%s", i, path); |
| 760 | mark_object_reachable(obj); |
| 761 | } |
| 762 | } |
| 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | static void fsck_index(struct index_state *istate, const char *index_path, |
| 767 | int is_current_worktree) |
| 768 | { |
| 769 | unsigned int i; |
| 770 | |
| 771 | /* TODO: audit for interaction with sparse-index. */ |
| 772 | ensure_full_index(istate); |
| 773 | for (i = 0; i < istate->cache_nr; i++) { |
| 774 | unsigned int mode; |
| 775 | struct blob *blob; |
| 776 | struct object *obj; |
| 777 | |
| 778 | mode = istate->cache[i]->ce_mode; |
| 779 | if (S_ISGITLINK(mode)) |
| 780 | continue; |
| 781 | blob = lookup_blob(istate->repo, |
| 782 | &istate->cache[i]->oid); |
| 783 | if (!blob) |
| 784 | continue; |
| 785 | obj = &blob->object; |
| 786 | obj->flags |= USED; |
| 787 | fsck_put_object_name(&fsck_walk_options, &obj->oid, |
| 788 | "%s:%s", |
| 789 | is_current_worktree ? "" : index_path, |
| 790 | istate->cache[i]->name); |
| 791 | mark_object_reachable(obj); |
| 792 | } |
| 793 | if (istate->cache_tree) |
| 794 | fsck_cache_tree(istate->repo, istate->cache_tree, index_path); |
| 795 | fsck_resolve_undo(istate, index_path); |
| 796 | } |
| 797 | |
| 798 | static int mark_object_for_connectivity(const struct object_id *oid, |
| 799 | struct object_info *oi UNUSED, |
| 800 | void *cb_data) |
| 801 | { |
| 802 | struct repository *repo = cb_data; |
| 803 | struct object *obj = lookup_unknown_object(repo, oid); |
| 804 | obj->flags |= HAS_OBJ; |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | static void fsck_refs(struct repository *r) |
| 809 | { |
| 810 | struct child_process refs_verify = CHILD_PROCESS_INIT; |
| 811 | struct progress *progress = NULL; |
| 812 | |
| 813 | if (show_progress) |
| 814 | progress = start_progress(r, _("Checking ref database"), 1); |
| 815 | |
| 816 | if (verbose) |
| 817 | fprintf_ln(stderr, _("Checking ref database")); |
| 818 | |
| 819 | child_process_init(&refs_verify); |
| 820 | refs_verify.git_cmd = 1; |
| 821 | strvec_pushl(&refs_verify.args, "refs", "verify", NULL); |
| 822 | if (verbose) |
| 823 | strvec_push(&refs_verify.args, "--verbose"); |
| 824 | if (check_strict) |
| 825 | strvec_push(&refs_verify.args, "--strict"); |
| 826 | |
| 827 | if (run_command(&refs_verify)) |
| 828 | errors_found |= ERROR_REFS; |
| 829 | |
| 830 | display_progress(progress, 1); |
| 831 | stop_progress(&progress); |
| 832 | } |
| 833 | |
| 834 | static char const * const fsck_usage[] = { |
| 835 | N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n" |
| 836 | " [--[no-]full] [--strict] [--verbose] [--lost-found]\n" |
| 837 | " [--[no-]dangling] [--[no-]progress] [--connectivity-only]\n" |
| 838 | " [--[no-]name-objects] [--[no-]references] [<object>...]"), |
| 839 | NULL |
| 840 | }; |
| 841 | |
| 842 | int cmd_fsck(int argc, |
| 843 | const char **argv, |
| 844 | const char *prefix, |
| 845 | struct repository *repo) |
| 846 | { |
| 847 | struct odb_fsck_options odb_fsck_opts = { |
| 848 | .flags = ODB_FSCK_FULL, |
| 849 | .object_cb = fsck_obj_buffer, |
| 850 | .object_payload = repo, |
| 851 | }; |
| 852 | int keep_cache_objects = 0; |
| 853 | int name_objects = 0; |
| 854 | int check_references = 1; |
| 855 | struct option fsck_opts[] = { |
| 856 | OPT__VERBOSE(&verbose, N_("be verbose")), |
| 857 | OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")), |
| 858 | OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")), |
| 859 | OPT_BOOL(0, "tags", &show_tags, N_("report tags")), |
| 860 | OPT_BOOL(0, "root", &show_root, N_("report root nodes")), |
| 861 | OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")), |
| 862 | OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")), |
| 863 | OPT_BIT(0, "full", &odb_fsck_opts.flags, |
| 864 | N_("also consider packs and alternate objects"), ODB_FSCK_FULL), |
| 865 | OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")), |
| 866 | OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")), |
| 867 | OPT_BOOL(0, "lost-found", &write_lost_and_found, |
| 868 | N_("write dangling objects in .git/lost-found")), |
| 869 | OPT_BOOL(0, "progress", &show_progress, N_("show progress")), |
| 870 | OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")), |
| 871 | OPT_BOOL(0, "references", &check_references, N_("check reference database consistency")), |
| 872 | OPT_END(), |
| 873 | }; |
| 874 | struct snapshot snap = { |
| 875 | .nr = 0, |
| 876 | .alloc = 0, |
| 877 | .ref = NULL |
| 878 | }; |
| 879 | |
| 880 | errors_found = 0; |
| 881 | disable_replace_refs(); |
| 882 | save_commit_buffer = 0; |
| 883 | |
| 884 | argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0); |
| 885 | |
| 886 | /* fsck knows how to handle missing promisor objects */ |
| 887 | repo->fetch_if_missing = 0; |
| 888 | |
| 889 | fsck_options_init(&fsck_walk_options, repo, FSCK_OPTIONS_DEFAULT); |
| 890 | fsck_walk_options.walk = mark_object; |
| 891 | |
| 892 | fsck_options_init(&fsck_obj_options, repo, FSCK_OPTIONS_DEFAULT); |
| 893 | fsck_obj_options.walk = mark_used; |
| 894 | fsck_obj_options.error_func = fsck_objects_error_func; |
| 895 | if (check_strict) |
| 896 | fsck_obj_options.strict = 1; |
| 897 | |
| 898 | if (show_progress == -1) |
| 899 | show_progress = isatty(2); |
| 900 | if (verbose) { |
| 901 | show_progress = 0; |
| 902 | odb_fsck_opts.flags |= ODB_FSCK_VERBOSE; |
| 903 | } |
| 904 | if (show_progress) |
| 905 | odb_fsck_opts.flags |= ODB_FSCK_PROGRESS; |
| 906 | |
| 907 | if (write_lost_and_found) { |
| 908 | odb_fsck_opts.flags |= ODB_FSCK_FULL; |
| 909 | include_reflogs = 0; |
| 910 | } |
| 911 | |
| 912 | if (name_objects) |
| 913 | fsck_enable_object_names(&fsck_walk_options); |
| 914 | |
| 915 | repo_config(repo, git_fsck_config, &fsck_obj_options); |
| 916 | prepare_repo_settings(repo); |
| 917 | |
| 918 | if (check_references) |
| 919 | fsck_refs(repo); |
| 920 | |
| 921 | /* |
| 922 | * Take a snapshot of the refs before walking objects to avoid looking |
| 923 | * at a set of refs that may be changed by the user while we are walking |
| 924 | * objects. We can still walk over new objects that are added during the |
| 925 | * execution of fsck but won't miss any objects that were reachable. |
| 926 | */ |
| 927 | snapshot_refs(repo, &snap, argc, argv); |
| 928 | |
| 929 | /* Ensure we get a "fresh" view of the odb */ |
| 930 | odb_reprepare(repo->objects); |
| 931 | |
| 932 | if (connectivity_only) { |
| 933 | odb_for_each_object(repo->objects, NULL, |
| 934 | mark_object_for_connectivity, repo, 0); |
| 935 | } else { |
| 936 | if (odb_fsck(repo->objects, &odb_fsck_opts) < 0) |
| 937 | errors_found |= ERROR_OBJECT; |
| 938 | |
| 939 | if (fsck_finish(&fsck_obj_options)) |
| 940 | errors_found |= ERROR_OBJECT; |
| 941 | } |
| 942 | |
| 943 | /* Process the snapshotted refs and the reflogs. */ |
| 944 | process_refs(repo, &snap); |
| 945 | |
| 946 | /* If not given any explicit objects, process index files too. */ |
| 947 | if (!argc) |
| 948 | keep_cache_objects = 1; |
| 949 | if (keep_cache_objects) { |
| 950 | /* |
| 951 | * TODO: Consider first walking these indexes in snapshot_refs, |
| 952 | * to snapshot where the index entries used to point, and then |
| 953 | * check those snapshotted locations here. |
| 954 | */ |
| 955 | struct worktree **worktrees, **p; |
| 956 | |
| 957 | verify_index_checksum = 1; |
| 958 | verify_ce_order = 1; |
| 959 | |
| 960 | worktrees = get_worktrees(repo); |
| 961 | for (p = worktrees; *p; p++) { |
| 962 | struct worktree *wt = *p; |
| 963 | struct index_state istate = |
| 964 | INDEX_STATE_INIT(repo); |
| 965 | char *path, *wt_gitdir; |
| 966 | |
| 967 | /* |
| 968 | * Make a copy since the buffer is reusable |
| 969 | * and may get overwritten by other calls |
| 970 | * while we're examining the index. |
| 971 | */ |
| 972 | path = xstrdup(worktree_git_path(wt, "index")); |
| 973 | wt_gitdir = get_worktree_git_dir(wt); |
| 974 | |
| 975 | read_index_from(&istate, path, wt_gitdir); |
| 976 | fsck_index(&istate, path, wt->is_current); |
| 977 | |
| 978 | discard_index(&istate); |
| 979 | free(wt_gitdir); |
| 980 | free(path); |
| 981 | } |
| 982 | free_worktrees(worktrees); |
| 983 | } |
| 984 | |
| 985 | check_connectivity(repo); |
| 986 | |
| 987 | if (repo->settings.core_commit_graph) { |
| 988 | struct odb_source_files *files = odb_source_files_downcast(repo->objects->source); |
| 989 | struct child_process commit_graph_verify = CHILD_PROCESS_INIT; |
| 990 | |
| 991 | for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) { |
| 992 | child_process_init(&commit_graph_verify); |
| 993 | commit_graph_verify.git_cmd = 1; |
| 994 | strvec_pushl(&commit_graph_verify.args, "commit-graph", |
| 995 | "verify", "--object-dir", dir->abspath, NULL); |
| 996 | if (show_progress) |
| 997 | strvec_push(&commit_graph_verify.args, "--progress"); |
| 998 | else |
| 999 | strvec_push(&commit_graph_verify.args, "--no-progress"); |
| 1000 | if (run_command(&commit_graph_verify)) |
| 1001 | errors_found |= ERROR_COMMIT_GRAPH; |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | free_snapshot_refs(&snap); |
| 1006 | return errors_found; |
| 1007 | } |