10 #define REACHABLE 0x0001
12 static int show_unreachable = 0;
13 static unsigned char head_sha1[20];
15 static void check_connectivity(void)
19 /* Look up all the requirements, warn about missing objects.. */
20 for (i = 0; i < nr_objs; i++) {
21 struct object *obj = objs[i];
23 if (show_unreachable && !(obj->flags & REACHABLE)) {
24 printf("unreachable %s %s\n", obj->type, sha1_to_hex(obj->sha1));
29 printf("missing %s %s\n", obj->type,
30 sha1_to_hex(obj->sha1));
33 printf("dangling %s %s\n", obj->type,
34 sha1_to_hex(obj->sha1));
39 static int fsck_tree(unsigned char *sha1, void *data, unsigned long size)
41 struct tree *item = lookup_tree(sha1);
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));
51 static int fsck_commit(unsigned char *sha1, void *data, unsigned long size)
53 struct commit *commit = lookup_commit(sha1);
54 if (parse_commit(commit))
59 printf("root %s\n", sha1_to_hex(sha1));
61 printf("bad commit date in %s\n", sha1_to_hex(sha1));
65 static int fsck_blob(unsigned char *sha1, void *data, unsigned long size)
67 struct blob *blob = lookup_blob(sha1);
68 blob->object.parsed = 1;
72 static int fsck_entry(unsigned char *sha1, char *tag, void *data,
75 if (!strcmp(tag, "blob")) {
76 if (fsck_blob(sha1, data, size) < 0)
78 } else if (!strcmp(tag, "tree")) {
79 if (fsck_tree(sha1, data, size) < 0)
81 } else if (!strcmp(tag, "commit")) {
82 if (fsck_commit(sha1, data, size) < 0)
89 static int fsck_name(char *hex)
91 unsigned char sha1[20];
92 if (!get_sha1_hex(hex, sha1)) {
93 unsigned long mapsize;
94 void *map = map_sha1_file(sha1, &mapsize);
98 void *buffer = unpack_sha1_file(map, mapsize, type, &size);
101 if (check_sha1_signature(sha1, buffer, size, type) < 0)
102 printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
103 munmap(map, mapsize);
104 if (!fsck_entry(sha1, type, buffer, size))
111 static int fsck_dir(int i, char *path)
113 DIR *dir = opendir(path);
117 return error("missing sha1 directory '%s'", path);
120 while ((de = readdir(dir)) != NULL) {
122 int len = strlen(de->d_name);
126 if (de->d_name[1] != '.')
129 if (de->d_name[0] != '.')
133 sprintf(name, "%02x", i);
134 memcpy(name+2, de->d_name, len+1);
135 if (!fsck_name(name))
138 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
144 int main(int argc, char **argv)
149 sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
150 for (i = 0; i < 256; i++) {
151 static char dir[4096];
152 sprintf(dir, "%s/%02x", sha1_dir, i);
157 for (i = 1; i < argc; i++) {
158 if (!strcmp(argv[i], "--unreachable")) {
159 show_unreachable = 1;
162 if (!get_sha1_hex(argv[i], head_sha1)) {
163 struct object *obj = &lookup_commit(head_sha1)->object;
165 mark_reachable(obj, REACHABLE);
169 error("fsck-cache [[--unreachable] <head-sha1>*]");
173 if (show_unreachable) {
174 fprintf(stderr, "unable to do reachability without a head\n");
175 show_unreachable = 0;
177 fprintf(stderr, "expect dangling commits - potential heads - due to lack of head information\n");
180 check_connectivity();