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"
22 #define REACHABLE 0x0001
24 #define HAS_OBJ 0x0004
25 /* This flag is set if something points to this object. */
30 static int show_unreachable
;
31 static int include_reflogs
= 1;
32 static int check_full
= 1;
33 static int connectivity_only
;
34 static int check_strict
;
35 static int keep_cache_objects
;
36 static struct fsck_options fsck_walk_options
= FSCK_OPTIONS_DEFAULT
;
37 static struct fsck_options fsck_obj_options
= FSCK_OPTIONS_DEFAULT
;
38 static struct object_id head_oid
;
39 static const char *head_points_at
;
40 static int errors_found
;
41 static int write_lost_and_found
;
43 static int show_progress
= -1;
44 static int show_dangling
= 1;
45 static int name_objects
;
46 #define ERROR_OBJECT 01
47 #define ERROR_REACHABLE 02
49 #define ERROR_REFS 010
51 static const char *describe_object(struct object
*obj
)
53 static struct strbuf buf
= STRBUF_INIT
;
54 char *name
= name_objects
?
55 lookup_decoration(fsck_walk_options
.object_names
, obj
) : NULL
;
58 strbuf_addstr(&buf
, oid_to_hex(&obj
->oid
));
60 strbuf_addf(&buf
, " (%s)", name
);
65 static const char *printable_type(struct object
*obj
)
69 if (obj
->type
== OBJ_NONE
) {
70 enum object_type type
= sha1_object_info(obj
->oid
.hash
, NULL
);
72 object_as_type(obj
, type
, 0);
75 ret
= typename(obj
->type
);
82 static int fsck_config(const char *var
, const char *value
, void *cb
)
84 if (strcmp(var
, "fsck.skiplist") == 0) {
86 struct strbuf sb
= STRBUF_INIT
;
88 if (git_config_pathname(&path
, var
, value
))
90 strbuf_addf(&sb
, "skiplist=%s", path
);
92 fsck_set_msg_types(&fsck_obj_options
, sb
.buf
);
97 if (skip_prefix(var
, "fsck.", &var
)) {
98 fsck_set_msg_type(&fsck_obj_options
, var
, value
);
102 return git_default_config(var
, value
, cb
);
105 static void objreport(struct object
*obj
, const char *msg_type
,
108 fprintf(stderr
, "%s in %s %s: %s\n",
109 msg_type
, printable_type(obj
), describe_object(obj
), err
);
112 static int objerror(struct object
*obj
, const char *err
)
114 errors_found
|= ERROR_OBJECT
;
115 objreport(obj
, "error", err
);
119 static int fsck_error_func(struct fsck_options
*o
,
120 struct object
*obj
, int type
, const char *message
)
122 objreport(obj
, (type
== FSCK_WARN
) ? "warning" : "error", message
);
123 return (type
== FSCK_WARN
) ? 0 : 1;
126 static struct object_array pending
;
128 static int mark_object(struct object
*obj
, int type
, void *data
, struct fsck_options
*options
)
130 struct object
*parent
= data
;
133 * The only case data is NULL or type is OBJ_ANY is when
134 * mark_object_reachable() calls us. All the callers of
135 * that function has non-NULL obj hence ...
138 /* ... these references to parent->fld are safe here */
139 printf("broken link from %7s %s\n",
140 printable_type(parent
), describe_object(parent
));
141 printf("broken link from %7s %s\n",
142 (type
== OBJ_ANY
? "unknown" : typename(type
)), "unknown");
143 errors_found
|= ERROR_REACHABLE
;
147 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
148 /* ... and the reference to parent is safe here */
149 objerror(parent
, "wrong object type in link");
151 if (obj
->flags
& REACHABLE
)
153 obj
->flags
|= REACHABLE
;
155 if (is_promisor_object(&obj
->oid
))
157 * Further recursion does not need to be performed on this
158 * object since it is a promisor object (so it does not need to
159 * be added to "pending").
163 if (!(obj
->flags
& HAS_OBJ
)) {
164 if (parent
&& !has_object_file(&obj
->oid
)) {
165 printf("broken link from %7s %s\n",
166 printable_type(parent
), describe_object(parent
));
167 printf(" to %7s %s\n",
168 printable_type(obj
), describe_object(obj
));
169 errors_found
|= ERROR_REACHABLE
;
174 add_object_array(obj
, NULL
, &pending
);
178 static void mark_object_reachable(struct object
*obj
)
180 mark_object(obj
, OBJ_ANY
, NULL
, NULL
);
183 static int traverse_one_object(struct object
*obj
)
185 return fsck_walk(obj
, obj
, &fsck_walk_options
);
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
, int type
, void *data
, struct fsck_options
*options
)
212 * Check a single reachable object
214 static void check_reachable_object(struct object
*obj
)
217 * We obviously want the object to be parsed,
218 * except if it was in a pack-file and we didn't
221 if (!(obj
->flags
& HAS_OBJ
)) {
222 if (is_promisor_object(&obj
->oid
))
224 if (has_sha1_pack(obj
->oid
.hash
))
225 return; /* it is in pack - forget about it */
226 printf("missing %s %s\n", printable_type(obj
),
227 describe_object(obj
));
228 errors_found
|= ERROR_REACHABLE
;
234 * Check a single unreachable object
236 static void check_unreachable_object(struct object
*obj
)
239 * Missing unreachable object? Ignore it. It's not like
240 * we miss it (since it can't be reached), nor do we want
241 * to complain about it being unreachable (since it does
244 if (!(obj
->flags
& HAS_OBJ
))
248 * Unreachable object that exists? Show it if asked to,
249 * since this is something that is prunable.
251 if (show_unreachable
) {
252 printf("unreachable %s %s\n", printable_type(obj
),
253 describe_object(obj
));
258 * "!USED" means that nothing at all points to it, including
259 * other unreachable objects. In other words, it's the "tip"
260 * of some set of unreachable objects, usually a commit that
263 * Such starting points are more interesting than some random
264 * set of unreachable objects, so we show them even if the user
265 * hasn't asked for _all_ unreachable objects. If you have
266 * deleted a branch by mistake, this is a prime candidate to
267 * start looking at, for example.
269 if (!(obj
->flags
& USED
)) {
271 printf("dangling %s %s\n", printable_type(obj
),
272 describe_object(obj
));
273 if (write_lost_and_found
) {
274 char *filename
= git_pathdup("lost-found/%s/%s",
275 obj
->type
== OBJ_COMMIT
? "commit" : "other",
276 describe_object(obj
));
279 if (safe_create_leading_directories_const(filename
)) {
280 error("Could not create lost-found");
284 f
= xfopen(filename
, "w");
285 if (obj
->type
== OBJ_BLOB
) {
286 if (stream_blob_to_fd(fileno(f
), &obj
->oid
, NULL
, 1))
287 die_errno("Could not write '%s'", filename
);
289 fprintf(f
, "%s\n", describe_object(obj
));
291 die_errno("Could not finish '%s'",
299 * Otherwise? It's there, it's unreachable, and some other unreachable
300 * object points to it. Ignore it - it's not interesting, and we showed
301 * all the interesting cases above.
305 static void check_object(struct object
*obj
)
308 fprintf(stderr
, "Checking %s\n", describe_object(obj
));
310 if (obj
->flags
& REACHABLE
)
311 check_reachable_object(obj
);
313 check_unreachable_object(obj
);
316 static void check_connectivity(void)
320 /* Traverse the pending reachable objects */
321 traverse_reachable();
323 /* Look up all the requirements, warn about missing objects.. */
324 max
= get_max_object_index();
326 fprintf(stderr
, "Checking connectivity (%d objects)\n", max
);
328 for (i
= 0; i
< max
; i
++) {
329 struct object
*obj
= get_indexed_object(i
);
336 static int fsck_obj(struct object
*obj
)
340 if (obj
->flags
& SEEN
)
345 fprintf(stderr
, "Checking %s %s\n",
346 printable_type(obj
), describe_object(obj
));
348 if (fsck_walk(obj
, NULL
, &fsck_obj_options
))
349 objerror(obj
, "broken links");
350 err
= fsck_object(obj
, NULL
, 0, &fsck_obj_options
);
354 if (obj
->type
== OBJ_COMMIT
) {
355 struct commit
*commit
= (struct commit
*) obj
;
357 if (!commit
->parents
&& show_root
)
358 printf("root %s\n", describe_object(&commit
->object
));
361 if (obj
->type
== OBJ_TAG
) {
362 struct tag
*tag
= (struct tag
*) obj
;
364 if (show_tags
&& tag
->tagged
) {
365 printf("tagged %s %s", printable_type(tag
->tagged
),
366 describe_object(tag
->tagged
));
367 printf(" (%s) in %s\n", tag
->tag
,
368 describe_object(&tag
->object
));
373 if (obj
->type
== OBJ_TREE
)
374 free_tree_buffer((struct tree
*)obj
);
375 if (obj
->type
== OBJ_COMMIT
)
376 free_commit_buffer((struct commit
*)obj
);
380 static int fsck_obj_buffer(const struct object_id
*oid
, enum object_type type
,
381 unsigned long size
, void *buffer
, int *eaten
)
384 * Note, buffer may be NULL if type is OBJ_BLOB. See
385 * verify_packfile(), data_valid variable for details.
388 obj
= parse_object_buffer(oid
, type
, size
, buffer
, eaten
);
390 errors_found
|= ERROR_OBJECT
;
391 return error("%s: object corrupt or missing", oid_to_hex(oid
));
393 obj
->flags
&= ~(REACHABLE
| SEEN
);
394 obj
->flags
|= HAS_OBJ
;
395 return fsck_obj(obj
);
398 static int default_refs
;
400 static void fsck_handle_reflog_oid(const char *refname
, struct object_id
*oid
,
401 timestamp_t timestamp
)
405 if (!is_null_oid(oid
)) {
406 obj
= lookup_object(oid
->hash
);
407 if (obj
&& (obj
->flags
& HAS_OBJ
)) {
408 if (timestamp
&& name_objects
)
409 add_decoration(fsck_walk_options
.object_names
,
411 xstrfmt("%s@{%"PRItime
"}", refname
, timestamp
));
413 mark_object_reachable(obj
);
414 } else if (!is_promisor_object(oid
)) {
415 error("%s: invalid reflog entry %s", refname
, oid_to_hex(oid
));
416 errors_found
|= ERROR_REACHABLE
;
421 static int fsck_handle_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
422 const char *email
, timestamp_t timestamp
, int tz
,
423 const char *message
, void *cb_data
)
425 const char *refname
= cb_data
;
428 fprintf(stderr
, "Checking reflog %s->%s\n",
429 oid_to_hex(ooid
), oid_to_hex(noid
));
431 fsck_handle_reflog_oid(refname
, ooid
, 0);
432 fsck_handle_reflog_oid(refname
, noid
, timestamp
);
436 static int fsck_handle_reflog(const char *logname
, const struct object_id
*oid
,
437 int flag
, void *cb_data
)
439 for_each_reflog_ent(logname
, fsck_handle_reflog_ent
, (void *)logname
);
443 static int fsck_handle_ref(const char *refname
, const struct object_id
*oid
,
444 int flag
, void *cb_data
)
448 obj
= parse_object(oid
);
450 if (is_promisor_object(oid
)) {
452 * Increment default_refs anyway, because this is a
458 error("%s: invalid sha1 pointer %s", refname
, oid_to_hex(oid
));
459 errors_found
|= ERROR_REACHABLE
;
460 /* We'll continue with the rest despite the error.. */
463 if (obj
->type
!= OBJ_COMMIT
&& is_branch(refname
)) {
464 error("%s: not a commit", refname
);
465 errors_found
|= ERROR_REFS
;
470 add_decoration(fsck_walk_options
.object_names
,
471 obj
, xstrdup(refname
));
472 mark_object_reachable(obj
);
477 static void get_default_heads(void)
479 if (head_points_at
&& !is_null_oid(&head_oid
))
480 fsck_handle_ref("HEAD", &head_oid
, 0, NULL
);
481 for_each_rawref(fsck_handle_ref
, NULL
);
483 for_each_reflog(fsck_handle_reflog
, NULL
);
486 * Not having any default heads isn't really fatal, but
487 * it does mean that "--unreachable" no longer makes any
488 * sense (since in this case everything will obviously
489 * be unreachable by definition.
491 * Showing dangling objects is valid, though (as those
492 * dangling objects are likely lost heads).
494 * So we just print a warning about it, and clear the
495 * "show_unreachable" flag.
498 fprintf(stderr
, "notice: No default references\n");
499 show_unreachable
= 0;
503 static struct object
*parse_loose_object(const struct object_id
*oid
,
508 enum object_type type
;
512 if (read_loose_object(path
, oid
->hash
, &type
, &size
, &contents
) < 0)
515 if (!contents
&& type
!= OBJ_BLOB
)
516 die("BUG: read_loose_object streamed a non-blob");
518 obj
= parse_object_buffer(oid
, type
, size
, contents
, &eaten
);
525 static int fsck_loose(const struct object_id
*oid
, const char *path
, void *data
)
527 struct object
*obj
= parse_loose_object(oid
, path
);
530 errors_found
|= ERROR_OBJECT
;
531 error("%s: object corrupt or missing: %s",
532 oid_to_hex(oid
), path
);
533 return 0; /* keep checking other objects */
536 obj
->flags
&= ~(REACHABLE
| SEEN
);
537 obj
->flags
|= HAS_OBJ
;
539 errors_found
|= ERROR_OBJECT
;
543 static int fsck_cruft(const char *basename
, const char *path
, void *data
)
545 if (!starts_with(basename
, "tmp_obj_"))
546 fprintf(stderr
, "bad sha1 file: %s\n", path
);
550 static int fsck_subdir(unsigned int nr
, const char *path
, void *progress
)
552 display_progress(progress
, nr
+ 1);
556 static void fsck_object_dir(const char *path
)
558 struct progress
*progress
= NULL
;
561 fprintf(stderr
, "Checking object directory\n");
564 progress
= start_progress(_("Checking object directories"), 256);
566 for_each_loose_file_in_objdir(path
, fsck_loose
, fsck_cruft
, fsck_subdir
,
568 display_progress(progress
, 256);
569 stop_progress(&progress
);
572 static int fsck_head_link(void)
574 int null_is_error
= 0;
577 fprintf(stderr
, "Checking HEAD link\n");
579 head_points_at
= resolve_ref_unsafe("HEAD", 0, &head_oid
, NULL
);
580 if (!head_points_at
) {
581 errors_found
|= ERROR_REFS
;
582 return error("Invalid HEAD");
584 if (!strcmp(head_points_at
, "HEAD"))
587 else if (!starts_with(head_points_at
, "refs/heads/")) {
588 errors_found
|= ERROR_REFS
;
589 return error("HEAD points to something strange (%s)",
592 if (is_null_oid(&head_oid
)) {
594 errors_found
|= ERROR_REFS
;
595 return error("HEAD: detached HEAD points at nothing");
597 fprintf(stderr
, "notice: HEAD points to an unborn branch (%s)\n",
598 head_points_at
+ 11);
603 static int fsck_cache_tree(struct cache_tree
*it
)
609 fprintf(stderr
, "Checking cache tree\n");
611 if (0 <= it
->entry_count
) {
612 struct object
*obj
= parse_object(&it
->oid
);
614 error("%s: invalid sha1 pointer in cache-tree",
615 oid_to_hex(&it
->oid
));
616 errors_found
|= ERROR_REFS
;
621 add_decoration(fsck_walk_options
.object_names
,
623 mark_object_reachable(obj
);
624 if (obj
->type
!= OBJ_TREE
)
625 err
|= objerror(obj
, "non-tree in cache-tree");
627 for (i
= 0; i
< it
->subtree_nr
; i
++)
628 err
|= fsck_cache_tree(it
->down
[i
]->cache_tree
);
632 static void mark_object_for_connectivity(const struct object_id
*oid
)
634 struct object
*obj
= lookup_unknown_object(oid
->hash
);
635 obj
->flags
|= HAS_OBJ
;
638 static int mark_loose_for_connectivity(const struct object_id
*oid
,
642 mark_object_for_connectivity(oid
);
646 static int mark_packed_for_connectivity(const struct object_id
*oid
,
647 struct packed_git
*pack
,
651 mark_object_for_connectivity(oid
);
655 static char const * const fsck_usage
[] = {
656 N_("git fsck [<options>] [<object>...]"),
660 static struct option fsck_opts
[] = {
661 OPT__VERBOSE(&verbose
, N_("be verbose")),
662 OPT_BOOL(0, "unreachable", &show_unreachable
, N_("show unreachable objects")),
663 OPT_BOOL(0, "dangling", &show_dangling
, N_("show dangling objects")),
664 OPT_BOOL(0, "tags", &show_tags
, N_("report tags")),
665 OPT_BOOL(0, "root", &show_root
, N_("report root nodes")),
666 OPT_BOOL(0, "cache", &keep_cache_objects
, N_("make index objects head nodes")),
667 OPT_BOOL(0, "reflogs", &include_reflogs
, N_("make reflogs head nodes (default)")),
668 OPT_BOOL(0, "full", &check_full
, N_("also consider packs and alternate objects")),
669 OPT_BOOL(0, "connectivity-only", &connectivity_only
, N_("check only connectivity")),
670 OPT_BOOL(0, "strict", &check_strict
, N_("enable more strict checking")),
671 OPT_BOOL(0, "lost-found", &write_lost_and_found
,
672 N_("write dangling objects in .git/lost-found")),
673 OPT_BOOL(0, "progress", &show_progress
, N_("show progress")),
674 OPT_BOOL(0, "name-objects", &name_objects
, N_("show verbose names for reachable objects")),
678 int cmd_fsck(int argc
, const char **argv
, const char *prefix
)
681 struct alternate_object_database
*alt
;
683 /* fsck knows how to handle missing promisor objects */
684 fetch_if_missing
= 0;
687 check_replace_refs
= 0;
689 argc
= parse_options(argc
, argv
, prefix
, fsck_opts
, fsck_usage
, 0);
691 fsck_walk_options
.walk
= mark_object
;
692 fsck_obj_options
.walk
= mark_used
;
693 fsck_obj_options
.error_func
= fsck_error_func
;
695 fsck_obj_options
.strict
= 1;
697 if (show_progress
== -1)
698 show_progress
= isatty(2);
702 if (write_lost_and_found
) {
708 fsck_walk_options
.object_names
=
709 xcalloc(1, sizeof(struct decoration
));
711 git_config(fsck_config
, NULL
);
714 if (connectivity_only
) {
715 for_each_loose_object(mark_loose_for_connectivity
, NULL
, 0);
716 for_each_packed_object(mark_packed_for_connectivity
, NULL
, 0);
718 struct alternate_object_database
*alt_odb_list
;
720 fsck_object_dir(get_object_directory());
722 prepare_alt_odb(the_repository
);
723 alt_odb_list
= the_repository
->objects
->alt_odb_list
;
724 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
)
725 fsck_object_dir(alt
->path
);
728 struct packed_git
*p
;
729 uint32_t total
= 0, count
= 0;
730 struct progress
*progress
= NULL
;
733 for (p
= get_packed_git(the_repository
); p
;
735 if (open_pack_index(p
))
737 total
+= p
->num_objects
;
740 progress
= start_progress(_("Checking objects"), total
);
742 for (p
= get_packed_git(the_repository
); p
;
744 /* verify gives error messages itself */
745 if (verify_pack(p
, fsck_obj_buffer
,
747 errors_found
|= ERROR_PACK
;
748 count
+= p
->num_objects
;
750 stop_progress(&progress
);
754 for (i
= 0; i
< argc
; i
++) {
755 const char *arg
= argv
[i
];
756 struct object_id oid
;
757 if (!get_oid(arg
, &oid
)) {
758 struct object
*obj
= lookup_object(oid
.hash
);
760 if (!obj
|| !(obj
->flags
& HAS_OBJ
)) {
761 if (is_promisor_object(&oid
))
763 error("%s: object missing", oid_to_hex(&oid
));
764 errors_found
|= ERROR_OBJECT
;
770 add_decoration(fsck_walk_options
.object_names
,
772 mark_object_reachable(obj
);
775 error("invalid parameter: expected sha1, got '%s'", arg
);
776 errors_found
|= ERROR_OBJECT
;
780 * If we've not been given any explicit head information, do the
781 * default ones from .git/refs. We also consider the index file
782 * in this case (ie this implies --cache).
786 keep_cache_objects
= 1;
789 if (keep_cache_objects
) {
790 verify_index_checksum
= 1;
793 for (i
= 0; i
< active_nr
; i
++) {
798 mode
= active_cache
[i
]->ce_mode
;
799 if (S_ISGITLINK(mode
))
801 blob
= lookup_blob(&active_cache
[i
]->oid
);
807 add_decoration(fsck_walk_options
.object_names
,
809 xstrfmt(":%s", active_cache
[i
]->name
));
810 mark_object_reachable(obj
);
812 if (active_cache_tree
)
813 fsck_cache_tree(active_cache_tree
);
816 check_connectivity();