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;
26 if (show_unreachable && !(obj->flags & REACHABLE)) {
27 printf("unreachable %s %s\n", obj->type, sha1_to_hex(obj->sha1));
32 printf("missing %s %s\n", obj->type,
33 sha1_to_hex(obj->sha1));
36 printf("dangling %s %s\n", obj->type,
37 sha1_to_hex(obj->sha1));
39 for (refs = obj->refs; refs; refs = refs->next) {
40 if (!refs->item->parsed) {
41 printf("broken link from %s\n",
42 sha1_to_hex(obj->sha1));
44 sha1_to_hex(refs->item->sha1));
50 static int fsck_tree(struct tree *item)
52 if (item->has_full_path) {
53 fprintf(stderr, "warning: fsck-cache: tree %s "
54 "has full pathnames in it\n",
55 sha1_to_hex(item->object.sha1));
60 static int fsck_commit(struct commit *commit)
64 if (!commit->parents && show_root)
65 printf("root %s\n", sha1_to_hex(commit->object.sha1));
67 printf("bad commit date in %s\n",
68 sha1_to_hex(commit->object.sha1));
72 static int fsck_tag(struct tag *tag)
77 printf("tagged %s %s",
79 sha1_to_hex(tag->tagged->sha1));
80 printf(" (%s) in %s\n",
81 tag->tag, sha1_to_hex(tag->object.sha1));
85 static int fsck_name(char *hex)
87 unsigned char sha1[20];
88 if (!get_sha1_hex(hex, sha1)) {
89 struct object *obj = parse_object(sha1);
92 if (obj->type == blob_type)
94 if (obj->type == tree_type)
95 return fsck_tree((struct tree *) obj);
96 if (obj->type == commit_type)
97 return fsck_commit((struct commit *) obj);
98 if (obj->type == tag_type)
99 return fsck_tag((struct tag *) obj);
104 static int fsck_dir(int i, char *path)
106 DIR *dir = opendir(path);
110 return error("missing sha1 directory '%s'", path);
113 while ((de = readdir(dir)) != NULL) {
115 int len = strlen(de->d_name);
119 if (de->d_name[1] != '.')
122 if (de->d_name[0] != '.')
126 sprintf(name, "%02x", i);
127 memcpy(name+2, de->d_name, len+1);
128 if (!fsck_name(name))
131 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
137 int main(int argc, char **argv)
142 for (i = 1; i < argc; i++) {
143 const char *arg = argv[i];
145 if (!strcmp(arg, "--unreachable")) {
146 show_unreachable = 1;
149 if (!strcmp(arg, "--tags")) {
153 if (!strcmp(arg, "--root")) {
158 usage("fsck-cache [--tags] [[--unreachable] <head-sha1>*]");
161 sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
162 for (i = 0; i < 256; i++) {
163 static char dir[4096];
164 sprintf(dir, "%s/%02x", sha1_dir, i);
169 for (i = 1; i < argc; i++) {
170 const char *arg = argv[i];
175 if (!get_sha1_hex(arg, head_sha1)) {
176 struct commit *commit = lookup_commit(head_sha1);
179 /* Error is printed by lookup_commit(). */
183 obj = &commit->object;
185 mark_reachable(obj, REACHABLE);
189 error("expected sha1, got %s", arg);
193 if (show_unreachable) {
194 fprintf(stderr, "unable to do reachability without a head\n");
195 show_unreachable = 0;
197 fprintf(stderr, "expect dangling commits - potential heads - due to lack of head information\n");
200 check_connectivity();