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