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