4 #include "repository.h"
12 #include "cache-tree.h"
13 #include "tree-walk.h"
15 #include "parse-options.h"
18 #include "streaming.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"
30 #include "pack-revindex.h"
31 #include "pack-bitmap.h"
33 #define REACHABLE 0x0001
35 #define HAS_OBJ 0x0004
36 /* This flag is set if something points to this object. */
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
;
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
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
)
75 type
= oid_object_info(the_repository
, oid
, NULL
);
77 ret
= type_name(type
);
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
);
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
,
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
);
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
);
115 BUG("%d (FSCK_IGNORE?) should never trigger this callback",
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 ...
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
)),
140 errors_found
|= ERROR_REACHABLE
;
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
)
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").
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"
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
;
173 add_object_array(obj
, NULL
, &pending
);
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
);
193 static int traverse_reachable(void)
195 struct progress
*progress
= NULL
;
199 progress
= start_delayed_progress(_("Checking connectivity"), 0);
201 result
|= traverse_one_object(object_array_pop(&pending
));
202 display_progress(progress
, ++nr
);
204 stop_progress(&progress
);
208 static int mark_used(struct object
*obj
, enum object_type object_type
,
209 void *data
, struct fsck_options
*options
)
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
,
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
,
248 mark_unreachable_referents(oid
);
252 static int mark_packed_unreachable_referents(const struct object_id
*oid
,
253 struct packed_git
*pack UNUSED
,
257 mark_unreachable_referents(oid
);
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
271 if (!(obj
->flags
& HAS_OBJ
)) {
272 if (is_promisor_object(&obj
->oid
))
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
;
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
295 if (!(obj
->flags
& HAS_OBJ
))
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
));
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
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
)) {
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
));
332 if (safe_create_leading_directories_const(filename
)) {
333 error(_("could not create lost-found"));
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
);
342 fprintf(f
, "%s\n", describe_object(&obj
->oid
));
344 die_errno(_("could not finish '%s'"),
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
)
361 fprintf_ln(stderr
, _("Checking %s"), describe_object(&obj
->oid
));
363 if (obj
->flags
& REACHABLE
)
364 check_reachable_object(obj
);
366 check_unreachable_object(obj
);
369 static void check_connectivity(void)
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
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
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();
399 fprintf_ln(stderr
, _("Checking connectivity (%d objects)"), max
);
401 for (i
= 0; i
< max
; i
++) {
402 struct object
*obj
= get_indexed_object(i
);
409 static int fsck_obj(struct object
*obj
, void *buffer
, unsigned long size
)
413 if (obj
->flags
& SEEN
)
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
);
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
),
444 describe_object(&tag
->object
.oid
));
449 if (obj
->type
== OBJ_TREE
)
450 free_tree_buffer((struct tree
*)obj
);
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.
462 obj
= parse_object_buffer(the_repository
, oid
, type
, size
, buffer
,
465 errors_found
|= ERROR_OBJECT
;
466 return error(_("%s: object corrupt or missing"),
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
)
481 if (!is_null_oid(oid
)) {
482 obj
= lookup_object(the_repository
, oid
);
483 if (obj
&& (obj
->flags
& HAS_OBJ
)) {
485 fsck_put_object_name(&fsck_walk_options
, oid
,
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
;
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
);
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
);
526 static int fsck_handle_ref(const char *refname
, const struct object_id
*oid
,
527 int flag UNUSED
, void *cb_data UNUSED
)
531 obj
= parse_object(the_repository
, oid
);
533 if (is_promisor_object(oid
)) {
535 * Increment default_refs anyway, because this is a
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.. */
547 if (obj
->type
!= OBJ_COMMIT
&& is_branch(refname
)) {
548 error(_("%s: not a commit"), refname
);
549 errors_found
|= ERROR_REFS
;
553 fsck_put_object_name(&fsck_walk_options
,
555 mark_object_reachable(obj
);
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
);
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.
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
;
617 enum object_type type
= OBJ_NONE
;
619 void *contents
= NULL
;
621 struct object_info oi
= OBJECT_INFO_INIT
;
622 struct object_id real_oid
= *null_oid();
625 strbuf_reset(&cb_data
->obj_type
);
626 oi
.type_name
= &cb_data
->obj_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
);
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
,
643 errors_found
|= ERROR_OBJECT
;
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
,
655 errors_found
|= ERROR_OBJECT
;
656 error(_("%s: object could not be parsed: %s"),
657 oid_to_hex(oid
), path
);
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
;
670 return 0; /* keep checking other objects, even if we saw an error */
673 static int fsck_cruft(const char *basename
, const char *path
,
676 if (!starts_with(basename
, "tmp_obj_"))
677 fprintf_ln(stderr
, _("bad sha1 file: %s"), path
);
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);
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
,
698 fprintf_ln(stderr
, _("Checking object directory"));
701 progress
= start_progress(_("Checking object directories"), 256);
703 for_each_loose_file_in_objdir(path
, fsck_loose
, fsck_cruft
, fsck_subdir
,
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;
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
))
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
)) {
734 errors_found
|= ERROR_REFS
;
735 return error(_("%s: detached HEAD points at nothing"),
739 _("notice: %s points to an unborn branch (%s)"),
740 head_ref_name
, *head_points_at
+ 11);
745 static int fsck_cache_tree(struct cache_tree
*it
, const char *index_path
)
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
);
756 error(_("%s: invalid sha1 pointer in cache-tree of %s"),
757 oid_to_hex(&it
->oid
), index_path
);
758 errors_found
|= ERROR_REFS
;
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
);
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
;
781 for_each_string_list_item(item
, resolve_undo
) {
782 const char *path
= item
->string
;
783 struct resolve_undo_info
*ru
= item
->util
;
788 for (i
= 0; i
< 3; i
++) {
791 if (!ru
->mode
[i
] || !S_ISREG(ru
->mode
[i
]))
794 obj
= parse_object(the_repository
, &ru
->oid
[i
]);
796 error(_("%s: invalid sha1 pointer in resolve-undo of %s"),
797 oid_to_hex(&ru
->oid
[i
]),
799 errors_found
|= ERROR_REFS
;
803 fsck_put_object_name(&fsck_walk_options
, &ru
->oid
[i
],
804 ":(%d):%s", i
, path
);
805 mark_object_reachable(obj
);
811 static void fsck_index(struct index_state
*istate
, const char *index_path
,
816 /* TODO: audit for interaction with sparse-index. */
817 ensure_full_index(istate
);
818 for (i
= 0; i
< istate
->cache_nr
; i
++) {
823 mode
= istate
->cache
[i
]->ce_mode
;
824 if (S_ISGITLINK(mode
))
826 blob
= lookup_blob(the_repository
,
827 &istate
->cache
[i
]->oid
);
832 fsck_put_object_name(&fsck_walk_options
, &obj
->oid
,
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
,
853 mark_object_for_connectivity(oid
);
857 static int mark_packed_for_connectivity(const struct object_id
*oid
,
858 struct packed_git
*pack UNUSED
,
862 mark_object_for_connectivity(oid
);
866 static int check_pack_rev_indexes(struct repository
*r
, int show_progress
)
868 struct progress
*progress
= NULL
;
869 uint32_t pack_count
= 0;
873 for (struct packed_git
*p
= get_all_packs(r
); p
; p
= p
->next
)
875 progress
= start_delayed_progress("Verifying reverse pack-indexes", pack_count
);
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
);
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>...]"),
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")),
924 int cmd_fsck(int argc
, const char **argv
, const char *prefix
)
927 struct object_directory
*odb
;
929 /* fsck knows how to handle missing promisor objects */
930 fetch_if_missing
= 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
;
942 fsck_obj_options
.strict
= 1;
944 if (show_progress
== -1)
945 show_progress
= isatty(2);
949 if (write_lost_and_found
) {
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);
964 prepare_alt_odb(the_repository
);
965 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
)
966 fsck_object_dir(odb
->path
);
969 struct packed_git
*p
;
970 uint32_t total
= 0, count
= 0;
971 struct progress
*progress
= NULL
;
974 for (p
= get_all_packs(the_repository
); p
;
976 if (open_pack_index(p
))
978 total
+= p
->num_objects
;
981 progress
= start_progress(_("Checking objects"), total
);
983 for (p
= get_all_packs(the_repository
); p
;
985 /* verify gives error messages itself */
986 if (verify_pack(the_repository
,
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
,
1006 if (!obj
|| !(obj
->flags
& HAS_OBJ
)) {
1007 if (is_promisor_object(&oid
))
1009 error(_("%s: object missing"), oid_to_hex(&oid
));
1010 errors_found
|= ERROR_OBJECT
;
1015 fsck_put_object_name(&fsck_walk_options
, &oid
,
1017 mark_object_reachable(obj
);
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).
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
);
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
);
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
;