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"
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
= odb_read_object_info(the_repository
->objects
,
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_objects_error_func(struct fsck_options
*o UNUSED
,
96 enum fsck_msg_type msg_type
,
97 enum fsck_msg_id msg_id UNUSED
,
100 struct fsck_object_report
*report
= fsck_report
;
101 const struct object_id
*oid
= report
->oid
;
102 enum object_type object_type
= report
->object_type
;
106 /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
107 fprintf_ln(stderr
, _("warning in %s %s: %s"),
108 printable_type(oid
, object_type
),
109 describe_object(oid
), message
);
112 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
113 fprintf_ln(stderr
, _("error in %s %s: %s"),
114 printable_type(oid
, object_type
),
115 describe_object(oid
), message
);
118 BUG("%d (FSCK_IGNORE?) should never trigger this callback",
123 static struct object_array pending
;
125 static int mark_object(struct object
*obj
, enum object_type type
,
126 void *data
, struct fsck_options
*options UNUSED
)
128 struct object
*parent
= data
;
131 * The only case data is NULL or type is OBJ_ANY is when
132 * mark_object_reachable() calls us. All the callers of
133 * that function has non-NULL obj hence ...
136 /* ... these references to parent->fld are safe here */
137 printf_ln(_("broken link from %7s %s"),
138 printable_type(&parent
->oid
, parent
->type
),
139 describe_object(&parent
->oid
));
140 printf_ln(_("broken link from %7s %s"),
141 (type
== OBJ_ANY
? _("unknown") : type_name(type
)),
143 errors_found
|= ERROR_REACHABLE
;
147 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
148 /* ... and the reference to parent is safe here */
149 objerror(parent
, _("wrong object type in link"));
151 if (obj
->flags
& REACHABLE
)
153 obj
->flags
|= REACHABLE
;
155 if (is_promisor_object(the_repository
, &obj
->oid
))
157 * Further recursion does not need to be performed on this
158 * object since it is a promisor object (so it does not need to
159 * be added to "pending").
163 if (!(obj
->flags
& HAS_OBJ
)) {
164 if (parent
&& !odb_has_object(the_repository
->objects
, &obj
->oid
, 1)) {
165 printf_ln(_("broken link from %7s %s\n"
167 printable_type(&parent
->oid
, parent
->type
),
168 describe_object(&parent
->oid
),
169 printable_type(&obj
->oid
, obj
->type
),
170 describe_object(&obj
->oid
));
171 errors_found
|= ERROR_REACHABLE
;
176 add_object_array(obj
, NULL
, &pending
);
180 static void mark_object_reachable(struct object
*obj
)
182 mark_object(obj
, OBJ_ANY
, NULL
, NULL
);
185 static int traverse_one_object(struct object
*obj
)
187 int result
= fsck_walk(obj
, obj
, &fsck_walk_options
);
189 if (obj
->type
== OBJ_TREE
) {
190 struct tree
*tree
= (struct tree
*)obj
;
191 free_tree_buffer(tree
);
196 static int traverse_reachable(void)
198 struct progress
*progress
= NULL
;
202 progress
= start_delayed_progress(the_repository
,
203 _("Checking connectivity"), 0);
205 result
|= traverse_one_object(object_array_pop(&pending
));
206 display_progress(progress
, ++nr
);
208 stop_progress(&progress
);
212 static int mark_used(struct object
*obj
, enum object_type type UNUSED
,
213 void *data UNUSED
, struct fsck_options
*options UNUSED
)
221 static void mark_unreachable_referents(const struct object_id
*oid
)
223 struct fsck_options options
= FSCK_OPTIONS_DEFAULT
;
224 struct object
*obj
= lookup_object(the_repository
, oid
);
226 if (!obj
|| !(obj
->flags
& HAS_OBJ
))
227 return; /* not part of our original set */
228 if (obj
->flags
& REACHABLE
)
229 return; /* reachable objects already traversed */
232 * Avoid passing OBJ_NONE to fsck_walk, which will parse the object
233 * (and we want to avoid parsing blobs).
235 if (obj
->type
== OBJ_NONE
) {
236 enum object_type type
= odb_read_object_info(the_repository
->objects
,
239 object_as_type(obj
, type
, 0);
242 options
.walk
= mark_used
;
243 fsck_walk(obj
, NULL
, &options
);
244 if (obj
->type
== OBJ_TREE
)
245 free_tree_buffer((struct tree
*)obj
);
248 static int mark_loose_unreachable_referents(const struct object_id
*oid
,
249 const char *path UNUSED
,
252 mark_unreachable_referents(oid
);
256 static int mark_packed_unreachable_referents(const struct object_id
*oid
,
257 struct packed_git
*pack UNUSED
,
261 mark_unreachable_referents(oid
);
266 * Check a single reachable object
268 static void check_reachable_object(struct object
*obj
)
271 * We obviously want the object to be parsed,
272 * except if it was in a pack-file and we didn't
275 if (!(obj
->flags
& HAS_OBJ
)) {
276 if (is_promisor_object(the_repository
, &obj
->oid
))
278 if (has_object_pack(the_repository
, &obj
->oid
))
279 return; /* it is in pack - forget about it */
280 printf_ln(_("missing %s %s"),
281 printable_type(&obj
->oid
, obj
->type
),
282 describe_object(&obj
->oid
));
283 errors_found
|= ERROR_REACHABLE
;
289 * Check a single unreachable object
291 static void check_unreachable_object(struct object
*obj
)
294 * Missing unreachable object? Ignore it. It's not like
295 * we miss it (since it can't be reached), nor do we want
296 * to complain about it being unreachable (since it does
299 if (!(obj
->flags
& HAS_OBJ
))
303 * Unreachable object that exists? Show it if asked to,
304 * since this is something that is prunable.
306 if (show_unreachable
) {
307 printf_ln(_("unreachable %s %s"),
308 printable_type(&obj
->oid
, obj
->type
),
309 describe_object(&obj
->oid
));
314 * "!USED" means that nothing at all points to it, including
315 * other unreachable objects. In other words, it's the "tip"
316 * of some set of unreachable objects, usually a commit that
319 * Such starting points are more interesting than some random
320 * set of unreachable objects, so we show them even if the user
321 * hasn't asked for _all_ unreachable objects. If you have
322 * deleted a branch by mistake, this is a prime candidate to
323 * start looking at, for example.
325 if (!(obj
->flags
& USED
)) {
327 printf_ln(_("dangling %s %s"),
328 printable_type(&obj
->oid
, obj
->type
),
329 describe_object(&obj
->oid
));
330 if (write_lost_and_found
) {
331 char *filename
= repo_git_path(the_repository
, "lost-found/%s/%s",
332 obj
->type
== OBJ_COMMIT
? "commit" : "other",
333 describe_object(&obj
->oid
));
336 if (safe_create_leading_directories_const(the_repository
, filename
)) {
337 error(_("could not create lost-found"));
341 f
= xfopen(filename
, "w");
342 if (obj
->type
== OBJ_BLOB
) {
343 if (stream_blob_to_fd(fileno(f
), &obj
->oid
, NULL
, 1))
344 die_errno(_("could not write '%s'"), filename
);
346 fprintf(f
, "%s\n", describe_object(&obj
->oid
));
348 die_errno(_("could not finish '%s'"),
356 * Otherwise? It's there, it's unreachable, and some other unreachable
357 * object points to it. Ignore it - it's not interesting, and we showed
358 * all the interesting cases above.
362 static void check_object(struct object
*obj
)
365 fprintf_ln(stderr
, _("Checking %s"), describe_object(&obj
->oid
));
367 if (obj
->flags
& REACHABLE
)
368 check_reachable_object(obj
);
370 check_unreachable_object(obj
);
373 static void check_connectivity(void)
377 /* Traverse the pending reachable objects */
378 traverse_reachable();
381 * With --connectivity-only, we won't have actually opened and marked
382 * unreachable objects with USED. Do that now to make --dangling, etc
385 if (connectivity_only
&& (show_dangling
|| write_lost_and_found
)) {
387 * Even though we already have a "struct object" for each of
388 * these in memory, we must not iterate over the internal
389 * object hash as we do below. Our loop would potentially
390 * resize the hash, making our iteration invalid.
392 * Instead, we'll just go back to the source list of objects,
393 * and ignore any that weren't present in our earlier
396 for_each_loose_object(the_repository
->objects
,
397 mark_loose_unreachable_referents
, NULL
, 0);
398 for_each_packed_object(the_repository
,
399 mark_packed_unreachable_referents
,
404 /* Look up all the requirements, warn about missing objects.. */
405 max
= get_max_object_index(the_repository
);
407 fprintf_ln(stderr
, _("Checking connectivity (%d objects)"), max
);
409 for (i
= 0; i
< max
; i
++) {
410 struct object
*obj
= get_indexed_object(the_repository
, i
);
417 static int fsck_obj(struct object
*obj
, void *buffer
, unsigned long size
)
421 if (obj
->flags
& SEEN
)
426 fprintf_ln(stderr
, _("Checking %s %s"),
427 printable_type(&obj
->oid
, obj
->type
),
428 describe_object(&obj
->oid
));
430 if (fsck_walk(obj
, NULL
, &fsck_obj_options
))
431 objerror(obj
, _("broken links"));
432 err
= fsck_object(obj
, buffer
, size
, &fsck_obj_options
);
436 if (obj
->type
== OBJ_COMMIT
) {
437 struct commit
*commit
= (struct commit
*) obj
;
439 if (!commit
->parents
&& show_root
)
440 printf_ln(_("root %s"),
441 describe_object(&commit
->object
.oid
));
444 if (obj
->type
== OBJ_TAG
) {
445 struct tag
*tag
= (struct tag
*) obj
;
447 if (show_tags
&& tag
->tagged
) {
448 printf_ln(_("tagged %s %s (%s) in %s"),
449 printable_type(&tag
->tagged
->oid
, tag
->tagged
->type
),
450 describe_object(&tag
->tagged
->oid
),
452 describe_object(&tag
->object
.oid
));
457 if (obj
->type
== OBJ_TREE
)
458 free_tree_buffer((struct tree
*)obj
);
462 static int fsck_obj_buffer(const struct object_id
*oid
, enum object_type type
,
463 unsigned long size
, void *buffer
, int *eaten
)
466 * Note, buffer may be NULL if type is OBJ_BLOB. See
467 * verify_packfile(), data_valid variable for details.
470 obj
= parse_object_buffer(the_repository
, oid
, type
, size
, buffer
,
473 errors_found
|= ERROR_OBJECT
;
474 return error(_("%s: object corrupt or missing"),
477 obj
->flags
&= ~(REACHABLE
| SEEN
);
478 obj
->flags
|= HAS_OBJ
;
479 return fsck_obj(obj
, buffer
, size
);
482 static int default_refs
;
484 static void fsck_handle_reflog_oid(const char *refname
, struct object_id
*oid
,
485 timestamp_t timestamp
)
489 if (!is_null_oid(oid
)) {
490 obj
= lookup_object(the_repository
, oid
);
491 if (obj
&& (obj
->flags
& HAS_OBJ
)) {
493 fsck_put_object_name(&fsck_walk_options
, oid
,
497 mark_object_reachable(obj
);
498 } else if (!is_promisor_object(the_repository
, oid
)) {
499 error(_("%s: invalid reflog entry %s"),
500 refname
, oid_to_hex(oid
));
501 errors_found
|= ERROR_REACHABLE
;
506 static int fsck_handle_reflog_ent(const char *refname
,
507 struct object_id
*ooid
, struct object_id
*noid
,
508 const char *email UNUSED
,
509 timestamp_t timestamp
, int tz UNUSED
,
510 const char *message UNUSED
, void *cb_data UNUSED
)
513 fprintf_ln(stderr
, _("Checking reflog %s->%s"),
514 oid_to_hex(ooid
), oid_to_hex(noid
));
516 fsck_handle_reflog_oid(refname
, ooid
, 0);
517 fsck_handle_reflog_oid(refname
, noid
, timestamp
);
521 static int fsck_handle_reflog(const char *logname
, void *cb_data
)
523 struct strbuf refname
= STRBUF_INIT
;
525 strbuf_worktree_ref(cb_data
, &refname
, logname
);
526 refs_for_each_reflog_ent(get_main_ref_store(the_repository
),
527 refname
.buf
, fsck_handle_reflog_ent
,
529 strbuf_release(&refname
);
533 static int fsck_handle_ref(const char *refname
, const char *referent UNUSED
, const struct object_id
*oid
,
534 int flag UNUSED
, void *cb_data UNUSED
)
538 obj
= parse_object(the_repository
, oid
);
540 if (is_promisor_object(the_repository
, oid
)) {
542 * Increment default_refs anyway, because this is a
548 error(_("%s: invalid sha1 pointer %s"),
549 refname
, oid_to_hex(oid
));
550 errors_found
|= ERROR_REACHABLE
;
551 /* We'll continue with the rest despite the error.. */
554 if (obj
->type
!= OBJ_COMMIT
&& is_branch(refname
)) {
555 error(_("%s: not a commit"), refname
);
556 errors_found
|= ERROR_REFS
;
560 fsck_put_object_name(&fsck_walk_options
,
562 mark_object_reachable(obj
);
567 static int fsck_head_link(const char *head_ref_name
,
568 const char **head_points_at
,
569 struct object_id
*head_oid
);
571 static void get_default_heads(void)
573 struct worktree
**worktrees
, **p
;
574 const char *head_points_at
;
575 struct object_id head_oid
;
577 refs_for_each_rawref(get_main_ref_store(the_repository
),
578 fsck_handle_ref
, NULL
);
580 worktrees
= get_worktrees();
581 for (p
= worktrees
; *p
; p
++) {
582 struct worktree
*wt
= *p
;
583 struct strbuf ref
= STRBUF_INIT
;
585 strbuf_worktree_ref(wt
, &ref
, "HEAD");
586 fsck_head_link(ref
.buf
, &head_points_at
, &head_oid
);
587 if (head_points_at
&& !is_null_oid(&head_oid
))
588 fsck_handle_ref(ref
.buf
, NULL
, &head_oid
, 0, NULL
);
589 strbuf_release(&ref
);
592 refs_for_each_reflog(get_worktree_ref_store(wt
),
593 fsck_handle_reflog
, wt
);
595 free_worktrees(worktrees
);
598 * Not having any default heads isn't really fatal, but
599 * it does mean that "--unreachable" no longer makes any
600 * sense (since in this case everything will obviously
601 * be unreachable by definition.
603 * Showing dangling objects is valid, though (as those
604 * dangling objects are likely lost heads).
606 * So we just print a warning about it, and clear the
607 * "show_unreachable" flag.
610 fprintf_ln(stderr
, _("notice: No default references"));
611 show_unreachable
= 0;
615 struct for_each_loose_cb
617 struct progress
*progress
;
620 static int fsck_loose(const struct object_id
*oid
, const char *path
,
624 enum object_type type
= OBJ_NONE
;
626 void *contents
= NULL
;
628 struct object_info oi
= OBJECT_INFO_INIT
;
629 struct object_id real_oid
= *null_oid(the_hash_algo
);
635 if (read_loose_object(the_repository
, path
, oid
, &real_oid
, &contents
, &oi
) < 0) {
636 if (contents
&& !oideq(&real_oid
, oid
))
637 err
= error(_("%s: hash-path mismatch, found at: %s"),
638 oid_to_hex(&real_oid
), path
);
640 err
= error(_("%s: object corrupt or missing: %s"),
641 oid_to_hex(oid
), path
);
644 errors_found
|= ERROR_OBJECT
;
646 return 0; /* keep checking other objects */
649 if (!contents
&& type
!= OBJ_BLOB
)
650 BUG("read_loose_object streamed a non-blob");
652 obj
= parse_object_buffer(the_repository
, oid
, type
, size
,
656 errors_found
|= ERROR_OBJECT
;
657 error(_("%s: object could not be parsed: %s"),
658 oid_to_hex(oid
), path
);
661 return 0; /* keep checking other objects */
664 obj
->flags
&= ~(REACHABLE
| SEEN
);
665 obj
->flags
|= HAS_OBJ
;
666 if (fsck_obj(obj
, contents
, size
))
667 errors_found
|= ERROR_OBJECT
;
671 return 0; /* keep checking other objects, even if we saw an error */
674 static int fsck_cruft(const char *basename
, const char *path
,
677 if (!starts_with(basename
, "tmp_obj_"))
678 fprintf_ln(stderr
, _("bad sha1 file: %s"), path
);
682 static int fsck_subdir(unsigned int nr
, const char *path UNUSED
, void *data
)
684 struct for_each_loose_cb
*cb_data
= data
;
685 struct progress
*progress
= cb_data
->progress
;
686 display_progress(progress
, nr
+ 1);
690 static void fsck_source(struct odb_source
*source
)
692 struct progress
*progress
= NULL
;
693 struct for_each_loose_cb cb_data
= {
694 .progress
= progress
,
698 fprintf_ln(stderr
, _("Checking object directory"));
701 progress
= start_progress(the_repository
,
702 _("Checking object directories"), 256);
704 for_each_loose_file_in_source(source
, fsck_loose
,
705 fsck_cruft
, fsck_subdir
, &cb_data
);
706 display_progress(progress
, 256);
707 stop_progress(&progress
);
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
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
720 head_ref_name
, 0, head_oid
,
722 if (!*head_points_at
) {
723 errors_found
|= ERROR_REFS
;
724 return error(_("invalid %s"), head_ref_name
);
726 if (!strcmp(*head_points_at
, head_ref_name
))
729 else if (!starts_with(*head_points_at
, "refs/heads/")) {
730 errors_found
|= ERROR_REFS
;
731 return error(_("%s points to something strange (%s)"),
732 head_ref_name
, *head_points_at
);
734 if (is_null_oid(head_oid
)) {
736 errors_found
|= ERROR_REFS
;
737 return error(_("%s: detached HEAD points at nothing"),
741 _("notice: %s points to an unborn branch (%s)"),
742 head_ref_name
, *head_points_at
+ 11);
747 static int fsck_cache_tree(struct cache_tree
*it
, const char *index_path
)
753 fprintf_ln(stderr
, _("Checking cache tree of %s"), index_path
);
755 if (0 <= it
->entry_count
) {
756 struct object
*obj
= parse_object(the_repository
, &it
->oid
);
758 error(_("%s: invalid sha1 pointer in cache-tree of %s"),
759 oid_to_hex(&it
->oid
), index_path
);
760 errors_found
|= ERROR_REFS
;
764 fsck_put_object_name(&fsck_walk_options
, &it
->oid
, ":");
765 mark_object_reachable(obj
);
766 if (obj
->type
!= OBJ_TREE
)
767 err
|= objerror(obj
, _("non-tree in cache-tree"));
769 for (i
= 0; i
< it
->subtree_nr
; i
++)
770 err
|= fsck_cache_tree(it
->down
[i
]->cache_tree
, index_path
);
774 static int fsck_resolve_undo(struct index_state
*istate
,
775 const char *index_path
)
777 struct string_list_item
*item
;
778 struct string_list
*resolve_undo
= istate
->resolve_undo
;
783 for_each_string_list_item(item
, resolve_undo
) {
784 const char *path
= item
->string
;
785 struct resolve_undo_info
*ru
= item
->util
;
790 for (i
= 0; i
< 3; i
++) {
793 if (!ru
->mode
[i
] || !S_ISREG(ru
->mode
[i
]))
796 obj
= parse_object(the_repository
, &ru
->oid
[i
]);
798 error(_("%s: invalid sha1 pointer in resolve-undo of %s"),
799 oid_to_hex(&ru
->oid
[i
]),
801 errors_found
|= ERROR_REFS
;
805 fsck_put_object_name(&fsck_walk_options
, &ru
->oid
[i
],
806 ":(%d):%s", i
, path
);
807 mark_object_reachable(obj
);
813 static void fsck_index(struct index_state
*istate
, const char *index_path
,
814 int is_current_worktree
)
818 /* TODO: audit for interaction with sparse-index. */
819 ensure_full_index(istate
);
820 for (i
= 0; i
< istate
->cache_nr
; i
++) {
825 mode
= istate
->cache
[i
]->ce_mode
;
826 if (S_ISGITLINK(mode
))
828 blob
= lookup_blob(the_repository
,
829 &istate
->cache
[i
]->oid
);
834 fsck_put_object_name(&fsck_walk_options
, &obj
->oid
,
836 is_current_worktree
? "" : index_path
,
837 istate
->cache
[i
]->name
);
838 mark_object_reachable(obj
);
840 if (istate
->cache_tree
)
841 fsck_cache_tree(istate
->cache_tree
, index_path
);
842 fsck_resolve_undo(istate
, index_path
);
845 static void mark_object_for_connectivity(const struct object_id
*oid
)
847 struct object
*obj
= lookup_unknown_object(the_repository
, oid
);
848 obj
->flags
|= HAS_OBJ
;
851 static int mark_loose_for_connectivity(const struct object_id
*oid
,
852 const char *path UNUSED
,
855 mark_object_for_connectivity(oid
);
859 static int mark_packed_for_connectivity(const struct object_id
*oid
,
860 struct packed_git
*pack UNUSED
,
864 mark_object_for_connectivity(oid
);
868 static int check_pack_rev_indexes(struct repository
*r
, int show_progress
)
870 struct progress
*progress
= NULL
;
871 uint32_t pack_count
= 0;
875 for (struct packed_git
*p
= get_all_packs(r
); p
; p
= p
->next
)
877 progress
= start_delayed_progress(the_repository
,
878 "Verifying reverse pack-indexes", pack_count
);
882 for (struct packed_git
*p
= get_all_packs(r
); p
; p
= p
->next
) {
883 int load_error
= load_pack_revindex_from_disk(p
);
885 if (load_error
< 0) {
886 error(_("unable to load rev-index for pack '%s'"), p
->pack_name
);
887 res
= ERROR_PACK_REV_INDEX
;
888 } else if (!load_error
&&
889 !load_pack_revindex(r
, p
) &&
890 verify_pack_revindex(p
)) {
891 error(_("invalid rev-index for pack '%s'"), p
->pack_name
);
892 res
= ERROR_PACK_REV_INDEX
;
894 display_progress(progress
, ++pack_count
);
896 stop_progress(&progress
);
901 static void fsck_refs(struct repository
*r
)
903 struct child_process refs_verify
= CHILD_PROCESS_INIT
;
904 struct progress
*progress
= NULL
;
907 progress
= start_progress(r
, _("Checking ref database"), 1);
910 fprintf_ln(stderr
, _("Checking ref database"));
912 child_process_init(&refs_verify
);
913 refs_verify
.git_cmd
= 1;
914 strvec_pushl(&refs_verify
.args
, "refs", "verify", NULL
);
916 strvec_push(&refs_verify
.args
, "--verbose");
918 strvec_push(&refs_verify
.args
, "--strict");
920 if (run_command(&refs_verify
))
921 errors_found
|= ERROR_REFS
;
923 display_progress(progress
, 1);
924 stop_progress(&progress
);
927 static char const * const fsck_usage
[] = {
928 N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n"
929 " [--[no-]full] [--strict] [--verbose] [--lost-found]\n"
930 " [--[no-]dangling] [--[no-]progress] [--connectivity-only]\n"
931 " [--[no-]name-objects] [--[no-]references] [<object>...]"),
935 static struct option fsck_opts
[] = {
936 OPT__VERBOSE(&verbose
, N_("be verbose")),
937 OPT_BOOL(0, "unreachable", &show_unreachable
, N_("show unreachable objects")),
938 OPT_BOOL(0, "dangling", &show_dangling
, N_("show dangling objects")),
939 OPT_BOOL(0, "tags", &show_tags
, N_("report tags")),
940 OPT_BOOL(0, "root", &show_root
, N_("report root nodes")),
941 OPT_BOOL(0, "cache", &keep_cache_objects
, N_("make index objects head nodes")),
942 OPT_BOOL(0, "reflogs", &include_reflogs
, N_("make reflogs head nodes (default)")),
943 OPT_BOOL(0, "full", &check_full
, N_("also consider packs and alternate objects")),
944 OPT_BOOL(0, "connectivity-only", &connectivity_only
, N_("check only connectivity")),
945 OPT_BOOL(0, "strict", &check_strict
, N_("enable more strict checking")),
946 OPT_BOOL(0, "lost-found", &write_lost_and_found
,
947 N_("write dangling objects in .git/lost-found")),
948 OPT_BOOL(0, "progress", &show_progress
, N_("show progress")),
949 OPT_BOOL(0, "name-objects", &name_objects
, N_("show verbose names for reachable objects")),
950 OPT_BOOL(0, "references", &check_references
, N_("check reference database consistency")),
954 int cmd_fsck(int argc
,
957 struct repository
*repo UNUSED
)
960 struct odb_source
*source
;
962 /* fsck knows how to handle missing promisor objects */
963 fetch_if_missing
= 0;
966 disable_replace_refs();
967 save_commit_buffer
= 0;
969 argc
= parse_options(argc
, argv
, prefix
, fsck_opts
, fsck_usage
, 0);
971 fsck_walk_options
.walk
= mark_object
;
972 fsck_obj_options
.walk
= mark_used
;
973 fsck_obj_options
.error_func
= fsck_objects_error_func
;
975 fsck_obj_options
.strict
= 1;
977 if (show_progress
== -1)
978 show_progress
= isatty(2);
982 if (write_lost_and_found
) {
988 fsck_enable_object_names(&fsck_walk_options
);
990 repo_config(the_repository
, git_fsck_config
, &fsck_obj_options
);
991 prepare_repo_settings(the_repository
);
993 if (check_references
)
994 fsck_refs(the_repository
);
996 if (connectivity_only
) {
997 for_each_loose_object(the_repository
->objects
,
998 mark_loose_for_connectivity
, NULL
, 0);
999 for_each_packed_object(the_repository
,
1000 mark_packed_for_connectivity
, NULL
, 0);
1002 odb_prepare_alternates(the_repository
->objects
);
1003 for (source
= the_repository
->objects
->sources
; source
; source
= source
->next
)
1004 fsck_source(source
);
1007 struct packed_git
*p
;
1008 uint32_t total
= 0, count
= 0;
1009 struct progress
*progress
= NULL
;
1011 if (show_progress
) {
1012 for (p
= get_all_packs(the_repository
); p
;
1014 if (open_pack_index(p
))
1016 total
+= p
->num_objects
;
1019 progress
= start_progress(the_repository
,
1020 _("Checking objects"), total
);
1022 for (p
= get_all_packs(the_repository
); p
;
1024 /* verify gives error messages itself */
1025 if (verify_pack(the_repository
,
1028 errors_found
|= ERROR_PACK
;
1029 count
+= p
->num_objects
;
1031 stop_progress(&progress
);
1034 if (fsck_finish(&fsck_obj_options
))
1035 errors_found
|= ERROR_OBJECT
;
1038 for (i
= 0; i
< argc
; i
++) {
1039 const char *arg
= argv
[i
];
1040 struct object_id oid
;
1041 if (!repo_get_oid(the_repository
, arg
, &oid
)) {
1042 struct object
*obj
= lookup_object(the_repository
,
1045 if (!obj
|| !(obj
->flags
& HAS_OBJ
)) {
1046 if (is_promisor_object(the_repository
, &oid
))
1048 error(_("%s: object missing"), oid_to_hex(&oid
));
1049 errors_found
|= ERROR_OBJECT
;
1054 fsck_put_object_name(&fsck_walk_options
, &oid
,
1056 mark_object_reachable(obj
);
1059 error(_("invalid parameter: expected sha1, got '%s'"), arg
);
1060 errors_found
|= ERROR_OBJECT
;
1064 * If we've not been given any explicit head information, do the
1065 * default ones from .git/refs. We also consider the index file
1066 * in this case (ie this implies --cache).
1069 get_default_heads();
1070 keep_cache_objects
= 1;
1073 if (keep_cache_objects
) {
1074 struct worktree
**worktrees
, **p
;
1076 verify_index_checksum
= 1;
1077 verify_ce_order
= 1;
1079 worktrees
= get_worktrees();
1080 for (p
= worktrees
; *p
; p
++) {
1081 struct worktree
*wt
= *p
;
1082 struct index_state istate
=
1083 INDEX_STATE_INIT(the_repository
);
1084 char *path
, *wt_gitdir
;
1087 * Make a copy since the buffer is reusable
1088 * and may get overwritten by other calls
1089 * while we're examining the index.
1091 path
= xstrdup(worktree_git_path(the_repository
, wt
, "index"));
1092 wt_gitdir
= get_worktree_git_dir(wt
);
1094 read_index_from(&istate
, path
, wt_gitdir
);
1095 fsck_index(&istate
, path
, wt
->is_current
);
1097 discard_index(&istate
);
1101 free_worktrees(worktrees
);
1104 errors_found
|= check_pack_rev_indexes(the_repository
, show_progress
);
1105 if (verify_bitmap_files(the_repository
))
1106 errors_found
|= ERROR_BITMAP
;
1108 check_connectivity();
1110 if (the_repository
->settings
.core_commit_graph
) {
1111 struct child_process commit_graph_verify
= CHILD_PROCESS_INIT
;
1113 odb_prepare_alternates(the_repository
->objects
);
1114 for (source
= the_repository
->objects
->sources
; source
; source
= source
->next
) {
1115 child_process_init(&commit_graph_verify
);
1116 commit_graph_verify
.git_cmd
= 1;
1117 strvec_pushl(&commit_graph_verify
.args
, "commit-graph",
1118 "verify", "--object-dir", source
->path
, NULL
);
1120 strvec_push(&commit_graph_verify
.args
, "--progress");
1122 strvec_push(&commit_graph_verify
.args
, "--no-progress");
1123 if (run_command(&commit_graph_verify
))
1124 errors_found
|= ERROR_COMMIT_GRAPH
;
1128 if (the_repository
->settings
.core_multi_pack_index
) {
1129 struct child_process midx_verify
= CHILD_PROCESS_INIT
;
1131 odb_prepare_alternates(the_repository
->objects
);
1132 for (source
= the_repository
->objects
->sources
; source
; source
= source
->next
) {
1133 child_process_init(&midx_verify
);
1134 midx_verify
.git_cmd
= 1;
1135 strvec_pushl(&midx_verify
.args
, "multi-pack-index",
1136 "verify", "--object-dir", source
->path
, NULL
);
1138 strvec_push(&midx_verify
.args
, "--progress");
1140 strvec_push(&midx_verify
.args
, "--no-progress");
1141 if (run_command(&midx_verify
))
1142 errors_found
|= ERROR_MULTI_PACK_INDEX
;
1146 return errors_found
;