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