]>
Commit | Line | Data |
---|---|---|
baffc0e7 | 1 | #include "builtin.h" |
4b182421 | 2 | #include "cache.h" |
ff5ebe39 DB |
3 | #include "commit.h" |
4 | #include "tree.h" | |
5 | #include "blob.h" | |
c418eda4 | 6 | #include "tag.h" |
944d8589 | 7 | #include "refs.h" |
f9253394 | 8 | #include "pack.h" |
53dc3f3e | 9 | #include "cache-tree.h" |
e9a95bef | 10 | #include "tree-walk.h" |
271b8d25 | 11 | #include "fsck.h" |
5ac0a206 | 12 | #include "parse-options.h" |
8ca12c0d | 13 | #include "dir.h" |
ff5ebe39 DB |
14 | |
15 | #define REACHABLE 0x0001 | |
2d9c58c6 | 16 | #define SEEN 0x0002 |
d9839e03 | 17 | |
96f1e58f DR |
18 | static int show_root; |
19 | static int show_tags; | |
20 | static int show_unreachable; | |
566842f6 | 21 | static int include_reflogs = 1; |
96f1e58f DR |
22 | static int check_full; |
23 | static int check_strict; | |
24 | static int keep_cache_objects; | |
d9839e03 | 25 | static unsigned char head_sha1[20]; |
469e2ebf | 26 | static const char *head_points_at; |
e2b4f635 | 27 | static int errors_found; |
68f6c019 | 28 | static int write_lost_and_found; |
20f1eb6b | 29 | static int verbose; |
e2b4f635 JH |
30 | #define ERROR_OBJECT 01 |
31 | #define ERROR_REACHABLE 02 | |
d9839e03 | 32 | |
962554c6 | 33 | #ifdef NO_D_INO_IN_DIRENT |
35a730f0 JH |
34 | #define SORT_DIRENT 0 |
35 | #define DIRENT_SORT_HINT(de) 0 | |
36 | #else | |
37 | #define SORT_DIRENT 1 | |
38 | #define DIRENT_SORT_HINT(de) ((de)->d_ino) | |
39 | #endif | |
f1f0d088 PB |
40 | |
41 | static void objreport(struct object *obj, const char *severity, | |
42 | const char *err, va_list params) | |
43 | { | |
44 | fprintf(stderr, "%s in %s %s: ", | |
885a86ab | 45 | severity, typename(obj->type), sha1_to_hex(obj->sha1)); |
f1f0d088 PB |
46 | vfprintf(stderr, err, params); |
47 | fputs("\n", stderr); | |
48 | } | |
49 | ||
a7928f8e | 50 | static int objerror(struct object *obj, const char *err, ...) |
f1f0d088 PB |
51 | { |
52 | va_list params; | |
53 | va_start(params, err); | |
e2b4f635 | 54 | errors_found |= ERROR_OBJECT; |
f1f0d088 PB |
55 | objreport(obj, "error", err, params); |
56 | va_end(params); | |
57 | return -1; | |
58 | } | |
59 | ||
ba002f3b | 60 | static int fsck_error_func(struct object *obj, int type, const char *err, ...) |
f1f0d088 PB |
61 | { |
62 | va_list params; | |
63 | va_start(params, err); | |
ba002f3b | 64 | objreport(obj, (type == FSCK_WARN) ? "warning" : "error", err, params); |
f1f0d088 | 65 | va_end(params); |
ba002f3b | 66 | return (type == FSCK_WARN) ? 0 : 1; |
f1f0d088 PB |
67 | } |
68 | ||
04d39759 LT |
69 | static struct object_array pending; |
70 | ||
271b8d25 MK |
71 | static int mark_object(struct object *obj, int type, void *data) |
72 | { | |
271b8d25 | 73 | struct object *parent = data; |
271b8d25 MK |
74 | |
75 | if (!obj) { | |
76 | printf("broken link from %7s %s\n", | |
77 | typename(parent->type), sha1_to_hex(parent->sha1)); | |
78 | printf("broken link from %7s %s\n", | |
79 | (type == OBJ_ANY ? "unknown" : typename(type)), "unknown"); | |
80 | errors_found |= ERROR_REACHABLE; | |
81 | return 1; | |
82 | } | |
83 | ||
84 | if (type != OBJ_ANY && obj->type != type) | |
85 | objerror(parent, "wrong object type in link"); | |
86 | ||
87 | if (obj->flags & REACHABLE) | |
88 | return 0; | |
89 | obj->flags |= REACHABLE; | |
90 | if (!obj->parsed) { | |
91 | if (parent && !has_sha1_file(obj->sha1)) { | |
92 | printf("broken link from %7s %s\n", | |
93 | typename(parent->type), sha1_to_hex(parent->sha1)); | |
94 | printf(" to %7s %s\n", | |
95 | typename(obj->type), sha1_to_hex(obj->sha1)); | |
96 | errors_found |= ERROR_REACHABLE; | |
97 | } | |
98 | return 1; | |
99 | } | |
100 | ||
04d39759 LT |
101 | add_object_array(obj, (void *) parent, &pending); |
102 | return 0; | |
103 | } | |
104 | ||
105 | static void mark_object_reachable(struct object *obj) | |
106 | { | |
107 | mark_object(obj, OBJ_ANY, 0); | |
108 | } | |
109 | ||
110 | static int traverse_one_object(struct object *obj, struct object *parent) | |
111 | { | |
112 | int result; | |
113 | struct tree *tree = NULL; | |
114 | ||
271b8d25 MK |
115 | if (obj->type == OBJ_TREE) { |
116 | obj->parsed = 0; | |
117 | tree = (struct tree *)obj; | |
118 | if (parse_tree(tree) < 0) | |
119 | return 1; /* error already displayed */ | |
120 | } | |
121 | result = fsck_walk(obj, mark_object, obj); | |
122 | if (tree) { | |
123 | free(tree->buffer); | |
124 | tree->buffer = NULL; | |
125 | } | |
271b8d25 MK |
126 | return result; |
127 | } | |
128 | ||
04d39759 | 129 | static int traverse_reachable(void) |
271b8d25 | 130 | { |
04d39759 LT |
131 | int result = 0; |
132 | while (pending.nr) { | |
133 | struct object_array_entry *entry; | |
134 | struct object *obj, *parent; | |
135 | ||
136 | entry = pending.objects + --pending.nr; | |
137 | obj = entry->item; | |
138 | parent = (struct object *) entry->name; | |
139 | result |= traverse_one_object(obj, parent); | |
140 | } | |
141 | return !!result; | |
271b8d25 MK |
142 | } |
143 | ||
144 | static int mark_used(struct object *obj, int type, void *data) | |
145 | { | |
146 | if (!obj) | |
147 | return 1; | |
148 | obj->used = 1; | |
149 | return 0; | |
f1f0d088 PB |
150 | } |
151 | ||
18af29f2 LT |
152 | /* |
153 | * Check a single reachable object | |
154 | */ | |
155 | static void check_reachable_object(struct object *obj) | |
156 | { | |
18af29f2 LT |
157 | /* |
158 | * We obviously want the object to be parsed, | |
159 | * except if it was in a pack-file and we didn't | |
160 | * do a full fsck | |
161 | */ | |
162 | if (!obj->parsed) { | |
cd673c1f | 163 | if (has_sha1_pack(obj->sha1)) |
18af29f2 LT |
164 | return; /* it is in pack - forget about it */ |
165 | printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1)); | |
e2b4f635 | 166 | errors_found |= ERROR_REACHABLE; |
18af29f2 LT |
167 | return; |
168 | } | |
18af29f2 LT |
169 | } |
170 | ||
171 | /* | |
172 | * Check a single unreachable object | |
173 | */ | |
174 | static void check_unreachable_object(struct object *obj) | |
175 | { | |
176 | /* | |
177 | * Missing unreachable object? Ignore it. It's not like | |
178 | * we miss it (since it can't be reached), nor do we want | |
179 | * to complain about it being unreachable (since it does | |
180 | * not exist). | |
181 | */ | |
182 | if (!obj->parsed) | |
183 | return; | |
184 | ||
185 | /* | |
186 | * Unreachable object that exists? Show it if asked to, | |
187 | * since this is something that is prunable. | |
188 | */ | |
189 | if (show_unreachable) { | |
190 | printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1)); | |
191 | return; | |
192 | } | |
193 | ||
194 | /* | |
195 | * "!used" means that nothing at all points to it, including | |
3dff5379 | 196 | * other unreachable objects. In other words, it's the "tip" |
18af29f2 LT |
197 | * of some set of unreachable objects, usually a commit that |
198 | * got dropped. | |
199 | * | |
200 | * Such starting points are more interesting than some random | |
201 | * set of unreachable objects, so we show them even if the user | |
202 | * hasn't asked for _all_ unreachable objects. If you have | |
203 | * deleted a branch by mistake, this is a prime candidate to | |
204 | * start looking at, for example. | |
205 | */ | |
206 | if (!obj->used) { | |
207 | printf("dangling %s %s\n", typename(obj->type), | |
208 | sha1_to_hex(obj->sha1)); | |
68f6c019 JS |
209 | if (write_lost_and_found) { |
210 | char *filename = git_path("lost-found/%s/%s", | |
211 | obj->type == OBJ_COMMIT ? "commit" : "other", | |
212 | sha1_to_hex(obj->sha1)); | |
213 | FILE *f; | |
214 | ||
215 | if (safe_create_leading_directories(filename)) { | |
216 | error("Could not create lost-found"); | |
217 | return; | |
218 | } | |
219 | if (!(f = fopen(filename, "w"))) | |
220 | die("Could not open %s", filename); | |
16a7fcfe JS |
221 | if (obj->type == OBJ_BLOB) { |
222 | enum object_type type; | |
223 | unsigned long size; | |
224 | char *buf = read_sha1_file(obj->sha1, | |
225 | &type, &size); | |
226 | if (buf) { | |
47d32af2 AR |
227 | if (fwrite(buf, size, 1, f) != 1) |
228 | die("Could not write %s: %s", | |
229 | filename, strerror(errno)); | |
16a7fcfe JS |
230 | free(buf); |
231 | } | |
232 | } else | |
233 | fprintf(f, "%s\n", sha1_to_hex(obj->sha1)); | |
47d32af2 AR |
234 | if (fclose(f)) |
235 | die("Could not finish %s: %s", | |
236 | filename, strerror(errno)); | |
68f6c019 | 237 | } |
18af29f2 LT |
238 | return; |
239 | } | |
240 | ||
241 | /* | |
242 | * Otherwise? It's there, it's unreachable, and some other unreachable | |
243 | * object points to it. Ignore it - it's not interesting, and we showed | |
244 | * all the interesting cases above. | |
245 | */ | |
246 | } | |
247 | ||
248 | static void check_object(struct object *obj) | |
249 | { | |
20f1eb6b JS |
250 | if (verbose) |
251 | fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1)); | |
252 | ||
18af29f2 LT |
253 | if (obj->flags & REACHABLE) |
254 | check_reachable_object(obj); | |
255 | else | |
256 | check_unreachable_object(obj); | |
257 | } | |
f1f0d088 | 258 | |
8ba0bbb2 LT |
259 | static void check_connectivity(void) |
260 | { | |
fc046a75 | 261 | int i, max; |
8ba0bbb2 | 262 | |
04d39759 LT |
263 | /* Traverse the pending reachable objects */ |
264 | traverse_reachable(); | |
265 | ||
8ba0bbb2 | 266 | /* Look up all the requirements, warn about missing objects.. */ |
fc046a75 | 267 | max = get_max_object_index(); |
20f1eb6b JS |
268 | if (verbose) |
269 | fprintf(stderr, "Checking connectivity (%d objects)\n", max); | |
270 | ||
fc046a75 | 271 | for (i = 0; i < max; i++) { |
fc046a75 | 272 | struct object *obj = get_indexed_object(i); |
8ba0bbb2 | 273 | |
18af29f2 LT |
274 | if (obj) |
275 | check_object(obj); | |
8ba0bbb2 LT |
276 | } |
277 | } | |
278 | ||
ba002f3b | 279 | static int fsck_sha1(const unsigned char *sha1) |
85003492 | 280 | { |
ba002f3b MK |
281 | struct object *obj = parse_object(sha1); |
282 | if (!obj) { | |
283 | errors_found |= ERROR_OBJECT; | |
284 | return error("%s: object corrupt or missing", | |
285 | sha1_to_hex(sha1)); | |
286 | } | |
287 | if (obj->flags & SEEN) | |
85003492 | 288 | return 0; |
ba002f3b | 289 | obj->flags |= SEEN; |
85003492 | 290 | |
20f1eb6b | 291 | if (verbose) |
ba002f3b MK |
292 | fprintf(stderr, "Checking %s %s\n", |
293 | typename(obj->type), sha1_to_hex(obj->sha1)); | |
42ea9cb2 | 294 | |
ba002f3b MK |
295 | if (fsck_walk(obj, mark_used, 0)) |
296 | objerror(obj, "broken links"); | |
297 | if (fsck_object(obj, check_strict, fsck_error_func)) | |
298 | return -1; | |
85003492 | 299 | |
ba002f3b MK |
300 | if (obj->type == OBJ_TREE) { |
301 | struct tree *item = (struct tree *) obj; | |
85003492 | 302 | |
ba002f3b MK |
303 | free(item->buffer); |
304 | item->buffer = NULL; | |
1ea34e36 | 305 | } |
1ea34e36 | 306 | |
ba002f3b MK |
307 | if (obj->type == OBJ_COMMIT) { |
308 | struct commit *commit = (struct commit *) obj; | |
de2eb7f6 | 309 | |
ba002f3b MK |
310 | free(commit->buffer); |
311 | commit->buffer = NULL; | |
4728b861 | 312 | |
ba002f3b MK |
313 | if (!commit->parents && show_root) |
314 | printf("root %s\n", sha1_to_hex(commit->object.sha1)); | |
315 | } | |
92d4c85d | 316 | |
ba002f3b MK |
317 | if (obj->type == OBJ_TAG) { |
318 | struct tag *tag = (struct tag *) obj; | |
20f1eb6b | 319 | |
ba002f3b MK |
320 | if (show_tags && tag->tagged) { |
321 | printf("tagged %s %s", typename(tag->tagged->type), sha1_to_hex(tag->tagged->sha1)); | |
322 | printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1)); | |
323 | } | |
92d4c85d | 324 | } |
889262ea | 325 | |
ff5ebe39 | 326 | return 0; |
20222118 LT |
327 | } |
328 | ||
7e8c174a LT |
329 | /* |
330 | * This is the sorting chunk size: make it reasonably | |
331 | * big so that we can sort well.. | |
332 | */ | |
333 | #define MAX_SHA1_ENTRIES (1024) | |
334 | ||
335 | struct sha1_entry { | |
336 | unsigned long ino; | |
20222118 | 337 | unsigned char sha1[20]; |
7e8c174a LT |
338 | }; |
339 | ||
340 | static struct { | |
341 | unsigned long nr; | |
342 | struct sha1_entry *entry[MAX_SHA1_ENTRIES]; | |
343 | } sha1_list; | |
344 | ||
345 | static int ino_compare(const void *_a, const void *_b) | |
346 | { | |
347 | const struct sha1_entry *a = _a, *b = _b; | |
348 | unsigned long ino1 = a->ino, ino2 = b->ino; | |
349 | return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0; | |
350 | } | |
351 | ||
352 | static void fsck_sha1_list(void) | |
353 | { | |
354 | int i, nr = sha1_list.nr; | |
355 | ||
35a730f0 JH |
356 | if (SORT_DIRENT) |
357 | qsort(sha1_list.entry, nr, | |
358 | sizeof(struct sha1_entry *), ino_compare); | |
7e8c174a LT |
359 | for (i = 0; i < nr; i++) { |
360 | struct sha1_entry *entry = sha1_list.entry[i]; | |
361 | unsigned char *sha1 = entry->sha1; | |
362 | ||
363 | sha1_list.entry[i] = NULL; | |
f1f0d088 | 364 | fsck_sha1(sha1); |
7e8c174a | 365 | free(entry); |
20222118 | 366 | } |
7e8c174a LT |
367 | sha1_list.nr = 0; |
368 | } | |
369 | ||
370 | static void add_sha1_list(unsigned char *sha1, unsigned long ino) | |
371 | { | |
372 | struct sha1_entry *entry = xmalloc(sizeof(*entry)); | |
373 | int nr; | |
374 | ||
375 | entry->ino = ino; | |
e702496e | 376 | hashcpy(entry->sha1, sha1); |
7e8c174a LT |
377 | nr = sha1_list.nr; |
378 | if (nr == MAX_SHA1_ENTRIES) { | |
379 | fsck_sha1_list(); | |
380 | nr = 0; | |
381 | } | |
382 | sha1_list.entry[nr] = entry; | |
383 | sha1_list.nr = ++nr; | |
20222118 LT |
384 | } |
385 | ||
b5524c82 | 386 | static void fsck_dir(int i, char *path) |
20222118 LT |
387 | { |
388 | DIR *dir = opendir(path); | |
389 | struct dirent *de; | |
390 | ||
230f1322 | 391 | if (!dir) |
b5524c82 | 392 | return; |
20222118 | 393 | |
20f1eb6b JS |
394 | if (verbose) |
395 | fprintf(stderr, "Checking directory %s\n", path); | |
396 | ||
20222118 LT |
397 | while ((de = readdir(dir)) != NULL) { |
398 | char name[100]; | |
7e8c174a | 399 | unsigned char sha1[20]; |
20222118 | 400 | |
8ca12c0d | 401 | if (is_dot_or_dotdot(de->d_name)) |
20222118 | 402 | continue; |
8ca12c0d | 403 | if (strlen(de->d_name) == 38) { |
20222118 | 404 | sprintf(name, "%02x", i); |
8ca12c0d | 405 | memcpy(name+2, de->d_name, 39); |
7e8c174a LT |
406 | if (get_sha1_hex(name, sha1) < 0) |
407 | break; | |
35a730f0 | 408 | add_sha1_list(sha1, DIRENT_SORT_HINT(de)); |
7e8c174a | 409 | continue; |
20222118 | 410 | } |
3d32a46b | 411 | if (!prefixcmp(de->d_name, "tmp_obj_")) |
a08c53a1 | 412 | continue; |
20222118 LT |
413 | fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name); |
414 | } | |
415 | closedir(dir); | |
20222118 LT |
416 | } |
417 | ||
96f1e58f | 418 | static int default_refs; |
944d8589 | 419 | |
883d60fa JS |
420 | static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1, |
421 | const char *email, unsigned long timestamp, int tz, | |
422 | const char *message, void *cb_data) | |
55dd5526 JH |
423 | { |
424 | struct object *obj; | |
425 | ||
20f1eb6b JS |
426 | if (verbose) |
427 | fprintf(stderr, "Checking reflog %s->%s\n", | |
428 | sha1_to_hex(osha1), sha1_to_hex(nsha1)); | |
429 | ||
55dd5526 JH |
430 | if (!is_null_sha1(osha1)) { |
431 | obj = lookup_object(osha1); | |
432 | if (obj) { | |
433 | obj->used = 1; | |
271b8d25 | 434 | mark_object_reachable(obj); |
55dd5526 JH |
435 | } |
436 | } | |
437 | obj = lookup_object(nsha1); | |
438 | if (obj) { | |
439 | obj->used = 1; | |
271b8d25 | 440 | mark_object_reachable(obj); |
55dd5526 JH |
441 | } |
442 | return 0; | |
443 | } | |
444 | ||
eb8381c8 NP |
445 | static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data) |
446 | { | |
447 | for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL); | |
448 | return 0; | |
449 | } | |
450 | ||
6232f62b LT |
451 | static int is_branch(const char *refname) |
452 | { | |
453 | return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/"); | |
454 | } | |
455 | ||
8da19775 | 456 | static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
1024932f | 457 | { |
1024932f LT |
458 | struct object *obj; |
459 | ||
6232f62b | 460 | obj = parse_object(sha1); |
8a498a05 | 461 | if (!obj) { |
944d8589 LT |
462 | error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1)); |
463 | /* We'll continue with the rest despite the error.. */ | |
464 | return 0; | |
8a498a05 | 465 | } |
6232f62b LT |
466 | if (obj->type != OBJ_COMMIT && is_branch(refname)) |
467 | error("%s: not a commit", refname); | |
944d8589 | 468 | default_refs++; |
1024932f | 469 | obj->used = 1; |
271b8d25 | 470 | mark_object_reachable(obj); |
55dd5526 | 471 | |
7c4d07c7 | 472 | return 0; |
1024932f LT |
473 | } |
474 | ||
1024932f LT |
475 | static void get_default_heads(void) |
476 | { | |
469e2ebf JH |
477 | if (head_points_at && !is_null_sha1(head_sha1)) |
478 | fsck_handle_ref("HEAD", head_sha1, 0, NULL); | |
cb5d709f | 479 | for_each_ref(fsck_handle_ref, NULL); |
566842f6 SP |
480 | if (include_reflogs) |
481 | for_each_reflog(fsck_handle_reflog, NULL); | |
071fa89e LT |
482 | |
483 | /* | |
484 | * Not having any default heads isn't really fatal, but | |
485 | * it does mean that "--unreachable" no longer makes any | |
486 | * sense (since in this case everything will obviously | |
487 | * be unreachable by definition. | |
488 | * | |
489 | * Showing dangling objects is valid, though (as those | |
490 | * dangling objects are likely lost heads). | |
491 | * | |
492 | * So we just print a warning about it, and clear the | |
493 | * "show_unreachable" flag. | |
494 | */ | |
495 | if (!default_refs) { | |
8eb2d0be | 496 | fprintf(stderr, "notice: No default references\n"); |
071fa89e LT |
497 | show_unreachable = 0; |
498 | } | |
1024932f LT |
499 | } |
500 | ||
8a498a05 JH |
501 | static void fsck_object_dir(const char *path) |
502 | { | |
503 | int i; | |
20f1eb6b JS |
504 | |
505 | if (verbose) | |
506 | fprintf(stderr, "Checking object directory\n"); | |
507 | ||
8a498a05 JH |
508 | for (i = 0; i < 256; i++) { |
509 | static char dir[4096]; | |
510 | sprintf(dir, "%s/%02x", path, i); | |
511 | fsck_dir(i, dir); | |
512 | } | |
513 | fsck_sha1_list(); | |
514 | } | |
515 | ||
c3330383 LT |
516 | static int fsck_head_link(void) |
517 | { | |
8da19775 | 518 | int flag; |
8eb2d0be | 519 | int null_is_error = 0; |
8eb2d0be | 520 | |
20f1eb6b JS |
521 | if (verbose) |
522 | fprintf(stderr, "Checking HEAD link\n"); | |
523 | ||
469e2ebf | 524 | head_points_at = resolve_ref("HEAD", head_sha1, 0, &flag); |
8eb2d0be JH |
525 | if (!head_points_at) |
526 | return error("Invalid HEAD"); | |
527 | if (!strcmp(head_points_at, "HEAD")) | |
528 | /* detached HEAD */ | |
529 | null_is_error = 1; | |
530 | else if (prefixcmp(head_points_at, "refs/heads/")) | |
8098a178 | 531 | return error("HEAD points to something strange (%s)", |
5b10b091 | 532 | head_points_at); |
469e2ebf | 533 | if (is_null_sha1(head_sha1)) { |
8eb2d0be JH |
534 | if (null_is_error) |
535 | return error("HEAD: detached HEAD points at nothing"); | |
536 | fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n", | |
537 | head_points_at + 11); | |
538 | } | |
c3330383 LT |
539 | return 0; |
540 | } | |
541 | ||
53dc3f3e JH |
542 | static int fsck_cache_tree(struct cache_tree *it) |
543 | { | |
544 | int i; | |
545 | int err = 0; | |
546 | ||
20f1eb6b JS |
547 | if (verbose) |
548 | fprintf(stderr, "Checking cache tree\n"); | |
549 | ||
53dc3f3e JH |
550 | if (0 <= it->entry_count) { |
551 | struct object *obj = parse_object(it->sha1); | |
6d60bbef JH |
552 | if (!obj) { |
553 | error("%s: invalid sha1 pointer in cache-tree", | |
554 | sha1_to_hex(it->sha1)); | |
555 | return 1; | |
556 | } | |
271b8d25 | 557 | mark_object_reachable(obj); |
cdc08b33 | 558 | obj->used = 1; |
1974632c | 559 | if (obj->type != OBJ_TREE) |
53dc3f3e JH |
560 | err |= objerror(obj, "non-tree in cache-tree"); |
561 | } | |
562 | for (i = 0; i < it->subtree_nr; i++) | |
563 | err |= fsck_cache_tree(it->down[i]->cache_tree); | |
564 | return err; | |
565 | } | |
566 | ||
5ac0a206 | 567 | static char const * const fsck_usage[] = { |
1b1dd23f | 568 | "git fsck [options] [<object>...]", |
5ac0a206 PH |
569 | NULL |
570 | }; | |
571 | ||
572 | static struct option fsck_opts[] = { | |
573 | OPT__VERBOSE(&verbose), | |
574 | OPT_BOOLEAN(0, "unreachable", &show_unreachable, "show unreachable objects"), | |
575 | OPT_BOOLEAN(0, "tags", &show_tags, "report tags"), | |
576 | OPT_BOOLEAN(0, "root", &show_root, "report root nodes"), | |
577 | OPT_BOOLEAN(0, "cache", &keep_cache_objects, "make index objects head nodes"), | |
578 | OPT_BOOLEAN(0, "reflogs", &include_reflogs, "make reflogs head nodes (default)"), | |
579 | OPT_BOOLEAN(0, "full", &check_full, "also consider alternate objects"), | |
dd46b9b9 | 580 | OPT_BOOLEAN(0, "strict", &check_strict, "enable more strict checking"), |
5ac0a206 PH |
581 | OPT_BOOLEAN(0, "lost-found", &write_lost_and_found, |
582 | "write dangling objects in .git/lost-found"), | |
583 | OPT_END(), | |
584 | }; | |
e2b4f635 | 585 | |
baffc0e7 | 586 | int cmd_fsck(int argc, const char **argv, const char *prefix) |
20222118 | 587 | { |
bcee6fd8 | 588 | int i, heads; |
e15ef669 | 589 | struct alternate_object_database *alt; |
20222118 | 590 | |
e2b4f635 | 591 | errors_found = 0; |
61e2b015 | 592 | |
5ac0a206 PH |
593 | argc = parse_options(argc, argv, fsck_opts, fsck_usage, 0); |
594 | if (write_lost_and_found) { | |
595 | check_full = 1; | |
596 | include_reflogs = 0; | |
889262ea LT |
597 | } |
598 | ||
c3330383 | 599 | fsck_head_link(); |
8a498a05 | 600 | fsck_object_dir(get_object_directory()); |
e15ef669 JH |
601 | |
602 | prepare_alt_odb(); | |
603 | for (alt = alt_odb_list; alt; alt = alt->next) { | |
604 | char namebuf[PATH_MAX]; | |
605 | int namelen = alt->name - alt->base; | |
606 | memcpy(namebuf, alt->base, namelen); | |
607 | namebuf[namelen - 1] = 0; | |
608 | fsck_object_dir(namebuf); | |
609 | } | |
610 | ||
8a498a05 | 611 | if (check_full) { |
8a498a05 | 612 | struct packed_git *p; |
e15ef669 | 613 | |
8a498a05 | 614 | prepare_packed_git(); |
f9253394 JH |
615 | for (p = packed_git; p; p = p->next) |
616 | /* verify gives error messages itself */ | |
77d3ecee | 617 | verify_pack(p); |
f9253394 | 618 | |
8a498a05 | 619 | for (p = packed_git; p; p = p->next) { |
5ac0a206 | 620 | uint32_t j, num; |
b77ffe8a SP |
621 | if (open_pack_index(p)) |
622 | continue; | |
623 | num = p->num_objects; | |
5ac0a206 PH |
624 | for (j = 0; j < num; j++) |
625 | fsck_sha1(nth_packed_object_sha1(p, j)); | |
8a498a05 | 626 | } |
bcee6fd8 LT |
627 | } |
628 | ||
629 | heads = 0; | |
3aed2fda | 630 | for (i = 0; i < argc; i++) { |
a6080a0a | 631 | const char *arg = argv[i]; |
469e2ebf JH |
632 | unsigned char sha1[20]; |
633 | if (!get_sha1(arg, sha1)) { | |
634 | struct object *obj = lookup_object(sha1); | |
e1a1388d | 635 | |
770896e5 LT |
636 | /* Error is printed by lookup_object(). */ |
637 | if (!obj) | |
e1a1388d JF |
638 | continue; |
639 | ||
ff5ebe39 | 640 | obj->used = 1; |
271b8d25 | 641 | mark_object_reachable(obj); |
bcee6fd8 | 642 | heads++; |
d9839e03 LT |
643 | continue; |
644 | } | |
f1f0d088 | 645 | error("invalid parameter: expected sha1, got '%s'", arg); |
d9839e03 | 646 | } |
d9839e03 | 647 | |
1024932f | 648 | /* |
d1af002d | 649 | * If we've not been given any explicit head information, do the |
e7bd907d LT |
650 | * default ones from .git/refs. We also consider the index file |
651 | * in this case (ie this implies --cache). | |
1024932f | 652 | */ |
e7bd907d | 653 | if (!heads) { |
1024932f LT |
654 | get_default_heads(); |
655 | keep_cache_objects = 1; | |
656 | } | |
657 | ||
ae7c0c92 | 658 | if (keep_cache_objects) { |
ae7c0c92 JH |
659 | read_cache(); |
660 | for (i = 0; i < active_nr; i++) { | |
8d9721c8 LT |
661 | unsigned int mode; |
662 | struct blob *blob; | |
ae7c0c92 | 663 | struct object *obj; |
8d9721c8 | 664 | |
7a51ed66 | 665 | mode = active_cache[i]->ce_mode; |
302b9282 | 666 | if (S_ISGITLINK(mode)) |
8d9721c8 LT |
667 | continue; |
668 | blob = lookup_blob(active_cache[i]->sha1); | |
ae7c0c92 JH |
669 | if (!blob) |
670 | continue; | |
671 | obj = &blob->object; | |
672 | obj->used = 1; | |
271b8d25 | 673 | mark_object_reachable(obj); |
ae7c0c92 | 674 | } |
53dc3f3e JH |
675 | if (active_cache_tree) |
676 | fsck_cache_tree(active_cache_tree); | |
ae7c0c92 JH |
677 | } |
678 | ||
8ba0bbb2 | 679 | check_connectivity(); |
e2b4f635 | 680 | return errors_found; |
20222118 | 681 | } |