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