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