10 #define REACHABLE 0x0001
12 static int show_tags = 0;
13 static int show_unreachable = 0;
14 static unsigned char head_sha1[20];
16 static void check_connectivity(void)
20 /* Look up all the requirements, warn about missing objects.. */
21 for (i = 0; i < nr_objs; i++) {
22 struct object *obj = objs[i];
24 if (show_unreachable && !(obj->flags & REACHABLE)) {
25 printf("unreachable %s %s\n", obj->type, sha1_to_hex(obj->sha1));
30 printf("missing %s %s\n", obj->type,
31 sha1_to_hex(obj->sha1));
34 printf("dangling %s %s\n", obj->type,
35 sha1_to_hex(obj->sha1));
40 static int fsck_tree(unsigned char *sha1, void *data, unsigned long size)
42 struct tree *item = lookup_tree(sha1);
45 if (item->has_full_path) {
46 fprintf(stderr, "warning: fsck-cache: tree %s "
47 "has full pathnames in it\n", sha1_to_hex(sha1));
52 static int fsck_commit(unsigned char *sha1, void *data, unsigned long size)
54 struct commit *commit = lookup_commit(sha1);
55 if (parse_commit(commit))
60 printf("root %s\n", sha1_to_hex(sha1));
62 printf("bad commit date in %s\n", sha1_to_hex(sha1));
66 static int fsck_blob(unsigned char *sha1, void *data, unsigned long size)
68 struct blob *blob = lookup_blob(sha1);
69 blob->object.parsed = 1;
73 static int fsck_tag(unsigned char *sha1, void *data, unsigned long size)
76 unsigned char object[20];
78 const char *type_line, *tag_line, *sig_line;
82 if (memcmp("object ", data, 7) || get_sha1_hex(data + 7, object))
85 type_line = data + 48;
86 if (memcmp("\ntype ", type_line-1, 6))
89 tag_line = strchr(type_line, '\n');
90 if (!tag_line || memcmp("tag ", ++tag_line, 4))
93 sig_line = strchr(tag_line, '\n');
98 typelen = tag_line - type_line - strlen("type \n");
101 taglen = sig_line - tag_line - strlen("tag \n");
106 strcpy(object_hex, sha1_to_hex(object));
107 printf("tagged %.*s %s (%.*s) in %s\n",
108 typelen, type_line + 5,
110 taglen, tag_line + 4,
115 static int fsck_entry(unsigned char *sha1, char *tag, void *data,
118 if (!strcmp(tag, "blob")) {
119 if (fsck_blob(sha1, data, size) < 0)
121 } else if (!strcmp(tag, "tree")) {
122 if (fsck_tree(sha1, data, size) < 0)
124 } else if (!strcmp(tag, "commit")) {
125 if (fsck_commit(sha1, data, size) < 0)
127 } else if (!strcmp(tag, "tag")) {
128 if (fsck_tag(sha1, data, size) < 0)
135 static int fsck_name(char *hex)
137 unsigned char sha1[20];
138 if (!get_sha1_hex(hex, sha1)) {
139 unsigned long mapsize;
140 void *map = map_sha1_file(sha1, &mapsize);
144 void *buffer = unpack_sha1_file(map, mapsize, type, &size);
147 if (check_sha1_signature(sha1, buffer, size, type) < 0)
148 printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
149 munmap(map, mapsize);
150 if (!fsck_entry(sha1, type, buffer, size))
157 static int fsck_dir(int i, char *path)
159 DIR *dir = opendir(path);
163 return error("missing sha1 directory '%s'", path);
166 while ((de = readdir(dir)) != NULL) {
168 int len = strlen(de->d_name);
172 if (de->d_name[1] != '.')
175 if (de->d_name[0] != '.')
179 sprintf(name, "%02x", i);
180 memcpy(name+2, de->d_name, len+1);
181 if (!fsck_name(name))
184 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
190 int main(int argc, char **argv)
195 for (i = 1; i < argc; i++) {
196 const char *arg = argv[i];
198 if (!strcmp(arg, "--unreachable")) {
199 show_unreachable = 1;
202 if (!strcmp(arg, "--tags")) {
207 usage("fsck-cache [--tags] [[--unreachable] <head-sha1>*]");
210 sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
211 for (i = 0; i < 256; i++) {
212 static char dir[4096];
213 sprintf(dir, "%s/%02x", sha1_dir, i);
218 for (i = 1; i < argc; i++) {
219 const char *arg = argv[i];
224 if (!get_sha1_hex(arg, head_sha1)) {
225 struct object *obj = &lookup_commit(head_sha1)->object;
227 mark_reachable(obj, REACHABLE);
231 error("expected sha1, got %s", arg);
235 if (show_unreachable) {
236 fprintf(stderr, "unable to do reachability without a head\n");
237 show_unreachable = 0;
239 fprintf(stderr, "expect dangling commits - potential heads - due to lack of head information\n");
242 check_connectivity();