1 #define USE_THE_REPOSITORY_VARIABLE
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.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 static int check_references
= 1;
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_objects_error_func(struct fsck_options
*o UNUSED
,
95 enum fsck_msg_type msg_type
,
96 enum fsck_msg_id msg_id UNUSED
,
99 struct fsck_object_report
*report
= fsck_report
;
100 const struct object_id
*oid
= report
->oid
;
101 enum object_type object_type
= report
->object_type
;
105 /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
106 fprintf_ln(stderr
, _("warning in %s %s: %s"),
107 printable_type(oid
, object_type
),
108 describe_object(oid
), message
);
111 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
112 fprintf_ln(stderr
, _("error in %s %s: %s"),
113 printable_type(oid
, object_type
),
114 describe_object(oid
), message
);
117 BUG("%d (FSCK_IGNORE?) should never trigger this callback",
122 static struct object_array pending
;
124 static int mark_object(struct object
*obj
, enum object_type type
,
125 void *data
, struct fsck_options
*options UNUSED
)
127 struct object
*parent
= data
;
130 * The only case data is NULL or type is OBJ_ANY is when
131 * mark_object_reachable() calls us. All the callers of
132 * that function has non-NULL obj hence ...
135 /* ... these references to parent->fld are safe here */
136 printf_ln(_("broken link from %7s %s"),
137 printable_type(&parent
->oid
, parent
->type
),
138 describe_object(&parent
->oid
));
139 printf_ln(_("broken link from %7s %s"),
140 (type
== OBJ_ANY
? _("unknown") : type_name(type
)),
142 errors_found
|= ERROR_REACHABLE
;
146 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
147 /* ... and the reference to parent is safe here */
148 objerror(parent
, _("wrong object type in link"));
150 if (obj
->flags
& REACHABLE
)
152 obj
->flags
|= REACHABLE
;
154 if (is_promisor_object(the_repository
, &obj
->oid
))
156 * Further recursion does not need to be performed on this
157 * object since it is a promisor object (so it does not need to
158 * be added to "pending").
162 if (!(obj
->flags
& HAS_OBJ
)) {
163 if (parent
&& !has_object(the_repository
, &obj
->oid
, 1)) {
164 printf_ln(_("broken link from %7s %s\n"
166 printable_type(&parent
->oid
, parent
->type
),
167 describe_object(&parent
->oid
),
168 printable_type(&obj
->oid
, obj
->type
),
169 describe_object(&obj
->oid
));
170 errors_found
|= ERROR_REACHABLE
;
175 add_object_array(obj
, NULL
, &pending
);
179 static void mark_object_reachable(struct object
*obj
)
181 mark_object(obj
, OBJ_ANY
, NULL
, NULL
);
184 static int traverse_one_object(struct object
*obj
)
186 int result
= fsck_walk(obj
, obj
, &fsck_walk_options
);
188 if (obj
->type
== OBJ_TREE
) {
189 struct tree
*tree
= (struct tree
*)obj
;
190 free_tree_buffer(tree
);
195 static int traverse_reachable(void)
197 struct progress
*progress
= NULL
;
201 progress
= start_delayed_progress(the_repository
,
202 _("Checking connectivity"), 0);
204 result
|= traverse_one_object(object_array_pop(&pending
));
205 display_progress(progress
, ++nr
);
207 stop_progress(&progress
);
211 static int mark_used(struct object
*obj
, enum object_type type UNUSED
,
212 void *data UNUSED
, struct fsck_options
*options UNUSED
)
220 static void mark_unreachable_referents(const struct object_id
*oid
)
222 struct fsck_options options
= FSCK_OPTIONS_DEFAULT
;
223 struct object
*obj
= lookup_object(the_repository
, oid
);
225 if (!obj
|| !(obj
->flags
& HAS_OBJ
))
226 return; /* not part of our original set */
227 if (obj
->flags
& REACHABLE
)
228 return; /* reachable objects already traversed */
231 * Avoid passing OBJ_NONE to fsck_walk, which will parse the object
232 * (and we want to avoid parsing blobs).
234 if (obj
->type
== OBJ_NONE
) {
235 enum object_type type
= oid_object_info(the_repository
,
238 object_as_type(obj
, type
, 0);
241 options
.walk
= mark_used
;
242 fsck_walk(obj
, NULL
, &options
);
243 if (obj
->type
== OBJ_TREE
)
244 free_tree_buffer((struct tree
*)obj
);
247 static int mark_loose_unreachable_referents(const struct object_id
*oid
,
248 const char *path UNUSED
,
251 mark_unreachable_referents(oid
);
255 static int mark_packed_unreachable_referents(const struct object_id
*oid
,
256 struct packed_git
*pack UNUSED
,
260 mark_unreachable_referents(oid
);
265 * Check a single reachable object
267 static void check_reachable_object(struct object
*obj
)
270 * We obviously want the object to be parsed,
271 * except if it was in a pack-file and we didn't
274 if (!(obj
->flags
& HAS_OBJ
)) {
275 if (is_promisor_object(the_repository
, &obj
->oid
))
277 if (has_object_pack(the_repository
, &obj
->oid
))
278 return; /* it is in pack - forget about it */
279 printf_ln(_("missing %s %s"),
280 printable_type(&obj
->oid
, obj
->type
),
281 describe_object(&obj
->oid
));
282 errors_found
|= ERROR_REACHABLE
;
288 * Check a single unreachable object
290 static void check_unreachable_object(struct object
*obj
)
293 * Missing unreachable object? Ignore it. It's not like
294 * we miss it (since it can't be reached), nor do we want
295 * to complain about it being unreachable (since it does
298 if (!(obj
->flags
& HAS_OBJ
))
302 * Unreachable object that exists? Show it if asked to,
303 * since this is something that is prunable.
305 if (show_unreachable
) {
306 printf_ln(_("unreachable %s %s"),
307 printable_type(&obj
->oid
, obj
->type
),
308 describe_object(&obj
->oid
));
313 * "!USED" means that nothing at all points to it, including
314 * other unreachable objects. In other words, it's the "tip"
315 * of some set of unreachable objects, usually a commit that
318 * Such starting points are more interesting than some random
319 * set of unreachable objects, so we show them even if the user
320 * hasn't asked for _all_ unreachable objects. If you have
321 * deleted a branch by mistake, this is a prime candidate to
322 * start looking at, for example.
324 if (!(obj
->flags
& USED
)) {
326 printf_ln(_("dangling %s %s"),
327 printable_type(&obj
->oid
, obj
->type
),
328 describe_object(&obj
->oid
));
329 if (write_lost_and_found
) {
330 char *filename
= repo_git_path(the_repository
, "lost-found/%s/%s",
331 obj
->type
== OBJ_COMMIT
? "commit" : "other",
332 describe_object(&obj
->oid
));
335 if (safe_create_leading_directories_const(the_repository
, filename
)) {
336 error(_("could not create lost-found"));
340 f
= xfopen(filename
, "w");
341 if (obj
->type
== OBJ_BLOB
) {
342 if (stream_blob_to_fd(fileno(f
), &obj
->oid
, NULL
, 1))
343 die_errno(_("could not write '%s'"), filename
);
345 fprintf(f
, "%s\n", describe_object(&obj
->oid
));
347 die_errno(_("could not finish '%s'"),
355 * Otherwise? It's there, it's unreachable, and some other unreachable
356 * object points to it. Ignore it - it's not interesting, and we showed
357 * all the interesting cases above.
361 static void check_object(struct object
*obj
)
364 fprintf_ln(stderr
, _("Checking %s"), describe_object(&obj
->oid
));
366 if (obj
->flags
& REACHABLE
)
367 check_reachable_object(obj
);
369 check_unreachable_object(obj
);
372 static void check_connectivity(void)
376 /* Traverse the pending reachable objects */
377 traverse_reachable();
380 * With --connectivity-only, we won't have actually opened and marked
381 * unreachable objects with USED. Do that now to make --dangling, etc
384 if (connectivity_only
&& (show_dangling
|| write_lost_and_found
)) {
386 * Even though we already have a "struct object" for each of
387 * these in memory, we must not iterate over the internal
388 * object hash as we do below. Our loop would potentially
389 * resize the hash, making our iteration invalid.
391 * Instead, we'll just go back to the source list of objects,
392 * and ignore any that weren't present in our earlier
395 for_each_loose_object(mark_loose_unreachable_referents
, NULL
, 0);
396 for_each_packed_object(the_repository
,
397 mark_packed_unreachable_referents
,
402 /* Look up all the requirements, warn about missing objects.. */
403 max
= get_max_object_index(the_repository
);
405 fprintf_ln(stderr
, _("Checking connectivity (%d objects)"), max
);
407 for (i
= 0; i
< max
; i
++) {
408 struct object
*obj
= get_indexed_object(the_repository
, i
);
415 static int fsck_obj(struct object
*obj
, void *buffer
, unsigned long size
)
419 if (obj
->flags
& SEEN
)
424 fprintf_ln(stderr
, _("Checking %s %s"),
425 printable_type(&obj
->oid
, obj
->type
),
426 describe_object(&obj
->oid
));
428 if (fsck_walk(obj
, NULL
, &fsck_obj_options
))
429 objerror(obj
, _("broken links"));
430 err
= fsck_object(obj
, buffer
, size
, &fsck_obj_options
);
434 if (obj
->type
== OBJ_COMMIT
) {
435 struct commit
*commit
= (struct commit
*) obj
;
437 if (!commit
->parents
&& show_root
)
438 printf_ln(_("root %s"),
439 describe_object(&commit
->object
.oid
));
442 if (obj
->type
== OBJ_TAG
) {
443 struct tag
*tag
= (struct tag
*) obj
;
445 if (show_tags
&& tag
->tagged
) {
446 printf_ln(_("tagged %s %s (%s) in %s"),
447 printable_type(&tag
->tagged
->oid
, tag
->tagged
->type
),
448 describe_object(&tag
->tagged
->oid
),
450 describe_object(&tag
->object
.oid
));
455 if (obj
->type
== OBJ_TREE
)
456 free_tree_buffer((struct tree
*)obj
);
460 static int fsck_obj_buffer(const struct object_id
*oid
, enum object_type type
,
461 unsigned long size
, void *buffer
, int *eaten
)
464 * Note, buffer may be NULL if type is OBJ_BLOB. See
465 * verify_packfile(), data_valid variable for details.
468 obj
= parse_object_buffer(the_repository
, oid
, type
, size
, buffer
,
471 errors_found
|= ERROR_OBJECT
;
472 return error(_("%s: object corrupt or missing"),
475 obj
->flags
&= ~(REACHABLE
| SEEN
);
476 obj
->flags
|= HAS_OBJ
;
477 return fsck_obj(obj
, buffer
, size
);
480 static int default_refs
;
482 static void fsck_handle_reflog_oid(const char *refname
, struct object_id
*oid
,
483 timestamp_t timestamp
)
487 if (!is_null_oid(oid
)) {
488 obj
= lookup_object(the_repository
, oid
);
489 if (obj
&& (obj
->flags
& HAS_OBJ
)) {
491 fsck_put_object_name(&fsck_walk_options
, oid
,
495 mark_object_reachable(obj
);
496 } else if (!is_promisor_object(the_repository
, oid
)) {
497 error(_("%s: invalid reflog entry %s"),
498 refname
, oid_to_hex(oid
));
499 errors_found
|= ERROR_REACHABLE
;
504 static int fsck_handle_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
505 const char *email UNUSED
,
506 timestamp_t timestamp
, int tz UNUSED
,
507 const char *message UNUSED
, void *cb_data
)
509 const char *refname
= cb_data
;
512 fprintf_ln(stderr
, _("Checking reflog %s->%s"),
513 oid_to_hex(ooid
), oid_to_hex(noid
));
515 fsck_handle_reflog_oid(refname
, ooid
, 0);
516 fsck_handle_reflog_oid(refname
, noid
, timestamp
);
520 static int fsck_handle_reflog(const char *logname
, void *cb_data
)
522 struct strbuf refname
= STRBUF_INIT
;
524 strbuf_worktree_ref(cb_data
, &refname
, logname
);
525 refs_for_each_reflog_ent(get_main_ref_store(the_repository
),
526 refname
.buf
, fsck_handle_reflog_ent
,
528 strbuf_release(&refname
);
532 static int fsck_handle_ref(const char *refname
, const char *referent UNUSED
, const struct object_id
*oid
,
533 int flag UNUSED
, void *cb_data UNUSED
)
537 obj
= parse_object(the_repository
, oid
);
539 if (is_promisor_object(the_repository
, oid
)) {
541 * Increment default_refs anyway, because this is a
547 error(_("%s: invalid sha1 pointer %s"),
548 refname
, oid_to_hex(oid
));
549 errors_found
|= ERROR_REACHABLE
;
550 /* We'll continue with the rest despite the error.. */
553 if (obj
->type
!= OBJ_COMMIT
&& is_branch(refname
)) {
554 error(_("%s: not a commit"), refname
);
555 errors_found
|= ERROR_REFS
;
559 fsck_put_object_name(&fsck_walk_options
,
561 mark_object_reachable(obj
);
566 static int fsck_head_link(const char *head_ref_name
,
567 const char **head_points_at
,
568 struct object_id
*head_oid
);
570 static void get_default_heads(void)
572 struct worktree
**worktrees
, **p
;
573 const char *head_points_at
;
574 struct object_id head_oid
;
576 refs_for_each_rawref(get_main_ref_store(the_repository
),
577 fsck_handle_ref
, NULL
);
579 worktrees
= get_worktrees();
580 for (p
= worktrees
; *p
; p
++) {
581 struct worktree
*wt
= *p
;
582 struct strbuf ref
= STRBUF_INIT
;
584 strbuf_worktree_ref(wt
, &ref
, "HEAD");
585 fsck_head_link(ref
.buf
, &head_points_at
, &head_oid
);
586 if (head_points_at
&& !is_null_oid(&head_oid
))
587 fsck_handle_ref(ref
.buf
, NULL
, &head_oid
, 0, NULL
);
588 strbuf_release(&ref
);
591 refs_for_each_reflog(get_worktree_ref_store(wt
),
592 fsck_handle_reflog
, wt
);
594 free_worktrees(worktrees
);
597 * Not having any default heads isn't really fatal, but
598 * it does mean that "--unreachable" no longer makes any
599 * sense (since in this case everything will obviously
600 * be unreachable by definition.
602 * Showing dangling objects is valid, though (as those
603 * dangling objects are likely lost heads).
605 * So we just print a warning about it, and clear the
606 * "show_unreachable" flag.
609 fprintf_ln(stderr
, _("notice: No default references"));
610 show_unreachable
= 0;
614 struct for_each_loose_cb
616 struct progress
*progress
;
619 static int fsck_loose(const struct object_id
*oid
, const char *path
,
623 enum object_type type
= OBJ_NONE
;
625 void *contents
= NULL
;
627 struct object_info oi
= OBJECT_INFO_INIT
;
628 struct object_id real_oid
= *null_oid(the_hash_algo
);
634 if (read_loose_object(path
, oid
, &real_oid
, &contents
, &oi
) < 0) {
635 if (contents
&& !oideq(&real_oid
, oid
))
636 err
= error(_("%s: hash-path mismatch, found at: %s"),
637 oid_to_hex(&real_oid
), path
);
639 err
= error(_("%s: object corrupt or missing: %s"),
640 oid_to_hex(oid
), path
);
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 .progress
= progress
,
697 fprintf_ln(stderr
, _("Checking object directory"));
700 progress
= start_progress(the_repository
,
701 _("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
);
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
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
719 head_ref_name
, 0, head_oid
,
721 if (!*head_points_at
) {
722 errors_found
|= ERROR_REFS
;
723 return error(_("invalid %s"), head_ref_name
);
725 if (!strcmp(*head_points_at
, head_ref_name
))
728 else if (!starts_with(*head_points_at
, "refs/heads/")) {
729 errors_found
|= ERROR_REFS
;
730 return error(_("%s points to something strange (%s)"),
731 head_ref_name
, *head_points_at
);
733 if (is_null_oid(head_oid
)) {
735 errors_found
|= ERROR_REFS
;
736 return error(_("%s: detached HEAD points at nothing"),
740 _("notice: %s points to an unborn branch (%s)"),
741 head_ref_name
, *head_points_at
+ 11);
746 static int fsck_cache_tree(struct cache_tree
*it
, const char *index_path
)
752 fprintf_ln(stderr
, _("Checking cache tree of %s"), index_path
);
754 if (0 <= it
->entry_count
) {
755 struct object
*obj
= parse_object(the_repository
, &it
->oid
);
757 error(_("%s: invalid sha1 pointer in cache-tree of %s"),
758 oid_to_hex(&it
->oid
), index_path
);
759 errors_found
|= ERROR_REFS
;
763 fsck_put_object_name(&fsck_walk_options
, &it
->oid
, ":");
764 mark_object_reachable(obj
);
765 if (obj
->type
!= OBJ_TREE
)
766 err
|= objerror(obj
, _("non-tree in cache-tree"));
768 for (i
= 0; i
< it
->subtree_nr
; i
++)
769 err
|= fsck_cache_tree(it
->down
[i
]->cache_tree
, index_path
);
773 static int fsck_resolve_undo(struct index_state
*istate
,
774 const char *index_path
)
776 struct string_list_item
*item
;
777 struct string_list
*resolve_undo
= istate
->resolve_undo
;
782 for_each_string_list_item(item
, resolve_undo
) {
783 const char *path
= item
->string
;
784 struct resolve_undo_info
*ru
= item
->util
;
789 for (i
= 0; i
< 3; i
++) {
792 if (!ru
->mode
[i
] || !S_ISREG(ru
->mode
[i
]))
795 obj
= parse_object(the_repository
, &ru
->oid
[i
]);
797 error(_("%s: invalid sha1 pointer in resolve-undo of %s"),
798 oid_to_hex(&ru
->oid
[i
]),
800 errors_found
|= ERROR_REFS
;
804 fsck_put_object_name(&fsck_walk_options
, &ru
->oid
[i
],
805 ":(%d):%s", i
, path
);
806 mark_object_reachable(obj
);
812 static void fsck_index(struct index_state
*istate
, const char *index_path
,
813 int is_current_worktree
)
817 /* TODO: audit for interaction with sparse-index. */
818 ensure_full_index(istate
);
819 for (i
= 0; i
< istate
->cache_nr
; i
++) {
824 mode
= istate
->cache
[i
]->ce_mode
;
825 if (S_ISGITLINK(mode
))
827 blob
= lookup_blob(the_repository
,
828 &istate
->cache
[i
]->oid
);
833 fsck_put_object_name(&fsck_walk_options
, &obj
->oid
,
835 is_current_worktree
? "" : index_path
,
836 istate
->cache
[i
]->name
);
837 mark_object_reachable(obj
);
839 if (istate
->cache_tree
)
840 fsck_cache_tree(istate
->cache_tree
, index_path
);
841 fsck_resolve_undo(istate
, index_path
);
844 static void mark_object_for_connectivity(const struct object_id
*oid
)
846 struct object
*obj
= lookup_unknown_object(the_repository
, oid
);
847 obj
->flags
|= HAS_OBJ
;
850 static int mark_loose_for_connectivity(const struct object_id
*oid
,
851 const char *path UNUSED
,
854 mark_object_for_connectivity(oid
);
858 static int mark_packed_for_connectivity(const struct object_id
*oid
,
859 struct packed_git
*pack UNUSED
,
863 mark_object_for_connectivity(oid
);
867 static int check_pack_rev_indexes(struct repository
*r
, int show_progress
)
869 struct progress
*progress
= NULL
;
870 uint32_t pack_count
= 0;
874 for (struct packed_git
*p
= get_all_packs(r
); p
; p
= p
->next
)
876 progress
= start_delayed_progress(the_repository
,
877 "Verifying reverse pack-indexes", pack_count
);
881 for (struct packed_git
*p
= get_all_packs(r
); p
; p
= p
->next
) {
882 int load_error
= load_pack_revindex_from_disk(p
);
884 if (load_error
< 0) {
885 error(_("unable to load rev-index for pack '%s'"), p
->pack_name
);
886 res
= ERROR_PACK_REV_INDEX
;
887 } else if (!load_error
&&
888 !load_pack_revindex(r
, p
) &&
889 verify_pack_revindex(p
)) {
890 error(_("invalid rev-index for pack '%s'"), p
->pack_name
);
891 res
= ERROR_PACK_REV_INDEX
;
893 display_progress(progress
, ++pack_count
);
895 stop_progress(&progress
);
900 static void fsck_refs(struct repository
*r
)
902 struct child_process refs_verify
= CHILD_PROCESS_INIT
;
903 struct progress
*progress
= NULL
;
906 progress
= start_progress(r
, _("Checking ref database"), 1);
909 fprintf_ln(stderr
, _("Checking ref database"));
911 child_process_init(&refs_verify
);
912 refs_verify
.git_cmd
= 1;
913 strvec_pushl(&refs_verify
.args
, "refs", "verify", NULL
);
915 strvec_push(&refs_verify
.args
, "--verbose");
917 strvec_push(&refs_verify
.args
, "--strict");
919 if (run_command(&refs_verify
))
920 errors_found
|= ERROR_REFS
;
922 display_progress(progress
, 1);
923 stop_progress(&progress
);
926 static char const * const fsck_usage
[] = {
927 N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n"
928 " [--[no-]full] [--strict] [--verbose] [--lost-found]\n"
929 " [--[no-]dangling] [--[no-]progress] [--connectivity-only]\n"
930 " [--[no-]name-objects] [--[no-]references] [<object>...]"),
934 static struct option fsck_opts
[] = {
935 OPT__VERBOSE(&verbose
, N_("be verbose")),
936 OPT_BOOL(0, "unreachable", &show_unreachable
, N_("show unreachable objects")),
937 OPT_BOOL(0, "dangling", &show_dangling
, N_("show dangling objects")),
938 OPT_BOOL(0, "tags", &show_tags
, N_("report tags")),
939 OPT_BOOL(0, "root", &show_root
, N_("report root nodes")),
940 OPT_BOOL(0, "cache", &keep_cache_objects
, N_("make index objects head nodes")),
941 OPT_BOOL(0, "reflogs", &include_reflogs
, N_("make reflogs head nodes (default)")),
942 OPT_BOOL(0, "full", &check_full
, N_("also consider packs and alternate objects")),
943 OPT_BOOL(0, "connectivity-only", &connectivity_only
, N_("check only connectivity")),
944 OPT_BOOL(0, "strict", &check_strict
, N_("enable more strict checking")),
945 OPT_BOOL(0, "lost-found", &write_lost_and_found
,
946 N_("write dangling objects in .git/lost-found")),
947 OPT_BOOL(0, "progress", &show_progress
, N_("show progress")),
948 OPT_BOOL(0, "name-objects", &name_objects
, N_("show verbose names for reachable objects")),
949 OPT_BOOL(0, "references", &check_references
, N_("check reference database consistency")),
953 int cmd_fsck(int argc
,
956 struct repository
*repo UNUSED
)
959 struct object_directory
*odb
;
961 /* fsck knows how to handle missing promisor objects */
962 fetch_if_missing
= 0;
965 disable_replace_refs();
966 save_commit_buffer
= 0;
968 argc
= parse_options(argc
, argv
, prefix
, fsck_opts
, fsck_usage
, 0);
970 fsck_walk_options
.walk
= mark_object
;
971 fsck_obj_options
.walk
= mark_used
;
972 fsck_obj_options
.error_func
= fsck_objects_error_func
;
974 fsck_obj_options
.strict
= 1;
976 if (show_progress
== -1)
977 show_progress
= isatty(2);
981 if (write_lost_and_found
) {
987 fsck_enable_object_names(&fsck_walk_options
);
989 git_config(git_fsck_config
, &fsck_obj_options
);
990 prepare_repo_settings(the_repository
);
992 if (check_references
)
993 fsck_refs(the_repository
);
995 if (connectivity_only
) {
996 for_each_loose_object(mark_loose_for_connectivity
, NULL
, 0);
997 for_each_packed_object(the_repository
,
998 mark_packed_for_connectivity
, NULL
, 0);
1000 prepare_alt_odb(the_repository
);
1001 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
)
1002 fsck_object_dir(odb
->path
);
1005 struct packed_git
*p
;
1006 uint32_t total
= 0, count
= 0;
1007 struct progress
*progress
= NULL
;
1009 if (show_progress
) {
1010 for (p
= get_all_packs(the_repository
); p
;
1012 if (open_pack_index(p
))
1014 total
+= p
->num_objects
;
1017 progress
= start_progress(the_repository
,
1018 _("Checking objects"), total
);
1020 for (p
= get_all_packs(the_repository
); p
;
1022 /* verify gives error messages itself */
1023 if (verify_pack(the_repository
,
1026 errors_found
|= ERROR_PACK
;
1027 count
+= p
->num_objects
;
1029 stop_progress(&progress
);
1032 if (fsck_finish(&fsck_obj_options
))
1033 errors_found
|= ERROR_OBJECT
;
1036 for (i
= 0; i
< argc
; i
++) {
1037 const char *arg
= argv
[i
];
1038 struct object_id oid
;
1039 if (!repo_get_oid(the_repository
, arg
, &oid
)) {
1040 struct object
*obj
= lookup_object(the_repository
,
1043 if (!obj
|| !(obj
->flags
& HAS_OBJ
)) {
1044 if (is_promisor_object(the_repository
, &oid
))
1046 error(_("%s: object missing"), oid_to_hex(&oid
));
1047 errors_found
|= ERROR_OBJECT
;
1052 fsck_put_object_name(&fsck_walk_options
, &oid
,
1054 mark_object_reachable(obj
);
1057 error(_("invalid parameter: expected sha1, got '%s'"), arg
);
1058 errors_found
|= ERROR_OBJECT
;
1062 * If we've not been given any explicit head information, do the
1063 * default ones from .git/refs. We also consider the index file
1064 * in this case (ie this implies --cache).
1067 get_default_heads();
1068 keep_cache_objects
= 1;
1071 if (keep_cache_objects
) {
1072 struct worktree
**worktrees
, **p
;
1074 verify_index_checksum
= 1;
1075 verify_ce_order
= 1;
1077 worktrees
= get_worktrees();
1078 for (p
= worktrees
; *p
; p
++) {
1079 struct worktree
*wt
= *p
;
1080 struct index_state istate
=
1081 INDEX_STATE_INIT(the_repository
);
1082 char *path
, *wt_gitdir
;
1085 * Make a copy since the buffer is reusable
1086 * and may get overwritten by other calls
1087 * while we're examining the index.
1089 path
= xstrdup(worktree_git_path(the_repository
, wt
, "index"));
1090 wt_gitdir
= get_worktree_git_dir(wt
);
1092 read_index_from(&istate
, path
, wt_gitdir
);
1093 fsck_index(&istate
, path
, wt
->is_current
);
1095 discard_index(&istate
);
1099 free_worktrees(worktrees
);
1102 errors_found
|= check_pack_rev_indexes(the_repository
, show_progress
);
1103 if (verify_bitmap_files(the_repository
))
1104 errors_found
|= ERROR_BITMAP
;
1106 check_connectivity();
1108 if (the_repository
->settings
.core_commit_graph
) {
1109 struct child_process commit_graph_verify
= CHILD_PROCESS_INIT
;
1111 prepare_alt_odb(the_repository
);
1112 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
) {
1113 child_process_init(&commit_graph_verify
);
1114 commit_graph_verify
.git_cmd
= 1;
1115 strvec_pushl(&commit_graph_verify
.args
, "commit-graph",
1116 "verify", "--object-dir", odb
->path
, NULL
);
1118 strvec_push(&commit_graph_verify
.args
, "--progress");
1120 strvec_push(&commit_graph_verify
.args
, "--no-progress");
1121 if (run_command(&commit_graph_verify
))
1122 errors_found
|= ERROR_COMMIT_GRAPH
;
1126 if (the_repository
->settings
.core_multi_pack_index
) {
1127 struct child_process midx_verify
= CHILD_PROCESS_INIT
;
1129 prepare_alt_odb(the_repository
);
1130 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
) {
1131 child_process_init(&midx_verify
);
1132 midx_verify
.git_cmd
= 1;
1133 strvec_pushl(&midx_verify
.args
, "multi-pack-index",
1134 "verify", "--object-dir", odb
->path
, NULL
);
1136 strvec_push(&midx_verify
.args
, "--progress");
1138 strvec_push(&midx_verify
.args
, "--no-progress");
1139 if (run_command(&midx_verify
))
1140 errors_found
|= ERROR_MULTI_PACK_INDEX
;
1144 return errors_found
;