1 #define USE_THE_INDEX_VARIABLE
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 "resolve-undo.h"
23 #include "run-command.h"
26 #define REACHABLE 0x0001
28 #define HAS_OBJ 0x0004
29 /* This flag is set if something points to this object. */
34 static int show_unreachable
;
35 static int include_reflogs
= 1;
36 static int check_full
= 1;
37 static int connectivity_only
;
38 static int check_strict
;
39 static int keep_cache_objects
;
40 static struct fsck_options fsck_walk_options
= FSCK_OPTIONS_DEFAULT
;
41 static struct fsck_options fsck_obj_options
= FSCK_OPTIONS_DEFAULT
;
42 static int errors_found
;
43 static int write_lost_and_found
;
45 static int show_progress
= -1;
46 static int show_dangling
= 1;
47 static int name_objects
;
48 #define ERROR_OBJECT 01
49 #define ERROR_REACHABLE 02
51 #define ERROR_REFS 010
52 #define ERROR_COMMIT_GRAPH 020
53 #define ERROR_MULTI_PACK_INDEX 040
55 static const char *describe_object(const struct object_id
*oid
)
57 return fsck_describe_object(&fsck_walk_options
, oid
);
60 static const char *printable_type(const struct object_id
*oid
,
61 enum object_type type
)
66 type
= oid_object_info(the_repository
, oid
, NULL
);
68 ret
= type_name(type
);
75 static int objerror(struct object
*obj
, const char *err
)
77 errors_found
|= ERROR_OBJECT
;
78 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
79 fprintf_ln(stderr
, _("error in %s %s: %s"),
80 printable_type(&obj
->oid
, obj
->type
),
81 describe_object(&obj
->oid
), err
);
85 static int fsck_error_func(struct fsck_options
*o
,
86 const struct object_id
*oid
,
87 enum object_type object_type
,
88 enum fsck_msg_type msg_type
,
89 enum fsck_msg_id msg_id
,
94 /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
95 fprintf_ln(stderr
, _("warning in %s %s: %s"),
96 printable_type(oid
, object_type
),
97 describe_object(oid
), message
);
100 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
101 fprintf_ln(stderr
, _("error in %s %s: %s"),
102 printable_type(oid
, object_type
),
103 describe_object(oid
), message
);
106 BUG("%d (FSCK_IGNORE?) should never trigger this callback",
111 static struct object_array pending
;
113 static int mark_object(struct object
*obj
, enum object_type type
,
114 void *data
, struct fsck_options
*options
)
116 struct object
*parent
= data
;
119 * The only case data is NULL or type is OBJ_ANY is when
120 * mark_object_reachable() calls us. All the callers of
121 * that function has non-NULL obj hence ...
124 /* ... these references to parent->fld are safe here */
125 printf_ln(_("broken link from %7s %s"),
126 printable_type(&parent
->oid
, parent
->type
),
127 describe_object(&parent
->oid
));
128 printf_ln(_("broken link from %7s %s"),
129 (type
== OBJ_ANY
? _("unknown") : type_name(type
)),
131 errors_found
|= ERROR_REACHABLE
;
135 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
136 /* ... and the reference to parent is safe here */
137 objerror(parent
, _("wrong object type in link"));
139 if (obj
->flags
& REACHABLE
)
141 obj
->flags
|= REACHABLE
;
143 if (is_promisor_object(&obj
->oid
))
145 * Further recursion does not need to be performed on this
146 * object since it is a promisor object (so it does not need to
147 * be added to "pending").
151 if (!(obj
->flags
& HAS_OBJ
)) {
152 if (parent
&& !has_object(the_repository
, &obj
->oid
, 1)) {
153 printf_ln(_("broken link from %7s %s\n"
155 printable_type(&parent
->oid
, parent
->type
),
156 describe_object(&parent
->oid
),
157 printable_type(&obj
->oid
, obj
->type
),
158 describe_object(&obj
->oid
));
159 errors_found
|= ERROR_REACHABLE
;
164 add_object_array(obj
, NULL
, &pending
);
168 static void mark_object_reachable(struct object
*obj
)
170 mark_object(obj
, OBJ_ANY
, NULL
, NULL
);
173 static int traverse_one_object(struct object
*obj
)
175 int result
= fsck_walk(obj
, obj
, &fsck_walk_options
);
177 if (obj
->type
== OBJ_TREE
) {
178 struct tree
*tree
= (struct tree
*)obj
;
179 free_tree_buffer(tree
);
184 static int traverse_reachable(void)
186 struct progress
*progress
= NULL
;
190 progress
= start_delayed_progress(_("Checking connectivity"), 0);
192 result
|= traverse_one_object(object_array_pop(&pending
));
193 display_progress(progress
, ++nr
);
195 stop_progress(&progress
);
199 static int mark_used(struct object
*obj
, enum object_type object_type
,
200 void *data
, struct fsck_options
*options
)
208 static void mark_unreachable_referents(const struct object_id
*oid
)
210 struct fsck_options options
= FSCK_OPTIONS_DEFAULT
;
211 struct object
*obj
= lookup_object(the_repository
, oid
);
213 if (!obj
|| !(obj
->flags
& HAS_OBJ
))
214 return; /* not part of our original set */
215 if (obj
->flags
& REACHABLE
)
216 return; /* reachable objects already traversed */
219 * Avoid passing OBJ_NONE to fsck_walk, which will parse the object
220 * (and we want to avoid parsing blobs).
222 if (obj
->type
== OBJ_NONE
) {
223 enum object_type type
= oid_object_info(the_repository
,
226 object_as_type(obj
, type
, 0);
229 options
.walk
= mark_used
;
230 fsck_walk(obj
, NULL
, &options
);
231 if (obj
->type
== OBJ_TREE
)
232 free_tree_buffer((struct tree
*)obj
);
235 static int mark_loose_unreachable_referents(const struct object_id
*oid
,
239 mark_unreachable_referents(oid
);
243 static int mark_packed_unreachable_referents(const struct object_id
*oid
,
244 struct packed_git
*pack
,
248 mark_unreachable_referents(oid
);
253 * Check a single reachable object
255 static void check_reachable_object(struct object
*obj
)
258 * We obviously want the object to be parsed,
259 * except if it was in a pack-file and we didn't
262 if (!(obj
->flags
& HAS_OBJ
)) {
263 if (is_promisor_object(&obj
->oid
))
265 if (has_object_pack(&obj
->oid
))
266 return; /* it is in pack - forget about it */
267 printf_ln(_("missing %s %s"),
268 printable_type(&obj
->oid
, obj
->type
),
269 describe_object(&obj
->oid
));
270 errors_found
|= ERROR_REACHABLE
;
276 * Check a single unreachable object
278 static void check_unreachable_object(struct object
*obj
)
281 * Missing unreachable object? Ignore it. It's not like
282 * we miss it (since it can't be reached), nor do we want
283 * to complain about it being unreachable (since it does
286 if (!(obj
->flags
& HAS_OBJ
))
290 * Unreachable object that exists? Show it if asked to,
291 * since this is something that is prunable.
293 if (show_unreachable
) {
294 printf_ln(_("unreachable %s %s"),
295 printable_type(&obj
->oid
, obj
->type
),
296 describe_object(&obj
->oid
));
301 * "!USED" means that nothing at all points to it, including
302 * other unreachable objects. In other words, it's the "tip"
303 * of some set of unreachable objects, usually a commit that
306 * Such starting points are more interesting than some random
307 * set of unreachable objects, so we show them even if the user
308 * hasn't asked for _all_ unreachable objects. If you have
309 * deleted a branch by mistake, this is a prime candidate to
310 * start looking at, for example.
312 if (!(obj
->flags
& USED
)) {
314 printf_ln(_("dangling %s %s"),
315 printable_type(&obj
->oid
, obj
->type
),
316 describe_object(&obj
->oid
));
317 if (write_lost_and_found
) {
318 char *filename
= git_pathdup("lost-found/%s/%s",
319 obj
->type
== OBJ_COMMIT
? "commit" : "other",
320 describe_object(&obj
->oid
));
323 if (safe_create_leading_directories_const(filename
)) {
324 error(_("could not create lost-found"));
328 f
= xfopen(filename
, "w");
329 if (obj
->type
== OBJ_BLOB
) {
330 if (stream_blob_to_fd(fileno(f
), &obj
->oid
, NULL
, 1))
331 die_errno(_("could not write '%s'"), filename
);
333 fprintf(f
, "%s\n", describe_object(&obj
->oid
));
335 die_errno(_("could not finish '%s'"),
343 * Otherwise? It's there, it's unreachable, and some other unreachable
344 * object points to it. Ignore it - it's not interesting, and we showed
345 * all the interesting cases above.
349 static void check_object(struct object
*obj
)
352 fprintf_ln(stderr
, _("Checking %s"), describe_object(&obj
->oid
));
354 if (obj
->flags
& REACHABLE
)
355 check_reachable_object(obj
);
357 check_unreachable_object(obj
);
360 static void check_connectivity(void)
364 /* Traverse the pending reachable objects */
365 traverse_reachable();
368 * With --connectivity-only, we won't have actually opened and marked
369 * unreachable objects with USED. Do that now to make --dangling, etc
372 if (connectivity_only
&& (show_dangling
|| write_lost_and_found
)) {
374 * Even though we already have a "struct object" for each of
375 * these in memory, we must not iterate over the internal
376 * object hash as we do below. Our loop would potentially
377 * resize the hash, making our iteration invalid.
379 * Instead, we'll just go back to the source list of objects,
380 * and ignore any that weren't present in our earlier
383 for_each_loose_object(mark_loose_unreachable_referents
, NULL
, 0);
384 for_each_packed_object(mark_packed_unreachable_referents
, NULL
, 0);
387 /* Look up all the requirements, warn about missing objects.. */
388 max
= get_max_object_index();
390 fprintf_ln(stderr
, _("Checking connectivity (%d objects)"), max
);
392 for (i
= 0; i
< max
; i
++) {
393 struct object
*obj
= get_indexed_object(i
);
400 static int fsck_obj(struct object
*obj
, void *buffer
, unsigned long size
)
404 if (obj
->flags
& SEEN
)
409 fprintf_ln(stderr
, _("Checking %s %s"),
410 printable_type(&obj
->oid
, obj
->type
),
411 describe_object(&obj
->oid
));
413 if (fsck_walk(obj
, NULL
, &fsck_obj_options
))
414 objerror(obj
, _("broken links"));
415 err
= fsck_object(obj
, buffer
, size
, &fsck_obj_options
);
419 if (obj
->type
== OBJ_COMMIT
) {
420 struct commit
*commit
= (struct commit
*) obj
;
422 if (!commit
->parents
&& show_root
)
423 printf_ln(_("root %s"),
424 describe_object(&commit
->object
.oid
));
427 if (obj
->type
== OBJ_TAG
) {
428 struct tag
*tag
= (struct tag
*) obj
;
430 if (show_tags
&& tag
->tagged
) {
431 printf_ln(_("tagged %s %s (%s) in %s"),
432 printable_type(&tag
->tagged
->oid
, tag
->tagged
->type
),
433 describe_object(&tag
->tagged
->oid
),
435 describe_object(&tag
->object
.oid
));
440 if (obj
->type
== OBJ_TREE
)
441 free_tree_buffer((struct tree
*)obj
);
445 static int fsck_obj_buffer(const struct object_id
*oid
, enum object_type type
,
446 unsigned long size
, void *buffer
, int *eaten
)
449 * Note, buffer may be NULL if type is OBJ_BLOB. See
450 * verify_packfile(), data_valid variable for details.
453 obj
= parse_object_buffer(the_repository
, oid
, type
, size
, buffer
,
456 errors_found
|= ERROR_OBJECT
;
457 return error(_("%s: object corrupt or missing"),
460 obj
->flags
&= ~(REACHABLE
| SEEN
);
461 obj
->flags
|= HAS_OBJ
;
462 return fsck_obj(obj
, buffer
, size
);
465 static int default_refs
;
467 static void fsck_handle_reflog_oid(const char *refname
, struct object_id
*oid
,
468 timestamp_t timestamp
)
472 if (!is_null_oid(oid
)) {
473 obj
= lookup_object(the_repository
, oid
);
474 if (obj
&& (obj
->flags
& HAS_OBJ
)) {
476 fsck_put_object_name(&fsck_walk_options
, oid
,
480 mark_object_reachable(obj
);
481 } else if (!is_promisor_object(oid
)) {
482 error(_("%s: invalid reflog entry %s"),
483 refname
, oid_to_hex(oid
));
484 errors_found
|= ERROR_REACHABLE
;
489 static int fsck_handle_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
490 const char *email UNUSED
,
491 timestamp_t timestamp
, int tz UNUSED
,
492 const char *message UNUSED
, void *cb_data
)
494 const char *refname
= cb_data
;
497 fprintf_ln(stderr
, _("Checking reflog %s->%s"),
498 oid_to_hex(ooid
), oid_to_hex(noid
));
500 fsck_handle_reflog_oid(refname
, ooid
, 0);
501 fsck_handle_reflog_oid(refname
, noid
, timestamp
);
505 static int fsck_handle_reflog(const char *logname
,
506 const struct object_id
*oid UNUSED
,
507 int flag UNUSED
, void *cb_data
)
509 struct strbuf refname
= STRBUF_INIT
;
511 strbuf_worktree_ref(cb_data
, &refname
, logname
);
512 for_each_reflog_ent(refname
.buf
, fsck_handle_reflog_ent
, refname
.buf
);
513 strbuf_release(&refname
);
517 static int fsck_handle_ref(const char *refname
, const struct object_id
*oid
,
518 int flag UNUSED
, void *cb_data UNUSED
)
522 obj
= parse_object(the_repository
, oid
);
524 if (is_promisor_object(oid
)) {
526 * Increment default_refs anyway, because this is a
532 error(_("%s: invalid sha1 pointer %s"),
533 refname
, oid_to_hex(oid
));
534 errors_found
|= ERROR_REACHABLE
;
535 /* We'll continue with the rest despite the error.. */
538 if (obj
->type
!= OBJ_COMMIT
&& is_branch(refname
)) {
539 error(_("%s: not a commit"), refname
);
540 errors_found
|= ERROR_REFS
;
544 fsck_put_object_name(&fsck_walk_options
,
546 mark_object_reachable(obj
);
551 static int fsck_head_link(const char *head_ref_name
,
552 const char **head_points_at
,
553 struct object_id
*head_oid
);
555 static void get_default_heads(void)
557 struct worktree
**worktrees
, **p
;
558 const char *head_points_at
;
559 struct object_id head_oid
;
561 for_each_rawref(fsck_handle_ref
, NULL
);
563 worktrees
= get_worktrees();
564 for (p
= worktrees
; *p
; p
++) {
565 struct worktree
*wt
= *p
;
566 struct strbuf ref
= STRBUF_INIT
;
568 strbuf_worktree_ref(wt
, &ref
, "HEAD");
569 fsck_head_link(ref
.buf
, &head_points_at
, &head_oid
);
570 if (head_points_at
&& !is_null_oid(&head_oid
))
571 fsck_handle_ref(ref
.buf
, &head_oid
, 0, NULL
);
572 strbuf_release(&ref
);
575 refs_for_each_reflog(get_worktree_ref_store(wt
),
576 fsck_handle_reflog
, wt
);
578 free_worktrees(worktrees
);
581 * Not having any default heads isn't really fatal, but
582 * it does mean that "--unreachable" no longer makes any
583 * sense (since in this case everything will obviously
584 * be unreachable by definition.
586 * Showing dangling objects is valid, though (as those
587 * dangling objects are likely lost heads).
589 * So we just print a warning about it, and clear the
590 * "show_unreachable" flag.
593 fprintf_ln(stderr
, _("notice: No default references"));
594 show_unreachable
= 0;
598 struct for_each_loose_cb
600 struct progress
*progress
;
601 struct strbuf obj_type
;
604 static int fsck_loose(const struct object_id
*oid
, const char *path
, void *data
)
606 struct for_each_loose_cb
*cb_data
= data
;
608 enum object_type type
= OBJ_NONE
;
610 void *contents
= NULL
;
612 struct object_info oi
= OBJECT_INFO_INIT
;
613 struct object_id real_oid
= *null_oid();
616 strbuf_reset(&cb_data
->obj_type
);
617 oi
.type_name
= &cb_data
->obj_type
;
621 if (read_loose_object(path
, oid
, &real_oid
, &contents
, &oi
) < 0) {
622 if (contents
&& !oideq(&real_oid
, oid
))
623 err
= error(_("%s: hash-path mismatch, found at: %s"),
624 oid_to_hex(&real_oid
), path
);
626 err
= error(_("%s: object corrupt or missing: %s"),
627 oid_to_hex(oid
), path
);
629 if (type
!= OBJ_NONE
&& type
< 0)
630 err
= error(_("%s: object is of unknown type '%s': %s"),
631 oid_to_hex(&real_oid
), cb_data
->obj_type
.buf
,
634 errors_found
|= ERROR_OBJECT
;
636 return 0; /* keep checking other objects */
639 if (!contents
&& type
!= OBJ_BLOB
)
640 BUG("read_loose_object streamed a non-blob");
642 obj
= parse_object_buffer(the_repository
, oid
, type
, size
,
646 errors_found
|= ERROR_OBJECT
;
647 error(_("%s: object could not be parsed: %s"),
648 oid_to_hex(oid
), path
);
651 return 0; /* keep checking other objects */
654 obj
->flags
&= ~(REACHABLE
| SEEN
);
655 obj
->flags
|= HAS_OBJ
;
656 if (fsck_obj(obj
, contents
, size
))
657 errors_found
|= ERROR_OBJECT
;
661 return 0; /* keep checking other objects, even if we saw an error */
664 static int fsck_cruft(const char *basename
, const char *path
, void *data
)
666 if (!starts_with(basename
, "tmp_obj_"))
667 fprintf_ln(stderr
, _("bad sha1 file: %s"), path
);
671 static int fsck_subdir(unsigned int nr
, const char *path
, void *data
)
673 struct for_each_loose_cb
*cb_data
= data
;
674 struct progress
*progress
= cb_data
->progress
;
675 display_progress(progress
, nr
+ 1);
679 static void fsck_object_dir(const char *path
)
681 struct progress
*progress
= NULL
;
682 struct for_each_loose_cb cb_data
= {
683 .obj_type
= STRBUF_INIT
,
684 .progress
= progress
,
688 fprintf_ln(stderr
, _("Checking object directory"));
691 progress
= start_progress(_("Checking object directories"), 256);
693 for_each_loose_file_in_objdir(path
, fsck_loose
, fsck_cruft
, fsck_subdir
,
695 display_progress(progress
, 256);
696 stop_progress(&progress
);
697 strbuf_release(&cb_data
.obj_type
);
700 static int fsck_head_link(const char *head_ref_name
,
701 const char **head_points_at
,
702 struct object_id
*head_oid
)
704 int null_is_error
= 0;
707 fprintf_ln(stderr
, _("Checking %s link"), head_ref_name
);
709 *head_points_at
= resolve_ref_unsafe(head_ref_name
, 0, head_oid
, NULL
);
710 if (!*head_points_at
) {
711 errors_found
|= ERROR_REFS
;
712 return error(_("invalid %s"), head_ref_name
);
714 if (!strcmp(*head_points_at
, head_ref_name
))
717 else if (!starts_with(*head_points_at
, "refs/heads/")) {
718 errors_found
|= ERROR_REFS
;
719 return error(_("%s points to something strange (%s)"),
720 head_ref_name
, *head_points_at
);
722 if (is_null_oid(head_oid
)) {
724 errors_found
|= ERROR_REFS
;
725 return error(_("%s: detached HEAD points at nothing"),
729 _("notice: %s points to an unborn branch (%s)"),
730 head_ref_name
, *head_points_at
+ 11);
735 static int fsck_cache_tree(struct cache_tree
*it
)
741 fprintf_ln(stderr
, _("Checking cache tree"));
743 if (0 <= it
->entry_count
) {
744 struct object
*obj
= parse_object(the_repository
, &it
->oid
);
746 error(_("%s: invalid sha1 pointer in cache-tree"),
747 oid_to_hex(&it
->oid
));
748 errors_found
|= ERROR_REFS
;
752 fsck_put_object_name(&fsck_walk_options
, &it
->oid
, ":");
753 mark_object_reachable(obj
);
754 if (obj
->type
!= OBJ_TREE
)
755 err
|= objerror(obj
, _("non-tree in cache-tree"));
757 for (i
= 0; i
< it
->subtree_nr
; i
++)
758 err
|= fsck_cache_tree(it
->down
[i
]->cache_tree
);
762 static int fsck_resolve_undo(struct index_state
*istate
)
764 struct string_list_item
*item
;
765 struct string_list
*resolve_undo
= istate
->resolve_undo
;
770 for_each_string_list_item(item
, resolve_undo
) {
771 const char *path
= item
->string
;
772 struct resolve_undo_info
*ru
= item
->util
;
777 for (i
= 0; i
< 3; i
++) {
780 if (!ru
->mode
[i
] || !S_ISREG(ru
->mode
[i
]))
783 obj
= parse_object(the_repository
, &ru
->oid
[i
]);
785 error(_("%s: invalid sha1 pointer in resolve-undo"),
786 oid_to_hex(&ru
->oid
[i
]));
787 errors_found
|= ERROR_REFS
;
791 fsck_put_object_name(&fsck_walk_options
, &ru
->oid
[i
],
792 ":(%d):%s", i
, path
);
793 mark_object_reachable(obj
);
799 static void mark_object_for_connectivity(const struct object_id
*oid
)
801 struct object
*obj
= lookup_unknown_object(the_repository
, oid
);
802 obj
->flags
|= HAS_OBJ
;
805 static int mark_loose_for_connectivity(const struct object_id
*oid
,
809 mark_object_for_connectivity(oid
);
813 static int mark_packed_for_connectivity(const struct object_id
*oid
,
814 struct packed_git
*pack
,
818 mark_object_for_connectivity(oid
);
822 static char const * const fsck_usage
[] = {
823 N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n"
824 " [--[no-]full] [--strict] [--verbose] [--lost-found]\n"
825 " [--[no-]dangling] [--[no-]progress] [--connectivity-only]\n"
826 " [--[no-]name-objects] [<object>...]"),
830 static struct option fsck_opts
[] = {
831 OPT__VERBOSE(&verbose
, N_("be verbose")),
832 OPT_BOOL(0, "unreachable", &show_unreachable
, N_("show unreachable objects")),
833 OPT_BOOL(0, "dangling", &show_dangling
, N_("show dangling objects")),
834 OPT_BOOL(0, "tags", &show_tags
, N_("report tags")),
835 OPT_BOOL(0, "root", &show_root
, N_("report root nodes")),
836 OPT_BOOL(0, "cache", &keep_cache_objects
, N_("make index objects head nodes")),
837 OPT_BOOL(0, "reflogs", &include_reflogs
, N_("make reflogs head nodes (default)")),
838 OPT_BOOL(0, "full", &check_full
, N_("also consider packs and alternate objects")),
839 OPT_BOOL(0, "connectivity-only", &connectivity_only
, N_("check only connectivity")),
840 OPT_BOOL(0, "strict", &check_strict
, N_("enable more strict checking")),
841 OPT_BOOL(0, "lost-found", &write_lost_and_found
,
842 N_("write dangling objects in .git/lost-found")),
843 OPT_BOOL(0, "progress", &show_progress
, N_("show progress")),
844 OPT_BOOL(0, "name-objects", &name_objects
, N_("show verbose names for reachable objects")),
848 int cmd_fsck(int argc
, const char **argv
, const char *prefix
)
851 struct object_directory
*odb
;
853 /* fsck knows how to handle missing promisor objects */
854 fetch_if_missing
= 0;
857 read_replace_refs
= 0;
858 save_commit_buffer
= 0;
860 argc
= parse_options(argc
, argv
, prefix
, fsck_opts
, fsck_usage
, 0);
862 fsck_walk_options
.walk
= mark_object
;
863 fsck_obj_options
.walk
= mark_used
;
864 fsck_obj_options
.error_func
= fsck_error_func
;
866 fsck_obj_options
.strict
= 1;
868 if (show_progress
== -1)
869 show_progress
= isatty(2);
873 if (write_lost_and_found
) {
879 fsck_enable_object_names(&fsck_walk_options
);
881 git_config(git_fsck_config
, &fsck_obj_options
);
882 prepare_repo_settings(the_repository
);
884 if (connectivity_only
) {
885 for_each_loose_object(mark_loose_for_connectivity
, NULL
, 0);
886 for_each_packed_object(mark_packed_for_connectivity
, NULL
, 0);
888 prepare_alt_odb(the_repository
);
889 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
)
890 fsck_object_dir(odb
->path
);
893 struct packed_git
*p
;
894 uint32_t total
= 0, count
= 0;
895 struct progress
*progress
= NULL
;
898 for (p
= get_all_packs(the_repository
); p
;
900 if (open_pack_index(p
))
902 total
+= p
->num_objects
;
905 progress
= start_progress(_("Checking objects"), total
);
907 for (p
= get_all_packs(the_repository
); p
;
909 /* verify gives error messages itself */
910 if (verify_pack(the_repository
,
913 errors_found
|= ERROR_PACK
;
914 count
+= p
->num_objects
;
916 stop_progress(&progress
);
919 if (fsck_finish(&fsck_obj_options
))
920 errors_found
|= ERROR_OBJECT
;
923 for (i
= 0; i
< argc
; i
++) {
924 const char *arg
= argv
[i
];
925 struct object_id oid
;
926 if (!get_oid(arg
, &oid
)) {
927 struct object
*obj
= lookup_object(the_repository
,
930 if (!obj
|| !(obj
->flags
& HAS_OBJ
)) {
931 if (is_promisor_object(&oid
))
933 error(_("%s: object missing"), oid_to_hex(&oid
));
934 errors_found
|= ERROR_OBJECT
;
939 fsck_put_object_name(&fsck_walk_options
, &oid
,
941 mark_object_reachable(obj
);
944 error(_("invalid parameter: expected sha1, got '%s'"), arg
);
945 errors_found
|= ERROR_OBJECT
;
949 * If we've not been given any explicit head information, do the
950 * default ones from .git/refs. We also consider the index file
951 * in this case (ie this implies --cache).
955 keep_cache_objects
= 1;
958 if (keep_cache_objects
) {
959 verify_index_checksum
= 1;
961 repo_read_index(the_repository
);
962 /* TODO: audit for interaction with sparse-index. */
963 ensure_full_index(&the_index
);
964 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
969 mode
= the_index
.cache
[i
]->ce_mode
;
970 if (S_ISGITLINK(mode
))
972 blob
= lookup_blob(the_repository
,
973 &the_index
.cache
[i
]->oid
);
978 fsck_put_object_name(&fsck_walk_options
, &obj
->oid
,
979 ":%s", the_index
.cache
[i
]->name
);
980 mark_object_reachable(obj
);
982 if (the_index
.cache_tree
)
983 fsck_cache_tree(the_index
.cache_tree
);
984 fsck_resolve_undo(&the_index
);
987 check_connectivity();
989 if (the_repository
->settings
.core_commit_graph
) {
990 struct child_process commit_graph_verify
= CHILD_PROCESS_INIT
;
992 prepare_alt_odb(the_repository
);
993 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
) {
994 child_process_init(&commit_graph_verify
);
995 commit_graph_verify
.git_cmd
= 1;
996 strvec_pushl(&commit_graph_verify
.args
, "commit-graph",
997 "verify", "--object-dir", odb
->path
, NULL
);
998 if (run_command(&commit_graph_verify
))
999 errors_found
|= ERROR_COMMIT_GRAPH
;
1003 if (the_repository
->settings
.core_multi_pack_index
) {
1004 struct child_process midx_verify
= CHILD_PROCESS_INIT
;
1006 prepare_alt_odb(the_repository
);
1007 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
) {
1008 child_process_init(&midx_verify
);
1009 midx_verify
.git_cmd
= 1;
1010 strvec_pushl(&midx_verify
.args
, "multi-pack-index",
1011 "verify", "--object-dir", odb
->path
, NULL
);
1012 if (run_command(&midx_verify
))
1013 errors_found
|= ERROR_MULTI_PACK_INDEX
;
1017 return errors_found
;