3 #include "repository.h"
11 #include "cache-tree.h"
12 #include "tree-walk.h"
14 #include "parse-options.h"
17 #include "streaming.h"
20 #include "object-store.h"
21 #include "run-command.h"
23 #define REACHABLE 0x0001
25 #define HAS_OBJ 0x0004
26 /* This flag is set if something points to this object. */
31 static int show_unreachable
;
32 static int include_reflogs
= 1;
33 static int check_full
= 1;
34 static int connectivity_only
;
35 static int check_strict
;
36 static int keep_cache_objects
;
37 static struct fsck_options fsck_walk_options
= FSCK_OPTIONS_DEFAULT
;
38 static struct fsck_options fsck_obj_options
= FSCK_OPTIONS_DEFAULT
;
39 static struct object_id head_oid
;
40 static const char *head_points_at
;
41 static int errors_found
;
42 static int write_lost_and_found
;
44 static int show_progress
= -1;
45 static int show_dangling
= 1;
46 static int name_objects
;
47 #define ERROR_OBJECT 01
48 #define ERROR_REACHABLE 02
50 #define ERROR_REFS 010
51 #define ERROR_COMMIT_GRAPH 020
53 static const char *describe_object(struct object
*obj
)
55 static struct strbuf buf
= STRBUF_INIT
;
56 char *name
= name_objects
?
57 lookup_decoration(fsck_walk_options
.object_names
, obj
) : NULL
;
60 strbuf_addstr(&buf
, oid_to_hex(&obj
->oid
));
62 strbuf_addf(&buf
, " (%s)", name
);
67 static const char *printable_type(struct object
*obj
)
71 if (obj
->type
== OBJ_NONE
) {
72 enum object_type type
= oid_object_info(the_repository
,
75 object_as_type(the_repository
, obj
, type
, 0);
78 ret
= type_name(obj
->type
);
85 static int fsck_config(const char *var
, const char *value
, void *cb
)
87 if (strcmp(var
, "fsck.skiplist") == 0) {
89 struct strbuf sb
= STRBUF_INIT
;
91 if (git_config_pathname(&path
, var
, value
))
93 strbuf_addf(&sb
, "skiplist=%s", path
);
95 fsck_set_msg_types(&fsck_obj_options
, sb
.buf
);
100 if (skip_prefix(var
, "fsck.", &var
)) {
101 fsck_set_msg_type(&fsck_obj_options
, var
, value
);
105 return git_default_config(var
, value
, cb
);
108 static void objreport(struct object
*obj
, const char *msg_type
,
111 fprintf(stderr
, "%s in %s %s: %s\n",
112 msg_type
, printable_type(obj
), describe_object(obj
), err
);
115 static int objerror(struct object
*obj
, const char *err
)
117 errors_found
|= ERROR_OBJECT
;
118 objreport(obj
, "error", err
);
122 static int fsck_error_func(struct fsck_options
*o
,
123 struct object
*obj
, int type
, const char *message
)
125 objreport(obj
, (type
== FSCK_WARN
) ? "warning" : "error", message
);
126 return (type
== FSCK_WARN
) ? 0 : 1;
129 static struct object_array pending
;
131 static int mark_object(struct object
*obj
, int type
, void *data
, struct fsck_options
*options
)
133 struct object
*parent
= data
;
136 * The only case data is NULL or type is OBJ_ANY is when
137 * mark_object_reachable() calls us. All the callers of
138 * that function has non-NULL obj hence ...
141 /* ... these references to parent->fld are safe here */
142 printf("broken link from %7s %s\n",
143 printable_type(parent
), describe_object(parent
));
144 printf("broken link from %7s %s\n",
145 (type
== OBJ_ANY
? "unknown" : type_name(type
)), "unknown");
146 errors_found
|= ERROR_REACHABLE
;
150 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
151 /* ... and the reference to parent is safe here */
152 objerror(parent
, "wrong object type in link");
154 if (obj
->flags
& REACHABLE
)
156 obj
->flags
|= REACHABLE
;
158 if (is_promisor_object(&obj
->oid
))
160 * Further recursion does not need to be performed on this
161 * object since it is a promisor object (so it does not need to
162 * be added to "pending").
166 if (!(obj
->flags
& HAS_OBJ
)) {
167 if (parent
&& !has_object_file(&obj
->oid
)) {
168 printf("broken link from %7s %s\n",
169 printable_type(parent
), describe_object(parent
));
170 printf(" to %7s %s\n",
171 printable_type(obj
), describe_object(obj
));
172 errors_found
|= ERROR_REACHABLE
;
177 add_object_array(obj
, NULL
, &pending
);
181 static void mark_object_reachable(struct object
*obj
)
183 mark_object(obj
, OBJ_ANY
, NULL
, NULL
);
186 static int traverse_one_object(struct object
*obj
)
188 int result
= fsck_walk(obj
, obj
, &fsck_walk_options
);
190 if (obj
->type
== OBJ_TREE
) {
191 struct tree
*tree
= (struct tree
*)obj
;
192 free_tree_buffer(tree
);
197 static int traverse_reachable(void)
199 struct progress
*progress
= NULL
;
203 progress
= start_delayed_progress(_("Checking connectivity"), 0);
205 result
|= traverse_one_object(object_array_pop(&pending
));
206 display_progress(progress
, ++nr
);
208 stop_progress(&progress
);
212 static int mark_used(struct object
*obj
, int type
, void *data
, struct fsck_options
*options
)
221 * Check a single reachable object
223 static void check_reachable_object(struct object
*obj
)
226 * We obviously want the object to be parsed,
227 * except if it was in a pack-file and we didn't
230 if (!(obj
->flags
& HAS_OBJ
)) {
231 if (is_promisor_object(&obj
->oid
))
233 if (has_object_pack(&obj
->oid
))
234 return; /* it is in pack - forget about it */
235 printf("missing %s %s\n", printable_type(obj
),
236 describe_object(obj
));
237 errors_found
|= ERROR_REACHABLE
;
243 * Check a single unreachable object
245 static void check_unreachable_object(struct object
*obj
)
248 * Missing unreachable object? Ignore it. It's not like
249 * we miss it (since it can't be reached), nor do we want
250 * to complain about it being unreachable (since it does
253 if (!(obj
->flags
& HAS_OBJ
))
257 * Unreachable object that exists? Show it if asked to,
258 * since this is something that is prunable.
260 if (show_unreachable
) {
261 printf("unreachable %s %s\n", printable_type(obj
),
262 describe_object(obj
));
267 * "!USED" means that nothing at all points to it, including
268 * other unreachable objects. In other words, it's the "tip"
269 * of some set of unreachable objects, usually a commit that
272 * Such starting points are more interesting than some random
273 * set of unreachable objects, so we show them even if the user
274 * hasn't asked for _all_ unreachable objects. If you have
275 * deleted a branch by mistake, this is a prime candidate to
276 * start looking at, for example.
278 if (!(obj
->flags
& USED
)) {
280 printf("dangling %s %s\n", printable_type(obj
),
281 describe_object(obj
));
282 if (write_lost_and_found
) {
283 char *filename
= git_pathdup("lost-found/%s/%s",
284 obj
->type
== OBJ_COMMIT
? "commit" : "other",
285 describe_object(obj
));
288 if (safe_create_leading_directories_const(filename
)) {
289 error("Could not create lost-found");
293 f
= xfopen(filename
, "w");
294 if (obj
->type
== OBJ_BLOB
) {
295 if (stream_blob_to_fd(fileno(f
), &obj
->oid
, NULL
, 1))
296 die_errno("Could not write '%s'", filename
);
298 fprintf(f
, "%s\n", describe_object(obj
));
300 die_errno("Could not finish '%s'",
308 * Otherwise? It's there, it's unreachable, and some other unreachable
309 * object points to it. Ignore it - it's not interesting, and we showed
310 * all the interesting cases above.
314 static void check_object(struct object
*obj
)
317 fprintf(stderr
, "Checking %s\n", describe_object(obj
));
319 if (obj
->flags
& REACHABLE
)
320 check_reachable_object(obj
);
322 check_unreachable_object(obj
);
325 static void check_connectivity(void)
329 /* Traverse the pending reachable objects */
330 traverse_reachable();
332 /* Look up all the requirements, warn about missing objects.. */
333 max
= get_max_object_index();
335 fprintf(stderr
, "Checking connectivity (%d objects)\n", max
);
337 for (i
= 0; i
< max
; i
++) {
338 struct object
*obj
= get_indexed_object(i
);
345 static int fsck_obj(struct object
*obj
, void *buffer
, unsigned long size
)
349 if (obj
->flags
& SEEN
)
354 fprintf(stderr
, "Checking %s %s\n",
355 printable_type(obj
), describe_object(obj
));
357 if (fsck_walk(obj
, NULL
, &fsck_obj_options
))
358 objerror(obj
, "broken links");
359 err
= fsck_object(obj
, buffer
, size
, &fsck_obj_options
);
363 if (obj
->type
== OBJ_COMMIT
) {
364 struct commit
*commit
= (struct commit
*) obj
;
366 if (!commit
->parents
&& show_root
)
367 printf("root %s\n", describe_object(&commit
->object
));
370 if (obj
->type
== OBJ_TAG
) {
371 struct tag
*tag
= (struct tag
*) obj
;
373 if (show_tags
&& tag
->tagged
) {
374 printf("tagged %s %s", printable_type(tag
->tagged
),
375 describe_object(tag
->tagged
));
376 printf(" (%s) in %s\n", tag
->tag
,
377 describe_object(&tag
->object
));
382 if (obj
->type
== OBJ_TREE
)
383 free_tree_buffer((struct tree
*)obj
);
384 if (obj
->type
== OBJ_COMMIT
)
385 free_commit_buffer((struct commit
*)obj
);
389 static int fsck_obj_buffer(const struct object_id
*oid
, enum object_type type
,
390 unsigned long size
, void *buffer
, int *eaten
)
393 * Note, buffer may be NULL if type is OBJ_BLOB. See
394 * verify_packfile(), data_valid variable for details.
397 obj
= parse_object_buffer(the_repository
, oid
, type
, size
, buffer
,
400 errors_found
|= ERROR_OBJECT
;
401 return error("%s: object corrupt or missing", oid_to_hex(oid
));
403 obj
->flags
&= ~(REACHABLE
| SEEN
);
404 obj
->flags
|= HAS_OBJ
;
405 return fsck_obj(obj
, buffer
, size
);
408 static int default_refs
;
410 static void fsck_handle_reflog_oid(const char *refname
, struct object_id
*oid
,
411 timestamp_t timestamp
)
415 if (!is_null_oid(oid
)) {
416 obj
= lookup_object(the_repository
, oid
->hash
);
417 if (obj
&& (obj
->flags
& HAS_OBJ
)) {
418 if (timestamp
&& name_objects
)
419 add_decoration(fsck_walk_options
.object_names
,
421 xstrfmt("%s@{%"PRItime
"}", refname
, timestamp
));
423 mark_object_reachable(obj
);
424 } else if (!is_promisor_object(oid
)) {
425 error("%s: invalid reflog entry %s", refname
, oid_to_hex(oid
));
426 errors_found
|= ERROR_REACHABLE
;
431 static int fsck_handle_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
432 const char *email
, timestamp_t timestamp
, int tz
,
433 const char *message
, void *cb_data
)
435 const char *refname
= cb_data
;
438 fprintf(stderr
, "Checking reflog %s->%s\n",
439 oid_to_hex(ooid
), oid_to_hex(noid
));
441 fsck_handle_reflog_oid(refname
, ooid
, 0);
442 fsck_handle_reflog_oid(refname
, noid
, timestamp
);
446 static int fsck_handle_reflog(const char *logname
, const struct object_id
*oid
,
447 int flag
, void *cb_data
)
449 for_each_reflog_ent(logname
, fsck_handle_reflog_ent
, (void *)logname
);
453 static int fsck_handle_ref(const char *refname
, const struct object_id
*oid
,
454 int flag
, void *cb_data
)
458 obj
= parse_object(the_repository
, oid
);
460 if (is_promisor_object(oid
)) {
462 * Increment default_refs anyway, because this is a
468 error("%s: invalid sha1 pointer %s", refname
, oid_to_hex(oid
));
469 errors_found
|= ERROR_REACHABLE
;
470 /* We'll continue with the rest despite the error.. */
473 if (obj
->type
!= OBJ_COMMIT
&& is_branch(refname
)) {
474 error("%s: not a commit", refname
);
475 errors_found
|= ERROR_REFS
;
480 add_decoration(fsck_walk_options
.object_names
,
481 obj
, xstrdup(refname
));
482 mark_object_reachable(obj
);
487 static void get_default_heads(void)
489 if (head_points_at
&& !is_null_oid(&head_oid
))
490 fsck_handle_ref("HEAD", &head_oid
, 0, NULL
);
491 for_each_rawref(fsck_handle_ref
, NULL
);
493 for_each_reflog(fsck_handle_reflog
, NULL
);
496 * Not having any default heads isn't really fatal, but
497 * it does mean that "--unreachable" no longer makes any
498 * sense (since in this case everything will obviously
499 * be unreachable by definition.
501 * Showing dangling objects is valid, though (as those
502 * dangling objects are likely lost heads).
504 * So we just print a warning about it, and clear the
505 * "show_unreachable" flag.
508 fprintf(stderr
, "notice: No default references\n");
509 show_unreachable
= 0;
513 static int fsck_loose(const struct object_id
*oid
, const char *path
, void *data
)
516 enum object_type type
;
521 if (read_loose_object(path
, oid
, &type
, &size
, &contents
) < 0) {
522 errors_found
|= ERROR_OBJECT
;
523 error("%s: object corrupt or missing: %s",
524 oid_to_hex(oid
), path
);
525 return 0; /* keep checking other objects */
528 if (!contents
&& type
!= OBJ_BLOB
)
529 BUG("read_loose_object streamed a non-blob");
531 obj
= parse_object_buffer(the_repository
, oid
, type
, size
,
535 errors_found
|= ERROR_OBJECT
;
536 error("%s: object could not be parsed: %s",
537 oid_to_hex(oid
), path
);
540 return 0; /* keep checking other objects */
543 obj
->flags
&= ~(REACHABLE
| SEEN
);
544 obj
->flags
|= HAS_OBJ
;
545 if (fsck_obj(obj
, contents
, size
))
546 errors_found
|= ERROR_OBJECT
;
550 return 0; /* keep checking other objects, even if we saw an error */
553 static int fsck_cruft(const char *basename
, const char *path
, void *data
)
555 if (!starts_with(basename
, "tmp_obj_"))
556 fprintf(stderr
, "bad sha1 file: %s\n", path
);
560 static int fsck_subdir(unsigned int nr
, const char *path
, void *progress
)
562 display_progress(progress
, nr
+ 1);
566 static void fsck_object_dir(const char *path
)
568 struct progress
*progress
= NULL
;
571 fprintf(stderr
, "Checking object directory\n");
574 progress
= start_progress(_("Checking object directories"), 256);
576 for_each_loose_file_in_objdir(path
, fsck_loose
, fsck_cruft
, fsck_subdir
,
578 display_progress(progress
, 256);
579 stop_progress(&progress
);
582 static int fsck_head_link(void)
584 int null_is_error
= 0;
587 fprintf(stderr
, "Checking HEAD link\n");
589 head_points_at
= resolve_ref_unsafe("HEAD", 0, &head_oid
, NULL
);
590 if (!head_points_at
) {
591 errors_found
|= ERROR_REFS
;
592 return error("Invalid HEAD");
594 if (!strcmp(head_points_at
, "HEAD"))
597 else if (!starts_with(head_points_at
, "refs/heads/")) {
598 errors_found
|= ERROR_REFS
;
599 return error("HEAD points to something strange (%s)",
602 if (is_null_oid(&head_oid
)) {
604 errors_found
|= ERROR_REFS
;
605 return error("HEAD: detached HEAD points at nothing");
607 fprintf(stderr
, "notice: HEAD points to an unborn branch (%s)\n",
608 head_points_at
+ 11);
613 static int fsck_cache_tree(struct cache_tree
*it
)
619 fprintf(stderr
, "Checking cache tree\n");
621 if (0 <= it
->entry_count
) {
622 struct object
*obj
= parse_object(the_repository
, &it
->oid
);
624 error("%s: invalid sha1 pointer in cache-tree",
625 oid_to_hex(&it
->oid
));
626 errors_found
|= ERROR_REFS
;
631 add_decoration(fsck_walk_options
.object_names
,
633 mark_object_reachable(obj
);
634 if (obj
->type
!= OBJ_TREE
)
635 err
|= objerror(obj
, "non-tree in cache-tree");
637 for (i
= 0; i
< it
->subtree_nr
; i
++)
638 err
|= fsck_cache_tree(it
->down
[i
]->cache_tree
);
642 static void mark_object_for_connectivity(const struct object_id
*oid
)
644 struct object
*obj
= lookup_unknown_object(oid
->hash
);
645 obj
->flags
|= HAS_OBJ
;
648 static int mark_loose_for_connectivity(const struct object_id
*oid
,
652 mark_object_for_connectivity(oid
);
656 static int mark_packed_for_connectivity(const struct object_id
*oid
,
657 struct packed_git
*pack
,
661 mark_object_for_connectivity(oid
);
665 static char const * const fsck_usage
[] = {
666 N_("git fsck [<options>] [<object>...]"),
670 static struct option fsck_opts
[] = {
671 OPT__VERBOSE(&verbose
, N_("be verbose")),
672 OPT_BOOL(0, "unreachable", &show_unreachable
, N_("show unreachable objects")),
673 OPT_BOOL(0, "dangling", &show_dangling
, N_("show dangling objects")),
674 OPT_BOOL(0, "tags", &show_tags
, N_("report tags")),
675 OPT_BOOL(0, "root", &show_root
, N_("report root nodes")),
676 OPT_BOOL(0, "cache", &keep_cache_objects
, N_("make index objects head nodes")),
677 OPT_BOOL(0, "reflogs", &include_reflogs
, N_("make reflogs head nodes (default)")),
678 OPT_BOOL(0, "full", &check_full
, N_("also consider packs and alternate objects")),
679 OPT_BOOL(0, "connectivity-only", &connectivity_only
, N_("check only connectivity")),
680 OPT_BOOL(0, "strict", &check_strict
, N_("enable more strict checking")),
681 OPT_BOOL(0, "lost-found", &write_lost_and_found
,
682 N_("write dangling objects in .git/lost-found")),
683 OPT_BOOL(0, "progress", &show_progress
, N_("show progress")),
684 OPT_BOOL(0, "name-objects", &name_objects
, N_("show verbose names for reachable objects")),
688 int cmd_fsck(int argc
, const char **argv
, const char *prefix
)
691 struct alternate_object_database
*alt
;
693 /* fsck knows how to handle missing promisor objects */
694 fetch_if_missing
= 0;
697 read_replace_refs
= 0;
699 argc
= parse_options(argc
, argv
, prefix
, fsck_opts
, fsck_usage
, 0);
701 fsck_walk_options
.walk
= mark_object
;
702 fsck_obj_options
.walk
= mark_used
;
703 fsck_obj_options
.error_func
= fsck_error_func
;
705 fsck_obj_options
.strict
= 1;
707 if (show_progress
== -1)
708 show_progress
= isatty(2);
712 if (write_lost_and_found
) {
718 fsck_walk_options
.object_names
=
719 xcalloc(1, sizeof(struct decoration
));
721 git_config(fsck_config
, NULL
);
724 if (connectivity_only
) {
725 for_each_loose_object(mark_loose_for_connectivity
, NULL
, 0);
726 for_each_packed_object(mark_packed_for_connectivity
, NULL
, 0);
728 struct alternate_object_database
*alt_odb_list
;
730 fsck_object_dir(get_object_directory());
732 prepare_alt_odb(the_repository
);
733 alt_odb_list
= the_repository
->objects
->alt_odb_list
;
734 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
)
735 fsck_object_dir(alt
->path
);
738 struct packed_git
*p
;
739 uint32_t total
= 0, count
= 0;
740 struct progress
*progress
= NULL
;
743 for (p
= get_all_packs(the_repository
); p
;
745 if (open_pack_index(p
))
747 total
+= p
->num_objects
;
750 progress
= start_progress(_("Checking objects"), total
);
752 for (p
= get_all_packs(the_repository
); p
;
754 /* verify gives error messages itself */
755 if (verify_pack(p
, fsck_obj_buffer
,
757 errors_found
|= ERROR_PACK
;
758 count
+= p
->num_objects
;
760 stop_progress(&progress
);
763 if (fsck_finish(&fsck_obj_options
))
764 errors_found
|= ERROR_OBJECT
;
767 for (i
= 0; i
< argc
; i
++) {
768 const char *arg
= argv
[i
];
769 struct object_id oid
;
770 if (!get_oid(arg
, &oid
)) {
771 struct object
*obj
= lookup_object(the_repository
,
774 if (!obj
|| !(obj
->flags
& HAS_OBJ
)) {
775 if (is_promisor_object(&oid
))
777 error("%s: object missing", oid_to_hex(&oid
));
778 errors_found
|= ERROR_OBJECT
;
784 add_decoration(fsck_walk_options
.object_names
,
786 mark_object_reachable(obj
);
789 error("invalid parameter: expected sha1, got '%s'", arg
);
790 errors_found
|= ERROR_OBJECT
;
794 * If we've not been given any explicit head information, do the
795 * default ones from .git/refs. We also consider the index file
796 * in this case (ie this implies --cache).
800 keep_cache_objects
= 1;
803 if (keep_cache_objects
) {
804 verify_index_checksum
= 1;
807 for (i
= 0; i
< active_nr
; i
++) {
812 mode
= active_cache
[i
]->ce_mode
;
813 if (S_ISGITLINK(mode
))
815 blob
= lookup_blob(the_repository
,
816 &active_cache
[i
]->oid
);
822 add_decoration(fsck_walk_options
.object_names
,
824 xstrfmt(":%s", active_cache
[i
]->name
));
825 mark_object_reachable(obj
);
827 if (active_cache_tree
)
828 fsck_cache_tree(active_cache_tree
);
831 check_connectivity();
833 if (!git_config_get_bool("core.commitgraph", &i
) && i
) {
834 struct child_process commit_graph_verify
= CHILD_PROCESS_INIT
;
835 const char *verify_argv
[] = { "commit-graph", "verify", NULL
, NULL
, NULL
};
837 commit_graph_verify
.argv
= verify_argv
;
838 commit_graph_verify
.git_cmd
= 1;
839 if (run_command(&commit_graph_verify
))
840 errors_found
|= ERROR_COMMIT_GRAPH
;
842 prepare_alt_odb(the_repository
);
843 for (alt
= the_repository
->objects
->alt_odb_list
; alt
; alt
= alt
->next
) {
844 verify_argv
[2] = "--object-dir";
845 verify_argv
[3] = alt
->path
;
846 if (run_command(&commit_graph_verify
))
847 errors_found
|= ERROR_COMMIT_GRAPH
;
851 if (!git_config_get_bool("core.multipackindex", &i
) && i
) {
852 struct child_process midx_verify
= CHILD_PROCESS_INIT
;
853 const char *midx_argv
[] = { "multi-pack-index", "verify", NULL
, NULL
, NULL
};
855 midx_verify
.argv
= midx_argv
;
856 midx_verify
.git_cmd
= 1;
857 if (run_command(&midx_verify
))
858 errors_found
|= ERROR_COMMIT_GRAPH
;
860 prepare_alt_odb(the_repository
);
861 for (alt
= the_repository
->objects
->alt_odb_list
; alt
; alt
= alt
->next
) {
862 midx_argv
[2] = "--object-dir";
863 midx_argv
[3] = alt
->path
;
864 if (run_command(&midx_verify
))
865 errors_found
|= ERROR_COMMIT_GRAPH
;