Don't add references to objects we couldn't find.
[alt-git.git] / fsck-cache.c
blob985adb68bb50d087895e2a2cb0b7b5d009f627f2
1 #include "cache.h"
3 #include <sys/types.h>
4 #include <dirent.h>
6 #include "commit.h"
7 #include "tree.h"
8 #include "blob.h"
10 #define REACHABLE 0x0001
12 static int show_unreachable = 0;
13 static unsigned char head_sha1[20];
15 static void check_connectivity(void)
17 int i;
19 /* Look up all the requirements, warn about missing objects.. */
20 for (i = 0; i < nr_objs; i++) {
21 struct object *obj = objs[i];
23 if (show_unreachable && !(obj->flags & REACHABLE)) {
24 printf("unreachable %s %s\n", obj->type, sha1_to_hex(obj->sha1));
25 continue;
28 if (!obj->parsed) {
29 printf("missing %s %s\n", obj->type,
30 sha1_to_hex(obj->sha1));
32 if (!obj->used) {
33 printf("dangling %s %s\n", obj->type,
34 sha1_to_hex(obj->sha1));
39 static int fsck_tree(unsigned char *sha1, void *data, unsigned long size)
41 struct tree *item = lookup_tree(sha1);
42 if (parse_tree(item))
43 return -1;
44 if (item->has_full_path) {
45 fprintf(stderr, "warning: fsck-cache: tree %s "
46 "has full pathnames in it\n", sha1_to_hex(sha1));
48 return 0;
51 static int fsck_commit(unsigned char *sha1, void *data, unsigned long size)
53 struct commit *commit = lookup_commit(sha1);
54 if (parse_commit(commit))
55 return -1;
56 if (!commit->tree)
57 return -1;
58 if (!commit->parents)
59 printf("root %s\n", sha1_to_hex(sha1));
60 return 0;
63 static int fsck_blob(unsigned char *sha1, void *data, unsigned long size)
65 struct blob *blob = lookup_blob(sha1);
66 blob->object.parsed = 1;
67 return 0;
70 static int fsck_entry(unsigned char *sha1, char *tag, void *data,
71 unsigned long size)
73 if (!strcmp(tag, "blob")) {
74 if (fsck_blob(sha1, data, size) < 0)
75 return -1;
76 } else if (!strcmp(tag, "tree")) {
77 if (fsck_tree(sha1, data, size) < 0)
78 return -1;
79 } else if (!strcmp(tag, "commit")) {
80 if (fsck_commit(sha1, data, size) < 0)
81 return -1;
82 } else
83 return -1;
84 return 0;
87 static int fsck_name(char *hex)
89 unsigned char sha1[20];
90 if (!get_sha1_hex(hex, sha1)) {
91 unsigned long mapsize;
92 void *map = map_sha1_file(sha1, &mapsize);
93 if (map) {
94 char type[100];
95 unsigned long size;
96 void *buffer = unpack_sha1_file(map, mapsize, type, &size);
97 if (!buffer)
98 return -1;
99 if (check_sha1_signature(sha1, buffer, size, type) < 0)
100 printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
101 munmap(map, mapsize);
102 if (!fsck_entry(sha1, type, buffer, size))
103 return 0;
106 return -1;
109 static int fsck_dir(int i, char *path)
111 DIR *dir = opendir(path);
112 struct dirent *de;
114 if (!dir) {
115 return error("missing sha1 directory '%s'", path);
118 while ((de = readdir(dir)) != NULL) {
119 char name[100];
120 int len = strlen(de->d_name);
122 switch (len) {
123 case 2:
124 if (de->d_name[1] != '.')
125 break;
126 case 1:
127 if (de->d_name[0] != '.')
128 break;
129 continue;
130 case 38:
131 sprintf(name, "%02x", i);
132 memcpy(name+2, de->d_name, len+1);
133 if (!fsck_name(name))
134 continue;
136 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
138 closedir(dir);
139 return 0;
142 int main(int argc, char **argv)
144 int i, heads;
145 char *sha1_dir;
147 sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
148 for (i = 0; i < 256; i++) {
149 static char dir[4096];
150 sprintf(dir, "%s/%02x", sha1_dir, i);
151 fsck_dir(i, dir);
154 heads = 0;
155 for (i = 1; i < argc; i++) {
156 if (!strcmp(argv[i], "--unreachable")) {
157 show_unreachable = 1;
158 continue;
160 if (!get_sha1_hex(argv[i], head_sha1)) {
161 struct object *obj = &lookup_commit(head_sha1)->object;
162 obj->used = 1;
163 mark_reachable(obj, REACHABLE);
164 heads++;
165 continue;
167 error("fsck-cache [[--unreachable] <head-sha1>*]");
170 if (!heads) {
171 if (show_unreachable) {
172 fprintf(stderr, "unable to do reachability without a head\n");
173 show_unreachable = 0;
175 fprintf(stderr, "expect dangling commits - potential heads - due to lack of head information\n");
178 check_connectivity();
179 return 0;