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 int errors_found;
27 static int write_lost_and_found;
29 #define ERROR_OBJECT 01
30 #define ERROR_REACHABLE 02
32 #ifdef NO_D_INO_IN_DIRENT
34 #define DIRENT_SORT_HINT(de) 0
37 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
40 static void objreport(struct object *obj, const char *severity,
41 const char *err, va_list params)
43 fprintf(stderr, "%s in %s %s: ",
44 severity, typename(obj->type), sha1_to_hex(obj->sha1));
45 vfprintf(stderr, err, params);
49 static int objerror(struct object *obj, const char *err, ...)
52 va_start(params, err);
53 errors_found |= ERROR_OBJECT;
54 objreport(obj, "error", err, params);
59 static int fsck_error_func(struct object *obj, int type, const char *err, ...)
62 va_start(params, err);
63 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", err, params);
65 return (type == FSCK_WARN) ? 0 : 1;
68 static struct object_array pending;
70 static int mark_object(struct object *obj, int type, void *data)
72 struct object *parent = data;
75 printf("broken link from %7s %s\n",
76 typename(parent->type), sha1_to_hex(parent->sha1));
77 printf("broken link from %7s %s\n",
78 (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
79 errors_found |= ERROR_REACHABLE;
83 if (type != OBJ_ANY && obj->type != type)
84 objerror(parent, "wrong object type in link");
86 if (obj->flags & REACHABLE)
88 obj->flags |= REACHABLE;
90 if (parent && !has_sha1_file(obj->sha1)) {
91 printf("broken link from %7s %s\n",
92 typename(parent->type), sha1_to_hex(parent->sha1));
93 printf(" to %7s %s\n",
94 typename(obj->type), sha1_to_hex(obj->sha1));
95 errors_found |= ERROR_REACHABLE;
100 add_object_array(obj, (void *) parent, &pending);
104 static void mark_object_reachable(struct object *obj)
106 mark_object(obj, OBJ_ANY, 0);
109 static int traverse_one_object(struct object *obj, struct object *parent)
112 struct tree *tree = NULL;
114 if (obj->type == OBJ_TREE) {
116 tree = (struct tree *)obj;
117 if (parse_tree(tree) < 0)
118 return 1; /* error already displayed */
120 result = fsck_walk(obj, mark_object, obj);
128 static int traverse_reachable(void)
132 struct object_array_entry *entry;
133 struct object *obj, *parent;
135 entry = pending.objects + --pending.nr;
137 parent = (struct object *) entry->name;
138 result |= traverse_one_object(obj, parent);
143 static int mark_used(struct object *obj, int type, void *data)
152 * Check a single reachable object
154 static void check_reachable_object(struct object *obj)
157 * We obviously want the object to be parsed,
158 * except if it was in a pack-file and we didn't
162 if (has_sha1_pack(obj->sha1, NULL))
163 return; /* it is in pack - forget about it */
164 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
165 errors_found |= ERROR_REACHABLE;
171 * Check a single unreachable object
173 static void check_unreachable_object(struct object *obj)
176 * Missing unreachable object? Ignore it. It's not like
177 * we miss it (since it can't be reached), nor do we want
178 * to complain about it being unreachable (since it does
185 * Unreachable object that exists? Show it if asked to,
186 * since this is something that is prunable.
188 if (show_unreachable) {
189 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
194 * "!used" means that nothing at all points to it, including
195 * other unreachable objects. In other words, it's the "tip"
196 * of some set of unreachable objects, usually a commit that
199 * Such starting points are more interesting than some random
200 * set of unreachable objects, so we show them even if the user
201 * hasn't asked for _all_ unreachable objects. If you have
202 * deleted a branch by mistake, this is a prime candidate to
203 * start looking at, for example.
206 printf("dangling %s %s\n", typename(obj->type),
207 sha1_to_hex(obj->sha1));
208 if (write_lost_and_found) {
209 char *filename = git_path("lost-found/%s/%s",
210 obj->type == OBJ_COMMIT ? "commit" : "other",
211 sha1_to_hex(obj->sha1));
214 if (safe_create_leading_directories(filename)) {
215 error("Could not create lost-found");
218 if (!(f = fopen(filename, "w")))
219 die("Could not open %s", filename);
220 if (obj->type == OBJ_BLOB) {
221 enum object_type type;
223 char *buf = read_sha1_file(obj->sha1,
226 if (fwrite(buf, size, 1, f) != 1)
227 die("Could not write %s: %s",
228 filename, strerror(errno));
232 fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
234 die("Could not finish %s: %s",
235 filename, strerror(errno));
241 * Otherwise? It's there, it's unreachable, and some other unreachable
242 * object points to it. Ignore it - it's not interesting, and we showed
243 * all the interesting cases above.
247 static void check_object(struct object *obj)
250 fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
252 if (obj->flags & REACHABLE)
253 check_reachable_object(obj);
255 check_unreachable_object(obj);
258 static void check_connectivity(void)
262 /* Traverse the pending reachable objects */
263 traverse_reachable();
265 /* Look up all the requirements, warn about missing objects.. */
266 max = get_max_object_index();
268 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
270 for (i = 0; i < max; i++) {
271 struct object *obj = get_indexed_object(i);
278 static int fsck_sha1(const unsigned char *sha1)
280 struct object *obj = parse_object(sha1);
282 errors_found |= ERROR_OBJECT;
283 return error("%s: object corrupt or missing",
286 if (obj->flags & SEEN)
291 fprintf(stderr, "Checking %s %s\n",
292 typename(obj->type), sha1_to_hex(obj->sha1));
294 if (fsck_walk(obj, mark_used, 0))
295 objerror(obj, "broken links");
296 if (fsck_object(obj, check_strict, fsck_error_func))
299 if (obj->type == OBJ_TREE) {
300 struct tree *item = (struct tree *) obj;
306 if (obj->type == OBJ_COMMIT) {
307 struct commit *commit = (struct commit *) obj;
309 free(commit->buffer);
310 commit->buffer = NULL;
312 if (!commit->parents && show_root)
313 printf("root %s\n", sha1_to_hex(commit->object.sha1));
316 if (obj->type == OBJ_TAG) {
317 struct tag *tag = (struct tag *) obj;
319 if (show_tags && tag->tagged) {
320 printf("tagged %s %s", typename(tag->tagged->type), sha1_to_hex(tag->tagged->sha1));
321 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
329 * This is the sorting chunk size: make it reasonably
330 * big so that we can sort well..
332 #define MAX_SHA1_ENTRIES (1024)
336 unsigned char sha1[20];
341 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
344 static int ino_compare(const void *_a, const void *_b)
346 const struct sha1_entry *a = _a, *b = _b;
347 unsigned long ino1 = a->ino, ino2 = b->ino;
348 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
351 static void fsck_sha1_list(void)
353 int i, nr = sha1_list.nr;
356 qsort(sha1_list.entry, nr,
357 sizeof(struct sha1_entry *), ino_compare);
358 for (i = 0; i < nr; i++) {
359 struct sha1_entry *entry = sha1_list.entry[i];
360 unsigned char *sha1 = entry->sha1;
362 sha1_list.entry[i] = NULL;
369 static void add_sha1_list(unsigned char *sha1, unsigned long ino)
371 struct sha1_entry *entry = xmalloc(sizeof(*entry));
375 hashcpy(entry->sha1, sha1);
377 if (nr == MAX_SHA1_ENTRIES) {
381 sha1_list.entry[nr] = entry;
385 static void fsck_dir(int i, char *path)
387 DIR *dir = opendir(path);
394 fprintf(stderr, "Checking directory %s\n", path);
396 while ((de = readdir(dir)) != NULL) {
398 unsigned char sha1[20];
400 if (is_dot_or_dotdot(de->d_name))
402 if (strlen(de->d_name) == 38) {
403 sprintf(name, "%02x", i);
404 memcpy(name+2, de->d_name, 39);
405 if (get_sha1_hex(name, sha1) < 0)
407 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
410 if (!prefixcmp(de->d_name, "tmp_obj_"))
412 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
417 static int default_refs;
419 static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
420 const char *email, unsigned long timestamp, int tz,
421 const char *message, void *cb_data)
426 fprintf(stderr, "Checking reflog %s->%s\n",
427 sha1_to_hex(osha1), sha1_to_hex(nsha1));
429 if (!is_null_sha1(osha1)) {
430 obj = lookup_object(osha1);
433 mark_object_reachable(obj);
436 obj = lookup_object(nsha1);
439 mark_object_reachable(obj);
444 static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
446 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
450 static int is_branch(const char *refname)
452 return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/");
455 static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
459 obj = parse_object(sha1);
461 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
462 /* We'll continue with the rest despite the error.. */
465 if (obj->type != OBJ_COMMIT && is_branch(refname))
466 error("%s: not a commit", refname);
469 mark_object_reachable(obj);
474 static void get_default_heads(void)
476 for_each_ref(fsck_handle_ref, NULL);
478 for_each_reflog(fsck_handle_reflog, NULL);
481 * Not having any default heads isn't really fatal, but
482 * it does mean that "--unreachable" no longer makes any
483 * sense (since in this case everything will obviously
484 * be unreachable by definition.
486 * Showing dangling objects is valid, though (as those
487 * dangling objects are likely lost heads).
489 * So we just print a warning about it, and clear the
490 * "show_unreachable" flag.
493 fprintf(stderr, "notice: No default references\n");
494 show_unreachable = 0;
498 static void fsck_object_dir(const char *path)
503 fprintf(stderr, "Checking object directory\n");
505 for (i = 0; i < 256; i++) {
506 static char dir[4096];
507 sprintf(dir, "%s/%02x", path, i);
513 static int fsck_head_link(void)
515 unsigned char sha1[20];
517 int null_is_error = 0;
518 const char *head_points_at = resolve_ref("HEAD", sha1, 0, &flag);
521 fprintf(stderr, "Checking HEAD link\n");
524 return error("Invalid HEAD");
525 if (!strcmp(head_points_at, "HEAD"))
528 else if (prefixcmp(head_points_at, "refs/heads/"))
529 return error("HEAD points to something strange (%s)",
531 if (is_null_sha1(sha1)) {
533 return error("HEAD: detached HEAD points at nothing");
534 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
535 head_points_at + 11);
540 static int fsck_cache_tree(struct cache_tree *it)
546 fprintf(stderr, "Checking cache tree\n");
548 if (0 <= it->entry_count) {
549 struct object *obj = parse_object(it->sha1);
551 error("%s: invalid sha1 pointer in cache-tree",
552 sha1_to_hex(it->sha1));
555 mark_object_reachable(obj);
557 if (obj->type != OBJ_TREE)
558 err |= objerror(obj, "non-tree in cache-tree");
560 for (i = 0; i < it->subtree_nr; i++)
561 err |= fsck_cache_tree(it->down[i]->cache_tree);
565 static char const * const fsck_usage[] = {
566 "git fsck [options] [<object>...]",
570 static struct option fsck_opts[] = {
571 OPT__VERBOSE(&verbose),
572 OPT_BOOLEAN(0, "unreachable", &show_unreachable, "show unreachable objects"),
573 OPT_BOOLEAN(0, "tags", &show_tags, "report tags"),
574 OPT_BOOLEAN(0, "root", &show_root, "report root nodes"),
575 OPT_BOOLEAN(0, "cache", &keep_cache_objects, "make index objects head nodes"),
576 OPT_BOOLEAN(0, "reflogs", &include_reflogs, "make reflogs head nodes (default)"),
577 OPT_BOOLEAN(0, "full", &check_full, "also consider alternate objects"),
578 OPT_BOOLEAN(0, "strict", &check_strict, "enable more strict checking"),
579 OPT_BOOLEAN(0, "lost-found", &write_lost_and_found,
580 "write dangling objects in .git/lost-found"),
584 int cmd_fsck(int argc, const char **argv, const char *prefix)
590 argc = parse_options(argc, argv, fsck_opts, fsck_usage, 0);
591 if (write_lost_and_found) {
597 fsck_object_dir(get_object_directory());
599 struct alternate_object_database *alt;
600 struct packed_git *p;
602 for (alt = alt_odb_list; alt; alt = alt->next) {
603 char namebuf[PATH_MAX];
604 int namelen = alt->name - alt->base;
605 memcpy(namebuf, alt->base, namelen);
606 namebuf[namelen - 1] = 0;
607 fsck_object_dir(namebuf);
609 prepare_packed_git();
610 for (p = packed_git; p; p = p->next)
611 /* verify gives error messages itself */
614 for (p = packed_git; p; p = p->next) {
616 if (open_pack_index(p))
618 num = p->num_objects;
619 for (j = 0; j < num; j++)
620 fsck_sha1(nth_packed_object_sha1(p, j));
625 for (i = 0; i < argc; i++) {
626 const char *arg = argv[i];
627 if (!get_sha1(arg, head_sha1)) {
628 struct object *obj = lookup_object(head_sha1);
630 /* Error is printed by lookup_object(). */
635 mark_object_reachable(obj);
639 error("invalid parameter: expected sha1, got '%s'", arg);
643 * If we've not been given any explicit head information, do the
644 * default ones from .git/refs. We also consider the index file
645 * in this case (ie this implies --cache).
649 keep_cache_objects = 1;
652 if (keep_cache_objects) {
654 for (i = 0; i < active_nr; i++) {
659 mode = active_cache[i]->ce_mode;
660 if (S_ISGITLINK(mode))
662 blob = lookup_blob(active_cache[i]->sha1);
667 mark_object_reachable(obj);
669 if (active_cache_tree)
670 fsck_cache_tree(active_cache_tree);
673 check_connectivity();