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-file.h"
23 #include "object-name.h"
24 #include "object-store.h"
25 #include "replace-object.h"
26 #include "resolve-undo.h"
27 #include "run-command.h"
30 #define REACHABLE 0x0001
32 #define HAS_OBJ 0x0004
33 /* This flag is set if something points to this object. */
38 static int show_unreachable
;
39 static int include_reflogs
= 1;
40 static int check_full
= 1;
41 static int connectivity_only
;
42 static int check_strict
;
43 static int keep_cache_objects
;
44 static struct fsck_options fsck_walk_options
= FSCK_OPTIONS_DEFAULT
;
45 static struct fsck_options fsck_obj_options
= FSCK_OPTIONS_DEFAULT
;
46 static int errors_found
;
47 static int write_lost_and_found
;
49 static int show_progress
= -1;
50 static int show_dangling
= 1;
51 static int name_objects
;
52 #define ERROR_OBJECT 01
53 #define ERROR_REACHABLE 02
55 #define ERROR_REFS 010
56 #define ERROR_COMMIT_GRAPH 020
57 #define ERROR_MULTI_PACK_INDEX 040
59 static const char *describe_object(const struct object_id
*oid
)
61 return fsck_describe_object(&fsck_walk_options
, oid
);
64 static const char *printable_type(const struct object_id
*oid
,
65 enum object_type type
)
70 type
= oid_object_info(the_repository
, oid
, NULL
);
72 ret
= type_name(type
);
79 static int objerror(struct object
*obj
, const char *err
)
81 errors_found
|= ERROR_OBJECT
;
82 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
83 fprintf_ln(stderr
, _("error in %s %s: %s"),
84 printable_type(&obj
->oid
, obj
->type
),
85 describe_object(&obj
->oid
), err
);
89 static int fsck_error_func(struct fsck_options
*o
,
90 const struct object_id
*oid
,
91 enum object_type object_type
,
92 enum fsck_msg_type msg_type
,
93 enum fsck_msg_id msg_id
,
98 /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
99 fprintf_ln(stderr
, _("warning in %s %s: %s"),
100 printable_type(oid
, object_type
),
101 describe_object(oid
), message
);
104 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
105 fprintf_ln(stderr
, _("error in %s %s: %s"),
106 printable_type(oid
, object_type
),
107 describe_object(oid
), message
);
110 BUG("%d (FSCK_IGNORE?) should never trigger this callback",
115 static struct object_array pending
;
117 static int mark_object(struct object
*obj
, enum object_type type
,
118 void *data
, struct fsck_options
*options
)
120 struct object
*parent
= data
;
123 * The only case data is NULL or type is OBJ_ANY is when
124 * mark_object_reachable() calls us. All the callers of
125 * that function has non-NULL obj hence ...
128 /* ... these references to parent->fld are safe here */
129 printf_ln(_("broken link from %7s %s"),
130 printable_type(&parent
->oid
, parent
->type
),
131 describe_object(&parent
->oid
));
132 printf_ln(_("broken link from %7s %s"),
133 (type
== OBJ_ANY
? _("unknown") : type_name(type
)),
135 errors_found
|= ERROR_REACHABLE
;
139 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
140 /* ... and the reference to parent is safe here */
141 objerror(parent
, _("wrong object type in link"));
143 if (obj
->flags
& REACHABLE
)
145 obj
->flags
|= REACHABLE
;
147 if (is_promisor_object(&obj
->oid
))
149 * Further recursion does not need to be performed on this
150 * object since it is a promisor object (so it does not need to
151 * be added to "pending").
155 if (!(obj
->flags
& HAS_OBJ
)) {
156 if (parent
&& !has_object(the_repository
, &obj
->oid
, 1)) {
157 printf_ln(_("broken link from %7s %s\n"
159 printable_type(&parent
->oid
, parent
->type
),
160 describe_object(&parent
->oid
),
161 printable_type(&obj
->oid
, obj
->type
),
162 describe_object(&obj
->oid
));
163 errors_found
|= ERROR_REACHABLE
;
168 add_object_array(obj
, NULL
, &pending
);
172 static void mark_object_reachable(struct object
*obj
)
174 mark_object(obj
, OBJ_ANY
, NULL
, NULL
);
177 static int traverse_one_object(struct object
*obj
)
179 int result
= fsck_walk(obj
, obj
, &fsck_walk_options
);
181 if (obj
->type
== OBJ_TREE
) {
182 struct tree
*tree
= (struct tree
*)obj
;
183 free_tree_buffer(tree
);
188 static int traverse_reachable(void)
190 struct progress
*progress
= NULL
;
194 progress
= start_delayed_progress(_("Checking connectivity"), 0);
196 result
|= traverse_one_object(object_array_pop(&pending
));
197 display_progress(progress
, ++nr
);
199 stop_progress(&progress
);
203 static int mark_used(struct object
*obj
, enum object_type object_type
,
204 void *data
, struct fsck_options
*options
)
212 static void mark_unreachable_referents(const struct object_id
*oid
)
214 struct fsck_options options
= FSCK_OPTIONS_DEFAULT
;
215 struct object
*obj
= lookup_object(the_repository
, oid
);
217 if (!obj
|| !(obj
->flags
& HAS_OBJ
))
218 return; /* not part of our original set */
219 if (obj
->flags
& REACHABLE
)
220 return; /* reachable objects already traversed */
223 * Avoid passing OBJ_NONE to fsck_walk, which will parse the object
224 * (and we want to avoid parsing blobs).
226 if (obj
->type
== OBJ_NONE
) {
227 enum object_type type
= oid_object_info(the_repository
,
230 object_as_type(obj
, type
, 0);
233 options
.walk
= mark_used
;
234 fsck_walk(obj
, NULL
, &options
);
235 if (obj
->type
== OBJ_TREE
)
236 free_tree_buffer((struct tree
*)obj
);
239 static int mark_loose_unreachable_referents(const struct object_id
*oid
,
240 const char *path UNUSED
,
243 mark_unreachable_referents(oid
);
247 static int mark_packed_unreachable_referents(const struct object_id
*oid
,
248 struct packed_git
*pack UNUSED
,
252 mark_unreachable_referents(oid
);
257 * Check a single reachable object
259 static void check_reachable_object(struct object
*obj
)
262 * We obviously want the object to be parsed,
263 * except if it was in a pack-file and we didn't
266 if (!(obj
->flags
& HAS_OBJ
)) {
267 if (is_promisor_object(&obj
->oid
))
269 if (has_object_pack(&obj
->oid
))
270 return; /* it is in pack - forget about it */
271 printf_ln(_("missing %s %s"),
272 printable_type(&obj
->oid
, obj
->type
),
273 describe_object(&obj
->oid
));
274 errors_found
|= ERROR_REACHABLE
;
280 * Check a single unreachable object
282 static void check_unreachable_object(struct object
*obj
)
285 * Missing unreachable object? Ignore it. It's not like
286 * we miss it (since it can't be reached), nor do we want
287 * to complain about it being unreachable (since it does
290 if (!(obj
->flags
& HAS_OBJ
))
294 * Unreachable object that exists? Show it if asked to,
295 * since this is something that is prunable.
297 if (show_unreachable
) {
298 printf_ln(_("unreachable %s %s"),
299 printable_type(&obj
->oid
, obj
->type
),
300 describe_object(&obj
->oid
));
305 * "!USED" means that nothing at all points to it, including
306 * other unreachable objects. In other words, it's the "tip"
307 * of some set of unreachable objects, usually a commit that
310 * Such starting points are more interesting than some random
311 * set of unreachable objects, so we show them even if the user
312 * hasn't asked for _all_ unreachable objects. If you have
313 * deleted a branch by mistake, this is a prime candidate to
314 * start looking at, for example.
316 if (!(obj
->flags
& USED
)) {
318 printf_ln(_("dangling %s %s"),
319 printable_type(&obj
->oid
, obj
->type
),
320 describe_object(&obj
->oid
));
321 if (write_lost_and_found
) {
322 char *filename
= git_pathdup("lost-found/%s/%s",
323 obj
->type
== OBJ_COMMIT
? "commit" : "other",
324 describe_object(&obj
->oid
));
327 if (safe_create_leading_directories_const(filename
)) {
328 error(_("could not create lost-found"));
332 f
= xfopen(filename
, "w");
333 if (obj
->type
== OBJ_BLOB
) {
334 if (stream_blob_to_fd(fileno(f
), &obj
->oid
, NULL
, 1))
335 die_errno(_("could not write '%s'"), filename
);
337 fprintf(f
, "%s\n", describe_object(&obj
->oid
));
339 die_errno(_("could not finish '%s'"),
347 * Otherwise? It's there, it's unreachable, and some other unreachable
348 * object points to it. Ignore it - it's not interesting, and we showed
349 * all the interesting cases above.
353 static void check_object(struct object
*obj
)
356 fprintf_ln(stderr
, _("Checking %s"), describe_object(&obj
->oid
));
358 if (obj
->flags
& REACHABLE
)
359 check_reachable_object(obj
);
361 check_unreachable_object(obj
);
364 static void check_connectivity(void)
368 /* Traverse the pending reachable objects */
369 traverse_reachable();
372 * With --connectivity-only, we won't have actually opened and marked
373 * unreachable objects with USED. Do that now to make --dangling, etc
376 if (connectivity_only
&& (show_dangling
|| write_lost_and_found
)) {
378 * Even though we already have a "struct object" for each of
379 * these in memory, we must not iterate over the internal
380 * object hash as we do below. Our loop would potentially
381 * resize the hash, making our iteration invalid.
383 * Instead, we'll just go back to the source list of objects,
384 * and ignore any that weren't present in our earlier
387 for_each_loose_object(mark_loose_unreachable_referents
, NULL
, 0);
388 for_each_packed_object(mark_packed_unreachable_referents
, NULL
, 0);
391 /* Look up all the requirements, warn about missing objects.. */
392 max
= get_max_object_index();
394 fprintf_ln(stderr
, _("Checking connectivity (%d objects)"), max
);
396 for (i
= 0; i
< max
; i
++) {
397 struct object
*obj
= get_indexed_object(i
);
404 static int fsck_obj(struct object
*obj
, void *buffer
, unsigned long size
)
408 if (obj
->flags
& SEEN
)
413 fprintf_ln(stderr
, _("Checking %s %s"),
414 printable_type(&obj
->oid
, obj
->type
),
415 describe_object(&obj
->oid
));
417 if (fsck_walk(obj
, NULL
, &fsck_obj_options
))
418 objerror(obj
, _("broken links"));
419 err
= fsck_object(obj
, buffer
, size
, &fsck_obj_options
);
423 if (obj
->type
== OBJ_COMMIT
) {
424 struct commit
*commit
= (struct commit
*) obj
;
426 if (!commit
->parents
&& show_root
)
427 printf_ln(_("root %s"),
428 describe_object(&commit
->object
.oid
));
431 if (obj
->type
== OBJ_TAG
) {
432 struct tag
*tag
= (struct tag
*) obj
;
434 if (show_tags
&& tag
->tagged
) {
435 printf_ln(_("tagged %s %s (%s) in %s"),
436 printable_type(&tag
->tagged
->oid
, tag
->tagged
->type
),
437 describe_object(&tag
->tagged
->oid
),
439 describe_object(&tag
->object
.oid
));
444 if (obj
->type
== OBJ_TREE
)
445 free_tree_buffer((struct tree
*)obj
);
449 static int fsck_obj_buffer(const struct object_id
*oid
, enum object_type type
,
450 unsigned long size
, void *buffer
, int *eaten
)
453 * Note, buffer may be NULL if type is OBJ_BLOB. See
454 * verify_packfile(), data_valid variable for details.
457 obj
= parse_object_buffer(the_repository
, oid
, type
, size
, buffer
,
460 errors_found
|= ERROR_OBJECT
;
461 return error(_("%s: object corrupt or missing"),
464 obj
->flags
&= ~(REACHABLE
| SEEN
);
465 obj
->flags
|= HAS_OBJ
;
466 return fsck_obj(obj
, buffer
, size
);
469 static int default_refs
;
471 static void fsck_handle_reflog_oid(const char *refname
, struct object_id
*oid
,
472 timestamp_t timestamp
)
476 if (!is_null_oid(oid
)) {
477 obj
= lookup_object(the_repository
, oid
);
478 if (obj
&& (obj
->flags
& HAS_OBJ
)) {
480 fsck_put_object_name(&fsck_walk_options
, oid
,
484 mark_object_reachable(obj
);
485 } else if (!is_promisor_object(oid
)) {
486 error(_("%s: invalid reflog entry %s"),
487 refname
, oid_to_hex(oid
));
488 errors_found
|= ERROR_REACHABLE
;
493 static int fsck_handle_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
494 const char *email UNUSED
,
495 timestamp_t timestamp
, int tz UNUSED
,
496 const char *message UNUSED
, void *cb_data
)
498 const char *refname
= cb_data
;
501 fprintf_ln(stderr
, _("Checking reflog %s->%s"),
502 oid_to_hex(ooid
), oid_to_hex(noid
));
504 fsck_handle_reflog_oid(refname
, ooid
, 0);
505 fsck_handle_reflog_oid(refname
, noid
, timestamp
);
509 static int fsck_handle_reflog(const char *logname
,
510 const struct object_id
*oid UNUSED
,
511 int flag UNUSED
, void *cb_data
)
513 struct strbuf refname
= STRBUF_INIT
;
515 strbuf_worktree_ref(cb_data
, &refname
, logname
);
516 for_each_reflog_ent(refname
.buf
, fsck_handle_reflog_ent
, refname
.buf
);
517 strbuf_release(&refname
);
521 static int fsck_handle_ref(const char *refname
, const struct object_id
*oid
,
522 int flag UNUSED
, void *cb_data UNUSED
)
526 obj
= parse_object(the_repository
, oid
);
528 if (is_promisor_object(oid
)) {
530 * Increment default_refs anyway, because this is a
536 error(_("%s: invalid sha1 pointer %s"),
537 refname
, oid_to_hex(oid
));
538 errors_found
|= ERROR_REACHABLE
;
539 /* We'll continue with the rest despite the error.. */
542 if (obj
->type
!= OBJ_COMMIT
&& is_branch(refname
)) {
543 error(_("%s: not a commit"), refname
);
544 errors_found
|= ERROR_REFS
;
548 fsck_put_object_name(&fsck_walk_options
,
550 mark_object_reachable(obj
);
555 static int fsck_head_link(const char *head_ref_name
,
556 const char **head_points_at
,
557 struct object_id
*head_oid
);
559 static void get_default_heads(void)
561 struct worktree
**worktrees
, **p
;
562 const char *head_points_at
;
563 struct object_id head_oid
;
565 for_each_rawref(fsck_handle_ref
, NULL
);
567 worktrees
= get_worktrees();
568 for (p
= worktrees
; *p
; p
++) {
569 struct worktree
*wt
= *p
;
570 struct strbuf ref
= STRBUF_INIT
;
572 strbuf_worktree_ref(wt
, &ref
, "HEAD");
573 fsck_head_link(ref
.buf
, &head_points_at
, &head_oid
);
574 if (head_points_at
&& !is_null_oid(&head_oid
))
575 fsck_handle_ref(ref
.buf
, &head_oid
, 0, NULL
);
576 strbuf_release(&ref
);
579 refs_for_each_reflog(get_worktree_ref_store(wt
),
580 fsck_handle_reflog
, wt
);
582 free_worktrees(worktrees
);
585 * Not having any default heads isn't really fatal, but
586 * it does mean that "--unreachable" no longer makes any
587 * sense (since in this case everything will obviously
588 * be unreachable by definition.
590 * Showing dangling objects is valid, though (as those
591 * dangling objects are likely lost heads).
593 * So we just print a warning about it, and clear the
594 * "show_unreachable" flag.
597 fprintf_ln(stderr
, _("notice: No default references"));
598 show_unreachable
= 0;
602 struct for_each_loose_cb
604 struct progress
*progress
;
605 struct strbuf obj_type
;
608 static int fsck_loose(const struct object_id
*oid
, const char *path
, void *data
)
610 struct for_each_loose_cb
*cb_data
= data
;
612 enum object_type type
= OBJ_NONE
;
614 void *contents
= NULL
;
616 struct object_info oi
= OBJECT_INFO_INIT
;
617 struct object_id real_oid
= *null_oid();
620 strbuf_reset(&cb_data
->obj_type
);
621 oi
.type_name
= &cb_data
->obj_type
;
625 if (read_loose_object(path
, oid
, &real_oid
, &contents
, &oi
) < 0) {
626 if (contents
&& !oideq(&real_oid
, oid
))
627 err
= error(_("%s: hash-path mismatch, found at: %s"),
628 oid_to_hex(&real_oid
), path
);
630 err
= error(_("%s: object corrupt or missing: %s"),
631 oid_to_hex(oid
), path
);
633 if (type
!= OBJ_NONE
&& type
< 0)
634 err
= error(_("%s: object is of unknown type '%s': %s"),
635 oid_to_hex(&real_oid
), cb_data
->obj_type
.buf
,
638 errors_found
|= ERROR_OBJECT
;
640 return 0; /* keep checking other objects */
643 if (!contents
&& type
!= OBJ_BLOB
)
644 BUG("read_loose_object streamed a non-blob");
646 obj
= parse_object_buffer(the_repository
, oid
, type
, size
,
650 errors_found
|= ERROR_OBJECT
;
651 error(_("%s: object could not be parsed: %s"),
652 oid_to_hex(oid
), path
);
655 return 0; /* keep checking other objects */
658 obj
->flags
&= ~(REACHABLE
| SEEN
);
659 obj
->flags
|= HAS_OBJ
;
660 if (fsck_obj(obj
, contents
, size
))
661 errors_found
|= ERROR_OBJECT
;
665 return 0; /* keep checking other objects, even if we saw an error */
668 static int fsck_cruft(const char *basename
, const char *path
,
671 if (!starts_with(basename
, "tmp_obj_"))
672 fprintf_ln(stderr
, _("bad sha1 file: %s"), path
);
676 static int fsck_subdir(unsigned int nr
, const char *path UNUSED
, void *data
)
678 struct for_each_loose_cb
*cb_data
= data
;
679 struct progress
*progress
= cb_data
->progress
;
680 display_progress(progress
, nr
+ 1);
684 static void fsck_object_dir(const char *path
)
686 struct progress
*progress
= NULL
;
687 struct for_each_loose_cb cb_data
= {
688 .obj_type
= STRBUF_INIT
,
689 .progress
= progress
,
693 fprintf_ln(stderr
, _("Checking object directory"));
696 progress
= start_progress(_("Checking object directories"), 256);
698 for_each_loose_file_in_objdir(path
, fsck_loose
, fsck_cruft
, fsck_subdir
,
700 display_progress(progress
, 256);
701 stop_progress(&progress
);
702 strbuf_release(&cb_data
.obj_type
);
705 static int fsck_head_link(const char *head_ref_name
,
706 const char **head_points_at
,
707 struct object_id
*head_oid
)
709 int null_is_error
= 0;
712 fprintf_ln(stderr
, _("Checking %s link"), head_ref_name
);
714 *head_points_at
= resolve_ref_unsafe(head_ref_name
, 0, head_oid
, NULL
);
715 if (!*head_points_at
) {
716 errors_found
|= ERROR_REFS
;
717 return error(_("invalid %s"), head_ref_name
);
719 if (!strcmp(*head_points_at
, head_ref_name
))
722 else if (!starts_with(*head_points_at
, "refs/heads/")) {
723 errors_found
|= ERROR_REFS
;
724 return error(_("%s points to something strange (%s)"),
725 head_ref_name
, *head_points_at
);
727 if (is_null_oid(head_oid
)) {
729 errors_found
|= ERROR_REFS
;
730 return error(_("%s: detached HEAD points at nothing"),
734 _("notice: %s points to an unborn branch (%s)"),
735 head_ref_name
, *head_points_at
+ 11);
740 static int fsck_cache_tree(struct cache_tree
*it
, const char *index_path
)
746 fprintf_ln(stderr
, _("Checking cache tree of %s"), index_path
);
748 if (0 <= it
->entry_count
) {
749 struct object
*obj
= parse_object(the_repository
, &it
->oid
);
751 error(_("%s: invalid sha1 pointer in cache-tree of %s"),
752 oid_to_hex(&it
->oid
), index_path
);
753 errors_found
|= ERROR_REFS
;
757 fsck_put_object_name(&fsck_walk_options
, &it
->oid
, ":");
758 mark_object_reachable(obj
);
759 if (obj
->type
!= OBJ_TREE
)
760 err
|= objerror(obj
, _("non-tree in cache-tree"));
762 for (i
= 0; i
< it
->subtree_nr
; i
++)
763 err
|= fsck_cache_tree(it
->down
[i
]->cache_tree
, index_path
);
767 static int fsck_resolve_undo(struct index_state
*istate
,
768 const char *index_path
)
770 struct string_list_item
*item
;
771 struct string_list
*resolve_undo
= istate
->resolve_undo
;
776 for_each_string_list_item(item
, resolve_undo
) {
777 const char *path
= item
->string
;
778 struct resolve_undo_info
*ru
= item
->util
;
783 for (i
= 0; i
< 3; i
++) {
786 if (!ru
->mode
[i
] || !S_ISREG(ru
->mode
[i
]))
789 obj
= parse_object(the_repository
, &ru
->oid
[i
]);
791 error(_("%s: invalid sha1 pointer in resolve-undo of %s"),
792 oid_to_hex(&ru
->oid
[i
]),
794 errors_found
|= ERROR_REFS
;
798 fsck_put_object_name(&fsck_walk_options
, &ru
->oid
[i
],
799 ":(%d):%s", i
, path
);
800 mark_object_reachable(obj
);
806 static void fsck_index(struct index_state
*istate
, const char *index_path
,
811 /* TODO: audit for interaction with sparse-index. */
812 ensure_full_index(istate
);
813 for (i
= 0; i
< istate
->cache_nr
; i
++) {
818 mode
= istate
->cache
[i
]->ce_mode
;
819 if (S_ISGITLINK(mode
))
821 blob
= lookup_blob(the_repository
,
822 &istate
->cache
[i
]->oid
);
827 fsck_put_object_name(&fsck_walk_options
, &obj
->oid
,
829 is_main_index
? "" : index_path
,
830 istate
->cache
[i
]->name
);
831 mark_object_reachable(obj
);
833 if (istate
->cache_tree
)
834 fsck_cache_tree(istate
->cache_tree
, index_path
);
835 fsck_resolve_undo(istate
, index_path
);
838 static void mark_object_for_connectivity(const struct object_id
*oid
)
840 struct object
*obj
= lookup_unknown_object(the_repository
, oid
);
841 obj
->flags
|= HAS_OBJ
;
844 static int mark_loose_for_connectivity(const struct object_id
*oid
,
845 const char *path UNUSED
,
848 mark_object_for_connectivity(oid
);
852 static int mark_packed_for_connectivity(const struct object_id
*oid
,
853 struct packed_git
*pack UNUSED
,
857 mark_object_for_connectivity(oid
);
861 static char const * const fsck_usage
[] = {
862 N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n"
863 " [--[no-]full] [--strict] [--verbose] [--lost-found]\n"
864 " [--[no-]dangling] [--[no-]progress] [--connectivity-only]\n"
865 " [--[no-]name-objects] [<object>...]"),
869 static struct option fsck_opts
[] = {
870 OPT__VERBOSE(&verbose
, N_("be verbose")),
871 OPT_BOOL(0, "unreachable", &show_unreachable
, N_("show unreachable objects")),
872 OPT_BOOL(0, "dangling", &show_dangling
, N_("show dangling objects")),
873 OPT_BOOL(0, "tags", &show_tags
, N_("report tags")),
874 OPT_BOOL(0, "root", &show_root
, N_("report root nodes")),
875 OPT_BOOL(0, "cache", &keep_cache_objects
, N_("make index objects head nodes")),
876 OPT_BOOL(0, "reflogs", &include_reflogs
, N_("make reflogs head nodes (default)")),
877 OPT_BOOL(0, "full", &check_full
, N_("also consider packs and alternate objects")),
878 OPT_BOOL(0, "connectivity-only", &connectivity_only
, N_("check only connectivity")),
879 OPT_BOOL(0, "strict", &check_strict
, N_("enable more strict checking")),
880 OPT_BOOL(0, "lost-found", &write_lost_and_found
,
881 N_("write dangling objects in .git/lost-found")),
882 OPT_BOOL(0, "progress", &show_progress
, N_("show progress")),
883 OPT_BOOL(0, "name-objects", &name_objects
, N_("show verbose names for reachable objects")),
887 int cmd_fsck(int argc
, const char **argv
, const char *prefix
)
890 struct object_directory
*odb
;
892 /* fsck knows how to handle missing promisor objects */
893 fetch_if_missing
= 0;
896 read_replace_refs
= 0;
897 save_commit_buffer
= 0;
899 argc
= parse_options(argc
, argv
, prefix
, fsck_opts
, fsck_usage
, 0);
901 fsck_walk_options
.walk
= mark_object
;
902 fsck_obj_options
.walk
= mark_used
;
903 fsck_obj_options
.error_func
= fsck_error_func
;
905 fsck_obj_options
.strict
= 1;
907 if (show_progress
== -1)
908 show_progress
= isatty(2);
912 if (write_lost_and_found
) {
918 fsck_enable_object_names(&fsck_walk_options
);
920 git_config(git_fsck_config
, &fsck_obj_options
);
921 prepare_repo_settings(the_repository
);
923 if (connectivity_only
) {
924 for_each_loose_object(mark_loose_for_connectivity
, NULL
, 0);
925 for_each_packed_object(mark_packed_for_connectivity
, NULL
, 0);
927 prepare_alt_odb(the_repository
);
928 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
)
929 fsck_object_dir(odb
->path
);
932 struct packed_git
*p
;
933 uint32_t total
= 0, count
= 0;
934 struct progress
*progress
= NULL
;
937 for (p
= get_all_packs(the_repository
); p
;
939 if (open_pack_index(p
))
941 total
+= p
->num_objects
;
944 progress
= start_progress(_("Checking objects"), total
);
946 for (p
= get_all_packs(the_repository
); p
;
948 /* verify gives error messages itself */
949 if (verify_pack(the_repository
,
952 errors_found
|= ERROR_PACK
;
953 count
+= p
->num_objects
;
955 stop_progress(&progress
);
958 if (fsck_finish(&fsck_obj_options
))
959 errors_found
|= ERROR_OBJECT
;
962 for (i
= 0; i
< argc
; i
++) {
963 const char *arg
= argv
[i
];
964 struct object_id oid
;
965 if (!repo_get_oid(the_repository
, arg
, &oid
)) {
966 struct object
*obj
= lookup_object(the_repository
,
969 if (!obj
|| !(obj
->flags
& HAS_OBJ
)) {
970 if (is_promisor_object(&oid
))
972 error(_("%s: object missing"), oid_to_hex(&oid
));
973 errors_found
|= ERROR_OBJECT
;
978 fsck_put_object_name(&fsck_walk_options
, &oid
,
980 mark_object_reachable(obj
);
983 error(_("invalid parameter: expected sha1, got '%s'"), arg
);
984 errors_found
|= ERROR_OBJECT
;
988 * If we've not been given any explicit head information, do the
989 * default ones from .git/refs. We also consider the index file
990 * in this case (ie this implies --cache).
994 keep_cache_objects
= 1;
997 if (keep_cache_objects
) {
998 struct worktree
**worktrees
, **p
;
1000 verify_index_checksum
= 1;
1001 verify_ce_order
= 1;
1003 worktrees
= get_worktrees();
1004 for (p
= worktrees
; *p
; p
++) {
1005 struct worktree
*wt
= *p
;
1006 struct index_state istate
=
1007 INDEX_STATE_INIT(the_repository
);
1011 * Make a copy since the buffer is reusable
1012 * and may get overwritten by other calls
1013 * while we're examining the index.
1015 path
= xstrdup(worktree_git_path(wt
, "index"));
1016 read_index_from(&istate
, path
, get_worktree_git_dir(wt
));
1017 fsck_index(&istate
, path
, wt
->is_current
);
1018 discard_index(&istate
);
1021 free_worktrees(worktrees
);
1024 check_connectivity();
1026 if (the_repository
->settings
.core_commit_graph
) {
1027 struct child_process commit_graph_verify
= CHILD_PROCESS_INIT
;
1029 prepare_alt_odb(the_repository
);
1030 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
) {
1031 child_process_init(&commit_graph_verify
);
1032 commit_graph_verify
.git_cmd
= 1;
1033 strvec_pushl(&commit_graph_verify
.args
, "commit-graph",
1034 "verify", "--object-dir", odb
->path
, NULL
);
1035 if (run_command(&commit_graph_verify
))
1036 errors_found
|= ERROR_COMMIT_GRAPH
;
1040 if (the_repository
->settings
.core_multi_pack_index
) {
1041 struct child_process midx_verify
= CHILD_PROCESS_INIT
;
1043 prepare_alt_odb(the_repository
);
1044 for (odb
= the_repository
->objects
->odb
; odb
; odb
= odb
->next
) {
1045 child_process_init(&midx_verify
);
1046 midx_verify
.git_cmd
= 1;
1047 strvec_pushl(&midx_verify
.args
, "multi-pack-index",
1048 "verify", "--object-dir", odb
->path
, NULL
);
1049 if (run_command(&midx_verify
))
1050 errors_found
|= ERROR_MULTI_PACK_INDEX
;
1054 return errors_found
;