10 #define REACHABLE 0x0001
12 static int show_root = 0;
13 static int show_tags = 0;
14 static int show_unreachable = 0;
15 static unsigned char head_sha1[20];
17 static void check_connectivity(void)
21 /* Look up all the requirements, warn about missing objects.. */
22 for (i = 0; i < nr_objs; i++) {
23 struct object *obj = objs[i];
24 struct object_list *refs;
27 printf("missing %s %s\n", obj->type, sha1_to_hex(obj->sha1));
31 for (refs = obj->refs; refs; refs = refs->next) {
32 if (refs->item->parsed)
34 printf("broken link from %7s %s\n",
35 obj->type, sha1_to_hex(obj->sha1));
36 printf(" to %7s %s\n",
37 obj->type, sha1_to_hex(refs->item->sha1));
40 /* Don't bother with tag reachability. */
41 if (obj->type == tag_type)
44 if (show_unreachable && !(obj->flags & REACHABLE)) {
45 printf("unreachable %s %s\n", obj->type, sha1_to_hex(obj->sha1));
50 printf("dangling %s %s\n", obj->type,
51 sha1_to_hex(obj->sha1));
56 static int fsck_tree(struct tree *item)
58 if (item->has_full_path) {
59 fprintf(stderr, "warning: fsck-cache: tree %s "
60 "has full pathnames in it\n",
61 sha1_to_hex(item->object.sha1));
66 static int fsck_commit(struct commit *commit)
70 if (!commit->parents && show_root)
71 printf("root %s\n", sha1_to_hex(commit->object.sha1));
73 printf("bad commit date in %s\n",
74 sha1_to_hex(commit->object.sha1));
78 static int fsck_tag(struct tag *tag)
83 printf("tagged %s %s",
85 sha1_to_hex(tag->tagged->sha1));
86 printf(" (%s) in %s\n",
87 tag->tag, sha1_to_hex(tag->object.sha1));
91 static int fsck_sha1(unsigned char *sha1)
93 struct object *obj = parse_object(sha1);
96 if (obj->type == blob_type)
98 if (obj->type == tree_type)
99 return fsck_tree((struct tree *) obj);
100 if (obj->type == commit_type)
101 return fsck_commit((struct commit *) obj);
102 if (obj->type == tag_type)
103 return fsck_tag((struct tag *) obj);
108 * This is the sorting chunk size: make it reasonably
109 * big so that we can sort well..
111 #define MAX_SHA1_ENTRIES (1024)
115 unsigned char sha1[20];
120 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
123 static int ino_compare(const void *_a, const void *_b)
125 const struct sha1_entry *a = _a, *b = _b;
126 unsigned long ino1 = a->ino, ino2 = b->ino;
127 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
130 static void fsck_sha1_list(void)
132 int i, nr = sha1_list.nr;
134 qsort(sha1_list.entry, nr, sizeof(struct sha1_entry *), ino_compare);
135 for (i = 0; i < nr; i++) {
136 struct sha1_entry *entry = sha1_list.entry[i];
137 unsigned char *sha1 = entry->sha1;
139 sha1_list.entry[i] = NULL;
140 if (fsck_sha1(sha1) < 0)
141 fprintf(stderr, "bad sha1 entry '%s'\n", sha1_to_hex(sha1));
147 static void add_sha1_list(unsigned char *sha1, unsigned long ino)
149 struct sha1_entry *entry = xmalloc(sizeof(*entry));
153 memcpy(entry->sha1, sha1, 20);
155 if (nr == MAX_SHA1_ENTRIES) {
159 sha1_list.entry[nr] = entry;
163 static int fsck_dir(int i, char *path)
165 DIR *dir = opendir(path);
169 return error("missing sha1 directory '%s'", path);
172 while ((de = readdir(dir)) != NULL) {
174 unsigned char sha1[20];
175 int len = strlen(de->d_name);
179 if (de->d_name[1] != '.')
182 if (de->d_name[0] != '.')
186 sprintf(name, "%02x", i);
187 memcpy(name+2, de->d_name, len+1);
188 if (get_sha1_hex(name, sha1) < 0)
190 add_sha1_list(sha1, de->d_ino);
193 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
199 int main(int argc, char **argv)
204 for (i = 1; i < argc; i++) {
205 const char *arg = argv[i];
207 if (!strcmp(arg, "--unreachable")) {
208 show_unreachable = 1;
211 if (!strcmp(arg, "--tags")) {
215 if (!strcmp(arg, "--root")) {
220 usage("fsck-cache [--tags] [[--unreachable] <head-sha1>*]");
223 sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
224 for (i = 0; i < 256; i++) {
225 static char dir[4096];
226 sprintf(dir, "%s/%02x", sha1_dir, i);
232 for (i = 1; i < argc; i++) {
233 const char *arg = argv[i];
238 if (!get_sha1(arg, head_sha1)) {
239 struct commit *commit = lookup_commit(head_sha1);
242 /* Error is printed by lookup_commit(). */
246 obj = &commit->object;
248 mark_reachable(obj, REACHABLE);
252 error("expected sha1, got %s", arg);
256 if (show_unreachable) {
257 fprintf(stderr, "unable to do reachability without a head\n");
258 show_unreachable = 0;
260 fprintf(stderr, "expect dangling commits - potential heads - due to lack of head information\n");
263 check_connectivity();