9 #include "cache-tree.h"
10 #include "tree-walk.h"
12 #include "parse-options.h"
15 #define REACHABLE 0x0001
20 static int show_unreachable;
21 static int include_reflogs = 1;
22 static int check_full;
23 static int check_strict;
24 static int keep_cache_objects;
25 static unsigned char head_sha1[20];
26 static const char *head_points_at;
27 static int errors_found;
28 static int write_lost_and_found;
30 #define ERROR_OBJECT 01
31 #define ERROR_REACHABLE 02
33 #ifdef NO_D_INO_IN_DIRENT
35 #define DIRENT_SORT_HINT(de) 0
38 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
41 static void objreport(struct object *obj, const char *severity,
42 const char *err, va_list params)
44 fprintf(stderr, "%s in %s %s: ",
45 severity, typename(obj->type), sha1_to_hex(obj->sha1));
46 vfprintf(stderr, err, params);
50 static int objerror(struct object *obj, const char *err, ...)
53 va_start(params, err);
54 errors_found |= ERROR_OBJECT;
55 objreport(obj, "error", err, params);
60 static int fsck_error_func(struct object *obj, int type, const char *err, ...)
63 va_start(params, err);
64 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", err, params);
66 return (type == FSCK_WARN) ? 0 : 1;
69 static struct object_array pending;
71 static int mark_object(struct object *obj, int type, void *data)
73 struct object *parent = data;
76 printf("broken link from %7s %s\n",
77 typename(parent->type), sha1_to_hex(parent->sha1));
78 printf("broken link from %7s %s\n",
79 (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
80 errors_found |= ERROR_REACHABLE;
84 if (type != OBJ_ANY && obj->type != type)
85 objerror(parent, "wrong object type in link");
87 if (obj->flags & REACHABLE)
89 obj->flags |= REACHABLE;
91 if (parent && !has_sha1_file(obj->sha1)) {
92 printf("broken link from %7s %s\n",
93 typename(parent->type), sha1_to_hex(parent->sha1));
94 printf(" to %7s %s\n",
95 typename(obj->type), sha1_to_hex(obj->sha1));
96 errors_found |= ERROR_REACHABLE;
101 add_object_array(obj, (void *) parent, &pending);
105 static void mark_object_reachable(struct object *obj)
107 mark_object(obj, OBJ_ANY, NULL);
110 static int traverse_one_object(struct object *obj, struct object *parent)
113 struct tree *tree = NULL;
115 if (obj->type == OBJ_TREE) {
117 tree = (struct tree *)obj;
118 if (parse_tree(tree) < 0)
119 return 1; /* error already displayed */
121 result = fsck_walk(obj, mark_object, obj);
129 static int traverse_reachable(void)
133 struct object_array_entry *entry;
134 struct object *obj, *parent;
136 entry = pending.objects + --pending.nr;
138 parent = (struct object *) entry->name;
139 result |= traverse_one_object(obj, parent);
144 static int mark_used(struct object *obj, int type, void *data)
153 * Check a single reachable object
155 static void check_reachable_object(struct object *obj)
158 * We obviously want the object to be parsed,
159 * except if it was in a pack-file and we didn't
163 if (has_sha1_pack(obj->sha1))
164 return; /* it is in pack - forget about it */
165 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
166 errors_found |= ERROR_REACHABLE;
172 * Check a single unreachable object
174 static void check_unreachable_object(struct object *obj)
177 * Missing unreachable object? Ignore it. It's not like
178 * we miss it (since it can't be reached), nor do we want
179 * to complain about it being unreachable (since it does
186 * Unreachable object that exists? Show it if asked to,
187 * since this is something that is prunable.
189 if (show_unreachable) {
190 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
195 * "!used" means that nothing at all points to it, including
196 * other unreachable objects. In other words, it's the "tip"
197 * of some set of unreachable objects, usually a commit that
200 * Such starting points are more interesting than some random
201 * set of unreachable objects, so we show them even if the user
202 * hasn't asked for _all_ unreachable objects. If you have
203 * deleted a branch by mistake, this is a prime candidate to
204 * start looking at, for example.
207 printf("dangling %s %s\n", typename(obj->type),
208 sha1_to_hex(obj->sha1));
209 if (write_lost_and_found) {
210 char *filename = git_path("lost-found/%s/%s",
211 obj->type == OBJ_COMMIT ? "commit" : "other",
212 sha1_to_hex(obj->sha1));
215 if (safe_create_leading_directories(filename)) {
216 error("Could not create lost-found");
219 if (!(f = fopen(filename, "w")))
220 die_errno("Could not open '%s'", filename);
221 if (obj->type == OBJ_BLOB) {
222 enum object_type type;
224 char *buf = read_sha1_file(obj->sha1,
227 if (fwrite(buf, size, 1, f) != 1)
228 die_errno("Could not write '%s'",
233 fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
235 die_errno("Could not finish '%s'",
242 * Otherwise? It's there, it's unreachable, and some other unreachable
243 * object points to it. Ignore it - it's not interesting, and we showed
244 * all the interesting cases above.
248 static void check_object(struct object *obj)
251 fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
253 if (obj->flags & REACHABLE)
254 check_reachable_object(obj);
256 check_unreachable_object(obj);
259 static void check_connectivity(void)
263 /* Traverse the pending reachable objects */
264 traverse_reachable();
266 /* Look up all the requirements, warn about missing objects.. */
267 max = get_max_object_index();
269 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
271 for (i = 0; i < max; i++) {
272 struct object *obj = get_indexed_object(i);
279 static int fsck_sha1(const unsigned char *sha1)
281 struct object *obj = parse_object(sha1);
283 errors_found |= ERROR_OBJECT;
284 return error("%s: object corrupt or missing",
287 if (obj->flags & SEEN)
292 fprintf(stderr, "Checking %s %s\n",
293 typename(obj->type), sha1_to_hex(obj->sha1));
295 if (fsck_walk(obj, mark_used, NULL))
296 objerror(obj, "broken links");
297 if (fsck_object(obj, check_strict, fsck_error_func))
300 if (obj->type == OBJ_TREE) {
301 struct tree *item = (struct tree *) obj;
307 if (obj->type == OBJ_COMMIT) {
308 struct commit *commit = (struct commit *) obj;
310 free(commit->buffer);
311 commit->buffer = NULL;
313 if (!commit->parents && show_root)
314 printf("root %s\n", sha1_to_hex(commit->object.sha1));
317 if (obj->type == OBJ_TAG) {
318 struct tag *tag = (struct tag *) obj;
320 if (show_tags && tag->tagged) {
321 printf("tagged %s %s", typename(tag->tagged->type), sha1_to_hex(tag->tagged->sha1));
322 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
330 * This is the sorting chunk size: make it reasonably
331 * big so that we can sort well..
333 #define MAX_SHA1_ENTRIES (1024)
337 unsigned char sha1[20];
342 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
345 static int ino_compare(const void *_a, const void *_b)
347 const struct sha1_entry *a = _a, *b = _b;
348 unsigned long ino1 = a->ino, ino2 = b->ino;
349 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
352 static void fsck_sha1_list(void)
354 int i, nr = sha1_list.nr;
357 qsort(sha1_list.entry, nr,
358 sizeof(struct sha1_entry *), ino_compare);
359 for (i = 0; i < nr; i++) {
360 struct sha1_entry *entry = sha1_list.entry[i];
361 unsigned char *sha1 = entry->sha1;
363 sha1_list.entry[i] = NULL;
370 static void add_sha1_list(unsigned char *sha1, unsigned long ino)
372 struct sha1_entry *entry = xmalloc(sizeof(*entry));
376 hashcpy(entry->sha1, sha1);
378 if (nr == MAX_SHA1_ENTRIES) {
382 sha1_list.entry[nr] = entry;
386 static void fsck_dir(int i, char *path)
388 DIR *dir = opendir(path);
395 fprintf(stderr, "Checking directory %s\n", path);
397 while ((de = readdir(dir)) != NULL) {
399 unsigned char sha1[20];
401 if (is_dot_or_dotdot(de->d_name))
403 if (strlen(de->d_name) == 38) {
404 sprintf(name, "%02x", i);
405 memcpy(name+2, de->d_name, 39);
406 if (get_sha1_hex(name, sha1) < 0)
408 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
411 if (!prefixcmp(de->d_name, "tmp_obj_"))
413 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
418 static int default_refs;
420 static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
421 const char *email, unsigned long timestamp, int tz,
422 const char *message, void *cb_data)
427 fprintf(stderr, "Checking reflog %s->%s\n",
428 sha1_to_hex(osha1), sha1_to_hex(nsha1));
430 if (!is_null_sha1(osha1)) {
431 obj = lookup_object(osha1);
434 mark_object_reachable(obj);
437 obj = lookup_object(nsha1);
440 mark_object_reachable(obj);
445 static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
447 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
451 static int is_branch(const char *refname)
453 return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/");
456 static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
460 obj = parse_object(sha1);
462 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
463 /* We'll continue with the rest despite the error.. */
466 if (obj->type != OBJ_COMMIT && is_branch(refname))
467 error("%s: not a commit", refname);
470 mark_object_reachable(obj);
475 static void get_default_heads(void)
477 if (head_points_at && !is_null_sha1(head_sha1))
478 fsck_handle_ref("HEAD", head_sha1, 0, NULL);
479 for_each_ref(fsck_handle_ref, NULL);
481 for_each_reflog(fsck_handle_reflog, NULL);
484 * Not having any default heads isn't really fatal, but
485 * it does mean that "--unreachable" no longer makes any
486 * sense (since in this case everything will obviously
487 * be unreachable by definition.
489 * Showing dangling objects is valid, though (as those
490 * dangling objects are likely lost heads).
492 * So we just print a warning about it, and clear the
493 * "show_unreachable" flag.
496 fprintf(stderr, "notice: No default references\n");
497 show_unreachable = 0;
501 static void fsck_object_dir(const char *path)
506 fprintf(stderr, "Checking object directory\n");
508 for (i = 0; i < 256; i++) {
509 static char dir[4096];
510 sprintf(dir, "%s/%02x", path, i);
516 static int fsck_head_link(void)
519 int null_is_error = 0;
522 fprintf(stderr, "Checking HEAD link\n");
524 head_points_at = resolve_ref("HEAD", head_sha1, 0, &flag);
526 return error("Invalid HEAD");
527 if (!strcmp(head_points_at, "HEAD"))
530 else if (prefixcmp(head_points_at, "refs/heads/"))
531 return error("HEAD points to something strange (%s)",
533 if (is_null_sha1(head_sha1)) {
535 return error("HEAD: detached HEAD points at nothing");
536 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
537 head_points_at + 11);
542 static int fsck_cache_tree(struct cache_tree *it)
548 fprintf(stderr, "Checking cache tree\n");
550 if (0 <= it->entry_count) {
551 struct object *obj = parse_object(it->sha1);
553 error("%s: invalid sha1 pointer in cache-tree",
554 sha1_to_hex(it->sha1));
557 mark_object_reachable(obj);
559 if (obj->type != OBJ_TREE)
560 err |= objerror(obj, "non-tree in cache-tree");
562 for (i = 0; i < it->subtree_nr; i++)
563 err |= fsck_cache_tree(it->down[i]->cache_tree);
567 static char const * const fsck_usage[] = {
568 "git fsck [options] [<object>...]",
572 static struct option fsck_opts[] = {
573 OPT__VERBOSE(&verbose),
574 OPT_BOOLEAN(0, "unreachable", &show_unreachable, "show unreachable objects"),
575 OPT_BOOLEAN(0, "tags", &show_tags, "report tags"),
576 OPT_BOOLEAN(0, "root", &show_root, "report root nodes"),
577 OPT_BOOLEAN(0, "cache", &keep_cache_objects, "make index objects head nodes"),
578 OPT_BOOLEAN(0, "reflogs", &include_reflogs, "make reflogs head nodes (default)"),
579 OPT_BOOLEAN(0, "full", &check_full, "also consider alternate objects"),
580 OPT_BOOLEAN(0, "strict", &check_strict, "enable more strict checking"),
581 OPT_BOOLEAN(0, "lost-found", &write_lost_and_found,
582 "write dangling objects in .git/lost-found"),
586 int cmd_fsck(int argc, const char **argv, const char *prefix)
589 struct alternate_object_database *alt;
592 read_replace_refs = 0;
594 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
595 if (write_lost_and_found) {
601 fsck_object_dir(get_object_directory());
604 for (alt = alt_odb_list; alt; alt = alt->next) {
605 char namebuf[PATH_MAX];
606 int namelen = alt->name - alt->base;
607 memcpy(namebuf, alt->base, namelen);
608 namebuf[namelen - 1] = 0;
609 fsck_object_dir(namebuf);
613 struct packed_git *p;
615 prepare_packed_git();
616 for (p = packed_git; p; p = p->next)
617 /* verify gives error messages itself */
620 for (p = packed_git; p; p = p->next) {
622 if (open_pack_index(p))
624 num = p->num_objects;
625 for (j = 0; j < num; j++)
626 fsck_sha1(nth_packed_object_sha1(p, j));
631 for (i = 0; i < argc; i++) {
632 const char *arg = argv[i];
633 unsigned char sha1[20];
634 if (!get_sha1(arg, sha1)) {
635 struct object *obj = lookup_object(sha1);
637 /* Error is printed by lookup_object(). */
642 mark_object_reachable(obj);
646 error("invalid parameter: expected sha1, got '%s'", arg);
650 * If we've not been given any explicit head information, do the
651 * default ones from .git/refs. We also consider the index file
652 * in this case (ie this implies --cache).
656 keep_cache_objects = 1;
659 if (keep_cache_objects) {
661 for (i = 0; i < active_nr; i++) {
666 mode = active_cache[i]->ce_mode;
667 if (S_ISGITLINK(mode))
669 blob = lookup_blob(active_cache[i]->sha1);
674 mark_object_reachable(obj);
676 if (active_cache_tree)
677 fsck_cache_tree(active_cache_tree);
680 check_connectivity();