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