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