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