builtin / fsck.con commit revision: use commit graph in get_reference() (ec0c579)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "repository.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 "pack.h"
  11#include "cache-tree.h"
  12#include "tree-walk.h"
  13#include "fsck.h"
  14#include "parse-options.h"
  15#include "dir.h"
  16#include "progress.h"
  17#include "streaming.h"
  18#include "decorate.h"
  19#include "packfile.h"
  20#include "object-store.h"
  21#include "run-command.h"
  22
  23#define REACHABLE 0x0001
  24#define SEEN      0x0002
  25#define HAS_OBJ   0x0004
  26/* This flag is set if something points to this object. */
  27#define USED      0x0008
  28
  29static int show_root;
  30static int show_tags;
  31static int show_unreachable;
  32static int include_reflogs = 1;
  33static int check_full = 1;
  34static int connectivity_only;
  35static int check_strict;
  36static int keep_cache_objects;
  37static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
  38static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
  39static struct object_id head_oid;
  40static const char *head_points_at;
  41static int errors_found;
  42static int write_lost_and_found;
  43static int verbose;
  44static int show_progress = -1;
  45static int show_dangling = 1;
  46static int name_objects;
  47#define ERROR_OBJECT 01
  48#define ERROR_REACHABLE 02
  49#define ERROR_PACK 04
  50#define ERROR_REFS 010
  51#define ERROR_COMMIT_GRAPH 020
  52
  53static const char *describe_object(struct object *obj)
  54{
  55        static struct strbuf buf = STRBUF_INIT;
  56        char *name = name_objects ?
  57                lookup_decoration(fsck_walk_options.object_names, obj) : NULL;
  58
  59        strbuf_reset(&buf);
  60        strbuf_addstr(&buf, oid_to_hex(&obj->oid));
  61        if (name)
  62                strbuf_addf(&buf, " (%s)", name);
  63
  64        return buf.buf;
  65}
  66
  67static const char *printable_type(struct object *obj)
  68{
  69        const char *ret;
  70
  71        if (obj->type == OBJ_NONE) {
  72                enum object_type type = oid_object_info(the_repository,
  73                                                        &obj->oid, NULL);
  74                if (type > 0)
  75                        object_as_type(the_repository, obj, type, 0);
  76        }
  77
  78        ret = type_name(obj->type);
  79        if (!ret)
  80                ret = "unknown";
  81
  82        return ret;
  83}
  84
  85static int fsck_config(const char *var, const char *value, void *cb)
  86{
  87        if (strcmp(var, "fsck.skiplist") == 0) {
  88                const char *path;
  89                struct strbuf sb = STRBUF_INIT;
  90
  91                if (git_config_pathname(&path, var, value))
  92                        return 1;
  93                strbuf_addf(&sb, "skiplist=%s", path);
  94                free((char *)path);
  95                fsck_set_msg_types(&fsck_obj_options, sb.buf);
  96                strbuf_release(&sb);
  97                return 0;
  98        }
  99
 100        if (skip_prefix(var, "fsck.", &var)) {
 101                fsck_set_msg_type(&fsck_obj_options, var, value);
 102                return 0;
 103        }
 104
 105        return git_default_config(var, value, cb);
 106}
 107
 108static void objreport(struct object *obj, const char *msg_type,
 109                        const char *err)
 110{
 111        fprintf(stderr, "%s in %s %s: %s\n",
 112                msg_type, printable_type(obj), describe_object(obj), err);
 113}
 114
 115static int objerror(struct object *obj, const char *err)
 116{
 117        errors_found |= ERROR_OBJECT;
 118        objreport(obj, "error", err);
 119        return -1;
 120}
 121
 122static int fsck_error_func(struct fsck_options *o,
 123        struct object *obj, int type, const char *message)
 124{
 125        objreport(obj, (type == FSCK_WARN) ? "warning" : "error", message);
 126        return (type == FSCK_WARN) ? 0 : 1;
 127}
 128
 129static struct object_array pending;
 130
 131static int mark_object(struct object *obj, int type, void *data, struct fsck_options *options)
 132{
 133        struct object *parent = data;
 134
 135        /*
 136         * The only case data is NULL or type is OBJ_ANY is when
 137         * mark_object_reachable() calls us.  All the callers of
 138         * that function has non-NULL obj hence ...
 139         */
 140        if (!obj) {
 141                /* ... these references to parent->fld are safe here */
 142                printf("broken link from %7s %s\n",
 143                           printable_type(parent), describe_object(parent));
 144                printf("broken link from %7s %s\n",
 145                           (type == OBJ_ANY ? "unknown" : type_name(type)), "unknown");
 146                errors_found |= ERROR_REACHABLE;
 147                return 1;
 148        }
 149
 150        if (type != OBJ_ANY && obj->type != type)
 151                /* ... and the reference to parent is safe here */
 152                objerror(parent, "wrong object type in link");
 153
 154        if (obj->flags & REACHABLE)
 155                return 0;
 156        obj->flags |= REACHABLE;
 157
 158        if (is_promisor_object(&obj->oid))
 159                /*
 160                 * Further recursion does not need to be performed on this
 161                 * object since it is a promisor object (so it does not need to
 162                 * be added to "pending").
 163                 */
 164                return 0;
 165
 166        if (!(obj->flags & HAS_OBJ)) {
 167                if (parent && !has_object_file(&obj->oid)) {
 168                        printf("broken link from %7s %s\n",
 169                                 printable_type(parent), describe_object(parent));
 170                        printf("              to %7s %s\n",
 171                                 printable_type(obj), describe_object(obj));
 172                        errors_found |= ERROR_REACHABLE;
 173                }
 174                return 1;
 175        }
 176
 177        add_object_array(obj, NULL, &pending);
 178        return 0;
 179}
 180
 181static void mark_object_reachable(struct object *obj)
 182{
 183        mark_object(obj, OBJ_ANY, NULL, NULL);
 184}
 185
 186static int traverse_one_object(struct object *obj)
 187{
 188        int result = fsck_walk(obj, obj, &fsck_walk_options);
 189
 190        if (obj->type == OBJ_TREE) {
 191                struct tree *tree = (struct tree *)obj;
 192                free_tree_buffer(tree);
 193        }
 194        return result;
 195}
 196
 197static int traverse_reachable(void)
 198{
 199        struct progress *progress = NULL;
 200        unsigned int nr = 0;
 201        int result = 0;
 202        if (show_progress)
 203                progress = start_delayed_progress(_("Checking connectivity"), 0);
 204        while (pending.nr) {
 205                result |= traverse_one_object(object_array_pop(&pending));
 206                display_progress(progress, ++nr);
 207        }
 208        stop_progress(&progress);
 209        return !!result;
 210}
 211
 212static int mark_used(struct object *obj, int type, void *data, struct fsck_options *options)
 213{
 214        if (!obj)
 215                return 1;
 216        obj->flags |= USED;
 217        return 0;
 218}
 219
 220/*
 221 * Check a single reachable object
 222 */
 223static void check_reachable_object(struct object *obj)
 224{
 225        /*
 226         * We obviously want the object to be parsed,
 227         * except if it was in a pack-file and we didn't
 228         * do a full fsck
 229         */
 230        if (!(obj->flags & HAS_OBJ)) {
 231                if (is_promisor_object(&obj->oid))
 232                        return;
 233                if (has_object_pack(&obj->oid))
 234                        return; /* it is in pack - forget about it */
 235                printf("missing %s %s\n", printable_type(obj),
 236                        describe_object(obj));
 237                errors_found |= ERROR_REACHABLE;
 238                return;
 239        }
 240}
 241
 242/*
 243 * Check a single unreachable object
 244 */
 245static void check_unreachable_object(struct object *obj)
 246{
 247        /*
 248         * Missing unreachable object? Ignore it. It's not like
 249         * we miss it (since it can't be reached), nor do we want
 250         * to complain about it being unreachable (since it does
 251         * not exist).
 252         */
 253        if (!(obj->flags & HAS_OBJ))
 254                return;
 255
 256        /*
 257         * Unreachable object that exists? Show it if asked to,
 258         * since this is something that is prunable.
 259         */
 260        if (show_unreachable) {
 261                printf("unreachable %s %s\n", printable_type(obj),
 262                        describe_object(obj));
 263                return;
 264        }
 265
 266        /*
 267         * "!USED" means that nothing at all points to it, including
 268         * other unreachable objects. In other words, it's the "tip"
 269         * of some set of unreachable objects, usually a commit that
 270         * got dropped.
 271         *
 272         * Such starting points are more interesting than some random
 273         * set of unreachable objects, so we show them even if the user
 274         * hasn't asked for _all_ unreachable objects. If you have
 275         * deleted a branch by mistake, this is a prime candidate to
 276         * start looking at, for example.
 277         */
 278        if (!(obj->flags & USED)) {
 279                if (show_dangling)
 280                        printf("dangling %s %s\n", printable_type(obj),
 281                               describe_object(obj));
 282                if (write_lost_and_found) {
 283                        char *filename = git_pathdup("lost-found/%s/%s",
 284                                obj->type == OBJ_COMMIT ? "commit" : "other",
 285                                describe_object(obj));
 286                        FILE *f;
 287
 288                        if (safe_create_leading_directories_const(filename)) {
 289                                error("Could not create lost-found");
 290                                free(filename);
 291                                return;
 292                        }
 293                        f = xfopen(filename, "w");
 294                        if (obj->type == OBJ_BLOB) {
 295                                if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
 296                                        die_errno("Could not write '%s'", filename);
 297                        } else
 298                                fprintf(f, "%s\n", describe_object(obj));
 299                        if (fclose(f))
 300                                die_errno("Could not finish '%s'",
 301                                          filename);
 302                        free(filename);
 303                }
 304                return;
 305        }
 306
 307        /*
 308         * Otherwise? It's there, it's unreachable, and some other unreachable
 309         * object points to it. Ignore it - it's not interesting, and we showed
 310         * all the interesting cases above.
 311         */
 312}
 313
 314static void check_object(struct object *obj)
 315{
 316        if (verbose)
 317                fprintf(stderr, "Checking %s\n", describe_object(obj));
 318
 319        if (obj->flags & REACHABLE)
 320                check_reachable_object(obj);
 321        else
 322                check_unreachable_object(obj);
 323}
 324
 325static void check_connectivity(void)
 326{
 327        int i, max;
 328
 329        /* Traverse the pending reachable objects */
 330        traverse_reachable();
 331
 332        /* Look up all the requirements, warn about missing objects.. */
 333        max = get_max_object_index();
 334        if (verbose)
 335                fprintf(stderr, "Checking connectivity (%d objects)\n", max);
 336
 337        for (i = 0; i < max; i++) {
 338                struct object *obj = get_indexed_object(i);
 339
 340                if (obj)
 341                        check_object(obj);
 342        }
 343}
 344
 345static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
 346{
 347        int err;
 348
 349        if (obj->flags & SEEN)
 350                return 0;
 351        obj->flags |= SEEN;
 352
 353        if (verbose)
 354                fprintf(stderr, "Checking %s %s\n",
 355                        printable_type(obj), describe_object(obj));
 356
 357        if (fsck_walk(obj, NULL, &fsck_obj_options))
 358                objerror(obj, "broken links");
 359        err = fsck_object(obj, buffer, size, &fsck_obj_options);
 360        if (err)
 361                goto out;
 362
 363        if (obj->type == OBJ_COMMIT) {
 364                struct commit *commit = (struct commit *) obj;
 365
 366                if (!commit->parents && show_root)
 367                        printf("root %s\n", describe_object(&commit->object));
 368        }
 369
 370        if (obj->type == OBJ_TAG) {
 371                struct tag *tag = (struct tag *) obj;
 372
 373                if (show_tags && tag->tagged) {
 374                        printf("tagged %s %s", printable_type(tag->tagged),
 375                                describe_object(tag->tagged));
 376                        printf(" (%s) in %s\n", tag->tag,
 377                                describe_object(&tag->object));
 378                }
 379        }
 380
 381out:
 382        if (obj->type == OBJ_TREE)
 383                free_tree_buffer((struct tree *)obj);
 384        if (obj->type == OBJ_COMMIT)
 385                free_commit_buffer(the_repository->parsed_objects,
 386                                   (struct commit *)obj);
 387        return err;
 388}
 389
 390static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
 391                           unsigned long size, void *buffer, int *eaten)
 392{
 393        /*
 394         * Note, buffer may be NULL if type is OBJ_BLOB. See
 395         * verify_packfile(), data_valid variable for details.
 396         */
 397        struct object *obj;
 398        obj = parse_object_buffer(the_repository, oid, type, size, buffer,
 399                                  eaten);
 400        if (!obj) {
 401                errors_found |= ERROR_OBJECT;
 402                return error("%s: object corrupt or missing", oid_to_hex(oid));
 403        }
 404        obj->flags &= ~(REACHABLE | SEEN);
 405        obj->flags |= HAS_OBJ;
 406        return fsck_obj(obj, buffer, size);
 407}
 408
 409static int default_refs;
 410
 411static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
 412        timestamp_t timestamp)
 413{
 414        struct object *obj;
 415
 416        if (!is_null_oid(oid)) {
 417                obj = lookup_object(the_repository, oid->hash);
 418                if (obj && (obj->flags & HAS_OBJ)) {
 419                        if (timestamp && name_objects)
 420                                add_decoration(fsck_walk_options.object_names,
 421                                        obj,
 422                                        xstrfmt("%s@{%"PRItime"}", refname, timestamp));
 423                        obj->flags |= USED;
 424                        mark_object_reachable(obj);
 425                } else if (!is_promisor_object(oid)) {
 426                        error("%s: invalid reflog entry %s", refname, oid_to_hex(oid));
 427                        errors_found |= ERROR_REACHABLE;
 428                }
 429        }
 430}
 431
 432static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
 433                const char *email, timestamp_t timestamp, int tz,
 434                const char *message, void *cb_data)
 435{
 436        const char *refname = cb_data;
 437
 438        if (verbose)
 439                fprintf(stderr, "Checking reflog %s->%s\n",
 440                        oid_to_hex(ooid), oid_to_hex(noid));
 441
 442        fsck_handle_reflog_oid(refname, ooid, 0);
 443        fsck_handle_reflog_oid(refname, noid, timestamp);
 444        return 0;
 445}
 446
 447static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
 448                              int flag, void *cb_data)
 449{
 450        for_each_reflog_ent(logname, fsck_handle_reflog_ent, (void *)logname);
 451        return 0;
 452}
 453
 454static int fsck_handle_ref(const char *refname, const struct object_id *oid,
 455                           int flag, void *cb_data)
 456{
 457        struct object *obj;
 458
 459        obj = parse_object(the_repository, oid);
 460        if (!obj) {
 461                if (is_promisor_object(oid)) {
 462                        /*
 463                         * Increment default_refs anyway, because this is a
 464                         * valid ref.
 465                         */
 466                         default_refs++;
 467                         return 0;
 468                }
 469                error("%s: invalid sha1 pointer %s", refname, oid_to_hex(oid));
 470                errors_found |= ERROR_REACHABLE;
 471                /* We'll continue with the rest despite the error.. */
 472                return 0;
 473        }
 474        if (obj->type != OBJ_COMMIT && is_branch(refname)) {
 475                error("%s: not a commit", refname);
 476                errors_found |= ERROR_REFS;
 477        }
 478        default_refs++;
 479        obj->flags |= USED;
 480        if (name_objects)
 481                add_decoration(fsck_walk_options.object_names,
 482                        obj, xstrdup(refname));
 483        mark_object_reachable(obj);
 484
 485        return 0;
 486}
 487
 488static void get_default_heads(void)
 489{
 490        if (head_points_at && !is_null_oid(&head_oid))
 491                fsck_handle_ref("HEAD", &head_oid, 0, NULL);
 492        for_each_rawref(fsck_handle_ref, NULL);
 493        if (include_reflogs)
 494                for_each_reflog(fsck_handle_reflog, NULL);
 495
 496        /*
 497         * Not having any default heads isn't really fatal, but
 498         * it does mean that "--unreachable" no longer makes any
 499         * sense (since in this case everything will obviously
 500         * be unreachable by definition.
 501         *
 502         * Showing dangling objects is valid, though (as those
 503         * dangling objects are likely lost heads).
 504         *
 505         * So we just print a warning about it, and clear the
 506         * "show_unreachable" flag.
 507         */
 508        if (!default_refs) {
 509                fprintf(stderr, "notice: No default references\n");
 510                show_unreachable = 0;
 511        }
 512}
 513
 514static int fsck_loose(const struct object_id *oid, const char *path, void *data)
 515{
 516        struct object *obj;
 517        enum object_type type;
 518        unsigned long size;
 519        void *contents;
 520        int eaten;
 521
 522        if (read_loose_object(path, oid, &type, &size, &contents) < 0) {
 523                errors_found |= ERROR_OBJECT;
 524                error("%s: object corrupt or missing: %s",
 525                      oid_to_hex(oid), path);
 526                return 0; /* keep checking other objects */
 527        }
 528
 529        if (!contents && type != OBJ_BLOB)
 530                BUG("read_loose_object streamed a non-blob");
 531
 532        obj = parse_object_buffer(the_repository, oid, type, size,
 533                                  contents, &eaten);
 534
 535        if (!obj) {
 536                errors_found |= ERROR_OBJECT;
 537                error("%s: object could not be parsed: %s",
 538                      oid_to_hex(oid), path);
 539                if (!eaten)
 540                        free(contents);
 541                return 0; /* keep checking other objects */
 542        }
 543
 544        obj->flags &= ~(REACHABLE | SEEN);
 545        obj->flags |= HAS_OBJ;
 546        if (fsck_obj(obj, contents, size))
 547                errors_found |= ERROR_OBJECT;
 548
 549        if (!eaten)
 550                free(contents);
 551        return 0; /* keep checking other objects, even if we saw an error */
 552}
 553
 554static int fsck_cruft(const char *basename, const char *path, void *data)
 555{
 556        if (!starts_with(basename, "tmp_obj_"))
 557                fprintf(stderr, "bad sha1 file: %s\n", path);
 558        return 0;
 559}
 560
 561static int fsck_subdir(unsigned int nr, const char *path, void *progress)
 562{
 563        display_progress(progress, nr + 1);
 564        return 0;
 565}
 566
 567static void fsck_object_dir(const char *path)
 568{
 569        struct progress *progress = NULL;
 570
 571        if (verbose)
 572                fprintf(stderr, "Checking object directory\n");
 573
 574        if (show_progress)
 575                progress = start_progress(_("Checking object directories"), 256);
 576
 577        for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
 578                                      progress);
 579        display_progress(progress, 256);
 580        stop_progress(&progress);
 581}
 582
 583static int fsck_head_link(void)
 584{
 585        int null_is_error = 0;
 586
 587        if (verbose)
 588                fprintf(stderr, "Checking HEAD link\n");
 589
 590        head_points_at = resolve_ref_unsafe("HEAD", 0, &head_oid, NULL);
 591        if (!head_points_at) {
 592                errors_found |= ERROR_REFS;
 593                return error("Invalid HEAD");
 594        }
 595        if (!strcmp(head_points_at, "HEAD"))
 596                /* detached HEAD */
 597                null_is_error = 1;
 598        else if (!starts_with(head_points_at, "refs/heads/")) {
 599                errors_found |= ERROR_REFS;
 600                return error("HEAD points to something strange (%s)",
 601                             head_points_at);
 602        }
 603        if (is_null_oid(&head_oid)) {
 604                if (null_is_error) {
 605                        errors_found |= ERROR_REFS;
 606                        return error("HEAD: detached HEAD points at nothing");
 607                }
 608                fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
 609                        head_points_at + 11);
 610        }
 611        return 0;
 612}
 613
 614static int fsck_cache_tree(struct cache_tree *it)
 615{
 616        int i;
 617        int err = 0;
 618
 619        if (verbose)
 620                fprintf(stderr, "Checking cache tree\n");
 621
 622        if (0 <= it->entry_count) {
 623                struct object *obj = parse_object(the_repository, &it->oid);
 624                if (!obj) {
 625                        error("%s: invalid sha1 pointer in cache-tree",
 626                              oid_to_hex(&it->oid));
 627                        errors_found |= ERROR_REFS;
 628                        return 1;
 629                }
 630                obj->flags |= USED;
 631                if (name_objects)
 632                        add_decoration(fsck_walk_options.object_names,
 633                                obj, xstrdup(":"));
 634                mark_object_reachable(obj);
 635                if (obj->type != OBJ_TREE)
 636                        err |= objerror(obj, "non-tree in cache-tree");
 637        }
 638        for (i = 0; i < it->subtree_nr; i++)
 639                err |= fsck_cache_tree(it->down[i]->cache_tree);
 640        return err;
 641}
 642
 643static void mark_object_for_connectivity(const struct object_id *oid)
 644{
 645        struct object *obj = lookup_unknown_object(oid->hash);
 646        obj->flags |= HAS_OBJ;
 647}
 648
 649static int mark_loose_for_connectivity(const struct object_id *oid,
 650                                       const char *path,
 651                                       void *data)
 652{
 653        mark_object_for_connectivity(oid);
 654        return 0;
 655}
 656
 657static int mark_packed_for_connectivity(const struct object_id *oid,
 658                                        struct packed_git *pack,
 659                                        uint32_t pos,
 660                                        void *data)
 661{
 662        mark_object_for_connectivity(oid);
 663        return 0;
 664}
 665
 666static char const * const fsck_usage[] = {
 667        N_("git fsck [<options>] [<object>...]"),
 668        NULL
 669};
 670
 671static struct option fsck_opts[] = {
 672        OPT__VERBOSE(&verbose, N_("be verbose")),
 673        OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
 674        OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
 675        OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
 676        OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
 677        OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
 678        OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
 679        OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
 680        OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
 681        OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
 682        OPT_BOOL(0, "lost-found", &write_lost_and_found,
 683                                N_("write dangling objects in .git/lost-found")),
 684        OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
 685        OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
 686        OPT_END(),
 687};
 688
 689int cmd_fsck(int argc, const char **argv, const char *prefix)
 690{
 691        int i;
 692        struct alternate_object_database *alt;
 693
 694        /* fsck knows how to handle missing promisor objects */
 695        fetch_if_missing = 0;
 696
 697        errors_found = 0;
 698        read_replace_refs = 0;
 699
 700        argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
 701
 702        fsck_walk_options.walk = mark_object;
 703        fsck_obj_options.walk = mark_used;
 704        fsck_obj_options.error_func = fsck_error_func;
 705        if (check_strict)
 706                fsck_obj_options.strict = 1;
 707
 708        if (show_progress == -1)
 709                show_progress = isatty(2);
 710        if (verbose)
 711                show_progress = 0;
 712
 713        if (write_lost_and_found) {
 714                check_full = 1;
 715                include_reflogs = 0;
 716        }
 717
 718        if (name_objects)
 719                fsck_walk_options.object_names =
 720                        xcalloc(1, sizeof(struct decoration));
 721
 722        git_config(fsck_config, NULL);
 723
 724        fsck_head_link();
 725        if (connectivity_only) {
 726                for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
 727                for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
 728        } else {
 729                struct alternate_object_database *alt_odb_list;
 730
 731                fsck_object_dir(get_object_directory());
 732
 733                prepare_alt_odb(the_repository);
 734                alt_odb_list = the_repository->objects->alt_odb_list;
 735                for (alt = alt_odb_list; alt; alt = alt->next)
 736                        fsck_object_dir(alt->path);
 737
 738                if (check_full) {
 739                        struct packed_git *p;
 740                        uint32_t total = 0, count = 0;
 741                        struct progress *progress = NULL;
 742
 743                        if (show_progress) {
 744                                for (p = get_all_packs(the_repository); p;
 745                                     p = p->next) {
 746                                        if (open_pack_index(p))
 747                                                continue;
 748                                        total += p->num_objects;
 749                                }
 750
 751                                progress = start_progress(_("Checking objects"), total);
 752                        }
 753                        for (p = get_all_packs(the_repository); p;
 754                             p = p->next) {
 755                                /* verify gives error messages itself */
 756                                if (verify_pack(p, fsck_obj_buffer,
 757                                                progress, count))
 758                                        errors_found |= ERROR_PACK;
 759                                count += p->num_objects;
 760                        }
 761                        stop_progress(&progress);
 762                }
 763
 764                if (fsck_finish(&fsck_obj_options))
 765                        errors_found |= ERROR_OBJECT;
 766        }
 767
 768        for (i = 0; i < argc; i++) {
 769                const char *arg = argv[i];
 770                struct object_id oid;
 771                if (!get_oid(arg, &oid)) {
 772                        struct object *obj = lookup_object(the_repository,
 773                                                           oid.hash);
 774
 775                        if (!obj || !(obj->flags & HAS_OBJ)) {
 776                                if (is_promisor_object(&oid))
 777                                        continue;
 778                                error("%s: object missing", oid_to_hex(&oid));
 779                                errors_found |= ERROR_OBJECT;
 780                                continue;
 781                        }
 782
 783                        obj->flags |= USED;
 784                        if (name_objects)
 785                                add_decoration(fsck_walk_options.object_names,
 786                                        obj, xstrdup(arg));
 787                        mark_object_reachable(obj);
 788                        continue;
 789                }
 790                error("invalid parameter: expected sha1, got '%s'", arg);
 791                errors_found |= ERROR_OBJECT;
 792        }
 793
 794        /*
 795         * If we've not been given any explicit head information, do the
 796         * default ones from .git/refs. We also consider the index file
 797         * in this case (ie this implies --cache).
 798         */
 799        if (!argc) {
 800                get_default_heads();
 801                keep_cache_objects = 1;
 802        }
 803
 804        if (keep_cache_objects) {
 805                verify_index_checksum = 1;
 806                verify_ce_order = 1;
 807                read_cache();
 808                for (i = 0; i < active_nr; i++) {
 809                        unsigned int mode;
 810                        struct blob *blob;
 811                        struct object *obj;
 812
 813                        mode = active_cache[i]->ce_mode;
 814                        if (S_ISGITLINK(mode))
 815                                continue;
 816                        blob = lookup_blob(the_repository,
 817                                           &active_cache[i]->oid);
 818                        if (!blob)
 819                                continue;
 820                        obj = &blob->object;
 821                        obj->flags |= USED;
 822                        if (name_objects)
 823                                add_decoration(fsck_walk_options.object_names,
 824                                        obj,
 825                                        xstrfmt(":%s", active_cache[i]->name));
 826                        mark_object_reachable(obj);
 827                }
 828                if (active_cache_tree)
 829                        fsck_cache_tree(active_cache_tree);
 830        }
 831
 832        check_connectivity();
 833
 834        if (!git_config_get_bool("core.commitgraph", &i) && i) {
 835                struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
 836                const char *verify_argv[] = { "commit-graph", "verify", NULL, NULL, NULL };
 837
 838                commit_graph_verify.argv = verify_argv;
 839                commit_graph_verify.git_cmd = 1;
 840                if (run_command(&commit_graph_verify))
 841                        errors_found |= ERROR_COMMIT_GRAPH;
 842
 843                prepare_alt_odb(the_repository);
 844                for (alt =  the_repository->objects->alt_odb_list; alt; alt = alt->next) {
 845                        verify_argv[2] = "--object-dir";
 846                        verify_argv[3] = alt->path;
 847                        if (run_command(&commit_graph_verify))
 848                                errors_found |= ERROR_COMMIT_GRAPH;
 849                }
 850        }
 851
 852        if (!git_config_get_bool("core.multipackindex", &i) && i) {
 853                struct child_process midx_verify = CHILD_PROCESS_INIT;
 854                const char *midx_argv[] = { "multi-pack-index", "verify", NULL, NULL, NULL };
 855
 856                midx_verify.argv = midx_argv;
 857                midx_verify.git_cmd = 1;
 858                if (run_command(&midx_verify))
 859                        errors_found |= ERROR_COMMIT_GRAPH;
 860
 861                prepare_alt_odb(the_repository);
 862                for (alt =  the_repository->objects->alt_odb_list; alt; alt = alt->next) {
 863                        midx_argv[2] = "--object-dir";
 864                        midx_argv[3] = alt->path;
 865                        if (run_command(&midx_verify))
 866                                errors_found |= ERROR_COMMIT_GRAPH;
 867                }
 868        }
 869
 870        return errors_found;
 871}