9 #include "cache-tree.h"
10 #include "tree-walk.h"
12 #include "parse-options.h"
15 #include "streaming.h"
17 #define REACHABLE 0x0001
19 #define HAS_OBJ 0x0004
23 static int show_unreachable;
24 static int include_reflogs = 1;
25 static int check_full = 1;
26 static int connectivity_only;
27 static int check_strict;
28 static int keep_cache_objects;
29 static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
30 static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
31 static struct object_id head_oid;
32 static const char *head_points_at;
33 static int errors_found;
34 static int write_lost_and_found;
36 static int show_progress = -1;
37 static int show_dangling = 1;
38 #define ERROR_OBJECT 01
39 #define ERROR_REACHABLE 02
42 #ifdef NO_D_INO_IN_DIRENT
44 #define DIRENT_SORT_HINT(de) 0
47 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
50 static int fsck_config(const char *var, const char *value, void *cb)
52 if (skip_prefix(var, "fsck.", &var)) {
53 fsck_set_msg_type(&fsck_obj_options, var, value);
57 return git_default_config(var, value, cb);
60 static void objreport(struct object *obj, const char *msg_type,
63 fprintf(stderr, "%s in %s %s: %s\n",
64 msg_type, typename(obj->type), sha1_to_hex(obj->sha1), err);
67 static int objerror(struct object *obj, const char *err)
69 errors_found |= ERROR_OBJECT;
70 objreport(obj, "error", err);
74 static int fsck_error_func(struct object *obj, int type, const char *message)
76 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", message);
77 return (type == FSCK_WARN) ? 0 : 1;
80 static struct object_array pending;
82 static int mark_object(struct object *obj, int type, void *data, struct fsck_options *options)
84 struct object *parent = data;
87 * The only case data is NULL or type is OBJ_ANY is when
88 * mark_object_reachable() calls us. All the callers of
89 * that function has non-NULL obj hence ...
92 /* ... these references to parent->fld are safe here */
93 printf("broken link from %7s %s\n",
94 typename(parent->type), sha1_to_hex(parent->sha1));
95 printf("broken link from %7s %s\n",
96 (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
97 errors_found |= ERROR_REACHABLE;
101 if (type != OBJ_ANY && obj->type != type)
102 /* ... and the reference to parent is safe here */
103 objerror(parent, "wrong object type in link");
105 if (obj->flags & REACHABLE)
107 obj->flags |= REACHABLE;
108 if (!(obj->flags & HAS_OBJ)) {
109 if (parent && !has_sha1_file(obj->sha1)) {
110 printf("broken link from %7s %s\n",
111 typename(parent->type), sha1_to_hex(parent->sha1));
112 printf(" to %7s %s\n",
113 typename(obj->type), sha1_to_hex(obj->sha1));
114 errors_found |= ERROR_REACHABLE;
119 add_object_array(obj, NULL, &pending);
123 static void mark_object_reachable(struct object *obj)
125 mark_object(obj, OBJ_ANY, NULL, NULL);
128 static int traverse_one_object(struct object *obj)
131 struct tree *tree = NULL;
133 if (obj->type == OBJ_TREE) {
134 tree = (struct tree *)obj;
135 if (parse_tree(tree) < 0)
136 return 1; /* error already displayed */
138 result = fsck_walk(obj, obj, &fsck_walk_options);
140 free_tree_buffer(tree);
144 static int traverse_reachable(void)
146 struct progress *progress = NULL;
150 progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2);
152 struct object_array_entry *entry;
155 entry = pending.objects + --pending.nr;
157 result |= traverse_one_object(obj);
158 display_progress(progress, ++nr);
160 stop_progress(&progress);
164 static int mark_used(struct object *obj, int type, void *data, struct fsck_options *options)
173 * Check a single reachable object
175 static void check_reachable_object(struct object *obj)
178 * We obviously want the object to be parsed,
179 * except if it was in a pack-file and we didn't
182 if (!(obj->flags & HAS_OBJ)) {
183 if (has_sha1_pack(obj->sha1))
184 return; /* it is in pack - forget about it */
185 if (connectivity_only && has_sha1_file(obj->sha1))
187 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
188 errors_found |= ERROR_REACHABLE;
194 * Check a single unreachable object
196 static void check_unreachable_object(struct object *obj)
199 * Missing unreachable object? Ignore it. It's not like
200 * we miss it (since it can't be reached), nor do we want
201 * to complain about it being unreachable (since it does
208 * Unreachable object that exists? Show it if asked to,
209 * since this is something that is prunable.
211 if (show_unreachable) {
212 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
217 * "!used" means that nothing at all points to it, including
218 * other unreachable objects. In other words, it's the "tip"
219 * of some set of unreachable objects, usually a commit that
222 * Such starting points are more interesting than some random
223 * set of unreachable objects, so we show them even if the user
224 * hasn't asked for _all_ unreachable objects. If you have
225 * deleted a branch by mistake, this is a prime candidate to
226 * start looking at, for example.
230 printf("dangling %s %s\n", typename(obj->type),
231 sha1_to_hex(obj->sha1));
232 if (write_lost_and_found) {
233 const char *filename = git_path("lost-found/%s/%s",
234 obj->type == OBJ_COMMIT ? "commit" : "other",
235 sha1_to_hex(obj->sha1));
238 if (safe_create_leading_directories_const(filename)) {
239 error("Could not create lost-found");
242 if (!(f = fopen(filename, "w")))
243 die_errno("Could not open '%s'", filename);
244 if (obj->type == OBJ_BLOB) {
245 if (stream_blob_to_fd(fileno(f), obj->sha1, NULL, 1))
246 die_errno("Could not write '%s'", filename);
248 fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
250 die_errno("Could not finish '%s'",
257 * Otherwise? It's there, it's unreachable, and some other unreachable
258 * object points to it. Ignore it - it's not interesting, and we showed
259 * all the interesting cases above.
263 static void check_object(struct object *obj)
266 fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
268 if (obj->flags & REACHABLE)
269 check_reachable_object(obj);
271 check_unreachable_object(obj);
274 static void check_connectivity(void)
278 /* Traverse the pending reachable objects */
279 traverse_reachable();
281 /* Look up all the requirements, warn about missing objects.. */
282 max = get_max_object_index();
284 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
286 for (i = 0; i < max; i++) {
287 struct object *obj = get_indexed_object(i);
294 static int fsck_obj(struct object *obj)
296 if (obj->flags & SEEN)
301 fprintf(stderr, "Checking %s %s\n",
302 typename(obj->type), sha1_to_hex(obj->sha1));
304 if (fsck_walk(obj, NULL, &fsck_obj_options))
305 objerror(obj, "broken links");
306 if (fsck_object(obj, NULL, 0, &fsck_obj_options))
309 if (obj->type == OBJ_TREE) {
310 struct tree *item = (struct tree *) obj;
312 free_tree_buffer(item);
315 if (obj->type == OBJ_COMMIT) {
316 struct commit *commit = (struct commit *) obj;
318 free_commit_buffer(commit);
320 if (!commit->parents && show_root)
321 printf("root %s\n", sha1_to_hex(commit->object.sha1));
324 if (obj->type == OBJ_TAG) {
325 struct tag *tag = (struct tag *) obj;
327 if (show_tags && tag->tagged) {
328 printf("tagged %s %s", typename(tag->tagged->type), sha1_to_hex(tag->tagged->sha1));
329 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
336 static int fsck_sha1(const unsigned char *sha1)
338 struct object *obj = parse_object(sha1);
340 errors_found |= ERROR_OBJECT;
341 return error("%s: object corrupt or missing",
344 obj->flags |= HAS_OBJ;
345 return fsck_obj(obj);
348 static int fsck_obj_buffer(const unsigned char *sha1, enum object_type type,
349 unsigned long size, void *buffer, int *eaten)
352 obj = parse_object_buffer(sha1, type, size, buffer, eaten);
354 errors_found |= ERROR_OBJECT;
355 return error("%s: object corrupt or missing", sha1_to_hex(sha1));
357 obj->flags = HAS_OBJ;
358 return fsck_obj(obj);
362 * This is the sorting chunk size: make it reasonably
363 * big so that we can sort well..
365 #define MAX_SHA1_ENTRIES (1024)
369 unsigned char sha1[20];
374 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
377 static int ino_compare(const void *_a, const void *_b)
379 const struct sha1_entry *a = _a, *b = _b;
380 unsigned long ino1 = a->ino, ino2 = b->ino;
381 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
384 static void fsck_sha1_list(void)
386 int i, nr = sha1_list.nr;
389 qsort(sha1_list.entry, nr,
390 sizeof(struct sha1_entry *), ino_compare);
391 for (i = 0; i < nr; i++) {
392 struct sha1_entry *entry = sha1_list.entry[i];
393 unsigned char *sha1 = entry->sha1;
395 sha1_list.entry[i] = NULL;
397 errors_found |= ERROR_OBJECT;
403 static void add_sha1_list(unsigned char *sha1, unsigned long ino)
405 struct sha1_entry *entry = xmalloc(sizeof(*entry));
409 hashcpy(entry->sha1, sha1);
411 if (nr == MAX_SHA1_ENTRIES) {
415 sha1_list.entry[nr] = entry;
419 static inline int is_loose_object_file(struct dirent *de,
420 char *name, unsigned char *sha1)
422 if (strlen(de->d_name) != 38)
424 memcpy(name + 2, de->d_name, 39);
425 return !get_sha1_hex(name, sha1);
428 static void fsck_dir(int i, char *path)
430 DIR *dir = opendir(path);
438 fprintf(stderr, "Checking directory %s\n", path);
440 sprintf(name, "%02x", i);
441 while ((de = readdir(dir)) != NULL) {
442 unsigned char sha1[20];
444 if (is_dot_or_dotdot(de->d_name))
446 if (is_loose_object_file(de, name, sha1)) {
447 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
450 if (starts_with(de->d_name, "tmp_obj_"))
452 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
457 static int default_refs;
459 static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
460 const char *email, unsigned long timestamp, int tz,
461 const char *message, void *cb_data)
466 fprintf(stderr, "Checking reflog %s->%s\n",
467 sha1_to_hex(osha1), sha1_to_hex(nsha1));
469 if (!is_null_sha1(osha1)) {
470 obj = lookup_object(osha1);
473 mark_object_reachable(obj);
476 obj = lookup_object(nsha1);
479 mark_object_reachable(obj);
484 static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
485 int flag, void *cb_data)
487 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
491 static int fsck_handle_ref(const char *refname, const struct object_id *oid,
492 int flag, void *cb_data)
496 obj = parse_object(oid->hash);
498 error("%s: invalid sha1 pointer %s", refname, oid_to_hex(oid));
499 errors_found |= ERROR_REACHABLE;
500 /* We'll continue with the rest despite the error.. */
503 if (obj->type != OBJ_COMMIT && is_branch(refname))
504 error("%s: not a commit", refname);
507 mark_object_reachable(obj);
512 static void get_default_heads(void)
514 if (head_points_at && !is_null_oid(&head_oid))
515 fsck_handle_ref("HEAD", &head_oid, 0, NULL);
516 for_each_rawref(fsck_handle_ref, NULL);
518 for_each_reflog(fsck_handle_reflog, NULL);
521 * Not having any default heads isn't really fatal, but
522 * it does mean that "--unreachable" no longer makes any
523 * sense (since in this case everything will obviously
524 * be unreachable by definition.
526 * Showing dangling objects is valid, though (as those
527 * dangling objects are likely lost heads).
529 * So we just print a warning about it, and clear the
530 * "show_unreachable" flag.
533 fprintf(stderr, "notice: No default references\n");
534 show_unreachable = 0;
538 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);
548 for (i = 0; i < 256; i++) {
549 static char dir[4096];
550 sprintf(dir, "%s/%02x", path, i);
552 display_progress(progress, i+1);
554 stop_progress(&progress);
558 static int fsck_head_link(void)
561 int null_is_error = 0;
564 fprintf(stderr, "Checking HEAD link\n");
566 head_points_at = resolve_ref_unsafe("HEAD", 0, head_oid.hash, &flag);
568 return error("Invalid HEAD");
569 if (!strcmp(head_points_at, "HEAD"))
572 else if (!starts_with(head_points_at, "refs/heads/"))
573 return error("HEAD points to something strange (%s)",
575 if (is_null_oid(&head_oid)) {
577 return error("HEAD: detached HEAD points at nothing");
578 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
579 head_points_at + 11);
584 static int fsck_cache_tree(struct cache_tree *it)
590 fprintf(stderr, "Checking cache tree\n");
592 if (0 <= it->entry_count) {
593 struct object *obj = parse_object(it->sha1);
595 error("%s: invalid sha1 pointer in cache-tree",
596 sha1_to_hex(it->sha1));
600 mark_object_reachable(obj);
601 if (obj->type != OBJ_TREE)
602 err |= objerror(obj, "non-tree in cache-tree");
604 for (i = 0; i < it->subtree_nr; i++)
605 err |= fsck_cache_tree(it->down[i]->cache_tree);
609 static char const * const fsck_usage[] = {
610 N_("git fsck [<options>] [<object>...]"),
614 static struct option fsck_opts[] = {
615 OPT__VERBOSE(&verbose, N_("be verbose")),
616 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
617 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
618 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
619 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
620 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
621 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
622 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
623 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
624 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
625 OPT_BOOL(0, "lost-found", &write_lost_and_found,
626 N_("write dangling objects in .git/lost-found")),
627 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
631 int cmd_fsck(int argc, const char **argv, const char *prefix)
634 struct alternate_object_database *alt;
637 check_replace_refs = 0;
639 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
641 fsck_walk_options.walk = mark_object;
642 fsck_obj_options.walk = mark_used;
643 fsck_obj_options.error_func = fsck_error_func;
645 fsck_obj_options.strict = 1;
647 if (show_progress == -1)
648 show_progress = isatty(2);
652 if (write_lost_and_found) {
657 git_config(fsck_config, NULL);
660 if (!connectivity_only)
661 fsck_object_dir(get_object_directory());
664 for (alt = alt_odb_list; alt; alt = alt->next) {
665 char namebuf[PATH_MAX];
666 int namelen = alt->name - alt->base;
667 memcpy(namebuf, alt->base, namelen);
668 namebuf[namelen - 1] = 0;
669 fsck_object_dir(namebuf);
673 struct packed_git *p;
674 uint32_t total = 0, count = 0;
675 struct progress *progress = NULL;
677 prepare_packed_git();
680 for (p = packed_git; p; p = p->next) {
681 if (open_pack_index(p))
683 total += p->num_objects;
686 progress = start_progress(_("Checking objects"), total);
688 for (p = packed_git; p; p = p->next) {
689 /* verify gives error messages itself */
690 if (verify_pack(p, fsck_obj_buffer,
692 errors_found |= ERROR_PACK;
693 count += p->num_objects;
695 stop_progress(&progress);
699 for (i = 0; i < argc; i++) {
700 const char *arg = argv[i];
701 unsigned char sha1[20];
702 if (!get_sha1(arg, sha1)) {
703 struct object *obj = lookup_object(sha1);
705 /* Error is printed by lookup_object(). */
710 mark_object_reachable(obj);
714 error("invalid parameter: expected sha1, got '%s'", arg);
718 * If we've not been given any explicit head information, do the
719 * default ones from .git/refs. We also consider the index file
720 * in this case (ie this implies --cache).
724 keep_cache_objects = 1;
727 if (keep_cache_objects) {
729 for (i = 0; i < active_nr; i++) {
734 mode = active_cache[i]->ce_mode;
735 if (S_ISGITLINK(mode))
737 blob = lookup_blob(active_cache[i]->sha1);
742 mark_object_reachable(obj);
744 if (active_cache_tree)
745 fsck_cache_tree(active_cache_tree);
748 check_connectivity();