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
];
25 if (show_unreachable
&& !(obj
->flags
& REACHABLE
)) {
26 printf("unreachable %s %s\n", obj
->type
, sha1_to_hex(obj
->sha1
));
31 printf("missing %s %s\n", obj
->type
,
32 sha1_to_hex(obj
->sha1
));
35 printf("dangling %s %s\n", obj
->type
,
36 sha1_to_hex(obj
->sha1
));
41 static int fsck_tree(unsigned char *sha1
, void *data
, unsigned long size
)
43 struct tree
*item
= lookup_tree(sha1
);
46 if (item
->has_full_path
) {
47 fprintf(stderr
, "warning: fsck-cache: tree %s "
48 "has full pathnames in it\n", sha1_to_hex(sha1
));
53 static int fsck_commit(unsigned char *sha1
, void *data
, unsigned long size
)
55 struct commit
*commit
= lookup_commit(sha1
);
56 if (parse_commit(commit
))
60 if (!commit
->parents
&& show_root
)
61 printf("root %s\n", sha1_to_hex(sha1
));
63 printf("bad commit date in %s\n", sha1_to_hex(sha1
));
67 static int fsck_blob(unsigned char *sha1
, void *data
, unsigned long size
)
69 struct blob
*blob
= lookup_blob(sha1
);
70 blob
->object
.parsed
= 1;
74 static int fsck_tag(unsigned char *sha1
, void *data
, unsigned long size
)
77 unsigned char object
[20];
79 const char *type_line
, *tag_line
, *sig_line
;
83 if (memcmp("object ", data
, 7) || get_sha1_hex(data
+ 7, object
))
86 type_line
= data
+ 48;
87 if (memcmp("\ntype ", type_line
-1, 6))
90 tag_line
= strchr(type_line
, '\n');
91 if (!tag_line
|| memcmp("tag ", ++tag_line
, 4))
94 sig_line
= strchr(tag_line
, '\n');
99 typelen
= tag_line
- type_line
- strlen("type \n");
102 taglen
= sig_line
- tag_line
- strlen("tag \n");
107 strcpy(object_hex
, sha1_to_hex(object
));
108 printf("tagged %.*s %s (%.*s) in %s\n",
109 typelen
, type_line
+ 5,
111 taglen
, tag_line
+ 4,
116 static int fsck_entry(unsigned char *sha1
, char *tag
, void *data
,
119 if (!strcmp(tag
, "blob")) {
120 if (fsck_blob(sha1
, data
, size
) < 0)
122 } else if (!strcmp(tag
, "tree")) {
123 if (fsck_tree(sha1
, data
, size
) < 0)
125 } else if (!strcmp(tag
, "commit")) {
126 if (fsck_commit(sha1
, data
, size
) < 0)
128 } else if (!strcmp(tag
, "tag")) {
129 if (fsck_tag(sha1
, data
, size
) < 0)
136 static int fsck_name(char *hex
)
138 unsigned char sha1
[20];
139 if (!get_sha1_hex(hex
, sha1
)) {
140 unsigned long mapsize
;
141 void *map
= map_sha1_file(sha1
, &mapsize
);
145 void *buffer
= unpack_sha1_file(map
, mapsize
, type
, &size
);
148 if (check_sha1_signature(sha1
, buffer
, size
, type
) < 0)
149 printf("sha1 mismatch %s\n", sha1_to_hex(sha1
));
150 munmap(map
, mapsize
);
151 if (!fsck_entry(sha1
, type
, buffer
, size
))
158 static int fsck_dir(int i
, char *path
)
160 DIR *dir
= opendir(path
);
164 return error("missing sha1 directory '%s'", path
);
167 while ((de
= readdir(dir
)) != NULL
) {
169 int len
= strlen(de
->d_name
);
173 if (de
->d_name
[1] != '.')
176 if (de
->d_name
[0] != '.')
180 sprintf(name
, "%02x", i
);
181 memcpy(name
+2, de
->d_name
, len
+1);
182 if (!fsck_name(name
))
185 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
191 int main(int argc
, char **argv
)
196 for (i
= 1; i
< argc
; i
++) {
197 const char *arg
= argv
[i
];
199 if (!strcmp(arg
, "--unreachable")) {
200 show_unreachable
= 1;
203 if (!strcmp(arg
, "--tags")) {
207 if (!strcmp(arg
, "--root")) {
212 usage("fsck-cache [--tags] [[--unreachable] <head-sha1>*]");
215 sha1_dir
= getenv(DB_ENVIRONMENT
) ? : DEFAULT_DB_ENVIRONMENT
;
216 for (i
= 0; i
< 256; i
++) {
217 static char dir
[4096];
218 sprintf(dir
, "%s/%02x", sha1_dir
, i
);
223 for (i
= 1; i
< argc
; i
++) {
224 const char *arg
= argv
[i
];
229 if (!get_sha1_hex(arg
, head_sha1
)) {
230 struct object
*obj
= &lookup_commit(head_sha1
)->object
;
232 mark_reachable(obj
, REACHABLE
);
236 error("expected sha1, got %s", arg
);
240 if (show_unreachable
) {
241 fprintf(stderr
, "unable to do reachability without a head\n");
242 show_unreachable
= 0;
244 fprintf(stderr
, "expect dangling commits - potential heads - due to lack of head information\n");
247 check_connectivity();