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