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
, '/'))
104 if (verify_ordered(last
, entry
) < 0) {
105 fprintf(stderr
, "tree %s not ordered\n",
106 sha1_to_hex(item
->object
.sha1
));
115 fprintf(stderr
, "warning: fsck-cache: tree %s "
116 "has full pathnames in it\n",
117 sha1_to_hex(item
->object
.sha1
));
123 static int fsck_commit(struct commit
*commit
)
127 if (!commit
->parents
&& show_root
)
128 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
130 printf("bad commit date in %s\n",
131 sha1_to_hex(commit
->object
.sha1
));
135 static int fsck_tag(struct tag
*tag
)
137 struct object
*tagged
= tag
->tagged
;
140 printf("bad object in tag %s\n", sha1_to_hex(tag
->object
.sha1
));
146 printf("tagged %s %s", tagged
->type
, sha1_to_hex(tagged
->sha1
));
147 printf(" (%s) in %s\n", tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
151 static int fsck_sha1(unsigned char *sha1
)
153 struct object
*obj
= parse_object(sha1
);
156 if (obj
->type
== blob_type
)
158 if (obj
->type
== tree_type
)
159 return fsck_tree((struct tree
*) obj
);
160 if (obj
->type
== commit_type
)
161 return fsck_commit((struct commit
*) obj
);
162 if (obj
->type
== tag_type
)
163 return fsck_tag((struct tag
*) obj
);
168 * This is the sorting chunk size: make it reasonably
169 * big so that we can sort well..
171 #define MAX_SHA1_ENTRIES (1024)
175 unsigned char sha1
[20];
180 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
183 static int ino_compare(const void *_a
, const void *_b
)
185 const struct sha1_entry
*a
= _a
, *b
= _b
;
186 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
187 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
190 static void fsck_sha1_list(void)
192 int i
, nr
= sha1_list
.nr
;
194 qsort(sha1_list
.entry
, nr
, sizeof(struct sha1_entry
*), ino_compare
);
195 for (i
= 0; i
< nr
; i
++) {
196 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
197 unsigned char *sha1
= entry
->sha1
;
199 sha1_list
.entry
[i
] = NULL
;
200 if (fsck_sha1(sha1
) < 0)
201 fprintf(stderr
, "bad sha1 entry '%s'\n", sha1_to_hex(sha1
));
207 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
209 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
213 memcpy(entry
->sha1
, sha1
, 20);
215 if (nr
== MAX_SHA1_ENTRIES
) {
219 sha1_list
.entry
[nr
] = entry
;
223 static int fsck_dir(int i
, char *path
)
225 DIR *dir
= opendir(path
);
229 return error("missing sha1 directory '%s'", path
);
232 while ((de
= readdir(dir
)) != NULL
) {
234 unsigned char sha1
[20];
235 int len
= strlen(de
->d_name
);
239 if (de
->d_name
[1] != '.')
242 if (de
->d_name
[0] != '.')
246 sprintf(name
, "%02x", i
);
247 memcpy(name
+2, de
->d_name
, len
+1);
248 if (get_sha1_hex(name
, sha1
) < 0)
250 add_sha1_list(sha1
, de
->d_ino
);
253 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
259 int main(int argc
, char **argv
)
264 for (i
= 1; i
< argc
; i
++) {
265 const char *arg
= argv
[i
];
267 if (!strcmp(arg
, "--unreachable")) {
268 show_unreachable
= 1;
271 if (!strcmp(arg
, "--tags")) {
275 if (!strcmp(arg
, "--root")) {
279 if (!strcmp(arg
, "--cache")) {
280 keep_cache_objects
= 1;
284 usage("fsck-cache [--tags] [[--unreachable] [--cache] <head-sha1>*]");
287 sha1_dir
= getenv(DB_ENVIRONMENT
) ? : DEFAULT_DB_ENVIRONMENT
;
288 for (i
= 0; i
< 256; i
++) {
289 static char dir
[4096];
290 sprintf(dir
, "%s/%02x", sha1_dir
, i
);
296 for (i
= 1; i
< argc
; i
++) {
297 const char *arg
= argv
[i
];
302 if (!get_sha1(arg
, head_sha1
)) {
303 struct object
*obj
= lookup_object(head_sha1
);
305 /* Error is printed by lookup_object(). */
310 mark_reachable(obj
, REACHABLE
);
314 error("expected sha1, got %s", arg
);
317 if (keep_cache_objects
) {
320 for (i
= 0; i
< active_nr
; i
++) {
321 struct blob
*blob
= lookup_blob(active_cache
[i
]->sha1
);
327 mark_reachable(obj
, REACHABLE
);
331 if (!heads
&& !keep_cache_objects
) {
332 if (show_unreachable
) {
333 fprintf(stderr
, "unable to do reachability without a head nor --cache\n");
334 show_unreachable
= 0;
337 fprintf(stderr
, "expect dangling commits - potential heads - due to lack of head information\n");
340 check_connectivity();