10 #define REACHABLE 0x0001
12 static int show_root = 0;
13 static int show_tags = 0;
14 static int show_unreachable = 0;
15 static int keep_cache_objects = 0;
16 static unsigned char head_sha1[20];
18 static void check_connectivity(void)
22 /* Look up all the requirements, warn about missing objects.. */
23 for (i = 0; i < nr_objs; i++) {
24 struct object *obj = objs[i];
25 struct object_list *refs;
28 printf("missing %s %s\n", obj->type, sha1_to_hex(obj->sha1));
32 for (refs = obj->refs; refs; refs = refs->next) {
33 if (refs->item->parsed)
35 printf("broken link from %7s %s\n",
36 obj->type, sha1_to_hex(obj->sha1));
37 printf(" to %7s %s\n",
38 refs->item->type, sha1_to_hex(refs->item->sha1));
41 /* Don't bother with tag reachability. */
42 if (obj->type == tag_type)
45 if (show_unreachable && !(obj->flags & REACHABLE)) {
46 printf("unreachable %s %s\n", obj->type, sha1_to_hex(obj->sha1));
51 printf("dangling %s %s\n", obj->type,
52 sha1_to_hex(obj->sha1));
58 * The entries in a tree are ordered in the _path_ order,
59 * which means that a directory entry is ordered by adding
60 * a slash to the end of it.
62 * So a directory called "a" is ordered _after_ a file
63 * called "a.c", because "a/" sorts after "a.c".
65 static int verify_ordered(struct tree_entry_list *a, struct tree_entry_list *b)
67 int len1 = strlen(a->name);
68 int len2 = strlen(b->name);
69 int len = len1 < len2 ? len1 : len2;
73 cmp = memcmp(a->name, b->name, len);
80 * Ok, the first <len> characters are the same.
81 * Now we need to order the next one, but turn
82 * a '\0' into a '/' for a directory entry.
86 if (!c1 && a->directory)
88 if (!c2 && b->directory)
90 return c1 < c2 ? 0 : -1;
93 static int fsck_tree(struct tree *item)
95 int has_full_path = 0;
96 struct tree_entry_list *entry, *last;
99 for (entry = item->entries; entry; entry = entry->next) {
100 if (strchr(entry->name, '/'))
103 switch (entry->mode) {
113 * This is nonstandard, but we had a few of these
114 * early on when we honored the full set of mode
120 printf("tree %s has entry %o %s\n",
121 sha1_to_hex(item->object.sha1),
122 entry->mode, entry->name);
126 if (verify_ordered(last, entry) < 0) {
127 fprintf(stderr, "tree %s not ordered\n",
128 sha1_to_hex(item->object.sha1));
137 fprintf(stderr, "warning: fsck-cache: tree %s "
138 "has full pathnames in it\n",
139 sha1_to_hex(item->object.sha1));
145 static int fsck_commit(struct commit *commit)
149 if (!commit->parents && show_root)
150 printf("root %s\n", sha1_to_hex(commit->object.sha1));
152 printf("bad commit date in %s\n",
153 sha1_to_hex(commit->object.sha1));
157 static int fsck_tag(struct tag *tag)
159 struct object *tagged = tag->tagged;
162 printf("bad object in tag %s\n", sha1_to_hex(tag->object.sha1));
168 printf("tagged %s %s", tagged->type, sha1_to_hex(tagged->sha1));
169 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
173 static int fsck_sha1(unsigned char *sha1)
175 struct object *obj = parse_object(sha1);
178 if (obj->type == blob_type)
180 if (obj->type == tree_type)
181 return fsck_tree((struct tree *) obj);
182 if (obj->type == commit_type)
183 return fsck_commit((struct commit *) obj);
184 if (obj->type == tag_type)
185 return fsck_tag((struct tag *) obj);
190 * This is the sorting chunk size: make it reasonably
191 * big so that we can sort well..
193 #define MAX_SHA1_ENTRIES (1024)
197 unsigned char sha1[20];
202 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
205 static int ino_compare(const void *_a, const void *_b)
207 const struct sha1_entry *a = _a, *b = _b;
208 unsigned long ino1 = a->ino, ino2 = b->ino;
209 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
212 static void fsck_sha1_list(void)
214 int i, nr = sha1_list.nr;
216 qsort(sha1_list.entry, nr, sizeof(struct sha1_entry *), ino_compare);
217 for (i = 0; i < nr; i++) {
218 struct sha1_entry *entry = sha1_list.entry[i];
219 unsigned char *sha1 = entry->sha1;
221 sha1_list.entry[i] = NULL;
222 if (fsck_sha1(sha1) < 0)
223 fprintf(stderr, "bad sha1 entry '%s'\n", sha1_to_hex(sha1));
229 static void add_sha1_list(unsigned char *sha1, unsigned long ino)
231 struct sha1_entry *entry = xmalloc(sizeof(*entry));
235 memcpy(entry->sha1, sha1, 20);
237 if (nr == MAX_SHA1_ENTRIES) {
241 sha1_list.entry[nr] = entry;
245 static int fsck_dir(int i, char *path)
247 DIR *dir = opendir(path);
251 return error("missing sha1 directory '%s'", path);
254 while ((de = readdir(dir)) != NULL) {
256 unsigned char sha1[20];
257 int len = strlen(de->d_name);
261 if (de->d_name[1] != '.')
264 if (de->d_name[0] != '.')
268 sprintf(name, "%02x", i);
269 memcpy(name+2, de->d_name, len+1);
270 if (get_sha1_hex(name, sha1) < 0)
272 add_sha1_list(sha1, de->d_ino);
275 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
281 int main(int argc, char **argv)
286 for (i = 1; i < argc; i++) {
287 const char *arg = argv[i];
289 if (!strcmp(arg, "--unreachable")) {
290 show_unreachable = 1;
293 if (!strcmp(arg, "--tags")) {
297 if (!strcmp(arg, "--root")) {
301 if (!strcmp(arg, "--cache")) {
302 keep_cache_objects = 1;
306 usage("fsck-cache [--tags] [[--unreachable] [--cache] <head-sha1>*]");
309 sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
310 for (i = 0; i < 256; i++) {
311 static char dir[4096];
312 sprintf(dir, "%s/%02x", sha1_dir, i);
318 for (i = 1; i < argc; i++) {
319 const char *arg = argv[i];
324 if (!get_sha1(arg, head_sha1)) {
325 struct object *obj = lookup_object(head_sha1);
327 /* Error is printed by lookup_object(). */
332 mark_reachable(obj, REACHABLE);
336 error("expected sha1, got %s", arg);
339 if (keep_cache_objects) {
342 for (i = 0; i < active_nr; i++) {
343 struct blob *blob = lookup_blob(active_cache[i]->sha1);
349 mark_reachable(obj, REACHABLE);
353 if (!heads && !keep_cache_objects) {
354 if (show_unreachable) {
355 fprintf(stderr, "unable to do reachability without a head nor --cache\n");
356 show_unreachable = 0;
359 fprintf(stderr, "expect dangling commits - potential heads - due to lack of head information\n");
362 check_connectivity();