]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-fsck.c
builtin-fsck: reports missing parent commits
[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"
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
17static int show_root;
18static int show_tags;
19static int show_unreachable;
566842f6 20static int include_reflogs = 1;
96f1e58f
DR
21static int check_full;
22static int check_strict;
23static int keep_cache_objects;
d9839e03 24static unsigned char head_sha1[20];
e2b4f635 25static int errors_found;
68f6c019 26static int write_lost_and_found;
20f1eb6b 27static 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
39static 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 48static 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
a7928f8e 58static int objwarning(struct object *obj, const char *err, ...)
f1f0d088
PB
59{
60 va_list params;
61 va_start(params, err);
62 objreport(obj, "warning", err, params);
63 va_end(params);
64 return -1;
65}
66
271b8d25
MK
67static 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
116static void mark_object_reachable(struct object *obj)
117{
118 mark_object(obj, OBJ_ANY, 0);
119}
120
121static int mark_used(struct object *obj, int type, void *data)
122{
123 if (!obj)
124 return 1;
125 obj->used = 1;
126 return 0;
127}
128
18af29f2
LT
129/*
130 * Check a single reachable object
131 */
132static 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 */
151static 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
221static 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
232static 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
85003492
LT
249/*
250 * The entries in a tree are ordered in the _path_ order,
251 * which means that a directory entry is ordered by adding
252 * a slash to the end of it.
253 *
254 * So a directory called "a" is ordered _after_ a file
255 * called "a.c", because "a/" sorts after "a.c".
256 */
a4f35a2d
JH
257#define TREE_UNORDERED (-1)
258#define TREE_HAS_DUPS (-2)
259
e9a95bef 260static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
85003492 261{
e9a95bef
LT
262 int len1 = strlen(name1);
263 int len2 = strlen(name2);
85003492
LT
264 int len = len1 < len2 ? len1 : len2;
265 unsigned char c1, c2;
266 int cmp;
267
e9a95bef 268 cmp = memcmp(name1, name2, len);
85003492
LT
269 if (cmp < 0)
270 return 0;
271 if (cmp > 0)
a4f35a2d 272 return TREE_UNORDERED;
85003492
LT
273
274 /*
275 * Ok, the first <len> characters are the same.
276 * Now we need to order the next one, but turn
277 * a '\0' into a '/' for a directory entry.
278 */
e9a95bef
LT
279 c1 = name1[len];
280 c2 = name2[len];
a4f35a2d
JH
281 if (!c1 && !c2)
282 /*
283 * git-write-tree used to write out a nonsense tree that has
284 * entries with the same name, one blob and one tree. Make
285 * sure we do not have duplicate entries.
286 */
287 return TREE_HAS_DUPS;
e9a95bef 288 if (!c1 && S_ISDIR(mode1))
85003492 289 c1 = '/';
e9a95bef 290 if (!c2 && S_ISDIR(mode2))
85003492 291 c2 = '/';
a4f35a2d 292 return c1 < c2 ? 0 : TREE_UNORDERED;
85003492
LT
293}
294
c418eda4 295static int fsck_tree(struct tree *item)
20222118 296{
64071805 297 int retval;
85003492 298 int has_full_path = 0;
cb2cada6 299 int has_empty_name = 0;
64071805
LT
300 int has_zero_pad = 0;
301 int has_bad_modes = 0;
302 int has_dup_entries = 0;
303 int not_properly_sorted = 0;
e9a95bef
LT
304 struct tree_desc desc;
305 unsigned o_mode;
306 const char *o_name;
307 const unsigned char *o_sha1;
85003492 308
20f1eb6b
JS
309 if (verbose)
310 fprintf(stderr, "Checking tree %s\n",
311 sha1_to_hex(item->object.sha1));
312
6fda5e51 313 init_tree_desc(&desc, item->buffer, item->size);
e9a95bef
LT
314
315 o_mode = 0;
316 o_name = NULL;
317 o_sha1 = NULL;
318 while (desc.size) {
319 unsigned mode;
320 const char *name;
321 const unsigned char *sha1;
322
323 sha1 = tree_entry_extract(&desc, &name, &mode);
324
325 if (strchr(name, '/'))
85003492 326 has_full_path = 1;
cb2cada6
SP
327 if (!*name)
328 has_empty_name = 1;
6fda5e51 329 has_zero_pad |= *(char *)desc.buffer == '0';
e9a95bef 330 update_tree_entry(&desc);
85003492 331
e9a95bef 332 switch (mode) {
42ea9cb2 333 /*
e9a95bef 334 * Standard modes..
42ea9cb2
LT
335 */
336 case S_IFREG | 0755:
337 case S_IFREG | 0644:
338 case S_IFLNK:
339 case S_IFDIR:
302b9282 340 case S_IFGITLINK:
42ea9cb2
LT
341 break;
342 /*
343 * This is nonstandard, but we had a few of these
344 * early on when we honored the full set of mode
345 * bits..
346 */
347 case S_IFREG | 0664:
de2eb7f6
LT
348 if (!check_strict)
349 break;
42ea9cb2 350 default:
64071805 351 has_bad_modes = 1;
42ea9cb2
LT
352 }
353
e9a95bef
LT
354 if (o_name) {
355 switch (verify_ordered(o_mode, o_name, mode, name)) {
a4f35a2d 356 case TREE_UNORDERED:
64071805
LT
357 not_properly_sorted = 1;
358 break;
a4f35a2d 359 case TREE_HAS_DUPS:
64071805
LT
360 has_dup_entries = 1;
361 break;
a4f35a2d
JH
362 default:
363 break;
85003492
LT
364 }
365 }
366
e9a95bef
LT
367 o_mode = mode;
368 o_name = name;
369 o_sha1 = sha1;
85003492 370 }
136f2e54
LT
371 free(item->buffer);
372 item->buffer = NULL;
85003492 373
64071805 374 retval = 0;
85003492 375 if (has_full_path) {
f1f0d088 376 objwarning(&item->object, "contains full pathnames");
1ea34e36 377 }
cb2cada6
SP
378 if (has_empty_name) {
379 objwarning(&item->object, "contains empty pathname");
380 }
64071805 381 if (has_zero_pad) {
f1f0d088 382 objwarning(&item->object, "contains zero-padded file modes");
64071805
LT
383 }
384 if (has_bad_modes) {
f1f0d088 385 objwarning(&item->object, "contains bad file modes");
64071805
LT
386 }
387 if (has_dup_entries) {
f1f0d088 388 retval = objerror(&item->object, "contains duplicate file entries");
64071805
LT
389 }
390 if (not_properly_sorted) {
f1f0d088 391 retval = objerror(&item->object, "not properly sorted");
64071805
LT
392 }
393 return retval;
1ea34e36
LT
394}
395
c418eda4 396static int fsck_commit(struct commit *commit)
1ea34e36 397{
de2eb7f6 398 char *buffer = commit->buffer;
f1f0d088 399 unsigned char tree_sha1[20], sha1[20];
45163382
MK
400 struct commit_graft *graft;
401 int parents = 0;
de2eb7f6 402
20f1eb6b
JS
403 if (verbose)
404 fprintf(stderr, "Checking commit %s\n",
405 sha1_to_hex(commit->object.sha1));
406
de2eb7f6 407 if (memcmp(buffer, "tree ", 5))
f1f0d088
PB
408 return objerror(&commit->object, "invalid format - expected 'tree' line");
409 if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
410 return objerror(&commit->object, "invalid 'tree' line format - bad sha1");
de2eb7f6
LT
411 buffer += 46;
412 while (!memcmp(buffer, "parent ", 7)) {
413 if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
f1f0d088 414 return objerror(&commit->object, "invalid 'parent' line format - bad sha1");
de2eb7f6 415 buffer += 48;
45163382
MK
416 parents++;
417 }
418 graft = lookup_commit_graft(commit->object.sha1);
419 if (graft) {
420 struct commit_list *p = commit->parents;
421 parents = 0;
422 while (p) {
423 p = p->next;
424 parents++;
425 }
426 if (graft->nr_parent == -1 && !parents)
427 ; /* shallow commit */
428 else if (graft->nr_parent != parents)
429 return objerror(&commit->object, "graft objects missing");
430 } else {
431 struct commit_list *p = commit->parents;
432 while (p && parents) {
433 p = p->next;
434 parents--;
435 }
436 if (p || parents)
437 return objerror(&commit->object, "parent objects missing");
de2eb7f6
LT
438 }
439 if (memcmp(buffer, "author ", 7))
f1f0d088 440 return objerror(&commit->object, "invalid format - expected 'author' line");
bd1e17e2
LT
441 free(commit->buffer);
442 commit->buffer = NULL;
ff5ebe39 443 if (!commit->tree)
f1f0d088 444 return objerror(&commit->object, "could not load commit's tree %s", tree_sha1);
ab7df187 445 if (!commit->parents && show_root)
c418eda4 446 printf("root %s\n", sha1_to_hex(commit->object.sha1));
e6948b6d 447 if (!commit->date)
a6080a0a 448 printf("bad commit date in %s\n",
c418eda4 449 sha1_to_hex(commit->object.sha1));
4728b861
LT
450 return 0;
451}
452
c418eda4 453static int fsck_tag(struct tag *tag)
ec4465ad 454{
92d4c85d
LT
455 struct object *tagged = tag->tagged;
456
20f1eb6b
JS
457 if (verbose)
458 fprintf(stderr, "Checking tag %s\n",
459 sha1_to_hex(tag->object.sha1));
460
92d4c85d 461 if (!tagged) {
f1f0d088 462 return objerror(&tag->object, "could not load tagged object");
92d4c85d 463 }
889262ea
LT
464 if (!show_tags)
465 return 0;
466
885a86ab 467 printf("tagged %s %s", typename(tagged->type), sha1_to_hex(tagged->sha1));
92d4c85d 468 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
ff5ebe39 469 return 0;
20222118
LT
470}
471
d72308e0 472static int fsck_sha1(const unsigned char *sha1)
20222118 473{
7e8c174a 474 struct object *obj = parse_object(sha1);
e2b4f635
JH
475 if (!obj) {
476 errors_found |= ERROR_OBJECT;
477 return error("%s: object corrupt or missing",
478 sha1_to_hex(sha1));
479 }
2d9c58c6
LT
480 if (obj->flags & SEEN)
481 return 0;
482 obj->flags |= SEEN;
271b8d25
MK
483 if (fsck_walk(obj, mark_used, 0))
484 objerror(obj, "broken links");
1974632c 485 if (obj->type == OBJ_BLOB)
7e8c174a 486 return 0;
1974632c 487 if (obj->type == OBJ_TREE)
7e8c174a 488 return fsck_tree((struct tree *) obj);
1974632c 489 if (obj->type == OBJ_COMMIT)
7e8c174a 490 return fsck_commit((struct commit *) obj);
1974632c 491 if (obj->type == OBJ_TAG)
7e8c174a 492 return fsck_tag((struct tag *) obj);
e2b4f635 493
f1f0d088 494 /* By now, parse_object() would've returned NULL instead. */
e2b4f635
JH
495 return objerror(obj, "unknown type '%d' (internal fsck error)",
496 obj->type);
7e8c174a
LT
497}
498
499/*
500 * This is the sorting chunk size: make it reasonably
501 * big so that we can sort well..
502 */
503#define MAX_SHA1_ENTRIES (1024)
504
505struct sha1_entry {
506 unsigned long ino;
20222118 507 unsigned char sha1[20];
7e8c174a
LT
508};
509
510static struct {
511 unsigned long nr;
512 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
513} sha1_list;
514
515static int ino_compare(const void *_a, const void *_b)
516{
517 const struct sha1_entry *a = _a, *b = _b;
518 unsigned long ino1 = a->ino, ino2 = b->ino;
519 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
520}
521
522static void fsck_sha1_list(void)
523{
524 int i, nr = sha1_list.nr;
525
35a730f0
JH
526 if (SORT_DIRENT)
527 qsort(sha1_list.entry, nr,
528 sizeof(struct sha1_entry *), ino_compare);
7e8c174a
LT
529 for (i = 0; i < nr; i++) {
530 struct sha1_entry *entry = sha1_list.entry[i];
531 unsigned char *sha1 = entry->sha1;
532
533 sha1_list.entry[i] = NULL;
f1f0d088 534 fsck_sha1(sha1);
7e8c174a 535 free(entry);
20222118 536 }
7e8c174a
LT
537 sha1_list.nr = 0;
538}
539
540static void add_sha1_list(unsigned char *sha1, unsigned long ino)
541{
542 struct sha1_entry *entry = xmalloc(sizeof(*entry));
543 int nr;
544
545 entry->ino = ino;
e702496e 546 hashcpy(entry->sha1, sha1);
7e8c174a
LT
547 nr = sha1_list.nr;
548 if (nr == MAX_SHA1_ENTRIES) {
549 fsck_sha1_list();
550 nr = 0;
551 }
552 sha1_list.entry[nr] = entry;
553 sha1_list.nr = ++nr;
20222118
LT
554}
555
b5524c82 556static void fsck_dir(int i, char *path)
20222118
LT
557{
558 DIR *dir = opendir(path);
559 struct dirent *de;
560
230f1322 561 if (!dir)
b5524c82 562 return;
20222118 563
20f1eb6b
JS
564 if (verbose)
565 fprintf(stderr, "Checking directory %s\n", path);
566
20222118
LT
567 while ((de = readdir(dir)) != NULL) {
568 char name[100];
7e8c174a 569 unsigned char sha1[20];
20222118
LT
570 int len = strlen(de->d_name);
571
572 switch (len) {
573 case 2:
574 if (de->d_name[1] != '.')
575 break;
576 case 1:
577 if (de->d_name[0] != '.')
578 break;
579 continue;
580 case 38:
581 sprintf(name, "%02x", i);
582 memcpy(name+2, de->d_name, len+1);
7e8c174a
LT
583 if (get_sha1_hex(name, sha1) < 0)
584 break;
35a730f0 585 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
7e8c174a 586 continue;
20222118
LT
587 }
588 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
589 }
590 closedir(dir);
20222118
LT
591}
592
96f1e58f 593static int default_refs;
944d8589 594
883d60fa
JS
595static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
596 const char *email, unsigned long timestamp, int tz,
597 const char *message, void *cb_data)
55dd5526
JH
598{
599 struct object *obj;
600
20f1eb6b
JS
601 if (verbose)
602 fprintf(stderr, "Checking reflog %s->%s\n",
603 sha1_to_hex(osha1), sha1_to_hex(nsha1));
604
55dd5526
JH
605 if (!is_null_sha1(osha1)) {
606 obj = lookup_object(osha1);
607 if (obj) {
608 obj->used = 1;
271b8d25 609 mark_object_reachable(obj);
55dd5526
JH
610 }
611 }
612 obj = lookup_object(nsha1);
613 if (obj) {
614 obj->used = 1;
271b8d25 615 mark_object_reachable(obj);
55dd5526
JH
616 }
617 return 0;
618}
619
eb8381c8
NP
620static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
621{
622 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
623 return 0;
624}
625
6232f62b
LT
626static int is_branch(const char *refname)
627{
628 return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/");
629}
630
8da19775 631static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1024932f 632{
1024932f
LT
633 struct object *obj;
634
6232f62b 635 obj = parse_object(sha1);
8a498a05 636 if (!obj) {
944d8589
LT
637 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
638 /* We'll continue with the rest despite the error.. */
639 return 0;
8a498a05 640 }
6232f62b
LT
641 if (obj->type != OBJ_COMMIT && is_branch(refname))
642 error("%s: not a commit", refname);
944d8589 643 default_refs++;
1024932f 644 obj->used = 1;
271b8d25 645 mark_object_reachable(obj);
55dd5526 646
7c4d07c7 647 return 0;
1024932f
LT
648}
649
1024932f
LT
650static void get_default_heads(void)
651{
cb5d709f 652 for_each_ref(fsck_handle_ref, NULL);
566842f6
SP
653 if (include_reflogs)
654 for_each_reflog(fsck_handle_reflog, NULL);
071fa89e
LT
655
656 /*
657 * Not having any default heads isn't really fatal, but
658 * it does mean that "--unreachable" no longer makes any
659 * sense (since in this case everything will obviously
660 * be unreachable by definition.
661 *
662 * Showing dangling objects is valid, though (as those
663 * dangling objects are likely lost heads).
664 *
665 * So we just print a warning about it, and clear the
666 * "show_unreachable" flag.
667 */
668 if (!default_refs) {
8eb2d0be 669 fprintf(stderr, "notice: No default references\n");
071fa89e
LT
670 show_unreachable = 0;
671 }
1024932f
LT
672}
673
8a498a05
JH
674static void fsck_object_dir(const char *path)
675{
676 int i;
20f1eb6b
JS
677
678 if (verbose)
679 fprintf(stderr, "Checking object directory\n");
680
8a498a05
JH
681 for (i = 0; i < 256; i++) {
682 static char dir[4096];
683 sprintf(dir, "%s/%02x", path, i);
684 fsck_dir(i, dir);
685 }
686 fsck_sha1_list();
687}
688
c3330383
LT
689static int fsck_head_link(void)
690{
c3330383 691 unsigned char sha1[20];
8da19775 692 int flag;
8eb2d0be
JH
693 int null_is_error = 0;
694 const char *head_points_at = resolve_ref("HEAD", sha1, 0, &flag);
695
20f1eb6b
JS
696 if (verbose)
697 fprintf(stderr, "Checking HEAD link\n");
698
8eb2d0be
JH
699 if (!head_points_at)
700 return error("Invalid HEAD");
701 if (!strcmp(head_points_at, "HEAD"))
702 /* detached HEAD */
703 null_is_error = 1;
704 else if (prefixcmp(head_points_at, "refs/heads/"))
8098a178 705 return error("HEAD points to something strange (%s)",
5b10b091 706 head_points_at);
8eb2d0be
JH
707 if (is_null_sha1(sha1)) {
708 if (null_is_error)
709 return error("HEAD: detached HEAD points at nothing");
710 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
711 head_points_at + 11);
712 }
c3330383
LT
713 return 0;
714}
715
53dc3f3e
JH
716static int fsck_cache_tree(struct cache_tree *it)
717{
718 int i;
719 int err = 0;
720
20f1eb6b
JS
721 if (verbose)
722 fprintf(stderr, "Checking cache tree\n");
723
53dc3f3e
JH
724 if (0 <= it->entry_count) {
725 struct object *obj = parse_object(it->sha1);
6d60bbef
JH
726 if (!obj) {
727 error("%s: invalid sha1 pointer in cache-tree",
728 sha1_to_hex(it->sha1));
729 return 1;
730 }
271b8d25 731 mark_object_reachable(obj);
cdc08b33 732 obj->used = 1;
1974632c 733 if (obj->type != OBJ_TREE)
53dc3f3e
JH
734 err |= objerror(obj, "non-tree in cache-tree");
735 }
736 for (i = 0; i < it->subtree_nr; i++)
737 err |= fsck_cache_tree(it->down[i]->cache_tree);
738 return err;
739}
740
5ac0a206
PH
741static char const * const fsck_usage[] = {
742 "git-fsck [options] [<object>...]",
743 NULL
744};
745
746static struct option fsck_opts[] = {
747 OPT__VERBOSE(&verbose),
748 OPT_BOOLEAN(0, "unreachable", &show_unreachable, "show unreachable objects"),
749 OPT_BOOLEAN(0, "tags", &show_tags, "report tags"),
750 OPT_BOOLEAN(0, "root", &show_root, "report root nodes"),
751 OPT_BOOLEAN(0, "cache", &keep_cache_objects, "make index objects head nodes"),
752 OPT_BOOLEAN(0, "reflogs", &include_reflogs, "make reflogs head nodes (default)"),
753 OPT_BOOLEAN(0, "full", &check_full, "also consider alternate objects"),
dd46b9b9 754 OPT_BOOLEAN(0, "strict", &check_strict, "enable more strict checking"),
5ac0a206
PH
755 OPT_BOOLEAN(0, "lost-found", &write_lost_and_found,
756 "write dangling objects in .git/lost-found"),
757 OPT_END(),
758};
e2b4f635 759
baffc0e7 760int cmd_fsck(int argc, const char **argv, const char *prefix)
20222118 761{
bcee6fd8 762 int i, heads;
20222118 763
e2b4f635 764 errors_found = 0;
61e2b015 765
5ac0a206
PH
766 argc = parse_options(argc, argv, fsck_opts, fsck_usage, 0);
767 if (write_lost_and_found) {
768 check_full = 1;
769 include_reflogs = 0;
889262ea
LT
770 }
771
c3330383 772 fsck_head_link();
8a498a05
JH
773 fsck_object_dir(get_object_directory());
774 if (check_full) {
d5a63b99 775 struct alternate_object_database *alt;
8a498a05
JH
776 struct packed_git *p;
777 prepare_alt_odb();
d5a63b99 778 for (alt = alt_odb_list; alt; alt = alt->next) {
a3eb250f 779 char namebuf[PATH_MAX];
d5a63b99
JH
780 int namelen = alt->name - alt->base;
781 memcpy(namebuf, alt->base, namelen);
a3eb250f
JH
782 namebuf[namelen - 1] = 0;
783 fsck_object_dir(namebuf);
8a498a05
JH
784 }
785 prepare_packed_git();
f9253394
JH
786 for (p = packed_git; p; p = p->next)
787 /* verify gives error messages itself */
f3bf9224 788 verify_pack(p, 0);
f9253394 789
8a498a05 790 for (p = packed_git; p; p = p->next) {
5ac0a206 791 uint32_t j, num;
b77ffe8a
SP
792 if (open_pack_index(p))
793 continue;
794 num = p->num_objects;
5ac0a206
PH
795 for (j = 0; j < num; j++)
796 fsck_sha1(nth_packed_object_sha1(p, j));
8a498a05 797 }
bcee6fd8
LT
798 }
799
800 heads = 0;
d9839e03 801 for (i = 1; i < argc; i++) {
a6080a0a 802 const char *arg = argv[i];
3c249c95 803 if (!get_sha1(arg, head_sha1)) {
770896e5 804 struct object *obj = lookup_object(head_sha1);
e1a1388d 805
770896e5
LT
806 /* Error is printed by lookup_object(). */
807 if (!obj)
e1a1388d
JF
808 continue;
809
ff5ebe39 810 obj->used = 1;
271b8d25 811 mark_object_reachable(obj);
bcee6fd8 812 heads++;
d9839e03
LT
813 continue;
814 }
f1f0d088 815 error("invalid parameter: expected sha1, got '%s'", arg);
d9839e03 816 }
d9839e03 817
1024932f 818 /*
d1af002d 819 * If we've not been given any explicit head information, do the
e7bd907d
LT
820 * default ones from .git/refs. We also consider the index file
821 * in this case (ie this implies --cache).
1024932f 822 */
e7bd907d 823 if (!heads) {
1024932f
LT
824 get_default_heads();
825 keep_cache_objects = 1;
826 }
827
ae7c0c92 828 if (keep_cache_objects) {
ae7c0c92
JH
829 read_cache();
830 for (i = 0; i < active_nr; i++) {
8d9721c8
LT
831 unsigned int mode;
832 struct blob *blob;
ae7c0c92 833 struct object *obj;
8d9721c8
LT
834
835 mode = ntohl(active_cache[i]->ce_mode);
302b9282 836 if (S_ISGITLINK(mode))
8d9721c8
LT
837 continue;
838 blob = lookup_blob(active_cache[i]->sha1);
ae7c0c92
JH
839 if (!blob)
840 continue;
841 obj = &blob->object;
842 obj->used = 1;
271b8d25 843 mark_object_reachable(obj);
ae7c0c92 844 }
53dc3f3e
JH
845 if (active_cache_tree)
846 fsck_cache_tree(active_cache_tree);
ae7c0c92
JH
847 }
848
8ba0bbb2 849 check_connectivity();
e2b4f635 850 return errors_found;
20222118 851}