1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
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 "run-command.h"
25 #define REACHABLE 0x0001
27 #define HAS_OBJ 0x0004
28 /* This flag is set if something points to this object. */
33 static int show_unreachable;
34 static int include_reflogs = 1;
35 static int check_full = 1;
36 static int connectivity_only;
37 static int check_strict;
38 static int keep_cache_objects;
39 static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
40 static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
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 bufs[] = {
56 STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
63 name = lookup_decoration(fsck_walk_options.object_names, obj);
66 b = (b + 1) % ARRAY_SIZE(bufs);
68 strbuf_addstr(buf, oid_to_hex(&obj->oid));
70 strbuf_addf(buf, " (%s)", name);
75 static const char *printable_type(struct object *obj)
79 if (obj->type == OBJ_NONE) {
80 enum object_type type = oid_object_info(the_repository,
83 object_as_type(the_repository, obj, type, 0);
86 ret = type_name(obj->type);
93 static int fsck_config(const char *var, const char *value, void *cb)
95 if (strcmp(var, "fsck.skiplist") == 0) {
97 struct strbuf sb = STRBUF_INIT;
99 if (git_config_pathname(&path, var, value))
101 strbuf_addf(&sb, "skiplist=%s", path);
103 fsck_set_msg_types(&fsck_obj_options, sb.buf);
108 if (skip_prefix(var, "fsck.", &var)) {
109 fsck_set_msg_type(&fsck_obj_options, var, value);
113 return git_default_config(var, value, cb);
116 static int objerror(struct object *obj, const char *err)
118 errors_found |= ERROR_OBJECT;
119 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
120 fprintf_ln(stderr, _("error in %s %s: %s"),
121 printable_type(obj), describe_object(obj), err);
125 static int fsck_error_func(struct fsck_options *o,
126 struct object *obj, int type, const char *message)
130 /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
131 fprintf_ln(stderr, _("warning in %s %s: %s"),
132 printable_type(obj), describe_object(obj), message);
135 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
136 fprintf_ln(stderr, _("error in %s %s: %s"),
137 printable_type(obj), describe_object(obj), message);
140 BUG("%d (FSCK_IGNORE?) should never trigger this callback", type);
144 static struct object_array pending;
146 static int mark_object(struct object *obj, int type, void *data, struct fsck_options *options)
148 struct object *parent = data;
151 * The only case data is NULL or type is OBJ_ANY is when
152 * mark_object_reachable() calls us. All the callers of
153 * that function has non-NULL obj hence ...
156 /* ... these references to parent->fld are safe here */
157 printf_ln(_("broken link from %7s %s"),
158 printable_type(parent), describe_object(parent));
159 printf_ln(_("broken link from %7s %s"),
160 (type == OBJ_ANY ? _("unknown") : type_name(type)),
162 errors_found |= ERROR_REACHABLE;
166 if (type != OBJ_ANY && obj->type != type)
167 /* ... and the reference to parent is safe here */
168 objerror(parent, _("wrong object type in link"));
170 if (obj->flags & REACHABLE)
172 obj->flags |= REACHABLE;
174 if (is_promisor_object(&obj->oid))
176 * Further recursion does not need to be performed on this
177 * object since it is a promisor object (so it does not need to
178 * be added to "pending").
182 if (!(obj->flags & HAS_OBJ)) {
183 if (parent && !has_object_file(&obj->oid)) {
184 printf_ln(_("broken link from %7s %s\n"
186 printable_type(parent),
187 describe_object(parent),
189 describe_object(obj));
190 errors_found |= ERROR_REACHABLE;
195 add_object_array(obj, NULL, &pending);
199 static void mark_object_reachable(struct object *obj)
201 mark_object(obj, OBJ_ANY, NULL, NULL);
204 static int traverse_one_object(struct object *obj)
206 int result = fsck_walk(obj, obj, &fsck_walk_options);
208 if (obj->type == OBJ_TREE) {
209 struct tree *tree = (struct tree *)obj;
210 free_tree_buffer(tree);
215 static int traverse_reachable(void)
217 struct progress *progress = NULL;
221 progress = start_delayed_progress(_("Checking connectivity"), 0);
223 result |= traverse_one_object(object_array_pop(&pending));
224 display_progress(progress, ++nr);
226 stop_progress(&progress);
230 static int mark_used(struct object *obj, int type, void *data, struct fsck_options *options)
239 * Check a single reachable object
241 static void check_reachable_object(struct object *obj)
244 * We obviously want the object to be parsed,
245 * except if it was in a pack-file and we didn't
248 if (!(obj->flags & HAS_OBJ)) {
249 if (is_promisor_object(&obj->oid))
251 if (has_object_pack(&obj->oid))
252 return; /* it is in pack - forget about it */
253 printf_ln(_("missing %s %s"), printable_type(obj),
254 describe_object(obj));
255 errors_found |= ERROR_REACHABLE;
261 * Check a single unreachable object
263 static void check_unreachable_object(struct object *obj)
266 * Missing unreachable object? Ignore it. It's not like
267 * we miss it (since it can't be reached), nor do we want
268 * to complain about it being unreachable (since it does
271 if (!(obj->flags & HAS_OBJ))
275 * Unreachable object that exists? Show it if asked to,
276 * since this is something that is prunable.
278 if (show_unreachable) {
279 printf_ln(_("unreachable %s %s"), printable_type(obj),
280 describe_object(obj));
285 * "!USED" means that nothing at all points to it, including
286 * other unreachable objects. In other words, it's the "tip"
287 * of some set of unreachable objects, usually a commit that
290 * Such starting points are more interesting than some random
291 * set of unreachable objects, so we show them even if the user
292 * hasn't asked for _all_ unreachable objects. If you have
293 * deleted a branch by mistake, this is a prime candidate to
294 * start looking at, for example.
296 if (!(obj->flags & USED)) {
298 printf_ln(_("dangling %s %s"), printable_type(obj),
299 describe_object(obj));
300 if (write_lost_and_found) {
301 char *filename = git_pathdup("lost-found/%s/%s",
302 obj->type == OBJ_COMMIT ? "commit" : "other",
303 describe_object(obj));
306 if (safe_create_leading_directories_const(filename)) {
307 error(_("could not create lost-found"));
311 f = xfopen(filename, "w");
312 if (obj->type == OBJ_BLOB) {
313 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
314 die_errno(_("could not write '%s'"), filename);
316 fprintf(f, "%s\n", describe_object(obj));
318 die_errno(_("could not finish '%s'"),
326 * Otherwise? It's there, it's unreachable, and some other unreachable
327 * object points to it. Ignore it - it's not interesting, and we showed
328 * all the interesting cases above.
332 static void check_object(struct object *obj)
335 fprintf_ln(stderr, _("Checking %s"), describe_object(obj));
337 if (obj->flags & REACHABLE)
338 check_reachable_object(obj);
340 check_unreachable_object(obj);
343 static void check_connectivity(void)
347 /* Traverse the pending reachable objects */
348 traverse_reachable();
350 /* Look up all the requirements, warn about missing objects.. */
351 max = get_max_object_index();
353 fprintf_ln(stderr, _("Checking connectivity (%d objects)"), max);
355 for (i = 0; i < max; i++) {
356 struct object *obj = get_indexed_object(i);
363 static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
367 if (obj->flags & SEEN)
372 fprintf_ln(stderr, _("Checking %s %s"),
373 printable_type(obj), describe_object(obj));
375 if (fsck_walk(obj, NULL, &fsck_obj_options))
376 objerror(obj, _("broken links"));
377 err = fsck_object(obj, buffer, size, &fsck_obj_options);
381 if (obj->type == OBJ_COMMIT) {
382 struct commit *commit = (struct commit *) obj;
384 if (!commit->parents && show_root)
385 printf_ln(_("root %s"),
386 describe_object(&commit->object));
389 if (obj->type == OBJ_TAG) {
390 struct tag *tag = (struct tag *) obj;
392 if (show_tags && tag->tagged) {
393 printf_ln(_("tagged %s %s (%s) in %s"),
394 printable_type(tag->tagged),
395 describe_object(tag->tagged),
397 describe_object(&tag->object));
402 if (obj->type == OBJ_TREE)
403 free_tree_buffer((struct tree *)obj);
404 if (obj->type == OBJ_COMMIT)
405 free_commit_buffer(the_repository->parsed_objects,
406 (struct commit *)obj);
410 static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
411 unsigned long size, void *buffer, int *eaten)
414 * Note, buffer may be NULL if type is OBJ_BLOB. See
415 * verify_packfile(), data_valid variable for details.
418 obj = parse_object_buffer(the_repository, oid, type, size, buffer,
421 errors_found |= ERROR_OBJECT;
422 return error(_("%s: object corrupt or missing"),
425 obj->flags &= ~(REACHABLE | SEEN);
426 obj->flags |= HAS_OBJ;
427 return fsck_obj(obj, buffer, size);
430 static int default_refs;
432 static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
433 timestamp_t timestamp)
437 if (!is_null_oid(oid)) {
438 obj = lookup_object(the_repository, oid->hash);
439 if (obj && (obj->flags & HAS_OBJ)) {
440 if (timestamp && name_objects)
441 add_decoration(fsck_walk_options.object_names,
443 xstrfmt("%s@{%"PRItime"}", refname, timestamp));
445 mark_object_reachable(obj);
446 } else if (!is_promisor_object(oid)) {
447 error(_("%s: invalid reflog entry %s"),
448 refname, oid_to_hex(oid));
449 errors_found |= ERROR_REACHABLE;
454 static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
455 const char *email, timestamp_t timestamp, int tz,
456 const char *message, void *cb_data)
458 const char *refname = cb_data;
461 fprintf_ln(stderr, _("Checking reflog %s->%s"),
462 oid_to_hex(ooid), oid_to_hex(noid));
464 fsck_handle_reflog_oid(refname, ooid, 0);
465 fsck_handle_reflog_oid(refname, noid, timestamp);
469 static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
470 int flag, void *cb_data)
472 struct strbuf refname = STRBUF_INIT;
474 strbuf_worktree_ref(cb_data, &refname, logname);
475 for_each_reflog_ent(refname.buf, fsck_handle_reflog_ent, refname.buf);
476 strbuf_release(&refname);
480 static int fsck_handle_ref(const char *refname, const struct object_id *oid,
481 int flag, void *cb_data)
485 obj = parse_object(the_repository, oid);
487 if (is_promisor_object(oid)) {
489 * Increment default_refs anyway, because this is a
495 error(_("%s: invalid sha1 pointer %s"),
496 refname, oid_to_hex(oid));
497 errors_found |= ERROR_REACHABLE;
498 /* We'll continue with the rest despite the error.. */
501 if (obj->type != OBJ_COMMIT && is_branch(refname)) {
502 error(_("%s: not a commit"), refname);
503 errors_found |= ERROR_REFS;
508 add_decoration(fsck_walk_options.object_names,
509 obj, xstrdup(refname));
510 mark_object_reachable(obj);
515 static int fsck_head_link(const char *head_ref_name,
516 const char **head_points_at,
517 struct object_id *head_oid);
519 static void get_default_heads(void)
521 struct worktree **worktrees, **p;
522 const char *head_points_at;
523 struct object_id head_oid;
525 for_each_rawref(fsck_handle_ref, NULL);
527 worktrees = get_worktrees(0);
528 for (p = worktrees; *p; p++) {
529 struct worktree *wt = *p;
530 struct strbuf ref = STRBUF_INIT;
532 strbuf_worktree_ref(wt, &ref, "HEAD");
533 fsck_head_link(ref.buf, &head_points_at, &head_oid);
534 if (head_points_at && !is_null_oid(&head_oid))
535 fsck_handle_ref(ref.buf, &head_oid, 0, NULL);
536 strbuf_release(&ref);
539 refs_for_each_reflog(get_worktree_ref_store(wt),
540 fsck_handle_reflog, wt);
542 free_worktrees(worktrees);
545 * Not having any default heads isn't really fatal, but
546 * it does mean that "--unreachable" no longer makes any
547 * sense (since in this case everything will obviously
548 * be unreachable by definition.
550 * Showing dangling objects is valid, though (as those
551 * dangling objects are likely lost heads).
553 * So we just print a warning about it, and clear the
554 * "show_unreachable" flag.
557 fprintf_ln(stderr, _("notice: No default references"));
558 show_unreachable = 0;
562 static int fsck_loose(const struct object_id *oid, const char *path, void *data)
565 enum object_type type;
570 if (read_loose_object(path, oid, &type, &size, &contents) < 0) {
571 errors_found |= ERROR_OBJECT;
572 error(_("%s: object corrupt or missing: %s"),
573 oid_to_hex(oid), path);
574 return 0; /* keep checking other objects */
577 if (!contents && type != OBJ_BLOB)
578 BUG("read_loose_object streamed a non-blob");
580 obj = parse_object_buffer(the_repository, oid, type, size,
584 errors_found |= ERROR_OBJECT;
585 error(_("%s: object could not be parsed: %s"),
586 oid_to_hex(oid), path);
589 return 0; /* keep checking other objects */
592 obj->flags &= ~(REACHABLE | SEEN);
593 obj->flags |= HAS_OBJ;
594 if (fsck_obj(obj, contents, size))
595 errors_found |= ERROR_OBJECT;
599 return 0; /* keep checking other objects, even if we saw an error */
602 static int fsck_cruft(const char *basename, const char *path, void *data)
604 if (!starts_with(basename, "tmp_obj_"))
605 fprintf_ln(stderr, _("bad sha1 file: %s"), path);
609 static int fsck_subdir(unsigned int nr, const char *path, void *progress)
611 display_progress(progress, nr + 1);
615 static void fsck_object_dir(const char *path)
617 struct progress *progress = NULL;
620 fprintf_ln(stderr, _("Checking object directory"));
623 progress = start_progress(_("Checking object directories"), 256);
625 for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
627 display_progress(progress, 256);
628 stop_progress(&progress);
631 static int fsck_head_link(const char *head_ref_name,
632 const char **head_points_at,
633 struct object_id *head_oid)
635 int null_is_error = 0;
638 fprintf_ln(stderr, _("Checking %s link"), head_ref_name);
640 *head_points_at = resolve_ref_unsafe(head_ref_name, 0, head_oid, NULL);
641 if (!*head_points_at) {
642 errors_found |= ERROR_REFS;
643 return error(_("invalid %s"), head_ref_name);
645 if (!strcmp(*head_points_at, head_ref_name))
648 else if (!starts_with(*head_points_at, "refs/heads/")) {
649 errors_found |= ERROR_REFS;
650 return error(_("%s points to something strange (%s)"),
651 head_ref_name, *head_points_at);
653 if (is_null_oid(head_oid)) {
655 errors_found |= ERROR_REFS;
656 return error(_("%s: detached HEAD points at nothing"),
660 _("notice: %s points to an unborn branch (%s)"),
661 head_ref_name, *head_points_at + 11);
666 static int fsck_cache_tree(struct cache_tree *it)
672 fprintf_ln(stderr, _("Checking cache tree"));
674 if (0 <= it->entry_count) {
675 struct object *obj = parse_object(the_repository, &it->oid);
677 error(_("%s: invalid sha1 pointer in cache-tree"),
678 oid_to_hex(&it->oid));
679 errors_found |= ERROR_REFS;
684 add_decoration(fsck_walk_options.object_names,
686 mark_object_reachable(obj);
687 if (obj->type != OBJ_TREE)
688 err |= objerror(obj, _("non-tree in cache-tree"));
690 for (i = 0; i < it->subtree_nr; i++)
691 err |= fsck_cache_tree(it->down[i]->cache_tree);
695 static void mark_object_for_connectivity(const struct object_id *oid)
697 struct object *obj = lookup_unknown_object(oid->hash);
698 obj->flags |= HAS_OBJ;
701 static int mark_loose_for_connectivity(const struct object_id *oid,
705 mark_object_for_connectivity(oid);
709 static int mark_packed_for_connectivity(const struct object_id *oid,
710 struct packed_git *pack,
714 mark_object_for_connectivity(oid);
718 static char const * const fsck_usage[] = {
719 N_("git fsck [<options>] [<object>...]"),
723 static struct option fsck_opts[] = {
724 OPT__VERBOSE(&verbose, N_("be verbose")),
725 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
726 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
727 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
728 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
729 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
730 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
731 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
732 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
733 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
734 OPT_BOOL(0, "lost-found", &write_lost_and_found,
735 N_("write dangling objects in .git/lost-found")),
736 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
737 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
741 int cmd_fsck(int argc, const char **argv, const char *prefix)
744 struct object_directory *odb;
746 /* fsck knows how to handle missing promisor objects */
747 fetch_if_missing = 0;
750 read_replace_refs = 0;
752 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
754 fsck_walk_options.walk = mark_object;
755 fsck_obj_options.walk = mark_used;
756 fsck_obj_options.error_func = fsck_error_func;
758 fsck_obj_options.strict = 1;
760 if (show_progress == -1)
761 show_progress = isatty(2);
765 if (write_lost_and_found) {
771 fsck_walk_options.object_names =
772 xcalloc(1, sizeof(struct decoration));
774 git_config(fsck_config, NULL);
776 if (connectivity_only) {
777 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
778 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
780 prepare_alt_odb(the_repository);
781 for (odb = the_repository->objects->odb; odb; odb = odb->next)
782 fsck_object_dir(odb->path);
785 struct packed_git *p;
786 uint32_t total = 0, count = 0;
787 struct progress *progress = NULL;
790 for (p = get_all_packs(the_repository); p;
792 if (open_pack_index(p))
794 total += p->num_objects;
797 progress = start_progress(_("Checking objects"), total);
799 for (p = get_all_packs(the_repository); p;
801 /* verify gives error messages itself */
802 if (verify_pack(the_repository,
805 errors_found |= ERROR_PACK;
806 count += p->num_objects;
808 stop_progress(&progress);
811 if (fsck_finish(&fsck_obj_options))
812 errors_found |= ERROR_OBJECT;
815 for (i = 0; i < argc; i++) {
816 const char *arg = argv[i];
817 struct object_id oid;
818 if (!get_oid(arg, &oid)) {
819 struct object *obj = lookup_object(the_repository,
822 if (!obj || !(obj->flags & HAS_OBJ)) {
823 if (is_promisor_object(&oid))
825 error(_("%s: object missing"), oid_to_hex(&oid));
826 errors_found |= ERROR_OBJECT;
832 add_decoration(fsck_walk_options.object_names,
834 mark_object_reachable(obj);
837 error(_("invalid parameter: expected sha1, got '%s'"), arg);
838 errors_found |= ERROR_OBJECT;
842 * If we've not been given any explicit head information, do the
843 * default ones from .git/refs. We also consider the index file
844 * in this case (ie this implies --cache).
848 keep_cache_objects = 1;
851 if (keep_cache_objects) {
852 verify_index_checksum = 1;
855 for (i = 0; i < active_nr; i++) {
860 mode = active_cache[i]->ce_mode;
861 if (S_ISGITLINK(mode))
863 blob = lookup_blob(the_repository,
864 &active_cache[i]->oid);
870 add_decoration(fsck_walk_options.object_names,
872 xstrfmt(":%s", active_cache[i]->name));
873 mark_object_reachable(obj);
875 if (active_cache_tree)
876 fsck_cache_tree(active_cache_tree);
879 check_connectivity();
881 if (!git_config_get_bool("core.commitgraph", &i) && i) {
882 struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
883 const char *verify_argv[] = { "commit-graph", "verify", NULL, NULL, NULL };
885 prepare_alt_odb(the_repository);
886 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
887 child_process_init(&commit_graph_verify);
888 commit_graph_verify.argv = verify_argv;
889 commit_graph_verify.git_cmd = 1;
890 verify_argv[2] = "--object-dir";
891 verify_argv[3] = odb->path;
892 if (run_command(&commit_graph_verify))
893 errors_found |= ERROR_COMMIT_GRAPH;
897 if (!git_config_get_bool("core.multipackindex", &i) && i) {
898 struct child_process midx_verify = CHILD_PROCESS_INIT;
899 const char *midx_argv[] = { "multi-pack-index", "verify", NULL, NULL, NULL };
901 prepare_alt_odb(the_repository);
902 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
903 child_process_init(&midx_verify);
904 midx_verify.argv = midx_argv;
905 midx_verify.git_cmd = 1;
906 midx_argv[2] = "--object-dir";
907 midx_argv[3] = odb->path;
908 if (run_command(&midx_verify))
909 errors_found |= ERROR_COMMIT_GRAPH;