4 #include "repository.h"
12 #include "cache-tree.h"
13 #include "tree-walk.h"
15 #include "parse-options.h"
18 #include "streaming.h"
21 #include "object-file.h"
22 #include "object-name.h"
23 #include "object-store-ll.h"
25 #include "read-cache-ll.h"
26 #include "replace-object.h"
27 #include "resolve-undo.h"
28 #include "run-command.h"
29 #include "sparse-index.h"
31 #include "pack-revindex.h"
32 #include "pack-bitmap.h"
34 #define REACHABLE 0x0001
36 #define HAS_OBJ 0x0004
37 /* This flag is set if something points to this object. */
42 static int show_unreachable
;
43 static int include_reflogs
= 1;
44 static int check_full
= 1;
45 static int connectivity_only
;
46 static int check_strict
;
47 static int keep_cache_objects
;
48 static struct fsck_options fsck_walk_options
= FSCK_OPTIONS_DEFAULT
;
49 static struct fsck_options fsck_obj_options
= FSCK_OPTIONS_DEFAULT
;
50 static int errors_found
;
51 static int write_lost_and_found
;
53 static int show_progress
= -1;
54 static int show_dangling
= 1;
55 static int name_objects
;
56 #define ERROR_OBJECT 01
57 #define ERROR_REACHABLE 02
59 #define ERROR_REFS 010
60 #define ERROR_COMMIT_GRAPH 020
61 #define ERROR_MULTI_PACK_INDEX 040
62 #define ERROR_PACK_REV_INDEX 0100
63 #define ERROR_BITMAP 0200
65 static const char *describe_object(const struct object_id
*oid
)
67 return fsck_describe_object(&fsck_walk_options
, oid
);
70 static const char *printable_type(const struct object_id
*oid
,
71 enum object_type type
)
76 type
= oid_object_info(the_repository
, oid
, NULL
);
78 ret
= type_name(type
);
85 static int objerror(struct object
*obj
, const char *err
)
87 errors_found
|= ERROR_OBJECT
;
88 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
89 fprintf_ln(stderr
, _("error in %s %s: %s"),
90 printable_type(&obj
->oid
, obj
->type
),
91 describe_object(&obj
->oid
), err
);
95 static int fsck_error_func(struct fsck_options
*o UNUSED
,
96 const struct object_id
*oid
,
97 enum object_type object_type
,
98 enum fsck_msg_type msg_type
,
99 enum fsck_msg_id msg_id UNUSED
,
104 /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
105 fprintf_ln(stderr
, _("warning in %s %s: %s"),
106 printable_type(oid
, object_type
),
107 describe_object(oid
), message
);
110 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
111 fprintf_ln(stderr
, _("error in %s %s: %s"),
112 printable_type(oid
, object_type
),
113 describe_object(oid
), message
);
116 BUG("%d (FSCK_IGNORE?) should never trigger this callback",
121 static struct object_array pending
;
123 static int mark_object(struct object
*obj
, enum object_type type
,
124 void *data
, struct fsck_options
*options UNUSED
)
126 struct object
*parent
= data
;
129 * The only case data is NULL or type is OBJ_ANY is when
130 * mark_object_reachable() calls us. All the callers of
131 * that function has non-NULL obj hence ...
134 /* ... these references to parent->fld are safe here */
135 printf_ln(_("broken link from %7s %s"),
136 printable_type(&parent
->oid
, parent
->type
),
137 describe_object(&parent
->oid
));
138 printf_ln(_("broken link from %7s %s"),
139 (type
== OBJ_ANY
? _("unknown") : type_name(type
)),
141 errors_found
|= ERROR_REACHABLE
;
145 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
146 /* ... and the reference to parent is safe here */
147 objerror(parent
, _("wrong object type in link"));
149 if (obj
->flags
& REACHABLE
)
151 obj
->flags
|= REACHABLE
;
153 if (is_promisor_object(&obj
->oid
))
155 * Further recursion does not need to be performed on this
156 * object since it is a promisor object (so it does not need to
157 * be added to "pending").
161 if (!(obj
->flags
& HAS_OBJ
)) {
162 if (parent
&& !has_object(the_repository
, &obj
->oid
, 1)) {
163 printf_ln(_("broken link from %7s %s\n"
165 printable_type(&parent
->oid
, parent
->type
),
166 describe_object(&parent
->oid
),
167 printable_type(&obj
->oid
, obj
->type
),
168 describe_object(&obj
->oid
));
169 errors_found
|= ERROR_REACHABLE
;
174 add_object_array(obj
, NULL
, &pending
);
178 static void mark_object_reachable(struct object
*obj
)
180 mark_object(obj
, OBJ_ANY
, NULL
, NULL
);
183 static int traverse_one_object(struct object
*obj
)
185 int result
= fsck_walk(obj
, obj
, &fsck_walk_options
);
187 if (obj
->type
== OBJ_TREE
) {
188 struct tree
*tree
= (struct tree
*)obj
;
189 free_tree_buffer(tree
);
194 static int traverse_reachable(void)
196 struct progress
*progress
= NULL
;
200 progress
= start_delayed_progress(_("Checking connectivity"), 0);
202 result
|= traverse_one_object(object_array_pop(&pending
));
203 display_progress(progress
, ++nr
);
205 stop_progress(&progress
);
209 static int mark_used(struct object
*obj
, int type UNUSED
,
210 void *data UNUSED
, struct fsck_options
*options UNUSED
)
218 static void mark_unreachable_referents(const struct object_id
*oid
)
220 struct fsck_options options
= FSCK_OPTIONS_DEFAULT
;
221 struct object
*obj
= lookup_object(the_repository
, oid
);
223 if (!obj
|| !(obj
->flags
& HAS_OBJ
))
224 return; /* not part of our original set */
225 if (obj
->flags
& REACHABLE
)
226 return; /* reachable objects already traversed */
229 * Avoid passing OBJ_NONE to fsck_walk, which will parse the object
230 * (and we want to avoid parsing blobs).
232 if (obj
->type
== OBJ_NONE
) {
233 enum object_type type
= oid_object_info(the_repository
,
236 object_as_type(obj
, type
, 0);
239 options
.walk
= mark_used
;
240 fsck_walk(obj
, NULL
, &options
);
241 if (obj
->type
== OBJ_TREE
)
242 free_tree_buffer((struct tree
*)obj
);
245 static int mark_loose_unreachable_referents(const struct object_id
*oid
,
246 const char *path UNUSED
,
249 mark_unreachable_referents(oid
);
253 static int mark_packed_unreachable_referents(const struct object_id
*oid
,
254 struct packed_git
*pack UNUSED
,
258 mark_unreachable_referents(oid
);
263 * Check a single reachable object
265 static void check_reachable_object(struct object
*obj
)
268 * We obviously want the object to be parsed,
269 * except if it was in a pack-file and we didn't
272 if (!(obj
->flags
& HAS_OBJ
)) {
273 if (is_promisor_object(&obj
->oid
))
275 if (has_object_pack(&obj
->oid
))
276 return; /* it is in pack - forget about it */
277 printf_ln(_("missing %s %s"),
278 printable_type(&obj
->oid
, obj
->type
),
279 describe_object(&obj
->oid
));
280 errors_found
|= ERROR_REACHABLE
;
286 * Check a single unreachable object
288 static void check_unreachable_object(struct object
*obj
)
291 * Missing unreachable object? Ignore it. It's not like
292 * we miss it (since it can't be reached), nor do we want
293 * to complain about it being unreachable (since it does
296 if (!(obj
->flags
& HAS_OBJ
))
300 * Unreachable object that exists? Show it if asked to,
301 * since this is something that is prunable.
303 if (show_unreachable
) {
304 printf_ln(_("unreachable %s %s"),
305 printable_type(&obj
->oid
, obj
->type
),
306 describe_object(&obj
->oid
));
311 * "!USED" means that nothing at all points to it, including
312 * other unreachable objects. In other words, it's the "tip"
313 * of some set of unreachable objects, usually a commit that
316 * Such starting points are more interesting than some random
317 * set of unreachable objects, so we show them even if the user
318 * hasn't asked for _all_ unreachable objects. If you have
319 * deleted a branch by mistake, this is a prime candidate to
320 * start looking at, for example.
322 if (!(obj
->flags
& USED
)) {
324 printf_ln(_("dangling %s %s"),
325 printable_type(&obj
->oid
, obj
->type
),
326 describe_object(&obj
->oid
));
327 if (write_lost_and_found
) {
328 char *filename
= git_pathdup("lost-found/%s/%s",
329 obj
->type
== OBJ_COMMIT
? "commit" : "other",
330 describe_object(&obj
->oid
));
333 if (safe_create_leading_directories_const(filename
)) {
334 error(_("could not create lost-found"));
338 f
= xfopen(filename
, "w");
339 if (obj
->type
== OBJ_BLOB
) {
340 if (stream_blob_to_fd(fileno(f
), &obj
->oid
, NULL
, 1))
341 die_errno(_("could not write '%s'"), filename
);
343 fprintf(f
, "%s\n", describe_object(&obj
->oid
));
345 die_errno(_("could not finish '%s'"),
353 * Otherwise? It's there, it's unreachable, and some other unreachable
354 * object points to it. Ignore it - it's not interesting, and we showed
355 * all the interesting cases above.
359 static void check_object(struct object
*obj
)
362 fprintf_ln(stderr
, _("Checking %s"), describe_object(&obj
->oid
));
364 if (obj
->flags
& REACHABLE
)
365 check_reachable_object(obj
);
367 check_unreachable_object(obj
);
370 static void check_connectivity(void)
374 /* Traverse the pending reachable objects */
375 traverse_reachable();
378 * With --connectivity-only, we won't have actually opened and marked
379 * unreachable objects with USED. Do that now to make --dangling, etc
382 if (connectivity_only
&& (show_dangling
|| write_lost_and_found
)) {
384 * Even though we already have a "struct object" for each of
385 * these in memory, we must not iterate over the internal
386 * object hash as we do below. Our loop would potentially
387 * resize the hash, making our iteration invalid.
389 * Instead, we'll just go back to the source list of objects,
390 * and ignore any that weren't present in our earlier
393 for_each_loose_object(mark_loose_unreachable_referents
, NULL
, 0);
394 for_each_packed_object(mark_packed_unreachable_referents
, NULL
, 0);
397 /* Look up all the requirements, warn about missing objects.. */
398 max
= get_max_object_index();
400 fprintf_ln(stderr
, _("Checking connectivity (%d objects)"), max
);
402 for (i
= 0; i
< max
; i
++) {
403 struct object
*obj
= get_indexed_object(i
);
410 static int fsck_obj(struct object
*obj
, void *buffer
, unsigned long size
)
414 if (obj
->flags
& SEEN
)
419 fprintf_ln(stderr
, _("Checking %s %s"),
420 printable_type(&obj
->oid
, obj
->type
),
421 describe_object(&obj
->oid
));
423 if (fsck_walk(obj
, NULL
, &fsck_obj_options
))
424 objerror(obj
, _("broken links"));
425 err
= fsck_object(obj
, buffer
, size
, &fsck_obj_options
);
429 if (obj
->type
== OBJ_COMMIT
) {
430 struct commit
*commit
= (struct commit
*) obj
;
432 if (!commit
->parents
&& show_root
)
433 printf_ln(_("root %s"),
434 describe_object(&commit
->object
.oid
));
437 if (obj
->type
== OBJ_TAG
) {
438 struct tag
*tag
= (struct tag
*) obj
;
440 if (show_tags
&& tag
->tagged
) {
441 printf_ln(_("tagged %s %s (%s) in %s"),
442 printable_type(&tag
->tagged
->oid
, tag
->tagged
->type
),
443 describe_object(&tag
->tagged
->oid
),
445 describe_object(&tag
->object
.oid
));
450 if (obj
->type
== OBJ_TREE
)
451 free_tree_buffer((struct tree
*)obj
);
455 static int fsck_obj_buffer(const struct object_id
*oid
, enum object_type type
,
456 unsigned long size
, void *buffer
, int *eaten
)
459 * Note, buffer may be NULL if type is OBJ_BLOB. See
460 * verify_packfile(), data_valid variable for details.
463 obj
= parse_object_buffer(the_repository
, oid
, type
, size
, buffer
,
466 errors_found
|= ERROR_OBJECT
;
467 return error(_("%s: object corrupt or missing"),
470 obj
->flags
&= ~(REACHABLE
| SEEN
);
471 obj
->flags
|= HAS_OBJ
;
472 return fsck_obj(obj
, buffer
, size
);
475 static int default_refs
;
477 static void fsck_handle_reflog_oid(const char *refname
, struct object_id
*oid
,
478 timestamp_t timestamp
)
482 if (!is_null_oid(oid
)) {
483 obj
= lookup_object(the_repository
, oid
);
484 if (obj
&& (obj
->flags
& HAS_OBJ
)) {
486 fsck_put_object_name(&fsck_walk_options
, oid
,
490 mark_object_reachable(obj
);
491 } else if (!is_promisor_object(oid
)) {
492 error(_("%s: invalid reflog entry %s"),
493 refname
, oid_to_hex(oid
));
494 errors_found
|= ERROR_REACHABLE
;
499 static int fsck_handle_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
500 const char *email UNUSED
,
501 timestamp_t timestamp
, int tz UNUSED
,
502 const char *message UNUSED
, void *cb_data
)
504 const char *refname
= cb_data
;
507 fprintf_ln(stderr
, _("Checking reflog %s->%s"),
508 oid_to_hex(ooid
), oid_to_hex(noid
));
510 fsck_handle_reflog_oid(refname
, ooid
, 0);
511 fsck_handle_reflog_oid(refname
, noid
, timestamp
);
515 static int fsck_handle_reflog(const char *logname
,
516 const struct object_id
*oid UNUSED
,
517 int flag UNUSED
, void *cb_data
)
519 struct strbuf refname
= STRBUF_INIT
;
521 strbuf_worktree_ref(cb_data
, &refname
, logname
);
522 for_each_reflog_ent(refname
.buf
, fsck_handle_reflog_ent
, refname
.buf
);
523 strbuf_release(&refname
);
527 static int fsck_handle_ref(const char *refname
, const struct object_id
*oid
,
528 int flag UNUSED
, void *cb_data UNUSED
)
532 obj
= parse_object(the_repository
, oid
);
534 if (is_promisor_object(oid
)) {
536 * Increment default_refs anyway, because this is a
542 error(_("%s: invalid sha1 pointer %s"),
543 refname
, oid_to_hex(oid
));
544 errors_found
|= ERROR_REACHABLE
;
545 /* We'll continue with the rest despite the error.. */
548 if (obj
->type
!= OBJ_COMMIT
&& is_branch(refname
)) {
549 error(_("%s: not a commit"), refname
);
550 errors_found
|= ERROR_REFS
;
554 fsck_put_object_name(&fsck_walk_options
,
556 mark_object_reachable(obj
);
561 static int fsck_head_link(const char *head_ref_name
,
562 const char **head_points_at
,
563 struct object_id
*head_oid
);
565 static void get_default_heads(void)
567 struct worktree
**worktrees
, **p
;
568 const char *head_points_at
;
569 struct object_id head_oid
;
571 for_each_rawref(fsck_handle_ref
, NULL
);
573 worktrees
= get_worktrees();
574 for (p
= worktrees
; *p
; p
++) {
575 struct worktree
*wt
= *p
;
576 struct strbuf ref
= STRBUF_INIT
;
578 strbuf_worktree_ref(wt
, &ref
, "HEAD");
579 fsck_head_link(ref
.buf
, &head_points_at
, &head_oid
);
580 if (head_points_at
&& !is_null_oid(&head_oid
))
581 fsck_handle_ref(ref
.buf
, &head_oid
, 0, NULL
);
582 strbuf_release(&ref
);
585 refs_for_each_reflog(get_worktree_ref_store(wt
),
586 fsck_handle_reflog
, wt
);
588 free_worktrees(worktrees
);
591 * Not having any default heads isn't really fatal, but
592 * it does mean that "--unreachable" no longer makes any
593 * sense (since in this case everything will obviously
594 * be unreachable by definition.
596 * Showing dangling objects is valid, though (as those
597 * dangling objects are likely lost heads).
599 * So we just print a warning about it, and clear the
600 * "show_unreachable" flag.
603 fprintf_ln(stderr
, _("notice: No default references"));
604 show_unreachable
= 0;
608 struct for_each_loose_cb
610 struct progress
*progress
;
611 struct strbuf obj_type
;
614 static int fsck_loose(const struct object_id
*oid
, const char *path
, void *data
)
616 struct for_each_loose_cb
*cb_data
= data
;
618 enum object_type type
= OBJ_NONE
;
620 void *contents
= NULL
;
622 struct object_info oi
= OBJECT_INFO_INIT
;
623 struct object_id real_oid
= *null_oid();
626 strbuf_reset(&cb_data
->obj_type
);
627 oi
.type_name
= &cb_data
->obj_type
;
631 if (read_loose_object(path
, oid
, &real_oid
, &contents
, &oi
) < 0) {
632 if (contents
&& !oideq(&real_oid
, oid
))
633 err
= error(_("%s: hash-path mismatch, found at: %s"),
634 oid_to_hex(&real_oid
), path
);
636 err
= error(_("%s: object corrupt or missing: %s"),
637 oid_to_hex(oid
), path
);
639 if (type
!= OBJ_NONE
&& type
< 0)
640 err
= error(_("%s: object is of unknown type '%s': %s"),
641 oid_to_hex(&real_oid
), cb_data
->obj_type
.buf
,
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_object_dir(const char *path
)
692 struct progress
*progress
= NULL
;
693 struct for_each_loose_cb cb_data
= {
694 .obj_type
= STRBUF_INIT
,
695 .progress
= progress
,
699 fprintf_ln(stderr
, _("Checking object directory"));
702 progress
= start_progress(_("Checking object directories"), 256);
704 for_each_loose_file_in_objdir(path
, fsck_loose
, fsck_cruft
, fsck_subdir
,
706 display_progress(progress
, 256);
707 stop_progress(&progress
);
708 strbuf_release(&cb_data
.obj_type
);
711 static int fsck_head_link(const char *head_ref_name
,
712 const char **head_points_at
,
713 struct object_id
*head_oid
)
715 int null_is_error
= 0;
718 fprintf_ln(stderr
, _("Checking %s link"), head_ref_name
);
720 *head_points_at
= resolve_ref_unsafe(head_ref_name
, 0, head_oid
, NULL
);
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("Verifying reverse pack-indexes", pack_count
);
880 for (struct packed_git
*p
= get_all_packs(r
); p
; p
= p
->next
) {
881 int load_error
= load_pack_revindex_from_disk(p
);
883 if (load_error
< 0) {
884 error(_("unable to load rev-index for pack '%s'"), p
->pack_name
);
885 res
= ERROR_PACK_REV_INDEX
;
886 } else if (!load_error
&&
887 !load_pack_revindex(r
, p
) &&
888 verify_pack_revindex(p
)) {
889 error(_("invalid rev-index for pack '%s'"), p
->pack_name
);
890 res
= ERROR_PACK_REV_INDEX
;
892 display_progress(progress
, ++pack_count
);
894 stop_progress(&progress
);
899 static char const * const fsck_usage
[] = {
900 N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n"
901 " [--[no-]full] [--strict] [--verbose] [--lost-found]\n"
902 " [--[no-]dangling] [--[no-]progress] [--connectivity-only]\n"
903 " [--[no-]name-objects] [<object>...]"),
907 static struct option fsck_opts
[] = {
908 OPT__VERBOSE(&verbose
, N_("be verbose")),
909 OPT_BOOL(0, "unreachable", &show_unreachable
, N_("show unreachable objects")),
910 OPT_BOOL(0, "dangling", &show_dangling
, N_("show dangling objects")),
911 OPT_BOOL(0, "tags", &show_tags
, N_("report tags")),
912 OPT_BOOL(0, "root", &show_root
, N_("report root nodes")),
913 OPT_BOOL(0, "cache", &keep_cache_objects
, N_("make index objects head nodes")),
914 OPT_BOOL(0, "reflogs", &include_reflogs
, N_("make reflogs head nodes (default)")),
915 OPT_BOOL(0, "full", &check_full
, N_("also consider packs and alternate objects")),
916 OPT_BOOL(0, "connectivity-only", &connectivity_only
, N_("check only connectivity")),
917 OPT_BOOL(0, "strict", &check_strict
, N_("enable more strict checking")),
918 OPT_BOOL(0, "lost-found", &write_lost_and_found
,
919 N_("write dangling objects in .git/lost-found")),
920 OPT_BOOL(0, "progress", &show_progress
, N_("show progress")),
921 OPT_BOOL(0, "name-objects", &name_objects
, N_("show verbose names for reachable objects")),
925 int cmd_fsck(int argc
, const char **argv
, const char *prefix
)
928 struct object_directory
*odb
;
930 /* fsck knows how to handle missing promisor objects */
931 fetch_if_missing
= 0;
934 disable_replace_refs();
935 save_commit_buffer
= 0;
937 argc
= parse_options(argc
, argv
, prefix
, fsck_opts
, fsck_usage
, 0);
939 fsck_walk_options
.walk
= mark_object
;
940 fsck_obj_options
.walk
= mark_used
;
941 fsck_obj_options
.error_func
= fsck_error_func
;
943 fsck_obj_options
.strict
= 1;
945 if (show_progress
== -1)
946 show_progress
= isatty(2);
950 if (write_lost_and_found
) {
956 fsck_enable_object_names(&fsck_walk_options
);
958 git_config(git_fsck_config
, &fsck_obj_options
);
959 prepare_repo_settings(the_repository
);
961 if (connectivity_only
) {
962 for_each_loose_object(mark_loose_for_connectivity
, NULL
, 0);
963 for_each_packed_object(mark_packed_for_connectivity
, NULL
, 0);
965 prepare_alt_odb(the_repository
);
966 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
)
967 fsck_object_dir(odb
->path
);
970 struct packed_git
*p
;
971 uint32_t total
= 0, count
= 0;
972 struct progress
*progress
= NULL
;
975 for (p
= get_all_packs(the_repository
); p
;
977 if (open_pack_index(p
))
979 total
+= p
->num_objects
;
982 progress
= start_progress(_("Checking objects"), total
);
984 for (p
= get_all_packs(the_repository
); p
;
986 /* verify gives error messages itself */
987 if (verify_pack(the_repository
,
990 errors_found
|= ERROR_PACK
;
991 count
+= p
->num_objects
;
993 stop_progress(&progress
);
996 if (fsck_finish(&fsck_obj_options
))
997 errors_found
|= ERROR_OBJECT
;
1000 for (i
= 0; i
< argc
; i
++) {
1001 const char *arg
= argv
[i
];
1002 struct object_id oid
;
1003 if (!repo_get_oid(the_repository
, arg
, &oid
)) {
1004 struct object
*obj
= lookup_object(the_repository
,
1007 if (!obj
|| !(obj
->flags
& HAS_OBJ
)) {
1008 if (is_promisor_object(&oid
))
1010 error(_("%s: object missing"), oid_to_hex(&oid
));
1011 errors_found
|= ERROR_OBJECT
;
1016 fsck_put_object_name(&fsck_walk_options
, &oid
,
1018 mark_object_reachable(obj
);
1021 error(_("invalid parameter: expected sha1, got '%s'"), arg
);
1022 errors_found
|= ERROR_OBJECT
;
1026 * If we've not been given any explicit head information, do the
1027 * default ones from .git/refs. We also consider the index file
1028 * in this case (ie this implies --cache).
1031 get_default_heads();
1032 keep_cache_objects
= 1;
1035 if (keep_cache_objects
) {
1036 struct worktree
**worktrees
, **p
;
1038 verify_index_checksum
= 1;
1039 verify_ce_order
= 1;
1041 worktrees
= get_worktrees();
1042 for (p
= worktrees
; *p
; p
++) {
1043 struct worktree
*wt
= *p
;
1044 struct index_state istate
=
1045 INDEX_STATE_INIT(the_repository
);
1049 * Make a copy since the buffer is reusable
1050 * and may get overwritten by other calls
1051 * while we're examining the index.
1053 path
= xstrdup(worktree_git_path(wt
, "index"));
1054 read_index_from(&istate
, path
, get_worktree_git_dir(wt
));
1055 fsck_index(&istate
, path
, wt
->is_current
);
1056 discard_index(&istate
);
1059 free_worktrees(worktrees
);
1062 errors_found
|= check_pack_rev_indexes(the_repository
, show_progress
);
1063 if (verify_bitmap_files(the_repository
))
1064 errors_found
|= ERROR_BITMAP
;
1066 check_connectivity();
1068 if (the_repository
->settings
.core_commit_graph
) {
1069 struct child_process commit_graph_verify
= CHILD_PROCESS_INIT
;
1071 prepare_alt_odb(the_repository
);
1072 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
) {
1073 child_process_init(&commit_graph_verify
);
1074 commit_graph_verify
.git_cmd
= 1;
1075 strvec_pushl(&commit_graph_verify
.args
, "commit-graph",
1076 "verify", "--object-dir", odb
->path
, NULL
);
1078 strvec_push(&commit_graph_verify
.args
, "--progress");
1080 strvec_push(&commit_graph_verify
.args
, "--no-progress");
1081 if (run_command(&commit_graph_verify
))
1082 errors_found
|= ERROR_COMMIT_GRAPH
;
1086 if (the_repository
->settings
.core_multi_pack_index
) {
1087 struct child_process midx_verify
= CHILD_PROCESS_INIT
;
1089 prepare_alt_odb(the_repository
);
1090 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
) {
1091 child_process_init(&midx_verify
);
1092 midx_verify
.git_cmd
= 1;
1093 strvec_pushl(&midx_verify
.args
, "multi-pack-index",
1094 "verify", "--object-dir", odb
->path
, NULL
);
1096 strvec_push(&midx_verify
.args
, "--progress");
1098 strvec_push(&midx_verify
.args
, "--no-progress");
1099 if (run_command(&midx_verify
))
1100 errors_found
|= ERROR_MULTI_PACK_INDEX
;
1104 return errors_found
;