builtin / fsck.con commit fsck: refactor how to describe objects (993a21b)
   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#define ERROR_REFS 010
  42
  43static const char *describe_object(struct object *obj)
  44{
  45        return oid_to_hex(&obj->oid);
  46}
  47
  48static int fsck_config(const char *var, const char *value, void *cb)
  49{
  50        if (strcmp(var, "fsck.skiplist") == 0) {
  51                const char *path;
  52                struct strbuf sb = STRBUF_INIT;
  53
  54                if (git_config_pathname(&path, var, value))
  55                        return 1;
  56                strbuf_addf(&sb, "skiplist=%s", path);
  57                free((char *)path);
  58                fsck_set_msg_types(&fsck_obj_options, sb.buf);
  59                strbuf_release(&sb);
  60                return 0;
  61        }
  62
  63        if (skip_prefix(var, "fsck.", &var)) {
  64                fsck_set_msg_type(&fsck_obj_options, var, value);
  65                return 0;
  66        }
  67
  68        return git_default_config(var, value, cb);
  69}
  70
  71static void objreport(struct object *obj, const char *msg_type,
  72                        const char *err)
  73{
  74        fprintf(stderr, "%s in %s %s: %s\n",
  75                msg_type, typename(obj->type), describe_object(obj), err);
  76}
  77
  78static int objerror(struct object *obj, const char *err)
  79{
  80        errors_found |= ERROR_OBJECT;
  81        objreport(obj, "error", err);
  82        return -1;
  83}
  84
  85static int fsck_error_func(struct object *obj, int type, const char *message)
  86{
  87        objreport(obj, (type == FSCK_WARN) ? "warning" : "error", message);
  88        return (type == FSCK_WARN) ? 0 : 1;
  89}
  90
  91static struct object_array pending;
  92
  93static int mark_object(struct object *obj, int type, void *data, struct fsck_options *options)
  94{
  95        struct object *parent = data;
  96
  97        /*
  98         * The only case data is NULL or type is OBJ_ANY is when
  99         * mark_object_reachable() calls us.  All the callers of
 100         * that function has non-NULL obj hence ...
 101         */
 102        if (!obj) {
 103                /* ... these references to parent->fld are safe here */
 104                printf("broken link from %7s %s\n",
 105                           typename(parent->type), describe_object(parent));
 106                printf("broken link from %7s %s\n",
 107                           (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
 108                errors_found |= ERROR_REACHABLE;
 109                return 1;
 110        }
 111
 112        if (type != OBJ_ANY && obj->type != type)
 113                /* ... and the reference to parent is safe here */
 114                objerror(parent, "wrong object type in link");
 115
 116        if (obj->flags & REACHABLE)
 117                return 0;
 118        obj->flags |= REACHABLE;
 119        if (!(obj->flags & HAS_OBJ)) {
 120                if (parent && !has_object_file(&obj->oid)) {
 121                        printf("broken link from %7s %s\n",
 122                                 typename(parent->type), describe_object(parent));
 123                        printf("              to %7s %s\n",
 124                                 typename(obj->type), describe_object(obj));
 125                        errors_found |= ERROR_REACHABLE;
 126                }
 127                return 1;
 128        }
 129
 130        add_object_array(obj, NULL, &pending);
 131        return 0;
 132}
 133
 134static void mark_object_reachable(struct object *obj)
 135{
 136        mark_object(obj, OBJ_ANY, NULL, NULL);
 137}
 138
 139static int traverse_one_object(struct object *obj)
 140{
 141        int result;
 142        struct tree *tree = NULL;
 143
 144        if (obj->type == OBJ_TREE) {
 145                tree = (struct tree *)obj;
 146                if (parse_tree(tree) < 0)
 147                        return 1; /* error already displayed */
 148        }
 149        result = fsck_walk(obj, obj, &fsck_walk_options);
 150        if (tree)
 151                free_tree_buffer(tree);
 152        return result;
 153}
 154
 155static int traverse_reachable(void)
 156{
 157        struct progress *progress = NULL;
 158        unsigned int nr = 0;
 159        int result = 0;
 160        if (show_progress)
 161                progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2);
 162        while (pending.nr) {
 163                struct object_array_entry *entry;
 164                struct object *obj;
 165
 166                entry = pending.objects + --pending.nr;
 167                obj = entry->item;
 168                result |= traverse_one_object(obj);
 169                display_progress(progress, ++nr);
 170        }
 171        stop_progress(&progress);
 172        return !!result;
 173}
 174
 175static int mark_used(struct object *obj, int type, void *data, struct fsck_options *options)
 176{
 177        if (!obj)
 178                return 1;
 179        obj->used = 1;
 180        return 0;
 181}
 182
 183/*
 184 * Check a single reachable object
 185 */
 186static void check_reachable_object(struct object *obj)
 187{
 188        /*
 189         * We obviously want the object to be parsed,
 190         * except if it was in a pack-file and we didn't
 191         * do a full fsck
 192         */
 193        if (!(obj->flags & HAS_OBJ)) {
 194                if (has_sha1_pack(obj->oid.hash))
 195                        return; /* it is in pack - forget about it */
 196                if (connectivity_only && has_object_file(&obj->oid))
 197                        return;
 198                printf("missing %s %s\n", typename(obj->type),
 199                        describe_object(obj));
 200                errors_found |= ERROR_REACHABLE;
 201                return;
 202        }
 203}
 204
 205/*
 206 * Check a single unreachable object
 207 */
 208static void check_unreachable_object(struct object *obj)
 209{
 210        /*
 211         * Missing unreachable object? Ignore it. It's not like
 212         * we miss it (since it can't be reached), nor do we want
 213         * to complain about it being unreachable (since it does
 214         * not exist).
 215         */
 216        if (!obj->parsed)
 217                return;
 218
 219        /*
 220         * Unreachable object that exists? Show it if asked to,
 221         * since this is something that is prunable.
 222         */
 223        if (show_unreachable) {
 224                printf("unreachable %s %s\n", typename(obj->type),
 225                        describe_object(obj));
 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                               describe_object(obj));
 245                if (write_lost_and_found) {
 246                        char *filename = git_pathdup("lost-found/%s/%s",
 247                                obj->type == OBJ_COMMIT ? "commit" : "other",
 248                                describe_object(obj));
 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->oid.hash, NULL, 1))
 260                                        die_errno("Could not write '%s'", filename);
 261                        } else
 262                                fprintf(f, "%s\n", describe_object(obj));
 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", describe_object(obj));
 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), describe_object(obj));
 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", describe_object(&commit->object));
 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),
 344                                describe_object(tag->tagged));
 345                        printf(" (%s) in %s\n", tag->tag,
 346                                describe_object(&tag->object));
 347                }
 348        }
 349
 350        return 0;
 351}
 352
 353static int fsck_sha1(const unsigned char *sha1)
 354{
 355        struct object *obj = parse_object(sha1);
 356        if (!obj) {
 357                errors_found |= ERROR_OBJECT;
 358                return error("%s: object corrupt or missing",
 359                             sha1_to_hex(sha1));
 360        }
 361        obj->flags |= HAS_OBJ;
 362        return fsck_obj(obj);
 363}
 364
 365static int fsck_obj_buffer(const unsigned char *sha1, enum object_type type,
 366                           unsigned long size, void *buffer, int *eaten)
 367{
 368        struct object *obj;
 369        obj = parse_object_buffer(sha1, type, size, buffer, eaten);
 370        if (!obj) {
 371                errors_found |= ERROR_OBJECT;
 372                return error("%s: object corrupt or missing", sha1_to_hex(sha1));
 373        }
 374        obj->flags = HAS_OBJ;
 375        return fsck_obj(obj);
 376}
 377
 378static int default_refs;
 379
 380static void fsck_handle_reflog_sha1(const char *refname, unsigned char *sha1)
 381{
 382        struct object *obj;
 383
 384        if (!is_null_sha1(sha1)) {
 385                obj = lookup_object(sha1);
 386                if (obj) {
 387                        obj->used = 1;
 388                        mark_object_reachable(obj);
 389                } else {
 390                        error("%s: invalid reflog entry %s", refname, sha1_to_hex(sha1));
 391                        errors_found |= ERROR_REACHABLE;
 392                }
 393        }
 394}
 395
 396static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
 397                const char *email, unsigned long timestamp, int tz,
 398                const char *message, void *cb_data)
 399{
 400        const char *refname = cb_data;
 401
 402        if (verbose)
 403                fprintf(stderr, "Checking reflog %s->%s\n",
 404                        sha1_to_hex(osha1), sha1_to_hex(nsha1));
 405
 406        fsck_handle_reflog_sha1(refname, osha1);
 407        fsck_handle_reflog_sha1(refname, nsha1);
 408        return 0;
 409}
 410
 411static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
 412                              int flag, void *cb_data)
 413{
 414        for_each_reflog_ent(logname, fsck_handle_reflog_ent, (void *)logname);
 415        return 0;
 416}
 417
 418static int fsck_handle_ref(const char *refname, const struct object_id *oid,
 419                           int flag, void *cb_data)
 420{
 421        struct object *obj;
 422
 423        obj = parse_object(oid->hash);
 424        if (!obj) {
 425                error("%s: invalid sha1 pointer %s", refname, oid_to_hex(oid));
 426                errors_found |= ERROR_REACHABLE;
 427                /* We'll continue with the rest despite the error.. */
 428                return 0;
 429        }
 430        if (obj->type != OBJ_COMMIT && is_branch(refname)) {
 431                error("%s: not a commit", refname);
 432                errors_found |= ERROR_REFS;
 433        }
 434        default_refs++;
 435        obj->used = 1;
 436        mark_object_reachable(obj);
 437
 438        return 0;
 439}
 440
 441static void get_default_heads(void)
 442{
 443        if (head_points_at && !is_null_oid(&head_oid))
 444                fsck_handle_ref("HEAD", &head_oid, 0, NULL);
 445        for_each_rawref(fsck_handle_ref, NULL);
 446        if (include_reflogs)
 447                for_each_reflog(fsck_handle_reflog, NULL);
 448
 449        /*
 450         * Not having any default heads isn't really fatal, but
 451         * it does mean that "--unreachable" no longer makes any
 452         * sense (since in this case everything will obviously
 453         * be unreachable by definition.
 454         *
 455         * Showing dangling objects is valid, though (as those
 456         * dangling objects are likely lost heads).
 457         *
 458         * So we just print a warning about it, and clear the
 459         * "show_unreachable" flag.
 460         */
 461        if (!default_refs) {
 462                fprintf(stderr, "notice: No default references\n");
 463                show_unreachable = 0;
 464        }
 465}
 466
 467static int fsck_loose(const unsigned char *sha1, const char *path, void *data)
 468{
 469        if (fsck_sha1(sha1))
 470                errors_found |= ERROR_OBJECT;
 471        return 0;
 472}
 473
 474static int fsck_cruft(const char *basename, const char *path, void *data)
 475{
 476        if (!starts_with(basename, "tmp_obj_"))
 477                fprintf(stderr, "bad sha1 file: %s\n", path);
 478        return 0;
 479}
 480
 481static int fsck_subdir(int nr, const char *path, void *progress)
 482{
 483        display_progress(progress, nr + 1);
 484        return 0;
 485}
 486
 487static void fsck_object_dir(const char *path)
 488{
 489        struct progress *progress = NULL;
 490
 491        if (verbose)
 492                fprintf(stderr, "Checking object directory\n");
 493
 494        if (show_progress)
 495                progress = start_progress(_("Checking object directories"), 256);
 496
 497        for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
 498                                      progress);
 499        display_progress(progress, 256);
 500        stop_progress(&progress);
 501}
 502
 503static int fsck_head_link(void)
 504{
 505        int null_is_error = 0;
 506
 507        if (verbose)
 508                fprintf(stderr, "Checking HEAD link\n");
 509
 510        head_points_at = resolve_ref_unsafe("HEAD", 0, head_oid.hash, NULL);
 511        if (!head_points_at) {
 512                errors_found |= ERROR_REFS;
 513                return error("Invalid HEAD");
 514        }
 515        if (!strcmp(head_points_at, "HEAD"))
 516                /* detached HEAD */
 517                null_is_error = 1;
 518        else if (!starts_with(head_points_at, "refs/heads/")) {
 519                errors_found |= ERROR_REFS;
 520                return error("HEAD points to something strange (%s)",
 521                             head_points_at);
 522        }
 523        if (is_null_oid(&head_oid)) {
 524                if (null_is_error) {
 525                        errors_found |= ERROR_REFS;
 526                        return error("HEAD: detached HEAD points at nothing");
 527                }
 528                fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
 529                        head_points_at + 11);
 530        }
 531        return 0;
 532}
 533
 534static int fsck_cache_tree(struct cache_tree *it)
 535{
 536        int i;
 537        int err = 0;
 538
 539        if (verbose)
 540                fprintf(stderr, "Checking cache tree\n");
 541
 542        if (0 <= it->entry_count) {
 543                struct object *obj = parse_object(it->sha1);
 544                if (!obj) {
 545                        error("%s: invalid sha1 pointer in cache-tree",
 546                              sha1_to_hex(it->sha1));
 547                        errors_found |= ERROR_REFS;
 548                        return 1;
 549                }
 550                obj->used = 1;
 551                mark_object_reachable(obj);
 552                if (obj->type != OBJ_TREE)
 553                        err |= objerror(obj, "non-tree in cache-tree");
 554        }
 555        for (i = 0; i < it->subtree_nr; i++)
 556                err |= fsck_cache_tree(it->down[i]->cache_tree);
 557        return err;
 558}
 559
 560static char const * const fsck_usage[] = {
 561        N_("git fsck [<options>] [<object>...]"),
 562        NULL
 563};
 564
 565static struct option fsck_opts[] = {
 566        OPT__VERBOSE(&verbose, N_("be verbose")),
 567        OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
 568        OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
 569        OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
 570        OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
 571        OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
 572        OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
 573        OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
 574        OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
 575        OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
 576        OPT_BOOL(0, "lost-found", &write_lost_and_found,
 577                                N_("write dangling objects in .git/lost-found")),
 578        OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
 579        OPT_END(),
 580};
 581
 582int cmd_fsck(int argc, const char **argv, const char *prefix)
 583{
 584        int i, heads;
 585        struct alternate_object_database *alt;
 586
 587        errors_found = 0;
 588        check_replace_refs = 0;
 589
 590        argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
 591
 592        fsck_walk_options.walk = mark_object;
 593        fsck_obj_options.walk = mark_used;
 594        fsck_obj_options.error_func = fsck_error_func;
 595        if (check_strict)
 596                fsck_obj_options.strict = 1;
 597
 598        if (show_progress == -1)
 599                show_progress = isatty(2);
 600        if (verbose)
 601                show_progress = 0;
 602
 603        if (write_lost_and_found) {
 604                check_full = 1;
 605                include_reflogs = 0;
 606        }
 607
 608        git_config(fsck_config, NULL);
 609
 610        fsck_head_link();
 611        if (!connectivity_only) {
 612                fsck_object_dir(get_object_directory());
 613
 614                prepare_alt_odb();
 615                for (alt = alt_odb_list; alt; alt = alt->next) {
 616                        /* directory name, minus trailing slash */
 617                        size_t namelen = alt->name - alt->base - 1;
 618                        struct strbuf name = STRBUF_INIT;
 619                        strbuf_add(&name, alt->base, namelen);
 620                        fsck_object_dir(name.buf);
 621                        strbuf_release(&name);
 622                }
 623        }
 624
 625        if (check_full) {
 626                struct packed_git *p;
 627                uint32_t total = 0, count = 0;
 628                struct progress *progress = NULL;
 629
 630                prepare_packed_git();
 631
 632                if (show_progress) {
 633                        for (p = packed_git; p; p = p->next) {
 634                                if (open_pack_index(p))
 635                                        continue;
 636                                total += p->num_objects;
 637                        }
 638
 639                        progress = start_progress(_("Checking objects"), total);
 640                }
 641                for (p = packed_git; p; p = p->next) {
 642                        /* verify gives error messages itself */
 643                        if (verify_pack(p, fsck_obj_buffer,
 644                                        progress, count))
 645                                errors_found |= ERROR_PACK;
 646                        count += p->num_objects;
 647                }
 648                stop_progress(&progress);
 649        }
 650
 651        heads = 0;
 652        for (i = 0; i < argc; i++) {
 653                const char *arg = argv[i];
 654                unsigned char sha1[20];
 655                if (!get_sha1(arg, sha1)) {
 656                        struct object *obj = lookup_object(sha1);
 657
 658                        /* Error is printed by lookup_object(). */
 659                        if (!obj)
 660                                continue;
 661
 662                        obj->used = 1;
 663                        mark_object_reachable(obj);
 664                        heads++;
 665                        continue;
 666                }
 667                error("invalid parameter: expected sha1, got '%s'", arg);
 668        }
 669
 670        /*
 671         * If we've not been given any explicit head information, do the
 672         * default ones from .git/refs. We also consider the index file
 673         * in this case (ie this implies --cache).
 674         */
 675        if (!heads) {
 676                get_default_heads();
 677                keep_cache_objects = 1;
 678        }
 679
 680        if (keep_cache_objects) {
 681                read_cache();
 682                for (i = 0; i < active_nr; i++) {
 683                        unsigned int mode;
 684                        struct blob *blob;
 685                        struct object *obj;
 686
 687                        mode = active_cache[i]->ce_mode;
 688                        if (S_ISGITLINK(mode))
 689                                continue;
 690                        blob = lookup_blob(active_cache[i]->sha1);
 691                        if (!blob)
 692                                continue;
 693                        obj = &blob->object;
 694                        obj->used = 1;
 695                        mark_object_reachable(obj);
 696                }
 697                if (active_cache_tree)
 698                        fsck_cache_tree(active_cache_tree);
 699        }
 700
 701        check_connectivity();
 702        return errors_found;
 703}