12443b165578fc458ea3fa66999ed77641aad209
   1#include "cache.h"
   2
   3#include <sys/types.h>
   4#include <dirent.h>
   5
   6#include "commit.h"
   7#include "tree.h"
   8#include "blob.h"
   9
  10#define REACHABLE 0x0001
  11
  12static int show_unreachable = 0;
  13static unsigned char head_sha1[20];
  14
  15static void check_connectivity(void)
  16{
  17        int i;
  18
  19        /* Look up all the requirements, warn about missing objects.. */
  20        for (i = 0; i < nr_objs; i++) {
  21                struct object *obj = objs[i];
  22
  23                if (show_unreachable && !(obj->flags & REACHABLE)) {
  24                        printf("unreachable %s %s\n", obj->type, sha1_to_hex(obj->sha1));
  25                        continue;
  26                }
  27
  28                if (!obj->parsed) {
  29                        printf("missing %s %s\n", obj->type, 
  30                               sha1_to_hex(obj->sha1));
  31                }
  32                if (!obj->used) {
  33                        printf("dangling %s %s\n", obj->type, 
  34                               sha1_to_hex(obj->sha1));
  35                }
  36        }
  37}
  38
  39static int fsck_tree(unsigned char *sha1, void *data, unsigned long size)
  40{
  41        struct tree *item = lookup_tree(sha1);
  42        if (parse_tree(item))
  43                return -1;
  44        if (item->has_full_path) {
  45                fprintf(stderr, "warning: fsck-cache: tree %s "
  46                        "has full pathnames in it\n", sha1_to_hex(sha1));
  47        }
  48        return 0;
  49}
  50
  51static int fsck_commit(unsigned char *sha1, void *data, unsigned long size)
  52{
  53        struct commit *commit = lookup_commit(sha1);
  54        if (parse_commit(commit))
  55                return -1;
  56        if (!commit->tree)
  57                return -1;
  58        if (!commit->parents)
  59                printf("root %s\n", sha1_to_hex(sha1));
  60        if (!commit->date)
  61                printf("bad commit date in %s\n", sha1_to_hex(sha1));
  62        return 0;
  63}
  64
  65static int fsck_blob(unsigned char *sha1, void *data, unsigned long size)
  66{
  67        struct blob *blob = lookup_blob(sha1);
  68        blob->object.parsed = 1;
  69        return 0;
  70}
  71
  72static int fsck_tag(unsigned char *sha1, void *data, unsigned long size)
  73{
  74        int typelen, taglen;
  75        unsigned char object[20];
  76        const char *type_line, *tag_line, *sig_line;
  77
  78        if (size < 64)
  79                return -1;
  80        if (memcmp("object ", data, 7) || get_sha1_hex(data + 7, object))
  81                return -1;
  82
  83        type_line = data + 48;
  84        if (memcmp("\ntype ", type_line-1, 6))
  85                return -1;
  86
  87        tag_line = strchr(type_line, '\n');
  88        if (!tag_line || memcmp("tag ", ++tag_line, 4))
  89                return -1;
  90
  91        sig_line = strchr(tag_line, '\n');
  92        if (!sig_line)
  93                return -1;
  94        sig_line++;
  95
  96        typelen = tag_line - type_line - strlen("type \n");
  97        if (typelen >= 20)
  98                return -1;
  99        taglen = sig_line - tag_line - strlen("tag \n");
 100
 101        printf("tagged %.*s %s (%.*s)\n",
 102                typelen, type_line + 5,
 103                sha1_to_hex(object),
 104                taglen, tag_line + 4);
 105        return 0;
 106}
 107
 108static int fsck_entry(unsigned char *sha1, char *tag, void *data, 
 109                      unsigned long size)
 110{
 111        if (!strcmp(tag, "blob")) {
 112                if (fsck_blob(sha1, data, size) < 0)
 113                        return -1;
 114        } else if (!strcmp(tag, "tree")) {
 115                if (fsck_tree(sha1, data, size) < 0)
 116                        return -1;
 117        } else if (!strcmp(tag, "commit")) {
 118                if (fsck_commit(sha1, data, size) < 0)
 119                        return -1;
 120        } else if (!strcmp(tag, "tag")) {
 121                if (fsck_tag(sha1, data, size) < 0)
 122                        return -1;
 123        } else
 124                return -1;
 125        return 0;
 126}
 127
 128static int fsck_name(char *hex)
 129{
 130        unsigned char sha1[20];
 131        if (!get_sha1_hex(hex, sha1)) {
 132                unsigned long mapsize;
 133                void *map = map_sha1_file(sha1, &mapsize);
 134                if (map) {
 135                        char type[100];
 136                        unsigned long size;
 137                        void *buffer = unpack_sha1_file(map, mapsize, type, &size);
 138                        if (!buffer)
 139                                return -1;
 140                        if (check_sha1_signature(sha1, buffer, size, type) < 0)
 141                                printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
 142                        munmap(map, mapsize);
 143                        if (!fsck_entry(sha1, type, buffer, size))
 144                                return 0;
 145                }
 146        }
 147        return -1;
 148}
 149
 150static int fsck_dir(int i, char *path)
 151{
 152        DIR *dir = opendir(path);
 153        struct dirent *de;
 154
 155        if (!dir) {
 156                return error("missing sha1 directory '%s'", path);
 157        }
 158
 159        while ((de = readdir(dir)) != NULL) {
 160                char name[100];
 161                int len = strlen(de->d_name);
 162
 163                switch (len) {
 164                case 2:
 165                        if (de->d_name[1] != '.')
 166                                break;
 167                case 1:
 168                        if (de->d_name[0] != '.')
 169                                break;
 170                        continue;
 171                case 38:
 172                        sprintf(name, "%02x", i);
 173                        memcpy(name+2, de->d_name, len+1);
 174                        if (!fsck_name(name))
 175                                continue;
 176                }
 177                fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
 178        }
 179        closedir(dir);
 180        return 0;
 181}
 182
 183int main(int argc, char **argv)
 184{
 185        int i, heads;
 186        char *sha1_dir;
 187
 188        sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
 189        for (i = 0; i < 256; i++) {
 190                static char dir[4096];
 191                sprintf(dir, "%s/%02x", sha1_dir, i);
 192                fsck_dir(i, dir);
 193        }
 194
 195        heads = 0;
 196        for (i = 1; i < argc; i++) {
 197                if (!strcmp(argv[i], "--unreachable")) {
 198                        show_unreachable = 1;
 199                        continue;
 200                }
 201                if (!get_sha1_hex(argv[i], head_sha1)) {
 202                        struct object *obj = &lookup_commit(head_sha1)->object;
 203                        obj->used = 1;
 204                        mark_reachable(obj, REACHABLE);
 205                        heads++;
 206                        continue;
 207                }
 208                error("fsck-cache [[--unreachable] <head-sha1>*]");
 209        }
 210
 211        if (!heads) {
 212                if (show_unreachable) {
 213                        fprintf(stderr, "unable to do reachability without a head\n");
 214                        show_unreachable = 0; 
 215                }
 216                fprintf(stderr, "expect dangling commits - potential heads - due to lack of head information\n");
 217        }
 218
 219        check_connectivity();
 220        return 0;
 221}