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