8 #include "cache-tree.h"
11 #define REACHABLE 0x0001
16 static int show_unreachable;
17 static int check_full;
18 static int check_strict;
19 static int keep_cache_objects;
20 static unsigned char head_sha1[20];
21 static int errors_found;
22 #define ERROR_OBJECT 01
23 #define ERROR_REACHABLE 02
25 #ifdef NO_D_INO_IN_DIRENT
27 #define DIRENT_SORT_HINT(de) 0
30 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
33 static void objreport(struct object *obj, const char *severity,
34 const char *err, va_list params)
36 fprintf(stderr, "%s in %s %s: ",
37 severity, typename(obj->type), sha1_to_hex(obj->sha1));
38 vfprintf(stderr, err, params);
42 static int objerror(struct object *obj, const char *err, ...)
45 va_start(params, err);
46 errors_found |= ERROR_OBJECT;
47 objreport(obj, "error", err, params);
52 static int objwarning(struct object *obj, const char *err, ...)
55 va_start(params, err);
56 objreport(obj, "warning", err, params);
62 * Check a single reachable object
64 static void check_reachable_object(struct object *obj)
66 const struct object_refs *refs;
69 * We obviously want the object to be parsed,
70 * except if it was in a pack-file and we didn't
74 if (has_sha1_pack(obj->sha1, NULL))
75 return; /* it is in pack - forget about it */
76 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
77 errors_found |= ERROR_REACHABLE;
82 * Check that everything that we try to reference is also good.
84 refs = lookup_object_refs(obj);
87 for (j = 0; j < refs->count; j++) {
88 struct object *ref = refs->ref[j];
90 (has_sha1_file(ref->sha1)))
92 printf("broken link from %7s %s\n",
93 typename(obj->type), sha1_to_hex(obj->sha1));
94 printf(" to %7s %s\n",
95 typename(ref->type), sha1_to_hex(ref->sha1));
96 errors_found |= ERROR_REACHABLE;
102 * Check a single unreachable object
104 static void check_unreachable_object(struct object *obj)
107 * Missing unreachable object? Ignore it. It's not like
108 * we miss it (since it can't be reached), nor do we want
109 * to complain about it being unreachable (since it does
116 * Unreachable object that exists? Show it if asked to,
117 * since this is something that is prunable.
119 if (show_unreachable) {
120 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
125 * "!used" means that nothing at all points to it, including
126 * other unreachable objects. In other words, it's the "tip"
127 * of some set of unreachable objects, usually a commit that
130 * Such starting points are more interesting than some random
131 * set of unreachable objects, so we show them even if the user
132 * hasn't asked for _all_ unreachable objects. If you have
133 * deleted a branch by mistake, this is a prime candidate to
134 * start looking at, for example.
137 printf("dangling %s %s\n", typename(obj->type),
138 sha1_to_hex(obj->sha1));
143 * Otherwise? It's there, it's unreachable, and some other unreachable
144 * object points to it. Ignore it - it's not interesting, and we showed
145 * all the interesting cases above.
149 static void check_object(struct object *obj)
151 if (obj->flags & REACHABLE)
152 check_reachable_object(obj);
154 check_unreachable_object(obj);
157 static void check_connectivity(void)
161 /* Look up all the requirements, warn about missing objects.. */
162 max = get_max_object_index();
163 for (i = 0; i < max; i++) {
164 struct object *obj = get_indexed_object(i);
172 * The entries in a tree are ordered in the _path_ order,
173 * which means that a directory entry is ordered by adding
174 * a slash to the end of it.
176 * So a directory called "a" is ordered _after_ a file
177 * called "a.c", because "a/" sorts after "a.c".
179 #define TREE_UNORDERED (-1)
180 #define TREE_HAS_DUPS (-2)
182 static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
184 int len1 = strlen(name1);
185 int len2 = strlen(name2);
186 int len = len1 < len2 ? len1 : len2;
187 unsigned char c1, c2;
190 cmp = memcmp(name1, name2, len);
194 return TREE_UNORDERED;
197 * Ok, the first <len> characters are the same.
198 * Now we need to order the next one, but turn
199 * a '\0' into a '/' for a directory entry.
205 * git-write-tree used to write out a nonsense tree that has
206 * entries with the same name, one blob and one tree. Make
207 * sure we do not have duplicate entries.
209 return TREE_HAS_DUPS;
210 if (!c1 && S_ISDIR(mode1))
212 if (!c2 && S_ISDIR(mode2))
214 return c1 < c2 ? 0 : TREE_UNORDERED;
217 static int fsck_tree(struct tree *item)
220 int has_full_path = 0;
221 int has_empty_name = 0;
222 int has_zero_pad = 0;
223 int has_bad_modes = 0;
224 int has_dup_entries = 0;
225 int not_properly_sorted = 0;
226 struct tree_desc desc;
229 const unsigned char *o_sha1;
231 init_tree_desc(&desc, item->buffer, item->size);
239 const unsigned char *sha1;
241 sha1 = tree_entry_extract(&desc, &name, &mode);
243 if (strchr(name, '/'))
247 has_zero_pad |= *(char *)desc.buffer == '0';
248 update_tree_entry(&desc);
260 * This is nonstandard, but we had a few of these
261 * early on when we honored the full set of mode
272 switch (verify_ordered(o_mode, o_name, mode, name)) {
274 not_properly_sorted = 1;
293 objwarning(&item->object, "contains full pathnames");
295 if (has_empty_name) {
296 objwarning(&item->object, "contains empty pathname");
299 objwarning(&item->object, "contains zero-padded file modes");
302 objwarning(&item->object, "contains bad file modes");
304 if (has_dup_entries) {
305 retval = objerror(&item->object, "contains duplicate file entries");
307 if (not_properly_sorted) {
308 retval = objerror(&item->object, "not properly sorted");
313 static int fsck_commit(struct commit *commit)
315 char *buffer = commit->buffer;
316 unsigned char tree_sha1[20], sha1[20];
318 if (memcmp(buffer, "tree ", 5))
319 return objerror(&commit->object, "invalid format - expected 'tree' line");
320 if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
321 return objerror(&commit->object, "invalid 'tree' line format - bad sha1");
323 while (!memcmp(buffer, "parent ", 7)) {
324 if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
325 return objerror(&commit->object, "invalid 'parent' line format - bad sha1");
328 if (memcmp(buffer, "author ", 7))
329 return objerror(&commit->object, "invalid format - expected 'author' line");
330 free(commit->buffer);
331 commit->buffer = NULL;
333 return objerror(&commit->object, "could not load commit's tree %s", tree_sha1);
334 if (!commit->parents && show_root)
335 printf("root %s\n", sha1_to_hex(commit->object.sha1));
337 printf("bad commit date in %s\n",
338 sha1_to_hex(commit->object.sha1));
342 static int fsck_tag(struct tag *tag)
344 struct object *tagged = tag->tagged;
347 return objerror(&tag->object, "could not load tagged object");
352 printf("tagged %s %s", typename(tagged->type), sha1_to_hex(tagged->sha1));
353 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
357 static int fsck_sha1(unsigned char *sha1)
359 struct object *obj = parse_object(sha1);
361 errors_found |= ERROR_OBJECT;
362 return error("%s: object corrupt or missing",
365 if (obj->flags & SEEN)
368 if (obj->type == OBJ_BLOB)
370 if (obj->type == OBJ_TREE)
371 return fsck_tree((struct tree *) obj);
372 if (obj->type == OBJ_COMMIT)
373 return fsck_commit((struct commit *) obj);
374 if (obj->type == OBJ_TAG)
375 return fsck_tag((struct tag *) obj);
377 /* By now, parse_object() would've returned NULL instead. */
378 return objerror(obj, "unknown type '%d' (internal fsck error)",
383 * This is the sorting chunk size: make it reasonably
384 * big so that we can sort well..
386 #define MAX_SHA1_ENTRIES (1024)
390 unsigned char sha1[20];
395 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
398 static int ino_compare(const void *_a, const void *_b)
400 const struct sha1_entry *a = _a, *b = _b;
401 unsigned long ino1 = a->ino, ino2 = b->ino;
402 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
405 static void fsck_sha1_list(void)
407 int i, nr = sha1_list.nr;
410 qsort(sha1_list.entry, nr,
411 sizeof(struct sha1_entry *), ino_compare);
412 for (i = 0; i < nr; i++) {
413 struct sha1_entry *entry = sha1_list.entry[i];
414 unsigned char *sha1 = entry->sha1;
416 sha1_list.entry[i] = NULL;
423 static void add_sha1_list(unsigned char *sha1, unsigned long ino)
425 struct sha1_entry *entry = xmalloc(sizeof(*entry));
429 hashcpy(entry->sha1, sha1);
431 if (nr == MAX_SHA1_ENTRIES) {
435 sha1_list.entry[nr] = entry;
439 static void fsck_dir(int i, char *path)
441 DIR *dir = opendir(path);
447 while ((de = readdir(dir)) != NULL) {
449 unsigned char sha1[20];
450 int len = strlen(de->d_name);
454 if (de->d_name[1] != '.')
457 if (de->d_name[0] != '.')
461 sprintf(name, "%02x", i);
462 memcpy(name+2, de->d_name, len+1);
463 if (get_sha1_hex(name, sha1) < 0)
465 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
468 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
473 static int default_refs;
475 static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
476 const char *email, unsigned long timestamp, int tz,
477 const char *message, void *cb_data)
481 if (!is_null_sha1(osha1)) {
482 obj = lookup_object(osha1);
485 mark_reachable(obj, REACHABLE);
488 obj = lookup_object(nsha1);
491 mark_reachable(obj, REACHABLE);
496 static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
498 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
502 static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
506 obj = lookup_object(sha1);
508 if (has_sha1_file(sha1)) {
510 return 0; /* it is in a pack */
512 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
513 /* We'll continue with the rest despite the error.. */
518 mark_reachable(obj, REACHABLE);
523 static void get_default_heads(void)
525 for_each_ref(fsck_handle_ref, NULL);
526 for_each_reflog(fsck_handle_reflog, NULL);
529 * Not having any default heads isn't really fatal, but
530 * it does mean that "--unreachable" no longer makes any
531 * sense (since in this case everything will obviously
532 * be unreachable by definition.
534 * Showing dangling objects is valid, though (as those
535 * dangling objects are likely lost heads).
537 * So we just print a warning about it, and clear the
538 * "show_unreachable" flag.
541 fprintf(stderr, "notice: No default references\n");
542 show_unreachable = 0;
546 static void fsck_object_dir(const char *path)
549 for (i = 0; i < 256; i++) {
550 static char dir[4096];
551 sprintf(dir, "%s/%02x", path, i);
557 static int fsck_head_link(void)
559 unsigned char sha1[20];
561 int null_is_error = 0;
562 const char *head_points_at = resolve_ref("HEAD", sha1, 0, &flag);
565 return error("Invalid HEAD");
566 if (!strcmp(head_points_at, "HEAD"))
569 else if (prefixcmp(head_points_at, "refs/heads/"))
570 return error("HEAD points to something strange (%s)",
572 if (is_null_sha1(sha1)) {
574 return error("HEAD: detached HEAD points at nothing");
575 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
576 head_points_at + 11);
581 static int fsck_cache_tree(struct cache_tree *it)
586 if (0 <= it->entry_count) {
587 struct object *obj = parse_object(it->sha1);
589 error("%s: invalid sha1 pointer in cache-tree",
590 sha1_to_hex(it->sha1));
593 mark_reachable(obj, REACHABLE);
595 if (obj->type != OBJ_TREE)
596 err |= objerror(obj, "non-tree in cache-tree");
598 for (i = 0; i < it->subtree_nr; i++)
599 err |= fsck_cache_tree(it->down[i]->cache_tree);
603 static const char fsck_usage[] =
604 "git-fsck [--tags] [--root] [[--unreachable] [--cache] [--full] "
605 "[--strict] <head-sha1>*]";
607 int cmd_fsck(int argc, char **argv, const char *prefix)
611 track_object_refs = 1;
614 for (i = 1; i < argc; i++) {
615 const char *arg = argv[i];
617 if (!strcmp(arg, "--unreachable")) {
618 show_unreachable = 1;
621 if (!strcmp(arg, "--tags")) {
625 if (!strcmp(arg, "--root")) {
629 if (!strcmp(arg, "--cache")) {
630 keep_cache_objects = 1;
633 if (!strcmp(arg, "--full")) {
637 if (!strcmp(arg, "--strict")) {
646 fsck_object_dir(get_object_directory());
648 struct alternate_object_database *alt;
649 struct packed_git *p;
651 for (alt = alt_odb_list; alt; alt = alt->next) {
652 char namebuf[PATH_MAX];
653 int namelen = alt->name - alt->base;
654 memcpy(namebuf, alt->base, namelen);
655 namebuf[namelen - 1] = 0;
656 fsck_object_dir(namebuf);
658 prepare_packed_git();
659 for (p = packed_git; p; p = p->next)
660 /* verify gives error messages itself */
663 for (p = packed_git; p; p = p->next) {
664 uint32_t i, num = num_packed_objects(p);
665 for (i = 0; i < num; i++) {
666 unsigned char sha1[20];
667 nth_packed_object_sha1(p, i, sha1);
674 for (i = 1; i < argc; i++) {
675 const char *arg = argv[i];
680 if (!get_sha1(arg, head_sha1)) {
681 struct object *obj = lookup_object(head_sha1);
683 /* Error is printed by lookup_object(). */
688 mark_reachable(obj, REACHABLE);
692 error("invalid parameter: expected sha1, got '%s'", arg);
696 * If we've not been given any explicit head information, do the
697 * default ones from .git/refs. We also consider the index file
698 * in this case (ie this implies --cache).
702 keep_cache_objects = 1;
705 if (keep_cache_objects) {
708 for (i = 0; i < active_nr; i++) {
709 struct blob *blob = lookup_blob(active_cache[i]->sha1);
715 mark_reachable(obj, REACHABLE);
717 if (active_cache_tree)
718 fsck_cache_tree(active_cache_tree);
721 check_connectivity();