fsck-cache.con commit Make git-*-pull say who wants them for missing objects. (b2d62f1)
   1#include <sys/types.h>
   2#include <dirent.h>
   3
   4#include "cache.h"
   5#include "commit.h"
   6#include "tree.h"
   7#include "blob.h"
   8#include "tag.h"
   9
  10#define REACHABLE 0x0001
  11
  12static int show_root = 0;
  13static int show_tags = 0;
  14static int show_unreachable = 0;
  15static unsigned char head_sha1[20];
  16
  17static void check_connectivity(void)
  18{
  19        int i;
  20
  21        /* Look up all the requirements, warn about missing objects.. */
  22        for (i = 0; i < nr_objs; i++) {
  23                struct object *obj = objs[i];
  24                struct object_list *refs;
  25
  26                if (!obj->parsed) {
  27                        printf("missing %s %s\n", obj->type, sha1_to_hex(obj->sha1));
  28                        continue;
  29                }
  30
  31                for (refs = obj->refs; refs; refs = refs->next) {
  32                        if (refs->item->parsed)
  33                                continue;
  34                        printf("broken link from %7s %s\n",
  35                               obj->type, sha1_to_hex(obj->sha1));
  36                        printf("              to %7s %s\n",
  37                               refs->item->type, sha1_to_hex(refs->item->sha1));
  38                }
  39
  40                /* Don't bother with tag reachability. */
  41                if (obj->type == tag_type)
  42                        continue;
  43
  44                if (show_unreachable && !(obj->flags & REACHABLE)) {
  45                        printf("unreachable %s %s\n", obj->type, sha1_to_hex(obj->sha1));
  46                        continue;
  47                }
  48
  49                if (!obj->used) {
  50                        printf("dangling %s %s\n", obj->type, 
  51                               sha1_to_hex(obj->sha1));
  52                }
  53        }
  54}
  55
  56/*
  57 * The entries in a tree are ordered in the _path_ order,
  58 * which means that a directory entry is ordered by adding
  59 * a slash to the end of it.
  60 *
  61 * So a directory called "a" is ordered _after_ a file
  62 * called "a.c", because "a/" sorts after "a.c".
  63 */
  64static int verify_ordered(struct tree_entry_list *a, struct tree_entry_list *b)
  65{
  66        int len1 = strlen(a->name);
  67        int len2 = strlen(b->name);
  68        int len = len1 < len2 ? len1 : len2;
  69        unsigned char c1, c2;
  70        int cmp;
  71
  72        cmp = memcmp(a->name, b->name, len);
  73        if (cmp < 0)
  74                return 0;
  75        if (cmp > 0)
  76                return -1;
  77
  78        /*
  79         * Ok, the first <len> characters are the same.
  80         * Now we need to order the next one, but turn
  81         * a '\0' into a '/' for a directory entry.
  82         */
  83        c1 = a->name[len];
  84        c2 = b->name[len];
  85        if (!c1 && a->directory)
  86                c1 = '/';
  87        if (!c2 && b->directory)
  88                c2 = '/';
  89        return c1 < c2 ? 0 : -1;
  90}
  91
  92static int fsck_tree(struct tree *item)
  93{
  94        int has_full_path = 0;
  95        struct tree_entry_list *entry, *last;
  96
  97        last = NULL;
  98        for (entry = item->entries; entry; entry = entry->next) {
  99                if (strchr(entry->name, '/'))
 100                        has_full_path = 1;
 101
 102                if (last) {
 103                        if (verify_ordered(last, entry) < 0) {
 104                                fprintf(stderr, "tree %s not ordered\n",
 105                                        sha1_to_hex(item->object.sha1));
 106                                return -1;
 107                        }
 108                }
 109
 110                last = entry;
 111        }
 112
 113        if (has_full_path) {
 114                fprintf(stderr, "warning: fsck-cache: tree %s "
 115                        "has full pathnames in it\n", 
 116                        sha1_to_hex(item->object.sha1));
 117        }
 118
 119        return 0;
 120}
 121
 122static int fsck_commit(struct commit *commit)
 123{
 124        if (!commit->tree)
 125                return -1;
 126        if (!commit->parents && show_root)
 127                printf("root %s\n", sha1_to_hex(commit->object.sha1));
 128        if (!commit->date)
 129                printf("bad commit date in %s\n", 
 130                       sha1_to_hex(commit->object.sha1));
 131        return 0;
 132}
 133
 134static int fsck_tag(struct tag *tag)
 135{
 136        struct object *tagged = tag->tagged;
 137
 138        if (!tagged) {
 139                printf("bad object in tag %s\n", sha1_to_hex(tag->object.sha1));
 140                return -1;
 141        }
 142        if (!show_tags)
 143                return 0;
 144
 145        printf("tagged %s %s", tagged->type, sha1_to_hex(tagged->sha1));
 146        printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
 147        return 0;
 148}
 149
 150static int fsck_sha1(unsigned char *sha1)
 151{
 152        struct object *obj = parse_object(sha1);
 153        if (!obj)
 154                return -1;
 155        if (obj->type == blob_type)
 156                return 0;
 157        if (obj->type == tree_type)
 158                return fsck_tree((struct tree *) obj);
 159        if (obj->type == commit_type)
 160                return fsck_commit((struct commit *) obj);
 161        if (obj->type == tag_type)
 162                return fsck_tag((struct tag *) obj);
 163        return -1;
 164}
 165
 166/*
 167 * This is the sorting chunk size: make it reasonably
 168 * big so that we can sort well..
 169 */
 170#define MAX_SHA1_ENTRIES (1024)
 171
 172struct sha1_entry {
 173        unsigned long ino;
 174        unsigned char sha1[20];
 175};
 176
 177static struct {
 178        unsigned long nr;
 179        struct sha1_entry *entry[MAX_SHA1_ENTRIES];
 180} sha1_list;
 181
 182static int ino_compare(const void *_a, const void *_b)
 183{
 184        const struct sha1_entry *a = _a, *b = _b;
 185        unsigned long ino1 = a->ino, ino2 = b->ino;
 186        return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
 187}
 188
 189static void fsck_sha1_list(void)
 190{
 191        int i, nr = sha1_list.nr;
 192
 193        qsort(sha1_list.entry, nr, sizeof(struct sha1_entry *), ino_compare);
 194        for (i = 0; i < nr; i++) {
 195                struct sha1_entry *entry = sha1_list.entry[i];
 196                unsigned char *sha1 = entry->sha1;
 197
 198                sha1_list.entry[i] = NULL;
 199                if (fsck_sha1(sha1) < 0)
 200                        fprintf(stderr, "bad sha1 entry '%s'\n", sha1_to_hex(sha1));
 201                free(entry);
 202        }
 203        sha1_list.nr = 0;
 204}
 205
 206static void add_sha1_list(unsigned char *sha1, unsigned long ino)
 207{
 208        struct sha1_entry *entry = xmalloc(sizeof(*entry));
 209        int nr;
 210
 211        entry->ino = ino;
 212        memcpy(entry->sha1, sha1, 20);
 213        nr = sha1_list.nr;
 214        if (nr == MAX_SHA1_ENTRIES) {
 215                fsck_sha1_list();
 216                nr = 0;
 217        }
 218        sha1_list.entry[nr] = entry;
 219        sha1_list.nr = ++nr;
 220}
 221
 222static int fsck_dir(int i, char *path)
 223{
 224        DIR *dir = opendir(path);
 225        struct dirent *de;
 226
 227        if (!dir) {
 228                return error("missing sha1 directory '%s'", path);
 229        }
 230
 231        while ((de = readdir(dir)) != NULL) {
 232                char name[100];
 233                unsigned char sha1[20];
 234                int len = strlen(de->d_name);
 235
 236                switch (len) {
 237                case 2:
 238                        if (de->d_name[1] != '.')
 239                                break;
 240                case 1:
 241                        if (de->d_name[0] != '.')
 242                                break;
 243                        continue;
 244                case 38:
 245                        sprintf(name, "%02x", i);
 246                        memcpy(name+2, de->d_name, len+1);
 247                        if (get_sha1_hex(name, sha1) < 0)
 248                                break;
 249                        add_sha1_list(sha1, de->d_ino);
 250                        continue;
 251                }
 252                fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
 253        }
 254        closedir(dir);
 255        return 0;
 256}
 257
 258int main(int argc, char **argv)
 259{
 260        int i, heads;
 261        char *sha1_dir;
 262
 263        for (i = 1; i < argc; i++) {
 264                const char *arg = argv[i];
 265
 266                if (!strcmp(arg, "--unreachable")) {
 267                        show_unreachable = 1;
 268                        continue;
 269                }
 270                if (!strcmp(arg, "--tags")) {
 271                        show_tags = 1;
 272                        continue;
 273                }
 274                if (!strcmp(arg, "--root")) {
 275                        show_root = 1;
 276                        continue;
 277                }
 278                if (*arg == '-')
 279                        usage("fsck-cache [--tags] [[--unreachable] <head-sha1>*]");
 280        }
 281
 282        sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
 283        for (i = 0; i < 256; i++) {
 284                static char dir[4096];
 285                sprintf(dir, "%s/%02x", sha1_dir, i);
 286                fsck_dir(i, dir);
 287        }
 288        fsck_sha1_list();
 289
 290        heads = 0;
 291        for (i = 1; i < argc; i++) {
 292                const char *arg = argv[i]; 
 293
 294                if (*arg == '-')
 295                        continue;
 296
 297                if (!get_sha1(arg, head_sha1)) {
 298                        struct commit *commit = lookup_commit(head_sha1);
 299                        struct object *obj;
 300
 301                        /* Error is printed by lookup_commit(). */
 302                        if (!commit)
 303                                continue;
 304
 305                        obj = &commit->object;
 306                        obj->used = 1;
 307                        mark_reachable(obj, REACHABLE);
 308                        heads++;
 309                        continue;
 310                }
 311                error("expected sha1, got %s", arg);
 312        }
 313
 314        if (!heads) {
 315                if (show_unreachable) {
 316                        fprintf(stderr, "unable to do reachability without a head\n");
 317                        show_unreachable = 0; 
 318                }
 319                fprintf(stderr, "expect dangling commits - potential heads - due to lack of head information\n");
 320        }
 321
 322        check_connectivity();
 323        return 0;
 324}