builtin / fsck.con commit builtin rebase: optionally auto-detect the upstream (8f5986d)
   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((struct commit *)obj);
 386        return err;
 387}
 388
 389static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
 390                           unsigned long size, void *buffer, int *eaten)
 391{
 392        /*
 393         * Note, buffer may be NULL if type is OBJ_BLOB. See
 394         * verify_packfile(), data_valid variable for details.
 395         */
 396        struct object *obj;
 397        obj = parse_object_buffer(the_repository, oid, type, size, buffer,
 398                                  eaten);
 399        if (!obj) {
 400                errors_found |= ERROR_OBJECT;
 401                return error("%s: object corrupt or missing", oid_to_hex(oid));
 402        }
 403        obj->flags &= ~(REACHABLE | SEEN);
 404        obj->flags |= HAS_OBJ;
 405        return fsck_obj(obj, buffer, size);
 406}
 407
 408static int default_refs;
 409
 410static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
 411        timestamp_t timestamp)
 412{
 413        struct object *obj;
 414
 415        if (!is_null_oid(oid)) {
 416                obj = lookup_object(the_repository, oid->hash);
 417                if (obj && (obj->flags & HAS_OBJ)) {
 418                        if (timestamp && name_objects)
 419                                add_decoration(fsck_walk_options.object_names,
 420                                        obj,
 421                                        xstrfmt("%s@{%"PRItime"}", refname, timestamp));
 422                        obj->flags |= USED;
 423                        mark_object_reachable(obj);
 424                } else if (!is_promisor_object(oid)) {
 425                        error("%s: invalid reflog entry %s", refname, oid_to_hex(oid));
 426                        errors_found |= ERROR_REACHABLE;
 427                }
 428        }
 429}
 430
 431static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
 432                const char *email, timestamp_t timestamp, int tz,
 433                const char *message, void *cb_data)
 434{
 435        const char *refname = cb_data;
 436
 437        if (verbose)
 438                fprintf(stderr, "Checking reflog %s->%s\n",
 439                        oid_to_hex(ooid), oid_to_hex(noid));
 440
 441        fsck_handle_reflog_oid(refname, ooid, 0);
 442        fsck_handle_reflog_oid(refname, noid, timestamp);
 443        return 0;
 444}
 445
 446static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
 447                              int flag, void *cb_data)
 448{
 449        for_each_reflog_ent(logname, fsck_handle_reflog_ent, (void *)logname);
 450        return 0;
 451}
 452
 453static int fsck_handle_ref(const char *refname, const struct object_id *oid,
 454                           int flag, void *cb_data)
 455{
 456        struct object *obj;
 457
 458        obj = parse_object(the_repository, oid);
 459        if (!obj) {
 460                if (is_promisor_object(oid)) {
 461                        /*
 462                         * Increment default_refs anyway, because this is a
 463                         * valid ref.
 464                         */
 465                         default_refs++;
 466                         return 0;
 467                }
 468                error("%s: invalid sha1 pointer %s", refname, oid_to_hex(oid));
 469                errors_found |= ERROR_REACHABLE;
 470                /* We'll continue with the rest despite the error.. */
 471                return 0;
 472        }
 473        if (obj->type != OBJ_COMMIT && is_branch(refname)) {
 474                error("%s: not a commit", refname);
 475                errors_found |= ERROR_REFS;
 476        }
 477        default_refs++;
 478        obj->flags |= USED;
 479        if (name_objects)
 480                add_decoration(fsck_walk_options.object_names,
 481                        obj, xstrdup(refname));
 482        mark_object_reachable(obj);
 483
 484        return 0;
 485}
 486
 487static void get_default_heads(void)
 488{
 489        if (head_points_at && !is_null_oid(&head_oid))
 490                fsck_handle_ref("HEAD", &head_oid, 0, NULL);
 491        for_each_rawref(fsck_handle_ref, NULL);
 492        if (include_reflogs)
 493                for_each_reflog(fsck_handle_reflog, NULL);
 494
 495        /*
 496         * Not having any default heads isn't really fatal, but
 497         * it does mean that "--unreachable" no longer makes any
 498         * sense (since in this case everything will obviously
 499         * be unreachable by definition.
 500         *
 501         * Showing dangling objects is valid, though (as those
 502         * dangling objects are likely lost heads).
 503         *
 504         * So we just print a warning about it, and clear the
 505         * "show_unreachable" flag.
 506         */
 507        if (!default_refs) {
 508                fprintf(stderr, "notice: No default references\n");
 509                show_unreachable = 0;
 510        }
 511}
 512
 513static int fsck_loose(const struct object_id *oid, const char *path, void *data)
 514{
 515        struct object *obj;
 516        enum object_type type;
 517        unsigned long size;
 518        void *contents;
 519        int eaten;
 520
 521        if (read_loose_object(path, oid, &type, &size, &contents) < 0) {
 522                errors_found |= ERROR_OBJECT;
 523                error("%s: object corrupt or missing: %s",
 524                      oid_to_hex(oid), path);
 525                return 0; /* keep checking other objects */
 526        }
 527
 528        if (!contents && type != OBJ_BLOB)
 529                BUG("read_loose_object streamed a non-blob");
 530
 531        obj = parse_object_buffer(the_repository, oid, type, size,
 532                                  contents, &eaten);
 533
 534        if (!obj) {
 535                errors_found |= ERROR_OBJECT;
 536                error("%s: object could not be parsed: %s",
 537                      oid_to_hex(oid), path);
 538                if (!eaten)
 539                        free(contents);
 540                return 0; /* keep checking other objects */
 541        }
 542
 543        obj->flags &= ~(REACHABLE | SEEN);
 544        obj->flags |= HAS_OBJ;
 545        if (fsck_obj(obj, contents, size))
 546                errors_found |= ERROR_OBJECT;
 547
 548        if (!eaten)
 549                free(contents);
 550        return 0; /* keep checking other objects, even if we saw an error */
 551}
 552
 553static int fsck_cruft(const char *basename, const char *path, void *data)
 554{
 555        if (!starts_with(basename, "tmp_obj_"))
 556                fprintf(stderr, "bad sha1 file: %s\n", path);
 557        return 0;
 558}
 559
 560static int fsck_subdir(unsigned int nr, const char *path, void *progress)
 561{
 562        display_progress(progress, nr + 1);
 563        return 0;
 564}
 565
 566static void fsck_object_dir(const char *path)
 567{
 568        struct progress *progress = NULL;
 569
 570        if (verbose)
 571                fprintf(stderr, "Checking object directory\n");
 572
 573        if (show_progress)
 574                progress = start_progress(_("Checking object directories"), 256);
 575
 576        for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
 577                                      progress);
 578        display_progress(progress, 256);
 579        stop_progress(&progress);
 580}
 581
 582static int fsck_head_link(void)
 583{
 584        int null_is_error = 0;
 585
 586        if (verbose)
 587                fprintf(stderr, "Checking HEAD link\n");
 588
 589        head_points_at = resolve_ref_unsafe("HEAD", 0, &head_oid, NULL);
 590        if (!head_points_at) {
 591                errors_found |= ERROR_REFS;
 592                return error("Invalid HEAD");
 593        }
 594        if (!strcmp(head_points_at, "HEAD"))
 595                /* detached HEAD */
 596                null_is_error = 1;
 597        else if (!starts_with(head_points_at, "refs/heads/")) {
 598                errors_found |= ERROR_REFS;
 599                return error("HEAD points to something strange (%s)",
 600                             head_points_at);
 601        }
 602        if (is_null_oid(&head_oid)) {
 603                if (null_is_error) {
 604                        errors_found |= ERROR_REFS;
 605                        return error("HEAD: detached HEAD points at nothing");
 606                }
 607                fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
 608                        head_points_at + 11);
 609        }
 610        return 0;
 611}
 612
 613static int fsck_cache_tree(struct cache_tree *it)
 614{
 615        int i;
 616        int err = 0;
 617
 618        if (verbose)
 619                fprintf(stderr, "Checking cache tree\n");
 620
 621        if (0 <= it->entry_count) {
 622                struct object *obj = parse_object(the_repository, &it->oid);
 623                if (!obj) {
 624                        error("%s: invalid sha1 pointer in cache-tree",
 625                              oid_to_hex(&it->oid));
 626                        errors_found |= ERROR_REFS;
 627                        return 1;
 628                }
 629                obj->flags |= USED;
 630                if (name_objects)
 631                        add_decoration(fsck_walk_options.object_names,
 632                                obj, xstrdup(":"));
 633                mark_object_reachable(obj);
 634                if (obj->type != OBJ_TREE)
 635                        err |= objerror(obj, "non-tree in cache-tree");
 636        }
 637        for (i = 0; i < it->subtree_nr; i++)
 638                err |= fsck_cache_tree(it->down[i]->cache_tree);
 639        return err;
 640}
 641
 642static void mark_object_for_connectivity(const struct object_id *oid)
 643{
 644        struct object *obj = lookup_unknown_object(oid->hash);
 645        obj->flags |= HAS_OBJ;
 646}
 647
 648static int mark_loose_for_connectivity(const struct object_id *oid,
 649                                       const char *path,
 650                                       void *data)
 651{
 652        mark_object_for_connectivity(oid);
 653        return 0;
 654}
 655
 656static int mark_packed_for_connectivity(const struct object_id *oid,
 657                                        struct packed_git *pack,
 658                                        uint32_t pos,
 659                                        void *data)
 660{
 661        mark_object_for_connectivity(oid);
 662        return 0;
 663}
 664
 665static char const * const fsck_usage[] = {
 666        N_("git fsck [<options>] [<object>...]"),
 667        NULL
 668};
 669
 670static struct option fsck_opts[] = {
 671        OPT__VERBOSE(&verbose, N_("be verbose")),
 672        OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
 673        OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
 674        OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
 675        OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
 676        OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
 677        OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
 678        OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
 679        OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
 680        OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
 681        OPT_BOOL(0, "lost-found", &write_lost_and_found,
 682                                N_("write dangling objects in .git/lost-found")),
 683        OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
 684        OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
 685        OPT_END(),
 686};
 687
 688int cmd_fsck(int argc, const char **argv, const char *prefix)
 689{
 690        int i;
 691        struct alternate_object_database *alt;
 692
 693        /* fsck knows how to handle missing promisor objects */
 694        fetch_if_missing = 0;
 695
 696        errors_found = 0;
 697        check_replace_refs = 0;
 698
 699        argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
 700
 701        fsck_walk_options.walk = mark_object;
 702        fsck_obj_options.walk = mark_used;
 703        fsck_obj_options.error_func = fsck_error_func;
 704        if (check_strict)
 705                fsck_obj_options.strict = 1;
 706
 707        if (show_progress == -1)
 708                show_progress = isatty(2);
 709        if (verbose)
 710                show_progress = 0;
 711
 712        if (write_lost_and_found) {
 713                check_full = 1;
 714                include_reflogs = 0;
 715        }
 716
 717        if (name_objects)
 718                fsck_walk_options.object_names =
 719                        xcalloc(1, sizeof(struct decoration));
 720
 721        git_config(fsck_config, NULL);
 722
 723        fsck_head_link();
 724        if (connectivity_only) {
 725                for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
 726                for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
 727        } else {
 728                struct alternate_object_database *alt_odb_list;
 729
 730                fsck_object_dir(get_object_directory());
 731
 732                prepare_alt_odb(the_repository);
 733                alt_odb_list = the_repository->objects->alt_odb_list;
 734                for (alt = alt_odb_list; alt; alt = alt->next)
 735                        fsck_object_dir(alt->path);
 736
 737                if (check_full) {
 738                        struct packed_git *p;
 739                        uint32_t total = 0, count = 0;
 740                        struct progress *progress = NULL;
 741
 742                        if (show_progress) {
 743                                for (p = get_packed_git(the_repository); p;
 744                                     p = p->next) {
 745                                        if (open_pack_index(p))
 746                                                continue;
 747                                        total += p->num_objects;
 748                                }
 749
 750                                progress = start_progress(_("Checking objects"), total);
 751                        }
 752                        for (p = get_packed_git(the_repository); p;
 753                             p = p->next) {
 754                                /* verify gives error messages itself */
 755                                if (verify_pack(p, fsck_obj_buffer,
 756                                                progress, count))
 757                                        errors_found |= ERROR_PACK;
 758                                count += p->num_objects;
 759                        }
 760                        stop_progress(&progress);
 761                }
 762
 763                if (fsck_finish(&fsck_obj_options))
 764                        errors_found |= ERROR_OBJECT;
 765        }
 766
 767        for (i = 0; i < argc; i++) {
 768                const char *arg = argv[i];
 769                struct object_id oid;
 770                if (!get_oid(arg, &oid)) {
 771                        struct object *obj = lookup_object(the_repository,
 772                                                           oid.hash);
 773
 774                        if (!obj || !(obj->flags & HAS_OBJ)) {
 775                                if (is_promisor_object(&oid))
 776                                        continue;
 777                                error("%s: object missing", oid_to_hex(&oid));
 778                                errors_found |= ERROR_OBJECT;
 779                                continue;
 780                        }
 781
 782                        obj->flags |= USED;
 783                        if (name_objects)
 784                                add_decoration(fsck_walk_options.object_names,
 785                                        obj, xstrdup(arg));
 786                        mark_object_reachable(obj);
 787                        continue;
 788                }
 789                error("invalid parameter: expected sha1, got '%s'", arg);
 790                errors_found |= ERROR_OBJECT;
 791        }
 792
 793        /*
 794         * If we've not been given any explicit head information, do the
 795         * default ones from .git/refs. We also consider the index file
 796         * in this case (ie this implies --cache).
 797         */
 798        if (!argc) {
 799                get_default_heads();
 800                keep_cache_objects = 1;
 801        }
 802
 803        if (keep_cache_objects) {
 804                verify_index_checksum = 1;
 805                verify_ce_order = 1;
 806                read_cache();
 807                for (i = 0; i < active_nr; i++) {
 808                        unsigned int mode;
 809                        struct blob *blob;
 810                        struct object *obj;
 811
 812                        mode = active_cache[i]->ce_mode;
 813                        if (S_ISGITLINK(mode))
 814                                continue;
 815                        blob = lookup_blob(the_repository,
 816                                           &active_cache[i]->oid);
 817                        if (!blob)
 818                                continue;
 819                        obj = &blob->object;
 820                        obj->flags |= USED;
 821                        if (name_objects)
 822                                add_decoration(fsck_walk_options.object_names,
 823                                        obj,
 824                                        xstrfmt(":%s", active_cache[i]->name));
 825                        mark_object_reachable(obj);
 826                }
 827                if (active_cache_tree)
 828                        fsck_cache_tree(active_cache_tree);
 829        }
 830
 831        check_connectivity();
 832
 833        if (!git_config_get_bool("core.commitgraph", &i) && i) {
 834                struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
 835                const char *verify_argv[] = { "commit-graph", "verify", NULL, NULL, NULL };
 836
 837                commit_graph_verify.argv = verify_argv;
 838                commit_graph_verify.git_cmd = 1;
 839                if (run_command(&commit_graph_verify))
 840                        errors_found |= ERROR_COMMIT_GRAPH;
 841
 842                prepare_alt_odb(the_repository);
 843                for (alt =  the_repository->objects->alt_odb_list; alt; alt = alt->next) {
 844                        verify_argv[2] = "--object-dir";
 845                        verify_argv[3] = alt->path;
 846                        if (run_command(&commit_graph_verify))
 847                                errors_found |= ERROR_COMMIT_GRAPH;
 848                }
 849        }
 850
 851        return errors_found;
 852}