5 #include "repository.h"
13 #include "cache-tree.h"
14 #include "tree-walk.h"
16 #include "parse-options.h"
19 #include "streaming.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"
29 #include "pack-revindex.h"
30 #include "pack-bitmap.h"
32 #define REACHABLE 0x0001
34 #define HAS_OBJ 0x0004
35 /* This flag is set if something points to this object. */
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
;
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
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
)
74 type
= oid_object_info(the_repository
, oid
, NULL
);
76 ret
= type_name(type
);
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
);
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
,
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
);
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
);
114 BUG("%d (FSCK_IGNORE?) should never trigger this callback",
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 ...
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
)),
139 errors_found
|= ERROR_REACHABLE
;
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
)
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").
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"
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
;
172 add_object_array(obj
, NULL
, &pending
);
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
);
192 static int traverse_reachable(void)
194 struct progress
*progress
= NULL
;
198 progress
= start_delayed_progress(_("Checking connectivity"), 0);
200 result
|= traverse_one_object(object_array_pop(&pending
));
201 display_progress(progress
, ++nr
);
203 stop_progress(&progress
);
207 static int mark_used(struct object
*obj
, enum object_type object_type
,
208 void *data
, struct fsck_options
*options
)
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
,
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
,
247 mark_unreachable_referents(oid
);
251 static int mark_packed_unreachable_referents(const struct object_id
*oid
,
252 struct packed_git
*pack UNUSED
,
256 mark_unreachable_referents(oid
);
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
270 if (!(obj
->flags
& HAS_OBJ
)) {
271 if (is_promisor_object(&obj
->oid
))
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
;
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
294 if (!(obj
->flags
& HAS_OBJ
))
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
));
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
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
)) {
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
));
331 if (safe_create_leading_directories_const(filename
)) {
332 error(_("could not create lost-found"));
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
);
341 fprintf(f
, "%s\n", describe_object(&obj
->oid
));
343 die_errno(_("could not finish '%s'"),
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
)
360 fprintf_ln(stderr
, _("Checking %s"), describe_object(&obj
->oid
));
362 if (obj
->flags
& REACHABLE
)
363 check_reachable_object(obj
);
365 check_unreachable_object(obj
);
368 static void check_connectivity(void)
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
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
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();
398 fprintf_ln(stderr
, _("Checking connectivity (%d objects)"), max
);
400 for (i
= 0; i
< max
; i
++) {
401 struct object
*obj
= get_indexed_object(i
);
408 static int fsck_obj(struct object
*obj
, void *buffer
, unsigned long size
)
412 if (obj
->flags
& SEEN
)
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
);
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
),
443 describe_object(&tag
->object
.oid
));
448 if (obj
->type
== OBJ_TREE
)
449 free_tree_buffer((struct tree
*)obj
);
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.
461 obj
= parse_object_buffer(the_repository
, oid
, type
, size
, buffer
,
464 errors_found
|= ERROR_OBJECT
;
465 return error(_("%s: object corrupt or missing"),
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
)
480 if (!is_null_oid(oid
)) {
481 obj
= lookup_object(the_repository
, oid
);
482 if (obj
&& (obj
->flags
& HAS_OBJ
)) {
484 fsck_put_object_name(&fsck_walk_options
, oid
,
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
;
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
);
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
);
525 static int fsck_handle_ref(const char *refname
, const struct object_id
*oid
,
526 int flag UNUSED
, void *cb_data UNUSED
)
530 obj
= parse_object(the_repository
, oid
);
532 if (is_promisor_object(oid
)) {
534 * Increment default_refs anyway, because this is a
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.. */
546 if (obj
->type
!= OBJ_COMMIT
&& is_branch(refname
)) {
547 error(_("%s: not a commit"), refname
);
548 errors_found
|= ERROR_REFS
;
552 fsck_put_object_name(&fsck_walk_options
,
554 mark_object_reachable(obj
);
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
);
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.
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
;
616 enum object_type type
= OBJ_NONE
;
618 void *contents
= NULL
;
620 struct object_info oi
= OBJECT_INFO_INIT
;
621 struct object_id real_oid
= *null_oid();
624 strbuf_reset(&cb_data
->obj_type
);
625 oi
.type_name
= &cb_data
->obj_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
);
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
,
642 errors_found
|= ERROR_OBJECT
;
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
,
654 errors_found
|= ERROR_OBJECT
;
655 error(_("%s: object could not be parsed: %s"),
656 oid_to_hex(oid
), path
);
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
;
669 return 0; /* keep checking other objects, even if we saw an error */
672 static int fsck_cruft(const char *basename
, const char *path
,
675 if (!starts_with(basename
, "tmp_obj_"))
676 fprintf_ln(stderr
, _("bad sha1 file: %s"), path
);
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);
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
,
697 fprintf_ln(stderr
, _("Checking object directory"));
700 progress
= start_progress(_("Checking object directories"), 256);
702 for_each_loose_file_in_objdir(path
, fsck_loose
, fsck_cruft
, fsck_subdir
,
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;
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
))
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
)) {
733 errors_found
|= ERROR_REFS
;
734 return error(_("%s: detached HEAD points at nothing"),
738 _("notice: %s points to an unborn branch (%s)"),
739 head_ref_name
, *head_points_at
+ 11);
744 static int fsck_cache_tree(struct cache_tree
*it
, const char *index_path
)
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
);
755 error(_("%s: invalid sha1 pointer in cache-tree of %s"),
756 oid_to_hex(&it
->oid
), index_path
);
757 errors_found
|= ERROR_REFS
;
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
);
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
;
780 for_each_string_list_item(item
, resolve_undo
) {
781 const char *path
= item
->string
;
782 struct resolve_undo_info
*ru
= item
->util
;
787 for (i
= 0; i
< 3; i
++) {
790 if (!ru
->mode
[i
] || !S_ISREG(ru
->mode
[i
]))
793 obj
= parse_object(the_repository
, &ru
->oid
[i
]);
795 error(_("%s: invalid sha1 pointer in resolve-undo of %s"),
796 oid_to_hex(&ru
->oid
[i
]),
798 errors_found
|= ERROR_REFS
;
802 fsck_put_object_name(&fsck_walk_options
, &ru
->oid
[i
],
803 ":(%d):%s", i
, path
);
804 mark_object_reachable(obj
);
810 static void fsck_index(struct index_state
*istate
, const char *index_path
,
815 /* TODO: audit for interaction with sparse-index. */
816 ensure_full_index(istate
);
817 for (i
= 0; i
< istate
->cache_nr
; i
++) {
822 mode
= istate
->cache
[i
]->ce_mode
;
823 if (S_ISGITLINK(mode
))
825 blob
= lookup_blob(the_repository
,
826 &istate
->cache
[i
]->oid
);
831 fsck_put_object_name(&fsck_walk_options
, &obj
->oid
,
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
,
852 mark_object_for_connectivity(oid
);
856 static int mark_packed_for_connectivity(const struct object_id
*oid
,
857 struct packed_git
*pack UNUSED
,
861 mark_object_for_connectivity(oid
);
865 static int check_pack_rev_indexes(struct repository
*r
, int show_progress
)
867 struct progress
*progress
= NULL
;
868 uint32_t pack_count
= 0;
872 for (struct packed_git
*p
= get_all_packs(r
); p
; p
= p
->next
)
874 progress
= start_delayed_progress("Verifying reverse pack-indexes", pack_count
);
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
);
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>...]"),
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")),
923 int cmd_fsck(int argc
, const char **argv
, const char *prefix
)
926 struct object_directory
*odb
;
928 /* fsck knows how to handle missing promisor objects */
929 fetch_if_missing
= 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
;
941 fsck_obj_options
.strict
= 1;
943 if (show_progress
== -1)
944 show_progress
= isatty(2);
948 if (write_lost_and_found
) {
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);
963 prepare_alt_odb(the_repository
);
964 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
)
965 fsck_object_dir(odb
->path
);
968 struct packed_git
*p
;
969 uint32_t total
= 0, count
= 0;
970 struct progress
*progress
= NULL
;
973 for (p
= get_all_packs(the_repository
); p
;
975 if (open_pack_index(p
))
977 total
+= p
->num_objects
;
980 progress
= start_progress(_("Checking objects"), total
);
982 for (p
= get_all_packs(the_repository
); p
;
984 /* verify gives error messages itself */
985 if (verify_pack(the_repository
,
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
,
1005 if (!obj
|| !(obj
->flags
& HAS_OBJ
)) {
1006 if (is_promisor_object(&oid
))
1008 error(_("%s: object missing"), oid_to_hex(&oid
));
1009 errors_found
|= ERROR_OBJECT
;
1014 fsck_put_object_name(&fsck_walk_options
, &oid
,
1016 mark_object_reachable(obj
);
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).
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
);
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
);
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
;