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