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