4 #include "repository.h"
12 #include "cache-tree.h"
14 #include "parse-options.h"
16 #include "streaming.h"
18 #include "object-file.h"
19 #include "object-name.h"
20 #include "object-store-ll.h"
22 #include "read-cache-ll.h"
23 #include "replace-object.h"
24 #include "resolve-undo.h"
25 #include "run-command.h"
26 #include "sparse-index.h"
28 #include "pack-revindex.h"
29 #include "pack-bitmap.h"
31 #define REACHABLE 0x0001
33 #define HAS_OBJ 0x0004
34 /* This flag is set if something points to this object. */
39 static int show_unreachable
;
40 static int include_reflogs
= 1;
41 static int check_full
= 1;
42 static int connectivity_only
;
43 static int check_strict
;
44 static int keep_cache_objects
;
45 static struct fsck_options fsck_walk_options
= FSCK_OPTIONS_DEFAULT
;
46 static struct fsck_options fsck_obj_options
= FSCK_OPTIONS_DEFAULT
;
47 static int errors_found
;
48 static int write_lost_and_found
;
50 static int show_progress
= -1;
51 static int show_dangling
= 1;
52 static int name_objects
;
53 #define ERROR_OBJECT 01
54 #define ERROR_REACHABLE 02
56 #define ERROR_REFS 010
57 #define ERROR_COMMIT_GRAPH 020
58 #define ERROR_MULTI_PACK_INDEX 040
59 #define ERROR_PACK_REV_INDEX 0100
60 #define ERROR_BITMAP 0200
62 static const char *describe_object(const struct object_id
*oid
)
64 return fsck_describe_object(&fsck_walk_options
, oid
);
67 static const char *printable_type(const struct object_id
*oid
,
68 enum object_type type
)
73 type
= oid_object_info(the_repository
, oid
, NULL
);
75 ret
= type_name(type
);
82 static int objerror(struct object
*obj
, const char *err
)
84 errors_found
|= ERROR_OBJECT
;
85 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
86 fprintf_ln(stderr
, _("error in %s %s: %s"),
87 printable_type(&obj
->oid
, obj
->type
),
88 describe_object(&obj
->oid
), err
);
92 static int fsck_error_func(struct fsck_options
*o UNUSED
,
93 const struct object_id
*oid
,
94 enum object_type object_type
,
95 enum fsck_msg_type msg_type
,
96 enum fsck_msg_id msg_id UNUSED
,
101 /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
102 fprintf_ln(stderr
, _("warning in %s %s: %s"),
103 printable_type(oid
, object_type
),
104 describe_object(oid
), message
);
107 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
108 fprintf_ln(stderr
, _("error in %s %s: %s"),
109 printable_type(oid
, object_type
),
110 describe_object(oid
), message
);
113 BUG("%d (FSCK_IGNORE?) should never trigger this callback",
118 static struct object_array pending
;
120 static int mark_object(struct object
*obj
, enum object_type type
,
121 void *data
, struct fsck_options
*options UNUSED
)
123 struct object
*parent
= data
;
126 * The only case data is NULL or type is OBJ_ANY is when
127 * mark_object_reachable() calls us. All the callers of
128 * that function has non-NULL obj hence ...
131 /* ... these references to parent->fld are safe here */
132 printf_ln(_("broken link from %7s %s"),
133 printable_type(&parent
->oid
, parent
->type
),
134 describe_object(&parent
->oid
));
135 printf_ln(_("broken link from %7s %s"),
136 (type
== OBJ_ANY
? _("unknown") : type_name(type
)),
138 errors_found
|= ERROR_REACHABLE
;
142 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
143 /* ... and the reference to parent is safe here */
144 objerror(parent
, _("wrong object type in link"));
146 if (obj
->flags
& REACHABLE
)
148 obj
->flags
|= REACHABLE
;
150 if (is_promisor_object(&obj
->oid
))
152 * Further recursion does not need to be performed on this
153 * object since it is a promisor object (so it does not need to
154 * be added to "pending").
158 if (!(obj
->flags
& HAS_OBJ
)) {
159 if (parent
&& !has_object(the_repository
, &obj
->oid
, 1)) {
160 printf_ln(_("broken link from %7s %s\n"
162 printable_type(&parent
->oid
, parent
->type
),
163 describe_object(&parent
->oid
),
164 printable_type(&obj
->oid
, obj
->type
),
165 describe_object(&obj
->oid
));
166 errors_found
|= ERROR_REACHABLE
;
171 add_object_array(obj
, NULL
, &pending
);
175 static void mark_object_reachable(struct object
*obj
)
177 mark_object(obj
, OBJ_ANY
, NULL
, NULL
);
180 static int traverse_one_object(struct object
*obj
)
182 int result
= fsck_walk(obj
, obj
, &fsck_walk_options
);
184 if (obj
->type
== OBJ_TREE
) {
185 struct tree
*tree
= (struct tree
*)obj
;
186 free_tree_buffer(tree
);
191 static int traverse_reachable(void)
193 struct progress
*progress
= NULL
;
197 progress
= start_delayed_progress(_("Checking connectivity"), 0);
199 result
|= traverse_one_object(object_array_pop(&pending
));
200 display_progress(progress
, ++nr
);
202 stop_progress(&progress
);
206 static int mark_used(struct object
*obj
, enum object_type type UNUSED
,
207 void *data UNUSED
, struct fsck_options
*options UNUSED
)
215 static void mark_unreachable_referents(const struct object_id
*oid
)
217 struct fsck_options options
= FSCK_OPTIONS_DEFAULT
;
218 struct object
*obj
= lookup_object(the_repository
, oid
);
220 if (!obj
|| !(obj
->flags
& HAS_OBJ
))
221 return; /* not part of our original set */
222 if (obj
->flags
& REACHABLE
)
223 return; /* reachable objects already traversed */
226 * Avoid passing OBJ_NONE to fsck_walk, which will parse the object
227 * (and we want to avoid parsing blobs).
229 if (obj
->type
== OBJ_NONE
) {
230 enum object_type type
= oid_object_info(the_repository
,
233 object_as_type(obj
, type
, 0);
236 options
.walk
= mark_used
;
237 fsck_walk(obj
, NULL
, &options
);
238 if (obj
->type
== OBJ_TREE
)
239 free_tree_buffer((struct tree
*)obj
);
242 static int mark_loose_unreachable_referents(const struct object_id
*oid
,
243 const char *path UNUSED
,
246 mark_unreachable_referents(oid
);
250 static int mark_packed_unreachable_referents(const struct object_id
*oid
,
251 struct packed_git
*pack UNUSED
,
255 mark_unreachable_referents(oid
);
260 * Check a single reachable object
262 static void check_reachable_object(struct object
*obj
)
265 * We obviously want the object to be parsed,
266 * except if it was in a pack-file and we didn't
269 if (!(obj
->flags
& HAS_OBJ
)) {
270 if (is_promisor_object(&obj
->oid
))
272 if (has_object_pack(&obj
->oid
))
273 return; /* it is in pack - forget about it */
274 printf_ln(_("missing %s %s"),
275 printable_type(&obj
->oid
, obj
->type
),
276 describe_object(&obj
->oid
));
277 errors_found
|= ERROR_REACHABLE
;
283 * Check a single unreachable object
285 static void check_unreachable_object(struct object
*obj
)
288 * Missing unreachable object? Ignore it. It's not like
289 * we miss it (since it can't be reached), nor do we want
290 * to complain about it being unreachable (since it does
293 if (!(obj
->flags
& HAS_OBJ
))
297 * Unreachable object that exists? Show it if asked to,
298 * since this is something that is prunable.
300 if (show_unreachable
) {
301 printf_ln(_("unreachable %s %s"),
302 printable_type(&obj
->oid
, obj
->type
),
303 describe_object(&obj
->oid
));
308 * "!USED" means that nothing at all points to it, including
309 * other unreachable objects. In other words, it's the "tip"
310 * of some set of unreachable objects, usually a commit that
313 * Such starting points are more interesting than some random
314 * set of unreachable objects, so we show them even if the user
315 * hasn't asked for _all_ unreachable objects. If you have
316 * deleted a branch by mistake, this is a prime candidate to
317 * start looking at, for example.
319 if (!(obj
->flags
& USED
)) {
321 printf_ln(_("dangling %s %s"),
322 printable_type(&obj
->oid
, obj
->type
),
323 describe_object(&obj
->oid
));
324 if (write_lost_and_found
) {
325 char *filename
= git_pathdup("lost-found/%s/%s",
326 obj
->type
== OBJ_COMMIT
? "commit" : "other",
327 describe_object(&obj
->oid
));
330 if (safe_create_leading_directories_const(filename
)) {
331 error(_("could not create lost-found"));
335 f
= xfopen(filename
, "w");
336 if (obj
->type
== OBJ_BLOB
) {
337 if (stream_blob_to_fd(fileno(f
), &obj
->oid
, NULL
, 1))
338 die_errno(_("could not write '%s'"), filename
);
340 fprintf(f
, "%s\n", describe_object(&obj
->oid
));
342 die_errno(_("could not finish '%s'"),
350 * Otherwise? It's there, it's unreachable, and some other unreachable
351 * object points to it. Ignore it - it's not interesting, and we showed
352 * all the interesting cases above.
356 static void check_object(struct object
*obj
)
359 fprintf_ln(stderr
, _("Checking %s"), describe_object(&obj
->oid
));
361 if (obj
->flags
& REACHABLE
)
362 check_reachable_object(obj
);
364 check_unreachable_object(obj
);
367 static void check_connectivity(void)
371 /* Traverse the pending reachable objects */
372 traverse_reachable();
375 * With --connectivity-only, we won't have actually opened and marked
376 * unreachable objects with USED. Do that now to make --dangling, etc
379 if (connectivity_only
&& (show_dangling
|| write_lost_and_found
)) {
381 * Even though we already have a "struct object" for each of
382 * these in memory, we must not iterate over the internal
383 * object hash as we do below. Our loop would potentially
384 * resize the hash, making our iteration invalid.
386 * Instead, we'll just go back to the source list of objects,
387 * and ignore any that weren't present in our earlier
390 for_each_loose_object(mark_loose_unreachable_referents
, NULL
, 0);
391 for_each_packed_object(mark_packed_unreachable_referents
, NULL
, 0);
394 /* Look up all the requirements, warn about missing objects.. */
395 max
= get_max_object_index();
397 fprintf_ln(stderr
, _("Checking connectivity (%d objects)"), max
);
399 for (i
= 0; i
< max
; i
++) {
400 struct object
*obj
= get_indexed_object(i
);
407 static int fsck_obj(struct object
*obj
, void *buffer
, unsigned long size
)
411 if (obj
->flags
& SEEN
)
416 fprintf_ln(stderr
, _("Checking %s %s"),
417 printable_type(&obj
->oid
, obj
->type
),
418 describe_object(&obj
->oid
));
420 if (fsck_walk(obj
, NULL
, &fsck_obj_options
))
421 objerror(obj
, _("broken links"));
422 err
= fsck_object(obj
, buffer
, size
, &fsck_obj_options
);
426 if (obj
->type
== OBJ_COMMIT
) {
427 struct commit
*commit
= (struct commit
*) obj
;
429 if (!commit
->parents
&& show_root
)
430 printf_ln(_("root %s"),
431 describe_object(&commit
->object
.oid
));
434 if (obj
->type
== OBJ_TAG
) {
435 struct tag
*tag
= (struct tag
*) obj
;
437 if (show_tags
&& tag
->tagged
) {
438 printf_ln(_("tagged %s %s (%s) in %s"),
439 printable_type(&tag
->tagged
->oid
, tag
->tagged
->type
),
440 describe_object(&tag
->tagged
->oid
),
442 describe_object(&tag
->object
.oid
));
447 if (obj
->type
== OBJ_TREE
)
448 free_tree_buffer((struct tree
*)obj
);
452 static int fsck_obj_buffer(const struct object_id
*oid
, enum object_type type
,
453 unsigned long size
, void *buffer
, int *eaten
)
456 * Note, buffer may be NULL if type is OBJ_BLOB. See
457 * verify_packfile(), data_valid variable for details.
460 obj
= parse_object_buffer(the_repository
, oid
, type
, size
, buffer
,
463 errors_found
|= ERROR_OBJECT
;
464 return error(_("%s: object corrupt or missing"),
467 obj
->flags
&= ~(REACHABLE
| SEEN
);
468 obj
->flags
|= HAS_OBJ
;
469 return fsck_obj(obj
, buffer
, size
);
472 static int default_refs
;
474 static void fsck_handle_reflog_oid(const char *refname
, struct object_id
*oid
,
475 timestamp_t timestamp
)
479 if (!is_null_oid(oid
)) {
480 obj
= lookup_object(the_repository
, oid
);
481 if (obj
&& (obj
->flags
& HAS_OBJ
)) {
483 fsck_put_object_name(&fsck_walk_options
, oid
,
487 mark_object_reachable(obj
);
488 } else if (!is_promisor_object(oid
)) {
489 error(_("%s: invalid reflog entry %s"),
490 refname
, oid_to_hex(oid
));
491 errors_found
|= ERROR_REACHABLE
;
496 static int fsck_handle_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
497 const char *email UNUSED
,
498 timestamp_t timestamp
, int tz UNUSED
,
499 const char *message UNUSED
, void *cb_data
)
501 const char *refname
= cb_data
;
504 fprintf_ln(stderr
, _("Checking reflog %s->%s"),
505 oid_to_hex(ooid
), oid_to_hex(noid
));
507 fsck_handle_reflog_oid(refname
, ooid
, 0);
508 fsck_handle_reflog_oid(refname
, noid
, timestamp
);
512 static int fsck_handle_reflog(const char *logname
,
513 const struct object_id
*oid UNUSED
,
514 int flag UNUSED
, void *cb_data
)
516 struct strbuf refname
= STRBUF_INIT
;
518 strbuf_worktree_ref(cb_data
, &refname
, logname
);
519 for_each_reflog_ent(refname
.buf
, fsck_handle_reflog_ent
, refname
.buf
);
520 strbuf_release(&refname
);
524 static int fsck_handle_ref(const char *refname
, const struct object_id
*oid
,
525 int flag UNUSED
, void *cb_data UNUSED
)
529 obj
= parse_object(the_repository
, oid
);
531 if (is_promisor_object(oid
)) {
533 * Increment default_refs anyway, because this is a
539 error(_("%s: invalid sha1 pointer %s"),
540 refname
, oid_to_hex(oid
));
541 errors_found
|= ERROR_REACHABLE
;
542 /* We'll continue with the rest despite the error.. */
545 if (obj
->type
!= OBJ_COMMIT
&& is_branch(refname
)) {
546 error(_("%s: not a commit"), refname
);
547 errors_found
|= ERROR_REFS
;
551 fsck_put_object_name(&fsck_walk_options
,
553 mark_object_reachable(obj
);
558 static int fsck_head_link(const char *head_ref_name
,
559 const char **head_points_at
,
560 struct object_id
*head_oid
);
562 static void get_default_heads(void)
564 struct worktree
**worktrees
, **p
;
565 const char *head_points_at
;
566 struct object_id head_oid
;
568 for_each_rawref(fsck_handle_ref
, NULL
);
570 worktrees
= get_worktrees();
571 for (p
= worktrees
; *p
; p
++) {
572 struct worktree
*wt
= *p
;
573 struct strbuf ref
= STRBUF_INIT
;
575 strbuf_worktree_ref(wt
, &ref
, "HEAD");
576 fsck_head_link(ref
.buf
, &head_points_at
, &head_oid
);
577 if (head_points_at
&& !is_null_oid(&head_oid
))
578 fsck_handle_ref(ref
.buf
, &head_oid
, 0, NULL
);
579 strbuf_release(&ref
);
582 refs_for_each_reflog(get_worktree_ref_store(wt
),
583 fsck_handle_reflog
, wt
);
585 free_worktrees(worktrees
);
588 * Not having any default heads isn't really fatal, but
589 * it does mean that "--unreachable" no longer makes any
590 * sense (since in this case everything will obviously
591 * be unreachable by definition.
593 * Showing dangling objects is valid, though (as those
594 * dangling objects are likely lost heads).
596 * So we just print a warning about it, and clear the
597 * "show_unreachable" flag.
600 fprintf_ln(stderr
, _("notice: No default references"));
601 show_unreachable
= 0;
605 struct for_each_loose_cb
607 struct progress
*progress
;
608 struct strbuf obj_type
;
611 static int fsck_loose(const struct object_id
*oid
, const char *path
, void *data
)
613 struct for_each_loose_cb
*cb_data
= data
;
615 enum object_type type
= OBJ_NONE
;
617 void *contents
= NULL
;
619 struct object_info oi
= OBJECT_INFO_INIT
;
620 struct object_id real_oid
= *null_oid();
623 strbuf_reset(&cb_data
->obj_type
);
624 oi
.type_name
= &cb_data
->obj_type
;
628 if (read_loose_object(path
, oid
, &real_oid
, &contents
, &oi
) < 0) {
629 if (contents
&& !oideq(&real_oid
, oid
))
630 err
= error(_("%s: hash-path mismatch, found at: %s"),
631 oid_to_hex(&real_oid
), path
);
633 err
= error(_("%s: object corrupt or missing: %s"),
634 oid_to_hex(oid
), path
);
636 if (type
!= OBJ_NONE
&& type
< 0)
637 err
= error(_("%s: object is of unknown type '%s': %s"),
638 oid_to_hex(&real_oid
), cb_data
->obj_type
.buf
,
641 errors_found
|= ERROR_OBJECT
;
643 return 0; /* keep checking other objects */
646 if (!contents
&& type
!= OBJ_BLOB
)
647 BUG("read_loose_object streamed a non-blob");
649 obj
= parse_object_buffer(the_repository
, oid
, type
, size
,
653 errors_found
|= ERROR_OBJECT
;
654 error(_("%s: object could not be parsed: %s"),
655 oid_to_hex(oid
), path
);
658 return 0; /* keep checking other objects */
661 obj
->flags
&= ~(REACHABLE
| SEEN
);
662 obj
->flags
|= HAS_OBJ
;
663 if (fsck_obj(obj
, contents
, size
))
664 errors_found
|= ERROR_OBJECT
;
668 return 0; /* keep checking other objects, even if we saw an error */
671 static int fsck_cruft(const char *basename
, const char *path
,
674 if (!starts_with(basename
, "tmp_obj_"))
675 fprintf_ln(stderr
, _("bad sha1 file: %s"), path
);
679 static int fsck_subdir(unsigned int nr
, const char *path UNUSED
, void *data
)
681 struct for_each_loose_cb
*cb_data
= data
;
682 struct progress
*progress
= cb_data
->progress
;
683 display_progress(progress
, nr
+ 1);
687 static void fsck_object_dir(const char *path
)
689 struct progress
*progress
= NULL
;
690 struct for_each_loose_cb cb_data
= {
691 .obj_type
= STRBUF_INIT
,
692 .progress
= progress
,
696 fprintf_ln(stderr
, _("Checking object directory"));
699 progress
= start_progress(_("Checking object directories"), 256);
701 for_each_loose_file_in_objdir(path
, fsck_loose
, fsck_cruft
, fsck_subdir
,
703 display_progress(progress
, 256);
704 stop_progress(&progress
);
705 strbuf_release(&cb_data
.obj_type
);
708 static int fsck_head_link(const char *head_ref_name
,
709 const char **head_points_at
,
710 struct object_id
*head_oid
)
712 int null_is_error
= 0;
715 fprintf_ln(stderr
, _("Checking %s link"), head_ref_name
);
717 *head_points_at
= resolve_ref_unsafe(head_ref_name
, 0, head_oid
, NULL
);
718 if (!*head_points_at
) {
719 errors_found
|= ERROR_REFS
;
720 return error(_("invalid %s"), head_ref_name
);
722 if (!strcmp(*head_points_at
, head_ref_name
))
725 else if (!starts_with(*head_points_at
, "refs/heads/")) {
726 errors_found
|= ERROR_REFS
;
727 return error(_("%s points to something strange (%s)"),
728 head_ref_name
, *head_points_at
);
730 if (is_null_oid(head_oid
)) {
732 errors_found
|= ERROR_REFS
;
733 return error(_("%s: detached HEAD points at nothing"),
737 _("notice: %s points to an unborn branch (%s)"),
738 head_ref_name
, *head_points_at
+ 11);
743 static int fsck_cache_tree(struct cache_tree
*it
, const char *index_path
)
749 fprintf_ln(stderr
, _("Checking cache tree of %s"), index_path
);
751 if (0 <= it
->entry_count
) {
752 struct object
*obj
= parse_object(the_repository
, &it
->oid
);
754 error(_("%s: invalid sha1 pointer in cache-tree of %s"),
755 oid_to_hex(&it
->oid
), index_path
);
756 errors_found
|= ERROR_REFS
;
760 fsck_put_object_name(&fsck_walk_options
, &it
->oid
, ":");
761 mark_object_reachable(obj
);
762 if (obj
->type
!= OBJ_TREE
)
763 err
|= objerror(obj
, _("non-tree in cache-tree"));
765 for (i
= 0; i
< it
->subtree_nr
; i
++)
766 err
|= fsck_cache_tree(it
->down
[i
]->cache_tree
, index_path
);
770 static int fsck_resolve_undo(struct index_state
*istate
,
771 const char *index_path
)
773 struct string_list_item
*item
;
774 struct string_list
*resolve_undo
= istate
->resolve_undo
;
779 for_each_string_list_item(item
, resolve_undo
) {
780 const char *path
= item
->string
;
781 struct resolve_undo_info
*ru
= item
->util
;
786 for (i
= 0; i
< 3; i
++) {
789 if (!ru
->mode
[i
] || !S_ISREG(ru
->mode
[i
]))
792 obj
= parse_object(the_repository
, &ru
->oid
[i
]);
794 error(_("%s: invalid sha1 pointer in resolve-undo of %s"),
795 oid_to_hex(&ru
->oid
[i
]),
797 errors_found
|= ERROR_REFS
;
801 fsck_put_object_name(&fsck_walk_options
, &ru
->oid
[i
],
802 ":(%d):%s", i
, path
);
803 mark_object_reachable(obj
);
809 static void fsck_index(struct index_state
*istate
, const char *index_path
,
810 int is_current_worktree
)
814 /* TODO: audit for interaction with sparse-index. */
815 ensure_full_index(istate
);
816 for (i
= 0; i
< istate
->cache_nr
; i
++) {
821 mode
= istate
->cache
[i
]->ce_mode
;
822 if (S_ISGITLINK(mode
))
824 blob
= lookup_blob(the_repository
,
825 &istate
->cache
[i
]->oid
);
830 fsck_put_object_name(&fsck_walk_options
, &obj
->oid
,
832 is_current_worktree
? "" : index_path
,
833 istate
->cache
[i
]->name
);
834 mark_object_reachable(obj
);
836 if (istate
->cache_tree
)
837 fsck_cache_tree(istate
->cache_tree
, index_path
);
838 fsck_resolve_undo(istate
, index_path
);
841 static void mark_object_for_connectivity(const struct object_id
*oid
)
843 struct object
*obj
= lookup_unknown_object(the_repository
, oid
);
844 obj
->flags
|= HAS_OBJ
;
847 static int mark_loose_for_connectivity(const struct object_id
*oid
,
848 const char *path UNUSED
,
851 mark_object_for_connectivity(oid
);
855 static int mark_packed_for_connectivity(const struct object_id
*oid
,
856 struct packed_git
*pack UNUSED
,
860 mark_object_for_connectivity(oid
);
864 static int check_pack_rev_indexes(struct repository
*r
, int show_progress
)
866 struct progress
*progress
= NULL
;
867 uint32_t pack_count
= 0;
871 for (struct packed_git
*p
= get_all_packs(r
); p
; p
= p
->next
)
873 progress
= start_delayed_progress("Verifying reverse pack-indexes", pack_count
);
877 for (struct packed_git
*p
= get_all_packs(r
); p
; p
= p
->next
) {
878 int load_error
= load_pack_revindex_from_disk(p
);
880 if (load_error
< 0) {
881 error(_("unable to load rev-index for pack '%s'"), p
->pack_name
);
882 res
= ERROR_PACK_REV_INDEX
;
883 } else if (!load_error
&&
884 !load_pack_revindex(r
, p
) &&
885 verify_pack_revindex(p
)) {
886 error(_("invalid rev-index for pack '%s'"), p
->pack_name
);
887 res
= ERROR_PACK_REV_INDEX
;
889 display_progress(progress
, ++pack_count
);
891 stop_progress(&progress
);
896 static char const * const fsck_usage
[] = {
897 N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n"
898 " [--[no-]full] [--strict] [--verbose] [--lost-found]\n"
899 " [--[no-]dangling] [--[no-]progress] [--connectivity-only]\n"
900 " [--[no-]name-objects] [<object>...]"),
904 static struct option fsck_opts
[] = {
905 OPT__VERBOSE(&verbose
, N_("be verbose")),
906 OPT_BOOL(0, "unreachable", &show_unreachable
, N_("show unreachable objects")),
907 OPT_BOOL(0, "dangling", &show_dangling
, N_("show dangling objects")),
908 OPT_BOOL(0, "tags", &show_tags
, N_("report tags")),
909 OPT_BOOL(0, "root", &show_root
, N_("report root nodes")),
910 OPT_BOOL(0, "cache", &keep_cache_objects
, N_("make index objects head nodes")),
911 OPT_BOOL(0, "reflogs", &include_reflogs
, N_("make reflogs head nodes (default)")),
912 OPT_BOOL(0, "full", &check_full
, N_("also consider packs and alternate objects")),
913 OPT_BOOL(0, "connectivity-only", &connectivity_only
, N_("check only connectivity")),
914 OPT_BOOL(0, "strict", &check_strict
, N_("enable more strict checking")),
915 OPT_BOOL(0, "lost-found", &write_lost_and_found
,
916 N_("write dangling objects in .git/lost-found")),
917 OPT_BOOL(0, "progress", &show_progress
, N_("show progress")),
918 OPT_BOOL(0, "name-objects", &name_objects
, N_("show verbose names for reachable objects")),
922 int cmd_fsck(int argc
, const char **argv
, const char *prefix
)
925 struct object_directory
*odb
;
927 /* fsck knows how to handle missing promisor objects */
928 fetch_if_missing
= 0;
931 disable_replace_refs();
932 save_commit_buffer
= 0;
934 argc
= parse_options(argc
, argv
, prefix
, fsck_opts
, fsck_usage
, 0);
936 fsck_walk_options
.walk
= mark_object
;
937 fsck_obj_options
.walk
= mark_used
;
938 fsck_obj_options
.error_func
= fsck_error_func
;
940 fsck_obj_options
.strict
= 1;
942 if (show_progress
== -1)
943 show_progress
= isatty(2);
947 if (write_lost_and_found
) {
953 fsck_enable_object_names(&fsck_walk_options
);
955 git_config(git_fsck_config
, &fsck_obj_options
);
956 prepare_repo_settings(the_repository
);
958 if (connectivity_only
) {
959 for_each_loose_object(mark_loose_for_connectivity
, NULL
, 0);
960 for_each_packed_object(mark_packed_for_connectivity
, NULL
, 0);
962 prepare_alt_odb(the_repository
);
963 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
)
964 fsck_object_dir(odb
->path
);
967 struct packed_git
*p
;
968 uint32_t total
= 0, count
= 0;
969 struct progress
*progress
= NULL
;
972 for (p
= get_all_packs(the_repository
); p
;
974 if (open_pack_index(p
))
976 total
+= p
->num_objects
;
979 progress
= start_progress(_("Checking objects"), total
);
981 for (p
= get_all_packs(the_repository
); p
;
983 /* verify gives error messages itself */
984 if (verify_pack(the_repository
,
987 errors_found
|= ERROR_PACK
;
988 count
+= p
->num_objects
;
990 stop_progress(&progress
);
993 if (fsck_finish(&fsck_obj_options
))
994 errors_found
|= ERROR_OBJECT
;
997 for (i
= 0; i
< argc
; i
++) {
998 const char *arg
= argv
[i
];
999 struct object_id oid
;
1000 if (!repo_get_oid(the_repository
, arg
, &oid
)) {
1001 struct object
*obj
= lookup_object(the_repository
,
1004 if (!obj
|| !(obj
->flags
& HAS_OBJ
)) {
1005 if (is_promisor_object(&oid
))
1007 error(_("%s: object missing"), oid_to_hex(&oid
));
1008 errors_found
|= ERROR_OBJECT
;
1013 fsck_put_object_name(&fsck_walk_options
, &oid
,
1015 mark_object_reachable(obj
);
1018 error(_("invalid parameter: expected sha1, got '%s'"), arg
);
1019 errors_found
|= ERROR_OBJECT
;
1023 * If we've not been given any explicit head information, do the
1024 * default ones from .git/refs. We also consider the index file
1025 * in this case (ie this implies --cache).
1028 get_default_heads();
1029 keep_cache_objects
= 1;
1032 if (keep_cache_objects
) {
1033 struct worktree
**worktrees
, **p
;
1035 verify_index_checksum
= 1;
1036 verify_ce_order
= 1;
1038 worktrees
= get_worktrees();
1039 for (p
= worktrees
; *p
; p
++) {
1040 struct worktree
*wt
= *p
;
1041 struct index_state istate
=
1042 INDEX_STATE_INIT(the_repository
);
1046 * Make a copy since the buffer is reusable
1047 * and may get overwritten by other calls
1048 * while we're examining the index.
1050 path
= xstrdup(worktree_git_path(wt
, "index"));
1051 read_index_from(&istate
, path
, get_worktree_git_dir(wt
));
1052 fsck_index(&istate
, path
, wt
->is_current
);
1053 discard_index(&istate
);
1056 free_worktrees(worktrees
);
1059 errors_found
|= check_pack_rev_indexes(the_repository
, show_progress
);
1060 if (verify_bitmap_files(the_repository
))
1061 errors_found
|= ERROR_BITMAP
;
1063 check_connectivity();
1065 if (the_repository
->settings
.core_commit_graph
) {
1066 struct child_process commit_graph_verify
= CHILD_PROCESS_INIT
;
1068 prepare_alt_odb(the_repository
);
1069 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
) {
1070 child_process_init(&commit_graph_verify
);
1071 commit_graph_verify
.git_cmd
= 1;
1072 strvec_pushl(&commit_graph_verify
.args
, "commit-graph",
1073 "verify", "--object-dir", odb
->path
, NULL
);
1075 strvec_push(&commit_graph_verify
.args
, "--progress");
1077 strvec_push(&commit_graph_verify
.args
, "--no-progress");
1078 if (run_command(&commit_graph_verify
))
1079 errors_found
|= ERROR_COMMIT_GRAPH
;
1083 if (the_repository
->settings
.core_multi_pack_index
) {
1084 struct child_process midx_verify
= CHILD_PROCESS_INIT
;
1086 prepare_alt_odb(the_repository
);
1087 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
) {
1088 child_process_init(&midx_verify
);
1089 midx_verify
.git_cmd
= 1;
1090 strvec_pushl(&midx_verify
.args
, "multi-pack-index",
1091 "verify", "--object-dir", odb
->path
, NULL
);
1093 strvec_push(&midx_verify
.args
, "--progress");
1095 strvec_push(&midx_verify
.args
, "--no-progress");
1096 if (run_command(&midx_verify
))
1097 errors_found
|= ERROR_MULTI_PACK_INDEX
;
1101 return errors_found
;