]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-fsck.c
Mention 'cpio' dependency in INSTALL
[thirdparty/git.git] / builtin-fsck.c
CommitLineData
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"
ff5ebe39
DB
11
12#define REACHABLE 0x0001
2d9c58c6 13#define SEEN 0x0002
d9839e03 14
96f1e58f
DR
15static int show_root;
16static int show_tags;
17static int show_unreachable;
566842f6 18static int include_reflogs = 1;
96f1e58f
DR
19static int check_full;
20static int check_strict;
21static int keep_cache_objects;
d9839e03 22static unsigned char head_sha1[20];
e2b4f635 23static int errors_found;
68f6c019 24static int write_lost_and_found;
20f1eb6b 25static int verbose;
e2b4f635
JH
26#define ERROR_OBJECT 01
27#define ERROR_REACHABLE 02
d9839e03 28
962554c6 29#ifdef NO_D_INO_IN_DIRENT
35a730f0
JH
30#define SORT_DIRENT 0
31#define DIRENT_SORT_HINT(de) 0
32#else
33#define SORT_DIRENT 1
34#define DIRENT_SORT_HINT(de) ((de)->d_ino)
35#endif
f1f0d088
PB
36
37static void objreport(struct object *obj, const char *severity,
38 const char *err, va_list params)
39{
40 fprintf(stderr, "%s in %s %s: ",
885a86ab 41 severity, typename(obj->type), sha1_to_hex(obj->sha1));
f1f0d088
PB
42 vfprintf(stderr, err, params);
43 fputs("\n", stderr);
44}
45
a7928f8e 46static int objerror(struct object *obj, const char *err, ...)
f1f0d088
PB
47{
48 va_list params;
49 va_start(params, err);
e2b4f635 50 errors_found |= ERROR_OBJECT;
f1f0d088
PB
51 objreport(obj, "error", err, params);
52 va_end(params);
53 return -1;
54}
55
a7928f8e 56static int objwarning(struct object *obj, const char *err, ...)
f1f0d088
PB
57{
58 va_list params;
59 va_start(params, err);
60 objreport(obj, "warning", err, params);
61 va_end(params);
62 return -1;
63}
64
18af29f2
LT
65/*
66 * Check a single reachable object
67 */
68static void check_reachable_object(struct object *obj)
69{
70 const struct object_refs *refs;
71
72 /*
73 * We obviously want the object to be parsed,
74 * except if it was in a pack-file and we didn't
75 * do a full fsck
76 */
77 if (!obj->parsed) {
efec43c0 78 if (has_sha1_pack(obj->sha1, NULL))
18af29f2
LT
79 return; /* it is in pack - forget about it */
80 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
e2b4f635 81 errors_found |= ERROR_REACHABLE;
18af29f2
LT
82 return;
83 }
84
85 /*
86 * Check that everything that we try to reference is also good.
87 */
88 refs = lookup_object_refs(obj);
89 if (refs) {
90 unsigned j;
91 for (j = 0; j < refs->count; j++) {
92 struct object *ref = refs->ref[j];
93 if (ref->parsed ||
94 (has_sha1_file(ref->sha1)))
95 continue;
96 printf("broken link from %7s %s\n",
97 typename(obj->type), sha1_to_hex(obj->sha1));
98 printf(" to %7s %s\n",
99 typename(ref->type), sha1_to_hex(ref->sha1));
e2b4f635 100 errors_found |= ERROR_REACHABLE;
18af29f2
LT
101 }
102 }
103}
104
105/*
106 * Check a single unreachable object
107 */
108static void check_unreachable_object(struct object *obj)
109{
110 /*
111 * Missing unreachable object? Ignore it. It's not like
112 * we miss it (since it can't be reached), nor do we want
113 * to complain about it being unreachable (since it does
114 * not exist).
115 */
116 if (!obj->parsed)
117 return;
118
119 /*
120 * Unreachable object that exists? Show it if asked to,
121 * since this is something that is prunable.
122 */
123 if (show_unreachable) {
124 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
125 return;
126 }
127
128 /*
129 * "!used" means that nothing at all points to it, including
3dff5379 130 * other unreachable objects. In other words, it's the "tip"
18af29f2
LT
131 * of some set of unreachable objects, usually a commit that
132 * got dropped.
133 *
134 * Such starting points are more interesting than some random
135 * set of unreachable objects, so we show them even if the user
136 * hasn't asked for _all_ unreachable objects. If you have
137 * deleted a branch by mistake, this is a prime candidate to
138 * start looking at, for example.
139 */
140 if (!obj->used) {
141 printf("dangling %s %s\n", typename(obj->type),
142 sha1_to_hex(obj->sha1));
68f6c019
JS
143 if (write_lost_and_found) {
144 char *filename = git_path("lost-found/%s/%s",
145 obj->type == OBJ_COMMIT ? "commit" : "other",
146 sha1_to_hex(obj->sha1));
147 FILE *f;
148
149 if (safe_create_leading_directories(filename)) {
150 error("Could not create lost-found");
151 return;
152 }
153 if (!(f = fopen(filename, "w")))
154 die("Could not open %s", filename);
16a7fcfe
JS
155 if (obj->type == OBJ_BLOB) {
156 enum object_type type;
157 unsigned long size;
158 char *buf = read_sha1_file(obj->sha1,
159 &type, &size);
160 if (buf) {
161 fwrite(buf, size, 1, f);
162 free(buf);
163 }
164 } else
165 fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
68f6c019
JS
166 fclose(f);
167 }
18af29f2
LT
168 return;
169 }
170
171 /*
172 * Otherwise? It's there, it's unreachable, and some other unreachable
173 * object points to it. Ignore it - it's not interesting, and we showed
174 * all the interesting cases above.
175 */
176}
177
178static void check_object(struct object *obj)
179{
20f1eb6b
JS
180 if (verbose)
181 fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
182
18af29f2
LT
183 if (obj->flags & REACHABLE)
184 check_reachable_object(obj);
185 else
186 check_unreachable_object(obj);
187}
f1f0d088 188
8ba0bbb2
LT
189static void check_connectivity(void)
190{
fc046a75 191 int i, max;
8ba0bbb2 192
8ba0bbb2 193 /* Look up all the requirements, warn about missing objects.. */
fc046a75 194 max = get_max_object_index();
20f1eb6b
JS
195 if (verbose)
196 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
197
fc046a75 198 for (i = 0; i < max; i++) {
fc046a75 199 struct object *obj = get_indexed_object(i);
8ba0bbb2 200
18af29f2
LT
201 if (obj)
202 check_object(obj);
8ba0bbb2
LT
203 }
204}
205
85003492
LT
206/*
207 * The entries in a tree are ordered in the _path_ order,
208 * which means that a directory entry is ordered by adding
209 * a slash to the end of it.
210 *
211 * So a directory called "a" is ordered _after_ a file
212 * called "a.c", because "a/" sorts after "a.c".
213 */
a4f35a2d
JH
214#define TREE_UNORDERED (-1)
215#define TREE_HAS_DUPS (-2)
216
e9a95bef 217static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
85003492 218{
e9a95bef
LT
219 int len1 = strlen(name1);
220 int len2 = strlen(name2);
85003492
LT
221 int len = len1 < len2 ? len1 : len2;
222 unsigned char c1, c2;
223 int cmp;
224
e9a95bef 225 cmp = memcmp(name1, name2, len);
85003492
LT
226 if (cmp < 0)
227 return 0;
228 if (cmp > 0)
a4f35a2d 229 return TREE_UNORDERED;
85003492
LT
230
231 /*
232 * Ok, the first <len> characters are the same.
233 * Now we need to order the next one, but turn
234 * a '\0' into a '/' for a directory entry.
235 */
e9a95bef
LT
236 c1 = name1[len];
237 c2 = name2[len];
a4f35a2d
JH
238 if (!c1 && !c2)
239 /*
240 * git-write-tree used to write out a nonsense tree that has
241 * entries with the same name, one blob and one tree. Make
242 * sure we do not have duplicate entries.
243 */
244 return TREE_HAS_DUPS;
e9a95bef 245 if (!c1 && S_ISDIR(mode1))
85003492 246 c1 = '/';
e9a95bef 247 if (!c2 && S_ISDIR(mode2))
85003492 248 c2 = '/';
a4f35a2d 249 return c1 < c2 ? 0 : TREE_UNORDERED;
85003492
LT
250}
251
c418eda4 252static int fsck_tree(struct tree *item)
20222118 253{
64071805 254 int retval;
85003492 255 int has_full_path = 0;
cb2cada6 256 int has_empty_name = 0;
64071805
LT
257 int has_zero_pad = 0;
258 int has_bad_modes = 0;
259 int has_dup_entries = 0;
260 int not_properly_sorted = 0;
e9a95bef
LT
261 struct tree_desc desc;
262 unsigned o_mode;
263 const char *o_name;
264 const unsigned char *o_sha1;
85003492 265
20f1eb6b
JS
266 if (verbose)
267 fprintf(stderr, "Checking tree %s\n",
268 sha1_to_hex(item->object.sha1));
269
6fda5e51 270 init_tree_desc(&desc, item->buffer, item->size);
e9a95bef
LT
271
272 o_mode = 0;
273 o_name = NULL;
274 o_sha1 = NULL;
275 while (desc.size) {
276 unsigned mode;
277 const char *name;
278 const unsigned char *sha1;
279
280 sha1 = tree_entry_extract(&desc, &name, &mode);
281
282 if (strchr(name, '/'))
85003492 283 has_full_path = 1;
cb2cada6
SP
284 if (!*name)
285 has_empty_name = 1;
6fda5e51 286 has_zero_pad |= *(char *)desc.buffer == '0';
e9a95bef 287 update_tree_entry(&desc);
85003492 288
e9a95bef 289 switch (mode) {
42ea9cb2 290 /*
e9a95bef 291 * Standard modes..
42ea9cb2
LT
292 */
293 case S_IFREG | 0755:
294 case S_IFREG | 0644:
295 case S_IFLNK:
296 case S_IFDIR:
302b9282 297 case S_IFGITLINK:
42ea9cb2
LT
298 break;
299 /*
300 * This is nonstandard, but we had a few of these
301 * early on when we honored the full set of mode
302 * bits..
303 */
304 case S_IFREG | 0664:
de2eb7f6
LT
305 if (!check_strict)
306 break;
42ea9cb2 307 default:
64071805 308 has_bad_modes = 1;
42ea9cb2
LT
309 }
310
e9a95bef
LT
311 if (o_name) {
312 switch (verify_ordered(o_mode, o_name, mode, name)) {
a4f35a2d 313 case TREE_UNORDERED:
64071805
LT
314 not_properly_sorted = 1;
315 break;
a4f35a2d 316 case TREE_HAS_DUPS:
64071805
LT
317 has_dup_entries = 1;
318 break;
a4f35a2d
JH
319 default:
320 break;
85003492
LT
321 }
322 }
323
e9a95bef
LT
324 o_mode = mode;
325 o_name = name;
326 o_sha1 = sha1;
85003492 327 }
136f2e54
LT
328 free(item->buffer);
329 item->buffer = NULL;
85003492 330
64071805 331 retval = 0;
85003492 332 if (has_full_path) {
f1f0d088 333 objwarning(&item->object, "contains full pathnames");
1ea34e36 334 }
cb2cada6
SP
335 if (has_empty_name) {
336 objwarning(&item->object, "contains empty pathname");
337 }
64071805 338 if (has_zero_pad) {
f1f0d088 339 objwarning(&item->object, "contains zero-padded file modes");
64071805
LT
340 }
341 if (has_bad_modes) {
f1f0d088 342 objwarning(&item->object, "contains bad file modes");
64071805
LT
343 }
344 if (has_dup_entries) {
f1f0d088 345 retval = objerror(&item->object, "contains duplicate file entries");
64071805
LT
346 }
347 if (not_properly_sorted) {
f1f0d088 348 retval = objerror(&item->object, "not properly sorted");
64071805
LT
349 }
350 return retval;
1ea34e36
LT
351}
352
c418eda4 353static int fsck_commit(struct commit *commit)
1ea34e36 354{
de2eb7f6 355 char *buffer = commit->buffer;
f1f0d088 356 unsigned char tree_sha1[20], sha1[20];
de2eb7f6 357
20f1eb6b
JS
358 if (verbose)
359 fprintf(stderr, "Checking commit %s\n",
360 sha1_to_hex(commit->object.sha1));
361
de2eb7f6 362 if (memcmp(buffer, "tree ", 5))
f1f0d088
PB
363 return objerror(&commit->object, "invalid format - expected 'tree' line");
364 if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
365 return objerror(&commit->object, "invalid 'tree' line format - bad sha1");
de2eb7f6
LT
366 buffer += 46;
367 while (!memcmp(buffer, "parent ", 7)) {
368 if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
f1f0d088 369 return objerror(&commit->object, "invalid 'parent' line format - bad sha1");
de2eb7f6
LT
370 buffer += 48;
371 }
372 if (memcmp(buffer, "author ", 7))
f1f0d088 373 return objerror(&commit->object, "invalid format - expected 'author' line");
bd1e17e2
LT
374 free(commit->buffer);
375 commit->buffer = NULL;
ff5ebe39 376 if (!commit->tree)
f1f0d088 377 return objerror(&commit->object, "could not load commit's tree %s", tree_sha1);
ab7df187 378 if (!commit->parents && show_root)
c418eda4 379 printf("root %s\n", sha1_to_hex(commit->object.sha1));
e6948b6d 380 if (!commit->date)
a6080a0a 381 printf("bad commit date in %s\n",
c418eda4 382 sha1_to_hex(commit->object.sha1));
4728b861
LT
383 return 0;
384}
385
c418eda4 386static int fsck_tag(struct tag *tag)
ec4465ad 387{
92d4c85d
LT
388 struct object *tagged = tag->tagged;
389
20f1eb6b
JS
390 if (verbose)
391 fprintf(stderr, "Checking tag %s\n",
392 sha1_to_hex(tag->object.sha1));
393
92d4c85d 394 if (!tagged) {
f1f0d088 395 return objerror(&tag->object, "could not load tagged object");
92d4c85d 396 }
889262ea
LT
397 if (!show_tags)
398 return 0;
399
885a86ab 400 printf("tagged %s %s", typename(tagged->type), sha1_to_hex(tagged->sha1));
92d4c85d 401 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
ff5ebe39 402 return 0;
20222118
LT
403}
404
d72308e0 405static int fsck_sha1(const unsigned char *sha1)
20222118 406{
7e8c174a 407 struct object *obj = parse_object(sha1);
e2b4f635
JH
408 if (!obj) {
409 errors_found |= ERROR_OBJECT;
410 return error("%s: object corrupt or missing",
411 sha1_to_hex(sha1));
412 }
2d9c58c6
LT
413 if (obj->flags & SEEN)
414 return 0;
415 obj->flags |= SEEN;
1974632c 416 if (obj->type == OBJ_BLOB)
7e8c174a 417 return 0;
1974632c 418 if (obj->type == OBJ_TREE)
7e8c174a 419 return fsck_tree((struct tree *) obj);
1974632c 420 if (obj->type == OBJ_COMMIT)
7e8c174a 421 return fsck_commit((struct commit *) obj);
1974632c 422 if (obj->type == OBJ_TAG)
7e8c174a 423 return fsck_tag((struct tag *) obj);
e2b4f635 424
f1f0d088 425 /* By now, parse_object() would've returned NULL instead. */
e2b4f635
JH
426 return objerror(obj, "unknown type '%d' (internal fsck error)",
427 obj->type);
7e8c174a
LT
428}
429
430/*
431 * This is the sorting chunk size: make it reasonably
432 * big so that we can sort well..
433 */
434#define MAX_SHA1_ENTRIES (1024)
435
436struct sha1_entry {
437 unsigned long ino;
20222118 438 unsigned char sha1[20];
7e8c174a
LT
439};
440
441static struct {
442 unsigned long nr;
443 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
444} sha1_list;
445
446static int ino_compare(const void *_a, const void *_b)
447{
448 const struct sha1_entry *a = _a, *b = _b;
449 unsigned long ino1 = a->ino, ino2 = b->ino;
450 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
451}
452
453static void fsck_sha1_list(void)
454{
455 int i, nr = sha1_list.nr;
456
35a730f0
JH
457 if (SORT_DIRENT)
458 qsort(sha1_list.entry, nr,
459 sizeof(struct sha1_entry *), ino_compare);
7e8c174a
LT
460 for (i = 0; i < nr; i++) {
461 struct sha1_entry *entry = sha1_list.entry[i];
462 unsigned char *sha1 = entry->sha1;
463
464 sha1_list.entry[i] = NULL;
f1f0d088 465 fsck_sha1(sha1);
7e8c174a 466 free(entry);
20222118 467 }
7e8c174a
LT
468 sha1_list.nr = 0;
469}
470
471static void add_sha1_list(unsigned char *sha1, unsigned long ino)
472{
473 struct sha1_entry *entry = xmalloc(sizeof(*entry));
474 int nr;
475
476 entry->ino = ino;
e702496e 477 hashcpy(entry->sha1, sha1);
7e8c174a
LT
478 nr = sha1_list.nr;
479 if (nr == MAX_SHA1_ENTRIES) {
480 fsck_sha1_list();
481 nr = 0;
482 }
483 sha1_list.entry[nr] = entry;
484 sha1_list.nr = ++nr;
20222118
LT
485}
486
b5524c82 487static void fsck_dir(int i, char *path)
20222118
LT
488{
489 DIR *dir = opendir(path);
490 struct dirent *de;
491
230f1322 492 if (!dir)
b5524c82 493 return;
20222118 494
20f1eb6b
JS
495 if (verbose)
496 fprintf(stderr, "Checking directory %s\n", path);
497
20222118
LT
498 while ((de = readdir(dir)) != NULL) {
499 char name[100];
7e8c174a 500 unsigned char sha1[20];
20222118
LT
501 int len = strlen(de->d_name);
502
503 switch (len) {
504 case 2:
505 if (de->d_name[1] != '.')
506 break;
507 case 1:
508 if (de->d_name[0] != '.')
509 break;
510 continue;
511 case 38:
512 sprintf(name, "%02x", i);
513 memcpy(name+2, de->d_name, len+1);
7e8c174a
LT
514 if (get_sha1_hex(name, sha1) < 0)
515 break;
35a730f0 516 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
7e8c174a 517 continue;
20222118
LT
518 }
519 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
520 }
521 closedir(dir);
20222118
LT
522}
523
96f1e58f 524static int default_refs;
944d8589 525
883d60fa
JS
526static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
527 const char *email, unsigned long timestamp, int tz,
528 const char *message, void *cb_data)
55dd5526
JH
529{
530 struct object *obj;
531
20f1eb6b
JS
532 if (verbose)
533 fprintf(stderr, "Checking reflog %s->%s\n",
534 sha1_to_hex(osha1), sha1_to_hex(nsha1));
535
55dd5526
JH
536 if (!is_null_sha1(osha1)) {
537 obj = lookup_object(osha1);
538 if (obj) {
539 obj->used = 1;
540 mark_reachable(obj, REACHABLE);
541 }
542 }
543 obj = lookup_object(nsha1);
544 if (obj) {
545 obj->used = 1;
546 mark_reachable(obj, REACHABLE);
547 }
548 return 0;
549}
550
eb8381c8
NP
551static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
552{
553 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
554 return 0;
555}
556
8da19775 557static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1024932f 558{
1024932f
LT
559 struct object *obj;
560
1024932f 561 obj = lookup_object(sha1);
8a498a05 562 if (!obj) {
7aaa715d 563 if (has_sha1_file(sha1)) {
659cacf5 564 default_refs++;
944d8589 565 return 0; /* it is in a pack */
659cacf5 566 }
944d8589
LT
567 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
568 /* We'll continue with the rest despite the error.. */
569 return 0;
8a498a05 570 }
944d8589 571 default_refs++;
1024932f
LT
572 obj->used = 1;
573 mark_reachable(obj, REACHABLE);
55dd5526 574
7c4d07c7 575 return 0;
1024932f
LT
576}
577
1024932f
LT
578static void get_default_heads(void)
579{
cb5d709f 580 for_each_ref(fsck_handle_ref, NULL);
566842f6
SP
581 if (include_reflogs)
582 for_each_reflog(fsck_handle_reflog, NULL);
071fa89e
LT
583
584 /*
585 * Not having any default heads isn't really fatal, but
586 * it does mean that "--unreachable" no longer makes any
587 * sense (since in this case everything will obviously
588 * be unreachable by definition.
589 *
590 * Showing dangling objects is valid, though (as those
591 * dangling objects are likely lost heads).
592 *
593 * So we just print a warning about it, and clear the
594 * "show_unreachable" flag.
595 */
596 if (!default_refs) {
8eb2d0be 597 fprintf(stderr, "notice: No default references\n");
071fa89e
LT
598 show_unreachable = 0;
599 }
1024932f
LT
600}
601
8a498a05
JH
602static void fsck_object_dir(const char *path)
603{
604 int i;
20f1eb6b
JS
605
606 if (verbose)
607 fprintf(stderr, "Checking object directory\n");
608
8a498a05
JH
609 for (i = 0; i < 256; i++) {
610 static char dir[4096];
611 sprintf(dir, "%s/%02x", path, i);
612 fsck_dir(i, dir);
613 }
614 fsck_sha1_list();
615}
616
c3330383
LT
617static int fsck_head_link(void)
618{
c3330383 619 unsigned char sha1[20];
8da19775 620 int flag;
8eb2d0be
JH
621 int null_is_error = 0;
622 const char *head_points_at = resolve_ref("HEAD", sha1, 0, &flag);
623
20f1eb6b
JS
624 if (verbose)
625 fprintf(stderr, "Checking HEAD link\n");
626
8eb2d0be
JH
627 if (!head_points_at)
628 return error("Invalid HEAD");
629 if (!strcmp(head_points_at, "HEAD"))
630 /* detached HEAD */
631 null_is_error = 1;
632 else if (prefixcmp(head_points_at, "refs/heads/"))
8098a178 633 return error("HEAD points to something strange (%s)",
5b10b091 634 head_points_at);
8eb2d0be
JH
635 if (is_null_sha1(sha1)) {
636 if (null_is_error)
637 return error("HEAD: detached HEAD points at nothing");
638 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
639 head_points_at + 11);
640 }
c3330383
LT
641 return 0;
642}
643
53dc3f3e
JH
644static int fsck_cache_tree(struct cache_tree *it)
645{
646 int i;
647 int err = 0;
648
20f1eb6b
JS
649 if (verbose)
650 fprintf(stderr, "Checking cache tree\n");
651
53dc3f3e
JH
652 if (0 <= it->entry_count) {
653 struct object *obj = parse_object(it->sha1);
6d60bbef
JH
654 if (!obj) {
655 error("%s: invalid sha1 pointer in cache-tree",
656 sha1_to_hex(it->sha1));
657 return 1;
658 }
cdc08b33
JH
659 mark_reachable(obj, REACHABLE);
660 obj->used = 1;
1974632c 661 if (obj->type != OBJ_TREE)
53dc3f3e
JH
662 err |= objerror(obj, "non-tree in cache-tree");
663 }
664 for (i = 0; i < it->subtree_nr; i++)
665 err |= fsck_cache_tree(it->down[i]->cache_tree);
666 return err;
667}
668
e2b4f635
JH
669static const char fsck_usage[] =
670"git-fsck [--tags] [--root] [[--unreachable] [--cache] [--full] "
20f1eb6b 671"[--strict] [--verbose] <head-sha1>*]";
e2b4f635 672
baffc0e7 673int cmd_fsck(int argc, const char **argv, const char *prefix)
20222118 674{
bcee6fd8 675 int i, heads;
20222118 676
3a7c352b 677 track_object_refs = 1;
e2b4f635 678 errors_found = 0;
61e2b015 679
889262ea
LT
680 for (i = 1; i < argc; i++) {
681 const char *arg = argv[i];
682
683 if (!strcmp(arg, "--unreachable")) {
684 show_unreachable = 1;
685 continue;
686 }
687 if (!strcmp(arg, "--tags")) {
688 show_tags = 1;
689 continue;
690 }
ab7df187
LT
691 if (!strcmp(arg, "--root")) {
692 show_root = 1;
693 continue;
694 }
ae7c0c92
JH
695 if (!strcmp(arg, "--cache")) {
696 keep_cache_objects = 1;
697 continue;
698 }
566842f6
SP
699 if (!strcmp(arg, "--no-reflogs")) {
700 include_reflogs = 0;
701 continue;
702 }
8a498a05
JH
703 if (!strcmp(arg, "--full")) {
704 check_full = 1;
705 continue;
706 }
de2eb7f6
LT
707 if (!strcmp(arg, "--strict")) {
708 check_strict = 1;
709 continue;
710 }
20f1eb6b
JS
711 if (!strcmp(arg, "--verbose")) {
712 verbose = 1;
713 continue;
714 }
68f6c019
JS
715 if (!strcmp(arg, "--lost-found")) {
716 check_full = 1;
717 include_reflogs = 0;
718 write_lost_and_found = 1;
719 continue;
720 }
889262ea 721 if (*arg == '-')
e2b4f635 722 usage(fsck_usage);
889262ea
LT
723 }
724
c3330383 725 fsck_head_link();
8a498a05
JH
726 fsck_object_dir(get_object_directory());
727 if (check_full) {
d5a63b99 728 struct alternate_object_database *alt;
8a498a05
JH
729 struct packed_git *p;
730 prepare_alt_odb();
d5a63b99 731 for (alt = alt_odb_list; alt; alt = alt->next) {
a3eb250f 732 char namebuf[PATH_MAX];
d5a63b99
JH
733 int namelen = alt->name - alt->base;
734 memcpy(namebuf, alt->base, namelen);
a3eb250f
JH
735 namebuf[namelen - 1] = 0;
736 fsck_object_dir(namebuf);
8a498a05
JH
737 }
738 prepare_packed_git();
f9253394
JH
739 for (p = packed_git; p; p = p->next)
740 /* verify gives error messages itself */
f3bf9224 741 verify_pack(p, 0);
f9253394 742
8a498a05 743 for (p = packed_git; p; p = p->next) {
b77ffe8a
SP
744 uint32_t i, num;
745 if (open_pack_index(p))
746 continue;
747 num = p->num_objects;
d72308e0
NP
748 for (i = 0; i < num; i++)
749 fsck_sha1(nth_packed_object_sha1(p, i));
8a498a05 750 }
bcee6fd8
LT
751 }
752
753 heads = 0;
d9839e03 754 for (i = 1; i < argc; i++) {
a6080a0a 755 const char *arg = argv[i];
889262ea
LT
756
757 if (*arg == '-')
d9839e03 758 continue;
889262ea 759
3c249c95 760 if (!get_sha1(arg, head_sha1)) {
770896e5 761 struct object *obj = lookup_object(head_sha1);
e1a1388d 762
770896e5
LT
763 /* Error is printed by lookup_object(). */
764 if (!obj)
e1a1388d
JF
765 continue;
766
ff5ebe39
DB
767 obj->used = 1;
768 mark_reachable(obj, REACHABLE);
bcee6fd8 769 heads++;
d9839e03
LT
770 continue;
771 }
f1f0d088 772 error("invalid parameter: expected sha1, got '%s'", arg);
d9839e03 773 }
d9839e03 774
1024932f 775 /*
d1af002d 776 * If we've not been given any explicit head information, do the
e7bd907d
LT
777 * default ones from .git/refs. We also consider the index file
778 * in this case (ie this implies --cache).
1024932f 779 */
e7bd907d 780 if (!heads) {
1024932f
LT
781 get_default_heads();
782 keep_cache_objects = 1;
783 }
784
ae7c0c92
JH
785 if (keep_cache_objects) {
786 int i;
787 read_cache();
788 for (i = 0; i < active_nr; i++) {
8d9721c8
LT
789 unsigned int mode;
790 struct blob *blob;
ae7c0c92 791 struct object *obj;
8d9721c8
LT
792
793 mode = ntohl(active_cache[i]->ce_mode);
302b9282 794 if (S_ISGITLINK(mode))
8d9721c8
LT
795 continue;
796 blob = lookup_blob(active_cache[i]->sha1);
ae7c0c92
JH
797 if (!blob)
798 continue;
799 obj = &blob->object;
800 obj->used = 1;
801 mark_reachable(obj, REACHABLE);
802 }
53dc3f3e
JH
803 if (active_cache_tree)
804 fsck_cache_tree(active_cache_tree);
ae7c0c92
JH
805 }
806
8ba0bbb2 807 check_connectivity();
e2b4f635 808 return errors_found;
20222118 809}