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