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
));
57 * The entries in a tree are ordered in the _path_ order,
58 * which means that a directory entry is ordered by adding
59 * a slash to the end of it.
61 * So a directory called "a" is ordered _after_ a file
62 * called "a.c", because "a/" sorts after "a.c".
64 static int verify_ordered(struct tree_entry_list
*a
, struct tree_entry_list
*b
)
66 int len1
= strlen(a
->name
);
67 int len2
= strlen(b
->name
);
68 int len
= len1
< len2
? len1
: len2
;
72 cmp
= memcmp(a
->name
, b
->name
, len
);
79 * Ok, the first <len> characters are the same.
80 * Now we need to order the next one, but turn
81 * a '\0' into a '/' for a directory entry.
85 if (!c1
&& a
->directory
)
87 if (!c2
&& b
->directory
)
89 return c1
< c2
? 0 : -1;
92 static int fsck_tree(struct tree
*item
)
94 int has_full_path
= 0;
95 struct tree_entry_list
*entry
, *last
;
98 for (entry
= item
->entries
; entry
; entry
= entry
->next
) {
99 if (strchr(entry
->name
, '/'))
103 if (verify_ordered(last
, entry
) < 0) {
104 fprintf(stderr
, "tree %s not ordered\n",
105 sha1_to_hex(item
->object
.sha1
));
114 fprintf(stderr
, "warning: fsck-cache: tree %s "
115 "has full pathnames in it\n",
116 sha1_to_hex(item
->object
.sha1
));
122 static int fsck_commit(struct commit
*commit
)
126 if (!commit
->parents
&& show_root
)
127 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
129 printf("bad commit date in %s\n",
130 sha1_to_hex(commit
->object
.sha1
));
134 static int fsck_tag(struct tag
*tag
)
139 printf("tagged %s %s",
141 sha1_to_hex(tag
->tagged
->sha1
));
142 printf(" (%s) in %s\n",
143 tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
147 static int fsck_sha1(unsigned char *sha1
)
149 struct object
*obj
= parse_object(sha1
);
152 if (obj
->type
== blob_type
)
154 if (obj
->type
== tree_type
)
155 return fsck_tree((struct tree
*) obj
);
156 if (obj
->type
== commit_type
)
157 return fsck_commit((struct commit
*) obj
);
158 if (obj
->type
== tag_type
)
159 return fsck_tag((struct tag
*) obj
);
164 * This is the sorting chunk size: make it reasonably
165 * big so that we can sort well..
167 #define MAX_SHA1_ENTRIES (1024)
171 unsigned char sha1
[20];
176 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
179 static int ino_compare(const void *_a
, const void *_b
)
181 const struct sha1_entry
*a
= _a
, *b
= _b
;
182 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
183 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
186 static void fsck_sha1_list(void)
188 int i
, nr
= sha1_list
.nr
;
190 qsort(sha1_list
.entry
, nr
, sizeof(struct sha1_entry
*), ino_compare
);
191 for (i
= 0; i
< nr
; i
++) {
192 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
193 unsigned char *sha1
= entry
->sha1
;
195 sha1_list
.entry
[i
] = NULL
;
196 if (fsck_sha1(sha1
) < 0)
197 fprintf(stderr
, "bad sha1 entry '%s'\n", sha1_to_hex(sha1
));
203 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
205 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
209 memcpy(entry
->sha1
, sha1
, 20);
211 if (nr
== MAX_SHA1_ENTRIES
) {
215 sha1_list
.entry
[nr
] = entry
;
219 static int fsck_dir(int i
, char *path
)
221 DIR *dir
= opendir(path
);
225 return error("missing sha1 directory '%s'", path
);
228 while ((de
= readdir(dir
)) != NULL
) {
230 unsigned char sha1
[20];
231 int len
= strlen(de
->d_name
);
235 if (de
->d_name
[1] != '.')
238 if (de
->d_name
[0] != '.')
242 sprintf(name
, "%02x", i
);
243 memcpy(name
+2, de
->d_name
, len
+1);
244 if (get_sha1_hex(name
, sha1
) < 0)
246 add_sha1_list(sha1
, de
->d_ino
);
249 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
255 int main(int argc
, char **argv
)
260 for (i
= 1; i
< argc
; i
++) {
261 const char *arg
= argv
[i
];
263 if (!strcmp(arg
, "--unreachable")) {
264 show_unreachable
= 1;
267 if (!strcmp(arg
, "--tags")) {
271 if (!strcmp(arg
, "--root")) {
276 usage("fsck-cache [--tags] [[--unreachable] <head-sha1>*]");
279 sha1_dir
= getenv(DB_ENVIRONMENT
) ? : DEFAULT_DB_ENVIRONMENT
;
280 for (i
= 0; i
< 256; i
++) {
281 static char dir
[4096];
282 sprintf(dir
, "%s/%02x", sha1_dir
, i
);
288 for (i
= 1; i
< argc
; i
++) {
289 const char *arg
= argv
[i
];
294 if (!get_sha1(arg
, head_sha1
)) {
295 struct commit
*commit
= lookup_commit(head_sha1
);
298 /* Error is printed by lookup_commit(). */
302 obj
= &commit
->object
;
304 mark_reachable(obj
, REACHABLE
);
308 error("expected sha1, got %s", arg
);
312 if (show_unreachable
) {
313 fprintf(stderr
, "unable to do reachability without a head\n");
314 show_unreachable
= 0;
316 fprintf(stderr
, "expect dangling commits - potential heads - due to lack of head information\n");
319 check_connectivity();