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