9 #include "cache-tree.h"
10 #include "tree-walk.h"
12 #include "parse-options.h"
15 #include "streaming.h"
18 #define REACHABLE 0x0001
20 #define HAS_OBJ 0x0004
24 static int show_unreachable;
25 static int include_reflogs = 1;
26 static int check_full = 1;
27 static int connectivity_only;
28 static int check_strict;
29 static int keep_cache_objects;
30 static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
31 static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
32 static struct object_id head_oid;
33 static const char *head_points_at;
34 static int errors_found;
35 static int write_lost_and_found;
37 static int show_progress = -1;
38 static int show_dangling = 1;
39 static int name_objects;
40 #define ERROR_OBJECT 01
41 #define ERROR_REACHABLE 02
43 #define ERROR_REFS 010
45 static const char *describe_object(struct object *obj)
47 static struct strbuf buf = STRBUF_INIT;
48 char *name = name_objects ?
49 lookup_decoration(fsck_walk_options.object_names, obj) : NULL;
52 strbuf_addstr(&buf, oid_to_hex(&obj->oid));
54 strbuf_addf(&buf, " (%s)", name);
59 static const char *printable_type(struct object *obj)
63 ret = typename(obj->type);
70 static int fsck_config(const char *var, const char *value, void *cb)
72 if (strcmp(var, "fsck.skiplist") == 0) {
74 struct strbuf sb = STRBUF_INIT;
76 if (git_config_pathname(&path, var, value))
78 strbuf_addf(&sb, "skiplist=%s", path);
80 fsck_set_msg_types(&fsck_obj_options, sb.buf);
85 if (skip_prefix(var, "fsck.", &var)) {
86 fsck_set_msg_type(&fsck_obj_options, var, value);
90 return git_default_config(var, value, cb);
93 static void objreport(struct object *obj, const char *msg_type,
96 fprintf(stderr, "%s in %s %s: %s\n",
97 msg_type, printable_type(obj), describe_object(obj), err);
100 static int objerror(struct object *obj, const char *err)
102 errors_found |= ERROR_OBJECT;
103 objreport(obj, "error", err);
107 static int fsck_error_func(struct fsck_options *o,
108 struct object *obj, int type, const char *message)
110 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", message);
111 return (type == FSCK_WARN) ? 0 : 1;
114 static struct object_array pending;
116 static int mark_object(struct object *obj, int type, void *data, struct fsck_options *options)
118 struct object *parent = data;
121 * The only case data is NULL or type is OBJ_ANY is when
122 * mark_object_reachable() calls us. All the callers of
123 * that function has non-NULL obj hence ...
126 /* ... these references to parent->fld are safe here */
127 printf("broken link from %7s %s\n",
128 printable_type(parent), describe_object(parent));
129 printf("broken link from %7s %s\n",
130 (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
131 errors_found |= ERROR_REACHABLE;
135 if (type != OBJ_ANY && obj->type != type)
136 /* ... and the reference to parent is safe here */
137 objerror(parent, "wrong object type in link");
139 if (obj->flags & REACHABLE)
141 obj->flags |= REACHABLE;
142 if (!(obj->flags & HAS_OBJ)) {
143 if (parent && !has_object_file(&obj->oid)) {
144 printf("broken link from %7s %s\n",
145 printable_type(parent), describe_object(parent));
146 printf(" to %7s %s\n",
147 printable_type(obj), describe_object(obj));
148 errors_found |= ERROR_REACHABLE;
153 add_object_array(obj, NULL, &pending);
157 static void mark_object_reachable(struct object *obj)
159 mark_object(obj, OBJ_ANY, NULL, NULL);
162 static int traverse_one_object(struct object *obj)
165 struct tree *tree = NULL;
167 if (obj->type == OBJ_TREE) {
168 tree = (struct tree *)obj;
169 if (parse_tree(tree) < 0)
170 return 1; /* error already displayed */
172 result = fsck_walk(obj, obj, &fsck_walk_options);
174 free_tree_buffer(tree);
178 static int traverse_reachable(void)
180 struct progress *progress = NULL;
184 progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2);
186 struct object_array_entry *entry;
189 entry = pending.objects + --pending.nr;
191 result |= traverse_one_object(obj);
192 display_progress(progress, ++nr);
194 stop_progress(&progress);
198 static int mark_used(struct object *obj, int type, void *data, struct fsck_options *options)
207 * Check a single reachable object
209 static void check_reachable_object(struct object *obj)
212 * We obviously want the object to be parsed,
213 * except if it was in a pack-file and we didn't
216 if (!(obj->flags & HAS_OBJ)) {
217 if (has_sha1_pack(obj->oid.hash))
218 return; /* it is in pack - forget about it */
219 printf("missing %s %s\n", printable_type(obj),
220 describe_object(obj));
221 errors_found |= ERROR_REACHABLE;
227 * Check a single unreachable object
229 static void check_unreachable_object(struct object *obj)
232 * Missing unreachable object? Ignore it. It's not like
233 * we miss it (since it can't be reached), nor do we want
234 * to complain about it being unreachable (since it does
237 if (!(obj->flags & HAS_OBJ))
241 * Unreachable object that exists? Show it if asked to,
242 * since this is something that is prunable.
244 if (show_unreachable) {
245 printf("unreachable %s %s\n", printable_type(obj),
246 describe_object(obj));
251 * "!used" means that nothing at all points to it, including
252 * other unreachable objects. In other words, it's the "tip"
253 * of some set of unreachable objects, usually a commit that
256 * Such starting points are more interesting than some random
257 * set of unreachable objects, so we show them even if the user
258 * hasn't asked for _all_ unreachable objects. If you have
259 * deleted a branch by mistake, this is a prime candidate to
260 * start looking at, for example.
264 printf("dangling %s %s\n", printable_type(obj),
265 describe_object(obj));
266 if (write_lost_and_found) {
267 char *filename = git_pathdup("lost-found/%s/%s",
268 obj->type == OBJ_COMMIT ? "commit" : "other",
269 describe_object(obj));
272 if (safe_create_leading_directories_const(filename)) {
273 error("Could not create lost-found");
277 if (!(f = fopen(filename, "w")))
278 die_errno("Could not open '%s'", filename);
279 if (obj->type == OBJ_BLOB) {
280 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
281 die_errno("Could not write '%s'", filename);
283 fprintf(f, "%s\n", describe_object(obj));
285 die_errno("Could not finish '%s'",
293 * Otherwise? It's there, it's unreachable, and some other unreachable
294 * object points to it. Ignore it - it's not interesting, and we showed
295 * all the interesting cases above.
299 static void check_object(struct object *obj)
302 fprintf(stderr, "Checking %s\n", describe_object(obj));
304 if (obj->flags & REACHABLE)
305 check_reachable_object(obj);
307 check_unreachable_object(obj);
310 static void check_connectivity(void)
314 /* Traverse the pending reachable objects */
315 traverse_reachable();
317 /* Look up all the requirements, warn about missing objects.. */
318 max = get_max_object_index();
320 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
322 for (i = 0; i < max; i++) {
323 struct object *obj = get_indexed_object(i);
330 static int fsck_obj(struct object *obj)
332 if (obj->flags & SEEN)
337 fprintf(stderr, "Checking %s %s\n",
338 printable_type(obj), describe_object(obj));
340 if (fsck_walk(obj, NULL, &fsck_obj_options))
341 objerror(obj, "broken links");
342 if (fsck_object(obj, NULL, 0, &fsck_obj_options))
345 if (obj->type == OBJ_TREE) {
346 struct tree *item = (struct tree *) obj;
348 free_tree_buffer(item);
351 if (obj->type == OBJ_COMMIT) {
352 struct commit *commit = (struct commit *) obj;
354 free_commit_buffer(commit);
356 if (!commit->parents && show_root)
357 printf("root %s\n", describe_object(&commit->object));
360 if (obj->type == OBJ_TAG) {
361 struct tag *tag = (struct tag *) obj;
363 if (show_tags && tag->tagged) {
364 printf("tagged %s %s", printable_type(tag->tagged),
365 describe_object(tag->tagged));
366 printf(" (%s) in %s\n", tag->tag,
367 describe_object(&tag->object));
374 static int fsck_sha1(const unsigned char *sha1)
376 struct object *obj = parse_object(sha1);
378 errors_found |= ERROR_OBJECT;
379 return error("%s: object corrupt or missing",
382 obj->flags |= HAS_OBJ;
383 return fsck_obj(obj);
386 static int fsck_obj_buffer(const unsigned char *sha1, enum object_type type,
387 unsigned long size, void *buffer, int *eaten)
390 * Note, buffer may be NULL if type is OBJ_BLOB. See
391 * verify_packfile(), data_valid variable for details.
394 obj = parse_object_buffer(sha1, type, size, buffer, eaten);
396 errors_found |= ERROR_OBJECT;
397 return error("%s: object corrupt or missing", sha1_to_hex(sha1));
399 obj->flags = HAS_OBJ;
400 return fsck_obj(obj);
403 static int default_refs;
405 static void fsck_handle_reflog_sha1(const char *refname, unsigned char *sha1,
406 unsigned long timestamp)
410 if (!is_null_sha1(sha1)) {
411 obj = lookup_object(sha1);
412 if (obj && (obj->flags & HAS_OBJ)) {
413 if (timestamp && name_objects)
414 add_decoration(fsck_walk_options.object_names,
416 xstrfmt("%s@{%ld}", refname, timestamp));
418 mark_object_reachable(obj);
420 error("%s: invalid reflog entry %s", refname, sha1_to_hex(sha1));
421 errors_found |= ERROR_REACHABLE;
426 static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
427 const char *email, unsigned long timestamp, int tz,
428 const char *message, void *cb_data)
430 const char *refname = cb_data;
433 fprintf(stderr, "Checking reflog %s->%s\n",
434 sha1_to_hex(osha1), sha1_to_hex(nsha1));
436 fsck_handle_reflog_sha1(refname, osha1, 0);
437 fsck_handle_reflog_sha1(refname, nsha1, timestamp);
441 static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
442 int flag, void *cb_data)
444 for_each_reflog_ent(logname, fsck_handle_reflog_ent, (void *)logname);
448 static int fsck_handle_ref(const char *refname, const struct object_id *oid,
449 int flag, void *cb_data)
453 obj = parse_object(oid->hash);
455 error("%s: invalid sha1 pointer %s", refname, oid_to_hex(oid));
456 errors_found |= ERROR_REACHABLE;
457 /* We'll continue with the rest despite the error.. */
460 if (obj->type != OBJ_COMMIT && is_branch(refname)) {
461 error("%s: not a commit", refname);
462 errors_found |= ERROR_REFS;
467 add_decoration(fsck_walk_options.object_names,
468 obj, xstrdup(refname));
469 mark_object_reachable(obj);
474 static void get_default_heads(void)
476 if (head_points_at && !is_null_oid(&head_oid))
477 fsck_handle_ref("HEAD", &head_oid, 0, NULL);
478 for_each_rawref(fsck_handle_ref, NULL);
480 for_each_reflog(fsck_handle_reflog, NULL);
483 * Not having any default heads isn't really fatal, but
484 * it does mean that "--unreachable" no longer makes any
485 * sense (since in this case everything will obviously
486 * be unreachable by definition.
488 * Showing dangling objects is valid, though (as those
489 * dangling objects are likely lost heads).
491 * So we just print a warning about it, and clear the
492 * "show_unreachable" flag.
495 fprintf(stderr, "notice: No default references\n");
496 show_unreachable = 0;
500 static int fsck_loose(const unsigned char *sha1, const char *path, void *data)
503 errors_found |= ERROR_OBJECT;
507 static int fsck_cruft(const char *basename, const char *path, void *data)
509 if (!starts_with(basename, "tmp_obj_"))
510 fprintf(stderr, "bad sha1 file: %s\n", path);
514 static int fsck_subdir(int nr, const char *path, void *progress)
516 display_progress(progress, nr + 1);
520 static void fsck_object_dir(const char *path)
522 struct progress *progress = NULL;
525 fprintf(stderr, "Checking object directory\n");
528 progress = start_progress(_("Checking object directories"), 256);
530 for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
532 display_progress(progress, 256);
533 stop_progress(&progress);
536 static int fsck_head_link(void)
538 int null_is_error = 0;
541 fprintf(stderr, "Checking HEAD link\n");
543 head_points_at = resolve_ref_unsafe("HEAD", 0, head_oid.hash, NULL);
544 if (!head_points_at) {
545 errors_found |= ERROR_REFS;
546 return error("Invalid HEAD");
548 if (!strcmp(head_points_at, "HEAD"))
551 else if (!starts_with(head_points_at, "refs/heads/")) {
552 errors_found |= ERROR_REFS;
553 return error("HEAD points to something strange (%s)",
556 if (is_null_oid(&head_oid)) {
558 errors_found |= ERROR_REFS;
559 return error("HEAD: detached HEAD points at nothing");
561 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
562 head_points_at + 11);
567 static int fsck_cache_tree(struct cache_tree *it)
573 fprintf(stderr, "Checking cache tree\n");
575 if (0 <= it->entry_count) {
576 struct object *obj = parse_object(it->sha1);
578 error("%s: invalid sha1 pointer in cache-tree",
579 sha1_to_hex(it->sha1));
580 errors_found |= ERROR_REFS;
585 add_decoration(fsck_walk_options.object_names,
587 mark_object_reachable(obj);
588 if (obj->type != OBJ_TREE)
589 err |= objerror(obj, "non-tree in cache-tree");
591 for (i = 0; i < it->subtree_nr; i++)
592 err |= fsck_cache_tree(it->down[i]->cache_tree);
596 static void mark_object_for_connectivity(const unsigned char *sha1)
598 struct object *obj = lookup_object(sha1);
601 * Setting the object type here isn't strictly necessary for a
602 * connectivity check. In most cases, our walk will expect a certain
603 * type (e.g., a tree referencing a blob) and will use lookup_blob() to
604 * assign the type. But doing it here has two advantages:
606 * 1. When the fsck_walk code looks at objects that _don't_ come from
607 * links (e.g., the tip of a ref), it may complain about the
608 * "unknown object type".
610 * 2. This serves as a nice cross-check that the graph links are
611 * sane. So --connectivity-only does not check that the bits of
612 * blobs are not corrupted, but it _does_ check that 100644 tree
613 * entries point to blobs, and so forth.
615 * Unfortunately we can't just use parse_object() here, because the
616 * whole point of --connectivity-only is to avoid reading the object
617 * data more than necessary.
619 if (!obj || obj->type == OBJ_NONE) {
620 enum object_type type = sha1_object_info(sha1, NULL);
623 error("%s: unable to read object type",
627 obj = (struct object *)lookup_commit(sha1);
630 obj = (struct object *)lookup_tree(sha1);
633 obj = (struct object *)lookup_blob(sha1);
636 obj = (struct object *)lookup_tag(sha1);
639 error("%s: unknown object type %d",
640 sha1_to_hex(sha1), type);
643 if (!obj || obj->type == OBJ_NONE) {
644 errors_found |= ERROR_OBJECT;
649 obj->flags |= HAS_OBJ;
652 static int mark_loose_for_connectivity(const unsigned char *sha1,
656 mark_object_for_connectivity(sha1);
660 static int mark_packed_for_connectivity(const unsigned char *sha1,
661 struct packed_git *pack,
665 mark_object_for_connectivity(sha1);
669 static char const * const fsck_usage[] = {
670 N_("git fsck [<options>] [<object>...]"),
674 static struct option fsck_opts[] = {
675 OPT__VERBOSE(&verbose, N_("be verbose")),
676 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
677 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
678 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
679 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
680 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
681 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
682 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
683 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
684 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
685 OPT_BOOL(0, "lost-found", &write_lost_and_found,
686 N_("write dangling objects in .git/lost-found")),
687 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
688 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
692 int cmd_fsck(int argc, const char **argv, const char *prefix)
695 struct alternate_object_database *alt;
698 check_replace_refs = 0;
700 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
702 fsck_walk_options.walk = mark_object;
703 fsck_obj_options.walk = mark_used;
704 fsck_obj_options.error_func = fsck_error_func;
706 fsck_obj_options.strict = 1;
708 if (show_progress == -1)
709 show_progress = isatty(2);
713 if (write_lost_and_found) {
719 fsck_walk_options.object_names =
720 xcalloc(1, sizeof(struct decoration));
722 git_config(fsck_config, NULL);
725 if (connectivity_only) {
726 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
727 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
729 fsck_object_dir(get_object_directory());
732 for (alt = alt_odb_list; alt; alt = alt->next)
733 fsck_object_dir(alt->path);
736 struct packed_git *p;
737 uint32_t total = 0, count = 0;
738 struct progress *progress = NULL;
740 prepare_packed_git();
743 for (p = packed_git; p; p = p->next) {
744 if (open_pack_index(p))
746 total += p->num_objects;
749 progress = start_progress(_("Checking objects"), total);
751 for (p = packed_git; p; p = p->next) {
752 /* verify gives error messages itself */
753 if (verify_pack(p, fsck_obj_buffer,
755 errors_found |= ERROR_PACK;
756 count += p->num_objects;
758 stop_progress(&progress);
763 for (i = 0; i < argc; i++) {
764 const char *arg = argv[i];
765 unsigned char sha1[20];
766 if (!get_sha1(arg, sha1)) {
767 struct object *obj = lookup_object(sha1);
769 if (!obj || !(obj->flags & HAS_OBJ)) {
770 error("%s: object missing", sha1_to_hex(sha1));
771 errors_found |= ERROR_OBJECT;
777 add_decoration(fsck_walk_options.object_names,
779 mark_object_reachable(obj);
783 error("invalid parameter: expected sha1, got '%s'", arg);
784 errors_found |= ERROR_OBJECT;
788 * If we've not been given any explicit head information, do the
789 * default ones from .git/refs. We also consider the index file
790 * in this case (ie this implies --cache).
794 keep_cache_objects = 1;
797 if (keep_cache_objects) {
799 for (i = 0; i < active_nr; i++) {
804 mode = active_cache[i]->ce_mode;
805 if (S_ISGITLINK(mode))
807 blob = lookup_blob(active_cache[i]->oid.hash);
813 add_decoration(fsck_walk_options.object_names,
815 xstrfmt(":%s", active_cache[i]->name));
816 mark_object_reachable(obj);
818 if (active_cache_tree)
819 fsck_cache_tree(active_cache_tree);
822 check_connectivity();