OSDN Git Service

Merge branch 'rs/fsck-obj-leakfix' into maint
[git-core/git.git] / builtin / fsck.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "commit.h"
5 #include "tree.h"
6 #include "blob.h"
7 #include "tag.h"
8 #include "refs.h"
9 #include "pack.h"
10 #include "cache-tree.h"
11 #include "tree-walk.h"
12 #include "fsck.h"
13 #include "parse-options.h"
14 #include "dir.h"
15 #include "progress.h"
16 #include "streaming.h"
17 #include "decorate.h"
18
19 #define REACHABLE 0x0001
20 #define SEEN      0x0002
21 #define HAS_OBJ   0x0004
22 /* This flag is set if something points to this object. */
23 #define USED      0x0008
24
25 static int show_root;
26 static int show_tags;
27 static int show_unreachable;
28 static int include_reflogs = 1;
29 static int check_full = 1;
30 static int connectivity_only;
31 static int check_strict;
32 static int keep_cache_objects;
33 static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
34 static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
35 static struct object_id head_oid;
36 static const char *head_points_at;
37 static int errors_found;
38 static int write_lost_and_found;
39 static int verbose;
40 static int show_progress = -1;
41 static int show_dangling = 1;
42 static int name_objects;
43 #define ERROR_OBJECT 01
44 #define ERROR_REACHABLE 02
45 #define ERROR_PACK 04
46 #define ERROR_REFS 010
47
48 static const char *describe_object(struct object *obj)
49 {
50         static struct strbuf buf = STRBUF_INIT;
51         char *name = name_objects ?
52                 lookup_decoration(fsck_walk_options.object_names, obj) : NULL;
53
54         strbuf_reset(&buf);
55         strbuf_addstr(&buf, oid_to_hex(&obj->oid));
56         if (name)
57                 strbuf_addf(&buf, " (%s)", name);
58
59         return buf.buf;
60 }
61
62 static const char *printable_type(struct object *obj)
63 {
64         const char *ret;
65
66         if (obj->type == OBJ_NONE) {
67                 enum object_type type = sha1_object_info(obj->oid.hash, NULL);
68                 if (type > 0)
69                         object_as_type(obj, type, 0);
70         }
71
72         ret = typename(obj->type);
73         if (!ret)
74                 ret = "unknown";
75
76         return ret;
77 }
78
79 static int fsck_config(const char *var, const char *value, void *cb)
80 {
81         if (strcmp(var, "fsck.skiplist") == 0) {
82                 const char *path;
83                 struct strbuf sb = STRBUF_INIT;
84
85                 if (git_config_pathname(&path, var, value))
86                         return 1;
87                 strbuf_addf(&sb, "skiplist=%s", path);
88                 free((char *)path);
89                 fsck_set_msg_types(&fsck_obj_options, sb.buf);
90                 strbuf_release(&sb);
91                 return 0;
92         }
93
94         if (skip_prefix(var, "fsck.", &var)) {
95                 fsck_set_msg_type(&fsck_obj_options, var, value);
96                 return 0;
97         }
98
99         return git_default_config(var, value, cb);
100 }
101
102 static void objreport(struct object *obj, const char *msg_type,
103                         const char *err)
104 {
105         fprintf(stderr, "%s in %s %s: %s\n",
106                 msg_type, printable_type(obj), describe_object(obj), err);
107 }
108
109 static int objerror(struct object *obj, const char *err)
110 {
111         errors_found |= ERROR_OBJECT;
112         objreport(obj, "error", err);
113         return -1;
114 }
115
116 static int fsck_error_func(struct fsck_options *o,
117         struct object *obj, int type, const char *message)
118 {
119         objreport(obj, (type == FSCK_WARN) ? "warning" : "error", message);
120         return (type == FSCK_WARN) ? 0 : 1;
121 }
122
123 static struct object_array pending;
124
125 static int mark_object(struct object *obj, int type, void *data, struct fsck_options *options)
126 {
127         struct object *parent = data;
128
129         /*
130          * The only case data is NULL or type is OBJ_ANY is when
131          * mark_object_reachable() calls us.  All the callers of
132          * that function has non-NULL obj hence ...
133          */
134         if (!obj) {
135                 /* ... these references to parent->fld are safe here */
136                 printf("broken link from %7s %s\n",
137                            printable_type(parent), describe_object(parent));
138                 printf("broken link from %7s %s\n",
139                            (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
140                 errors_found |= ERROR_REACHABLE;
141                 return 1;
142         }
143
144         if (type != OBJ_ANY && obj->type != type)
145                 /* ... and the reference to parent is safe here */
146                 objerror(parent, "wrong object type in link");
147
148         if (obj->flags & REACHABLE)
149                 return 0;
150         obj->flags |= REACHABLE;
151         if (!(obj->flags & HAS_OBJ)) {
152                 if (parent && !has_object_file(&obj->oid)) {
153                         printf("broken link from %7s %s\n",
154                                  printable_type(parent), describe_object(parent));
155                         printf("              to %7s %s\n",
156                                  printable_type(obj), describe_object(obj));
157                         errors_found |= ERROR_REACHABLE;
158                 }
159                 return 1;
160         }
161
162         add_object_array(obj, NULL, &pending);
163         return 0;
164 }
165
166 static void mark_object_reachable(struct object *obj)
167 {
168         mark_object(obj, OBJ_ANY, NULL, NULL);
169 }
170
171 static int traverse_one_object(struct object *obj)
172 {
173         return fsck_walk(obj, obj, &fsck_walk_options);
174 }
175
176 static int traverse_reachable(void)
177 {
178         struct progress *progress = NULL;
179         unsigned int nr = 0;
180         int result = 0;
181         if (show_progress)
182                 progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2);
183         while (pending.nr) {
184                 struct object_array_entry *entry;
185                 struct object *obj;
186
187                 entry = pending.objects + --pending.nr;
188                 obj = entry->item;
189                 result |= traverse_one_object(obj);
190                 display_progress(progress, ++nr);
191         }
192         stop_progress(&progress);
193         return !!result;
194 }
195
196 static int mark_used(struct object *obj, int type, void *data, struct fsck_options *options)
197 {
198         if (!obj)
199                 return 1;
200         obj->flags |= USED;
201         return 0;
202 }
203
204 /*
205  * Check a single reachable object
206  */
207 static void check_reachable_object(struct object *obj)
208 {
209         /*
210          * We obviously want the object to be parsed,
211          * except if it was in a pack-file and we didn't
212          * do a full fsck
213          */
214         if (!(obj->flags & HAS_OBJ)) {
215                 if (has_sha1_pack(obj->oid.hash))
216                         return; /* it is in pack - forget about it */
217                 printf("missing %s %s\n", printable_type(obj),
218                         describe_object(obj));
219                 errors_found |= ERROR_REACHABLE;
220                 return;
221         }
222 }
223
224 /*
225  * Check a single unreachable object
226  */
227 static void check_unreachable_object(struct object *obj)
228 {
229         /*
230          * Missing unreachable object? Ignore it. It's not like
231          * we miss it (since it can't be reached), nor do we want
232          * to complain about it being unreachable (since it does
233          * not exist).
234          */
235         if (!(obj->flags & HAS_OBJ))
236                 return;
237
238         /*
239          * Unreachable object that exists? Show it if asked to,
240          * since this is something that is prunable.
241          */
242         if (show_unreachable) {
243                 printf("unreachable %s %s\n", printable_type(obj),
244                         describe_object(obj));
245                 return;
246         }
247
248         /*
249          * "!USED" means that nothing at all points to it, including
250          * other unreachable objects. In other words, it's the "tip"
251          * of some set of unreachable objects, usually a commit that
252          * got dropped.
253          *
254          * Such starting points are more interesting than some random
255          * set of unreachable objects, so we show them even if the user
256          * hasn't asked for _all_ unreachable objects. If you have
257          * deleted a branch by mistake, this is a prime candidate to
258          * start looking at, for example.
259          */
260         if (!(obj->flags & USED)) {
261                 if (show_dangling)
262                         printf("dangling %s %s\n", printable_type(obj),
263                                describe_object(obj));
264                 if (write_lost_and_found) {
265                         char *filename = git_pathdup("lost-found/%s/%s",
266                                 obj->type == OBJ_COMMIT ? "commit" : "other",
267                                 describe_object(obj));
268                         FILE *f;
269
270                         if (safe_create_leading_directories_const(filename)) {
271                                 error("Could not create lost-found");
272                                 free(filename);
273                                 return;
274                         }
275                         f = xfopen(filename, "w");
276                         if (obj->type == OBJ_BLOB) {
277                                 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
278                                         die_errno("Could not write '%s'", filename);
279                         } else
280                                 fprintf(f, "%s\n", describe_object(obj));
281                         if (fclose(f))
282                                 die_errno("Could not finish '%s'",
283                                           filename);
284                         free(filename);
285                 }
286                 return;
287         }
288
289         /*
290          * Otherwise? It's there, it's unreachable, and some other unreachable
291          * object points to it. Ignore it - it's not interesting, and we showed
292          * all the interesting cases above.
293          */
294 }
295
296 static void check_object(struct object *obj)
297 {
298         if (verbose)
299                 fprintf(stderr, "Checking %s\n", describe_object(obj));
300
301         if (obj->flags & REACHABLE)
302                 check_reachable_object(obj);
303         else
304                 check_unreachable_object(obj);
305 }
306
307 static void check_connectivity(void)
308 {
309         int i, max;
310
311         /* Traverse the pending reachable objects */
312         traverse_reachable();
313
314         /* Look up all the requirements, warn about missing objects.. */
315         max = get_max_object_index();
316         if (verbose)
317                 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
318
319         for (i = 0; i < max; i++) {
320                 struct object *obj = get_indexed_object(i);
321
322                 if (obj)
323                         check_object(obj);
324         }
325 }
326
327 static int fsck_obj(struct object *obj)
328 {
329         int err;
330
331         if (obj->flags & SEEN)
332                 return 0;
333         obj->flags |= SEEN;
334
335         if (verbose)
336                 fprintf(stderr, "Checking %s %s\n",
337                         printable_type(obj), describe_object(obj));
338
339         if (fsck_walk(obj, NULL, &fsck_obj_options))
340                 objerror(obj, "broken links");
341         err = fsck_object(obj, NULL, 0, &fsck_obj_options);
342         if (err)
343                 goto out;
344
345         if (obj->type == OBJ_COMMIT) {
346                 struct commit *commit = (struct commit *) obj;
347
348                 if (!commit->parents && show_root)
349                         printf("root %s\n", describe_object(&commit->object));
350         }
351
352         if (obj->type == OBJ_TAG) {
353                 struct tag *tag = (struct tag *) obj;
354
355                 if (show_tags && tag->tagged) {
356                         printf("tagged %s %s", printable_type(tag->tagged),
357                                 describe_object(tag->tagged));
358                         printf(" (%s) in %s\n", tag->tag,
359                                 describe_object(&tag->object));
360                 }
361         }
362
363 out:
364         if (obj->type == OBJ_TREE)
365                 free_tree_buffer((struct tree *)obj);
366         if (obj->type == OBJ_COMMIT)
367                 free_commit_buffer((struct commit *)obj);
368         return err;
369 }
370
371 static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
372                            unsigned long size, void *buffer, int *eaten)
373 {
374         /*
375          * Note, buffer may be NULL if type is OBJ_BLOB. See
376          * verify_packfile(), data_valid variable for details.
377          */
378         struct object *obj;
379         obj = parse_object_buffer(oid, type, size, buffer, eaten);
380         if (!obj) {
381                 errors_found |= ERROR_OBJECT;
382                 return error("%s: object corrupt or missing", oid_to_hex(oid));
383         }
384         obj->flags &= ~(REACHABLE | SEEN);
385         obj->flags |= HAS_OBJ;
386         return fsck_obj(obj);
387 }
388
389 static int default_refs;
390
391 static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
392         timestamp_t timestamp)
393 {
394         struct object *obj;
395
396         if (!is_null_oid(oid)) {
397                 obj = lookup_object(oid->hash);
398                 if (obj && (obj->flags & HAS_OBJ)) {
399                         if (timestamp && name_objects)
400                                 add_decoration(fsck_walk_options.object_names,
401                                         obj,
402                                         xstrfmt("%s@{%"PRItime"}", refname, timestamp));
403                         obj->flags |= USED;
404                         mark_object_reachable(obj);
405                 } else {
406                         error("%s: invalid reflog entry %s", refname, oid_to_hex(oid));
407                         errors_found |= ERROR_REACHABLE;
408                 }
409         }
410 }
411
412 static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
413                 const char *email, timestamp_t timestamp, int tz,
414                 const char *message, void *cb_data)
415 {
416         const char *refname = cb_data;
417
418         if (verbose)
419                 fprintf(stderr, "Checking reflog %s->%s\n",
420                         oid_to_hex(ooid), oid_to_hex(noid));
421
422         fsck_handle_reflog_oid(refname, ooid, 0);
423         fsck_handle_reflog_oid(refname, noid, timestamp);
424         return 0;
425 }
426
427 static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
428                               int flag, void *cb_data)
429 {
430         for_each_reflog_ent(logname, fsck_handle_reflog_ent, (void *)logname);
431         return 0;
432 }
433
434 static int fsck_handle_ref(const char *refname, const struct object_id *oid,
435                            int flag, void *cb_data)
436 {
437         struct object *obj;
438
439         obj = parse_object(oid);
440         if (!obj) {
441                 error("%s: invalid sha1 pointer %s", refname, oid_to_hex(oid));
442                 errors_found |= ERROR_REACHABLE;
443                 /* We'll continue with the rest despite the error.. */
444                 return 0;
445         }
446         if (obj->type != OBJ_COMMIT && is_branch(refname)) {
447                 error("%s: not a commit", refname);
448                 errors_found |= ERROR_REFS;
449         }
450         default_refs++;
451         obj->flags |= USED;
452         if (name_objects)
453                 add_decoration(fsck_walk_options.object_names,
454                         obj, xstrdup(refname));
455         mark_object_reachable(obj);
456
457         return 0;
458 }
459
460 static void get_default_heads(void)
461 {
462         if (head_points_at && !is_null_oid(&head_oid))
463                 fsck_handle_ref("HEAD", &head_oid, 0, NULL);
464         for_each_rawref(fsck_handle_ref, NULL);
465         if (include_reflogs)
466                 for_each_reflog(fsck_handle_reflog, NULL);
467
468         /*
469          * Not having any default heads isn't really fatal, but
470          * it does mean that "--unreachable" no longer makes any
471          * sense (since in this case everything will obviously
472          * be unreachable by definition.
473          *
474          * Showing dangling objects is valid, though (as those
475          * dangling objects are likely lost heads).
476          *
477          * So we just print a warning about it, and clear the
478          * "show_unreachable" flag.
479          */
480         if (!default_refs) {
481                 fprintf(stderr, "notice: No default references\n");
482                 show_unreachable = 0;
483         }
484 }
485
486 static struct object *parse_loose_object(const struct object_id *oid,
487                                          const char *path)
488 {
489         struct object *obj;
490         void *contents;
491         enum object_type type;
492         unsigned long size;
493         int eaten;
494
495         if (read_loose_object(path, oid->hash, &type, &size, &contents) < 0)
496                 return NULL;
497
498         if (!contents && type != OBJ_BLOB)
499                 die("BUG: read_loose_object streamed a non-blob");
500
501         obj = parse_object_buffer(oid, type, size, contents, &eaten);
502
503         if (!eaten)
504                 free(contents);
505         return obj;
506 }
507
508 static int fsck_loose(const struct object_id *oid, const char *path, void *data)
509 {
510         struct object *obj = parse_loose_object(oid, path);
511
512         if (!obj) {
513                 errors_found |= ERROR_OBJECT;
514                 error("%s: object corrupt or missing: %s",
515                       oid_to_hex(oid), path);
516                 return 0; /* keep checking other objects */
517         }
518
519         obj->flags &= ~(REACHABLE | SEEN);
520         obj->flags |= HAS_OBJ;
521         if (fsck_obj(obj))
522                 errors_found |= ERROR_OBJECT;
523         return 0;
524 }
525
526 static int fsck_cruft(const char *basename, const char *path, void *data)
527 {
528         if (!starts_with(basename, "tmp_obj_"))
529                 fprintf(stderr, "bad sha1 file: %s\n", path);
530         return 0;
531 }
532
533 static int fsck_subdir(unsigned int nr, const char *path, void *progress)
534 {
535         display_progress(progress, nr + 1);
536         return 0;
537 }
538
539 static void fsck_object_dir(const char *path)
540 {
541         struct progress *progress = NULL;
542
543         if (verbose)
544                 fprintf(stderr, "Checking object directory\n");
545
546         if (show_progress)
547                 progress = start_progress(_("Checking object directories"), 256);
548
549         for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
550                                       progress);
551         display_progress(progress, 256);
552         stop_progress(&progress);
553 }
554
555 static int fsck_head_link(void)
556 {
557         int null_is_error = 0;
558
559         if (verbose)
560                 fprintf(stderr, "Checking HEAD link\n");
561
562         head_points_at = resolve_ref_unsafe("HEAD", 0, head_oid.hash, NULL);
563         if (!head_points_at) {
564                 errors_found |= ERROR_REFS;
565                 return error("Invalid HEAD");
566         }
567         if (!strcmp(head_points_at, "HEAD"))
568                 /* detached HEAD */
569                 null_is_error = 1;
570         else if (!starts_with(head_points_at, "refs/heads/")) {
571                 errors_found |= ERROR_REFS;
572                 return error("HEAD points to something strange (%s)",
573                              head_points_at);
574         }
575         if (is_null_oid(&head_oid)) {
576                 if (null_is_error) {
577                         errors_found |= ERROR_REFS;
578                         return error("HEAD: detached HEAD points at nothing");
579                 }
580                 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
581                         head_points_at + 11);
582         }
583         return 0;
584 }
585
586 static int fsck_cache_tree(struct cache_tree *it)
587 {
588         int i;
589         int err = 0;
590
591         if (verbose)
592                 fprintf(stderr, "Checking cache tree\n");
593
594         if (0 <= it->entry_count) {
595                 struct object *obj = parse_object(&it->oid);
596                 if (!obj) {
597                         error("%s: invalid sha1 pointer in cache-tree",
598                               oid_to_hex(&it->oid));
599                         errors_found |= ERROR_REFS;
600                         return 1;
601                 }
602                 obj->flags |= USED;
603                 if (name_objects)
604                         add_decoration(fsck_walk_options.object_names,
605                                 obj, xstrdup(":"));
606                 mark_object_reachable(obj);
607                 if (obj->type != OBJ_TREE)
608                         err |= objerror(obj, "non-tree in cache-tree");
609         }
610         for (i = 0; i < it->subtree_nr; i++)
611                 err |= fsck_cache_tree(it->down[i]->cache_tree);
612         return err;
613 }
614
615 static void mark_object_for_connectivity(const struct object_id *oid)
616 {
617         struct object *obj = lookup_unknown_object(oid->hash);
618         obj->flags |= HAS_OBJ;
619 }
620
621 static int mark_loose_for_connectivity(const struct object_id *oid,
622                                        const char *path,
623                                        void *data)
624 {
625         mark_object_for_connectivity(oid);
626         return 0;
627 }
628
629 static int mark_packed_for_connectivity(const struct object_id *oid,
630                                         struct packed_git *pack,
631                                         uint32_t pos,
632                                         void *data)
633 {
634         mark_object_for_connectivity(oid);
635         return 0;
636 }
637
638 static char const * const fsck_usage[] = {
639         N_("git fsck [<options>] [<object>...]"),
640         NULL
641 };
642
643 static struct option fsck_opts[] = {
644         OPT__VERBOSE(&verbose, N_("be verbose")),
645         OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
646         OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
647         OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
648         OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
649         OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
650         OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
651         OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
652         OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
653         OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
654         OPT_BOOL(0, "lost-found", &write_lost_and_found,
655                                 N_("write dangling objects in .git/lost-found")),
656         OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
657         OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
658         OPT_END(),
659 };
660
661 int cmd_fsck(int argc, const char **argv, const char *prefix)
662 {
663         int i;
664         struct alternate_object_database *alt;
665
666         errors_found = 0;
667         check_replace_refs = 0;
668
669         argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
670
671         fsck_walk_options.walk = mark_object;
672         fsck_obj_options.walk = mark_used;
673         fsck_obj_options.error_func = fsck_error_func;
674         if (check_strict)
675                 fsck_obj_options.strict = 1;
676
677         if (show_progress == -1)
678                 show_progress = isatty(2);
679         if (verbose)
680                 show_progress = 0;
681
682         if (write_lost_and_found) {
683                 check_full = 1;
684                 include_reflogs = 0;
685         }
686
687         if (name_objects)
688                 fsck_walk_options.object_names =
689                         xcalloc(1, sizeof(struct decoration));
690
691         git_config(fsck_config, NULL);
692
693         fsck_head_link();
694         if (connectivity_only) {
695                 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
696                 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
697         } else {
698                 fsck_object_dir(get_object_directory());
699
700                 prepare_alt_odb();
701                 for (alt = alt_odb_list; alt; alt = alt->next)
702                         fsck_object_dir(alt->path);
703
704                 if (check_full) {
705                         struct packed_git *p;
706                         uint32_t total = 0, count = 0;
707                         struct progress *progress = NULL;
708
709                         prepare_packed_git();
710
711                         if (show_progress) {
712                                 for (p = packed_git; p; p = p->next) {
713                                         if (open_pack_index(p))
714                                                 continue;
715                                         total += p->num_objects;
716                                 }
717
718                                 progress = start_progress(_("Checking objects"), total);
719                         }
720                         for (p = packed_git; p; p = p->next) {
721                                 /* verify gives error messages itself */
722                                 if (verify_pack(p, fsck_obj_buffer,
723                                                 progress, count))
724                                         errors_found |= ERROR_PACK;
725                                 count += p->num_objects;
726                         }
727                         stop_progress(&progress);
728                 }
729         }
730
731         for (i = 0; i < argc; i++) {
732                 const char *arg = argv[i];
733                 unsigned char sha1[20];
734                 if (!get_sha1(arg, sha1)) {
735                         struct object *obj = lookup_object(sha1);
736
737                         if (!obj || !(obj->flags & HAS_OBJ)) {
738                                 error("%s: object missing", sha1_to_hex(sha1));
739                                 errors_found |= ERROR_OBJECT;
740                                 continue;
741                         }
742
743                         obj->flags |= USED;
744                         if (name_objects)
745                                 add_decoration(fsck_walk_options.object_names,
746                                         obj, xstrdup(arg));
747                         mark_object_reachable(obj);
748                         continue;
749                 }
750                 error("invalid parameter: expected sha1, got '%s'", arg);
751                 errors_found |= ERROR_OBJECT;
752         }
753
754         /*
755          * If we've not been given any explicit head information, do the
756          * default ones from .git/refs. We also consider the index file
757          * in this case (ie this implies --cache).
758          */
759         if (!argc) {
760                 get_default_heads();
761                 keep_cache_objects = 1;
762         }
763
764         if (keep_cache_objects) {
765                 verify_index_checksum = 1;
766                 read_cache();
767                 for (i = 0; i < active_nr; i++) {
768                         unsigned int mode;
769                         struct blob *blob;
770                         struct object *obj;
771
772                         mode = active_cache[i]->ce_mode;
773                         if (S_ISGITLINK(mode))
774                                 continue;
775                         blob = lookup_blob(&active_cache[i]->oid);
776                         if (!blob)
777                                 continue;
778                         obj = &blob->object;
779                         obj->flags |= USED;
780                         if (name_objects)
781                                 add_decoration(fsck_walk_options.object_names,
782                                         obj,
783                                         xstrfmt(":%s", active_cache[i]->name));
784                         mark_object_reachable(obj);
785                 }
786                 if (active_cache_tree)
787                         fsck_cache_tree(active_cache_tree);
788         }
789
790         check_connectivity();
791         return errors_found;
792 }