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