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