fsck-cache.con commit [PATCH] Do not let rsync obliterate .git/object symbolic link. (0ffb0bc)
   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\n", 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        return 0;
  61}
  62
  63static int fsck_entry(unsigned char *sha1, char *tag, void *data, 
  64                      unsigned long size)
  65{
  66        if (!strcmp(tag, "blob")) {
  67                lookup_blob(sha1); /* Nothing to check; but notice it. */
  68        } else if (!strcmp(tag, "tree")) {
  69                if (fsck_tree(sha1, data, size) < 0)
  70                        return -1;
  71        } else if (!strcmp(tag, "commit")) {
  72                if (fsck_commit(sha1, data, size) < 0)
  73                        return -1;
  74        } else
  75                return -1;
  76        return 0;
  77}
  78
  79static int fsck_name(char *hex)
  80{
  81        unsigned char sha1[20];
  82        if (!get_sha1_hex(hex, sha1)) {
  83                unsigned long mapsize;
  84                void *map = map_sha1_file(sha1, &mapsize);
  85                if (map) {
  86                        char type[100];
  87                        unsigned long size;
  88                        void *buffer = NULL;
  89                        if (!check_sha1_signature(sha1, map, mapsize))
  90                                buffer = unpack_sha1_file(map, mapsize, type,
  91                                                          &size);
  92                        munmap(map, mapsize);
  93                        if (buffer && !fsck_entry(sha1, type, buffer, size))
  94                                return 0;
  95                }
  96        }
  97        return -1;
  98}
  99
 100static int fsck_dir(int i, char *path)
 101{
 102        DIR *dir = opendir(path);
 103        struct dirent *de;
 104
 105        if (!dir) {
 106                return error("missing sha1 directory '%s'", path);
 107        }
 108
 109        while ((de = readdir(dir)) != NULL) {
 110                char name[100];
 111                int len = strlen(de->d_name);
 112
 113                switch (len) {
 114                case 2:
 115                        if (de->d_name[1] != '.')
 116                                break;
 117                case 1:
 118                        if (de->d_name[0] != '.')
 119                                break;
 120                        continue;
 121                case 38:
 122                        sprintf(name, "%02x", i);
 123                        memcpy(name+2, de->d_name, len+1);
 124                        if (!fsck_name(name))
 125                                continue;
 126                }
 127                fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
 128        }
 129        closedir(dir);
 130        return 0;
 131}
 132
 133int main(int argc, char **argv)
 134{
 135        int i, heads;
 136        char *sha1_dir;
 137
 138        sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
 139        for (i = 0; i < 256; i++) {
 140                static char dir[4096];
 141                sprintf(dir, "%s/%02x", sha1_dir, i);
 142                fsck_dir(i, dir);
 143        }
 144
 145        heads = 0;
 146        for (i = 1; i < argc; i++) {
 147                if (!strcmp(argv[i], "--unreachable")) {
 148                        show_unreachable = 1;
 149                        continue;
 150                }
 151                if (!get_sha1_hex(argv[i], head_sha1)) {
 152                        struct object *obj = &lookup_commit(head_sha1)->object;
 153                        obj->used = 1;
 154                        mark_reachable(obj, REACHABLE);
 155                        heads++;
 156                        continue;
 157                }
 158                error("fsck-cache [[--unreachable] <head-sha1>*]");
 159        }
 160
 161        if (!heads) {
 162                if (show_unreachable) {
 163                        fprintf(stderr, "unable to do reachability without a head\n");
 164                        show_unreachable = 0; 
 165                }
 166                fprintf(stderr, "expect dangling commits - potential heads - due to lack of head information\n");
 167        }
 168
 169        check_connectivity();
 170        return 0;
 171}