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-store.h"
22 #include "replace-object.h"
23 #include "resolve-undo.h"
24 #include "run-command.h"
27 #define REACHABLE 0x0001
29 #define HAS_OBJ 0x0004
30 /* This flag is set if something points to this object. */
35 static int show_unreachable
;
36 static int include_reflogs
= 1;
37 static int check_full
= 1;
38 static int connectivity_only
;
39 static int check_strict
;
40 static int keep_cache_objects
;
41 static struct fsck_options fsck_walk_options
= FSCK_OPTIONS_DEFAULT
;
42 static struct fsck_options fsck_obj_options
= FSCK_OPTIONS_DEFAULT
;
43 static int errors_found
;
44 static int write_lost_and_found
;
46 static int show_progress
= -1;
47 static int show_dangling
= 1;
48 static int name_objects
;
49 #define ERROR_OBJECT 01
50 #define ERROR_REACHABLE 02
52 #define ERROR_REFS 010
53 #define ERROR_COMMIT_GRAPH 020
54 #define ERROR_MULTI_PACK_INDEX 040
56 static const char *describe_object(const struct object_id
*oid
)
58 return fsck_describe_object(&fsck_walk_options
, oid
);
61 static const char *printable_type(const struct object_id
*oid
,
62 enum object_type type
)
67 type
= oid_object_info(the_repository
, oid
, NULL
);
69 ret
= type_name(type
);
76 static int objerror(struct object
*obj
, const char *err
)
78 errors_found
|= ERROR_OBJECT
;
79 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
80 fprintf_ln(stderr
, _("error in %s %s: %s"),
81 printable_type(&obj
->oid
, obj
->type
),
82 describe_object(&obj
->oid
), err
);
86 static int fsck_error_func(struct fsck_options
*o
,
87 const struct object_id
*oid
,
88 enum object_type object_type
,
89 enum fsck_msg_type msg_type
,
90 enum fsck_msg_id msg_id
,
95 /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
96 fprintf_ln(stderr
, _("warning in %s %s: %s"),
97 printable_type(oid
, object_type
),
98 describe_object(oid
), message
);
101 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
102 fprintf_ln(stderr
, _("error in %s %s: %s"),
103 printable_type(oid
, object_type
),
104 describe_object(oid
), message
);
107 BUG("%d (FSCK_IGNORE?) should never trigger this callback",
112 static struct object_array pending
;
114 static int mark_object(struct object
*obj
, enum object_type type
,
115 void *data
, struct fsck_options
*options
)
117 struct object
*parent
= data
;
120 * The only case data is NULL or type is OBJ_ANY is when
121 * mark_object_reachable() calls us. All the callers of
122 * that function has non-NULL obj hence ...
125 /* ... these references to parent->fld are safe here */
126 printf_ln(_("broken link from %7s %s"),
127 printable_type(&parent
->oid
, parent
->type
),
128 describe_object(&parent
->oid
));
129 printf_ln(_("broken link from %7s %s"),
130 (type
== OBJ_ANY
? _("unknown") : type_name(type
)),
132 errors_found
|= ERROR_REACHABLE
;
136 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
137 /* ... and the reference to parent is safe here */
138 objerror(parent
, _("wrong object type in link"));
140 if (obj
->flags
& REACHABLE
)
142 obj
->flags
|= REACHABLE
;
144 if (is_promisor_object(&obj
->oid
))
146 * Further recursion does not need to be performed on this
147 * object since it is a promisor object (so it does not need to
148 * be added to "pending").
152 if (!(obj
->flags
& HAS_OBJ
)) {
153 if (parent
&& !has_object(the_repository
, &obj
->oid
, 1)) {
154 printf_ln(_("broken link from %7s %s\n"
156 printable_type(&parent
->oid
, parent
->type
),
157 describe_object(&parent
->oid
),
158 printable_type(&obj
->oid
, obj
->type
),
159 describe_object(&obj
->oid
));
160 errors_found
|= ERROR_REACHABLE
;
165 add_object_array(obj
, NULL
, &pending
);
169 static void mark_object_reachable(struct object
*obj
)
171 mark_object(obj
, OBJ_ANY
, NULL
, NULL
);
174 static int traverse_one_object(struct object
*obj
)
176 int result
= fsck_walk(obj
, obj
, &fsck_walk_options
);
178 if (obj
->type
== OBJ_TREE
) {
179 struct tree
*tree
= (struct tree
*)obj
;
180 free_tree_buffer(tree
);
185 static int traverse_reachable(void)
187 struct progress
*progress
= NULL
;
191 progress
= start_delayed_progress(_("Checking connectivity"), 0);
193 result
|= traverse_one_object(object_array_pop(&pending
));
194 display_progress(progress
, ++nr
);
196 stop_progress(&progress
);
200 static int mark_used(struct object
*obj
, enum object_type object_type
,
201 void *data
, struct fsck_options
*options
)
209 static void mark_unreachable_referents(const struct object_id
*oid
)
211 struct fsck_options options
= FSCK_OPTIONS_DEFAULT
;
212 struct object
*obj
= lookup_object(the_repository
, oid
);
214 if (!obj
|| !(obj
->flags
& HAS_OBJ
))
215 return; /* not part of our original set */
216 if (obj
->flags
& REACHABLE
)
217 return; /* reachable objects already traversed */
220 * Avoid passing OBJ_NONE to fsck_walk, which will parse the object
221 * (and we want to avoid parsing blobs).
223 if (obj
->type
== OBJ_NONE
) {
224 enum object_type type
= oid_object_info(the_repository
,
227 object_as_type(obj
, type
, 0);
230 options
.walk
= mark_used
;
231 fsck_walk(obj
, NULL
, &options
);
232 if (obj
->type
== OBJ_TREE
)
233 free_tree_buffer((struct tree
*)obj
);
236 static int mark_loose_unreachable_referents(const struct object_id
*oid
,
237 const char *path UNUSED
,
240 mark_unreachable_referents(oid
);
244 static int mark_packed_unreachable_referents(const struct object_id
*oid
,
245 struct packed_git
*pack UNUSED
,
249 mark_unreachable_referents(oid
);
254 * Check a single reachable object
256 static void check_reachable_object(struct object
*obj
)
259 * We obviously want the object to be parsed,
260 * except if it was in a pack-file and we didn't
263 if (!(obj
->flags
& HAS_OBJ
)) {
264 if (is_promisor_object(&obj
->oid
))
266 if (has_object_pack(&obj
->oid
))
267 return; /* it is in pack - forget about it */
268 printf_ln(_("missing %s %s"),
269 printable_type(&obj
->oid
, obj
->type
),
270 describe_object(&obj
->oid
));
271 errors_found
|= ERROR_REACHABLE
;
277 * Check a single unreachable object
279 static void check_unreachable_object(struct object
*obj
)
282 * Missing unreachable object? Ignore it. It's not like
283 * we miss it (since it can't be reached), nor do we want
284 * to complain about it being unreachable (since it does
287 if (!(obj
->flags
& HAS_OBJ
))
291 * Unreachable object that exists? Show it if asked to,
292 * since this is something that is prunable.
294 if (show_unreachable
) {
295 printf_ln(_("unreachable %s %s"),
296 printable_type(&obj
->oid
, obj
->type
),
297 describe_object(&obj
->oid
));
302 * "!USED" means that nothing at all points to it, including
303 * other unreachable objects. In other words, it's the "tip"
304 * of some set of unreachable objects, usually a commit that
307 * Such starting points are more interesting than some random
308 * set of unreachable objects, so we show them even if the user
309 * hasn't asked for _all_ unreachable objects. If you have
310 * deleted a branch by mistake, this is a prime candidate to
311 * start looking at, for example.
313 if (!(obj
->flags
& USED
)) {
315 printf_ln(_("dangling %s %s"),
316 printable_type(&obj
->oid
, obj
->type
),
317 describe_object(&obj
->oid
));
318 if (write_lost_and_found
) {
319 char *filename
= git_pathdup("lost-found/%s/%s",
320 obj
->type
== OBJ_COMMIT
? "commit" : "other",
321 describe_object(&obj
->oid
));
324 if (safe_create_leading_directories_const(filename
)) {
325 error(_("could not create lost-found"));
329 f
= xfopen(filename
, "w");
330 if (obj
->type
== OBJ_BLOB
) {
331 if (stream_blob_to_fd(fileno(f
), &obj
->oid
, NULL
, 1))
332 die_errno(_("could not write '%s'"), filename
);
334 fprintf(f
, "%s\n", describe_object(&obj
->oid
));
336 die_errno(_("could not finish '%s'"),
344 * Otherwise? It's there, it's unreachable, and some other unreachable
345 * object points to it. Ignore it - it's not interesting, and we showed
346 * all the interesting cases above.
350 static void check_object(struct object
*obj
)
353 fprintf_ln(stderr
, _("Checking %s"), describe_object(&obj
->oid
));
355 if (obj
->flags
& REACHABLE
)
356 check_reachable_object(obj
);
358 check_unreachable_object(obj
);
361 static void check_connectivity(void)
365 /* Traverse the pending reachable objects */
366 traverse_reachable();
369 * With --connectivity-only, we won't have actually opened and marked
370 * unreachable objects with USED. Do that now to make --dangling, etc
373 if (connectivity_only
&& (show_dangling
|| write_lost_and_found
)) {
375 * Even though we already have a "struct object" for each of
376 * these in memory, we must not iterate over the internal
377 * object hash as we do below. Our loop would potentially
378 * resize the hash, making our iteration invalid.
380 * Instead, we'll just go back to the source list of objects,
381 * and ignore any that weren't present in our earlier
384 for_each_loose_object(mark_loose_unreachable_referents
, NULL
, 0);
385 for_each_packed_object(mark_packed_unreachable_referents
, NULL
, 0);
388 /* Look up all the requirements, warn about missing objects.. */
389 max
= get_max_object_index();
391 fprintf_ln(stderr
, _("Checking connectivity (%d objects)"), max
);
393 for (i
= 0; i
< max
; i
++) {
394 struct object
*obj
= get_indexed_object(i
);
401 static int fsck_obj(struct object
*obj
, void *buffer
, unsigned long size
)
405 if (obj
->flags
& SEEN
)
410 fprintf_ln(stderr
, _("Checking %s %s"),
411 printable_type(&obj
->oid
, obj
->type
),
412 describe_object(&obj
->oid
));
414 if (fsck_walk(obj
, NULL
, &fsck_obj_options
))
415 objerror(obj
, _("broken links"));
416 err
= fsck_object(obj
, buffer
, size
, &fsck_obj_options
);
420 if (obj
->type
== OBJ_COMMIT
) {
421 struct commit
*commit
= (struct commit
*) obj
;
423 if (!commit
->parents
&& show_root
)
424 printf_ln(_("root %s"),
425 describe_object(&commit
->object
.oid
));
428 if (obj
->type
== OBJ_TAG
) {
429 struct tag
*tag
= (struct tag
*) obj
;
431 if (show_tags
&& tag
->tagged
) {
432 printf_ln(_("tagged %s %s (%s) in %s"),
433 printable_type(&tag
->tagged
->oid
, tag
->tagged
->type
),
434 describe_object(&tag
->tagged
->oid
),
436 describe_object(&tag
->object
.oid
));
441 if (obj
->type
== OBJ_TREE
)
442 free_tree_buffer((struct tree
*)obj
);
446 static int fsck_obj_buffer(const struct object_id
*oid
, enum object_type type
,
447 unsigned long size
, void *buffer
, int *eaten
)
450 * Note, buffer may be NULL if type is OBJ_BLOB. See
451 * verify_packfile(), data_valid variable for details.
454 obj
= parse_object_buffer(the_repository
, oid
, type
, size
, buffer
,
457 errors_found
|= ERROR_OBJECT
;
458 return error(_("%s: object corrupt or missing"),
461 obj
->flags
&= ~(REACHABLE
| SEEN
);
462 obj
->flags
|= HAS_OBJ
;
463 return fsck_obj(obj
, buffer
, size
);
466 static int default_refs
;
468 static void fsck_handle_reflog_oid(const char *refname
, struct object_id
*oid
,
469 timestamp_t timestamp
)
473 if (!is_null_oid(oid
)) {
474 obj
= lookup_object(the_repository
, oid
);
475 if (obj
&& (obj
->flags
& HAS_OBJ
)) {
477 fsck_put_object_name(&fsck_walk_options
, oid
,
481 mark_object_reachable(obj
);
482 } else if (!is_promisor_object(oid
)) {
483 error(_("%s: invalid reflog entry %s"),
484 refname
, oid_to_hex(oid
));
485 errors_found
|= ERROR_REACHABLE
;
490 static int fsck_handle_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
491 const char *email UNUSED
,
492 timestamp_t timestamp
, int tz UNUSED
,
493 const char *message UNUSED
, void *cb_data
)
495 const char *refname
= cb_data
;
498 fprintf_ln(stderr
, _("Checking reflog %s->%s"),
499 oid_to_hex(ooid
), oid_to_hex(noid
));
501 fsck_handle_reflog_oid(refname
, ooid
, 0);
502 fsck_handle_reflog_oid(refname
, noid
, timestamp
);
506 static int fsck_handle_reflog(const char *logname
,
507 const struct object_id
*oid UNUSED
,
508 int flag UNUSED
, void *cb_data
)
510 struct strbuf refname
= STRBUF_INIT
;
512 strbuf_worktree_ref(cb_data
, &refname
, logname
);
513 for_each_reflog_ent(refname
.buf
, fsck_handle_reflog_ent
, refname
.buf
);
514 strbuf_release(&refname
);
518 static int fsck_handle_ref(const char *refname
, const struct object_id
*oid
,
519 int flag UNUSED
, void *cb_data UNUSED
)
523 obj
= parse_object(the_repository
, oid
);
525 if (is_promisor_object(oid
)) {
527 * Increment default_refs anyway, because this is a
533 error(_("%s: invalid sha1 pointer %s"),
534 refname
, oid_to_hex(oid
));
535 errors_found
|= ERROR_REACHABLE
;
536 /* We'll continue with the rest despite the error.. */
539 if (obj
->type
!= OBJ_COMMIT
&& is_branch(refname
)) {
540 error(_("%s: not a commit"), refname
);
541 errors_found
|= ERROR_REFS
;
545 fsck_put_object_name(&fsck_walk_options
,
547 mark_object_reachable(obj
);
552 static int fsck_head_link(const char *head_ref_name
,
553 const char **head_points_at
,
554 struct object_id
*head_oid
);
556 static void get_default_heads(void)
558 struct worktree
**worktrees
, **p
;
559 const char *head_points_at
;
560 struct object_id head_oid
;
562 for_each_rawref(fsck_handle_ref
, NULL
);
564 worktrees
= get_worktrees();
565 for (p
= worktrees
; *p
; p
++) {
566 struct worktree
*wt
= *p
;
567 struct strbuf ref
= STRBUF_INIT
;
569 strbuf_worktree_ref(wt
, &ref
, "HEAD");
570 fsck_head_link(ref
.buf
, &head_points_at
, &head_oid
);
571 if (head_points_at
&& !is_null_oid(&head_oid
))
572 fsck_handle_ref(ref
.buf
, &head_oid
, 0, NULL
);
573 strbuf_release(&ref
);
576 refs_for_each_reflog(get_worktree_ref_store(wt
),
577 fsck_handle_reflog
, wt
);
579 free_worktrees(worktrees
);
582 * Not having any default heads isn't really fatal, but
583 * it does mean that "--unreachable" no longer makes any
584 * sense (since in this case everything will obviously
585 * be unreachable by definition.
587 * Showing dangling objects is valid, though (as those
588 * dangling objects are likely lost heads).
590 * So we just print a warning about it, and clear the
591 * "show_unreachable" flag.
594 fprintf_ln(stderr
, _("notice: No default references"));
595 show_unreachable
= 0;
599 struct for_each_loose_cb
601 struct progress
*progress
;
602 struct strbuf obj_type
;
605 static int fsck_loose(const struct object_id
*oid
, const char *path
, void *data
)
607 struct for_each_loose_cb
*cb_data
= data
;
609 enum object_type type
= OBJ_NONE
;
611 void *contents
= NULL
;
613 struct object_info oi
= OBJECT_INFO_INIT
;
614 struct object_id real_oid
= *null_oid();
617 strbuf_reset(&cb_data
->obj_type
);
618 oi
.type_name
= &cb_data
->obj_type
;
622 if (read_loose_object(path
, oid
, &real_oid
, &contents
, &oi
) < 0) {
623 if (contents
&& !oideq(&real_oid
, oid
))
624 err
= error(_("%s: hash-path mismatch, found at: %s"),
625 oid_to_hex(&real_oid
), path
);
627 err
= error(_("%s: object corrupt or missing: %s"),
628 oid_to_hex(oid
), path
);
630 if (type
!= OBJ_NONE
&& type
< 0)
631 err
= error(_("%s: object is of unknown type '%s': %s"),
632 oid_to_hex(&real_oid
), cb_data
->obj_type
.buf
,
635 errors_found
|= ERROR_OBJECT
;
637 return 0; /* keep checking other objects */
640 if (!contents
&& type
!= OBJ_BLOB
)
641 BUG("read_loose_object streamed a non-blob");
643 obj
= parse_object_buffer(the_repository
, oid
, type
, size
,
647 errors_found
|= ERROR_OBJECT
;
648 error(_("%s: object could not be parsed: %s"),
649 oid_to_hex(oid
), path
);
652 return 0; /* keep checking other objects */
655 obj
->flags
&= ~(REACHABLE
| SEEN
);
656 obj
->flags
|= HAS_OBJ
;
657 if (fsck_obj(obj
, contents
, size
))
658 errors_found
|= ERROR_OBJECT
;
662 return 0; /* keep checking other objects, even if we saw an error */
665 static int fsck_cruft(const char *basename
, const char *path
,
668 if (!starts_with(basename
, "tmp_obj_"))
669 fprintf_ln(stderr
, _("bad sha1 file: %s"), path
);
673 static int fsck_subdir(unsigned int nr
, const char *path UNUSED
, void *data
)
675 struct for_each_loose_cb
*cb_data
= data
;
676 struct progress
*progress
= cb_data
->progress
;
677 display_progress(progress
, nr
+ 1);
681 static void fsck_object_dir(const char *path
)
683 struct progress
*progress
= NULL
;
684 struct for_each_loose_cb cb_data
= {
685 .obj_type
= STRBUF_INIT
,
686 .progress
= progress
,
690 fprintf_ln(stderr
, _("Checking object directory"));
693 progress
= start_progress(_("Checking object directories"), 256);
695 for_each_loose_file_in_objdir(path
, fsck_loose
, fsck_cruft
, fsck_subdir
,
697 display_progress(progress
, 256);
698 stop_progress(&progress
);
699 strbuf_release(&cb_data
.obj_type
);
702 static int fsck_head_link(const char *head_ref_name
,
703 const char **head_points_at
,
704 struct object_id
*head_oid
)
706 int null_is_error
= 0;
709 fprintf_ln(stderr
, _("Checking %s link"), head_ref_name
);
711 *head_points_at
= resolve_ref_unsafe(head_ref_name
, 0, head_oid
, NULL
);
712 if (!*head_points_at
) {
713 errors_found
|= ERROR_REFS
;
714 return error(_("invalid %s"), head_ref_name
);
716 if (!strcmp(*head_points_at
, head_ref_name
))
719 else if (!starts_with(*head_points_at
, "refs/heads/")) {
720 errors_found
|= ERROR_REFS
;
721 return error(_("%s points to something strange (%s)"),
722 head_ref_name
, *head_points_at
);
724 if (is_null_oid(head_oid
)) {
726 errors_found
|= ERROR_REFS
;
727 return error(_("%s: detached HEAD points at nothing"),
731 _("notice: %s points to an unborn branch (%s)"),
732 head_ref_name
, *head_points_at
+ 11);
737 static int fsck_cache_tree(struct cache_tree
*it
, const char *index_path
)
743 fprintf_ln(stderr
, _("Checking cache tree of %s"), index_path
);
745 if (0 <= it
->entry_count
) {
746 struct object
*obj
= parse_object(the_repository
, &it
->oid
);
748 error(_("%s: invalid sha1 pointer in cache-tree of %s"),
749 oid_to_hex(&it
->oid
), index_path
);
750 errors_found
|= ERROR_REFS
;
754 fsck_put_object_name(&fsck_walk_options
, &it
->oid
, ":");
755 mark_object_reachable(obj
);
756 if (obj
->type
!= OBJ_TREE
)
757 err
|= objerror(obj
, _("non-tree in cache-tree"));
759 for (i
= 0; i
< it
->subtree_nr
; i
++)
760 err
|= fsck_cache_tree(it
->down
[i
]->cache_tree
, index_path
);
764 static int fsck_resolve_undo(struct index_state
*istate
,
765 const char *index_path
)
767 struct string_list_item
*item
;
768 struct string_list
*resolve_undo
= istate
->resolve_undo
;
773 for_each_string_list_item(item
, resolve_undo
) {
774 const char *path
= item
->string
;
775 struct resolve_undo_info
*ru
= item
->util
;
780 for (i
= 0; i
< 3; i
++) {
783 if (!ru
->mode
[i
] || !S_ISREG(ru
->mode
[i
]))
786 obj
= parse_object(the_repository
, &ru
->oid
[i
]);
788 error(_("%s: invalid sha1 pointer in resolve-undo of %s"),
789 oid_to_hex(&ru
->oid
[i
]),
791 errors_found
|= ERROR_REFS
;
795 fsck_put_object_name(&fsck_walk_options
, &ru
->oid
[i
],
796 ":(%d):%s", i
, path
);
797 mark_object_reachable(obj
);
803 static void fsck_index(struct index_state
*istate
, const char *index_path
,
808 /* TODO: audit for interaction with sparse-index. */
809 ensure_full_index(istate
);
810 for (i
= 0; i
< istate
->cache_nr
; i
++) {
815 mode
= istate
->cache
[i
]->ce_mode
;
816 if (S_ISGITLINK(mode
))
818 blob
= lookup_blob(the_repository
,
819 &istate
->cache
[i
]->oid
);
824 fsck_put_object_name(&fsck_walk_options
, &obj
->oid
,
826 is_main_index
? "" : index_path
,
827 istate
->cache
[i
]->name
);
828 mark_object_reachable(obj
);
830 if (istate
->cache_tree
)
831 fsck_cache_tree(istate
->cache_tree
, index_path
);
832 fsck_resolve_undo(istate
, index_path
);
835 static void mark_object_for_connectivity(const struct object_id
*oid
)
837 struct object
*obj
= lookup_unknown_object(the_repository
, oid
);
838 obj
->flags
|= HAS_OBJ
;
841 static int mark_loose_for_connectivity(const struct object_id
*oid
,
842 const char *path UNUSED
,
845 mark_object_for_connectivity(oid
);
849 static int mark_packed_for_connectivity(const struct object_id
*oid
,
850 struct packed_git
*pack UNUSED
,
854 mark_object_for_connectivity(oid
);
858 static char const * const fsck_usage
[] = {
859 N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n"
860 " [--[no-]full] [--strict] [--verbose] [--lost-found]\n"
861 " [--[no-]dangling] [--[no-]progress] [--connectivity-only]\n"
862 " [--[no-]name-objects] [<object>...]"),
866 static struct option fsck_opts
[] = {
867 OPT__VERBOSE(&verbose
, N_("be verbose")),
868 OPT_BOOL(0, "unreachable", &show_unreachable
, N_("show unreachable objects")),
869 OPT_BOOL(0, "dangling", &show_dangling
, N_("show dangling objects")),
870 OPT_BOOL(0, "tags", &show_tags
, N_("report tags")),
871 OPT_BOOL(0, "root", &show_root
, N_("report root nodes")),
872 OPT_BOOL(0, "cache", &keep_cache_objects
, N_("make index objects head nodes")),
873 OPT_BOOL(0, "reflogs", &include_reflogs
, N_("make reflogs head nodes (default)")),
874 OPT_BOOL(0, "full", &check_full
, N_("also consider packs and alternate objects")),
875 OPT_BOOL(0, "connectivity-only", &connectivity_only
, N_("check only connectivity")),
876 OPT_BOOL(0, "strict", &check_strict
, N_("enable more strict checking")),
877 OPT_BOOL(0, "lost-found", &write_lost_and_found
,
878 N_("write dangling objects in .git/lost-found")),
879 OPT_BOOL(0, "progress", &show_progress
, N_("show progress")),
880 OPT_BOOL(0, "name-objects", &name_objects
, N_("show verbose names for reachable objects")),
884 int cmd_fsck(int argc
, const char **argv
, const char *prefix
)
887 struct object_directory
*odb
;
889 /* fsck knows how to handle missing promisor objects */
890 fetch_if_missing
= 0;
893 read_replace_refs
= 0;
894 save_commit_buffer
= 0;
896 argc
= parse_options(argc
, argv
, prefix
, fsck_opts
, fsck_usage
, 0);
898 fsck_walk_options
.walk
= mark_object
;
899 fsck_obj_options
.walk
= mark_used
;
900 fsck_obj_options
.error_func
= fsck_error_func
;
902 fsck_obj_options
.strict
= 1;
904 if (show_progress
== -1)
905 show_progress
= isatty(2);
909 if (write_lost_and_found
) {
915 fsck_enable_object_names(&fsck_walk_options
);
917 git_config(git_fsck_config
, &fsck_obj_options
);
918 prepare_repo_settings(the_repository
);
920 if (connectivity_only
) {
921 for_each_loose_object(mark_loose_for_connectivity
, NULL
, 0);
922 for_each_packed_object(mark_packed_for_connectivity
, NULL
, 0);
924 prepare_alt_odb(the_repository
);
925 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
)
926 fsck_object_dir(odb
->path
);
929 struct packed_git
*p
;
930 uint32_t total
= 0, count
= 0;
931 struct progress
*progress
= NULL
;
934 for (p
= get_all_packs(the_repository
); p
;
936 if (open_pack_index(p
))
938 total
+= p
->num_objects
;
941 progress
= start_progress(_("Checking objects"), total
);
943 for (p
= get_all_packs(the_repository
); p
;
945 /* verify gives error messages itself */
946 if (verify_pack(the_repository
,
949 errors_found
|= ERROR_PACK
;
950 count
+= p
->num_objects
;
952 stop_progress(&progress
);
955 if (fsck_finish(&fsck_obj_options
))
956 errors_found
|= ERROR_OBJECT
;
959 for (i
= 0; i
< argc
; i
++) {
960 const char *arg
= argv
[i
];
961 struct object_id oid
;
962 if (!get_oid(arg
, &oid
)) {
963 struct object
*obj
= lookup_object(the_repository
,
966 if (!obj
|| !(obj
->flags
& HAS_OBJ
)) {
967 if (is_promisor_object(&oid
))
969 error(_("%s: object missing"), oid_to_hex(&oid
));
970 errors_found
|= ERROR_OBJECT
;
975 fsck_put_object_name(&fsck_walk_options
, &oid
,
977 mark_object_reachable(obj
);
980 error(_("invalid parameter: expected sha1, got '%s'"), arg
);
981 errors_found
|= ERROR_OBJECT
;
985 * If we've not been given any explicit head information, do the
986 * default ones from .git/refs. We also consider the index file
987 * in this case (ie this implies --cache).
991 keep_cache_objects
= 1;
994 if (keep_cache_objects
) {
995 struct worktree
**worktrees
, **p
;
997 verify_index_checksum
= 1;
1000 worktrees
= get_worktrees();
1001 for (p
= worktrees
; *p
; p
++) {
1002 struct worktree
*wt
= *p
;
1003 struct index_state istate
=
1004 INDEX_STATE_INIT(the_repository
);
1008 * Make a copy since the buffer is reusable
1009 * and may get overwritten by other calls
1010 * while we're examining the index.
1012 path
= xstrdup(worktree_git_path(wt
, "index"));
1013 read_index_from(&istate
, path
, get_worktree_git_dir(wt
));
1014 fsck_index(&istate
, path
, wt
->is_current
);
1015 discard_index(&istate
);
1018 free_worktrees(worktrees
);
1021 check_connectivity();
1023 if (the_repository
->settings
.core_commit_graph
) {
1024 struct child_process commit_graph_verify
= CHILD_PROCESS_INIT
;
1026 prepare_alt_odb(the_repository
);
1027 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
) {
1028 child_process_init(&commit_graph_verify
);
1029 commit_graph_verify
.git_cmd
= 1;
1030 strvec_pushl(&commit_graph_verify
.args
, "commit-graph",
1031 "verify", "--object-dir", odb
->path
, NULL
);
1032 if (run_command(&commit_graph_verify
))
1033 errors_found
|= ERROR_COMMIT_GRAPH
;
1037 if (the_repository
->settings
.core_multi_pack_index
) {
1038 struct child_process midx_verify
= CHILD_PROCESS_INIT
;
1040 prepare_alt_odb(the_repository
);
1041 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
) {
1042 child_process_init(&midx_verify
);
1043 midx_verify
.git_cmd
= 1;
1044 strvec_pushl(&midx_verify
.args
, "multi-pack-index",
1045 "verify", "--object-dir", odb
->path
, NULL
);
1046 if (run_command(&midx_verify
))
1047 errors_found
|= ERROR_MULTI_PACK_INDEX
;
1051 return errors_found
;