10 #include "cache-tree.h"
11 #include "tree-walk.h"
13 #include "parse-options.h"
16 #include "streaming.h"
19 #define REACHABLE 0x0001
21 #define HAS_OBJ 0x0004
22 /* This flag is set if something points to this object. */
27 static int show_unreachable;
28 static int include_reflogs = 1;
29 static int check_full = 1;
30 static int connectivity_only;
31 static int check_strict;
32 static int keep_cache_objects;
33 static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
34 static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
35 static struct object_id head_oid;
36 static const char *head_points_at;
37 static int errors_found;
38 static int write_lost_and_found;
40 static int show_progress = -1;
41 static int show_dangling = 1;
42 static int name_objects;
43 #define ERROR_OBJECT 01
44 #define ERROR_REACHABLE 02
46 #define ERROR_REFS 010
48 static const char *describe_object(struct object *obj)
50 static struct strbuf buf = STRBUF_INIT;
51 char *name = name_objects ?
52 lookup_decoration(fsck_walk_options.object_names, obj) : NULL;
55 strbuf_addstr(&buf, oid_to_hex(&obj->oid));
57 strbuf_addf(&buf, " (%s)", name);
62 static const char *printable_type(struct object *obj)
66 if (obj->type == OBJ_NONE) {
67 enum object_type type = sha1_object_info(obj->oid.hash, NULL);
69 object_as_type(obj, type, 0);
72 ret = typename(obj->type);
79 static int fsck_config(const char *var, const char *value, void *cb)
81 if (strcmp(var, "fsck.skiplist") == 0) {
83 struct strbuf sb = STRBUF_INIT;
85 if (git_config_pathname(&path, var, value))
87 strbuf_addf(&sb, "skiplist=%s", path);
89 fsck_set_msg_types(&fsck_obj_options, sb.buf);
94 if (skip_prefix(var, "fsck.", &var)) {
95 fsck_set_msg_type(&fsck_obj_options, var, value);
99 return git_default_config(var, value, cb);
102 static void objreport(struct object *obj, const char *msg_type,
105 fprintf(stderr, "%s in %s %s: %s\n",
106 msg_type, printable_type(obj), describe_object(obj), err);
109 static int objerror(struct object *obj, const char *err)
111 errors_found |= ERROR_OBJECT;
112 objreport(obj, "error", err);
116 static int fsck_error_func(struct fsck_options *o,
117 struct object *obj, int type, const char *message)
119 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", message);
120 return (type == FSCK_WARN) ? 0 : 1;
123 static struct object_array pending;
125 static int mark_object(struct object *obj, int type, void *data, struct fsck_options *options)
127 struct object *parent = data;
130 * The only case data is NULL or type is OBJ_ANY is when
131 * mark_object_reachable() calls us. All the callers of
132 * that function has non-NULL obj hence ...
135 /* ... these references to parent->fld are safe here */
136 printf("broken link from %7s %s\n",
137 printable_type(parent), describe_object(parent));
138 printf("broken link from %7s %s\n",
139 (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
140 errors_found |= ERROR_REACHABLE;
144 if (type != OBJ_ANY && obj->type != type)
145 /* ... and the reference to parent is safe here */
146 objerror(parent, "wrong object type in link");
148 if (obj->flags & REACHABLE)
150 obj->flags |= REACHABLE;
151 if (!(obj->flags & HAS_OBJ)) {
152 if (parent && !has_object_file(&obj->oid)) {
153 printf("broken link from %7s %s\n",
154 printable_type(parent), describe_object(parent));
155 printf(" to %7s %s\n",
156 printable_type(obj), describe_object(obj));
157 errors_found |= ERROR_REACHABLE;
162 add_object_array(obj, NULL, &pending);
166 static void mark_object_reachable(struct object *obj)
168 mark_object(obj, OBJ_ANY, NULL, NULL);
171 static int traverse_one_object(struct object *obj)
173 return fsck_walk(obj, obj, &fsck_walk_options);
176 static int traverse_reachable(void)
178 struct progress *progress = NULL;
182 progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2);
184 struct object_array_entry *entry;
187 entry = pending.objects + --pending.nr;
189 result |= traverse_one_object(obj);
190 display_progress(progress, ++nr);
192 stop_progress(&progress);
196 static int mark_used(struct object *obj, int type, void *data, struct fsck_options *options)
205 * Check a single reachable object
207 static void check_reachable_object(struct object *obj)
210 * We obviously want the object to be parsed,
211 * except if it was in a pack-file and we didn't
214 if (!(obj->flags & HAS_OBJ)) {
215 if (has_sha1_pack(obj->oid.hash))
216 return; /* it is in pack - forget about it */
217 printf("missing %s %s\n", printable_type(obj),
218 describe_object(obj));
219 errors_found |= ERROR_REACHABLE;
225 * Check a single unreachable object
227 static void check_unreachable_object(struct object *obj)
230 * Missing unreachable object? Ignore it. It's not like
231 * we miss it (since it can't be reached), nor do we want
232 * to complain about it being unreachable (since it does
235 if (!(obj->flags & HAS_OBJ))
239 * Unreachable object that exists? Show it if asked to,
240 * since this is something that is prunable.
242 if (show_unreachable) {
243 printf("unreachable %s %s\n", printable_type(obj),
244 describe_object(obj));
249 * "!USED" means that nothing at all points to it, including
250 * other unreachable objects. In other words, it's the "tip"
251 * of some set of unreachable objects, usually a commit that
254 * Such starting points are more interesting than some random
255 * set of unreachable objects, so we show them even if the user
256 * hasn't asked for _all_ unreachable objects. If you have
257 * deleted a branch by mistake, this is a prime candidate to
258 * start looking at, for example.
260 if (!(obj->flags & USED)) {
262 printf("dangling %s %s\n", printable_type(obj),
263 describe_object(obj));
264 if (write_lost_and_found) {
265 char *filename = git_pathdup("lost-found/%s/%s",
266 obj->type == OBJ_COMMIT ? "commit" : "other",
267 describe_object(obj));
270 if (safe_create_leading_directories_const(filename)) {
271 error("Could not create lost-found");
275 f = xfopen(filename, "w");
276 if (obj->type == OBJ_BLOB) {
277 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
278 die_errno("Could not write '%s'", filename);
280 fprintf(f, "%s\n", describe_object(obj));
282 die_errno("Could not finish '%s'",
290 * Otherwise? It's there, it's unreachable, and some other unreachable
291 * object points to it. Ignore it - it's not interesting, and we showed
292 * all the interesting cases above.
296 static void check_object(struct object *obj)
299 fprintf(stderr, "Checking %s\n", describe_object(obj));
301 if (obj->flags & REACHABLE)
302 check_reachable_object(obj);
304 check_unreachable_object(obj);
307 static void check_connectivity(void)
311 /* Traverse the pending reachable objects */
312 traverse_reachable();
314 /* Look up all the requirements, warn about missing objects.. */
315 max = get_max_object_index();
317 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
319 for (i = 0; i < max; i++) {
320 struct object *obj = get_indexed_object(i);
327 static int fsck_obj(struct object *obj)
329 if (obj->flags & SEEN)
334 fprintf(stderr, "Checking %s %s\n",
335 printable_type(obj), describe_object(obj));
337 if (fsck_walk(obj, NULL, &fsck_obj_options))
338 objerror(obj, "broken links");
339 if (fsck_object(obj, NULL, 0, &fsck_obj_options))
342 if (obj->type == OBJ_TREE) {
343 struct tree *item = (struct tree *) obj;
345 free_tree_buffer(item);
348 if (obj->type == OBJ_COMMIT) {
349 struct commit *commit = (struct commit *) obj;
351 free_commit_buffer(commit);
353 if (!commit->parents && show_root)
354 printf("root %s\n", describe_object(&commit->object));
357 if (obj->type == OBJ_TAG) {
358 struct tag *tag = (struct tag *) obj;
360 if (show_tags && tag->tagged) {
361 printf("tagged %s %s", printable_type(tag->tagged),
362 describe_object(tag->tagged));
363 printf(" (%s) in %s\n", tag->tag,
364 describe_object(&tag->object));
371 static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
372 unsigned long size, void *buffer, int *eaten)
375 * Note, buffer may be NULL if type is OBJ_BLOB. See
376 * verify_packfile(), data_valid variable for details.
379 obj = parse_object_buffer(oid, type, size, buffer, eaten);
381 errors_found |= ERROR_OBJECT;
382 return error("%s: object corrupt or missing", oid_to_hex(oid));
384 obj->flags &= ~(REACHABLE | SEEN);
385 obj->flags |= HAS_OBJ;
386 return fsck_obj(obj);
389 static int default_refs;
391 static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
392 timestamp_t timestamp)
396 if (!is_null_oid(oid)) {
397 obj = lookup_object(oid->hash);
398 if (obj && (obj->flags & HAS_OBJ)) {
399 if (timestamp && name_objects)
400 add_decoration(fsck_walk_options.object_names,
402 xstrfmt("%s@{%"PRItime"}", refname, timestamp));
404 mark_object_reachable(obj);
406 error("%s: invalid reflog entry %s", refname, oid_to_hex(oid));
407 errors_found |= ERROR_REACHABLE;
412 static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
413 const char *email, timestamp_t timestamp, int tz,
414 const char *message, void *cb_data)
416 const char *refname = cb_data;
419 fprintf(stderr, "Checking reflog %s->%s\n",
420 oid_to_hex(ooid), oid_to_hex(noid));
422 fsck_handle_reflog_oid(refname, ooid, 0);
423 fsck_handle_reflog_oid(refname, noid, timestamp);
427 static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
428 int flag, void *cb_data)
430 for_each_reflog_ent(logname, fsck_handle_reflog_ent, (void *)logname);
434 static int fsck_handle_ref(const char *refname, const struct object_id *oid,
435 int flag, void *cb_data)
439 obj = parse_object(oid);
441 error("%s: invalid sha1 pointer %s", refname, oid_to_hex(oid));
442 errors_found |= ERROR_REACHABLE;
443 /* We'll continue with the rest despite the error.. */
446 if (obj->type != OBJ_COMMIT && is_branch(refname)) {
447 error("%s: not a commit", refname);
448 errors_found |= ERROR_REFS;
453 add_decoration(fsck_walk_options.object_names,
454 obj, xstrdup(refname));
455 mark_object_reachable(obj);
460 static void get_default_heads(void)
462 if (head_points_at && !is_null_oid(&head_oid))
463 fsck_handle_ref("HEAD", &head_oid, 0, NULL);
464 for_each_rawref(fsck_handle_ref, NULL);
466 for_each_reflog(fsck_handle_reflog, NULL);
469 * Not having any default heads isn't really fatal, but
470 * it does mean that "--unreachable" no longer makes any
471 * sense (since in this case everything will obviously
472 * be unreachable by definition.
474 * Showing dangling objects is valid, though (as those
475 * dangling objects are likely lost heads).
477 * So we just print a warning about it, and clear the
478 * "show_unreachable" flag.
481 fprintf(stderr, "notice: No default references\n");
482 show_unreachable = 0;
486 static struct object *parse_loose_object(const struct object_id *oid,
491 enum object_type type;
495 if (read_loose_object(path, oid->hash, &type, &size, &contents) < 0)
498 if (!contents && type != OBJ_BLOB)
499 die("BUG: read_loose_object streamed a non-blob");
501 obj = parse_object_buffer(oid, type, size, contents, &eaten);
508 static int fsck_loose(const struct object_id *oid, const char *path, void *data)
510 struct object *obj = parse_loose_object(oid, path);
513 errors_found |= ERROR_OBJECT;
514 error("%s: object corrupt or missing: %s",
515 oid_to_hex(oid), path);
516 return 0; /* keep checking other objects */
519 obj->flags &= ~(REACHABLE | SEEN);
520 obj->flags |= HAS_OBJ;
522 errors_found |= ERROR_OBJECT;
526 static int fsck_cruft(const char *basename, const char *path, void *data)
528 if (!starts_with(basename, "tmp_obj_"))
529 fprintf(stderr, "bad sha1 file: %s\n", path);
533 static int fsck_subdir(unsigned int nr, const char *path, void *progress)
535 display_progress(progress, nr + 1);
539 static void fsck_object_dir(const char *path)
541 struct progress *progress = NULL;
544 fprintf(stderr, "Checking object directory\n");
547 progress = start_progress(_("Checking object directories"), 256);
549 for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
551 display_progress(progress, 256);
552 stop_progress(&progress);
555 static int fsck_head_link(void)
557 int null_is_error = 0;
560 fprintf(stderr, "Checking HEAD link\n");
562 head_points_at = resolve_ref_unsafe("HEAD", 0, head_oid.hash, NULL);
563 if (!head_points_at) {
564 errors_found |= ERROR_REFS;
565 return error("Invalid HEAD");
567 if (!strcmp(head_points_at, "HEAD"))
570 else if (!starts_with(head_points_at, "refs/heads/")) {
571 errors_found |= ERROR_REFS;
572 return error("HEAD points to something strange (%s)",
575 if (is_null_oid(&head_oid)) {
577 errors_found |= ERROR_REFS;
578 return error("HEAD: detached HEAD points at nothing");
580 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
581 head_points_at + 11);
586 static int fsck_cache_tree(struct cache_tree *it)
592 fprintf(stderr, "Checking cache tree\n");
594 if (0 <= it->entry_count) {
595 struct object *obj = parse_object(&it->oid);
597 error("%s: invalid sha1 pointer in cache-tree",
598 oid_to_hex(&it->oid));
599 errors_found |= ERROR_REFS;
604 add_decoration(fsck_walk_options.object_names,
606 mark_object_reachable(obj);
607 if (obj->type != OBJ_TREE)
608 err |= objerror(obj, "non-tree in cache-tree");
610 for (i = 0; i < it->subtree_nr; i++)
611 err |= fsck_cache_tree(it->down[i]->cache_tree);
615 static void mark_object_for_connectivity(const struct object_id *oid)
617 struct object *obj = lookup_unknown_object(oid->hash);
618 obj->flags |= HAS_OBJ;
621 static int mark_loose_for_connectivity(const struct object_id *oid,
625 mark_object_for_connectivity(oid);
629 static int mark_packed_for_connectivity(const struct object_id *oid,
630 struct packed_git *pack,
634 mark_object_for_connectivity(oid);
638 static char const * const fsck_usage[] = {
639 N_("git fsck [<options>] [<object>...]"),
643 static struct option fsck_opts[] = {
644 OPT__VERBOSE(&verbose, N_("be verbose")),
645 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
646 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
647 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
648 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
649 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
650 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
651 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
652 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
653 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
654 OPT_BOOL(0, "lost-found", &write_lost_and_found,
655 N_("write dangling objects in .git/lost-found")),
656 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
657 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
661 int cmd_fsck(int argc, const char **argv, const char *prefix)
664 struct alternate_object_database *alt;
667 check_replace_refs = 0;
669 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
671 fsck_walk_options.walk = mark_object;
672 fsck_obj_options.walk = mark_used;
673 fsck_obj_options.error_func = fsck_error_func;
675 fsck_obj_options.strict = 1;
677 if (show_progress == -1)
678 show_progress = isatty(2);
682 if (write_lost_and_found) {
688 fsck_walk_options.object_names =
689 xcalloc(1, sizeof(struct decoration));
691 git_config(fsck_config, NULL);
694 if (connectivity_only) {
695 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
696 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
698 fsck_object_dir(get_object_directory());
701 for (alt = alt_odb_list; alt; alt = alt->next)
702 fsck_object_dir(alt->path);
705 struct packed_git *p;
706 uint32_t total = 0, count = 0;
707 struct progress *progress = NULL;
709 prepare_packed_git();
712 for (p = packed_git; p; p = p->next) {
713 if (open_pack_index(p))
715 total += p->num_objects;
718 progress = start_progress(_("Checking objects"), total);
720 for (p = packed_git; p; p = p->next) {
721 /* verify gives error messages itself */
722 if (verify_pack(p, fsck_obj_buffer,
724 errors_found |= ERROR_PACK;
725 count += p->num_objects;
727 stop_progress(&progress);
731 for (i = 0; i < argc; i++) {
732 const char *arg = argv[i];
733 struct object_id oid;
734 if (!get_oid(arg, &oid)) {
735 struct object *obj = lookup_object(oid.hash);
737 if (!obj || !(obj->flags & HAS_OBJ)) {
738 error("%s: object missing", oid_to_hex(&oid));
739 errors_found |= ERROR_OBJECT;
745 add_decoration(fsck_walk_options.object_names,
747 mark_object_reachable(obj);
750 error("invalid parameter: expected sha1, got '%s'", arg);
751 errors_found |= ERROR_OBJECT;
755 * If we've not been given any explicit head information, do the
756 * default ones from .git/refs. We also consider the index file
757 * in this case (ie this implies --cache).
761 keep_cache_objects = 1;
764 if (keep_cache_objects) {
765 verify_index_checksum = 1;
767 for (i = 0; i < active_nr; i++) {
772 mode = active_cache[i]->ce_mode;
773 if (S_ISGITLINK(mode))
775 blob = lookup_blob(&active_cache[i]->oid);
781 add_decoration(fsck_walk_options.object_names,
783 xstrfmt(":%s", active_cache[i]->name));
784 mark_object_reachable(obj);
786 if (active_cache_tree)
787 fsck_cache_tree(active_cache_tree);
790 check_connectivity();