9 #include "cache-tree.h"
10 #include "tree-walk.h"
12 #include "parse-options.h"
16 #define REACHABLE 0x0001
21 static int show_unreachable;
22 static int include_reflogs = 1;
23 static int check_full = 1;
24 static int check_strict;
25 static int keep_cache_objects;
26 static unsigned char head_sha1[20];
27 static const char *head_points_at;
28 static int errors_found;
29 static int write_lost_and_found;
31 static int show_progress = -1;
32 static int show_dangling = 1;
33 #define ERROR_OBJECT 01
34 #define ERROR_REACHABLE 02
37 #ifdef NO_D_INO_IN_DIRENT
39 #define DIRENT_SORT_HINT(de) 0
42 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
45 static void objreport(struct object *obj, const char *severity,
46 const char *err, va_list params)
48 fprintf(stderr, "%s in %s %s: ",
49 severity, typename(obj->type), sha1_to_hex(obj->sha1));
50 vfprintf(stderr, err, params);
54 __attribute__((format (printf, 2, 3)))
55 static int objerror(struct object *obj, const char *err, ...)
58 va_start(params, err);
59 errors_found |= ERROR_OBJECT;
60 objreport(obj, "error", err, params);
65 __attribute__((format (printf, 3, 4)))
66 static int fsck_error_func(struct object *obj, int type, const char *err, ...)
69 va_start(params, err);
70 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", err, params);
72 return (type == FSCK_WARN) ? 0 : 1;
75 static struct object_array pending;
77 static int mark_object(struct object *obj, int type, void *data)
79 struct object *parent = data;
82 * The only case data is NULL or type is OBJ_ANY is when
83 * mark_object_reachable() calls us. All the callers of
84 * that function has non-NULL obj hence ...
87 /* ... these references to parent->fld are safe here */
88 printf("broken link from %7s %s\n",
89 typename(parent->type), sha1_to_hex(parent->sha1));
90 printf("broken link from %7s %s\n",
91 (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
92 errors_found |= ERROR_REACHABLE;
96 if (type != OBJ_ANY && obj->type != type)
97 /* ... and the reference to parent is safe here */
98 objerror(parent, "wrong object type in link");
100 if (obj->flags & REACHABLE)
102 obj->flags |= REACHABLE;
104 if (parent && !has_sha1_file(obj->sha1)) {
105 printf("broken link from %7s %s\n",
106 typename(parent->type), sha1_to_hex(parent->sha1));
107 printf(" to %7s %s\n",
108 typename(obj->type), sha1_to_hex(obj->sha1));
109 errors_found |= ERROR_REACHABLE;
114 add_object_array(obj, (void *) parent, &pending);
118 static void mark_object_reachable(struct object *obj)
120 mark_object(obj, OBJ_ANY, NULL);
123 static int traverse_one_object(struct object *obj)
126 struct tree *tree = NULL;
128 if (obj->type == OBJ_TREE) {
130 tree = (struct tree *)obj;
131 if (parse_tree(tree) < 0)
132 return 1; /* error already displayed */
134 result = fsck_walk(obj, mark_object, obj);
142 static int traverse_reachable(void)
144 struct progress *progress = NULL;
148 progress = start_progress_delay("Checking connectivity", 0, 0, 2);
150 struct object_array_entry *entry;
153 entry = pending.objects + --pending.nr;
155 result |= traverse_one_object(obj);
156 display_progress(progress, ++nr);
158 stop_progress(&progress);
162 static int mark_used(struct object *obj, int type, void *data)
171 * Check a single reachable object
173 static void check_reachable_object(struct object *obj)
176 * We obviously want the object to be parsed,
177 * except if it was in a pack-file and we didn't
181 if (has_sha1_pack(obj->sha1))
182 return; /* it is in pack - forget about it */
183 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
184 errors_found |= ERROR_REACHABLE;
190 * Check a single unreachable object
192 static void check_unreachable_object(struct object *obj)
195 * Missing unreachable object? Ignore it. It's not like
196 * we miss it (since it can't be reached), nor do we want
197 * to complain about it being unreachable (since it does
204 * Unreachable object that exists? Show it if asked to,
205 * since this is something that is prunable.
207 if (show_unreachable) {
208 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
213 * "!used" means that nothing at all points to it, including
214 * other unreachable objects. In other words, it's the "tip"
215 * of some set of unreachable objects, usually a commit that
218 * Such starting points are more interesting than some random
219 * set of unreachable objects, so we show them even if the user
220 * hasn't asked for _all_ unreachable objects. If you have
221 * deleted a branch by mistake, this is a prime candidate to
222 * start looking at, for example.
226 printf("dangling %s %s\n", typename(obj->type),
227 sha1_to_hex(obj->sha1));
228 if (write_lost_and_found) {
229 char *filename = git_path("lost-found/%s/%s",
230 obj->type == OBJ_COMMIT ? "commit" : "other",
231 sha1_to_hex(obj->sha1));
234 if (safe_create_leading_directories(filename)) {
235 error("Could not create lost-found");
238 if (!(f = fopen(filename, "w")))
239 die_errno("Could not open '%s'", filename);
240 if (obj->type == OBJ_BLOB) {
241 enum object_type type;
243 char *buf = read_sha1_file(obj->sha1,
245 if (buf && fwrite(buf, 1, size, f) != size)
246 die_errno("Could not write '%s'", filename);
249 fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
251 die_errno("Could not finish '%s'",
258 * Otherwise? It's there, it's unreachable, and some other unreachable
259 * object points to it. Ignore it - it's not interesting, and we showed
260 * all the interesting cases above.
264 static void check_object(struct object *obj)
267 fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
269 if (obj->flags & REACHABLE)
270 check_reachable_object(obj);
272 check_unreachable_object(obj);
275 static void check_connectivity(void)
279 /* Traverse the pending reachable objects */
280 traverse_reachable();
282 /* Look up all the requirements, warn about missing objects.. */
283 max = get_max_object_index();
285 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
287 for (i = 0; i < max; i++) {
288 struct object *obj = get_indexed_object(i);
295 static int fsck_obj(struct object *obj)
297 if (obj->flags & SEEN)
302 fprintf(stderr, "Checking %s %s\n",
303 typename(obj->type), sha1_to_hex(obj->sha1));
305 if (fsck_walk(obj, mark_used, NULL))
306 objerror(obj, "broken links");
307 if (fsck_object(obj, check_strict, fsck_error_func))
310 if (obj->type == OBJ_TREE) {
311 struct tree *item = (struct tree *) obj;
317 if (obj->type == OBJ_COMMIT) {
318 struct commit *commit = (struct commit *) obj;
320 free(commit->buffer);
321 commit->buffer = NULL;
323 if (!commit->parents && show_root)
324 printf("root %s\n", sha1_to_hex(commit->object.sha1));
327 if (obj->type == OBJ_TAG) {
328 struct tag *tag = (struct tag *) obj;
330 if (show_tags && tag->tagged) {
331 printf("tagged %s %s", typename(tag->tagged->type), sha1_to_hex(tag->tagged->sha1));
332 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
339 static int fsck_sha1(const unsigned char *sha1)
341 struct object *obj = parse_object(sha1);
343 errors_found |= ERROR_OBJECT;
344 return error("%s: object corrupt or missing",
347 return fsck_obj(obj);
350 static int fsck_obj_buffer(const unsigned char *sha1, enum object_type type,
351 unsigned long size, void *buffer, int *eaten)
354 obj = parse_object_buffer(sha1, type, size, buffer, eaten);
356 errors_found |= ERROR_OBJECT;
357 return error("%s: object corrupt or missing", sha1_to_hex(sha1));
359 return fsck_obj(obj);
363 * This is the sorting chunk size: make it reasonably
364 * big so that we can sort well..
366 #define MAX_SHA1_ENTRIES (1024)
370 unsigned char sha1[20];
375 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
378 static int ino_compare(const void *_a, const void *_b)
380 const struct sha1_entry *a = _a, *b = _b;
381 unsigned long ino1 = a->ino, ino2 = b->ino;
382 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
385 static void fsck_sha1_list(void)
387 int i, nr = sha1_list.nr;
390 qsort(sha1_list.entry, nr,
391 sizeof(struct sha1_entry *), ino_compare);
392 for (i = 0; i < nr; i++) {
393 struct sha1_entry *entry = sha1_list.entry[i];
394 unsigned char *sha1 = entry->sha1;
396 sha1_list.entry[i] = NULL;
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 (!prefixcmp(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 unsigned char *sha1, int flag, void *cb_data)
486 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
490 static int is_branch(const char *refname)
492 return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/");
495 static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
499 obj = parse_object(sha1);
501 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
502 /* We'll continue with the rest despite the error.. */
505 if (obj->type != OBJ_COMMIT && is_branch(refname))
506 error("%s: not a commit", refname);
509 mark_object_reachable(obj);
514 static void get_default_heads(void)
516 if (head_points_at && !is_null_sha1(head_sha1))
517 fsck_handle_ref("HEAD", head_sha1, 0, NULL);
518 for_each_ref(fsck_handle_ref, NULL);
520 for_each_reflog(fsck_handle_reflog, NULL);
523 * Not having any default heads isn't really fatal, but
524 * it does mean that "--unreachable" no longer makes any
525 * sense (since in this case everything will obviously
526 * be unreachable by definition.
528 * Showing dangling objects is valid, though (as those
529 * dangling objects are likely lost heads).
531 * So we just print a warning about it, and clear the
532 * "show_unreachable" flag.
535 fprintf(stderr, "notice: No default references\n");
536 show_unreachable = 0;
540 static void fsck_object_dir(const char *path)
543 struct progress *progress = NULL;
546 fprintf(stderr, "Checking object directory\n");
549 progress = start_progress("Checking object directories", 256);
550 for (i = 0; i < 256; i++) {
551 static char dir[4096];
552 sprintf(dir, "%s/%02x", path, i);
554 display_progress(progress, i+1);
556 stop_progress(&progress);
560 static int fsck_head_link(void)
563 int null_is_error = 0;
566 fprintf(stderr, "Checking HEAD link\n");
568 head_points_at = resolve_ref_unsafe("HEAD", head_sha1, 0, &flag);
570 return error("Invalid HEAD");
571 if (!strcmp(head_points_at, "HEAD"))
574 else if (prefixcmp(head_points_at, "refs/heads/"))
575 return error("HEAD points to something strange (%s)",
577 if (is_null_sha1(head_sha1)) {
579 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->sha1);
597 error("%s: invalid sha1 pointer in cache-tree",
598 sha1_to_hex(it->sha1));
602 mark_object_reachable(obj);
603 if (obj->type != OBJ_TREE)
604 err |= objerror(obj, "non-tree in cache-tree");
606 for (i = 0; i < it->subtree_nr; i++)
607 err |= fsck_cache_tree(it->down[i]->cache_tree);
611 static char const * const fsck_usage[] = {
612 "git fsck [options] [<object>...]",
616 static struct option fsck_opts[] = {
617 OPT__VERBOSE(&verbose, "be verbose"),
618 OPT_BOOLEAN(0, "unreachable", &show_unreachable, "show unreachable objects"),
619 OPT_BOOL(0, "dangling", &show_dangling, "show dangling objects"),
620 OPT_BOOLEAN(0, "tags", &show_tags, "report tags"),
621 OPT_BOOLEAN(0, "root", &show_root, "report root nodes"),
622 OPT_BOOLEAN(0, "cache", &keep_cache_objects, "make index objects head nodes"),
623 OPT_BOOLEAN(0, "reflogs", &include_reflogs, "make reflogs head nodes (default)"),
624 OPT_BOOLEAN(0, "full", &check_full, "also consider packs and alternate objects"),
625 OPT_BOOLEAN(0, "strict", &check_strict, "enable more strict checking"),
626 OPT_BOOLEAN(0, "lost-found", &write_lost_and_found,
627 "write dangling objects in .git/lost-found"),
628 OPT_BOOL(0, "progress", &show_progress, "show progress"),
632 int cmd_fsck(int argc, const char **argv, const char *prefix)
635 struct alternate_object_database *alt;
638 read_replace_refs = 0;
640 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
642 if (show_progress == -1)
643 show_progress = isatty(2);
647 if (write_lost_and_found) {
653 fsck_object_dir(get_object_directory());
656 for (alt = alt_odb_list; alt; alt = alt->next) {
657 char namebuf[PATH_MAX];
658 int namelen = alt->name - alt->base;
659 memcpy(namebuf, alt->base, namelen);
660 namebuf[namelen - 1] = 0;
661 fsck_object_dir(namebuf);
665 struct packed_git *p;
666 uint32_t total = 0, count = 0;
667 struct progress *progress = NULL;
669 prepare_packed_git();
672 for (p = packed_git; p; p = p->next) {
673 if (open_pack_index(p))
675 total += p->num_objects;
678 progress = start_progress("Checking objects", total);
680 for (p = packed_git; p; p = p->next) {
681 /* verify gives error messages itself */
682 if (verify_pack(p, fsck_obj_buffer,
684 errors_found |= ERROR_PACK;
685 count += p->num_objects;
687 stop_progress(&progress);
691 for (i = 0; i < argc; i++) {
692 const char *arg = argv[i];
693 unsigned char sha1[20];
694 if (!get_sha1(arg, sha1)) {
695 struct object *obj = lookup_object(sha1);
697 /* Error is printed by lookup_object(). */
702 mark_object_reachable(obj);
706 error("invalid parameter: expected sha1, got '%s'", arg);
710 * If we've not been given any explicit head information, do the
711 * default ones from .git/refs. We also consider the index file
712 * in this case (ie this implies --cache).
716 keep_cache_objects = 1;
719 if (keep_cache_objects) {
721 for (i = 0; i < active_nr; i++) {
726 mode = active_cache[i]->ce_mode;
727 if (S_ISGITLINK(mode))
729 blob = lookup_blob(active_cache[i]->sha1);
734 mark_object_reachable(obj);
736 if (active_cache_tree)
737 fsck_cache_tree(active_cache_tree);
740 check_connectivity();