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"
24 #define REACHABLE 0x0001
26 #define HAS_OBJ 0x0004
27 /* This flag is set if something points to this object. */
32 static int show_unreachable;
33 static int include_reflogs = 1;
34 static int check_full = 1;
35 static int connectivity_only;
36 static int check_strict;
37 static int keep_cache_objects;
38 static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
39 static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
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
50 #define ERROR_COMMIT_GRAPH 020
52 static const char *describe_object(struct object *obj)
54 static struct strbuf buf = STRBUF_INIT;
55 char *name = name_objects ?
56 lookup_decoration(fsck_walk_options.object_names, obj) : NULL;
59 strbuf_addstr(&buf, oid_to_hex(&obj->oid));
61 strbuf_addf(&buf, " (%s)", name);
66 static const char *printable_type(struct object *obj)
70 if (obj->type == OBJ_NONE) {
71 enum object_type type = oid_object_info(the_repository,
74 object_as_type(the_repository, obj, type, 0);
77 ret = type_name(obj->type);
84 static int fsck_config(const char *var, const char *value, void *cb)
86 if (strcmp(var, "fsck.skiplist") == 0) {
88 struct strbuf sb = STRBUF_INIT;
90 if (git_config_pathname(&path, var, value))
92 strbuf_addf(&sb, "skiplist=%s", path);
94 fsck_set_msg_types(&fsck_obj_options, sb.buf);
99 if (skip_prefix(var, "fsck.", &var)) {
100 fsck_set_msg_type(&fsck_obj_options, var, value);
104 return git_default_config(var, value, cb);
107 static void objreport(struct object *obj, const char *msg_type,
110 fprintf(stderr, "%s in %s %s: %s\n",
111 msg_type, printable_type(obj), describe_object(obj), err);
114 static int objerror(struct object *obj, const char *err)
116 errors_found |= ERROR_OBJECT;
117 objreport(obj, "error", err);
121 static int fsck_error_func(struct fsck_options *o,
122 struct object *obj, int type, const char *message)
124 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", message);
125 return (type == FSCK_WARN) ? 0 : 1;
128 static struct object_array pending;
130 static int mark_object(struct object *obj, int type, void *data, struct fsck_options *options)
132 struct object *parent = data;
135 * The only case data is NULL or type is OBJ_ANY is when
136 * mark_object_reachable() calls us. All the callers of
137 * that function has non-NULL obj hence ...
140 /* ... these references to parent->fld are safe here */
141 printf("broken link from %7s %s\n",
142 printable_type(parent), describe_object(parent));
143 printf("broken link from %7s %s\n",
144 (type == OBJ_ANY ? "unknown" : type_name(type)), "unknown");
145 errors_found |= ERROR_REACHABLE;
149 if (type != OBJ_ANY && obj->type != type)
150 /* ... and the reference to parent is safe here */
151 objerror(parent, "wrong object type in link");
153 if (obj->flags & REACHABLE)
155 obj->flags |= REACHABLE;
157 if (is_promisor_object(&obj->oid))
159 * Further recursion does not need to be performed on this
160 * object since it is a promisor object (so it does not need to
161 * be added to "pending").
165 if (!(obj->flags & HAS_OBJ)) {
166 if (parent && !has_object_file(&obj->oid)) {
167 printf("broken link from %7s %s\n",
168 printable_type(parent), describe_object(parent));
169 printf(" to %7s %s\n",
170 printable_type(obj), describe_object(obj));
171 errors_found |= ERROR_REACHABLE;
176 add_object_array(obj, NULL, &pending);
180 static void mark_object_reachable(struct object *obj)
182 mark_object(obj, OBJ_ANY, NULL, NULL);
185 static int traverse_one_object(struct object *obj)
187 int result = fsck_walk(obj, obj, &fsck_walk_options);
189 if (obj->type == OBJ_TREE) {
190 struct tree *tree = (struct tree *)obj;
191 free_tree_buffer(tree);
196 static int traverse_reachable(void)
198 struct progress *progress = NULL;
202 progress = start_delayed_progress(_("Checking connectivity"), 0);
204 result |= traverse_one_object(object_array_pop(&pending));
205 display_progress(progress, ++nr);
207 stop_progress(&progress);
211 static int mark_used(struct object *obj, int type, void *data, struct fsck_options *options)
220 * Check a single reachable object
222 static void check_reachable_object(struct object *obj)
225 * We obviously want the object to be parsed,
226 * except if it was in a pack-file and we didn't
229 if (!(obj->flags & HAS_OBJ)) {
230 if (is_promisor_object(&obj->oid))
232 if (has_object_pack(&obj->oid))
233 return; /* it is in pack - forget about it */
234 printf("missing %s %s\n", printable_type(obj),
235 describe_object(obj));
236 errors_found |= ERROR_REACHABLE;
242 * Check a single unreachable object
244 static void check_unreachable_object(struct object *obj)
247 * Missing unreachable object? Ignore it. It's not like
248 * we miss it (since it can't be reached), nor do we want
249 * to complain about it being unreachable (since it does
252 if (!(obj->flags & HAS_OBJ))
256 * Unreachable object that exists? Show it if asked to,
257 * since this is something that is prunable.
259 if (show_unreachable) {
260 printf("unreachable %s %s\n", printable_type(obj),
261 describe_object(obj));
266 * "!USED" means that nothing at all points to it, including
267 * other unreachable objects. In other words, it's the "tip"
268 * of some set of unreachable objects, usually a commit that
271 * Such starting points are more interesting than some random
272 * set of unreachable objects, so we show them even if the user
273 * hasn't asked for _all_ unreachable objects. If you have
274 * deleted a branch by mistake, this is a prime candidate to
275 * start looking at, for example.
277 if (!(obj->flags & USED)) {
279 printf("dangling %s %s\n", printable_type(obj),
280 describe_object(obj));
281 if (write_lost_and_found) {
282 char *filename = git_pathdup("lost-found/%s/%s",
283 obj->type == OBJ_COMMIT ? "commit" : "other",
284 describe_object(obj));
287 if (safe_create_leading_directories_const(filename)) {
288 error("Could not create lost-found");
292 f = xfopen(filename, "w");
293 if (obj->type == OBJ_BLOB) {
294 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
295 die_errno("Could not write '%s'", filename);
297 fprintf(f, "%s\n", describe_object(obj));
299 die_errno("Could not finish '%s'",
307 * Otherwise? It's there, it's unreachable, and some other unreachable
308 * object points to it. Ignore it - it's not interesting, and we showed
309 * all the interesting cases above.
313 static void check_object(struct object *obj)
316 fprintf(stderr, "Checking %s\n", describe_object(obj));
318 if (obj->flags & REACHABLE)
319 check_reachable_object(obj);
321 check_unreachable_object(obj);
324 static void check_connectivity(void)
328 /* Traverse the pending reachable objects */
329 traverse_reachable();
331 /* Look up all the requirements, warn about missing objects.. */
332 max = get_max_object_index();
334 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
336 for (i = 0; i < max; i++) {
337 struct object *obj = get_indexed_object(i);
344 static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
348 if (obj->flags & SEEN)
353 fprintf(stderr, "Checking %s %s\n",
354 printable_type(obj), describe_object(obj));
356 if (fsck_walk(obj, NULL, &fsck_obj_options))
357 objerror(obj, "broken links");
358 err = fsck_object(obj, buffer, size, &fsck_obj_options);
362 if (obj->type == OBJ_COMMIT) {
363 struct commit *commit = (struct commit *) obj;
365 if (!commit->parents && show_root)
366 printf("root %s\n", describe_object(&commit->object));
369 if (obj->type == OBJ_TAG) {
370 struct tag *tag = (struct tag *) obj;
372 if (show_tags && tag->tagged) {
373 printf("tagged %s %s", printable_type(tag->tagged),
374 describe_object(tag->tagged));
375 printf(" (%s) in %s\n", tag->tag,
376 describe_object(&tag->object));
381 if (obj->type == OBJ_TREE)
382 free_tree_buffer((struct tree *)obj);
383 if (obj->type == OBJ_COMMIT)
384 free_commit_buffer((struct commit *)obj);
388 static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
389 unsigned long size, void *buffer, int *eaten)
392 * Note, buffer may be NULL if type is OBJ_BLOB. See
393 * verify_packfile(), data_valid variable for details.
396 obj = parse_object_buffer(the_repository, oid, type, size, buffer,
399 errors_found |= ERROR_OBJECT;
400 return error("%s: object corrupt or missing", oid_to_hex(oid));
402 obj->flags &= ~(REACHABLE | SEEN);
403 obj->flags |= HAS_OBJ;
404 return fsck_obj(obj, buffer, size);
407 static int default_refs;
409 static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
410 timestamp_t timestamp)
414 if (!is_null_oid(oid)) {
415 obj = lookup_object(the_repository, oid->hash);
416 if (obj && (obj->flags & HAS_OBJ)) {
417 if (timestamp && name_objects)
418 add_decoration(fsck_walk_options.object_names,
420 xstrfmt("%s@{%"PRItime"}", refname, timestamp));
422 mark_object_reachable(obj);
423 } else if (!is_promisor_object(oid)) {
424 error("%s: invalid reflog entry %s", refname, oid_to_hex(oid));
425 errors_found |= ERROR_REACHABLE;
430 static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
431 const char *email, timestamp_t timestamp, int tz,
432 const char *message, void *cb_data)
434 const char *refname = cb_data;
437 fprintf(stderr, "Checking reflog %s->%s\n",
438 oid_to_hex(ooid), oid_to_hex(noid));
440 fsck_handle_reflog_oid(refname, ooid, 0);
441 fsck_handle_reflog_oid(refname, noid, timestamp);
445 static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
446 int flag, void *cb_data)
448 struct strbuf refname = STRBUF_INIT;
450 strbuf_worktree_ref(cb_data, &refname, logname);
451 for_each_reflog_ent(refname.buf, fsck_handle_reflog_ent, refname.buf);
452 strbuf_release(&refname);
456 static int fsck_handle_ref(const char *refname, const struct object_id *oid,
457 int flag, void *cb_data)
461 obj = parse_object(the_repository, oid);
463 if (is_promisor_object(oid)) {
465 * Increment default_refs anyway, because this is a
471 error("%s: invalid sha1 pointer %s", refname, oid_to_hex(oid));
472 errors_found |= ERROR_REACHABLE;
473 /* We'll continue with the rest despite the error.. */
476 if (obj->type != OBJ_COMMIT && is_branch(refname)) {
477 error("%s: not a commit", refname);
478 errors_found |= ERROR_REFS;
483 add_decoration(fsck_walk_options.object_names,
484 obj, xstrdup(refname));
485 mark_object_reachable(obj);
490 static int fsck_head_link(const char *head_ref_name,
491 const char **head_points_at,
492 struct object_id *head_oid);
494 static void get_default_heads(void)
496 struct worktree **worktrees, **p;
497 const char *head_points_at;
498 struct object_id head_oid;
500 for_each_rawref(fsck_handle_ref, NULL);
502 worktrees = get_worktrees(0);
503 for (p = worktrees; *p; p++) {
504 struct worktree *wt = *p;
505 struct strbuf ref = STRBUF_INIT;
507 strbuf_worktree_ref(wt, &ref, "HEAD");
508 fsck_head_link(ref.buf, &head_points_at, &head_oid);
509 if (head_points_at && !is_null_oid(&head_oid))
510 fsck_handle_ref(ref.buf, &head_oid, 0, NULL);
511 strbuf_release(&ref);
514 refs_for_each_reflog(get_worktree_ref_store(wt),
515 fsck_handle_reflog, wt);
517 free_worktrees(worktrees);
520 * Not having any default heads isn't really fatal, but
521 * it does mean that "--unreachable" no longer makes any
522 * sense (since in this case everything will obviously
523 * be unreachable by definition.
525 * Showing dangling objects is valid, though (as those
526 * dangling objects are likely lost heads).
528 * So we just print a warning about it, and clear the
529 * "show_unreachable" flag.
532 fprintf(stderr, "notice: No default references\n");
533 show_unreachable = 0;
537 static int fsck_loose(const struct object_id *oid, const char *path, void *data)
540 enum object_type type;
545 if (read_loose_object(path, oid, &type, &size, &contents) < 0) {
546 errors_found |= ERROR_OBJECT;
547 error("%s: object corrupt or missing: %s",
548 oid_to_hex(oid), path);
549 return 0; /* keep checking other objects */
552 if (!contents && type != OBJ_BLOB)
553 BUG("read_loose_object streamed a non-blob");
555 obj = parse_object_buffer(the_repository, oid, type, size,
559 errors_found |= ERROR_OBJECT;
560 error("%s: object could not be parsed: %s",
561 oid_to_hex(oid), path);
564 return 0; /* keep checking other objects */
567 obj->flags &= ~(REACHABLE | SEEN);
568 obj->flags |= HAS_OBJ;
569 if (fsck_obj(obj, contents, size))
570 errors_found |= ERROR_OBJECT;
574 return 0; /* keep checking other objects, even if we saw an error */
577 static int fsck_cruft(const char *basename, const char *path, void *data)
579 if (!starts_with(basename, "tmp_obj_"))
580 fprintf(stderr, "bad sha1 file: %s\n", path);
584 static int fsck_subdir(unsigned int nr, const char *path, void *progress)
586 display_progress(progress, nr + 1);
590 static void fsck_object_dir(const char *path)
592 struct progress *progress = NULL;
595 fprintf(stderr, "Checking object directory\n");
598 progress = start_progress(_("Checking object directories"), 256);
600 for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
602 display_progress(progress, 256);
603 stop_progress(&progress);
606 static int fsck_head_link(const char *head_ref_name,
607 const char **head_points_at,
608 struct object_id *head_oid)
610 int null_is_error = 0;
613 fprintf(stderr, "Checking %s link\n", head_ref_name);
615 *head_points_at = resolve_ref_unsafe(head_ref_name, 0, head_oid, NULL);
616 if (!*head_points_at) {
617 errors_found |= ERROR_REFS;
618 return error("Invalid %s", head_ref_name);
620 if (!strcmp(*head_points_at, head_ref_name))
623 else if (!starts_with(*head_points_at, "refs/heads/")) {
624 errors_found |= ERROR_REFS;
625 return error("%s points to something strange (%s)",
626 head_ref_name, *head_points_at);
628 if (is_null_oid(head_oid)) {
630 errors_found |= ERROR_REFS;
631 return error("%s: detached HEAD points at nothing",
634 fprintf(stderr, "notice: %s points to an unborn branch (%s)\n",
635 head_ref_name, *head_points_at + 11);
640 static int fsck_cache_tree(struct cache_tree *it)
646 fprintf(stderr, "Checking cache tree\n");
648 if (0 <= it->entry_count) {
649 struct object *obj = parse_object(the_repository, &it->oid);
651 error("%s: invalid sha1 pointer in cache-tree",
652 oid_to_hex(&it->oid));
653 errors_found |= ERROR_REFS;
658 add_decoration(fsck_walk_options.object_names,
660 mark_object_reachable(obj);
661 if (obj->type != OBJ_TREE)
662 err |= objerror(obj, "non-tree in cache-tree");
664 for (i = 0; i < it->subtree_nr; i++)
665 err |= fsck_cache_tree(it->down[i]->cache_tree);
669 static void mark_object_for_connectivity(const struct object_id *oid)
671 struct object *obj = lookup_unknown_object(oid->hash);
672 obj->flags |= HAS_OBJ;
675 static int mark_loose_for_connectivity(const struct object_id *oid,
679 mark_object_for_connectivity(oid);
683 static int mark_packed_for_connectivity(const struct object_id *oid,
684 struct packed_git *pack,
688 mark_object_for_connectivity(oid);
692 static char const * const fsck_usage[] = {
693 N_("git fsck [<options>] [<object>...]"),
697 static struct option fsck_opts[] = {
698 OPT__VERBOSE(&verbose, N_("be verbose")),
699 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
700 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
701 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
702 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
703 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
704 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
705 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
706 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
707 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
708 OPT_BOOL(0, "lost-found", &write_lost_and_found,
709 N_("write dangling objects in .git/lost-found")),
710 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
711 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
715 int cmd_fsck(int argc, const char **argv, const char *prefix)
718 struct alternate_object_database *alt;
720 /* fsck knows how to handle missing promisor objects */
721 fetch_if_missing = 0;
724 read_replace_refs = 0;
726 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
728 fsck_walk_options.walk = mark_object;
729 fsck_obj_options.walk = mark_used;
730 fsck_obj_options.error_func = fsck_error_func;
732 fsck_obj_options.strict = 1;
734 if (show_progress == -1)
735 show_progress = isatty(2);
739 if (write_lost_and_found) {
745 fsck_walk_options.object_names =
746 xcalloc(1, sizeof(struct decoration));
748 git_config(fsck_config, NULL);
750 if (connectivity_only) {
751 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
752 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
754 struct alternate_object_database *alt_odb_list;
756 fsck_object_dir(get_object_directory());
758 prepare_alt_odb(the_repository);
759 alt_odb_list = the_repository->objects->alt_odb_list;
760 for (alt = alt_odb_list; alt; alt = alt->next)
761 fsck_object_dir(alt->path);
764 struct packed_git *p;
765 uint32_t total = 0, count = 0;
766 struct progress *progress = NULL;
769 for (p = get_all_packs(the_repository); p;
771 if (open_pack_index(p))
773 total += p->num_objects;
776 progress = start_progress(_("Checking objects"), total);
778 for (p = get_all_packs(the_repository); p;
780 /* verify gives error messages itself */
781 if (verify_pack(p, fsck_obj_buffer,
783 errors_found |= ERROR_PACK;
784 count += p->num_objects;
786 stop_progress(&progress);
789 if (fsck_finish(&fsck_obj_options))
790 errors_found |= ERROR_OBJECT;
793 for (i = 0; i < argc; i++) {
794 const char *arg = argv[i];
795 struct object_id oid;
796 if (!get_oid(arg, &oid)) {
797 struct object *obj = lookup_object(the_repository,
800 if (!obj || !(obj->flags & HAS_OBJ)) {
801 if (is_promisor_object(&oid))
803 error("%s: object missing", oid_to_hex(&oid));
804 errors_found |= ERROR_OBJECT;
810 add_decoration(fsck_walk_options.object_names,
812 mark_object_reachable(obj);
815 error("invalid parameter: expected sha1, got '%s'", arg);
816 errors_found |= ERROR_OBJECT;
820 * If we've not been given any explicit head information, do the
821 * default ones from .git/refs. We also consider the index file
822 * in this case (ie this implies --cache).
826 keep_cache_objects = 1;
829 if (keep_cache_objects) {
830 verify_index_checksum = 1;
833 for (i = 0; i < active_nr; i++) {
838 mode = active_cache[i]->ce_mode;
839 if (S_ISGITLINK(mode))
841 blob = lookup_blob(the_repository,
842 &active_cache[i]->oid);
848 add_decoration(fsck_walk_options.object_names,
850 xstrfmt(":%s", active_cache[i]->name));
851 mark_object_reachable(obj);
853 if (active_cache_tree)
854 fsck_cache_tree(active_cache_tree);
857 check_connectivity();
859 if (!git_config_get_bool("core.commitgraph", &i) && i) {
860 struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
861 const char *verify_argv[] = { "commit-graph", "verify", NULL, NULL, NULL };
863 commit_graph_verify.argv = verify_argv;
864 commit_graph_verify.git_cmd = 1;
865 if (run_command(&commit_graph_verify))
866 errors_found |= ERROR_COMMIT_GRAPH;
868 prepare_alt_odb(the_repository);
869 for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
870 verify_argv[2] = "--object-dir";
871 verify_argv[3] = alt->path;
872 if (run_command(&commit_graph_verify))
873 errors_found |= ERROR_COMMIT_GRAPH;
877 if (!git_config_get_bool("core.multipackindex", &i) && i) {
878 struct child_process midx_verify = CHILD_PROCESS_INIT;
879 const char *midx_argv[] = { "multi-pack-index", "verify", NULL, NULL, NULL };
881 midx_verify.argv = midx_argv;
882 midx_verify.git_cmd = 1;
883 if (run_command(&midx_verify))
884 errors_found |= ERROR_COMMIT_GRAPH;
886 prepare_alt_odb(the_repository);
887 for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
888 midx_argv[2] = "--object-dir";
889 midx_argv[3] = alt->path;
890 if (run_command(&midx_verify))
891 errors_found |= ERROR_COMMIT_GRAPH;