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