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