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