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