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",
29 obj->type, sha1_to_hex(obj->sha1));
33 for (refs = obj->refs; refs; refs = refs->next) {
34 if (refs->item->parsed)
36 printf("broken link from %7s %s\n",
37 obj->type, sha1_to_hex(obj->sha1));
38 printf(" to %7s %s\n",
39 refs->item->type, sha1_to_hex(refs->item->sha1));
42 if (show_unreachable && !(obj->flags & REACHABLE)) {
43 printf("unreachable %s %s\n",
44 obj->type, sha1_to_hex(obj->sha1));
49 printf("dangling %s %s\n", obj->type,
50 sha1_to_hex(obj->sha1));
56 * The entries in a tree are ordered in the _path_ order,
57 * which means that a directory entry is ordered by adding
58 * a slash to the end of it.
60 * So a directory called "a" is ordered _after_ a file
61 * called "a.c", because "a/" sorts after "a.c".
63 #define TREE_UNORDERED (-1)
64 #define TREE_HAS_DUPS (-2)
66 static int verify_ordered(struct tree_entry_list *a, struct tree_entry_list *b)
68 int len1 = strlen(a->name);
69 int len2 = strlen(b->name);
70 int len = len1 < len2 ? len1 : len2;
74 cmp = memcmp(a->name, b->name, len);
78 return TREE_UNORDERED;
81 * Ok, the first <len> characters are the same.
82 * Now we need to order the next one, but turn
83 * a '\0' into a '/' for a directory entry.
89 * git-write-tree used to write out a nonsense tree that has
90 * entries with the same name, one blob and one tree. Make
91 * sure we do not have duplicate entries.
94 if (!c1 && a->directory)
96 if (!c2 && b->directory)
98 return c1 < c2 ? 0 : TREE_UNORDERED;
101 static int fsck_tree(struct tree *item)
103 int has_full_path = 0;
104 struct tree_entry_list *entry, *last;
107 for (entry = item->entries; entry; entry = entry->next) {
108 if (strchr(entry->name, '/'))
111 switch (entry->mode) {
121 * This is nonstandard, but we had a few of these
122 * early on when we honored the full set of mode
128 printf("tree %s has entry %o %s\n",
129 sha1_to_hex(item->object.sha1),
130 entry->mode, entry->name);
134 switch (verify_ordered(last, entry)) {
136 fprintf(stderr, "tree %s not ordered\n",
137 sha1_to_hex(item->object.sha1));
140 fprintf(stderr, "tree %s has duplicate entries for '%s'\n",
141 sha1_to_hex(item->object.sha1),
153 fprintf(stderr, "warning: git-fsck-cache: tree %s "
154 "has full pathnames in it\n",
155 sha1_to_hex(item->object.sha1));
161 static int fsck_commit(struct commit *commit)
163 free(commit->buffer);
164 commit->buffer = NULL;
167 if (!commit->parents && show_root)
168 printf("root %s\n", sha1_to_hex(commit->object.sha1));
170 printf("bad commit date in %s\n",
171 sha1_to_hex(commit->object.sha1));
175 static int fsck_tag(struct tag *tag)
177 struct object *tagged = tag->tagged;
180 printf("bad object in tag %s\n", sha1_to_hex(tag->object.sha1));
186 printf("tagged %s %s", tagged->type, sha1_to_hex(tagged->sha1));
187 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
191 static int fsck_sha1(unsigned char *sha1)
193 struct object *obj = parse_object(sha1);
196 if (obj->type == blob_type)
198 if (obj->type == tree_type)
199 return fsck_tree((struct tree *) obj);
200 if (obj->type == commit_type)
201 return fsck_commit((struct commit *) obj);
202 if (obj->type == tag_type)
203 return fsck_tag((struct tag *) obj);
208 * This is the sorting chunk size: make it reasonably
209 * big so that we can sort well..
211 #define MAX_SHA1_ENTRIES (1024)
215 unsigned char sha1[20];
220 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
223 static int ino_compare(const void *_a, const void *_b)
225 const struct sha1_entry *a = _a, *b = _b;
226 unsigned long ino1 = a->ino, ino2 = b->ino;
227 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
230 static void fsck_sha1_list(void)
232 int i, nr = sha1_list.nr;
234 qsort(sha1_list.entry, nr, sizeof(struct sha1_entry *), ino_compare);
235 for (i = 0; i < nr; i++) {
236 struct sha1_entry *entry = sha1_list.entry[i];
237 unsigned char *sha1 = entry->sha1;
239 sha1_list.entry[i] = NULL;
240 if (fsck_sha1(sha1) < 0)
241 fprintf(stderr, "bad sha1 entry '%s'\n", sha1_to_hex(sha1));
247 static void add_sha1_list(unsigned char *sha1, unsigned long ino)
249 struct sha1_entry *entry = xmalloc(sizeof(*entry));
253 memcpy(entry->sha1, sha1, 20);
255 if (nr == MAX_SHA1_ENTRIES) {
259 sha1_list.entry[nr] = entry;
263 static int fsck_dir(int i, char *path)
265 DIR *dir = opendir(path);
269 return error("missing sha1 directory '%s'", path);
272 while ((de = readdir(dir)) != NULL) {
274 unsigned char sha1[20];
275 int len = strlen(de->d_name);
279 if (de->d_name[1] != '.')
282 if (de->d_name[0] != '.')
286 sprintf(name, "%02x", i);
287 memcpy(name+2, de->d_name, len+1);
288 if (get_sha1_hex(name, sha1) < 0)
290 add_sha1_list(sha1, de->d_ino);
293 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
299 static int read_sha1_reference(const char *path)
302 unsigned char sha1[20];
303 int fd = open(path, O_RDONLY), len;
309 len = read(fd, hexname, sizeof(hexname));
314 if (get_sha1_hex(hexname, sha1) < 0)
317 obj = lookup_object(sha1);
319 return error("%s: invalid sha1 pointer %.40s", path, hexname);
322 mark_reachable(obj, REACHABLE);
326 static int find_file_objects(const char *base, const char *name)
328 int baselen = strlen(base);
329 int namelen = strlen(name);
330 char *path = xmalloc(baselen + namelen + 2);
333 memcpy(path, base, baselen);
335 memcpy(path + baselen + 1, name, namelen+1);
336 if (stat(path, &st) < 0)
340 * Recurse into directories
342 if (S_ISDIR(st.st_mode)) {
344 DIR *dir = opendir(path);
347 while ((de = readdir(dir)) != NULL) {
348 if (de->d_name[0] == '.')
350 count += find_file_objects(path, de->d_name);
356 if (S_ISREG(st.st_mode))
357 return read_sha1_reference(path) == 0;
361 static void get_default_heads(void)
363 char *git_dir = gitenv(GIT_DIR_ENVIRONMENT) ? : DEFAULT_GIT_DIR_ENVIRONMENT;
364 int count = find_file_objects(git_dir, "refs");
366 die("No default references");
369 int main(int argc, char **argv)
374 for (i = 1; i < argc; i++) {
375 const char *arg = argv[i];
377 if (!strcmp(arg, "--unreachable")) {
378 show_unreachable = 1;
381 if (!strcmp(arg, "--tags")) {
385 if (!strcmp(arg, "--root")) {
389 if (!strcmp(arg, "--cache")) {
390 keep_cache_objects = 1;
394 usage("git-fsck-cache [--tags] [[--unreachable] [--cache] <head-sha1>*]");
397 sha1_dir = get_object_directory();
398 for (i = 0; i < 256; i++) {
399 static char dir[4096];
400 sprintf(dir, "%s/%02x", sha1_dir, i);
406 for (i = 1; i < argc; i++) {
407 const char *arg = argv[i];
412 if (!get_sha1(arg, head_sha1)) {
413 struct object *obj = lookup_object(head_sha1);
415 /* Error is printed by lookup_object(). */
420 mark_reachable(obj, REACHABLE);
424 error("expected sha1, got %s", arg);
428 * If we've not been given any explicit head information, do the
429 * default ones from .git/refs. We also consider the index file
430 * in this case (ie this implies --cache).
434 keep_cache_objects = 1;
437 if (keep_cache_objects) {
440 for (i = 0; i < active_nr; i++) {
441 struct blob *blob = lookup_blob(active_cache[i]->sha1);
447 mark_reachable(obj, REACHABLE);
451 check_connectivity();