replace strbuf_expand_dict_cb() with strbuf_expand_step()
[git.git] / builtin / fsck.c
blobdcc165bf0c508da9b0ebe238a83fc5861def40c7
1 #include "builtin.h"
2 #include "cache.h"
3 #include "gettext.h"
4 #include "hex.h"
5 #include "repository.h"
6 #include "config.h"
7 #include "commit.h"
8 #include "tree.h"
9 #include "blob.h"
10 #include "tag.h"
11 #include "refs.h"
12 #include "pack.h"
13 #include "cache-tree.h"
14 #include "tree-walk.h"
15 #include "fsck.h"
16 #include "parse-options.h"
17 #include "dir.h"
18 #include "progress.h"
19 #include "streaming.h"
20 #include "decorate.h"
21 #include "packfile.h"
22 #include "object-file.h"
23 #include "object-name.h"
24 #include "object-store.h"
25 #include "replace-object.h"
26 #include "resolve-undo.h"
27 #include "run-command.h"
28 #include "worktree.h"
29 #include "pack-revindex.h"
30 #include "pack-bitmap.h"
32 #define REACHABLE 0x0001
33 #define SEEN 0x0002
34 #define HAS_OBJ 0x0004
35 /* This flag is set if something points to this object. */
36 #define USED 0x0008
38 static int show_root;
39 static int show_tags;
40 static int show_unreachable;
41 static int include_reflogs = 1;
42 static int check_full = 1;
43 static int connectivity_only;
44 static int check_strict;
45 static int keep_cache_objects;
46 static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
47 static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
48 static int errors_found;
49 static int write_lost_and_found;
50 static int verbose;
51 static int show_progress = -1;
52 static int show_dangling = 1;
53 static int name_objects;
54 #define ERROR_OBJECT 01
55 #define ERROR_REACHABLE 02
56 #define ERROR_PACK 04
57 #define ERROR_REFS 010
58 #define ERROR_COMMIT_GRAPH 020
59 #define ERROR_MULTI_PACK_INDEX 040
60 #define ERROR_PACK_REV_INDEX 0100
61 #define ERROR_BITMAP 0200
63 static const char *describe_object(const struct object_id *oid)
65 return fsck_describe_object(&fsck_walk_options, oid);
68 static const char *printable_type(const struct object_id *oid,
69 enum object_type type)
71 const char *ret;
73 if (type == OBJ_NONE)
74 type = oid_object_info(the_repository, oid, NULL);
76 ret = type_name(type);
77 if (!ret)
78 ret = _("unknown");
80 return ret;
83 static int objerror(struct object *obj, const char *err)
85 errors_found |= ERROR_OBJECT;
86 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
87 fprintf_ln(stderr, _("error in %s %s: %s"),
88 printable_type(&obj->oid, obj->type),
89 describe_object(&obj->oid), err);
90 return -1;
93 static int fsck_error_func(struct fsck_options *o,
94 const struct object_id *oid,
95 enum object_type object_type,
96 enum fsck_msg_type msg_type,
97 enum fsck_msg_id msg_id,
98 const char *message)
100 switch (msg_type) {
101 case FSCK_WARN:
102 /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
103 fprintf_ln(stderr, _("warning in %s %s: %s"),
104 printable_type(oid, object_type),
105 describe_object(oid), message);
106 return 0;
107 case FSCK_ERROR:
108 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
109 fprintf_ln(stderr, _("error in %s %s: %s"),
110 printable_type(oid, object_type),
111 describe_object(oid), message);
112 return 1;
113 default:
114 BUG("%d (FSCK_IGNORE?) should never trigger this callback",
115 msg_type);
119 static struct object_array pending;
121 static int mark_object(struct object *obj, enum object_type type,
122 void *data, struct fsck_options *options)
124 struct object *parent = data;
127 * The only case data is NULL or type is OBJ_ANY is when
128 * mark_object_reachable() calls us. All the callers of
129 * that function has non-NULL obj hence ...
131 if (!obj) {
132 /* ... these references to parent->fld are safe here */
133 printf_ln(_("broken link from %7s %s"),
134 printable_type(&parent->oid, parent->type),
135 describe_object(&parent->oid));
136 printf_ln(_("broken link from %7s %s"),
137 (type == OBJ_ANY ? _("unknown") : type_name(type)),
138 _("unknown"));
139 errors_found |= ERROR_REACHABLE;
140 return 1;
143 if (type != OBJ_ANY && obj->type != type)
144 /* ... and the reference to parent is safe here */
145 objerror(parent, _("wrong object type in link"));
147 if (obj->flags & REACHABLE)
148 return 0;
149 obj->flags |= REACHABLE;
151 if (is_promisor_object(&obj->oid))
153 * Further recursion does not need to be performed on this
154 * object since it is a promisor object (so it does not need to
155 * be added to "pending").
157 return 0;
159 if (!(obj->flags & HAS_OBJ)) {
160 if (parent && !has_object(the_repository, &obj->oid, 1)) {
161 printf_ln(_("broken link from %7s %s\n"
162 " to %7s %s"),
163 printable_type(&parent->oid, parent->type),
164 describe_object(&parent->oid),
165 printable_type(&obj->oid, obj->type),
166 describe_object(&obj->oid));
167 errors_found |= ERROR_REACHABLE;
169 return 1;
172 add_object_array(obj, NULL, &pending);
173 return 0;
176 static void mark_object_reachable(struct object *obj)
178 mark_object(obj, OBJ_ANY, NULL, NULL);
181 static int traverse_one_object(struct object *obj)
183 int result = fsck_walk(obj, obj, &fsck_walk_options);
185 if (obj->type == OBJ_TREE) {
186 struct tree *tree = (struct tree *)obj;
187 free_tree_buffer(tree);
189 return result;
192 static int traverse_reachable(void)
194 struct progress *progress = NULL;
195 unsigned int nr = 0;
196 int result = 0;
197 if (show_progress)
198 progress = start_delayed_progress(_("Checking connectivity"), 0);
199 while (pending.nr) {
200 result |= traverse_one_object(object_array_pop(&pending));
201 display_progress(progress, ++nr);
203 stop_progress(&progress);
204 return !!result;
207 static int mark_used(struct object *obj, enum object_type object_type,
208 void *data, struct fsck_options *options)
210 if (!obj)
211 return 1;
212 obj->flags |= USED;
213 return 0;
216 static void mark_unreachable_referents(const struct object_id *oid)
218 struct fsck_options options = FSCK_OPTIONS_DEFAULT;
219 struct object *obj = lookup_object(the_repository, oid);
221 if (!obj || !(obj->flags & HAS_OBJ))
222 return; /* not part of our original set */
223 if (obj->flags & REACHABLE)
224 return; /* reachable objects already traversed */
227 * Avoid passing OBJ_NONE to fsck_walk, which will parse the object
228 * (and we want to avoid parsing blobs).
230 if (obj->type == OBJ_NONE) {
231 enum object_type type = oid_object_info(the_repository,
232 &obj->oid, NULL);
233 if (type > 0)
234 object_as_type(obj, type, 0);
237 options.walk = mark_used;
238 fsck_walk(obj, NULL, &options);
239 if (obj->type == OBJ_TREE)
240 free_tree_buffer((struct tree *)obj);
243 static int mark_loose_unreachable_referents(const struct object_id *oid,
244 const char *path UNUSED,
245 void *data UNUSED)
247 mark_unreachable_referents(oid);
248 return 0;
251 static int mark_packed_unreachable_referents(const struct object_id *oid,
252 struct packed_git *pack UNUSED,
253 uint32_t pos UNUSED,
254 void *data UNUSED)
256 mark_unreachable_referents(oid);
257 return 0;
261 * Check a single reachable object
263 static void check_reachable_object(struct object *obj)
266 * We obviously want the object to be parsed,
267 * except if it was in a pack-file and we didn't
268 * do a full fsck
270 if (!(obj->flags & HAS_OBJ)) {
271 if (is_promisor_object(&obj->oid))
272 return;
273 if (has_object_pack(&obj->oid))
274 return; /* it is in pack - forget about it */
275 printf_ln(_("missing %s %s"),
276 printable_type(&obj->oid, obj->type),
277 describe_object(&obj->oid));
278 errors_found |= ERROR_REACHABLE;
279 return;
284 * Check a single unreachable object
286 static void check_unreachable_object(struct object *obj)
289 * Missing unreachable object? Ignore it. It's not like
290 * we miss it (since it can't be reached), nor do we want
291 * to complain about it being unreachable (since it does
292 * not exist).
294 if (!(obj->flags & HAS_OBJ))
295 return;
298 * Unreachable object that exists? Show it if asked to,
299 * since this is something that is prunable.
301 if (show_unreachable) {
302 printf_ln(_("unreachable %s %s"),
303 printable_type(&obj->oid, obj->type),
304 describe_object(&obj->oid));
305 return;
309 * "!USED" means that nothing at all points to it, including
310 * other unreachable objects. In other words, it's the "tip"
311 * of some set of unreachable objects, usually a commit that
312 * got dropped.
314 * Such starting points are more interesting than some random
315 * set of unreachable objects, so we show them even if the user
316 * hasn't asked for _all_ unreachable objects. If you have
317 * deleted a branch by mistake, this is a prime candidate to
318 * start looking at, for example.
320 if (!(obj->flags & USED)) {
321 if (show_dangling)
322 printf_ln(_("dangling %s %s"),
323 printable_type(&obj->oid, obj->type),
324 describe_object(&obj->oid));
325 if (write_lost_and_found) {
326 char *filename = git_pathdup("lost-found/%s/%s",
327 obj->type == OBJ_COMMIT ? "commit" : "other",
328 describe_object(&obj->oid));
329 FILE *f;
331 if (safe_create_leading_directories_const(filename)) {
332 error(_("could not create lost-found"));
333 free(filename);
334 return;
336 f = xfopen(filename, "w");
337 if (obj->type == OBJ_BLOB) {
338 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
339 die_errno(_("could not write '%s'"), filename);
340 } else
341 fprintf(f, "%s\n", describe_object(&obj->oid));
342 if (fclose(f))
343 die_errno(_("could not finish '%s'"),
344 filename);
345 free(filename);
347 return;
351 * Otherwise? It's there, it's unreachable, and some other unreachable
352 * object points to it. Ignore it - it's not interesting, and we showed
353 * all the interesting cases above.
357 static void check_object(struct object *obj)
359 if (verbose)
360 fprintf_ln(stderr, _("Checking %s"), describe_object(&obj->oid));
362 if (obj->flags & REACHABLE)
363 check_reachable_object(obj);
364 else
365 check_unreachable_object(obj);
368 static void check_connectivity(void)
370 int i, max;
372 /* Traverse the pending reachable objects */
373 traverse_reachable();
376 * With --connectivity-only, we won't have actually opened and marked
377 * unreachable objects with USED. Do that now to make --dangling, etc
378 * accurate.
380 if (connectivity_only && (show_dangling || write_lost_and_found)) {
382 * Even though we already have a "struct object" for each of
383 * these in memory, we must not iterate over the internal
384 * object hash as we do below. Our loop would potentially
385 * resize the hash, making our iteration invalid.
387 * Instead, we'll just go back to the source list of objects,
388 * and ignore any that weren't present in our earlier
389 * traversal.
391 for_each_loose_object(mark_loose_unreachable_referents, NULL, 0);
392 for_each_packed_object(mark_packed_unreachable_referents, NULL, 0);
395 /* Look up all the requirements, warn about missing objects.. */
396 max = get_max_object_index();
397 if (verbose)
398 fprintf_ln(stderr, _("Checking connectivity (%d objects)"), max);
400 for (i = 0; i < max; i++) {
401 struct object *obj = get_indexed_object(i);
403 if (obj)
404 check_object(obj);
408 static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
410 int err;
412 if (obj->flags & SEEN)
413 return 0;
414 obj->flags |= SEEN;
416 if (verbose)
417 fprintf_ln(stderr, _("Checking %s %s"),
418 printable_type(&obj->oid, obj->type),
419 describe_object(&obj->oid));
421 if (fsck_walk(obj, NULL, &fsck_obj_options))
422 objerror(obj, _("broken links"));
423 err = fsck_object(obj, buffer, size, &fsck_obj_options);
424 if (err)
425 goto out;
427 if (obj->type == OBJ_COMMIT) {
428 struct commit *commit = (struct commit *) obj;
430 if (!commit->parents && show_root)
431 printf_ln(_("root %s"),
432 describe_object(&commit->object.oid));
435 if (obj->type == OBJ_TAG) {
436 struct tag *tag = (struct tag *) obj;
438 if (show_tags && tag->tagged) {
439 printf_ln(_("tagged %s %s (%s) in %s"),
440 printable_type(&tag->tagged->oid, tag->tagged->type),
441 describe_object(&tag->tagged->oid),
442 tag->tag,
443 describe_object(&tag->object.oid));
447 out:
448 if (obj->type == OBJ_TREE)
449 free_tree_buffer((struct tree *)obj);
450 return err;
453 static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
454 unsigned long size, void *buffer, int *eaten)
457 * Note, buffer may be NULL if type is OBJ_BLOB. See
458 * verify_packfile(), data_valid variable for details.
460 struct object *obj;
461 obj = parse_object_buffer(the_repository, oid, type, size, buffer,
462 eaten);
463 if (!obj) {
464 errors_found |= ERROR_OBJECT;
465 return error(_("%s: object corrupt or missing"),
466 oid_to_hex(oid));
468 obj->flags &= ~(REACHABLE | SEEN);
469 obj->flags |= HAS_OBJ;
470 return fsck_obj(obj, buffer, size);
473 static int default_refs;
475 static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
476 timestamp_t timestamp)
478 struct object *obj;
480 if (!is_null_oid(oid)) {
481 obj = lookup_object(the_repository, oid);
482 if (obj && (obj->flags & HAS_OBJ)) {
483 if (timestamp)
484 fsck_put_object_name(&fsck_walk_options, oid,
485 "%s@{%"PRItime"}",
486 refname, timestamp);
487 obj->flags |= USED;
488 mark_object_reachable(obj);
489 } else if (!is_promisor_object(oid)) {
490 error(_("%s: invalid reflog entry %s"),
491 refname, oid_to_hex(oid));
492 errors_found |= ERROR_REACHABLE;
497 static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
498 const char *email UNUSED,
499 timestamp_t timestamp, int tz UNUSED,
500 const char *message UNUSED, void *cb_data)
502 const char *refname = cb_data;
504 if (verbose)
505 fprintf_ln(stderr, _("Checking reflog %s->%s"),
506 oid_to_hex(ooid), oid_to_hex(noid));
508 fsck_handle_reflog_oid(refname, ooid, 0);
509 fsck_handle_reflog_oid(refname, noid, timestamp);
510 return 0;
513 static int fsck_handle_reflog(const char *logname,
514 const struct object_id *oid UNUSED,
515 int flag UNUSED, void *cb_data)
517 struct strbuf refname = STRBUF_INIT;
519 strbuf_worktree_ref(cb_data, &refname, logname);
520 for_each_reflog_ent(refname.buf, fsck_handle_reflog_ent, refname.buf);
521 strbuf_release(&refname);
522 return 0;
525 static int fsck_handle_ref(const char *refname, const struct object_id *oid,
526 int flag UNUSED, void *cb_data UNUSED)
528 struct object *obj;
530 obj = parse_object(the_repository, oid);
531 if (!obj) {
532 if (is_promisor_object(oid)) {
534 * Increment default_refs anyway, because this is a
535 * valid ref.
537 default_refs++;
538 return 0;
540 error(_("%s: invalid sha1 pointer %s"),
541 refname, oid_to_hex(oid));
542 errors_found |= ERROR_REACHABLE;
543 /* We'll continue with the rest despite the error.. */
544 return 0;
546 if (obj->type != OBJ_COMMIT && is_branch(refname)) {
547 error(_("%s: not a commit"), refname);
548 errors_found |= ERROR_REFS;
550 default_refs++;
551 obj->flags |= USED;
552 fsck_put_object_name(&fsck_walk_options,
553 oid, "%s", refname);
554 mark_object_reachable(obj);
556 return 0;
559 static int fsck_head_link(const char *head_ref_name,
560 const char **head_points_at,
561 struct object_id *head_oid);
563 static void get_default_heads(void)
565 struct worktree **worktrees, **p;
566 const char *head_points_at;
567 struct object_id head_oid;
569 for_each_rawref(fsck_handle_ref, NULL);
571 worktrees = get_worktrees();
572 for (p = worktrees; *p; p++) {
573 struct worktree *wt = *p;
574 struct strbuf ref = STRBUF_INIT;
576 strbuf_worktree_ref(wt, &ref, "HEAD");
577 fsck_head_link(ref.buf, &head_points_at, &head_oid);
578 if (head_points_at && !is_null_oid(&head_oid))
579 fsck_handle_ref(ref.buf, &head_oid, 0, NULL);
580 strbuf_release(&ref);
582 if (include_reflogs)
583 refs_for_each_reflog(get_worktree_ref_store(wt),
584 fsck_handle_reflog, wt);
586 free_worktrees(worktrees);
589 * Not having any default heads isn't really fatal, but
590 * it does mean that "--unreachable" no longer makes any
591 * sense (since in this case everything will obviously
592 * be unreachable by definition.
594 * Showing dangling objects is valid, though (as those
595 * dangling objects are likely lost heads).
597 * So we just print a warning about it, and clear the
598 * "show_unreachable" flag.
600 if (!default_refs) {
601 fprintf_ln(stderr, _("notice: No default references"));
602 show_unreachable = 0;
606 struct for_each_loose_cb
608 struct progress *progress;
609 struct strbuf obj_type;
612 static int fsck_loose(const struct object_id *oid, const char *path, void *data)
614 struct for_each_loose_cb *cb_data = data;
615 struct object *obj;
616 enum object_type type = OBJ_NONE;
617 unsigned long size;
618 void *contents = NULL;
619 int eaten;
620 struct object_info oi = OBJECT_INFO_INIT;
621 struct object_id real_oid = *null_oid();
622 int err = 0;
624 strbuf_reset(&cb_data->obj_type);
625 oi.type_name = &cb_data->obj_type;
626 oi.sizep = &size;
627 oi.typep = &type;
629 if (read_loose_object(path, oid, &real_oid, &contents, &oi) < 0) {
630 if (contents && !oideq(&real_oid, oid))
631 err = error(_("%s: hash-path mismatch, found at: %s"),
632 oid_to_hex(&real_oid), path);
633 else
634 err = error(_("%s: object corrupt or missing: %s"),
635 oid_to_hex(oid), path);
637 if (type != OBJ_NONE && type < 0)
638 err = error(_("%s: object is of unknown type '%s': %s"),
639 oid_to_hex(&real_oid), cb_data->obj_type.buf,
640 path);
641 if (err < 0) {
642 errors_found |= ERROR_OBJECT;
643 free(contents);
644 return 0; /* keep checking other objects */
647 if (!contents && type != OBJ_BLOB)
648 BUG("read_loose_object streamed a non-blob");
650 obj = parse_object_buffer(the_repository, oid, type, size,
651 contents, &eaten);
653 if (!obj) {
654 errors_found |= ERROR_OBJECT;
655 error(_("%s: object could not be parsed: %s"),
656 oid_to_hex(oid), path);
657 if (!eaten)
658 free(contents);
659 return 0; /* keep checking other objects */
662 obj->flags &= ~(REACHABLE | SEEN);
663 obj->flags |= HAS_OBJ;
664 if (fsck_obj(obj, contents, size))
665 errors_found |= ERROR_OBJECT;
667 if (!eaten)
668 free(contents);
669 return 0; /* keep checking other objects, even if we saw an error */
672 static int fsck_cruft(const char *basename, const char *path,
673 void *data UNUSED)
675 if (!starts_with(basename, "tmp_obj_"))
676 fprintf_ln(stderr, _("bad sha1 file: %s"), path);
677 return 0;
680 static int fsck_subdir(unsigned int nr, const char *path UNUSED, void *data)
682 struct for_each_loose_cb *cb_data = data;
683 struct progress *progress = cb_data->progress;
684 display_progress(progress, nr + 1);
685 return 0;
688 static void fsck_object_dir(const char *path)
690 struct progress *progress = NULL;
691 struct for_each_loose_cb cb_data = {
692 .obj_type = STRBUF_INIT,
693 .progress = progress,
696 if (verbose)
697 fprintf_ln(stderr, _("Checking object directory"));
699 if (show_progress)
700 progress = start_progress(_("Checking object directories"), 256);
702 for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
703 &cb_data);
704 display_progress(progress, 256);
705 stop_progress(&progress);
706 strbuf_release(&cb_data.obj_type);
709 static int fsck_head_link(const char *head_ref_name,
710 const char **head_points_at,
711 struct object_id *head_oid)
713 int null_is_error = 0;
715 if (verbose)
716 fprintf_ln(stderr, _("Checking %s link"), head_ref_name);
718 *head_points_at = resolve_ref_unsafe(head_ref_name, 0, head_oid, NULL);
719 if (!*head_points_at) {
720 errors_found |= ERROR_REFS;
721 return error(_("invalid %s"), head_ref_name);
723 if (!strcmp(*head_points_at, head_ref_name))
724 /* detached HEAD */
725 null_is_error = 1;
726 else if (!starts_with(*head_points_at, "refs/heads/")) {
727 errors_found |= ERROR_REFS;
728 return error(_("%s points to something strange (%s)"),
729 head_ref_name, *head_points_at);
731 if (is_null_oid(head_oid)) {
732 if (null_is_error) {
733 errors_found |= ERROR_REFS;
734 return error(_("%s: detached HEAD points at nothing"),
735 head_ref_name);
737 fprintf_ln(stderr,
738 _("notice: %s points to an unborn branch (%s)"),
739 head_ref_name, *head_points_at + 11);
741 return 0;
744 static int fsck_cache_tree(struct cache_tree *it, const char *index_path)
746 int i;
747 int err = 0;
749 if (verbose)
750 fprintf_ln(stderr, _("Checking cache tree of %s"), index_path);
752 if (0 <= it->entry_count) {
753 struct object *obj = parse_object(the_repository, &it->oid);
754 if (!obj) {
755 error(_("%s: invalid sha1 pointer in cache-tree of %s"),
756 oid_to_hex(&it->oid), index_path);
757 errors_found |= ERROR_REFS;
758 return 1;
760 obj->flags |= USED;
761 fsck_put_object_name(&fsck_walk_options, &it->oid, ":");
762 mark_object_reachable(obj);
763 if (obj->type != OBJ_TREE)
764 err |= objerror(obj, _("non-tree in cache-tree"));
766 for (i = 0; i < it->subtree_nr; i++)
767 err |= fsck_cache_tree(it->down[i]->cache_tree, index_path);
768 return err;
771 static int fsck_resolve_undo(struct index_state *istate,
772 const char *index_path)
774 struct string_list_item *item;
775 struct string_list *resolve_undo = istate->resolve_undo;
777 if (!resolve_undo)
778 return 0;
780 for_each_string_list_item(item, resolve_undo) {
781 const char *path = item->string;
782 struct resolve_undo_info *ru = item->util;
783 int i;
785 if (!ru)
786 continue;
787 for (i = 0; i < 3; i++) {
788 struct object *obj;
790 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
791 continue;
793 obj = parse_object(the_repository, &ru->oid[i]);
794 if (!obj) {
795 error(_("%s: invalid sha1 pointer in resolve-undo of %s"),
796 oid_to_hex(&ru->oid[i]),
797 index_path);
798 errors_found |= ERROR_REFS;
799 continue;
801 obj->flags |= USED;
802 fsck_put_object_name(&fsck_walk_options, &ru->oid[i],
803 ":(%d):%s", i, path);
804 mark_object_reachable(obj);
807 return 0;
810 static void fsck_index(struct index_state *istate, const char *index_path,
811 int is_main_index)
813 unsigned int i;
815 /* TODO: audit for interaction with sparse-index. */
816 ensure_full_index(istate);
817 for (i = 0; i < istate->cache_nr; i++) {
818 unsigned int mode;
819 struct blob *blob;
820 struct object *obj;
822 mode = istate->cache[i]->ce_mode;
823 if (S_ISGITLINK(mode))
824 continue;
825 blob = lookup_blob(the_repository,
826 &istate->cache[i]->oid);
827 if (!blob)
828 continue;
829 obj = &blob->object;
830 obj->flags |= USED;
831 fsck_put_object_name(&fsck_walk_options, &obj->oid,
832 "%s:%s",
833 is_main_index ? "" : index_path,
834 istate->cache[i]->name);
835 mark_object_reachable(obj);
837 if (istate->cache_tree)
838 fsck_cache_tree(istate->cache_tree, index_path);
839 fsck_resolve_undo(istate, index_path);
842 static void mark_object_for_connectivity(const struct object_id *oid)
844 struct object *obj = lookup_unknown_object(the_repository, oid);
845 obj->flags |= HAS_OBJ;
848 static int mark_loose_for_connectivity(const struct object_id *oid,
849 const char *path UNUSED,
850 void *data UNUSED)
852 mark_object_for_connectivity(oid);
853 return 0;
856 static int mark_packed_for_connectivity(const struct object_id *oid,
857 struct packed_git *pack UNUSED,
858 uint32_t pos UNUSED,
859 void *data UNUSED)
861 mark_object_for_connectivity(oid);
862 return 0;
865 static int check_pack_rev_indexes(struct repository *r, int show_progress)
867 struct progress *progress = NULL;
868 uint32_t pack_count = 0;
869 int res = 0;
871 if (show_progress) {
872 for (struct packed_git *p = get_all_packs(r); p; p = p->next)
873 pack_count++;
874 progress = start_delayed_progress("Verifying reverse pack-indexes", pack_count);
875 pack_count = 0;
878 for (struct packed_git *p = get_all_packs(r); p; p = p->next) {
879 int load_error = load_pack_revindex_from_disk(p);
881 if (load_error < 0) {
882 error(_("unable to load rev-index for pack '%s'"), p->pack_name);
883 res = ERROR_PACK_REV_INDEX;
884 } else if (!load_error &&
885 !load_pack_revindex(r, p) &&
886 verify_pack_revindex(p)) {
887 error(_("invalid rev-index for pack '%s'"), p->pack_name);
888 res = ERROR_PACK_REV_INDEX;
890 display_progress(progress, ++pack_count);
892 stop_progress(&progress);
894 return res;
897 static char const * const fsck_usage[] = {
898 N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n"
899 " [--[no-]full] [--strict] [--verbose] [--lost-found]\n"
900 " [--[no-]dangling] [--[no-]progress] [--connectivity-only]\n"
901 " [--[no-]name-objects] [<object>...]"),
902 NULL
905 static struct option fsck_opts[] = {
906 OPT__VERBOSE(&verbose, N_("be verbose")),
907 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
908 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
909 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
910 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
911 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
912 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
913 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
914 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
915 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
916 OPT_BOOL(0, "lost-found", &write_lost_and_found,
917 N_("write dangling objects in .git/lost-found")),
918 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
919 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
920 OPT_END(),
923 int cmd_fsck(int argc, const char **argv, const char *prefix)
925 int i;
926 struct object_directory *odb;
928 /* fsck knows how to handle missing promisor objects */
929 fetch_if_missing = 0;
931 errors_found = 0;
932 read_replace_refs = 0;
933 save_commit_buffer = 0;
935 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
937 fsck_walk_options.walk = mark_object;
938 fsck_obj_options.walk = mark_used;
939 fsck_obj_options.error_func = fsck_error_func;
940 if (check_strict)
941 fsck_obj_options.strict = 1;
943 if (show_progress == -1)
944 show_progress = isatty(2);
945 if (verbose)
946 show_progress = 0;
948 if (write_lost_and_found) {
949 check_full = 1;
950 include_reflogs = 0;
953 if (name_objects)
954 fsck_enable_object_names(&fsck_walk_options);
956 git_config(git_fsck_config, &fsck_obj_options);
957 prepare_repo_settings(the_repository);
959 if (connectivity_only) {
960 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
961 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
962 } else {
963 prepare_alt_odb(the_repository);
964 for (odb = the_repository->objects->odb; odb; odb = odb->next)
965 fsck_object_dir(odb->path);
967 if (check_full) {
968 struct packed_git *p;
969 uint32_t total = 0, count = 0;
970 struct progress *progress = NULL;
972 if (show_progress) {
973 for (p = get_all_packs(the_repository); p;
974 p = p->next) {
975 if (open_pack_index(p))
976 continue;
977 total += p->num_objects;
980 progress = start_progress(_("Checking objects"), total);
982 for (p = get_all_packs(the_repository); p;
983 p = p->next) {
984 /* verify gives error messages itself */
985 if (verify_pack(the_repository,
986 p, fsck_obj_buffer,
987 progress, count))
988 errors_found |= ERROR_PACK;
989 count += p->num_objects;
991 stop_progress(&progress);
994 if (fsck_finish(&fsck_obj_options))
995 errors_found |= ERROR_OBJECT;
998 for (i = 0; i < argc; i++) {
999 const char *arg = argv[i];
1000 struct object_id oid;
1001 if (!repo_get_oid(the_repository, arg, &oid)) {
1002 struct object *obj = lookup_object(the_repository,
1003 &oid);
1005 if (!obj || !(obj->flags & HAS_OBJ)) {
1006 if (is_promisor_object(&oid))
1007 continue;
1008 error(_("%s: object missing"), oid_to_hex(&oid));
1009 errors_found |= ERROR_OBJECT;
1010 continue;
1013 obj->flags |= USED;
1014 fsck_put_object_name(&fsck_walk_options, &oid,
1015 "%s", arg);
1016 mark_object_reachable(obj);
1017 continue;
1019 error(_("invalid parameter: expected sha1, got '%s'"), arg);
1020 errors_found |= ERROR_OBJECT;
1024 * If we've not been given any explicit head information, do the
1025 * default ones from .git/refs. We also consider the index file
1026 * in this case (ie this implies --cache).
1028 if (!argc) {
1029 get_default_heads();
1030 keep_cache_objects = 1;
1033 if (keep_cache_objects) {
1034 struct worktree **worktrees, **p;
1036 verify_index_checksum = 1;
1037 verify_ce_order = 1;
1039 worktrees = get_worktrees();
1040 for (p = worktrees; *p; p++) {
1041 struct worktree *wt = *p;
1042 struct index_state istate =
1043 INDEX_STATE_INIT(the_repository);
1044 char *path;
1047 * Make a copy since the buffer is reusable
1048 * and may get overwritten by other calls
1049 * while we're examining the index.
1051 path = xstrdup(worktree_git_path(wt, "index"));
1052 read_index_from(&istate, path, get_worktree_git_dir(wt));
1053 fsck_index(&istate, path, wt->is_current);
1054 discard_index(&istate);
1055 free(path);
1057 free_worktrees(worktrees);
1060 errors_found |= check_pack_rev_indexes(the_repository, show_progress);
1061 if (verify_bitmap_files(the_repository))
1062 errors_found |= ERROR_BITMAP;
1064 check_connectivity();
1066 if (the_repository->settings.core_commit_graph) {
1067 struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
1069 prepare_alt_odb(the_repository);
1070 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
1071 child_process_init(&commit_graph_verify);
1072 commit_graph_verify.git_cmd = 1;
1073 strvec_pushl(&commit_graph_verify.args, "commit-graph",
1074 "verify", "--object-dir", odb->path, NULL);
1075 if (run_command(&commit_graph_verify))
1076 errors_found |= ERROR_COMMIT_GRAPH;
1080 if (the_repository->settings.core_multi_pack_index) {
1081 struct child_process midx_verify = CHILD_PROCESS_INIT;
1083 prepare_alt_odb(the_repository);
1084 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
1085 child_process_init(&midx_verify);
1086 midx_verify.git_cmd = 1;
1087 strvec_pushl(&midx_verify.args, "multi-pack-index",
1088 "verify", "--object-dir", odb->path, NULL);
1089 if (run_command(&midx_verify))
1090 errors_found |= ERROR_MULTI_PACK_INDEX;
1094 return errors_found;