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