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