9 #include "cache-tree.h"
10 #include "tree-walk.h"
12 #include "parse-options.h"
14 #define REACHABLE 0x0001
19 static int show_unreachable;
20 static int include_reflogs = 1;
21 static int check_full;
22 static int check_strict;
23 static int keep_cache_objects;
24 static unsigned char head_sha1[20];
25 static int errors_found;
26 static int write_lost_and_found;
28 #define ERROR_OBJECT 01
29 #define ERROR_REACHABLE 02
31 #ifdef NO_D_INO_IN_DIRENT
33 #define DIRENT_SORT_HINT(de) 0
36 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
39 static void objreport(struct object *obj, const char *severity,
40 const char *err, va_list params)
42 fprintf(stderr, "%s in %s %s: ",
43 severity, typename(obj->type), sha1_to_hex(obj->sha1));
44 vfprintf(stderr, err, params);
48 static int objerror(struct object *obj, const char *err, ...)
51 va_start(params, err);
52 errors_found |= ERROR_OBJECT;
53 objreport(obj, "error", err, params);
58 static int objwarning(struct object *obj, const char *err, ...)
61 va_start(params, err);
62 objreport(obj, "warning", err, params);
67 static int mark_object(struct object *obj, int type, void *data)
69 struct tree *tree = NULL;
70 struct object *parent = data;
74 printf("broken link from %7s %s\n",
75 typename(parent->type), sha1_to_hex(parent->sha1));
76 printf("broken link from %7s %s\n",
77 (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
78 errors_found |= ERROR_REACHABLE;
82 if (type != OBJ_ANY && obj->type != type)
83 objerror(parent, "wrong object type in link");
85 if (obj->flags & REACHABLE)
87 obj->flags |= REACHABLE;
89 if (parent && !has_sha1_file(obj->sha1)) {
90 printf("broken link from %7s %s\n",
91 typename(parent->type), sha1_to_hex(parent->sha1));
92 printf(" to %7s %s\n",
93 typename(obj->type), sha1_to_hex(obj->sha1));
94 errors_found |= ERROR_REACHABLE;
99 if (obj->type == OBJ_TREE) {
101 tree = (struct tree *)obj;
102 if (parse_tree(tree) < 0)
103 return 1; /* error already displayed */
105 result = fsck_walk(obj, mark_object, obj);
116 static void mark_object_reachable(struct object *obj)
118 mark_object(obj, OBJ_ANY, 0);
121 static int mark_used(struct object *obj, int type, void *data)
130 * Check a single reachable object
132 static void check_reachable_object(struct object *obj)
135 * We obviously want the object to be parsed,
136 * except if it was in a pack-file and we didn't
140 if (has_sha1_pack(obj->sha1, NULL))
141 return; /* it is in pack - forget about it */
142 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
143 errors_found |= ERROR_REACHABLE;
149 * Check a single unreachable object
151 static void check_unreachable_object(struct object *obj)
154 * Missing unreachable object? Ignore it. It's not like
155 * we miss it (since it can't be reached), nor do we want
156 * to complain about it being unreachable (since it does
163 * Unreachable object that exists? Show it if asked to,
164 * since this is something that is prunable.
166 if (show_unreachable) {
167 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
172 * "!used" means that nothing at all points to it, including
173 * other unreachable objects. In other words, it's the "tip"
174 * of some set of unreachable objects, usually a commit that
177 * Such starting points are more interesting than some random
178 * set of unreachable objects, so we show them even if the user
179 * hasn't asked for _all_ unreachable objects. If you have
180 * deleted a branch by mistake, this is a prime candidate to
181 * start looking at, for example.
184 printf("dangling %s %s\n", typename(obj->type),
185 sha1_to_hex(obj->sha1));
186 if (write_lost_and_found) {
187 char *filename = git_path("lost-found/%s/%s",
188 obj->type == OBJ_COMMIT ? "commit" : "other",
189 sha1_to_hex(obj->sha1));
192 if (safe_create_leading_directories(filename)) {
193 error("Could not create lost-found");
196 if (!(f = fopen(filename, "w")))
197 die("Could not open %s", filename);
198 if (obj->type == OBJ_BLOB) {
199 enum object_type type;
201 char *buf = read_sha1_file(obj->sha1,
204 fwrite(buf, size, 1, f);
208 fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
215 * Otherwise? It's there, it's unreachable, and some other unreachable
216 * object points to it. Ignore it - it's not interesting, and we showed
217 * all the interesting cases above.
221 static void check_object(struct object *obj)
224 fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
226 if (obj->flags & REACHABLE)
227 check_reachable_object(obj);
229 check_unreachable_object(obj);
232 static void check_connectivity(void)
236 /* Look up all the requirements, warn about missing objects.. */
237 max = get_max_object_index();
239 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
241 for (i = 0; i < max; i++) {
242 struct object *obj = get_indexed_object(i);
250 * The entries in a tree are ordered in the _path_ order,
251 * which means that a directory entry is ordered by adding
252 * a slash to the end of it.
254 * So a directory called "a" is ordered _after_ a file
255 * called "a.c", because "a/" sorts after "a.c".
257 #define TREE_UNORDERED (-1)
258 #define TREE_HAS_DUPS (-2)
260 static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
262 int len1 = strlen(name1);
263 int len2 = strlen(name2);
264 int len = len1 < len2 ? len1 : len2;
265 unsigned char c1, c2;
268 cmp = memcmp(name1, name2, len);
272 return TREE_UNORDERED;
275 * Ok, the first <len> characters are the same.
276 * Now we need to order the next one, but turn
277 * a '\0' into a '/' for a directory entry.
283 * git-write-tree used to write out a nonsense tree that has
284 * entries with the same name, one blob and one tree. Make
285 * sure we do not have duplicate entries.
287 return TREE_HAS_DUPS;
288 if (!c1 && S_ISDIR(mode1))
290 if (!c2 && S_ISDIR(mode2))
292 return c1 < c2 ? 0 : TREE_UNORDERED;
295 static int fsck_tree(struct tree *item)
298 int has_full_path = 0;
299 int has_empty_name = 0;
300 int has_zero_pad = 0;
301 int has_bad_modes = 0;
302 int has_dup_entries = 0;
303 int not_properly_sorted = 0;
304 struct tree_desc desc;
307 const unsigned char *o_sha1;
310 fprintf(stderr, "Checking tree %s\n",
311 sha1_to_hex(item->object.sha1));
313 init_tree_desc(&desc, item->buffer, item->size);
321 const unsigned char *sha1;
323 sha1 = tree_entry_extract(&desc, &name, &mode);
325 if (strchr(name, '/'))
329 has_zero_pad |= *(char *)desc.buffer == '0';
330 update_tree_entry(&desc);
343 * This is nonstandard, but we had a few of these
344 * early on when we honored the full set of mode
355 switch (verify_ordered(o_mode, o_name, mode, name)) {
357 not_properly_sorted = 1;
376 objwarning(&item->object, "contains full pathnames");
378 if (has_empty_name) {
379 objwarning(&item->object, "contains empty pathname");
382 objwarning(&item->object, "contains zero-padded file modes");
385 objwarning(&item->object, "contains bad file modes");
387 if (has_dup_entries) {
388 retval = objerror(&item->object, "contains duplicate file entries");
390 if (not_properly_sorted) {
391 retval = objerror(&item->object, "not properly sorted");
396 static int fsck_commit(struct commit *commit)
398 char *buffer = commit->buffer;
399 unsigned char tree_sha1[20], sha1[20];
402 fprintf(stderr, "Checking commit %s\n",
403 sha1_to_hex(commit->object.sha1));
405 if (memcmp(buffer, "tree ", 5))
406 return objerror(&commit->object, "invalid format - expected 'tree' line");
407 if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
408 return objerror(&commit->object, "invalid 'tree' line format - bad sha1");
410 while (!memcmp(buffer, "parent ", 7)) {
411 if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
412 return objerror(&commit->object, "invalid 'parent' line format - bad sha1");
415 if (memcmp(buffer, "author ", 7))
416 return objerror(&commit->object, "invalid format - expected 'author' line");
417 free(commit->buffer);
418 commit->buffer = NULL;
420 return objerror(&commit->object, "could not load commit's tree %s", tree_sha1);
421 if (!commit->parents && show_root)
422 printf("root %s\n", sha1_to_hex(commit->object.sha1));
424 printf("bad commit date in %s\n",
425 sha1_to_hex(commit->object.sha1));
429 static int fsck_tag(struct tag *tag)
431 struct object *tagged = tag->tagged;
434 fprintf(stderr, "Checking tag %s\n",
435 sha1_to_hex(tag->object.sha1));
438 return objerror(&tag->object, "could not load tagged object");
443 printf("tagged %s %s", typename(tagged->type), sha1_to_hex(tagged->sha1));
444 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
448 static int fsck_sha1(const unsigned char *sha1)
450 struct object *obj = parse_object(sha1);
452 errors_found |= ERROR_OBJECT;
453 return error("%s: object corrupt or missing",
456 if (obj->flags & SEEN)
459 if (fsck_walk(obj, mark_used, 0))
460 objerror(obj, "broken links");
461 if (obj->type == OBJ_BLOB)
463 if (obj->type == OBJ_TREE)
464 return fsck_tree((struct tree *) obj);
465 if (obj->type == OBJ_COMMIT)
466 return fsck_commit((struct commit *) obj);
467 if (obj->type == OBJ_TAG)
468 return fsck_tag((struct tag *) obj);
470 /* By now, parse_object() would've returned NULL instead. */
471 return objerror(obj, "unknown type '%d' (internal fsck error)",
476 * This is the sorting chunk size: make it reasonably
477 * big so that we can sort well..
479 #define MAX_SHA1_ENTRIES (1024)
483 unsigned char sha1[20];
488 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
491 static int ino_compare(const void *_a, const void *_b)
493 const struct sha1_entry *a = _a, *b = _b;
494 unsigned long ino1 = a->ino, ino2 = b->ino;
495 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
498 static void fsck_sha1_list(void)
500 int i, nr = sha1_list.nr;
503 qsort(sha1_list.entry, nr,
504 sizeof(struct sha1_entry *), ino_compare);
505 for (i = 0; i < nr; i++) {
506 struct sha1_entry *entry = sha1_list.entry[i];
507 unsigned char *sha1 = entry->sha1;
509 sha1_list.entry[i] = NULL;
516 static void add_sha1_list(unsigned char *sha1, unsigned long ino)
518 struct sha1_entry *entry = xmalloc(sizeof(*entry));
522 hashcpy(entry->sha1, sha1);
524 if (nr == MAX_SHA1_ENTRIES) {
528 sha1_list.entry[nr] = entry;
532 static void fsck_dir(int i, char *path)
534 DIR *dir = opendir(path);
541 fprintf(stderr, "Checking directory %s\n", path);
543 while ((de = readdir(dir)) != NULL) {
545 unsigned char sha1[20];
546 int len = strlen(de->d_name);
550 if (de->d_name[1] != '.')
553 if (de->d_name[0] != '.')
557 sprintf(name, "%02x", i);
558 memcpy(name+2, de->d_name, len+1);
559 if (get_sha1_hex(name, sha1) < 0)
561 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
564 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
569 static int default_refs;
571 static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
572 const char *email, unsigned long timestamp, int tz,
573 const char *message, void *cb_data)
578 fprintf(stderr, "Checking reflog %s->%s\n",
579 sha1_to_hex(osha1), sha1_to_hex(nsha1));
581 if (!is_null_sha1(osha1)) {
582 obj = lookup_object(osha1);
585 mark_object_reachable(obj);
588 obj = lookup_object(nsha1);
591 mark_object_reachable(obj);
596 static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
598 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
602 static int is_branch(const char *refname)
604 return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/");
607 static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
611 obj = parse_object(sha1);
613 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
614 /* We'll continue with the rest despite the error.. */
617 if (obj->type != OBJ_COMMIT && is_branch(refname))
618 error("%s: not a commit", refname);
621 mark_object_reachable(obj);
626 static void get_default_heads(void)
628 for_each_ref(fsck_handle_ref, NULL);
630 for_each_reflog(fsck_handle_reflog, NULL);
633 * Not having any default heads isn't really fatal, but
634 * it does mean that "--unreachable" no longer makes any
635 * sense (since in this case everything will obviously
636 * be unreachable by definition.
638 * Showing dangling objects is valid, though (as those
639 * dangling objects are likely lost heads).
641 * So we just print a warning about it, and clear the
642 * "show_unreachable" flag.
645 fprintf(stderr, "notice: No default references\n");
646 show_unreachable = 0;
650 static void fsck_object_dir(const char *path)
655 fprintf(stderr, "Checking object directory\n");
657 for (i = 0; i < 256; i++) {
658 static char dir[4096];
659 sprintf(dir, "%s/%02x", path, i);
665 static int fsck_head_link(void)
667 unsigned char sha1[20];
669 int null_is_error = 0;
670 const char *head_points_at = resolve_ref("HEAD", sha1, 0, &flag);
673 fprintf(stderr, "Checking HEAD link\n");
676 return error("Invalid HEAD");
677 if (!strcmp(head_points_at, "HEAD"))
680 else if (prefixcmp(head_points_at, "refs/heads/"))
681 return error("HEAD points to something strange (%s)",
683 if (is_null_sha1(sha1)) {
685 return error("HEAD: detached HEAD points at nothing");
686 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
687 head_points_at + 11);
692 static int fsck_cache_tree(struct cache_tree *it)
698 fprintf(stderr, "Checking cache tree\n");
700 if (0 <= it->entry_count) {
701 struct object *obj = parse_object(it->sha1);
703 error("%s: invalid sha1 pointer in cache-tree",
704 sha1_to_hex(it->sha1));
707 mark_object_reachable(obj);
709 if (obj->type != OBJ_TREE)
710 err |= objerror(obj, "non-tree in cache-tree");
712 for (i = 0; i < it->subtree_nr; i++)
713 err |= fsck_cache_tree(it->down[i]->cache_tree);
717 static char const * const fsck_usage[] = {
718 "git-fsck [options] [<object>...]",
722 static struct option fsck_opts[] = {
723 OPT__VERBOSE(&verbose),
724 OPT_BOOLEAN(0, "unreachable", &show_unreachable, "show unreachable objects"),
725 OPT_BOOLEAN(0, "tags", &show_tags, "report tags"),
726 OPT_BOOLEAN(0, "root", &show_root, "report root nodes"),
727 OPT_BOOLEAN(0, "cache", &keep_cache_objects, "make index objects head nodes"),
728 OPT_BOOLEAN(0, "reflogs", &include_reflogs, "make reflogs head nodes (default)"),
729 OPT_BOOLEAN(0, "full", &check_full, "also consider alternate objects"),
730 OPT_BOOLEAN(0, "strict", &check_strict, "enable more strict checking"),
731 OPT_BOOLEAN(0, "lost-found", &write_lost_and_found,
732 "write dangling objects in .git/lost-found"),
736 int cmd_fsck(int argc, const char **argv, const char *prefix)
742 argc = parse_options(argc, argv, fsck_opts, fsck_usage, 0);
743 if (write_lost_and_found) {
749 fsck_object_dir(get_object_directory());
751 struct alternate_object_database *alt;
752 struct packed_git *p;
754 for (alt = alt_odb_list; alt; alt = alt->next) {
755 char namebuf[PATH_MAX];
756 int namelen = alt->name - alt->base;
757 memcpy(namebuf, alt->base, namelen);
758 namebuf[namelen - 1] = 0;
759 fsck_object_dir(namebuf);
761 prepare_packed_git();
762 for (p = packed_git; p; p = p->next)
763 /* verify gives error messages itself */
766 for (p = packed_git; p; p = p->next) {
768 if (open_pack_index(p))
770 num = p->num_objects;
771 for (j = 0; j < num; j++)
772 fsck_sha1(nth_packed_object_sha1(p, j));
777 for (i = 1; i < argc; i++) {
778 const char *arg = argv[i];
779 if (!get_sha1(arg, head_sha1)) {
780 struct object *obj = lookup_object(head_sha1);
782 /* Error is printed by lookup_object(). */
787 mark_object_reachable(obj);
791 error("invalid parameter: expected sha1, got '%s'", arg);
795 * If we've not been given any explicit head information, do the
796 * default ones from .git/refs. We also consider the index file
797 * in this case (ie this implies --cache).
801 keep_cache_objects = 1;
804 if (keep_cache_objects) {
806 for (i = 0; i < active_nr; i++) {
811 mode = ntohl(active_cache[i]->ce_mode);
812 if (S_ISGITLINK(mode))
814 blob = lookup_blob(active_cache[i]->sha1);
819 mark_object_reachable(obj);
821 if (active_cache_tree)
822 fsck_cache_tree(active_cache_tree);
825 check_connectivity();