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