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