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