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