5 #include "repository.h"
13 #include "cache-tree.h"
14 #include "tree-walk.h"
16 #include "parse-options.h"
19 #include "streaming.h"
22 #include "object-name.h"
23 #include "object-store.h"
24 #include "replace-object.h"
25 #include "resolve-undo.h"
26 #include "run-command.h"
29 #define REACHABLE 0x0001
31 #define HAS_OBJ 0x0004
32 /* This flag is set if something points to this object. */
37 static int show_unreachable
;
38 static int include_reflogs
= 1;
39 static int check_full
= 1;
40 static int connectivity_only
;
41 static int check_strict
;
42 static int keep_cache_objects
;
43 static struct fsck_options fsck_walk_options
= FSCK_OPTIONS_DEFAULT
;
44 static struct fsck_options fsck_obj_options
= FSCK_OPTIONS_DEFAULT
;
45 static int errors_found
;
46 static int write_lost_and_found
;
48 static int show_progress
= -1;
49 static int show_dangling
= 1;
50 static int name_objects
;
51 #define ERROR_OBJECT 01
52 #define ERROR_REACHABLE 02
54 #define ERROR_REFS 010
55 #define ERROR_COMMIT_GRAPH 020
56 #define ERROR_MULTI_PACK_INDEX 040
58 static const char *describe_object(const struct object_id
*oid
)
60 return fsck_describe_object(&fsck_walk_options
, oid
);
63 static const char *printable_type(const struct object_id
*oid
,
64 enum object_type type
)
69 type
= oid_object_info(the_repository
, oid
, NULL
);
71 ret
= type_name(type
);
78 static int objerror(struct object
*obj
, const char *err
)
80 errors_found
|= ERROR_OBJECT
;
81 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
82 fprintf_ln(stderr
, _("error in %s %s: %s"),
83 printable_type(&obj
->oid
, obj
->type
),
84 describe_object(&obj
->oid
), err
);
88 static int fsck_error_func(struct fsck_options
*o
,
89 const struct object_id
*oid
,
90 enum object_type object_type
,
91 enum fsck_msg_type msg_type
,
92 enum fsck_msg_id msg_id
,
97 /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
98 fprintf_ln(stderr
, _("warning in %s %s: %s"),
99 printable_type(oid
, object_type
),
100 describe_object(oid
), message
);
103 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
104 fprintf_ln(stderr
, _("error in %s %s: %s"),
105 printable_type(oid
, object_type
),
106 describe_object(oid
), message
);
109 BUG("%d (FSCK_IGNORE?) should never trigger this callback",
114 static struct object_array pending
;
116 static int mark_object(struct object
*obj
, enum object_type type
,
117 void *data
, struct fsck_options
*options
)
119 struct object
*parent
= data
;
122 * The only case data is NULL or type is OBJ_ANY is when
123 * mark_object_reachable() calls us. All the callers of
124 * that function has non-NULL obj hence ...
127 /* ... these references to parent->fld are safe here */
128 printf_ln(_("broken link from %7s %s"),
129 printable_type(&parent
->oid
, parent
->type
),
130 describe_object(&parent
->oid
));
131 printf_ln(_("broken link from %7s %s"),
132 (type
== OBJ_ANY
? _("unknown") : type_name(type
)),
134 errors_found
|= ERROR_REACHABLE
;
138 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
139 /* ... and the reference to parent is safe here */
140 objerror(parent
, _("wrong object type in link"));
142 if (obj
->flags
& REACHABLE
)
144 obj
->flags
|= REACHABLE
;
146 if (is_promisor_object(&obj
->oid
))
148 * Further recursion does not need to be performed on this
149 * object since it is a promisor object (so it does not need to
150 * be added to "pending").
154 if (!(obj
->flags
& HAS_OBJ
)) {
155 if (parent
&& !has_object(the_repository
, &obj
->oid
, 1)) {
156 printf_ln(_("broken link from %7s %s\n"
158 printable_type(&parent
->oid
, parent
->type
),
159 describe_object(&parent
->oid
),
160 printable_type(&obj
->oid
, obj
->type
),
161 describe_object(&obj
->oid
));
162 errors_found
|= ERROR_REACHABLE
;
167 add_object_array(obj
, NULL
, &pending
);
171 static void mark_object_reachable(struct object
*obj
)
173 mark_object(obj
, OBJ_ANY
, NULL
, NULL
);
176 static int traverse_one_object(struct object
*obj
)
178 int result
= fsck_walk(obj
, obj
, &fsck_walk_options
);
180 if (obj
->type
== OBJ_TREE
) {
181 struct tree
*tree
= (struct tree
*)obj
;
182 free_tree_buffer(tree
);
187 static int traverse_reachable(void)
189 struct progress
*progress
= NULL
;
193 progress
= start_delayed_progress(_("Checking connectivity"), 0);
195 result
|= traverse_one_object(object_array_pop(&pending
));
196 display_progress(progress
, ++nr
);
198 stop_progress(&progress
);
202 static int mark_used(struct object
*obj
, enum object_type object_type
,
203 void *data
, struct fsck_options
*options
)
211 static void mark_unreachable_referents(const struct object_id
*oid
)
213 struct fsck_options options
= FSCK_OPTIONS_DEFAULT
;
214 struct object
*obj
= lookup_object(the_repository
, oid
);
216 if (!obj
|| !(obj
->flags
& HAS_OBJ
))
217 return; /* not part of our original set */
218 if (obj
->flags
& REACHABLE
)
219 return; /* reachable objects already traversed */
222 * Avoid passing OBJ_NONE to fsck_walk, which will parse the object
223 * (and we want to avoid parsing blobs).
225 if (obj
->type
== OBJ_NONE
) {
226 enum object_type type
= oid_object_info(the_repository
,
229 object_as_type(obj
, type
, 0);
232 options
.walk
= mark_used
;
233 fsck_walk(obj
, NULL
, &options
);
234 if (obj
->type
== OBJ_TREE
)
235 free_tree_buffer((struct tree
*)obj
);
238 static int mark_loose_unreachable_referents(const struct object_id
*oid
,
239 const char *path UNUSED
,
242 mark_unreachable_referents(oid
);
246 static int mark_packed_unreachable_referents(const struct object_id
*oid
,
247 struct packed_git
*pack UNUSED
,
251 mark_unreachable_referents(oid
);
256 * Check a single reachable object
258 static void check_reachable_object(struct object
*obj
)
261 * We obviously want the object to be parsed,
262 * except if it was in a pack-file and we didn't
265 if (!(obj
->flags
& HAS_OBJ
)) {
266 if (is_promisor_object(&obj
->oid
))
268 if (has_object_pack(&obj
->oid
))
269 return; /* it is in pack - forget about it */
270 printf_ln(_("missing %s %s"),
271 printable_type(&obj
->oid
, obj
->type
),
272 describe_object(&obj
->oid
));
273 errors_found
|= ERROR_REACHABLE
;
279 * Check a single unreachable object
281 static void check_unreachable_object(struct object
*obj
)
284 * Missing unreachable object? Ignore it. It's not like
285 * we miss it (since it can't be reached), nor do we want
286 * to complain about it being unreachable (since it does
289 if (!(obj
->flags
& HAS_OBJ
))
293 * Unreachable object that exists? Show it if asked to,
294 * since this is something that is prunable.
296 if (show_unreachable
) {
297 printf_ln(_("unreachable %s %s"),
298 printable_type(&obj
->oid
, obj
->type
),
299 describe_object(&obj
->oid
));
304 * "!USED" means that nothing at all points to it, including
305 * other unreachable objects. In other words, it's the "tip"
306 * of some set of unreachable objects, usually a commit that
309 * Such starting points are more interesting than some random
310 * set of unreachable objects, so we show them even if the user
311 * hasn't asked for _all_ unreachable objects. If you have
312 * deleted a branch by mistake, this is a prime candidate to
313 * start looking at, for example.
315 if (!(obj
->flags
& USED
)) {
317 printf_ln(_("dangling %s %s"),
318 printable_type(&obj
->oid
, obj
->type
),
319 describe_object(&obj
->oid
));
320 if (write_lost_and_found
) {
321 char *filename
= git_pathdup("lost-found/%s/%s",
322 obj
->type
== OBJ_COMMIT
? "commit" : "other",
323 describe_object(&obj
->oid
));
326 if (safe_create_leading_directories_const(filename
)) {
327 error(_("could not create lost-found"));
331 f
= xfopen(filename
, "w");
332 if (obj
->type
== OBJ_BLOB
) {
333 if (stream_blob_to_fd(fileno(f
), &obj
->oid
, NULL
, 1))
334 die_errno(_("could not write '%s'"), filename
);
336 fprintf(f
, "%s\n", describe_object(&obj
->oid
));
338 die_errno(_("could not finish '%s'"),
346 * Otherwise? It's there, it's unreachable, and some other unreachable
347 * object points to it. Ignore it - it's not interesting, and we showed
348 * all the interesting cases above.
352 static void check_object(struct object
*obj
)
355 fprintf_ln(stderr
, _("Checking %s"), describe_object(&obj
->oid
));
357 if (obj
->flags
& REACHABLE
)
358 check_reachable_object(obj
);
360 check_unreachable_object(obj
);
363 static void check_connectivity(void)
367 /* Traverse the pending reachable objects */
368 traverse_reachable();
371 * With --connectivity-only, we won't have actually opened and marked
372 * unreachable objects with USED. Do that now to make --dangling, etc
375 if (connectivity_only
&& (show_dangling
|| write_lost_and_found
)) {
377 * Even though we already have a "struct object" for each of
378 * these in memory, we must not iterate over the internal
379 * object hash as we do below. Our loop would potentially
380 * resize the hash, making our iteration invalid.
382 * Instead, we'll just go back to the source list of objects,
383 * and ignore any that weren't present in our earlier
386 for_each_loose_object(mark_loose_unreachable_referents
, NULL
, 0);
387 for_each_packed_object(mark_packed_unreachable_referents
, NULL
, 0);
390 /* Look up all the requirements, warn about missing objects.. */
391 max
= get_max_object_index();
393 fprintf_ln(stderr
, _("Checking connectivity (%d objects)"), max
);
395 for (i
= 0; i
< max
; i
++) {
396 struct object
*obj
= get_indexed_object(i
);
403 static int fsck_obj(struct object
*obj
, void *buffer
, unsigned long size
)
407 if (obj
->flags
& SEEN
)
412 fprintf_ln(stderr
, _("Checking %s %s"),
413 printable_type(&obj
->oid
, obj
->type
),
414 describe_object(&obj
->oid
));
416 if (fsck_walk(obj
, NULL
, &fsck_obj_options
))
417 objerror(obj
, _("broken links"));
418 err
= fsck_object(obj
, buffer
, size
, &fsck_obj_options
);
422 if (obj
->type
== OBJ_COMMIT
) {
423 struct commit
*commit
= (struct commit
*) obj
;
425 if (!commit
->parents
&& show_root
)
426 printf_ln(_("root %s"),
427 describe_object(&commit
->object
.oid
));
430 if (obj
->type
== OBJ_TAG
) {
431 struct tag
*tag
= (struct tag
*) obj
;
433 if (show_tags
&& tag
->tagged
) {
434 printf_ln(_("tagged %s %s (%s) in %s"),
435 printable_type(&tag
->tagged
->oid
, tag
->tagged
->type
),
436 describe_object(&tag
->tagged
->oid
),
438 describe_object(&tag
->object
.oid
));
443 if (obj
->type
== OBJ_TREE
)
444 free_tree_buffer((struct tree
*)obj
);
448 static int fsck_obj_buffer(const struct object_id
*oid
, enum object_type type
,
449 unsigned long size
, void *buffer
, int *eaten
)
452 * Note, buffer may be NULL if type is OBJ_BLOB. See
453 * verify_packfile(), data_valid variable for details.
456 obj
= parse_object_buffer(the_repository
, oid
, type
, size
, buffer
,
459 errors_found
|= ERROR_OBJECT
;
460 return error(_("%s: object corrupt or missing"),
463 obj
->flags
&= ~(REACHABLE
| SEEN
);
464 obj
->flags
|= HAS_OBJ
;
465 return fsck_obj(obj
, buffer
, size
);
468 static int default_refs
;
470 static void fsck_handle_reflog_oid(const char *refname
, struct object_id
*oid
,
471 timestamp_t timestamp
)
475 if (!is_null_oid(oid
)) {
476 obj
= lookup_object(the_repository
, oid
);
477 if (obj
&& (obj
->flags
& HAS_OBJ
)) {
479 fsck_put_object_name(&fsck_walk_options
, oid
,
483 mark_object_reachable(obj
);
484 } else if (!is_promisor_object(oid
)) {
485 error(_("%s: invalid reflog entry %s"),
486 refname
, oid_to_hex(oid
));
487 errors_found
|= ERROR_REACHABLE
;
492 static int fsck_handle_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
493 const char *email UNUSED
,
494 timestamp_t timestamp
, int tz UNUSED
,
495 const char *message UNUSED
, void *cb_data
)
497 const char *refname
= cb_data
;
500 fprintf_ln(stderr
, _("Checking reflog %s->%s"),
501 oid_to_hex(ooid
), oid_to_hex(noid
));
503 fsck_handle_reflog_oid(refname
, ooid
, 0);
504 fsck_handle_reflog_oid(refname
, noid
, timestamp
);
508 static int fsck_handle_reflog(const char *logname
,
509 const struct object_id
*oid UNUSED
,
510 int flag UNUSED
, void *cb_data
)
512 struct strbuf refname
= STRBUF_INIT
;
514 strbuf_worktree_ref(cb_data
, &refname
, logname
);
515 for_each_reflog_ent(refname
.buf
, fsck_handle_reflog_ent
, refname
.buf
);
516 strbuf_release(&refname
);
520 static int fsck_handle_ref(const char *refname
, const struct object_id
*oid
,
521 int flag UNUSED
, void *cb_data UNUSED
)
525 obj
= parse_object(the_repository
, oid
);
527 if (is_promisor_object(oid
)) {
529 * Increment default_refs anyway, because this is a
535 error(_("%s: invalid sha1 pointer %s"),
536 refname
, oid_to_hex(oid
));
537 errors_found
|= ERROR_REACHABLE
;
538 /* We'll continue with the rest despite the error.. */
541 if (obj
->type
!= OBJ_COMMIT
&& is_branch(refname
)) {
542 error(_("%s: not a commit"), refname
);
543 errors_found
|= ERROR_REFS
;
547 fsck_put_object_name(&fsck_walk_options
,
549 mark_object_reachable(obj
);
554 static int fsck_head_link(const char *head_ref_name
,
555 const char **head_points_at
,
556 struct object_id
*head_oid
);
558 static void get_default_heads(void)
560 struct worktree
**worktrees
, **p
;
561 const char *head_points_at
;
562 struct object_id head_oid
;
564 for_each_rawref(fsck_handle_ref
, NULL
);
566 worktrees
= get_worktrees();
567 for (p
= worktrees
; *p
; p
++) {
568 struct worktree
*wt
= *p
;
569 struct strbuf ref
= STRBUF_INIT
;
571 strbuf_worktree_ref(wt
, &ref
, "HEAD");
572 fsck_head_link(ref
.buf
, &head_points_at
, &head_oid
);
573 if (head_points_at
&& !is_null_oid(&head_oid
))
574 fsck_handle_ref(ref
.buf
, &head_oid
, 0, NULL
);
575 strbuf_release(&ref
);
578 refs_for_each_reflog(get_worktree_ref_store(wt
),
579 fsck_handle_reflog
, wt
);
581 free_worktrees(worktrees
);
584 * Not having any default heads isn't really fatal, but
585 * it does mean that "--unreachable" no longer makes any
586 * sense (since in this case everything will obviously
587 * be unreachable by definition.
589 * Showing dangling objects is valid, though (as those
590 * dangling objects are likely lost heads).
592 * So we just print a warning about it, and clear the
593 * "show_unreachable" flag.
596 fprintf_ln(stderr
, _("notice: No default references"));
597 show_unreachable
= 0;
601 struct for_each_loose_cb
603 struct progress
*progress
;
604 struct strbuf obj_type
;
607 static int fsck_loose(const struct object_id
*oid
, const char *path
, void *data
)
609 struct for_each_loose_cb
*cb_data
= data
;
611 enum object_type type
= OBJ_NONE
;
613 void *contents
= NULL
;
615 struct object_info oi
= OBJECT_INFO_INIT
;
616 struct object_id real_oid
= *null_oid();
619 strbuf_reset(&cb_data
->obj_type
);
620 oi
.type_name
= &cb_data
->obj_type
;
624 if (read_loose_object(path
, oid
, &real_oid
, &contents
, &oi
) < 0) {
625 if (contents
&& !oideq(&real_oid
, oid
))
626 err
= error(_("%s: hash-path mismatch, found at: %s"),
627 oid_to_hex(&real_oid
), path
);
629 err
= error(_("%s: object corrupt or missing: %s"),
630 oid_to_hex(oid
), path
);
632 if (type
!= OBJ_NONE
&& type
< 0)
633 err
= error(_("%s: object is of unknown type '%s': %s"),
634 oid_to_hex(&real_oid
), cb_data
->obj_type
.buf
,
637 errors_found
|= ERROR_OBJECT
;
639 return 0; /* keep checking other objects */
642 if (!contents
&& type
!= OBJ_BLOB
)
643 BUG("read_loose_object streamed a non-blob");
645 obj
= parse_object_buffer(the_repository
, oid
, type
, size
,
649 errors_found
|= ERROR_OBJECT
;
650 error(_("%s: object could not be parsed: %s"),
651 oid_to_hex(oid
), path
);
654 return 0; /* keep checking other objects */
657 obj
->flags
&= ~(REACHABLE
| SEEN
);
658 obj
->flags
|= HAS_OBJ
;
659 if (fsck_obj(obj
, contents
, size
))
660 errors_found
|= ERROR_OBJECT
;
664 return 0; /* keep checking other objects, even if we saw an error */
667 static int fsck_cruft(const char *basename
, const char *path
,
670 if (!starts_with(basename
, "tmp_obj_"))
671 fprintf_ln(stderr
, _("bad sha1 file: %s"), path
);
675 static int fsck_subdir(unsigned int nr
, const char *path UNUSED
, void *data
)
677 struct for_each_loose_cb
*cb_data
= data
;
678 struct progress
*progress
= cb_data
->progress
;
679 display_progress(progress
, nr
+ 1);
683 static void fsck_object_dir(const char *path
)
685 struct progress
*progress
= NULL
;
686 struct for_each_loose_cb cb_data
= {
687 .obj_type
= STRBUF_INIT
,
688 .progress
= progress
,
692 fprintf_ln(stderr
, _("Checking object directory"));
695 progress
= start_progress(_("Checking object directories"), 256);
697 for_each_loose_file_in_objdir(path
, fsck_loose
, fsck_cruft
, fsck_subdir
,
699 display_progress(progress
, 256);
700 stop_progress(&progress
);
701 strbuf_release(&cb_data
.obj_type
);
704 static int fsck_head_link(const char *head_ref_name
,
705 const char **head_points_at
,
706 struct object_id
*head_oid
)
708 int null_is_error
= 0;
711 fprintf_ln(stderr
, _("Checking %s link"), head_ref_name
);
713 *head_points_at
= resolve_ref_unsafe(head_ref_name
, 0, head_oid
, NULL
);
714 if (!*head_points_at
) {
715 errors_found
|= ERROR_REFS
;
716 return error(_("invalid %s"), head_ref_name
);
718 if (!strcmp(*head_points_at
, head_ref_name
))
721 else if (!starts_with(*head_points_at
, "refs/heads/")) {
722 errors_found
|= ERROR_REFS
;
723 return error(_("%s points to something strange (%s)"),
724 head_ref_name
, *head_points_at
);
726 if (is_null_oid(head_oid
)) {
728 errors_found
|= ERROR_REFS
;
729 return error(_("%s: detached HEAD points at nothing"),
733 _("notice: %s points to an unborn branch (%s)"),
734 head_ref_name
, *head_points_at
+ 11);
739 static int fsck_cache_tree(struct cache_tree
*it
, const char *index_path
)
745 fprintf_ln(stderr
, _("Checking cache tree of %s"), index_path
);
747 if (0 <= it
->entry_count
) {
748 struct object
*obj
= parse_object(the_repository
, &it
->oid
);
750 error(_("%s: invalid sha1 pointer in cache-tree of %s"),
751 oid_to_hex(&it
->oid
), index_path
);
752 errors_found
|= ERROR_REFS
;
756 fsck_put_object_name(&fsck_walk_options
, &it
->oid
, ":");
757 mark_object_reachable(obj
);
758 if (obj
->type
!= OBJ_TREE
)
759 err
|= objerror(obj
, _("non-tree in cache-tree"));
761 for (i
= 0; i
< it
->subtree_nr
; i
++)
762 err
|= fsck_cache_tree(it
->down
[i
]->cache_tree
, index_path
);
766 static int fsck_resolve_undo(struct index_state
*istate
,
767 const char *index_path
)
769 struct string_list_item
*item
;
770 struct string_list
*resolve_undo
= istate
->resolve_undo
;
775 for_each_string_list_item(item
, resolve_undo
) {
776 const char *path
= item
->string
;
777 struct resolve_undo_info
*ru
= item
->util
;
782 for (i
= 0; i
< 3; i
++) {
785 if (!ru
->mode
[i
] || !S_ISREG(ru
->mode
[i
]))
788 obj
= parse_object(the_repository
, &ru
->oid
[i
]);
790 error(_("%s: invalid sha1 pointer in resolve-undo of %s"),
791 oid_to_hex(&ru
->oid
[i
]),
793 errors_found
|= ERROR_REFS
;
797 fsck_put_object_name(&fsck_walk_options
, &ru
->oid
[i
],
798 ":(%d):%s", i
, path
);
799 mark_object_reachable(obj
);
805 static void fsck_index(struct index_state
*istate
, const char *index_path
,
810 /* TODO: audit for interaction with sparse-index. */
811 ensure_full_index(istate
);
812 for (i
= 0; i
< istate
->cache_nr
; i
++) {
817 mode
= istate
->cache
[i
]->ce_mode
;
818 if (S_ISGITLINK(mode
))
820 blob
= lookup_blob(the_repository
,
821 &istate
->cache
[i
]->oid
);
826 fsck_put_object_name(&fsck_walk_options
, &obj
->oid
,
828 is_main_index
? "" : index_path
,
829 istate
->cache
[i
]->name
);
830 mark_object_reachable(obj
);
832 if (istate
->cache_tree
)
833 fsck_cache_tree(istate
->cache_tree
, index_path
);
834 fsck_resolve_undo(istate
, index_path
);
837 static void mark_object_for_connectivity(const struct object_id
*oid
)
839 struct object
*obj
= lookup_unknown_object(the_repository
, oid
);
840 obj
->flags
|= HAS_OBJ
;
843 static int mark_loose_for_connectivity(const struct object_id
*oid
,
844 const char *path UNUSED
,
847 mark_object_for_connectivity(oid
);
851 static int mark_packed_for_connectivity(const struct object_id
*oid
,
852 struct packed_git
*pack UNUSED
,
856 mark_object_for_connectivity(oid
);
860 static char const * const fsck_usage
[] = {
861 N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n"
862 " [--[no-]full] [--strict] [--verbose] [--lost-found]\n"
863 " [--[no-]dangling] [--[no-]progress] [--connectivity-only]\n"
864 " [--[no-]name-objects] [<object>...]"),
868 static struct option fsck_opts
[] = {
869 OPT__VERBOSE(&verbose
, N_("be verbose")),
870 OPT_BOOL(0, "unreachable", &show_unreachable
, N_("show unreachable objects")),
871 OPT_BOOL(0, "dangling", &show_dangling
, N_("show dangling objects")),
872 OPT_BOOL(0, "tags", &show_tags
, N_("report tags")),
873 OPT_BOOL(0, "root", &show_root
, N_("report root nodes")),
874 OPT_BOOL(0, "cache", &keep_cache_objects
, N_("make index objects head nodes")),
875 OPT_BOOL(0, "reflogs", &include_reflogs
, N_("make reflogs head nodes (default)")),
876 OPT_BOOL(0, "full", &check_full
, N_("also consider packs and alternate objects")),
877 OPT_BOOL(0, "connectivity-only", &connectivity_only
, N_("check only connectivity")),
878 OPT_BOOL(0, "strict", &check_strict
, N_("enable more strict checking")),
879 OPT_BOOL(0, "lost-found", &write_lost_and_found
,
880 N_("write dangling objects in .git/lost-found")),
881 OPT_BOOL(0, "progress", &show_progress
, N_("show progress")),
882 OPT_BOOL(0, "name-objects", &name_objects
, N_("show verbose names for reachable objects")),
886 int cmd_fsck(int argc
, const char **argv
, const char *prefix
)
889 struct object_directory
*odb
;
891 /* fsck knows how to handle missing promisor objects */
892 fetch_if_missing
= 0;
895 read_replace_refs
= 0;
896 save_commit_buffer
= 0;
898 argc
= parse_options(argc
, argv
, prefix
, fsck_opts
, fsck_usage
, 0);
900 fsck_walk_options
.walk
= mark_object
;
901 fsck_obj_options
.walk
= mark_used
;
902 fsck_obj_options
.error_func
= fsck_error_func
;
904 fsck_obj_options
.strict
= 1;
906 if (show_progress
== -1)
907 show_progress
= isatty(2);
911 if (write_lost_and_found
) {
917 fsck_enable_object_names(&fsck_walk_options
);
919 git_config(git_fsck_config
, &fsck_obj_options
);
920 prepare_repo_settings(the_repository
);
922 if (connectivity_only
) {
923 for_each_loose_object(mark_loose_for_connectivity
, NULL
, 0);
924 for_each_packed_object(mark_packed_for_connectivity
, NULL
, 0);
926 prepare_alt_odb(the_repository
);
927 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
)
928 fsck_object_dir(odb
->path
);
931 struct packed_git
*p
;
932 uint32_t total
= 0, count
= 0;
933 struct progress
*progress
= NULL
;
936 for (p
= get_all_packs(the_repository
); p
;
938 if (open_pack_index(p
))
940 total
+= p
->num_objects
;
943 progress
= start_progress(_("Checking objects"), total
);
945 for (p
= get_all_packs(the_repository
); p
;
947 /* verify gives error messages itself */
948 if (verify_pack(the_repository
,
951 errors_found
|= ERROR_PACK
;
952 count
+= p
->num_objects
;
954 stop_progress(&progress
);
957 if (fsck_finish(&fsck_obj_options
))
958 errors_found
|= ERROR_OBJECT
;
961 for (i
= 0; i
< argc
; i
++) {
962 const char *arg
= argv
[i
];
963 struct object_id oid
;
964 if (!repo_get_oid(the_repository
, arg
, &oid
)) {
965 struct object
*obj
= lookup_object(the_repository
,
968 if (!obj
|| !(obj
->flags
& HAS_OBJ
)) {
969 if (is_promisor_object(&oid
))
971 error(_("%s: object missing"), oid_to_hex(&oid
));
972 errors_found
|= ERROR_OBJECT
;
977 fsck_put_object_name(&fsck_walk_options
, &oid
,
979 mark_object_reachable(obj
);
982 error(_("invalid parameter: expected sha1, got '%s'"), arg
);
983 errors_found
|= ERROR_OBJECT
;
987 * If we've not been given any explicit head information, do the
988 * default ones from .git/refs. We also consider the index file
989 * in this case (ie this implies --cache).
993 keep_cache_objects
= 1;
996 if (keep_cache_objects
) {
997 struct worktree
**worktrees
, **p
;
999 verify_index_checksum
= 1;
1000 verify_ce_order
= 1;
1002 worktrees
= get_worktrees();
1003 for (p
= worktrees
; *p
; p
++) {
1004 struct worktree
*wt
= *p
;
1005 struct index_state istate
=
1006 INDEX_STATE_INIT(the_repository
);
1010 * Make a copy since the buffer is reusable
1011 * and may get overwritten by other calls
1012 * while we're examining the index.
1014 path
= xstrdup(worktree_git_path(wt
, "index"));
1015 read_index_from(&istate
, path
, get_worktree_git_dir(wt
));
1016 fsck_index(&istate
, path
, wt
->is_current
);
1017 discard_index(&istate
);
1020 free_worktrees(worktrees
);
1023 check_connectivity();
1025 if (the_repository
->settings
.core_commit_graph
) {
1026 struct child_process commit_graph_verify
= CHILD_PROCESS_INIT
;
1028 prepare_alt_odb(the_repository
);
1029 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
) {
1030 child_process_init(&commit_graph_verify
);
1031 commit_graph_verify
.git_cmd
= 1;
1032 strvec_pushl(&commit_graph_verify
.args
, "commit-graph",
1033 "verify", "--object-dir", odb
->path
, NULL
);
1034 if (run_command(&commit_graph_verify
))
1035 errors_found
|= ERROR_COMMIT_GRAPH
;
1039 if (the_repository
->settings
.core_multi_pack_index
) {
1040 struct child_process midx_verify
= CHILD_PROCESS_INIT
;
1042 prepare_alt_odb(the_repository
);
1043 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
) {
1044 child_process_init(&midx_verify
);
1045 midx_verify
.git_cmd
= 1;
1046 strvec_pushl(&midx_verify
.args
, "multi-pack-index",
1047 "verify", "--object-dir", odb
->path
, NULL
);
1048 if (run_command(&midx_verify
))
1049 errors_found
|= ERROR_MULTI_PACK_INDEX
;
1053 return errors_found
;