cache.h: remove this no-longer-used header
[alt-git.git] / builtin / fsck.c
blob3e169f413ddda77865acd6f8d7e322f9a22058d6
1 #include "builtin.h"
2 #include "gettext.h"
3 #include "hex.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-file.h"
22 #include "object-name.h"
23 #include "object-store.h"
24 #include "read-cache-ll.h"
25 #include "replace-object.h"
26 #include "resolve-undo.h"
27 #include "run-command.h"
28 #include "sparse-index.h"
29 #include "worktree.h"
30 #include "pack-revindex.h"
31 #include "pack-bitmap.h"
33 #define REACHABLE 0x0001
34 #define SEEN 0x0002
35 #define HAS_OBJ 0x0004
36 /* This flag is set if something points to this object. */
37 #define USED 0x0008
39 static int show_root;
40 static int show_tags;
41 static int show_unreachable;
42 static int include_reflogs = 1;
43 static int check_full = 1;
44 static int connectivity_only;
45 static int check_strict;
46 static int keep_cache_objects;
47 static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
48 static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
49 static int errors_found;
50 static int write_lost_and_found;
51 static int verbose;
52 static int show_progress = -1;
53 static int show_dangling = 1;
54 static int name_objects;
55 #define ERROR_OBJECT 01
56 #define ERROR_REACHABLE 02
57 #define ERROR_PACK 04
58 #define ERROR_REFS 010
59 #define ERROR_COMMIT_GRAPH 020
60 #define ERROR_MULTI_PACK_INDEX 040
61 #define ERROR_PACK_REV_INDEX 0100
62 #define ERROR_BITMAP 0200
64 static const char *describe_object(const struct object_id *oid)
66 return fsck_describe_object(&fsck_walk_options, oid);
69 static const char *printable_type(const struct object_id *oid,
70 enum object_type type)
72 const char *ret;
74 if (type == OBJ_NONE)
75 type = oid_object_info(the_repository, oid, NULL);
77 ret = type_name(type);
78 if (!ret)
79 ret = _("unknown");
81 return ret;
84 static int objerror(struct object *obj, const char *err)
86 errors_found |= ERROR_OBJECT;
87 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
88 fprintf_ln(stderr, _("error in %s %s: %s"),
89 printable_type(&obj->oid, obj->type),
90 describe_object(&obj->oid), err);
91 return -1;
94 static int fsck_error_func(struct fsck_options *o,
95 const struct object_id *oid,
96 enum object_type object_type,
97 enum fsck_msg_type msg_type,
98 enum fsck_msg_id msg_id,
99 const char *message)
101 switch (msg_type) {
102 case FSCK_WARN:
103 /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
104 fprintf_ln(stderr, _("warning in %s %s: %s"),
105 printable_type(oid, object_type),
106 describe_object(oid), message);
107 return 0;
108 case FSCK_ERROR:
109 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
110 fprintf_ln(stderr, _("error in %s %s: %s"),
111 printable_type(oid, object_type),
112 describe_object(oid), message);
113 return 1;
114 default:
115 BUG("%d (FSCK_IGNORE?) should never trigger this callback",
116 msg_type);
120 static struct object_array pending;
122 static int mark_object(struct object *obj, enum object_type type,
123 void *data, struct fsck_options *options)
125 struct object *parent = data;
128 * The only case data is NULL or type is OBJ_ANY is when
129 * mark_object_reachable() calls us. All the callers of
130 * that function has non-NULL obj hence ...
132 if (!obj) {
133 /* ... these references to parent->fld are safe here */
134 printf_ln(_("broken link from %7s %s"),
135 printable_type(&parent->oid, parent->type),
136 describe_object(&parent->oid));
137 printf_ln(_("broken link from %7s %s"),
138 (type == OBJ_ANY ? _("unknown") : type_name(type)),
139 _("unknown"));
140 errors_found |= ERROR_REACHABLE;
141 return 1;
144 if (type != OBJ_ANY && obj->type != type)
145 /* ... and the reference to parent is safe here */
146 objerror(parent, _("wrong object type in link"));
148 if (obj->flags & REACHABLE)
149 return 0;
150 obj->flags |= REACHABLE;
152 if (is_promisor_object(&obj->oid))
154 * Further recursion does not need to be performed on this
155 * object since it is a promisor object (so it does not need to
156 * be added to "pending").
158 return 0;
160 if (!(obj->flags & HAS_OBJ)) {
161 if (parent && !has_object(the_repository, &obj->oid, 1)) {
162 printf_ln(_("broken link from %7s %s\n"
163 " to %7s %s"),
164 printable_type(&parent->oid, parent->type),
165 describe_object(&parent->oid),
166 printable_type(&obj->oid, obj->type),
167 describe_object(&obj->oid));
168 errors_found |= ERROR_REACHABLE;
170 return 1;
173 add_object_array(obj, NULL, &pending);
174 return 0;
177 static void mark_object_reachable(struct object *obj)
179 mark_object(obj, OBJ_ANY, NULL, NULL);
182 static int traverse_one_object(struct object *obj)
184 int result = fsck_walk(obj, obj, &fsck_walk_options);
186 if (obj->type == OBJ_TREE) {
187 struct tree *tree = (struct tree *)obj;
188 free_tree_buffer(tree);
190 return result;
193 static int traverse_reachable(void)
195 struct progress *progress = NULL;
196 unsigned int nr = 0;
197 int result = 0;
198 if (show_progress)
199 progress = start_delayed_progress(_("Checking connectivity"), 0);
200 while (pending.nr) {
201 result |= traverse_one_object(object_array_pop(&pending));
202 display_progress(progress, ++nr);
204 stop_progress(&progress);
205 return !!result;
208 static int mark_used(struct object *obj, enum object_type object_type,
209 void *data, struct fsck_options *options)
211 if (!obj)
212 return 1;
213 obj->flags |= USED;
214 return 0;
217 static void mark_unreachable_referents(const struct object_id *oid)
219 struct fsck_options options = FSCK_OPTIONS_DEFAULT;
220 struct object *obj = lookup_object(the_repository, oid);
222 if (!obj || !(obj->flags & HAS_OBJ))
223 return; /* not part of our original set */
224 if (obj->flags & REACHABLE)
225 return; /* reachable objects already traversed */
228 * Avoid passing OBJ_NONE to fsck_walk, which will parse the object
229 * (and we want to avoid parsing blobs).
231 if (obj->type == OBJ_NONE) {
232 enum object_type type = oid_object_info(the_repository,
233 &obj->oid, NULL);
234 if (type > 0)
235 object_as_type(obj, type, 0);
238 options.walk = mark_used;
239 fsck_walk(obj, NULL, &options);
240 if (obj->type == OBJ_TREE)
241 free_tree_buffer((struct tree *)obj);
244 static int mark_loose_unreachable_referents(const struct object_id *oid,
245 const char *path UNUSED,
246 void *data UNUSED)
248 mark_unreachable_referents(oid);
249 return 0;
252 static int mark_packed_unreachable_referents(const struct object_id *oid,
253 struct packed_git *pack UNUSED,
254 uint32_t pos UNUSED,
255 void *data UNUSED)
257 mark_unreachable_referents(oid);
258 return 0;
262 * Check a single reachable object
264 static void check_reachable_object(struct object *obj)
267 * We obviously want the object to be parsed,
268 * except if it was in a pack-file and we didn't
269 * do a full fsck
271 if (!(obj->flags & HAS_OBJ)) {
272 if (is_promisor_object(&obj->oid))
273 return;
274 if (has_object_pack(&obj->oid))
275 return; /* it is in pack - forget about it */
276 printf_ln(_("missing %s %s"),
277 printable_type(&obj->oid, obj->type),
278 describe_object(&obj->oid));
279 errors_found |= ERROR_REACHABLE;
280 return;
285 * Check a single unreachable object
287 static void check_unreachable_object(struct object *obj)
290 * Missing unreachable object? Ignore it. It's not like
291 * we miss it (since it can't be reached), nor do we want
292 * to complain about it being unreachable (since it does
293 * not exist).
295 if (!(obj->flags & HAS_OBJ))
296 return;
299 * Unreachable object that exists? Show it if asked to,
300 * since this is something that is prunable.
302 if (show_unreachable) {
303 printf_ln(_("unreachable %s %s"),
304 printable_type(&obj->oid, obj->type),
305 describe_object(&obj->oid));
306 return;
310 * "!USED" means that nothing at all points to it, including
311 * other unreachable objects. In other words, it's the "tip"
312 * of some set of unreachable objects, usually a commit that
313 * got dropped.
315 * Such starting points are more interesting than some random
316 * set of unreachable objects, so we show them even if the user
317 * hasn't asked for _all_ unreachable objects. If you have
318 * deleted a branch by mistake, this is a prime candidate to
319 * start looking at, for example.
321 if (!(obj->flags & USED)) {
322 if (show_dangling)
323 printf_ln(_("dangling %s %s"),
324 printable_type(&obj->oid, obj->type),
325 describe_object(&obj->oid));
326 if (write_lost_and_found) {
327 char *filename = git_pathdup("lost-found/%s/%s",
328 obj->type == OBJ_COMMIT ? "commit" : "other",
329 describe_object(&obj->oid));
330 FILE *f;
332 if (safe_create_leading_directories_const(filename)) {
333 error(_("could not create lost-found"));
334 free(filename);
335 return;
337 f = xfopen(filename, "w");
338 if (obj->type == OBJ_BLOB) {
339 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
340 die_errno(_("could not write '%s'"), filename);
341 } else
342 fprintf(f, "%s\n", describe_object(&obj->oid));
343 if (fclose(f))
344 die_errno(_("could not finish '%s'"),
345 filename);
346 free(filename);
348 return;
352 * Otherwise? It's there, it's unreachable, and some other unreachable
353 * object points to it. Ignore it - it's not interesting, and we showed
354 * all the interesting cases above.
358 static void check_object(struct object *obj)
360 if (verbose)
361 fprintf_ln(stderr, _("Checking %s"), describe_object(&obj->oid));
363 if (obj->flags & REACHABLE)
364 check_reachable_object(obj);
365 else
366 check_unreachable_object(obj);
369 static void check_connectivity(void)
371 int i, max;
373 /* Traverse the pending reachable objects */
374 traverse_reachable();
377 * With --connectivity-only, we won't have actually opened and marked
378 * unreachable objects with USED. Do that now to make --dangling, etc
379 * accurate.
381 if (connectivity_only && (show_dangling || write_lost_and_found)) {
383 * Even though we already have a "struct object" for each of
384 * these in memory, we must not iterate over the internal
385 * object hash as we do below. Our loop would potentially
386 * resize the hash, making our iteration invalid.
388 * Instead, we'll just go back to the source list of objects,
389 * and ignore any that weren't present in our earlier
390 * traversal.
392 for_each_loose_object(mark_loose_unreachable_referents, NULL, 0);
393 for_each_packed_object(mark_packed_unreachable_referents, NULL, 0);
396 /* Look up all the requirements, warn about missing objects.. */
397 max = get_max_object_index();
398 if (verbose)
399 fprintf_ln(stderr, _("Checking connectivity (%d objects)"), max);
401 for (i = 0; i < max; i++) {
402 struct object *obj = get_indexed_object(i);
404 if (obj)
405 check_object(obj);
409 static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
411 int err;
413 if (obj->flags & SEEN)
414 return 0;
415 obj->flags |= SEEN;
417 if (verbose)
418 fprintf_ln(stderr, _("Checking %s %s"),
419 printable_type(&obj->oid, obj->type),
420 describe_object(&obj->oid));
422 if (fsck_walk(obj, NULL, &fsck_obj_options))
423 objerror(obj, _("broken links"));
424 err = fsck_object(obj, buffer, size, &fsck_obj_options);
425 if (err)
426 goto out;
428 if (obj->type == OBJ_COMMIT) {
429 struct commit *commit = (struct commit *) obj;
431 if (!commit->parents && show_root)
432 printf_ln(_("root %s"),
433 describe_object(&commit->object.oid));
436 if (obj->type == OBJ_TAG) {
437 struct tag *tag = (struct tag *) obj;
439 if (show_tags && tag->tagged) {
440 printf_ln(_("tagged %s %s (%s) in %s"),
441 printable_type(&tag->tagged->oid, tag->tagged->type),
442 describe_object(&tag->tagged->oid),
443 tag->tag,
444 describe_object(&tag->object.oid));
448 out:
449 if (obj->type == OBJ_TREE)
450 free_tree_buffer((struct tree *)obj);
451 return err;
454 static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
455 unsigned long size, void *buffer, int *eaten)
458 * Note, buffer may be NULL if type is OBJ_BLOB. See
459 * verify_packfile(), data_valid variable for details.
461 struct object *obj;
462 obj = parse_object_buffer(the_repository, oid, type, size, buffer,
463 eaten);
464 if (!obj) {
465 errors_found |= ERROR_OBJECT;
466 return error(_("%s: object corrupt or missing"),
467 oid_to_hex(oid));
469 obj->flags &= ~(REACHABLE | SEEN);
470 obj->flags |= HAS_OBJ;
471 return fsck_obj(obj, buffer, size);
474 static int default_refs;
476 static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
477 timestamp_t timestamp)
479 struct object *obj;
481 if (!is_null_oid(oid)) {
482 obj = lookup_object(the_repository, oid);
483 if (obj && (obj->flags & HAS_OBJ)) {
484 if (timestamp)
485 fsck_put_object_name(&fsck_walk_options, oid,
486 "%s@{%"PRItime"}",
487 refname, timestamp);
488 obj->flags |= USED;
489 mark_object_reachable(obj);
490 } else if (!is_promisor_object(oid)) {
491 error(_("%s: invalid reflog entry %s"),
492 refname, oid_to_hex(oid));
493 errors_found |= ERROR_REACHABLE;
498 static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
499 const char *email UNUSED,
500 timestamp_t timestamp, int tz UNUSED,
501 const char *message UNUSED, void *cb_data)
503 const char *refname = cb_data;
505 if (verbose)
506 fprintf_ln(stderr, _("Checking reflog %s->%s"),
507 oid_to_hex(ooid), oid_to_hex(noid));
509 fsck_handle_reflog_oid(refname, ooid, 0);
510 fsck_handle_reflog_oid(refname, noid, timestamp);
511 return 0;
514 static int fsck_handle_reflog(const char *logname,
515 const struct object_id *oid UNUSED,
516 int flag UNUSED, void *cb_data)
518 struct strbuf refname = STRBUF_INIT;
520 strbuf_worktree_ref(cb_data, &refname, logname);
521 for_each_reflog_ent(refname.buf, fsck_handle_reflog_ent, refname.buf);
522 strbuf_release(&refname);
523 return 0;
526 static int fsck_handle_ref(const char *refname, const struct object_id *oid,
527 int flag UNUSED, void *cb_data UNUSED)
529 struct object *obj;
531 obj = parse_object(the_repository, oid);
532 if (!obj) {
533 if (is_promisor_object(oid)) {
535 * Increment default_refs anyway, because this is a
536 * valid ref.
538 default_refs++;
539 return 0;
541 error(_("%s: invalid sha1 pointer %s"),
542 refname, oid_to_hex(oid));
543 errors_found |= ERROR_REACHABLE;
544 /* We'll continue with the rest despite the error.. */
545 return 0;
547 if (obj->type != OBJ_COMMIT && is_branch(refname)) {
548 error(_("%s: not a commit"), refname);
549 errors_found |= ERROR_REFS;
551 default_refs++;
552 obj->flags |= USED;
553 fsck_put_object_name(&fsck_walk_options,
554 oid, "%s", refname);
555 mark_object_reachable(obj);
557 return 0;
560 static int fsck_head_link(const char *head_ref_name,
561 const char **head_points_at,
562 struct object_id *head_oid);
564 static void get_default_heads(void)
566 struct worktree **worktrees, **p;
567 const char *head_points_at;
568 struct object_id head_oid;
570 for_each_rawref(fsck_handle_ref, NULL);
572 worktrees = get_worktrees();
573 for (p = worktrees; *p; p++) {
574 struct worktree *wt = *p;
575 struct strbuf ref = STRBUF_INIT;
577 strbuf_worktree_ref(wt, &ref, "HEAD");
578 fsck_head_link(ref.buf, &head_points_at, &head_oid);
579 if (head_points_at && !is_null_oid(&head_oid))
580 fsck_handle_ref(ref.buf, &head_oid, 0, NULL);
581 strbuf_release(&ref);
583 if (include_reflogs)
584 refs_for_each_reflog(get_worktree_ref_store(wt),
585 fsck_handle_reflog, wt);
587 free_worktrees(worktrees);
590 * Not having any default heads isn't really fatal, but
591 * it does mean that "--unreachable" no longer makes any
592 * sense (since in this case everything will obviously
593 * be unreachable by definition.
595 * Showing dangling objects is valid, though (as those
596 * dangling objects are likely lost heads).
598 * So we just print a warning about it, and clear the
599 * "show_unreachable" flag.
601 if (!default_refs) {
602 fprintf_ln(stderr, _("notice: No default references"));
603 show_unreachable = 0;
607 struct for_each_loose_cb
609 struct progress *progress;
610 struct strbuf obj_type;
613 static int fsck_loose(const struct object_id *oid, const char *path, void *data)
615 struct for_each_loose_cb *cb_data = data;
616 struct object *obj;
617 enum object_type type = OBJ_NONE;
618 unsigned long size;
619 void *contents = NULL;
620 int eaten;
621 struct object_info oi = OBJECT_INFO_INIT;
622 struct object_id real_oid = *null_oid();
623 int err = 0;
625 strbuf_reset(&cb_data->obj_type);
626 oi.type_name = &cb_data->obj_type;
627 oi.sizep = &size;
628 oi.typep = &type;
630 if (read_loose_object(path, oid, &real_oid, &contents, &oi) < 0) {
631 if (contents && !oideq(&real_oid, oid))
632 err = error(_("%s: hash-path mismatch, found at: %s"),
633 oid_to_hex(&real_oid), path);
634 else
635 err = error(_("%s: object corrupt or missing: %s"),
636 oid_to_hex(oid), path);
638 if (type != OBJ_NONE && type < 0)
639 err = error(_("%s: object is of unknown type '%s': %s"),
640 oid_to_hex(&real_oid), cb_data->obj_type.buf,
641 path);
642 if (err < 0) {
643 errors_found |= ERROR_OBJECT;
644 free(contents);
645 return 0; /* keep checking other objects */
648 if (!contents && type != OBJ_BLOB)
649 BUG("read_loose_object streamed a non-blob");
651 obj = parse_object_buffer(the_repository, oid, type, size,
652 contents, &eaten);
654 if (!obj) {
655 errors_found |= ERROR_OBJECT;
656 error(_("%s: object could not be parsed: %s"),
657 oid_to_hex(oid), path);
658 if (!eaten)
659 free(contents);
660 return 0; /* keep checking other objects */
663 obj->flags &= ~(REACHABLE | SEEN);
664 obj->flags |= HAS_OBJ;
665 if (fsck_obj(obj, contents, size))
666 errors_found |= ERROR_OBJECT;
668 if (!eaten)
669 free(contents);
670 return 0; /* keep checking other objects, even if we saw an error */
673 static int fsck_cruft(const char *basename, const char *path,
674 void *data UNUSED)
676 if (!starts_with(basename, "tmp_obj_"))
677 fprintf_ln(stderr, _("bad sha1 file: %s"), path);
678 return 0;
681 static int fsck_subdir(unsigned int nr, const char *path UNUSED, void *data)
683 struct for_each_loose_cb *cb_data = data;
684 struct progress *progress = cb_data->progress;
685 display_progress(progress, nr + 1);
686 return 0;
689 static void fsck_object_dir(const char *path)
691 struct progress *progress = NULL;
692 struct for_each_loose_cb cb_data = {
693 .obj_type = STRBUF_INIT,
694 .progress = progress,
697 if (verbose)
698 fprintf_ln(stderr, _("Checking object directory"));
700 if (show_progress)
701 progress = start_progress(_("Checking object directories"), 256);
703 for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
704 &cb_data);
705 display_progress(progress, 256);
706 stop_progress(&progress);
707 strbuf_release(&cb_data.obj_type);
710 static int fsck_head_link(const char *head_ref_name,
711 const char **head_points_at,
712 struct object_id *head_oid)
714 int null_is_error = 0;
716 if (verbose)
717 fprintf_ln(stderr, _("Checking %s link"), head_ref_name);
719 *head_points_at = resolve_ref_unsafe(head_ref_name, 0, head_oid, NULL);
720 if (!*head_points_at) {
721 errors_found |= ERROR_REFS;
722 return error(_("invalid %s"), head_ref_name);
724 if (!strcmp(*head_points_at, head_ref_name))
725 /* detached HEAD */
726 null_is_error = 1;
727 else if (!starts_with(*head_points_at, "refs/heads/")) {
728 errors_found |= ERROR_REFS;
729 return error(_("%s points to something strange (%s)"),
730 head_ref_name, *head_points_at);
732 if (is_null_oid(head_oid)) {
733 if (null_is_error) {
734 errors_found |= ERROR_REFS;
735 return error(_("%s: detached HEAD points at nothing"),
736 head_ref_name);
738 fprintf_ln(stderr,
739 _("notice: %s points to an unborn branch (%s)"),
740 head_ref_name, *head_points_at + 11);
742 return 0;
745 static int fsck_cache_tree(struct cache_tree *it, const char *index_path)
747 int i;
748 int err = 0;
750 if (verbose)
751 fprintf_ln(stderr, _("Checking cache tree of %s"), index_path);
753 if (0 <= it->entry_count) {
754 struct object *obj = parse_object(the_repository, &it->oid);
755 if (!obj) {
756 error(_("%s: invalid sha1 pointer in cache-tree of %s"),
757 oid_to_hex(&it->oid), index_path);
758 errors_found |= ERROR_REFS;
759 return 1;
761 obj->flags |= USED;
762 fsck_put_object_name(&fsck_walk_options, &it->oid, ":");
763 mark_object_reachable(obj);
764 if (obj->type != OBJ_TREE)
765 err |= objerror(obj, _("non-tree in cache-tree"));
767 for (i = 0; i < it->subtree_nr; i++)
768 err |= fsck_cache_tree(it->down[i]->cache_tree, index_path);
769 return err;
772 static int fsck_resolve_undo(struct index_state *istate,
773 const char *index_path)
775 struct string_list_item *item;
776 struct string_list *resolve_undo = istate->resolve_undo;
778 if (!resolve_undo)
779 return 0;
781 for_each_string_list_item(item, resolve_undo) {
782 const char *path = item->string;
783 struct resolve_undo_info *ru = item->util;
784 int i;
786 if (!ru)
787 continue;
788 for (i = 0; i < 3; i++) {
789 struct object *obj;
791 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
792 continue;
794 obj = parse_object(the_repository, &ru->oid[i]);
795 if (!obj) {
796 error(_("%s: invalid sha1 pointer in resolve-undo of %s"),
797 oid_to_hex(&ru->oid[i]),
798 index_path);
799 errors_found |= ERROR_REFS;
800 continue;
802 obj->flags |= USED;
803 fsck_put_object_name(&fsck_walk_options, &ru->oid[i],
804 ":(%d):%s", i, path);
805 mark_object_reachable(obj);
808 return 0;
811 static void fsck_index(struct index_state *istate, const char *index_path,
812 int is_main_index)
814 unsigned int i;
816 /* TODO: audit for interaction with sparse-index. */
817 ensure_full_index(istate);
818 for (i = 0; i < istate->cache_nr; i++) {
819 unsigned int mode;
820 struct blob *blob;
821 struct object *obj;
823 mode = istate->cache[i]->ce_mode;
824 if (S_ISGITLINK(mode))
825 continue;
826 blob = lookup_blob(the_repository,
827 &istate->cache[i]->oid);
828 if (!blob)
829 continue;
830 obj = &blob->object;
831 obj->flags |= USED;
832 fsck_put_object_name(&fsck_walk_options, &obj->oid,
833 "%s:%s",
834 is_main_index ? "" : index_path,
835 istate->cache[i]->name);
836 mark_object_reachable(obj);
838 if (istate->cache_tree)
839 fsck_cache_tree(istate->cache_tree, index_path);
840 fsck_resolve_undo(istate, index_path);
843 static void mark_object_for_connectivity(const struct object_id *oid)
845 struct object *obj = lookup_unknown_object(the_repository, oid);
846 obj->flags |= HAS_OBJ;
849 static int mark_loose_for_connectivity(const struct object_id *oid,
850 const char *path UNUSED,
851 void *data UNUSED)
853 mark_object_for_connectivity(oid);
854 return 0;
857 static int mark_packed_for_connectivity(const struct object_id *oid,
858 struct packed_git *pack UNUSED,
859 uint32_t pos UNUSED,
860 void *data UNUSED)
862 mark_object_for_connectivity(oid);
863 return 0;
866 static int check_pack_rev_indexes(struct repository *r, int show_progress)
868 struct progress *progress = NULL;
869 uint32_t pack_count = 0;
870 int res = 0;
872 if (show_progress) {
873 for (struct packed_git *p = get_all_packs(r); p; p = p->next)
874 pack_count++;
875 progress = start_delayed_progress("Verifying reverse pack-indexes", pack_count);
876 pack_count = 0;
879 for (struct packed_git *p = get_all_packs(r); p; p = p->next) {
880 int load_error = load_pack_revindex_from_disk(p);
882 if (load_error < 0) {
883 error(_("unable to load rev-index for pack '%s'"), p->pack_name);
884 res = ERROR_PACK_REV_INDEX;
885 } else if (!load_error &&
886 !load_pack_revindex(r, p) &&
887 verify_pack_revindex(p)) {
888 error(_("invalid rev-index for pack '%s'"), p->pack_name);
889 res = ERROR_PACK_REV_INDEX;
891 display_progress(progress, ++pack_count);
893 stop_progress(&progress);
895 return res;
898 static char const * const fsck_usage[] = {
899 N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n"
900 " [--[no-]full] [--strict] [--verbose] [--lost-found]\n"
901 " [--[no-]dangling] [--[no-]progress] [--connectivity-only]\n"
902 " [--[no-]name-objects] [<object>...]"),
903 NULL
906 static struct option fsck_opts[] = {
907 OPT__VERBOSE(&verbose, N_("be verbose")),
908 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
909 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
910 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
911 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
912 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
913 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
914 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
915 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
916 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
917 OPT_BOOL(0, "lost-found", &write_lost_and_found,
918 N_("write dangling objects in .git/lost-found")),
919 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
920 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
921 OPT_END(),
924 int cmd_fsck(int argc, const char **argv, const char *prefix)
926 int i;
927 struct object_directory *odb;
929 /* fsck knows how to handle missing promisor objects */
930 fetch_if_missing = 0;
932 errors_found = 0;
933 read_replace_refs = 0;
934 save_commit_buffer = 0;
936 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
938 fsck_walk_options.walk = mark_object;
939 fsck_obj_options.walk = mark_used;
940 fsck_obj_options.error_func = fsck_error_func;
941 if (check_strict)
942 fsck_obj_options.strict = 1;
944 if (show_progress == -1)
945 show_progress = isatty(2);
946 if (verbose)
947 show_progress = 0;
949 if (write_lost_and_found) {
950 check_full = 1;
951 include_reflogs = 0;
954 if (name_objects)
955 fsck_enable_object_names(&fsck_walk_options);
957 git_config(git_fsck_config, &fsck_obj_options);
958 prepare_repo_settings(the_repository);
960 if (connectivity_only) {
961 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
962 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
963 } else {
964 prepare_alt_odb(the_repository);
965 for (odb = the_repository->objects->odb; odb; odb = odb->next)
966 fsck_object_dir(odb->path);
968 if (check_full) {
969 struct packed_git *p;
970 uint32_t total = 0, count = 0;
971 struct progress *progress = NULL;
973 if (show_progress) {
974 for (p = get_all_packs(the_repository); p;
975 p = p->next) {
976 if (open_pack_index(p))
977 continue;
978 total += p->num_objects;
981 progress = start_progress(_("Checking objects"), total);
983 for (p = get_all_packs(the_repository); p;
984 p = p->next) {
985 /* verify gives error messages itself */
986 if (verify_pack(the_repository,
987 p, fsck_obj_buffer,
988 progress, count))
989 errors_found |= ERROR_PACK;
990 count += p->num_objects;
992 stop_progress(&progress);
995 if (fsck_finish(&fsck_obj_options))
996 errors_found |= ERROR_OBJECT;
999 for (i = 0; i < argc; i++) {
1000 const char *arg = argv[i];
1001 struct object_id oid;
1002 if (!repo_get_oid(the_repository, arg, &oid)) {
1003 struct object *obj = lookup_object(the_repository,
1004 &oid);
1006 if (!obj || !(obj->flags & HAS_OBJ)) {
1007 if (is_promisor_object(&oid))
1008 continue;
1009 error(_("%s: object missing"), oid_to_hex(&oid));
1010 errors_found |= ERROR_OBJECT;
1011 continue;
1014 obj->flags |= USED;
1015 fsck_put_object_name(&fsck_walk_options, &oid,
1016 "%s", arg);
1017 mark_object_reachable(obj);
1018 continue;
1020 error(_("invalid parameter: expected sha1, got '%s'"), arg);
1021 errors_found |= ERROR_OBJECT;
1025 * If we've not been given any explicit head information, do the
1026 * default ones from .git/refs. We also consider the index file
1027 * in this case (ie this implies --cache).
1029 if (!argc) {
1030 get_default_heads();
1031 keep_cache_objects = 1;
1034 if (keep_cache_objects) {
1035 struct worktree **worktrees, **p;
1037 verify_index_checksum = 1;
1038 verify_ce_order = 1;
1040 worktrees = get_worktrees();
1041 for (p = worktrees; *p; p++) {
1042 struct worktree *wt = *p;
1043 struct index_state istate =
1044 INDEX_STATE_INIT(the_repository);
1045 char *path;
1048 * Make a copy since the buffer is reusable
1049 * and may get overwritten by other calls
1050 * while we're examining the index.
1052 path = xstrdup(worktree_git_path(wt, "index"));
1053 read_index_from(&istate, path, get_worktree_git_dir(wt));
1054 fsck_index(&istate, path, wt->is_current);
1055 discard_index(&istate);
1056 free(path);
1058 free_worktrees(worktrees);
1061 errors_found |= check_pack_rev_indexes(the_repository, show_progress);
1062 if (verify_bitmap_files(the_repository))
1063 errors_found |= ERROR_BITMAP;
1065 check_connectivity();
1067 if (the_repository->settings.core_commit_graph) {
1068 struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
1070 prepare_alt_odb(the_repository);
1071 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
1072 child_process_init(&commit_graph_verify);
1073 commit_graph_verify.git_cmd = 1;
1074 strvec_pushl(&commit_graph_verify.args, "commit-graph",
1075 "verify", "--object-dir", odb->path, NULL);
1076 if (run_command(&commit_graph_verify))
1077 errors_found |= ERROR_COMMIT_GRAPH;
1081 if (the_repository->settings.core_multi_pack_index) {
1082 struct child_process midx_verify = CHILD_PROCESS_INIT;
1084 prepare_alt_odb(the_repository);
1085 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
1086 child_process_init(&midx_verify);
1087 midx_verify.git_cmd = 1;
1088 strvec_pushl(&midx_verify.args, "multi-pack-index",
1089 "verify", "--object-dir", odb->path, NULL);
1090 if (run_command(&midx_verify))
1091 errors_found |= ERROR_MULTI_PACK_INDEX;
1095 return errors_found;